blob: 0c642e3a77c279cb8c07bca0527dd497bd207bcf [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 */
Andrew Walbranfd265ec2018-09-12 17:50:55 +010041static bool copy_to_unmapped(paddr_t to, const void *from, size_t size)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010042{
Andrew Scull80871322018-08-06 12:04:09 +010043 paddr_t to_end = pa_add(to, size);
44 void *ptr;
Andrew Scull265ada92018-07-30 15:19:01 +010045
Andrew Scull80871322018-08-06 12:04:09 +010046 ptr = mm_identity_map(to, to_end, MM_MODE_W);
47 if (!ptr) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048 return false;
49 }
50
Andrew Scull80871322018-08-06 12:04:09 +010051 memcpy(ptr, from, size);
Andrew Sculld9225b32018-11-19 16:12:41 +000052 arch_mm_write_back_dcache(ptr, size);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010053
Andrew Scull80871322018-08-06 12:04:09 +010054 mm_unmap(to, to_end, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010055
56 return true;
57}
58
59/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010060 * Looks for a file in the given cpio archive. The filename is not
61 * null-terminated, so we use a memory iterator to represent it. The file, if
62 * found, is returned in the "it" argument.
63 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010064static bool memiter_find_file(const struct memiter *cpio,
65 const struct memiter *filename,
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010066 struct memiter *it)
67{
68 const char *fname;
69 const void *fcontents;
70 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010071 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010072
73 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
74 if (memiter_iseq(filename, fname)) {
75 memiter_init(it, fcontents, fsize);
76 return true;
77 }
78 }
79
80 return false;
81}
82
83/**
84 * Looks for a file in the given cpio archive. The file, if found, is returned
85 * in the "it" argument.
86 */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010087static bool find_file(const struct memiter *cpio, const char *name,
88 struct memiter *it)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010089{
90 const char *fname;
91 const void *fcontents;
92 size_t fsize;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010093 struct memiter iter = *cpio;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010094
95 while (cpio_next(&iter, &fname, &fcontents, &fsize)) {
96 if (!strcmp(fname, name)) {
97 memiter_init(it, fcontents, fsize);
98 return true;
99 }
100 }
101
102 return false;
103}
104
105/**
106 * Loads the primary VM.
107 */
Andrew Scull37402872018-10-24 14:23:06 +0100108bool load_primary(const struct memiter *cpio, uintreg_t kernel_arg,
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100109 struct memiter *initrd)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100110{
111 struct memiter it;
Andrew Scullf16c0c22018-10-26 18:41:24 +0100112 paddr_t primary_begin = layout_primary_begin();
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100113
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100114 if (!find_file(cpio, "vmlinuz", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100115 dlog("Unable to find vmlinuz\n");
116 return false;
117 }
118
Andrew Scullf16c0c22018-10-26 18:41:24 +0100119 dlog("Copying primary to %p\n", pa_addr(primary_begin));
120 if (!copy_to_unmapped(primary_begin, it.next, it.limit - it.next)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100121 dlog("Unable to relocate kernel for primary vm.\n");
122 return false;
123 }
124
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100125 if (!find_file(cpio, "initrd.img", initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100126 dlog("Unable to find initrd.img\n");
127 return false;
128 }
129
130 {
Andrew Scull19503262018-09-20 14:48:39 +0100131 struct vm *vm;
132
Andrew Scull19503262018-09-20 14:48:39 +0100133 if (!vm_init(MAX_CPUS, &vm)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100134 dlog("Unable to initialise primary vm\n");
135 return false;
136 }
137
Andrew Scull19503262018-09-20 14:48:39 +0100138 if (vm->id != HF_PRIMARY_VM_ID) {
139 dlog("Primary vm was not given correct id\n");
140 return false;
141 }
142
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100143 /* Map the 1TB of memory. */
144 /* TODO: We should do a whitelist rather than a blacklist. */
Andrew Scull78d6fd92018-09-06 15:08:36 +0100145 if (!mm_vm_identity_map(
Andrew Scull19503262018-09-20 14:48:39 +0100146 &vm->ptable, pa_init(0),
Andrew Scull78d6fd92018-09-06 15:08:36 +0100147 pa_init(UINT64_C(1024) * 1024 * 1024 * 1024),
148 MM_MODE_R | MM_MODE_W | MM_MODE_X |
149 MM_MODE_NOINVALIDATE,
150 NULL)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100151 dlog("Unable to initialise memory for primary vm\n");
152 return false;
153 }
154
Andrew Scull19503262018-09-20 14:48:39 +0100155 if (!mm_ptable_unmap_hypervisor(&vm->ptable,
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100156 MM_MODE_NOINVALIDATE)) {
157 dlog("Unable to unmap hypervisor from primary vm\n");
158 return false;
159 }
160
Andrew Scullf16c0c22018-10-26 18:41:24 +0100161 vm_start_vcpu(vm, 0, ipa_from_pa(primary_begin), kernel_arg);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100162 }
163
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100164 return true;
165}
166
167/**
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100168 * Try to find a memory range of the given size within the given ranges, and
169 * remove it from them. Return true on success, or false if no large enough
170 * contiguous range is found.
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100171 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100172bool carve_out_mem_range(struct mem_range *mem_ranges, size_t mem_ranges_count,
173 uint64_t size_to_find, paddr_t *found_begin,
174 paddr_t *found_end)
175{
176 size_t i;
177
178 /* TODO(b/116191358): Consider being cleverer about how we pack VMs
179 * together, with a non-greedy algorithm. */
180 for (i = 0; i < mem_ranges_count; ++i) {
181 if (size_to_find <=
182 pa_addr(mem_ranges[i].end) - pa_addr(mem_ranges[i].begin)) {
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100183 /*
184 * This range is big enough, take some of it from the
185 * end and reduce its size accordingly.
186 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100187 *found_end = mem_ranges[i].end;
188 *found_begin = pa_init(pa_addr(mem_ranges[i].end) -
189 size_to_find);
190 mem_ranges[i].end = *found_begin;
191 return true;
192 }
193 }
194 return false;
195}
196
197/**
198 * Given arrays of memory ranges before and after memory was removed for
199 * secondary VMs, add the difference to the reserved ranges of the given update.
200 * Return true on success, or false if there would be more than MAX_MEM_RANGES
201 * reserved ranges after adding the new ones.
202 * `before` and `after` must be arrays of exactly `mem_ranges_count` elements.
203 */
204bool update_reserved_ranges(struct boot_params_update *update,
205 const struct mem_range *before,
206 const struct mem_range *after,
207 size_t mem_ranges_count)
208{
209 size_t i;
210
211 for (i = 0; i < mem_ranges_count; ++i) {
212 if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) {
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 = before[i].begin;
220 update->reserved_ranges[update->reserved_ranges_count]
221 .end = after[i].begin;
222 update->reserved_ranges_count++;
223 }
224 if (pa_addr(after[i].end) < pa_addr(before[i].end)) {
225 if (update->reserved_ranges_count >= MAX_MEM_RANGES) {
226 dlog("Too many reserved ranges after loading "
227 "secondary VMs.\n");
228 return false;
229 }
230 update->reserved_ranges[update->reserved_ranges_count]
231 .begin = after[i].end;
232 update->reserved_ranges[update->reserved_ranges_count]
233 .end = before[i].end;
234 update->reserved_ranges_count++;
235 }
236 }
237
238 return true;
239}
240
241/**
242 * Loads all secondary VMs into the memory ranges from the given params.
243 * Memory reserved for the VMs is added to the `reserved_ranges` of `update`.
244 */
245bool load_secondary(const struct memiter *cpio,
246 const struct boot_params *params,
247 struct boot_params_update *update)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100248{
Andrew Scull19503262018-09-20 14:48:39 +0100249 struct vm *primary;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100250 struct memiter it;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100251 struct memiter name;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100252 uint64_t mem;
253 uint64_t cpu;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100254 struct mem_range mem_ranges_available[MAX_MEM_RANGES];
255 size_t i;
256
257 static_assert(
258 sizeof(mem_ranges_available) == sizeof(params->mem_ranges),
259 "mem_range arrays must be the same size for memcpy.");
260 static_assert(sizeof(mem_ranges_available) < 500,
261 "This will use too much stack, either make "
262 "MAX_MEM_RANGES smaller or change this.");
263 memcpy(mem_ranges_available, params->mem_ranges,
264 sizeof(mem_ranges_available));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100265
Andrew Scull19503262018-09-20 14:48:39 +0100266 primary = vm_get(HF_PRIMARY_VM_ID);
267
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100268 if (!find_file(cpio, "vms.txt", &it)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100269 dlog("vms.txt is missing\n");
270 return true;
271 }
272
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100273 /* Round the last addresses down to the page size. */
274 for (i = 0; i < params->mem_ranges_count; ++i) {
275 mem_ranges_available[i].end =
276 pa_init(pa_addr(mem_ranges_available[i].end) &
277 ~(PAGE_SIZE - 1));
278 }
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100279
Andrew Scull19503262018-09-20 14:48:39 +0100280 while (memiter_parse_uint(&it, &mem) && memiter_parse_uint(&it, &cpu) &&
281 memiter_parse_str(&it, &name)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100282 struct memiter kernel;
Andrew Scull80871322018-08-06 12:04:09 +0100283 paddr_t secondary_mem_begin;
284 paddr_t secondary_mem_end;
285 ipaddr_t secondary_entry;
Andrew Scull36e4bae2018-09-27 17:50:56 +0100286 const char *p;
Andrew Scull19503262018-09-20 14:48:39 +0100287 struct vm *vm;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100288
Andrew Scull36e4bae2018-09-27 17:50:56 +0100289 dlog("Loading ");
290 for (p = name.next; p != name.limit; ++p) {
291 dlog("%c", *p);
292 }
293 dlog("\n");
294
295 if (!memiter_find_file(cpio, &name, &kernel)) {
296 dlog("Unable to load kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100297 continue;
298 }
299
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100300 /* Round up to page size. */
301 mem = (mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100302
303 if (mem < kernel.limit - kernel.next) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100304 dlog("Kernel is larger than available memory\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100305 continue;
306 }
307
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100308 if (!carve_out_mem_range(
309 mem_ranges_available, params->mem_ranges_count, mem,
310 &secondary_mem_begin, &secondary_mem_end)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100311 dlog("Not enough memory (%u bytes)\n", mem);
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100312 continue;
313 }
Andrew Scull80871322018-08-06 12:04:09 +0100314
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100315 if (!copy_to_unmapped(secondary_mem_begin, kernel.next,
Andrew Walbranfd265ec2018-09-12 17:50:55 +0100316 kernel.limit - kernel.next)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100317 dlog("Unable to copy kernel\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100318 continue;
319 }
320
Andrew Scull19503262018-09-20 14:48:39 +0100321 if (!vm_init(cpu, &vm)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100322 dlog("Unable to initialise VM\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100323 continue;
324 }
325
326 /* TODO: Remove this. */
327 /* Grant VM access to uart. */
Andrew Scull24e032f2018-10-15 17:18:12 +0100328 mm_vm_identity_map(&vm->ptable, pa_init(PL011_BASE),
329 pa_add(pa_init(PL011_BASE), PAGE_SIZE),
330 MM_MODE_R | MM_MODE_W | MM_MODE_D |
331 MM_MODE_NOINVALIDATE,
332 NULL);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100333
334 /* Grant the VM access to the memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100335 if (!mm_vm_identity_map(&vm->ptable, secondary_mem_begin,
336 secondary_mem_end,
Andrew Scull80871322018-08-06 12:04:09 +0100337 MM_MODE_R | MM_MODE_W | MM_MODE_X |
338 MM_MODE_NOINVALIDATE,
339 &secondary_entry)) {
Andrew Scull36e4bae2018-09-27 17:50:56 +0100340 dlog("Unable to initialise memory\n");
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100341 continue;
342 }
343
344 /* Deny the primary VM access to this memory. */
Andrew Scull19503262018-09-20 14:48:39 +0100345 if (!mm_vm_unmap(&primary->ptable, secondary_mem_begin,
Andrew Scull80871322018-08-06 12:04:09 +0100346 secondary_mem_end, MM_MODE_NOINVALIDATE)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100347 dlog("Unable to unmap secondary VM from primary VM\n");
348 return false;
349 }
350
Andrew Scull36e4bae2018-09-27 17:50:56 +0100351 dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100352 pa_addr(secondary_mem_begin));
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100353
Andrew Scull19503262018-09-20 14:48:39 +0100354 vm_start_vcpu(vm, 0, secondary_entry, 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100355 }
356
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100357 /*
358 * Add newly reserved areas to update params by looking at the
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100359 * difference between the available ranges from the original params and
360 * the updated mem_ranges_available. We assume that the number and order
361 * of available ranges is the same, i.e. we don't remove any ranges
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100362 * above only make them smaller.
363 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100364 return update_reserved_ranges(update, params->mem_ranges,
365 mem_ranges_available,
366 params->mem_ranges_count);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100367}