blob: a6bf7d7d1bf9c19e4b2dad29b844848d109e122f [file] [log] [blame]
Andrew Scull18c78fc2018-08-20 12:57:41 +01001#include "hf/load.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01002
Andrew Walbran34ce72e2018-09-13 16:47:44 +01003#include <assert.h>
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01004#include <stdbool.h>
5
Andrew Scull18c78fc2018-08-20 12:57:41 +01006#include "hf/api.h"
Andrew Walbran34ce72e2018-09-13 16:47:44 +01007#include "hf/boot_params.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +01008#include "hf/dlog.h"
9#include "hf/memiter.h"
10#include "hf/mm.h"
11#include "hf/std.h"
12#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010013
Andrew Scull19503262018-09-20 14:48:39 +010014#include "vmapi/hf/call.h"
15
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010016/**
17 * Copies data to an unmapped location by mapping it for write, copying the
18 * data, then unmapping it.
19 */
Andrew Walbranfd265ec2018-09-12 17:50:55 +010020static bool copy_to_unmapped(paddr_t to, const void *from, size_t size)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010021{
Andrew Scull80871322018-08-06 12:04:09 +010022 paddr_t to_end = pa_add(to, size);
23 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010024
Andrew Scull80871322018-08-06 12:04:09 +010025 ptr = mm_identity_map(to, to_end, MM_MODE_W);
26 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010027 return false;
28 }
29
Andrew Scull80871322018-08-06 12:04:09 +010030 memcpy(ptr, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010031
Andrew Scull80871322018-08-06 12:04:09 +010032 mm_unmap(to, to_end, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010033
34 return true;
35}
36
37/**
38 * Moves the kernel of the primary VM to its final destination.
39 */
40static bool relocate(const char *from, size_t size)
41{
42 /* TODO: This is a hack. We must read the alignment from the binary. */
43 extern char bin_end[];
44 size_t tmp = (size_t)&bin_end[0];
Andrew Scull265ada92018-07-30 15:19:01 +010045 paddr_t dest = pa_init((tmp + 0x80000 - 1) & ~(0x80000 - 1));
46 dlog("bin_end is at %p, copying to %p\n", &bin_end[0], pa_addr(dest));
Andrew Walbranfd265ec2018-09-12 17:50:55 +010047 return copy_to_unmapped(dest, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048}
49
50/**
51 * Looks for a file in the given cpio archive. The filename is not
52 * null-terminated, so we use a memory iterator to represent it. The file, if
53 * found, is returned in the "it" argument.
54 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010055static bool memiter_find_file(const struct memiter *cpio,
56 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010057 struct memiter *it)
58{
59 const char *fname;
60 const void *fcontents;
61 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010062 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010063
64 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
65 if (memiter_iseq(filename, fname)) {
66 memiter_init(it, fcontents, fsize);
67 return true;
68 }
69 }
70
71 return false;
72}
73
74/**
75 * Looks for a file in the given cpio archive. The file, if found, is returned
76 * in the "it" argument.
77 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010078static bool find_file(const struct memiter *cpio, const char *name,
79 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010080{
81 const char *fname;
82 const void *fcontents;
83 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010084 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010085
86 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
87 if (!strcmp(fname, name)) {
88 memiter_init(it, fcontents, fsize);
89 return true;
90 }
91 }
92
93 return false;
94}
95
96/**
97 * Loads the primary VM.
98 */
Andrew Scull265ada92018-07-30 15:19:01 +010099// TODO: kernel_arg is a size_t???
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100100bool load_primary(const struct memiter *cpio, size_t kernel_arg,
101 struct memiter *initrd)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100102{
103 struct memiter it;
104
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100105 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100106 dlog("Unable to find vmlinuz\n");
107 return false;
108 }
109
110 if (!relocate(it.next, it.limit - it.next)) {
111 dlog("Unable to relocate kernel for primary vm.\n");
112 return false;
113 }
114
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100115 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116 dlog("Unable to find initrd.img\n");
117 return false;
118 }
119
120 {
Andrew Scull1b8d0442018-08-06 15:47:04 +0100121 uintpaddr_t tmp = (uintpaddr_t)&load_primary;
Andrew Scull19503262018-09-20 14:48:39 +0100122 struct vm *vm;
123
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100124 tmp = (tmp + 0x80000 - 1) & ~(0x80000 - 1);
Andrew Scull19503262018-09-20 14:48:39 +0100125 if (!vm_init(MAX_CPUS, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100126 dlog("Unable to initialise primary vm\n");
127 return false;
128 }
129
Andrew Scull19503262018-09-20 14:48:39 +0100130 if (vm->id != HF_PRIMARY_VM_ID) {
131 dlog("Primary vm was not given correct id\n");
132 return false;
133 }
134
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100135 /* Map the 1TB of memory. */
136 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100137 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100138 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100139 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
140 MM_MODE_R | MM_MODE_W | MM_MODE_X |
141 MM_MODE_NOINVALIDATE,
142 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100143 dlog("Unable to initialise memory for primary vm\n");
144 return false;
145 }
146
Andrew Scull19503262018-09-20 14:48:39 +0100147 if (!mm_ptable_unmap_hypervisor(&vm->ptable,
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100148 MM_MODE_NOINVALIDATE)) {
149 dlog("Unable to unmap hypervisor from primary vm\n");
150 return false;
151 }
152
Andrew Scull19503262018-09-20 14:48:39 +0100153 vm_start_vcpu(vm, 0, ipa_init(tmp), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100154 }
155
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100156 return true;
157}
158
159/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100160 * Try to find a memory range of the given size within the given ranges, and
161 * remove it from them. Return true on success, or false if no large enough
162 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100163 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100164bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
165 uint64_t size_to_find, paddr_t *found_begin,
166 paddr_t *found_end)
167{
168 size_t i;
169
170 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
171 * together, with a non-greedy algorithm. */
172 for (i = 0; i < mem_ranges_count; ++i) {
173 if (size_to_find <=
174 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
175 /* This range is big enough, take some of it from the
176 * end and reduce its size accordingly. */
177 *found_end = mem_ranges[i].end;
178 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
179 size_to_find);
180 mem_ranges[i].end = *found_begin;
181 return true;
182 }
183 }
184 return false;
185}
186
187/**
188 * Given arrays of memory ranges before and after memory was removed for
189 * secondary VMs, add the difference to the reserved ranges of the given update.
190 * Return true on success, or false if there would be more than MAX_MEM_RANGES
191 * reserved ranges after adding the new ones.
192 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
193 */
194bool update_reserved_ranges(struct boot_params_update *update,
195 const struct mem_range *before,
196 const struct mem_range *after,
197 size_t mem_ranges_count)
198{
199 size_t i;
200
201 for (i = 0; i < mem_ranges_count; ++i) {
202 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
203 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
204 dlog("Too many reserved ranges after loading "
205 "secondary VMs.\n");
206 return false;
207 }
208 update->reserved_ranges[update->reserved_ranges_count]
209 .begin = before[i].begin;
210 update->reserved_ranges[update->reserved_ranges_count]
211 .end = after[i].begin;
212 update->reserved_ranges_count++;
213 }
214 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
215 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
216 dlog("Too many reserved ranges after loading "
217 "secondary VMs.\n");
218 return false;
219 }
220 update->reserved_ranges[update->reserved_ranges_count]
221 .begin = after[i].end;
222 update->reserved_ranges[update->reserved_ranges_count]
223 .end = before[i].end;
224 update->reserved_ranges_count++;
225 }
226 }
227
228 return true;
229}
230
231/**
232 * Loads all secondary VMs into the memory ranges from the given params.
233 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
234 */
235bool load_secondary(const struct memiter *cpio,
236 const struct boot_params *params,
237 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100238{
Andrew Scull19503262018-09-20 14:48:39 +0100239 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100240 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100241 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100242 uint64_t mem;
243 uint64_t cpu;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100244 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
245 size_t i;
246
247 static_assert(
248 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
249 "mem_range arrays must be the same size for memcpy.");
250 static_assert(sizeof(mem_ranges_available) < 500,
251 "This will use too much stack, either make "
252 "MAX_MEM_RANGES smaller or change this.");
253 memcpy(mem_ranges_available, params->mem_ranges,
254 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100255
Andrew Scull19503262018-09-20 14:48:39 +0100256 primary = vm_get(HF_PRIMARY_VM_ID);
257
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100258 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100259 dlog("vms.txt is missing\n");
260 return true;
261 }
262
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100263 /* Round the last addresses down to the page size. */
264 for (i = 0; i < params->mem_ranges_count; ++i) {
265 mem_ranges_available[i].end =
266 pa_init(pa_addr(mem_ranges_available[i].end) &
267 ~(PAGE_SIZE - 1));
268 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100269
Andrew Scull19503262018-09-20 14:48:39 +0100270 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
271 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100272 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100273 paddr_t secondary_mem_begin;
274 paddr_t secondary_mem_end;
275 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100276 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100277 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100278
Andrew Scull36e4bae2018-09-27 17:50:56 +0100279 dlog("Loading ");
280 for (p = name.next; p != name.limit; ++p) {
281 dlog("%c", *p);
282 }
283 dlog("\n");
284
285 if (!memiter_find_file(cpio, &name, &kernel)) {
286 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100287 continue;
288 }
289
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100290 /* Round up to page size. */
291 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100292
293 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100294 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100295 continue;
296 }
297
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100298 if (!carve_out_mem_range(
299 mem_ranges_available, params->mem_ranges_count, mem,
300 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100301 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100302 continue;
303 }
Andrew Scull80871322018-08-06 12:04:09 +0100304
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100305 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100306 kernel.limit - kernel.next)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100307 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100308 continue;
309 }
310
Andrew Scull19503262018-09-20 14:48:39 +0100311 if (!vm_init(cpu, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100312 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100313 continue;
314 }
315
316 /* TODO: Remove this. */
317 /* Grant VM access to uart. */
Andrew Scull19503262018-09-20 14:48:39 +0100318 mm_vm_identity_map_page(&vm->ptable, pa_init(PL011_BASE),
Andrew Scull80871322018-08-06 12:04:09 +0100319 MM_MODE_R | MM_MODE_W | MM_MODE_D |
320 MM_MODE_NOINVALIDATE,
321 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100322
323 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100324 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
325 secondary_mem_end,
Andrew Scull80871322018-08-06 12:04:09 +0100326 MM_MODE_R | MM_MODE_W | MM_MODE_X |
327 MM_MODE_NOINVALIDATE,
328 &secondary_entry)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100329 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100330 continue;
331 }
332
333 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100334 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scull80871322018-08-06 12:04:09 +0100335 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100336 dlog("Unable to unmap secondary VM from primary VM\n");
337 return false;
338 }
339
Andrew Scull36e4bae2018-09-27 17:50:56 +0100340 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100341 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100342
Andrew Scull19503262018-09-20 14:48:39 +0100343 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100344 }
345
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100346 /* Add newly reserved areas to update params by looking at the
347 * difference between the available ranges from the original params and
348 * the updated mem_ranges_available. We assume that the number and order
349 * of available ranges is the same, i.e. we don't remove any ranges
350 * above only make them smaller. */
351 return update_reserved_ranges(update, params->mem_ranges,
352 mem_ranges_available,
353 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100354}