blob: 606848e32b9b892549026392eaa324bcc52180ff [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 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/load.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
19#include <stdbool.h>
20
Fuad Tabba77a4b012019-11-15 12:13:08 +000021#include "hf/arch/vm.h"
22
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010024#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010025#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/dlog.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010027#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/memiter.h"
29#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010030#include "hf/plat/console.h"
Andrew Scullb1a6d0d2020-01-29 11:25:12 +000031#include "hf/plat/iommu.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010032#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010033#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010034#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010035
Andrew Scull19503262018-09-20 14:48:39 +010036#include "vmapi/hf/call.h"
37
Andrew Walbran9daa57e2019-09-27 13:33:20 +010038alignas(PAGE_SIZE) static uint8_t tee_send_buffer[HF_MAILBOX_SIZE];
39alignas(PAGE_SIZE) static uint8_t tee_recv_buffer[HF_MAILBOX_SIZE];
40
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010041/**
42 * Copies data to an unmapped location by mapping it for write, copying the
43 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000044 *
45 * The data is written so that it is available to all cores with the cache
46 * disabled. When switching to the partitions, the caching is initially disabled
47 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010049static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010050 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010051{
David Brazdil7a462ec2019-08-15 12:27:47 +010052 const void *from = memiter_base(from_it);
53 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010054 paddr_t to_end = pa_add(to, size);
55 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010056
Andrew Scull3c0a90a2019-07-01 11:55:53 +010057 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010058 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010059 return false;
60 }
61
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010062 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010063 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010064
Andrew Scull72b43c02019-09-18 13:53:45 +010065 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010066
67 return true;
68}
69
Andrew Scull72b43c02019-09-18 13:53:45 +010070static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
71 paddr_t end, const struct manifest_vm *manifest_vm,
72 const struct memiter *cpio, struct mpool *ppool)
73{
Andrew Scull72b43c02019-09-18 13:53:45 +010074 struct memiter kernel;
75
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
82 if (pa_difference(begin, end) < memiter_size(&kernel)) {
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
92 return true;
93}
94
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010095/**
Andrew Scullae9962e2019-10-03 16:51:16 +010096 * Performs VM loading activities that are common between the primary and
97 * secondaries.
98 */
99static bool load_common(const struct manifest_vm *manifest_vm, struct vm *vm)
100{
101 vm->smc_whitelist = manifest_vm->smc_whitelist;
102
Fuad Tabba56970712020-01-10 11:20:09 +0000103 /* Initialize architecture-specific features. */
Fuad Tabba77a4b012019-11-15 12:13:08 +0000104 arch_vm_features_set(vm);
105
Andrew Scullae9962e2019-10-03 16:51:16 +0100106 return true;
107}
108
109/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100110 * Loads the primary VM.
111 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100112static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100113 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100114 const struct memiter *cpio,
115 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116{
Olivier Deprez62d99e32020-01-09 15:58:07 +0100117 paddr_t primary_begin;
118 ipaddr_t primary_entry;
David Brazdile6f83222019-09-23 14:47:37 +0100119 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000120 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100121 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100122 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000123 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100124
Olivier Deprez62d99e32020-01-09 15:58:07 +0100125 if (manifest_vm->is_ffa_partition) {
126 primary_begin = pa_init(manifest_vm->sp.load_addr);
127 primary_entry = ipa_add(ipa_from_pa(primary_begin),
128 manifest_vm->sp.ep_offset);
129 } else {
130 primary_begin =
131 (manifest_vm->primary.boot_address ==
132 MANIFEST_INVALID_ADDRESS)
133 ? layout_primary_begin()
134 : pa_init(manifest_vm->primary.boot_address);
135 primary_entry = ipa_from_pa(primary_begin);
136 }
137
David Brazdil080ee312020-02-25 15:30:30 -0800138 paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX);
Andrew Scull72b43c02019-09-18 13:53:45 +0100139
Olivier Deprez62d99e32020-01-09 15:58:07 +0100140 /*
141 * Load the kernel if a filename is specified in the VM manifest.
142 * For an FF-A partition, kernel_filename is undefined indicating
143 * the partition package has already been loaded prior to Hafnium
144 * booting.
145 */
146 if (!string_is_empty(&manifest_vm->kernel_filename)) {
147 if (!load_kernel(stage1_locked, primary_begin, primary_end,
148 manifest_vm, cpio, ppool)) {
149 dlog_error("Unable to load primary kernel.\n");
150 return false;
151 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100152 }
153
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100154 if (!vm_init_next(MAX_CPUS, ppool, &vm)) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000155 dlog_error("Unable to initialise primary VM.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100156 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100157 }
158
David Brazdile6f83222019-09-23 14:47:37 +0100159 if (vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7586e042020-02-18 18:19:26 +0000160 dlog_error("Primary VM was not given correct ID.\n");
David Brazdile6f83222019-09-23 14:47:37 +0100161 return false;
162 }
163
Andrew Scull3c257452019-11-26 13:32:50 +0000164 vm_locked = vm_lock(vm);
165
Andrew Scullae9962e2019-10-03 16:51:16 +0100166 if (!load_common(manifest_vm, vm)) {
Andrew Scull3c257452019-11-26 13:32:50 +0000167 ret = false;
168 goto out;
Andrew Scullae9962e2019-10-03 16:51:16 +0100169 }
170
Andrew Scull48929fd2020-01-28 10:39:10 +0000171 if (params->device_mem_ranges_count == 0) {
172 /*
173 * Map 1TB of address space as device memory to, most likely,
174 * make all devices available to the primary VM.
175 *
176 * TODO: remove this once all targets provide valid ranges.
177 */
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800178 dlog_warning(
179 "Device memory not provided, defaulting to 1 TB.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000180
181 if (!vm_identity_map(
182 vm_locked, pa_init(0),
183 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
184 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
185 dlog_error(
186 "Unable to initialise address space for "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800187 "primary VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000188 ret = false;
189 goto out;
190 }
David Brazdile6f83222019-09-23 14:47:37 +0100191 }
192
Andrew Scullb5f49e02019-10-02 13:20:47 +0100193 /* Map normal memory as such to permit caching, execution, etc. */
194 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000195 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
196 params->mem_ranges[i].end,
197 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
198 NULL)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000199 dlog_error(
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800200 "Unable to initialise memory for primary "
201 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000202 ret = false;
203 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100204 }
205 }
206
Andrew Scull48929fd2020-01-28 10:39:10 +0000207 /* Map device memory as such to prevent execution, speculation etc. */
208 for (i = 0; i < params->device_mem_ranges_count; ++i) {
209 if (!vm_identity_map(
210 vm_locked, params->device_mem_ranges[i].begin,
211 params->device_mem_ranges[i].end,
212 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
213 dlog("Unable to initialise device memory for primary "
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800214 "VM.\n");
Andrew Scull48929fd2020-01-28 10:39:10 +0000215 ret = false;
216 goto out;
217 }
218 }
219
Andrew Scull3c257452019-11-26 13:32:50 +0000220 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800221 dlog_error("Unable to unmap hypervisor from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000222 ret = false;
223 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100224 }
225
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000226 if (!plat_iommu_unmap_iommus(vm_locked, ppool)) {
Andrew Scullf6ab9bc2020-02-26 12:56:37 -0800227 dlog_error("Unable to unmap IOMMUs from primary VM.\n");
Andrew Scullb1a6d0d2020-01-29 11:25:12 +0000228 ret = false;
229 goto out;
230 }
231
Andrew Walbran7586e042020-02-18 18:19:26 +0000232 dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n",
233 vm->vcpu_count, pa_addr(primary_begin));
234
David Brazdile6f83222019-09-23 14:47:37 +0100235 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Olivier Deprez62d99e32020-01-09 15:58:07 +0100236 vcpu_on(vcpu_locked, primary_entry, params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100237 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000238 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100239
Andrew Scull3c257452019-11-26 13:32:50 +0000240out:
241 vm_unlock(&vm_locked);
242
243 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100244}
245
Andrew Scull72b43c02019-09-18 13:53:45 +0100246/*
247 * Loads a secondary VM.
248 */
249static bool load_secondary(struct mm_stage1_locked stage1_locked,
250 paddr_t mem_begin, paddr_t mem_end,
251 const struct manifest_vm *manifest_vm,
252 const struct memiter *cpio, struct mpool *ppool)
253{
254 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000255 struct vm_locked vm_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100256 struct vcpu *vcpu;
257 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000258 bool ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100259
Olivier Deprez62d99e32020-01-09 15:58:07 +0100260 /*
261 * Load the kernel if a filename is specified in the VM manifest.
262 * For an FF-A partition, kernel_filename is undefined indicating
263 * the partition package has already been loaded prior to Hafnium
264 * booting.
265 */
266 if (!string_is_empty(&manifest_vm->kernel_filename)) {
267 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm,
268 cpio, ppool)) {
269 dlog_error("Unable to load kernel.\n");
270 return false;
271 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100272 }
273
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100274 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000275 dlog_error("Unable to initialise VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100276 return false;
277 }
278
Andrew Scullae9962e2019-10-03 16:51:16 +0100279 if (!load_common(manifest_vm, vm)) {
280 return false;
281 }
282
Andrew Scull3c257452019-11-26 13:32:50 +0000283 vm_locked = vm_lock(vm);
284
Andrew Scull72b43c02019-09-18 13:53:45 +0100285 /* Grant the VM access to the memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000286 if (!vm_identity_map(vm_locked, mem_begin, mem_end,
287 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
288 &secondary_entry)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000289 dlog_error("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000290 ret = false;
291 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100292 }
293
Andrew Walbran17eebf92020-02-05 16:35:49 +0000294 dlog_info("Loaded with %u vCPUs, entry at %#x.\n",
295 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
Andrew Scull72b43c02019-09-18 13:53:45 +0100296
Olivier Deprez62d99e32020-01-09 15:58:07 +0100297 if (manifest_vm->is_ffa_partition) {
298 secondary_entry =
299 ipa_add(secondary_entry, manifest_vm->sp.ep_offset);
300 }
301
Andrew Scull72b43c02019-09-18 13:53:45 +0100302 vcpu = vm_get_vcpu(vm, 0);
303 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
304 pa_difference(mem_begin, mem_end));
Andrew Scull3c257452019-11-26 13:32:50 +0000305 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100306
Andrew Scull3c257452019-11-26 13:32:50 +0000307out:
308 vm_unlock(&vm_locked);
309
310 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100311}
312
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100313/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100314 * Try to find a memory range of the given size within the given ranges, and
315 * remove it from them. Return true on success, or false if no large enough
316 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100317 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900318static bool carve_out_mem_range(struct mem_range *mem_ranges,
319 size_t mem_ranges_count, uint64_t size_to_find,
320 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100321{
322 size_t i;
323
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000324 /*
325 * TODO(b/116191358): Consider being cleverer about how we pack VMs
326 * together, with a non-greedy algorithm.
327 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100328 for (i = 0; i < mem_ranges_count; ++i) {
329 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100330 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100331 /*
332 * This range is big enough, take some of it from the
333 * end and reduce its size accordingly.
334 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100335 *found_end = mem_ranges[i].end;
336 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
337 size_to_find);
338 mem_ranges[i].end = *found_begin;
339 return true;
340 }
341 }
342 return false;
343}
344
345/**
346 * Given arrays of memory ranges before and after memory was removed for
347 * secondary VMs, add the difference to the reserved ranges of the given update.
348 * Return true on success, or false if there would be more than MAX_MEM_RANGES
349 * reserved ranges after adding the new ones.
350 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
351 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900352static bool update_reserved_ranges(struct boot_params_update *update,
353 const struct mem_range *before,
354 const struct mem_range *after,
355 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100356{
357 size_t i;
358
359 for (i = 0; i < mem_ranges_count; ++i) {
360 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
361 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000362 dlog_error(
363 "Too many reserved ranges after "
364 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100365 return false;
366 }
367 update->reserved_ranges[update->reserved_ranges_count]
368 .begin = before[i].begin;
369 update->reserved_ranges[update->reserved_ranges_count]
370 .end = after[i].begin;
371 update->reserved_ranges_count++;
372 }
373 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
374 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000375 dlog_error(
376 "Too many reserved ranges after "
377 "loading secondary VMs.\n");
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100378 return false;
379 }
380 update->reserved_ranges[update->reserved_ranges_count]
381 .begin = after[i].end;
382 update->reserved_ranges[update->reserved_ranges_count]
383 .end = before[i].end;
384 update->reserved_ranges_count++;
385 }
386 }
387
388 return true;
389}
390
Andrew Scull72b43c02019-09-18 13:53:45 +0100391/*
392 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100393 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100394bool load_vms(struct mm_stage1_locked stage1_locked,
395 const struct manifest *manifest, const struct memiter *cpio,
396 const struct boot_params *params,
397 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100398{
Andrew Scull19503262018-09-20 14:48:39 +0100399 struct vm *primary;
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100400 struct vm *tee;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100401 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
Andrew Scull3c257452019-11-26 13:32:50 +0000402 struct vm_locked primary_vm_locked;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100403 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000404 bool success = true;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100405
Andrew Scullae9962e2019-10-03 16:51:16 +0100406 if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
407 cpio, params, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000408 dlog_error("Unable to load primary VM.\n");
Andrew Scull72b43c02019-09-18 13:53:45 +0100409 return false;
410 }
411
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100412 /*
413 * Initialise the dummy VM which represents TrustZone, and set up its
414 * RX/TX buffers.
415 */
416 tee = vm_init(HF_TEE_VM_ID, 0, ppool);
417 CHECK(tee != NULL);
418 tee->mailbox.send = &tee_send_buffer;
419 tee->mailbox.recv = &tee_recv_buffer;
420
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100421 static_assert(
422 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
423 "mem_range arrays must be the same size for memcpy.");
424 static_assert(sizeof(mem_ranges_available) < 500,
425 "This will use too much stack, either make "
426 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100427 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
428 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100429
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100430 /* Round the last addresses down to the page size. */
431 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000432 mem_ranges_available[i].end = pa_init(align_down(
433 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100434 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100435
Andrew Scull3c257452019-11-26 13:32:50 +0000436 primary = vm_find(HF_PRIMARY_VM_ID);
437 primary_vm_locked = vm_lock(primary);
438
David Brazdil0251b942019-09-10 15:59:50 +0100439 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100440 const struct manifest_vm *manifest_vm = &manifest->vm[i];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100441 ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100442 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100443 paddr_t secondary_mem_begin;
444 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100445
David Brazdil7a462ec2019-08-15 12:27:47 +0100446 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100447 continue;
448 }
449
Andrew Walbran17eebf92020-02-05 16:35:49 +0000450 dlog_info("Loading VM%d: %s.\n", (int)vm_id,
451 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100452
David Brazdil7a462ec2019-08-15 12:27:47 +0100453 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
Olivier Deprez62d99e32020-01-09 15:58:07 +0100454
455 if (manifest_vm->is_ffa_partition) {
456 secondary_mem_begin =
457 pa_init(manifest_vm->sp.load_addr);
458 secondary_mem_end =
459 pa_init(manifest_vm->sp.load_addr + mem_size);
460 } else if (!carve_out_mem_range(mem_ranges_available,
461 params->mem_ranges_count,
462 mem_size, &secondary_mem_begin,
463 &secondary_mem_end)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000464 dlog_error("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100465 continue;
466 }
Andrew Scull80871322018-08-06 12:04:09 +0100467
Andrew Scull72b43c02019-09-18 13:53:45 +0100468 if (!load_secondary(stage1_locked, secondary_mem_begin,
469 secondary_mem_end, manifest_vm, cpio,
470 ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000471 dlog_error("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100472 continue;
473 }
474
475 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000476 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
477 secondary_mem_end, ppool)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000478 dlog_error(
479 "Unable to unmap secondary VM from primary "
480 "VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000481 success = false;
482 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100483 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100484 }
485
Andrew Scull3c257452019-11-26 13:32:50 +0000486 vm_unlock(&primary_vm_locked);
487
488 if (!success) {
489 return false;
490 }
491
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100492 /*
493 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100494 * difference between the available ranges from the original params and
495 * the updated mem_ranges_available. We assume that the number and order
496 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100497 * above only make them smaller.
498 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100499 return update_reserved_ranges(update, params->mem_ranges,
500 mem_ranges_available,
501 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100502}