blob: b815e1e5ffc000e72b07033d30d274203cb5daaa [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/load.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010010
11#include <stdbool.h>
12
Maksims Svecovs134b8f92022-03-04 15:14:09 +000013#include "hf/arch/init.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
J-Alvesa9c7cba2021-08-25 16:26:11 +010015#include "hf/arch/plat/ffa.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000016#include "hf/arch/vm.h"
17
Andrew Scull18c78fc2018-08-20 12:57:41 +010018#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010019#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010020#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/dlog.h"
Fuad Tabba50469e02020-06-30 15:14:28 +010022#include "hf/fdt_patch.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010023#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/memiter.h"
25#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010026#include "hf/plat/console.h"
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -050027#include "hf/plat/interrupts.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000028#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010029#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010030#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010031#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010032
Andrew Scull19503262018-09-20 14:48:39 +010033#include "vmapi/hf/call.h"
Manish Pandeyd34f8892020-06-19 17:41:07 +010034#include "vmapi/hf/ffa.h"
Andrew Scull19503262018-09-20 14:48:39 +010035
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010036/**
37 * Copies data to an unmapped location by mapping it for write, copying the
38 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000039 *
40 * The data is written so that it is available to all cores with the cache
41 * disabled. When switching to the partitions, the caching is initially disabled
42 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010043 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010044static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010045 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010046{
David Brazdil7a462ec2019-08-15 12:27:47 +010047 const void *from = memiter_base(from_it);
48 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010049 paddr_t to_end = pa_add(to, size);
50 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010051
Andrew Scull3c0a90a2019-07-01 11:55:53 +010052 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010053 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010054 return false;
55 }
56
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010057 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010058 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010059
Andrew Scull72b43c02019-09-18 13:53:45 +010060 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010061
62 return true;
63}
64
Fuad Tabba50469e02020-06-30 15:14:28 +010065/**
66 * Loads the secondary VM's kernel.
67 * Stores the kernel size in kernel_size (if kernel_size is not NULL).
68 * Returns false if it cannot load the kernel.
69 */
Andrew Scull72b43c02019-09-18 13:53:45 +010070static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
71 paddr_t end, const struct manifest_vm *manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +010072 const struct memiter *cpio, struct mpool *ppool,
73 size_t *kernel_size)
Andrew Scull72b43c02019-09-18 13:53:45 +010074{
Andrew Scull72b43c02019-09-18 13:53:45 +010075 struct memiter kernel;
Fuad Tabba50469e02020-06-30 15:14:28 +010076 size_t size;
Andrew Scull72b43c02019-09-18 13:53:45 +010077
David Brazdil136f2942019-09-23 14:11:03 +010078 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000079 dlog_error("Could not find kernel file \"%s\".\n",
80 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010081 return false;
82 }
83
Fuad Tabba50469e02020-06-30 15:14:28 +010084 size = memiter_size(&kernel);
85 if (pa_difference(begin, end) < size) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000086 dlog_error("Kernel is larger than available memory.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010087 return false;
88 }
89
90 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000091 dlog_error("Unable to copy kernel.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010092 return false;
93 }
94
Fuad Tabba50469e02020-06-30 15:14:28 +010095 if (kernel_size) {
96 *kernel_size = size;
97 }
98
Andrew Scull72b43c02019-09-18 13:53:45 +010099 return true;
100}
101
Manish Pandeyd34f8892020-06-19 17:41:07 +0100102/*
103 * Link RX/TX buffers provided in partition manifest to mailbox
104 */
105static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked,
106 struct vm_locked vm_locked, struct rx_tx rxtx,
107 struct mpool *ppool)
108{
109 struct ffa_value ret;
110 ipaddr_t send;
111 ipaddr_t recv;
112 uint32_t page_count;
113
114 send = ipa_init(rxtx.tx_buffer->base_address);
115 recv = ipa_init(rxtx.rx_buffer->base_address);
116 page_count = rxtx.tx_buffer->page_count;
117
118 ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv,
119 page_count, ppool);
120 if (ret.func != FFA_SUCCESS_32) {
121 return false;
122 }
123
124 dlog_verbose(" mailbox: send = %#x, recv = %#x\n",
125 vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv);
126
127 return true;
128}
129
Raghu Krishnamurthyad38a9c2022-07-20 07:30:36 -0700130static void infer_interrupt(struct interrupt_info interrupt,
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500131 struct interrupt_descriptor *int_desc)
132{
133 uint32_t attr = interrupt.attributes;
134
135 interrupt_desc_set_id(int_desc, interrupt.id);
136 interrupt_desc_set_priority(int_desc,
137 (attr >> INT_DESC_PRIORITY_SHIFT) & 0xff);
138
139 /* Refer to the comments in interrupt_descriptor struct definition. */
140 interrupt_desc_set_type_config_sec_state(
141 int_desc,
142 (((attr >> INT_DESC_TYPE_SHIFT) & 0x3) << 2) |
143 (((attr >> INT_DESC_CONFIG_SHIFT) & 0x1) << 1) |
144 ((attr >> INT_DESC_SEC_STATE_SHIFT) & 0x1));
145
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700146 if (interrupt.mpidr_valid) {
147 interrupt_desc_set_mpidr(int_desc, interrupt.mpidr);
148 } else {
149 interrupt_desc_set_mpidr_invalid(int_desc);
150 }
151
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500152 interrupt_desc_set_valid(int_desc, true);
153}
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;
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700169 vm_locked.vm->uuid = manifest_vm->partition.uuid;
Andrew Scullae9962e2019-10-03 16:51:16 +0100170
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500171 /* Populate the interrupt descriptor for current VM. */
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700172 for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) {
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500173 dev_region = manifest_vm->partition.dev_regions[i];
174
175 CHECK(dev_region.interrupt_count <=
Raghu Krishnamurthy641dcd82022-07-19 23:21:20 -0700176 PARTITION_MAX_INTERRUPTS_PER_DEVICE);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500177
178 for (uint8_t j = 0; j < dev_region.interrupt_count; j++) {
Raghu Krishnamurthy98da1ca2022-10-04 08:59:01 -0700179 struct interrupt_descriptor int_desc = {0};
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500180
181 interrupt = dev_region.interrupts[j];
182 infer_interrupt(interrupt, &int_desc);
183 vm_locked.vm->interrupt_desc[k] = int_desc;
184
Madhukar Pappireddy72454a12021-08-03 12:21:46 -0500185 /*
186 * Configure the physical interrupts allocated for this
187 * VM in its partition manifest.
188 */
189 plat_interrupts_configure_interrupt(int_desc);
Madhukar Pappireddy5fc8be12021-08-03 11:42:53 -0500190 k++;
191 CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS);
192 }
193 }
194 dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k);
195
Manish Pandeyd34f8892020-06-19 17:41:07 +0100196 if (manifest_vm->is_ffa_partition) {
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +0000197 vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100198 /* Link rxtx buffers to mailbox */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700199 if (manifest_vm->partition.rxtx.available) {
Manish Pandeyd34f8892020-06-19 17:41:07 +0100200 if (!link_rxtx_to_mailbox(stage1_locked, vm_locked,
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700201 manifest_vm->partition.rxtx,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100202 ppool)) {
203 dlog_error(
204 "Unable to Link RX/TX buffer with "
205 "mailbox.\n");
206 return false;
207 }
208 }
J-Alvesb37fd082020-10-22 12:29:21 +0100209
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100210 vm_locked.vm->messaging_method =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700211 manifest_vm->partition.messaging_method;
Manish Pandeyf3be6392020-09-24 17:26:09 +0100212
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500213 vm_locked.vm->ns_interrupts_action =
214 manifest_vm->partition.ns_interrupts_action;
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100215
Madhukar Pappireddy5c04a382022-12-28 11:29:26 -0600216 vm_locked.vm->other_s_interrupts_action =
217 manifest_vm->partition.other_s_interrupts_action;
218
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500219 vm_locked.vm->me_signal_virq =
220 manifest_vm->partition.me_signal_virq;
221
J-Alvesa4730db2021-11-02 10:31:01 +0000222 vm_locked.vm->notifications.enabled =
223 manifest_vm->partition.notification_support;
224
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700225 vm_locked.vm->boot_order = manifest_vm->partition.boot_order;
226
J-Alves7d38f7b2022-04-13 13:22:30 +0100227 vm_locked.vm->boot_info.gp_register_num =
228 manifest_vm->partition.gp_register_num;
229
230 if (manifest_vm->partition.boot_info) {
231 /*
232 * If the partition expects the boot information blob
233 * per the ff-a v1.1 boot protocol, then its address
234 * shall match the partition's load address.
235 */
236 vm_locked.vm->boot_info.blob_addr =
237 ipa_init(manifest_vm->partition.load_addr);
238 }
239
J-Alvesb37fd082020-10-22 12:29:21 +0100240 /* Updating boot list according to boot_order */
241 vm_update_boot(vm_locked.vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100242
J-Alves09ff9d82021-11-02 11:55:20 +0000243 if (vm_locked_are_notifications_enabled(vm_locked) &&
J-Alvesa4730db2021-11-02 10:31:01 +0000244 !plat_ffa_notifications_bitmap_create_call(
245 vm_locked.vm->id, vm_locked.vm->vcpu_count)) {
246 return false;
J-Alvesa9c7cba2021-08-25 16:26:11 +0100247 }
Manish Pandeyd34f8892020-06-19 17:41:07 +0100248 }
J-Alvesb37fd082020-10-22 12:29:21 +0100249
Fuad Tabba56970712020-01-10 11:20:09 +0000250 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100251 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000252
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500253 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
254 ppool)) {
255 dlog_error("Unable to attach upstream peripheral device\n");
256 return false;
257 }
258
Andrew Scullae9962e2019-10-03 16:51:16 +0100259 return true;
260}
261
262/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100263 * Loads the primary VM.
264 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100265static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100266 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100267 const struct memiter *cpio,
268 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100270 paddr_t primary_begin;
271 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100272 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000273 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100274 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100275 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000276 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100277
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700278 if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) {
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700279 primary_begin = pa_init(manifest_vm->partition.load_addr);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100280 primary_entry = ipa_add(ipa_from_pa(primary_begin),
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700281 manifest_vm->partition.ep_offset);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100282 } else {
283 primary_begin =
284 (manifest_vm->primary.boot_address ==
285 MANIFEST_INVALID_ADDRESS)
286 ? layout_primary_begin()
287 : pa_init(manifest_vm->primary.boot_address);
288 primary_entry = ipa_from_pa(primary_begin);
289 }
290
David Brazdil080ee312020-02-25 15:30:30 -0800291 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100292
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800293 /* Primary VM must be a VM */
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -0700294 CHECK(manifest_vm->partition.run_time_el == EL1);
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800295
Olivier Deprez62d99e32020-01-09 15:58:07 +0100296 /*
297 * Load the kernel if a filename is specified in the VM manifest.
298 * For an FF-A partition, kernel_filename is undefined indicating
299 * the partition package has already been loaded prior to Hafnium
300 * booting.
301 */
302 if (!string_is_empty(&manifest_vm->kernel_filename)) {
303 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100304 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100305 dlog_error("Unable to load primary kernel.\n");
306 return false;
307 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100308 }
309
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800310 if (!vm_init_next(MAX_CPUS, ppool, &vm, false)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000311 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100312 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100313 }
314
David Brazdile6f83222019-09-23 14:47:37 +0100315 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000316 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100317 return false;
318 }
319
Andrew Scull3c257452019-11-26 13:32:50 +0000320 vm_locked = vm_lock(vm);
321
Andrew Scull48929fd2020-01-28 10:39:10 +0000322 if (params->device_mem_ranges_count == 0) {
323 /*
324 * Map 1TB of address space as device memory to, most likely,
325 * make all devices available to the primary VM.
326 *
327 * TODO: remove this once all targets provide valid ranges.
328 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800329 dlog_warning(
330 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000331
332 if (!vm_identity_map(
333 vm_locked, pa_init(0),
334 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
335 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
336 dlog_error(
337 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800338 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000339 ret = false;
340 goto out;
341 }
David Brazdile6f83222019-09-23 14:47:37 +0100342 }
343
Andrew Scullb5f49e02019-10-02 13:20:47 +0100344 /* Map normal memory as such to permit caching, execution, etc. */
345 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000346 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
347 params->mem_ranges[i].end,
348 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
349 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000350 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800351 "Unable to initialise memory for primary "
352 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000353 ret = false;
354 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100355 }
356 }
357
Andrew Scull48929fd2020-01-28 10:39:10 +0000358 /* Map device memory as such to prevent execution, speculation etc. */
359 for (i = 0; i < params->device_mem_ranges_count; ++i) {
360 if (!vm_identity_map(
361 vm_locked, params->device_mem_ranges[i].begin,
362 params->device_mem_ranges[i].end,
363 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
364 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800365 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000366 ret = false;
367 goto out;
368 }
369 }
370
Manish Pandeyd34f8892020-06-19 17:41:07 +0100371 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
372 ret = false;
373 goto out;
374 }
375
Andrew Scull3c257452019-11-26 13:32:50 +0000376 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800377 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000378 ret = false;
379 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100380 }
381
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000382 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800383 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000384 ret = false;
385 goto out;
386 }
387
Andrew Walbran7586e042020-02-18 18:19:26 +0000388 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
389 vm->vcpu_count, pa_addr(primary_begin));
390
Olivier Deprezb9adff42021-02-01 12:14:05 +0100391 /* Mark the primary to be the first booted VM */
392 vm_update_boot(vm);
393
David Brazdile6f83222019-09-23 14:47:37 +0100394 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100395 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100396 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000397 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100398
Andrew Scull3c257452019-11-26 13:32:50 +0000399out:
400 vm_unlock(&vm_locked);
401
402 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100403}
404
Fuad Tabba50469e02020-06-30 15:14:28 +0100405/**
406 * Loads the secondary VM's FDT.
407 * Stores the total allocated size for the FDT in fdt_allocated_size (if
408 * fdt_allocated_size is not NULL). The allocated size includes additional space
409 * for potential patching.
410 */
411static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
412 paddr_t end, size_t fdt_max_size,
413 const struct manifest_vm *manifest_vm,
414 const struct memiter *cpio, struct mpool *ppool,
415 paddr_t *fdt_addr, size_t *fdt_allocated_size)
416{
417 struct memiter fdt;
418 size_t allocated_size;
419
420 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
421
422 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
423 dlog_error("Cannot open the secondary VM's FDT.\n");
424 return false;
425 }
426
427 /*
Olivier Deprez701e8bf2022-04-06 18:45:18 +0200428 * Ensure the FDT has one additional page at the end for patching,
Fuad Tabba50469e02020-06-30 15:14:28 +0100429 * and align it to the page boundary.
430 */
431 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
432
433 if (allocated_size > fdt_max_size) {
434 dlog_error(
435 "FDT allocated space (%u) is more than the specified "
436 "maximum to use (%u).\n",
437 allocated_size, fdt_max_size);
438 return false;
439 }
440
441 /* Load the FDT to the end of the VM's allocated memory space. */
442 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
443
444 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
445 allocated_size, pa_addr(*fdt_addr));
446
447 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
448 dlog_error("Unable to copy FDT.\n");
449 return false;
450 }
451
452 if (fdt_allocated_size) {
453 *fdt_allocated_size = allocated_size;
454 }
455
456 return true;
457}
458
Olivier Deprez035fa152022-03-14 11:19:10 +0100459/**
460 * Convert the manifest memory region attributes to mode consumed by mm layer.
461 */
462static uint32_t memory_region_attributes_to_mode(uint32_t attributes)
463{
464 uint32_t mode = 0U;
465
466 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
467 mode |= MM_MODE_R;
468 }
469
470 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
471 mode |= MM_MODE_W;
472 }
473
474 if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) {
475 mode |= MM_MODE_X;
476 }
477
478 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) ||
479 (mode == (MM_MODE_R | MM_MODE_X)));
480
481 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
482 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
483 }
484
485 return mode;
486}
487
488/**
489 * Convert the manifest device region attributes to mode consumed by mm layer.
490 */
491static uint32_t device_region_attributes_to_mode(uint32_t attributes)
492{
493 uint32_t mode = 0U;
494
495 if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) {
496 mode |= MM_MODE_R;
497 }
498
499 if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) {
500 mode |= MM_MODE_W;
501 }
502
503 assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R));
504
505 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) {
506 mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID);
507 }
508
509 return mode | MM_MODE_D;
510}
511
Daniel Boulbye6df3452022-07-14 18:14:26 +0100512static bool ffa_map_memory_regions(const struct manifest_vm *manifest_vm,
513 const struct vm_locked vm_locked,
514 const struct vm_locked primary_vm_locked,
515 paddr_t mem_end, bool is_el0_partition,
516 struct mpool *ppool)
517{
518#if LOG_LEVEL >= LOG_LEVEL_WARNING
519 const char *error_string = " region security state ignored for ";
520#endif
521 int j = 0;
522 paddr_t region_begin;
523 paddr_t region_end;
524 paddr_t alloc_base = mem_end;
525 size_t size;
526 size_t total_alloc = 0;
527 uint32_t map_mode;
528 uint32_t attributes;
529
530 /* Map memory-regions */
531 while (j < manifest_vm->partition.mem_region_count) {
532 size = manifest_vm->partition.mem_regions[j].page_count *
533 PAGE_SIZE;
534 /*
535 * For memory-regions without base-address, memory
536 * should be allocated inside partition's page table.
537 * Start allocating memory regions in partition's
538 * page table, starting from the end.
539 * TODO: Add mechanism to let partition know of these
540 * memory regions
541 */
542 if (manifest_vm->partition.mem_regions[j].base_address ==
543 MANIFEST_INVALID_ADDRESS) {
544 total_alloc += size;
545 /* Don't go beyond half the VM's memory space */
546 if (total_alloc >
547 (manifest_vm->secondary.mem_size / 2)) {
548 dlog_error(
549 "Not enough space for memory-"
550 "region allocation");
551 return false;
552 }
553
554 region_end = alloc_base;
555 region_begin = pa_subtract(alloc_base, size);
556 alloc_base = region_begin;
557 } else {
558 /*
559 * Identity map memory region for both case,
560 * VA(S-EL0) or IPA(S-EL1).
561 */
562 region_begin =
563 pa_init(manifest_vm->partition.mem_regions[j]
564 .base_address);
565 region_end = pa_add(region_begin, size);
566 }
567
568 attributes = manifest_vm->partition.mem_regions[j].attributes;
569 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
570 if (plat_ffa_is_vm_id(vm_locked.vm->id)) {
571 dlog_warning("Memory%sVMs\n", error_string);
572 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
573
574 } else if (!is_el0_partition) {
575 dlog_warning(
576 "Memory%sS-EL1 "
577 "partitions.\n",
578 error_string);
579 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
580 }
581 }
582
583 map_mode = memory_region_attributes_to_mode(attributes);
584
585 if (is_el0_partition) {
586 map_mode |= MM_MODE_USER | MM_MODE_NG;
587 }
588
589 if (!vm_identity_map(vm_locked, region_begin, region_end,
590 map_mode, ppool, NULL)) {
591 dlog_error(
592 "Unable to map secondary VM "
593 "memory-region.\n");
594 return false;
595 }
596
597 /* Deny the primary VM access to this memory */
598 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
599 ppool)) {
600 dlog_error(
601 "Unable to unmap secondary VM memory-"
602 "region from primary VM.\n");
603 return false;
604 }
605
606 dlog_verbose("Memory region %#x - %#x allocated.\n",
607 region_begin, region_end);
608
609 j++;
610 }
611
612 /* Map device-regions */
613 j = 0;
614 while (j < manifest_vm->partition.dev_region_count) {
615 region_begin = pa_init(
616 manifest_vm->partition.dev_regions[j].base_address);
617 size = manifest_vm->partition.dev_regions[j].page_count *
618 PAGE_SIZE;
619 region_end = pa_add(region_begin, size);
620
621 attributes = manifest_vm->partition.dev_regions[j].attributes;
622 if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) {
623 if (plat_ffa_is_vm_id(vm_locked.vm->id)) {
624 dlog_warning("Device%sVMs\n", error_string);
625 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
626 } else if (!is_el0_partition) {
627 dlog_warning(
628 "Device%sS-EL1 "
629 "partitions.\n",
630 error_string);
631 attributes &= ~MANIFEST_REGION_ATTR_SECURITY;
632 }
633 }
634
635 map_mode = device_region_attributes_to_mode(attributes);
636 if (is_el0_partition) {
637 map_mode |= MM_MODE_USER | MM_MODE_NG;
638 }
639
640 if (!vm_identity_map(vm_locked, region_begin, region_end,
641 map_mode, ppool, NULL)) {
642 dlog_error(
643 "Unable to map secondary VM "
644 "device-region.\n");
645 return false;
646 }
647 /* Deny primary VM access to this region */
648 if (!vm_unmap(primary_vm_locked, region_begin, region_end,
649 ppool)) {
650 dlog_error(
651 "Unable to unmap secondary VM device-"
652 "region from primary VM.\n");
653 return false;
654 }
655 j++;
656 }
657 return true;
658}
659
Andrew Scull72b43c02019-09-18 13:53:45 +0100660/*
661 * Loads a secondary VM.
662 */
663static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100664 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100665 paddr_t mem_begin, paddr_t mem_end,
666 const struct manifest_vm *manifest_vm,
667 const struct memiter *cpio, struct mpool *ppool)
668{
669 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000670 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100671 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100672 struct vcpu *vcpu;
673 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000674 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100675 paddr_t fdt_addr;
676 bool has_fdt;
677 size_t kernel_size = 0;
678 const size_t mem_size = pa_difference(mem_begin, mem_end);
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800679 uint32_t map_mode;
Daniel Boulbye6df3452022-07-14 18:14:26 +0100680 bool is_el0_partition = manifest_vm->partition.run_time_el == S_EL0;
Andrew Scull72b43c02019-09-18 13:53:45 +0100681
Olivier Deprez62d99e32020-01-09 15:58:07 +0100682 /*
683 * Load the kernel if a filename is specified in the VM manifest.
684 * For an FF-A partition, kernel_filename is undefined indicating
685 * the partition package has already been loaded prior to Hafnium
686 * booting.
687 */
688 if (!string_is_empty(&manifest_vm->kernel_filename)) {
689 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100690 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100691 dlog_error("Unable to load kernel.\n");
692 return false;
693 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100694 }
695
Fuad Tabba50469e02020-06-30 15:14:28 +0100696 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
697 if (has_fdt) {
698 /*
699 * Ensure that the FDT does not overwrite the kernel or overlap
700 * its page, for the FDT to start at a page boundary.
701 */
702 const size_t fdt_max_size =
703 mem_size - align_up(kernel_size, PAGE_SIZE);
704
705 size_t fdt_allocated_size;
706
707 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
708 manifest_vm, cpio, ppool, &fdt_addr,
709 &fdt_allocated_size)) {
710 dlog_error("Unable to load FDT.\n");
711 return false;
712 }
713
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -0700714 if (manifest_vm->is_ffa_partition) {
715 plat_ffa_parse_partition_manifest(
716 stage1_locked, fdt_addr, fdt_allocated_size,
717 manifest_vm, ppool);
718 }
719
Fuad Tabba50469e02020-06-30 15:14:28 +0100720 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
721 mem_begin, mem_end, ppool)) {
722 dlog_error("Unable to patch FDT.\n");
723 return false;
724 }
725 }
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800726 /*
727 * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the
728 * FF-A 1.0 spec.
729 */
Daniel Boulbye6df3452022-07-14 18:14:26 +0100730 CHECK(!is_el0_partition || manifest_vm->secondary.vcpu_count == 1);
Fuad Tabba50469e02020-06-30 15:14:28 +0100731
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800732 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm,
Daniel Boulbye6df3452022-07-14 18:14:26 +0100733 is_el0_partition)) {
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,
763 primary_vm_locked, mem_end,
764 is_el0_partition, ppool)) {
765 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
Manish Pandey2145c212020-05-01 16:04:22 +0100805 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
806 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
Madhukar Pappireddy486360d2022-09-06 15:32:24 -0500812 /* Enable relevant virtual interrupts for Secure Partitions. */
813 plat_ffa_enable_virtual_interrupts(vcpu_locked, vm_locked);
Madhukar Pappireddy046dad02022-06-21 18:43:33 -0500814
Fuad Tabba50469e02020-06-30 15:14:28 +0100815 if (has_fdt) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100816 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
Fuad Tabba50469e02020-06-30 15:14:28 +0100817 pa_addr(fdt_addr));
818 } else {
819 /*
820 * Without an FDT, secondary VMs expect the memory size to be
821 * passed in register x0, which is what
822 * vcpu_secondary_reset_and_start does in this case.
823 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100824 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
825 mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100826 }
827
Max Shvetsov40108e72020-08-27 12:39:50 +0100828 vcpu_unlock(&vcpu_locked);
829
Andrew Scull3c257452019-11-26 13:32:50 +0000830 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100831
Andrew Scull3c257452019-11-26 13:32:50 +0000832out:
833 vm_unlock(&vm_locked);
834
835 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100836}
837
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100838/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100839 * Try to find a memory range of the given size within the given ranges, and
840 * remove it from them. Return true on success, or false if no large enough
841 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100842 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900843static bool carve_out_mem_range(struct mem_range *mem_ranges,
844 size_t mem_ranges_count, uint64_t size_to_find,
845 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100846{
847 size_t i;
848
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000849 /*
850 * TODO(b/116191358): Consider being cleverer about how we pack VMs
851 * together, with a non-greedy algorithm.
852 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100853 for (i = 0; i < mem_ranges_count; ++i) {
854 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100855 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100856 /*
857 * This range is big enough, take some of it from the
858 * end and reduce its size accordingly.
859 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100860 *found_end = mem_ranges[i].end;
861 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
862 size_to_find);
863 mem_ranges[i].end = *found_begin;
864 return true;
865 }
866 }
867 return false;
868}
869
870/**
871 * Given arrays of memory ranges before and after memory was removed for
872 * secondary VMs, add the difference to the reserved ranges of the given update.
873 * Return true on success, or false if there would be more than MAX_MEM_RANGES
874 * reserved ranges after adding the new ones.
875 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
876 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900877static bool update_reserved_ranges(struct boot_params_update *update,
878 const struct mem_range *before,
879 const struct mem_range *after,
880 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100881{
882 size_t i;
883
884 for (i = 0; i < mem_ranges_count; ++i) {
885 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
886 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000887 dlog_error(
888 "Too many reserved ranges after "
889 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100890 return false;
891 }
892 update->reserved_ranges[update->reserved_ranges_count]
893 .begin = before[i].begin;
894 update->reserved_ranges[update->reserved_ranges_count]
895 .end = after[i].begin;
896 update->reserved_ranges_count++;
897 }
898 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
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 = after[i].end;
907 update->reserved_ranges[update->reserved_ranges_count]
908 .end = before[i].end;
909 update->reserved_ranges_count++;
910 }
911 }
912
913 return true;
914}
915
Olivier Deprez112d2b52020-09-30 07:39:23 +0200916static bool init_other_world_vm(struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100917{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200918 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100919 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100920
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100921 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200922 * Initialise the dummy VM which represents the opposite world:
923 * -TrustZone (or the SPMC) when running the Hypervisor
924 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100925 */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800926 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false);
Olivier Deprez96a2a262020-06-11 17:21:38 +0200927 CHECK(other_world_vm != NULL);
928
929 for (i = 0; i < MAX_CPUS; i++) {
930 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
931 struct cpu *cpu = cpu_find_index(i);
932
933 vcpu->cpu = cpu;
934 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100935
Olivier Deprez112d2b52020-09-30 07:39:23 +0200936 return arch_other_world_vm_init(other_world_vm, ppool);
937}
938
939/*
940 * Loads alls VMs from the manifest.
941 */
942bool load_vms(struct mm_stage1_locked stage1_locked,
943 const struct manifest *manifest, const struct memiter *cpio,
944 const struct boot_params *params,
945 struct boot_params_update *update, struct mpool *ppool)
946{
947 struct vm *primary;
948 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
949 struct vm_locked primary_vm_locked;
950 size_t i;
951 bool success = true;
952
Andrew Walbranf8716782020-10-29 17:07:07 +0000953 /**
954 * Only try to load the primary VM if it is supposed to be in this
955 * world.
956 */
957 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
958 if (!load_primary(stage1_locked,
959 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
960 params, ppool)) {
961 dlog_error("Unable to load primary VM.\n");
962 return false;
963 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200964 }
965
966 if (!init_other_world_vm(ppool)) {
967 return false;
968 }
969
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100970 static_assert(
971 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
972 "mem_range arrays must be the same size for memcpy.");
973 static_assert(sizeof(mem_ranges_available) < 500,
974 "This will use too much stack, either make "
975 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100976 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
977 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100978
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100979 /* Round the last addresses down to the page size. */
980 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000981 mem_ranges_available[i].end = pa_init(align_down(
982 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100983 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100984
Andrew Scull3c257452019-11-26 13:32:50 +0000985 primary = vm_find(HF_PRIMARY_VM_ID);
986 primary_vm_locked = vm_lock(primary);
987
David Brazdil0251b942019-09-10 15:59:50 +0100988 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100989 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100990 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100991 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100992 paddr_t secondary_mem_begin;
993 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100994
David Brazdil7a462ec2019-08-15 12:27:47 +0100995 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100996 continue;
997 }
998
Olivier Deprez2a8ee342020-08-03 15:10:44 +0200999 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Andrew Walbran17eebf92020-02-05 16:35:49 +00001000 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001001
David Brazdil7a462ec2019-08-15 12:27:47 +01001002 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001003
Raghu Krishnamurthyb49549e2021-07-02 08:27:38 -07001004 if (manifest_vm->is_ffa_partition &&
1005 !manifest->vm[i].is_hyp_loaded) {
Olivier Deprez62d99e32020-01-09 15:58:07 +01001006 secondary_mem_begin =
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001007 pa_init(manifest_vm->partition.load_addr);
1008 secondary_mem_end = pa_init(
1009 manifest_vm->partition.load_addr + mem_size);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001010 } else if (!carve_out_mem_range(mem_ranges_available,
1011 params->mem_ranges_count,
1012 mem_size, &secondary_mem_begin,
1013 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001014 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001015 continue;
1016 }
Andrew Scull80871322018-08-06 12:04:09 +01001017
Manish Pandey2145c212020-05-01 16:04:22 +01001018 if (!load_secondary(stage1_locked, primary_vm_locked,
1019 secondary_mem_begin, secondary_mem_end,
1020 manifest_vm, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001021 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001022 continue;
1023 }
1024
1025 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +00001026 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
1027 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +00001028 dlog_error(
1029 "Unable to unmap secondary VM from primary "
1030 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +00001031 success = false;
1032 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +01001033 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001034 }
1035
Andrew Scull3c257452019-11-26 13:32:50 +00001036 vm_unlock(&primary_vm_locked);
1037
1038 if (!success) {
1039 return false;
1040 }
1041
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001042 /*
1043 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001044 * difference between the available ranges from the original params and
1045 * the updated mem_ranges_available. We assume that the number and order
1046 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +01001047 * above only make them smaller.
1048 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +01001049 return update_reserved_ranges(update, params->mem_ranges,
1050 mem_ranges_available,
1051 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01001052}