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