blob: 4521e1a78ea0b765b7ffac48820f119c41595c7e [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
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Fuad Tabba77a4b012019-11-15 12:13:08 +000014#include "hf/arch/vm.h"
15
Andrew Scull18c78fc2018-08-20 12:57:41 +010016#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010017#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010018#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/dlog.h"
Fuad Tabba50469e02020-06-30 15:14:28 +010020#include "hf/fdt_patch.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010021#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/memiter.h"
23#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010024#include "hf/plat/console.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000025#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010026#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010027#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010029
Andrew Scull19503262018-09-20 14:48:39 +010030#include "vmapi/hf/call.h"
Manish Pandeyd34f8892020-06-19 17:41:07 +010031#include "vmapi/hf/ffa.h"
Andrew Scull19503262018-09-20 14:48:39 +010032
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010033/**
34 * Copies data to an unmapped location by mapping it for write, copying the
35 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000036 *
37 * The data is written so that it is available to all cores with the cache
38 * disabled. When switching to the partitions, the caching is initially disabled
39 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010040 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010041static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010042 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010043{
David Brazdil7a462ec2019-08-15 12:27:47 +010044 const void *from = memiter_base(from_it);
45 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010046 paddr_t to_end = pa_add(to, size);
47 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010048
Andrew Scull3c0a90a2019-07-01 11:55:53 +010049 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010050 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010051 return false;
52 }
53
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010054 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010055 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010056
Andrew Scull72b43c02019-09-18 13:53:45 +010057 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010058
59 return true;
60}
61
Fuad Tabba50469e02020-06-30 15:14:28 +010062/**
63 * Loads the secondary VM's kernel.
64 * Stores the kernel size in kernel_size (if kernel_size is not NULL).
65 * Returns false if it cannot load the kernel.
66 */
Andrew Scull72b43c02019-09-18 13:53:45 +010067static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
68 paddr_t end, const struct manifest_vm *manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +010069 const struct memiter *cpio, struct mpool *ppool,
70 size_t *kernel_size)
Andrew Scull72b43c02019-09-18 13:53:45 +010071{
Andrew Scull72b43c02019-09-18 13:53:45 +010072 struct memiter kernel;
Fuad Tabba50469e02020-06-30 15:14:28 +010073 size_t size;
Andrew Scull72b43c02019-09-18 13:53:45 +010074
David Brazdil136f2942019-09-23 14:11:03 +010075 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000076 dlog_error("Could not find kernel file \"%s\".\n",
77 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010078 return false;
79 }
80
Fuad Tabba50469e02020-06-30 15:14:28 +010081 size = memiter_size(&kernel);
82 if (pa_difference(begin, end) < size) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000083 dlog_error("Kernel is larger than available memory.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010084 return false;
85 }
86
87 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +000088 dlog_error("Unable to copy kernel.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +010089 return false;
90 }
91
Fuad Tabba50469e02020-06-30 15:14:28 +010092 if (kernel_size) {
93 *kernel_size = size;
94 }
95
Andrew Scull72b43c02019-09-18 13:53:45 +010096 return true;
97}
98
Manish Pandeyd34f8892020-06-19 17:41:07 +010099/*
100 * Link RX/TX buffers provided in partition manifest to mailbox
101 */
102static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked,
103 struct vm_locked vm_locked, struct rx_tx rxtx,
104 struct mpool *ppool)
105{
106 struct ffa_value ret;
107 ipaddr_t send;
108 ipaddr_t recv;
109 uint32_t page_count;
110
111 send = ipa_init(rxtx.tx_buffer->base_address);
112 recv = ipa_init(rxtx.rx_buffer->base_address);
113 page_count = rxtx.tx_buffer->page_count;
114
115 ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv,
116 page_count, ppool);
117 if (ret.func != FFA_SUCCESS_32) {
118 return false;
119 }
120
121 dlog_verbose(" mailbox: send = %#x, recv = %#x\n",
122 vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv);
123
124 return true;
125}
126
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100127/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100128 * Performs VM loading activities that are common between the primary and
129 * secondaries.
130 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100131static bool load_common(struct mm_stage1_locked stage1_locked,
132 struct vm_locked vm_locked,
133 const struct manifest_vm *manifest_vm,
134 struct mpool *ppool)
Andrew Scullae9962e2019-10-03 16:51:16 +0100135{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100136 vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist;
137 vm_locked.vm->uuid = manifest_vm->sp.uuid;
Andrew Scullae9962e2019-10-03 16:51:16 +0100138
Manish Pandeyd34f8892020-06-19 17:41:07 +0100139 if (manifest_vm->is_ffa_partition) {
140 /* Link rxtx buffers to mailbox */
141 if (manifest_vm->sp.rxtx.available) {
142 if (!link_rxtx_to_mailbox(stage1_locked, vm_locked,
143 manifest_vm->sp.rxtx,
144 ppool)) {
145 dlog_error(
146 "Unable to Link RX/TX buffer with "
147 "mailbox.\n");
148 return false;
149 }
150 }
J-Alvesb37fd082020-10-22 12:29:21 +0100151
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100152 vm_locked.vm->messaging_method =
153 manifest_vm->sp.messaging_method;
Manish Pandeyf3be6392020-09-24 17:26:09 +0100154
J-Alvesb37fd082020-10-22 12:29:21 +0100155 vm_locked.vm->boot_order = manifest_vm->sp.boot_order;
156 /* Updating boot list according to boot_order */
157 vm_update_boot(vm_locked.vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +0100158 }
J-Alvesb37fd082020-10-22 12:29:21 +0100159
Fuad Tabba56970712020-01-10 11:20:09 +0000160 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100161 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000162
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500163 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
164 ppool)) {
165 dlog_error("Unable to attach upstream peripheral device\n");
166 return false;
167 }
168
Andrew Scullae9962e2019-10-03 16:51:16 +0100169 return true;
170}
171
172/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100173 * Loads the primary VM.
174 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100175static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100176 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100177 const struct memiter *cpio,
178 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100179{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100180 paddr_t primary_begin;
181 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100182 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000183 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100184 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100185 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000186 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100187
Olivier Deprez62d99e32020-01-09 15:58:07 +0100188 if (manifest_vm->is_ffa_partition) {
189 primary_begin = pa_init(manifest_vm->sp.load_addr);
190 primary_entry = ipa_add(ipa_from_pa(primary_begin),
191 manifest_vm->sp.ep_offset);
192 } else {
193 primary_begin =
194 (manifest_vm->primary.boot_address ==
195 MANIFEST_INVALID_ADDRESS)
196 ? layout_primary_begin()
197 : pa_init(manifest_vm->primary.boot_address);
198 primary_entry = ipa_from_pa(primary_begin);
199 }
200
David Brazdil080ee312020-02-25 15:30:30 -0800201 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100202
Olivier Deprez62d99e32020-01-09 15:58:07 +0100203 /*
204 * Load the kernel if a filename is specified in the VM manifest.
205 * For an FF-A partition, kernel_filename is undefined indicating
206 * the partition package has already been loaded prior to Hafnium
207 * booting.
208 */
209 if (!string_is_empty(&manifest_vm->kernel_filename)) {
210 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100211 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100212 dlog_error("Unable to load primary kernel.\n");
213 return false;
214 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100215 }
216
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100217 if (!vm_init_next(MAX_CPUS, ppool, &vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000218 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100219 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100220 }
221
David Brazdile6f83222019-09-23 14:47:37 +0100222 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000223 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100224 return false;
225 }
226
Andrew Scull3c257452019-11-26 13:32:50 +0000227 vm_locked = vm_lock(vm);
228
Andrew Scull48929fd2020-01-28 10:39:10 +0000229 if (params->device_mem_ranges_count == 0) {
230 /*
231 * Map 1TB of address space as device memory to, most likely,
232 * make all devices available to the primary VM.
233 *
234 * TODO: remove this once all targets provide valid ranges.
235 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800236 dlog_warning(
237 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000238
239 if (!vm_identity_map(
240 vm_locked, pa_init(0),
241 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
242 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
243 dlog_error(
244 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800245 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000246 ret = false;
247 goto out;
248 }
David Brazdile6f83222019-09-23 14:47:37 +0100249 }
250
Andrew Scullb5f49e02019-10-02 13:20:47 +0100251 /* Map normal memory as such to permit caching, execution, etc. */
252 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000253 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
254 params->mem_ranges[i].end,
255 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
256 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000257 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800258 "Unable to initialise memory for primary "
259 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000260 ret = false;
261 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100262 }
263 }
264
Andrew Scull48929fd2020-01-28 10:39:10 +0000265 /* Map device memory as such to prevent execution, speculation etc. */
266 for (i = 0; i < params->device_mem_ranges_count; ++i) {
267 if (!vm_identity_map(
268 vm_locked, params->device_mem_ranges[i].begin,
269 params->device_mem_ranges[i].end,
270 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
271 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800272 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000273 ret = false;
274 goto out;
275 }
276 }
277
Manish Pandeyd34f8892020-06-19 17:41:07 +0100278 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
279 ret = false;
280 goto out;
281 }
282
Andrew Scull3c257452019-11-26 13:32:50 +0000283 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800284 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000285 ret = false;
286 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100287 }
288
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000289 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800290 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000291 ret = false;
292 goto out;
293 }
294
Andrew Walbran7586e042020-02-18 18:19:26 +0000295 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
296 vm->vcpu_count, pa_addr(primary_begin));
297
Olivier Deprezb9adff42021-02-01 12:14:05 +0100298 /* Mark the primary to be the first booted VM */
299 vm_update_boot(vm);
300
David Brazdile6f83222019-09-23 14:47:37 +0100301 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100302 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100303 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000304 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100305
Andrew Scull3c257452019-11-26 13:32:50 +0000306out:
307 vm_unlock(&vm_locked);
308
309 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100310}
311
Fuad Tabba50469e02020-06-30 15:14:28 +0100312/**
313 * Loads the secondary VM's FDT.
314 * Stores the total allocated size for the FDT in fdt_allocated_size (if
315 * fdt_allocated_size is not NULL). The allocated size includes additional space
316 * for potential patching.
317 */
318static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
319 paddr_t end, size_t fdt_max_size,
320 const struct manifest_vm *manifest_vm,
321 const struct memiter *cpio, struct mpool *ppool,
322 paddr_t *fdt_addr, size_t *fdt_allocated_size)
323{
324 struct memiter fdt;
325 size_t allocated_size;
326
327 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
328
329 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
330 dlog_error("Cannot open the secondary VM's FDT.\n");
331 return false;
332 }
333
334 /*
335 * Ensure the FDT has one additional page at the end for patching, and
336 * and align it to the page boundary.
337 */
338 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
339
340 if (allocated_size > fdt_max_size) {
341 dlog_error(
342 "FDT allocated space (%u) is more than the specified "
343 "maximum to use (%u).\n",
344 allocated_size, fdt_max_size);
345 return false;
346 }
347
348 /* Load the FDT to the end of the VM's allocated memory space. */
349 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
350
351 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
352 allocated_size, pa_addr(*fdt_addr));
353
354 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
355 dlog_error("Unable to copy FDT.\n");
356 return false;
357 }
358
359 if (fdt_allocated_size) {
360 *fdt_allocated_size = allocated_size;
361 }
362
363 return true;
364}
365
Andrew Scull72b43c02019-09-18 13:53:45 +0100366/*
367 * Loads a secondary VM.
368 */
369static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100370 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100371 paddr_t mem_begin, paddr_t mem_end,
372 const struct manifest_vm *manifest_vm,
373 const struct memiter *cpio, struct mpool *ppool)
374{
375 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000376 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100377 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100378 struct vcpu *vcpu;
379 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000380 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100381 paddr_t fdt_addr;
382 bool has_fdt;
383 size_t kernel_size = 0;
384 const size_t mem_size = pa_difference(mem_begin, mem_end);
Andrew Scull72b43c02019-09-18 13:53:45 +0100385
Olivier Deprez62d99e32020-01-09 15:58:07 +0100386 /*
387 * Load the kernel if a filename is specified in the VM manifest.
388 * For an FF-A partition, kernel_filename is undefined indicating
389 * the partition package has already been loaded prior to Hafnium
390 * booting.
391 */
392 if (!string_is_empty(&manifest_vm->kernel_filename)) {
393 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100394 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100395 dlog_error("Unable to load kernel.\n");
396 return false;
397 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100398 }
399
Fuad Tabba50469e02020-06-30 15:14:28 +0100400 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
401 if (has_fdt) {
402 /*
403 * Ensure that the FDT does not overwrite the kernel or overlap
404 * its page, for the FDT to start at a page boundary.
405 */
406 const size_t fdt_max_size =
407 mem_size - align_up(kernel_size, PAGE_SIZE);
408
409 size_t fdt_allocated_size;
410
411 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
412 manifest_vm, cpio, ppool, &fdt_addr,
413 &fdt_allocated_size)) {
414 dlog_error("Unable to load FDT.\n");
415 return false;
416 }
417
418 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
419 mem_begin, mem_end, ppool)) {
420 dlog_error("Unable to patch FDT.\n");
421 return false;
422 }
423 }
424
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100425 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000426 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100427 return false;
428 }
429
Andrew Scull3c257452019-11-26 13:32:50 +0000430 vm_locked = vm_lock(vm);
431
Andrew Scull72b43c02019-09-18 13:53:45 +0100432 /* Grant the VM access to the memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000433 if (!vm_identity_map(vm_locked, mem_begin, mem_end,
434 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
435 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000436 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000437 ret = false;
438 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100439 }
440
Olivier Deprez62d99e32020-01-09 15:58:07 +0100441 if (manifest_vm->is_ffa_partition) {
Manish Pandey2145c212020-05-01 16:04:22 +0100442 int j = 0;
443 paddr_t region_begin;
444 paddr_t region_end;
445 paddr_t alloc_base = mem_end;
446 size_t size;
447 size_t total_alloc = 0;
448
449 /* Map memory-regions */
450 while (j < manifest_vm->sp.mem_region_count) {
451 size = manifest_vm->sp.mem_regions[j].page_count *
452 PAGE_SIZE;
453 /*
454 * For memory-regions without base-address, memory
455 * should be allocated inside partition's page table.
456 * Start allocating memory regions in partition's
457 * page table, starting from the end.
458 * TODO: Add mechanism to let partition know of these
459 * memory regions
460 */
461 if (manifest_vm->sp.mem_regions[j].base_address ==
462 MANIFEST_INVALID_ADDRESS) {
463 total_alloc += size;
464 /* Don't go beyond half the VM's memory space */
465 if (total_alloc >
466 (manifest_vm->secondary.mem_size / 2)) {
467 dlog_error(
468 "Not enough space for memory-"
469 "region allocation");
470 ret = false;
471 goto out;
472 }
473
474 region_end = alloc_base;
475 region_begin = pa_subtract(alloc_base, size);
476 alloc_base = region_begin;
477
478 if (!vm_identity_map(
479 vm_locked, region_begin, region_end,
480 manifest_vm->sp.mem_regions[j]
481 .attributes,
482 ppool, NULL)) {
483 dlog_error(
484 "Unable to map secondary VM "
485 "memory-region.\n");
486 ret = false;
487 goto out;
488 }
489
490 dlog_info(
491 " Memory region %#x - %#x allocated\n",
492 region_begin, region_end);
493 } else {
494 /*
495 * Identity map memory region for both case,
496 * VA(S-EL0) or IPA(S-EL1).
497 */
498 region_begin =
499 pa_init(manifest_vm->sp.mem_regions[j]
500 .base_address);
501 region_end = pa_add(region_begin, size);
502
503 if (!vm_identity_map(
504 vm_locked, region_begin, region_end,
505 manifest_vm->sp.mem_regions[j]
506 .attributes,
507 ppool, NULL)) {
508 dlog_error(
509 "Unable to map secondary VM "
510 "memory-region.\n");
511 ret = false;
512 goto out;
513 }
514 }
515
516 /* Deny the primary VM access to this memory */
517 if (!vm_unmap(primary_vm_locked, region_begin,
518 region_end, ppool)) {
519 dlog_error(
520 "Unable to unmap secondary VM memory-"
521 "region from primary VM.\n");
522 ret = false;
523 goto out;
524 }
525
526 j++;
527 }
528
529 /* Map device-regions */
530 j = 0;
531 while (j < manifest_vm->sp.dev_region_count) {
532 region_begin = pa_init(
533 manifest_vm->sp.dev_regions[j].base_address);
534 size = manifest_vm->sp.dev_regions[j].page_count *
535 PAGE_SIZE;
536 region_end = pa_add(region_begin, size);
537
538 if (!vm_identity_map(
539 vm_locked, region_begin, region_end,
540 manifest_vm->sp.dev_regions[j].attributes,
541 ppool, NULL)) {
542 dlog_error(
543 "Unable to map secondary VM "
544 "device-region.\n");
545 ret = false;
546 goto out;
547 }
548 /* Deny primary VM access to this region */
549 if (!vm_unmap(primary_vm_locked, region_begin,
550 region_end, ppool)) {
551 dlog_error(
552 "Unable to unmap secondary VM device-"
553 "region from primary VM.\n");
554 ret = false;
555 goto out;
556 }
557 j++;
558 }
559
Olivier Deprez62d99e32020-01-09 15:58:07 +0100560 secondary_entry =
561 ipa_add(secondary_entry, manifest_vm->sp.ep_offset);
562 }
563
Manish Pandeyd34f8892020-06-19 17:41:07 +0100564 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
565 ret = false;
566 goto out;
567 }
568
Manish Pandey2145c212020-05-01 16:04:22 +0100569 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
570 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
571
Andrew Scull72b43c02019-09-18 13:53:45 +0100572 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100573
Max Shvetsov40108e72020-08-27 12:39:50 +0100574 vcpu_locked = vcpu_lock(vcpu);
Fuad Tabba50469e02020-06-30 15:14:28 +0100575 if (has_fdt) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100576 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
Fuad Tabba50469e02020-06-30 15:14:28 +0100577 pa_addr(fdt_addr));
578 } else {
579 /*
580 * Without an FDT, secondary VMs expect the memory size to be
581 * passed in register x0, which is what
582 * vcpu_secondary_reset_and_start does in this case.
583 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100584 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
585 mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100586 }
587
Max Shvetsov40108e72020-08-27 12:39:50 +0100588 vcpu_unlock(&vcpu_locked);
589
Andrew Scull3c257452019-11-26 13:32:50 +0000590 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100591
Andrew Scull3c257452019-11-26 13:32:50 +0000592out:
593 vm_unlock(&vm_locked);
594
595 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100596}
597
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100598/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100599 * Try to find a memory range of the given size within the given ranges, and
600 * remove it from them. Return true on success, or false if no large enough
601 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100602 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900603static bool carve_out_mem_range(struct mem_range *mem_ranges,
604 size_t mem_ranges_count, uint64_t size_to_find,
605 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100606{
607 size_t i;
608
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000609 /*
610 * TODO(b/116191358): Consider being cleverer about how we pack VMs
611 * together, with a non-greedy algorithm.
612 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100613 for (i = 0; i < mem_ranges_count; ++i) {
614 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100615 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100616 /*
617 * This range is big enough, take some of it from the
618 * end and reduce its size accordingly.
619 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100620 *found_end = mem_ranges[i].end;
621 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
622 size_to_find);
623 mem_ranges[i].end = *found_begin;
624 return true;
625 }
626 }
627 return false;
628}
629
630/**
631 * Given arrays of memory ranges before and after memory was removed for
632 * secondary VMs, add the difference to the reserved ranges of the given update.
633 * Return true on success, or false if there would be more than MAX_MEM_RANGES
634 * reserved ranges after adding the new ones.
635 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
636 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900637static bool update_reserved_ranges(struct boot_params_update *update,
638 const struct mem_range *before,
639 const struct mem_range *after,
640 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100641{
642 size_t i;
643
644 for (i = 0; i < mem_ranges_count; ++i) {
645 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
646 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000647 dlog_error(
648 "Too many reserved ranges after "
649 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100650 return false;
651 }
652 update->reserved_ranges[update->reserved_ranges_count]
653 .begin = before[i].begin;
654 update->reserved_ranges[update->reserved_ranges_count]
655 .end = after[i].begin;
656 update->reserved_ranges_count++;
657 }
658 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
659 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000660 dlog_error(
661 "Too many reserved ranges after "
662 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100663 return false;
664 }
665 update->reserved_ranges[update->reserved_ranges_count]
666 .begin = after[i].end;
667 update->reserved_ranges[update->reserved_ranges_count]
668 .end = before[i].end;
669 update->reserved_ranges_count++;
670 }
671 }
672
673 return true;
674}
675
Olivier Deprez112d2b52020-09-30 07:39:23 +0200676static bool init_other_world_vm(struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100677{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200678 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100679 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100680
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100681 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200682 * Initialise the dummy VM which represents the opposite world:
683 * -TrustZone (or the SPMC) when running the Hypervisor
684 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100685 */
Olivier Deprez96a2a262020-06-11 17:21:38 +0200686 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool);
687 CHECK(other_world_vm != NULL);
688
689 for (i = 0; i < MAX_CPUS; i++) {
690 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
691 struct cpu *cpu = cpu_find_index(i);
692
693 vcpu->cpu = cpu;
694 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100695
Olivier Deprez112d2b52020-09-30 07:39:23 +0200696 return arch_other_world_vm_init(other_world_vm, ppool);
697}
698
699/*
700 * Loads alls VMs from the manifest.
701 */
702bool load_vms(struct mm_stage1_locked stage1_locked,
703 const struct manifest *manifest, const struct memiter *cpio,
704 const struct boot_params *params,
705 struct boot_params_update *update, struct mpool *ppool)
706{
707 struct vm *primary;
708 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
709 struct vm_locked primary_vm_locked;
710 size_t i;
711 bool success = true;
712
Andrew Walbranf8716782020-10-29 17:07:07 +0000713 /**
714 * Only try to load the primary VM if it is supposed to be in this
715 * world.
716 */
717 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
718 if (!load_primary(stage1_locked,
719 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
720 params, ppool)) {
721 dlog_error("Unable to load primary VM.\n");
722 return false;
723 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200724 }
725
726 if (!init_other_world_vm(ppool)) {
727 return false;
728 }
729
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100730 static_assert(
731 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
732 "mem_range arrays must be the same size for memcpy.");
733 static_assert(sizeof(mem_ranges_available) < 500,
734 "This will use too much stack, either make "
735 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100736 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
737 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100738
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100739 /* Round the last addresses down to the page size. */
740 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000741 mem_ranges_available[i].end = pa_init(align_down(
742 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100743 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100744
Andrew Scull3c257452019-11-26 13:32:50 +0000745 primary = vm_find(HF_PRIMARY_VM_ID);
746 primary_vm_locked = vm_lock(primary);
747
David Brazdil0251b942019-09-10 15:59:50 +0100748 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100749 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100750 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100751 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100752 paddr_t secondary_mem_begin;
753 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100754
David Brazdil7a462ec2019-08-15 12:27:47 +0100755 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100756 continue;
757 }
758
Olivier Deprez2a8ee342020-08-03 15:10:44 +0200759 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000760 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100761
David Brazdil7a462ec2019-08-15 12:27:47 +0100762 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100763
764 if (manifest_vm->is_ffa_partition) {
765 secondary_mem_begin =
766 pa_init(manifest_vm->sp.load_addr);
767 secondary_mem_end =
768 pa_init(manifest_vm->sp.load_addr + mem_size);
769 } else if (!carve_out_mem_range(mem_ranges_available,
770 params->mem_ranges_count,
771 mem_size, &secondary_mem_begin,
772 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000773 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100774 continue;
775 }
Andrew Scull80871322018-08-06 12:04:09 +0100776
Manish Pandey2145c212020-05-01 16:04:22 +0100777 if (!load_secondary(stage1_locked, primary_vm_locked,
778 secondary_mem_begin, secondary_mem_end,
779 manifest_vm, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000780 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100781 continue;
782 }
783
784 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000785 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
786 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000787 dlog_error(
788 "Unable to unmap secondary VM from primary "
789 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000790 success = false;
791 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100792 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100793 }
794
Andrew Scull3c257452019-11-26 13:32:50 +0000795 vm_unlock(&primary_vm_locked);
796
797 if (!success) {
798 return false;
799 }
800
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100801 /*
802 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100803 * difference between the available ranges from the original params and
804 * the updated mem_ranges_available. We assume that the number and order
805 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100806 * above only make them smaller.
807 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100808 return update_reserved_ranges(update, params->mem_ranges,
809 mem_ranges_available,
810 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100811}