blob: c118b89ecf31e0a423ff4381f5e49725729b9c30 [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/**
55 * Moves the kernel of the primary VM to its final destination.
56 */
57static bool relocate(const char *from, size_t size)
58{
Andrew Scull5991ec92018-10-08 14:55:02 +010059 paddr_t dest = layout_primary_begin();
60 dlog("Copying to %p\n", pa_addr(dest));
Andrew Walbranfd265ec2018-09-12 17:50:55 +010061 return copy_to_unmapped(dest, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010062}
63
64/**
65 * 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
110/**
111 * Loads the primary VM.
112 */
Andrew Scull265ada92018-07-30 15:19:01 +0100113// TODO: kernel_arg is a size_t???
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100114bool load_primary(const struct memiter *cpio, size_t kernel_arg,
115 struct memiter *initrd)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116{
117 struct memiter it;
118
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
124 if (!relocate(it.next, it.limit - it.next)) {
125 dlog("Unable to relocate kernel for primary vm.\n");
126 return false;
127 }
128
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100129 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100130 dlog("Unable to find initrd.img\n");
131 return false;
132 }
133
134 {
Andrew Scull1b8d0442018-08-06 15:47:04 +0100135 uintpaddr_t tmp = (uintpaddr_t)&load_primary;
Andrew Scull19503262018-09-20 14:48:39 +0100136 struct vm *vm;
137
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100138 tmp = (tmp + 0x80000 - 1) & ~(0x80000 - 1);
Andrew Scull19503262018-09-20 14:48:39 +0100139 if (!vm_init(MAX_CPUS, &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),
154 MM_MODE_R | MM_MODE_W | MM_MODE_X |
155 MM_MODE_NOINVALIDATE,
156 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100157 dlog("Unable to initialise memory for primary vm\n");
158 return false;
159 }
160
Andrew Scull19503262018-09-20 14:48:39 +0100161 if (!mm_ptable_unmap_hypervisor(&vm->ptable,
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100162 MM_MODE_NOINVALIDATE)) {
163 dlog("Unable to unmap hypervisor from primary vm\n");
164 return false;
165 }
166
Andrew Scull19503262018-09-20 14:48:39 +0100167 vm_start_vcpu(vm, 0, ipa_init(tmp), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100168 }
169
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100170 return true;
171}
172
173/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100174 * Try to find a memory range of the given size within the given ranges, and
175 * remove it from them. Return true on success, or false if no large enough
176 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100177 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100178bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
179 uint64_t size_to_find, paddr_t *found_begin,
180 paddr_t *found_end)
181{
182 size_t i;
183
184 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
185 * together, with a non-greedy algorithm. */
186 for (i = 0; i < mem_ranges_count; ++i) {
187 if (size_to_find <=
188 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
189 /* This range is big enough, take some of it from the
190 * end and reduce its size accordingly. */
191 *found_end = mem_ranges[i].end;
192 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
193 size_to_find);
194 mem_ranges[i].end = *found_begin;
195 return true;
196 }
197 }
198 return false;
199}
200
201/**
202 * Given arrays of memory ranges before and after memory was removed for
203 * secondary VMs, add the difference to the reserved ranges of the given update.
204 * Return true on success, or false if there would be more than MAX_MEM_RANGES
205 * reserved ranges after adding the new ones.
206 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
207 */
208bool update_reserved_ranges(struct boot_params_update *update,
209 const struct mem_range *before,
210 const struct mem_range *after,
211 size_t mem_ranges_count)
212{
213 size_t i;
214
215 for (i = 0; i < mem_ranges_count; ++i) {
216 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
217 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
218 dlog("Too many reserved ranges after loading "
219 "secondary VMs.\n");
220 return false;
221 }
222 update->reserved_ranges[update->reserved_ranges_count]
223 .begin = before[i].begin;
224 update->reserved_ranges[update->reserved_ranges_count]
225 .end = after[i].begin;
226 update->reserved_ranges_count++;
227 }
228 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
229 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
230 dlog("Too many reserved ranges after loading "
231 "secondary VMs.\n");
232 return false;
233 }
234 update->reserved_ranges[update->reserved_ranges_count]
235 .begin = after[i].end;
236 update->reserved_ranges[update->reserved_ranges_count]
237 .end = before[i].end;
238 update->reserved_ranges_count++;
239 }
240 }
241
242 return true;
243}
244
245/**
246 * Loads all secondary VMs into the memory ranges from the given params.
247 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
248 */
249bool load_secondary(const struct memiter *cpio,
250 const struct boot_params *params,
251 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100252{
Andrew Scull19503262018-09-20 14:48:39 +0100253 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100254 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100255 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100256 uint64_t mem;
257 uint64_t cpu;
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.");
267 memcpy(mem_ranges_available, params->mem_ranges,
268 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269
Andrew Scull19503262018-09-20 14:48:39 +0100270 primary = vm_get(HF_PRIMARY_VM_ID);
271
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100272 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100273 dlog("vms.txt is missing\n");
274 return true;
275 }
276
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100277 /* Round the last addresses down to the page size. */
278 for (i = 0; i < params->mem_ranges_count; ++i) {
279 mem_ranges_available[i].end =
280 pa_init(pa_addr(mem_ranges_available[i].end) &
281 ~(PAGE_SIZE - 1));
282 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100283
Andrew Scull19503262018-09-20 14:48:39 +0100284 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
285 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100286 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100287 paddr_t secondary_mem_begin;
288 paddr_t secondary_mem_end;
289 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100290 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100291 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100292
Andrew Scull36e4bae2018-09-27 17:50:56 +0100293 dlog("Loading ");
294 for (p = name.next; p != name.limit; ++p) {
295 dlog("%c", *p);
296 }
297 dlog("\n");
298
299 if (!memiter_find_file(cpio, &name, &kernel)) {
300 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100301 continue;
302 }
303
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100304 /* Round up to page size. */
305 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100306
307 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100308 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100309 continue;
310 }
311
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100312 if (!carve_out_mem_range(
313 mem_ranges_available, params->mem_ranges_count, mem,
314 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100315 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100316 continue;
317 }
Andrew Scull80871322018-08-06 12:04:09 +0100318
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100319 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100320 kernel.limit - kernel.next)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100321 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100322 continue;
323 }
324
Andrew Scull19503262018-09-20 14:48:39 +0100325 if (!vm_init(cpu, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100326 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100327 continue;
328 }
329
330 /* TODO: Remove this. */
331 /* Grant VM access to uart. */
Andrew Scull24e032f2018-10-15 17:18:12 +0100332 mm_vm_identity_map(&vm->ptable, pa_init(PL011_BASE),
333 pa_add(pa_init(PL011_BASE), PAGE_SIZE),
334 MM_MODE_R | MM_MODE_W | MM_MODE_D |
335 MM_MODE_NOINVALIDATE,
336 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100337
338 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100339 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
340 secondary_mem_end,
Andrew Scull80871322018-08-06 12:04:09 +0100341 MM_MODE_R | MM_MODE_W | MM_MODE_X |
342 MM_MODE_NOINVALIDATE,
343 &secondary_entry)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100344 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100345 continue;
346 }
347
348 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100349 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scull80871322018-08-06 12:04:09 +0100350 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100351 dlog("Unable to unmap secondary VM from primary VM\n");
352 return false;
353 }
354
Andrew Scull36e4bae2018-09-27 17:50:56 +0100355 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100356 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100357
Andrew Scull19503262018-09-20 14:48:39 +0100358 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100359 }
360
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100361 /* Add newly reserved areas to update params by looking at the
362 * difference between the available ranges from the original params and
363 * the updated mem_ranges_available. We assume that the number and order
364 * of available ranges is the same, i.e. we don't remove any ranges
365 * above only make them smaller. */
366 return update_reserved_ranges(update, params->mem_ranges,
367 mem_ranges_available,
368 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100369}