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