blob: 6732b28abc5e96cc62386d11f14f321161e4a67e [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"
Andrew Scull5991ec92018-10-08 14:55:02 +01009#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010010#include "hf/memiter.h"
11#include "hf/mm.h"
12#include "hf/std.h"
13#include "hf/vm.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010014
Andrew Scull19503262018-09-20 14:48:39 +010015#include "vmapi/hf/call.h"
16
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010017/**
18 * Copies data to an unmapped location by mapping it for write, copying the
19 * data, then unmapping it.
20 */
Andrew Walbranfd265ec2018-09-12 17:50:55 +010021static bool copy_to_unmapped(paddr_t to, const void *from, size_t size)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010022{
Andrew Scull80871322018-08-06 12:04:09 +010023 paddr_t to_end = pa_add(to, size);
24 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010025
Andrew Scull80871322018-08-06 12:04:09 +010026 ptr = mm_identity_map(to, to_end, MM_MODE_W);
27 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010028 return false;
29 }
30
Andrew Scull80871322018-08-06 12:04:09 +010031 memcpy(ptr, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010032
Andrew Scull80871322018-08-06 12:04:09 +010033 mm_unmap(to, to_end, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010034
35 return true;
36}
37
38/**
39 * Moves the kernel of the primary VM to its final destination.
40 */
41static bool relocate(const char *from, size_t size)
42{
Andrew Scull5991ec92018-10-08 14:55:02 +010043 paddr_t dest = layout_primary_begin();
44 dlog("Copying to %p\n", pa_addr(dest));
Andrew Walbranfd265ec2018-09-12 17:50:55 +010045 return copy_to_unmapped(dest, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010046}
47
48/**
49 * Looks for a file in the given cpio archive. The filename is not
50 * null-terminated, so we use a memory iterator to represent it. The file, if
51 * found, is returned in the "it" argument.
52 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010053static bool memiter_find_file(const struct memiter *cpio,
54 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010055 struct memiter *it)
56{
57 const char *fname;
58 const void *fcontents;
59 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010060 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010061
62 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
63 if (memiter_iseq(filename, fname)) {
64 memiter_init(it, fcontents, fsize);
65 return true;
66 }
67 }
68
69 return false;
70}
71
72/**
73 * Looks for a file in the given cpio archive. The file, if found, is returned
74 * in the "it" argument.
75 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010076static bool find_file(const struct memiter *cpio, const char *name,
77 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010078{
79 const char *fname;
80 const void *fcontents;
81 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010082 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010083
84 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
85 if (!strcmp(fname, name)) {
86 memiter_init(it, fcontents, fsize);
87 return true;
88 }
89 }
90
91 return false;
92}
93
94/**
95 * Loads the primary VM.
96 */
Andrew Scull265ada92018-07-30 15:19:01 +010097// TODO: kernel_arg is a size_t???
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010098bool load_primary(const struct memiter *cpio, size_t kernel_arg,
99 struct memiter *initrd)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100100{
101 struct memiter it;
102
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100103 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100104 dlog("Unable to find vmlinuz\n");
105 return false;
106 }
107
108 if (!relocate(it.next, it.limit - it.next)) {
109 dlog("Unable to relocate kernel for primary vm.\n");
110 return false;
111 }
112
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100113 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100114 dlog("Unable to find initrd.img\n");
115 return false;
116 }
117
118 {
Andrew Scull1b8d0442018-08-06 15:47:04 +0100119 uintpaddr_t tmp = (uintpaddr_t)&load_primary;
Andrew Scull19503262018-09-20 14:48:39 +0100120 struct vm *vm;
121
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100122 tmp = (tmp + 0x80000 - 1) & ~(0x80000 - 1);
Andrew Scull19503262018-09-20 14:48:39 +0100123 if (!vm_init(MAX_CPUS, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100124 dlog("Unable to initialise primary vm\n");
125 return false;
126 }
127
Andrew Scull19503262018-09-20 14:48:39 +0100128 if (vm->id != HF_PRIMARY_VM_ID) {
129 dlog("Primary vm was not given correct id\n");
130 return false;
131 }
132
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100133 /* Map the 1TB of memory. */
134 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100135 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100136 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100137 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
138 MM_MODE_R | MM_MODE_W | MM_MODE_X |
139 MM_MODE_NOINVALIDATE,
140 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100141 dlog("Unable to initialise memory for primary vm\n");
142 return false;
143 }
144
Andrew Scull19503262018-09-20 14:48:39 +0100145 if (!mm_ptable_unmap_hypervisor(&vm->ptable,
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100146 MM_MODE_NOINVALIDATE)) {
147 dlog("Unable to unmap hypervisor from primary vm\n");
148 return false;
149 }
150
Andrew Scull19503262018-09-20 14:48:39 +0100151 vm_start_vcpu(vm, 0, ipa_init(tmp), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100152 }
153
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100154 return true;
155}
156
157/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100158 * Try to find a memory range of the given size within the given ranges, and
159 * remove it from them. Return true on success, or false if no large enough
160 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100161 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100162bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
163 uint64_t size_to_find, paddr_t *found_begin,
164 paddr_t *found_end)
165{
166 size_t i;
167
168 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
169 * together, with a non-greedy algorithm. */
170 for (i = 0; i < mem_ranges_count; ++i) {
171 if (size_to_find <=
172 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
173 /* This range is big enough, take some of it from the
174 * end and reduce its size accordingly. */
175 *found_end = mem_ranges[i].end;
176 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
177 size_to_find);
178 mem_ranges[i].end = *found_begin;
179 return true;
180 }
181 }
182 return false;
183}
184
185/**
186 * Given arrays of memory ranges before and after memory was removed for
187 * secondary VMs, add the difference to the reserved ranges of the given update.
188 * Return true on success, or false if there would be more than MAX_MEM_RANGES
189 * reserved ranges after adding the new ones.
190 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
191 */
192bool update_reserved_ranges(struct boot_params_update *update,
193 const struct mem_range *before,
194 const struct mem_range *after,
195 size_t mem_ranges_count)
196{
197 size_t i;
198
199 for (i = 0; i < mem_ranges_count; ++i) {
200 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
201 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
202 dlog("Too many reserved ranges after loading "
203 "secondary VMs.\n");
204 return false;
205 }
206 update->reserved_ranges[update->reserved_ranges_count]
207 .begin = before[i].begin;
208 update->reserved_ranges[update->reserved_ranges_count]
209 .end = after[i].begin;
210 update->reserved_ranges_count++;
211 }
212 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
213 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
214 dlog("Too many reserved ranges after loading "
215 "secondary VMs.\n");
216 return false;
217 }
218 update->reserved_ranges[update->reserved_ranges_count]
219 .begin = after[i].end;
220 update->reserved_ranges[update->reserved_ranges_count]
221 .end = before[i].end;
222 update->reserved_ranges_count++;
223 }
224 }
225
226 return true;
227}
228
229/**
230 * Loads all secondary VMs into the memory ranges from the given params.
231 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
232 */
233bool load_secondary(const struct memiter *cpio,
234 const struct boot_params *params,
235 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100236{
Andrew Scull19503262018-09-20 14:48:39 +0100237 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100238 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100239 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100240 uint64_t mem;
241 uint64_t cpu;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100242 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
243 size_t i;
244
245 static_assert(
246 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
247 "mem_range arrays must be the same size for memcpy.");
248 static_assert(sizeof(mem_ranges_available) < 500,
249 "This will use too much stack, either make "
250 "MAX_MEM_RANGES smaller or change this.");
251 memcpy(mem_ranges_available, params->mem_ranges,
252 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100253
Andrew Scull19503262018-09-20 14:48:39 +0100254 primary = vm_get(HF_PRIMARY_VM_ID);
255
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100256 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100257 dlog("vms.txt is missing\n");
258 return true;
259 }
260
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100261 /* Round the last addresses down to the page size. */
262 for (i = 0; i < params->mem_ranges_count; ++i) {
263 mem_ranges_available[i].end =
264 pa_init(pa_addr(mem_ranges_available[i].end) &
265 ~(PAGE_SIZE - 1));
266 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100267
Andrew Scull19503262018-09-20 14:48:39 +0100268 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
269 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100270 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100271 paddr_t secondary_mem_begin;
272 paddr_t secondary_mem_end;
273 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100274 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100275 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100276
Andrew Scull36e4bae2018-09-27 17:50:56 +0100277 dlog("Loading ");
278 for (p = name.next; p != name.limit; ++p) {
279 dlog("%c", *p);
280 }
281 dlog("\n");
282
283 if (!memiter_find_file(cpio, &name, &kernel)) {
284 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100285 continue;
286 }
287
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100288 /* Round up to page size. */
289 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100290
291 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100292 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100293 continue;
294 }
295
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100296 if (!carve_out_mem_range(
297 mem_ranges_available, params->mem_ranges_count, mem,
298 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100299 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100300 continue;
301 }
Andrew Scull80871322018-08-06 12:04:09 +0100302
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100303 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100304 kernel.limit - kernel.next)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100305 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100306 continue;
307 }
308
Andrew Scull19503262018-09-20 14:48:39 +0100309 if (!vm_init(cpu, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100310 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100311 continue;
312 }
313
314 /* TODO: Remove this. */
315 /* Grant VM access to uart. */
Andrew Scull19503262018-09-20 14:48:39 +0100316 mm_vm_identity_map_page(&vm->ptable, pa_init(PL011_BASE),
Andrew Scull80871322018-08-06 12:04:09 +0100317 MM_MODE_R | MM_MODE_W | MM_MODE_D |
318 MM_MODE_NOINVALIDATE,
319 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100320
321 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100322 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
323 secondary_mem_end,
Andrew Scull80871322018-08-06 12:04:09 +0100324 MM_MODE_R | MM_MODE_W | MM_MODE_X |
325 MM_MODE_NOINVALIDATE,
326 &secondary_entry)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100327 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100328 continue;
329 }
330
331 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100332 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scull80871322018-08-06 12:04:09 +0100333 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100334 dlog("Unable to unmap secondary VM from primary VM\n");
335 return false;
336 }
337
Andrew Scull36e4bae2018-09-27 17:50:56 +0100338 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100339 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100340
Andrew Scull19503262018-09-20 14:48:39 +0100341 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100342 }
343
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100344 /* Add newly reserved areas to update params by looking at the
345 * difference between the available ranges from the original params and
346 * the updated mem_ranges_available. We assume that the number and order
347 * of available ranges is the same, i.e. we don't remove any ranges
348 * above only make them smaller. */
349 return update_reserved_ranges(update, params->mem_ranges,
350 mem_ranges_available,
351 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100352}