blob: 352bab4c0076f638c572eb7071038142957d5ab0 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/load.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010010
11#include <stdbool.h>
12
Maksims Svecovs134b8f92022-03-04 15:14:09 +000013#include "hf/arch/init.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
J-Alvesa9c7cba2021-08-25 16:26:11 +010015#include "hf/arch/plat/ffa.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000016#include "hf/arch/vm.h"
17
Andrew Scull18c78fc2018-08-20 12:57:41 +010018#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010019#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010020#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/dlog.h"
Fuad Tabba50469e02020-06-30 15:14:28 +010022#include "hf/fdt_patch.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010023#include "hf/layout.h"
J-Alves77b6f4f2023-03-15 11:34:49 +000024#include "hf/manifest.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/memiter.h"
26#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010027#include "hf/plat/console.h"
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -050028#include "hf/plat/interrupts.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000029#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010030#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010031#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010032#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010033
Andrew Scull19503262018-09-20 14:48:39 +010034#include "vmapi/hf/call.h"
Manish Pandeyd34f8892020-06-19 17:41:07 +010035#include "vmapi/hf/ffa.h"
Andrew Scull19503262018-09-20 14:48:39 +010036
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010037/**
38 * Copies data to an unmapped location by mapping it for write, copying the
39 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000040 *
41 * The data is written so that it is available to all cores with the cache
42 * disabled. When switching to the partitions, the caching is initially disabled
43 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010044 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010045static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010046 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010047{
David Brazdil7a462ec2019-08-15 12:27:47 +010048 const void *from = memiter_base(from_it);
49 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010050 paddr_t to_end = pa_add(to, size);
51 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010052
Andrew Scull3c0a90a2019-07-01 11:55:53 +010053 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010054 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010055 return false;
56 }
57
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010058 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010059 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010060
Andrew Scull72b43c02019-09-18 13:53:45 +010061 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010062
63 return true;
64}
65
Fuad Tabba50469e02020-06-30 15:14:28 +010066/**
67 * Loads the secondary VM's kernel.
68 * Stores the kernel size in kernel_size (if kernel_size is not NULL).
69 * Returns false if it cannot load the kernel.
70 */
Andrew Scull72b43c02019-09-18 13:53:45 +010071static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
72 paddr_t end, const struct manifest_vm *manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +010073 const struct memiter *cpio, struct mpool *ppool,
74 size_t *kernel_size)
Andrew Scull72b43c02019-09-18 13:53:45 +010075{
Andrew Scull72b43c02019-09-18 13:53:45 +010076 struct memiter kernel;
Fuad Tabba50469e02020-06-30 15:14:28 +010077 size_t size;
Andrew Scull72b43c02019-09-18 13:53:45 +010078
David Brazdil136f2942019-09-23 14:11:03 +010079 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000080 dlog_error("Could not find kernel file \"%s\".\n",
81 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010082 return false;
83 }
84
Fuad Tabba50469e02020-06-30 15:14:28 +010085 size = memiter_size(&kernel);
86 if (pa_difference(begin, end) < size) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000087 dlog_error("Kernel is larger than available memory.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010088 return false;
89 }
90
91 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000092 dlog_error("Unable to copy kernel.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010093 return false;
94 }
95
Fuad Tabba50469e02020-06-30 15:14:28 +010096 if (kernel_size) {
97 *kernel_size = size;
98 }
99
Andrew Scull72b43c02019-09-18 13:53:45 +0100100 return true;
101}
102
Manish Pandeyd34f8892020-06-19 17:41:07 +0100103/*
104 * Link RX/TX buffers provided in partition manifest to mailbox
105 */
106static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked,
107 struct vm_locked vm_locked, struct rx_tx rxtx,
108 struct mpool *ppool)
109{
110 struct ffa_value ret;
111 ipaddr_t send;
112 ipaddr_t recv;
113 uint32_t page_count;
114
115 send = ipa_init(rxtx.tx_buffer->base_address);
116 recv = ipa_init(rxtx.rx_buffer->base_address);
117 page_count = rxtx.tx_buffer->page_count;
118
119 ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv,
120 page_count, ppool);
121 if (ret.func != FFA_SUCCESS_32) {
122 return false;
123 }
124
125 dlog_verbose(" mailbox: send = %#x, recv = %#x\n",
126 vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv);
127
128 return true;
129}
130
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700131static void infer_interrupt(struct interrupt_info interrupt,
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500132 struct interrupt_descriptor *int_desc)
133{
134 uint32_t attr = interrupt.attributes;
135
136 interrupt_desc_set_id(int_desc, interrupt.id);
137 interrupt_desc_set_priority(int_desc,
138 (attr >> INT_DESC_PRIORITY_SHIFT) & 0xff);
139
140 /* Refer to the comments in interrupt_descriptor struct definition. */
141 interrupt_desc_set_type_config_sec_state(
142 int_desc,
143 (((attr >> INT_DESC_TYPE_SHIFT) & 0x3) << 2) |
144 (((attr >> INT_DESC_CONFIG_SHIFT) & 0x1) << 1) |
145 ((attr >> INT_DESC_SEC_STATE_SHIFT) & 0x1));
146
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700147 if (interrupt.mpidr_valid) {
148 interrupt_desc_set_mpidr(int_desc, interrupt.mpidr);
149 } else {
150 interrupt_desc_set_mpidr_invalid(int_desc);
151 }
152
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500153 interrupt_desc_set_valid(int_desc, true);
Madhukar Pappireddy938faaf2023-07-31 17:56:55 -0500154 interrupt_desc_set_enabled(int_desc, true);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500155}
156
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100157/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100158 * Performs VM loading activities that are common between the primary and
159 * secondaries.
160 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100161static bool load_common(struct mm_stage1_locked stage1_locked,
162 struct vm_locked vm_locked,
163 const struct manifest_vm *manifest_vm,
164 struct mpool *ppool)
Andrew Scullae9962e2019-10-03 16:51:16 +0100165{
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500166 struct device_region dev_region;
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700167 struct interrupt_info interrupt;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500168 uint32_t k = 0;
169
Manish Pandeyd34f8892020-06-19 17:41:07 +0100170 vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist;
Olivier Depreza15f2352022-09-26 09:17:24 +0200171 vm_locked.vm->power_management =
172 manifest_vm->partition.power_management;
Andrew Scullae9962e2019-10-03 16:51:16 +0100173
Kathleen Capella422b10b2023-06-30 18:28:27 -0400174 /* Populate array of UUIDs. */
175 for (uint16_t i = 0; i < PARTITION_MAX_UUIDS; i++) {
176 struct ffa_uuid current_uuid = manifest_vm->partition.uuids[i];
177
178 if (ffa_uuid_is_null(&current_uuid)) {
179 break;
180 }
181
182 vm_locked.vm->uuids[i] = current_uuid;
183 }
184
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500185 /* Populate the interrupt descriptor for current VM. */
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700186 for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) {
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500187 dev_region = manifest_vm->partition.dev_regions[i];
188
189 CHECK(dev_region.interrupt_count <=
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700190 PARTITION_MAX_INTERRUPTS_PER_DEVICE);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500191
192 for (uint8_t j = 0; j < dev_region.interrupt_count; j++) {
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700193 struct interrupt_descriptor int_desc = {0};
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500194
195 interrupt = dev_region.interrupts[j];
196 infer_interrupt(interrupt, &int_desc);
197 vm_locked.vm->interrupt_desc[k] = int_desc;
Madhukar Pappireddy938faaf2023-07-31 17:56:55 -0500198 assert(int_desc.enabled);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500199
Madhukar Pappireddy72454a12021-08-03 12:21:46 -0500200 /*
201 * Configure the physical interrupts allocated for this
202 * VM in its partition manifest.
203 */
204 plat_interrupts_configure_interrupt(int_desc);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500205 k++;
206 CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS);
207 }
208 }
209 dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k);
210
Manish Pandeyd34f8892020-06-19 17:41:07 +0100211 if (manifest_vm->is_ffa_partition) {
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +0000212 vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100213 /* Link rxtx buffers to mailbox */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700214 if (manifest_vm->partition.rxtx.available) {
Manish Pandeyd34f8892020-06-19 17:41:07 +0100215 if (!link_rxtx_to_mailbox(stage1_locked, vm_locked,
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700216 manifest_vm->partition.rxtx,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100217 ppool)) {
218 dlog_error(
219 "Unable to Link RX/TX buffer with "
220 "mailbox.\n");
221 return false;
222 }
223 }
J-Alvesb37fd082020-10-22 12:29:21 +0100224
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100225 vm_locked.vm->messaging_method =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700226 manifest_vm->partition.messaging_method;
Manish Pandeyf3be6392020-09-24 17:26:09 +0100227
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500228 vm_locked.vm->ns_interrupts_action =
229 manifest_vm->partition.ns_interrupts_action;
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100230
Madhukar Pappireddy5c04a382022-12-28 11:29:26 -0600231 vm_locked.vm->other_s_interrupts_action =
232 manifest_vm->partition.other_s_interrupts_action;
233
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500234 vm_locked.vm->me_signal_virq =
235 manifest_vm->partition.me_signal_virq;
236
J-Alvesa4730db2021-11-02 10:31:01 +0000237 vm_locked.vm->notifications.enabled =
238 manifest_vm->partition.notification_support;
239
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700240 vm_locked.vm->boot_order = manifest_vm->partition.boot_order;
241
J-Alves7d38f7b2022-04-13 13:22:30 +0100242 vm_locked.vm->boot_info.gp_register_num =
243 manifest_vm->partition.gp_register_num;
244
245 if (manifest_vm->partition.boot_info) {
246 /*
247 * If the partition expects the boot information blob
248 * per the ff-a v1.1 boot protocol, then its address
249 * shall match the partition's load address.
250 */
251 vm_locked.vm->boot_info.blob_addr =
252 ipa_init(manifest_vm->partition.load_addr);
253 }
254
J-Alvesb37fd082020-10-22 12:29:21 +0100255 /* Updating boot list according to boot_order */
Olivier Deprez181074b2023-02-02 14:53:23 +0100256 vcpu_update_boot(vm_get_vcpu(vm_locked.vm, 0));
J-Alvesa0f317d2021-06-09 13:31:59 +0100257
J-Alves09ff9d82021-11-02 11:55:20 +0000258 if (vm_locked_are_notifications_enabled(vm_locked) &&
J-Alvesa4730db2021-11-02 10:31:01 +0000259 !plat_ffa_notifications_bitmap_create_call(
260 vm_locked.vm->id, vm_locked.vm->vcpu_count)) {
261 return false;
J-Alvesa9c7cba2021-08-25 16:26:11 +0100262 }
Manish Pandeyd34f8892020-06-19 17:41:07 +0100263 }
J-Alvesb37fd082020-10-22 12:29:21 +0100264
Fuad Tabba56970712020-01-10 11:20:09 +0000265 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100266 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000267
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500268 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
269 ppool)) {
270 dlog_error("Unable to attach upstream peripheral device\n");
271 return false;
272 }
273
Andrew Scullae9962e2019-10-03 16:51:16 +0100274 return true;
275}
276
277/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100278 * Loads the primary VM.
279 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100280static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100281 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100282 const struct memiter *cpio,
283 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100284{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100285 paddr_t primary_begin;
286 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100287 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000288 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100289 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100290 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000291 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100292
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700293 if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) {
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700294 primary_begin = pa_init(manifest_vm->partition.load_addr);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100295 primary_entry = ipa_add(ipa_from_pa(primary_begin),
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700296 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100297 } else {
298 primary_begin =
299 (manifest_vm->primary.boot_address ==
300 MANIFEST_INVALID_ADDRESS)
301 ? layout_primary_begin()
302 : pa_init(manifest_vm->primary.boot_address);
303 primary_entry = ipa_from_pa(primary_begin);
304 }
305
David Brazdil080ee312020-02-25 15:30:30 -0800306 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100307
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800308 /* Primary VM must be a VM */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700309 CHECK(manifest_vm->partition.run_time_el == EL1);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800310
Olivier Deprez62d99e32020-01-09 15:58:07 +0100311 /*
312 * Load the kernel if a filename is specified in the VM manifest.
313 * For an FF-A partition, kernel_filename is undefined indicating
314 * the partition package has already been loaded prior to Hafnium
315 * booting.
316 */
317 if (!string_is_empty(&manifest_vm->kernel_filename)) {
318 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100319 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100320 dlog_error("Unable to load primary kernel.\n");
321 return false;
322 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100323 }
324
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600325 if (!vm_init_next(MAX_CPUS, ppool, &vm, false,
326 manifest_vm->partition.dma_device_count)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000327 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100328 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100329 }
330
David Brazdile6f83222019-09-23 14:47:37 +0100331 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000332 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100333 return false;
334 }
335
Andrew Scull3c257452019-11-26 13:32:50 +0000336 vm_locked = vm_lock(vm);
337
Andrew Scull48929fd2020-01-28 10:39:10 +0000338 if (params->device_mem_ranges_count == 0) {
339 /*
340 * Map 1TB of address space as device memory to, most likely,
341 * make all devices available to the primary VM.
342 *
343 * TODO: remove this once all targets provide valid ranges.
344 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800345 dlog_warning(
346 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000347
348 if (!vm_identity_map(
349 vm_locked, pa_init(0),
350 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
351 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
352 dlog_error(
353 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800354 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000355 ret = false;
356 goto out;
357 }
David Brazdile6f83222019-09-23 14:47:37 +0100358 }
359
Andrew Scullb5f49e02019-10-02 13:20:47 +0100360 /* Map normal memory as such to permit caching, execution, etc. */
361 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000362 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
363 params->mem_ranges[i].end,
364 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
365 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000366 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800367 "Unable to initialise memory for primary "
368 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000369 ret = false;
370 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100371 }
372 }
373
Andrew Scull48929fd2020-01-28 10:39:10 +0000374 /* Map device memory as such to prevent execution, speculation etc. */
375 for (i = 0; i < params->device_mem_ranges_count; ++i) {
376 if (!vm_identity_map(
377 vm_locked, params->device_mem_ranges[i].begin,
378 params->device_mem_ranges[i].end,
379 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
380 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800381 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000382 ret = false;
383 goto out;
384 }
385 }
386
Manish Pandeyd34f8892020-06-19 17:41:07 +0100387 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
388 ret = false;
389 goto out;
390 }
391
Andrew Scull3c257452019-11-26 13:32:50 +0000392 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800393 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000394 ret = false;
395 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100396 }
397
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000398 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800399 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000400 ret = false;
401 goto out;
402 }
403
Andrew Walbran7586e042020-02-18 18:19:26 +0000404 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
405 vm->vcpu_count, pa_addr(primary_begin));
406
Olivier Deprez181074b2023-02-02 14:53:23 +0100407 /* Mark the first VM vCPU to be the first booted vCPU. */
408 vcpu_update_boot(vm_get_vcpu(vm, 0));
Olivier Deprezb9adff42021-02-01 12:14:05 +0100409
David Brazdile6f83222019-09-23 14:47:37 +0100410 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100411 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100412 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000413 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100414
Andrew Scull3c257452019-11-26 13:32:50 +0000415out:
416 vm_unlock(&vm_locked);
417
418 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100419}
420
Fuad Tabba50469e02020-06-30 15:14:28 +0100421/**
422 * Loads the secondary VM's FDT.
423 * Stores the total allocated size for the FDT in fdt_allocated_size (if
424 * fdt_allocated_size is not NULL). The allocated size includes additional space
425 * for potential patching.
426 */
427static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
428 paddr_t end, size_t fdt_max_size,
429 const struct manifest_vm *manifest_vm,
430 const struct memiter *cpio, struct mpool *ppool,
431 paddr_t *fdt_addr, size_t *fdt_allocated_size)
432{
433 struct memiter fdt;
434 size_t allocated_size;
435
436 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
437
438 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
439 dlog_error("Cannot open the secondary VM's FDT.\n");
440 return false;
441 }
442
443 /*
Olivier Deprez701e8bf2022-04-06 18:45:18 +0200444 * Ensure the FDT has one additional page at the end for patching,
Fuad Tabba50469e02020-06-30 15:14:28 +0100445 * and align it to the page boundary.
446 */
447 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
448
449 if (allocated_size > fdt_max_size) {
450 dlog_error(
451 "FDT allocated space (%u) is more than the specified "
452 "maximum to use (%u).\n",
453 allocated_size, fdt_max_size);
454 return false;
455 }
456
457 /* Load the FDT to the end of the VM's allocated memory space. */
458 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
459
460 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
461 allocated_size, pa_addr(*fdt_addr));
462
463 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
464 dlog_error("Unable to copy FDT.\n");
465 return false;
466 }
467
468 if (fdt_allocated_size) {
469 *fdt_allocated_size = allocated_size;
470 }
471
472 return true;
473}
474
Olivier Deprez035fa152022-03-14 11:19:10 +0100475/**
476 * Convert the manifest memory region attributes to mode consumed by mm layer.
477 */
478static uint32_t memory_region_attributes_to_mode(uint32_t attributes)
479{
480 uint32_t mode = 0U;
481
482 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
483 mode |= MM_MODE_R;
484 }
485
486 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
487 mode |= MM_MODE_W;
488 }
489
490 if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) {
491 mode |= MM_MODE_X;
492 }
493
494 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) ||
495 (mode == (MM_MODE_R | MM_MODE_X)));
496
497 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
498 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
499 }
500
501 return mode;
502}
503
504/**
505 * Convert the manifest device region attributes to mode consumed by mm layer.
506 */
507static uint32_t device_region_attributes_to_mode(uint32_t attributes)
508{
509 uint32_t mode = 0U;
510
511 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
512 mode |= MM_MODE_R;
513 }
514
515 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
516 mode |= MM_MODE_W;
517 }
518
519 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R));
520
521 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
522 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
523 }
524
525 return mode | MM_MODE_D;
526}
527
Daniel Boulbye6df3452022-07-14 18:14:26 +0100528static bool ffa_map_memory_regions(const struct manifest_vm *manifest_vm,
529 const struct vm_locked vm_locked,
530 const struct vm_locked primary_vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000531 bool is_el0_partition, struct mpool *ppool)
Daniel Boulbye6df3452022-07-14 18:14:26 +0100532{
533#if LOG_LEVEL >= LOG_LEVEL_WARNING
534 const char *error_string = " region security state ignored for ";
535#endif
536 int j = 0;
537 paddr_t region_begin;
538 paddr_t region_end;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100539 size_t size;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100540 uint32_t map_mode;
541 uint32_t attributes;
542
543 /* Map memory-regions */
544 while (j < manifest_vm->partition.mem_region_count) {
545 size = manifest_vm->partition.mem_regions[j].page_count *
546 PAGE_SIZE;
547 /*
J-Alvesd8a1d362023-03-08 11:15:28 +0000548 * Identity map memory region for both case,
549 * VA(S-EL0) or IPA(S-EL1).
Daniel Boulbye6df3452022-07-14 18:14:26 +0100550 */
J-Alvesd8a1d362023-03-08 11:15:28 +0000551 region_begin = pa_init(
552 manifest_vm->partition.mem_regions[j].base_address);
553 region_end = pa_add(region_begin, size);
Daniel Boulbye6df3452022-07-14 18:14:26 +0100554
555 attributes = manifest_vm->partition.mem_regions[j].attributes;
556 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100557 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100558 dlog_warning("Memory%sVMs\n", error_string);
559 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100560 }
561 }
562
563 map_mode = memory_region_attributes_to_mode(attributes);
564
565 if (is_el0_partition) {
566 map_mode |= MM_MODE_USER | MM_MODE_NG;
567 }
568
569 if (!vm_identity_map(vm_locked, region_begin, region_end,
570 map_mode, ppool, NULL)) {
571 dlog_error(
572 "Unable to map secondary VM "
573 "memory-region.\n");
574 return false;
575 }
576
577 /* Deny the primary VM access to this memory */
578 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
579 ppool)) {
580 dlog_error(
581 "Unable to unmap secondary VM memory-"
582 "region from primary VM.\n");
583 return false;
584 }
585
586 dlog_verbose("Memory region %#x - %#x allocated.\n",
587 region_begin, region_end);
588
589 j++;
590 }
591
592 /* Map device-regions */
593 j = 0;
594 while (j < manifest_vm->partition.dev_region_count) {
595 region_begin = pa_init(
596 manifest_vm->partition.dev_regions[j].base_address);
597 size = manifest_vm->partition.dev_regions[j].page_count *
598 PAGE_SIZE;
599 region_end = pa_add(region_begin, size);
600
601 attributes = manifest_vm->partition.dev_regions[j].attributes;
602 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100603 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100604 dlog_warning("Device%sVMs\n", error_string);
605 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100606 }
607 }
608
609 map_mode = device_region_attributes_to_mode(attributes);
610 if (is_el0_partition) {
611 map_mode |= MM_MODE_USER | MM_MODE_NG;
612 }
613
614 if (!vm_identity_map(vm_locked, region_begin, region_end,
615 map_mode, ppool, NULL)) {
616 dlog_error(
617 "Unable to map secondary VM "
618 "device-region.\n");
619 return false;
620 }
621 /* Deny primary VM access to this region */
622 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
623 ppool)) {
624 dlog_error(
625 "Unable to unmap secondary VM device-"
626 "region from primary VM.\n");
627 return false;
628 }
629 j++;
630 }
631 return true;
632}
633
Andrew Scull72b43c02019-09-18 13:53:45 +0100634/*
635 * Loads a secondary VM.
636 */
637static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100638 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100639 paddr_t mem_begin, paddr_t mem_end,
640 const struct manifest_vm *manifest_vm,
J-Alves77b6f4f2023-03-15 11:34:49 +0000641 const struct boot_params *boot_params,
Andrew Scull72b43c02019-09-18 13:53:45 +0100642 const struct memiter *cpio, struct mpool *ppool)
643{
644 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000645 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100646 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100647 struct vcpu *vcpu;
648 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000649 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100650 paddr_t fdt_addr;
651 bool has_fdt;
652 size_t kernel_size = 0;
653 const size_t mem_size = pa_difference(mem_begin, mem_end);
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800654 uint32_t map_mode;
Daniel Boulby874d5432023-04-27 12:40:24 +0100655 bool is_el0_partition = manifest_vm->partition.run_time_el == S_EL0 ||
656 manifest_vm->partition.run_time_el == EL0;
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100657 size_t n;
Andrew Scull72b43c02019-09-18 13:53:45 +0100658
Olivier Deprez62d99e32020-01-09 15:58:07 +0100659 /*
660 * Load the kernel if a filename is specified in the VM manifest.
661 * For an FF-A partition, kernel_filename is undefined indicating
662 * the partition package has already been loaded prior to Hafnium
663 * booting.
664 */
665 if (!string_is_empty(&manifest_vm->kernel_filename)) {
666 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100667 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100668 dlog_error("Unable to load kernel.\n");
669 return false;
670 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100671 }
672
Fuad Tabba50469e02020-06-30 15:14:28 +0100673 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
674 if (has_fdt) {
675 /*
676 * Ensure that the FDT does not overwrite the kernel or overlap
677 * its page, for the FDT to start at a page boundary.
678 */
679 const size_t fdt_max_size =
680 mem_size - align_up(kernel_size, PAGE_SIZE);
681
682 size_t fdt_allocated_size;
683
684 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
685 manifest_vm, cpio, ppool, &fdt_addr,
686 &fdt_allocated_size)) {
687 dlog_error("Unable to load FDT.\n");
688 return false;
689 }
690
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700691 if (manifest_vm->is_ffa_partition) {
692 plat_ffa_parse_partition_manifest(
693 stage1_locked, fdt_addr, fdt_allocated_size,
J-Alves77b6f4f2023-03-15 11:34:49 +0000694 manifest_vm, boot_params, ppool);
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700695 }
696
Fuad Tabba50469e02020-06-30 15:14:28 +0100697 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
698 mem_begin, mem_end, ppool)) {
699 dlog_error("Unable to patch FDT.\n");
700 return false;
701 }
702 }
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800703 /*
704 * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the
705 * FF-A 1.0 spec.
706 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100707 CHECK(!is_el0_partition || manifest_vm->secondary.vcpu_count == 1);
Fuad Tabba50469e02020-06-30 15:14:28 +0100708
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800709 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm,
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600710 is_el0_partition,
711 manifest_vm->partition.dma_device_count)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000712 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100713 return false;
714 }
715
Andrew Scull3c257452019-11-26 13:32:50 +0000716 vm_locked = vm_lock(vm);
717
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800718 /*
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700719 * Grant the VM access to the memory. For VM's we mark all memory in
720 * stage-2 tables as RWX and the VM can control permissions using
721 * stage-1 translations. For S-EL0 partitions, hafnium maps the entire
722 * region of memory for the partition as RX. The partition is then
723 * expected to perform its owns relocations and call the FFA_MEM_PERM_*
724 * API's to change permissions on its image layout.
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800725 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100726 if (is_el0_partition) {
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700727 map_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG;
728 } else {
729 map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800730 }
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700731
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800732 if (!vm_identity_map(vm_locked, mem_begin, mem_end, map_mode, ppool,
Andrew Scull3c257452019-11-26 13:32:50 +0000733 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000734 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000735 ret = false;
736 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100737 }
738
Olivier Deprez62d99e32020-01-09 15:58:07 +0100739 if (manifest_vm->is_ffa_partition) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100740 if (!ffa_map_memory_regions(manifest_vm, vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000741 primary_vm_locked, is_el0_partition,
742 ppool)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100743 ret = false;
744 goto out;
Manish Pandey2145c212020-05-01 16:04:22 +0100745 }
746
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700747 secondary_entry = ipa_add(secondary_entry,
748 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100749 }
750
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800751 /*
752 * Map hypervisor into the VM's page table. The hypervisor pages will
753 * not be accessible from EL0 since it will not be marked for user
754 * access.
755 * TODO: Map only the exception vectors and data that exception vectors
756 * require and not the entire hypervisor. This helps with speculative
757 * side-channel attacks.
758 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100759 if (is_el0_partition) {
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800760 CHECK(vm_identity_map(vm_locked, layout_text_begin(),
761 layout_text_end(), MM_MODE_X, ppool,
762 NULL));
763
764 CHECK(vm_identity_map(vm_locked, layout_rodata_begin(),
765 layout_rodata_end(), MM_MODE_R, ppool,
766 NULL));
767
768 CHECK(vm_identity_map(vm_locked, layout_data_begin(),
769 layout_data_end(), MM_MODE_R | MM_MODE_W,
770 ppool, NULL));
Maksims Svecovs134b8f92022-03-04 15:14:09 +0000771
772 CHECK(arch_stack_mm_init(mm_lock_ptable_unsafe(&vm->ptable),
773 ppool));
774
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800775 plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool);
776 }
777
Manish Pandeyd34f8892020-06-19 17:41:07 +0100778 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
779 ret = false;
780 goto out;
781 }
782
Manish Pandey2145c212020-05-01 16:04:22 +0100783 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
784 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
785
Andrew Scull72b43c02019-09-18 13:53:45 +0100786 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100787
Max Shvetsov40108e72020-08-27 12:39:50 +0100788 vcpu_locked = vcpu_lock(vcpu);
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500789
Fuad Tabba50469e02020-06-30 15:14:28 +0100790 if (has_fdt) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100791 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
Fuad Tabba50469e02020-06-30 15:14:28 +0100792 pa_addr(fdt_addr));
793 } else {
794 /*
795 * Without an FDT, secondary VMs expect the memory size to be
796 * passed in register x0, which is what
797 * vcpu_secondary_reset_and_start does in this case.
798 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100799 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
800 mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100801 }
802
Max Shvetsov40108e72020-08-27 12:39:50 +0100803 vcpu_unlock(&vcpu_locked);
804
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100805 /*
806 * For all vCPUs,
807 * in a VM: enable the notification pending virtual interrupt if
808 * requested in the manifest.
809 * in a SP: enable the NPI and managed exit virtual interrupts if
810 * requested in the manifest. For a S-EL0 partition, enable
811 * the virtual interrupts IDs matching the secure physical
812 * interrupt IDs declared in device regions.
813 */
814 for (n = 0; n < manifest_vm->secondary.vcpu_count; n++) {
815 vcpu = vm_get_vcpu(vm, n);
816 vcpu_locked = vcpu_lock(vcpu);
817 plat_ffa_enable_virtual_interrupts(vcpu_locked, vm_locked);
818 vcpu_unlock(&vcpu_locked);
819 }
820
Andrew Scull3c257452019-11-26 13:32:50 +0000821 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100822
Andrew Scull3c257452019-11-26 13:32:50 +0000823out:
824 vm_unlock(&vm_locked);
825
826 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100827}
828
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100829/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100830 * Try to find a memory range of the given size within the given ranges, and
831 * remove it from them. Return true on success, or false if no large enough
832 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100833 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900834static bool carve_out_mem_range(struct mem_range *mem_ranges,
835 size_t mem_ranges_count, uint64_t size_to_find,
836 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100837{
838 size_t i;
839
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000840 /*
841 * TODO(b/116191358): Consider being cleverer about how we pack VMs
842 * together, with a non-greedy algorithm.
843 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100844 for (i = 0; i < mem_ranges_count; ++i) {
845 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100846 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100847 /*
848 * This range is big enough, take some of it from the
849 * end and reduce its size accordingly.
850 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100851 *found_end = mem_ranges[i].end;
852 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
853 size_to_find);
854 mem_ranges[i].end = *found_begin;
855 return true;
856 }
857 }
858 return false;
859}
860
861/**
862 * Given arrays of memory ranges before and after memory was removed for
863 * secondary VMs, add the difference to the reserved ranges of the given update.
864 * Return true on success, or false if there would be more than MAX_MEM_RANGES
865 * reserved ranges after adding the new ones.
866 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
867 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900868static bool update_reserved_ranges(struct boot_params_update *update,
869 const struct mem_range *before,
870 const struct mem_range *after,
871 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100872{
873 size_t i;
874
875 for (i = 0; i < mem_ranges_count; ++i) {
876 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
877 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000878 dlog_error(
879 "Too many reserved ranges after "
880 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100881 return false;
882 }
883 update->reserved_ranges[update->reserved_ranges_count]
884 .begin = before[i].begin;
885 update->reserved_ranges[update->reserved_ranges_count]
886 .end = after[i].begin;
887 update->reserved_ranges_count++;
888 }
889 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
890 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000891 dlog_error(
892 "Too many reserved ranges after "
893 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100894 return false;
895 }
896 update->reserved_ranges[update->reserved_ranges_count]
897 .begin = after[i].end;
898 update->reserved_ranges[update->reserved_ranges_count]
899 .end = before[i].end;
900 update->reserved_ranges_count++;
901 }
902 }
903
904 return true;
905}
906
Olivier Deprez05046922023-03-09 15:48:40 +0100907static bool init_other_world_vm(const struct boot_params *params,
908 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100909{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200910 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100911 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100912
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100913 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200914 * Initialise the dummy VM which represents the opposite world:
915 * -TrustZone (or the SPMC) when running the Hypervisor
916 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100917 */
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600918 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false, 0);
Olivier Deprez96a2a262020-06-11 17:21:38 +0200919 CHECK(other_world_vm != NULL);
920
921 for (i = 0; i < MAX_CPUS; i++) {
922 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
923 struct cpu *cpu = cpu_find_index(i);
924
925 vcpu->cpu = cpu;
926 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100927
Olivier Deprez05046922023-03-09 15:48:40 +0100928 return arch_other_world_vm_init(other_world_vm, params, ppool);
Olivier Deprez112d2b52020-09-30 07:39:23 +0200929}
930
931/*
932 * Loads alls VMs from the manifest.
933 */
934bool load_vms(struct mm_stage1_locked stage1_locked,
935 const struct manifest *manifest, const struct memiter *cpio,
936 const struct boot_params *params,
937 struct boot_params_update *update, struct mpool *ppool)
938{
939 struct vm *primary;
940 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
941 struct vm_locked primary_vm_locked;
942 size_t i;
943 bool success = true;
944
Andrew Walbranf8716782020-10-29 17:07:07 +0000945 /**
946 * Only try to load the primary VM if it is supposed to be in this
947 * world.
948 */
949 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
950 if (!load_primary(stage1_locked,
951 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
952 params, ppool)) {
953 dlog_error("Unable to load primary VM.\n");
954 return false;
955 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200956 }
957
Olivier Deprez05046922023-03-09 15:48:40 +0100958 if (!init_other_world_vm(params, ppool)) {
Olivier Deprez112d2b52020-09-30 07:39:23 +0200959 return false;
960 }
961
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100962 static_assert(
963 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
964 "mem_range arrays must be the same size for memcpy.");
965 static_assert(sizeof(mem_ranges_available) < 500,
966 "This will use too much stack, either make "
967 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100968 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
969 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100970
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100971 /* Round the last addresses down to the page size. */
972 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000973 mem_ranges_available[i].end = pa_init(align_down(
974 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100975 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100976
Andrew Scull3c257452019-11-26 13:32:50 +0000977 primary = vm_find(HF_PRIMARY_VM_ID);
978 primary_vm_locked = vm_lock(primary);
979
David Brazdil0251b942019-09-10 15:59:50 +0100980 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100981 const struct manifest_vm *manifest_vm = &manifest->vm[i];
J-Alves19e20cf2023-08-02 12:48:55 +0100982 ffa_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100983 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100984 paddr_t secondary_mem_begin;
985 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100986
David Brazdil7a462ec2019-08-15 12:27:47 +0100987 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100988 continue;
989 }
990
Olivier Deprez2a8ee342020-08-03 15:10:44 +0200991 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000992 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100993
David Brazdil7a462ec2019-08-15 12:27:47 +0100994 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100995
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700996 if (manifest_vm->is_ffa_partition &&
997 !manifest->vm[i].is_hyp_loaded) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100998 secondary_mem_begin =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700999 pa_init(manifest_vm->partition.load_addr);
1000 secondary_mem_end = pa_init(
1001 manifest_vm->partition.load_addr + mem_size);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001002 } else if (!carve_out_mem_range(mem_ranges_available,
1003 params->mem_ranges_count,
1004 mem_size, &secondary_mem_begin,
1005 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001006 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001007 continue;
1008 }
Andrew Scull80871322018-08-06 12:04:09 +01001009
Manish Pandey2145c212020-05-01 16:04:22 +01001010 if (!load_secondary(stage1_locked, primary_vm_locked,
1011 secondary_mem_begin, secondary_mem_end,
J-Alves77b6f4f2023-03-15 11:34:49 +00001012 manifest_vm, params, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001013 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001014 continue;
1015 }
1016
1017 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +00001018 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
1019 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001020 dlog_error(
1021 "Unable to unmap secondary VM from primary "
1022 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +00001023 success = false;
1024 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001025 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001026 }
1027
Andrew Scull3c257452019-11-26 13:32:50 +00001028 vm_unlock(&primary_vm_locked);
1029
1030 if (!success) {
1031 return false;
1032 }
1033
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001034 /*
1035 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001036 * difference between the available ranges from the original params and
1037 * the updated mem_ranges_available. We assume that the number and order
1038 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001039 * above only make them smaller.
1040 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001041 return update_reserved_ranges(update, params->mem_ranges,
1042 mem_ranges_available,
1043 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001044}