blob: d46207c3c5db4931843b70f1f2671f9edb3827ac [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
Maksims Svecovs134b8f92022-03-04 15:14:09 +000011#include "hf/arch/init.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000013#include "hf/arch/vm.h"
14
Andrew Scull18c78fc2018-08-20 12:57:41 +010015#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010016#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010017#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010018#include "hf/dlog.h"
Fuad Tabba50469e02020-06-30 15:14:28 +010019#include "hf/fdt_patch.h"
Karl Meakin902af082024-11-28 14:58:38 +000020#include "hf/ffa/interrupts.h"
21#include "hf/ffa/notifications.h"
22#include "hf/ffa/setup_and_discovery.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"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000028#include "hf/plat/iommu.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010029#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010031
Manish Pandeyd34f8892020-06-19 17:41:07 +010032#include "vmapi/hf/ffa.h"
Andrew Scull19503262018-09-20 14:48:39 +010033
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034/**
35 * Copies data to an unmapped location by mapping it for write, copying the
36 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000037 *
38 * The data is written so that it is available to all cores with the cache
39 * disabled. When switching to the partitions, the caching is initially disabled
40 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010041 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010042static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010043 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010044{
David Brazdil7a462ec2019-08-15 12:27:47 +010045 const void *from = memiter_base(from_it);
46 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010047 paddr_t to_end = pa_add(to, size);
48 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010049
Andrew Scull3c0a90a2019-07-01 11:55:53 +010050 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010051 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010052 return false;
53 }
54
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010055 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010056 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010057
Andrew Scull72b43c02019-09-18 13:53:45 +010058 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010059
60 return true;
61}
62
Fuad Tabba50469e02020-06-30 15:14:28 +010063/**
64 * Loads the secondary VM's kernel.
65 * Stores the kernel size in kernel_size (if kernel_size is not NULL).
66 * Returns false if it cannot load the kernel.
67 */
Andrew Scull72b43c02019-09-18 13:53:45 +010068static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
69 paddr_t end, const struct manifest_vm *manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +010070 const struct memiter *cpio, struct mpool *ppool,
71 size_t *kernel_size)
Andrew Scull72b43c02019-09-18 13:53:45 +010072{
Andrew Scull72b43c02019-09-18 13:53:45 +010073 struct memiter kernel;
Fuad Tabba50469e02020-06-30 15:14:28 +010074 size_t size;
Andrew Scull72b43c02019-09-18 13:53:45 +010075
David Brazdil136f2942019-09-23 14:11:03 +010076 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000077 dlog_error("Could not find kernel file \"%s\".\n",
78 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010079 return false;
80 }
81
Fuad Tabba50469e02020-06-30 15:14:28 +010082 size = memiter_size(&kernel);
83 if (pa_difference(begin, end) < size) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000084 dlog_error("Kernel is larger than available memory.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010085 return false;
86 }
87
88 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000089 dlog_error("Unable to copy kernel.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010090 return false;
91 }
92
Fuad Tabba50469e02020-06-30 15:14:28 +010093 if (kernel_size) {
94 *kernel_size = size;
95 }
96
Andrew Scull72b43c02019-09-18 13:53:45 +010097 return true;
98}
99
Manish Pandeyd34f8892020-06-19 17:41:07 +0100100/*
101 * Link RX/TX buffers provided in partition manifest to mailbox
102 */
103static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked,
104 struct vm_locked vm_locked, struct rx_tx rxtx,
105 struct mpool *ppool)
106{
107 struct ffa_value ret;
108 ipaddr_t send;
109 ipaddr_t recv;
110 uint32_t page_count;
111
112 send = ipa_init(rxtx.tx_buffer->base_address);
113 recv = ipa_init(rxtx.rx_buffer->base_address);
114 page_count = rxtx.tx_buffer->page_count;
115
116 ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv,
117 page_count, ppool);
118 if (ret.func != FFA_SUCCESS_32) {
119 return false;
120 }
121
Karl Meakine8937d92024-03-19 16:04:25 +0000122 dlog_verbose(" mailbox: send = %p, recv = %p\n",
Manish Pandeyd34f8892020-06-19 17:41:07 +0100123 vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv);
124
125 return true;
126}
127
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700128static void infer_interrupt(struct interrupt_info interrupt,
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500129 struct interrupt_descriptor *int_desc)
130{
131 uint32_t attr = interrupt.attributes;
132
Daniel Boulby18485942024-10-14 16:23:03 +0100133 int_desc->interrupt_id = interrupt.id;
134 int_desc->priority = (attr >> INT_INFO_ATTR_PRIORITY_SHIFT) & 0xff;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500135
Daniel Boulby18485942024-10-14 16:23:03 +0100136 int_desc->type = (attr >> INT_INFO_ATTR_TYPE_SHIFT) & 0x3;
137 int_desc->config = (attr >> INT_INFO_ATTR_CONFIG_SHIFT) & 0x1;
138 int_desc->sec_state = (attr >> INT_INFO_ATTR_SEC_STATE_SHIFT) & 0x1;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500139
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700140 if (interrupt.mpidr_valid) {
Daniel Boulby18485942024-10-14 16:23:03 +0100141 int_desc->mpidr_valid = true;
142 int_desc->mpidr = interrupt.mpidr;
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700143 } else {
Daniel Boulby18485942024-10-14 16:23:03 +0100144 int_desc->mpidr_valid = false;
145 int_desc->mpidr = 0;
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700146 }
147
Daniel Boulby18485942024-10-14 16:23:03 +0100148 int_desc->valid = true;
149 int_desc->enabled = true;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500150}
151
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100152/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100153 * Performs VM loading activities that are common between the primary and
154 * secondaries.
155 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100156static bool load_common(struct mm_stage1_locked stage1_locked,
157 struct vm_locked vm_locked,
158 const struct manifest_vm *manifest_vm,
159 struct mpool *ppool)
Andrew Scullae9962e2019-10-03 16:51:16 +0100160{
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500161 struct device_region dev_region;
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700162 struct interrupt_info interrupt;
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500163 uint32_t k = 0;
164
Manish Pandeyd34f8892020-06-19 17:41:07 +0100165 vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist;
Olivier Depreza15f2352022-09-26 09:17:24 +0200166 vm_locked.vm->power_management =
167 manifest_vm->partition.power_management;
Andrew Scullae9962e2019-10-03 16:51:16 +0100168
Kathleen Capella422b10b2023-06-30 18:28:27 -0400169 /* Populate array of UUIDs. */
170 for (uint16_t i = 0; i < PARTITION_MAX_UUIDS; i++) {
171 struct ffa_uuid current_uuid = manifest_vm->partition.uuids[i];
172
173 if (ffa_uuid_is_null(&current_uuid)) {
174 break;
175 }
176
177 vm_locked.vm->uuids[i] = current_uuid;
178 }
179
J-Alvescc542042024-07-24 19:13:22 +0100180 /*
181 * Populate the interrupt descriptor for current VM.
182 * They can be enabled in runtime using HF_INTERRUPT_ENABLE.
183 */
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700184 for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) {
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500185 dev_region = manifest_vm->partition.dev_regions[i];
186
187 CHECK(dev_region.interrupt_count <=
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700188 PARTITION_MAX_INTERRUPTS_PER_DEVICE);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500189
190 for (uint8_t j = 0; j < dev_region.interrupt_count; j++) {
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700191 struct interrupt_descriptor int_desc = {0};
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500192
193 interrupt = dev_region.interrupts[j];
194 infer_interrupt(interrupt, &int_desc);
195 vm_locked.vm->interrupt_desc[k] = int_desc;
Madhukar Pappireddy938faaf2023-07-31 17:56:55 -0500196 assert(int_desc.enabled);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500197
198 k++;
199 CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS);
200 }
201 }
J-Alvescc542042024-07-24 19:13:22 +0100202
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500203 dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k);
204
Manish Pandeyd34f8892020-06-19 17:41:07 +0100205 if (manifest_vm->is_ffa_partition) {
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +0000206 vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100207 /* Link rxtx buffers to mailbox */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700208 if (manifest_vm->partition.rxtx.available) {
Manish Pandeyd34f8892020-06-19 17:41:07 +0100209 if (!link_rxtx_to_mailbox(stage1_locked, vm_locked,
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700210 manifest_vm->partition.rxtx,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100211 ppool)) {
212 dlog_error(
213 "Unable to Link RX/TX buffer with "
214 "mailbox.\n");
215 return false;
216 }
217 }
J-Alvesb37fd082020-10-22 12:29:21 +0100218
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100219 vm_locked.vm->messaging_method =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700220 manifest_vm->partition.messaging_method;
Manish Pandeyf3be6392020-09-24 17:26:09 +0100221
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500222 vm_locked.vm->ns_interrupts_action =
223 manifest_vm->partition.ns_interrupts_action;
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100224
Madhukar Pappireddy5c04a382022-12-28 11:29:26 -0600225 vm_locked.vm->other_s_interrupts_action =
226 manifest_vm->partition.other_s_interrupts_action;
227
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500228 vm_locked.vm->me_signal_virq =
229 manifest_vm->partition.me_signal_virq;
230
J-Alvesa4730db2021-11-02 10:31:01 +0000231 vm_locked.vm->notifications.enabled =
232 manifest_vm->partition.notification_support;
233
Karl Meakina603e082024-08-02 17:57:27 +0100234 vm_locked.vm->vm_availability_messages.vm_created =
235 manifest_vm->partition.vm_availability_messages
236 .vm_created;
237 vm_locked.vm->vm_availability_messages.vm_destroyed =
238 manifest_vm->partition.vm_availability_messages
239 .vm_destroyed;
240
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700241 vm_locked.vm->boot_order = manifest_vm->partition.boot_order;
242
J-Alves7d38f7b2022-04-13 13:22:30 +0100243 vm_locked.vm->boot_info.gp_register_num =
244 manifest_vm->partition.gp_register_num;
245
246 if (manifest_vm->partition.boot_info) {
247 /*
248 * If the partition expects the boot information blob
249 * per the ff-a v1.1 boot protocol, then its address
250 * shall match the partition's load address.
251 */
252 vm_locked.vm->boot_info.blob_addr =
253 ipa_init(manifest_vm->partition.load_addr);
254 }
255
J-Alvesce42f7a2025-02-10 13:57:41 +0000256 vm_locked.vm->sri_policy = manifest_vm->partition.sri_policy;
257
J-Alvesb37fd082020-10-22 12:29:21 +0100258 /* Updating boot list according to boot_order */
Madhukar Pappireddya49ba162024-11-25 09:40:45 -0600259 vm_update_boot(vm_locked.vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100260
J-Alves09ff9d82021-11-02 11:55:20 +0000261 if (vm_locked_are_notifications_enabled(vm_locked) &&
Karl Meakin117c8082024-12-04 16:03:28 +0000262 !ffa_notifications_bitmap_create_call(
J-Alvesa4730db2021-11-02 10:31:01 +0000263 vm_locked.vm->id, vm_locked.vm->vcpu_count)) {
264 return false;
J-Alvesa9c7cba2021-08-25 16:26:11 +0100265 }
Manish Pandeyd34f8892020-06-19 17:41:07 +0100266 }
J-Alvesb37fd082020-10-22 12:29:21 +0100267
Fuad Tabba56970712020-01-10 11:20:09 +0000268 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100269 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000270
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500271 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
272 ppool)) {
273 dlog_error("Unable to attach upstream peripheral device\n");
274 return false;
275 }
276
Andrew Scullae9962e2019-10-03 16:51:16 +0100277 return true;
278}
279
280/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100281 * Loads the primary VM.
282 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100283static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100284 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100285 const struct memiter *cpio,
286 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100287{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100288 paddr_t primary_begin;
289 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100290 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000291 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100292 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100293 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000294 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100295
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700296 if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) {
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700297 primary_begin = pa_init(manifest_vm->partition.load_addr);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100298 primary_entry = ipa_add(ipa_from_pa(primary_begin),
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700299 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100300 } else {
301 primary_begin =
302 (manifest_vm->primary.boot_address ==
303 MANIFEST_INVALID_ADDRESS)
304 ? layout_primary_begin()
305 : pa_init(manifest_vm->primary.boot_address);
306 primary_entry = ipa_from_pa(primary_begin);
307 }
308
David Brazdil080ee312020-02-25 15:30:30 -0800309 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100310
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800311 /* Primary VM must be a VM */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700312 CHECK(manifest_vm->partition.run_time_el == EL1);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800313
Olivier Deprez62d99e32020-01-09 15:58:07 +0100314 /*
315 * Load the kernel if a filename is specified in the VM manifest.
316 * For an FF-A partition, kernel_filename is undefined indicating
317 * the partition package has already been loaded prior to Hafnium
318 * booting.
319 */
320 if (!string_is_empty(&manifest_vm->kernel_filename)) {
321 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100322 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100323 dlog_error("Unable to load primary kernel.\n");
324 return false;
325 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100326 }
327
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600328 if (!vm_init_next(MAX_CPUS, ppool, &vm, false,
329 manifest_vm->partition.dma_device_count)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000330 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100331 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100332 }
333
Karl Meakin5e996992024-05-20 11:27:07 +0100334 if (!vm_is_primary(vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000335 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100336 return false;
337 }
338
Andrew Scull3c257452019-11-26 13:32:50 +0000339 vm_locked = vm_lock(vm);
340
Andrew Scull48929fd2020-01-28 10:39:10 +0000341 if (params->device_mem_ranges_count == 0) {
342 /*
343 * Map 1TB of address space as device memory to, most likely,
344 * make all devices available to the primary VM.
345 *
346 * TODO: remove this once all targets provide valid ranges.
347 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800348 dlog_warning(
349 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000350
351 if (!vm_identity_map(
352 vm_locked, pa_init(0),
353 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
354 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
355 dlog_error(
356 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800357 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000358 ret = false;
359 goto out;
360 }
David Brazdile6f83222019-09-23 14:47:37 +0100361 }
362
Andrew Scullb5f49e02019-10-02 13:20:47 +0100363 /* Map normal memory as such to permit caching, execution, etc. */
364 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000365 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
366 params->mem_ranges[i].end,
367 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
368 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000369 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800370 "Unable to initialise memory for primary "
371 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000372 ret = false;
373 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100374 }
375 }
376
Andrew Scull48929fd2020-01-28 10:39:10 +0000377 /* Map device memory as such to prevent execution, speculation etc. */
378 for (i = 0; i < params->device_mem_ranges_count; ++i) {
379 if (!vm_identity_map(
380 vm_locked, params->device_mem_ranges[i].begin,
381 params->device_mem_ranges[i].end,
382 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
383 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800384 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000385 ret = false;
386 goto out;
387 }
388 }
389
Manish Pandeyd34f8892020-06-19 17:41:07 +0100390 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
391 ret = false;
392 goto out;
393 }
394
Andrew Scull3c257452019-11-26 13:32:50 +0000395 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800396 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000397 ret = false;
398 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100399 }
400
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000401 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800402 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000403 ret = false;
404 goto out;
405 }
406
Karl Meakine8937d92024-03-19 16:04:25 +0000407 dlog_info("Loaded primary VM with %u vCPUs, entry at %#lx.\n",
Andrew Walbran7586e042020-02-18 18:19:26 +0000408 vm->vcpu_count, pa_addr(primary_begin));
409
Madhukar Pappireddya49ba162024-11-25 09:40:45 -0600410 /* Mark the first VM to be the first booted VM. */
411 vm_update_boot(vm);
Olivier Deprezb9adff42021-02-01 12:14:05 +0100412
David Brazdile6f83222019-09-23 14:47:37 +0100413 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100414 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100415 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000416 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100417
Andrew Scull3c257452019-11-26 13:32:50 +0000418out:
419 vm_unlock(&vm_locked);
420
421 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100422}
423
Fuad Tabba50469e02020-06-30 15:14:28 +0100424/**
425 * Loads the secondary VM's FDT.
426 * Stores the total allocated size for the FDT in fdt_allocated_size (if
427 * fdt_allocated_size is not NULL). The allocated size includes additional space
428 * for potential patching.
429 */
430static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
431 paddr_t end, size_t fdt_max_size,
432 const struct manifest_vm *manifest_vm,
433 const struct memiter *cpio, struct mpool *ppool,
434 paddr_t *fdt_addr, size_t *fdt_allocated_size)
435{
436 struct memiter fdt;
437 size_t allocated_size;
438
439 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
440
441 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
442 dlog_error("Cannot open the secondary VM's FDT.\n");
443 return false;
444 }
445
446 /*
Olivier Deprez701e8bf2022-04-06 18:45:18 +0200447 * Ensure the FDT has one additional page at the end for patching,
Fuad Tabba50469e02020-06-30 15:14:28 +0100448 * and align it to the page boundary.
449 */
450 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
451
452 if (allocated_size > fdt_max_size) {
453 dlog_error(
Karl Meakine8937d92024-03-19 16:04:25 +0000454 "FDT allocated space (%zu) is more than the specified "
455 "maximum to use (%zu).\n",
Fuad Tabba50469e02020-06-30 15:14:28 +0100456 allocated_size, fdt_max_size);
457 return false;
458 }
459
460 /* Load the FDT to the end of the VM's allocated memory space. */
461 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
462
Karl Meakine8937d92024-03-19 16:04:25 +0000463 dlog_info("Loading secondary FDT of allocated size %zu at 0x%lx.\n",
Fuad Tabba50469e02020-06-30 15:14:28 +0100464 allocated_size, pa_addr(*fdt_addr));
465
466 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
467 dlog_error("Unable to copy FDT.\n");
468 return false;
469 }
470
471 if (fdt_allocated_size) {
472 *fdt_allocated_size = allocated_size;
473 }
474
475 return true;
476}
477
Olivier Deprez035fa152022-03-14 11:19:10 +0100478/**
479 * Convert the manifest memory region attributes to mode consumed by mm layer.
480 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000481static mm_mode_t memory_region_attributes_to_mode(uint32_t attributes)
Olivier Deprez035fa152022-03-14 11:19:10 +0100482{
Karl Meakin07a69ab2025-02-07 14:53:19 +0000483 mm_mode_t mode = 0U;
Olivier Deprez035fa152022-03-14 11:19:10 +0100484
485 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
486 mode |= MM_MODE_R;
487 }
488
489 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
490 mode |= MM_MODE_W;
491 }
492
493 if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) {
494 mode |= MM_MODE_X;
495 }
496
497 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) ||
498 (mode == (MM_MODE_R | MM_MODE_X)));
499
500 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
Karl Meakin07a69ab2025-02-07 14:53:19 +0000501 mode |= arch_mm_extra_mode_from_vm(HF_HYPERVISOR_VM_ID);
Olivier Deprez035fa152022-03-14 11:19:10 +0100502 }
503
504 return mode;
505}
506
507/**
508 * Convert the manifest device region attributes to mode consumed by mm layer.
509 */
Karl Meakin07a69ab2025-02-07 14:53:19 +0000510static mm_mode_t device_region_attributes_to_mode(uint32_t attributes)
Olivier Deprez035fa152022-03-14 11:19:10 +0100511{
Karl Meakin07a69ab2025-02-07 14:53:19 +0000512 mm_mode_t mode = 0U;
Olivier Deprez035fa152022-03-14 11:19:10 +0100513
514 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
515 mode |= MM_MODE_R;
516 }
517
518 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
519 mode |= MM_MODE_W;
520 }
521
522 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R));
523
524 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
Karl Meakin07a69ab2025-02-07 14:53:19 +0000525 mode |= arch_mm_extra_mode_from_vm(HF_HYPERVISOR_VM_ID);
Olivier Deprez035fa152022-03-14 11:19:10 +0100526 }
527
528 return mode | MM_MODE_D;
529}
530
Daniel Boulbye6df3452022-07-14 18:14:26 +0100531static bool ffa_map_memory_regions(const struct manifest_vm *manifest_vm,
532 const struct vm_locked vm_locked,
533 const struct vm_locked primary_vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000534 bool is_el0_partition, struct mpool *ppool)
Daniel Boulbye6df3452022-07-14 18:14:26 +0100535{
536#if LOG_LEVEL >= LOG_LEVEL_WARNING
537 const char *error_string = " region security state ignored for ";
538#endif
539 int j = 0;
540 paddr_t region_begin;
541 paddr_t region_end;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100542 size_t size;
Karl Meakin07a69ab2025-02-07 14:53:19 +0000543 mm_mode_t map_mode;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100544 uint32_t attributes;
545
546 /* Map memory-regions */
547 while (j < manifest_vm->partition.mem_region_count) {
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500548 struct memory_region mem_region;
549
550 mem_region = manifest_vm->partition.mem_regions[j];
551 size = mem_region.page_count * PAGE_SIZE;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100552 /*
J-Alvesd8a1d362023-03-08 11:15:28 +0000553 * Identity map memory region for both case,
554 * VA(S-EL0) or IPA(S-EL1).
Daniel Boulbye6df3452022-07-14 18:14:26 +0100555 */
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500556 region_begin = pa_init(mem_region.base_address);
J-Alvesd8a1d362023-03-08 11:15:28 +0000557 region_end = pa_add(region_begin, size);
Daniel Boulbye6df3452022-07-14 18:14:26 +0100558
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500559 attributes = mem_region.attributes;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100560 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100561 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100562 dlog_warning("Memory%sVMs\n", error_string);
563 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100564 }
565 }
566
567 map_mode = memory_region_attributes_to_mode(attributes);
568
569 if (is_el0_partition) {
570 map_mode |= MM_MODE_USER | MM_MODE_NG;
571 }
572
573 if (!vm_identity_map(vm_locked, region_begin, region_end,
574 map_mode, ppool, NULL)) {
575 dlog_error(
576 "Unable to map secondary VM "
577 "memory-region.\n");
578 return false;
579 }
580
Madhukar Pappireddy0e57d3d2023-10-11 15:49:05 -0500581 /*
582 * Enforce static DMA isolation through stage 2 address
583 * translation.
584 * Only the DMA device that is specified as part of this memory
585 * region node in the partition manifest will be granted access
586 * to the memory region.
587 */
588 if (mem_region.dma_prop.stream_count > 0 &&
589 !vm_iommu_mm_identity_map(
590 vm_locked, region_begin, region_end, map_mode,
591 ppool, NULL, mem_region.dma_prop.dma_device_id)) {
592 dlog_error(
593 "Unable to map memory-region in the page "
594 "tables of DMA device.\n");
595 return false;
596 }
597
Daniel Boulbye6df3452022-07-14 18:14:26 +0100598 /* Deny the primary VM access to this memory */
599 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
600 ppool)) {
601 dlog_error(
602 "Unable to unmap secondary VM memory-"
603 "region from primary VM.\n");
604 return false;
605 }
606
Karl Meakine8937d92024-03-19 16:04:25 +0000607 dlog_verbose("Memory region %#lx - %#lx allocated.\n",
608 pa_addr(region_begin), pa_addr(region_end));
Daniel Boulbye6df3452022-07-14 18:14:26 +0100609
610 j++;
611 }
612
613 /* Map device-regions */
614 j = 0;
615 while (j < manifest_vm->partition.dev_region_count) {
616 region_begin = pa_init(
617 manifest_vm->partition.dev_regions[j].base_address);
618 size = manifest_vm->partition.dev_regions[j].page_count *
619 PAGE_SIZE;
620 region_end = pa_add(region_begin, size);
621
622 attributes = manifest_vm->partition.dev_regions[j].attributes;
623 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
J-Alves661e1b72023-08-02 13:39:40 +0100624 if (ffa_is_vm_id(vm_locked.vm->id)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100625 dlog_warning("Device%sVMs\n", error_string);
626 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100627 }
628 }
629
630 map_mode = device_region_attributes_to_mode(attributes);
631 if (is_el0_partition) {
632 map_mode |= MM_MODE_USER | MM_MODE_NG;
633 }
634
635 if (!vm_identity_map(vm_locked, region_begin, region_end,
636 map_mode, ppool, NULL)) {
637 dlog_error(
638 "Unable to map secondary VM "
639 "device-region.\n");
640 return false;
641 }
642 /* Deny primary VM access to this region */
643 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
644 ppool)) {
645 dlog_error(
646 "Unable to unmap secondary VM device-"
647 "region from primary VM.\n");
648 return false;
649 }
650 j++;
651 }
652 return true;
653}
654
Andrew Scull72b43c02019-09-18 13:53:45 +0100655/*
656 * Loads a secondary VM.
657 */
658static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100659 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100660 paddr_t mem_begin, paddr_t mem_end,
661 const struct manifest_vm *manifest_vm,
J-Alves77b6f4f2023-03-15 11:34:49 +0000662 const struct boot_params *boot_params,
Andrew Scull72b43c02019-09-18 13:53:45 +0100663 const struct memiter *cpio, struct mpool *ppool)
664{
665 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000666 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100667 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100668 struct vcpu *vcpu;
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600669 ipaddr_t partition_primary_ep;
Andrew Scull3c257452019-11-26 13:32:50 +0000670 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100671 paddr_t fdt_addr;
672 bool has_fdt;
673 size_t kernel_size = 0;
674 const size_t mem_size = pa_difference(mem_begin, mem_end);
Karl Meakin07a69ab2025-02-07 14:53:19 +0000675 mm_mode_t map_mode;
Daniel Boulby874d5432023-04-27 12:40:24 +0100676 bool is_el0_partition = manifest_vm->partition.run_time_el == S_EL0 ||
677 manifest_vm->partition.run_time_el == EL0;
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100678 size_t n;
Andrew Scull72b43c02019-09-18 13:53:45 +0100679
Olivier Deprez62d99e32020-01-09 15:58:07 +0100680 /*
681 * Load the kernel if a filename is specified in the VM manifest.
682 * For an FF-A partition, kernel_filename is undefined indicating
683 * the partition package has already been loaded prior to Hafnium
684 * booting.
685 */
686 if (!string_is_empty(&manifest_vm->kernel_filename)) {
687 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100688 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100689 dlog_error("Unable to load kernel.\n");
690 return false;
691 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100692 }
693
Fuad Tabba50469e02020-06-30 15:14:28 +0100694 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
695 if (has_fdt) {
696 /*
697 * Ensure that the FDT does not overwrite the kernel or overlap
698 * its page, for the FDT to start at a page boundary.
699 */
700 const size_t fdt_max_size =
701 mem_size - align_up(kernel_size, PAGE_SIZE);
702
703 size_t fdt_allocated_size;
704
705 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
706 manifest_vm, cpio, ppool, &fdt_addr,
707 &fdt_allocated_size)) {
708 dlog_error("Unable to load FDT.\n");
709 return false;
710 }
711
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700712 if (manifest_vm->is_ffa_partition) {
Karl Meakin117c8082024-12-04 16:03:28 +0000713 ffa_setup_parse_partition_manifest(
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700714 stage1_locked, fdt_addr, fdt_allocated_size,
J-Alves77b6f4f2023-03-15 11:34:49 +0000715 manifest_vm, boot_params, ppool);
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700716 }
717
Fuad Tabba50469e02020-06-30 15:14:28 +0100718 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
719 mem_begin, mem_end, ppool)) {
720 dlog_error("Unable to patch FDT.\n");
721 return false;
722 }
723 }
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800724 /*
725 * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the
726 * FF-A 1.0 spec.
727 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100728 CHECK(!is_el0_partition || manifest_vm->secondary.vcpu_count == 1);
Fuad Tabba50469e02020-06-30 15:14:28 +0100729
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800730 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm,
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600731 is_el0_partition,
732 manifest_vm->partition.dma_device_count)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000733 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100734 return false;
735 }
736
Andrew Scull3c257452019-11-26 13:32:50 +0000737 vm_locked = vm_lock(vm);
738
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800739 /*
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700740 * Grant the VM access to the memory. For VM's we mark all memory in
741 * stage-2 tables as RWX and the VM can control permissions using
742 * stage-1 translations. For S-EL0 partitions, hafnium maps the entire
743 * region of memory for the partition as RX. The partition is then
744 * expected to perform its owns relocations and call the FFA_MEM_PERM_*
745 * API's to change permissions on its image layout.
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800746 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100747 if (is_el0_partition) {
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700748 map_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG;
749 } else {
750 map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800751 }
Raghu Krishnamurthy2b801692021-07-18 17:46:43 -0700752
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800753 if (!vm_identity_map(vm_locked, mem_begin, mem_end, map_mode, ppool,
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600754 &partition_primary_ep)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000755 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000756 ret = false;
757 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100758 }
759
Olivier Deprez62d99e32020-01-09 15:58:07 +0100760 if (manifest_vm->is_ffa_partition) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100761 if (!ffa_map_memory_regions(manifest_vm, vm_locked,
J-Alvesd8a1d362023-03-08 11:15:28 +0000762 primary_vm_locked, is_el0_partition,
763 ppool)) {
Daniel Boulbye6df3452022-07-14 18:14:26 +0100764 ret = false;
765 goto out;
Manish Pandey2145c212020-05-01 16:04:22 +0100766 }
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600767 partition_primary_ep = ipa_add(
768 partition_primary_ep, manifest_vm->partition.ep_offset);
Manish Pandey2145c212020-05-01 16:04:22 +0100769
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600770 /*
771 * If MP endpoints dont specify the entrypoint for secondary
772 * execution contexts, FF-A spec advises SPMC to reuse the
773 * entrypoint computed for primary execution context.
774 */
775 vm->secondary_ep = partition_primary_ep;
Olivier Deprez62d99e32020-01-09 15:58:07 +0100776 }
777
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800778 /*
779 * Map hypervisor into the VM's page table. The hypervisor pages will
780 * not be accessible from EL0 since it will not be marked for user
781 * access.
782 * TODO: Map only the exception vectors and data that exception vectors
783 * require and not the entire hypervisor. This helps with speculative
784 * side-channel attacks.
785 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100786 if (is_el0_partition) {
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800787 CHECK(vm_identity_map(vm_locked, layout_text_begin(),
788 layout_text_end(), MM_MODE_X, ppool,
789 NULL));
790
791 CHECK(vm_identity_map(vm_locked, layout_rodata_begin(),
792 layout_rodata_end(), MM_MODE_R, ppool,
793 NULL));
794
795 CHECK(vm_identity_map(vm_locked, layout_data_begin(),
796 layout_data_end(), MM_MODE_R | MM_MODE_W,
797 ppool, NULL));
Maksims Svecovs134b8f92022-03-04 15:14:09 +0000798
799 CHECK(arch_stack_mm_init(mm_lock_ptable_unsafe(&vm->ptable),
800 ppool));
801
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800802 plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool);
803 }
804
Manish Pandeyd34f8892020-06-19 17:41:07 +0100805 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
806 ret = false;
807 goto out;
808 }
809
Karl Meakine8937d92024-03-19 16:04:25 +0000810 dlog_info("Loaded with %u vCPUs, entry at %#lx.\n",
Manish Pandey2145c212020-05-01 16:04:22 +0100811 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
812
Andrew Scull72b43c02019-09-18 13:53:45 +0100813 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100814
Max Shvetsov40108e72020-08-27 12:39:50 +0100815 vcpu_locked = vcpu_lock(vcpu);
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500816
Fuad Tabba50469e02020-06-30 15:14:28 +0100817 if (has_fdt) {
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600818 vcpu_secondary_reset_and_start(
819 vcpu_locked, partition_primary_ep, pa_addr(fdt_addr));
Fuad Tabba50469e02020-06-30 15:14:28 +0100820 } else {
821 /*
822 * Without an FDT, secondary VMs expect the memory size to be
823 * passed in register x0, which is what
824 * vcpu_secondary_reset_and_start does in this case.
825 */
Madhukar Pappireddy958c8412024-11-25 09:54:17 -0600826 vcpu_secondary_reset_and_start(vcpu_locked,
827 partition_primary_ep, mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100828 }
829
Max Shvetsov40108e72020-08-27 12:39:50 +0100830 vcpu_unlock(&vcpu_locked);
831
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100832 /*
833 * For all vCPUs,
834 * in a VM: enable the notification pending virtual interrupt if
835 * requested in the manifest.
836 * in a SP: enable the NPI and managed exit virtual interrupts if
837 * requested in the manifest. For a S-EL0 partition, enable
838 * the virtual interrupts IDs matching the secure physical
839 * interrupt IDs declared in device regions.
840 */
841 for (n = 0; n < manifest_vm->secondary.vcpu_count; n++) {
842 vcpu = vm_get_vcpu(vm, n);
843 vcpu_locked = vcpu_lock(vcpu);
Karl Meakin117c8082024-12-04 16:03:28 +0000844 ffa_interrupts_enable_virtual_interrupts(vcpu_locked,
845 vm_locked);
Jens Wiklanderb697ad02022-11-04 10:15:03 +0100846 vcpu_unlock(&vcpu_locked);
847 }
848
Andrew Scull3c257452019-11-26 13:32:50 +0000849 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100850
Andrew Scull3c257452019-11-26 13:32:50 +0000851out:
852 vm_unlock(&vm_locked);
853
854 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100855}
856
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100857/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100858 * Try to find a memory range of the given size within the given ranges, and
859 * remove it from them. Return true on success, or false if no large enough
860 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100861 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900862static bool carve_out_mem_range(struct mem_range *mem_ranges,
863 size_t mem_ranges_count, uint64_t size_to_find,
864 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100865{
866 size_t i;
867
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000868 /*
869 * TODO(b/116191358): Consider being cleverer about how we pack VMs
870 * together, with a non-greedy algorithm.
871 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100872 for (i = 0; i < mem_ranges_count; ++i) {
873 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100874 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100875 /*
876 * This range is big enough, take some of it from the
877 * end and reduce its size accordingly.
878 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100879 *found_end = mem_ranges[i].end;
880 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
881 size_to_find);
882 mem_ranges[i].end = *found_begin;
883 return true;
884 }
885 }
886 return false;
887}
888
889/**
890 * Given arrays of memory ranges before and after memory was removed for
891 * secondary VMs, add the difference to the reserved ranges of the given update.
892 * Return true on success, or false if there would be more than MAX_MEM_RANGES
893 * reserved ranges after adding the new ones.
894 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
895 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900896static bool update_reserved_ranges(struct boot_params_update *update,
897 const struct mem_range *before,
898 const struct mem_range *after,
899 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100900{
901 size_t i;
902
903 for (i = 0; i < mem_ranges_count; ++i) {
904 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
905 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000906 dlog_error(
907 "Too many reserved ranges after "
908 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100909 return false;
910 }
911 update->reserved_ranges[update->reserved_ranges_count]
912 .begin = before[i].begin;
913 update->reserved_ranges[update->reserved_ranges_count]
914 .end = after[i].begin;
915 update->reserved_ranges_count++;
916 }
917 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
918 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000919 dlog_error(
920 "Too many reserved ranges after "
921 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100922 return false;
923 }
924 update->reserved_ranges[update->reserved_ranges_count]
925 .begin = after[i].end;
926 update->reserved_ranges[update->reserved_ranges_count]
927 .end = before[i].end;
928 update->reserved_ranges_count++;
929 }
930 }
931
932 return true;
933}
934
Olivier Deprez05046922023-03-09 15:48:40 +0100935static bool init_other_world_vm(const struct boot_params *params,
936 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100937{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200938 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100939 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100940
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100941 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200942 * Initialise the dummy VM which represents the opposite world:
943 * -TrustZone (or the SPMC) when running the Hypervisor
944 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100945 */
Madhukar Pappireddy070f49e2024-01-12 13:02:27 -0600946 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false, 0);
Olivier Deprez96a2a262020-06-11 17:21:38 +0200947 CHECK(other_world_vm != NULL);
948
949 for (i = 0; i < MAX_CPUS; i++) {
950 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
951 struct cpu *cpu = cpu_find_index(i);
952
953 vcpu->cpu = cpu;
954 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100955
Olivier Deprez05046922023-03-09 15:48:40 +0100956 return arch_other_world_vm_init(other_world_vm, params, ppool);
Olivier Deprez112d2b52020-09-30 07:39:23 +0200957}
958
959/*
960 * Loads alls VMs from the manifest.
961 */
962bool load_vms(struct mm_stage1_locked stage1_locked,
963 const struct manifest *manifest, const struct memiter *cpio,
964 const struct boot_params *params,
965 struct boot_params_update *update, struct mpool *ppool)
966{
967 struct vm *primary;
968 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
969 struct vm_locked primary_vm_locked;
Olivier Deprez112d2b52020-09-30 07:39:23 +0200970 bool success = true;
971
Andrew Walbranf8716782020-10-29 17:07:07 +0000972 /**
973 * Only try to load the primary VM if it is supposed to be in this
974 * world.
975 */
976 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
977 if (!load_primary(stage1_locked,
978 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
979 params, ppool)) {
980 dlog_error("Unable to load primary VM.\n");
981 return false;
982 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200983 }
984
Olivier Deprez05046922023-03-09 15:48:40 +0100985 if (!init_other_world_vm(params, ppool)) {
Olivier Deprez112d2b52020-09-30 07:39:23 +0200986 return false;
987 }
988
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100989 static_assert(
990 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
991 "mem_range arrays must be the same size for memcpy.");
992 static_assert(sizeof(mem_ranges_available) < 500,
993 "This will use too much stack, either make "
994 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100995 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
996 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100997
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100998 /* Round the last addresses down to the page size. */
J-Alves217e1a22025-03-20 19:19:47 +0000999 for (size_t i = 0UL; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001000 mem_ranges_available[i].end = pa_init(align_down(
1001 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001002 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001003
Andrew Scull3c257452019-11-26 13:32:50 +00001004 primary = vm_find(HF_PRIMARY_VM_ID);
1005 primary_vm_locked = vm_lock(primary);
1006
J-Alves217e1a22025-03-20 19:19:47 +00001007 for (size_t i = 0UL; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +01001008 const struct manifest_vm *manifest_vm = &manifest->vm[i];
J-Alves217e1a22025-03-20 19:19:47 +00001009 ffa_id_t vm_id = (ffa_id_t)(HF_VM_ID_OFFSET + i);
David Brazdil7a462ec2019-08-15 12:27:47 +01001010 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +01001011 paddr_t secondary_mem_begin;
1012 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001013
David Brazdil7a462ec2019-08-15 12:27:47 +01001014 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001015 continue;
1016 }
1017
Olivier Deprez2a8ee342020-08-03 15:10:44 +02001018 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Karl Meakine8937d92024-03-19 16:04:25 +00001019 manifest_vm->debug_name.data);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001020
David Brazdil7a462ec2019-08-15 12:27:47 +01001021 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001022
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -07001023 if (manifest_vm->is_ffa_partition &&
1024 !manifest->vm[i].is_hyp_loaded) {
Olivier Deprez62d99e32020-01-09 15:58:07 +01001025 secondary_mem_begin =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001026 pa_init(manifest_vm->partition.load_addr);
1027 secondary_mem_end = pa_init(
1028 manifest_vm->partition.load_addr + mem_size);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001029 } else if (!carve_out_mem_range(mem_ranges_available,
1030 params->mem_ranges_count,
1031 mem_size, &secondary_mem_begin,
1032 &secondary_mem_end)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001033 dlog_error("Not enough memory (%lu bytes).\n",
1034 mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001035 continue;
1036 }
Andrew Scull80871322018-08-06 12:04:09 +01001037
Manish Pandey2145c212020-05-01 16:04:22 +01001038 if (!load_secondary(stage1_locked, primary_vm_locked,
1039 secondary_mem_begin, secondary_mem_end,
J-Alves77b6f4f2023-03-15 11:34:49 +00001040 manifest_vm, params, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001041 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001042 continue;
1043 }
1044
1045 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +00001046 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
1047 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001048 dlog_error(
1049 "Unable to unmap secondary VM from primary "
1050 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +00001051 success = false;
1052 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001053 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001054 }
1055
Andrew Scull3c257452019-11-26 13:32:50 +00001056 vm_unlock(&primary_vm_locked);
1057
1058 if (!success) {
1059 return false;
1060 }
1061
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001062 /*
1063 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001064 * difference between the available ranges from the original params and
1065 * the updated mem_ranges_available. We assume that the number and order
1066 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001067 * above only make them smaller.
1068 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001069 return update_reserved_ranges(update, params->mem_ranges,
1070 mem_ranges_available,
1071 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001072}