blob: a9c0e5dee13dbc7cb1fc60d33b3877cfd4c6729e [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.
Andrew Sculld9225b32018-11-19 16:12:41 +000036 *
37 * The data is written so that it is available to all cores with the cache
38 * disabled. When switching to the partitions, the caching is initially disabled
39 * so the data must be available without the cache.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010040 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000041static bool copy_to_unmapped(paddr_t to, const void *from, size_t size,
42 struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010043{
Andrew Scull80871322018-08-06 12:04:09 +010044 paddr_t to_end = pa_add(to, size);
45 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010046
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000047 ptr = mm_identity_map(to, to_end, MM_MODE_W, ppool);
Andrew Scull80871322018-08-06 12:04:09 +010048 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010049 return false;
50 }
51
Andrew Scull80871322018-08-06 12:04:09 +010052 memcpy(ptr, from, size);
Andrew Sculld9225b32018-11-19 16:12:41 +000053 arch_mm_write_back_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010054
Andrew Scullda241972019-01-05 18:17:48 +000055 mm_unmap(to, to_end, ppool);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010056
57 return true;
58}
59
60/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010061 * Looks for a file in the given cpio archive. The filename is not
62 * null-terminated, so we use a memory iterator to represent it. The file, if
63 * found, is returned in the "it" argument.
64 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010065static bool memiter_find_file(const struct memiter *cpio,
66 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010067 struct memiter *it)
68{
69 const char *fname;
70 const void *fcontents;
71 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010072 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010073
74 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
75 if (memiter_iseq(filename, fname)) {
76 memiter_init(it, fcontents, fsize);
77 return true;
78 }
79 }
80
81 return false;
82}
83
84/**
85 * Looks for a file in the given cpio archive. The file, if found, is returned
86 * in the "it" argument.
87 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010088static bool find_file(const struct memiter *cpio, const char *name,
89 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010090{
91 const char *fname;
92 const void *fcontents;
93 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010094 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010095
96 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
97 if (!strcmp(fname, name)) {
98 memiter_init(it, fcontents, fsize);
99 return true;
100 }
101 }
102
103 return false;
104}
105
106/**
107 * Loads the primary VM.
108 */
Andrew Scull37402872018-10-24 14:23:06 +0100109bool load_primary(const struct memiter *cpio, uintreg_t kernel_arg,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000110 struct memiter *initrd, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100111{
112 struct memiter it;
Andrew Scullf16c0c22018-10-26 18:41:24 +0100113 paddr_t primary_begin = layout_primary_begin();
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100114
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100115 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100116 dlog("Unable to find vmlinuz\n");
117 return false;
118 }
119
Andrew Scullf16c0c22018-10-26 18:41:24 +0100120 dlog("Copying primary to %p\n", pa_addr(primary_begin));
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000121 if (!copy_to_unmapped(primary_begin, it.next, it.limit - it.next,
122 ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100123 dlog("Unable to relocate kernel for primary vm.\n");
124 return false;
125 }
126
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100127 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100128 dlog("Unable to find initrd.img\n");
129 return false;
130 }
131
132 {
Andrew Scull19503262018-09-20 14:48:39 +0100133 struct vm *vm;
134
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000135 if (!vm_init(MAX_CPUS, ppool, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100136 dlog("Unable to initialise primary vm\n");
137 return false;
138 }
139
Andrew Scull19503262018-09-20 14:48:39 +0100140 if (vm->id != HF_PRIMARY_VM_ID) {
141 dlog("Primary vm was not given correct id\n");
142 return false;
143 }
144
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100145 /* Map the 1TB of memory. */
146 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100147 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100148 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100149 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
Andrew Scullda241972019-01-05 18:17:48 +0000150 MM_MODE_R | MM_MODE_W | MM_MODE_X, NULL, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100151 dlog("Unable to initialise memory for primary vm\n");
152 return false;
153 }
154
Andrew Scullda241972019-01-05 18:17:48 +0000155 if (!mm_vm_unmap_hypervisor(&vm->ptable, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100156 dlog("Unable to unmap hypervisor from primary vm\n");
157 return false;
158 }
159
Andrew Scullf16c0c22018-10-26 18:41:24 +0100160 vm_start_vcpu(vm, 0, ipa_from_pa(primary_begin), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100161 }
162
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100163 return true;
164}
165
166/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100167 * Try to find a memory range of the given size within the given ranges, and
168 * remove it from them. Return true on success, or false if no large enough
169 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100170 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100171bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
172 uint64_t size_to_find, paddr_t *found_begin,
173 paddr_t *found_end)
174{
175 size_t i;
176
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000177 /*
178 * TODO(b/116191358): Consider being cleverer about how we pack VMs
179 * together, with a non-greedy algorithm.
180 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100181 for (i = 0; i < mem_ranges_count; ++i) {
182 if (size_to_find <=
183 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100184 /*
185 * This range is big enough, take some of it from the
186 * end and reduce its size accordingly.
187 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100188 *found_end = mem_ranges[i].end;
189 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
190 size_to_find);
191 mem_ranges[i].end = *found_begin;
192 return true;
193 }
194 }
195 return false;
196}
197
198/**
199 * Given arrays of memory ranges before and after memory was removed for
200 * secondary VMs, add the difference to the reserved ranges of the given update.
201 * Return true on success, or false if there would be more than MAX_MEM_RANGES
202 * reserved ranges after adding the new ones.
203 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
204 */
205bool update_reserved_ranges(struct boot_params_update *update,
206 const struct mem_range *before,
207 const struct mem_range *after,
208 size_t mem_ranges_count)
209{
210 size_t i;
211
212 for (i = 0; i < mem_ranges_count; ++i) {
213 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
214 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
215 dlog("Too many reserved ranges after loading "
216 "secondary VMs.\n");
217 return false;
218 }
219 update->reserved_ranges[update->reserved_ranges_count]
220 .begin = before[i].begin;
221 update->reserved_ranges[update->reserved_ranges_count]
222 .end = after[i].begin;
223 update->reserved_ranges_count++;
224 }
225 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
226 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
227 dlog("Too many reserved ranges after loading "
228 "secondary VMs.\n");
229 return false;
230 }
231 update->reserved_ranges[update->reserved_ranges_count]
232 .begin = after[i].end;
233 update->reserved_ranges[update->reserved_ranges_count]
234 .end = before[i].end;
235 update->reserved_ranges_count++;
236 }
237 }
238
239 return true;
240}
241
242/**
243 * Loads all secondary VMs into the memory ranges from the given params.
244 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
245 */
246bool load_secondary(const struct memiter *cpio,
247 const struct boot_params *params,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000248 struct boot_params_update *update, struct mpool *ppool)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100249{
Andrew Scull19503262018-09-20 14:48:39 +0100250 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100251 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100252 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100253 uint64_t mem;
254 uint64_t cpu;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100255 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
256 size_t i;
257
258 static_assert(
259 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
260 "mem_range arrays must be the same size for memcpy.");
261 static_assert(sizeof(mem_ranges_available) < 500,
262 "This will use too much stack, either make "
263 "MAX_MEM_RANGES smaller or change this.");
264 memcpy(mem_ranges_available, params->mem_ranges,
265 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100266
Andrew Scull19503262018-09-20 14:48:39 +0100267 primary = vm_get(HF_PRIMARY_VM_ID);
268
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100269 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100270 dlog("vms.txt is missing\n");
271 return true;
272 }
273
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100274 /* Round the last addresses down to the page size. */
275 for (i = 0; i < params->mem_ranges_count; ++i) {
276 mem_ranges_available[i].end =
277 pa_init(pa_addr(mem_ranges_available[i].end) &
278 ~(PAGE_SIZE - 1));
279 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100280
Andrew Scull19503262018-09-20 14:48:39 +0100281 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
282 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100283 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100284 paddr_t secondary_mem_begin;
285 paddr_t secondary_mem_end;
286 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100287 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100288 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100289
Andrew Scull36e4bae2018-09-27 17:50:56 +0100290 dlog("Loading ");
291 for (p = name.next; p != name.limit; ++p) {
292 dlog("%c", *p);
293 }
294 dlog("\n");
295
296 if (!memiter_find_file(cpio, &name, &kernel)) {
297 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100298 continue;
299 }
300
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100301 /* Round up to page size. */
302 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100303
304 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100305 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100306 continue;
307 }
308
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100309 if (!carve_out_mem_range(
310 mem_ranges_available, params->mem_ranges_count, mem,
311 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100312 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100313 continue;
314 }
Andrew Scull80871322018-08-06 12:04:09 +0100315
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100316 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000317 kernel.limit - kernel.next, ppool)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100318 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100319 continue;
320 }
321
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000322 if (!vm_init(cpu, ppool, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100323 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100324 continue;
325 }
326
327 /* TODO: Remove this. */
328 /* Grant VM access to uart. */
Andrew Scull24e032f2018-10-15 17:18:12 +0100329 mm_vm_identity_map(&vm->ptable, pa_init(PL011_BASE),
330 pa_add(pa_init(PL011_BASE), PAGE_SIZE),
Andrew Scullda241972019-01-05 18:17:48 +0000331 MM_MODE_R | MM_MODE_W, NULL, ppool);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100332
333 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100334 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
335 secondary_mem_end,
Andrew Scullda241972019-01-05 18:17:48 +0000336 MM_MODE_R | MM_MODE_W | MM_MODE_X,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000337 &secondary_entry, ppool)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100338 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100339 continue;
340 }
341
342 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100343 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scullda241972019-01-05 18:17:48 +0000344 secondary_mem_end, ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100345 dlog("Unable to unmap secondary VM from primary VM\n");
346 return false;
347 }
348
Andrew Scull36e4bae2018-09-27 17:50:56 +0100349 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100350 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100351
Andrew Scull19503262018-09-20 14:48:39 +0100352 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100353 }
354
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100355 /*
356 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100357 * difference between the available ranges from the original params and
358 * the updated mem_ranges_available. We assume that the number and order
359 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100360 * above only make them smaller.
361 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100362 return update_reserved_ranges(update, params->mem_ranges,
363 mem_ranges_available,
364 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100365}