blob: 26a96410f1566aba8782e857c883169af00d7e4f [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;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100107 vm->uuid = manifest_vm->sp.uuid;
Andrew Scullae9962e2019-10-03 16:51:16 +0100108
Fuad Tabba56970712020-01-10 11:20:09 +0000109 /* Initialize architecture-specific features. */
Fuad Tabba77a4b012019-11-15 12:13:08 +0000110 arch_vm_features_set(vm);
111
Andrew Scullae9962e2019-10-03 16:51:16 +0100112 return true;
113}
114
115/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116 * Loads the primary VM.
117 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100118static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100119 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100120 const struct memiter *cpio,
121 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100122{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100123 paddr_t primary_begin;
124 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100125 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000126 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100127 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100128 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000129 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100130
Olivier Deprez62d99e32020-01-09 15:58:07 +0100131 if (manifest_vm->is_ffa_partition) {
132 primary_begin = pa_init(manifest_vm->sp.load_addr);
133 primary_entry = ipa_add(ipa_from_pa(primary_begin),
134 manifest_vm->sp.ep_offset);
135 } else {
136 primary_begin =
137 (manifest_vm->primary.boot_address ==
138 MANIFEST_INVALID_ADDRESS)
139 ? layout_primary_begin()
140 : pa_init(manifest_vm->primary.boot_address);
141 primary_entry = ipa_from_pa(primary_begin);
142 }
143
David Brazdil080ee312020-02-25 15:30:30 -0800144 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100145
Olivier Deprez62d99e32020-01-09 15:58:07 +0100146 /*
147 * Load the kernel if a filename is specified in the VM manifest.
148 * For an FF-A partition, kernel_filename is undefined indicating
149 * the partition package has already been loaded prior to Hafnium
150 * booting.
151 */
152 if (!string_is_empty(&manifest_vm->kernel_filename)) {
153 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100154 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100155 dlog_error("Unable to load primary kernel.\n");
156 return false;
157 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100158 }
159
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100160 if (!vm_init_next(MAX_CPUS, ppool, &vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000161 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100162 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100163 }
164
David Brazdile6f83222019-09-23 14:47:37 +0100165 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000166 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100167 return false;
168 }
169
Andrew Scull3c257452019-11-26 13:32:50 +0000170 vm_locked = vm_lock(vm);
171
Andrew Scullae9962e2019-10-03 16:51:16 +0100172 if (!load_common(manifest_vm, vm)) {
Andrew Scull3c257452019-11-26 13:32:50 +0000173 ret = false;
174 goto out;
Andrew Scullae9962e2019-10-03 16:51:16 +0100175 }
176
Andrew Scull48929fd2020-01-28 10:39:10 +0000177 if (params->device_mem_ranges_count == 0) {
178 /*
179 * Map 1TB of address space as device memory to, most likely,
180 * make all devices available to the primary VM.
181 *
182 * TODO: remove this once all targets provide valid ranges.
183 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800184 dlog_warning(
185 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000186
187 if (!vm_identity_map(
188 vm_locked, pa_init(0),
189 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
190 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
191 dlog_error(
192 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800193 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000194 ret = false;
195 goto out;
196 }
David Brazdile6f83222019-09-23 14:47:37 +0100197 }
198
Andrew Scullb5f49e02019-10-02 13:20:47 +0100199 /* Map normal memory as such to permit caching, execution, etc. */
200 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000201 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
202 params->mem_ranges[i].end,
203 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
204 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000205 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800206 "Unable to initialise memory for primary "
207 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000208 ret = false;
209 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100210 }
211 }
212
Andrew Scull48929fd2020-01-28 10:39:10 +0000213 /* Map device memory as such to prevent execution, speculation etc. */
214 for (i = 0; i < params->device_mem_ranges_count; ++i) {
215 if (!vm_identity_map(
216 vm_locked, params->device_mem_ranges[i].begin,
217 params->device_mem_ranges[i].end,
218 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
219 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800220 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000221 ret = false;
222 goto out;
223 }
224 }
225
Andrew Scull3c257452019-11-26 13:32:50 +0000226 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800227 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000228 ret = false;
229 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100230 }
231
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000232 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800233 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000234 ret = false;
235 goto out;
236 }
237
Andrew Walbran7586e042020-02-18 18:19:26 +0000238 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
239 vm->vcpu_count, pa_addr(primary_begin));
240
David Brazdile6f83222019-09-23 14:47:37 +0100241 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100242 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100243 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000244 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100245
Andrew Scull3c257452019-11-26 13:32:50 +0000246out:
247 vm_unlock(&vm_locked);
248
249 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100250}
251
Fuad Tabba50469e02020-06-30 15:14:28 +0100252/**
253 * Loads the secondary VM's FDT.
254 * Stores the total allocated size for the FDT in fdt_allocated_size (if
255 * fdt_allocated_size is not NULL). The allocated size includes additional space
256 * for potential patching.
257 */
258static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
259 paddr_t end, size_t fdt_max_size,
260 const struct manifest_vm *manifest_vm,
261 const struct memiter *cpio, struct mpool *ppool,
262 paddr_t *fdt_addr, size_t *fdt_allocated_size)
263{
264 struct memiter fdt;
265 size_t allocated_size;
266
267 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
268
269 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
270 dlog_error("Cannot open the secondary VM's FDT.\n");
271 return false;
272 }
273
274 /*
275 * Ensure the FDT has one additional page at the end for patching, and
276 * and align it to the page boundary.
277 */
278 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
279
280 if (allocated_size > fdt_max_size) {
281 dlog_error(
282 "FDT allocated space (%u) is more than the specified "
283 "maximum to use (%u).\n",
284 allocated_size, fdt_max_size);
285 return false;
286 }
287
288 /* Load the FDT to the end of the VM's allocated memory space. */
289 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
290
291 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
292 allocated_size, pa_addr(*fdt_addr));
293
294 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
295 dlog_error("Unable to copy FDT.\n");
296 return false;
297 }
298
299 if (fdt_allocated_size) {
300 *fdt_allocated_size = allocated_size;
301 }
302
303 return true;
304}
305
Andrew Scull72b43c02019-09-18 13:53:45 +0100306/*
307 * Loads a secondary VM.
308 */
309static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100310 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100311 paddr_t mem_begin, paddr_t mem_end,
312 const struct manifest_vm *manifest_vm,
313 const struct memiter *cpio, struct mpool *ppool)
314{
315 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000316 struct vm_locked vm_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100317 struct vcpu *vcpu;
318 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000319 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100320 paddr_t fdt_addr;
321 bool has_fdt;
322 size_t kernel_size = 0;
323 const size_t mem_size = pa_difference(mem_begin, mem_end);
Andrew Scull72b43c02019-09-18 13:53:45 +0100324
Olivier Deprez62d99e32020-01-09 15:58:07 +0100325 /*
326 * Load the kernel if a filename is specified in the VM manifest.
327 * For an FF-A partition, kernel_filename is undefined indicating
328 * the partition package has already been loaded prior to Hafnium
329 * booting.
330 */
331 if (!string_is_empty(&manifest_vm->kernel_filename)) {
332 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100333 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100334 dlog_error("Unable to load kernel.\n");
335 return false;
336 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100337 }
338
Fuad Tabba50469e02020-06-30 15:14:28 +0100339 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
340 if (has_fdt) {
341 /*
342 * Ensure that the FDT does not overwrite the kernel or overlap
343 * its page, for the FDT to start at a page boundary.
344 */
345 const size_t fdt_max_size =
346 mem_size - align_up(kernel_size, PAGE_SIZE);
347
348 size_t fdt_allocated_size;
349
350 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
351 manifest_vm, cpio, ppool, &fdt_addr,
352 &fdt_allocated_size)) {
353 dlog_error("Unable to load FDT.\n");
354 return false;
355 }
356
357 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
358 mem_begin, mem_end, ppool)) {
359 dlog_error("Unable to patch FDT.\n");
360 return false;
361 }
362 }
363
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100364 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000365 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100366 return false;
367 }
368
Andrew Scullae9962e2019-10-03 16:51:16 +0100369 if (!load_common(manifest_vm, vm)) {
370 return false;
371 }
372
Andrew Scull3c257452019-11-26 13:32:50 +0000373 vm_locked = vm_lock(vm);
374
Andrew Scull72b43c02019-09-18 13:53:45 +0100375 /* Grant the VM access to the memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000376 if (!vm_identity_map(vm_locked, mem_begin, mem_end,
377 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
378 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000379 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000380 ret = false;
381 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100382 }
383
Olivier Deprez62d99e32020-01-09 15:58:07 +0100384 if (manifest_vm->is_ffa_partition) {
Manish Pandey2145c212020-05-01 16:04:22 +0100385 int j = 0;
386 paddr_t region_begin;
387 paddr_t region_end;
388 paddr_t alloc_base = mem_end;
389 size_t size;
390 size_t total_alloc = 0;
391
392 /* Map memory-regions */
393 while (j < manifest_vm->sp.mem_region_count) {
394 size = manifest_vm->sp.mem_regions[j].page_count *
395 PAGE_SIZE;
396 /*
397 * For memory-regions without base-address, memory
398 * should be allocated inside partition's page table.
399 * Start allocating memory regions in partition's
400 * page table, starting from the end.
401 * TODO: Add mechanism to let partition know of these
402 * memory regions
403 */
404 if (manifest_vm->sp.mem_regions[j].base_address ==
405 MANIFEST_INVALID_ADDRESS) {
406 total_alloc += size;
407 /* Don't go beyond half the VM's memory space */
408 if (total_alloc >
409 (manifest_vm->secondary.mem_size / 2)) {
410 dlog_error(
411 "Not enough space for memory-"
412 "region allocation");
413 ret = false;
414 goto out;
415 }
416
417 region_end = alloc_base;
418 region_begin = pa_subtract(alloc_base, size);
419 alloc_base = region_begin;
420
421 if (!vm_identity_map(
422 vm_locked, region_begin, region_end,
423 manifest_vm->sp.mem_regions[j]
424 .attributes,
425 ppool, NULL)) {
426 dlog_error(
427 "Unable to map secondary VM "
428 "memory-region.\n");
429 ret = false;
430 goto out;
431 }
432
433 dlog_info(
434 " Memory region %#x - %#x allocated\n",
435 region_begin, region_end);
436 } else {
437 /*
438 * Identity map memory region for both case,
439 * VA(S-EL0) or IPA(S-EL1).
440 */
441 region_begin =
442 pa_init(manifest_vm->sp.mem_regions[j]
443 .base_address);
444 region_end = pa_add(region_begin, size);
445
446 if (!vm_identity_map(
447 vm_locked, region_begin, region_end,
448 manifest_vm->sp.mem_regions[j]
449 .attributes,
450 ppool, NULL)) {
451 dlog_error(
452 "Unable to map secondary VM "
453 "memory-region.\n");
454 ret = false;
455 goto out;
456 }
457 }
458
459 /* Deny the primary VM access to this memory */
460 if (!vm_unmap(primary_vm_locked, region_begin,
461 region_end, ppool)) {
462 dlog_error(
463 "Unable to unmap secondary VM memory-"
464 "region from primary VM.\n");
465 ret = false;
466 goto out;
467 }
468
469 j++;
470 }
471
472 /* Map device-regions */
473 j = 0;
474 while (j < manifest_vm->sp.dev_region_count) {
475 region_begin = pa_init(
476 manifest_vm->sp.dev_regions[j].base_address);
477 size = manifest_vm->sp.dev_regions[j].page_count *
478 PAGE_SIZE;
479 region_end = pa_add(region_begin, size);
480
481 if (!vm_identity_map(
482 vm_locked, region_begin, region_end,
483 manifest_vm->sp.dev_regions[j].attributes,
484 ppool, NULL)) {
485 dlog_error(
486 "Unable to map secondary VM "
487 "device-region.\n");
488 ret = false;
489 goto out;
490 }
491 /* Deny primary VM access to this region */
492 if (!vm_unmap(primary_vm_locked, region_begin,
493 region_end, ppool)) {
494 dlog_error(
495 "Unable to unmap secondary VM device-"
496 "region from primary VM.\n");
497 ret = false;
498 goto out;
499 }
500 j++;
501 }
502
Olivier Deprez62d99e32020-01-09 15:58:07 +0100503 secondary_entry =
504 ipa_add(secondary_entry, manifest_vm->sp.ep_offset);
505 }
506
Manish Pandey2145c212020-05-01 16:04:22 +0100507 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
508 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
509
Andrew Scull72b43c02019-09-18 13:53:45 +0100510 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100511
512 if (has_fdt) {
513 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
514 pa_addr(fdt_addr));
515 } else {
516 /*
517 * Without an FDT, secondary VMs expect the memory size to be
518 * passed in register x0, which is what
519 * vcpu_secondary_reset_and_start does in this case.
520 */
521 vcpu_secondary_reset_and_start(vcpu, secondary_entry, mem_size);
522 }
523
Andrew Scull3c257452019-11-26 13:32:50 +0000524 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100525
Andrew Scull3c257452019-11-26 13:32:50 +0000526out:
527 vm_unlock(&vm_locked);
528
529 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100530}
531
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100532/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100533 * Try to find a memory range of the given size within the given ranges, and
534 * remove it from them. Return true on success, or false if no large enough
535 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100536 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900537static bool carve_out_mem_range(struct mem_range *mem_ranges,
538 size_t mem_ranges_count, uint64_t size_to_find,
539 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100540{
541 size_t i;
542
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000543 /*
544 * TODO(b/116191358): Consider being cleverer about how we pack VMs
545 * together, with a non-greedy algorithm.
546 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100547 for (i = 0; i < mem_ranges_count; ++i) {
548 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100549 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100550 /*
551 * This range is big enough, take some of it from the
552 * end and reduce its size accordingly.
553 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100554 *found_end = mem_ranges[i].end;
555 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
556 size_to_find);
557 mem_ranges[i].end = *found_begin;
558 return true;
559 }
560 }
561 return false;
562}
563
564/**
565 * Given arrays of memory ranges before and after memory was removed for
566 * secondary VMs, add the difference to the reserved ranges of the given update.
567 * Return true on success, or false if there would be more than MAX_MEM_RANGES
568 * reserved ranges after adding the new ones.
569 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
570 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900571static bool update_reserved_ranges(struct boot_params_update *update,
572 const struct mem_range *before,
573 const struct mem_range *after,
574 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100575{
576 size_t i;
577
578 for (i = 0; i < mem_ranges_count; ++i) {
579 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
580 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000581 dlog_error(
582 "Too many reserved ranges after "
583 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100584 return false;
585 }
586 update->reserved_ranges[update->reserved_ranges_count]
587 .begin = before[i].begin;
588 update->reserved_ranges[update->reserved_ranges_count]
589 .end = after[i].begin;
590 update->reserved_ranges_count++;
591 }
592 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
593 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000594 dlog_error(
595 "Too many reserved ranges after "
596 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100597 return false;
598 }
599 update->reserved_ranges[update->reserved_ranges_count]
600 .begin = after[i].end;
601 update->reserved_ranges[update->reserved_ranges_count]
602 .end = before[i].end;
603 update->reserved_ranges_count++;
604 }
605 }
606
607 return true;
608}
609
Andrew Scull72b43c02019-09-18 13:53:45 +0100610/*
611 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100612 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100613bool load_vms(struct mm_stage1_locked stage1_locked,
614 const struct manifest *manifest, const struct memiter *cpio,
615 const struct boot_params *params,
616 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100617{
Andrew Scull19503262018-09-20 14:48:39 +0100618 struct vm *primary;
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100619 struct vm *tee;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100620 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
Andrew Scull3c257452019-11-26 13:32:50 +0000621 struct vm_locked primary_vm_locked;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100622 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000623 bool success = true;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100624
Andrew Scullae9962e2019-10-03 16:51:16 +0100625 if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
626 cpio, params, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000627 dlog_error("Unable to load primary VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100628 return false;
629 }
630
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100631 /*
632 * Initialise the dummy VM which represents TrustZone, and set up its
633 * RX/TX buffers.
634 */
635 tee = vm_init(HF_TEE_VM_ID, 0, ppool);
636 CHECK(tee != NULL);
637 tee->mailbox.send = &tee_send_buffer;
638 tee->mailbox.recv = &tee_recv_buffer;
639
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100640 static_assert(
641 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
642 "mem_range arrays must be the same size for memcpy.");
643 static_assert(sizeof(mem_ranges_available) < 500,
644 "This will use too much stack, either make "
645 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100646 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
647 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100648
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100649 /* Round the last addresses down to the page size. */
650 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000651 mem_ranges_available[i].end = pa_init(align_down(
652 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100653 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100654
Andrew Scull3c257452019-11-26 13:32:50 +0000655 primary = vm_find(HF_PRIMARY_VM_ID);
656 primary_vm_locked = vm_lock(primary);
657
David Brazdil0251b942019-09-10 15:59:50 +0100658 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100659 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100661 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100662 paddr_t secondary_mem_begin;
663 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100664
David Brazdil7a462ec2019-08-15 12:27:47 +0100665 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100666 continue;
667 }
668
Andrew Walbran17eebf92020-02-05 16:35:49 +0000669 dlog_info("Loading VM%d: %s.\n", (int)vm_id,
670 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100671
David Brazdil7a462ec2019-08-15 12:27:47 +0100672 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100673
674 if (manifest_vm->is_ffa_partition) {
675 secondary_mem_begin =
676 pa_init(manifest_vm->sp.load_addr);
677 secondary_mem_end =
678 pa_init(manifest_vm->sp.load_addr + mem_size);
679 } else if (!carve_out_mem_range(mem_ranges_available,
680 params->mem_ranges_count,
681 mem_size, &secondary_mem_begin,
682 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000683 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100684 continue;
685 }
Andrew Scull80871322018-08-06 12:04:09 +0100686
Manish Pandey2145c212020-05-01 16:04:22 +0100687 if (!load_secondary(stage1_locked, primary_vm_locked,
688 secondary_mem_begin, secondary_mem_end,
689 manifest_vm, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000690 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100691 continue;
692 }
693
694 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000695 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
696 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000697 dlog_error(
698 "Unable to unmap secondary VM from primary "
699 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000700 success = false;
701 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100702 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100703 }
704
Andrew Scull3c257452019-11-26 13:32:50 +0000705 vm_unlock(&primary_vm_locked);
706
707 if (!success) {
708 return false;
709 }
710
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100711 /*
712 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100713 * difference between the available ranges from the original params and
714 * the updated mem_ranges_available. We assume that the number and order
715 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100716 * above only make them smaller.
717 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100718 return update_reserved_ranges(update, params->mem_ranges,
719 mem_ranges_available,
720 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100721}