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 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 9 | #include "hf/load.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 10 | |
| 11 | #include <stdbool.h> |
| 12 | |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 13 | #include "hf/arch/vm.h" |
| 14 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 15 | #include "hf/api.h" |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 16 | #include "hf/boot_params.h" |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 17 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 18 | #include "hf/dlog.h" |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 19 | #include "hf/fdt_patch.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 20 | #include "hf/layout.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/memiter.h" |
| 22 | #include "hf/mm.h" |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 23 | #include "hf/plat/console.h" |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 24 | #include "hf/plat/iommu.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 25 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 26 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 27 | #include "hf/vm.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 28 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 29 | #include "vmapi/hf/call.h" |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 30 | #include "vmapi/hf/ffa.h" |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 31 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 32 | alignas(PAGE_SIZE) static uint8_t tee_send_buffer[HF_MAILBOX_SIZE]; |
| 33 | alignas(PAGE_SIZE) static uint8_t tee_recv_buffer[HF_MAILBOX_SIZE]; |
| 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 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 64 | /** |
| 65 | * Loads the secondary VM's kernel. |
| 66 | * Stores the kernel size in kernel_size (if kernel_size is not NULL). |
| 67 | * Returns false if it cannot load the kernel. |
| 68 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 69 | static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin, |
| 70 | paddr_t end, const struct manifest_vm *manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 71 | const struct memiter *cpio, struct mpool *ppool, |
| 72 | size_t *kernel_size) |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 73 | { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 74 | struct memiter kernel; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 75 | size_t size; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 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 Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 78 | dlog_error("Could not find kernel file \"%s\".\n", |
| 79 | string_data(&manifest_vm->kernel_filename)); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 83 | size = memiter_size(&kernel); |
| 84 | if (pa_difference(begin, end) < size) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 85 | dlog_error("Kernel is larger than available memory.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 90 | dlog_error("Unable to copy kernel.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 94 | if (kernel_size) { |
| 95 | *kernel_size = size; |
| 96 | } |
| 97 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Link RX/TX buffers provided in partition manifest to mailbox |
| 103 | */ |
| 104 | static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked, |
| 105 | struct vm_locked vm_locked, struct rx_tx rxtx, |
| 106 | struct mpool *ppool) |
| 107 | { |
| 108 | struct ffa_value ret; |
| 109 | ipaddr_t send; |
| 110 | ipaddr_t recv; |
| 111 | uint32_t page_count; |
| 112 | |
| 113 | send = ipa_init(rxtx.tx_buffer->base_address); |
| 114 | recv = ipa_init(rxtx.rx_buffer->base_address); |
| 115 | page_count = rxtx.tx_buffer->page_count; |
| 116 | |
| 117 | ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv, |
| 118 | page_count, ppool); |
| 119 | if (ret.func != FFA_SUCCESS_32) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | dlog_verbose(" mailbox: send = %#x, recv = %#x\n", |
| 124 | vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv); |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 129 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 130 | * Performs VM loading activities that are common between the primary and |
| 131 | * secondaries. |
| 132 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 133 | static bool load_common(struct mm_stage1_locked stage1_locked, |
| 134 | struct vm_locked vm_locked, |
| 135 | const struct manifest_vm *manifest_vm, |
| 136 | struct mpool *ppool) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 137 | { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 138 | vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist; |
| 139 | vm_locked.vm->uuid = manifest_vm->sp.uuid; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 140 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 141 | if (manifest_vm->is_ffa_partition) { |
| 142 | /* Link rxtx buffers to mailbox */ |
| 143 | if (manifest_vm->sp.rxtx.available) { |
| 144 | if (!link_rxtx_to_mailbox(stage1_locked, vm_locked, |
| 145 | manifest_vm->sp.rxtx, |
| 146 | ppool)) { |
| 147 | dlog_error( |
| 148 | "Unable to Link RX/TX buffer with " |
| 149 | "mailbox.\n"); |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | } |
Fuad Tabba | 5697071 | 2020-01-10 11:20:09 +0000 | [diff] [blame] | 154 | /* Initialize architecture-specific features. */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 155 | arch_vm_features_set(vm_locked.vm); |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 156 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 161 | * Loads the primary VM. |
| 162 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 163 | static bool load_primary(struct mm_stage1_locked stage1_locked, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 164 | const struct manifest_vm *manifest_vm, |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 165 | const struct memiter *cpio, |
| 166 | const struct boot_params *params, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 167 | { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 168 | paddr_t primary_begin; |
| 169 | ipaddr_t primary_entry; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 170 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 171 | struct vm_locked vm_locked; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 172 | struct vcpu_locked vcpu_locked; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 173 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 174 | bool ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 175 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 176 | if (manifest_vm->is_ffa_partition) { |
| 177 | primary_begin = pa_init(manifest_vm->sp.load_addr); |
| 178 | primary_entry = ipa_add(ipa_from_pa(primary_begin), |
| 179 | manifest_vm->sp.ep_offset); |
| 180 | } else { |
| 181 | primary_begin = |
| 182 | (manifest_vm->primary.boot_address == |
| 183 | MANIFEST_INVALID_ADDRESS) |
| 184 | ? layout_primary_begin() |
| 185 | : pa_init(manifest_vm->primary.boot_address); |
| 186 | primary_entry = ipa_from_pa(primary_begin); |
| 187 | } |
| 188 | |
David Brazdil | 080ee31 | 2020-02-25 15:30:30 -0800 | [diff] [blame] | 189 | paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 190 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Load the kernel if a filename is specified in the VM manifest. |
| 193 | * For an FF-A partition, kernel_filename is undefined indicating |
| 194 | * the partition package has already been loaded prior to Hafnium |
| 195 | * booting. |
| 196 | */ |
| 197 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 198 | if (!load_kernel(stage1_locked, primary_begin, primary_end, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 199 | manifest_vm, cpio, ppool, NULL)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 200 | dlog_error("Unable to load primary kernel.\n"); |
| 201 | return false; |
| 202 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 205 | if (!vm_init_next(MAX_CPUS, ppool, &vm)) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 206 | dlog_error("Unable to initialise primary VM.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 207 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 208 | } |
| 209 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 210 | if (vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 211 | dlog_error("Primary VM was not given correct ID.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 215 | vm_locked = vm_lock(vm); |
| 216 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 217 | if (params->device_mem_ranges_count == 0) { |
| 218 | /* |
| 219 | * Map 1TB of address space as device memory to, most likely, |
| 220 | * make all devices available to the primary VM. |
| 221 | * |
| 222 | * TODO: remove this once all targets provide valid ranges. |
| 223 | */ |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 224 | dlog_warning( |
| 225 | "Device memory not provided, defaulting to 1 TB.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 226 | |
| 227 | if (!vm_identity_map( |
| 228 | vm_locked, pa_init(0), |
| 229 | pa_init(UINT64_C(1024) * 1024 * 1024 * 1024), |
| 230 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 231 | dlog_error( |
| 232 | "Unable to initialise address space for " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 233 | "primary VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 234 | ret = false; |
| 235 | goto out; |
| 236 | } |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 239 | /* Map normal memory as such to permit caching, execution, etc. */ |
| 240 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 241 | if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin, |
| 242 | params->mem_ranges[i].end, |
| 243 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 244 | NULL)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 245 | dlog_error( |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 246 | "Unable to initialise memory for primary " |
| 247 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 248 | ret = false; |
| 249 | goto out; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 253 | /* Map device memory as such to prevent execution, speculation etc. */ |
| 254 | for (i = 0; i < params->device_mem_ranges_count; ++i) { |
| 255 | if (!vm_identity_map( |
| 256 | vm_locked, params->device_mem_ranges[i].begin, |
| 257 | params->device_mem_ranges[i].end, |
| 258 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 259 | dlog("Unable to initialise device memory for primary " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 260 | "VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 261 | ret = false; |
| 262 | goto out; |
| 263 | } |
| 264 | } |
| 265 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 266 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 267 | ret = false; |
| 268 | goto out; |
| 269 | } |
| 270 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 271 | if (!vm_unmap_hypervisor(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 272 | dlog_error("Unable to unmap hypervisor from primary VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 273 | ret = false; |
| 274 | goto out; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 277 | if (!plat_iommu_unmap_iommus(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 278 | dlog_error("Unable to unmap IOMMUs from primary VM.\n"); |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 279 | ret = false; |
| 280 | goto out; |
| 281 | } |
| 282 | |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 283 | dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n", |
| 284 | vm->vcpu_count, pa_addr(primary_begin)); |
| 285 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 286 | vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0)); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 287 | vcpu_on(vcpu_locked, primary_entry, params->kernel_arg); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 288 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 289 | ret = true; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 290 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 291 | out: |
| 292 | vm_unlock(&vm_locked); |
| 293 | |
| 294 | return ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 297 | /** |
| 298 | * Loads the secondary VM's FDT. |
| 299 | * Stores the total allocated size for the FDT in fdt_allocated_size (if |
| 300 | * fdt_allocated_size is not NULL). The allocated size includes additional space |
| 301 | * for potential patching. |
| 302 | */ |
| 303 | static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked, |
| 304 | paddr_t end, size_t fdt_max_size, |
| 305 | const struct manifest_vm *manifest_vm, |
| 306 | const struct memiter *cpio, struct mpool *ppool, |
| 307 | paddr_t *fdt_addr, size_t *fdt_allocated_size) |
| 308 | { |
| 309 | struct memiter fdt; |
| 310 | size_t allocated_size; |
| 311 | |
| 312 | CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename)); |
| 313 | |
| 314 | if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) { |
| 315 | dlog_error("Cannot open the secondary VM's FDT.\n"); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Ensure the FDT has one additional page at the end for patching, and |
| 321 | * and align it to the page boundary. |
| 322 | */ |
| 323 | allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE; |
| 324 | |
| 325 | if (allocated_size > fdt_max_size) { |
| 326 | dlog_error( |
| 327 | "FDT allocated space (%u) is more than the specified " |
| 328 | "maximum to use (%u).\n", |
| 329 | allocated_size, fdt_max_size); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | /* Load the FDT to the end of the VM's allocated memory space. */ |
| 334 | *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size))); |
| 335 | |
| 336 | dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n", |
| 337 | allocated_size, pa_addr(*fdt_addr)); |
| 338 | |
| 339 | if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) { |
| 340 | dlog_error("Unable to copy FDT.\n"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (fdt_allocated_size) { |
| 345 | *fdt_allocated_size = allocated_size; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 351 | /* |
| 352 | * Loads a secondary VM. |
| 353 | */ |
| 354 | static bool load_secondary(struct mm_stage1_locked stage1_locked, |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 355 | struct vm_locked primary_vm_locked, |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 356 | paddr_t mem_begin, paddr_t mem_end, |
| 357 | const struct manifest_vm *manifest_vm, |
| 358 | const struct memiter *cpio, struct mpool *ppool) |
| 359 | { |
| 360 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 361 | struct vm_locked vm_locked; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 362 | struct vcpu *vcpu; |
| 363 | ipaddr_t secondary_entry; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 364 | bool ret; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 365 | paddr_t fdt_addr; |
| 366 | bool has_fdt; |
| 367 | size_t kernel_size = 0; |
| 368 | const size_t mem_size = pa_difference(mem_begin, mem_end); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 369 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Load the kernel if a filename is specified in the VM manifest. |
| 372 | * For an FF-A partition, kernel_filename is undefined indicating |
| 373 | * the partition package has already been loaded prior to Hafnium |
| 374 | * booting. |
| 375 | */ |
| 376 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 377 | if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 378 | cpio, ppool, &kernel_size)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 379 | dlog_error("Unable to load kernel.\n"); |
| 380 | return false; |
| 381 | } |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 384 | has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename); |
| 385 | if (has_fdt) { |
| 386 | /* |
| 387 | * Ensure that the FDT does not overwrite the kernel or overlap |
| 388 | * its page, for the FDT to start at a page boundary. |
| 389 | */ |
| 390 | const size_t fdt_max_size = |
| 391 | mem_size - align_up(kernel_size, PAGE_SIZE); |
| 392 | |
| 393 | size_t fdt_allocated_size; |
| 394 | |
| 395 | if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size, |
| 396 | manifest_vm, cpio, ppool, &fdt_addr, |
| 397 | &fdt_allocated_size)) { |
| 398 | dlog_error("Unable to load FDT.\n"); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size, |
| 403 | mem_begin, mem_end, ppool)) { |
| 404 | dlog_error("Unable to patch FDT.\n"); |
| 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 409 | if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 410 | dlog_error("Unable to initialise VM.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 414 | vm_locked = vm_lock(vm); |
| 415 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 416 | /* Grant the VM access to the memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 417 | if (!vm_identity_map(vm_locked, mem_begin, mem_end, |
| 418 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 419 | &secondary_entry)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 420 | dlog_error("Unable to initialise memory.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 421 | ret = false; |
| 422 | goto out; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 425 | if (manifest_vm->is_ffa_partition) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 426 | int j = 0; |
| 427 | paddr_t region_begin; |
| 428 | paddr_t region_end; |
| 429 | paddr_t alloc_base = mem_end; |
| 430 | size_t size; |
| 431 | size_t total_alloc = 0; |
| 432 | |
| 433 | /* Map memory-regions */ |
| 434 | while (j < manifest_vm->sp.mem_region_count) { |
| 435 | size = manifest_vm->sp.mem_regions[j].page_count * |
| 436 | PAGE_SIZE; |
| 437 | /* |
| 438 | * For memory-regions without base-address, memory |
| 439 | * should be allocated inside partition's page table. |
| 440 | * Start allocating memory regions in partition's |
| 441 | * page table, starting from the end. |
| 442 | * TODO: Add mechanism to let partition know of these |
| 443 | * memory regions |
| 444 | */ |
| 445 | if (manifest_vm->sp.mem_regions[j].base_address == |
| 446 | MANIFEST_INVALID_ADDRESS) { |
| 447 | total_alloc += size; |
| 448 | /* Don't go beyond half the VM's memory space */ |
| 449 | if (total_alloc > |
| 450 | (manifest_vm->secondary.mem_size / 2)) { |
| 451 | dlog_error( |
| 452 | "Not enough space for memory-" |
| 453 | "region allocation"); |
| 454 | ret = false; |
| 455 | goto out; |
| 456 | } |
| 457 | |
| 458 | region_end = alloc_base; |
| 459 | region_begin = pa_subtract(alloc_base, size); |
| 460 | alloc_base = region_begin; |
| 461 | |
| 462 | if (!vm_identity_map( |
| 463 | vm_locked, region_begin, region_end, |
| 464 | manifest_vm->sp.mem_regions[j] |
| 465 | .attributes, |
| 466 | ppool, NULL)) { |
| 467 | dlog_error( |
| 468 | "Unable to map secondary VM " |
| 469 | "memory-region.\n"); |
| 470 | ret = false; |
| 471 | goto out; |
| 472 | } |
| 473 | |
| 474 | dlog_info( |
| 475 | " Memory region %#x - %#x allocated\n", |
| 476 | region_begin, region_end); |
| 477 | } else { |
| 478 | /* |
| 479 | * Identity map memory region for both case, |
| 480 | * VA(S-EL0) or IPA(S-EL1). |
| 481 | */ |
| 482 | region_begin = |
| 483 | pa_init(manifest_vm->sp.mem_regions[j] |
| 484 | .base_address); |
| 485 | region_end = pa_add(region_begin, size); |
| 486 | |
| 487 | if (!vm_identity_map( |
| 488 | vm_locked, region_begin, region_end, |
| 489 | manifest_vm->sp.mem_regions[j] |
| 490 | .attributes, |
| 491 | ppool, NULL)) { |
| 492 | dlog_error( |
| 493 | "Unable to map secondary VM " |
| 494 | "memory-region.\n"); |
| 495 | ret = false; |
| 496 | goto out; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* Deny the primary VM access to this memory */ |
| 501 | if (!vm_unmap(primary_vm_locked, region_begin, |
| 502 | region_end, ppool)) { |
| 503 | dlog_error( |
| 504 | "Unable to unmap secondary VM memory-" |
| 505 | "region from primary VM.\n"); |
| 506 | ret = false; |
| 507 | goto out; |
| 508 | } |
| 509 | |
| 510 | j++; |
| 511 | } |
| 512 | |
| 513 | /* Map device-regions */ |
| 514 | j = 0; |
| 515 | while (j < manifest_vm->sp.dev_region_count) { |
| 516 | region_begin = pa_init( |
| 517 | manifest_vm->sp.dev_regions[j].base_address); |
| 518 | size = manifest_vm->sp.dev_regions[j].page_count * |
| 519 | PAGE_SIZE; |
| 520 | region_end = pa_add(region_begin, size); |
| 521 | |
| 522 | if (!vm_identity_map( |
| 523 | vm_locked, region_begin, region_end, |
| 524 | manifest_vm->sp.dev_regions[j].attributes, |
| 525 | ppool, NULL)) { |
| 526 | dlog_error( |
| 527 | "Unable to map secondary VM " |
| 528 | "device-region.\n"); |
| 529 | ret = false; |
| 530 | goto out; |
| 531 | } |
| 532 | /* Deny primary VM access to this region */ |
| 533 | if (!vm_unmap(primary_vm_locked, region_begin, |
| 534 | region_end, ppool)) { |
| 535 | dlog_error( |
| 536 | "Unable to unmap secondary VM device-" |
| 537 | "region from primary VM.\n"); |
| 538 | ret = false; |
| 539 | goto out; |
| 540 | } |
| 541 | j++; |
| 542 | } |
| 543 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 544 | secondary_entry = |
| 545 | ipa_add(secondary_entry, manifest_vm->sp.ep_offset); |
| 546 | } |
| 547 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 548 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 549 | ret = false; |
| 550 | goto out; |
| 551 | } |
| 552 | |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 553 | dlog_info("Loaded with %u vCPUs, entry at %#x.\n", |
| 554 | manifest_vm->secondary.vcpu_count, pa_addr(mem_begin)); |
| 555 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 556 | vcpu = vm_get_vcpu(vm, 0); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 557 | |
| 558 | if (has_fdt) { |
| 559 | vcpu_secondary_reset_and_start(vcpu, secondary_entry, |
| 560 | pa_addr(fdt_addr)); |
| 561 | } else { |
| 562 | /* |
| 563 | * Without an FDT, secondary VMs expect the memory size to be |
| 564 | * passed in register x0, which is what |
| 565 | * vcpu_secondary_reset_and_start does in this case. |
| 566 | */ |
| 567 | vcpu_secondary_reset_and_start(vcpu, secondary_entry, mem_size); |
| 568 | } |
| 569 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 570 | ret = true; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 571 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 572 | out: |
| 573 | vm_unlock(&vm_locked); |
| 574 | |
| 575 | return ret; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 578 | /** |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 579 | * Try to find a memory range of the given size within the given ranges, and |
| 580 | * remove it from them. Return true on success, or false if no large enough |
| 581 | * contiguous range is found. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 582 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 583 | static bool carve_out_mem_range(struct mem_range *mem_ranges, |
| 584 | size_t mem_ranges_count, uint64_t size_to_find, |
| 585 | paddr_t *found_begin, paddr_t *found_end) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 586 | { |
| 587 | size_t i; |
| 588 | |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 589 | /* |
| 590 | * TODO(b/116191358): Consider being cleverer about how we pack VMs |
| 591 | * together, with a non-greedy algorithm. |
| 592 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 593 | for (i = 0; i < mem_ranges_count; ++i) { |
| 594 | if (size_to_find <= |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 595 | pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) { |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 596 | /* |
| 597 | * This range is big enough, take some of it from the |
| 598 | * end and reduce its size accordingly. |
| 599 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 600 | *found_end = mem_ranges[i].end; |
| 601 | *found_begin = pa_init(pa_addr(mem_ranges[i].end) - |
| 602 | size_to_find); |
| 603 | mem_ranges[i].end = *found_begin; |
| 604 | return true; |
| 605 | } |
| 606 | } |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Given arrays of memory ranges before and after memory was removed for |
| 612 | * secondary VMs, add the difference to the reserved ranges of the given update. |
| 613 | * Return true on success, or false if there would be more than MAX_MEM_RANGES |
| 614 | * reserved ranges after adding the new ones. |
| 615 | * `before` and `after` must be arrays of exactly `mem_ranges_count` elements. |
| 616 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 617 | static bool update_reserved_ranges(struct boot_params_update *update, |
| 618 | const struct mem_range *before, |
| 619 | const struct mem_range *after, |
| 620 | size_t mem_ranges_count) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 621 | { |
| 622 | size_t i; |
| 623 | |
| 624 | for (i = 0; i < mem_ranges_count; ++i) { |
| 625 | if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) { |
| 626 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 627 | dlog_error( |
| 628 | "Too many reserved ranges after " |
| 629 | "loading secondary VMs.\n"); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | update->reserved_ranges[update->reserved_ranges_count] |
| 633 | .begin = before[i].begin; |
| 634 | update->reserved_ranges[update->reserved_ranges_count] |
| 635 | .end = after[i].begin; |
| 636 | update->reserved_ranges_count++; |
| 637 | } |
| 638 | if (pa_addr(after[i].end) < pa_addr(before[i].end)) { |
| 639 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 640 | dlog_error( |
| 641 | "Too many reserved ranges after " |
| 642 | "loading secondary VMs.\n"); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 643 | return false; |
| 644 | } |
| 645 | update->reserved_ranges[update->reserved_ranges_count] |
| 646 | .begin = after[i].end; |
| 647 | update->reserved_ranges[update->reserved_ranges_count] |
| 648 | .end = before[i].end; |
| 649 | update->reserved_ranges_count++; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 656 | /* |
| 657 | * Loads alls VMs from the manifest. |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 658 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 659 | bool load_vms(struct mm_stage1_locked stage1_locked, |
| 660 | const struct manifest *manifest, const struct memiter *cpio, |
| 661 | const struct boot_params *params, |
| 662 | struct boot_params_update *update, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 663 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 664 | struct vm *primary; |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 665 | struct vm *tee; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 666 | struct mem_range mem_ranges_available[MAX_MEM_RANGES]; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 667 | struct vm_locked primary_vm_locked; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 668 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 669 | bool success = true; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 670 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 671 | if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX], |
| 672 | cpio, params, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 673 | dlog_error("Unable to load primary VM.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 677 | /* |
| 678 | * Initialise the dummy VM which represents TrustZone, and set up its |
| 679 | * RX/TX buffers. |
| 680 | */ |
| 681 | tee = vm_init(HF_TEE_VM_ID, 0, ppool); |
| 682 | CHECK(tee != NULL); |
| 683 | tee->mailbox.send = &tee_send_buffer; |
| 684 | tee->mailbox.recv = &tee_recv_buffer; |
| 685 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 686 | static_assert( |
| 687 | sizeof(mem_ranges_available) == sizeof(params->mem_ranges), |
| 688 | "mem_range arrays must be the same size for memcpy."); |
| 689 | static_assert(sizeof(mem_ranges_available) < 500, |
| 690 | "This will use too much stack, either make " |
| 691 | "MAX_MEM_RANGES smaller or change this."); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 692 | memcpy_s(mem_ranges_available, sizeof(mem_ranges_available), |
| 693 | params->mem_ranges, sizeof(params->mem_ranges)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 694 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 695 | /* Round the last addresses down to the page size. */ |
| 696 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 697 | mem_ranges_available[i].end = pa_init(align_down( |
| 698 | pa_addr(mem_ranges_available[i].end), PAGE_SIZE)); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 699 | } |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 700 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 701 | primary = vm_find(HF_PRIMARY_VM_ID); |
| 702 | primary_vm_locked = vm_lock(primary); |
| 703 | |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 704 | for (i = 0; i < manifest->vm_count; ++i) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 705 | const struct manifest_vm *manifest_vm = &manifest->vm[i]; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 706 | ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 707 | uint64_t mem_size; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 708 | paddr_t secondary_mem_begin; |
| 709 | paddr_t secondary_mem_end; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 710 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 711 | if (vm_id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 712 | continue; |
| 713 | } |
| 714 | |
Olivier Deprez | 2a8ee34 | 2020-08-03 15:10:44 +0200 | [diff] [blame] | 715 | dlog_info("Loading VM id %#x: %s.\n", vm_id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 716 | manifest_vm->debug_name); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 717 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 718 | mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 719 | |
| 720 | if (manifest_vm->is_ffa_partition) { |
| 721 | secondary_mem_begin = |
| 722 | pa_init(manifest_vm->sp.load_addr); |
| 723 | secondary_mem_end = |
| 724 | pa_init(manifest_vm->sp.load_addr + mem_size); |
| 725 | } else if (!carve_out_mem_range(mem_ranges_available, |
| 726 | params->mem_ranges_count, |
| 727 | mem_size, &secondary_mem_begin, |
| 728 | &secondary_mem_end)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 729 | dlog_error("Not enough memory (%u bytes).\n", mem_size); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 730 | continue; |
| 731 | } |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 732 | |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 733 | if (!load_secondary(stage1_locked, primary_vm_locked, |
| 734 | secondary_mem_begin, secondary_mem_end, |
| 735 | manifest_vm, cpio, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 736 | dlog_error("Unable to load VM.\n"); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 737 | continue; |
| 738 | } |
| 739 | |
| 740 | /* Deny the primary VM access to this memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 741 | if (!vm_unmap(primary_vm_locked, secondary_mem_begin, |
| 742 | secondary_mem_end, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 743 | dlog_error( |
| 744 | "Unable to unmap secondary VM from primary " |
| 745 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 746 | success = false; |
| 747 | break; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 748 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 751 | vm_unlock(&primary_vm_locked); |
| 752 | |
| 753 | if (!success) { |
| 754 | return false; |
| 755 | } |
| 756 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 757 | /* |
| 758 | * Add newly reserved areas to update params by looking at the |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 759 | * difference between the available ranges from the original params and |
| 760 | * the updated mem_ranges_available. We assume that the number and order |
| 761 | * 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] | 762 | * above only make them smaller. |
| 763 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 764 | return update_reserved_ranges(update, params->mem_ranges, |
| 765 | mem_ranges_available, |
| 766 | params->mem_ranges_count); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 767 | } |