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 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 11 | #include "hf/arch/init.h" |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 12 | #include "hf/arch/other_world.h" |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 13 | #include "hf/arch/vm.h" |
| 14 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 15 | #include "hf/api.h" |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 16 | #include "hf/boot_params.h" |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 17 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 18 | #include "hf/dlog.h" |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 19 | #include "hf/fdt_patch.h" |
Karl Meakin | 902af08 | 2024-11-28 14:58:38 +0000 | [diff] [blame] | 20 | #include "hf/ffa/interrupts.h" |
| 21 | #include "hf/ffa/notifications.h" |
| 22 | #include "hf/ffa/setup_and_discovery.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 23 | #include "hf/layout.h" |
J-Alves | 77b6f4f | 2023-03-15 11:34:49 +0000 | [diff] [blame] | 24 | #include "hf/manifest.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 25 | #include "hf/memiter.h" |
| 26 | #include "hf/mm.h" |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 27 | #include "hf/plat/console.h" |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 28 | #include "hf/plat/iommu.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 29 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 30 | #include "hf/vm.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 31 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 32 | #include "vmapi/hf/ffa.h" |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 33 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 34 | /** |
| 35 | * Copies data to an unmapped location by mapping it for write, copying the |
| 36 | * data, then unmapping it. |
Andrew Scull | d9225b3 | 2018-11-19 16:12:41 +0000 | [diff] [blame] | 37 | * |
| 38 | * The data is written so that it is available to all cores with the cache |
| 39 | * disabled. When switching to the partitions, the caching is initially disabled |
| 40 | * so the data must be available without the cache. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 41 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 42 | 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] | 43 | struct memiter *from_it, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 44 | { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 45 | const void *from = memiter_base(from_it); |
| 46 | size_t size = memiter_size(from_it); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 47 | paddr_t to_end = pa_add(to, size); |
| 48 | void *ptr; |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 49 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 50 | 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] | 51 | if (!ptr) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 55 | memcpy_s(ptr, size, from, size); |
Andrew Scull | c059fbe | 2019-09-12 12:58:40 +0100 | [diff] [blame] | 56 | arch_mm_flush_dcache(ptr, size); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 57 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 58 | CHECK(mm_unmap(stage1_locked, to, to_end, ppool)); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Loads the secondary VM's kernel. |
| 65 | * Stores the kernel size in kernel_size (if kernel_size is not NULL). |
| 66 | * Returns false if it cannot load the kernel. |
| 67 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 68 | static bool load_kernel(struct mm_stage1_locked stage1_locked, paddr_t begin, |
| 69 | paddr_t end, const struct manifest_vm *manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 70 | const struct memiter *cpio, struct mpool *ppool, |
| 71 | size_t *kernel_size) |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 72 | { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 73 | struct memiter kernel; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 74 | size_t size; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 75 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 76 | if (!cpio_get_file(cpio, &manifest_vm->kernel_filename, &kernel)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 77 | dlog_error("Could not find kernel file \"%s\".\n", |
| 78 | string_data(&manifest_vm->kernel_filename)); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 82 | size = memiter_size(&kernel); |
| 83 | if (pa_difference(begin, end) < size) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 84 | dlog_error("Kernel is larger than available memory.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (!copy_to_unmapped(stage1_locked, begin, &kernel, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 89 | dlog_error("Unable to copy kernel.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 93 | if (kernel_size) { |
| 94 | *kernel_size = size; |
| 95 | } |
| 96 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Link RX/TX buffers provided in partition manifest to mailbox |
| 102 | */ |
| 103 | static bool link_rxtx_to_mailbox(struct mm_stage1_locked stage1_locked, |
| 104 | struct vm_locked vm_locked, struct rx_tx rxtx, |
| 105 | struct mpool *ppool) |
| 106 | { |
| 107 | struct ffa_value ret; |
| 108 | ipaddr_t send; |
| 109 | ipaddr_t recv; |
| 110 | uint32_t page_count; |
| 111 | |
| 112 | send = ipa_init(rxtx.tx_buffer->base_address); |
| 113 | recv = ipa_init(rxtx.rx_buffer->base_address); |
| 114 | page_count = rxtx.tx_buffer->page_count; |
| 115 | |
| 116 | ret = api_vm_configure_pages(stage1_locked, vm_locked, send, recv, |
| 117 | page_count, ppool); |
| 118 | if (ret.func != FFA_SUCCESS_32) { |
| 119 | return false; |
| 120 | } |
| 121 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 122 | dlog_verbose(" mailbox: send = %p, recv = %p\n", |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 123 | vm_locked.vm->mailbox.send, vm_locked.vm->mailbox.recv); |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
Raghu Krishnamurthy | ad38a9c | 2022-07-20 07:30:36 -0700 | [diff] [blame] | 128 | static void infer_interrupt(struct interrupt_info interrupt, |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 129 | struct interrupt_descriptor *int_desc) |
| 130 | { |
| 131 | uint32_t attr = interrupt.attributes; |
| 132 | |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 133 | int_desc->interrupt_id = interrupt.id; |
| 134 | int_desc->priority = (attr >> INT_INFO_ATTR_PRIORITY_SHIFT) & 0xff; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 135 | |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 136 | int_desc->type = (attr >> INT_INFO_ATTR_TYPE_SHIFT) & 0x3; |
| 137 | int_desc->config = (attr >> INT_INFO_ATTR_CONFIG_SHIFT) & 0x1; |
| 138 | int_desc->sec_state = (attr >> INT_INFO_ATTR_SEC_STATE_SHIFT) & 0x1; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 139 | |
Raghu Krishnamurthy | 98da1ca | 2022-10-04 08:59:01 -0700 | [diff] [blame] | 140 | if (interrupt.mpidr_valid) { |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 141 | int_desc->mpidr_valid = true; |
| 142 | int_desc->mpidr = interrupt.mpidr; |
Raghu Krishnamurthy | 98da1ca | 2022-10-04 08:59:01 -0700 | [diff] [blame] | 143 | } else { |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 144 | int_desc->mpidr_valid = false; |
| 145 | int_desc->mpidr = 0; |
Raghu Krishnamurthy | 98da1ca | 2022-10-04 08:59:01 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Daniel Boulby | 1848594 | 2024-10-14 16:23:03 +0100 | [diff] [blame] | 148 | int_desc->valid = true; |
| 149 | int_desc->enabled = true; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 152 | /** |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 153 | * Performs VM loading activities that are common between the primary and |
| 154 | * secondaries. |
| 155 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 156 | static bool load_common(struct mm_stage1_locked stage1_locked, |
| 157 | struct vm_locked vm_locked, |
| 158 | const struct manifest_vm *manifest_vm, |
| 159 | struct mpool *ppool) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 160 | { |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 161 | struct device_region dev_region; |
Raghu Krishnamurthy | ad38a9c | 2022-07-20 07:30:36 -0700 | [diff] [blame] | 162 | struct interrupt_info interrupt; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 163 | uint32_t k = 0; |
| 164 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 165 | vm_locked.vm->smc_whitelist = manifest_vm->smc_whitelist; |
Olivier Deprez | a15f235 | 2022-09-26 09:17:24 +0200 | [diff] [blame] | 166 | vm_locked.vm->power_management = |
| 167 | manifest_vm->partition.power_management; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 168 | |
Kathleen Capella | 422b10b | 2023-06-30 18:28:27 -0400 | [diff] [blame] | 169 | /* Populate array of UUIDs. */ |
| 170 | for (uint16_t i = 0; i < PARTITION_MAX_UUIDS; i++) { |
| 171 | struct ffa_uuid current_uuid = manifest_vm->partition.uuids[i]; |
| 172 | |
| 173 | if (ffa_uuid_is_null(¤t_uuid)) { |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | vm_locked.vm->uuids[i] = current_uuid; |
| 178 | } |
| 179 | |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 180 | /* |
| 181 | * Populate the interrupt descriptor for current VM. |
| 182 | * They can be enabled in runtime using HF_INTERRUPT_ENABLE. |
| 183 | */ |
Raghu Krishnamurthy | 641dcd8 | 2022-07-19 23:21:20 -0700 | [diff] [blame] | 184 | for (uint16_t i = 0; i < PARTITION_MAX_DEVICE_REGIONS; i++) { |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 185 | dev_region = manifest_vm->partition.dev_regions[i]; |
| 186 | |
| 187 | CHECK(dev_region.interrupt_count <= |
Raghu Krishnamurthy | 641dcd8 | 2022-07-19 23:21:20 -0700 | [diff] [blame] | 188 | PARTITION_MAX_INTERRUPTS_PER_DEVICE); |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 189 | |
| 190 | for (uint8_t j = 0; j < dev_region.interrupt_count; j++) { |
Raghu Krishnamurthy | 98da1ca | 2022-10-04 08:59:01 -0700 | [diff] [blame] | 191 | struct interrupt_descriptor int_desc = {0}; |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 192 | |
| 193 | interrupt = dev_region.interrupts[j]; |
| 194 | infer_interrupt(interrupt, &int_desc); |
| 195 | vm_locked.vm->interrupt_desc[k] = int_desc; |
Madhukar Pappireddy | 938faaf | 2023-07-31 17:56:55 -0500 | [diff] [blame] | 196 | assert(int_desc.enabled); |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 197 | |
| 198 | k++; |
| 199 | CHECK(k <= VM_MANIFEST_MAX_INTERRUPTS); |
| 200 | } |
| 201 | } |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 202 | |
Madhukar Pappireddy | 5fc8be1 | 2021-08-03 11:42:53 -0500 | [diff] [blame] | 203 | dlog_verbose("VM has %d physical interrupts defined in manifest.\n", k); |
| 204 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 205 | if (manifest_vm->is_ffa_partition) { |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 206 | vm_locked.vm->ffa_version = manifest_vm->partition.ffa_version; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 207 | /* Link rxtx buffers to mailbox */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 208 | if (manifest_vm->partition.rxtx.available) { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 209 | if (!link_rxtx_to_mailbox(stage1_locked, vm_locked, |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 210 | manifest_vm->partition.rxtx, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 211 | ppool)) { |
| 212 | dlog_error( |
| 213 | "Unable to Link RX/TX buffer with " |
| 214 | "mailbox.\n"); |
| 215 | return false; |
| 216 | } |
| 217 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 218 | |
Maksims Svecovs | b596eab | 2021-04-27 00:52:27 +0100 | [diff] [blame] | 219 | vm_locked.vm->messaging_method = |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 220 | manifest_vm->partition.messaging_method; |
Manish Pandey | f3be639 | 2020-09-24 17:26:09 +0100 | [diff] [blame] | 221 | |
Madhukar Pappireddy | 8415405 | 2022-06-21 18:30:25 -0500 | [diff] [blame] | 222 | vm_locked.vm->ns_interrupts_action = |
| 223 | manifest_vm->partition.ns_interrupts_action; |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 224 | |
Madhukar Pappireddy | 5c04a38 | 2022-12-28 11:29:26 -0600 | [diff] [blame] | 225 | vm_locked.vm->other_s_interrupts_action = |
| 226 | manifest_vm->partition.other_s_interrupts_action; |
| 227 | |
Madhukar Pappireddy | 046dad0 | 2022-06-21 18:43:33 -0500 | [diff] [blame] | 228 | vm_locked.vm->me_signal_virq = |
| 229 | manifest_vm->partition.me_signal_virq; |
| 230 | |
J-Alves | a4730db | 2021-11-02 10:31:01 +0000 | [diff] [blame] | 231 | vm_locked.vm->notifications.enabled = |
| 232 | manifest_vm->partition.notification_support; |
| 233 | |
Karl Meakin | a603e08 | 2024-08-02 17:57:27 +0100 | [diff] [blame] | 234 | vm_locked.vm->vm_availability_messages.vm_created = |
| 235 | manifest_vm->partition.vm_availability_messages |
| 236 | .vm_created; |
| 237 | vm_locked.vm->vm_availability_messages.vm_destroyed = |
| 238 | manifest_vm->partition.vm_availability_messages |
| 239 | .vm_destroyed; |
| 240 | |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 241 | vm_locked.vm->boot_order = manifest_vm->partition.boot_order; |
| 242 | |
J-Alves | 7d38f7b | 2022-04-13 13:22:30 +0100 | [diff] [blame] | 243 | vm_locked.vm->boot_info.gp_register_num = |
| 244 | manifest_vm->partition.gp_register_num; |
| 245 | |
Madhukar Pappireddy | c35573d | 2025-03-17 18:00:32 -0500 | [diff] [blame] | 246 | vm_locked.vm->lifecycle_support = |
| 247 | manifest_vm->partition.lifecycle_support; |
| 248 | |
| 249 | vm_locked.vm->abort_action = |
| 250 | manifest_vm->partition.abort_action; |
| 251 | |
J-Alves | 7d38f7b | 2022-04-13 13:22:30 +0100 | [diff] [blame] | 252 | if (manifest_vm->partition.boot_info) { |
| 253 | /* |
| 254 | * If the partition expects the boot information blob |
| 255 | * per the ff-a v1.1 boot protocol, then its address |
| 256 | * shall match the partition's load address. |
| 257 | */ |
| 258 | vm_locked.vm->boot_info.blob_addr = |
| 259 | ipa_init(manifest_vm->partition.load_addr); |
| 260 | } |
| 261 | |
J-Alves | ce42f7a | 2025-02-10 13:57:41 +0000 | [diff] [blame] | 262 | vm_locked.vm->sri_policy = manifest_vm->partition.sri_policy; |
| 263 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 264 | /* Updating boot list according to boot_order */ |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 265 | vm_update_boot(vm_locked.vm); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 266 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 267 | if (vm_locked_are_notifications_enabled(vm_locked) && |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 268 | !ffa_notifications_bitmap_create_call( |
J-Alves | a4730db | 2021-11-02 10:31:01 +0000 | [diff] [blame] | 269 | vm_locked.vm->id, vm_locked.vm->vcpu_count)) { |
| 270 | return false; |
J-Alves | a9c7cba | 2021-08-25 16:26:11 +0100 | [diff] [blame] | 271 | } |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 272 | } |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 273 | |
Fuad Tabba | 5697071 | 2020-01-10 11:20:09 +0000 | [diff] [blame] | 274 | /* Initialize architecture-specific features. */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 275 | arch_vm_features_set(vm_locked.vm); |
Fuad Tabba | 77a4b01 | 2019-11-15 12:13:08 +0000 | [diff] [blame] | 276 | |
Madhukar Pappireddy | 54680c7 | 2020-10-23 15:02:38 -0500 | [diff] [blame] | 277 | if (!plat_iommu_attach_peripheral(stage1_locked, vm_locked, manifest_vm, |
| 278 | ppool)) { |
| 279 | dlog_error("Unable to attach upstream peripheral device\n"); |
| 280 | return false; |
| 281 | } |
| 282 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
| 286 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 287 | * Loads the primary VM. |
| 288 | */ |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 289 | static bool load_primary(struct mm_stage1_locked stage1_locked, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 290 | const struct manifest_vm *manifest_vm, |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 291 | const struct memiter *cpio, |
| 292 | const struct boot_params *params, struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 293 | { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 294 | paddr_t primary_begin; |
| 295 | ipaddr_t primary_entry; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 296 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 297 | struct vm_locked vm_locked; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 298 | struct vcpu_locked vcpu_locked; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 299 | size_t i; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 300 | bool ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 301 | |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 302 | if (manifest_vm->is_ffa_partition && !manifest_vm->is_hyp_loaded) { |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 303 | primary_begin = pa_init(manifest_vm->partition.load_addr); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 304 | primary_entry = ipa_add(ipa_from_pa(primary_begin), |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 305 | manifest_vm->partition.ep_offset); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 306 | } else { |
| 307 | primary_begin = |
| 308 | (manifest_vm->primary.boot_address == |
| 309 | MANIFEST_INVALID_ADDRESS) |
| 310 | ? layout_primary_begin() |
| 311 | : pa_init(manifest_vm->primary.boot_address); |
| 312 | primary_entry = ipa_from_pa(primary_begin); |
| 313 | } |
| 314 | |
David Brazdil | 080ee31 | 2020-02-25 15:30:30 -0800 | [diff] [blame] | 315 | paddr_t primary_end = pa_add(primary_begin, RSIZE_MAX); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 316 | |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 317 | /* Primary VM must be a VM */ |
Raghu Krishnamurthy | 8c250a9 | 2021-07-02 12:16:42 -0700 | [diff] [blame] | 318 | CHECK(manifest_vm->partition.run_time_el == EL1); |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 319 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 320 | /* |
| 321 | * Load the kernel if a filename is specified in the VM manifest. |
| 322 | * For an FF-A partition, kernel_filename is undefined indicating |
| 323 | * the partition package has already been loaded prior to Hafnium |
| 324 | * booting. |
| 325 | */ |
| 326 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 327 | if (!load_kernel(stage1_locked, primary_begin, primary_end, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 328 | manifest_vm, cpio, ppool, NULL)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 329 | dlog_error("Unable to load primary kernel.\n"); |
| 330 | return false; |
| 331 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 334 | if (!vm_init_next(MAX_CPUS, ppool, &vm, false, |
| 335 | manifest_vm->partition.dma_device_count)) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 336 | dlog_error("Unable to initialise primary VM.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 337 | return false; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 340 | if (!vm_is_primary(vm)) { |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 341 | dlog_error("Primary VM was not given correct ID.\n"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 342 | return false; |
| 343 | } |
| 344 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 345 | vm_locked = vm_lock(vm); |
| 346 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 347 | if (params->device_mem_ranges_count == 0) { |
| 348 | /* |
| 349 | * Map 1TB of address space as device memory to, most likely, |
| 350 | * make all devices available to the primary VM. |
| 351 | * |
| 352 | * TODO: remove this once all targets provide valid ranges. |
| 353 | */ |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 354 | dlog_warning( |
| 355 | "Device memory not provided, defaulting to 1 TB.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 356 | |
| 357 | if (!vm_identity_map( |
| 358 | vm_locked, pa_init(0), |
| 359 | pa_init(UINT64_C(1024) * 1024 * 1024 * 1024), |
| 360 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 361 | dlog_error( |
| 362 | "Unable to initialise address space for " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 363 | "primary VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 364 | ret = false; |
| 365 | goto out; |
| 366 | } |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 369 | /* Map normal memory as such to permit caching, execution, etc. */ |
| 370 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 371 | if (!vm_identity_map(vm_locked, params->mem_ranges[i].begin, |
| 372 | params->mem_ranges[i].end, |
| 373 | MM_MODE_R | MM_MODE_W | MM_MODE_X, ppool, |
| 374 | NULL)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 375 | dlog_error( |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 376 | "Unable to initialise memory for primary " |
| 377 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 378 | ret = false; |
| 379 | goto out; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 383 | /* Map device memory as such to prevent execution, speculation etc. */ |
| 384 | for (i = 0; i < params->device_mem_ranges_count; ++i) { |
| 385 | if (!vm_identity_map( |
| 386 | vm_locked, params->device_mem_ranges[i].begin, |
| 387 | params->device_mem_ranges[i].end, |
| 388 | MM_MODE_R | MM_MODE_W | MM_MODE_D, ppool, NULL)) { |
| 389 | dlog("Unable to initialise device memory for primary " |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 390 | "VM.\n"); |
Andrew Scull | 48929fd | 2020-01-28 10:39:10 +0000 | [diff] [blame] | 391 | ret = false; |
| 392 | goto out; |
| 393 | } |
| 394 | } |
| 395 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 396 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 397 | ret = false; |
| 398 | goto out; |
| 399 | } |
| 400 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 401 | if (!vm_unmap_hypervisor(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 402 | dlog_error("Unable to unmap hypervisor from primary VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 403 | ret = false; |
| 404 | goto out; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 407 | if (!plat_iommu_unmap_iommus(vm_locked, ppool)) { |
Andrew Scull | f6ab9bc | 2020-02-26 12:56:37 -0800 | [diff] [blame] | 408 | dlog_error("Unable to unmap IOMMUs from primary VM.\n"); |
Andrew Scull | b1a6d0d | 2020-01-29 11:25:12 +0000 | [diff] [blame] | 409 | ret = false; |
| 410 | goto out; |
| 411 | } |
| 412 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 413 | dlog_info("Loaded primary VM with %u vCPUs, entry at %#lx.\n", |
Andrew Walbran | 7586e04 | 2020-02-18 18:19:26 +0000 | [diff] [blame] | 414 | vm->vcpu_count, pa_addr(primary_begin)); |
| 415 | |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 416 | /* Mark the first VM to be the first booted VM. */ |
| 417 | vm_update_boot(vm); |
Olivier Deprez | b9adff4 | 2021-02-01 12:14:05 +0100 | [diff] [blame] | 418 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 419 | vcpu_locked = vcpu_lock(vm_get_vcpu(vm, 0)); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 420 | vcpu_on(vcpu_locked, primary_entry, params->kernel_arg); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 421 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 422 | ret = true; |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 423 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 424 | out: |
| 425 | vm_unlock(&vm_locked); |
| 426 | |
| 427 | return ret; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 430 | /** |
| 431 | * Loads the secondary VM's FDT. |
| 432 | * Stores the total allocated size for the FDT in fdt_allocated_size (if |
| 433 | * fdt_allocated_size is not NULL). The allocated size includes additional space |
| 434 | * for potential patching. |
| 435 | */ |
| 436 | static bool load_secondary_fdt(struct mm_stage1_locked stage1_locked, |
| 437 | paddr_t end, size_t fdt_max_size, |
| 438 | const struct manifest_vm *manifest_vm, |
| 439 | const struct memiter *cpio, struct mpool *ppool, |
| 440 | paddr_t *fdt_addr, size_t *fdt_allocated_size) |
| 441 | { |
| 442 | struct memiter fdt; |
| 443 | size_t allocated_size; |
| 444 | |
| 445 | CHECK(!string_is_empty(&manifest_vm->secondary.fdt_filename)); |
| 446 | |
| 447 | if (!cpio_get_file(cpio, &manifest_vm->secondary.fdt_filename, &fdt)) { |
| 448 | dlog_error("Cannot open the secondary VM's FDT.\n"); |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | /* |
Olivier Deprez | 701e8bf | 2022-04-06 18:45:18 +0200 | [diff] [blame] | 453 | * Ensure the FDT has one additional page at the end for patching, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 454 | * and align it to the page boundary. |
| 455 | */ |
| 456 | allocated_size = align_up(memiter_size(&fdt), PAGE_SIZE) + PAGE_SIZE; |
| 457 | |
| 458 | if (allocated_size > fdt_max_size) { |
| 459 | dlog_error( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 460 | "FDT allocated space (%zu) is more than the specified " |
| 461 | "maximum to use (%zu).\n", |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 462 | allocated_size, fdt_max_size); |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | /* Load the FDT to the end of the VM's allocated memory space. */ |
| 467 | *fdt_addr = pa_init(pa_addr(pa_sub(end, allocated_size))); |
| 468 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 469 | dlog_info("Loading secondary FDT of allocated size %zu at 0x%lx.\n", |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 470 | allocated_size, pa_addr(*fdt_addr)); |
| 471 | |
| 472 | if (!copy_to_unmapped(stage1_locked, *fdt_addr, &fdt, ppool)) { |
| 473 | dlog_error("Unable to copy FDT.\n"); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | if (fdt_allocated_size) { |
| 478 | *fdt_allocated_size = allocated_size; |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 484 | /** |
| 485 | * Convert the manifest memory region attributes to mode consumed by mm layer. |
| 486 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 487 | static mm_mode_t memory_region_attributes_to_mode(uint32_t attributes) |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 488 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 489 | mm_mode_t mode = 0U; |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 490 | |
| 491 | if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) { |
| 492 | mode |= MM_MODE_R; |
| 493 | } |
| 494 | |
| 495 | if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) { |
| 496 | mode |= MM_MODE_W; |
| 497 | } |
| 498 | |
| 499 | if ((attributes & MANIFEST_REGION_ATTR_EXEC) != 0U) { |
| 500 | mode |= MM_MODE_X; |
| 501 | } |
| 502 | |
| 503 | assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R) || |
| 504 | (mode == (MM_MODE_R | MM_MODE_X))); |
| 505 | |
| 506 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 507 | mode |= arch_mm_extra_mode_from_vm(HF_HYPERVISOR_VM_ID); |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | return mode; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Convert the manifest device region attributes to mode consumed by mm layer. |
| 515 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 516 | static mm_mode_t device_region_attributes_to_mode(uint32_t attributes) |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 517 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 518 | mm_mode_t mode = 0U; |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 519 | |
| 520 | if ((attributes & MANIFEST_REGION_ATTR_READ) != 0U) { |
| 521 | mode |= MM_MODE_R; |
| 522 | } |
| 523 | |
| 524 | if ((attributes & MANIFEST_REGION_ATTR_WRITE) != 0U) { |
| 525 | mode |= MM_MODE_W; |
| 526 | } |
| 527 | |
| 528 | assert((mode == (MM_MODE_R | MM_MODE_W)) || (mode == MM_MODE_R)); |
| 529 | |
| 530 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0U) { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 531 | mode |= arch_mm_extra_mode_from_vm(HF_HYPERVISOR_VM_ID); |
Olivier Deprez | 035fa15 | 2022-03-14 11:19:10 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return mode | MM_MODE_D; |
| 535 | } |
| 536 | |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 537 | static bool ffa_map_memory_regions(const struct manifest_vm *manifest_vm, |
| 538 | const struct vm_locked vm_locked, |
| 539 | const struct vm_locked primary_vm_locked, |
J-Alves | d8a1d36 | 2023-03-08 11:15:28 +0000 | [diff] [blame] | 540 | bool is_el0_partition, struct mpool *ppool) |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 541 | { |
| 542 | #if LOG_LEVEL >= LOG_LEVEL_WARNING |
| 543 | const char *error_string = " region security state ignored for "; |
| 544 | #endif |
| 545 | int j = 0; |
| 546 | paddr_t region_begin; |
| 547 | paddr_t region_end; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 548 | size_t size; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 549 | mm_mode_t map_mode; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 550 | uint32_t attributes; |
| 551 | |
| 552 | /* Map memory-regions */ |
| 553 | while (j < manifest_vm->partition.mem_region_count) { |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 554 | struct memory_region mem_region; |
| 555 | |
| 556 | mem_region = manifest_vm->partition.mem_regions[j]; |
| 557 | size = mem_region.page_count * PAGE_SIZE; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 558 | /* |
J-Alves | d8a1d36 | 2023-03-08 11:15:28 +0000 | [diff] [blame] | 559 | * Identity map memory region for both case, |
| 560 | * VA(S-EL0) or IPA(S-EL1). |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 561 | */ |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 562 | region_begin = pa_init(mem_region.base_address); |
J-Alves | d8a1d36 | 2023-03-08 11:15:28 +0000 | [diff] [blame] | 563 | region_end = pa_add(region_begin, size); |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 564 | |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 565 | attributes = mem_region.attributes; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 566 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) { |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 567 | if (ffa_is_vm_id(vm_locked.vm->id)) { |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 568 | dlog_warning("Memory%sVMs\n", error_string); |
| 569 | attributes &= ~MANIFEST_REGION_ATTR_SECURITY; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | map_mode = memory_region_attributes_to_mode(attributes); |
| 574 | |
| 575 | if (is_el0_partition) { |
| 576 | map_mode |= MM_MODE_USER | MM_MODE_NG; |
| 577 | } |
| 578 | |
| 579 | if (!vm_identity_map(vm_locked, region_begin, region_end, |
| 580 | map_mode, ppool, NULL)) { |
| 581 | dlog_error( |
| 582 | "Unable to map secondary VM " |
| 583 | "memory-region.\n"); |
| 584 | return false; |
| 585 | } |
| 586 | |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 587 | /* |
| 588 | * Enforce static DMA isolation through stage 2 address |
| 589 | * translation. |
| 590 | * Only the DMA device that is specified as part of this memory |
| 591 | * region node in the partition manifest will be granted access |
| 592 | * to the memory region. |
| 593 | */ |
| 594 | if (mem_region.dma_prop.stream_count > 0 && |
| 595 | !vm_iommu_mm_identity_map( |
| 596 | vm_locked, region_begin, region_end, map_mode, |
| 597 | ppool, NULL, mem_region.dma_prop.dma_device_id)) { |
| 598 | dlog_error( |
| 599 | "Unable to map memory-region in the page " |
| 600 | "tables of DMA device.\n"); |
| 601 | return false; |
| 602 | } |
| 603 | |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 604 | /* Deny the primary VM access to this memory */ |
| 605 | if (!vm_unmap(primary_vm_locked, region_begin, region_end, |
| 606 | ppool)) { |
| 607 | dlog_error( |
| 608 | "Unable to unmap secondary VM memory-" |
| 609 | "region from primary VM.\n"); |
| 610 | return false; |
| 611 | } |
| 612 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 613 | dlog_verbose("Memory region %#lx - %#lx allocated.\n", |
| 614 | pa_addr(region_begin), pa_addr(region_end)); |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 615 | |
| 616 | j++; |
| 617 | } |
| 618 | |
| 619 | /* Map device-regions */ |
| 620 | j = 0; |
| 621 | while (j < manifest_vm->partition.dev_region_count) { |
| 622 | region_begin = pa_init( |
| 623 | manifest_vm->partition.dev_regions[j].base_address); |
| 624 | size = manifest_vm->partition.dev_regions[j].page_count * |
| 625 | PAGE_SIZE; |
| 626 | region_end = pa_add(region_begin, size); |
| 627 | |
| 628 | attributes = manifest_vm->partition.dev_regions[j].attributes; |
| 629 | if ((attributes & MANIFEST_REGION_ATTR_SECURITY) != 0) { |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 630 | if (ffa_is_vm_id(vm_locked.vm->id)) { |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 631 | dlog_warning("Device%sVMs\n", error_string); |
| 632 | attributes &= ~MANIFEST_REGION_ATTR_SECURITY; |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | map_mode = device_region_attributes_to_mode(attributes); |
| 637 | if (is_el0_partition) { |
| 638 | map_mode |= MM_MODE_USER | MM_MODE_NG; |
| 639 | } |
| 640 | |
| 641 | if (!vm_identity_map(vm_locked, region_begin, region_end, |
| 642 | map_mode, ppool, NULL)) { |
| 643 | dlog_error( |
| 644 | "Unable to map secondary VM " |
| 645 | "device-region.\n"); |
| 646 | return false; |
| 647 | } |
| 648 | /* Deny primary VM access to this region */ |
| 649 | if (!vm_unmap(primary_vm_locked, region_begin, region_end, |
| 650 | ppool)) { |
| 651 | dlog_error( |
| 652 | "Unable to unmap secondary VM device-" |
| 653 | "region from primary VM.\n"); |
| 654 | return false; |
| 655 | } |
| 656 | j++; |
| 657 | } |
| 658 | return true; |
| 659 | } |
| 660 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 661 | /* |
| 662 | * Loads a secondary VM. |
| 663 | */ |
| 664 | static bool load_secondary(struct mm_stage1_locked stage1_locked, |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 665 | struct vm_locked primary_vm_locked, |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 666 | paddr_t mem_begin, paddr_t mem_end, |
| 667 | const struct manifest_vm *manifest_vm, |
J-Alves | 77b6f4f | 2023-03-15 11:34:49 +0000 | [diff] [blame] | 668 | const struct boot_params *boot_params, |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 669 | const struct memiter *cpio, struct mpool *ppool) |
| 670 | { |
| 671 | struct vm *vm; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 672 | struct vm_locked vm_locked; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 673 | struct vcpu_locked vcpu_locked; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 674 | struct vcpu *vcpu; |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 675 | ipaddr_t partition_primary_ep; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 676 | bool ret; |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 677 | paddr_t fdt_addr; |
| 678 | bool has_fdt; |
| 679 | size_t kernel_size = 0; |
| 680 | const size_t mem_size = pa_difference(mem_begin, mem_end); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 681 | mm_mode_t map_mode; |
Daniel Boulby | 874d543 | 2023-04-27 12:40:24 +0100 | [diff] [blame] | 682 | bool is_el0_partition = manifest_vm->partition.run_time_el == S_EL0 || |
| 683 | manifest_vm->partition.run_time_el == EL0; |
Jens Wiklander | b697ad0 | 2022-11-04 10:15:03 +0100 | [diff] [blame] | 684 | size_t n; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 685 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 686 | /* |
| 687 | * Load the kernel if a filename is specified in the VM manifest. |
| 688 | * For an FF-A partition, kernel_filename is undefined indicating |
| 689 | * the partition package has already been loaded prior to Hafnium |
| 690 | * booting. |
| 691 | */ |
| 692 | if (!string_is_empty(&manifest_vm->kernel_filename)) { |
| 693 | if (!load_kernel(stage1_locked, mem_begin, mem_end, manifest_vm, |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 694 | cpio, ppool, &kernel_size)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 695 | dlog_error("Unable to load kernel.\n"); |
| 696 | return false; |
| 697 | } |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 700 | has_fdt = !string_is_empty(&manifest_vm->secondary.fdt_filename); |
| 701 | if (has_fdt) { |
| 702 | /* |
| 703 | * Ensure that the FDT does not overwrite the kernel or overlap |
| 704 | * its page, for the FDT to start at a page boundary. |
| 705 | */ |
| 706 | const size_t fdt_max_size = |
| 707 | mem_size - align_up(kernel_size, PAGE_SIZE); |
| 708 | |
| 709 | size_t fdt_allocated_size; |
| 710 | |
| 711 | if (!load_secondary_fdt(stage1_locked, mem_end, fdt_max_size, |
| 712 | manifest_vm, cpio, ppool, &fdt_addr, |
| 713 | &fdt_allocated_size)) { |
| 714 | dlog_error("Unable to load FDT.\n"); |
| 715 | return false; |
| 716 | } |
| 717 | |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 718 | if (manifest_vm->is_ffa_partition) { |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 719 | ffa_setup_parse_partition_manifest( |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 720 | stage1_locked, fdt_addr, fdt_allocated_size, |
J-Alves | 77b6f4f | 2023-03-15 11:34:49 +0000 | [diff] [blame] | 721 | manifest_vm, boot_params, ppool); |
Raghu Krishnamurthy | b49549e | 2021-07-02 08:27:38 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 724 | if (!fdt_patch_mem(stage1_locked, fdt_addr, fdt_allocated_size, |
| 725 | mem_begin, mem_end, ppool)) { |
| 726 | dlog_error("Unable to patch FDT.\n"); |
| 727 | return false; |
| 728 | } |
| 729 | } |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 730 | /* |
| 731 | * An S-EL0 partition must contain only 1 vCPU (UP migratable) per the |
| 732 | * FF-A 1.0 spec. |
| 733 | */ |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 734 | CHECK(!is_el0_partition || manifest_vm->secondary.vcpu_count == 1); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 735 | |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 736 | if (!vm_init_next(manifest_vm->secondary.vcpu_count, ppool, &vm, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 737 | is_el0_partition, |
| 738 | manifest_vm->partition.dma_device_count)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 739 | dlog_error("Unable to initialise VM.\n"); |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 743 | vm_locked = vm_lock(vm); |
| 744 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 745 | /* |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 746 | * Grant the VM access to the memory. For VM's we mark all memory in |
| 747 | * stage-2 tables as RWX and the VM can control permissions using |
| 748 | * stage-1 translations. For S-EL0 partitions, hafnium maps the entire |
| 749 | * region of memory for the partition as RX. The partition is then |
| 750 | * expected to perform its owns relocations and call the FFA_MEM_PERM_* |
| 751 | * API's to change permissions on its image layout. |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 752 | */ |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 753 | if (is_el0_partition) { |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 754 | map_mode = MM_MODE_R | MM_MODE_X | MM_MODE_USER | MM_MODE_NG; |
| 755 | } else { |
| 756 | map_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 757 | } |
Raghu Krishnamurthy | 2b80169 | 2021-07-18 17:46:43 -0700 | [diff] [blame] | 758 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 759 | if (!vm_identity_map(vm_locked, mem_begin, mem_end, map_mode, ppool, |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 760 | &partition_primary_ep)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 761 | dlog_error("Unable to initialise memory.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 762 | ret = false; |
| 763 | goto out; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 766 | if (manifest_vm->is_ffa_partition) { |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 767 | if (!ffa_map_memory_regions(manifest_vm, vm_locked, |
J-Alves | d8a1d36 | 2023-03-08 11:15:28 +0000 | [diff] [blame] | 768 | primary_vm_locked, is_el0_partition, |
| 769 | ppool)) { |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 770 | ret = false; |
| 771 | goto out; |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 772 | } |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 773 | partition_primary_ep = ipa_add( |
| 774 | partition_primary_ep, manifest_vm->partition.ep_offset); |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 775 | |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 776 | /* |
| 777 | * If MP endpoints dont specify the entrypoint for secondary |
| 778 | * execution contexts, FF-A spec advises SPMC to reuse the |
| 779 | * entrypoint computed for primary execution context. |
| 780 | */ |
| 781 | vm->secondary_ep = partition_primary_ep; |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 784 | /* |
| 785 | * Map hypervisor into the VM's page table. The hypervisor pages will |
| 786 | * not be accessible from EL0 since it will not be marked for user |
| 787 | * access. |
| 788 | * TODO: Map only the exception vectors and data that exception vectors |
| 789 | * require and not the entire hypervisor. This helps with speculative |
| 790 | * side-channel attacks. |
| 791 | */ |
Daniel Boulby | e6df345 | 2022-07-14 18:14:26 +0100 | [diff] [blame] | 792 | if (is_el0_partition) { |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 793 | CHECK(vm_identity_map(vm_locked, layout_text_begin(), |
| 794 | layout_text_end(), MM_MODE_X, ppool, |
| 795 | NULL)); |
| 796 | |
| 797 | CHECK(vm_identity_map(vm_locked, layout_rodata_begin(), |
| 798 | layout_rodata_end(), MM_MODE_R, ppool, |
| 799 | NULL)); |
| 800 | |
| 801 | CHECK(vm_identity_map(vm_locked, layout_data_begin(), |
| 802 | layout_data_end(), MM_MODE_R | MM_MODE_W, |
| 803 | ppool, NULL)); |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 804 | |
| 805 | CHECK(arch_stack_mm_init(mm_lock_ptable_unsafe(&vm->ptable), |
| 806 | ppool)); |
| 807 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 808 | plat_console_mm_init(mm_lock_ptable_unsafe(&vm->ptable), ppool); |
| 809 | } |
| 810 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 811 | if (!load_common(stage1_locked, vm_locked, manifest_vm, ppool)) { |
| 812 | ret = false; |
| 813 | goto out; |
| 814 | } |
| 815 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 816 | dlog_info("Loaded with %u vCPUs, entry at %#lx.\n", |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 817 | manifest_vm->secondary.vcpu_count, pa_addr(mem_begin)); |
| 818 | |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 819 | vcpu = vm_get_vcpu(vm, 0); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 820 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 821 | vcpu_locked = vcpu_lock(vcpu); |
Madhukar Pappireddy | 046dad0 | 2022-06-21 18:43:33 -0500 | [diff] [blame] | 822 | |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 823 | if (has_fdt) { |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 824 | vcpu_secondary_reset_and_start( |
| 825 | vcpu_locked, partition_primary_ep, pa_addr(fdt_addr)); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 826 | } else { |
| 827 | /* |
| 828 | * Without an FDT, secondary VMs expect the memory size to be |
| 829 | * passed in register x0, which is what |
| 830 | * vcpu_secondary_reset_and_start does in this case. |
| 831 | */ |
Madhukar Pappireddy | 958c841 | 2024-11-25 09:54:17 -0600 | [diff] [blame] | 832 | vcpu_secondary_reset_and_start(vcpu_locked, |
| 833 | partition_primary_ep, mem_size); |
Fuad Tabba | 50469e0 | 2020-06-30 15:14:28 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 836 | vcpu_unlock(&vcpu_locked); |
| 837 | |
Jens Wiklander | b697ad0 | 2022-11-04 10:15:03 +0100 | [diff] [blame] | 838 | /* |
| 839 | * For all vCPUs, |
| 840 | * in a VM: enable the notification pending virtual interrupt if |
| 841 | * requested in the manifest. |
| 842 | * in a SP: enable the NPI and managed exit virtual interrupts if |
| 843 | * requested in the manifest. For a S-EL0 partition, enable |
| 844 | * the virtual interrupts IDs matching the secure physical |
| 845 | * interrupt IDs declared in device regions. |
| 846 | */ |
| 847 | for (n = 0; n < manifest_vm->secondary.vcpu_count; n++) { |
| 848 | vcpu = vm_get_vcpu(vm, n); |
| 849 | vcpu_locked = vcpu_lock(vcpu); |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 850 | ffa_interrupts_enable_virtual_interrupts(vcpu_locked, |
| 851 | vm_locked); |
Jens Wiklander | b697ad0 | 2022-11-04 10:15:03 +0100 | [diff] [blame] | 852 | vcpu_unlock(&vcpu_locked); |
| 853 | } |
| 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 | 0504692 | 2023-03-09 15:48:40 +0100 | [diff] [blame] | 941 | static bool init_other_world_vm(const struct boot_params *params, |
| 942 | struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 943 | { |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 944 | struct vm *other_world_vm; |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 945 | size_t i; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 946 | |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 947 | /* |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 948 | * Initialise the dummy VM which represents the opposite world: |
| 949 | * -TrustZone (or the SPMC) when running the Hypervisor |
| 950 | * -the Hypervisor when running TZ/SPMC |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 951 | */ |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 952 | other_world_vm = vm_init(HF_OTHER_WORLD_ID, MAX_CPUS, ppool, false, 0); |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 953 | CHECK(other_world_vm != NULL); |
| 954 | |
| 955 | for (i = 0; i < MAX_CPUS; i++) { |
| 956 | struct vcpu *vcpu = vm_get_vcpu(other_world_vm, i); |
| 957 | struct cpu *cpu = cpu_find_index(i); |
| 958 | |
| 959 | vcpu->cpu = cpu; |
| 960 | } |
Andrew Walbran | 9daa57e | 2019-09-27 13:33:20 +0100 | [diff] [blame] | 961 | |
Olivier Deprez | 0504692 | 2023-03-09 15:48:40 +0100 | [diff] [blame] | 962 | return arch_other_world_vm_init(other_world_vm, params, ppool); |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Loads alls VMs from the manifest. |
| 967 | */ |
| 968 | bool load_vms(struct mm_stage1_locked stage1_locked, |
| 969 | const struct manifest *manifest, const struct memiter *cpio, |
| 970 | const struct boot_params *params, |
| 971 | struct boot_params_update *update, struct mpool *ppool) |
| 972 | { |
| 973 | struct vm *primary; |
| 974 | struct mem_range mem_ranges_available[MAX_MEM_RANGES]; |
| 975 | struct vm_locked primary_vm_locked; |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 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 | |
Olivier Deprez | 0504692 | 2023-03-09 15:48:40 +0100 | [diff] [blame] | 991 | if (!init_other_world_vm(params, ppool)) { |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 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. */ |
J-Alves | 217e1a2 | 2025-03-20 19:19:47 +0000 | [diff] [blame] | 1005 | for (size_t i = 0UL; 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 | |
J-Alves | 217e1a2 | 2025-03-20 19:19:47 +0000 | [diff] [blame] | 1013 | for (size_t i = 0UL; 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]; |
J-Alves | 217e1a2 | 2025-03-20 19:19:47 +0000 | [diff] [blame] | 1015 | ffa_id_t vm_id = (ffa_id_t)(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, |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1025 | manifest_vm->debug_name.data); |
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)) { |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1039 | dlog_error("Not enough memory (%lu bytes).\n", |
| 1040 | mem_size); |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1041 | continue; |
| 1042 | } |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1043 | |
Manish Pandey | 2145c21 | 2020-05-01 16:04:22 +0100 | [diff] [blame] | 1044 | if (!load_secondary(stage1_locked, primary_vm_locked, |
| 1045 | secondary_mem_begin, secondary_mem_end, |
J-Alves | 77b6f4f | 2023-03-15 11:34:49 +0000 | [diff] [blame] | 1046 | manifest_vm, params, cpio, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1047 | dlog_error("Unable to load VM.\n"); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | /* Deny the primary VM access to this memory. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1052 | if (!vm_unmap(primary_vm_locked, secondary_mem_begin, |
| 1053 | secondary_mem_end, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1054 | dlog_error( |
| 1055 | "Unable to unmap secondary VM from primary " |
| 1056 | "VM.\n"); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1057 | success = false; |
| 1058 | break; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 1059 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1062 | vm_unlock(&primary_vm_locked); |
| 1063 | |
| 1064 | if (!success) { |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 1068 | /* |
| 1069 | * Add newly reserved areas to update params by looking at the |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1070 | * difference between the available ranges from the original params and |
| 1071 | * the updated mem_ranges_available. We assume that the number and order |
| 1072 | * 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] | 1073 | * above only make them smaller. |
| 1074 | */ |
Andrew Walbran | 34ce72e | 2018-09-13 16:47:44 +0100 | [diff] [blame] | 1075 | return update_reserved_ranges(update, params->mem_ranges, |
| 1076 | mem_ranges_available, |
| 1077 | params->mem_ranges_count); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1078 | } |