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