blob: 83eed532214655887847c9df0d4ee11689ffac35 [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
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"
Andrew Scull5991ec92018-10-08 14:55:02 +010020#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/memiter.h"
22#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010023#include "hf/plat/console.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000024#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010025#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010028
Andrew Scull19503262018-09-20 14:48:39 +010029#include "vmapi/hf/call.h"
30
Andrew Walbran9daa57e2019-09-27 13:33:20 +010031alignas(PAGE_SIZE) static uint8_t tee_send_buffer[HF_MAILBOX_SIZE];
32alignas(PAGE_SIZE) static uint8_t tee_recv_buffer[HF_MAILBOX_SIZE];
33
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
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100100/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100101 * Performs VM loading activities that are common between the primary and
102 * secondaries.
103 */
104static bool load_common(const struct manifest_vm *manifest_vm, struct vm *vm)
105{
106 vm->smc_whitelist = manifest_vm->smc_whitelist;
107
Fuad Tabba56970712020-01-10 11:20:09 +0000108 /* Initialize architecture-specific features. */
Fuad Tabba77a4b012019-11-15 12:13:08 +0000109 arch_vm_features_set(vm);
110
Andrew Scullae9962e2019-10-03 16:51:16 +0100111 return true;
112}
113
114/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100115 * Loads the primary VM.
116 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100117static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100118 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100119 const struct memiter *cpio,
120 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100121{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100122 paddr_t primary_begin;
123 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100124 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000125 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100126 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100127 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000128 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100129
Olivier Deprez62d99e32020-01-09 15:58:07 +0100130 if (manifest_vm->is_ffa_partition) {
131 primary_begin = pa_init(manifest_vm->sp.load_addr);
132 primary_entry = ipa_add(ipa_from_pa(primary_begin),
133 manifest_vm->sp.ep_offset);
134 } else {
135 primary_begin =
136 (manifest_vm->primary.boot_address ==
137 MANIFEST_INVALID_ADDRESS)
138 ? layout_primary_begin()
139 : pa_init(manifest_vm->primary.boot_address);
140 primary_entry = ipa_from_pa(primary_begin);
141 }
142
David Brazdil080ee312020-02-25 15:30:30 -0800143 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100144
Olivier Deprez62d99e32020-01-09 15:58:07 +0100145 /*
146 * Load the kernel if a filename is specified in the VM manifest.
147 * For an FF-A partition, kernel_filename is undefined indicating
148 * the partition package has already been loaded prior to Hafnium
149 * booting.
150 */
151 if (!string_is_empty(&manifest_vm->kernel_filename)) {
152 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100153 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100154 dlog_error("Unable to load primary kernel.\n");
155 return false;
156 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100157 }
158
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100159 if (!vm_init_next(MAX_CPUS, ppool, &vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000160 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100161 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100162 }
163
David Brazdile6f83222019-09-23 14:47:37 +0100164 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000165 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100166 return false;
167 }
168
Andrew Scull3c257452019-11-26 13:32:50 +0000169 vm_locked = vm_lock(vm);
170
Andrew Scullae9962e2019-10-03 16:51:16 +0100171 if (!load_common(manifest_vm, vm)) {
Andrew Scull3c257452019-11-26 13:32:50 +0000172 ret = false;
173 goto out;
Andrew Scullae9962e2019-10-03 16:51:16 +0100174 }
175
Andrew Scull48929fd2020-01-28 10:39:10 +0000176 if (params->device_mem_ranges_count == 0) {
177 /*
178 * Map 1TB of address space as device memory to, most likely,
179 * make all devices available to the primary VM.
180 *
181 * TODO: remove this once all targets provide valid ranges.
182 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800183 dlog_warning(
184 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000185
186 if (!vm_identity_map(
187 vm_locked, pa_init(0),
188 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
189 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
190 dlog_error(
191 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800192 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000193 ret = false;
194 goto out;
195 }
David Brazdile6f83222019-09-23 14:47:37 +0100196 }
197
Andrew Scullb5f49e02019-10-02 13:20:47 +0100198 /* Map normal memory as such to permit caching, execution, etc. */
199 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000200 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
201 params->mem_ranges[i].end,
202 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
203 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000204 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800205 "Unable to initialise memory for primary "
206 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000207 ret = false;
208 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100209 }
210 }
211
Andrew Scull48929fd2020-01-28 10:39:10 +0000212 /* Map device memory as such to prevent execution, speculation etc. */
213 for (i = 0; i < params->device_mem_ranges_count; ++i) {
214 if (!vm_identity_map(
215 vm_locked, params->device_mem_ranges[i].begin,
216 params->device_mem_ranges[i].end,
217 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
218 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800219 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000220 ret = false;
221 goto out;
222 }
223 }
224
Andrew Scull3c257452019-11-26 13:32:50 +0000225 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800226 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000227 ret = false;
228 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100229 }
230
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000231 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800232 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000233 ret = false;
234 goto out;
235 }
236
Andrew Walbran7586e042020-02-18 18:19:26 +0000237 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
238 vm->vcpu_count, pa_addr(primary_begin));
239
David Brazdile6f83222019-09-23 14:47:37 +0100240 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100241 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100242 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000243 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100244
Andrew Scull3c257452019-11-26 13:32:50 +0000245out:
246 vm_unlock(&vm_locked);
247
248 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100249}
250
Fuad Tabba50469e02020-06-30 15:14:28 +0100251/**
252 * Loads the secondary VM's FDT.
253 * Stores the total allocated size for the FDT in fdt_allocated_size (if
254 * fdt_allocated_size is not NULL). The allocated size includes additional space
255 * for potential patching.
256 */
257static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
258 paddr_t end, size_t fdt_max_size,
259 const struct manifest_vm *manifest_vm,
260 const struct memiter *cpio, struct mpool *ppool,
261 paddr_t *fdt_addr, size_t *fdt_allocated_size)
262{
263 struct memiter fdt;
264 size_t allocated_size;
265
266 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
267
268 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
269 dlog_error("Cannot open the secondary VM's FDT.\n");
270 return false;
271 }
272
273 /*
274 * Ensure the FDT has one additional page at the end for patching, and
275 * and align it to the page boundary.
276 */
277 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
278
279 if (allocated_size > fdt_max_size) {
280 dlog_error(
281 "FDT allocated space (%u) is more than the specified "
282 "maximum to use (%u).\n",
283 allocated_size, fdt_max_size);
284 return false;
285 }
286
287 /* Load the FDT to the end of the VM's allocated memory space. */
288 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
289
290 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
291 allocated_size, pa_addr(*fdt_addr));
292
293 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
294 dlog_error("Unable to copy FDT.\n");
295 return false;
296 }
297
298 if (fdt_allocated_size) {
299 *fdt_allocated_size = allocated_size;
300 }
301
302 return true;
303}
304
Andrew Scull72b43c02019-09-18 13:53:45 +0100305/*
306 * Loads a secondary VM.
307 */
308static bool load_secondary(struct mm_stage1_locked stage1_locked,
309 paddr_t mem_begin, paddr_t mem_end,
310 const struct manifest_vm *manifest_vm,
311 const struct memiter *cpio, struct mpool *ppool)
312{
313 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000314 struct vm_locked vm_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100315 struct vcpu *vcpu;
316 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000317 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100318 paddr_t fdt_addr;
319 bool has_fdt;
320 size_t kernel_size = 0;
321 const size_t mem_size = pa_difference(mem_begin, mem_end);
Andrew Scull72b43c02019-09-18 13:53:45 +0100322
Olivier Deprez62d99e32020-01-09 15:58:07 +0100323 /*
324 * Load the kernel if a filename is specified in the VM manifest.
325 * For an FF-A partition, kernel_filename is undefined indicating
326 * the partition package has already been loaded prior to Hafnium
327 * booting.
328 */
329 if (!string_is_empty(&manifest_vm->kernel_filename)) {
330 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100331 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100332 dlog_error("Unable to load kernel.\n");
333 return false;
334 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100335 }
336
Fuad Tabba50469e02020-06-30 15:14:28 +0100337 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
338 if (has_fdt) {
339 /*
340 * Ensure that the FDT does not overwrite the kernel or overlap
341 * its page, for the FDT to start at a page boundary.
342 */
343 const size_t fdt_max_size =
344 mem_size - align_up(kernel_size, PAGE_SIZE);
345
346 size_t fdt_allocated_size;
347
348 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
349 manifest_vm, cpio, ppool, &fdt_addr,
350 &fdt_allocated_size)) {
351 dlog_error("Unable to load FDT.\n");
352 return false;
353 }
354
355 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
356 mem_begin, mem_end, ppool)) {
357 dlog_error("Unable to patch FDT.\n");
358 return false;
359 }
360 }
361
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100362 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000363 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100364 return false;
365 }
366
Andrew Scullae9962e2019-10-03 16:51:16 +0100367 if (!load_common(manifest_vm, vm)) {
368 return false;
369 }
370
Andrew Scull3c257452019-11-26 13:32:50 +0000371 vm_locked = vm_lock(vm);
372
Andrew Scull72b43c02019-09-18 13:53:45 +0100373 /* Grant the VM access to the memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000374 if (!vm_identity_map(vm_locked, mem_begin, mem_end,
375 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
376 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000377 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000378 ret = false;
379 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100380 }
381
Andrew Walbran17eebf92020-02-05 16:35:49 +0000382 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
383 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
Andrew Scull72b43c02019-09-18 13:53:45 +0100384
Olivier Deprez62d99e32020-01-09 15:58:07 +0100385 if (manifest_vm->is_ffa_partition) {
386 secondary_entry =
387 ipa_add(secondary_entry, manifest_vm->sp.ep_offset);
388 }
389
Andrew Scull72b43c02019-09-18 13:53:45 +0100390 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100391
392 if (has_fdt) {
393 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
394 pa_addr(fdt_addr));
395 } else {
396 /*
397 * Without an FDT, secondary VMs expect the memory size to be
398 * passed in register x0, which is what
399 * vcpu_secondary_reset_and_start does in this case.
400 */
401 vcpu_secondary_reset_and_start(vcpu, secondary_entry, mem_size);
402 }
403
Andrew Scull3c257452019-11-26 13:32:50 +0000404 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100405
Andrew Scull3c257452019-11-26 13:32:50 +0000406out:
407 vm_unlock(&vm_locked);
408
409 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100410}
411
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100412/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100413 * Try to find a memory range of the given size within the given ranges, and
414 * remove it from them. Return true on success, or false if no large enough
415 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100416 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900417static bool carve_out_mem_range(struct mem_range *mem_ranges,
418 size_t mem_ranges_count, uint64_t size_to_find,
419 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100420{
421 size_t i;
422
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000423 /*
424 * TODO(b/116191358): Consider being cleverer about how we pack VMs
425 * together, with a non-greedy algorithm.
426 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100427 for (i = 0; i < mem_ranges_count; ++i) {
428 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100429 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100430 /*
431 * This range is big enough, take some of it from the
432 * end and reduce its size accordingly.
433 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100434 *found_end = mem_ranges[i].end;
435 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
436 size_to_find);
437 mem_ranges[i].end = *found_begin;
438 return true;
439 }
440 }
441 return false;
442}
443
444/**
445 * Given arrays of memory ranges before and after memory was removed for
446 * secondary VMs, add the difference to the reserved ranges of the given update.
447 * Return true on success, or false if there would be more than MAX_MEM_RANGES
448 * reserved ranges after adding the new ones.
449 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
450 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900451static bool update_reserved_ranges(struct boot_params_update *update,
452 const struct mem_range *before,
453 const struct mem_range *after,
454 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100455{
456 size_t i;
457
458 for (i = 0; i < mem_ranges_count; ++i) {
459 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
460 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000461 dlog_error(
462 "Too many reserved ranges after "
463 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100464 return false;
465 }
466 update->reserved_ranges[update->reserved_ranges_count]
467 .begin = before[i].begin;
468 update->reserved_ranges[update->reserved_ranges_count]
469 .end = after[i].begin;
470 update->reserved_ranges_count++;
471 }
472 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
473 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000474 dlog_error(
475 "Too many reserved ranges after "
476 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100477 return false;
478 }
479 update->reserved_ranges[update->reserved_ranges_count]
480 .begin = after[i].end;
481 update->reserved_ranges[update->reserved_ranges_count]
482 .end = before[i].end;
483 update->reserved_ranges_count++;
484 }
485 }
486
487 return true;
488}
489
Andrew Scull72b43c02019-09-18 13:53:45 +0100490/*
491 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100492 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100493bool load_vms(struct mm_stage1_locked stage1_locked,
494 const struct manifest *manifest, const struct memiter *cpio,
495 const struct boot_params *params,
496 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100497{
Andrew Scull19503262018-09-20 14:48:39 +0100498 struct vm *primary;
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100499 struct vm *tee;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100500 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
Andrew Scull3c257452019-11-26 13:32:50 +0000501 struct vm_locked primary_vm_locked;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100502 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000503 bool success = true;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100504
Andrew Scullae9962e2019-10-03 16:51:16 +0100505 if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
506 cpio, params, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000507 dlog_error("Unable to load primary VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100508 return false;
509 }
510
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100511 /*
512 * Initialise the dummy VM which represents TrustZone, and set up its
513 * RX/TX buffers.
514 */
515 tee = vm_init(HF_TEE_VM_ID, 0, ppool);
516 CHECK(tee != NULL);
517 tee->mailbox.send = &tee_send_buffer;
518 tee->mailbox.recv = &tee_recv_buffer;
519
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100520 static_assert(
521 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
522 "mem_range arrays must be the same size for memcpy.");
523 static_assert(sizeof(mem_ranges_available) < 500,
524 "This will use too much stack, either make "
525 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100526 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
527 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100528
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100529 /* Round the last addresses down to the page size. */
530 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000531 mem_ranges_available[i].end = pa_init(align_down(
532 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100533 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100534
Andrew Scull3c257452019-11-26 13:32:50 +0000535 primary = vm_find(HF_PRIMARY_VM_ID);
536 primary_vm_locked = vm_lock(primary);
537
David Brazdil0251b942019-09-10 15:59:50 +0100538 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100539 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100540 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100541 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100542 paddr_t secondary_mem_begin;
543 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100544
David Brazdil7a462ec2019-08-15 12:27:47 +0100545 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100546 continue;
547 }
548
Andrew Walbran17eebf92020-02-05 16:35:49 +0000549 dlog_info("Loading VM%d: %s.\n", (int)vm_id,
550 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100551
David Brazdil7a462ec2019-08-15 12:27:47 +0100552 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100553
554 if (manifest_vm->is_ffa_partition) {
555 secondary_mem_begin =
556 pa_init(manifest_vm->sp.load_addr);
557 secondary_mem_end =
558 pa_init(manifest_vm->sp.load_addr + mem_size);
559 } else if (!carve_out_mem_range(mem_ranges_available,
560 params->mem_ranges_count,
561 mem_size, &secondary_mem_begin,
562 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000563 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100564 continue;
565 }
Andrew Scull80871322018-08-06 12:04:09 +0100566
Andrew Scull72b43c02019-09-18 13:53:45 +0100567 if (!load_secondary(stage1_locked, secondary_mem_begin,
568 secondary_mem_end, manifest_vm, cpio,
569 ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000570 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100571 continue;
572 }
573
574 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000575 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
576 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000577 dlog_error(
578 "Unable to unmap secondary VM from primary "
579 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000580 success = false;
581 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100582 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100583 }
584
Andrew Scull3c257452019-11-26 13:32:50 +0000585 vm_unlock(&primary_vm_locked);
586
587 if (!success) {
588 return false;
589 }
590
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100591 /*
592 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100593 * difference between the available ranges from the original params and
594 * the updated mem_ranges_available. We assume that the number and order
595 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100596 * above only make them smaller.
597 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100598 return update_reserved_ranges(update, params->mem_ranges,
599 mem_ranges_available,
600 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100601}