blob: db19e05b1ea0fa09e78dc2cd1b480a28acdb3c8e [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 Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/dlog.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010024#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/memiter.h"
26#include "hf/mm.h"
Andrew Walbran48699362019-05-20 14:38:00 +010027#include "hf/plat/console.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010028#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010029#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010031
Andrew Scull19503262018-09-20 14:48:39 +010032#include "vmapi/hf/call.h"
33
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034/**
35 * Copies data to an unmapped location by mapping it for write, copying the
36 * data, then unmapping it.
Andrew Sculld9225b32018-11-19 16:12:41 +000037 *
38 * The data is written so that it is available to all cores with the cache
39 * disabled. When switching to the partitions, the caching is initially disabled
40 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010041 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +010042static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to,
David Brazdil7a462ec2019-08-15 12:27:47 +010043 struct memiter *from_it, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010044{
David Brazdil7a462ec2019-08-15 12:27:47 +010045 const void *from = memiter_base(from_it);
46 size_t size = memiter_size(from_it);
Andrew Scull80871322018-08-06 12:04:09 +010047 paddr_t to_end = pa_add(to, size);
48 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010049
Andrew Scull3c0a90a2019-07-01 11:55:53 +010050 ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010051 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010052 return false;
53 }
54
Andrew Sculla1aa2ba2019-04-05 11:49:02 +010055 memcpy_s(ptr, size, from, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +010056 arch_mm_flush_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010057
Andrew Scull3c0a90a2019-07-01 11:55:53 +010058 mm_unmap(stage1_locked, to, to_end, ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010059
60 return true;
61}
62
63/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010064 * Looks for a file in the given cpio archive. The filename is not
65 * null-terminated, so we use a memory iterator to represent it. The file, if
66 * found, is returned in the "it" argument.
67 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010068static bool memiter_find_file(const struct memiter *cpio,
69 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010070 struct memiter *it)
71{
72 const char *fname;
73 const void *fcontents;
74 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010075 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010076
77 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
78 if (memiter_iseq(filename, fname)) {
79 memiter_init(it, fcontents, fsize);
80 return true;
81 }
82 }
83
84 return false;
85}
86
87/**
88 * Looks for a file in the given cpio archive. The file, if found, is returned
89 * in the "it" argument.
90 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010091static bool find_file(const struct memiter *cpio, const char *name,
92 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010093{
94 const char *fname;
95 const void *fcontents;
96 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010097 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010098
99 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
100 if (!strcmp(fname, name)) {
101 memiter_init(it, fcontents, fsize);
102 return true;
103 }
104 }
105
106 return false;
107}
108
109/**
110 * Loads the primary VM.
111 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100112bool load_primary(struct mm_stage1_locked stage1_locked,
113 const struct memiter *cpio, uintreg_t kernel_arg,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000114 struct memiter *initrd, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100115{
116 struct memiter it;
Andrew Scullf16c0c22018-10-26 18:41:24 +0100117 paddr_t primary_begin = layout_primary_begin();
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100118
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100119 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100120 dlog("Unable to find vmlinuz\n");
121 return false;
122 }
123
Andrew Scullf16c0c22018-10-26 18:41:24 +0100124 dlog("Copying primary to %p\n", pa_addr(primary_begin));
David Brazdil7a462ec2019-08-15 12:27:47 +0100125 if (!copy_to_unmapped(stage1_locked, primary_begin, &it, ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100126 dlog("Unable to relocate kernel for primary vm.\n");
127 return false;
128 }
129
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100130 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100131 dlog("Unable to find initrd.img\n");
132 return false;
133 }
134
135 {
Andrew Scull19503262018-09-20 14:48:39 +0100136 struct vm *vm;
Andrew Walbranb58f8992019-04-15 12:29:31 +0100137 struct vcpu_locked vcpu_locked;
Andrew Scull19503262018-09-20 14:48:39 +0100138
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000139 if (!vm_init(MAX_CPUS, ppool, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100140 dlog("Unable to initialise primary vm\n");
141 return false;
142 }
143
Andrew Scull19503262018-09-20 14:48:39 +0100144 if (vm->id != HF_PRIMARY_VM_ID) {
145 dlog("Primary vm was not given correct id\n");
146 return false;
147 }
148
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100149 /* Map the 1TB of memory. */
150 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100151 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100152 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100153 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
Andrew Scullda241972019-01-05 18:17:48 +0000154 MM_MODE_R | MM_MODE_W | MM_MODE_X, NULL, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100155 dlog("Unable to initialise memory for primary vm\n");
156 return false;
157 }
158
Andrew Scullda241972019-01-05 18:17:48 +0000159 if (!mm_vm_unmap_hypervisor(&vm->ptable, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100160 dlog("Unable to unmap hypervisor from primary vm\n");
161 return false;
162 }
163
Andrew Walbrane1310df2019-04-29 17:28:28 +0100164 vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0));
Andrew Walbranb58f8992019-04-15 12:29:31 +0100165 vcpu_on(vcpu_locked, ipa_from_pa(primary_begin), kernel_arg);
166 vcpu_unlock(&vcpu_locked);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100167 }
168
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100169 return true;
170}
171
172/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100173 * Try to find a memory range of the given size within the given ranges, and
174 * remove it from them. Return true on success, or false if no large enough
175 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100176 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900177static bool carve_out_mem_range(struct mem_range *mem_ranges,
178 size_t mem_ranges_count, uint64_t size_to_find,
179 paddr_t *found_begin, paddr_t *found_end)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100180{
181 size_t i;
182
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000183 /*
184 * TODO(b/116191358): Consider being cleverer about how we pack VMs
185 * together, with a non-greedy algorithm.
186 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100187 for (i = 0; i < mem_ranges_count; ++i) {
188 if (size_to_find <=
Andrew Walbran2cb43392019-04-17 12:52:45 +0100189 pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100190 /*
191 * This range is big enough, take some of it from the
192 * end and reduce its size accordingly.
193 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100194 *found_end = mem_ranges[i].end;
195 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
196 size_to_find);
197 mem_ranges[i].end = *found_begin;
198 return true;
199 }
200 }
201 return false;
202}
203
204/**
205 * Given arrays of memory ranges before and after memory was removed for
206 * secondary VMs, add the difference to the reserved ranges of the given update.
207 * Return true on success, or false if there would be more than MAX_MEM_RANGES
208 * reserved ranges after adding the new ones.
209 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
210 */
Hong-Seok Kim09648362019-05-23 15:47:11 +0900211static bool update_reserved_ranges(struct boot_params_update *update,
212 const struct mem_range *before,
213 const struct mem_range *after,
214 size_t mem_ranges_count)
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100215{
216 size_t i;
217
218 for (i = 0; i < mem_ranges_count; ++i) {
219 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
220 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
221 dlog("Too many reserved ranges after loading "
222 "secondary VMs.\n");
223 return false;
224 }
225 update->reserved_ranges[update->reserved_ranges_count]
226 .begin = before[i].begin;
227 update->reserved_ranges[update->reserved_ranges_count]
228 .end = after[i].begin;
229 update->reserved_ranges_count++;
230 }
231 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
232 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
233 dlog("Too many reserved ranges after loading "
234 "secondary VMs.\n");
235 return false;
236 }
237 update->reserved_ranges[update->reserved_ranges_count]
238 .begin = after[i].end;
239 update->reserved_ranges[update->reserved_ranges_count]
240 .end = before[i].end;
241 update->reserved_ranges_count++;
242 }
243 }
244
245 return true;
246}
247
248/**
249 * Loads all secondary VMs into the memory ranges from the given params.
250 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
251 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100252bool load_secondary(struct mm_stage1_locked stage1_locked,
David Brazdil0dbb41f2019-09-09 18:03:35 +0100253 const struct manifest *manifest, const struct memiter *cpio,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100254 const struct boot_params *params,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000255 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100256{
Andrew Scull19503262018-09-20 14:48:39 +0100257 struct vm *primary;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100258 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
259 size_t i;
260
261 static_assert(
262 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
263 "mem_range arrays must be the same size for memcpy.");
264 static_assert(sizeof(mem_ranges_available) < 500,
265 "This will use too much stack, either make "
266 "MAX_MEM_RANGES smaller or change this.");
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100267 memcpy_s(mem_ranges_available, sizeof(mem_ranges_available),
268 params->mem_ranges, sizeof(params->mem_ranges));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269
Andrew Walbran42347a92019-05-09 13:59:03 +0100270 primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Scull19503262018-09-20 14:48:39 +0100271
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100272 /* Round the last addresses down to the page size. */
273 for (i = 0; i < params->mem_ranges_count; ++i) {
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000274 mem_ranges_available[i].end = pa_init(align_down(
275 pa_addr(mem_ranges_available[i].end), PAGE_SIZE));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100276 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100277
David Brazdil0251b942019-09-10 15:59:50 +0100278 for (i = 0; i < manifest->vm_count; ++i) {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100279 const struct manifest_vm *manifest_vm = &manifest->vm[i];
David Brazdil7a462ec2019-08-15 12:27:47 +0100280 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
281 struct vm *vm;
282 struct vcpu *vcpu;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100283 struct memiter kernel;
David Brazdil0dbb41f2019-09-09 18:03:35 +0100284 struct memiter kernel_filename;
David Brazdil7a462ec2019-08-15 12:27:47 +0100285 uint64_t mem_size;
Andrew Scull80871322018-08-06 12:04:09 +0100286 paddr_t secondary_mem_begin;
287 paddr_t secondary_mem_end;
288 ipaddr_t secondary_entry;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100289
David Brazdil7a462ec2019-08-15 12:27:47 +0100290 if (vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100291 continue;
292 }
293
David Brazdil0dbb41f2019-09-09 18:03:35 +0100294 dlog("Loading VM%d: %s.\n", (int)vm_id,
295 manifest_vm->debug_name);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100296
David Brazdil0dbb41f2019-09-09 18:03:35 +0100297 memiter_init(&kernel_filename,
298 manifest_vm->secondary.kernel_filename,
299 strnlen_s(manifest_vm->secondary.kernel_filename,
300 MANIFEST_MAX_STRING_LENGTH));
301 if (!memiter_find_file(cpio, &kernel_filename, &kernel)) {
302 dlog("Could not find kernel file \"%s\".\n",
303 manifest_vm->secondary.kernel_filename);
David Brazdil7a462ec2019-08-15 12:27:47 +0100304 continue;
305 }
306
307 mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE);
308 if (mem_size < memiter_size(&kernel)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100309 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100310 continue;
311 }
312
David Brazdil7a462ec2019-08-15 12:27:47 +0100313 if (!carve_out_mem_range(mem_ranges_available,
314 params->mem_ranges_count, mem_size,
315 &secondary_mem_begin,
316 &secondary_mem_end)) {
317 dlog("Not enough memory (%u bytes)\n", mem_size);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100318 continue;
319 }
Andrew Scull80871322018-08-06 12:04:09 +0100320
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100321 if (!copy_to_unmapped(stage1_locked, secondary_mem_begin,
David Brazdil7a462ec2019-08-15 12:27:47 +0100322 &kernel, ppool)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100323 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100324 continue;
325 }
326
David Brazdil7a462ec2019-08-15 12:27:47 +0100327 if (!vm_init(manifest_vm->secondary.vcpu_count, ppool, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100328 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100329 continue;
330 }
331
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100332 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100333 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
334 secondary_mem_end,
Andrew Scullda241972019-01-05 18:17:48 +0000335 MM_MODE_R | MM_MODE_W | MM_MODE_X,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000336 &secondary_entry, ppool)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100337 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100338 continue;
339 }
340
341 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100342 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scullda241972019-01-05 18:17:48 +0000343 secondary_mem_end, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100344 dlog("Unable to unmap secondary VM from primary VM\n");
345 return false;
346 }
347
David Brazdil7a462ec2019-08-15 12:27:47 +0100348 dlog("Loaded with %u vcpus, entry at %#x\n",
349 manifest_vm->secondary.vcpu_count,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100350 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100351
Andrew Walbrane1310df2019-04-29 17:28:28 +0100352 vcpu = vm_get_vcpu(vm, 0);
Andrew Walbran9a43fee2019-04-18 17:42:32 +0100353 vcpu_secondary_reset_and_start(
354 vcpu, secondary_entry,
Andrew Walbran2b87c702019-04-16 18:16:05 +0100355 pa_difference(secondary_mem_begin, secondary_mem_end));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100356 }
357
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100358 /*
359 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100360 * difference between the available ranges from the original params and
361 * the updated mem_ranges_available. We assume that the number and order
362 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100363 * above only make them smaller.
364 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100365 return update_reserved_ranges(update, params->mem_ranges,
366 mem_ranges_available,
367 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100368}