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 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 13 | #include "hf/arch/init.h" |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 14 | #include "hf/arch/other_world.h" |
J-Alves | a9c7cba | 2021-08-25 16:26:11 +0100 | [diff] [blame] | 15 | #include "hf/arch/plat/ffa.h" |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 16 | #include "hf/arch/vm.h" |
| 17 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 18 | #include "hf/api.h" |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 19 | #include "hf/boot_params.h" |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 20 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/dlog.h" |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 22 | #include "hf/fdt_patch.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 23 | #include "hf/layout.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 24 | #include "hf/memiter.h" |
| 25 | #include "hf/mm.h" |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 26 | #include "hf/plat/console.h" |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 27 | #include "hf/plat/interrupts.h" |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 28 | #include "hf/plat/iommu.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 29 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 30 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 31 | #include "hf/vm.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 32 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 33 | #include "vmapi/hf/call.h" |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 34 | #include "vmapi/hf/ffa.h" |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 35 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 36 | /** |
| 37 | * Copies data to an unmapped location by mapping it for write, copying the |
| 38 | * data, then unmapping it. |
Andrew Scull | d9225b3 | 2018-11-19 16:12:41 +0000 | [diff] [blame] | 39 | * |
| 40 | * The data is written so that it is available to all cores with the cache |
| 41 | * disabled. When switching to the partitions, the caching is initially disabled |
| 42 | * so the data must be available without the cache. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 43 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 44 | 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] | 45 | struct memiter *from_it, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 46 | { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 47 | const void *from = memiter_base(from_it); |
| 48 | size_t size = memiter_size(from_it); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 49 | paddr_t to_end = pa_add(to, size); |
| 50 | void *ptr; |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 51 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 52 | 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] | 53 | if (!ptr) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 57 | memcpy_s(ptr, size, from, size); |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 58 | arch_mm_flush_dcache(ptr, size); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 59 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 60 | CHECK(mm_unmap(stage1_locked, to, to_end, ppool)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 65 | /** |
| 66 | * Loads the secondary VM's kernel. |
| 67 | * Stores the kernel size in kernel_size (if kernel_size is not NULL). |
| 68 | * Returns false if it cannot load the kernel. |
| 69 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 70 | static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin, |
| 71 | paddr_t end, const struct manifest_vm *manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 72 | const struct memiter *cpio, struct mpool *ppool, |
| 73 | size_t *kernel_size) |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 74 | { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 75 | struct memiter kernel; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 76 | size_t size; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 77 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 78 | if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 79 | dlog_error("Could not find kernel file \"%s\".\n", |
| 80 | string_data(&manifest_vm->kernel_filename)); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 84 | size = memiter_size(&kernel); |
| 85 | if (pa_difference(begin, end) < size) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 86 | dlog_error("Kernel is larger than available memory.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 91 | dlog_error("Unable to copy kernel.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 95 | if (kernel_size) { |
| 96 | *kernel_size = size; |
| 97 | } |
| 98 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Link RX/TX buffers provided in partition manifest to mailbox |
| 104 | */ |
| 105 | static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked, |
| 106 | struct vm_locked vm_locked, struct rx_tx rxtx, |
| 107 | struct mpool *ppool) |
| 108 | { |
| 109 | struct ffa_value ret; |
| 110 | ipaddr_t send; |
| 111 | ipaddr_t recv; |
| 112 | uint32_t page_count; |
| 113 | |
| 114 | send = ipa_init(rxtx.tx_buffer->base_address); |
| 115 | recv = ipa_init(rxtx.rx_buffer->base_address); |
| 116 | page_count = rxtx.tx_buffer->page_count; |
| 117 | |
| 118 | ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv, |
| 119 | page_count, ppool); |
| 120 | if (ret.func != FFA_SUCCESS_32) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | dlog_verbose(" mailbox: send = %#x, recv = %#x\n", |
| 125 | vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv); |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Raghu Krishnamurthy | ad38a9c | 2022-07-20 07:30:36 -0700 | [diff] [blame] | 130 | static void infer_interrupt(struct interrupt_info interrupt, |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 131 | struct interrupt_descriptor *int_desc) |
| 132 | { |
| 133 | uint32_t attr = interrupt.attributes; |
| 134 | |
| 135 | interrupt_desc_set_id(int_desc, interrupt.id); |
| 136 | interrupt_desc_set_priority(int_desc, |
| 137 | (attr >> INT_DESC_PRIORITY_SHIFT) & 0xff); |
| 138 | |
| 139 | /* Refer to the comments in interrupt_descriptor struct definition. */ |
| 140 | interrupt_desc_set_type_config_sec_state( |
| 141 | int_desc, |
| 142 | (((attr >> INT_DESC_TYPE_SHIFT) & 0x3) << 2) | |
| 143 | (((attr >> INT_DESC_CONFIG_SHIFT) & 0x1) << 1) | |
| 144 | ((attr >> INT_DESC_SEC_STATE_SHIFT) & 0x1)); |
| 145 | |
| 146 | interrupt_desc_set_valid(int_desc, true); |
| 147 | } |
| 148 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 149 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 150 | * Performs VM loading activities that are common between the primary and |
| 151 | * secondaries. |
| 152 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 153 | static bool load_common(struct mm_stage1_locked stage1_locked, |
| 154 | struct vm_locked vm_locked, |
| 155 | const struct manifest_vm *manifest_vm, |
| 156 | struct mpool *ppool) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 157 | { |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 158 | struct device_region dev_region; |
Raghu Krishnamurthy | ad38a9c | 2022-07-20 07:30:36 -0700 | [diff] [blame] | 159 | struct interrupt_info interrupt; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 160 | uint32_t k = 0; |
| 161 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 162 | vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist; |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 163 | vm_locked.vm->uuid = manifest_vm->partition.uuid; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 164 | |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 165 | /* Populate the interrupt descriptor for current VM. */ |
Raghu Krishnamurthy | 641dcd8 | 2022-07-19 23:21:20 -0700 | [diff] [blame] | 166 | for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) { |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 167 | dev_region = manifest_vm->partition.dev_regions[i]; |
| 168 | |
| 169 | CHECK(dev_region.interrupt_count <= |
Raghu Krishnamurthy | 641dcd8 | 2022-07-19 23:21:20 -0700 | [diff] [blame] | 170 | PARTITION_MAX_INTERRUPTS_PER_DEVICE); |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 171 | |
| 172 | for (uint8_t j = 0; j < dev_region.interrupt_count; j++) { |
| 173 | struct interrupt_descriptor int_desc; |
| 174 | |
| 175 | interrupt = dev_region.interrupts[j]; |
| 176 | infer_interrupt(interrupt, &int_desc); |
| 177 | vm_locked.vm->interrupt_desc[k] = int_desc; |
| 178 | |
Madhukar Pappireddy | 72454a1 | 2021-08-03 12:21:46 -0500 | [diff] [blame] | 179 | /* |
| 180 | * Configure the physical interrupts allocated for this |
| 181 | * VM in its partition manifest. |
| 182 | */ |
| 183 | plat_interrupts_configure_interrupt(int_desc); |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 184 | k++; |
| 185 | CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS); |
| 186 | } |
| 187 | } |
| 188 | dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k); |
| 189 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 190 | if (manifest_vm->is_ffa_partition) { |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 191 | vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 192 | /* Link rxtx buffers to mailbox */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 193 | if (manifest_vm->partition.rxtx.available) { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 194 | if (!link_rxtx_to_mailbox(stage1_locked, vm_locked, |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 195 | manifest_vm->partition.rxtx, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 196 | ppool)) { |
| 197 | dlog_error( |
| 198 | "Unable to Link RX/TX buffer with " |
| 199 | "mailbox.\n"); |
| 200 | return false; |
| 201 | } |
| 202 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 203 | |
Maksims Svecovs | b596eab | 2021-04-27 00:52:27 +0100 | [diff] [blame] | 204 | vm_locked.vm->messaging_method = |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 205 | manifest_vm->partition.messaging_method; |
Manish Pandey | f3be639 | 2020-09-24 17:26:09 +0100 | [diff] [blame] | 206 | |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 207 | vm_locked.vm->managed_exit = |
| 208 | manifest_vm->partition.managed_exit; |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 209 | |
J-Alves | a4730db | 2021-11-02 10:31:01 +0000 | [diff] [blame] | 210 | vm_locked.vm->notifications.enabled = |
| 211 | manifest_vm->partition.notification_support; |
| 212 | |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 213 | vm_locked.vm->boot_order = manifest_vm->partition.boot_order; |
| 214 | |
J-Alves | 7d38f7b | 2022-04-13 13:22:30 +0100 | [diff] [blame] | 215 | vm_locked.vm->boot_info.gp_register_num = |
| 216 | manifest_vm->partition.gp_register_num; |
| 217 | |
| 218 | if (manifest_vm->partition.boot_info) { |
| 219 | /* |
| 220 | * If the partition expects the boot information blob |
| 221 | * per the ff-a v1.1 boot protocol, then its address |
| 222 | * shall match the partition's load address. |
| 223 | */ |
| 224 | vm_locked.vm->boot_info.blob_addr = |
| 225 | ipa_init(manifest_vm->partition.load_addr); |
| 226 | } |
| 227 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 228 | /* Updating boot list according to boot_order */ |
| 229 | vm_update_boot(vm_locked.vm); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 230 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 231 | if (vm_locked_are_notifications_enabled(vm_locked) && |
J-Alves | a4730db | 2021-11-02 10:31:01 +0000 | [diff] [blame] | 232 | !plat_ffa_notifications_bitmap_create_call( |
| 233 | vm_locked.vm->id, vm_locked.vm->vcpu_count)) { |
| 234 | return false; |
J-Alves | a9c7cba | 2021-08-25 16:26:11 +0100 | [diff] [blame] | 235 | } |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 236 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 237 | |
Fuad Tabba | 5697071 | 2020-01-10 11:20:09 +0000 | [diff] [blame] | 238 | /* Initialize architecture-specific features. */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 239 | arch_vm_features_set(vm_locked.vm); |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 240 | |
Madhukar Pappireddy | 54680c7 | 2020-10-23 15:02:38 -0500 | [diff] [blame] | 241 | if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm, |
| 242 | ppool)) { |
| 243 | dlog_error("Unable to attach upstream peripheral device\n"); |
| 244 | return false; |
| 245 | } |
| 246 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 251 | * Loads the primary VM. |
| 252 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 253 | static bool load_primary(struct mm_stage1_locked stage1_locked, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 254 | const struct manifest_vm *manifest_vm, |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 255 | const struct memiter *cpio, |
| 256 | const struct boot_params *params, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 257 | { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 258 | paddr_t primary_begin; |
| 259 | ipaddr_t primary_entry; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 260 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 261 | struct vm_locked vm_locked; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 262 | struct vcpu_locked vcpu_locked; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 263 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 264 | bool ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 265 | |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 266 | if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) { |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 267 | primary_begin = pa_init(manifest_vm->partition.load_addr); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 268 | primary_entry = ipa_add(ipa_from_pa(primary_begin), |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 269 | manifest_vm->partition.ep_offset); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 270 | } else { |
| 271 | primary_begin = |
| 272 | (manifest_vm->primary.boot_address == |
| 273 | MANIFEST_INVALID_ADDRESS) |
| 274 | ? layout_primary_begin() |
| 275 | : pa_init(manifest_vm->primary.boot_address); |
| 276 | primary_entry = ipa_from_pa(primary_begin); |
| 277 | } |
| 278 | |
David Brazdil | 080ee31 | 2020-02-25 15:30:30 -0800 | [diff] [blame] | 279 | paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 280 | |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 281 | /* Primary VM must be a VM */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 282 | CHECK(manifest_vm->partition.run_time_el == EL1); |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 283 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 284 | /* |
| 285 | * Load the kernel if a filename is specified in the VM manifest. |
| 286 | * For an FF-A partition, kernel_filename is undefined indicating |
| 287 | * the partition package has already been loaded prior to Hafnium |
| 288 | * booting. |
| 289 | */ |
| 290 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 291 | if (!load_kernel(stage1_locked, primary_begin, primary_end, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 292 | manifest_vm, cpio, ppool, NULL)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 293 | dlog_error("Unable to load primary kernel.\n"); |
| 294 | return false; |
| 295 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 298 | if (!vm_init_next(MAX_CPUS, ppool, &vm, false)) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 299 | dlog_error("Unable to initialise primary VM.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 300 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 301 | } |
| 302 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 303 | if (vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 304 | dlog_error("Primary VM was not given correct ID.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 308 | vm_locked = vm_lock(vm); |
| 309 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 310 | if (params->device_mem_ranges_count == 0) { |
| 311 | /* |
| 312 | * Map 1TB of address space as device memory to, most likely, |
| 313 | * make all devices available to the primary VM. |
| 314 | * |
| 315 | * TODO: remove this once all targets provide valid ranges. |
| 316 | */ |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 317 | dlog_warning( |
| 318 | "Device memory not provided, defaulting to 1 TB.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 319 | |
| 320 | if (!vm_identity_map( |
| 321 | vm_locked, pa_init(0), |
| 322 | pa_init(UINT64_C(1024) * 1024 * 1024 * 1024), |
| 323 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 324 | dlog_error( |
| 325 | "Unable to initialise address space for " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 326 | "primary VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 327 | ret = false; |
| 328 | goto out; |
| 329 | } |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 332 | /* Map normal memory as such to permit caching, execution, etc. */ |
| 333 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 334 | if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin, |
| 335 | params->mem_ranges[i].end, |
| 336 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 337 | NULL)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 338 | dlog_error( |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 339 | "Unable to initialise memory for primary " |
| 340 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 341 | ret = false; |
| 342 | goto out; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 346 | /* Map device memory as such to prevent execution, speculation etc. */ |
| 347 | for (i = 0; i < params->device_mem_ranges_count; ++i) { |
| 348 | if (!vm_identity_map( |
| 349 | vm_locked, params->device_mem_ranges[i].begin, |
| 350 | params->device_mem_ranges[i].end, |
| 351 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 352 | dlog("Unable to initialise device memory for primary " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 353 | "VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 354 | ret = false; |
| 355 | goto out; |
| 356 | } |
| 357 | } |
| 358 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 359 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 360 | ret = false; |
| 361 | goto out; |
| 362 | } |
| 363 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 364 | if (!vm_unmap_hypervisor(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 365 | dlog_error("Unable to unmap hypervisor from primary VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 366 | ret = false; |
| 367 | goto out; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 370 | if (!plat_iommu_unmap_iommus(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 371 | dlog_error("Unable to unmap IOMMUs from primary VM.\n"); |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 372 | ret = false; |
| 373 | goto out; |
| 374 | } |
| 375 | |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 376 | dlog_info("Loaded primary VM with %u vCPUs, entry at %#x.\n", |
| 377 | vm->vcpu_count, pa_addr(primary_begin)); |
| 378 | |
Olivier Deprez | b9adff4 | 2021-02-01 12:14:05 +0100 | [diff] [blame] | 379 | /* Mark the primary to be the first booted VM */ |
| 380 | vm_update_boot(vm); |
| 381 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 382 | vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0)); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 383 | vcpu_on(vcpu_locked, primary_entry, params->kernel_arg); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 384 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 385 | ret = true; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 386 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 387 | out: |
| 388 | vm_unlock(&vm_locked); |
| 389 | |
| 390 | return ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 393 | /** |
| 394 | * Loads the secondary VM's FDT. |
| 395 | * Stores the total allocated size for the FDT in fdt_allocated_size (if |
| 396 | * fdt_allocated_size is not NULL). The allocated size includes additional space |
| 397 | * for potential patching. |
| 398 | */ |
| 399 | static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked, |
| 400 | paddr_t end, size_t fdt_max_size, |
| 401 | const struct manifest_vm *manifest_vm, |
| 402 | const struct memiter *cpio, struct mpool *ppool, |
| 403 | paddr_t *fdt_addr, size_t *fdt_allocated_size) |
| 404 | { |
| 405 | struct memiter fdt; |
| 406 | size_t allocated_size; |
| 407 | |
| 408 | CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename)); |
| 409 | |
| 410 | if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) { |
| 411 | dlog_error("Cannot open the secondary VM's FDT.\n"); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | /* |
Olivier Deprez | 701e8bf | 2022-04-06 18:45:18 +0200 | [diff] [blame] | 416 | * Ensure the FDT has one additional page at the end for patching, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 417 | * and align it to the page boundary. |
| 418 | */ |
| 419 | allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE; |
| 420 | |
| 421 | if (allocated_size > fdt_max_size) { |
| 422 | dlog_error( |
| 423 | "FDT allocated space (%u) is more than the specified " |
| 424 | "maximum to use (%u).\n", |
| 425 | allocated_size, fdt_max_size); |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | /* Load the FDT to the end of the VM's allocated memory space. */ |
| 430 | *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size))); |
| 431 | |
| 432 | dlog_info("Loading secondary FDT of allocated size %u at 0x%x.\n", |
| 433 | allocated_size, pa_addr(*fdt_addr)); |
| 434 | |
| 435 | if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) { |
| 436 | dlog_error("Unable to copy FDT.\n"); |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | if (fdt_allocated_size) { |
| 441 | *fdt_allocated_size = allocated_size; |
| 442 | } |
| 443 | |
| 444 | return true; |
| 445 | } |
| 446 | |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 447 | /** |
| 448 | * Convert the manifest memory region attributes to mode consumed by mm layer. |
| 449 | */ |
| 450 | static uint32_t memory_region_attributes_to_mode(uint32_t attributes) |
| 451 | { |
| 452 | uint32_t mode = 0U; |
| 453 | |
| 454 | if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) { |
| 455 | mode |= MM_MODE_R; |
| 456 | } |
| 457 | |
| 458 | if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) { |
| 459 | mode |= MM_MODE_W; |
| 460 | } |
| 461 | |
| 462 | if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) { |
| 463 | mode |= MM_MODE_X; |
| 464 | } |
| 465 | |
| 466 | assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) || |
| 467 | (mode == (MM_MODE_R | MM_MODE_X))); |
| 468 | |
| 469 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) { |
| 470 | mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID); |
| 471 | } |
| 472 | |
| 473 | return mode; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Convert the manifest device region attributes to mode consumed by mm layer. |
| 478 | */ |
| 479 | static uint32_t device_region_attributes_to_mode(uint32_t attributes) |
| 480 | { |
| 481 | uint32_t mode = 0U; |
| 482 | |
| 483 | if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) { |
| 484 | mode |= MM_MODE_R; |
| 485 | } |
| 486 | |
| 487 | if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) { |
| 488 | mode |= MM_MODE_W; |
| 489 | } |
| 490 | |
| 491 | assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R)); |
| 492 | |
| 493 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) { |
| 494 | mode |= arch_mm_extra_attributes_from_vm(HF_HYPERVISOR_VM_ID); |
| 495 | } |
| 496 | |
| 497 | return mode | MM_MODE_D; |
| 498 | } |
| 499 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 500 | /* |
| 501 | * Loads a secondary VM. |
| 502 | */ |
| 503 | static bool load_secondary(struct mm_stage1_locked stage1_locked, |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 504 | struct vm_locked primary_vm_locked, |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 505 | paddr_t mem_begin, paddr_t mem_end, |
| 506 | const struct manifest_vm *manifest_vm, |
| 507 | const struct memiter *cpio, struct mpool *ppool) |
| 508 | { |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 509 | const char *error_string = " region security state ignored for "; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 510 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 511 | struct vm_locked vm_locked; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 512 | struct vcpu_locked vcpu_locked; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 513 | struct vcpu *vcpu; |
| 514 | ipaddr_t secondary_entry; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 515 | bool ret; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 516 | paddr_t fdt_addr; |
| 517 | bool has_fdt; |
| 518 | size_t kernel_size = 0; |
| 519 | const size_t mem_size = pa_difference(mem_begin, mem_end); |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 520 | uint32_t map_mode; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 521 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 522 | /* |
| 523 | * Load the kernel if a filename is specified in the VM manifest. |
| 524 | * For an FF-A partition, kernel_filename is undefined indicating |
| 525 | * the partition package has already been loaded prior to Hafnium |
| 526 | * booting. |
| 527 | */ |
| 528 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 529 | if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 530 | cpio, ppool, &kernel_size)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 531 | dlog_error("Unable to load kernel.\n"); |
| 532 | return false; |
| 533 | } |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 536 | has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename); |
| 537 | if (has_fdt) { |
| 538 | /* |
| 539 | * Ensure that the FDT does not overwrite the kernel or overlap |
| 540 | * its page, for the FDT to start at a page boundary. |
| 541 | */ |
| 542 | const size_t fdt_max_size = |
| 543 | mem_size - align_up(kernel_size, PAGE_SIZE); |
| 544 | |
| 545 | size_t fdt_allocated_size; |
| 546 | |
| 547 | if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size, |
| 548 | manifest_vm, cpio, ppool, &fdt_addr, |
| 549 | &fdt_allocated_size)) { |
| 550 | dlog_error("Unable to load FDT.\n"); |
| 551 | return false; |
| 552 | } |
| 553 | |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 554 | if (manifest_vm->is_ffa_partition) { |
| 555 | plat_ffa_parse_partition_manifest( |
| 556 | stage1_locked, fdt_addr, fdt_allocated_size, |
| 557 | manifest_vm, ppool); |
| 558 | } |
| 559 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 560 | if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size, |
| 561 | mem_begin, mem_end, ppool)) { |
| 562 | dlog_error("Unable to patch FDT.\n"); |
| 563 | return false; |
| 564 | } |
| 565 | } |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 566 | /* |
| 567 | * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the |
| 568 | * FF-A 1.0 spec. |
| 569 | */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 570 | CHECK(manifest_vm->partition.run_time_el != S_EL0 || |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 571 | manifest_vm->secondary.vcpu_count == 1); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 572 | |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 573 | if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm, |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 574 | (manifest_vm->partition.run_time_el == S_EL0))) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 575 | dlog_error("Unable to initialise VM.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 579 | vm_locked = vm_lock(vm); |
| 580 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 581 | /* |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 582 | * Grant the VM access to the memory. For VM's we mark all memory in |
| 583 | * stage-2 tables as RWX and the VM can control permissions using |
| 584 | * stage-1 translations. For S-EL0 partitions, hafnium maps the entire |
| 585 | * region of memory for the partition as RX. The partition is then |
| 586 | * expected to perform its owns relocations and call the FFA_MEM_PERM_* |
| 587 | * API's to change permissions on its image layout. |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 588 | */ |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 589 | if (vm->el0_partition) { |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 590 | map_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG; |
| 591 | } else { |
| 592 | map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 593 | } |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 594 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 595 | 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] | 596 | &secondary_entry)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 597 | dlog_error("Unable to initialise memory.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 598 | ret = false; |
| 599 | goto out; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 602 | if (manifest_vm->is_ffa_partition) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 603 | int j = 0; |
| 604 | paddr_t region_begin; |
| 605 | paddr_t region_end; |
| 606 | paddr_t alloc_base = mem_end; |
| 607 | size_t size; |
| 608 | size_t total_alloc = 0; |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 609 | uint32_t attributes; |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 610 | |
| 611 | /* Map memory-regions */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 612 | while (j < manifest_vm->partition.mem_region_count) { |
| 613 | size = manifest_vm->partition.mem_regions[j] |
| 614 | .page_count * |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 615 | PAGE_SIZE; |
| 616 | /* |
| 617 | * For memory-regions without base-address, memory |
| 618 | * should be allocated inside partition's page table. |
| 619 | * Start allocating memory regions in partition's |
| 620 | * page table, starting from the end. |
| 621 | * TODO: Add mechanism to let partition know of these |
| 622 | * memory regions |
| 623 | */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 624 | if (manifest_vm->partition.mem_regions[j] |
| 625 | .base_address == MANIFEST_INVALID_ADDRESS) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 626 | total_alloc += size; |
| 627 | /* Don't go beyond half the VM's memory space */ |
| 628 | if (total_alloc > |
| 629 | (manifest_vm->secondary.mem_size / 2)) { |
| 630 | dlog_error( |
| 631 | "Not enough space for memory-" |
| 632 | "region allocation"); |
| 633 | ret = false; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | region_end = alloc_base; |
| 638 | region_begin = pa_subtract(alloc_base, size); |
| 639 | alloc_base = region_begin; |
| 640 | |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 641 | attributes = |
| 642 | manifest_vm->partition.mem_regions[j] |
| 643 | .attributes; |
| 644 | if ((attributes & |
| 645 | MANIFEST_REGION_ATTR_SECURITY) != 0) { |
| 646 | if (plat_ffa_is_vm_id( |
| 647 | vm_locked.vm->id)) { |
| 648 | dlog_warning("Memory%sVMs\n", |
| 649 | error_string); |
| 650 | attributes &= |
| 651 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 652 | } else if (!vm->el0_partition) { |
| 653 | dlog_warning( |
| 654 | "Memory%sS-EL1 " |
| 655 | "partitions.\n", |
| 656 | error_string); |
| 657 | attributes &= |
| 658 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | map_mode = memory_region_attributes_to_mode( |
| 663 | attributes); |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 664 | if (vm->el0_partition) { |
| 665 | map_mode |= MM_MODE_USER | MM_MODE_NG; |
| 666 | } |
| 667 | |
| 668 | if (!vm_identity_map(vm_locked, region_begin, |
| 669 | region_end, map_mode, |
| 670 | ppool, NULL)) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 671 | dlog_error( |
| 672 | "Unable to map secondary VM " |
| 673 | "memory-region.\n"); |
| 674 | ret = false; |
| 675 | goto out; |
| 676 | } |
| 677 | |
Olivier Deprez | 86d87ae | 2021-08-19 14:27:46 +0200 | [diff] [blame] | 678 | dlog_verbose( |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 679 | " Memory region %#x - %#x allocated\n", |
| 680 | region_begin, region_end); |
| 681 | } else { |
| 682 | /* |
| 683 | * Identity map memory region for both case, |
| 684 | * VA(S-EL0) or IPA(S-EL1). |
| 685 | */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 686 | region_begin = pa_init( |
| 687 | manifest_vm->partition.mem_regions[j] |
| 688 | .base_address); |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 689 | region_end = pa_add(region_begin, size); |
| 690 | |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 691 | attributes = |
| 692 | manifest_vm->partition.mem_regions[j] |
| 693 | .attributes; |
| 694 | if ((attributes & |
| 695 | MANIFEST_REGION_ATTR_SECURITY) != 0) { |
| 696 | if (plat_ffa_is_vm_id( |
| 697 | vm_locked.vm->id)) { |
| 698 | dlog_warning("Memory%sVMs\n", |
| 699 | error_string); |
| 700 | attributes &= |
| 701 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 702 | |
| 703 | } else if (!vm->el0_partition) { |
| 704 | dlog_warning( |
| 705 | "Memory%sS-EL1 " |
| 706 | "partitions.\n", |
| 707 | error_string); |
| 708 | attributes &= |
| 709 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | map_mode = memory_region_attributes_to_mode( |
| 714 | attributes); |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 715 | if (vm->el0_partition) { |
| 716 | map_mode |= MM_MODE_USER | MM_MODE_NG; |
| 717 | } |
| 718 | |
| 719 | if (!vm_identity_map(vm_locked, region_begin, |
| 720 | region_end, map_mode, |
| 721 | ppool, NULL)) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 722 | dlog_error( |
| 723 | "Unable to map secondary VM " |
| 724 | "memory-region.\n"); |
| 725 | ret = false; |
| 726 | goto out; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* Deny the primary VM access to this memory */ |
| 731 | if (!vm_unmap(primary_vm_locked, region_begin, |
| 732 | region_end, ppool)) { |
| 733 | dlog_error( |
| 734 | "Unable to unmap secondary VM memory-" |
| 735 | "region from primary VM.\n"); |
| 736 | ret = false; |
| 737 | goto out; |
| 738 | } |
| 739 | |
| 740 | j++; |
| 741 | } |
| 742 | |
| 743 | /* Map device-regions */ |
| 744 | j = 0; |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 745 | while (j < manifest_vm->partition.dev_region_count) { |
| 746 | region_begin = |
| 747 | pa_init(manifest_vm->partition.dev_regions[j] |
| 748 | .base_address); |
| 749 | size = manifest_vm->partition.dev_regions[j] |
| 750 | .page_count * |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 751 | PAGE_SIZE; |
| 752 | region_end = pa_add(region_begin, size); |
| 753 | |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 754 | attributes = manifest_vm->partition.dev_regions[j] |
| 755 | .attributes; |
| 756 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) { |
| 757 | if (plat_ffa_is_vm_id(vm_locked.vm->id)) { |
| 758 | dlog_warning("Device%sVMs\n", |
| 759 | error_string); |
| 760 | attributes &= |
| 761 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 762 | } else if (!vm->el0_partition) { |
| 763 | dlog_warning( |
| 764 | "Device%sS-EL1 " |
| 765 | "partitions.\n", |
| 766 | error_string); |
| 767 | attributes &= |
| 768 | ~MANIFEST_REGION_ATTR_SECURITY; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | map_mode = device_region_attributes_to_mode(attributes); |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 773 | if (vm->el0_partition) { |
| 774 | map_mode |= MM_MODE_USER | MM_MODE_NG; |
| 775 | } |
| 776 | |
| 777 | if (!vm_identity_map(vm_locked, region_begin, |
| 778 | region_end, map_mode, ppool, |
| 779 | NULL)) { |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 780 | dlog_error( |
| 781 | "Unable to map secondary VM " |
| 782 | "device-region.\n"); |
| 783 | ret = false; |
| 784 | goto out; |
| 785 | } |
| 786 | /* Deny primary VM access to this region */ |
| 787 | if (!vm_unmap(primary_vm_locked, region_begin, |
| 788 | region_end, ppool)) { |
| 789 | dlog_error( |
| 790 | "Unable to unmap secondary VM device-" |
| 791 | "region from primary VM.\n"); |
| 792 | ret = false; |
| 793 | goto out; |
| 794 | } |
| 795 | j++; |
| 796 | } |
| 797 | |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 798 | secondary_entry = ipa_add(secondary_entry, |
| 799 | manifest_vm->partition.ep_offset); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 802 | /* |
| 803 | * Map hypervisor into the VM's page table. The hypervisor pages will |
| 804 | * not be accessible from EL0 since it will not be marked for user |
| 805 | * access. |
| 806 | * TODO: Map only the exception vectors and data that exception vectors |
| 807 | * require and not the entire hypervisor. This helps with speculative |
| 808 | * side-channel attacks. |
| 809 | */ |
| 810 | if (vm->el0_partition) { |
| 811 | CHECK(vm_identity_map(vm_locked, layout_text_begin(), |
| 812 | layout_text_end(), MM_MODE_X, ppool, |
| 813 | NULL)); |
| 814 | |
| 815 | CHECK(vm_identity_map(vm_locked, layout_rodata_begin(), |
| 816 | layout_rodata_end(), MM_MODE_R, ppool, |
| 817 | NULL)); |
| 818 | |
| 819 | CHECK(vm_identity_map(vm_locked, layout_data_begin(), |
| 820 | layout_data_end(), MM_MODE_R | MM_MODE_W, |
| 821 | ppool, NULL)); |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 822 | |
| 823 | CHECK(arch_stack_mm_init(mm_lock_ptable_unsafe(&vm->ptable), |
| 824 | ppool)); |
| 825 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 826 | plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool); |
| 827 | } |
| 828 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 829 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 830 | ret = false; |
| 831 | goto out; |
| 832 | } |
| 833 | |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 834 | dlog_info("Loaded with %u vCPUs, entry at %#x.\n", |
| 835 | manifest_vm->secondary.vcpu_count, pa_addr(mem_begin)); |
| 836 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 837 | vcpu = vm_get_vcpu(vm, 0); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 838 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 839 | vcpu_locked = vcpu_lock(vcpu); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 840 | if (has_fdt) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 841 | vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 842 | pa_addr(fdt_addr)); |
| 843 | } else { |
| 844 | /* |
| 845 | * Without an FDT, secondary VMs expect the memory size to be |
| 846 | * passed in register x0, which is what |
| 847 | * vcpu_secondary_reset_and_start does in this case. |
| 848 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 849 | vcpu_secondary_reset_and_start(vcpu_locked, secondary_entry, |
| 850 | mem_size); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 853 | vcpu_unlock(&vcpu_locked); |
| 854 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 855 | ret = true; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 856 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 857 | out: |
| 858 | vm_unlock(&vm_locked); |
| 859 | |
| 860 | return ret; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 863 | /** |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 864 | * Try to find a memory range of the given size within the given ranges, and |
| 865 | * remove it from them. Return true on success, or false if no large enough |
| 866 | * contiguous range is found. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 867 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 868 | static bool carve_out_mem_range(struct mem_range *mem_ranges, |
| 869 | size_t mem_ranges_count, uint64_t size_to_find, |
| 870 | paddr_t *found_begin, paddr_t *found_end) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 871 | { |
| 872 | size_t i; |
| 873 | |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 874 | /* |
| 875 | * TODO(b/116191358): Consider being cleverer about how we pack VMs |
| 876 | * together, with a non-greedy algorithm. |
| 877 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 878 | for (i = 0; i < mem_ranges_count; ++i) { |
| 879 | if (size_to_find <= |
Andrew Walbran | 2cb4339 | 2019-04-17 12:52:45 +0100 | [diff] [blame] | 880 | pa_difference(mem_ranges[i].begin, mem_ranges[i].end)) { |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 881 | /* |
| 882 | * This range is big enough, take some of it from the |
| 883 | * end and reduce its size accordingly. |
| 884 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 885 | *found_end = mem_ranges[i].end; |
| 886 | *found_begin = pa_init(pa_addr(mem_ranges[i].end) - |
| 887 | size_to_find); |
| 888 | mem_ranges[i].end = *found_begin; |
| 889 | return true; |
| 890 | } |
| 891 | } |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Given arrays of memory ranges before and after memory was removed for |
| 897 | * secondary VMs, add the difference to the reserved ranges of the given update. |
| 898 | * Return true on success, or false if there would be more than MAX_MEM_RANGES |
| 899 | * reserved ranges after adding the new ones. |
| 900 | * `before` and `after` must be arrays of exactly `mem_ranges_count` elements. |
| 901 | */ |
Hong-Seok Kim | 0964836 | 2019-05-23 15:47:11 +0900 | [diff] [blame] | 902 | static bool update_reserved_ranges(struct boot_params_update *update, |
| 903 | const struct mem_range *before, |
| 904 | const struct mem_range *after, |
| 905 | size_t mem_ranges_count) |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 906 | { |
| 907 | size_t i; |
| 908 | |
| 909 | for (i = 0; i < mem_ranges_count; ++i) { |
| 910 | if (pa_addr(after[i].begin) > pa_addr(before[i].begin)) { |
| 911 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 912 | dlog_error( |
| 913 | "Too many reserved ranges after " |
| 914 | "loading secondary VMs.\n"); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | update->reserved_ranges[update->reserved_ranges_count] |
| 918 | .begin = before[i].begin; |
| 919 | update->reserved_ranges[update->reserved_ranges_count] |
| 920 | .end = after[i].begin; |
| 921 | update->reserved_ranges_count++; |
| 922 | } |
| 923 | if (pa_addr(after[i].end) < pa_addr(before[i].end)) { |
| 924 | if (update->reserved_ranges_count >= MAX_MEM_RANGES) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 925 | dlog_error( |
| 926 | "Too many reserved ranges after " |
| 927 | "loading secondary VMs.\n"); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 928 | return false; |
| 929 | } |
| 930 | update->reserved_ranges[update->reserved_ranges_count] |
| 931 | .begin = after[i].end; |
| 932 | update->reserved_ranges[update->reserved_ranges_count] |
| 933 | .end = before[i].end; |
| 934 | update->reserved_ranges_count++; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | return true; |
| 939 | } |
| 940 | |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 941 | static bool init_other_world_vm(struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 942 | { |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 943 | struct vm *other_world_vm; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 944 | size_t i; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 945 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 946 | /* |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 947 | * Initialise the dummy VM which represents the opposite world: |
| 948 | * -TrustZone (or the SPMC) when running the Hypervisor |
| 949 | * -the Hypervisor when running TZ/SPMC |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 950 | */ |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 951 | 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] | 952 | CHECK(other_world_vm != NULL); |
| 953 | |
| 954 | for (i = 0; i < MAX_CPUS; i++) { |
| 955 | struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i); |
| 956 | struct cpu *cpu = cpu_find_index(i); |
| 957 | |
| 958 | vcpu->cpu = cpu; |
| 959 | } |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 960 | |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 961 | return arch_other_world_vm_init(other_world_vm, ppool); |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Loads alls VMs from the manifest. |
| 966 | */ |
| 967 | bool load_vms(struct mm_stage1_locked stage1_locked, |
| 968 | const struct manifest *manifest, const struct memiter *cpio, |
| 969 | const struct boot_params *params, |
| 970 | struct boot_params_update *update, struct mpool *ppool) |
| 971 | { |
| 972 | struct vm *primary; |
| 973 | struct mem_range mem_ranges_available[MAX_MEM_RANGES]; |
| 974 | struct vm_locked primary_vm_locked; |
| 975 | size_t i; |
| 976 | bool success = true; |
| 977 | |
Andrew Walbran | f871678 | 2020-10-29 17:07:07 +0000 | [diff] [blame] | 978 | /** |
| 979 | * Only try to load the primary VM if it is supposed to be in this |
| 980 | * world. |
| 981 | */ |
| 982 | if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) { |
| 983 | if (!load_primary(stage1_locked, |
| 984 | &manifest->vm[HF_PRIMARY_VM_INDEX], cpio, |
| 985 | params, ppool)) { |
| 986 | dlog_error("Unable to load primary VM.\n"); |
| 987 | return false; |
| 988 | } |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (!init_other_world_vm(ppool)) { |
| 992 | return false; |
| 993 | } |
| 994 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 995 | static_assert( |
| 996 | sizeof(mem_ranges_available) == sizeof(params->mem_ranges), |
| 997 | "mem_range arrays must be the same size for memcpy."); |
| 998 | static_assert(sizeof(mem_ranges_available) < 500, |
| 999 | "This will use too much stack, either make " |
| 1000 | "MAX_MEM_RANGES smaller or change this."); |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 1001 | memcpy_s(mem_ranges_available, sizeof(mem_ranges_available), |
| 1002 | params->mem_ranges, sizeof(params->mem_ranges)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1003 | |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1004 | /* Round the last addresses down to the page size. */ |
| 1005 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 1006 | mem_ranges_available[i].end = pa_init(align_down( |
| 1007 | pa_addr(mem_ranges_available[i].end), PAGE_SIZE)); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1008 | } |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 1009 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1010 | primary = vm_find(HF_PRIMARY_VM_ID); |
| 1011 | primary_vm_locked = vm_lock(primary); |
| 1012 | |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 1013 | for (i = 0; i < manifest->vm_count; ++i) { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 1014 | const struct manifest_vm *manifest_vm = &manifest->vm[i]; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1015 | ffa_vm_id_t vm_id = HF_VM_ID_OFFSET + i; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 1016 | uint64_t mem_size; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1017 | paddr_t secondary_mem_begin; |
| 1018 | paddr_t secondary_mem_end; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1019 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 1020 | if (vm_id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1021 | continue; |
| 1022 | } |
| 1023 | |
Olivier Deprez | 2a8ee34 | 2020-08-03 15:10:44 +0200 | [diff] [blame] | 1024 | dlog_info("Loading VM id %#x: %s.\n", vm_id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1025 | manifest_vm->debug_name); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1026 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 1027 | mem_size = align_up(manifest_vm->secondary.mem_size, PAGE_SIZE); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 1028 | |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 1029 | if (manifest_vm->is_ffa_partition && |
| 1030 | !manifest->vm[i].is_hyp_loaded) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 1031 | secondary_mem_begin = |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 1032 | pa_init(manifest_vm->partition.load_addr); |
| 1033 | secondary_mem_end = pa_init( |
| 1034 | manifest_vm->partition.load_addr + mem_size); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 1035 | } else if (!carve_out_mem_range(mem_ranges_available, |
| 1036 | params->mem_ranges_count, |
| 1037 | mem_size, &secondary_mem_begin, |
| 1038 | &secondary_mem_end)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1039 | dlog_error("Not enough memory (%u bytes).\n", mem_size); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1040 | continue; |
| 1041 | } |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1042 | |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 1043 | if (!load_secondary(stage1_locked, primary_vm_locked, |
| 1044 | secondary_mem_begin, secondary_mem_end, |
| 1045 | manifest_vm, cpio, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1046 | dlog_error("Unable to load VM.\n"); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 1047 | continue; |
| 1048 | } |
| 1049 | |
| 1050 | /* Deny the primary VM access to this memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1051 | if (!vm_unmap(primary_vm_locked, secondary_mem_begin, |
| 1052 | secondary_mem_end, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1053 | dlog_error( |
| 1054 | "Unable to unmap secondary VM from primary " |
| 1055 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1056 | success = false; |
| 1057 | break; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 1058 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1061 | vm_unlock(&primary_vm_locked); |
| 1062 | |
| 1063 | if (!success) { |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 1067 | /* |
| 1068 | * Add newly reserved areas to update params by looking at the |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1069 | * difference between the available ranges from the original params and |
| 1070 | * the updated mem_ranges_available. We assume that the number and order |
| 1071 | * 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] | 1072 | * above only make them smaller. |
| 1073 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1074 | return update_reserved_ranges(update, params->mem_ranges, |
| 1075 | mem_ranges_available, |
| 1076 | params->mem_ranges_count); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1077 | } |