Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/load.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stdbool.h> |
| 20 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 21 | #include "hf/arch/vm.h" |
| 22 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/api.h" |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 24 | #include "hf/boot_params.h" |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 25 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 26 | #include "hf/dlog.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 27 | #include "hf/layout.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 28 | #include "hf/memiter.h" |
| 29 | #include "hf/mm.h" |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 30 | #include "hf/plat/console.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 31 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 32 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 33 | #include "hf/vm.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 34 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 35 | #include "vmapi/hf/call.h" |
| 36 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 37 | /** |
| 38 | * Copies data to an unmapped location by mapping it for write, copying the |
| 39 | * data, then unmapping it. |
Andrew Scull | d9225b3 | 2018-11-19 16:12:41 +0000 | [diff] [blame] | 40 | * |
| 41 | * The data is written so that it is available to all cores with the cache |
| 42 | * disabled. When switching to the partitions, the caching is initially disabled |
| 43 | * so the data must be available without the cache. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 44 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 45 | static bool copy_to_unmapped(struct mm_stage1_locked stage1_locked, paddr_t to, |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 46 | struct memiter *from_it, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 47 | { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 48 | const void *from = memiter_base(from_it); |
| 49 | size_t size = memiter_size(from_it); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 50 | paddr_t to_end = pa_add(to, size); |
| 51 | void *ptr; |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 52 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 53 | ptr = mm_identity_map(stage1_locked, to, to_end, MM_MODE_W, ppool); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 54 | if (!ptr) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 58 | memcpy_s(ptr, size, from, size); |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 59 | arch_mm_flush_dcache(ptr, size); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 60 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 61 | CHECK(mm_unmap(stage1_locked, to, to_end, ppool)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 66 | static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin, |
| 67 | paddr_t end, const struct manifest_vm *manifest_vm, |
| 68 | const struct memiter *cpio, struct mpool *ppool) |
| 69 | { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 70 | struct memiter kernel; |
| 71 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 72 | if (string_is_empty(&manifest_vm->kernel_filename)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 73 | /* This signals the kernel has been preloaded. */ |
| 74 | return true; |
| 75 | } |
| 76 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 77 | if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 78 | dlog("Could not find kernel file \"%s\".\n", |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 79 | string_data(&manifest_vm->kernel_filename)); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if (pa_difference(begin, end) < memiter_size(&kernel)) { |
| 84 | dlog("Kernel is larger than available memory.\n"); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) { |
| 89 | dlog("Unable to copy kernel.\n"); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 96 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 97 | * Performs VM loading activities that are common between the primary and |
| 98 | * secondaries. |
| 99 | */ |
| 100 | static bool load_common(const struct manifest_vm *manifest_vm, struct vm *vm) |
| 101 | { |
| 102 | vm->smc_whitelist = manifest_vm->smc_whitelist; |
| 103 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 104 | /* Initialize architecture-specific features. */ |
| 105 | arch_vm_features_set(vm); |
| 106 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 111 | * Loads the primary VM. |
| 112 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 113 | static bool load_primary(struct mm_stage1_locked stage1_locked, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 114 | const struct manifest_vm *manifest_vm, |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 115 | const struct memiter *cpio, |
| 116 | const struct boot_params *params, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 117 | { |
Andrew Scull | f16c0c2 | 2018-10-26 18:41:24 +0100 | [diff] [blame] | 118 | paddr_t primary_begin = layout_primary_begin(); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 119 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 120 | struct vm_locked vm_locked; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 121 | struct vcpu_locked vcpu_locked; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 122 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 123 | bool ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 124 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 125 | /* |
| 126 | * TODO: This bound is currently meaningless but will be addressed when |
| 127 | * the manifest specifies the load address. |
| 128 | */ |
| 129 | paddr_t primary_end = pa_add(primary_begin, 0x8000000); |
| 130 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 131 | if (!load_kernel(stage1_locked, primary_begin, primary_end, manifest_vm, |
| 132 | cpio, ppool)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 133 | dlog("Unable to load primary kernel."); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 137 | if (!vm_init(MAX_CPUS, ppool, &vm)) { |
| 138 | dlog("Unable to initialise primary vm\n"); |
| 139 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 140 | } |
| 141 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 142 | if (vm->id != HF_PRIMARY_VM_ID) { |
| 143 | dlog("Primary vm was not given correct id\n"); |
| 144 | return false; |
| 145 | } |
| 146 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 147 | vm_locked = vm_lock(vm); |
| 148 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 149 | if (!load_common(manifest_vm, vm)) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 150 | ret = false; |
| 151 | goto out; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 154 | /* |
| 155 | * Map 1TB of address space as device memory to, most likely, make all |
| 156 | * devices available to the primary VM. |
| 157 | * |
| 158 | * TODO: We should do a whitelist rather than a blacklist. |
| 159 | */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 160 | if (!vm_identity_map(vm_locked, pa_init(0), |
| 161 | pa_init(UINT64_C(1024) * 1024 * 1024 * 1024), |
| 162 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 163 | dlog("Unable to initialise address space for primary vm\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 164 | ret = false; |
| 165 | goto out; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 168 | /* Map normal memory as such to permit caching, execution, etc. */ |
| 169 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 170 | if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin, |
| 171 | params->mem_ranges[i].end, |
| 172 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 173 | NULL)) { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 174 | dlog("Unable to initialise memory for primary vm\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 175 | ret = false; |
| 176 | goto out; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 180 | if (!vm_unmap_hypervisor(vm_locked, ppool)) { |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 181 | dlog("Unable to unmap hypervisor from primary vm\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 182 | ret = false; |
| 183 | goto out; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0)); |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 187 | vcpu_on(vcpu_locked, ipa_from_pa(primary_begin), params->kernel_arg); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 188 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 189 | ret = true; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 190 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 191 | out: |
| 192 | vm_unlock(&vm_locked); |
| 193 | |
| 194 | return ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 197 | /* |
| 198 | * Loads a secondary VM. |
| 199 | */ |
| 200 | static bool load_secondary(struct mm_stage1_locked stage1_locked, |
| 201 | paddr_t mem_begin, paddr_t mem_end, |
| 202 | const struct manifest_vm *manifest_vm, |
| 203 | const struct memiter *cpio, struct mpool *ppool) |
| 204 | { |
| 205 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 206 | struct vm_locked vm_locked; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 207 | struct vcpu *vcpu; |
| 208 | ipaddr_t secondary_entry; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 209 | bool ret; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 210 | |
| 211 | if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, cpio, |
| 212 | ppool)) { |
| 213 | dlog("Unable to load kernel.\n"); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | if (!vm_init(manifest_vm->secondary.vcpu_count, ppool, &vm)) { |
| 218 | dlog("Unable to initialise VM.\n"); |
| 219 | return false; |
| 220 | } |
| 221 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 222 | if (!load_common(manifest_vm, vm)) { |
| 223 | return false; |
| 224 | } |
| 225 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 226 | vm_locked = vm_lock(vm); |
| 227 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 228 | /* Grant the VM access to the memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 229 | if (!vm_identity_map(vm_locked, mem_begin, mem_end, |
| 230 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 231 | &secondary_entry)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 232 | dlog("Unable to initialise memory.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 233 | ret = false; |
| 234 | goto out; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 237 | dlog("Loaded with %u vCPUs, entry at %#x.\n", |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 238 | manifest_vm->secondary.vcpu_count, pa_addr(mem_begin)); |
| 239 | |
| 240 | vcpu = vm_get_vcpu(vm, 0); |
| 241 | vcpu_secondary_reset_and_start(vcpu, secondary_entry, |
| 242 | pa_difference(mem_begin, mem_end)); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 243 | ret = true; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 244 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 245 | out: |
| 246 | vm_unlock(&vm_locked); |
| 247 | |
| 248 | return ret; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 251 | /** |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 252 | * Try to find a memory range of the given size within the given ranges, and |
| 253 | * remove it from them. Return true on success, or false if no large enough |
| 254 | * contiguous range is found. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 255 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 256 | static bool carve_out_mem_range(struct mem_range *mem_ranges, |
| 257 | size_t mem_ranges_count, uint64_t size_to_find, |
| 258 | paddr_t *found_begin, paddr_t *found_end) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 259 | { |
| 260 | size_t i; |
| 261 | |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 262 | /* |
| 263 | * TODO(b/116191358): Consider being cleverer about how we pack VMs |
| 264 | * together, with a non-greedy algorithm. |
| 265 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 266 | for (i = 0; i < mem_ranges_count; ++i) { |
| 267 | if (size_to_find <= |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 268 | pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) { |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 269 | /* |
| 270 | * This range is big enough, take some of it from the |
| 271 | * end and reduce its size accordingly. |
| 272 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 273 | *found_end = mem_ranges[i].end; |
| 274 | *found_begin = pa_init(pa_addr(mem_ranges[i].end) - |
| 275 | size_to_find); |
| 276 | mem_ranges[i].end = *found_begin; |
| 277 | return true; |
| 278 | } |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Given arrays of memory ranges before and after memory was removed for |
| 285 | * secondary VMs, add the difference to the reserved ranges of the given update. |
| 286 | * Return true on success, or false if there would be more than MAX_MEM_RANGES |
| 287 | * reserved ranges after adding the new ones. |
| 288 | * `before` and `after` must be arrays of exactly `mem_ranges_count` elements. |
| 289 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 290 | static bool update_reserved_ranges(struct boot_params_update *update, |
| 291 | const struct mem_range *before, |
| 292 | const struct mem_range *after, |
| 293 | size_t mem_ranges_count) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 294 | { |
| 295 | size_t i; |
| 296 | |
| 297 | for (i = 0; i < mem_ranges_count; ++i) { |
| 298 | if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) { |
| 299 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
| 300 | dlog("Too many reserved ranges after loading " |
| 301 | "secondary VMs.\n"); |
| 302 | return false; |
| 303 | } |
| 304 | update->reserved_ranges[update->reserved_ranges_count] |
| 305 | .begin = before[i].begin; |
| 306 | update->reserved_ranges[update->reserved_ranges_count] |
| 307 | .end = after[i].begin; |
| 308 | update->reserved_ranges_count++; |
| 309 | } |
| 310 | if (pa_addr(after[i].end) < pa_addr(before[i].end)) { |
| 311 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
| 312 | dlog("Too many reserved ranges after loading " |
| 313 | "secondary VMs.\n"); |
| 314 | return false; |
| 315 | } |
| 316 | update->reserved_ranges[update->reserved_ranges_count] |
| 317 | .begin = after[i].end; |
| 318 | update->reserved_ranges[update->reserved_ranges_count] |
| 319 | .end = before[i].end; |
| 320 | update->reserved_ranges_count++; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Loads alls VMs from the manifest. |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 329 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 330 | bool load_vms(struct mm_stage1_locked stage1_locked, |
| 331 | const struct manifest *manifest, const struct memiter *cpio, |
| 332 | const struct boot_params *params, |
| 333 | struct boot_params_update *update, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 334 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 335 | struct vm *primary; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 336 | struct mem_range mem_ranges_available[MAX_MEM_RANGES]; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 337 | struct vm_locked primary_vm_locked; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 338 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 339 | bool success = true; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 340 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 341 | if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX], |
| 342 | cpio, params, ppool)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 343 | dlog("Unable to load primary VM.\n"); |
| 344 | return false; |
| 345 | } |
| 346 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 347 | static_assert( |
| 348 | sizeof(mem_ranges_available) == sizeof(params->mem_ranges), |
| 349 | "mem_range arrays must be the same size for memcpy."); |
| 350 | static_assert(sizeof(mem_ranges_available) < 500, |
| 351 | "This will use too much stack, either make " |
| 352 | "MAX_MEM_RANGES smaller or change this."); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 353 | memcpy_s(mem_ranges_available, sizeof(mem_ranges_available), |
| 354 | params->mem_ranges, sizeof(params->mem_ranges)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 355 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 356 | /* Round the last addresses down to the page size. */ |
| 357 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 358 | mem_ranges_available[i].end = pa_init(align_down( |
| 359 | pa_addr(mem_ranges_available[i].end), PAGE_SIZE)); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 360 | } |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 361 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 362 | primary = vm_find(HF_PRIMARY_VM_ID); |
| 363 | primary_vm_locked = vm_lock(primary); |
| 364 | |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 365 | for (i = 0; i < manifest->vm_count; ++i) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 366 | const struct manifest_vm *manifest_vm = &manifest->vm[i]; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 367 | spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 368 | uint64_t mem_size; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 369 | paddr_t secondary_mem_begin; |
| 370 | paddr_t secondary_mem_end; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 371 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 372 | if (vm_id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 373 | continue; |
| 374 | } |
| 375 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 376 | dlog("Loading VM%d: %s.\n", (int)vm_id, |
| 377 | manifest_vm->debug_name); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 378 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 379 | mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 380 | if (!carve_out_mem_range(mem_ranges_available, |
| 381 | params->mem_ranges_count, mem_size, |
| 382 | &secondary_mem_begin, |
| 383 | &secondary_mem_end)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 384 | dlog("Not enough memory (%u bytes).\n", mem_size); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 385 | continue; |
| 386 | } |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 387 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 388 | if (!load_secondary(stage1_locked, secondary_mem_begin, |
| 389 | secondary_mem_end, manifest_vm, cpio, |
| 390 | ppool)) { |
| 391 | dlog("Unable to load VM.\n"); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 392 | continue; |
| 393 | } |
| 394 | |
| 395 | /* Deny the primary VM access to this memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 396 | if (!vm_unmap(primary_vm_locked, secondary_mem_begin, |
| 397 | secondary_mem_end, ppool)) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 398 | dlog("Unable to unmap secondary VM from primary VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 399 | success = false; |
| 400 | break; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 401 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 404 | vm_unlock(&primary_vm_locked); |
| 405 | |
| 406 | if (!success) { |
| 407 | return false; |
| 408 | } |
| 409 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 410 | /* |
| 411 | * Add newly reserved areas to update params by looking at the |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 412 | * difference between the available ranges from the original params and |
| 413 | * the updated mem_ranges_available. We assume that the number and order |
| 414 | * of available ranges is the same, i.e. we don't remove any ranges |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 415 | * above only make them smaller. |
| 416 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 417 | return update_reserved_ranges(update, params->mem_ranges, |
| 418 | mem_ranges_available, |
| 419 | params->mem_ranges_count); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 420 | } |