blob: c4ed9a29aa2b7a58bab543071ed3e9a24c9a6aaf [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
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
Andrew Walbran34ce72e2018-09-13 16:47:44 +010019#include <assert.h>
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010020#include <stdbool.h>
21
Andrew Scull18c78fc2018-08-20 12:57:41 +010022#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +010023#include "hf/boot_params.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"
28#include "hf/std.h"
29#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010030
Andrew Scull19503262018-09-20 14:48:39 +010031#include "vmapi/hf/call.h"
32
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010033/**
34 * Copies data to an unmapped location by mapping it for write, copying the
35 * data, then unmapping it.
36 */
Andrew Walbranfd265ec2018-09-12 17:50:55 +010037static bool copy_to_unmapped(paddr_t to, const void *from, size_t size)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010038{
Andrew Scull80871322018-08-06 12:04:09 +010039 paddr_t to_end = pa_add(to, size);
40 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010041
Andrew Scull80871322018-08-06 12:04:09 +010042 ptr = mm_identity_map(to, to_end, MM_MODE_W);
43 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010044 return false;
45 }
46
Andrew Scull80871322018-08-06 12:04:09 +010047 memcpy(ptr, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048
Andrew Scull80871322018-08-06 12:04:09 +010049 mm_unmap(to, to_end, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010050
51 return true;
52}
53
54/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010055 * Looks for a file in the given cpio archive. The filename is not
56 * null-terminated, so we use a memory iterator to represent it. The file, if
57 * found, is returned in the "it" argument.
58 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010059static bool memiter_find_file(const struct memiter *cpio,
60 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010061 struct memiter *it)
62{
63 const char *fname;
64 const void *fcontents;
65 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010066 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010067
68 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
69 if (memiter_iseq(filename, fname)) {
70 memiter_init(it, fcontents, fsize);
71 return true;
72 }
73 }
74
75 return false;
76}
77
78/**
79 * Looks for a file in the given cpio archive. The file, if found, is returned
80 * in the "it" argument.
81 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010082static bool find_file(const struct memiter *cpio, const char *name,
83 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010084{
85 const char *fname;
86 const void *fcontents;
87 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010088 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010089
90 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
91 if (!strcmp(fname, name)) {
92 memiter_init(it, fcontents, fsize);
93 return true;
94 }
95 }
96
97 return false;
98}
99
100/**
101 * Loads the primary VM.
102 */
Andrew Scull37402872018-10-24 14:23:06 +0100103bool load_primary(const struct memiter *cpio, uintreg_t kernel_arg,
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100104 struct memiter *initrd)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100105{
106 struct memiter it;
Andrew Scullf16c0c22018-10-26 18:41:24 +0100107 paddr_t primary_begin = layout_primary_begin();
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100108
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100109 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100110 dlog("Unable to find vmlinuz\n");
111 return false;
112 }
113
Andrew Scullf16c0c22018-10-26 18:41:24 +0100114 dlog("Copying primary to %p\n", pa_addr(primary_begin));
115 if (!copy_to_unmapped(primary_begin, it.next, it.limit - it.next)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116 dlog("Unable to relocate kernel for primary vm.\n");
117 return false;
118 }
119
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100120 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100121 dlog("Unable to find initrd.img\n");
122 return false;
123 }
124
125 {
Andrew Scull19503262018-09-20 14:48:39 +0100126 struct vm *vm;
127
Andrew Scull19503262018-09-20 14:48:39 +0100128 if (!vm_init(MAX_CPUS, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100129 dlog("Unable to initialise primary vm\n");
130 return false;
131 }
132
Andrew Scull19503262018-09-20 14:48:39 +0100133 if (vm->id != HF_PRIMARY_VM_ID) {
134 dlog("Primary vm was not given correct id\n");
135 return false;
136 }
137
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100138 /* Map the 1TB of memory. */
139 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100140 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100141 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100142 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
143 MM_MODE_R | MM_MODE_W | MM_MODE_X |
144 MM_MODE_NOINVALIDATE,
145 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100146 dlog("Unable to initialise memory for primary vm\n");
147 return false;
148 }
149
Andrew Scull19503262018-09-20 14:48:39 +0100150 if (!mm_ptable_unmap_hypervisor(&vm->ptable,
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100151 MM_MODE_NOINVALIDATE)) {
152 dlog("Unable to unmap hypervisor from primary vm\n");
153 return false;
154 }
155
Andrew Scullf16c0c22018-10-26 18:41:24 +0100156 vm_start_vcpu(vm, 0, ipa_from_pa(primary_begin), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100157 }
158
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100159 return true;
160}
161
162/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100163 * Try to find a memory range of the given size within the given ranges, and
164 * remove it from them. Return true on success, or false if no large enough
165 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100166 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100167bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
168 uint64_t size_to_find, paddr_t *found_begin,
169 paddr_t *found_end)
170{
171 size_t i;
172
173 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
174 * together, with a non-greedy algorithm. */
175 for (i = 0; i < mem_ranges_count; ++i) {
176 if (size_to_find <=
177 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100178 /*
179 * This range is big enough, take some of it from the
180 * end and reduce its size accordingly.
181 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100182 *found_end = mem_ranges[i].end;
183 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
184 size_to_find);
185 mem_ranges[i].end = *found_begin;
186 return true;
187 }
188 }
189 return false;
190}
191
192/**
193 * Given arrays of memory ranges before and after memory was removed for
194 * secondary VMs, add the difference to the reserved ranges of the given update.
195 * Return true on success, or false if there would be more than MAX_MEM_RANGES
196 * reserved ranges after adding the new ones.
197 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
198 */
199bool update_reserved_ranges(struct boot_params_update *update,
200 const struct mem_range *before,
201 const struct mem_range *after,
202 size_t mem_ranges_count)
203{
204 size_t i;
205
206 for (i = 0; i < mem_ranges_count; ++i) {
207 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
208 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
209 dlog("Too many reserved ranges after loading "
210 "secondary VMs.\n");
211 return false;
212 }
213 update->reserved_ranges[update->reserved_ranges_count]
214 .begin = before[i].begin;
215 update->reserved_ranges[update->reserved_ranges_count]
216 .end = after[i].begin;
217 update->reserved_ranges_count++;
218 }
219 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
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 = after[i].end;
227 update->reserved_ranges[update->reserved_ranges_count]
228 .end = before[i].end;
229 update->reserved_ranges_count++;
230 }
231 }
232
233 return true;
234}
235
236/**
237 * Loads all secondary VMs into the memory ranges from the given params.
238 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
239 */
240bool load_secondary(const struct memiter *cpio,
241 const struct boot_params *params,
242 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100243{
Andrew Scull19503262018-09-20 14:48:39 +0100244 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100245 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100246 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100247 uint64_t mem;
248 uint64_t cpu;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100249 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
250 size_t i;
251
252 static_assert(
253 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
254 "mem_range arrays must be the same size for memcpy.");
255 static_assert(sizeof(mem_ranges_available) < 500,
256 "This will use too much stack, either make "
257 "MAX_MEM_RANGES smaller or change this.");
258 memcpy(mem_ranges_available, params->mem_ranges,
259 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100260
Andrew Scull19503262018-09-20 14:48:39 +0100261 primary = vm_get(HF_PRIMARY_VM_ID);
262
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100263 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100264 dlog("vms.txt is missing\n");
265 return true;
266 }
267
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100268 /* Round the last addresses down to the page size. */
269 for (i = 0; i < params->mem_ranges_count; ++i) {
270 mem_ranges_available[i].end =
271 pa_init(pa_addr(mem_ranges_available[i].end) &
272 ~(PAGE_SIZE - 1));
273 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100274
Andrew Scull19503262018-09-20 14:48:39 +0100275 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
276 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100277 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100278 paddr_t secondary_mem_begin;
279 paddr_t secondary_mem_end;
280 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100281 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100282 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100283
Andrew Scull36e4bae2018-09-27 17:50:56 +0100284 dlog("Loading ");
285 for (p = name.next; p != name.limit; ++p) {
286 dlog("%c", *p);
287 }
288 dlog("\n");
289
290 if (!memiter_find_file(cpio, &name, &kernel)) {
291 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100292 continue;
293 }
294
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100295 /* Round up to page size. */
296 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100297
298 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100299 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100300 continue;
301 }
302
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100303 if (!carve_out_mem_range(
304 mem_ranges_available, params->mem_ranges_count, mem,
305 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100306 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100307 continue;
308 }
Andrew Scull80871322018-08-06 12:04:09 +0100309
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100310 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100311 kernel.limit - kernel.next)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100312 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100313 continue;
314 }
315
Andrew Scull19503262018-09-20 14:48:39 +0100316 if (!vm_init(cpu, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100317 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100318 continue;
319 }
320
321 /* TODO: Remove this. */
322 /* Grant VM access to uart. */
Andrew Scull24e032f2018-10-15 17:18:12 +0100323 mm_vm_identity_map(&vm->ptable, pa_init(PL011_BASE),
324 pa_add(pa_init(PL011_BASE), PAGE_SIZE),
325 MM_MODE_R | MM_MODE_W | MM_MODE_D |
326 MM_MODE_NOINVALIDATE,
327 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100328
329 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100330 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
331 secondary_mem_end,
Andrew Scull80871322018-08-06 12:04:09 +0100332 MM_MODE_R | MM_MODE_W | MM_MODE_X |
333 MM_MODE_NOINVALIDATE,
334 &secondary_entry)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100335 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100336 continue;
337 }
338
339 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100340 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scull80871322018-08-06 12:04:09 +0100341 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100342 dlog("Unable to unmap secondary VM from primary VM\n");
343 return false;
344 }
345
Andrew Scull36e4bae2018-09-27 17:50:56 +0100346 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100347 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100348
Andrew Scull19503262018-09-20 14:48:39 +0100349 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100350 }
351
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100352 /*
353 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100354 * difference between the available ranges from the original params and
355 * the updated mem_ranges_available. We assume that the number and order
356 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100357 * above only make them smaller.
358 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100359 return update_reserved_ranges(update, params->mem_ranges,
360 mem_ranges_available,
361 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100362}