blob: c3ae89878214a6bfb68211252bccdb785c4dc294 [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"
Fuad Tabba77a4b012019-11-15 12:13:08 +000015#include "hf/arch/vm.h"
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010018#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010019#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010020#include "hf/dlog.h"
Fuad Tabba50469e02020-06-30 15:14:28 +010021#include "hf/fdt_patch.h"
Karl Meakin902af082024-11-28 14:58:38 +000022#include "hf/ffa/interrupts.h"
23#include "hf/ffa/notifications.h"
24#include "hf/ffa/setup_and_discovery.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010025#include "hf/layout.h"
J-Alves77b6f4f2023-03-15 11:34:49 +000026#include "hf/manifest.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/memiter.h"
28#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010029#include "hf/plat/console.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000030#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010031#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010032#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034
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
Karl Meakine8937d92024-03-19 16:04:25 +0000125 dlog_verbose(" mailbox: send = %p, recv = %p\n",
Manish Pandeyd34f8892020-06-19 17:41:07 +0100126 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
Daniel Boulby18485942024-10-14 16:23:03 +0100136 int_desc->interrupt_id = interrupt.id;
137 int_desc->priority = (attr >> INT_INFO_ATTR_PRIORITY_SHIFT) & 0xff;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500138
Daniel Boulby18485942024-10-14 16:23:03 +0100139 int_desc->type = (attr >> INT_INFO_ATTR_TYPE_SHIFT) & 0x3;
140 int_desc->config = (attr >> INT_INFO_ATTR_CONFIG_SHIFT) & 0x1;
141 int_desc->sec_state = (attr >> INT_INFO_ATTR_SEC_STATE_SHIFT) & 0x1;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500142
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700143 if (interrupt.mpidr_valid) {
Daniel Boulby18485942024-10-14 16:23:03 +0100144 int_desc->mpidr_valid = true;
145 int_desc->mpidr = interrupt.mpidr;
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700146 } else {
Daniel Boulby18485942024-10-14 16:23:03 +0100147 int_desc->mpidr_valid = false;
148 int_desc->mpidr = 0;
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700149 }
150
Daniel Boulby18485942024-10-14 16:23:03 +0100151 int_desc->valid = true;
152 int_desc->enabled = true;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500153}
154
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100155/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100156 * Performs VM loading activities that are common between the primary and
157 * secondaries.
158 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100159static bool load_common(struct mm_stage1_locked stage1_locked,
160 struct vm_locked vm_locked,
161 const struct manifest_vm *manifest_vm,
162 struct mpool *ppool)
Andrew Scullae9962e2019-10-03 16:51:16 +0100163{
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500164 struct device_region dev_region;
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700165 struct interrupt_info interrupt;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500166 uint32_t k = 0;
167
Manish Pandeyd34f8892020-06-19 17:41:07 +0100168 vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist;
Olivier Depreza15f2352022-09-26 09:17:24 +0200169 vm_locked.vm->power_management =
170 manifest_vm->partition.power_management;
Andrew Scullae9962e2019-10-03 16:51:16 +0100171
Kathleen Capella422b10b2023-06-30 18:28:27 -0400172 /* Populate array of UUIDs. */
173 for (uint16_t i = 0; i < PARTITION_MAX_UUIDS; i++) {
174 struct ffa_uuid current_uuid = manifest_vm->partition.uuids[i];
175
176 if (ffa_uuid_is_null(&current_uuid)) {
177 break;
178 }
179
180 vm_locked.vm->uuids[i] = current_uuid;
181 }
182
J-Alvescc542042024-07-24 19:13:22 +0100183 /*
184 * Populate the interrupt descriptor for current VM.
185 * They can be enabled in runtime using HF_INTERRUPT_ENABLE.
186 */
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700187 for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) {
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500188 dev_region = manifest_vm->partition.dev_regions[i];
189
190 CHECK(dev_region.interrupt_count <=
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700191 PARTITION_MAX_INTERRUPTS_PER_DEVICE);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500192
193 for (uint8_t j = 0; j < dev_region.interrupt_count; j++) {
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700194 struct interrupt_descriptor int_desc = {0};
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500195
196 interrupt = dev_region.interrupts[j];
197 infer_interrupt(interrupt, &int_desc);
198 vm_locked.vm->interrupt_desc[k] = int_desc;
Madhukar Pappireddy938faaf2023-07-31 17:56:55 -0500199 assert(int_desc.enabled);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500200
201 k++;
202 CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS);
203 }
204 }
J-Alvescc542042024-07-24 19:13:22 +0100205
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500206 dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k);
207
Manish Pandeyd34f8892020-06-19 17:41:07 +0100208 if (manifest_vm->is_ffa_partition) {
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +0000209 vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100210 /* Link rxtx buffers to mailbox */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700211 if (manifest_vm->partition.rxtx.available) {
Manish Pandeyd34f8892020-06-19 17:41:07 +0100212 if (!link_rxtx_to_mailbox(stage1_locked, vm_locked,
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700213 manifest_vm->partition.rxtx,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100214 ppool)) {
215 dlog_error(
216 "Unable to Link RX/TX buffer with "
217 "mailbox.\n");
218 return false;
219 }
220 }
J-Alvesb37fd082020-10-22 12:29:21 +0100221
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100222 vm_locked.vm->messaging_method =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700223 manifest_vm->partition.messaging_method;
Manish Pandeyf3be6392020-09-24 17:26:09 +0100224
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500225 vm_locked.vm->ns_interrupts_action =
226 manifest_vm->partition.ns_interrupts_action;
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100227
Madhukar Pappireddy5c04a382022-12-28 11:29:26 -0600228 vm_locked.vm->other_s_interrupts_action =
229 manifest_vm->partition.other_s_interrupts_action;
230
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500231 vm_locked.vm->me_signal_virq =
232 manifest_vm->partition.me_signal_virq;
233
J-Alvesa4730db2021-11-02 10:31:01 +0000234 vm_locked.vm->notifications.enabled =
235 manifest_vm->partition.notification_support;
236
Karl Meakina603e082024-08-02 17:57:27 +0100237 vm_locked.vm->vm_availability_messages.vm_created =
238 manifest_vm->partition.vm_availability_messages
239 .vm_created;
240 vm_locked.vm->vm_availability_messages.vm_destroyed =
241 manifest_vm->partition.vm_availability_messages
242 .vm_destroyed;
243
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700244 vm_locked.vm->boot_order = manifest_vm->partition.boot_order;
245
J-Alves7d38f7b2022-04-13 13:22:30 +0100246 vm_locked.vm->boot_info.gp_register_num =
247 manifest_vm->partition.gp_register_num;
248
249 if (manifest_vm->partition.boot_info) {
250 /*
251 * If the partition expects the boot information blob
252 * per the ff-a v1.1 boot protocol, then its address
253 * shall match the partition's load address.
254 */
255 vm_locked.vm->boot_info.blob_addr =
256 ipa_init(manifest_vm->partition.load_addr);
257 }
258
J-Alvesb37fd082020-10-22 12:29:21 +0100259 /* Updating boot list according to boot_order */
Olivier Deprez181074b2023-02-02 14:53:23 +0100260 vcpu_update_boot(vm_get_vcpu(vm_locked.vm, 0));
J-Alvesa0f317d2021-06-09 13:31:59 +0100261
J-Alves09ff9d82021-11-02 11:55:20 +0000262 if (vm_locked_are_notifications_enabled(vm_locked) &&
J-Alvesa4730db2021-11-02 10:31:01 +0000263 !plat_ffa_notifications_bitmap_create_call(
264 vm_locked.vm->id, vm_locked.vm->vcpu_count)) {
265 return false;
J-Alvesa9c7cba2021-08-25 16:26:11 +0100266 }
Manish Pandeyd34f8892020-06-19 17:41:07 +0100267 }
J-Alvesb37fd082020-10-22 12:29:21 +0100268
Fuad Tabba56970712020-01-10 11:20:09 +0000269 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100270 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000271
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500272 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
273 ppool)) {
274 dlog_error("Unable to attach upstream peripheral device\n");
275 return false;
276 }
277
Andrew Scullae9962e2019-10-03 16:51:16 +0100278 return true;
279}
280
281/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100282 * Loads the primary VM.
283 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100284static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100285 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100286 const struct memiter *cpio,
287 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100288{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100289 paddr_t primary_begin;
290 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100291 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000292 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100293 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100294 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000295 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100296
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700297 if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) {
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700298 primary_begin = pa_init(manifest_vm->partition.load_addr);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100299 primary_entry = ipa_add(ipa_from_pa(primary_begin),
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700300 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100301 } else {
302 primary_begin =
303 (manifest_vm->primary.boot_address ==
304 MANIFEST_INVALID_ADDRESS)
305 ? layout_primary_begin()
306 : pa_init(manifest_vm->primary.boot_address);
307 primary_entry = ipa_from_pa(primary_begin);
308 }
309
David Brazdil080ee312020-02-25 15:30:30 -0800310 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100311
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800312 /* Primary VM must be a VM */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700313 CHECK(manifest_vm->partition.run_time_el == EL1);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800314
Olivier Deprez62d99e32020-01-09 15:58:07 +0100315 /*
316 * Load the kernel if a filename is specified in the VM manifest.
317 * For an FF-A partition, kernel_filename is undefined indicating
318 * the partition package has already been loaded prior to Hafnium
319 * booting.
320 */
321 if (!string_is_empty(&manifest_vm->kernel_filename)) {
322 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100323 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100324 dlog_error("Unable to load primary kernel.\n");
325 return false;
326 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100327 }
328
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600329 if (!vm_init_next(MAX_CPUS, ppool, &vm, false,
330 manifest_vm->partition.dma_device_count)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000331 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100332 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100333 }
334
Karl Meakin5e996992024-05-20 11:27:07 +0100335 if (!vm_is_primary(vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000336 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100337 return false;
338 }
339
Andrew Scull3c257452019-11-26 13:32:50 +0000340 vm_locked = vm_lock(vm);
341
Andrew Scull48929fd2020-01-28 10:39:10 +0000342 if (params->device_mem_ranges_count == 0) {
343 /*
344 * Map 1TB of address space as device memory to, most likely,
345 * make all devices available to the primary VM.
346 *
347 * TODO: remove this once all targets provide valid ranges.
348 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800349 dlog_warning(
350 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000351
352 if (!vm_identity_map(
353 vm_locked, pa_init(0),
354 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
355 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
356 dlog_error(
357 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800358 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000359 ret = false;
360 goto out;
361 }
David Brazdile6f83222019-09-23 14:47:37 +0100362 }
363
Andrew Scullb5f49e02019-10-02 13:20:47 +0100364 /* Map normal memory as such to permit caching, execution, etc. */
365 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000366 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
367 params->mem_ranges[i].end,
368 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
369 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000370 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800371 "Unable to initialise memory for primary "
372 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000373 ret = false;
374 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100375 }
376 }
377
Andrew Scull48929fd2020-01-28 10:39:10 +0000378 /* Map device memory as such to prevent execution, speculation etc. */
379 for (i = 0; i < params->device_mem_ranges_count; ++i) {
380 if (!vm_identity_map(
381 vm_locked, params->device_mem_ranges[i].begin,
382 params->device_mem_ranges[i].end,
383 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
384 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800385 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000386 ret = false;
387 goto out;
388 }
389 }
390
Manish Pandeyd34f8892020-06-19 17:41:07 +0100391 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
392 ret = false;
393 goto out;
394 }
395
Andrew Scull3c257452019-11-26 13:32:50 +0000396 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800397 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000398 ret = false;
399 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100400 }
401
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000402 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800403 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000404 ret = false;
405 goto out;
406 }
407
Karl Meakine8937d92024-03-19 16:04:25 +0000408 dlog_info("Loaded primary VM with %u vCPUs, entry at %#lx.\n",
Andrew Walbran7586e042020-02-18 18:19:26 +0000409 vm->vcpu_count, pa_addr(primary_begin));
410
Olivier Deprez181074b2023-02-02 14:53:23 +0100411 /* Mark the first VM vCPU to be the first booted vCPU. */
412 vcpu_update_boot(vm_get_vcpu(vm, 0));
Olivier Deprezb9adff42021-02-01 12:14:05 +0100413
David Brazdile6f83222019-09-23 14:47:37 +0100414 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100415 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100416 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000417 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100418
Andrew Scull3c257452019-11-26 13:32:50 +0000419out:
420 vm_unlock(&vm_locked);
421
422 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100423}
424
Fuad Tabba50469e02020-06-30 15:14:28 +0100425/**
426 * Loads the secondary VM's FDT.
427 * Stores the total allocated size for the FDT in fdt_allocated_size (if
428 * fdt_allocated_size is not NULL). The allocated size includes additional space
429 * for potential patching.
430 */
431static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
432 paddr_t end, size_t fdt_max_size,
433 const struct manifest_vm *manifest_vm,
434 const struct memiter *cpio, struct mpool *ppool,
435 paddr_t *fdt_addr, size_t *fdt_allocated_size)
436{
437 struct memiter fdt;
438 size_t allocated_size;
439
440 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
441
442 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
443 dlog_error("Cannot open the secondary VM's FDT.\n");
444 return false;
445 }
446
447 /*
Olivier Deprez701e8bf2022-04-06 18:45:18 +0200448 * Ensure the FDT has one additional page at the end for patching,
Fuad Tabba50469e02020-06-30 15:14:28 +0100449 * and align it to the page boundary.
450 */
451 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
452
453 if (allocated_size > fdt_max_size) {
454 dlog_error(
Karl Meakine8937d92024-03-19 16:04:25 +0000455 "FDT allocated space (%zu) is more than the specified "
456 "maximum to use (%zu).\n",
Fuad Tabba50469e02020-06-30 15:14:28 +0100457 allocated_size, fdt_max_size);
458 return false;
459 }
460
461 /* Load the FDT to the end of the VM's allocated memory space. */
462 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
463
Karl Meakine8937d92024-03-19 16:04:25 +0000464 dlog_info("Loading secondary FDT of allocated size %zu at 0x%lx.\n",
Fuad Tabba50469e02020-06-30 15:14:28 +0100465 allocated_size, pa_addr(*fdt_addr));
466
467 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
468 dlog_error("Unable to copy FDT.\n");
469 return false;
470 }
471
472 if (fdt_allocated_size) {
473 *fdt_allocated_size = allocated_size;
474 }
475
476 return true;
477}
478
Olivier Deprez035fa152022-03-14 11:19:10 +0100479/**
480 * Convert the manifest memory region attributes to mode consumed by mm layer.
481 */
482static uint32_t memory_region_attributes_to_mode(uint32_t attributes)
483{
484 uint32_t mode = 0U;
485
486 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
487 mode |= MM_MODE_R;
488 }
489
490 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
491 mode |= MM_MODE_W;
492 }
493
494 if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) {
495 mode |= MM_MODE_X;
496 }
497
498 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) ||
499 (mode == (MM_MODE_R | MM_MODE_X)));
500
501 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
502 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
503 }
504
505 return mode;
506}
507
508/**
509 * Convert the manifest device region attributes to mode consumed by mm layer.
510 */
511static uint32_t device_region_attributes_to_mode(uint32_t attributes)
512{
513 uint32_t mode = 0U;
514
515 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
516 mode |= MM_MODE_R;
517 }
518
519 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
520 mode |= MM_MODE_W;
521 }
522
523 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R));
524
525 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
526 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
527 }
528
529 return mode | MM_MODE_D;
530}
531
Daniel Boulbye6df3452022-07-14 18:14:26 +0100532static bool ffa_map_memory_regions(const struct manifest_vm *manifest_vm,
533 const struct vm_locked vm_locked,
534 const struct vm_locked primary_vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000535 bool is_el0_partition, struct mpool *ppool)
Daniel Boulbye6df3452022-07-14 18:14:26 +0100536{
537#if LOG_LEVEL >= LOG_LEVEL_WARNING
538 const char *error_string = " region security state ignored for ";
539#endif
540 int j = 0;
541 paddr_t region_begin;
542 paddr_t region_end;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100543 size_t size;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100544 uint32_t map_mode;
545 uint32_t attributes;
546
547 /* Map memory-regions */
548 while (j < manifest_vm->partition.mem_region_count) {
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500549 struct memory_region mem_region;
550
551 mem_region = manifest_vm->partition.mem_regions[j];
552 size = mem_region.page_count * PAGE_SIZE;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100553 /*
J-Alvesd8a1d362023-03-08 11:15:28 +0000554 * Identity map memory region for both case,
555 * VA(S-EL0) or IPA(S-EL1).
Daniel Boulbye6df3452022-07-14 18:14:26 +0100556 */
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500557 region_begin = pa_init(mem_region.base_address);
J-Alvesd8a1d362023-03-08 11:15:28 +0000558 region_end = pa_add(region_begin, size);
Daniel Boulbye6df3452022-07-14 18:14:26 +0100559
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500560 attributes = mem_region.attributes;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100561 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100562 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100563 dlog_warning("Memory%sVMs\n", error_string);
564 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100565 }
566 }
567
568 map_mode = memory_region_attributes_to_mode(attributes);
569
570 if (is_el0_partition) {
571 map_mode |= MM_MODE_USER | MM_MODE_NG;
572 }
573
574 if (!vm_identity_map(vm_locked, region_begin, region_end,
575 map_mode, ppool, NULL)) {
576 dlog_error(
577 "Unable to map secondary VM "
578 "memory-region.\n");
579 return false;
580 }
581
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500582 /*
583 * Enforce static DMA isolation through stage 2 address
584 * translation.
585 * Only the DMA device that is specified as part of this memory
586 * region node in the partition manifest will be granted access
587 * to the memory region.
588 */
589 if (mem_region.dma_prop.stream_count > 0 &&
590 !vm_iommu_mm_identity_map(
591 vm_locked, region_begin, region_end, map_mode,
592 ppool, NULL, mem_region.dma_prop.dma_device_id)) {
593 dlog_error(
594 "Unable to map memory-region in the page "
595 "tables of DMA device.\n");
596 return false;
597 }
598
Daniel Boulbye6df3452022-07-14 18:14:26 +0100599 /* Deny the primary VM access to this memory */
600 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
601 ppool)) {
602 dlog_error(
603 "Unable to unmap secondary VM memory-"
604 "region from primary VM.\n");
605 return false;
606 }
607
Karl Meakine8937d92024-03-19 16:04:25 +0000608 dlog_verbose("Memory region %#lx - %#lx allocated.\n",
609 pa_addr(region_begin), pa_addr(region_end));
Daniel Boulbye6df3452022-07-14 18:14:26 +0100610
611 j++;
612 }
613
614 /* Map device-regions */
615 j = 0;
616 while (j < manifest_vm->partition.dev_region_count) {
617 region_begin = pa_init(
618 manifest_vm->partition.dev_regions[j].base_address);
619 size = manifest_vm->partition.dev_regions[j].page_count *
620 PAGE_SIZE;
621 region_end = pa_add(region_begin, size);
622
623 attributes = manifest_vm->partition.dev_regions[j].attributes;
624 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100625 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100626 dlog_warning("Device%sVMs\n", error_string);
627 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100628 }
629 }
630
631 map_mode = device_region_attributes_to_mode(attributes);
632 if (is_el0_partition) {
633 map_mode |= MM_MODE_USER | MM_MODE_NG;
634 }
635
636 if (!vm_identity_map(vm_locked, region_begin, region_end,
637 map_mode, ppool, NULL)) {
638 dlog_error(
639 "Unable to map secondary VM "
640 "device-region.\n");
641 return false;
642 }
643 /* Deny primary VM access to this region */
644 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
645 ppool)) {
646 dlog_error(
647 "Unable to unmap secondary VM device-"
648 "region from primary VM.\n");
649 return false;
650 }
651 j++;
652 }
653 return true;
654}
655
Andrew Scull72b43c02019-09-18 13:53:45 +0100656/*
657 * Loads a secondary VM.
658 */
659static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100660 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100661 paddr_t mem_begin, paddr_t mem_end,
662 const struct manifest_vm *manifest_vm,
J-Alves77b6f4f2023-03-15 11:34:49 +0000663 const struct boot_params *boot_params,
Andrew Scull72b43c02019-09-18 13:53:45 +0100664 const struct memiter *cpio, struct mpool *ppool)
665{
666 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000667 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100668 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100669 struct vcpu *vcpu;
670 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000671 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100672 paddr_t fdt_addr;
673 bool has_fdt;
674 size_t kernel_size = 0;
675 const size_t mem_size = pa_difference(mem_begin, mem_end);
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800676 uint32_t map_mode;
Daniel Boulby874d5432023-04-27 12:40:24 +0100677 bool is_el0_partition = manifest_vm->partition.run_time_el == S_EL0 ||
678 manifest_vm->partition.run_time_el == EL0;
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100679 size_t n;
Andrew Scull72b43c02019-09-18 13:53:45 +0100680
Olivier Deprez62d99e32020-01-09 15:58:07 +0100681 /*
682 * Load the kernel if a filename is specified in the VM manifest.
683 * For an FF-A partition, kernel_filename is undefined indicating
684 * the partition package has already been loaded prior to Hafnium
685 * booting.
686 */
687 if (!string_is_empty(&manifest_vm->kernel_filename)) {
688 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100689 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100690 dlog_error("Unable to load kernel.\n");
691 return false;
692 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100693 }
694
Fuad Tabba50469e02020-06-30 15:14:28 +0100695 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
696 if (has_fdt) {
697 /*
698 * Ensure that the FDT does not overwrite the kernel or overlap
699 * its page, for the FDT to start at a page boundary.
700 */
701 const size_t fdt_max_size =
702 mem_size - align_up(kernel_size, PAGE_SIZE);
703
704 size_t fdt_allocated_size;
705
706 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
707 manifest_vm, cpio, ppool, &fdt_addr,
708 &fdt_allocated_size)) {
709 dlog_error("Unable to load FDT.\n");
710 return false;
711 }
712
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700713 if (manifest_vm->is_ffa_partition) {
714 plat_ffa_parse_partition_manifest(
715 stage1_locked, fdt_addr, fdt_allocated_size,
J-Alves77b6f4f2023-03-15 11:34:49 +0000716 manifest_vm, boot_params, ppool);
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700717 }
718
Fuad Tabba50469e02020-06-30 15:14:28 +0100719 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
720 mem_begin, mem_end, ppool)) {
721 dlog_error("Unable to patch FDT.\n");
722 return false;
723 }
724 }
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800725 /*
726 * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the
727 * FF-A 1.0 spec.
728 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100729 CHECK(!is_el0_partition || manifest_vm->secondary.vcpu_count == 1);
Fuad Tabba50469e02020-06-30 15:14:28 +0100730
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800731 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm,
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600732 is_el0_partition,
733 manifest_vm->partition.dma_device_count)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000734 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100735 return false;
736 }
737
Andrew Scull3c257452019-11-26 13:32:50 +0000738 vm_locked = vm_lock(vm);
739
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800740 /*
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700741 * Grant the VM access to the memory. For VM's we mark all memory in
742 * stage-2 tables as RWX and the VM can control permissions using
743 * stage-1 translations. For S-EL0 partitions, hafnium maps the entire
744 * region of memory for the partition as RX. The partition is then
745 * expected to perform its owns relocations and call the FFA_MEM_PERM_*
746 * API's to change permissions on its image layout.
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800747 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100748 if (is_el0_partition) {
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700749 map_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG;
750 } else {
751 map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800752 }
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700753
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800754 if (!vm_identity_map(vm_locked, mem_begin, mem_end, map_mode, ppool,
Andrew Scull3c257452019-11-26 13:32:50 +0000755 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000756 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000757 ret = false;
758 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100759 }
760
Olivier Deprez62d99e32020-01-09 15:58:07 +0100761 if (manifest_vm->is_ffa_partition) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100762 if (!ffa_map_memory_regions(manifest_vm, vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000763 primary_vm_locked, is_el0_partition,
764 ppool)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100765 ret = false;
766 goto out;
Manish Pandey2145c212020-05-01 16:04:22 +0100767 }
768
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700769 secondary_entry = ipa_add(secondary_entry,
770 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100771 }
772
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800773 /*
774 * Map hypervisor into the VM's page table. The hypervisor pages will
775 * not be accessible from EL0 since it will not be marked for user
776 * access.
777 * TODO: Map only the exception vectors and data that exception vectors
778 * require and not the entire hypervisor. This helps with speculative
779 * side-channel attacks.
780 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100781 if (is_el0_partition) {
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800782 CHECK(vm_identity_map(vm_locked, layout_text_begin(),
783 layout_text_end(), MM_MODE_X, ppool,
784 NULL));
785
786 CHECK(vm_identity_map(vm_locked, layout_rodata_begin(),
787 layout_rodata_end(), MM_MODE_R, ppool,
788 NULL));
789
790 CHECK(vm_identity_map(vm_locked, layout_data_begin(),
791 layout_data_end(), MM_MODE_R | MM_MODE_W,
792 ppool, NULL));
Maksims Svecovs134b8f92022-03-04 15:14:09 +0000793
794 CHECK(arch_stack_mm_init(mm_lock_ptable_unsafe(&vm->ptable),
795 ppool));
796
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800797 plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool);
798 }
799
Manish Pandeyd34f8892020-06-19 17:41:07 +0100800 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
801 ret = false;
802 goto out;
803 }
804
Karl Meakine8937d92024-03-19 16:04:25 +0000805 dlog_info("Loaded with %u vCPUs, entry at %#lx.\n",
Manish Pandey2145c212020-05-01 16:04:22 +0100806 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
807
Andrew Scull72b43c02019-09-18 13:53:45 +0100808 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100809
Max Shvetsov40108e72020-08-27 12:39:50 +0100810 vcpu_locked = vcpu_lock(vcpu);
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500811
Fuad Tabba50469e02020-06-30 15:14:28 +0100812 if (has_fdt) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100813 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
Fuad Tabba50469e02020-06-30 15:14:28 +0100814 pa_addr(fdt_addr));
815 } else {
816 /*
817 * Without an FDT, secondary VMs expect the memory size to be
818 * passed in register x0, which is what
819 * vcpu_secondary_reset_and_start does in this case.
820 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100821 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
822 mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100823 }
824
Max Shvetsov40108e72020-08-27 12:39:50 +0100825 vcpu_unlock(&vcpu_locked);
826
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100827 /*
828 * For all vCPUs,
829 * in a VM: enable the notification pending virtual interrupt if
830 * requested in the manifest.
831 * in a SP: enable the NPI and managed exit virtual interrupts if
832 * requested in the manifest. For a S-EL0 partition, enable
833 * the virtual interrupts IDs matching the secure physical
834 * interrupt IDs declared in device regions.
835 */
836 for (n = 0; n < manifest_vm->secondary.vcpu_count; n++) {
837 vcpu = vm_get_vcpu(vm, n);
838 vcpu_locked = vcpu_lock(vcpu);
839 plat_ffa_enable_virtual_interrupts(vcpu_locked, vm_locked);
840 vcpu_unlock(&vcpu_locked);
841 }
842
Andrew Scull3c257452019-11-26 13:32:50 +0000843 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100844
Andrew Scull3c257452019-11-26 13:32:50 +0000845out:
846 vm_unlock(&vm_locked);
847
848 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100849}
850
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100851/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100852 * Try to find a memory range of the given size within the given ranges, and
853 * remove it from them. Return true on success, or false if no large enough
854 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100855 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900856static bool carve_out_mem_range(struct mem_range *mem_ranges,
857 size_t mem_ranges_count, uint64_t size_to_find,
858 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100859{
860 size_t i;
861
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000862 /*
863 * TODO(b/116191358): Consider being cleverer about how we pack VMs
864 * together, with a non-greedy algorithm.
865 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100866 for (i = 0; i < mem_ranges_count; ++i) {
867 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100868 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100869 /*
870 * This range is big enough, take some of it from the
871 * end and reduce its size accordingly.
872 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100873 *found_end = mem_ranges[i].end;
874 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
875 size_to_find);
876 mem_ranges[i].end = *found_begin;
877 return true;
878 }
879 }
880 return false;
881}
882
883/**
884 * Given arrays of memory ranges before and after memory was removed for
885 * secondary VMs, add the difference to the reserved ranges of the given update.
886 * Return true on success, or false if there would be more than MAX_MEM_RANGES
887 * reserved ranges after adding the new ones.
888 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
889 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900890static bool update_reserved_ranges(struct boot_params_update *update,
891 const struct mem_range *before,
892 const struct mem_range *after,
893 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100894{
895 size_t i;
896
897 for (i = 0; i < mem_ranges_count; ++i) {
898 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
899 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000900 dlog_error(
901 "Too many reserved ranges after "
902 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100903 return false;
904 }
905 update->reserved_ranges[update->reserved_ranges_count]
906 .begin = before[i].begin;
907 update->reserved_ranges[update->reserved_ranges_count]
908 .end = after[i].begin;
909 update->reserved_ranges_count++;
910 }
911 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
912 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000913 dlog_error(
914 "Too many reserved ranges after "
915 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100916 return false;
917 }
918 update->reserved_ranges[update->reserved_ranges_count]
919 .begin = after[i].end;
920 update->reserved_ranges[update->reserved_ranges_count]
921 .end = before[i].end;
922 update->reserved_ranges_count++;
923 }
924 }
925
926 return true;
927}
928
Olivier Deprez05046922023-03-09 15:48:40 +0100929static bool init_other_world_vm(const struct boot_params *params,
930 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100931{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200932 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100933 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100934
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100935 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200936 * Initialise the dummy VM which represents the opposite world:
937 * -TrustZone (or the SPMC) when running the Hypervisor
938 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100939 */
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600940 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false, 0);
Olivier Deprez96a2a262020-06-11 17:21:38 +0200941 CHECK(other_world_vm != NULL);
942
943 for (i = 0; i < MAX_CPUS; i++) {
944 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
945 struct cpu *cpu = cpu_find_index(i);
946
947 vcpu->cpu = cpu;
948 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100949
Olivier Deprez05046922023-03-09 15:48:40 +0100950 return arch_other_world_vm_init(other_world_vm, params, ppool);
Olivier Deprez112d2b52020-09-30 07:39:23 +0200951}
952
953/*
954 * Loads alls VMs from the manifest.
955 */
956bool load_vms(struct mm_stage1_locked stage1_locked,
957 const struct manifest *manifest, const struct memiter *cpio,
958 const struct boot_params *params,
959 struct boot_params_update *update, struct mpool *ppool)
960{
961 struct vm *primary;
962 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
963 struct vm_locked primary_vm_locked;
964 size_t i;
965 bool success = true;
966
Andrew Walbranf8716782020-10-29 17:07:07 +0000967 /**
968 * Only try to load the primary VM if it is supposed to be in this
969 * world.
970 */
971 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
972 if (!load_primary(stage1_locked,
973 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
974 params, ppool)) {
975 dlog_error("Unable to load primary VM.\n");
976 return false;
977 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200978 }
979
Olivier Deprez05046922023-03-09 15:48:40 +0100980 if (!init_other_world_vm(params, ppool)) {
Olivier Deprez112d2b52020-09-30 07:39:23 +0200981 return false;
982 }
983
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100984 static_assert(
985 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
986 "mem_range arrays must be the same size for memcpy.");
987 static_assert(sizeof(mem_ranges_available) < 500,
988 "This will use too much stack, either make "
989 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100990 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
991 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100992
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100993 /* Round the last addresses down to the page size. */
994 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000995 mem_ranges_available[i].end = pa_init(align_down(
996 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100997 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100998
Andrew Scull3c257452019-11-26 13:32:50 +0000999 primary = vm_find(HF_PRIMARY_VM_ID);
1000 primary_vm_locked = vm_lock(primary);
1001
David Brazdil0251b942019-09-10 15:59:50 +01001002 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +01001003 const struct manifest_vm *manifest_vm = &manifest->vm[i];
J-Alves19e20cf2023-08-02 12:48:55 +01001004 ffa_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +01001005 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +01001006 paddr_t secondary_mem_begin;
1007 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001008
David Brazdil7a462ec2019-08-15 12:27:47 +01001009 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001010 continue;
1011 }
1012
Olivier Deprez2a8ee342020-08-03 15:10:44 +02001013 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Karl Meakine8937d92024-03-19 16:04:25 +00001014 manifest_vm->debug_name.data);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001015
David Brazdil7a462ec2019-08-15 12:27:47 +01001016 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001017
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -07001018 if (manifest_vm->is_ffa_partition &&
1019 !manifest->vm[i].is_hyp_loaded) {
Olivier Deprez62d99e32020-01-09 15:58:07 +01001020 secondary_mem_begin =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001021 pa_init(manifest_vm->partition.load_addr);
1022 secondary_mem_end = pa_init(
1023 manifest_vm->partition.load_addr + mem_size);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001024 } else if (!carve_out_mem_range(mem_ranges_available,
1025 params->mem_ranges_count,
1026 mem_size, &secondary_mem_begin,
1027 &secondary_mem_end)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001028 dlog_error("Not enough memory (%lu bytes).\n",
1029 mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001030 continue;
1031 }
Andrew Scull80871322018-08-06 12:04:09 +01001032
Manish Pandey2145c212020-05-01 16:04:22 +01001033 if (!load_secondary(stage1_locked, primary_vm_locked,
1034 secondary_mem_begin, secondary_mem_end,
J-Alves77b6f4f2023-03-15 11:34:49 +00001035 manifest_vm, params, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001036 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001037 continue;
1038 }
1039
1040 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +00001041 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
1042 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001043 dlog_error(
1044 "Unable to unmap secondary VM from primary "
1045 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +00001046 success = false;
1047 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001048 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001049 }
1050
Andrew Scull3c257452019-11-26 13:32:50 +00001051 vm_unlock(&primary_vm_locked);
1052
1053 if (!success) {
1054 return false;
1055 }
1056
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001057 /*
1058 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001059 * difference between the available ranges from the original params and
1060 * the updated mem_ranges_available. We assume that the number and order
1061 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001062 * above only make them smaller.
1063 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001064 return update_reserved_ranges(update, params->mem_ranges,
1065 mem_ranges_available,
1066 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001067}