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