blob: 5c7fa2a4f9322c59165de254c6e76a9de23323f2 [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
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010022#include "hf/boot_params.h"
Andrew Scull72b43c02019-09-18 13:53:45 +010023#include "hf/check.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/dlog.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010025#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/memiter.h"
27#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010028#include "hf/plat/console.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010029#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010030#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010031#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010032
Andrew Scull19503262018-09-20 14:48:39 +010033#include "vmapi/hf/call.h"
34
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010035/**
36 * Copies data to an unmapped location by mapping it for write, copying the
37 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000038 *
39 * The data is written so that it is available to all cores with the cache
40 * disabled. When switching to the partitions, the caching is initially disabled
41 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010042 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010043static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010044 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010045{
David Brazdil7a462ec2019-08-15 12:27:47 +010046 const void *from = memiter_base(from_it);
47 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010048 paddr_t to_end = pa_add(to, size);
49 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010050
Andrew Scull3c0a90a2019-07-01 11:55:53 +010051 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010052 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010053 return false;
54 }
55
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010056 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010057 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010058
Andrew Scull72b43c02019-09-18 13:53:45 +010059 CHECK(mm_unmap(stage1_locked, to, to_end, ppool));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010060
61 return true;
62}
63
Andrew Scull72b43c02019-09-18 13:53:45 +010064static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
65 paddr_t end, const struct manifest_vm *manifest_vm,
66 const struct memiter *cpio, struct mpool *ppool)
67{
Andrew Scull72b43c02019-09-18 13:53:45 +010068 struct memiter kernel;
69
David Brazdil136f2942019-09-23 14:11:03 +010070 if (string_is_empty(&manifest_vm->kernel_filename)) {
Andrew Scull72b43c02019-09-18 13:53:45 +010071 /* This signals the kernel has been preloaded. */
72 return true;
73 }
74
David Brazdil136f2942019-09-23 14:11:03 +010075 if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) {
Andrew Scull72b43c02019-09-18 13:53:45 +010076 dlog("Could not find kernel file \"%s\".\n",
David Brazdil136f2942019-09-23 14:11:03 +010077 string_data(&manifest_vm->kernel_filename));
Andrew Scull72b43c02019-09-18 13:53:45 +010078 return false;
79 }
80
81 if (pa_difference(begin, end) < memiter_size(&kernel)) {
82 dlog("Kernel is larger than available memory.\n");
83 return false;
84 }
85
86 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
87 dlog("Unable to copy kernel.\n");
88 return false;
89 }
90
91 return true;
92}
93
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010094/**
Andrew Scullae9962e2019-10-03 16:51:16 +010095 * Performs VM loading activities that are common between the primary and
96 * secondaries.
97 */
98static bool load_common(const struct manifest_vm *manifest_vm, struct vm *vm)
99{
100 vm->smc_whitelist = manifest_vm->smc_whitelist;
101
102 return true;
103}
104
105/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100106 * Loads the primary VM.
107 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100108static bool load_primary(struct mm_stage1_locked stage1_locked,
Andrew Scullae9962e2019-10-03 16:51:16 +0100109 const struct manifest_vm *manifest_vm,
Andrew Scullb5f49e02019-10-02 13:20:47 +0100110 const struct memiter *cpio,
111 const struct boot_params *params, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100112{
Andrew Scullf16c0c22018-10-26 18:41:24 +0100113 paddr_t primary_begin = layout_primary_begin();
David Brazdile6f83222019-09-23 14:47:37 +0100114 struct vm *vm;
115 struct vcpu_locked vcpu_locked;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100116 size_t i;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100117
Andrew Scull72b43c02019-09-18 13:53:45 +0100118 /*
119 * TODO: This bound is currently meaningless but will be addressed when
120 * the manifest specifies the load address.
121 */
122 paddr_t primary_end = pa_add(primary_begin, 0x8000000);
123
Andrew Scullae9962e2019-10-03 16:51:16 +0100124 if (!load_kernel(stage1_locked, primary_begin, primary_end, manifest_vm,
125 cpio, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100126 dlog("Unable to load primary kernel.");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100127 return false;
128 }
129
David Brazdile6f83222019-09-23 14:47:37 +0100130 if (!vm_init(MAX_CPUS, ppool, &vm)) {
131 dlog("Unable to initialise primary vm\n");
132 return false;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100133 }
134
David Brazdile6f83222019-09-23 14:47:37 +0100135 if (vm->id != HF_PRIMARY_VM_ID) {
136 dlog("Primary vm was not given correct id\n");
137 return false;
138 }
139
Andrew Scullae9962e2019-10-03 16:51:16 +0100140 if (!load_common(manifest_vm, vm)) {
141 return false;
142 }
143
Andrew Scullb5f49e02019-10-02 13:20:47 +0100144 /*
145 * Map 1TB of address space as device memory to, most likely, make all
146 * devices available to the primary VM.
147 *
148 * TODO: We should do a whitelist rather than a blacklist.
149 */
David Brazdile6f83222019-09-23 14:47:37 +0100150 if (!mm_vm_identity_map(&vm->ptable, pa_init(0),
151 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
Andrew Scullb5f49e02019-10-02 13:20:47 +0100152 MM_MODE_R | MM_MODE_W | MM_MODE_D, NULL,
David Brazdile6f83222019-09-23 14:47:37 +0100153 ppool)) {
Andrew Scullb5f49e02019-10-02 13:20:47 +0100154 dlog("Unable to initialise address space for primary vm\n");
David Brazdile6f83222019-09-23 14:47:37 +0100155 return false;
156 }
157
Andrew Scullb5f49e02019-10-02 13:20:47 +0100158 /* Map normal memory as such to permit caching, execution, etc. */
159 for (i = 0; i < params->mem_ranges_count; ++i) {
160 if (!mm_vm_identity_map(
161 &vm->ptable, params->mem_ranges[i].begin,
162 params->mem_ranges[i].end,
163 MM_MODE_R | MM_MODE_W | MM_MODE_X, NULL, ppool)) {
164 dlog("Unable to initialise memory for primary vm\n");
165 return false;
166 }
167 }
168
David Brazdile6f83222019-09-23 14:47:37 +0100169 if (!mm_vm_unmap_hypervisor(&vm->ptable, ppool)) {
170 dlog("Unable to unmap hypervisor from primary vm\n");
171 return false;
172 }
173
174 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Andrew Scullb5f49e02019-10-02 13:20:47 +0100175 vcpu_on(vcpu_locked, ipa_from_pa(primary_begin), params->kernel_arg);
David Brazdile6f83222019-09-23 14:47:37 +0100176 vcpu_unlock(&vcpu_locked);
177
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100178 return true;
179}
180
Andrew Scull72b43c02019-09-18 13:53:45 +0100181/*
182 * Loads a secondary VM.
183 */
184static bool load_secondary(struct mm_stage1_locked stage1_locked,
185 paddr_t mem_begin, paddr_t mem_end,
186 const struct manifest_vm *manifest_vm,
187 const struct memiter *cpio, struct mpool *ppool)
188{
189 struct vm *vm;
190 struct vcpu *vcpu;
191 ipaddr_t secondary_entry;
192
193 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, cpio,
194 ppool)) {
195 dlog("Unable to load kernel.\n");
196 return false;
197 }
198
199 if (!vm_init(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
200 dlog("Unable to initialise VM.\n");
201 return false;
202 }
203
Andrew Scullae9962e2019-10-03 16:51:16 +0100204 if (!load_common(manifest_vm, vm)) {
205 return false;
206 }
207
Andrew Scull72b43c02019-09-18 13:53:45 +0100208 /* Grant the VM access to the memory. */
209 if (!mm_vm_identity_map(&vm->ptable, mem_begin, mem_end,
210 MM_MODE_R | MM_MODE_W | MM_MODE_X,
211 &secondary_entry, ppool)) {
212 dlog("Unable to initialise memory.\n");
213 return false;
214 }
215
216 dlog("Loaded with %u vcpus, entry at %#x.\n",
217 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
218
219 vcpu = vm_get_vcpu(vm, 0);
220 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
221 pa_difference(mem_begin, mem_end));
222
223 return true;
224}
225
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100226/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100227 * Try to find a memory range of the given size within the given ranges, and
228 * remove it from them. Return true on success, or false if no large enough
229 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100230 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900231static bool carve_out_mem_range(struct mem_range *mem_ranges,
232 size_t mem_ranges_count, uint64_t size_to_find,
233 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100234{
235 size_t i;
236
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000237 /*
238 * TODO(b/116191358): Consider being cleverer about how we pack VMs
239 * together, with a non-greedy algorithm.
240 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100241 for (i = 0; i < mem_ranges_count; ++i) {
242 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100243 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100244 /*
245 * This range is big enough, take some of it from the
246 * end and reduce its size accordingly.
247 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100248 *found_end = mem_ranges[i].end;
249 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
250 size_to_find);
251 mem_ranges[i].end = *found_begin;
252 return true;
253 }
254 }
255 return false;
256}
257
258/**
259 * Given arrays of memory ranges before and after memory was removed for
260 * secondary VMs, add the difference to the reserved ranges of the given update.
261 * Return true on success, or false if there would be more than MAX_MEM_RANGES
262 * reserved ranges after adding the new ones.
263 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
264 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900265static bool update_reserved_ranges(struct boot_params_update *update,
266 const struct mem_range *before,
267 const struct mem_range *after,
268 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100269{
270 size_t i;
271
272 for (i = 0; i < mem_ranges_count; ++i) {
273 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
274 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
275 dlog("Too many reserved ranges after loading "
276 "secondary VMs.\n");
277 return false;
278 }
279 update->reserved_ranges[update->reserved_ranges_count]
280 .begin = before[i].begin;
281 update->reserved_ranges[update->reserved_ranges_count]
282 .end = after[i].begin;
283 update->reserved_ranges_count++;
284 }
285 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
286 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
287 dlog("Too many reserved ranges after loading "
288 "secondary VMs.\n");
289 return false;
290 }
291 update->reserved_ranges[update->reserved_ranges_count]
292 .begin = after[i].end;
293 update->reserved_ranges[update->reserved_ranges_count]
294 .end = before[i].end;
295 update->reserved_ranges_count++;
296 }
297 }
298
299 return true;
300}
301
Andrew Scull72b43c02019-09-18 13:53:45 +0100302/*
303 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100304 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100305bool load_vms(struct mm_stage1_locked stage1_locked,
306 const struct manifest *manifest, const struct memiter *cpio,
307 const struct boot_params *params,
308 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100309{
Andrew Scull19503262018-09-20 14:48:39 +0100310 struct vm *primary;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100311 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
312 size_t i;
313
Andrew Scullae9962e2019-10-03 16:51:16 +0100314 if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
315 cpio, params, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100316 dlog("Unable to load primary VM.\n");
317 return false;
318 }
319
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100320 static_assert(
321 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
322 "mem_range arrays must be the same size for memcpy.");
323 static_assert(sizeof(mem_ranges_available) < 500,
324 "This will use too much stack, either make "
325 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100326 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
327 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100328
Andrew Walbran42347a92019-05-09 13:59:03 +0100329 primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Scull19503262018-09-20 14:48:39 +0100330
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100331 /* Round the last addresses down to the page size. */
332 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000333 mem_ranges_available[i].end = pa_init(align_down(
334 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100335 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100336
David Brazdil0251b942019-09-10 15:59:50 +0100337 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100338 const struct manifest_vm *manifest_vm = &manifest->vm[i];
David Brazdil7a462ec2019-08-15 12:27:47 +0100339 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100340 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100341 paddr_t secondary_mem_begin;
342 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100343
David Brazdil7a462ec2019-08-15 12:27:47 +0100344 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100345 continue;
346 }
347
David Brazdil0dbb41f2019-09-09 18:03:35 +0100348 dlog("Loading VM%d: %s.\n", (int)vm_id,
349 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100350
David Brazdil7a462ec2019-08-15 12:27:47 +0100351 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
David Brazdil7a462ec2019-08-15 12:27:47 +0100352 if (!carve_out_mem_range(mem_ranges_available,
353 params->mem_ranges_count, mem_size,
354 &secondary_mem_begin,
355 &secondary_mem_end)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100356 dlog("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100357 continue;
358 }
Andrew Scull80871322018-08-06 12:04:09 +0100359
Andrew Scull72b43c02019-09-18 13:53:45 +0100360 if (!load_secondary(stage1_locked, secondary_mem_begin,
361 secondary_mem_end, manifest_vm, cpio,
362 ppool)) {
363 dlog("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100364 continue;
365 }
366
367 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100368 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scullda241972019-01-05 18:17:48 +0000369 secondary_mem_end, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100370 dlog("Unable to unmap secondary VM from primary VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100371 return false;
372 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100373 }
374
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100375 /*
376 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100377 * difference between the available ranges from the original params and
378 * the updated mem_ranges_available. We assume that the number and order
379 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100380 * above only make them smaller.
381 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100382 return update_reserved_ranges(update, params->mem_ranges,
383 mem_ranges_available,
384 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100385}