blob: b179ba99f25b08f33c993dd6ce139529b004ddc0 [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
64/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010065 * Looks for a file in the given cpio archive. The filename is not
66 * null-terminated, so we use a memory iterator to represent it. The file, if
67 * found, is returned in the "it" argument.
68 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010069static bool memiter_find_file(const struct memiter *cpio,
70 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010071 struct memiter *it)
72{
73 const char *fname;
74 const void *fcontents;
75 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010076 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010077
78 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
79 if (memiter_iseq(filename, fname)) {
80 memiter_init(it, fcontents, fsize);
81 return true;
82 }
83 }
84
85 return false;
86}
87
88/**
89 * Looks for a file in the given cpio archive. The file, if found, is returned
90 * in the "it" argument.
91 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010092static bool find_file(const struct memiter *cpio, const char *name,
93 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010094{
95 const char *fname;
96 const void *fcontents;
97 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010098 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010099
100 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
101 if (!strcmp(fname, name)) {
102 memiter_init(it, fcontents, fsize);
103 return true;
104 }
105 }
106
107 return false;
108}
109
Andrew Scull72b43c02019-09-18 13:53:45 +0100110static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin,
111 paddr_t end, const struct manifest_vm *manifest_vm,
112 const struct memiter *cpio, struct mpool *ppool)
113{
114 struct memiter kernel_filename;
115 struct memiter kernel;
116
117 memiter_init(&kernel_filename, manifest_vm->kernel_filename,
118 strnlen_s(manifest_vm->kernel_filename,
119 MANIFEST_MAX_STRING_LENGTH));
120
121 if (memiter_size(&kernel_filename) == 0) {
122 /* This signals the kernel has been preloaded. */
123 return true;
124 }
125
126 if (!memiter_find_file(cpio, &kernel_filename, &kernel)) {
127 dlog("Could not find kernel file \"%s\".\n",
128 manifest_vm->kernel_filename);
129 return false;
130 }
131
132 if (pa_difference(begin, end) < memiter_size(&kernel)) {
133 dlog("Kernel is larger than available memory.\n");
134 return false;
135 }
136
137 if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) {
138 dlog("Unable to copy kernel.\n");
139 return false;
140 }
141
142 return true;
143}
144
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100145/**
146 * Loads the primary VM.
147 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100148static bool load_primary(struct mm_stage1_locked stage1_locked,
149 const struct manifest *manifest,
150 const struct memiter *cpio, uintreg_t kernel_arg,
151 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100152{
Andrew Scullf16c0c22018-10-26 18:41:24 +0100153 paddr_t primary_begin = layout_primary_begin();
Andrew Scull72b43c02019-09-18 13:53:45 +0100154 const struct manifest_vm *manifest_vm =
155 &manifest->vm[HF_PRIMARY_VM_ID - HF_VM_ID_OFFSET];
156 struct memiter initrd;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100157
Andrew Scull72b43c02019-09-18 13:53:45 +0100158 /*
159 * TODO: This bound is currently meaningless but will be addressed when
160 * the manifest specifies the load address.
161 */
162 paddr_t primary_end = pa_add(primary_begin, 0x8000000);
163
164 if (!load_kernel(stage1_locked, primary_begin, primary_end, manifest_vm,
165 cpio, ppool)) {
166 dlog("Unable to load primary kernel.");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100167 return false;
168 }
169
Andrew Scull72b43c02019-09-18 13:53:45 +0100170 if (!find_file(cpio, "initrd.img", &initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100171 dlog("Unable to find initrd.img\n");
172 return false;
173 }
174
Andrew Scull72b43c02019-09-18 13:53:45 +0100175 update->initrd_begin = pa_from_va(va_from_ptr(initrd.next));
176 update->initrd_end = pa_from_va(va_from_ptr(initrd.limit));
177
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100178 {
Andrew Scull19503262018-09-20 14:48:39 +0100179 struct vm *vm;
Andrew Walbranb58f8992019-04-15 12:29:31 +0100180 struct vcpu_locked vcpu_locked;
Andrew Scull19503262018-09-20 14:48:39 +0100181
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000182 if (!vm_init(MAX_CPUS, ppool, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100183 dlog("Unable to initialise primary vm\n");
184 return false;
185 }
186
Andrew Scull19503262018-09-20 14:48:39 +0100187 if (vm->id != HF_PRIMARY_VM_ID) {
188 dlog("Primary vm was not given correct id\n");
189 return false;
190 }
191
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100192 /* Map the 1TB of memory. */
193 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100194 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100195 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100196 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
Andrew Scullda241972019-01-05 18:17:48 +0000197 MM_MODE_R | MM_MODE_W | MM_MODE_X, NULL, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100198 dlog("Unable to initialise memory for primary vm\n");
199 return false;
200 }
201
Andrew Scullda241972019-01-05 18:17:48 +0000202 if (!mm_vm_unmap_hypervisor(&vm->ptable, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100203 dlog("Unable to unmap hypervisor from primary vm\n");
204 return false;
205 }
206
Andrew Walbrane1310df2019-04-29 17:28:28 +0100207 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Andrew Walbranb58f8992019-04-15 12:29:31 +0100208 vcpu_on(vcpu_locked, ipa_from_pa(primary_begin), kernel_arg);
209 vcpu_unlock(&vcpu_locked);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100210 }
211
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100212 return true;
213}
214
Andrew Scull72b43c02019-09-18 13:53:45 +0100215/*
216 * Loads a secondary VM.
217 */
218static bool load_secondary(struct mm_stage1_locked stage1_locked,
219 paddr_t mem_begin, paddr_t mem_end,
220 const struct manifest_vm *manifest_vm,
221 const struct memiter *cpio, struct mpool *ppool)
222{
223 struct vm *vm;
224 struct vcpu *vcpu;
225 ipaddr_t secondary_entry;
226
227 if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, cpio,
228 ppool)) {
229 dlog("Unable to load kernel.\n");
230 return false;
231 }
232
233 if (!vm_init(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
234 dlog("Unable to initialise VM.\n");
235 return false;
236 }
237
238 /* Grant the VM access to the memory. */
239 if (!mm_vm_identity_map(&vm->ptable, mem_begin, mem_end,
240 MM_MODE_R | MM_MODE_W | MM_MODE_X,
241 &secondary_entry, ppool)) {
242 dlog("Unable to initialise memory.\n");
243 return false;
244 }
245
246 dlog("Loaded with %u vcpus, entry at %#x.\n",
247 manifest_vm->secondary.vcpu_count, pa_addr(mem_begin));
248
249 vcpu = vm_get_vcpu(vm, 0);
250 vcpu_secondary_reset_and_start(vcpu, secondary_entry,
251 pa_difference(mem_begin, mem_end));
252
253 return true;
254}
255
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100256/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100257 * Try to find a memory range of the given size within the given ranges, and
258 * remove it from them. Return true on success, or false if no large enough
259 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100260 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900261static bool carve_out_mem_range(struct mem_range *mem_ranges,
262 size_t mem_ranges_count, uint64_t size_to_find,
263 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100264{
265 size_t i;
266
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000267 /*
268 * TODO(b/116191358): Consider being cleverer about how we pack VMs
269 * together, with a non-greedy algorithm.
270 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100271 for (i = 0; i < mem_ranges_count; ++i) {
272 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100273 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100274 /*
275 * This range is big enough, take some of it from the
276 * end and reduce its size accordingly.
277 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100278 *found_end = mem_ranges[i].end;
279 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
280 size_to_find);
281 mem_ranges[i].end = *found_begin;
282 return true;
283 }
284 }
285 return false;
286}
287
288/**
289 * Given arrays of memory ranges before and after memory was removed for
290 * secondary VMs, add the difference to the reserved ranges of the given update.
291 * Return true on success, or false if there would be more than MAX_MEM_RANGES
292 * reserved ranges after adding the new ones.
293 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
294 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900295static bool update_reserved_ranges(struct boot_params_update *update,
296 const struct mem_range *before,
297 const struct mem_range *after,
298 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100299{
300 size_t i;
301
302 for (i = 0; i < mem_ranges_count; ++i) {
303 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
304 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
305 dlog("Too many reserved ranges after loading "
306 "secondary VMs.\n");
307 return false;
308 }
309 update->reserved_ranges[update->reserved_ranges_count]
310 .begin = before[i].begin;
311 update->reserved_ranges[update->reserved_ranges_count]
312 .end = after[i].begin;
313 update->reserved_ranges_count++;
314 }
315 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
316 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
317 dlog("Too many reserved ranges after loading "
318 "secondary VMs.\n");
319 return false;
320 }
321 update->reserved_ranges[update->reserved_ranges_count]
322 .begin = after[i].end;
323 update->reserved_ranges[update->reserved_ranges_count]
324 .end = before[i].end;
325 update->reserved_ranges_count++;
326 }
327 }
328
329 return true;
330}
331
Andrew Scull72b43c02019-09-18 13:53:45 +0100332/*
333 * Loads alls VMs from the manifest.
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100334 */
Andrew Scull72b43c02019-09-18 13:53:45 +0100335bool load_vms(struct mm_stage1_locked stage1_locked,
336 const struct manifest *manifest, const struct memiter *cpio,
337 const struct boot_params *params,
338 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100339{
Andrew Scull19503262018-09-20 14:48:39 +0100340 struct vm *primary;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100341 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
342 size_t i;
343
Andrew Scull72b43c02019-09-18 13:53:45 +0100344 if (!load_primary(stage1_locked, manifest, cpio, params->kernel_arg,
345 update, ppool)) {
346 dlog("Unable to load primary VM.\n");
347 return false;
348 }
349
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100350 static_assert(
351 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
352 "mem_range arrays must be the same size for memcpy.");
353 static_assert(sizeof(mem_ranges_available) < 500,
354 "This will use too much stack, either make "
355 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100356 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
357 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100358
Andrew Walbran42347a92019-05-09 13:59:03 +0100359 primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Scull19503262018-09-20 14:48:39 +0100360
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100361 /* Round the last addresses down to the page size. */
362 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000363 mem_ranges_available[i].end = pa_init(align_down(
364 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100365 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100366
David Brazdil0251b942019-09-10 15:59:50 +0100367 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100368 const struct manifest_vm *manifest_vm = &manifest->vm[i];
David Brazdil7a462ec2019-08-15 12:27:47 +0100369 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
David Brazdil7a462ec2019-08-15 12:27:47 +0100370 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100371 paddr_t secondary_mem_begin;
372 paddr_t secondary_mem_end;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100373
David Brazdil7a462ec2019-08-15 12:27:47 +0100374 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100375 continue;
376 }
377
David Brazdil0dbb41f2019-09-09 18:03:35 +0100378 dlog("Loading VM%d: %s.\n", (int)vm_id,
379 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100380
David Brazdil7a462ec2019-08-15 12:27:47 +0100381 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
David Brazdil7a462ec2019-08-15 12:27:47 +0100382 if (!carve_out_mem_range(mem_ranges_available,
383 params->mem_ranges_count, mem_size,
384 &secondary_mem_begin,
385 &secondary_mem_end)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100386 dlog("Not enough memory (%u bytes).\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100387 continue;
388 }
Andrew Scull80871322018-08-06 12:04:09 +0100389
Andrew Scull72b43c02019-09-18 13:53:45 +0100390 if (!load_secondary(stage1_locked, secondary_mem_begin,
391 secondary_mem_end, manifest_vm, cpio,
392 ppool)) {
393 dlog("Unable to load VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100394 continue;
395 }
396
397 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100398 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scullda241972019-01-05 18:17:48 +0000399 secondary_mem_end, ppool)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100400 dlog("Unable to unmap secondary VM from primary VM.\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100401 return false;
402 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100403 }
404
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100405 /*
406 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100407 * difference between the available ranges from the original params and
408 * the updated mem_ranges_available. We assume that the number and order
409 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100410 * above only make them smaller.
411 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100412 return update_reserved_ranges(update, params->mem_ranges,
413 mem_ranges_available,
414 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100415}