blob: 5501e1216bdff3894e79b1d73e072665ce0dfadc [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
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100155 vm_locked.vm->managed_exit = manifest_vm->sp.managed_exit;
156
J-Alvesb37fd082020-10-22 12:29:21 +0100157 vm_locked.vm->boot_order = manifest_vm->sp.boot_order;
158 /* Updating boot list according to boot_order */
159 vm_update_boot(vm_locked.vm);
J-Alvesa0f317d2021-06-09 13:31:59 +0100160
161 /* TODO: Enable in accordance to VM's manifest. */
162 vm_locked.vm->notifications.enabled = true;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100163 }
J-Alvesb37fd082020-10-22 12:29:21 +0100164
Fuad Tabba56970712020-01-10 11:20:09 +0000165 /* Initialize architecture-specific features. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100166 arch_vm_features_set(vm_locked.vm);
Fuad Tabba77a4b012019-11-15 12:13:08 +0000167
Madhukar Pappireddy54680c72020-10-23 15:02:38 -0500168 if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm,
169 ppool)) {
170 dlog_error("Unable to attach upstream peripheral device\n");
171 return false;
172 }
173
Andrew Scullae9962e2019-10-03 16:51:16 +0100174 return true;
175}
176
177/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100178 * Loads the primary VM.
179 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100180static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100181 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100182 const struct memiter *cpio,
183 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100184{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100185 paddr_t primary_begin;
186 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100187 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000188 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100189 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100190 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000191 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100192
Olivier Deprez62d99e32020-01-09 15:58:07 +0100193 if (manifest_vm->is_ffa_partition) {
194 primary_begin = pa_init(manifest_vm->sp.load_addr);
195 primary_entry = ipa_add(ipa_from_pa(primary_begin),
196 manifest_vm->sp.ep_offset);
197 } else {
198 primary_begin =
199 (manifest_vm->primary.boot_address ==
200 MANIFEST_INVALID_ADDRESS)
201 ? layout_primary_begin()
202 : pa_init(manifest_vm->primary.boot_address);
203 primary_entry = ipa_from_pa(primary_begin);
204 }
205
David Brazdil080ee312020-02-25 15:30:30 -0800206 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100207
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800208 /* Primary VM must be a VM */
209 CHECK(manifest_vm->sp.run_time_el == EL1);
210
Olivier Deprez62d99e32020-01-09 15:58:07 +0100211 /*
212 * Load the kernel if a filename is specified in the VM manifest.
213 * For an FF-A partition, kernel_filename is undefined indicating
214 * the partition package has already been loaded prior to Hafnium
215 * booting.
216 */
217 if (!string_is_empty(&manifest_vm->kernel_filename)) {
218 if (!load_kernel(stage1_locked, primary_begin, primary_end,
Fuad Tabba50469e02020-06-30 15:14:28 +0100219 manifest_vm, cpio, ppool, NULL)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100220 dlog_error("Unable to load primary kernel.\n");
221 return false;
222 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100223 }
224
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800225 if (!vm_init_next(MAX_CPUS, ppool, &vm, false)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000226 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100227 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100228 }
229
David Brazdile6f83222019-09-23 14:47:37 +0100230 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000231 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100232 return false;
233 }
234
Andrew Scull3c257452019-11-26 13:32:50 +0000235 vm_locked = vm_lock(vm);
236
Andrew Scull48929fd2020-01-28 10:39:10 +0000237 if (params->device_mem_ranges_count == 0) {
238 /*
239 * Map 1TB of address space as device memory to, most likely,
240 * make all devices available to the primary VM.
241 *
242 * TODO: remove this once all targets provide valid ranges.
243 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800244 dlog_warning(
245 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000246
247 if (!vm_identity_map(
248 vm_locked, pa_init(0),
249 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
250 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
251 dlog_error(
252 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800253 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000254 ret = false;
255 goto out;
256 }
David Brazdile6f83222019-09-23 14:47:37 +0100257 }
258
Andrew Scullb5f49e02019-10-02 13:20:47 +0100259 /* Map normal memory as such to permit caching, execution, etc. */
260 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000261 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
262 params->mem_ranges[i].end,
263 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
264 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000265 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800266 "Unable to initialise memory for primary "
267 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000268 ret = false;
269 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100270 }
271 }
272
Andrew Scull48929fd2020-01-28 10:39:10 +0000273 /* Map device memory as such to prevent execution, speculation etc. */
274 for (i = 0; i < params->device_mem_ranges_count; ++i) {
275 if (!vm_identity_map(
276 vm_locked, params->device_mem_ranges[i].begin,
277 params->device_mem_ranges[i].end,
278 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
279 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800280 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000281 ret = false;
282 goto out;
283 }
284 }
285
Manish Pandeyd34f8892020-06-19 17:41:07 +0100286 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
287 ret = false;
288 goto out;
289 }
290
Andrew Scull3c257452019-11-26 13:32:50 +0000291 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800292 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000293 ret = false;
294 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100295 }
296
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000297 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800298 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000299 ret = false;
300 goto out;
301 }
302
Andrew Walbran7586e042020-02-18 18:19:26 +0000303 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
304 vm->vcpu_count, pa_addr(primary_begin));
305
Olivier Deprezb9adff42021-02-01 12:14:05 +0100306 /* Mark the primary to be the first booted VM */
307 vm_update_boot(vm);
308
David Brazdile6f83222019-09-23 14:47:37 +0100309 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100310 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100311 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000312 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100313
Andrew Scull3c257452019-11-26 13:32:50 +0000314out:
315 vm_unlock(&vm_locked);
316
317 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100318}
319
Fuad Tabba50469e02020-06-30 15:14:28 +0100320/**
321 * Loads the secondary VM's FDT.
322 * Stores the total allocated size for the FDT in fdt_allocated_size (if
323 * fdt_allocated_size is not NULL). The allocated size includes additional space
324 * for potential patching.
325 */
326static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked,
327 paddr_t end, size_t fdt_max_size,
328 const struct manifest_vm *manifest_vm,
329 const struct memiter *cpio, struct mpool *ppool,
330 paddr_t *fdt_addr, size_t *fdt_allocated_size)
331{
332 struct memiter fdt;
333 size_t allocated_size;
334
335 CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename));
336
337 if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) {
338 dlog_error("Cannot open the secondary VM's FDT.\n");
339 return false;
340 }
341
342 /*
343 * Ensure the FDT has one additional page at the end for patching, and
344 * and align it to the page boundary.
345 */
346 allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE;
347
348 if (allocated_size > fdt_max_size) {
349 dlog_error(
350 "FDT allocated space (%u) is more than the specified "
351 "maximum to use (%u).\n",
352 allocated_size, fdt_max_size);
353 return false;
354 }
355
356 /* Load the FDT to the end of the VM's allocated memory space. */
357 *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size)));
358
359 dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n",
360 allocated_size, pa_addr(*fdt_addr));
361
362 if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) {
363 dlog_error("Unable to copy FDT.\n");
364 return false;
365 }
366
367 if (fdt_allocated_size) {
368 *fdt_allocated_size = allocated_size;
369 }
370
371 return true;
372}
373
Andrew Scull72b43c02019-09-18 13:53:45 +0100374/*
375 * Loads a secondary VM.
376 */
377static bool load_secondary(struct mm_stage1_locked stage1_locked,
Manish Pandey2145c212020-05-01 16:04:22 +0100378 struct vm_locked primary_vm_locked,
Andrew Scull72b43c02019-09-18 13:53:45 +0100379 paddr_t mem_begin, paddr_t mem_end,
380 const struct manifest_vm *manifest_vm,
381 const struct memiter *cpio, struct mpool *ppool)
382{
383 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000384 struct vm_locked vm_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +0100385 struct vcpu_locked vcpu_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100386 struct vcpu *vcpu;
387 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000388 bool ret;
Fuad Tabba50469e02020-06-30 15:14:28 +0100389 paddr_t fdt_addr;
390 bool has_fdt;
391 size_t kernel_size = 0;
392 const size_t mem_size = pa_difference(mem_begin, mem_end);
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800393 uint32_t map_mode;
Andrew Scull72b43c02019-09-18 13:53:45 +0100394
Olivier Deprez62d99e32020-01-09 15:58:07 +0100395 /*
396 * Load the kernel if a filename is specified in the VM manifest.
397 * For an FF-A partition, kernel_filename is undefined indicating
398 * the partition package has already been loaded prior to Hafnium
399 * booting.
400 */
401 if (!string_is_empty(&manifest_vm->kernel_filename)) {
402 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
Fuad Tabba50469e02020-06-30 15:14:28 +0100403 cpio, ppool, &kernel_size)) {
Olivier Deprez62d99e32020-01-09 15:58:07 +0100404 dlog_error("Unable to load kernel.\n");
405 return false;
406 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100407 }
408
Fuad Tabba50469e02020-06-30 15:14:28 +0100409 has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename);
410 if (has_fdt) {
411 /*
412 * Ensure that the FDT does not overwrite the kernel or overlap
413 * its page, for the FDT to start at a page boundary.
414 */
415 const size_t fdt_max_size =
416 mem_size - align_up(kernel_size, PAGE_SIZE);
417
418 size_t fdt_allocated_size;
419
420 if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size,
421 manifest_vm, cpio, ppool, &fdt_addr,
422 &fdt_allocated_size)) {
423 dlog_error("Unable to load FDT.\n");
424 return false;
425 }
426
427 if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size,
428 mem_begin, mem_end, ppool)) {
429 dlog_error("Unable to patch FDT.\n");
430 return false;
431 }
432 }
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800433 /*
434 * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the
435 * FF-A 1.0 spec.
436 */
437 CHECK(manifest_vm->sp.run_time_el != S_EL0 ||
438 manifest_vm->secondary.vcpu_count == 1);
Fuad Tabba50469e02020-06-30 15:14:28 +0100439
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800440 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm,
441 (manifest_vm->sp.run_time_el == S_EL0))) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000442 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100443 return false;
444 }
445
Andrew Scull3c257452019-11-26 13:32:50 +0000446 vm_locked = vm_lock(vm);
447
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800448 /*
449 * Grant the VM access to the memory. TODO: For S-EL0 partitions,
450 * mapping all of its memory as RWX is bad from a security standpoint.
451 * Should just skip this and expect this to be present in the memory
452 * regions?
453 */
454 map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
455 if (vm->el0_partition) {
456 map_mode |= MM_MODE_USER | MM_MODE_NG;
457 }
458 if (!vm_identity_map(vm_locked, mem_begin, mem_end, map_mode, ppool,
Andrew Scull3c257452019-11-26 13:32:50 +0000459 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000460 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000461 ret = false;
462 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100463 }
464
Olivier Deprez62d99e32020-01-09 15:58:07 +0100465 if (manifest_vm->is_ffa_partition) {
Manish Pandey2145c212020-05-01 16:04:22 +0100466 int j = 0;
467 paddr_t region_begin;
468 paddr_t region_end;
469 paddr_t alloc_base = mem_end;
470 size_t size;
471 size_t total_alloc = 0;
472
473 /* Map memory-regions */
474 while (j < manifest_vm->sp.mem_region_count) {
475 size = manifest_vm->sp.mem_regions[j].page_count *
476 PAGE_SIZE;
477 /*
478 * For memory-regions without base-address, memory
479 * should be allocated inside partition's page table.
480 * Start allocating memory regions in partition's
481 * page table, starting from the end.
482 * TODO: Add mechanism to let partition know of these
483 * memory regions
484 */
485 if (manifest_vm->sp.mem_regions[j].base_address ==
486 MANIFEST_INVALID_ADDRESS) {
487 total_alloc += size;
488 /* Don't go beyond half the VM's memory space */
489 if (total_alloc >
490 (manifest_vm->secondary.mem_size / 2)) {
491 dlog_error(
492 "Not enough space for memory-"
493 "region allocation");
494 ret = false;
495 goto out;
496 }
497
498 region_end = alloc_base;
499 region_begin = pa_subtract(alloc_base, size);
500 alloc_base = region_begin;
501
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800502 map_mode = manifest_vm->sp.mem_regions[j]
503 .attributes;
504 if (vm->el0_partition) {
505 map_mode |= MM_MODE_USER | MM_MODE_NG;
506 }
507
508 if (!vm_identity_map(vm_locked, region_begin,
509 region_end, map_mode,
510 ppool, NULL)) {
Manish Pandey2145c212020-05-01 16:04:22 +0100511 dlog_error(
512 "Unable to map secondary VM "
513 "memory-region.\n");
514 ret = false;
515 goto out;
516 }
517
518 dlog_info(
519 " Memory region %#x - %#x allocated\n",
520 region_begin, region_end);
521 } else {
522 /*
523 * Identity map memory region for both case,
524 * VA(S-EL0) or IPA(S-EL1).
525 */
526 region_begin =
527 pa_init(manifest_vm->sp.mem_regions[j]
528 .base_address);
529 region_end = pa_add(region_begin, size);
530
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800531 map_mode = manifest_vm->sp.mem_regions[j]
532 .attributes;
533 if (vm->el0_partition) {
534 map_mode |= MM_MODE_USER | MM_MODE_NG;
535 }
536
537 if (!vm_identity_map(vm_locked, region_begin,
538 region_end, map_mode,
539 ppool, NULL)) {
Manish Pandey2145c212020-05-01 16:04:22 +0100540 dlog_error(
541 "Unable to map secondary VM "
542 "memory-region.\n");
543 ret = false;
544 goto out;
545 }
546 }
547
548 /* Deny the primary VM access to this memory */
549 if (!vm_unmap(primary_vm_locked, region_begin,
550 region_end, ppool)) {
551 dlog_error(
552 "Unable to unmap secondary VM memory-"
553 "region from primary VM.\n");
554 ret = false;
555 goto out;
556 }
557
558 j++;
559 }
560
561 /* Map device-regions */
562 j = 0;
563 while (j < manifest_vm->sp.dev_region_count) {
564 region_begin = pa_init(
565 manifest_vm->sp.dev_regions[j].base_address);
566 size = manifest_vm->sp.dev_regions[j].page_count *
567 PAGE_SIZE;
568 region_end = pa_add(region_begin, size);
569
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800570 map_mode = manifest_vm->sp.dev_regions[j].attributes;
571 if (vm->el0_partition) {
572 map_mode |= MM_MODE_USER | MM_MODE_NG;
573 }
574
575 if (!vm_identity_map(vm_locked, region_begin,
576 region_end, map_mode, ppool,
577 NULL)) {
Manish Pandey2145c212020-05-01 16:04:22 +0100578 dlog_error(
579 "Unable to map secondary VM "
580 "device-region.\n");
581 ret = false;
582 goto out;
583 }
584 /* Deny primary VM access to this region */
585 if (!vm_unmap(primary_vm_locked, region_begin,
586 region_end, ppool)) {
587 dlog_error(
588 "Unable to unmap secondary VM device-"
589 "region from primary VM.\n");
590 ret = false;
591 goto out;
592 }
593 j++;
594 }
595
Olivier Deprez62d99e32020-01-09 15:58:07 +0100596 secondary_entry =
597 ipa_add(secondary_entry, manifest_vm->sp.ep_offset);
598 }
599
Raghu Krishnamurthyd3ab8c32021-02-10 19:11:30 -0800600 /*
601 * Map hypervisor into the VM's page table. The hypervisor pages will
602 * not be accessible from EL0 since it will not be marked for user
603 * access.
604 * TODO: Map only the exception vectors and data that exception vectors
605 * require and not the entire hypervisor. This helps with speculative
606 * side-channel attacks.
607 */
608 if (vm->el0_partition) {
609 CHECK(vm_identity_map(vm_locked, layout_text_begin(),
610 layout_text_end(), MM_MODE_X, ppool,
611 NULL));
612
613 CHECK(vm_identity_map(vm_locked, layout_rodata_begin(),
614 layout_rodata_end(), MM_MODE_R, ppool,
615 NULL));
616
617 CHECK(vm_identity_map(vm_locked, layout_data_begin(),
618 layout_data_end(), MM_MODE_R | MM_MODE_W,
619 ppool, NULL));
620 plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool);
621 }
622
Manish Pandeyd34f8892020-06-19 17:41:07 +0100623 if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) {
624 ret = false;
625 goto out;
626 }
627
Manish Pandey2145c212020-05-01 16:04:22 +0100628 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
629 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
630
Andrew Scull72b43c02019-09-18 13:53:45 +0100631 vcpu = vm_get_vcpu(vm, 0);
Fuad Tabba50469e02020-06-30 15:14:28 +0100632
Max Shvetsov40108e72020-08-27 12:39:50 +0100633 vcpu_locked = vcpu_lock(vcpu);
Fuad Tabba50469e02020-06-30 15:14:28 +0100634 if (has_fdt) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100635 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
Fuad Tabba50469e02020-06-30 15:14:28 +0100636 pa_addr(fdt_addr));
637 } else {
638 /*
639 * Without an FDT, secondary VMs expect the memory size to be
640 * passed in register x0, which is what
641 * vcpu_secondary_reset_and_start does in this case.
642 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100643 vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry,
644 mem_size);
Fuad Tabba50469e02020-06-30 15:14:28 +0100645 }
646
Max Shvetsov40108e72020-08-27 12:39:50 +0100647 vcpu_unlock(&vcpu_locked);
648
Andrew Scull3c257452019-11-26 13:32:50 +0000649 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100650
Andrew Scull3c257452019-11-26 13:32:50 +0000651out:
652 vm_unlock(&vm_locked);
653
654 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100655}
656
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100657/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100658 * Try to find a memory range of the given size within the given ranges, and
659 * remove it from them. Return true on success, or false if no large enough
660 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100661 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900662static bool carve_out_mem_range(struct mem_range *mem_ranges,
663 size_t mem_ranges_count, uint64_t size_to_find,
664 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100665{
666 size_t i;
667
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000668 /*
669 * TODO(b/116191358): Consider being cleverer about how we pack VMs
670 * together, with a non-greedy algorithm.
671 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100672 for (i = 0; i < mem_ranges_count; ++i) {
673 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100674 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100675 /*
676 * This range is big enough, take some of it from the
677 * end and reduce its size accordingly.
678 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100679 *found_end = mem_ranges[i].end;
680 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
681 size_to_find);
682 mem_ranges[i].end = *found_begin;
683 return true;
684 }
685 }
686 return false;
687}
688
689/**
690 * Given arrays of memory ranges before and after memory was removed for
691 * secondary VMs, add the difference to the reserved ranges of the given update.
692 * Return true on success, or false if there would be more than MAX_MEM_RANGES
693 * reserved ranges after adding the new ones.
694 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
695 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900696static bool update_reserved_ranges(struct boot_params_update *update,
697 const struct mem_range *before,
698 const struct mem_range *after,
699 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100700{
701 size_t i;
702
703 for (i = 0; i < mem_ranges_count; ++i) {
704 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
705 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000706 dlog_error(
707 "Too many reserved ranges after "
708 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100709 return false;
710 }
711 update->reserved_ranges[update->reserved_ranges_count]
712 .begin = before[i].begin;
713 update->reserved_ranges[update->reserved_ranges_count]
714 .end = after[i].begin;
715 update->reserved_ranges_count++;
716 }
717 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
718 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000719 dlog_error(
720 "Too many reserved ranges after "
721 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100722 return false;
723 }
724 update->reserved_ranges[update->reserved_ranges_count]
725 .begin = after[i].end;
726 update->reserved_ranges[update->reserved_ranges_count]
727 .end = before[i].end;
728 update->reserved_ranges_count++;
729 }
730 }
731
732 return true;
733}
734
Olivier Deprez112d2b52020-09-30 07:39:23 +0200735static bool init_other_world_vm(struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100736{
Olivier Deprez96a2a262020-06-11 17:21:38 +0200737 struct vm *other_world_vm;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100738 size_t i;
Andrew Scull72b43c02019-09-18 13:53:45 +0100739
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100740 /*
Olivier Deprez96a2a262020-06-11 17:21:38 +0200741 * Initialise the dummy VM which represents the opposite world:
742 * -TrustZone (or the SPMC) when running the Hypervisor
743 * -the Hypervisor when running TZ/SPMC
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100744 */
Raghu Krishnamurthycd1eceb2021-01-04 12:20:48 -0800745 other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false);
Olivier Deprez96a2a262020-06-11 17:21:38 +0200746 CHECK(other_world_vm != NULL);
747
748 for (i = 0; i < MAX_CPUS; i++) {
749 struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i);
750 struct cpu *cpu = cpu_find_index(i);
751
752 vcpu->cpu = cpu;
753 }
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100754
Olivier Deprez112d2b52020-09-30 07:39:23 +0200755 return arch_other_world_vm_init(other_world_vm, ppool);
756}
757
758/*
759 * Loads alls VMs from the manifest.
760 */
761bool load_vms(struct mm_stage1_locked stage1_locked,
762 const struct manifest *manifest, const struct memiter *cpio,
763 const struct boot_params *params,
764 struct boot_params_update *update, struct mpool *ppool)
765{
766 struct vm *primary;
767 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
768 struct vm_locked primary_vm_locked;
769 size_t i;
770 bool success = true;
771
Andrew Walbranf8716782020-10-29 17:07:07 +0000772 /**
773 * Only try to load the primary VM if it is supposed to be in this
774 * world.
775 */
776 if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
777 if (!load_primary(stage1_locked,
778 &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
779 params, ppool)) {
780 dlog_error("Unable to load primary VM.\n");
781 return false;
782 }
Olivier Deprez112d2b52020-09-30 07:39:23 +0200783 }
784
785 if (!init_other_world_vm(ppool)) {
786 return false;
787 }
788
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100789 static_assert(
790 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
791 "mem_range arrays must be the same size for memcpy.");
792 static_assert(sizeof(mem_ranges_available) < 500,
793 "This will use too much stack, either make "
794 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100795 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
796 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100797
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100798 /* Round the last addresses down to the page size. */
799 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000800 mem_ranges_available[i].end = pa_init(align_down(
801 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100802 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100803
Andrew Scull3c257452019-11-26 13:32:50 +0000804 primary = vm_find(HF_PRIMARY_VM_ID);
805 primary_vm_locked = vm_lock(primary);
806
David Brazdil0251b942019-09-10 15:59:50 +0100807 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100808 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100810 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100811 paddr_t secondary_mem_begin;
812 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100813
David Brazdil7a462ec2019-08-15 12:27:47 +0100814 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100815 continue;
816 }
817
Olivier Deprez2a8ee342020-08-03 15:10:44 +0200818 dlog_info("Loading VM id %#x: %s.\n", vm_id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000819 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100820
David Brazdil7a462ec2019-08-15 12:27:47 +0100821 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100822
823 if (manifest_vm->is_ffa_partition) {
824 secondary_mem_begin =
825 pa_init(manifest_vm->sp.load_addr);
826 secondary_mem_end =
827 pa_init(manifest_vm->sp.load_addr + mem_size);
828 } else if (!carve_out_mem_range(mem_ranges_available,
829 params->mem_ranges_count,
830 mem_size, &secondary_mem_begin,
831 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000832 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100833 continue;
834 }
Andrew Scull80871322018-08-06 12:04:09 +0100835
Manish Pandey2145c212020-05-01 16:04:22 +0100836 if (!load_secondary(stage1_locked, primary_vm_locked,
837 secondary_mem_begin, secondary_mem_end,
838 manifest_vm, cpio, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000839 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100840 continue;
841 }
842
843 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000844 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
845 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000846 dlog_error(
847 "Unable to unmap secondary VM from primary "
848 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000849 success = false;
850 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100851 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100852 }
853
Andrew Scull3c257452019-11-26 13:32:50 +0000854 vm_unlock(&primary_vm_locked);
855
856 if (!success) {
857 return false;
858 }
859
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100860 /*
861 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100862 * difference between the available ranges from the original params and
863 * the updated mem_ranges_available. We assume that the number and order
864 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100865 * above only make them smaller.
866 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100867 return update_reserved_ranges(update, params->mem_ranges,
868 mem_ranges_available,
869 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100870}