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