Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2 | * Copyright 2022 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/api.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 10 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 11 | #include "hf/arch/cpu.h" |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 12 | #include "hf/arch/ffa.h" |
J-Alves | 0a21db1 | 2024-03-28 12:43:30 +0000 | [diff] [blame] | 13 | #include "hf/arch/memcpy_trapped.h" |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 14 | #include "hf/arch/mm.h" |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 15 | #include "hf/arch/other_world.h" |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 16 | #include "hf/arch/plat/ffa.h" |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 17 | #include "hf/arch/timer.h" |
Olivier Deprez | 764fd2e | 2020-07-29 15:14:09 +0200 | [diff] [blame] | 18 | #include "hf/arch/vm.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 19 | |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 20 | #include "hf/bits.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 21 | #include "hf/check.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 22 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 23 | #include "hf/ffa_internal.h" |
| 24 | #include "hf/ffa_memory.h" |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 25 | #include "hf/ffa_v1_0.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 26 | #include "hf/mm.h" |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 27 | #include "hf/plat/console.h" |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 28 | #include "hf/plat/interrupts.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 29 | #include "hf/spinlock.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 30 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 31 | #include "hf/std.h" |
Karl Meakin | c5cebbc | 2024-06-17 11:30:27 +0100 | [diff] [blame] | 32 | #include "hf/vcpu.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 33 | #include "hf/vm.h" |
| 34 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 35 | #include "vmapi/hf/call.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 36 | #include "vmapi/hf/ffa.h" |
J-Alves | 126ab50 | 2022-09-29 11:37:33 +0100 | [diff] [blame] | 37 | #include "vmapi/hf/ffa_v1_0.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 38 | |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 39 | static_assert(sizeof(struct ffa_partition_info_v1_0) == 8, |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 40 | "Partition information descriptor size doesn't match the one in " |
| 41 | "the FF-A 1.0 EAC specification, Table 82."); |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 42 | static_assert(sizeof(struct ffa_partition_info) == 24, |
| 43 | "Partition information descriptor size doesn't match the one in " |
| 44 | "the FF-A 1.1 BETA0 EAC specification, Table 13.34."); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 45 | static_assert((sizeof(struct ffa_partition_info) & 7) == 0, |
| 46 | "Partition information descriptor must be a multiple of 8 bytes" |
| 47 | " for ffa_partition_info_get_regs to work correctly. Information" |
| 48 | " from this structure are returned in 8 byte registers and the" |
| 49 | " count of 8 byte registers is returned by the ABI."); |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 50 | /* |
| 51 | * To eliminate the risk of deadlocks, we define a partial order for the |
| 52 | * acquisition of locks held concurrently by the same physical CPU. Our current |
| 53 | * ordering requirements are as follows: |
| 54 | * |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 55 | * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 56 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 57 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 58 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 59 | */ |
| 60 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 61 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 62 | "Currently, a page is mapped for the send and receive buffers so " |
| 63 | "the maximum request is the size of a page."); |
| 64 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 65 | static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE, |
| 66 | "The page pool entry size must be at least as big as the mailbox " |
| 67 | "size, so that memory region descriptors can be copied from the " |
| 68 | "mailbox for memory sharing."); |
| 69 | |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 70 | /* |
| 71 | * Maximum ffa_partition_info entries that can be returned by an invocation |
| 72 | * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available |
| 73 | * registers/args in struct ffa_value divided by size of struct |
| 74 | * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e. |
| 75 | * 15 uint64_t fields. For FF-A v1.1, this value should be 5. |
| 76 | */ |
| 77 | #define MAX_INFO_REGS_ENTRIES_PER_CALL \ |
| 78 | ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info)) |
| 79 | static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5, |
| 80 | "FF-A v1.1 supports no more than 5 entries" |
| 81 | " per FFA_PARTITION_INFO_GET_REGS64 calls"); |
| 82 | |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 83 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 86 | * Initialises the API page pool by taking ownership of the contents of the |
| 87 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 88 | */ |
| 89 | void api_init(struct mpool *ppool) |
| 90 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 91 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 94 | /** |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 95 | * Get target VM vCPU: |
| 96 | * If VM is UP then return first vCPU. |
| 97 | * If VM is MP then return vCPU whose index matches current CPU index. |
| 98 | */ |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 99 | struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current) |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 100 | { |
| 101 | ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu); |
| 102 | struct vcpu *vcpu = NULL; |
| 103 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 104 | CHECK((vm != NULL) && (current != NULL)); |
| 105 | |
Karl Meakin | 8204182 | 2024-05-20 11:14:34 +0100 | [diff] [blame] | 106 | if (vm_is_up(vm)) { |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 107 | vcpu = vm_get_vcpu(vm, 0); |
| 108 | } else if (current_cpu_index < vm->vcpu_count) { |
| 109 | vcpu = vm_get_vcpu(vm, current_cpu_index); |
| 110 | } |
| 111 | |
| 112 | return vcpu; |
| 113 | } |
| 114 | |
| 115 | /** |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 116 | * Switches the physical CPU back to the corresponding vCPU of the VM whose ID |
| 117 | * is given as argument of the function. |
| 118 | * |
| 119 | * Called to change the context between SPs for direct messaging (when Hafnium |
| 120 | * is SPMC), and on the context of the remaining 'api_switch_to_*' functions. |
| 121 | * |
| 122 | * This function works for partitions that are: |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 123 | * - UP migratable. |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 124 | * - MP with pinned Execution Contexts. |
| 125 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 126 | struct vcpu *api_switch_to_vm(struct vcpu_locked current_locked, |
| 127 | struct ffa_value to_ret, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 128 | enum vcpu_state vcpu_state, ffa_id_t to_id) |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 129 | { |
| 130 | struct vm *to_vm = vm_find(to_id); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 131 | struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current_locked.vcpu); |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 132 | |
| 133 | CHECK(next != NULL); |
| 134 | |
| 135 | /* Set the return value for the target VM. */ |
| 136 | arch_regs_set_retval(&next->regs, to_ret); |
| 137 | |
| 138 | /* Set the current vCPU state. */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 139 | current_locked.vcpu->state = vcpu_state; |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 140 | |
| 141 | return next; |
| 142 | } |
| 143 | |
| 144 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 145 | * Switches the physical CPU back to the corresponding vCPU of the primary VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 146 | * |
| 147 | * This triggers the scheduling logic to run. Run in the context of secondary VM |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 148 | * to cause FFA_RUN to return and the primary VM to regain control of the CPU. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 149 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 150 | struct vcpu *api_switch_to_primary(struct vcpu_locked current_locked, |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 151 | struct ffa_value primary_ret, |
| 152 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 153 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 154 | return api_switch_to_vm(current_locked, primary_ret, secondary_state, |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 155 | HF_PRIMARY_VM_ID); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 159 | * Choose next vCPU to run to be the counterpart vCPU in the other |
| 160 | * world (run the normal world if currently running in the secure |
| 161 | * world). Set current vCPU state to the given vcpu_state parameter. |
| 162 | * Set FF-A return values to the target vCPU in the other world. |
| 163 | * |
| 164 | * Called in context of a direct message response from a secure |
| 165 | * partition to a VM. |
| 166 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 167 | struct vcpu *api_switch_to_other_world(struct vcpu_locked current_locked, |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 168 | struct ffa_value other_world_ret, |
| 169 | enum vcpu_state vcpu_state) |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 170 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 171 | return api_switch_to_vm(current_locked, other_world_ret, vcpu_state, |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 172 | HF_OTHER_WORLD_ID); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 176 | * Returns true if the given vCPU is executing in context of an |
| 177 | * FFA_MSG_SEND_DIRECT_REQ invocation. |
| 178 | */ |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 179 | bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked) |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 180 | { |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 181 | return locked.vcpu->direct_request_origin.vm_id != HF_INVALID_VM_ID; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 185 | * Returns true if the VM owning the given vCPU is supporting managed exit and |
| 186 | * the vCPU is currently processing a managed exit. |
| 187 | */ |
| 188 | static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked) |
| 189 | { |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 190 | return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) && |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 191 | vcpu_locked.vcpu->processing_managed_exit); |
| 192 | } |
| 193 | |
| 194 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 195 | * Returns to the primary VM and signals that the vCPU still has work to do so. |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 196 | */ |
| 197 | struct vcpu *api_preempt(struct vcpu *current) |
| 198 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 199 | struct vcpu_locked current_locked; |
| 200 | struct vcpu *next; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 201 | struct ffa_value ret = { |
| 202 | .func = FFA_INTERRUPT_32, |
Olivier Deprez | 0d725f5 | 2022-07-06 14:20:27 +0200 | [diff] [blame] | 203 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 206 | current_locked = vcpu_lock(current); |
| 207 | next = api_switch_to_primary(current_locked, ret, VCPU_STATE_PREEMPTED); |
| 208 | vcpu_unlock(¤t_locked); |
| 209 | |
| 210 | return next; |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 214 | * Puts the current vCPU in wait for interrupt mode, and returns to the primary |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 215 | * VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 216 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 217 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 218 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 219 | struct vcpu_locked current_locked; |
| 220 | struct vcpu *next; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 221 | struct ffa_value ret = { |
| 222 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 223 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 224 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 225 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 226 | current_locked = vcpu_lock(current); |
| 227 | next = api_switch_to_primary(current_locked, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 228 | VCPU_STATE_BLOCKED_INTERRUPT); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 229 | vcpu_unlock(¤t_locked); |
| 230 | |
| 231 | return next; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 235 | * Puts the current vCPU in off mode, and returns to the primary VM. |
| 236 | */ |
| 237 | struct vcpu *api_vcpu_off(struct vcpu *current) |
| 238 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 239 | struct vcpu_locked current_locked; |
| 240 | struct vcpu *next; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 241 | struct ffa_value ret = { |
| 242 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 243 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 244 | }; |
| 245 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 246 | current_locked = vcpu_lock(current); |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 247 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 248 | next = api_switch_to_primary(current_locked, ret, VCPU_STATE_OFF); |
| 249 | vcpu_unlock(¤t_locked); |
| 250 | |
| 251 | return next; |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 255 | * The current vCPU is blocked on some resource and needs to relinquish |
| 256 | * control back to the execution context of the endpoint that originally |
| 257 | * allocated cycles to it. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 258 | */ |
Madhukar Pappireddy | 184501c | 2023-05-23 17:24:06 -0500 | [diff] [blame] | 259 | struct ffa_value api_yield(struct vcpu *current, struct vcpu **next, |
| 260 | struct ffa_value *args) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 261 | { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 262 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 263 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 264 | bool transition_allowed; |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 265 | enum vcpu_state next_state = VCPU_STATE_RUNNING; |
Madhukar Pappireddy | 184501c | 2023-05-23 17:24:06 -0500 | [diff] [blame] | 266 | uint32_t timeout_low = 0; |
| 267 | uint32_t timeout_high = 0; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 268 | struct vcpu_locked next_locked = (struct vcpu_locked){ |
| 269 | .vcpu = NULL, |
| 270 | }; |
Madhukar Pappireddy | 184501c | 2023-05-23 17:24:06 -0500 | [diff] [blame] | 271 | |
| 272 | if (args != NULL) { |
| 273 | if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U || |
| 274 | args->arg7 != 0U) { |
| 275 | dlog_error( |
| 276 | "Parameters passed through registers X4-X7 " |
| 277 | "must be zero\n"); |
| 278 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 279 | } |
| 280 | timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF; |
| 281 | timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF; |
| 282 | } |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 283 | |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 284 | if (vm_is_primary(current->vm)) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 285 | /* NOOP on the primary as it makes the scheduling decisions. */ |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 286 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 289 | current_locked = vcpu_lock(current); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 290 | transition_allowed = plat_ffa_check_runtime_state_transition( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 291 | current_locked, current->vm->id, HF_INVALID_VM_ID, next_locked, |
| 292 | FFA_YIELD_32, &next_state); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 293 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 294 | if (!transition_allowed) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 295 | ret = ffa_error(FFA_DENIED); |
| 296 | goto out; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Madhukar Pappireddy | 6576407 | 2023-05-23 17:18:58 -0500 | [diff] [blame] | 299 | /* |
| 300 | * The current vCPU is expected to move to BLOCKED state. However, |
| 301 | * under certain circumstances, it is allowed for the current vCPU |
| 302 | * to be resumed immediately without ever moving to BLOCKED state. One |
| 303 | * such scenario occurs when an SP's execution context attempts to |
| 304 | * yield cycles while handling secure interrupt. Refer to the comments |
| 305 | * in the SPMC variant of the plat_ffa_yield_prepare function. |
| 306 | */ |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 307 | assert(!vm_id_is_current_world(current->vm->id) || |
| 308 | next_state == VCPU_STATE_BLOCKED); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 309 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 310 | ret = plat_ffa_yield_prepare(current_locked, next, timeout_low, |
| 311 | timeout_high); |
| 312 | out: |
| 313 | vcpu_unlock(¤t_locked); |
| 314 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 318 | * Switches to the primary so that it can switch to the target, or kick it if it |
| 319 | * is already running on a different physical CPU. |
| 320 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 321 | static struct vcpu *api_wake_up_locked(struct vcpu_locked current_locked, |
| 322 | struct vcpu *target_vcpu) |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 323 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 324 | struct ffa_value ret = { |
Andrew Walbran | 7e82460 | 2020-10-22 16:51:40 +0100 | [diff] [blame] | 325 | .func = FFA_INTERRUPT_32, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 326 | .arg1 = ffa_vm_vcpu(target_vcpu->vm->id, |
| 327 | vcpu_index(target_vcpu)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 328 | }; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 329 | |
| 330 | return api_switch_to_primary(current_locked, ret, VCPU_STATE_BLOCKED); |
| 331 | } |
| 332 | |
| 333 | struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu) |
| 334 | { |
| 335 | struct vcpu_locked current_locked; |
| 336 | struct vcpu *next; |
| 337 | |
| 338 | current_locked = vcpu_lock(current); |
| 339 | next = api_wake_up_locked(current_locked, target_vcpu); |
| 340 | vcpu_unlock(¤t_locked); |
| 341 | |
| 342 | return next; |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 346 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 347 | */ |
| 348 | struct vcpu *api_abort(struct vcpu *current) |
| 349 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 350 | struct ffa_value ret = ffa_error(FFA_ABORTED); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 351 | struct vcpu_locked current_locked; |
| 352 | struct vcpu *next; |
Madhukar Pappireddy | 9e09d7a | 2023-08-08 14:53:49 -0500 | [diff] [blame] | 353 | struct vm_locked vm_locked; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 354 | |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 355 | dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 356 | vcpu_index(current)); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 357 | |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 358 | if (vm_is_primary(current->vm)) { |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 359 | /* TODO: what to do when the primary aborts? */ |
| 360 | for (;;) { |
| 361 | /* Do nothing. */ |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | atomic_store_explicit(¤t->vm->aborting, true, |
| 366 | memory_order_relaxed); |
| 367 | |
Madhukar Pappireddy | 9e09d7a | 2023-08-08 14:53:49 -0500 | [diff] [blame] | 368 | vm_locked = vm_lock(current->vm); |
| 369 | plat_ffa_free_vm_resources(vm_locked); |
| 370 | vm_unlock(&vm_locked); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 371 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 372 | current_locked = vcpu_lock(current); |
| 373 | next = api_switch_to_primary(current_locked, ret, VCPU_STATE_ABORTED); |
| 374 | vcpu_unlock(¤t_locked); |
| 375 | |
| 376 | return next; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 379 | /* |
| 380 | * Format the partition info descriptors according to the version supported |
| 381 | * by the endpoint and return the size of the array created. |
| 382 | */ |
| 383 | static struct ffa_value send_versioned_partition_info_descriptors( |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 384 | struct vm_locked vm_locked, struct ffa_partition_info *partitions, |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 385 | uint32_t vm_count) |
| 386 | { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 387 | struct vm *vm = vm_locked.vm; |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 388 | enum ffa_version version = vm->ffa_version; |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 389 | uint32_t partition_info_size; |
| 390 | uint32_t buffer_size; |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 391 | struct ffa_value ret; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 392 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 393 | /* Acquire receiver's RX buffer. */ |
| 394 | if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) { |
| 395 | dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id); |
| 396 | return ret; |
| 397 | } |
| 398 | |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 399 | if (vm_is_mailbox_busy(vm_locked)) { |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 400 | /* |
| 401 | * Can't retrieve memory information if the mailbox is not |
| 402 | * available. |
| 403 | */ |
| 404 | dlog_verbose("RX buffer not ready.\n"); |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 405 | return ffa_error(FFA_BUSY); |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 408 | if (version == FFA_VERSION_1_0) { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 409 | struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv; |
| 410 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 411 | partition_info_size = sizeof(struct ffa_partition_info_v1_0); |
| 412 | buffer_size = partition_info_size * vm_count; |
| 413 | if (buffer_size > HF_MAILBOX_SIZE) { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 414 | dlog_error( |
| 415 | "Partition information does not fit in the " |
J-Alves | 0a21db1 | 2024-03-28 12:43:30 +0000 | [diff] [blame] | 416 | "VM's RX buffer.\n"); |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 417 | return ffa_error(FFA_NO_MEMORY); |
| 418 | } |
| 419 | |
| 420 | for (uint32_t i = 0; i < vm_count; i++) { |
| 421 | /* |
| 422 | * Populate the VM's RX buffer with the partition |
Kathleen Capella | 402fa85 | 2022-11-09 16:16:51 -0500 | [diff] [blame] | 423 | * information. Clear properties bits that must be zero |
| 424 | * according to DEN0077A FF-A v1.0 REL Table 8.25. |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 425 | */ |
| 426 | recv_mailbox[i].vm_id = partitions[i].vm_id; |
| 427 | recv_mailbox[i].vcpu_count = partitions[i].vcpu_count; |
Kathleen Capella | 402fa85 | 2022-11-09 16:16:51 -0500 | [diff] [blame] | 428 | recv_mailbox[i].properties = |
| 429 | partitions[i].properties & |
| 430 | ~FFA_PARTITION_v1_0_RES_MASK; |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | } else { |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 434 | partition_info_size = sizeof(struct ffa_partition_info); |
| 435 | buffer_size = partition_info_size * vm_count; |
J-Alves | 0a21db1 | 2024-03-28 12:43:30 +0000 | [diff] [blame] | 436 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 437 | if (buffer_size > HF_MAILBOX_SIZE) { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 438 | dlog_error( |
| 439 | "Partition information does not fit in the " |
J-Alves | 0a824e9 | 2024-04-26 16:20:12 +0100 | [diff] [blame] | 440 | "VM's RX buffer.\n"); |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 441 | return ffa_error(FFA_NO_MEMORY); |
| 442 | } |
| 443 | |
J-Alves | 0a21db1 | 2024-03-28 12:43:30 +0000 | [diff] [blame] | 444 | /* |
| 445 | * Populate the VM's RX buffer with the partition information. |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 446 | */ |
J-Alves | 0a21db1 | 2024-03-28 12:43:30 +0000 | [diff] [blame] | 447 | if (!memcpy_trapped(vm->mailbox.recv, HF_MAILBOX_SIZE, |
| 448 | partitions, buffer_size)) { |
| 449 | dlog_error( |
| 450 | "%s: Failed to copy ffa_partition_info " |
| 451 | "descriptor\n", |
| 452 | __func__); |
| 453 | return ffa_error(FFA_ABORTED); |
| 454 | } |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 457 | vm->mailbox.recv_size = buffer_size; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 458 | |
| 459 | /* Sender is Hypervisor in the normal world (TEE in secure world). */ |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 460 | vm->mailbox.recv_sender = HF_VM_ID_BASE; |
| 461 | vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 462 | vm->mailbox.state = MAILBOX_STATE_FULL; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 463 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 464 | /* |
| 465 | * Return the count of partition information descriptors in w2 |
| 466 | * and the size of the descriptors in w3. |
| 467 | */ |
| 468 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 469 | .arg2 = vm_count, |
| 470 | .arg3 = partition_info_size}; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 471 | } |
| 472 | |
J-Alves | 7fb0fdb | 2024-10-09 11:28:07 +0100 | [diff] [blame] | 473 | /** |
| 474 | * Set properties accoridng to the version of the partition, and the FF-A |
| 475 | * features it supports. |
| 476 | */ |
| 477 | static ffa_partition_properties_t api_ffa_partitions_info_get_properties( |
| 478 | ffa_id_t caller_id, struct vm *vm) |
| 479 | { |
| 480 | ffa_partition_properties_t properties; |
| 481 | |
| 482 | properties = plat_ffa_partition_properties(caller_id, vm); |
| 483 | properties |= FFA_PARTITION_AARCH64_EXEC; |
| 484 | |
| 485 | if (vm->ffa_version >= FFA_VERSION_1_1) { |
| 486 | properties |= vm_are_notifications_enabled(vm) |
| 487 | ? FFA_PARTITION_NOTIFICATION |
| 488 | : 0; |
| 489 | properties |= vm->messaging_method & FFA_PARTITION_INDIRECT_MSG; |
| 490 | } |
| 491 | |
| 492 | return properties; |
| 493 | } |
| 494 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 495 | static void api_ffa_fill_partition_info( |
J-Alves | 7fb0fdb | 2024-10-09 11:28:07 +0100 | [diff] [blame] | 496 | struct ffa_partition_info *out_partition, struct vm *vm, |
| 497 | ffa_id_t caller_id) |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 498 | { |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 499 | out_partition->vm_id = vm->id; |
| 500 | out_partition->vcpu_count = vm->vcpu_count; |
J-Alves | 7fb0fdb | 2024-10-09 11:28:07 +0100 | [diff] [blame] | 501 | out_partition->properties = |
| 502 | api_ffa_partitions_info_get_properties(caller_id, vm); |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 503 | } |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 504 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 505 | /** |
| 506 | * Find VMs with UUID matching `uuid_to_find` , and fill `out_partitions` with |
| 507 | * partition infos. Returns number of VMs that matched. |
| 508 | * A null UUID matches against any VM. |
| 509 | * If `count_flag` is true, no partition infos are written to `out_partitions`, |
| 510 | * only the number of VMs that matched is returned. |
| 511 | */ |
| 512 | static ffa_vm_count_t api_ffa_fill_partitions_info_array( |
| 513 | struct ffa_partition_info out_partitions[], size_t out_partitions_len, |
J-Alves | 7fb0fdb | 2024-10-09 11:28:07 +0100 | [diff] [blame] | 514 | const struct ffa_uuid *uuid_to_find, bool count_flag, |
| 515 | ffa_id_t caller_id) |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 516 | { |
| 517 | ffa_vm_count_t vms_found = 0; |
| 518 | bool match_any = ffa_uuid_is_null(uuid_to_find); |
| 519 | |
| 520 | assert(vm_get_count() <= out_partitions_len); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * Iterate through the VMs to find the ones with a matching |
| 524 | * UUID. A Null UUID retrieves information for all VMs. |
| 525 | */ |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 526 | for (ffa_vm_count_t vm_idx = 0; vm_idx < vm_get_count(); vm_idx++) { |
| 527 | struct vm *vm = vm_find_index(vm_idx); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 528 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 529 | for (size_t uuid_idx = 0; uuid_idx < PARTITION_MAX_UUIDS; |
| 530 | uuid_idx++) { |
| 531 | struct ffa_uuid uuid = vm->uuids[uuid_idx]; |
| 532 | struct ffa_partition_info *out_partition = |
| 533 | &out_partitions[vms_found]; |
| 534 | |
Kathleen Capella | a1de3c5 | 2023-08-28 20:10:02 -0400 | [diff] [blame] | 535 | /* |
| 536 | * Null UUID indicates reaching the end of a |
| 537 | * partition's array of UUIDs. |
| 538 | */ |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 539 | if (ffa_uuid_is_null(&uuid)) { |
Kathleen Capella | a1de3c5 | 2023-08-28 20:10:02 -0400 | [diff] [blame] | 540 | break; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 543 | if (match_any || ffa_uuid_equal(uuid_to_find, &uuid)) { |
| 544 | vms_found++; |
| 545 | |
Kathleen Capella | a1de3c5 | 2023-08-28 20:10:02 -0400 | [diff] [blame] | 546 | if (count_flag) { |
| 547 | continue; |
| 548 | } |
| 549 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 550 | api_ffa_fill_partition_info(out_partition, vm, |
J-Alves | 7fb0fdb | 2024-10-09 11:28:07 +0100 | [diff] [blame] | 551 | caller_id); |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 552 | if (match_any) { |
| 553 | out_partition->uuid = uuid; |
Kathleen Capella | a1de3c5 | 2023-08-28 20:10:02 -0400 | [diff] [blame] | 554 | } |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 559 | return vms_found; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 560 | } |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 561 | |
| 562 | static inline void api_ffa_pack_vmid_count_props( |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 563 | uint64_t *xn, ffa_id_t vm_id, ffa_vcpu_count_t vcpu_count, |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 564 | ffa_partition_properties_t properties) |
| 565 | { |
| 566 | *xn = (uint64_t)vm_id; |
| 567 | *xn |= (uint64_t)vcpu_count << 16; |
| 568 | *xn |= (uint64_t)properties << 32; |
| 569 | } |
| 570 | |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 571 | /** |
Raghu Krishnamurthy | e74d653 | 2023-06-07 12:21:54 -0700 | [diff] [blame] | 572 | * This function forwards the FFA_PARTITION_INFO_GET_REGS ABI to the other world |
| 573 | * when hafnium is the hypervisor to determine the secure partitions. When |
| 574 | * hafnium is the SPMC, this function forwards the call to the SPMD to discover |
| 575 | * SPMD logical partitions. The function returns true when partition information |
| 576 | * is filled in the partitions array and false if there are errors. Note that |
| 577 | * the SPMD and SPMC may return an FF-A error code of FFA_NOT_SUPPORTED when |
| 578 | * there are no SPMD logical partitions or no secure partitions respectively, |
| 579 | * and this is not considered a failure of the forwarded call. A caller is |
| 580 | * expected to check the return value before consuming the information in the |
| 581 | * partitions array passed in and ret_count. |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 582 | */ |
Raghu Krishnamurthy | e74d653 | 2023-06-07 12:21:54 -0700 | [diff] [blame] | 583 | static bool api_ffa_partition_info_get_regs_forward( |
| 584 | const struct ffa_uuid *uuid, const uint16_t tag, |
| 585 | struct ffa_partition_info *partitions, uint16_t partitions_len, |
| 586 | ffa_vm_count_t *ret_count) |
| 587 | { |
| 588 | (void)tag; |
| 589 | struct ffa_value ret; |
| 590 | uint16_t last_index = UINT16_MAX; |
| 591 | uint16_t curr_index = 0; |
| 592 | uint16_t start_index = 0; |
| 593 | |
| 594 | if (!plat_ffa_partition_info_get_regs_forward_allowed()) { |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | while (start_index <= last_index) { |
| 599 | ret = ffa_partition_info_get_regs(uuid, start_index, 0); |
| 600 | if (ffa_func_id(ret) != FFA_SUCCESS_64) { |
| 601 | /* |
| 602 | * If there are no logical partitions, SPMD returns |
| 603 | * NOT_SUPPORTED, that is not an error. If there are no |
| 604 | * secure partitions the SPMC returns NOT_SUPPORTED. |
| 605 | */ |
| 606 | if ((ffa_func_id(ret) == FFA_ERROR_32) && |
| 607 | (ffa_error_code(ret) == FFA_NOT_SUPPORTED)) { |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | if (!api_ffa_fill_partition_info_from_regs( |
| 615 | ret, start_index, partitions, partitions_len, |
| 616 | ret_count)) { |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | last_index = ffa_partition_info_regs_get_last_idx(ret); |
| 621 | curr_index = ffa_partition_info_regs_get_curr_idx(ret); |
| 622 | start_index = curr_index + 1; |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 627 | bool api_ffa_fill_partition_info_from_regs( |
| 628 | struct ffa_value ret, uint16_t start_index, |
| 629 | struct ffa_partition_info *partitions, uint16_t partitions_len, |
| 630 | ffa_vm_count_t *ret_count) |
| 631 | { |
| 632 | uint16_t vm_count = *ret_count; |
| 633 | uint16_t curr_index = 0; |
| 634 | uint8_t num_entries = 0; |
| 635 | uint8_t idx = 0; |
| 636 | /* List of pointers to args in return value. */ |
| 637 | uint64_t *arg_ptrs[] = { |
| 638 | &ret.arg3, |
| 639 | &ret.arg4, |
| 640 | &ret.arg5, |
| 641 | &ret.arg6, |
| 642 | &ret.arg7, |
| 643 | &ret.extended_val.arg8, |
| 644 | &ret.extended_val.arg9, |
| 645 | &ret.extended_val.arg10, |
| 646 | &ret.extended_val.arg11, |
| 647 | &ret.extended_val.arg12, |
| 648 | &ret.extended_val.arg13, |
| 649 | &ret.extended_val.arg14, |
| 650 | &ret.extended_val.arg15, |
| 651 | &ret.extended_val.arg16, |
| 652 | &ret.extended_val.arg17, |
| 653 | }; |
| 654 | |
| 655 | if (vm_count > partitions_len) { |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Tags are currently unused in the implementation. Expect it to be |
| 661 | * zero since the implementation does not provide a tag when calling |
| 662 | * the FFA_PARTITION_INFO_GET_REGS ABI. |
| 663 | */ |
| 664 | assert(ffa_partition_info_regs_get_tag(ret) == 0); |
| 665 | |
| 666 | /* |
| 667 | * Hafnium expects the size of the returned descriptor to be equal to |
| 668 | * the size of the structure in the FF-A 1.1 specification. When future |
| 669 | * enhancements are made, this assert can be relaxed. |
| 670 | */ |
| 671 | assert(ffa_partition_info_regs_get_desc_size(ret) == |
| 672 | sizeof(struct ffa_partition_info)); |
| 673 | |
| 674 | curr_index = ffa_partition_info_regs_get_curr_idx(ret); |
| 675 | |
| 676 | /* FF-A 1.2 ALP0, section 14.9.2 Usage rule 7. */ |
| 677 | assert(start_index <= curr_index); |
| 678 | |
| 679 | num_entries = curr_index - start_index + 1; |
| 680 | if (num_entries > (partitions_len - vm_count) || |
| 681 | num_entries > MAX_INFO_REGS_ENTRIES_PER_CALL) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | while (num_entries) { |
| 686 | uint64_t info = *(arg_ptrs[(ptrdiff_t)(idx++)]); |
| 687 | uint64_t uuid_lo = *(arg_ptrs[(ptrdiff_t)(idx++)]); |
| 688 | uint64_t uuid_high = *(arg_ptrs[(ptrdiff_t)(idx++)]); |
| 689 | |
| 690 | partitions[vm_count].vm_id = info & 0xFFFF; |
| 691 | partitions[vm_count].vcpu_count = (info >> 16) & 0xFFFF; |
| 692 | partitions[vm_count].properties = (info >> 32); |
| 693 | partitions[vm_count].uuid.uuid[0] = uuid_lo & 0xFFFFFFFF; |
| 694 | partitions[vm_count].uuid.uuid[1] = |
| 695 | (uuid_lo >> 32) & 0xFFFFFFFF; |
| 696 | partitions[vm_count].uuid.uuid[2] = uuid_high & 0xFFFFFFFF; |
| 697 | partitions[vm_count].uuid.uuid[3] = |
| 698 | (uuid_high >> 32) & 0xFFFFFFFF; |
| 699 | vm_count++; |
| 700 | num_entries--; |
| 701 | } |
| 702 | |
| 703 | *ret_count = vm_count; |
| 704 | return true; |
| 705 | } |
| 706 | |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 707 | struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current, |
| 708 | const struct ffa_uuid *uuid, |
| 709 | const uint16_t start_index, |
| 710 | const uint16_t tag) |
| 711 | { |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 712 | struct vm *current_vm = current->vm; |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 713 | static struct ffa_partition_info partitions[2 * MAX_VMS]; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 714 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
| 715 | ffa_vm_count_t vm_count = 0; |
| 716 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 717 | uint16_t max_idx = 0; |
| 718 | uint16_t curr_idx = 0; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 719 | uint8_t num_entries_to_ret = 0; |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 720 | uint8_t arg_idx = 3; |
| 721 | |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 722 | /* list of pointers to args in return value */ |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 723 | uint64_t *arg_ptrs[] = { |
| 724 | &(ret).func, |
| 725 | &(ret).arg1, |
| 726 | &(ret).arg2, |
| 727 | &(ret).arg3, |
| 728 | &(ret).arg4, |
| 729 | &(ret).arg5, |
| 730 | &(ret).arg6, |
| 731 | &(ret).arg7, |
| 732 | &(ret).extended_val.arg8, |
| 733 | &(ret).extended_val.arg9, |
| 734 | &(ret).extended_val.arg10, |
| 735 | &(ret).extended_val.arg11, |
| 736 | &(ret).extended_val.arg12, |
| 737 | &(ret).extended_val.arg13, |
| 738 | &(ret).extended_val.arg14, |
| 739 | &(ret).extended_val.arg15, |
| 740 | &(ret).extended_val.arg16, |
| 741 | &(ret).extended_val.arg17, |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 742 | }; |
| 743 | |
| 744 | /* TODO: Add support for using tags */ |
| 745 | if (tag != 0) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 746 | dlog_error("Tag not 0. Unsupported tag. %d\n", tag); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 747 | return ffa_error(FFA_RETRY); |
| 748 | } |
| 749 | |
| 750 | memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions)); |
| 751 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 752 | vm_count = api_ffa_fill_partitions_info_array( |
| 753 | partitions, ARRAY_SIZE(partitions), uuid, false, |
| 754 | current_vm->id); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 755 | |
| 756 | /* If UUID is Null vm_count must not be zero at this stage. */ |
| 757 | CHECK(!uuid_is_null || vm_count != 0); |
| 758 | |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 759 | /* |
| 760 | * When running the Hypervisor: |
| 761 | * - If UUID is Null the Hypervisor forwards the query to the SPMC for |
| 762 | * it to fill with secure partitions information. |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 763 | * When running the SPMC, the SPMC forwards the call to the SPMD to |
| 764 | * discover any EL3 SPMD logical partitions, if the call came from an |
| 765 | * SP. Otherwise, the call is not forwarded. |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 766 | * TODO: Note that for this ABI, forwarding on every invocation when |
| 767 | * uuid is Null is inefficient,and if performance becomes a problem, |
| 768 | * this would be a good place to optimize using strategies such as |
| 769 | * caching info etc. For now, assuming this inefficiency is not a major |
| 770 | * issue. |
| 771 | * - If UUID is non-Null vm_count may be zero because the UUID matches |
| 772 | * a secure partition and the query is forwarded to the SPMC. |
| 773 | * When running the SPMC: |
| 774 | * - If UUID is non-Null and vm_count is zero it means there is no such |
| 775 | * partition identified in the system. |
| 776 | */ |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 777 | if (vm_id_is_current_world(current_vm->id)) { |
Raghu Krishnamurthy | e74d653 | 2023-06-07 12:21:54 -0700 | [diff] [blame] | 778 | if (!api_ffa_partition_info_get_regs_forward( |
| 779 | uuid, tag, partitions, ARRAY_SIZE(partitions), |
| 780 | &vm_count)) { |
Raghu Krishnamurthy | 2d2b0f4 | 2023-04-23 09:54:45 -0700 | [diff] [blame] | 781 | dlog_error( |
| 782 | "Failed to forward " |
| 783 | "ffa_partition_info_get_regs.\n"); |
| 784 | return ffa_error(FFA_DENIED); |
| 785 | } |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 786 | } |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * Unrecognized UUID: does not match any of the VMs (or SPs) |
| 790 | * and is not Null. |
| 791 | */ |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 792 | if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) { |
Olivier Deprez | 89da0a4 | 2023-09-01 17:38:33 +0200 | [diff] [blame] | 793 | dlog_verbose( |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 794 | "Invalid parameters. vm_count = %d (must not be zero " |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 795 | "or > %lu)\n", |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 796 | vm_count, ARRAY_SIZE(partitions)); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 797 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 798 | } |
| 799 | |
| 800 | if (start_index >= vm_count) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 801 | dlog_error( |
| 802 | "start index = %d vm_count = %d (start_index must be " |
| 803 | "less than vm_count)\n", |
| 804 | start_index, vm_count); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 805 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 806 | } |
| 807 | |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 808 | max_idx = vm_count - 1; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 809 | num_entries_to_ret = (max_idx - start_index) + 1; |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 810 | num_entries_to_ret = |
| 811 | MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 812 | curr_idx = start_index + num_entries_to_ret - 1; |
| 813 | assert(curr_idx <= max_idx); |
| 814 | |
| 815 | ret.func = FFA_SUCCESS_64; |
| 816 | ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48; |
| 817 | ret.arg2 |= curr_idx << 16; |
| 818 | ret.arg2 |= max_idx; |
| 819 | |
| 820 | if (num_entries_to_ret > 1) { |
| 821 | ret.extended_val.valid = 1; |
| 822 | } |
| 823 | |
| 824 | for (uint16_t idx = start_index; idx <= curr_idx; ++idx) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 825 | uint64_t *xn_0 = arg_ptrs[arg_idx++]; |
| 826 | uint64_t *xn_1 = arg_ptrs[arg_idx++]; |
| 827 | uint64_t *xn_2 = arg_ptrs[arg_idx++]; |
| 828 | |
| 829 | api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id, |
| 830 | partitions[idx].vcpu_count, |
| 831 | partitions[idx].properties); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 832 | if (uuid_is_null) { |
Karl Meakin | 9478e32 | 2024-09-23 17:47:09 +0100 | [diff] [blame] | 833 | ffa_uuid_to_u64x2(xn_1, xn_2, &partitions[idx].uuid); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 834 | } |
Raghu Krishnamurthy | cded31b | 2023-04-23 15:07:35 -0700 | [diff] [blame] | 835 | assert(arg_idx <= ARRAY_SIZE(arg_ptrs)); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | return ret; |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 839 | } |
| 840 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 841 | struct ffa_value api_ffa_partition_info_get(struct vcpu *current, |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 842 | const struct ffa_uuid *uuid, |
| 843 | const uint32_t flags) |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 844 | { |
| 845 | struct vm *current_vm = current->vm; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 846 | ffa_vm_count_t vm_count = 0; |
Olivier Deprez | 013f4d6 | 2022-11-21 15:46:20 +0100 | [diff] [blame] | 847 | bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) == |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 848 | FFA_PARTITION_COUNT_FLAG; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 849 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
Daniel Boulby | ce54184 | 2022-05-31 15:54:31 +0100 | [diff] [blame] | 850 | struct ffa_partition_info partitions[2 * MAX_VMS] = {0}; |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 851 | struct vm_locked vm_locked; |
| 852 | struct ffa_value ret; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 853 | |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 854 | /* Bits 31:1 Must Be Zero */ |
Olivier Deprez | 013f4d6 | 2022-11-21 15:46:20 +0100 | [diff] [blame] | 855 | if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) { |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 856 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 857 | } |
| 858 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 859 | vm_count = api_ffa_fill_partitions_info_array( |
| 860 | partitions, ARRAY_SIZE(partitions), uuid, count_flag, |
| 861 | current_vm->id); |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 862 | |
Olivier Deprez | e562e54 | 2020-06-11 17:31:54 +0200 | [diff] [blame] | 863 | /* If UUID is Null vm_count must not be zero at this stage. */ |
| 864 | CHECK(!uuid_is_null || vm_count != 0); |
| 865 | |
| 866 | /* |
| 867 | * When running the Hypervisor: |
| 868 | * - If UUID is Null the Hypervisor forwards the query to the SPMC for |
| 869 | * it to fill with secure partitions information. |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 870 | * - If UUID is non-Null vm_count may be zero because the UUID matches |
Olivier Deprez | e562e54 | 2020-06-11 17:31:54 +0200 | [diff] [blame] | 871 | * a secure partition and the query is forwarded to the SPMC. |
| 872 | * When running the SPMC: |
| 873 | * - If UUID is non-Null and vm_count is zero it means there is no such |
| 874 | * partition identified in the system. |
| 875 | */ |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 876 | plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count); |
Olivier Deprez | e562e54 | 2020-06-11 17:31:54 +0200 | [diff] [blame] | 877 | |
| 878 | /* |
| 879 | * Unrecognized UUID: does not match any of the VMs (or SPs) |
| 880 | * and is not Null. |
| 881 | */ |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 882 | if (vm_count == 0) { |
| 883 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 884 | } |
| 885 | |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 886 | /* |
| 887 | * If the count flag is set we don't need to return the partition info |
| 888 | * descriptors. |
| 889 | */ |
| 890 | if (count_flag) { |
| 891 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 892 | .arg2 = vm_count}; |
| 893 | } |
| 894 | |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 895 | vm_locked = vm_lock(current_vm); |
| 896 | ret = send_versioned_partition_info_descriptors(vm_locked, partitions, |
| 897 | vm_count); |
| 898 | vm_unlock(&vm_locked); |
| 899 | return ret; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 902 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 903 | * Returns the ID of the VM. |
| 904 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 905 | struct ffa_value api_ffa_id_get(const struct vcpu *current) |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 906 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 907 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 908 | .arg2 = current->vm->id}; |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /** |
Olivier Deprez | 421677d | 2021-06-18 12:18:53 +0200 | [diff] [blame] | 912 | * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual |
| 913 | * FF-A instances. |
| 914 | * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET. |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 915 | */ |
| 916 | struct ffa_value api_ffa_spm_id_get(void) |
| 917 | { |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 918 | if (FFA_VERSION_1_1 <= FFA_VERSION_COMPILED) { |
| 919 | /* |
| 920 | * Return the SPMC ID that was fetched during FF-A |
| 921 | * initialization. |
| 922 | */ |
| 923 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 924 | .arg2 = arch_ffa_spmc_id_get()}; |
| 925 | } |
| 926 | |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 927 | return ffa_error(FFA_NOT_SUPPORTED); |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 931 | * This function is called by the architecture-specific context switching |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 932 | * function to indicate that register state for the given vCPU has been saved |
| 933 | * and can therefore be used by other pCPUs. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 934 | */ |
| 935 | void api_regs_state_saved(struct vcpu *vcpu) |
| 936 | { |
| 937 | sl_lock(&vcpu->lock); |
| 938 | vcpu->regs_available = true; |
| 939 | sl_unlock(&vcpu->lock); |
| 940 | } |
J-Alves | 0ffce75 | 2024-10-09 14:37:10 +0100 | [diff] [blame^] | 941 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 942 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 943 | * Assuming that the arguments have already been checked by the caller, injects |
| 944 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 945 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 946 | * is next run, which is up to the scheduler. |
| 947 | * |
| 948 | * Returns: |
| 949 | * - 0 on success if no further action is needed. |
| 950 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 951 | * up or kick the target vCPU. |
| 952 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 953 | int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked, |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 954 | uint32_t intid, |
| 955 | struct vcpu_locked current_locked, |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 956 | struct vcpu **next) |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 957 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 958 | struct vcpu *target_vcpu = target_locked.vcpu; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 959 | struct vcpu *current = current_locked.vcpu; |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 960 | struct interrupts *interrupts = &target_vcpu->interrupts; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 961 | int64_t ret = 0; |
| 962 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 963 | /* |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 964 | * We only need to change state and (maybe) trigger a virtual interrupt |
| 965 | * if it is enabled and was not previously pending. Otherwise we can |
| 966 | * skip everything except setting the pending bit. |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 967 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 968 | if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) && |
| 969 | !vcpu_is_virt_interrupt_pending(interrupts, intid))) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 970 | goto out; |
| 971 | } |
| 972 | |
| 973 | /* Increment the count. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 974 | vcpu_interrupt_count_increment(target_locked, interrupts, intid); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 975 | |
| 976 | /* |
| 977 | * Only need to update state if there was not already an |
| 978 | * interrupt enabled and pending. |
| 979 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 980 | if (vcpu_interrupt_count_get(target_locked) != 1) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 981 | goto out; |
| 982 | } |
| 983 | |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 984 | if (vm_is_primary(current->vm)) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 985 | /* |
| 986 | * If the call came from the primary VM, let it know that it |
| 987 | * should run or kick the target vCPU. |
| 988 | */ |
| 989 | ret = 1; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 990 | } else if (current != target_vcpu && next != NULL) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 991 | *next = api_wake_up_locked(current_locked, target_vcpu); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | out: |
| 995 | /* Either way, make it pending. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 996 | vcpu_virt_interrupt_set_pending(interrupts, intid); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 997 | |
Olivier Deprez | c19a6ea | 2020-08-06 11:16:07 +0200 | [diff] [blame] | 998 | return ret; |
| 999 | } |
| 1000 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1001 | /** |
J-Alves | 2ced167 | 2022-12-12 14:35:38 +0000 | [diff] [blame] | 1002 | * Constructs the return value from a successful FFA_MSG_WAIT call, when used |
| 1003 | * with FFA_MSG_SEND_32. |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1004 | */ |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 1005 | struct ffa_value ffa_msg_recv_return(const struct vm *receiver) |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1006 | { |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1007 | switch (receiver->mailbox.recv_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1008 | case FFA_MSG_SEND_32: |
| 1009 | return (struct ffa_value){ |
| 1010 | .func = FFA_MSG_SEND_32, |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1011 | .arg1 = (receiver->mailbox.recv_sender << 16) | |
| 1012 | receiver->id, |
| 1013 | .arg3 = receiver->mailbox.recv_size}; |
J-Alves | d9e7c8f | 2024-09-11 13:37:25 +0100 | [diff] [blame] | 1014 | default: |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1015 | return (struct ffa_value){ |
| 1016 | .func = FFA_RUN_32, |
| 1017 | /* |
| 1018 | * TODO: FFA_RUN should return vCPU and VM ID in arg1. |
| 1019 | * Retrieving vCPU requires a rework of the function, |
| 1020 | * while receiver ID must be set because it's checked by |
| 1021 | * other APIs (eg: FFA_NOTIFICATION_GET). |
| 1022 | */ |
| 1023 | .arg1 = receiver->id}; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1024 | } |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1027 | /** |
| 1028 | * Change the state of mailbox to empty, such that the ownership is given to the |
| 1029 | * Partition manager. |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1030 | * Returns FFA_SUCCESS if the mailbox was reset successfully, FFA_ERROR |
| 1031 | * otherwise. |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1032 | */ |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1033 | static struct ffa_value api_release_mailbox(struct vm_locked vm_locked) |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1034 | { |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1035 | struct ffa_value ret = {.func = FFA_SUCCESS_32}; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1036 | ffa_id_t vm_id = vm_locked.vm->id; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1037 | |
| 1038 | switch (vm_locked.vm->mailbox.state) { |
| 1039 | case MAILBOX_STATE_EMPTY: |
| 1040 | dlog_verbose("Mailbox of %x is empty.\n", vm_id); |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1041 | ret = ffa_error(FFA_DENIED); |
J-Alves | 31f8bef | 2023-07-14 12:46:28 +0100 | [diff] [blame] | 1042 | break; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1043 | case MAILBOX_STATE_FULL: |
| 1044 | /* Check it doesn't have pending RX full notifications. */ |
| 1045 | if (vm_are_fwk_notifications_pending(vm_locked)) { |
| 1046 | dlog_verbose( |
| 1047 | "Mailbox of endpoint %x has pending " |
| 1048 | "messages.\n", |
| 1049 | vm_id); |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1050 | ret = ffa_error(FFA_DENIED); |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1051 | } |
| 1052 | break; |
| 1053 | case MAILBOX_STATE_OTHER_WORLD_OWNED: |
| 1054 | /* |
| 1055 | * The SPMC shouldn't let SP's mailbox get into this state. |
| 1056 | * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst |
| 1057 | * the mailbox is in this state. In that case, we should report |
| 1058 | * error. |
| 1059 | */ |
| 1060 | if (vm_id_is_current_world(vm_id)) { |
| 1061 | dlog_verbose( |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1062 | "Mailbox of endpoint %x is in an incorrect " |
| 1063 | "state.\n", |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1064 | vm_id); |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1065 | ret = ffa_error(FFA_ABORTED); |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1066 | } |
| 1067 | break; |
| 1068 | } |
| 1069 | |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1070 | if (ret.func == FFA_SUCCESS_32) { |
| 1071 | vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 1074 | return ret; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Kathleen Capella | db6a08b | 2023-10-04 13:42:39 -0400 | [diff] [blame] | 1077 | /* |
| 1078 | * Helper to check if extended arguments (corresponding to regs x8-x17) |
| 1079 | * are zeroed out. |
| 1080 | */ |
Kathleen Capella | 036cc59 | 2023-11-30 18:26:15 -0500 | [diff] [blame] | 1081 | bool api_extended_args_are_zero(struct ffa_value *args) |
Kathleen Capella | db6a08b | 2023-10-04 13:42:39 -0400 | [diff] [blame] | 1082 | { |
| 1083 | if (args->extended_val.arg8 != 0U || args->extended_val.arg9 != 0U || |
| 1084 | args->extended_val.arg10 != 0U || args->extended_val.arg11 != 0U || |
| 1085 | args->extended_val.arg12 != 0U || args->extended_val.arg13 != 0U || |
| 1086 | args->extended_val.arg14 != 0U || args->extended_val.arg15 != 0U || |
| 1087 | args->extended_val.arg16 != 0U || args->extended_val.arg17 != 0U) { |
| 1088 | return false; |
| 1089 | } |
| 1090 | return true; |
| 1091 | } |
| 1092 | |
Kathleen Capella | be1a0b7 | 2024-08-16 12:56:39 -0400 | [diff] [blame] | 1093 | static void api_ffa_msg_wait_rx_release(struct vcpu *current) |
| 1094 | { |
| 1095 | struct vm_locked vm_locked; |
| 1096 | |
| 1097 | vm_locked = plat_ffa_vm_find_locked(current->vm->id); |
| 1098 | if (vm_locked.vm == NULL) { |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | api_release_mailbox(vm_locked); |
| 1103 | |
| 1104 | if (vm_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY) { |
| 1105 | dlog_warning("Mailbox not released to producer\n"); |
| 1106 | } |
| 1107 | |
| 1108 | vm_unlock(&vm_locked); |
| 1109 | } |
| 1110 | |
Kathleen Capella | 7253bd5 | 2024-08-14 17:36:09 -0400 | [diff] [blame] | 1111 | static bool api_retain_rx_buffer_ownership(struct ffa_value args) |
| 1112 | { |
| 1113 | return ((args.arg2 & FFA_MSG_WAIT_FLAG_RETAIN_RX) != 0U); |
| 1114 | } |
| 1115 | |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 1116 | struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next, |
| 1117 | struct ffa_value *args) |
| 1118 | { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1119 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 1120 | enum vcpu_state next_state = VCPU_STATE_RUNNING; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1121 | struct ffa_value ret; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1122 | struct vcpu_locked next_locked = (struct vcpu_locked){ |
| 1123 | .vcpu = NULL, |
| 1124 | }; |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 1125 | |
Kathleen Capella | 7253bd5 | 2024-08-14 17:36:09 -0400 | [diff] [blame] | 1126 | if (args->arg1 != 0U || args->arg3 != 0U || args->arg4 != 0U || |
| 1127 | args->arg5 != 0U || args->arg6 != 0U || args->arg7 != 0U) { |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 1128 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1129 | } |
| 1130 | |
Kathleen Capella | 7253bd5 | 2024-08-14 17:36:09 -0400 | [diff] [blame] | 1131 | if (current->vm->ffa_version >= FFA_VERSION_1_2) { |
| 1132 | if (!api_extended_args_are_zero(args)) { |
| 1133 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1134 | } |
| 1135 | } else { |
| 1136 | if (args->arg2 != 0U) { |
| 1137 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1138 | } |
Kathleen Capella | db6a08b | 2023-10-04 13:42:39 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1141 | current_locked = vcpu_lock(current); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1142 | if (!plat_ffa_check_runtime_state_transition( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1143 | current_locked, current->vm->id, HF_INVALID_VM_ID, |
| 1144 | next_locked, FFA_MSG_WAIT_32, &next_state)) { |
| 1145 | ret = ffa_error(FFA_DENIED); |
| 1146 | goto out; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 1149 | assert(!vm_id_is_current_world(current->vm->id) || |
| 1150 | next_state == VCPU_STATE_WAITING); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1151 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1152 | ret = plat_ffa_msg_wait_prepare(current_locked, next); |
Kathleen Capella | be1a0b7 | 2024-08-16 12:56:39 -0400 | [diff] [blame] | 1153 | |
| 1154 | /* |
| 1155 | * To maintain partial ordering of locks, release vCPU lock before |
| 1156 | * releasing the VM's RX buffer, a process which requires locking the |
| 1157 | * VM. |
| 1158 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1159 | out: |
| 1160 | vcpu_unlock(¤t_locked); |
Kathleen Capella | be1a0b7 | 2024-08-16 12:56:39 -0400 | [diff] [blame] | 1161 | |
Kathleen Capella | 7253bd5 | 2024-08-14 17:36:09 -0400 | [diff] [blame] | 1162 | if (ret.func != FFA_ERROR_32 && |
| 1163 | !api_retain_rx_buffer_ownership(*args)) { |
Kathleen Capella | be1a0b7 | 2024-08-16 12:56:39 -0400 | [diff] [blame] | 1164 | api_ffa_msg_wait_rx_release(current); |
| 1165 | } |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1166 | return ret; |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 1167 | } |
| 1168 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1169 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1170 | * Prepares the vCPU to run by updating its state and fetching whether a return |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1171 | * value needs to be forced onto the vCPU. |
| 1172 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1173 | static bool api_vcpu_prepare_run(struct vcpu_locked current_locked, |
| 1174 | struct vcpu_locked vcpu_next_locked, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1175 | struct ffa_value *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1176 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1177 | struct vm_locked vm_locked; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1178 | bool ret; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1179 | uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE; |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 1180 | bool vcpu_was_init_state = false; |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1181 | bool need_vm_lock; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1182 | struct two_vcpu_locked vcpus_locked; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1183 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1184 | /* |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1185 | * Check that the registers are available so that the vCPU can be run. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1186 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 1187 | * The VM lock is not needed in the common case so it must only be taken |
| 1188 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 1189 | * dependencies in the common run case meaning the sensitive context |
| 1190 | * switch performance is consistent. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1191 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1192 | struct vcpu *vcpu = vcpu_next_locked.vcpu; |
| 1193 | struct vcpu *current = current_locked.vcpu; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1194 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1195 | /* The VM needs to be locked to deliver mailbox messages. */ |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1196 | need_vm_lock = vcpu->state == VCPU_STATE_WAITING || |
| 1197 | (!vcpu->vm->el0_partition && |
| 1198 | (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT || |
| 1199 | vcpu->state == VCPU_STATE_BLOCKED || |
| 1200 | vcpu->state == VCPU_STATE_PREEMPTED)); |
| 1201 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1202 | if (need_vm_lock) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1203 | vcpu_unlock(&vcpu_next_locked); |
| 1204 | vcpu_unlock(¤t_locked); |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1205 | vm_locked = vm_lock(vcpu->vm); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1206 | |
| 1207 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 1208 | vcpus_locked = vcpu_lock_both(current, vcpu); |
| 1209 | current_locked = vcpus_locked.vcpu1; |
| 1210 | vcpu_next_locked = vcpus_locked.vcpu2; |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * If the vCPU is already running somewhere then we can't run it here |
| 1215 | * simultaneously. While it is actually running then the state should be |
| 1216 | * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it |
| 1217 | * stops running but while Hafnium is in the process of switching back |
| 1218 | * to the primary there will be a brief period while the state has been |
| 1219 | * updated but `regs_available` is still false (until |
| 1220 | * `api_regs_state_saved` is called). We can't start running it again |
| 1221 | * until this has finished, so count this state as still running for the |
| 1222 | * purposes of this check. |
| 1223 | */ |
| 1224 | if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) { |
| 1225 | /* |
| 1226 | * vCPU is running on another pCPU. |
| 1227 | * |
| 1228 | * It's okay not to return the sleep duration here because the |
| 1229 | * other physical CPU that is currently running this vCPU will |
| 1230 | * return the sleep duration if needed. |
| 1231 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1232 | *run_ret = ffa_error(FFA_BUSY); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1233 | ret = false; |
| 1234 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1235 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1238 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Kathleen Capella | 6e9765b | 2023-07-11 17:44:58 -0400 | [diff] [blame] | 1239 | dlog_verbose("VM %#x was aborted, cannot run vCPU %u\n", |
| 1240 | vcpu->vm->id, vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1241 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 1242 | } |
Kathleen Capella | 6ab0513 | 2023-05-10 12:27:35 -0400 | [diff] [blame] | 1243 | *run_ret = ffa_error(FFA_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 1244 | ret = false; |
| 1245 | goto out; |
| 1246 | } |
| 1247 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1248 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1249 | case VCPU_STATE_RUNNING: |
| 1250 | case VCPU_STATE_OFF: |
| 1251 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1252 | ret = false; |
| 1253 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1254 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1255 | case VCPU_STATE_WAITING: |
| 1256 | /* |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 1257 | * An initial FFA_RUN is necessary for SP's secondary vCPUs to |
| 1258 | * reach the message wait loop. |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1259 | */ |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 1260 | if (vcpu->rt_model == RTM_SP_INIT) { |
| 1261 | /* |
| 1262 | * TODO: this should be removed, but omitting it makes |
| 1263 | * normal world arch gicv3 tests failing. |
| 1264 | */ |
| 1265 | vcpu->rt_model = RTM_NONE; |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 1266 | |
| 1267 | vcpu_was_init_state = true; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1271 | assert(need_vm_lock == true); |
| 1272 | if (!vm_locked.vm->el0_partition && |
| 1273 | plat_ffa_inject_notification_pending_interrupt( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1274 | vcpu_next_locked, current_locked, vm_locked)) { |
Federico Recanati | 9dccc4b | 2022-04-27 12:52:36 +0200 | [diff] [blame] | 1275 | /* TODO: setting a return value to override |
| 1276 | * the placeholder (FFA_ERROR(INTERRUPTED)) |
| 1277 | * set by FFA_MSG_WAIT. FF-A v1.1 allows |
| 1278 | * FFA_MSG_WAIT to successfully return even if |
| 1279 | * it didn't receive a message. TFTF tests are |
| 1280 | * still expecting an FFA_ERROR instead, |
| 1281 | * should be fixed? |
| 1282 | */ |
| 1283 | arch_regs_set_retval( |
| 1284 | &vcpu->regs, |
| 1285 | (struct ffa_value){.func = FFA_RUN_32, |
| 1286 | // TODO: does it make sense |
| 1287 | // to set vCPU/receiver? |
| 1288 | .arg1 = 0}); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1289 | break; |
| 1290 | } |
| 1291 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1292 | /* |
| 1293 | * A pending message allows the vCPU to run so the message can |
| 1294 | * be delivered directly. |
| 1295 | */ |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1296 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1297 | arch_regs_set_retval(&vcpu->regs, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1298 | ffa_msg_recv_return(vcpu->vm)); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | } |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1301 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1302 | if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) { |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | if (arch_timer_enabled(&vcpu->regs)) { |
| 1307 | timer_remaining_ns = |
| 1308 | arch_timer_remaining_ns(&vcpu->regs); |
| 1309 | if (timer_remaining_ns == 0) { |
| 1310 | break; |
| 1311 | } |
| 1312 | } else { |
| 1313 | dlog_verbose("Timer disabled\n"); |
| 1314 | } |
| 1315 | run_ret->func = FFA_MSG_WAIT_32; |
| 1316 | run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
| 1317 | run_ret->arg2 = timer_remaining_ns; |
| 1318 | ret = false; |
| 1319 | goto out; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1320 | case VCPU_STATE_BLOCKED_INTERRUPT: |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1321 | if (need_vm_lock && |
| 1322 | plat_ffa_inject_notification_pending_interrupt( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1323 | vcpu_next_locked, current_locked, vm_locked)) { |
| 1324 | assert(vcpu_interrupt_count_get(vcpu_next_locked) > 0); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1325 | break; |
| 1326 | } |
| 1327 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1328 | /* Allow virtual interrupts to be delivered. */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1329 | if (vcpu_interrupt_count_get(vcpu_next_locked) > 0) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1333 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1334 | timer_remaining_ns = |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 1335 | arch_timer_remaining_ns(&vcpu->regs); |
| 1336 | |
| 1337 | /* |
| 1338 | * The timer expired so allow the interrupt to be |
| 1339 | * delivered. |
| 1340 | */ |
| 1341 | if (timer_remaining_ns == 0) { |
| 1342 | break; |
| 1343 | } |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1344 | } |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 1345 | |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1346 | /* |
| 1347 | * The vCPU is not ready to run, return the appropriate code to |
| 1348 | * the primary which called vcpu_run. |
| 1349 | */ |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1350 | run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1351 | run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
| 1352 | run_ret->arg2 = timer_remaining_ns; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1353 | |
| 1354 | ret = false; |
| 1355 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1356 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1357 | case VCPU_STATE_BLOCKED: |
| 1358 | /* A blocked vCPU is run unconditionally. Fall through. */ |
| 1359 | case VCPU_STATE_PREEMPTED: |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1360 | /* Check NPI is to be injected here. */ |
| 1361 | if (need_vm_lock) { |
| 1362 | plat_ffa_inject_notification_pending_interrupt( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1363 | vcpu_next_locked, current_locked, vm_locked); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1364 | } |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1365 | break; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1366 | default: |
| 1367 | /* |
| 1368 | * Execution not expected to reach here. Deny the request |
| 1369 | * gracefully. |
| 1370 | */ |
| 1371 | *run_ret = ffa_error(FFA_DENIED); |
| 1372 | ret = false; |
| 1373 | goto out; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1376 | plat_ffa_init_schedule_mode_ffa_run(current_locked, vcpu_next_locked); |
Madhukar Pappireddy | 1480fce | 2022-06-21 18:09:25 -0500 | [diff] [blame] | 1377 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1378 | /* It has been decided that the vCPU should be run. */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1379 | vcpu->cpu = current_locked.vcpu->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1380 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1381 | |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 1382 | if (vcpu_was_init_state) { |
| 1383 | vcpu_set_phys_core_idx(vcpu); |
| 1384 | vcpu_set_boot_info_gp_reg(vcpu); |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 1385 | } |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 1386 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1387 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1388 | * Mark the registers as unavailable now that we're about to reflect |
| 1389 | * them onto the real registers. This will also prevent another physical |
| 1390 | * CPU from trying to read these registers. |
| 1391 | */ |
| 1392 | vcpu->regs_available = false; |
| 1393 | |
| 1394 | ret = true; |
| 1395 | |
| 1396 | out: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1397 | if (need_vm_lock) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1398 | vm_unlock(&vm_locked); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1399 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1400 | return ret; |
| 1401 | } |
| 1402 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1403 | struct ffa_value api_ffa_run(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx, |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1404 | struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1405 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1406 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1407 | struct vcpu *vcpu; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1408 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 1409 | enum vcpu_state next_state = VCPU_STATE_RUNNING; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1410 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1411 | struct vcpu_locked vcpu_next_locked; |
| 1412 | struct two_vcpu_locked vcpus_locked; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1413 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1414 | current_locked = vcpu_lock(current); |
| 1415 | if (!plat_ffa_run_checks(current_locked, vm_id, vcpu_idx, &ret, next)) { |
| 1416 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 1417 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1418 | |
Raghu Krishnamurthy | 62f97a7 | 2021-07-27 02:14:59 -0700 | [diff] [blame] | 1419 | if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1420 | goto out; |
Raghu Krishnamurthy | 62f97a7 | 2021-07-27 02:14:59 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1423 | /* The requested VM must exist. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1424 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1425 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1426 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1427 | } |
| 1428 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1429 | /* The requested vCPU must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1430 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1431 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 1432 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1433 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1434 | /* |
| 1435 | * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC |
| 1436 | * bypasses the intermediate execution contexts and resumes the |
| 1437 | * SP execution context that was originally preempted. |
| 1438 | */ |
| 1439 | if (*next != NULL) { |
| 1440 | vcpu = *next; |
| 1441 | } else { |
| 1442 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
| 1443 | } |
| 1444 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1445 | /* |
| 1446 | * Unlock current vCPU to allow it to be locked together with next |
| 1447 | * vcpu. |
| 1448 | */ |
| 1449 | vcpu_unlock(¤t_locked); |
| 1450 | |
| 1451 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 1452 | vcpus_locked = vcpu_lock_both(current, vcpu); |
| 1453 | current_locked = vcpus_locked.vcpu1; |
| 1454 | vcpu_next_locked = vcpus_locked.vcpu2; |
| 1455 | |
| 1456 | if (!plat_ffa_check_runtime_state_transition( |
| 1457 | current_locked, current->vm->id, HF_INVALID_VM_ID, |
| 1458 | vcpu_next_locked, FFA_RUN_32, &next_state)) { |
| 1459 | ret = ffa_error(FFA_DENIED); |
| 1460 | goto out_vcpu; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1461 | } |
| 1462 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1463 | if (!api_vcpu_prepare_run(current_locked, vcpu_next_locked, &ret)) { |
| 1464 | goto out_vcpu; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1465 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1466 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1467 | /* |
| 1468 | * Inject timer interrupt if timer has expired. It's safe to access |
| 1469 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 1470 | * regs_available was true (and then set it to false) before returning |
| 1471 | * true. |
| 1472 | */ |
| 1473 | if (arch_timer_pending(&vcpu->regs)) { |
| 1474 | /* Make virtual timer interrupt pending. */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1475 | api_interrupt_inject_locked(vcpu_next_locked, |
| 1476 | HF_VIRTUAL_TIMER_INTID, |
| 1477 | vcpu_next_locked, NULL); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * Set the mask bit so the hardware interrupt doesn't fire |
| 1481 | * again. Ideally we wouldn't do this because it affects what |
| 1482 | * the secondary vCPU sees, but if we don't then we end up with |
| 1483 | * a loop of the interrupt firing each time we try to return to |
| 1484 | * the secondary vCPU. |
| 1485 | */ |
| 1486 | arch_timer_mask(&vcpu->regs); |
| 1487 | } |
| 1488 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1489 | /* Switch to the vCPU. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1490 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1491 | |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 1492 | assert(!vm_id_is_current_world(current->vm->id) || |
| 1493 | next_state == VCPU_STATE_BLOCKED); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1494 | current->state = VCPU_STATE_BLOCKED; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1495 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 1496 | /* |
| 1497 | * Set a placeholder return code to the scheduler. This will be |
| 1498 | * overwritten when the switch back to the primary occurs. |
| 1499 | */ |
J-Alves | bd32c97 | 2024-02-02 16:20:04 +0000 | [diff] [blame] | 1500 | ret = api_ffa_interrupt_return(0); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 1501 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1502 | out_vcpu: |
| 1503 | vcpu_unlock(&vcpu_next_locked); |
| 1504 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1505 | out: |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 1506 | vcpu_unlock(¤t_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1507 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1511 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 1512 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 1513 | static bool api_mode_valid_owned_and_exclusive(uint32_t mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1514 | { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 1515 | return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 1516 | MM_MODE_SHARED)) == 0; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1520 | * Configures the hypervisor's stage-1 view of the send and receive pages. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1521 | */ |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1522 | static struct ffa_value api_vm_configure_stage1( |
| 1523 | struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked, |
| 1524 | paddr_t pa_send_begin, paddr_t pa_send_end, paddr_t pa_recv_begin, |
| 1525 | paddr_t pa_recv_end, uint32_t extra_attributes, |
| 1526 | struct mpool *local_page_pool) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1527 | { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1528 | struct ffa_value ret; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1529 | |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1530 | /* |
| 1531 | * Map the send page as read-only in the SPMC/hypervisor address space. |
| 1532 | */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1533 | vm_locked.vm->mailbox.send = |
| 1534 | mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1535 | MM_MODE_R | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1536 | if (!vm_locked.vm->mailbox.send) { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1537 | ret = ffa_error(FFA_NO_MEMORY); |
| 1538 | goto out; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /* |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1542 | * Map the receive page as writable in the SPMC/hypervisor address |
| 1543 | * space. On failure, unmap the send page before returning. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1544 | */ |
| 1545 | vm_locked.vm->mailbox.recv = |
| 1546 | mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1547 | MM_MODE_W | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1548 | if (!vm_locked.vm->mailbox.recv) { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1549 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1550 | goto fail_undo_send; |
| 1551 | } |
| 1552 | |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1553 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1554 | goto out; |
| 1555 | |
| 1556 | /* |
| 1557 | * The following mappings will not require more memory than is available |
| 1558 | * in the local pool. |
| 1559 | */ |
| 1560 | fail_undo_send: |
| 1561 | vm_locked.vm->mailbox.send = NULL; |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 1562 | CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 1563 | local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1564 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1565 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1566 | return ret; |
| 1567 | } |
| 1568 | |
| 1569 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1570 | * Sanity checks and configures the send and receive pages in the VM stage-2 |
| 1571 | * and hypervisor stage-1 page tables. |
| 1572 | * |
| 1573 | * Returns: |
| 1574 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1575 | * aligned, are the same or have invalid attributes. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1576 | * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers |
| 1577 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1578 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1579 | * - FFA_SUCCESS on success if no further action is needed. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1580 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1581 | |
| 1582 | struct ffa_value api_vm_configure_pages( |
| 1583 | struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked, |
| 1584 | ipaddr_t send, ipaddr_t recv, uint32_t page_count, |
| 1585 | struct mpool *local_page_pool) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1586 | { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1587 | struct ffa_value ret; |
| 1588 | paddr_t pa_send_begin; |
| 1589 | paddr_t pa_send_end; |
| 1590 | paddr_t pa_recv_begin; |
| 1591 | paddr_t pa_recv_end; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1592 | uint32_t orig_send_mode = 0; |
| 1593 | uint32_t orig_recv_mode = 0; |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1594 | uint32_t extra_attributes; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1595 | |
| 1596 | /* We only allow these to be setup once. */ |
| 1597 | if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1598 | dlog_error("%s: Mailboxes have already been setup for VM %#x\n", |
| 1599 | __func__, vm_locked.vm->id); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1600 | ret = ffa_error(FFA_DENIED); |
| 1601 | goto out; |
| 1602 | } |
| 1603 | |
| 1604 | /* Hafnium only supports a fixed size of RX/TX buffers. */ |
| 1605 | if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) { |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1606 | dlog_error("%s: Page count must be %zu, it is %d\n", __func__, |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1607 | HF_MAILBOX_SIZE / FFA_PAGE_SIZE, page_count); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1608 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1609 | goto out; |
| 1610 | } |
| 1611 | |
| 1612 | /* Fail if addresses are not page-aligned. */ |
| 1613 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 1614 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1615 | dlog_error("%s: Mailbox buffers not page-aligned\n", __func__); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1616 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1617 | goto out; |
| 1618 | } |
| 1619 | |
| 1620 | /* Convert to physical addresses. */ |
| 1621 | pa_send_begin = pa_from_ipa(send); |
| 1622 | pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE); |
| 1623 | pa_recv_begin = pa_from_ipa(recv); |
| 1624 | pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE); |
| 1625 | |
| 1626 | /* Fail if the same page is used for the send and receive pages. */ |
| 1627 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1628 | dlog_error("%s: Mailbox buffers overlap\n", __func__); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1629 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1630 | goto out; |
| 1631 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1632 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1633 | /* Set stage 2 translation tables only for virtual FF-A instances. */ |
| 1634 | if (vm_id_is_current_world(vm_locked.vm->id)) { |
| 1635 | /* |
| 1636 | * Ensure the pages are valid, owned and exclusive to the VM and |
| 1637 | * that the VM has the required access to the memory. |
| 1638 | */ |
| 1639 | if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE), |
| 1640 | &orig_send_mode) || |
| 1641 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 1642 | (orig_send_mode & MM_MODE_R) == 0 || |
| 1643 | (orig_send_mode & MM_MODE_W) == 0) { |
| 1644 | dlog_error( |
| 1645 | "VM doesn't have required access rights to map " |
| 1646 | "TX buffer in stage 2.\n"); |
| 1647 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1648 | goto out; |
| 1649 | } |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1650 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1651 | if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE), |
| 1652 | &orig_recv_mode) || |
| 1653 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 1654 | (orig_recv_mode & MM_MODE_R) == 0) { |
| 1655 | dlog_error( |
| 1656 | "VM doesn't have required access rights to map " |
| 1657 | "RX buffer in stage 2.\n"); |
| 1658 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1659 | goto out; |
| 1660 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1661 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1662 | /* Take memory ownership away from the VM and mark as shared. */ |
| 1663 | uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | |
| 1664 | MM_MODE_W; |
| 1665 | if (vm_locked.vm->el0_partition) { |
| 1666 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1667 | } |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1668 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1669 | if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
| 1670 | mode, local_page_pool, NULL)) { |
| 1671 | dlog_error( |
| 1672 | "Cannot allocate a new entry in stage 2 " |
| 1673 | "translation table.\n"); |
| 1674 | ret = ffa_error(FFA_NO_MEMORY); |
| 1675 | goto out; |
| 1676 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1677 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1678 | mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R; |
| 1679 | if (vm_locked.vm->el0_partition) { |
| 1680 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1681 | } |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1682 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1683 | if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
| 1684 | mode, local_page_pool, NULL)) { |
| 1685 | /* TODO: partial defrag of failed range. */ |
| 1686 | /* Recover any memory consumed in failed mapping. */ |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1687 | dlog_error("%s: cannot map recv page\n", __func__); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1688 | vm_ptable_defrag(vm_locked, local_page_pool); |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1689 | ret = ffa_error(FFA_NO_MEMORY); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1690 | goto fail_undo_send; |
| 1691 | } |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1692 | } else { |
| 1693 | ret = arch_other_world_vm_configure_rxtx_map( |
| 1694 | vm_locked, local_page_pool, pa_send_begin, pa_send_end, |
| 1695 | pa_recv_begin, pa_recv_end); |
| 1696 | if (ret.func != FFA_SUCCESS_32) { |
| 1697 | goto out; |
| 1698 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1699 | } |
| 1700 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1701 | /* Get extra send/recv pages mapping attributes for the given VM ID. */ |
| 1702 | extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id); |
| 1703 | |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1704 | /* |
| 1705 | * For EL0 partitions, since both the partition and the hypervisor code |
| 1706 | * use the EL2&0 translation regime, it is critical to mark the mappings |
| 1707 | * of the send and recv buffers as non-global in the TLB. For one, if we |
| 1708 | * dont mark it as non-global, it would cause TLB conflicts since there |
| 1709 | * would be an identity mapping with non-global attribute in the |
| 1710 | * partitions page tables, but another identity mapping in the |
| 1711 | * hypervisor page tables with the global attribute. The other issue is |
| 1712 | * one of security, we dont want other partitions to be able to access |
| 1713 | * other partitions buffers through cached translations. |
| 1714 | */ |
| 1715 | if (vm_locked.vm->el0_partition) { |
| 1716 | extra_attributes |= MM_MODE_NG; |
| 1717 | } |
| 1718 | |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1719 | ret = api_vm_configure_stage1( |
| 1720 | mm_stage1_locked, vm_locked, pa_send_begin, pa_send_end, |
| 1721 | pa_recv_begin, pa_recv_end, extra_attributes, local_page_pool); |
| 1722 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1723 | goto fail_undo_send_and_recv; |
| 1724 | } |
| 1725 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1726 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1727 | goto out; |
| 1728 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1729 | fail_undo_send_and_recv: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1730 | CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1731 | orig_recv_mode, local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1732 | |
| 1733 | fail_undo_send: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1734 | CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1735 | orig_send_mode, local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1736 | |
| 1737 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1738 | return ret; |
| 1739 | } |
| 1740 | |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1741 | /** |
| 1742 | * Read the buffer addresses and VM ID of an FFA_RXTX_MAP request. Handles |
| 1743 | * forwarded messages by reading from the hypervisor's TX buffer. |
| 1744 | * |
| 1745 | * Returns the VM/SP ID on success. |
| 1746 | * |
| 1747 | * Returns `HF_INVALID_VM_ID` when the arguments provided via the ABI |
| 1748 | * FFA_RXTX_MAP indicate the SPMC should retrieve the RXTX description from the |
| 1749 | * Hypervisor RXTX buffers, and the hypervisor hasn't given its own RXTX buffers |
| 1750 | * for the SPMC to map. |
| 1751 | */ |
| 1752 | static ffa_id_t api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send, |
| 1753 | ipaddr_t *recv, uint32_t *page_count) |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1754 | { |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1755 | bool forwarded; |
| 1756 | struct vm_locked vm_locked; |
| 1757 | ffa_id_t owner_vm_id; |
| 1758 | struct ffa_endpoint_rx_tx_descriptor *endpoint_desc; |
| 1759 | struct ffa_composite_memory_region *rx_region; |
| 1760 | struct ffa_composite_memory_region *tx_region; |
| 1761 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1762 | /* |
| 1763 | * If the message has been forwarded the effective addresses are in |
| 1764 | * hypervisor's TX buffer. |
| 1765 | */ |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1766 | forwarded = (current_vm->id == HF_OTHER_WORLD_ID) && |
| 1767 | (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) && |
| 1768 | (*page_count == 0); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1769 | |
| 1770 | if (forwarded) { |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1771 | vm_locked = vm_lock(current_vm); |
| 1772 | endpoint_desc = (struct ffa_endpoint_rx_tx_descriptor *) |
| 1773 | vm_locked.vm->mailbox.send; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1774 | |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1775 | if (endpoint_desc == NULL) { |
| 1776 | dlog_error( |
| 1777 | "Trying to access RXTX description, but " |
| 1778 | "hypervisor has not provided RXTX buffers\n"); |
| 1779 | vm_unlock(&vm_locked); |
| 1780 | return HF_INVALID_VM_ID; |
| 1781 | } |
| 1782 | |
| 1783 | rx_region = ffa_endpoint_get_rx_memory_region(endpoint_desc); |
| 1784 | tx_region = ffa_endpoint_get_tx_memory_region(endpoint_desc); |
| 1785 | |
| 1786 | owner_vm_id = endpoint_desc->endpoint_id; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1787 | *recv = ipa_init(rx_region->constituents[0].address); |
| 1788 | *send = ipa_init(tx_region->constituents[0].address); |
| 1789 | *page_count = rx_region->constituents[0].page_count; |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1790 | |
| 1791 | vm_unlock(&vm_locked); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1792 | } else { |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1793 | owner_vm_id = current_vm->id; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1794 | } |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1795 | |
| 1796 | return owner_vm_id; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1797 | } |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1798 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1799 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1800 | * Configures the VM to send/receive data through the specified pages. The pages |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1801 | * must not be shared. Locking of the page tables combined with a local memory |
| 1802 | * pool ensures there will always be enough memory to recover from any errors |
| 1803 | * that arise. The stage-1 page tables must be locked so memory cannot be taken |
| 1804 | * by another core which could result in this transaction being unable to roll |
| 1805 | * back in the case of an error. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1806 | * |
| 1807 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1808 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1809 | * aligned, are the same or have invalid attributes. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1810 | * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1811 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1812 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1813 | * - FFA_SUCCESS on success if no further action is needed. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1814 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1815 | struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv, |
Federico Recanati | 9f1b653 | 2022-04-14 13:15:28 +0200 | [diff] [blame] | 1816 | uint32_t page_count, struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1817 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1818 | struct ffa_value ret; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1819 | struct vm_locked owner_vm_locked; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1820 | struct mm_stage1_locked mm_stage1_locked; |
| 1821 | struct mpool local_page_pool; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1822 | ffa_id_t owner_vm_id; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1823 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1824 | /* |
| 1825 | * Get the original buffer addresses and VM ID in case of forwarded |
| 1826 | * message. |
| 1827 | */ |
Karl Meakin | b9705e2 | 2024-04-05 13:58:28 +0100 | [diff] [blame] | 1828 | owner_vm_id = api_get_rxtx_description(current->vm, &send, &recv, |
| 1829 | &page_count); |
| 1830 | if (owner_vm_id == HF_INVALID_VM_ID) { |
| 1831 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1832 | } |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1833 | |
| 1834 | owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id); |
| 1835 | if (owner_vm_locked.vm == NULL) { |
| 1836 | dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n", |
| 1837 | owner_vm_id); |
| 1838 | return ffa_error(FFA_DENIED); |
| 1839 | } |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1840 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1841 | /* |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1842 | * Create a local pool so any freed memory can't be used by another |
| 1843 | * thread. This is to ensure the original mapping can be restored if any |
| 1844 | * stage of the process fails. |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1845 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1846 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1847 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1848 | mm_stage1_locked = mm_lock_stage1(); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1849 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1850 | ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send, |
| 1851 | recv, page_count, &local_page_pool); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1852 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1853 | goto exit; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1856 | /* Forward buffer mapping to SPMC if coming from a VM. */ |
| 1857 | plat_ffa_rxtx_map_forward(owner_vm_locked); |
| 1858 | |
Federico Recanati | 9f1b653 | 2022-04-14 13:15:28 +0200 | [diff] [blame] | 1859 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1860 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1861 | exit: |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1862 | mpool_fini(&local_page_pool); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1863 | mm_unlock_stage1(&mm_stage1_locked); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1864 | vm_unlock(&owner_vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1865 | |
| 1866 | return ret; |
| 1867 | } |
| 1868 | |
| 1869 | /** |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1870 | * Unmaps the RX/TX buffer pair with a partition or partition manager from the |
| 1871 | * translation regime of the caller. Unmap the region for the hypervisor and |
| 1872 | * set the memory region to owned and exclusive for the component. Since the |
| 1873 | * memory region mapped in the page table, when the buffers were originally |
| 1874 | * created we can safely remap it. |
| 1875 | * |
| 1876 | * Returns: |
| 1877 | * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on |
| 1878 | * behalf of the caller. |
| 1879 | * - FFA_SUCCESS on success if no further action is needed. |
| 1880 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1881 | struct ffa_value api_ffa_rxtx_unmap(ffa_id_t allocator_id, struct vcpu *current) |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1882 | { |
| 1883 | struct vm *vm = current->vm; |
| 1884 | struct vm_locked vm_locked; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1885 | ffa_id_t owner_vm_id; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1886 | struct mm_stage1_locked mm_stage1_locked; |
| 1887 | paddr_t send_pa_begin; |
| 1888 | paddr_t send_pa_end; |
| 1889 | paddr_t recv_pa_begin; |
| 1890 | paddr_t recv_pa_end; |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1891 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1892 | |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 1893 | if (vm->id == HF_HYPERVISOR_VM_ID && !ffa_is_vm_id(allocator_id)) { |
Olivier Deprez | 106c8ce | 2024-02-08 11:49:02 +0100 | [diff] [blame] | 1894 | dlog_verbose( |
J-Alves | 9bd324a | 2023-01-12 10:52:52 +0000 | [diff] [blame] | 1895 | "The Hypervisor must specify a valid VM ID in register " |
| 1896 | "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n"); |
| 1897 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1898 | } |
| 1899 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1900 | /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */ |
| 1901 | if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) { |
J-Alves | 9bd324a | 2023-01-12 10:52:52 +0000 | [diff] [blame] | 1902 | dlog_error( |
| 1903 | "The register W1 (containing ID) must be 0 at virtual " |
| 1904 | "instances.\n"); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1905 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1906 | } |
| 1907 | |
| 1908 | /* VM ID of which buffers have to be unmapped. */ |
| 1909 | owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id; |
| 1910 | |
| 1911 | vm_locked = plat_ffa_vm_find_locked(owner_vm_id); |
| 1912 | vm = vm_locked.vm; |
| 1913 | if (vm == NULL) { |
| 1914 | dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n", |
| 1915 | owner_vm_id); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1916 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1917 | } |
| 1918 | |
| 1919 | /* Get send and receive buffers. */ |
| 1920 | if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) { |
Olivier Deprez | 86d87ae | 2021-08-19 14:27:46 +0200 | [diff] [blame] | 1921 | dlog_verbose( |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1922 | "No buffer pair registered on behalf of the caller.\n"); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1923 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1924 | goto out; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | /* Currently a mailbox size of 1 page is assumed. */ |
| 1928 | send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send)); |
| 1929 | send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE); |
| 1930 | recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv)); |
| 1931 | recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE); |
| 1932 | |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1933 | mm_stage1_locked = mm_lock_stage1(); |
| 1934 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1935 | /* Reset stage 2 mapping only for virtual FF-A instances. */ |
| 1936 | if (vm_id_is_current_world(owner_vm_id)) { |
| 1937 | /* |
| 1938 | * Set the memory region of the buffers back to the default mode |
| 1939 | * for the VM. Since this memory region was already mapped for |
| 1940 | * the RXTX buffers we can safely remap them. |
| 1941 | */ |
| 1942 | CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end, |
| 1943 | MM_MODE_R | MM_MODE_W | MM_MODE_X, |
| 1944 | &api_page_pool, NULL)); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1945 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1946 | CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end, |
| 1947 | MM_MODE_R | MM_MODE_W | MM_MODE_X, |
| 1948 | &api_page_pool, NULL)); |
Karl Meakin | 738bee1 | 2024-01-12 15:19:45 +0000 | [diff] [blame] | 1949 | } else { |
| 1950 | ret = arch_other_world_vm_configure_rxtx_unmap( |
| 1951 | vm_locked, &api_page_pool, send_pa_begin, send_pa_end, |
| 1952 | recv_pa_begin, recv_pa_end); |
| 1953 | if (ret.func != FFA_SUCCESS_32) { |
| 1954 | goto out; |
| 1955 | } |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1956 | } |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* Unmap the buffers in the partition manager. */ |
| 1959 | CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end, |
| 1960 | &api_page_pool)); |
| 1961 | CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end, |
| 1962 | &api_page_pool)); |
| 1963 | |
| 1964 | vm->mailbox.send = NULL; |
| 1965 | vm->mailbox.recv = NULL; |
Federico Recanati | 10bd06c | 2022-02-23 17:32:59 +0100 | [diff] [blame] | 1966 | plat_ffa_vm_destroy(vm_locked); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1967 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1968 | /* Forward buffer unmapping to SPMC if coming from a VM. */ |
J-Alves | 7007993 | 2022-12-07 17:32:20 +0000 | [diff] [blame] | 1969 | plat_ffa_rxtx_unmap_forward(vm_locked); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1970 | |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1971 | mm_unlock_stage1(&mm_stage1_locked); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1972 | |
| 1973 | out: |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1974 | vm_unlock(&vm_locked); |
| 1975 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1976 | return ret; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /** |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1980 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 1981 | * and notifies the receiver. |
| 1982 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1983 | struct ffa_value api_ffa_msg_send2(ffa_id_t sender_vm_id, uint32_t flags, |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1984 | struct vcpu *current) |
| 1985 | { |
| 1986 | struct vm *from = current->vm; |
| 1987 | struct vm *to; |
| 1988 | struct vm_locked to_locked; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1989 | ffa_id_t msg_sender_id; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1990 | struct vm_locked sender_locked; |
| 1991 | const void *from_msg; |
| 1992 | struct ffa_value ret; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 1993 | ffa_id_t sender_id; |
| 1994 | ffa_id_t receiver_id; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1995 | uint32_t msg_size; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1996 | |
Karl Meakin | a5ea909 | 2024-05-28 15:40:33 +0100 | [diff] [blame] | 1997 | alignas(8) struct ffa_partition_rxtx_header header; |
| 1998 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1999 | /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */ |
| 2000 | if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) { |
| 2001 | dlog_error("Sender VM ID must be zero.\n"); |
| 2002 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2003 | } |
| 2004 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2005 | /* |
| 2006 | * Get message sender's mailbox, which can be different to the `from` vm |
| 2007 | * when the message is forwarded. |
| 2008 | */ |
| 2009 | msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id; |
| 2010 | sender_locked = plat_ffa_vm_find_locked(msg_sender_id); |
| 2011 | if (sender_locked.vm == NULL) { |
| 2012 | dlog_error("Cannot send message from VM ID %#x, not found.\n", |
| 2013 | msg_sender_id); |
| 2014 | return ffa_error(FFA_DENIED); |
| 2015 | } |
| 2016 | |
| 2017 | from_msg = sender_locked.vm->mailbox.send; |
| 2018 | if (from_msg == NULL) { |
| 2019 | dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n", |
| 2020 | msg_sender_id); |
| 2021 | ret = ffa_error(FFA_DENIED); |
| 2022 | goto out_unlock_sender; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Copy message header as safety measure to avoid multiple accesses to |
| 2027 | * unsafe memory which could be 'corrupted' between safety checks and |
| 2028 | * final buffer copy. |
| 2029 | */ |
J-Alves | 8a5fd9d | 2024-04-09 11:22:49 +0100 | [diff] [blame] | 2030 | if (!memcpy_trapped(&header, FFA_RXTX_HEADER_SIZE, from_msg, |
| 2031 | FFA_RXTX_HEADER_SIZE)) { |
| 2032 | dlog_error( |
| 2033 | "%s: Failed to copy message from sender's(%x) TX " |
| 2034 | "buffer.\n", |
| 2035 | __func__, sender_locked.vm->id); |
| 2036 | ret = ffa_error(FFA_ABORTED); |
| 2037 | goto out_unlock_sender; |
| 2038 | } |
| 2039 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2040 | sender_id = ffa_rxtx_header_sender(&header); |
| 2041 | receiver_id = ffa_rxtx_header_receiver(&header); |
| 2042 | |
| 2043 | /* Ensure Sender IDs from API and from message header match. */ |
| 2044 | if (msg_sender_id != sender_id) { |
| 2045 | dlog_error( |
| 2046 | "Message sender VM ID (%#x) doesn't match header's VM " |
| 2047 | "ID (%#x).\n", |
| 2048 | msg_sender_id, sender_id); |
| 2049 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2050 | goto out_unlock_sender; |
| 2051 | } |
| 2052 | |
| 2053 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 2054 | if (receiver_id == sender_id) { |
| 2055 | dlog_error("Sender and receive VM IDs must be different.\n"); |
| 2056 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2057 | goto out_unlock_sender; |
| 2058 | } |
| 2059 | |
Federico Recanati | 7b39ff2 | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2060 | /* `flags` can be set only at secure virtual FF-A instances. */ |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 2061 | if (ffa_is_vm_id(sender_id) && (flags != 0)) { |
Federico Recanati | 7b39ff2 | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2062 | dlog_error("flags must be zero.\n"); |
| 2063 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2064 | } |
| 2065 | |
Olivier Deprez | 37b7511 | 2024-03-04 18:27:30 +0100 | [diff] [blame] | 2066 | if (header.offset != FFA_RXTX_HEADER_SIZE) { |
| 2067 | dlog_error("Indirect msg payload must follow the header.\n"); |
| 2068 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2069 | } |
| 2070 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2071 | /* |
| 2072 | * Check if the message has to be forwarded to the SPMC, in |
| 2073 | * this case return, the SPMC will handle the buffer copy. |
| 2074 | */ |
| 2075 | if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) { |
| 2076 | goto out_unlock_sender; |
| 2077 | } |
| 2078 | |
| 2079 | /* Ensure the receiver VM exists. */ |
| 2080 | to_locked = plat_ffa_vm_find_locked(receiver_id); |
| 2081 | to = to_locked.vm; |
| 2082 | |
| 2083 | if (to == NULL) { |
| 2084 | dlog_error("Cannot deliver message to VM %#x, not found.\n", |
| 2085 | receiver_id); |
| 2086 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2087 | goto out_unlock_sender; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Check sender and receiver can use indirect messages. |
| 2092 | * Sender is the VM/SP who originally sent the message, not the |
| 2093 | * hypervisor possibly relaying it. |
| 2094 | */ |
| 2095 | if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) { |
| 2096 | dlog_verbose("VM %#x doesn't support indirect message\n", |
| 2097 | sender_id); |
| 2098 | ret = ffa_error(FFA_DENIED); |
| 2099 | goto out; |
| 2100 | } |
| 2101 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2102 | if (vm_is_mailbox_busy(to_locked)) { |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2103 | dlog_error( |
| 2104 | "Cannot deliver message to VM %#x, RX buffer not " |
| 2105 | "ready.\n", |
| 2106 | receiver_id); |
| 2107 | ret = ffa_error(FFA_BUSY); |
| 2108 | goto out; |
| 2109 | } |
| 2110 | |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2111 | /* Acquire receiver's RX buffer. */ |
| 2112 | if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) { |
| 2113 | dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id); |
| 2114 | goto out; |
| 2115 | } |
| 2116 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2117 | /* Check the size of transfer. */ |
| 2118 | msg_size = FFA_RXTX_HEADER_SIZE + header.size; |
Olivier Deprez | f1dd1e1 | 2024-03-04 18:21:01 +0100 | [diff] [blame] | 2119 | if ((msg_size > FFA_MSG_PAYLOAD_MAX) || |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2120 | (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) { |
| 2121 | dlog_error("Message is too big.\n"); |
| 2122 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2123 | goto out; |
| 2124 | } |
| 2125 | |
| 2126 | /* Copy data. */ |
J-Alves | 8a5fd9d | 2024-04-09 11:22:49 +0100 | [diff] [blame] | 2127 | if (!memcpy_trapped(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, |
| 2128 | msg_size)) { |
| 2129 | dlog_error( |
| 2130 | "%s: Failed to copy message to receiver's(%x) RX " |
| 2131 | "buffer.\n", |
| 2132 | __func__, to->id); |
| 2133 | ret = ffa_error(FFA_ABORTED); |
| 2134 | goto out; |
| 2135 | } |
| 2136 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2137 | to->mailbox.recv_size = msg_size; |
| 2138 | to->mailbox.recv_sender = sender_id; |
| 2139 | to->mailbox.recv_func = FFA_MSG_SEND2_32; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2140 | to->mailbox.state = MAILBOX_STATE_FULL; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2141 | |
J-Alves | 3bb8259 | 2023-01-23 11:20:54 +0000 | [diff] [blame] | 2142 | /* |
| 2143 | * Set framework notifications, only if the SP has enabled |
| 2144 | * receipt of notifications. |
| 2145 | * If VMs have provided the RX buffer it is implied they already |
| 2146 | * support indirect messaging, and therefore framework notifications. |
| 2147 | */ |
| 2148 | if (ffa_is_vm_id(to_locked.vm->id) || |
| 2149 | vm_are_notifications_enabled(to_locked.vm)) { |
| 2150 | ffa_notifications_bitmap_t rx_buffer_full = |
| 2151 | ffa_is_vm_id(sender_id) |
| 2152 | ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK |
| 2153 | : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2154 | |
J-Alves | 3bb8259 | 2023-01-23 11:20:54 +0000 | [diff] [blame] | 2155 | vm_notifications_framework_set_pending(to_locked, |
| 2156 | rx_buffer_full); |
| 2157 | |
| 2158 | if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) { |
| 2159 | dlog_verbose("SRI was NOT delayed. vcpu: %u!\n", |
| 2160 | vcpu_index(current)); |
| 2161 | plat_ffa_sri_trigger_not_delayed(current->cpu); |
| 2162 | } else { |
J-Alves | ea8ccfe | 2024-10-09 11:47:24 +0100 | [diff] [blame] | 2163 | plat_ffa_sri_set_delayed(current->cpu); |
J-Alves | 3bb8259 | 2023-01-23 11:20:54 +0000 | [diff] [blame] | 2164 | } |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2168 | |
| 2169 | out: |
| 2170 | vm_unlock(&to_locked); |
| 2171 | |
| 2172 | out_unlock_sender: |
| 2173 | vm_unlock(&sender_locked); |
| 2174 | |
| 2175 | return ret; |
| 2176 | } |
| 2177 | |
| 2178 | /** |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 2179 | * Releases the caller's mailbox so that a new message can be received. The |
| 2180 | * caller must have copied out all data they wish to preserve as new messages |
| 2181 | * will overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2182 | * |
| 2183 | * Returns: |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2184 | * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but |
| 2185 | * there's no buffer pair mapped. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2186 | * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read. |
| 2187 | * - FFA_SUCCESS on success if no further action is needed. |
| 2188 | * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 2189 | * needs to wake up or kick waiters. Waiters should be retrieved by calling |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2190 | * hf_mailbox_waiter_get. |
| 2191 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 2192 | struct ffa_value api_ffa_rx_release(ffa_id_t receiver_id, struct vcpu *current) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2193 | { |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2194 | struct vm *current_vm = current->vm; |
| 2195 | struct vm *vm; |
| 2196 | struct vm_locked vm_locked; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 2197 | ffa_id_t current_vm_id = current_vm->id; |
| 2198 | ffa_id_t release_vm_id; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2199 | struct ffa_value ret; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2200 | |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2201 | /* `receiver_id` can be set only at Non-Secure Physical interface. */ |
| 2202 | if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) { |
| 2203 | dlog_error("Invalid `receiver_id`, must be zero.\n"); |
| 2204 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * VM ID to be released: `receiver_id` if message has been forwarded by |
| 2209 | * Hypervisor to release a VM's buffer, current VM ID otherwise. |
| 2210 | */ |
| 2211 | if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) { |
| 2212 | release_vm_id = current_vm_id; |
| 2213 | } else { |
| 2214 | release_vm_id = receiver_id; |
| 2215 | } |
| 2216 | |
| 2217 | vm_locked = plat_ffa_vm_find_locked(release_vm_id); |
| 2218 | vm = vm_locked.vm; |
| 2219 | if (vm == NULL) { |
| 2220 | dlog_error("No buffer registered for VM ID %#x.\n", |
| 2221 | release_vm_id); |
| 2222 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2223 | } |
| 2224 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2225 | if (plat_ffa_rx_release_forward(vm_locked, &ret)) { |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2226 | goto out; |
| 2227 | } |
| 2228 | |
Karl Meakin | 1064a9c | 2024-06-04 17:20:43 +0100 | [diff] [blame] | 2229 | ret = api_release_mailbox(vm_locked); |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2230 | |
| 2231 | out: |
| 2232 | vm_unlock(&vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 2233 | |
| 2234 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 2235 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2236 | |
| 2237 | /** |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2238 | * Acquire ownership of an RX buffer before writing to it. Both |
| 2239 | * Hypervisor and SPMC are producers of VM's RX buffer, and they |
| 2240 | * could contend for the same buffer. SPMC owns VM's RX buffer after |
| 2241 | * it's mapped in its translation regime. This ABI should be |
| 2242 | * used by the Hypervisor to get the ownership of a VM's RX buffer |
| 2243 | * from the SPMC solving the aforementioned possible contention. |
| 2244 | * |
| 2245 | * Returns: |
| 2246 | * - FFA_DENIED: callee cannot relinquish ownership of RX buffer. |
| 2247 | * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM. |
| 2248 | * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance. |
| 2249 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 2250 | struct ffa_value api_ffa_rx_acquire(ffa_id_t receiver_id, struct vcpu *current) |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2251 | { |
| 2252 | struct vm_locked receiver_locked; |
| 2253 | struct vm *receiver; |
| 2254 | struct ffa_value ret; |
| 2255 | |
| 2256 | if ((current->vm->id != HF_HYPERVISOR_VM_ID) || |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 2257 | !ffa_is_vm_id(receiver_id)) { |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2258 | dlog_error( |
| 2259 | "FFA_RX_ACQUIRE not supported at this FF-A " |
| 2260 | "instance.\n"); |
| 2261 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2262 | } |
| 2263 | |
| 2264 | receiver_locked = plat_ffa_vm_find_locked(receiver_id); |
| 2265 | receiver = receiver_locked.vm; |
| 2266 | |
| 2267 | if (receiver == NULL || receiver->mailbox.recv == NULL) { |
| 2268 | dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n", |
| 2269 | receiver_id); |
| 2270 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2271 | goto out; |
| 2272 | } |
| 2273 | |
| 2274 | if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) { |
| 2275 | dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id); |
| 2276 | ret = ffa_error(FFA_DENIED); |
| 2277 | goto out; |
| 2278 | } |
| 2279 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2280 | receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED; |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2281 | |
| 2282 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2283 | |
| 2284 | out: |
| 2285 | vm_unlock(&receiver_locked); |
| 2286 | |
| 2287 | return ret; |
| 2288 | } |
| 2289 | |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2290 | /* |
| 2291 | * Returns true if intid relates with either of those: |
| 2292 | * - NPI |
| 2293 | * - ME |
| 2294 | * - Virtual Timer. |
| 2295 | * |
| 2296 | * These are VIs with no expected interrupt descriptor. |
| 2297 | */ |
| 2298 | static bool api_is_maintenance_virtual_interrupt(uint32_t intid) |
| 2299 | { |
| 2300 | return intid == HF_NOTIFICATION_PENDING_INTID || |
| 2301 | intid == HF_MANAGED_EXIT_INTID || |
| 2302 | intid == HF_VIRTUAL_TIMER_INTID; |
| 2303 | } |
| 2304 | |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2305 | /** |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2306 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 2307 | * |
| 2308 | * Returns 0 on success, or -1 if the intid is invalid. |
| 2309 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2310 | int64_t api_interrupt_enable(uint32_t intid, bool enable, |
| 2311 | enum interrupt_type type, struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2312 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2313 | struct vcpu_locked current_locked; |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2314 | struct interrupts *interrupts = ¤t->interrupts; |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2315 | struct interrupt_descriptor *int_desc = NULL; |
| 2316 | struct vm *vm = current->vm; |
| 2317 | struct vm_locked vm_locked; |
| 2318 | |
| 2319 | int64_t ret = -1; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2320 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2321 | if (intid >= HF_NUM_INTIDS) { |
| 2322 | return -1; |
| 2323 | } |
| 2324 | |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2325 | vm_locked = vm_lock(vm); |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2326 | current_locked = vcpu_lock(current); |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2327 | |
| 2328 | int_desc = vm_interrupt_set_enable(vm_locked, intid, enable); |
| 2329 | |
| 2330 | if (!api_is_maintenance_virtual_interrupt(intid)) { |
| 2331 | if (int_desc == NULL) { |
| 2332 | dlog_error("%s: invalid interrupt ID.\n", __func__); |
| 2333 | goto out; |
| 2334 | } |
| 2335 | |
| 2336 | plat_interrupts_configure_interrupt(*int_desc); |
| 2337 | } |
| 2338 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2339 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2340 | /* |
| 2341 | * If it is pending and was not enabled before, increment the |
| 2342 | * count. |
| 2343 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2344 | if (vcpu_is_virt_interrupt_pending(interrupts, intid) && |
| 2345 | !vcpu_is_virt_interrupt_enabled(interrupts, intid)) { |
| 2346 | vcpu_interrupt_count_increment(current_locked, |
| 2347 | interrupts, intid); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2348 | } |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2349 | |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2350 | vcpu_virt_interrupt_set_enabled(interrupts, intid); |
| 2351 | vcpu_virt_interrupt_set_type(interrupts, intid, type); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2352 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2353 | /* |
| 2354 | * If it is pending and was enabled before, decrement the count. |
| 2355 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2356 | if (vcpu_is_virt_interrupt_pending(interrupts, intid) && |
| 2357 | vcpu_is_virt_interrupt_enabled(interrupts, intid)) { |
| 2358 | vcpu_interrupt_count_decrement(current_locked, |
| 2359 | interrupts, intid); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2360 | } |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2361 | vcpu_virt_interrupt_clear_enabled(interrupts, intid); |
| 2362 | vcpu_virt_interrupt_set_type(interrupts, intid, |
| 2363 | INTERRUPT_TYPE_IRQ); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2366 | ret = 0; |
| 2367 | |
| 2368 | out: |
| 2369 | vm_unlock(&vm_locked); |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2370 | vcpu_unlock(¤t_locked); |
J-Alves | cc54204 | 2024-07-24 19:13:22 +0100 | [diff] [blame] | 2371 | |
| 2372 | return ret; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 2377 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 2378 | * HF_INVALID_INTID if there are no pending interrupts. |
| 2379 | */ |
Madhukar Pappireddy | c64d064 | 2024-08-07 16:55:46 -0500 | [diff] [blame] | 2380 | uint32_t api_interrupt_get(struct vcpu_locked current_locked) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2381 | { |
Raghu Krishnamurthy | 61a025b | 2022-07-20 08:37:04 -0700 | [diff] [blame] | 2382 | uint32_t i; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2383 | uint32_t first_interrupt = HF_INVALID_INTID; |
Madhukar Pappireddy | c64d064 | 2024-08-07 16:55:46 -0500 | [diff] [blame] | 2384 | struct interrupts *interrupts = ¤t_locked.vcpu->interrupts; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* |
| 2387 | * Find the first enabled and pending interrupt ID, return it, and |
| 2388 | * deactivate it. |
| 2389 | */ |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2390 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 2391 | uint32_t enabled_and_pending = |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2392 | interrupts->interrupt_enabled.bitmap[i] & |
| 2393 | interrupts->interrupt_pending.bitmap[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2394 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2395 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2396 | uint8_t bit_index = ctz(enabled_and_pending); |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2397 | |
| 2398 | first_interrupt = |
| 2399 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2400 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2401 | /* |
| 2402 | * Mark it as no longer pending and decrement the count. |
| 2403 | */ |
J-Alves | b8730e9 | 2024-08-07 18:28:55 +0100 | [diff] [blame] | 2404 | vcpu_interrupt_clear_decrement(current_locked, |
| 2405 | first_interrupt); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | } |
| 2408 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2409 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2410 | return first_interrupt; |
| 2411 | } |
| 2412 | |
| 2413 | /** |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2414 | * Negotiate the FF-A version to be used for this FF-A instance. |
| 2415 | * See section 13.2 of the FF-A v1.2 ALP1 spec. |
| 2416 | * |
| 2417 | * Returns Hafnium's version number (`FFA_VERSION_COMPILED`) on success. |
| 2418 | * Returns `FFA_NOT_SUPPORTED` on error: |
| 2419 | * - The version is invalid (highest bit set). |
| 2420 | * - The requested version is incompatible. |
| 2421 | * - The version has already been negotiated and cannot be changed. |
| 2422 | */ |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2423 | struct ffa_value api_ffa_version(struct vcpu *current, |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2424 | enum ffa_version requested_version) |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2425 | { |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2426 | static_assert(sizeof(enum ffa_version) == 4, |
| 2427 | "enum ffa_version must be 4 bytes wide"); |
| 2428 | |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2429 | const struct ffa_value error = {.func = (uint32_t)FFA_NOT_SUPPORTED}; |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2430 | struct vm_locked current_vm_locked; |
| 2431 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2432 | if (!ffa_version_is_valid(requested_version)) { |
| 2433 | dlog_error( |
| 2434 | "FFA_VERSION: requested version %#x is invalid " |
| 2435 | "(highest bit must be zero)\n", |
| 2436 | requested_version); |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2437 | return error; |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 2438 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2439 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2440 | if (!ffa_versions_are_compatible(requested_version, |
| 2441 | FFA_VERSION_COMPILED)) { |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2442 | dlog_error( |
| 2443 | "FFA_VERSION: requested version v%u.%u is not " |
| 2444 | "compatible with v%u.%u\n", |
| 2445 | ffa_version_get_major(requested_version), |
| 2446 | ffa_version_get_minor(requested_version), |
| 2447 | ffa_version_get_major(FFA_VERSION_COMPILED), |
| 2448 | ffa_version_get_minor(FFA_VERSION_COMPILED)); |
| 2449 | return error; |
J-Alves | 3cc9d6f | 2023-07-20 16:46:15 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2452 | current_vm_locked = vm_lock(current->vm); |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2453 | |
| 2454 | if (current_vm_locked.vm->ffa_version_negotiated && |
| 2455 | requested_version != current_vm_locked.vm->ffa_version) { |
| 2456 | vm_unlock(¤t_vm_locked); |
| 2457 | dlog_error( |
| 2458 | "FFA_VERSION: Cannot change FF-A version after other " |
| 2459 | "FF-A calls have been made\n"); |
| 2460 | return error; |
| 2461 | } |
| 2462 | |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2463 | current_vm_locked.vm->ffa_version = requested_version; |
| 2464 | vm_unlock(¤t_vm_locked); |
| 2465 | |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 2466 | return (struct ffa_value){.func = (uint32_t)FFA_VERSION_COMPILED}; |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2467 | } |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 2468 | |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2469 | /** |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2470 | * Helper for success return of FFA_FEATURES. |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2471 | */ |
| 2472 | struct ffa_value api_ffa_feature_success(uint32_t arg2) |
| 2473 | { |
| 2474 | return (struct ffa_value){ |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2475 | .func = FFA_SUCCESS_32, |
| 2476 | .arg1 = 0U, |
| 2477 | .arg2 = arg2, |
| 2478 | }; |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2481 | static struct ffa_value ffa_features_function(uint32_t func, |
| 2482 | uint32_t input_property, |
| 2483 | struct vcpu *current) |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2484 | { |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2485 | const enum ffa_version ffa_version = current->vm->ffa_version; |
Karl Meakin | f1ed5f1 | 2024-02-22 15:57:36 +0000 | [diff] [blame] | 2486 | const bool el0_partition = current->vm->el0_partition; |
| 2487 | |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2488 | switch (func) { |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2489 | /* Check support of the given Function ID. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2490 | case FFA_ERROR_32: |
| 2491 | case FFA_SUCCESS_32: |
| 2492 | case FFA_INTERRUPT_32: |
| 2493 | case FFA_VERSION_32: |
| 2494 | case FFA_FEATURES_32: |
| 2495 | case FFA_RX_RELEASE_32: |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 2496 | case FFA_RXTX_UNMAP_32: |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 2497 | case FFA_PARTITION_INFO_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2498 | case FFA_ID_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2499 | case FFA_MSG_WAIT_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2500 | case FFA_RUN_32: |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 2501 | case FFA_MEM_DONATE_64: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2502 | case FFA_MEM_DONATE_32: |
| 2503 | case FFA_MEM_LEND_32: |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 2504 | case FFA_MEM_LEND_64: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2505 | case FFA_MEM_SHARE_32: |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 2506 | case FFA_MEM_SHARE_64: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2507 | case FFA_MEM_RETRIEVE_RESP_32: |
| 2508 | case FFA_MEM_RELINQUISH_32: |
| 2509 | case FFA_MEM_RECLAIM_32: |
J-Alves | ff676c1 | 2022-05-13 17:25:33 +0100 | [diff] [blame] | 2510 | case FFA_MEM_FRAG_RX_32: |
| 2511 | case FFA_MEM_FRAG_TX_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 2512 | case FFA_MSG_SEND_DIRECT_RESP_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2513 | case FFA_MSG_SEND_DIRECT_RESP_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 2514 | case FFA_MSG_SEND_DIRECT_REQ_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2515 | case FFA_MSG_SEND_DIRECT_REQ_32: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2516 | |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 2517 | /* FF-A v1.1 features. */ |
| 2518 | case FFA_SPM_ID_GET_32: |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2519 | case FFA_NOTIFICATION_BITMAP_CREATE_32: |
| 2520 | case FFA_NOTIFICATION_BITMAP_DESTROY_32: |
| 2521 | case FFA_NOTIFICATION_BIND_32: |
| 2522 | case FFA_NOTIFICATION_UNBIND_32: |
| 2523 | case FFA_NOTIFICATION_SET_32: |
| 2524 | case FFA_NOTIFICATION_GET_32: |
| 2525 | case FFA_NOTIFICATION_INFO_GET_64: |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2526 | case FFA_MSG_SEND2_32: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2527 | if (FFA_VERSION_1_1 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2528 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2529 | } |
| 2530 | |
Kathleen Capella | e4fe296 | 2023-09-01 17:08:47 -0400 | [diff] [blame] | 2531 | /* FF-A v1.2 features. */ |
| 2532 | case FFA_CONSOLE_LOG_32: |
| 2533 | case FFA_CONSOLE_LOG_64: |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 2534 | case FFA_PARTITION_INFO_GET_REGS_64: |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2535 | case FFA_MSG_SEND_DIRECT_REQ2_64: |
Kathleen Capella | 087e502 | 2023-09-07 18:04:15 -0400 | [diff] [blame] | 2536 | case FFA_MSG_SEND_DIRECT_RESP2_64: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2537 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2538 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2541 | return api_ffa_feature_success(0); |
| 2542 | |
Karl Meakin | 5a22264 | 2024-06-20 10:23:37 +0100 | [diff] [blame] | 2543 | /* These functions are only supported on S-EL0 partitions. */ |
| 2544 | case FFA_MEM_PERM_GET_32: |
| 2545 | case FFA_MEM_PERM_SET_32: |
| 2546 | case FFA_MEM_PERM_GET_64: |
| 2547 | case FFA_MEM_PERM_SET_64: |
| 2548 | if (!(vm_id_is_current_world(current->vm->id) && |
| 2549 | el0_partition)) { |
| 2550 | dlog_verbose( |
| 2551 | "FFA_FEATURE: %s is only supported on S-EL0 " |
| 2552 | "partitions\n", |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2553 | ffa_func_name(func)); |
Karl Meakin | 5a22264 | 2024-06-20 10:23:37 +0100 | [diff] [blame] | 2554 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2555 | } |
| 2556 | return api_ffa_feature_success(0); |
| 2557 | |
Karl Meakin | 1a8c0cd | 2024-06-20 10:26:12 +0100 | [diff] [blame] | 2558 | case FFA_SECONDARY_EP_REGISTER_64: |
| 2559 | if (FFA_VERSION_COMPILED < FFA_VERSION_1_1) { |
| 2560 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2561 | } |
| 2562 | |
| 2563 | if (!(vm_id_is_current_world(current->vm->id) && |
| 2564 | current->vm->vcpu_count > 1)) { |
| 2565 | dlog_verbose( |
| 2566 | "FFA_FEATURE: %s is only supported on SPs with " |
| 2567 | "more than 1 vCPU\n", |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2568 | ffa_func_name(func)); |
Karl Meakin | 1a8c0cd | 2024-06-20 10:26:12 +0100 | [diff] [blame] | 2569 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2570 | } |
| 2571 | return api_ffa_feature_success(0); |
| 2572 | |
Karl Meakin | 650cb14 | 2024-06-20 10:31:57 +0100 | [diff] [blame] | 2573 | /* |
| 2574 | * This function is restricted to the secure virtual FF-A instance (i.e. |
| 2575 | * only report success to SPs). |
| 2576 | */ |
| 2577 | case FFA_YIELD_32: |
| 2578 | if (!vm_id_is_current_world(current->vm->id)) { |
| 2579 | dlog_verbose( |
| 2580 | "FFA_FEATURES: %s is only supported at secure " |
| 2581 | "virtual FF-A instance\n", |
| 2582 | ffa_func_name(FFA_YIELD_32)); |
| 2583 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2584 | } |
| 2585 | return api_ffa_feature_success(0); |
| 2586 | |
Karl Meakin | 0fd6729 | 2024-02-06 17:33:05 +0000 | [diff] [blame] | 2587 | case FFA_RXTX_MAP_64: { |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2588 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2589 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Karl Meakin | 0fd6729 | 2024-02-06 17:33:05 +0000 | [diff] [blame] | 2592 | uint32_t arg2 = 0; |
| 2593 | struct ffa_features_rxtx_map_params params = { |
| 2594 | .min_buf_size = FFA_RXTX_MAP_MIN_BUF_4K, |
| 2595 | .mbz = 0, |
| 2596 | .max_buf_size = |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2597 | (ffa_version >= FFA_VERSION_1_2) |
Karl Meakin | 0fd6729 | 2024-02-06 17:33:05 +0000 | [diff] [blame] | 2598 | ? FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT |
| 2599 | : 0, |
| 2600 | }; |
| 2601 | |
| 2602 | memcpy_s(&arg2, sizeof(arg2), ¶ms, sizeof(params)); |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2603 | return api_ffa_feature_success(arg2); |
Karl Meakin | 0fd6729 | 2024-02-06 17:33:05 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 2606 | case FFA_MEM_RETRIEVE_REQ_64: |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2607 | case FFA_MEM_RETRIEVE_REQ_32: { |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2608 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2609 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2610 | } |
| 2611 | |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2612 | if (ANY_BITS_SET(input_property, |
| 2613 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT, |
| 2614 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT) || |
| 2615 | IS_BIT_SET(input_property, |
| 2616 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT)) { |
| 2617 | dlog_warning( |
| 2618 | "FFA_FEATURES: Bits[%u:%u] and Bit[%u] of " |
| 2619 | "input_property should be 0 (input_property = " |
| 2620 | "%#x)\n", |
| 2621 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT, |
| 2622 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT, |
| 2623 | FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT, |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2624 | input_property); |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2627 | if (ffa_version >= FFA_VERSION_1_1 && |
| 2628 | (input_property & |
| 2629 | FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) { |
Karl Meakin | 4e8e479 | 2024-07-05 16:28:42 +0100 | [diff] [blame] | 2630 | dlog_verbose( |
| 2631 | "FFA_FEATURES: NS bit support must be 1\n"); |
Karl Meakin | 6fd6c1d | 2024-05-13 12:09:13 +0100 | [diff] [blame] | 2632 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | return api_ffa_feature_success( |
| 2636 | FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2637 | FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT | |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2638 | FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT); |
Karl Meakin | 49ec1e4 | 2024-05-10 13:08:24 +0100 | [diff] [blame] | 2639 | } |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2640 | |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2641 | default: |
| 2642 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | static struct ffa_value ffa_features_feature(enum ffa_feature_id feature, |
| 2647 | uint32_t input_property, |
| 2648 | struct vcpu *current) |
| 2649 | { |
| 2650 | const bool el0_partition = current->vm->el0_partition; |
| 2651 | |
| 2652 | if (ANY_BITS_SET(feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT, |
| 2653 | FFA_FEATURES_FEATURE_MBZ_LO_BIT)) { |
| 2654 | dlog_verbose( |
| 2655 | "FFA_FEATURES: feature ID %#x is invalid (bits [%u:%u] " |
| 2656 | "must be zero)\n", |
| 2657 | feature, FFA_FEATURES_FEATURE_MBZ_HI_BIT, |
| 2658 | FFA_FEATURES_FEATURE_MBZ_LO_BIT); |
| 2659 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2660 | } |
| 2661 | if (input_property != 0) { |
| 2662 | dlog_verbose( |
| 2663 | "FFA_FEATURES: input_property must be 0 " |
| 2664 | "(input_property = %#x)\n", |
| 2665 | input_property); |
| 2666 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2667 | } |
| 2668 | |
| 2669 | switch (feature) { |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2670 | /* Check support of a feature provided respective feature ID. */ |
Karl Meakin | 17c9ad3 | 2024-04-12 16:41:00 +0100 | [diff] [blame] | 2671 | |
| 2672 | /* |
| 2673 | * For NPI and MEI, report the IDs as supported only to partitions at |
| 2674 | * the virtual FF-A instances. |
| 2675 | */ |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2676 | case FFA_FEATURE_NPI: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2677 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2678 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
Karl Meakin | f1ed5f1 | 2024-02-22 15:57:36 +0000 | [diff] [blame] | 2681 | if (el0_partition) { |
| 2682 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2683 | } |
Karl Meakin | 17c9ad3 | 2024-04-12 16:41:00 +0100 | [diff] [blame] | 2684 | if (!vm_id_is_current_world(current->vm->id)) { |
| 2685 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2686 | } |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2687 | return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID); |
Karl Meakin | 17c9ad3 | 2024-04-12 16:41:00 +0100 | [diff] [blame] | 2688 | |
| 2689 | case FFA_FEATURE_MEI: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2690 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2691 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2692 | } |
J-Alves | c4d9ae8 | 2024-05-14 10:15:03 +0100 | [diff] [blame] | 2693 | if (el0_partition) { |
| 2694 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2695 | } |
Karl Meakin | 17c9ad3 | 2024-04-12 16:41:00 +0100 | [diff] [blame] | 2696 | if (!vm_id_is_current_world(current->vm->id)) { |
| 2697 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2698 | } |
| 2699 | return api_ffa_feature_success(HF_MANAGED_EXIT_INTID); |
| 2700 | |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2701 | case FFA_FEATURE_SRI: |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2702 | if (FFA_VERSION_1_2 > FFA_VERSION_COMPILED) { |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2703 | return ffa_error(FFA_NOT_SUPPORTED); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
Karl Meakin | cb6642c | 2024-02-27 14:43:45 +0000 | [diff] [blame] | 2706 | if (!ffa_is_vm_id(current->vm->id)) { |
| 2707 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2708 | } |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2709 | return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID); |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 2710 | |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2711 | /* Platform specific feature support. */ |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2712 | default: |
Karl Meakin | 4271ff9 | 2024-05-13 12:33:15 +0100 | [diff] [blame] | 2713 | return ffa_error(FFA_NOT_SUPPORTED); |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2714 | } |
| 2715 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2716 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2717 | /** |
Karl Meakin | 9c5b1d3 | 2024-06-24 12:10:36 +0100 | [diff] [blame] | 2718 | * Discovery function returning information about the implementation of optional |
| 2719 | * FF-A interfaces. See section 13.3 of the FF-A v1.2 ALP1 spec. |
| 2720 | * |
| 2721 | * `function_or_feature_id` is interpreted as either a function ID or a feature |
| 2722 | * ID, depending on the value of bit 31. |
| 2723 | * When it is a feature ID, bits [30:8] MBZ and input_property MBZ. |
| 2724 | * |
| 2725 | * Returns `FFA_SUCCESS` if the interface is supported. |
| 2726 | * Returns `FFA_NOT_SUPPORTED` if the interface is not supported or the |
| 2727 | * parameters are invalid. |
| 2728 | */ |
| 2729 | struct ffa_value api_ffa_features(uint32_t function_or_feature_id, |
| 2730 | uint32_t input_property, struct vcpu *current) |
| 2731 | { |
| 2732 | return IS_BIT_UNSET(function_or_feature_id, FFA_FEATURES_FEATURE_BIT) |
| 2733 | ? ffa_features_feature(function_or_feature_id, |
| 2734 | input_property, current) |
| 2735 | : ffa_features_function(function_or_feature_id, |
| 2736 | input_property, current); |
| 2737 | } |
| 2738 | |
| 2739 | /** |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2740 | * FF-A specification states that x2/w2 Must Be Zero for FFA_MSG_SEND_DIRECT_REQ |
| 2741 | * and FFA_MSG_SEND_DIRECT_RESP interfaces when used for partition messages. See |
| 2742 | * FF-A v1.2 Table 16.6: FFA_MSG_SEND_DIRECT_REQ function syntax. |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2743 | */ |
| 2744 | static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args) |
| 2745 | { |
| 2746 | return args.arg2 == 0U; |
| 2747 | } |
| 2748 | |
| 2749 | /** |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2750 | * Limits size of arguments in ffa_value structure to 32-bit. |
| 2751 | */ |
| 2752 | static struct ffa_value api_ffa_value_copy32(struct ffa_value args) |
| 2753 | { |
| 2754 | return (struct ffa_value){ |
| 2755 | .func = (uint32_t)args.func, |
| 2756 | .arg1 = (uint32_t)args.arg1, |
| 2757 | .arg2 = (uint32_t)0, |
| 2758 | .arg3 = (uint32_t)args.arg3, |
| 2759 | .arg4 = (uint32_t)args.arg4, |
| 2760 | .arg5 = (uint32_t)args.arg5, |
| 2761 | .arg6 = (uint32_t)args.arg6, |
| 2762 | .arg7 = (uint32_t)args.arg7, |
| 2763 | }; |
| 2764 | } |
| 2765 | |
| 2766 | /** |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2767 | * Helper to copy direct message payload, depending on SMC used, direct |
| 2768 | * messaging interface used, and expected registers size. |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2769 | */ |
| 2770 | static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args) |
| 2771 | { |
| 2772 | if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 || |
| 2773 | args.func == FFA_MSG_SEND_DIRECT_RESP_32) { |
| 2774 | return api_ffa_value_copy32(args); |
| 2775 | } |
| 2776 | |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2777 | if (args.func == FFA_MSG_SEND_DIRECT_REQ_64 || |
| 2778 | args.func == FFA_MSG_SEND_DIRECT_RESP_64) { |
| 2779 | args.arg2 = 0; |
| 2780 | } |
| 2781 | |
Kathleen Capella | db6a08b | 2023-10-04 13:42:39 -0400 | [diff] [blame] | 2782 | if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64) { |
| 2783 | args.extended_val.valid = true; |
| 2784 | } |
| 2785 | |
Kathleen Capella | 087e502 | 2023-09-07 18:04:15 -0400 | [diff] [blame] | 2786 | if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64) { |
| 2787 | args.arg2 = 0; |
| 2788 | args.arg3 = 0; |
| 2789 | } |
| 2790 | |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2791 | return args; |
| 2792 | } |
| 2793 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 2794 | /** |
| 2795 | * Return a pointer to the first UUID in `uuids` that is equal to |
| 2796 | * `target_uuid`. If `target_uuid` is 0-0-0-0, it matches any UUID. |
| 2797 | */ |
| 2798 | static struct ffa_uuid *ffa_uuid_find(struct ffa_uuid *uuids, |
| 2799 | size_t uuids_count, |
| 2800 | struct ffa_uuid target_uuid) |
| 2801 | { |
| 2802 | if (ffa_uuid_is_null(&target_uuid)) { |
| 2803 | return &uuids[0]; |
| 2804 | } |
| 2805 | |
| 2806 | for (size_t i = 0; i < uuids_count; i++) { |
| 2807 | if (ffa_uuid_is_null(&uuids[i])) { |
| 2808 | break; |
| 2809 | } |
| 2810 | if (ffa_uuid_equal(&target_uuid, &uuids[i])) { |
| 2811 | return &uuids[i]; |
| 2812 | } |
| 2813 | } |
| 2814 | return NULL; |
| 2815 | } |
| 2816 | |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2817 | static bool api_ffa_dir_msg_req2_is_uuid_valid(struct vm *receiver_vm, |
| 2818 | struct ffa_value args) |
| 2819 | { |
| 2820 | struct ffa_uuid target_uuid; |
| 2821 | |
Karl Meakin | 9478e32 | 2024-09-23 17:47:09 +0100 | [diff] [blame] | 2822 | ffa_uuid_from_u64x2(args.arg2, args.arg3, &target_uuid); |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2823 | |
Karl Meakin | fb9c2a2 | 2024-08-19 14:29:37 +0100 | [diff] [blame] | 2824 | return ffa_uuid_find(receiver_vm->uuids, PARTITION_MAX_UUIDS, |
| 2825 | target_uuid) != NULL; |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2829 | * Send an FF-A direct message request. |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2830 | * This handler covers both FFA_MSG_SEND_DIRECT_REQ_32/64 |
| 2831 | * and FFA_MSG_SEND_DIRECT_REQ2_64 (introduced in FF-A v1.2) with function-based |
| 2832 | * checks to accomodate for the difference between the ABIs. |
| 2833 | * |
| 2834 | * FFA_MSG_SEND_DIRECT_REQ2_64 works mostly the same as |
| 2835 | * FFA_MSG_SEND_DIRECT_REQ_32/64, but adds the ability to send a direct message |
| 2836 | * request to a specified UUID within a partition and the usage of an extended |
| 2837 | * range of registers (x4-x17, instead of x4-x7) to be used as part of the |
| 2838 | * message payload. |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2839 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 2840 | struct ffa_value api_ffa_msg_send_direct_req(ffa_id_t sender_vm_id, |
| 2841 | ffa_id_t receiver_vm_id, |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2842 | struct ffa_value args, |
| 2843 | struct vcpu *current, |
| 2844 | struct vcpu **next) |
| 2845 | { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 2846 | struct ffa_value ret; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2847 | struct vm *receiver_vm; |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2848 | struct vm_locked receiver_locked; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2849 | struct vcpu *receiver_vcpu; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 2850 | struct vcpu_locked current_locked; |
| 2851 | struct vcpu_locked receiver_vcpu_locked; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2852 | struct two_vcpu_locked vcpus_locked; |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 2853 | enum vcpu_state next_state = VCPU_STATE_RUNNING; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2854 | |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2855 | if ((args.func == FFA_MSG_SEND_DIRECT_REQ_32 || |
| 2856 | args.func == FFA_MSG_SEND_DIRECT_REQ_64) && |
| 2857 | !api_ffa_dir_msg_is_arg2_zero(args)) { |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2858 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2859 | } |
| 2860 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2861 | if (!plat_ffa_is_direct_request_valid(current, sender_vm_id, |
| 2862 | receiver_vm_id)) { |
J-Alves | e0caac3 | 2022-12-19 14:37:08 +0000 | [diff] [blame] | 2863 | dlog_verbose("Invalid direct message request.\n"); |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 2864 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2867 | if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 2868 | return ret; |
| 2869 | } |
| 2870 | |
J-Alves | bd32c97 | 2024-02-02 16:20:04 +0000 | [diff] [blame] | 2871 | ret = api_ffa_interrupt_return(0); |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 2872 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2873 | receiver_vm = vm_find(receiver_vm_id); |
| 2874 | if (receiver_vm == NULL) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2875 | dlog_verbose("Invalid Receiver!\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2876 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2877 | } |
| 2878 | |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2879 | if (args.func == FFA_MSG_SEND_DIRECT_REQ2_64 && |
| 2880 | !api_ffa_dir_msg_req2_is_uuid_valid(receiver_vm, args)) { |
| 2881 | dlog_verbose("UUID unrecognized for this VM\n"); |
| 2882 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2883 | } |
| 2884 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2885 | /* |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 2886 | * Check if sender supports sending direct message req, and if |
| 2887 | * receiver supports receipt of direct message requests. |
| 2888 | */ |
Kathleen Capella | 41fea93 | 2023-06-23 17:39:28 -0400 | [diff] [blame] | 2889 | if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm, |
| 2890 | args.func)) { |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 2891 | return ffa_error(FFA_DENIED); |
| 2892 | } |
| 2893 | |
| 2894 | /* |
Olivier Deprez | c13a869 | 2022-04-08 17:47:14 +0200 | [diff] [blame] | 2895 | * Per FF-A EAC spec section 4.4.1 the firmware framework supports |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2896 | * UP (migratable) or MP partitions with a number of vCPUs matching the |
| 2897 | * number of PEs in the system. It further states that MP partitions |
| 2898 | * accepting direct request messages cannot migrate. |
| 2899 | */ |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 2900 | receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2901 | if (receiver_vcpu == NULL) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2902 | dlog_verbose("Invalid vCPU!\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2903 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2904 | } |
| 2905 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 2906 | /* |
| 2907 | * If VM must be locked, it must be done before any of its vCPUs are |
| 2908 | * locked. |
| 2909 | */ |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2910 | receiver_locked = vm_lock(receiver_vm); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 2911 | |
| 2912 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 2913 | vcpus_locked = vcpu_lock_both(current, receiver_vcpu); |
| 2914 | current_locked = vcpus_locked.vcpu1; |
| 2915 | receiver_vcpu_locked = vcpus_locked.vcpu2; |
| 2916 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2917 | /* |
| 2918 | * If destination vCPU is executing or already received an |
| 2919 | * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is |
| 2920 | * busy. There is a brief period of time where the vCPU state has |
| 2921 | * changed but regs_available is still false thus consider this case as |
| 2922 | * the vCPU not yet ready to receive a direct message request. |
| 2923 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 2924 | if (is_ffa_direct_msg_request_ongoing(receiver_vcpu_locked) || |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2925 | receiver_vcpu->state == VCPU_STATE_RUNNING || |
| 2926 | !receiver_vcpu->regs_available) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2927 | dlog_verbose("Receiver is busy with another request.\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2928 | ret = ffa_error(FFA_BUSY); |
| 2929 | goto out; |
| 2930 | } |
| 2931 | |
J-Alves | 7ea3f7e | 2024-03-27 11:05:11 +0000 | [diff] [blame] | 2932 | if (!plat_ffa_check_runtime_state_transition( |
| 2933 | current_locked, sender_vm_id, HF_INVALID_VM_ID, |
| 2934 | receiver_vcpu_locked, args.func, &next_state)) { |
| 2935 | ret = ffa_error(FFA_DENIED); |
| 2936 | goto out; |
| 2937 | } |
| 2938 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2939 | if (atomic_load_explicit(&receiver_vcpu->vm->aborting, |
| 2940 | memory_order_relaxed)) { |
| 2941 | if (receiver_vcpu->state != VCPU_STATE_ABORTED) { |
Kathleen Capella | 6e9765b | 2023-07-11 17:44:58 -0400 | [diff] [blame] | 2942 | dlog_verbose( |
| 2943 | "Receiver VM %#x aborted, cannot run vCPU %u\n", |
| 2944 | receiver_vcpu->vm->id, |
| 2945 | vcpu_index(receiver_vcpu)); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2946 | receiver_vcpu->state = VCPU_STATE_ABORTED; |
| 2947 | } |
| 2948 | |
| 2949 | ret = ffa_error(FFA_ABORTED); |
| 2950 | goto out; |
| 2951 | } |
| 2952 | |
| 2953 | switch (receiver_vcpu->state) { |
| 2954 | case VCPU_STATE_OFF: |
| 2955 | case VCPU_STATE_RUNNING: |
| 2956 | case VCPU_STATE_ABORTED: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2957 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2958 | case VCPU_STATE_BLOCKED: |
| 2959 | case VCPU_STATE_PREEMPTED: |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2960 | dlog_verbose("Receiver's vCPU can't receive request (%u)!\n", |
| 2961 | vcpu_index(receiver_vcpu)); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2962 | ret = ffa_error(FFA_BUSY); |
| 2963 | goto out; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2964 | case VCPU_STATE_WAITING: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2965 | /* |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2966 | * We expect target vCPU to be in WAITING state after either |
| 2967 | * having called ffa_msg_wait or sent a direct message response. |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2968 | */ |
| 2969 | break; |
| 2970 | } |
| 2971 | |
| 2972 | /* Inject timer interrupt if any pending */ |
| 2973 | if (arch_timer_pending(&receiver_vcpu->regs)) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 2974 | api_interrupt_inject_locked(receiver_vcpu_locked, |
| 2975 | HF_VIRTUAL_TIMER_INTID, |
| 2976 | current_locked, NULL); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2977 | |
| 2978 | arch_timer_mask(&receiver_vcpu->regs); |
| 2979 | } |
| 2980 | |
| 2981 | /* The receiver vCPU runs upon direct message invocation */ |
| 2982 | receiver_vcpu->cpu = current->cpu; |
| 2983 | receiver_vcpu->state = VCPU_STATE_RUNNING; |
| 2984 | receiver_vcpu->regs_available = false; |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 2985 | receiver_vcpu->direct_request_origin.is_ffa_req2 = |
| 2986 | (args.func == FFA_MSG_SEND_DIRECT_REQ2_64); |
| 2987 | receiver_vcpu->direct_request_origin.vm_id = sender_vm_id; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2988 | |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2989 | arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args)); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2990 | |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 2991 | assert(!vm_id_is_current_world(current->vm->id) || |
| 2992 | next_state == VCPU_STATE_BLOCKED); |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2993 | current->state = VCPU_STATE_BLOCKED; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2994 | |
Raghu Krishnamurthy | a9ccf12 | 2023-03-27 20:42:01 -0700 | [diff] [blame] | 2995 | plat_ffa_wind_call_chain_ffa_direct_req( |
| 2996 | current_locked, receiver_vcpu_locked, sender_vm_id); |
Madhukar Pappireddy | 49fe670 | 2022-06-21 17:52:23 -0500 | [diff] [blame] | 2997 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2998 | /* Switch to receiver vCPU targeted to by direct msg request */ |
| 2999 | *next = receiver_vcpu; |
| 3000 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 3001 | if (!receiver_locked.vm->el0_partition) { |
| 3002 | /* |
| 3003 | * If the scheduler in the system is giving CPU cycles to the |
| 3004 | * receiver, due to pending notifications, inject the NPI |
| 3005 | * interrupt. Following call assumes that '*next' has been set |
| 3006 | * to receiver_vcpu. |
| 3007 | */ |
| 3008 | plat_ffa_inject_notification_pending_interrupt( |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3009 | receiver_vcpu_locked, current_locked, receiver_locked); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3012 | /* |
| 3013 | * Since this flow will lead to a VM switch, the return value will not |
| 3014 | * be applied to current vCPU. |
| 3015 | */ |
| 3016 | |
| 3017 | out: |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3018 | vcpu_unlock(&receiver_vcpu_locked); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 3019 | vm_unlock(&receiver_locked); |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3020 | vcpu_unlock(¤t_locked); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3021 | |
| 3022 | return ret; |
| 3023 | } |
| 3024 | |
| 3025 | /** |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3026 | * Resume the target vCPU after the current vCPU sent a direct response. |
| 3027 | * Current vCPU moves to waiting state. |
| 3028 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3029 | void api_ffa_resume_direct_resp_target(struct vcpu_locked current_locked, |
| 3030 | struct vcpu **next, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 3031 | ffa_id_t receiver_vm_id, |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3032 | struct ffa_value to_ret, |
| 3033 | bool is_nwd_call_chain) |
| 3034 | { |
Raghu Krishnamurthy | a9ccf12 | 2023-03-27 20:42:01 -0700 | [diff] [blame] | 3035 | if (plat_ffa_is_spmd_lp_id(receiver_vm_id) || |
| 3036 | !vm_id_is_current_world(receiver_vm_id)) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3037 | *next = api_switch_to_other_world(current_locked, to_ret, |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3038 | VCPU_STATE_WAITING); |
| 3039 | |
| 3040 | /* End of NWd scheduled call chain. */ |
| 3041 | assert(!is_nwd_call_chain || |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3042 | (current_locked.vcpu->call_chain.prev_node == NULL)); |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3043 | } else if (receiver_vm_id == HF_PRIMARY_VM_ID) { |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3044 | *next = api_switch_to_primary(current_locked, to_ret, |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3045 | VCPU_STATE_WAITING); |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3046 | } else if (vm_id_is_current_world(receiver_vm_id)) { |
| 3047 | /* |
| 3048 | * It is expected the receiver_vm_id to be from an SP, otherwise |
| 3049 | * 'plat_ffa_is_direct_response_valid' should have |
| 3050 | * made function return error before getting to this point. |
| 3051 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3052 | *next = api_switch_to_vm(current_locked, to_ret, |
| 3053 | VCPU_STATE_WAITING, receiver_vm_id); |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3054 | } else { |
| 3055 | panic("Invalid direct message response invocation"); |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3060 | * Send an FF-A direct message response. |
Kathleen Capella | 087e502 | 2023-09-07 18:04:15 -0400 | [diff] [blame] | 3061 | * This handler covers both FFA_MSG_SEND_DIRECT_RESP_32/64 |
| 3062 | * and FFA_MSG_SEND_DIRECT_RESP2_64 (introduced in FF-A v1.2) with |
| 3063 | * function-based checks to accomodate for the difference between the ABIs. |
| 3064 | * |
| 3065 | * FFA_MSG_SEND_DIRECT_RESP2_64 is used to respond to requests sent via |
| 3066 | * FFA_MSG_SEND_DIRECT_REQ2_64 and adds the usage of an extended range |
| 3067 | * of registers (x4-x17, instead of x4-x7) to be used as part of the |
| 3068 | * message payload. |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3069 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 3070 | struct ffa_value api_ffa_msg_send_direct_resp(ffa_id_t sender_vm_id, |
| 3071 | ffa_id_t receiver_vm_id, |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3072 | struct ffa_value args, |
| 3073 | struct vcpu *current, |
| 3074 | struct vcpu **next) |
| 3075 | { |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 3076 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3077 | struct vcpu_locked next_locked = (struct vcpu_locked){ |
| 3078 | .vcpu = NULL, |
| 3079 | }; |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 3080 | enum vcpu_state next_state = VCPU_STATE_RUNNING; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3081 | struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32}; |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 3082 | struct ffa_value signal_interrupt = |
| 3083 | (struct ffa_value){.func = FFA_INTERRUPT_32}; |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3084 | struct ffa_value to_ret = api_ffa_dir_msg_value(args); |
| 3085 | struct two_vcpu_locked vcpus_locked; |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 3086 | bool received_req2; |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 3087 | |
Kathleen Capella | 3523887 | 2024-01-12 15:05:52 -0500 | [diff] [blame] | 3088 | /* |
| 3089 | * If using FFA_MSG_SEND_DIRECT_RESP, the caller's |
| 3090 | * - x2 MBZ for partition messages |
| 3091 | * - x8-x17 SBZ if caller's FF-A version >= FF-A v1.2 |
| 3092 | */ |
| 3093 | if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) { |
| 3094 | if (!api_ffa_dir_msg_is_arg2_zero(args)) { |
| 3095 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3096 | } |
| 3097 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3098 | if (current->vm->ffa_version >= FFA_VERSION_1_2 && |
Kathleen Capella | 3523887 | 2024-01-12 15:05:52 -0500 | [diff] [blame] | 3099 | !api_extended_args_are_zero(&args)) { |
| 3100 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3101 | } |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 3104 | if (!plat_ffa_is_direct_response_valid(current, sender_vm_id, |
| 3105 | receiver_vm_id)) { |
J-Alves | e0caac3 | 2022-12-19 14:37:08 +0000 | [diff] [blame] | 3106 | dlog_verbose("Invalid direct response call.\n"); |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 3107 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3110 | current_locked = vcpu_lock(current); |
| 3111 | |
| 3112 | if (!plat_ffa_check_runtime_state_transition( |
| 3113 | current_locked, sender_vm_id, receiver_vm_id, next_locked, |
| 3114 | args.func, &next_state)) { |
| 3115 | ret = ffa_error(FFA_DENIED); |
| 3116 | goto out; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 3117 | } |
| 3118 | |
Madhukar Pappireddy | d456ac2 | 2023-06-26 15:29:34 -0500 | [diff] [blame] | 3119 | assert(!vm_id_is_current_world(current->vm->id) || |
| 3120 | next_state == VCPU_STATE_WAITING); |
Madhukar Pappireddy | 469fa71 | 2022-06-21 18:53:33 -0500 | [diff] [blame] | 3121 | |
| 3122 | /* |
| 3123 | * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a |
| 3124 | * defined originator. |
| 3125 | */ |
| 3126 | if (!is_ffa_direct_msg_request_ongoing(current_locked)) { |
| 3127 | /* |
| 3128 | * Sending direct response but direct request origin |
| 3129 | * vCPU is not set. |
| 3130 | */ |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3131 | ret = ffa_error(FFA_DENIED); |
| 3132 | goto out; |
Madhukar Pappireddy | 469fa71 | 2022-06-21 18:53:33 -0500 | [diff] [blame] | 3133 | } |
| 3134 | |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 3135 | received_req2 = current->direct_request_origin.is_ffa_req2; |
| 3136 | |
| 3137 | if (args.func != FFA_MSG_SEND_DIRECT_RESP2_64 && received_req2) { |
| 3138 | dlog_verbose( |
| 3139 | "%s: FFA_MSG_SEND_DIRECT_RESP must be used with " |
| 3140 | "FFA_MSG_SEND_DIRECT_REQ.\n", |
| 3141 | __func__); |
| 3142 | ret = ffa_error(FFA_DENIED); |
| 3143 | goto out; |
| 3144 | } else if (args.func == FFA_MSG_SEND_DIRECT_RESP2_64 && |
| 3145 | !received_req2) { |
| 3146 | dlog_verbose( |
| 3147 | "%s: FFA_MSG_SEND_DIRECT_RESP2 must be used with " |
| 3148 | "FFA_MSG_SEND_DIRECT_REQ2.\n", |
| 3149 | __func__); |
| 3150 | ret = ffa_error(FFA_DENIED); |
| 3151 | goto out; |
| 3152 | } |
| 3153 | |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 3154 | if (api_ffa_is_managed_exit_ongoing(current_locked)) { |
J-Alves | bd32c97 | 2024-02-02 16:20:04 +0000 | [diff] [blame] | 3155 | struct interrupts *interrupts = ¤t->interrupts; |
| 3156 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3157 | /* |
Madhukar Pappireddy | 469fa71 | 2022-06-21 18:53:33 -0500 | [diff] [blame] | 3158 | * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal |
Madhukar Pappireddy | ed4ab94 | 2021-08-03 14:22:53 -0500 | [diff] [blame] | 3159 | * a secure interrupt to a SP that is performing managed exit. |
| 3160 | * We have taken a implementation defined choice to not allow |
| 3161 | * Managed exit while a SP is processing a secure interrupt. |
| 3162 | */ |
Madhukar Pappireddy | 7945bb5 | 2024-08-20 15:22:41 -0500 | [diff] [blame] | 3163 | CHECK(current->scheduling_mode != SPMC_MODE); |
Madhukar Pappireddy | ed4ab94 | 2021-08-03 14:22:53 -0500 | [diff] [blame] | 3164 | |
Madhukar Pappireddy | dd6fdfb | 2021-12-14 12:30:36 -0600 | [diff] [blame] | 3165 | plat_interrupts_set_priority_mask(current->priority_mask); |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 3166 | /* |
| 3167 | * A SP may be signaled a managed exit but actually not trap |
| 3168 | * the virtual interrupt, probably because it has virtual |
| 3169 | * interrupts masked, and emit direct resp. In this case the |
| 3170 | * managed exit operation is considered completed and it would |
| 3171 | * also need to clear the pending managed exit flag for the SP |
| 3172 | * vCPU. |
| 3173 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 3174 | current->processing_managed_exit = false; |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 3175 | |
| 3176 | if (vcpu_is_virt_interrupt_pending(interrupts, |
| 3177 | HF_MANAGED_EXIT_INTID)) { |
J-Alves | b8730e9 | 2024-08-07 18:28:55 +0100 | [diff] [blame] | 3178 | vcpu_interrupt_clear_decrement(current_locked, |
| 3179 | HF_MANAGED_EXIT_INTID); |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 3180 | } |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Kathleen Capella | e468c11 | 2023-12-13 17:56:28 -0500 | [diff] [blame] | 3183 | /* Clear direct request origin vm_id and request type for the caller. */ |
| 3184 | current->direct_request_origin.is_ffa_req2 = false; |
| 3185 | current->direct_request_origin.vm_id = HF_INVALID_VM_ID; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3186 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3187 | api_ffa_resume_direct_resp_target(current_locked, next, receiver_vm_id, |
| 3188 | to_ret, false); |
| 3189 | |
| 3190 | /* |
| 3191 | * Unlock current vCPU to allow it to be locked together with next |
| 3192 | * vcpu. |
| 3193 | */ |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 3194 | vcpu_unlock(¤t_locked); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3195 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3196 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 3197 | vcpus_locked = vcpu_lock_both(current, *next); |
| 3198 | current_locked = vcpus_locked.vcpu1; |
| 3199 | next_locked = vcpus_locked.vcpu2; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3200 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3201 | plat_ffa_unwind_call_chain_ffa_direct_resp(current_locked, next_locked); |
J-Alves | bd32c97 | 2024-02-02 16:20:04 +0000 | [diff] [blame] | 3202 | |
| 3203 | /* |
| 3204 | * Check if there is a pending secure interrupt. |
| 3205 | * If there is, return back to the caller with FFA_INTERRUPT, |
| 3206 | * and set the `next` vcpu in a preempted state. |
| 3207 | */ |
| 3208 | if (plat_ffa_intercept_call(current_locked, next_locked, |
| 3209 | &signal_interrupt)) { |
| 3210 | ret = signal_interrupt; |
| 3211 | *next = NULL; |
| 3212 | } |
| 3213 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3214 | vcpu_unlock(&next_locked); |
Madhukar Pappireddy | c0fb87e | 2022-06-21 17:59:15 -0500 | [diff] [blame] | 3215 | |
Madhukar Pappireddy | bd10e57 | 2023-03-06 16:39:49 -0600 | [diff] [blame] | 3216 | out: |
| 3217 | vcpu_unlock(¤t_locked); |
| 3218 | return ret; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 3221 | static bool api_memory_region_check_flags( |
| 3222 | struct ffa_memory_region *memory_region, uint32_t share_func) |
| 3223 | { |
| 3224 | switch (share_func) { |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 3225 | case FFA_MEM_SHARE_64: |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 3226 | case FFA_MEM_SHARE_32: |
| 3227 | if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != |
| 3228 | 0U) { |
| 3229 | return false; |
| 3230 | } |
| 3231 | /* Intentional fall-through */ |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 3232 | case FFA_MEM_LEND_64: |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 3233 | case FFA_MEM_LEND_32: |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 3234 | case FFA_MEM_DONATE_64: |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 3235 | case FFA_MEM_DONATE_32: { |
| 3236 | /* Bits 31:2 Must Be Zero. */ |
| 3237 | ffa_memory_receiver_flags_t to_mask = |
| 3238 | ~(FFA_MEMORY_REGION_FLAG_CLEAR | |
| 3239 | FFA_MEMORY_REGION_FLAG_TIME_SLICE); |
| 3240 | |
| 3241 | if ((memory_region->flags & to_mask) != 0U) { |
| 3242 | return false; |
| 3243 | } |
| 3244 | break; |
| 3245 | } |
| 3246 | default: |
| 3247 | panic("Check for mem send calls only.\n"); |
| 3248 | } |
| 3249 | |
| 3250 | /* Last check reserved values are 0 */ |
| 3251 | return true; |
| 3252 | } |
| 3253 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3254 | /* |
| 3255 | * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0. |
| 3256 | */ |
| 3257 | static void api_ffa_memory_region_v1_1_from_v1_0( |
| 3258 | struct ffa_memory_region_v1_0 *memory_region_v1_0, |
| 3259 | struct ffa_memory_region *memory_region_v1_1) |
| 3260 | { |
| 3261 | memory_region_v1_1->sender = memory_region_v1_0->sender; |
J-Alves | 0084706 | 2022-10-03 17:08:44 +0100 | [diff] [blame] | 3262 | memory_region_v1_1->handle = memory_region_v1_0->handle; |
Karl Meakin | 84710f3 | 2023-10-12 15:14:49 +0100 | [diff] [blame] | 3263 | memory_region_v1_1->attributes = |
| 3264 | ffa_memory_attributes_extend(memory_region_v1_0->attributes); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3265 | memory_region_v1_1->flags = memory_region_v1_0->flags; |
| 3266 | memory_region_v1_1->tag = memory_region_v1_0->tag; |
| 3267 | memory_region_v1_1->memory_access_desc_size = |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3268 | sizeof(struct ffa_memory_access_v1_0); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3269 | memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count; |
Daniel Boulby | 41ef8ba | 2023-10-13 17:01:22 +0100 | [diff] [blame] | 3270 | memory_region_v1_1->receivers_offset = sizeof(struct ffa_memory_region); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3271 | |
| 3272 | /* Zero reserved fields. */ |
| 3273 | for (uint32_t i = 0; i < 3U; i++) { |
| 3274 | memory_region_v1_1->reserved[i] = 0U; |
| 3275 | } |
| 3276 | } |
| 3277 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3278 | /** |
| 3279 | * Updates a v1.0 transaction descriptor to v1.1. This gives us the |
| 3280 | * memory_access_desc_size field we need for forwards compatability. |
| 3281 | * Copy the receivers and composite descriptors to the new struct. |
| 3282 | * We also check the fields in the v1.0 transaction descriptor and return: |
| 3283 | * - FFA_ERROR FFA_INVALID_PARAMETERS: If any of the fields are not valid |
| 3284 | * values, eg the reserved fields are not 0, receiver_count is too large or |
| 3285 | * composite offsets are not 0 for retrieve requests or in bounds for send |
| 3286 | * requests. |
| 3287 | * - FFA ERROR FFA_NOT_SUPPORTED: If an invalid ffa_version is supplied to the |
| 3288 | * function. Or the fragment length is more than a single page. |
| 3289 | * - FFA_ERROR FFA_NO_MEMORY: If we do not have enough memory for a scratch |
| 3290 | * memory transaction descriptor. |
| 3291 | * - FFA_SUCCESS: If a successful update has occured. |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3292 | */ |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3293 | static struct ffa_value api_ffa_memory_transaction_descriptor_v1_1_from_v1_0( |
| 3294 | void *allocated, uint32_t *fragment_length, uint32_t *total_length, |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3295 | enum ffa_version ffa_version, bool send_transaction) |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3296 | { |
| 3297 | struct ffa_memory_region_v1_0 *memory_region_v1_0; |
| 3298 | struct ffa_memory_region *memory_region_v1_1 = NULL; |
| 3299 | struct ffa_composite_memory_region *composite_v1_0; |
| 3300 | struct ffa_composite_memory_region *composite_v1_1; |
| 3301 | size_t receivers_length; |
| 3302 | size_t space_left; |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3303 | size_t receivers_end; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3304 | size_t composite_offset_v1_1; |
| 3305 | size_t composite_offset_v1_0; |
| 3306 | size_t fragment_constituents_size; |
| 3307 | size_t fragment_length_v1_1; |
| 3308 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3309 | assert(fragment_length != NULL); |
| 3310 | assert(total_length != NULL); |
| 3311 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3312 | if (ffa_version >= FFA_VERSION_1_1) { |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3313 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 3314 | } |
| 3315 | |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3316 | if (ffa_version != FFA_VERSION_1_0) { |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3317 | dlog_verbose("%s: Unsupported FF-A version %x\n", __func__, |
| 3318 | ffa_version); |
| 3319 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3320 | } |
| 3321 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3322 | dlog_verbose( |
J-Alves | 0a824e9 | 2024-04-26 16:20:12 +0100 | [diff] [blame] | 3323 | "Updating memory transaction descriptor from v1.0 to v1.1.\n"); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3324 | |
| 3325 | memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated; |
| 3326 | |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3327 | receivers_length = sizeof(struct ffa_memory_access_v1_0) * |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3328 | memory_region_v1_0->receiver_count; |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3329 | receivers_end = sizeof(struct ffa_memory_region) + receivers_length; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3330 | |
| 3331 | /* |
| 3332 | * Check the specified composite offset of v1.0 descriptor, and that all |
| 3333 | * receivers were configured with the same offset. |
| 3334 | */ |
| 3335 | composite_offset_v1_0 = |
| 3336 | memory_region_v1_0->receivers[0].composite_memory_region_offset; |
| 3337 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3338 | /* Determine the composite offset for v1.1 descriptor. */ |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3339 | if (send_transaction) { |
| 3340 | fragment_constituents_size = |
| 3341 | *fragment_length - composite_offset_v1_0 - |
| 3342 | sizeof(struct ffa_composite_memory_region); |
| 3343 | fragment_length_v1_1 = |
| 3344 | receivers_end + |
| 3345 | sizeof(struct ffa_composite_memory_region) + |
| 3346 | fragment_constituents_size; |
| 3347 | composite_offset_v1_1 = receivers_end; |
| 3348 | } else { |
| 3349 | fragment_constituents_size = 0; |
| 3350 | fragment_length_v1_1 = receivers_end; |
| 3351 | composite_offset_v1_1 = 0; |
| 3352 | } |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3353 | |
| 3354 | /* |
| 3355 | * Currently only support the simpler cases: memory transaction |
| 3356 | * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE. |
| 3357 | * TODO: allocate the entries needed for this fragment_length_v1_1. |
| 3358 | * - Check corner when v1.1 descriptor converted size surpasses |
| 3359 | * the size of the entry. |
| 3360 | */ |
| 3361 | if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) { |
| 3362 | dlog_verbose( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 3363 | "Translation of FF-A v1.0 descriptors for over %lu is " |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3364 | "unsupported.", |
| 3365 | MM_PPOOL_ENTRY_SIZE); |
| 3366 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3367 | } |
| 3368 | |
| 3369 | space_left = fragment_length_v1_1; |
| 3370 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3371 | /* |
| 3372 | * Allocate a page of memory to construct the v1.1 memory descriptor. |
| 3373 | * Earlier we checked that the fragment_length_v1_1 would not be larger |
| 3374 | * than a page. |
| 3375 | */ |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3376 | memory_region_v1_1 = |
| 3377 | (struct ffa_memory_region *)mpool_alloc(&api_page_pool); |
| 3378 | if (memory_region_v1_1 == NULL) { |
| 3379 | return ffa_error(FFA_NO_MEMORY); |
| 3380 | } |
| 3381 | |
| 3382 | /* Translate header from v1.0 to v1.1. */ |
| 3383 | api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0, |
| 3384 | memory_region_v1_1); |
| 3385 | |
| 3386 | space_left -= sizeof(struct ffa_memory_region); |
| 3387 | |
| 3388 | /* Copy memory access information. */ |
Daniel Boulby | 41ef8ba | 2023-10-13 17:01:22 +0100 | [diff] [blame] | 3389 | memcpy_s((uint8_t *)memory_region_v1_1 + |
| 3390 | memory_region_v1_1->receivers_offset, |
| 3391 | space_left, memory_region_v1_0->receivers, receivers_length); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3392 | |
| 3393 | /* Initialize the memory access descriptors with composite offset. */ |
| 3394 | for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) { |
| 3395 | struct ffa_memory_access *receiver = |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3396 | ffa_memory_region_get_receiver(memory_region_v1_1, i); |
| 3397 | assert(receiver != NULL); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3398 | receiver->composite_memory_region_offset = |
| 3399 | composite_offset_v1_1; |
| 3400 | } |
| 3401 | |
| 3402 | space_left -= receivers_length; |
| 3403 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3404 | /* Composite memory descriptors to copy. */ |
| 3405 | if (send_transaction) { |
| 3406 | /* Init v1.1 composite. */ |
| 3407 | composite_v1_1 = (struct ffa_composite_memory_region |
| 3408 | *)((uint8_t *)memory_region_v1_1 + |
| 3409 | composite_offset_v1_1); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3410 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3411 | composite_v1_0 = ffa_memory_region_get_composite_v1_0( |
| 3412 | memory_region_v1_0, 0); |
| 3413 | composite_v1_1->constituent_count = |
| 3414 | composite_v1_0->constituent_count; |
| 3415 | composite_v1_1->page_count = composite_v1_0->page_count; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3416 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3417 | space_left -= sizeof(struct ffa_composite_memory_region); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3418 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3419 | /* Initialize v1.1 constituents. */ |
| 3420 | memcpy_s(composite_v1_1->constituents, space_left, |
| 3421 | composite_v1_0->constituents, |
| 3422 | fragment_constituents_size); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3423 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3424 | space_left -= fragment_constituents_size; |
| 3425 | } |
| 3426 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3427 | assert(space_left == 0U); |
| 3428 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3429 | /* |
| 3430 | * Remove the v1.0 fragment size, and resultant size of v1.1 fragment. |
| 3431 | */ |
| 3432 | *total_length = *total_length - *fragment_length + fragment_length_v1_1; |
| 3433 | *fragment_length = fragment_length_v1_1; |
| 3434 | |
| 3435 | /* |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3436 | * After successfully updating to v1.1 copy the descriptor to the |
| 3437 | * internal buffer given as a parameter (used to prevent TOCTOU attacks) |
| 3438 | * and free the scratch memory used to construct it. |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3439 | */ |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3440 | memcpy_s(allocated, MM_PPOOL_ENTRY_SIZE, memory_region_v1_1, |
| 3441 | *fragment_length); |
| 3442 | mpool_free(&api_page_pool, memory_region_v1_1); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3443 | |
| 3444 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 3445 | } |
| 3446 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3447 | struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length, |
| 3448 | uint32_t fragment_length, ipaddr_t address, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3449 | uint32_t page_count, struct vcpu *current) |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3450 | { |
| 3451 | struct vm *from = current->vm; |
| 3452 | struct vm *to; |
| 3453 | const void *from_msg; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3454 | void *allocated_entry; |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3455 | struct ffa_memory_region *memory_region = NULL; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3456 | struct ffa_value ret; |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3457 | bool targets_other_world = false; |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3458 | enum ffa_version ffa_version; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3459 | |
| 3460 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 3461 | /* |
| 3462 | * Hafnium only supports passing the descriptor in the TX |
| 3463 | * mailbox. |
| 3464 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3465 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3468 | if (fragment_length > length) { |
| 3469 | dlog_verbose( |
| 3470 | "Fragment length %d greater than total length %d.\n", |
| 3471 | fragment_length, length); |
| 3472 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3473 | } |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3474 | |
| 3475 | if (fragment_length > HF_MAILBOX_SIZE || |
| 3476 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3477 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | /* |
| 3481 | * Check that the sender has configured its send buffer. If the TX |
| 3482 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 3483 | * be safely accessed after releasing the lock since the TX mailbox |
| 3484 | * address can only be configured once. |
| 3485 | */ |
| 3486 | sl_lock(&from->lock); |
| 3487 | from_msg = from->mailbox.send; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3488 | ffa_version = from->ffa_version; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3489 | sl_unlock(&from->lock); |
| 3490 | |
| 3491 | if (from_msg == NULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3492 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3496 | * Copy the memory region descriptor to a fresh page from the memory |
| 3497 | * pool. This prevents the sender from changing it underneath us, and |
| 3498 | * also lets us keep it around in the share state table if needed. |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3499 | */ |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3500 | allocated_entry = mpool_alloc(&api_page_pool); |
| 3501 | if (allocated_entry == NULL) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3502 | dlog_verbose("Failed to allocate memory region copy.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3503 | return ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3504 | } |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3505 | |
J-Alves | 8f2150d | 2024-03-28 16:15:56 +0000 | [diff] [blame] | 3506 | if (!memcpy_trapped(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg, |
| 3507 | fragment_length)) { |
| 3508 | dlog_error( |
| 3509 | "%s: Failed to copy FF-A memory region descriptor.\n", |
| 3510 | __func__); |
| 3511 | return ffa_error(FFA_ABORTED); |
| 3512 | } |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3513 | |
Daniel Boulby | c7dc932 | 2023-10-27 15:12:07 +0100 | [diff] [blame] | 3514 | if (!ffa_memory_region_sanity_check(allocated_entry, ffa_version, |
| 3515 | fragment_length, true)) { |
| 3516 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3517 | goto out; |
| 3518 | } |
| 3519 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3520 | ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0( |
| 3521 | allocated_entry, &fragment_length, &length, ffa_version, true); |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3522 | if (ret.func != FFA_SUCCESS_32) { |
| 3523 | goto out; |
| 3524 | } |
| 3525 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3526 | memory_region = allocated_entry; |
| 3527 | |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3528 | if (fragment_length < sizeof(struct ffa_memory_region) + |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3529 | memory_region->memory_access_desc_size) { |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3530 | dlog_verbose( |
| 3531 | "Initial fragment length %d smaller than header size " |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 3532 | "%lu.\n", |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3533 | fragment_length, |
| 3534 | sizeof(struct ffa_memory_region) + |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3535 | memory_region->memory_access_desc_size); |
| 3536 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3537 | goto out; |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3538 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3539 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 3540 | if (!api_memory_region_check_flags(memory_region, share_func)) { |
| 3541 | dlog_verbose( |
| 3542 | "Memory region reserved arguments must be zero.\n"); |
| 3543 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3544 | goto out; |
| 3545 | } |
| 3546 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3547 | if (memory_region->receiver_count == 0U) { |
| 3548 | dlog_verbose("Receiver count can't be 0.\n"); |
| 3549 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3550 | goto out; |
| 3551 | } |
| 3552 | |
J-Alves | 95fbb31 | 2024-03-20 15:19:16 +0000 | [diff] [blame] | 3553 | if ((share_func == FFA_MEM_DONATE_32 || |
| 3554 | share_func == FFA_MEM_DONATE_64) && |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3555 | memory_region->receiver_count != 1U) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3556 | dlog_verbose( |
J-Alves | 0a824e9 | 2024-04-26 16:20:12 +0100 | [diff] [blame] | 3557 | "FFA_MEM_DONATE only supports one recipient. Specified " |
| 3558 | "%u\n", |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3559 | memory_region->receiver_count); |
| 3560 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3561 | goto out; |
| 3562 | } |
| 3563 | |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3564 | /* |
| 3565 | * Ensure that the receiver VM exists and isn't the same as the sender. |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3566 | * If there is a receiver from the other world, track it for later |
| 3567 | * forwarding if needed. |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3568 | */ |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3569 | for (uint32_t i = 0U; i < memory_region->receiver_count; i++) { |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3570 | struct ffa_memory_access *receiver = |
| 3571 | ffa_memory_region_get_receiver(memory_region, i); |
J-Alves | c9227c8 | 2024-04-24 21:00:58 +0100 | [diff] [blame] | 3572 | ffa_id_t receiver_id; |
| 3573 | |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3574 | assert(receiver != NULL); |
J-Alves | c9227c8 | 2024-04-24 21:00:58 +0100 | [diff] [blame] | 3575 | |
| 3576 | receiver_id = receiver->receiver_permissions.receiver; |
Daniel Boulby | d5ae44b | 2023-12-12 12:18:11 +0000 | [diff] [blame] | 3577 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3578 | to = vm_find(receiver_id); |
| 3579 | |
J-Alves | 7de61af | 2024-03-27 10:29:46 +0000 | [diff] [blame] | 3580 | if ((vm_id_is_current_world(receiver_id) && to == NULL) || |
| 3581 | to == from) { |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3582 | dlog_verbose("%s: invalid receiver.\n", __func__); |
| 3583 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3584 | goto out; |
| 3585 | } |
| 3586 | |
J-Alves | c9227c8 | 2024-04-24 21:00:58 +0100 | [diff] [blame] | 3587 | if (!plat_ffa_is_memory_send_valid( |
| 3588 | receiver_id, from->id, share_func, |
| 3589 | memory_region->receiver_count > 1)) { |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3590 | ret = ffa_error(FFA_DENIED); |
| 3591 | goto out; |
| 3592 | } |
| 3593 | |
| 3594 | /* Capture if any of the receivers is from the other world. */ |
| 3595 | if (!targets_other_world) { |
| 3596 | targets_other_world = |
| 3597 | !vm_id_is_current_world(receiver_id); |
| 3598 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 3601 | if (targets_other_world) { |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 3602 | ret = plat_ffa_other_world_mem_send( |
| 3603 | from, share_func, &memory_region, length, |
| 3604 | fragment_length, &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3605 | } else { |
| 3606 | struct vm_locked from_locked = vm_lock(from); |
| 3607 | |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3608 | ret = ffa_memory_send(from_locked, memory_region, length, |
| 3609 | fragment_length, share_func, |
| 3610 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3611 | /* |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3612 | * ffa_memory_send takes ownership of the memory_region, so |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3613 | * make sure we don't free it. |
| 3614 | */ |
| 3615 | memory_region = NULL; |
| 3616 | |
| 3617 | vm_unlock(&from_locked); |
| 3618 | } |
| 3619 | |
| 3620 | out: |
| 3621 | if (memory_region != NULL) { |
| 3622 | mpool_free(&api_page_pool, memory_region); |
| 3623 | } |
| 3624 | |
| 3625 | return ret; |
| 3626 | } |
| 3627 | |
J-Alves | c7270b7 | 2024-09-12 10:16:34 +0100 | [diff] [blame] | 3628 | /** |
| 3629 | * An FFA_MEM_RETRIEVE_REQ from the hypervisor must specify the handle of the |
| 3630 | * memory transaction it is querying and all other fields must be 0. |
| 3631 | */ |
| 3632 | static bool api_ffa_memory_hypervisor_retrieve_request_validate( |
| 3633 | struct ffa_memory_region *request, enum ffa_version version) |
| 3634 | { |
| 3635 | switch (version) { |
| 3636 | case FFA_VERSION_1_0: { |
| 3637 | struct ffa_memory_region_v1_0 *request_v1_0 = |
| 3638 | (struct ffa_memory_region_v1_0 *)request; |
| 3639 | |
| 3640 | return request_v1_0->sender == 0U && |
| 3641 | request_v1_0->attributes.shareability == 0U && |
| 3642 | request_v1_0->attributes.cacheability == 0U && |
| 3643 | request_v1_0->attributes.type == 0U && |
| 3644 | request_v1_0->attributes.security == 0U && |
| 3645 | request_v1_0->flags == 0U && request_v1_0->tag == 0U && |
| 3646 | request_v1_0->receiver_count == 0U && |
| 3647 | plat_ffa_memory_handle_allocated_by_current_world( |
| 3648 | request_v1_0->handle); |
| 3649 | } |
| 3650 | default: |
| 3651 | return request->sender == 0U && |
| 3652 | request->attributes.shareability == 0U && |
| 3653 | request->attributes.cacheability == 0U && |
| 3654 | request->attributes.type == 0U && |
| 3655 | request->attributes.security == 0U && |
| 3656 | request->flags == 0U && request->tag == 0U && |
| 3657 | request->memory_access_desc_size == 0U && |
| 3658 | request->receiver_count == 0U && |
| 3659 | request->receivers_offset == 0U && |
| 3660 | plat_ffa_memory_handle_allocated_by_current_world( |
| 3661 | request->handle); |
| 3662 | } |
| 3663 | } |
| 3664 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3665 | struct ffa_value api_ffa_mem_retrieve_req(uint32_t length, |
| 3666 | uint32_t fragment_length, |
| 3667 | ipaddr_t address, uint32_t page_count, |
| 3668 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3669 | { |
| 3670 | struct vm *to = current->vm; |
| 3671 | struct vm_locked to_locked; |
| 3672 | const void *to_msg; |
J-Alves | 0084706 | 2022-10-03 17:08:44 +0100 | [diff] [blame] | 3673 | void *retrieve_msg; |
| 3674 | struct ffa_memory_region *retrieve_request = NULL; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3675 | uint32_t message_buffer_size; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3676 | struct ffa_value ret; |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 3677 | enum ffa_version ffa_version; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3678 | |
| 3679 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 3680 | /* |
| 3681 | * Hafnium only supports passing the descriptor in the TX |
| 3682 | * mailbox. |
| 3683 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3684 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 3687 | if (fragment_length != length) { |
J-Alves | 33c47bf | 2022-09-29 11:36:20 +0100 | [diff] [blame] | 3688 | dlog_verbose("Fragmentation not supported.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3689 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3690 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3691 | |
J-Alves | 0084706 | 2022-10-03 17:08:44 +0100 | [diff] [blame] | 3692 | retrieve_msg = cpu_get_buffer(current->cpu); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3693 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 3694 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 3695 | dlog_verbose("Retrieve request too long.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3696 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | to_locked = vm_lock(to); |
| 3700 | to_msg = to->mailbox.send; |
J-Alves | 0084706 | 2022-10-03 17:08:44 +0100 | [diff] [blame] | 3701 | ffa_version = to->ffa_version; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3702 | |
| 3703 | if (to_msg == NULL) { |
| 3704 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3705 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3706 | goto out; |
| 3707 | } |
| 3708 | |
| 3709 | /* |
| 3710 | * Copy the retrieve request descriptor to an internal buffer, so that |
| 3711 | * the caller can't change it underneath us. |
| 3712 | */ |
J-Alves | 8f2150d | 2024-03-28 16:15:56 +0000 | [diff] [blame] | 3713 | if (!memcpy_trapped(retrieve_msg, message_buffer_size, to_msg, |
| 3714 | length)) { |
| 3715 | dlog_error( |
| 3716 | "%s: Failed to copy FF-A retrieve request " |
| 3717 | "descriptor.\n", |
| 3718 | __func__); |
| 3719 | ret = ffa_error(FFA_ABORTED); |
| 3720 | goto out; |
| 3721 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3722 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 3723 | if ((vm_is_mailbox_other_world_owned(to_locked) && |
| 3724 | !plat_ffa_acquire_receiver_rx(to_locked, &ret)) || |
| 3725 | vm_is_mailbox_busy(to_locked)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3726 | /* |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 3727 | * Can't retrieve memory information if the mailbox is |
| 3728 | * not available. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3729 | */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3730 | dlog_verbose("%s: RX buffer not ready.\n", __func__); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3731 | ret = ffa_error(FFA_BUSY); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3732 | goto out; |
| 3733 | } |
| 3734 | |
Daniel Boulby | 296ee70 | 2023-11-28 13:36:55 +0000 | [diff] [blame] | 3735 | if (!is_ffa_hypervisor_retrieve_request(retrieve_msg)) { |
Daniel Boulby | 44e9b3b | 2024-01-17 12:21:44 +0000 | [diff] [blame] | 3736 | if (!ffa_memory_region_sanity_check(retrieve_msg, ffa_version, |
| 3737 | fragment_length, false)) { |
| 3738 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3739 | goto out; |
| 3740 | } |
| 3741 | /* |
| 3742 | * If required, transform the retrieve request to FF-A v1.1. |
| 3743 | */ |
| 3744 | ret = api_ffa_memory_transaction_descriptor_v1_1_from_v1_0( |
| 3745 | retrieve_msg, &fragment_length, &length, ffa_version, |
| 3746 | false); |
Daniel Boulby | c7dc932 | 2023-10-27 15:12:07 +0100 | [diff] [blame] | 3747 | |
Daniel Boulby | 44e9b3b | 2024-01-17 12:21:44 +0000 | [diff] [blame] | 3748 | if (ret.func != FFA_SUCCESS_32) { |
| 3749 | goto out; |
| 3750 | } |
J-Alves | c7270b7 | 2024-09-12 10:16:34 +0100 | [diff] [blame] | 3751 | } else { |
| 3752 | if (!api_ffa_memory_hypervisor_retrieve_request_validate( |
| 3753 | retrieve_msg, ffa_version)) { |
| 3754 | dlog_verbose( |
| 3755 | "All fields except the handle in the " |
| 3756 | "memory access descriptor must be zero for a " |
| 3757 | "hypervisor retrieve request.\n"); |
| 3758 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3759 | goto out; |
| 3760 | } |
J-Alves | 0084706 | 2022-10-03 17:08:44 +0100 | [diff] [blame] | 3761 | } |
| 3762 | |
Daniel Boulby | 36e0bdb | 2023-10-09 12:00:41 +0100 | [diff] [blame] | 3763 | retrieve_request = retrieve_msg; |
Kathleen Capella | 578784f | 2023-09-01 18:03:27 -0400 | [diff] [blame] | 3764 | |
J-Alves | b5084cf | 2022-07-06 14:20:12 +0100 | [diff] [blame] | 3765 | if (plat_ffa_memory_handle_allocated_by_current_world( |
| 3766 | retrieve_request->handle)) { |
| 3767 | ret = ffa_memory_retrieve(to_locked, retrieve_request, length, |
| 3768 | &api_page_pool); |
| 3769 | } else { |
Daniel Boulby | fdd59f7 | 2023-10-19 16:43:19 +0100 | [diff] [blame] | 3770 | dlog_error("Invalid FF-A memory handle.\n"); |
| 3771 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
J-Alves | b5084cf | 2022-07-06 14:20:12 +0100 | [diff] [blame] | 3772 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3773 | out: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3774 | vm_unlock(&to_locked); |
| 3775 | return ret; |
| 3776 | } |
| 3777 | |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 3778 | /** |
| 3779 | * Copies the memory relinquish descriptor from the partition's TX buffer, to |
| 3780 | * the buffer of the local CPU. Do it safely, and return error if: |
| 3781 | * - FFA_ABORTED: if the `memcpy_trapped` fails. |
| 3782 | * - FFA_INVALID_PARAMETERS: if the size of the full memory relinquish |
| 3783 | * descriptor doesn't fit the local CPU buffer. |
| 3784 | * |
| 3785 | * Returns FFA_SUCCESS if copying goes well, and sets 'out_relinquish' |
| 3786 | * to the address of the cpu buffer with the relinquish descriptor. |
| 3787 | */ |
| 3788 | static struct ffa_value api_get_ffa_mem_relinquish_descriptor( |
| 3789 | struct vcpu *current, const void *from_msg, |
| 3790 | struct ffa_mem_relinquish **out_relinquish) |
| 3791 | { |
| 3792 | struct ffa_mem_relinquish *relinquish_request; |
| 3793 | uint32_t from_msg_size; |
| 3794 | uint32_t total_from_msg_size; |
| 3795 | uint32_t dst_size; |
| 3796 | vaddr_t dst; |
| 3797 | vaddr_t src; |
| 3798 | |
| 3799 | assert(from_msg != NULL); |
| 3800 | assert(out_relinquish != NULL); |
| 3801 | |
| 3802 | /* |
| 3803 | * Copy the relinquish descriptor to an internal buffer, so that the |
| 3804 | * caller can't change it underneath us. |
| 3805 | */ |
| 3806 | relinquish_request = |
| 3807 | (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu); |
| 3808 | |
| 3809 | /* Set the destination for the copy. */ |
| 3810 | dst = va_from_ptr(relinquish_request); |
| 3811 | src = va_from_ptr(from_msg); |
| 3812 | |
| 3813 | dst_size = cpu_get_buffer_size(current->cpu); |
| 3814 | |
| 3815 | /* Only copy the size to start with. */ |
| 3816 | from_msg_size = sizeof(struct ffa_mem_relinquish); |
| 3817 | total_from_msg_size = from_msg_size; |
| 3818 | |
| 3819 | if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src), |
| 3820 | from_msg_size)) { |
| 3821 | dlog_error( |
| 3822 | "%s: Failed to copy FF-A memory relinquish " |
| 3823 | "descriptor.\n", |
| 3824 | __func__); |
| 3825 | return ffa_error(FFA_ABORTED); |
| 3826 | } |
| 3827 | |
| 3828 | if (relinquish_request->endpoint_count != 1) { |
| 3829 | dlog_error("%s: relinquish descriptor must have 1 endpoint\n", |
| 3830 | __func__); |
| 3831 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * Increment the `dst` to the position right after the copied header. |
| 3836 | * Increment the `src` to point at the list of endpoints. |
| 3837 | * |
| 3838 | * Calculate the new `dst_size` which is the size of the allocated cpu |
| 3839 | * buffer, minus the size of the copied memory relinquish header. |
| 3840 | * |
| 3841 | * Only after the above: determine the new `from_msg_size` in accordance |
| 3842 | * to the endpoint count. |
| 3843 | */ |
| 3844 | dst = va_add(dst, from_msg_size); |
| 3845 | src = va_add(src, from_msg_size); |
| 3846 | |
| 3847 | /* |
| 3848 | * Check if it is safe to copy the rest of the message. |
| 3849 | * This also serves as a santiy check to 'endpoint_count'. |
| 3850 | * The size of what is left in the descriptor, based on endpoint_count, |
| 3851 | * shall not be bigger than the size of the mailbox minus the size of |
| 3852 | * the header which was previously copied in this function. |
| 3853 | */ |
| 3854 | dst_size -= from_msg_size; |
| 3855 | from_msg_size = relinquish_request->endpoint_count * sizeof(ffa_id_t); |
| 3856 | total_from_msg_size += from_msg_size; |
| 3857 | |
| 3858 | if (total_from_msg_size > HF_MAILBOX_SIZE || |
| 3859 | total_from_msg_size > dst_size) { |
| 3860 | dlog_verbose( |
| 3861 | "Relinquish message too long. Endpoint count: %u\n", |
| 3862 | relinquish_request->endpoint_count); |
| 3863 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3864 | } |
| 3865 | |
| 3866 | /* Copy the remaining fragment. */ |
| 3867 | if (!memcpy_trapped(ptr_from_va(dst), dst_size, ptr_from_va(src), |
| 3868 | from_msg_size)) { |
| 3869 | dlog_error("%s: Failed to copy FF-A relinquish request.\n", |
| 3870 | __func__); |
| 3871 | return ffa_error(FFA_ABORTED); |
| 3872 | } |
| 3873 | |
| 3874 | /* |
| 3875 | * Set the output address for the relinquish descriptor to the current |
| 3876 | * cpu's buffer. |
| 3877 | */ |
| 3878 | *out_relinquish = relinquish_request; |
| 3879 | |
| 3880 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 3881 | } |
| 3882 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3883 | struct ffa_value api_ffa_mem_relinquish(struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3884 | { |
| 3885 | struct vm *from = current->vm; |
| 3886 | struct vm_locked from_locked; |
| 3887 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3888 | struct ffa_value ret; |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 3889 | struct ffa_mem_relinquish *relinquish_request; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3890 | |
| 3891 | from_locked = vm_lock(from); |
| 3892 | from_msg = from->mailbox.send; |
| 3893 | |
| 3894 | if (from_msg == NULL) { |
| 3895 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3896 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3897 | goto out; |
| 3898 | } |
| 3899 | |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 3900 | ret = api_get_ffa_mem_relinquish_descriptor(current, from_msg, |
| 3901 | &relinquish_request); |
| 3902 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3903 | /* |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 3904 | * If the descriptor was safely copied, continue with the handling of |
| 3905 | * the retrieve request. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3906 | */ |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 3907 | if (ret.func == FFA_SUCCESS_32) { |
| 3908 | ret = ffa_memory_relinquish(from_locked, relinquish_request, |
| 3909 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3910 | } |
J-Alves | 8f2150d | 2024-03-28 16:15:56 +0000 | [diff] [blame] | 3911 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3912 | out: |
| 3913 | vm_unlock(&from_locked); |
| 3914 | return ret; |
| 3915 | } |
| 3916 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3917 | struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle, |
| 3918 | ffa_memory_region_flags_t flags, |
| 3919 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3920 | { |
| 3921 | struct vm *to = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3922 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3923 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 3924 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3925 | struct vm_locked to_locked = vm_lock(to); |
| 3926 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3927 | ret = ffa_memory_reclaim(to_locked, handle, flags, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3928 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3929 | |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3930 | vm_unlock(&to_locked); |
| 3931 | } else { |
J-Alves | fc19b37 | 2022-07-06 12:17:35 +0100 | [diff] [blame] | 3932 | ret = plat_ffa_other_world_mem_reclaim(to, handle, flags, |
| 3933 | &api_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | return ret; |
| 3937 | } |
| 3938 | |
| 3939 | struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle, |
| 3940 | uint32_t fragment_offset, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 3941 | ffa_id_t sender_vm_id, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3942 | struct vcpu *current) |
| 3943 | { |
| 3944 | struct vm *to = current->vm; |
| 3945 | struct vm_locked to_locked; |
| 3946 | struct ffa_value ret; |
| 3947 | |
| 3948 | /* Sender ID MBZ at virtual instance. */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3949 | if (vm_id_is_current_world(to->id)) { |
| 3950 | if (sender_vm_id != 0) { |
| 3951 | dlog_verbose("%s: Invalid sender.\n", __func__); |
| 3952 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3953 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | to_locked = vm_lock(to); |
| 3957 | |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 3958 | if (vm_is_mailbox_busy(to_locked)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3959 | /* |
| 3960 | * Can't retrieve memory information if the mailbox is not |
| 3961 | * available. |
| 3962 | */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3963 | dlog_verbose("%s: RX buffer not ready partition %x.\n", |
| 3964 | __func__, to_locked.vm->id); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3965 | ret = ffa_error(FFA_BUSY); |
| 3966 | goto out; |
| 3967 | } |
| 3968 | |
| 3969 | ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset, |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3970 | sender_vm_id, &api_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3971 | out: |
| 3972 | vm_unlock(&to_locked); |
| 3973 | return ret; |
| 3974 | } |
| 3975 | |
| 3976 | struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle, |
| 3977 | uint32_t fragment_length, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 3978 | ffa_id_t sender_vm_id, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3979 | struct vcpu *current) |
| 3980 | { |
| 3981 | struct vm *from = current->vm; |
| 3982 | const void *from_msg; |
| 3983 | void *fragment_copy; |
| 3984 | struct ffa_value ret; |
| 3985 | |
| 3986 | /* Sender ID MBZ at virtual instance. */ |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 3987 | if (vm_id_is_current_world(from->id) && sender_vm_id != 0) { |
| 3988 | dlog_verbose("Invalid sender."); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3989 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3990 | } |
| 3991 | |
| 3992 | /* |
| 3993 | * Check that the sender has configured its send buffer. If the TX |
| 3994 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 3995 | * be safely accessed after releasing the lock since the TX mailbox |
| 3996 | * address can only be configured once. |
| 3997 | */ |
| 3998 | sl_lock(&from->lock); |
| 3999 | from_msg = from->mailbox.send; |
| 4000 | sl_unlock(&from->lock); |
| 4001 | |
| 4002 | if (from_msg == NULL) { |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 4003 | dlog_verbose("Mailbox from %x is not set.\n", from->id); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 4004 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | * Copy the fragment to a fresh page from the memory pool. This prevents |
| 4009 | * the sender from changing it underneath us, and also lets us keep it |
| 4010 | * around in the share state table if needed. |
| 4011 | */ |
| 4012 | if (fragment_length > HF_MAILBOX_SIZE || |
| 4013 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
| 4014 | dlog_verbose( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4015 | "Fragment length %d larger than mailbox size %zu.\n", |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 4016 | fragment_length, HF_MAILBOX_SIZE); |
| 4017 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4018 | } |
| 4019 | if (fragment_length < sizeof(struct ffa_memory_region_constituent) || |
| 4020 | fragment_length % sizeof(struct ffa_memory_region_constituent) != |
| 4021 | 0) { |
| 4022 | dlog_verbose("Invalid fragment length %d.\n", fragment_length); |
| 4023 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4024 | } |
| 4025 | fragment_copy = mpool_alloc(&api_page_pool); |
| 4026 | if (fragment_copy == NULL) { |
| 4027 | dlog_verbose("Failed to allocate fragment copy.\n"); |
| 4028 | return ffa_error(FFA_NO_MEMORY); |
| 4029 | } |
J-Alves | 8f2150d | 2024-03-28 16:15:56 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (!memcpy_trapped(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, |
| 4032 | fragment_length)) { |
| 4033 | dlog_error("%s: Failed to copy fragment.\n", __func__); |
| 4034 | return ffa_error(FFA_ABORTED); |
| 4035 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 4036 | |
| 4037 | /* |
| 4038 | * Hafnium doesn't support fragmentation of memory retrieve requests |
| 4039 | * (because it doesn't support caller-specified mappings, so a request |
| 4040 | * will never be larger than a single page), so this must be part of a |
| 4041 | * memory send (i.e. donate, lend or share) request. |
| 4042 | * |
| 4043 | * We can tell from the handle whether the memory transaction is for the |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 4044 | * other world or not. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 4045 | */ |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 4046 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 4047 | struct vm_locked from_locked = vm_lock(from); |
| 4048 | |
| 4049 | ret = ffa_memory_send_continue(from_locked, fragment_copy, |
| 4050 | fragment_length, handle, |
| 4051 | &api_page_pool); |
| 4052 | /* |
| 4053 | * `ffa_memory_send_continue` takes ownership of the |
| 4054 | * fragment_copy, so we don't need to free it here. |
| 4055 | */ |
| 4056 | vm_unlock(&from_locked); |
| 4057 | } else { |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 4058 | ret = plat_ffa_other_world_mem_send_continue( |
| 4059 | from, fragment_copy, fragment_length, handle, |
| 4060 | &api_page_pool); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 4061 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 4062 | |
| 4063 | return ret; |
| 4064 | } |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 4065 | |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 4066 | /** |
| 4067 | * Register an entry point for a vCPU in warm boot cases. |
| 4068 | * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER. |
| 4069 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 4070 | struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point, |
| 4071 | struct vcpu *current) |
| 4072 | { |
| 4073 | struct vm_locked vm_locked; |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 4074 | struct vcpu_locked current_locked; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 4075 | |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 4076 | /* |
| 4077 | * Reject if interface is not supported at this FF-A instance |
| 4078 | * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP. |
| 4079 | */ |
| 4080 | if (!plat_ffa_is_secondary_ep_register_supported() || |
Karl Meakin | 8204182 | 2024-05-20 11:14:34 +0100 | [diff] [blame] | 4081 | vm_is_up(current->vm)) { |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 4082 | return ffa_error(FFA_NOT_SUPPORTED); |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * No further check is made on the address validity |
| 4087 | * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known |
| 4088 | * from the VM or vCPU structure. |
| 4089 | * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1: |
| 4090 | * For each SP [...] the Framework assumes that the same entry point |
| 4091 | * address is used for initializing any execution context during a |
| 4092 | * secondary cold boot. |
| 4093 | * If this function is invoked multiple times, then the entry point |
| 4094 | * address specified in the last valid invocation must be used by the |
| 4095 | * callee. |
| 4096 | */ |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 4097 | current_locked = vcpu_lock(current); |
| 4098 | if (current->rt_model != RTM_SP_INIT) { |
| 4099 | dlog_error( |
| 4100 | "FFA_SECONDARY_EP_REGISTER can only be called while " |
| 4101 | "vCPU in run-time state for initialization.\n"); |
| 4102 | vcpu_unlock(¤t_locked); |
| 4103 | return ffa_error(FFA_DENIED); |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 4104 | } |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 4105 | vcpu_unlock(¤t_locked); |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 4106 | |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 4107 | vm_locked = vm_lock(current->vm); |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 4108 | vm_locked.vm->secondary_ep = entry_point; |
| 4109 | vm_unlock(&vm_locked); |
| 4110 | |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 4111 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 4112 | } |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 4113 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4114 | struct ffa_value api_ffa_notification_bitmap_create(ffa_id_t vm_id, |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 4115 | ffa_vcpu_count_t vcpu_count, |
| 4116 | struct vcpu *current) |
| 4117 | { |
J-Alves | 7ccaccf | 2024-02-01 14:25:23 +0000 | [diff] [blame] | 4118 | const struct ffa_value ret = |
| 4119 | plat_ffa_is_notifications_bitmap_access_valid(current, vm_id); |
| 4120 | |
| 4121 | if (ffa_func_id(ret) != FFA_SUCCESS_32) { |
| 4122 | dlog_verbose( |
| 4123 | "FFA_NOTIFICATION_BITMAP_CREATE to be used by " |
| 4124 | "hypervisor for valid NWd VM IDs only (%x).\n", |
| 4125 | vm_id); |
| 4126 | return ret; |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count); |
| 4130 | } |
| 4131 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4132 | struct ffa_value api_ffa_notification_bitmap_destroy(ffa_id_t vm_id, |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 4133 | struct vcpu *current) |
| 4134 | { |
J-Alves | 7ccaccf | 2024-02-01 14:25:23 +0000 | [diff] [blame] | 4135 | const struct ffa_value ret = |
| 4136 | plat_ffa_is_notifications_bitmap_access_valid(current, vm_id); |
| 4137 | |
| 4138 | if (ffa_func_id(ret) != FFA_SUCCESS_32) { |
| 4139 | dlog_verbose( |
| 4140 | "FFA_NOTIFICATION_BITMAP_DESTROY to be used by " |
| 4141 | "hypervisor for valid NWd VM IDs only (%x).\n", |
| 4142 | vm_id); |
| 4143 | return ret; |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | return plat_ffa_notifications_bitmap_destroy(vm_id); |
| 4147 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4148 | |
| 4149 | struct ffa_value api_ffa_notification_update_bindings( |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4150 | ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4151 | ffa_notifications_bitmap_t notifications, bool is_bind, |
| 4152 | struct vcpu *current) |
| 4153 | { |
| 4154 | struct ffa_value ret = {.func = FFA_SUCCESS_32}; |
| 4155 | struct vm_locked receiver_locked; |
| 4156 | const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4157 | const ffa_id_t id_to_update = is_bind ? sender_vm_id : HF_INVALID_VM_ID; |
| 4158 | const ffa_id_t id_to_validate = |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4159 | is_bind ? HF_INVALID_VM_ID : sender_vm_id; |
J-Alves | 1daeaea | 2022-09-06 17:41:24 +0100 | [diff] [blame] | 4160 | const uint32_t flags_mbz = |
| 4161 | is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U; |
| 4162 | |
| 4163 | if ((flags_mbz & flags) != 0U) { |
| 4164 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4165 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4166 | |
| 4167 | if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id, |
| 4168 | receiver_vm_id)) { |
| 4169 | dlog_verbose("Invalid use of notifications bind interface.\n"); |
| 4170 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4171 | } |
| 4172 | |
J-Alves | b15e940 | 2021-09-08 11:44:42 +0100 | [diff] [blame] | 4173 | if (plat_ffa_notifications_update_bindings_forward( |
| 4174 | receiver_vm_id, sender_vm_id, flags, notifications, is_bind, |
| 4175 | &ret)) { |
J-Alves | b15e940 | 2021-09-08 11:44:42 +0100 | [diff] [blame] | 4176 | return ret; |
| 4177 | } |
| 4178 | |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4179 | if (notifications == 0U) { |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4180 | dlog_verbose("No notifications have been specified %lx.\n", |
J-Alves | 1512acc | 2024-02-01 16:57:10 +0000 | [diff] [blame] | 4181 | notifications); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4182 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4183 | } |
| 4184 | |
| 4185 | /** |
| 4186 | * This check assumes receiver is the current VM, and has been enforced |
| 4187 | * by 'plat_ffa_is_notifications_bind_valid'. |
| 4188 | */ |
| 4189 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 4190 | |
| 4191 | if (receiver_locked.vm == NULL) { |
| 4192 | dlog_verbose("Receiver doesn't exist!\n"); |
J-Alves | 7333dcf | 2024-02-01 17:25:10 +0000 | [diff] [blame] | 4193 | return ffa_error(FFA_INVALID_PARAMETERS); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
J-Alves | 3e55c4d | 2024-10-09 14:24:07 +0100 | [diff] [blame] | 4196 | if (receiver_locked.vm->ffa_version < FFA_VERSION_1_1) { |
| 4197 | dlog_verbose( |
| 4198 | "%s: caller (%x) version should be GE to FF-A v1.1.\n", |
| 4199 | __func__, receiver_locked.vm->id); |
| 4200 | ret = ffa_error(FFA_NOT_SUPPORTED); |
| 4201 | goto out; |
| 4202 | } |
| 4203 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 4204 | if (!vm_locked_are_notifications_enabled(receiver_locked)) { |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4205 | dlog_verbose("Notifications are not enabled.\n"); |
| 4206 | ret = ffa_error(FFA_NOT_SUPPORTED); |
| 4207 | goto out; |
| 4208 | } |
| 4209 | |
| 4210 | if (is_bind && vm_id_is_current_world(sender_vm_id) && |
| 4211 | vm_find(sender_vm_id) == NULL) { |
| 4212 | dlog_verbose("Sender VM does not exist!\n"); |
| 4213 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4214 | goto out; |
| 4215 | } |
| 4216 | |
| 4217 | /* |
| 4218 | * Can't bind/unbind notifications if at least one is bound to a |
| 4219 | * different sender. |
| 4220 | */ |
| 4221 | if (!vm_notifications_validate_bound_sender( |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 4222 | receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_validate, |
| 4223 | notifications)) { |
J-Alves | f020814 | 2024-02-01 17:28:58 +0000 | [diff] [blame] | 4224 | dlog_verbose( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4225 | "Sender %x not permitted to set notifications %lx to " |
J-Alves | f020814 | 2024-02-01 17:28:58 +0000 | [diff] [blame] | 4226 | "%x.\n", |
| 4227 | sender_vm_id, notifications, receiver_vm_id); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4228 | ret = ffa_error(FFA_DENIED); |
| 4229 | goto out; |
| 4230 | } |
| 4231 | |
| 4232 | /** |
| 4233 | * Check if there is a pending notification within those specified in |
| 4234 | * the bitmap. |
| 4235 | */ |
| 4236 | if (vm_are_notifications_pending(receiver_locked, |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 4237 | ffa_is_vm_id(sender_vm_id), |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4238 | notifications)) { |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4239 | dlog_verbose("Notifications within '%lx' pending.\n", |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4240 | notifications); |
| 4241 | ret = ffa_error(FFA_DENIED); |
| 4242 | goto out; |
| 4243 | } |
| 4244 | |
| 4245 | vm_notifications_update_bindings( |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 4246 | receiver_locked, ffa_is_vm_id(sender_vm_id), id_to_update, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 4247 | notifications, is_per_vcpu && is_bind); |
| 4248 | |
| 4249 | out: |
| 4250 | vm_unlock(&receiver_locked); |
| 4251 | return ret; |
| 4252 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4253 | |
| 4254 | struct ffa_value api_ffa_notification_set( |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4255 | ffa_id_t sender_vm_id, ffa_id_t receiver_vm_id, uint32_t flags, |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4256 | ffa_notifications_bitmap_t notifications, struct vcpu *current) |
| 4257 | { |
| 4258 | struct ffa_value ret; |
| 4259 | struct vm_locked receiver_locked; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4260 | /* |
| 4261 | * Check if is per-vCPU or global, and extracting vCPU ID according |
| 4262 | * to table 17.19 of the FF-A v1.1 Beta 0 spec. |
| 4263 | */ |
| 4264 | bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
| 4265 | ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16); |
J-Alves | 6b754a1 | 2024-02-14 14:26:30 +0000 | [diff] [blame] | 4266 | const uint32_t flags_mbz = |
| 4267 | ~(FFA_NOTIFICATIONS_FLAG_PER_VCPU | |
| 4268 | FFA_NOTIFICATIONS_FLAG_DELAY_SRI | (0xFFFFU << 16)); |
J-Alves | 41c5da3 | 2024-06-19 10:05:05 +0100 | [diff] [blame] | 4269 | const bool delay_sri = (FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) != 0U; |
J-Alves | 6b754a1 | 2024-02-14 14:26:30 +0000 | [diff] [blame] | 4270 | |
| 4271 | if ((flags_mbz & flags) != 0U) { |
| 4272 | dlog_verbose("%s: caller shouldn't set bits that MBZ.\n", |
| 4273 | __func__); |
| 4274 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4275 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4276 | |
J-Alves | 30ac91d | 2024-02-14 15:33:12 +0000 | [diff] [blame] | 4277 | /* Global notifications must target any vCPU. */ |
| 4278 | if (!is_per_vcpu && vcpu_id != 0U) { |
| 4279 | dlog_verbose( |
| 4280 | "For global notifications vCPU ID MBZ in call to set " |
| 4281 | "notifications.\n"); |
| 4282 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4283 | } |
| 4284 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4285 | if (!plat_ffa_is_notification_set_valid(current, sender_vm_id, |
| 4286 | receiver_vm_id)) { |
| 4287 | dlog_verbose("Invalid use of notifications set interface.\n"); |
| 4288 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4289 | } |
| 4290 | |
| 4291 | if (notifications == 0U) { |
| 4292 | dlog_verbose("No notifications have been specified.\n"); |
| 4293 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4294 | } |
| 4295 | |
J-Alves | 41c5da3 | 2024-06-19 10:05:05 +0100 | [diff] [blame] | 4296 | /* |
| 4297 | * The 'Delay Schedule Receiver interrupt flag' only applies to the |
| 4298 | * secure virtual FF-A instance. |
| 4299 | */ |
| 4300 | if (!vm_id_is_current_world(sender_vm_id) && delay_sri) { |
| 4301 | dlog_verbose( |
| 4302 | "The delay SRI flag can only be set at the secure " |
| 4303 | "virtual FF-A instance.\n"); |
| 4304 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4305 | } |
| 4306 | |
J-Alves | de7bd2f | 2021-09-09 19:54:35 +0100 | [diff] [blame] | 4307 | if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id, |
| 4308 | flags, notifications, &ret)) { |
| 4309 | return ret; |
| 4310 | } |
| 4311 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4312 | /* |
| 4313 | * This check assumes receiver is the current VM, and has been enforced |
| 4314 | * by 'plat_ffa_is_notification_set_valid'. |
| 4315 | */ |
| 4316 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 4317 | |
| 4318 | if (receiver_locked.vm == NULL) { |
| 4319 | dlog_verbose("Receiver ID is not valid.\n"); |
| 4320 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4321 | } |
| 4322 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 4323 | if (!vm_locked_are_notifications_enabled(receiver_locked)) { |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4324 | dlog_verbose("Receiver's notifications not enabled.\n"); |
| 4325 | ret = ffa_error(FFA_DENIED); |
| 4326 | goto out; |
| 4327 | } |
| 4328 | |
| 4329 | /* |
J-Alves | 89c2213 | 2024-02-14 15:44:41 +0000 | [diff] [blame] | 4330 | * Check the if the notifications are bound as global if per-vCPU flag |
| 4331 | * is set, or if they are bound as per-vCPU and caller setting as |
| 4332 | * global. In either case, return FFA_INVALID_PARAMETERS. |
| 4333 | */ |
| 4334 | if (vm_notifications_validate_binding( |
| 4335 | receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id, |
| 4336 | notifications, !is_per_vcpu)) { |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4337 | dlog_verbose("Notifications in %lx are %s\n", notifications, |
J-Alves | 89c2213 | 2024-02-14 15:44:41 +0000 | [diff] [blame] | 4338 | !is_per_vcpu ? "global" : "per-vCPU"); |
| 4339 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4340 | goto out; |
| 4341 | } |
| 4342 | |
| 4343 | /* |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4344 | * If notifications are not bound to the sender, they wouldn't be |
| 4345 | * enabled either for the receiver. |
| 4346 | */ |
| 4347 | if (!vm_notifications_validate_binding( |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 4348 | receiver_locked, ffa_is_vm_id(sender_vm_id), sender_vm_id, |
| 4349 | notifications, is_per_vcpu)) { |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4350 | dlog_verbose("Notifications bindings not valid.\n"); |
| 4351 | ret = ffa_error(FFA_DENIED); |
| 4352 | goto out; |
| 4353 | } |
| 4354 | |
| 4355 | if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) { |
| 4356 | dlog_verbose("Invalid VCPU ID!\n"); |
| 4357 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4358 | goto out; |
| 4359 | } |
| 4360 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 4361 | /* Set notifications pending. */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 4362 | vm_notifications_partition_set_pending( |
J-Alves | 661e1b7 | 2023-08-02 13:39:40 +0100 | [diff] [blame] | 4363 | receiver_locked, ffa_is_vm_id(sender_vm_id), notifications, |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 4364 | vcpu_id, is_per_vcpu); |
| 4365 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4366 | dlog_verbose("Set the notifications: %lx.\n", notifications); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4367 | |
J-Alves | 41c5da3 | 2024-06-19 10:05:05 +0100 | [diff] [blame] | 4368 | if (!delay_sri) { |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4369 | dlog_verbose("SRI was NOT delayed. vcpu: %u!\n", |
| 4370 | vcpu_index(current)); |
| 4371 | plat_ffa_sri_trigger_not_delayed(current->cpu); |
| 4372 | } else { |
J-Alves | ea8ccfe | 2024-10-09 11:47:24 +0100 | [diff] [blame] | 4373 | plat_ffa_sri_set_delayed(current->cpu); |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4374 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4375 | |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4376 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4377 | out: |
| 4378 | vm_unlock(&receiver_locked); |
| 4379 | |
| 4380 | return ret; |
| 4381 | } |
| 4382 | |
| 4383 | static struct ffa_value api_ffa_notification_get_success_return( |
| 4384 | ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm, |
| 4385 | ffa_notifications_bitmap_t from_framework) |
| 4386 | { |
| 4387 | return (struct ffa_value){ |
| 4388 | .func = FFA_SUCCESS_32, |
| 4389 | .arg1 = 0U, |
| 4390 | .arg2 = (uint32_t)from_sp, |
| 4391 | .arg3 = (uint32_t)(from_sp >> 32), |
| 4392 | .arg4 = (uint32_t)from_vm, |
| 4393 | .arg5 = (uint32_t)(from_vm >> 32), |
| 4394 | .arg6 = (uint32_t)from_framework, |
| 4395 | .arg7 = (uint32_t)(from_framework >> 32), |
| 4396 | }; |
| 4397 | } |
| 4398 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 4399 | struct ffa_value api_ffa_notification_get(ffa_id_t receiver_vm_id, |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4400 | ffa_vcpu_index_t vcpu_id, |
| 4401 | uint32_t flags, struct vcpu *current) |
| 4402 | { |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 4403 | ffa_notifications_bitmap_t framework_notifications = 0; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4404 | ffa_notifications_bitmap_t sp_notifications = 0; |
| 4405 | ffa_notifications_bitmap_t vm_notifications = 0; |
| 4406 | struct vm_locked receiver_locked; |
| 4407 | struct ffa_value ret; |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 4408 | const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP | |
| 4409 | FFA_NOTIFICATION_FLAG_BITMAP_SPM | |
| 4410 | FFA_NOTIFICATION_FLAG_BITMAP_SP | |
| 4411 | FFA_NOTIFICATION_FLAG_BITMAP_VM); |
| 4412 | |
| 4413 | /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */ |
| 4414 | if ((flags & flags_mbz) != 0U) { |
| 4415 | dlog_verbose( |
| 4416 | "Invalid flags bit(s) set in notifications get. [31:4] " |
| 4417 | "MBZ(%x)\n", |
| 4418 | flags); |
| 4419 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4420 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4421 | |
| 4422 | /* |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 4423 | * Following check should capture wrong uses of the interface, |
| 4424 | * depending on whether Hafnium is SPMC or hypervisor. On the |
| 4425 | * rest of the function it is assumed this condition is met. |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4426 | */ |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 4427 | if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id, |
| 4428 | flags)) { |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4429 | dlog_verbose("Invalid use of notifications get interface.\n"); |
| 4430 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4431 | } |
| 4432 | |
| 4433 | /* |
| 4434 | * This check assumes receiver is the current VM, and has been enforced |
| 4435 | * by `plat_ffa_is_notifications_get_valid`. |
| 4436 | */ |
| 4437 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 4438 | |
| 4439 | /* |
| 4440 | * `plat_ffa_is_notifications_get_valid` ensures following is never |
| 4441 | * true. |
| 4442 | */ |
| 4443 | CHECK(receiver_locked.vm != NULL); |
| 4444 | |
J-Alves | 6045881 | 2024-03-22 11:57:10 +0000 | [diff] [blame] | 4445 | if (receiver_locked.vm->vcpu_count <= vcpu_id) { |
J-Alves | 1abb334 | 2022-01-05 11:59:10 +0000 | [diff] [blame] | 4446 | dlog_verbose( |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 4447 | "Invalid VCPU ID %u. vcpu count %u current core: " |
| 4448 | "%zu!\n", |
J-Alves | 1abb334 | 2022-01-05 11:59:10 +0000 | [diff] [blame] | 4449 | vcpu_id, receiver_locked.vm->vcpu_count, |
| 4450 | cpu_index(current->cpu)); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4451 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4452 | goto out; |
| 4453 | } |
| 4454 | |
| 4455 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) { |
J-Alves | 98ff956 | 2021-09-09 14:39:41 +0100 | [diff] [blame] | 4456 | if (!plat_ffa_notifications_get_from_sp( |
| 4457 | receiver_locked, vcpu_id, &sp_notifications, |
| 4458 | &ret)) { |
| 4459 | dlog_verbose("Failed to get notifications from sps."); |
| 4460 | goto out; |
| 4461 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) { |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 4465 | vm_notifications = vm_notifications_partition_get_pending( |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4466 | receiver_locked, true, vcpu_id); |
| 4467 | } |
| 4468 | |
J-Alves | d605a09 | 2022-03-28 14:20:48 +0100 | [diff] [blame] | 4469 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U || |
| 4470 | (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) { |
| 4471 | if (!plat_ffa_notifications_get_framework_notifications( |
| 4472 | receiver_locked, &framework_notifications, flags, |
| 4473 | vcpu_id, &ret)) { |
| 4474 | dlog_verbose( |
| 4475 | "Failed to get notifications from " |
| 4476 | "framework.\n"); |
| 4477 | goto out; |
| 4478 | } |
| 4479 | } |
| 4480 | |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 4481 | ret = api_ffa_notification_get_success_return( |
| 4482 | sp_notifications, vm_notifications, framework_notifications); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4483 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 4484 | if (!receiver_locked.vm->el0_partition && |
| 4485 | !vm_are_global_notifications_pending(receiver_locked)) { |
| 4486 | vm_notifications_set_npi_injected(receiver_locked, false); |
| 4487 | } |
| 4488 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 4489 | out: |
| 4490 | vm_unlock(&receiver_locked); |
| 4491 | |
| 4492 | return ret; |
| 4493 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4494 | |
| 4495 | /** |
| 4496 | * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by |
| 4497 | * the section 17.7.1 of the FF-A v1.1 Beta0 specification. |
| 4498 | */ |
| 4499 | static struct ffa_value api_ffa_notification_info_get_success_return( |
| 4500 | const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes, |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 4501 | uint32_t lists_count) |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4502 | { |
| 4503 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64}; |
| 4504 | |
| 4505 | /* |
| 4506 | * Copying content of ids into ret structure. Use 5 registers (x3-x7) to |
| 4507 | * hold the list of ids. |
| 4508 | */ |
| 4509 | memcpy_s(&ret.arg3, |
| 4510 | sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids, |
| 4511 | sizeof(ids[0]) * ids_count); |
| 4512 | |
| 4513 | /* |
| 4514 | * According to the spec x2 should have: |
| 4515 | * - Bit flagging if there are more notifications pending; |
| 4516 | * - The total number of elements (i.e. total list size); |
| 4517 | * - The number of VCPU IDs within each VM specific list. |
| 4518 | */ |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 4519 | ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler() |
| 4520 | ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING |
| 4521 | : 0; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4522 | |
| 4523 | ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK) |
| 4524 | << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT; |
| 4525 | |
| 4526 | for (unsigned int i = 0; i < lists_count; i++) { |
| 4527 | ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK) |
| 4528 | << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1); |
| 4529 | } |
| 4530 | |
| 4531 | return ret; |
| 4532 | } |
| 4533 | |
| 4534 | struct ffa_value api_ffa_notification_info_get(struct vcpu *current) |
| 4535 | { |
| 4536 | /* |
| 4537 | * Following set of variables should be populated with the return info. |
| 4538 | * At a successfull handling of this interface, they should be used |
| 4539 | * to populate the 'ret' structure in accordance to the table 17.29 |
| 4540 | * of the FF-A v1.1 Beta0 specification. |
| 4541 | */ |
| 4542 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS]; |
| 4543 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 4544 | uint32_t lists_count = 0; |
| 4545 | uint32_t ids_count = 0; |
| 4546 | bool list_is_full = false; |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4547 | struct ffa_value result; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4548 | |
| 4549 | /* |
| 4550 | * This interface can only be called at NS virtual/physical FF-A |
| 4551 | * instance by the endpoint implementing the primary scheduler and the |
| 4552 | * Hypervisor/OS kernel. |
| 4553 | * In the SPM, following check passes if call has been forwarded from |
| 4554 | * the hypervisor. |
| 4555 | */ |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 4556 | |
| 4557 | if (!vm_is_primary(current->vm)) { |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4558 | dlog_verbose( |
| 4559 | "Only the receiver's scheduler can use this " |
| 4560 | "interface\n"); |
| 4561 | return ffa_error(FFA_NOT_SUPPORTED); |
| 4562 | } |
| 4563 | |
J-Alves | ca058c2 | 2021-09-10 14:02:07 +0100 | [diff] [blame] | 4564 | /* |
| 4565 | * Forward call to the other world, and fill the arrays used to assemble |
| 4566 | * return. |
| 4567 | */ |
| 4568 | plat_ffa_notification_info_get_forward( |
| 4569 | ids, &ids_count, lists_sizes, &lists_count, |
| 4570 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 4571 | |
| 4572 | list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS; |
| 4573 | |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4574 | /* Get notifications' info from this world */ |
| 4575 | for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full; |
| 4576 | ++index) { |
| 4577 | struct vm_locked vm_locked = vm_lock(vm_find_index(index)); |
| 4578 | |
| 4579 | list_is_full = vm_notifications_info_get( |
| 4580 | vm_locked, ids, &ids_count, lists_sizes, &lists_count, |
| 4581 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 4582 | |
| 4583 | vm_unlock(&vm_locked); |
| 4584 | } |
| 4585 | |
| 4586 | if (!list_is_full) { |
| 4587 | /* Grab notifications info from other world */ |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 4588 | plat_ffa_vm_notifications_info_get( |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4589 | ids, &ids_count, lists_sizes, &lists_count, |
| 4590 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 4591 | } |
| 4592 | |
| 4593 | if (ids_count == 0) { |
J-Alves | ca058c2 | 2021-09-10 14:02:07 +0100 | [diff] [blame] | 4594 | dlog_verbose( |
| 4595 | "Notification info get has no data to retrieve.\n"); |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4596 | result = ffa_error(FFA_NO_DATA); |
| 4597 | } else { |
| 4598 | result = api_ffa_notification_info_get_success_return( |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 4599 | ids, ids_count, lists_sizes, lists_count); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4600 | } |
| 4601 | |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 4602 | return result; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 4603 | } |
Raghu Krishnamurthy | ea6d25f | 2021-09-14 15:27:06 -0700 | [diff] [blame] | 4604 | |
| 4605 | struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current) |
| 4606 | { |
| 4607 | struct vm_locked vm_locked; |
| 4608 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4609 | bool mode_ret = false; |
| 4610 | uint32_t mode = 0; |
| 4611 | |
| 4612 | if (!plat_ffa_is_mem_perm_get_valid(current)) { |
J-Alves | 5a09917 | 2024-02-22 14:34:51 +0000 | [diff] [blame] | 4613 | return ffa_error(FFA_DENIED); |
Raghu Krishnamurthy | ea6d25f | 2021-09-14 15:27:06 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | if (!(current->vm->el0_partition)) { |
| 4617 | return ffa_error(FFA_DENIED); |
| 4618 | } |
| 4619 | |
| 4620 | vm_locked = vm_lock(current->vm); |
| 4621 | |
| 4622 | /* |
| 4623 | * mm_get_mode is used to check if the given base_addr page is already |
| 4624 | * mapped. If the page is unmapped, return error. If the page is mapped |
| 4625 | * appropriate attributes are returned to the caller. Note that |
| 4626 | * mm_get_mode returns true if the address is in the valid VA range as |
| 4627 | * supported by the architecture and MMU configurations, as opposed to |
| 4628 | * whether a page is mapped or not. For a page to be known as mapped, |
| 4629 | * the API must return true AND the returned mode must not have |
| 4630 | * MM_MODE_INVALID set. |
| 4631 | */ |
| 4632 | mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr, |
| 4633 | va_add(base_addr, PAGE_SIZE), &mode); |
| 4634 | if (!mode_ret || (mode & MM_MODE_INVALID)) { |
| 4635 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4636 | goto out; |
| 4637 | } |
| 4638 | |
| 4639 | /* No memory should be marked RWX */ |
| 4640 | CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) != |
| 4641 | (MM_MODE_R | MM_MODE_W | MM_MODE_X)); |
| 4642 | |
| 4643 | /* |
| 4644 | * S-EL0 partitions are expected to have all their pages marked as |
| 4645 | * non-global. |
| 4646 | */ |
| 4647 | CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) == |
| 4648 | (MM_MODE_NG | MM_MODE_USER)); |
| 4649 | |
| 4650 | if (mode & MM_MODE_W) { |
| 4651 | /* No memory should be writeable but not readable. */ |
| 4652 | CHECK(mode & MM_MODE_R); |
| 4653 | ret = (struct ffa_value){.func = FFA_SUCCESS_32, |
| 4654 | .arg2 = (uint32_t)(FFA_MEM_PERM_RW)}; |
| 4655 | } else if (mode & MM_MODE_R) { |
| 4656 | ret = (struct ffa_value){.func = FFA_SUCCESS_32, |
| 4657 | .arg2 = (uint32_t)(FFA_MEM_PERM_RX)}; |
| 4658 | if (!(mode & MM_MODE_X)) { |
| 4659 | ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO); |
| 4660 | } |
| 4661 | } |
| 4662 | out: |
| 4663 | vm_unlock(&vm_locked); |
| 4664 | return ret; |
| 4665 | } |
| 4666 | |
| 4667 | struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count, |
| 4668 | uint32_t mem_perm, struct vcpu *current) |
| 4669 | { |
| 4670 | struct vm_locked vm_locked; |
| 4671 | struct ffa_value ret; |
| 4672 | bool mode_ret = false; |
| 4673 | uint32_t original_mode; |
| 4674 | uint32_t new_mode; |
| 4675 | struct mpool local_page_pool; |
| 4676 | |
| 4677 | if (!plat_ffa_is_mem_perm_set_valid(current)) { |
J-Alves | 5a09917 | 2024-02-22 14:34:51 +0000 | [diff] [blame] | 4678 | return ffa_error(FFA_DENIED); |
Raghu Krishnamurthy | ea6d25f | 2021-09-14 15:27:06 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | if (!(current->vm->el0_partition)) { |
| 4682 | return ffa_error(FFA_DENIED); |
| 4683 | } |
| 4684 | |
| 4685 | if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) { |
| 4686 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4687 | } |
| 4688 | |
| 4689 | if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) && |
| 4690 | (mem_perm != FFA_MEM_PERM_RX)) { |
| 4691 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4692 | } |
| 4693 | |
| 4694 | /* |
| 4695 | * Create a local pool so any freed memory can't be used by another |
| 4696 | * thread. This is to ensure the original mapping can be restored if any |
| 4697 | * stage of the process fails. |
| 4698 | */ |
| 4699 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 4700 | |
| 4701 | vm_locked = vm_lock(current->vm); |
| 4702 | |
| 4703 | /* |
| 4704 | * All regions accessible by the partition are mapped during boot. If we |
| 4705 | * cannot get a successful translation for the page range, the request |
| 4706 | * to change permissions is rejected. |
| 4707 | * mm_get_mode is used to check if the given address range is already |
| 4708 | * mapped. If the range is unmapped, return error. If the range is |
| 4709 | * mapped appropriate attributes are returned to the caller. Note that |
| 4710 | * mm_get_mode returns true if the address is in the valid VA range as |
| 4711 | * supported by the architecture and MMU configurations, as opposed to |
| 4712 | * whether a page is mapped or not. For a page to be known as mapped, |
| 4713 | * the API must return true AND the returned mode must not have |
| 4714 | * MM_MODE_INVALID set. |
| 4715 | */ |
| 4716 | |
| 4717 | mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr, |
| 4718 | va_add(base_addr, page_count * PAGE_SIZE), |
| 4719 | &original_mode); |
| 4720 | if (!mode_ret || (original_mode & MM_MODE_INVALID)) { |
| 4721 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4722 | goto out; |
| 4723 | } |
| 4724 | |
| 4725 | /* Device memory cannot be marked as executable */ |
| 4726 | if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) { |
| 4727 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 4728 | goto out; |
| 4729 | } |
| 4730 | |
| 4731 | new_mode = MM_MODE_USER | MM_MODE_NG; |
| 4732 | |
| 4733 | if (mem_perm == FFA_MEM_PERM_RW) { |
| 4734 | new_mode |= MM_MODE_R | MM_MODE_W; |
| 4735 | } else if (mem_perm == FFA_MEM_PERM_RX) { |
| 4736 | new_mode |= MM_MODE_R | MM_MODE_X; |
| 4737 | } else if (mem_perm == FFA_MEM_PERM_RO) { |
| 4738 | new_mode |= MM_MODE_R; |
| 4739 | } |
| 4740 | |
| 4741 | /* |
| 4742 | * Safe to re-map memory, since we know the requested permissions are |
| 4743 | * valid, and the memory requested to be re-mapped is also valid. |
| 4744 | */ |
| 4745 | if (!mm_identity_prepare( |
| 4746 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 4747 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 4748 | new_mode, &local_page_pool)) { |
| 4749 | /* |
| 4750 | * Defrag the table into the local page pool. |
| 4751 | * mm_identity_prepare could have allocated or freed pages to |
| 4752 | * split blocks or tables etc. |
| 4753 | */ |
| 4754 | mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool); |
| 4755 | |
| 4756 | /* |
| 4757 | * Guaranteed to succeed mapping with old mode since the mapping |
| 4758 | * with old mode already existed and we have a local page pool |
| 4759 | * that should have sufficient memory to go back to the original |
| 4760 | * state. |
| 4761 | */ |
| 4762 | CHECK(mm_identity_prepare( |
| 4763 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 4764 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 4765 | original_mode, &local_page_pool)); |
| 4766 | mm_identity_commit( |
| 4767 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 4768 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 4769 | original_mode, &local_page_pool); |
| 4770 | |
| 4771 | mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool); |
| 4772 | ret = ffa_error(FFA_NO_MEMORY); |
| 4773 | goto out; |
| 4774 | } |
| 4775 | |
| 4776 | mm_identity_commit( |
| 4777 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 4778 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode, |
| 4779 | &local_page_pool); |
| 4780 | |
| 4781 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 4782 | |
| 4783 | out: |
| 4784 | mpool_fini(&local_page_pool); |
| 4785 | vm_unlock(&vm_locked); |
| 4786 | |
| 4787 | return ret; |
| 4788 | } |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4789 | |
| 4790 | /** |
Karl Meakin | c5cebbc | 2024-06-17 11:30:27 +0100 | [diff] [blame] | 4791 | * Send the contents of the given vCPU's log buffer to the log, preceded |
Karl Meakin | 705b56e | 2024-05-22 10:04:32 +0100 | [diff] [blame] | 4792 | * by the VM ID and followed by a newline. |
| 4793 | */ |
Karl Meakin | 6f1f121 | 2024-07-16 10:18:16 +0100 | [diff] [blame] | 4794 | void api_flush_log_buffer(struct vcpu_locked *vcpu_locked) |
Karl Meakin | 705b56e | 2024-05-22 10:04:32 +0100 | [diff] [blame] | 4795 | { |
Karl Meakin | 6f1f121 | 2024-07-16 10:18:16 +0100 | [diff] [blame] | 4796 | /* |
| 4797 | * NOTE: This line is parsed by `hftest.py`. |
| 4798 | * If you change the format, make sure to update |
| 4799 | * `HFTEST_CTRL_JSON_REGEX` as well. |
| 4800 | */ |
| 4801 | struct vcpu *vcpu = vcpu_locked->vcpu; |
| 4802 | struct log_buffer *buffer = &vcpu->log_buffer; |
| 4803 | ffa_id_t vm_id = vcpu->vm->id; |
| 4804 | ffa_id_t vcpu_id = vcpu_index(vcpu); |
| 4805 | |
Karl Meakin | 7de2695 | 2024-06-14 14:50:19 +0100 | [diff] [blame] | 4806 | buffer->chars[buffer->len] = '\0'; |
Olivier Deprez | bd43209 | 2024-07-26 14:01:12 +0200 | [diff] [blame] | 4807 | dlog("[%x %u] %s\n", vm_id, vcpu_id, buffer->chars); |
Karl Meakin | 7de2695 | 2024-06-14 14:50:19 +0100 | [diff] [blame] | 4808 | buffer->len = 0; |
Karl Meakin | 705b56e | 2024-05-22 10:04:32 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /** |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4812 | * Implements FF-A v1.2 FFA_CONSOLE_LOG ABI for buffered logging. |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4813 | */ |
| 4814 | struct ffa_value api_ffa_console_log(const struct ffa_value args, |
| 4815 | struct vcpu *current) |
| 4816 | { |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4817 | /* Maximum number of characters is 128: 16 registers of 8 bytes each. */ |
| 4818 | char chars[128] = {0}; |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 4819 | const bool v1_2 = current->vm->ffa_version >= FFA_VERSION_1_2; |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4820 | const bool log32 = args.func == FFA_CONSOLE_LOG_32; |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4821 | |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4822 | /* |
| 4823 | * 32bit: always 6 registers |
| 4824 | * 64bit and less than v1.2: 6 registers |
| 4825 | * 64bit and v1.2 or greater: 16 registers |
| 4826 | */ |
Karl Meakin | 66a38bd | 2024-05-28 16:00:56 +0100 | [diff] [blame] | 4827 | /* NOLINTNEXTLINE(readability-avoid-nested-conditional-operator) */ |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4828 | const size_t registers_max = log32 ? 6 : (v1_2 ? 16 : 6); |
| 4829 | const size_t chars_max = |
| 4830 | registers_max * (log32 ? sizeof(uint32_t) : sizeof(uint64_t)); |
| 4831 | const size_t chars_count = args.arg1; |
Karl Meakin | c5cebbc | 2024-06-17 11:30:27 +0100 | [diff] [blame] | 4832 | struct vcpu_locked vcpu_locked; |
| 4833 | struct log_buffer *log_buffer; |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4834 | |
| 4835 | assert(args.func == FFA_CONSOLE_LOG_32 || |
| 4836 | args.func == FFA_CONSOLE_LOG_64); |
| 4837 | |
| 4838 | if (chars_count == 0 || chars_count > chars_max) { |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4839 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4840 | } |
| 4841 | |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4842 | if (log32) { |
| 4843 | uint32_t *registers = (uint32_t *)chars; |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4844 | |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4845 | registers[0] = args.arg2 & 0xffffffff; |
| 4846 | registers[1] = args.arg3 & 0xffffffff; |
| 4847 | registers[2] = args.arg4 & 0xffffffff; |
| 4848 | registers[3] = args.arg5 & 0xffffffff; |
| 4849 | registers[4] = args.arg6 & 0xffffffff; |
| 4850 | registers[5] = args.arg7 & 0xffffffff; |
| 4851 | } else { |
| 4852 | uint64_t *registers = (uint64_t *)chars; |
| 4853 | |
| 4854 | registers[0] = args.arg2; |
| 4855 | registers[1] = args.arg3; |
| 4856 | registers[2] = args.arg4; |
| 4857 | registers[3] = args.arg5; |
| 4858 | registers[4] = args.arg6; |
| 4859 | registers[5] = args.arg7; |
| 4860 | if (v1_2) { |
| 4861 | registers[6] = args.extended_val.arg8; |
| 4862 | registers[7] = args.extended_val.arg9; |
| 4863 | registers[8] = args.extended_val.arg10; |
| 4864 | registers[9] = args.extended_val.arg11; |
| 4865 | registers[10] = args.extended_val.arg12; |
| 4866 | registers[11] = args.extended_val.arg13; |
| 4867 | registers[12] = args.extended_val.arg14; |
| 4868 | registers[13] = args.extended_val.arg15; |
| 4869 | registers[14] = args.extended_val.arg16; |
| 4870 | registers[15] = args.extended_val.arg17; |
| 4871 | } |
| 4872 | } |
| 4873 | |
Karl Meakin | c5cebbc | 2024-06-17 11:30:27 +0100 | [diff] [blame] | 4874 | vcpu_locked = vcpu_lock(current); |
| 4875 | log_buffer = ¤t->log_buffer; |
Karl Meakin | 7de2695 | 2024-06-14 14:50:19 +0100 | [diff] [blame] | 4876 | |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4877 | for (size_t i = 0; i < chars_count; i++) { |
| 4878 | bool flush = false; |
| 4879 | const char c = chars[i]; |
| 4880 | |
| 4881 | if (c == '\n' || c == '\0') { |
| 4882 | flush = true; |
| 4883 | } else { |
Karl Meakin | 7de2695 | 2024-06-14 14:50:19 +0100 | [diff] [blame] | 4884 | log_buffer->chars[log_buffer->len++] = c; |
| 4885 | flush = log_buffer->len == LOG_BUFFER_SIZE; |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | if (flush) { |
Karl Meakin | 6f1f121 | 2024-07-16 10:18:16 +0100 | [diff] [blame] | 4889 | api_flush_log_buffer(&vcpu_locked); |
Karl Meakin | 740f74f | 2024-02-14 18:04:48 +0000 | [diff] [blame] | 4890 | } |
| 4891 | } |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4892 | |
Karl Meakin | c5cebbc | 2024-06-17 11:30:27 +0100 | [diff] [blame] | 4893 | vcpu_unlock(&vcpu_locked); |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 4894 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 4895 | } |