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