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