blob: 0e45abaa4a9bf3c2ba9588ce3623022c0eee5fda [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
14/**
15 * Copies data to an unmapped location by mapping it for write, copying the
16 * data, then unmapping it.
17 */
Andrew Walbranfd265ec2018-09-12 17:50:55 +010018static bool copy_to_unmapped(paddr_t to, const void *from, size_t size)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010019{
Andrew Scull80871322018-08-06 12:04:09 +010020 paddr_t to_end = pa_add(to, size);
21 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010022
Andrew Scull80871322018-08-06 12:04:09 +010023 ptr = mm_identity_map(to, to_end, MM_MODE_W);
24 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010025 return false;
26 }
27
Andrew Scull80871322018-08-06 12:04:09 +010028 memcpy(ptr, from, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010029
Andrew Scull80871322018-08-06 12:04:09 +010030 mm_unmap(to, to_end, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010031
32 return true;
33}
34
35/**
36 * Moves the kernel of the primary VM to its final destination.
37 */
38static bool relocate(const char *from, size_t size)
39{
40 /* TODO: This is a hack. We must read the alignment from the binary. */
41 extern char bin_end[];
42 size_t tmp = (size_t)&bin_end[0];
Andrew Scull265ada92018-07-30 15:19:01 +010043 paddr_t dest = pa_init((tmp + 0x80000 - 1) & ~(0x80000 - 1));
44 dlog("bin_end is at %p, copying to %p\n", &bin_end[0], 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;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100120 tmp = (tmp + 0x80000 - 1) & ~(0x80000 - 1);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100121 if (!vm_init(&primary_vm, 0, MAX_CPUS)) {
122 dlog("Unable to initialise primary vm\n");
123 return false;
124 }
125
126 /* Map the 1TB of memory. */
127 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100128 if (!mm_vm_identity_map(
129 &primary_vm.ptable, pa_init(0),
130 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
131 MM_MODE_R | MM_MODE_W | MM_MODE_X |
132 MM_MODE_NOINVALIDATE,
133 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100134 dlog("Unable to initialise memory for primary vm\n");
135 return false;
136 }
137
138 if (!mm_ptable_unmap_hypervisor(&primary_vm.ptable,
139 MM_MODE_NOINVALIDATE)) {
140 dlog("Unable to unmap hypervisor from primary vm\n");
141 return false;
142 }
143
Andrew Scull89a75242018-08-06 17:04:55 +0100144 vm_start_vcpu(&primary_vm, 0, ipa_init(tmp), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100145 }
146
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100147 return true;
148}
149
150/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100151 * Try to find a memory range of the given size within the given ranges, and
152 * remove it from them. Return true on success, or false if no large enough
153 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100154 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100155bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
156 uint64_t size_to_find, paddr_t *found_begin,
157 paddr_t *found_end)
158{
159 size_t i;
160
161 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
162 * together, with a non-greedy algorithm. */
163 for (i = 0; i < mem_ranges_count; ++i) {
164 if (size_to_find <=
165 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
166 /* This range is big enough, take some of it from the
167 * end and reduce its size accordingly. */
168 *found_end = mem_ranges[i].end;
169 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
170 size_to_find);
171 mem_ranges[i].end = *found_begin;
172 return true;
173 }
174 }
175 return false;
176}
177
178/**
179 * Given arrays of memory ranges before and after memory was removed for
180 * secondary VMs, add the difference to the reserved ranges of the given update.
181 * Return true on success, or false if there would be more than MAX_MEM_RANGES
182 * reserved ranges after adding the new ones.
183 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
184 */
185bool update_reserved_ranges(struct boot_params_update *update,
186 const struct mem_range *before,
187 const struct mem_range *after,
188 size_t mem_ranges_count)
189{
190 size_t i;
191
192 for (i = 0; i < mem_ranges_count; ++i) {
193 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
194 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
195 dlog("Too many reserved ranges after loading "
196 "secondary VMs.\n");
197 return false;
198 }
199 update->reserved_ranges[update->reserved_ranges_count]
200 .begin = before[i].begin;
201 update->reserved_ranges[update->reserved_ranges_count]
202 .end = after[i].begin;
203 update->reserved_ranges_count++;
204 }
205 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
206 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
207 dlog("Too many reserved ranges after loading "
208 "secondary VMs.\n");
209 return false;
210 }
211 update->reserved_ranges[update->reserved_ranges_count]
212 .begin = after[i].end;
213 update->reserved_ranges[update->reserved_ranges_count]
214 .end = before[i].end;
215 update->reserved_ranges_count++;
216 }
217 }
218
219 return true;
220}
221
222/**
223 * Loads all secondary VMs into the memory ranges from the given params.
224 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
225 */
226bool load_secondary(const struct memiter *cpio,
227 const struct boot_params *params,
228 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100229{
230 struct memiter it;
231 struct memiter str;
232 uint64_t mem;
233 uint64_t cpu;
234 uint32_t count;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100235 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
236 size_t i;
237
238 static_assert(
239 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
240 "mem_range arrays must be the same size for memcpy.");
241 static_assert(sizeof(mem_ranges_available) < 500,
242 "This will use too much stack, either make "
243 "MAX_MEM_RANGES smaller or change this.");
244 memcpy(mem_ranges_available, params->mem_ranges,
245 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100246
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100247 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100248 dlog("vms.txt is missing\n");
249 return true;
250 }
251
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100252 /* Round the last addresses down to the page size. */
253 for (i = 0; i < params->mem_ranges_count; ++i) {
254 mem_ranges_available[i].end =
255 pa_init(pa_addr(mem_ranges_available[i].end) &
256 ~(PAGE_SIZE - 1));
257 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100258
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100259 for (count = 0;
260 memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
261 memiter_parse_str(&it, &str) && count < MAX_VMS;
262 count++) {
263 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100264 paddr_t secondary_mem_begin;
265 paddr_t secondary_mem_end;
266 ipaddr_t secondary_entry;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100267
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100268 if (!memiter_find_file(cpio, &str, &kernel)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269 dlog("Unable to load kernel for vm %u\n", count);
270 continue;
271 }
272
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100273 /* Round up to page size. */
274 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100275
276 if (mem < kernel.limit - kernel.next) {
277 dlog("Kernel is larger than available memory for vm "
278 "%u\n",
279 count);
280 continue;
281 }
282
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100283 if (!carve_out_mem_range(
284 mem_ranges_available, params->mem_ranges_count, mem,
285 &secondary_mem_begin, &secondary_mem_end)) {
286 dlog("Not enough memory for vm %u (%u bytes)\n", count,
287 mem);
288 continue;
289 }
Andrew Scull80871322018-08-06 12:04:09 +0100290
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100291 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100292 kernel.limit - kernel.next)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100293 dlog("Unable to copy kernel for vm %u\n", count);
294 continue;
295 }
296
Andrew Scullf3d45592018-09-20 14:30:22 +0100297 if (!vm_init(&secondary_vm[count], count + 1, cpu)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100298 dlog("Unable to initialise vm %u\n", count);
299 continue;
300 }
301
302 /* TODO: Remove this. */
303 /* Grant VM access to uart. */
Andrew Scull80871322018-08-06 12:04:09 +0100304 mm_vm_identity_map_page(&secondary_vm[count].ptable,
305 pa_init(PL011_BASE),
306 MM_MODE_R | MM_MODE_W | MM_MODE_D |
307 MM_MODE_NOINVALIDATE,
308 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100309
310 /* Grant the VM access to the memory. */
Andrew Scull80871322018-08-06 12:04:09 +0100311 if (!mm_vm_identity_map(&secondary_vm[count].ptable,
312 secondary_mem_begin, secondary_mem_end,
313 MM_MODE_R | MM_MODE_W | MM_MODE_X |
314 MM_MODE_NOINVALIDATE,
315 &secondary_entry)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100316 dlog("Unable to initialise memory for vm %u\n", count);
317 continue;
318 }
319
320 /* Deny the primary VM access to this memory. */
Andrew Scull80871322018-08-06 12:04:09 +0100321 if (!mm_vm_unmap(&primary_vm.ptable, secondary_mem_begin,
322 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100323 dlog("Unable to unmap secondary VM from primary VM\n");
324 return false;
325 }
326
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100327 dlog("Loaded VM%u with %u vcpus, entry at 0x%x\n", count, cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100328 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100329
Andrew Scullf3d45592018-09-20 14:30:22 +0100330 vm_start_vcpu(&secondary_vm[count], 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100331 }
332
333 secondary_vm_count = count;
334
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100335 /* Add newly reserved areas to update params by looking at the
336 * difference between the available ranges from the original params and
337 * the updated mem_ranges_available. We assume that the number and order
338 * of available ranges is the same, i.e. we don't remove any ranges
339 * above only make them smaller. */
340 return update_reserved_ranges(update, params->mem_ranges,
341 mem_ranges_available,
342 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100343}