blob: e2a272e0f17141f38b44be6e070361504c1c03d7 [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 Scull877ae4b2019-07-02 12:52:33 +010031#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010032#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010033#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034
Andrew Scull19503262018-09-20 14:48:39 +010035#include "vmapi/hf/call.h"
36
Andrew Walbran9daa57e2019-09-27 13:33:20 +010037alignas(PAGE_SIZE) static uint8_t tee_send_buffer[HF_MAILBOX_SIZE];
38alignas(PAGE_SIZE) static uint8_t tee_recv_buffer[HF_MAILBOX_SIZE];
39
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010040/**
41 * Copies data to an unmapped location by mapping it for write, copying the
42 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000043 *
44 * The data is written so that it is available to all cores with the cache
45 * disabled. When switching to the partitions, the caching is initially disabled
46 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010047 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010048static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010049 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010050{
David Brazdil7a462ec2019-08-15 12:27:47 +010051 const void *from = memiter_base(from_it);
52 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010053 paddr_t to_end = pa_add(to, size);
54 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010055
Andrew Scull3c0a90a2019-07-01 11:55:53 +010056 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010057 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010058 return false;
59 }
60
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010061 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010062 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010063
Andrew Scull72b43c02019-09-18 13:53:45 +010064 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010065
66 return true;
67}
68
Andrew Scull72b43c02019-09-18 13:53:45 +010069static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
70 paddr_t end, const struct manifest_vm *manifest_vm,
71 const struct memiter *cpio, struct mpool *ppool)
72{
Andrew Scull72b43c02019-09-18 13:53:45 +010073 struct memiter kernel;
74
David Brazdil136f2942019-09-23 14:11:03 +010075 if (string_is_empty(&manifest_vm->kernel_filename)) {
Andrew Scull72b43c02019-09-18 13:53:45 +010076 /* This signals the kernel has been preloaded. */
77 return true;
78 }
79
David Brazdil136f2942019-09-23 14:11:03 +010080 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Scull72b43c02019-09-18 13:53:45 +010081 dlog("Could not find kernel file \"%s\".\n",
David Brazdil136f2942019-09-23 14:11:03 +010082 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010083 return false;
84 }
85
86 if (pa_difference(begin, end) < memiter_size(&kernel)) {
87 dlog("Kernel is larger than available memory.\n");
88 return false;
89 }
90
91 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
92 dlog("Unable to copy kernel.\n");
93 return false;
94 }
95
96 return true;
97}
98
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010099/**
Andrew Scullae9962e2019-10-03 16:51:16 +0100100 * Performs VM loading activities that are common between the primary and
101 * secondaries.
102 */
103static bool load_common(const struct manifest_vm *manifest_vm, struct vm *vm)
104{
105 vm->smc_whitelist = manifest_vm->smc_whitelist;
106
Fuad Tabba56970712020-01-10 11:20:09 +0000107 /* Initialize architecture-specific features. */
Fuad Tabba77a4b012019-11-15 12:13:08 +0000108 arch_vm_features_set(vm);
109
Andrew Scullae9962e2019-10-03 16:51:16 +0100110 return true;
111}
112
113/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100114 * Loads the primary VM.
115 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100116static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100117 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100118 const struct memiter *cpio,
119 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100120{
Andrew Scullf16c0c22018-10-26 18:41:24 +0100121 paddr_t primary_begin = layout_primary_begin();
David Brazdile6f83222019-09-23 14:47:37 +0100122 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000123 struct vm_locked vm_locked;
David Brazdile6f83222019-09-23 14:47:37 +0100124 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100125 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000126 bool ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100127
Andrew Scull72b43c02019-09-18 13:53:45 +0100128 /*
129 * TODO: This bound is currently meaningless but will be addressed when
130 * the manifest specifies the load address.
131 */
132 paddr_t primary_end = pa_add(primary_begin, 0x8000000);
133
Andrew Scullae9962e2019-10-03 16:51:16 +0100134 if (!load_kernel(stage1_locked, primary_begin, primary_end, manifest_vm,
135 cpio, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100136 dlog("Unable to load primary kernel.");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100137 return false;
138 }
139
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100140 if (!vm_init_next(MAX_CPUS, ppool, &vm)) {
David Brazdile6f83222019-09-23 14:47:37 +0100141 dlog("Unable to initialise primary vm\n");
142 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100143 }
144
David Brazdile6f83222019-09-23 14:47:37 +0100145 if (vm->id != HF_PRIMARY_VM_ID) {
146 dlog("Primary vm was not given correct id\n");
147 return false;
148 }
149
Andrew Scull3c257452019-11-26 13:32:50 +0000150 vm_locked = vm_lock(vm);
151
Andrew Scullae9962e2019-10-03 16:51:16 +0100152 if (!load_common(manifest_vm, vm)) {
Andrew Scull3c257452019-11-26 13:32:50 +0000153 ret = false;
154 goto out;
Andrew Scullae9962e2019-10-03 16:51:16 +0100155 }
156
Andrew Scullb5f49e02019-10-02 13:20:47 +0100157 /*
158 * Map 1TB of address space as device memory to, most likely, make all
159 * devices available to the primary VM.
160 *
161 * TODO: We should do a whitelist rather than a blacklist.
162 */
Andrew Scull3c257452019-11-26 13:32:50 +0000163 if (!vm_identity_map(vm_locked, pa_init(0),
164 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
165 MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) {
Andrew Scullb5f49e02019-10-02 13:20:47 +0100166 dlog("Unable to initialise address space for primary vm\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000167 ret = false;
168 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100169 }
170
Andrew Scullb5f49e02019-10-02 13:20:47 +0100171 /* Map normal memory as such to permit caching, execution, etc. */
172 for (i = 0; i < params->mem_ranges_count; ++i) {
Andrew Scull3c257452019-11-26 13:32:50 +0000173 if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin,
174 params->mem_ranges[i].end,
175 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
176 NULL)) {
Andrew Scullb5f49e02019-10-02 13:20:47 +0100177 dlog("Unable to initialise memory for primary vm\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000178 ret = false;
179 goto out;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100180 }
181 }
182
Andrew Scull3c257452019-11-26 13:32:50 +0000183 if (!vm_unmap_hypervisor(vm_locked, ppool)) {
David Brazdile6f83222019-09-23 14:47:37 +0100184 dlog("Unable to unmap hypervisor from primary vm\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000185 ret = false;
186 goto out;
David Brazdile6f83222019-09-23 14:47:37 +0100187 }
188
189 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Andrew Scullb5f49e02019-10-02 13:20:47 +0100190 vcpu_on(vcpu_locked, ipa_from_pa(primary_begin), params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100191 vcpu_unlock(&vcpu_locked);
Andrew Scull3c257452019-11-26 13:32:50 +0000192 ret = true;
David Brazdile6f83222019-09-23 14:47:37 +0100193
Andrew Scull3c257452019-11-26 13:32:50 +0000194out:
195 vm_unlock(&vm_locked);
196
197 return ret;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100198}
199
Andrew Scull72b43c02019-09-18 13:53:45 +0100200/*
201 * Loads a secondary VM.
202 */
203static bool load_secondary(struct mm_stage1_locked stage1_locked,
204 paddr_t mem_begin, paddr_t mem_end,
205 const struct manifest_vm *manifest_vm,
206 const struct memiter *cpio, struct mpool *ppool)
207{
208 struct vm *vm;
Andrew Scull3c257452019-11-26 13:32:50 +0000209 struct vm_locked vm_locked;
Andrew Scull72b43c02019-09-18 13:53:45 +0100210 struct vcpu *vcpu;
211 ipaddr_t secondary_entry;
Andrew Scull3c257452019-11-26 13:32:50 +0000212 bool ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100213
214 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, cpio,
215 ppool)) {
216 dlog("Unable to load kernel.\n");
217 return false;
218 }
219
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100220 if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100221 dlog("Unable to initialise VM.\n");
222 return false;
223 }
224
Andrew Scullae9962e2019-10-03 16:51:16 +0100225 if (!load_common(manifest_vm, vm)) {
226 return false;
227 }
228
Andrew Scull3c257452019-11-26 13:32:50 +0000229 vm_locked = vm_lock(vm);
230
Andrew Scull72b43c02019-09-18 13:53:45 +0100231 /* Grant the VM access to the memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000232 if (!vm_identity_map(vm_locked, mem_begin, mem_end,
233 MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool,
234 &secondary_entry)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100235 dlog("Unable to initialise memory.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000236 ret = false;
237 goto out;
Andrew Scull72b43c02019-09-18 13:53:45 +0100238 }
239
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000240 dlog("Loaded with %u vCPUs, entry at %#x.\n",
Andrew Scull72b43c02019-09-18 13:53:45 +0100241 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
242
243 vcpu = vm_get_vcpu(vm, 0);
244 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
245 pa_difference(mem_begin, mem_end));
Andrew Scull3c257452019-11-26 13:32:50 +0000246 ret = true;
Andrew Scull72b43c02019-09-18 13:53:45 +0100247
Andrew Scull3c257452019-11-26 13:32:50 +0000248out:
249 vm_unlock(&vm_locked);
250
251 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100252}
253
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100254/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100255 * Try to find a memory range of the given size within the given ranges, and
256 * remove it from them. Return true on success, or false if no large enough
257 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100258 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900259static bool carve_out_mem_range(struct mem_range *mem_ranges,
260 size_t mem_ranges_count, uint64_t size_to_find,
261 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100262{
263 size_t i;
264
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000265 /*
266 * TODO(b/116191358): Consider being cleverer about how we pack VMs
267 * together, with a non-greedy algorithm.
268 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100269 for (i = 0; i < mem_ranges_count; ++i) {
270 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100271 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100272 /*
273 * This range is big enough, take some of it from the
274 * end and reduce its size accordingly.
275 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100276 *found_end = mem_ranges[i].end;
277 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
278 size_to_find);
279 mem_ranges[i].end = *found_begin;
280 return true;
281 }
282 }
283 return false;
284}
285
286/**
287 * Given arrays of memory ranges before and after memory was removed for
288 * secondary VMs, add the difference to the reserved ranges of the given update.
289 * Return true on success, or false if there would be more than MAX_MEM_RANGES
290 * reserved ranges after adding the new ones.
291 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
292 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900293static bool update_reserved_ranges(struct boot_params_update *update,
294 const struct mem_range *before,
295 const struct mem_range *after,
296 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100297{
298 size_t i;
299
300 for (i = 0; i < mem_ranges_count; ++i) {
301 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
302 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
303 dlog("Too many reserved ranges after loading "
304 "secondary VMs.\n");
305 return false;
306 }
307 update->reserved_ranges[update->reserved_ranges_count]
308 .begin = before[i].begin;
309 update->reserved_ranges[update->reserved_ranges_count]
310 .end = after[i].begin;
311 update->reserved_ranges_count++;
312 }
313 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
314 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
315 dlog("Too many reserved ranges after loading "
316 "secondary VMs.\n");
317 return false;
318 }
319 update->reserved_ranges[update->reserved_ranges_count]
320 .begin = after[i].end;
321 update->reserved_ranges[update->reserved_ranges_count]
322 .end = before[i].end;
323 update->reserved_ranges_count++;
324 }
325 }
326
327 return true;
328}
329
Andrew Scull72b43c02019-09-18 13:53:45 +0100330/*
331 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100332 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100333bool load_vms(struct mm_stage1_locked stage1_locked,
334 const struct manifest *manifest, const struct memiter *cpio,
335 const struct boot_params *params,
336 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100337{
Andrew Scull19503262018-09-20 14:48:39 +0100338 struct vm *primary;
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100339 struct vm *tee;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100340 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
Andrew Scull3c257452019-11-26 13:32:50 +0000341 struct vm_locked primary_vm_locked;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100342 size_t i;
Andrew Scull3c257452019-11-26 13:32:50 +0000343 bool success = true;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100344
Andrew Scullae9962e2019-10-03 16:51:16 +0100345 if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
346 cpio, params, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100347 dlog("Unable to load primary VM.\n");
348 return false;
349 }
350
Andrew Walbran9daa57e2019-09-27 13:33:20 +0100351 /*
352 * Initialise the dummy VM which represents TrustZone, and set up its
353 * RX/TX buffers.
354 */
355 tee = vm_init(HF_TEE_VM_ID, 0, ppool);
356 CHECK(tee != NULL);
357 tee->mailbox.send = &tee_send_buffer;
358 tee->mailbox.recv = &tee_recv_buffer;
359
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100360 static_assert(
361 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
362 "mem_range arrays must be the same size for memcpy.");
363 static_assert(sizeof(mem_ranges_available) < 500,
364 "This will use too much stack, either make "
365 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100366 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
367 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100368
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100369 /* Round the last addresses down to the page size. */
370 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000371 mem_ranges_available[i].end = pa_init(align_down(
372 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100373 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100374
Andrew Scull3c257452019-11-26 13:32:50 +0000375 primary = vm_find(HF_PRIMARY_VM_ID);
376 primary_vm_locked = vm_lock(primary);
377
David Brazdil0251b942019-09-10 15:59:50 +0100378 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100379 const struct manifest_vm *manifest_vm = &manifest->vm[i];
David Brazdil7a462ec2019-08-15 12:27:47 +0100380 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100381 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100382 paddr_t secondary_mem_begin;
383 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100384
David Brazdil7a462ec2019-08-15 12:27:47 +0100385 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100386 continue;
387 }
388
David Brazdil0dbb41f2019-09-09 18:03:35 +0100389 dlog("Loading VM%d: %s.\n", (int)vm_id,
390 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100391
David Brazdil7a462ec2019-08-15 12:27:47 +0100392 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
David Brazdil7a462ec2019-08-15 12:27:47 +0100393 if (!carve_out_mem_range(mem_ranges_available,
394 params->mem_ranges_count, mem_size,
395 &secondary_mem_begin,
396 &secondary_mem_end)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100397 dlog("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100398 continue;
399 }
Andrew Scull80871322018-08-06 12:04:09 +0100400
Andrew Scull72b43c02019-09-18 13:53:45 +0100401 if (!load_secondary(stage1_locked, secondary_mem_begin,
402 secondary_mem_end, manifest_vm, cpio,
403 ppool)) {
404 dlog("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100405 continue;
406 }
407
408 /* Deny the primary VM access to this memory. */
Andrew Scull3c257452019-11-26 13:32:50 +0000409 if (!vm_unmap(primary_vm_locked, secondary_mem_begin,
410 secondary_mem_end, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100411 dlog("Unable to unmap secondary VM from primary VM.\n");
Andrew Scull3c257452019-11-26 13:32:50 +0000412 success = false;
413 break;
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100414 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100415 }
416
Andrew Scull3c257452019-11-26 13:32:50 +0000417 vm_unlock(&primary_vm_locked);
418
419 if (!success) {
420 return false;
421 }
422
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100423 /*
424 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100425 * difference between the available ranges from the original params and
426 * the updated mem_ranges_available. We assume that the number and order
427 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100428 * above only make them smaller.
429 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100430 return update_reserved_ranges(update, params->mem_ranges,
431 mem_ranges_available,
432 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100433}