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" |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 13 | #include "hf/arch/mm.h" |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 14 | #include "hf/arch/other_world.h" |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 15 | #include "hf/arch/plat/ffa.h" |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 16 | #include "hf/arch/timer.h" |
Olivier Deprez | 764fd2e | 2020-07-29 15:14:09 +0200 | [diff] [blame] | 17 | #include "hf/arch/vm.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 18 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 19 | #include "hf/check.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 20 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 21 | #include "hf/ffa_internal.h" |
| 22 | #include "hf/ffa_memory.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 23 | #include "hf/mm.h" |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 24 | #include "hf/plat/console.h" |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 25 | #include "hf/plat/interrupts.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 26 | #include "hf/spinlock.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 27 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 28 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 29 | #include "hf/vm.h" |
| 30 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 31 | #include "vmapi/hf/call.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 32 | #include "vmapi/hf/ffa.h" |
J-Alves | 126ab50 | 2022-09-29 11:37:33 +0100 | [diff] [blame^] | 33 | #include "vmapi/hf/ffa_v1_0.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 34 | |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 35 | static_assert(sizeof(struct ffa_partition_info_v1_0) == 8, |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 36 | "Partition information descriptor size doesn't match the one in " |
| 37 | "the FF-A 1.0 EAC specification, Table 82."); |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 38 | static_assert(sizeof(struct ffa_partition_info) == 24, |
| 39 | "Partition information descriptor size doesn't match the one in " |
| 40 | "the FF-A 1.1 BETA0 EAC specification, Table 13.34."); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 41 | static_assert((sizeof(struct ffa_partition_info) & 7) == 0, |
| 42 | "Partition information descriptor must be a multiple of 8 bytes" |
| 43 | " for ffa_partition_info_get_regs to work correctly. Information" |
| 44 | " from this structure are returned in 8 byte registers and the" |
| 45 | " count of 8 byte registers is returned by the ABI."); |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 46 | /* |
| 47 | * To eliminate the risk of deadlocks, we define a partial order for the |
| 48 | * acquisition of locks held concurrently by the same physical CPU. Our current |
| 49 | * ordering requirements are as follows: |
| 50 | * |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 51 | * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 52 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 53 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 54 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 57 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 58 | "Currently, a page is mapped for the send and receive buffers so " |
| 59 | "the maximum request is the size of a page."); |
| 60 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 61 | static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE, |
| 62 | "The page pool entry size must be at least as big as the mailbox " |
| 63 | "size, so that memory region descriptors can be copied from the " |
| 64 | "mailbox for memory sharing."); |
| 65 | |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 66 | /* |
| 67 | * Maximum ffa_partition_info entries that can be returned by an invocation |
| 68 | * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available |
| 69 | * registers/args in struct ffa_value divided by size of struct |
| 70 | * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e. |
| 71 | * 15 uint64_t fields. For FF-A v1.1, this value should be 5. |
| 72 | */ |
| 73 | #define MAX_INFO_REGS_ENTRIES_PER_CALL \ |
| 74 | ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info)) |
| 75 | static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5, |
| 76 | "FF-A v1.1 supports no more than 5 entries" |
| 77 | " per FFA_PARTITION_INFO_GET_REGS64 calls"); |
| 78 | |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 79 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 82 | * Initialises the API page pool by taking ownership of the contents of the |
| 83 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 84 | */ |
| 85 | void api_init(struct mpool *ppool) |
| 86 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 87 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 90 | /** |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 91 | * Get target VM vCPU: |
| 92 | * If VM is UP then return first vCPU. |
| 93 | * If VM is MP then return vCPU whose index matches current CPU index. |
| 94 | */ |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu); |
| 98 | struct vcpu *vcpu = NULL; |
| 99 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 100 | CHECK((vm != NULL) && (current != NULL)); |
| 101 | |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 102 | if (vm->vcpu_count == 1) { |
| 103 | vcpu = vm_get_vcpu(vm, 0); |
| 104 | } else if (current_cpu_index < vm->vcpu_count) { |
| 105 | vcpu = vm_get_vcpu(vm, current_cpu_index); |
| 106 | } |
| 107 | |
| 108 | return vcpu; |
| 109 | } |
| 110 | |
| 111 | /** |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 112 | * Switches the physical CPU back to the corresponding vCPU of the VM whose ID |
| 113 | * is given as argument of the function. |
| 114 | * |
| 115 | * Called to change the context between SPs for direct messaging (when Hafnium |
| 116 | * is SPMC), and on the context of the remaining 'api_switch_to_*' functions. |
| 117 | * |
| 118 | * This function works for partitions that are: |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 119 | * - UP migratable. |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 120 | * - MP with pinned Execution Contexts. |
| 121 | */ |
| 122 | static struct vcpu *api_switch_to_vm(struct vcpu *current, |
| 123 | struct ffa_value to_ret, |
| 124 | enum vcpu_state vcpu_state, |
| 125 | ffa_vm_id_t to_id) |
| 126 | { |
| 127 | struct vm *to_vm = vm_find(to_id); |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 128 | struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current); |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 129 | |
| 130 | CHECK(next != NULL); |
| 131 | |
| 132 | /* Set the return value for the target VM. */ |
| 133 | arch_regs_set_retval(&next->regs, to_ret); |
| 134 | |
| 135 | /* Set the current vCPU state. */ |
| 136 | sl_lock(¤t->lock); |
| 137 | current->state = vcpu_state; |
| 138 | sl_unlock(¤t->lock); |
| 139 | |
| 140 | return next; |
| 141 | } |
| 142 | |
| 143 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 144 | * 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] | 145 | * |
| 146 | * 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] | 147 | * 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] | 148 | */ |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 149 | struct vcpu *api_switch_to_primary(struct vcpu *current, |
| 150 | struct ffa_value primary_ret, |
| 151 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 152 | { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 153 | /* |
| 154 | * If the secondary is blocked but has a timer running, sleep until the |
| 155 | * timer fires rather than indefinitely. |
| 156 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 157 | switch (primary_ret.func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 158 | case HF_FFA_RUN_WAIT_FOR_INTERRUPT: |
| 159 | case FFA_MSG_WAIT_32: { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 160 | if (arch_timer_enabled_current()) { |
| 161 | uint64_t remaining_ns = |
| 162 | arch_timer_remaining_ns_current(); |
| 163 | |
| 164 | if (remaining_ns == 0) { |
| 165 | /* |
| 166 | * Timer is pending, so the current vCPU should |
| 167 | * be run again right away. |
| 168 | */ |
Andrew Walbran | 7e82460 | 2020-10-22 16:51:40 +0100 | [diff] [blame] | 169 | primary_ret = (struct ffa_value){ |
| 170 | .func = FFA_INTERRUPT_32}; |
| 171 | |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 172 | } else { |
| 173 | primary_ret.arg2 = remaining_ns; |
| 174 | } |
| 175 | } else { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 176 | primary_ret.arg2 = FFA_SLEEP_INDEFINITE; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 177 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 178 | break; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 179 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 180 | |
| 181 | default: |
| 182 | /* Do nothing. */ |
| 183 | break; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 184 | } |
| 185 | |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 186 | return api_switch_to_vm(current, primary_ret, secondary_state, |
| 187 | HF_PRIMARY_VM_ID); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 191 | * Choose next vCPU to run to be the counterpart vCPU in the other |
| 192 | * world (run the normal world if currently running in the secure |
| 193 | * world). Set current vCPU state to the given vcpu_state parameter. |
| 194 | * Set FF-A return values to the target vCPU in the other world. |
| 195 | * |
| 196 | * Called in context of a direct message response from a secure |
| 197 | * partition to a VM. |
| 198 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 199 | struct vcpu *api_switch_to_other_world(struct vcpu *current, |
| 200 | struct ffa_value other_world_ret, |
| 201 | enum vcpu_state vcpu_state) |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 202 | { |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 203 | return api_switch_to_vm(current, other_world_ret, vcpu_state, |
| 204 | HF_OTHER_WORLD_ID); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 208 | * Returns true if the given vCPU is executing in context of an |
| 209 | * FFA_MSG_SEND_DIRECT_REQ invocation. |
| 210 | */ |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 211 | bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked) |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 212 | { |
| 213 | return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID; |
| 214 | } |
| 215 | |
| 216 | /** |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 217 | * Returns true if the VM owning the given vCPU is supporting managed exit and |
| 218 | * the vCPU is currently processing a managed exit. |
| 219 | */ |
| 220 | static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked) |
| 221 | { |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 222 | return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) && |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 223 | vcpu_locked.vcpu->processing_managed_exit); |
| 224 | } |
| 225 | |
| 226 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 227 | * 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] | 228 | */ |
| 229 | struct vcpu *api_preempt(struct vcpu *current) |
| 230 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 231 | struct ffa_value ret = { |
| 232 | .func = FFA_INTERRUPT_32, |
Olivier Deprez | 0d725f5 | 2022-07-06 14:20:27 +0200 | [diff] [blame] | 233 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 236 | return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 240 | * 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] | 241 | * VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 242 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 243 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 244 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 245 | struct ffa_value ret = { |
| 246 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 247 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 248 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 249 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 250 | return api_switch_to_primary(current, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 251 | VCPU_STATE_BLOCKED_INTERRUPT); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 255 | * Puts the current vCPU in off mode, and returns to the primary VM. |
| 256 | */ |
| 257 | struct vcpu *api_vcpu_off(struct vcpu *current) |
| 258 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 259 | struct ffa_value ret = { |
| 260 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 261 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | /* |
| 265 | * Disable the timer, so the scheduler doesn't get told to call back |
| 266 | * based on it. |
| 267 | */ |
| 268 | arch_timer_disable_current(); |
| 269 | |
| 270 | return api_switch_to_primary(current, ret, VCPU_STATE_OFF); |
| 271 | } |
| 272 | |
| 273 | /** |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 274 | * The current vCPU is blocked on some resource and needs to relinquish |
| 275 | * control back to the execution context of the endpoint that originally |
| 276 | * allocated cycles to it. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 277 | */ |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 278 | struct ffa_value api_yield(struct vcpu *current, struct vcpu **next) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 279 | { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 280 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 281 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 282 | bool transition_allowed; |
| 283 | enum vcpu_state next_state = VCPU_STATE_BLOCKED; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 284 | |
| 285 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 286 | /* NOOP on the primary as it makes the scheduling decisions. */ |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 287 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 290 | current_locked = vcpu_lock(current); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 291 | transition_allowed = plat_ffa_check_runtime_state_transition( |
| 292 | current, current->vm->id, HF_INVALID_VM_ID, NULL, FFA_YIELD_32, |
| 293 | &next_state); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 294 | vcpu_unlock(¤t_locked); |
| 295 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 296 | if (!transition_allowed) { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 297 | return ffa_error(FFA_DENIED); |
| 298 | } |
| 299 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 300 | assert(next_state == VCPU_STATE_BLOCKED); |
| 301 | |
Madhukar Pappireddy | 1f2f213 | 2023-02-14 17:48:44 -0600 | [diff] [blame] | 302 | plat_ffa_yield_prepare(current); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 303 | *next = api_switch_to_primary( |
| 304 | current, |
| 305 | (struct ffa_value){.func = FFA_YIELD_32, |
| 306 | .arg1 = ffa_vm_vcpu(current->vm->id, |
| 307 | vcpu_index(current))}, |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 308 | next_state); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 309 | |
| 310 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 314 | * Switches to the primary so that it can switch to the target, or kick it if it |
| 315 | * is already running on a different physical CPU. |
| 316 | */ |
| 317 | struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu) |
| 318 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 319 | struct ffa_value ret = { |
Andrew Walbran | 7e82460 | 2020-10-22 16:51:40 +0100 | [diff] [blame] | 320 | .func = FFA_INTERRUPT_32, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 321 | .arg1 = ffa_vm_vcpu(target_vcpu->vm->id, |
| 322 | vcpu_index(target_vcpu)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 323 | }; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 324 | return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED); |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 328 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 329 | */ |
| 330 | struct vcpu *api_abort(struct vcpu *current) |
| 331 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 332 | struct ffa_value ret = ffa_error(FFA_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 333 | |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 334 | dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 335 | vcpu_index(current)); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 336 | |
| 337 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 338 | /* TODO: what to do when the primary aborts? */ |
| 339 | for (;;) { |
| 340 | /* Do nothing. */ |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | atomic_store_explicit(¤t->vm->aborting, true, |
| 345 | memory_order_relaxed); |
| 346 | |
| 347 | /* TODO: free resources once all vCPUs abort. */ |
| 348 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 349 | return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 352 | /* |
| 353 | * Format the partition info descriptors according to the version supported |
| 354 | * by the endpoint and return the size of the array created. |
| 355 | */ |
| 356 | static struct ffa_value send_versioned_partition_info_descriptors( |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 357 | struct vm_locked vm_locked, struct ffa_partition_info *partitions, |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 358 | uint32_t vm_count) |
| 359 | { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 360 | struct vm *vm = vm_locked.vm; |
| 361 | uint32_t version = vm->ffa_version; |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 362 | uint32_t partition_info_size; |
| 363 | uint32_t buffer_size; |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 364 | struct ffa_value ret; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 365 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 366 | /* Acquire receiver's RX buffer. */ |
| 367 | if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) { |
| 368 | dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id); |
| 369 | return ret; |
| 370 | } |
| 371 | |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 372 | if (vm_is_mailbox_busy(vm_locked)) { |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 373 | /* |
| 374 | * Can't retrieve memory information if the mailbox is not |
| 375 | * available. |
| 376 | */ |
| 377 | dlog_verbose("RX buffer not ready.\n"); |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 378 | return ffa_error(FFA_BUSY); |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 381 | if (version == MAKE_FFA_VERSION(1, 0)) { |
| 382 | struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv; |
| 383 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 384 | partition_info_size = sizeof(struct ffa_partition_info_v1_0); |
| 385 | buffer_size = partition_info_size * vm_count; |
| 386 | if (buffer_size > HF_MAILBOX_SIZE) { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 387 | dlog_error( |
| 388 | "Partition information does not fit in the " |
| 389 | "VM's RX " |
| 390 | "buffer.\n"); |
| 391 | return ffa_error(FFA_NO_MEMORY); |
| 392 | } |
| 393 | |
| 394 | for (uint32_t i = 0; i < vm_count; i++) { |
| 395 | /* |
| 396 | * Populate the VM's RX buffer with the partition |
Kathleen Capella | 402fa85 | 2022-11-09 16:16:51 -0500 | [diff] [blame] | 397 | * information. Clear properties bits that must be zero |
| 398 | * according to DEN0077A FF-A v1.0 REL Table 8.25. |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 399 | */ |
| 400 | recv_mailbox[i].vm_id = partitions[i].vm_id; |
| 401 | recv_mailbox[i].vcpu_count = partitions[i].vcpu_count; |
Kathleen Capella | 402fa85 | 2022-11-09 16:16:51 -0500 | [diff] [blame] | 402 | recv_mailbox[i].properties = |
| 403 | partitions[i].properties & |
| 404 | ~FFA_PARTITION_v1_0_RES_MASK; |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | } else { |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 408 | partition_info_size = sizeof(struct ffa_partition_info); |
| 409 | buffer_size = partition_info_size * vm_count; |
| 410 | if (buffer_size > HF_MAILBOX_SIZE) { |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 411 | dlog_error( |
| 412 | "Partition information does not fit in the " |
| 413 | "VM's RX " |
| 414 | "buffer.\n"); |
| 415 | return ffa_error(FFA_NO_MEMORY); |
| 416 | } |
| 417 | |
| 418 | /* Populate the VM's RX buffer with the partition information. |
| 419 | */ |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 420 | memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions, |
| 421 | buffer_size); |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 424 | vm->mailbox.recv_size = buffer_size; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 425 | |
| 426 | /* Sender is Hypervisor in the normal world (TEE in secure world). */ |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 427 | vm->mailbox.recv_sender = HF_VM_ID_BASE; |
| 428 | vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 429 | vm->mailbox.state = MAILBOX_STATE_FULL; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 430 | |
Daniel Boulby | 835e159 | 2022-05-30 17:38:51 +0100 | [diff] [blame] | 431 | /* |
| 432 | * Return the count of partition information descriptors in w2 |
| 433 | * and the size of the descriptors in w3. |
| 434 | */ |
| 435 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 436 | .arg2 = vm_count, |
| 437 | .arg3 = partition_info_size}; |
Daniel Boulby | 1ddb3d7 | 2021-12-16 18:16:50 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 440 | static void api_ffa_fill_partitions_info_array( |
| 441 | struct ffa_partition_info *partitions, size_t partitions_len, |
| 442 | const struct ffa_uuid *uuid, bool count_flag, ffa_vm_id_t vm_id, |
| 443 | ffa_vm_count_t *vm_count_out) |
| 444 | { |
| 445 | ffa_vm_count_t vm_count = 0; |
| 446 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
| 447 | |
| 448 | assert(vm_get_count() <= partitions_len); |
| 449 | |
| 450 | /* |
| 451 | * Iterate through the VMs to find the ones with a matching |
| 452 | * UUID. A Null UUID retrieves information for all VMs. |
| 453 | */ |
| 454 | for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) { |
| 455 | struct vm *vm = vm_find_index(index); |
| 456 | |
| 457 | if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) { |
| 458 | uint16_t array_index = vm_count; |
| 459 | |
| 460 | ++vm_count; |
| 461 | if (count_flag) { |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | partitions[array_index].vm_id = vm->id; |
| 466 | partitions[array_index].vcpu_count = vm->vcpu_count; |
| 467 | partitions[array_index].properties = |
| 468 | plat_ffa_partition_properties(vm_id, vm); |
| 469 | partitions[array_index].properties |= |
| 470 | vm_are_notifications_enabled(vm) |
| 471 | ? FFA_PARTITION_NOTIFICATION |
| 472 | : 0; |
| 473 | partitions[array_index].properties |= |
| 474 | FFA_PARTITION_AARCH64_EXEC; |
| 475 | if (uuid_is_null) { |
| 476 | partitions[array_index].uuid = vm->uuid; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | *vm_count_out = vm_count; |
| 482 | } |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 483 | |
| 484 | static inline void api_ffa_pack_vmid_count_props( |
| 485 | uint64_t *xn, ffa_vm_id_t vm_id, ffa_vcpu_count_t vcpu_count, |
| 486 | ffa_partition_properties_t properties) |
| 487 | { |
| 488 | *xn = (uint64_t)vm_id; |
| 489 | *xn |= (uint64_t)vcpu_count << 16; |
| 490 | *xn |= (uint64_t)properties << 32; |
| 491 | } |
| 492 | |
| 493 | static inline void api_ffa_pack_uuid(uint64_t *xn_1, uint64_t *xn_2, |
| 494 | struct ffa_uuid *uuid) |
| 495 | { |
| 496 | *xn_1 = (uint64_t)uuid->uuid[0]; |
| 497 | *xn_1 |= (uint64_t)uuid->uuid[1] << 32; |
| 498 | *xn_2 = (uint64_t)uuid->uuid[2]; |
| 499 | *xn_2 |= (uint64_t)uuid->uuid[3] << 32; |
| 500 | } |
| 501 | |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 502 | struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current, |
| 503 | const struct ffa_uuid *uuid, |
| 504 | const uint16_t start_index, |
| 505 | const uint16_t tag) |
| 506 | { |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 507 | struct vm *current_vm = current->vm; |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 508 | static struct ffa_partition_info partitions[2 * MAX_VMS]; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 509 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
| 510 | ffa_vm_count_t vm_count = 0; |
| 511 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 512 | uint16_t max_idx = 0; |
| 513 | uint16_t curr_idx = 0; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 514 | uint8_t num_entries_to_ret = 0; |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 515 | uint8_t arg_idx = 3; |
| 516 | |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 517 | /* list of pointers to args in return value */ |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 518 | uint64_t *arg_ptrs[] = { |
| 519 | &(ret).func, |
| 520 | &(ret).arg1, |
| 521 | &(ret).arg2, |
| 522 | &(ret).arg3, |
| 523 | &(ret).arg4, |
| 524 | &(ret).arg5, |
| 525 | &(ret).arg6, |
| 526 | &(ret).arg7, |
| 527 | &(ret).extended_val.arg8, |
| 528 | &(ret).extended_val.arg9, |
| 529 | &(ret).extended_val.arg10, |
| 530 | &(ret).extended_val.arg11, |
| 531 | &(ret).extended_val.arg12, |
| 532 | &(ret).extended_val.arg13, |
| 533 | &(ret).extended_val.arg14, |
| 534 | &(ret).extended_val.arg15, |
| 535 | &(ret).extended_val.arg16, |
| 536 | &(ret).extended_val.arg17, |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | /* TODO: Add support for using tags */ |
| 540 | if (tag != 0) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 541 | dlog_error("Tag not 0. Unsupported tag. %d\n", tag); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 542 | return ffa_error(FFA_RETRY); |
| 543 | } |
| 544 | |
| 545 | memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions)); |
| 546 | |
| 547 | api_ffa_fill_partitions_info_array(partitions, ARRAY_SIZE(partitions), |
| 548 | uuid, false, current_vm->id, |
| 549 | &vm_count); |
| 550 | |
| 551 | /* If UUID is Null vm_count must not be zero at this stage. */ |
| 552 | CHECK(!uuid_is_null || vm_count != 0); |
| 553 | |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 554 | /* |
| 555 | * When running the Hypervisor: |
| 556 | * - If UUID is Null the Hypervisor forwards the query to the SPMC for |
| 557 | * it to fill with secure partitions information. |
| 558 | * TODO: Note that for this ABI, forwarding on every invocation when |
| 559 | * uuid is Null is inefficient,and if performance becomes a problem, |
| 560 | * this would be a good place to optimize using strategies such as |
| 561 | * caching info etc. For now, assuming this inefficiency is not a major |
| 562 | * issue. |
| 563 | * - If UUID is non-Null vm_count may be zero because the UUID matches |
| 564 | * a secure partition and the query is forwarded to the SPMC. |
| 565 | * When running the SPMC: |
| 566 | * - If UUID is non-Null and vm_count is zero it means there is no such |
| 567 | * partition identified in the system. |
| 568 | */ |
| 569 | if (!plat_ffa_partition_info_get_regs_forward( |
| 570 | uuid, start_index, tag, partitions, ARRAY_SIZE(partitions), |
| 571 | &vm_count)) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 572 | dlog_error("Failed to forward ffa_partition_info_get_regs.\n"); |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 573 | return ffa_error(FFA_DENIED); |
| 574 | } |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * Unrecognized UUID: does not match any of the VMs (or SPs) |
| 578 | * and is not Null. |
| 579 | */ |
Raghu Krishnamurthy | ef432cb | 2022-12-29 06:56:32 -0800 | [diff] [blame] | 580 | if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 581 | dlog_error( |
| 582 | "Invalid parameters. vm_count = %d (must not be zero " |
| 583 | "or > %d)\n", |
| 584 | vm_count, ARRAY_SIZE(partitions)); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 585 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 586 | } |
| 587 | |
| 588 | if (start_index >= vm_count) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 589 | dlog_error( |
| 590 | "start index = %d vm_count = %d (start_index must be " |
| 591 | "less than vm_count)\n", |
| 592 | start_index, vm_count); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 593 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 594 | } |
| 595 | |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 596 | max_idx = vm_count - 1; |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 597 | num_entries_to_ret = (max_idx - start_index) + 1; |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 598 | num_entries_to_ret = |
| 599 | MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 600 | curr_idx = start_index + num_entries_to_ret - 1; |
| 601 | assert(curr_idx <= max_idx); |
| 602 | |
| 603 | ret.func = FFA_SUCCESS_64; |
| 604 | ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48; |
| 605 | ret.arg2 |= curr_idx << 16; |
| 606 | ret.arg2 |= max_idx; |
| 607 | |
| 608 | if (num_entries_to_ret > 1) { |
| 609 | ret.extended_val.valid = 1; |
| 610 | } |
| 611 | |
| 612 | for (uint16_t idx = start_index; idx <= curr_idx; ++idx) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 613 | uint64_t *xn_0 = arg_ptrs[arg_idx++]; |
| 614 | uint64_t *xn_1 = arg_ptrs[arg_idx++]; |
| 615 | uint64_t *xn_2 = arg_ptrs[arg_idx++]; |
| 616 | |
| 617 | api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id, |
| 618 | partitions[idx].vcpu_count, |
| 619 | partitions[idx].properties); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 620 | if (uuid_is_null) { |
Raghu Krishnamurthy | befdcc8 | 2023-02-24 06:41:56 -0800 | [diff] [blame] | 621 | api_ffa_pack_uuid(xn_1, xn_2, &partitions[idx].uuid); |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 622 | } |
| 623 | assert(arg_idx < ARRAY_SIZE(arg_ptrs)); |
| 624 | } |
| 625 | |
| 626 | return ret; |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 629 | struct ffa_value api_ffa_partition_info_get(struct vcpu *current, |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 630 | const struct ffa_uuid *uuid, |
| 631 | const uint32_t flags) |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 632 | { |
| 633 | struct vm *current_vm = current->vm; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 634 | ffa_vm_count_t vm_count = 0; |
Olivier Deprez | 013f4d6 | 2022-11-21 15:46:20 +0100 | [diff] [blame] | 635 | bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) == |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 636 | FFA_PARTITION_COUNT_FLAG; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 637 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
Daniel Boulby | ce54184 | 2022-05-31 15:54:31 +0100 | [diff] [blame] | 638 | struct ffa_partition_info partitions[2 * MAX_VMS] = {0}; |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 639 | struct vm_locked vm_locked; |
| 640 | struct ffa_value ret; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 641 | |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 642 | /* Bits 31:1 Must Be Zero */ |
Olivier Deprez | 013f4d6 | 2022-11-21 15:46:20 +0100 | [diff] [blame] | 643 | if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) { |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 644 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 645 | } |
| 646 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 647 | /* |
Kathleen Capella | 402fa85 | 2022-11-09 16:16:51 -0500 | [diff] [blame] | 648 | * No need to count if we are returning the number of partitions as we |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 649 | * already know this. |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 650 | */ |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 651 | if (uuid_is_null && count_flag) { |
| 652 | vm_count = vm_get_count(); |
| 653 | } else { |
Raghu Krishnamurthy | f67776f | 2022-12-26 10:10:05 -0800 | [diff] [blame] | 654 | api_ffa_fill_partitions_info_array( |
| 655 | partitions, ARRAY_SIZE(partitions), uuid, count_flag, |
| 656 | current_vm->id, &vm_count); |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Olivier Deprez | e562e54 | 2020-06-11 17:31:54 +0200 | [diff] [blame] | 659 | /* If UUID is Null vm_count must not be zero at this stage. */ |
| 660 | CHECK(!uuid_is_null || vm_count != 0); |
| 661 | |
| 662 | /* |
| 663 | * When running the Hypervisor: |
| 664 | * - If UUID is Null the Hypervisor forwards the query to the SPMC for |
| 665 | * it to fill with secure partitions information. |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 666 | * - 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] | 667 | * a secure partition and the query is forwarded to the SPMC. |
| 668 | * When running the SPMC: |
| 669 | * - If UUID is non-Null and vm_count is zero it means there is no such |
| 670 | * partition identified in the system. |
| 671 | */ |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 672 | plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count); |
Olivier Deprez | e562e54 | 2020-06-11 17:31:54 +0200 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * Unrecognized UUID: does not match any of the VMs (or SPs) |
| 676 | * and is not Null. |
| 677 | */ |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 678 | if (vm_count == 0) { |
| 679 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 680 | } |
| 681 | |
Daniel Boulby | b46cad1 | 2021-12-13 17:47:21 +0000 | [diff] [blame] | 682 | /* |
| 683 | * If the count flag is set we don't need to return the partition info |
| 684 | * descriptors. |
| 685 | */ |
| 686 | if (count_flag) { |
| 687 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 688 | .arg2 = vm_count}; |
| 689 | } |
| 690 | |
Daniel Boulby | 191b5f0 | 2022-02-17 17:26:14 +0000 | [diff] [blame] | 691 | vm_locked = vm_lock(current_vm); |
| 692 | ret = send_versioned_partition_info_descriptors(vm_locked, partitions, |
| 693 | vm_count); |
| 694 | vm_unlock(&vm_locked); |
| 695 | return ret; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 698 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 699 | * Returns the ID of the VM. |
| 700 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 701 | struct ffa_value api_ffa_id_get(const struct vcpu *current) |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 702 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 703 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 704 | .arg2 = current->vm->id}; |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
Olivier Deprez | 421677d | 2021-06-18 12:18:53 +0200 | [diff] [blame] | 708 | * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual |
| 709 | * FF-A instances. |
| 710 | * 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] | 711 | */ |
| 712 | struct ffa_value api_ffa_spm_id_get(void) |
| 713 | { |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 714 | #if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) |
| 715 | /* |
| 716 | * Return the SPMC ID that was fetched during FF-A |
| 717 | * initialization. |
| 718 | */ |
| 719 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 720 | .arg2 = arch_ffa_spmc_id_get()}; |
| 721 | #else |
| 722 | return ffa_error(FFA_NOT_SUPPORTED); |
| 723 | #endif |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 727 | * This function is called by the architecture-specific context switching |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 728 | * function to indicate that register state for the given vCPU has been saved |
| 729 | * and can therefore be used by other pCPUs. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 730 | */ |
| 731 | void api_regs_state_saved(struct vcpu *vcpu) |
| 732 | { |
| 733 | sl_lock(&vcpu->lock); |
| 734 | vcpu->regs_available = true; |
| 735 | sl_unlock(&vcpu->lock); |
| 736 | } |
| 737 | |
| 738 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 739 | * Retrieves the next waiter and removes it from the wait list if the VM's |
| 740 | * mailbox is in a writable state. |
| 741 | */ |
| 742 | static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm) |
| 743 | { |
| 744 | struct wait_entry *entry; |
| 745 | struct vm *vm = locked_vm.vm; |
| 746 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 747 | if (vm->mailbox.state != MAILBOX_STATE_EMPTY || |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 748 | vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) { |
| 749 | /* The mailbox is not writable or there are no waiters. */ |
| 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | /* Remove waiter from the wait list. */ |
| 754 | entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry, |
| 755 | wait_links); |
| 756 | list_remove(&entry->wait_links); |
| 757 | return entry; |
| 758 | } |
| 759 | |
| 760 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 761 | * Assuming that the arguments have already been checked by the caller, injects |
| 762 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 763 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 764 | * is next run, which is up to the scheduler. |
| 765 | * |
| 766 | * Returns: |
| 767 | * - 0 on success if no further action is needed. |
| 768 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 769 | * up or kick the target vCPU. |
| 770 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 771 | int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked, |
| 772 | uint32_t intid, struct vcpu *current, |
| 773 | struct vcpu **next) |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 774 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 775 | struct vcpu *target_vcpu = target_locked.vcpu; |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 776 | struct interrupts *interrupts = &target_vcpu->interrupts; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 777 | int64_t ret = 0; |
| 778 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 779 | /* |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 780 | * We only need to change state and (maybe) trigger a virtual interrupt |
| 781 | * if it is enabled and was not previously pending. Otherwise we can |
| 782 | * skip everything except setting the pending bit. |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 783 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 784 | if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) && |
| 785 | !vcpu_is_virt_interrupt_pending(interrupts, intid))) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 786 | goto out; |
| 787 | } |
| 788 | |
| 789 | /* Increment the count. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 790 | vcpu_interrupt_count_increment(target_locked, interrupts, intid); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Only need to update state if there was not already an |
| 794 | * interrupt enabled and pending. |
| 795 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 796 | if (vcpu_interrupt_count_get(target_locked) != 1) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 797 | goto out; |
| 798 | } |
| 799 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 800 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 801 | /* |
| 802 | * If the call came from the primary VM, let it know that it |
| 803 | * should run or kick the target vCPU. |
| 804 | */ |
| 805 | ret = 1; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 806 | } else if (current != target_vcpu && next != NULL) { |
| 807 | *next = api_wake_up(current, target_vcpu); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | out: |
| 811 | /* Either way, make it pending. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 812 | vcpu_virt_interrupt_set_pending(interrupts, intid); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 813 | |
Olivier Deprez | c19a6ea | 2020-08-06 11:16:07 +0200 | [diff] [blame] | 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | /* Wrapper to internal_interrupt_inject with locking of target vCPU */ |
| 818 | static int64_t internal_interrupt_inject(struct vcpu *target_vcpu, |
| 819 | uint32_t intid, struct vcpu *current, |
| 820 | struct vcpu **next) |
| 821 | { |
| 822 | int64_t ret; |
| 823 | struct vcpu_locked target_locked; |
| 824 | |
| 825 | target_locked = vcpu_lock(target_vcpu); |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 826 | ret = api_interrupt_inject_locked(target_locked, intid, current, next); |
Olivier Deprez | c19a6ea | 2020-08-06 11:16:07 +0200 | [diff] [blame] | 827 | vcpu_unlock(&target_locked); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 828 | |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | /** |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 833 | * Constructs the return value from a successful FFA_MSG_POLL or |
| 834 | * FFA_MSG_WAIT call. |
| 835 | * |
| 836 | * Note: FFA_MSG_POLL is deprecated in FF-A v1.1 and should not be used with |
| 837 | * FFA_MSG_SEND2; no check is done on mixing FF-A v1.0 and FF-A v1.1 indirect |
| 838 | * message protocols. |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 839 | */ |
J-Alves | 27b7196 | 2022-12-12 15:29:58 +0000 | [diff] [blame] | 840 | struct ffa_value ffa_msg_recv_return(const struct vm *receiver) |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 841 | { |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 842 | switch (receiver->mailbox.recv_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 843 | case FFA_MSG_SEND_32: |
| 844 | return (struct ffa_value){ |
| 845 | .func = FFA_MSG_SEND_32, |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 846 | .arg1 = (receiver->mailbox.recv_sender << 16) | |
| 847 | receiver->id, |
| 848 | .arg3 = receiver->mailbox.recv_size}; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 849 | case FFA_MSG_SEND2_32: |
| 850 | return (struct ffa_value){ |
| 851 | .func = FFA_RUN_32, |
| 852 | /* |
| 853 | * TODO: FFA_RUN should return vCPU and VM ID in arg1. |
| 854 | * Retrieving vCPU requires a rework of the function, |
| 855 | * while receiver ID must be set because it's checked by |
| 856 | * other APIs (eg: FFA_NOTIFICATION_GET). |
| 857 | */ |
| 858 | .arg1 = receiver->id}; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 859 | default: |
| 860 | /* This should never be reached, but return an error in case. */ |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 861 | dlog_error("Tried to return an invalid message function %#x\n", |
| 862 | receiver->mailbox.recv_func); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 863 | return ffa_error(FFA_DENIED); |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 864 | } |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 865 | } |
| 866 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 867 | /** |
| 868 | * Change the state of mailbox to empty, such that the ownership is given to the |
| 869 | * Partition manager. |
| 870 | * Returns true if the mailbox was reset successfully, false otherwise. |
| 871 | */ |
| 872 | static bool api_release_mailbox(struct vm_locked vm_locked, int32_t *error_code) |
| 873 | { |
| 874 | ffa_vm_id_t vm_id = vm_locked.vm->id; |
| 875 | int32_t error_code_to_ret = 0; |
| 876 | bool ret = false; |
| 877 | |
| 878 | switch (vm_locked.vm->mailbox.state) { |
| 879 | case MAILBOX_STATE_EMPTY: |
| 880 | dlog_verbose("Mailbox of %x is empty.\n", vm_id); |
| 881 | error_code_to_ret = FFA_DENIED; |
| 882 | goto out; |
| 883 | case MAILBOX_STATE_FULL: |
| 884 | /* Check it doesn't have pending RX full notifications. */ |
| 885 | if (vm_are_fwk_notifications_pending(vm_locked)) { |
| 886 | dlog_verbose( |
| 887 | "Mailbox of endpoint %x has pending " |
| 888 | "messages.\n", |
| 889 | vm_id); |
| 890 | error_code_to_ret = FFA_DENIED; |
| 891 | goto out; |
| 892 | } |
| 893 | break; |
| 894 | case MAILBOX_STATE_OTHER_WORLD_OWNED: |
| 895 | /* |
| 896 | * The SPMC shouldn't let SP's mailbox get into this state. |
| 897 | * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst |
| 898 | * the mailbox is in this state. In that case, we should report |
| 899 | * error. |
| 900 | */ |
| 901 | if (vm_id_is_current_world(vm_id)) { |
| 902 | dlog_verbose( |
| 903 | "Mailbox of endpoint %x in a wrongful state.\n", |
| 904 | vm_id); |
| 905 | error_code_to_ret = FFA_ABORTED; |
| 906 | goto out; |
| 907 | } |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
| 912 | ret = true; |
| 913 | out: |
| 914 | if (error_code != NULL) { |
| 915 | *error_code = error_code_to_ret; |
| 916 | } |
| 917 | |
| 918 | return ret; |
| 919 | } |
| 920 | |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 921 | struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next, |
| 922 | struct ffa_value *args) |
| 923 | { |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 924 | enum vcpu_state next_state = VCPU_STATE_WAITING; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 925 | struct ffa_value ret; |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 926 | |
| 927 | if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U || |
| 928 | args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U || |
| 929 | args->arg7 != 0U) { |
| 930 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 931 | } |
| 932 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 933 | if (!plat_ffa_check_runtime_state_transition( |
| 934 | current, current->vm->id, HF_INVALID_VM_ID, NULL, |
| 935 | FFA_MSG_WAIT_32, &next_state)) { |
| 936 | return ffa_error(FFA_DENIED); |
| 937 | } |
| 938 | |
| 939 | assert(next_state == VCPU_STATE_WAITING); |
| 940 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 941 | ret = plat_ffa_msg_wait_prepare(current, next); |
| 942 | |
| 943 | if (ret.func != FFA_ERROR_32) { |
| 944 | struct vm_locked vm_locked = vm_lock(current->vm); |
| 945 | |
| 946 | api_release_mailbox(vm_locked, NULL); |
| 947 | vm_unlock(&vm_locked); |
| 948 | } |
| 949 | |
| 950 | return ret; |
Madhukar Pappireddy | 5522c67 | 2021-12-17 16:35:51 -0600 | [diff] [blame] | 951 | } |
| 952 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 953 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 954 | * 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] | 955 | * value needs to be forced onto the vCPU. |
| 956 | */ |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 957 | static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 958 | struct ffa_value *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 959 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 960 | struct vcpu_locked vcpu_locked; |
| 961 | struct vm_locked vm_locked; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 962 | bool ret; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 963 | uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE; |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 964 | bool vcpu_was_init_state = false; |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 965 | bool need_vm_lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 966 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 967 | /* |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 968 | * 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] | 969 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 970 | * The VM lock is not needed in the common case so it must only be taken |
| 971 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 972 | * dependencies in the common run case meaning the sensitive context |
| 973 | * switch performance is consistent. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 974 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 975 | vcpu_locked = vcpu_lock(vcpu); |
| 976 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 977 | /* The VM needs to be locked to deliver mailbox messages. */ |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 978 | need_vm_lock = vcpu->state == VCPU_STATE_WAITING || |
| 979 | (!vcpu->vm->el0_partition && |
| 980 | (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT || |
| 981 | vcpu->state == VCPU_STATE_BLOCKED || |
| 982 | vcpu->state == VCPU_STATE_PREEMPTED)); |
| 983 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 984 | if (need_vm_lock) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 985 | vcpu_unlock(&vcpu_locked); |
| 986 | vm_locked = vm_lock(vcpu->vm); |
| 987 | vcpu_locked = vcpu_lock(vcpu); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* |
| 991 | * If the vCPU is already running somewhere then we can't run it here |
| 992 | * simultaneously. While it is actually running then the state should be |
| 993 | * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it |
| 994 | * stops running but while Hafnium is in the process of switching back |
| 995 | * to the primary there will be a brief period while the state has been |
| 996 | * updated but `regs_available` is still false (until |
| 997 | * `api_regs_state_saved` is called). We can't start running it again |
| 998 | * until this has finished, so count this state as still running for the |
| 999 | * purposes of this check. |
| 1000 | */ |
| 1001 | if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) { |
| 1002 | /* |
| 1003 | * vCPU is running on another pCPU. |
| 1004 | * |
| 1005 | * It's okay not to return the sleep duration here because the |
| 1006 | * other physical CPU that is currently running this vCPU will |
| 1007 | * return the sleep duration if needed. |
| 1008 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1009 | *run_ret = ffa_error(FFA_BUSY); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 1010 | ret = false; |
| 1011 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1012 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1015 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 1016 | dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1017 | vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1018 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 1019 | } |
| 1020 | ret = false; |
| 1021 | goto out; |
| 1022 | } |
| 1023 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1024 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1025 | case VCPU_STATE_RUNNING: |
| 1026 | case VCPU_STATE_OFF: |
| 1027 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1028 | ret = false; |
| 1029 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1030 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1031 | case VCPU_STATE_WAITING: |
| 1032 | /* |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 1033 | * An initial FFA_RUN is necessary for SP's secondary vCPUs to |
| 1034 | * reach the message wait loop. |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1035 | */ |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 1036 | if (vcpu->rt_model == RTM_SP_INIT) { |
| 1037 | /* |
| 1038 | * TODO: this should be removed, but omitting it makes |
| 1039 | * normal world arch gicv3 tests failing. |
| 1040 | */ |
| 1041 | vcpu->rt_model = RTM_NONE; |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 1042 | |
| 1043 | vcpu_was_init_state = true; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1044 | break; |
| 1045 | } |
| 1046 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1047 | assert(need_vm_lock == true); |
| 1048 | if (!vm_locked.vm->el0_partition && |
| 1049 | plat_ffa_inject_notification_pending_interrupt( |
| 1050 | vcpu_locked, current, vm_locked)) { |
Federico Recanati | 9dccc4b | 2022-04-27 12:52:36 +0200 | [diff] [blame] | 1051 | /* TODO: setting a return value to override |
| 1052 | * the placeholder (FFA_ERROR(INTERRUPTED)) |
| 1053 | * set by FFA_MSG_WAIT. FF-A v1.1 allows |
| 1054 | * FFA_MSG_WAIT to successfully return even if |
| 1055 | * it didn't receive a message. TFTF tests are |
| 1056 | * still expecting an FFA_ERROR instead, |
| 1057 | * should be fixed? |
| 1058 | */ |
| 1059 | arch_regs_set_retval( |
| 1060 | &vcpu->regs, |
| 1061 | (struct ffa_value){.func = FFA_RUN_32, |
| 1062 | // TODO: does it make sense |
| 1063 | // to set vCPU/receiver? |
| 1064 | .arg1 = 0}); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
| 1067 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1068 | /* |
| 1069 | * A pending message allows the vCPU to run so the message can |
| 1070 | * be delivered directly. |
| 1071 | */ |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1072 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1073 | arch_regs_set_retval(&vcpu->regs, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1074 | ffa_msg_recv_return(vcpu->vm)); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1077 | |
| 1078 | if (vcpu_interrupt_count_get(vcpu_locked) > 0) { |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | if (arch_timer_enabled(&vcpu->regs)) { |
| 1083 | timer_remaining_ns = |
| 1084 | arch_timer_remaining_ns(&vcpu->regs); |
| 1085 | if (timer_remaining_ns == 0) { |
| 1086 | break; |
| 1087 | } |
| 1088 | } else { |
| 1089 | dlog_verbose("Timer disabled\n"); |
| 1090 | } |
| 1091 | run_ret->func = FFA_MSG_WAIT_32; |
| 1092 | run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
| 1093 | run_ret->arg2 = timer_remaining_ns; |
| 1094 | ret = false; |
| 1095 | goto out; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1096 | case VCPU_STATE_BLOCKED_INTERRUPT: |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1097 | if (need_vm_lock && |
| 1098 | plat_ffa_inject_notification_pending_interrupt( |
| 1099 | vcpu_locked, current, vm_locked)) { |
| 1100 | assert(vcpu_interrupt_count_get(vcpu_locked) > 0); |
| 1101 | break; |
| 1102 | } |
| 1103 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1104 | /* Allow virtual interrupts to be delivered. */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1105 | if (vcpu_interrupt_count_get(vcpu_locked) > 0) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | } |
| 1108 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1109 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1110 | timer_remaining_ns = |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 1111 | arch_timer_remaining_ns(&vcpu->regs); |
| 1112 | |
| 1113 | /* |
| 1114 | * The timer expired so allow the interrupt to be |
| 1115 | * delivered. |
| 1116 | */ |
| 1117 | if (timer_remaining_ns == 0) { |
| 1118 | break; |
| 1119 | } |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1120 | } |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 1121 | |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1122 | /* |
| 1123 | * The vCPU is not ready to run, return the appropriate code to |
| 1124 | * the primary which called vcpu_run. |
| 1125 | */ |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1126 | run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 1127 | run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
| 1128 | run_ret->arg2 = timer_remaining_ns; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1129 | |
| 1130 | ret = false; |
| 1131 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1132 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1133 | case VCPU_STATE_BLOCKED: |
| 1134 | /* A blocked vCPU is run unconditionally. Fall through. */ |
| 1135 | case VCPU_STATE_PREEMPTED: |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 1136 | /* Check NPI is to be injected here. */ |
| 1137 | if (need_vm_lock) { |
| 1138 | plat_ffa_inject_notification_pending_interrupt( |
| 1139 | vcpu_locked, current, vm_locked); |
| 1140 | } |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1141 | break; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1142 | default: |
| 1143 | /* |
| 1144 | * Execution not expected to reach here. Deny the request |
| 1145 | * gracefully. |
| 1146 | */ |
| 1147 | *run_ret = ffa_error(FFA_DENIED); |
| 1148 | ret = false; |
| 1149 | goto out; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Madhukar Pappireddy | 1480fce | 2022-06-21 18:09:25 -0500 | [diff] [blame] | 1152 | plat_ffa_init_schedule_mode_ffa_run(current, vcpu_locked); |
| 1153 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1154 | /* It has been decided that the vCPU should be run. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1155 | vcpu->cpu = current->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1156 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1157 | |
Olivier Deprez | 04168ed | 2022-09-26 09:20:00 +0200 | [diff] [blame] | 1158 | if (vcpu_was_init_state) { |
| 1159 | vcpu_set_phys_core_idx(vcpu); |
| 1160 | vcpu_set_boot_info_gp_reg(vcpu); |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 1161 | } |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 1162 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1163 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1164 | * Mark the registers as unavailable now that we're about to reflect |
| 1165 | * them onto the real registers. This will also prevent another physical |
| 1166 | * CPU from trying to read these registers. |
| 1167 | */ |
| 1168 | vcpu->regs_available = false; |
| 1169 | |
| 1170 | ret = true; |
| 1171 | |
| 1172 | out: |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1173 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1174 | if (need_vm_lock) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 1175 | vm_unlock(&vm_locked); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1176 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1177 | return ret; |
| 1178 | } |
| 1179 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1180 | struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx, |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1181 | struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1182 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1183 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1184 | struct vcpu *vcpu; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1185 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1186 | enum vcpu_state next_state = VCPU_STATE_BLOCKED; |
| 1187 | struct vcpu_locked current_locked; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1188 | |
Raghu Krishnamurthy | 048d63f | 2021-12-11 12:45:41 -0800 | [diff] [blame] | 1189 | if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) { |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1190 | return ret; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 1191 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1192 | |
Raghu Krishnamurthy | 62f97a7 | 2021-07-27 02:14:59 -0700 | [diff] [blame] | 1193 | if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) { |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1197 | /* The requested VM must exist. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1198 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1199 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1200 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1203 | /* The requested vCPU must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1204 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1205 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 1206 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1207 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1208 | /* |
| 1209 | * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC |
| 1210 | * bypasses the intermediate execution contexts and resumes the |
| 1211 | * SP execution context that was originally preempted. |
| 1212 | */ |
| 1213 | if (*next != NULL) { |
| 1214 | vcpu = *next; |
| 1215 | } else { |
| 1216 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
| 1217 | } |
| 1218 | |
| 1219 | if (!plat_ffa_check_runtime_state_transition(current, current->vm->id, |
| 1220 | HF_INVALID_VM_ID, vcpu, |
| 1221 | FFA_RUN_32, &next_state)) { |
| 1222 | return ffa_error(FFA_DENIED); |
| 1223 | } |
| 1224 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 1225 | if (!api_vcpu_prepare_run(current, vcpu, &ret)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1226 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1227 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1228 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1229 | /* |
| 1230 | * Inject timer interrupt if timer has expired. It's safe to access |
| 1231 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 1232 | * regs_available was true (and then set it to false) before returning |
| 1233 | * true. |
| 1234 | */ |
| 1235 | if (arch_timer_pending(&vcpu->regs)) { |
| 1236 | /* Make virtual timer interrupt pending. */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 1237 | internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu, |
| 1238 | NULL); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * Set the mask bit so the hardware interrupt doesn't fire |
| 1242 | * again. Ideally we wouldn't do this because it affects what |
| 1243 | * the secondary vCPU sees, but if we don't then we end up with |
| 1244 | * a loop of the interrupt firing each time we try to return to |
| 1245 | * the secondary vCPU. |
| 1246 | */ |
| 1247 | arch_timer_mask(&vcpu->regs); |
| 1248 | } |
| 1249 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1250 | /* Switch to the vCPU. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1251 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 1252 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 1253 | assert(next_state == VCPU_STATE_BLOCKED); |
| 1254 | current_locked = vcpu_lock(current); |
| 1255 | current->state = VCPU_STATE_BLOCKED; |
| 1256 | vcpu_unlock(¤t_locked); |
| 1257 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 1258 | /* |
| 1259 | * Set a placeholder return code to the scheduler. This will be |
| 1260 | * overwritten when the switch back to the primary occurs. |
| 1261 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1262 | ret.func = FFA_INTERRUPT_32; |
Andrew Walbran | 7e82460 | 2020-10-22 16:51:40 +0100 | [diff] [blame] | 1263 | ret.arg1 = 0; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 1264 | ret.arg2 = 0; |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 1265 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 1266 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1267 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1271 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 1272 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 1273 | static bool api_mode_valid_owned_and_exclusive(uint32_t mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1274 | { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 1275 | return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 1276 | MM_MODE_SHARED)) == 0; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1280 | * 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] | 1281 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1282 | static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked, |
| 1283 | struct vm_locked vm_locked, |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1284 | paddr_t pa_send_begin, paddr_t pa_send_end, |
| 1285 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1286 | uint32_t extra_attributes, |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1287 | struct mpool *local_page_pool) |
| 1288 | { |
| 1289 | bool ret; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1290 | |
| 1291 | /* Map the send page as read-only in the hypervisor address space. */ |
| 1292 | vm_locked.vm->mailbox.send = |
| 1293 | mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1294 | MM_MODE_R | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1295 | if (!vm_locked.vm->mailbox.send) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1296 | goto fail; |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Map the receive page as writable in the hypervisor address space. On |
| 1301 | * failure, unmap the send page before returning. |
| 1302 | */ |
| 1303 | vm_locked.vm->mailbox.recv = |
| 1304 | mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1305 | MM_MODE_W | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1306 | if (!vm_locked.vm->mailbox.recv) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1307 | goto fail_undo_send; |
| 1308 | } |
| 1309 | |
| 1310 | ret = true; |
| 1311 | goto out; |
| 1312 | |
| 1313 | /* |
| 1314 | * The following mappings will not require more memory than is available |
| 1315 | * in the local pool. |
| 1316 | */ |
| 1317 | fail_undo_send: |
| 1318 | vm_locked.vm->mailbox.send = NULL; |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 1319 | CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 1320 | local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1321 | |
| 1322 | fail: |
| 1323 | ret = false; |
| 1324 | |
| 1325 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1330 | * Sanity checks and configures the send and receive pages in the VM stage-2 |
| 1331 | * and hypervisor stage-1 page tables. |
| 1332 | * |
| 1333 | * Returns: |
| 1334 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1335 | * aligned, are the same or have invalid attributes. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1336 | * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers |
| 1337 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1338 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1339 | * - FFA_SUCCESS on success if no further action is needed. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1340 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1341 | |
| 1342 | struct ffa_value api_vm_configure_pages( |
| 1343 | struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked, |
| 1344 | ipaddr_t send, ipaddr_t recv, uint32_t page_count, |
| 1345 | struct mpool *local_page_pool) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1346 | { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1347 | struct ffa_value ret; |
| 1348 | paddr_t pa_send_begin; |
| 1349 | paddr_t pa_send_end; |
| 1350 | paddr_t pa_recv_begin; |
| 1351 | paddr_t pa_recv_end; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1352 | uint32_t orig_send_mode = 0; |
| 1353 | uint32_t orig_recv_mode = 0; |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1354 | uint32_t extra_attributes; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1355 | |
| 1356 | /* We only allow these to be setup once. */ |
| 1357 | if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) { |
| 1358 | ret = ffa_error(FFA_DENIED); |
| 1359 | goto out; |
| 1360 | } |
| 1361 | |
| 1362 | /* Hafnium only supports a fixed size of RX/TX buffers. */ |
| 1363 | if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) { |
| 1364 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1365 | goto out; |
| 1366 | } |
| 1367 | |
| 1368 | /* Fail if addresses are not page-aligned. */ |
| 1369 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 1370 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
| 1371 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1372 | goto out; |
| 1373 | } |
| 1374 | |
| 1375 | /* Convert to physical addresses. */ |
| 1376 | pa_send_begin = pa_from_ipa(send); |
| 1377 | pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE); |
| 1378 | pa_recv_begin = pa_from_ipa(recv); |
| 1379 | pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE); |
| 1380 | |
| 1381 | /* Fail if the same page is used for the send and receive pages. */ |
| 1382 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
| 1383 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1384 | goto out; |
| 1385 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1386 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1387 | /* Set stage 2 translation tables only for virtual FF-A instances. */ |
| 1388 | if (vm_id_is_current_world(vm_locked.vm->id)) { |
| 1389 | /* |
| 1390 | * Ensure the pages are valid, owned and exclusive to the VM and |
| 1391 | * that the VM has the required access to the memory. |
| 1392 | */ |
| 1393 | if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE), |
| 1394 | &orig_send_mode) || |
| 1395 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 1396 | (orig_send_mode & MM_MODE_R) == 0 || |
| 1397 | (orig_send_mode & MM_MODE_W) == 0) { |
| 1398 | dlog_error( |
| 1399 | "VM doesn't have required access rights to map " |
| 1400 | "TX buffer in stage 2.\n"); |
| 1401 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1402 | goto out; |
| 1403 | } |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1404 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1405 | if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE), |
| 1406 | &orig_recv_mode) || |
| 1407 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 1408 | (orig_recv_mode & MM_MODE_R) == 0) { |
| 1409 | dlog_error( |
| 1410 | "VM doesn't have required access rights to map " |
| 1411 | "RX buffer in stage 2.\n"); |
| 1412 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1413 | goto out; |
| 1414 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1415 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1416 | /* Take memory ownership away from the VM and mark as shared. */ |
| 1417 | uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | |
| 1418 | MM_MODE_W; |
| 1419 | if (vm_locked.vm->el0_partition) { |
| 1420 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1421 | } |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1422 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1423 | if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
| 1424 | mode, local_page_pool, NULL)) { |
| 1425 | dlog_error( |
| 1426 | "Cannot allocate a new entry in stage 2 " |
| 1427 | "translation table.\n"); |
| 1428 | ret = ffa_error(FFA_NO_MEMORY); |
| 1429 | goto out; |
| 1430 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1431 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1432 | mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R; |
| 1433 | if (vm_locked.vm->el0_partition) { |
| 1434 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1435 | } |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1436 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1437 | if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
| 1438 | mode, local_page_pool, NULL)) { |
| 1439 | /* TODO: partial defrag of failed range. */ |
| 1440 | /* Recover any memory consumed in failed mapping. */ |
| 1441 | vm_ptable_defrag(vm_locked, local_page_pool); |
| 1442 | goto fail_undo_send; |
| 1443 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1446 | /* Get extra send/recv pages mapping attributes for the given VM ID. */ |
| 1447 | extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id); |
| 1448 | |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1449 | /* |
| 1450 | * For EL0 partitions, since both the partition and the hypervisor code |
| 1451 | * use the EL2&0 translation regime, it is critical to mark the mappings |
| 1452 | * of the send and recv buffers as non-global in the TLB. For one, if we |
| 1453 | * dont mark it as non-global, it would cause TLB conflicts since there |
| 1454 | * would be an identity mapping with non-global attribute in the |
| 1455 | * partitions page tables, but another identity mapping in the |
| 1456 | * hypervisor page tables with the global attribute. The other issue is |
| 1457 | * one of security, we dont want other partitions to be able to access |
| 1458 | * other partitions buffers through cached translations. |
| 1459 | */ |
| 1460 | if (vm_locked.vm->el0_partition) { |
| 1461 | extra_attributes |= MM_MODE_NG; |
| 1462 | } |
| 1463 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1464 | if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin, |
| 1465 | pa_send_end, pa_recv_begin, pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1466 | extra_attributes, local_page_pool)) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1467 | goto fail_undo_send_and_recv; |
| 1468 | } |
| 1469 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1470 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1471 | goto out; |
| 1472 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1473 | fail_undo_send_and_recv: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1474 | CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1475 | orig_recv_mode, local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1476 | |
| 1477 | fail_undo_send: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1478 | CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1479 | orig_send_mode, local_page_pool, NULL)); |
| 1480 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1481 | |
| 1482 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1483 | return ret; |
| 1484 | } |
| 1485 | |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1486 | static void api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send, |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1487 | ipaddr_t *recv, uint32_t *page_count, |
| 1488 | ffa_vm_id_t *owner_vm_id) |
| 1489 | { |
| 1490 | /* |
| 1491 | * If the message has been forwarded the effective addresses are in |
| 1492 | * hypervisor's TX buffer. |
| 1493 | */ |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1494 | bool forwarded = (current_vm->id == HF_OTHER_WORLD_ID) && |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1495 | (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) && |
| 1496 | (*page_count == 0); |
| 1497 | |
| 1498 | if (forwarded) { |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1499 | struct vm_locked vm_locked = vm_lock(current_vm); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1500 | struct ffa_endpoint_rx_tx_descriptor *endpoint_desc = |
| 1501 | (struct ffa_endpoint_rx_tx_descriptor *) |
| 1502 | vm_locked.vm->mailbox.send; |
| 1503 | struct ffa_composite_memory_region *rx_region = |
| 1504 | ffa_enpoint_get_rx_memory_region(endpoint_desc); |
| 1505 | struct ffa_composite_memory_region *tx_region = |
| 1506 | ffa_enpoint_get_tx_memory_region(endpoint_desc); |
| 1507 | |
| 1508 | *owner_vm_id = endpoint_desc->endpoint_id; |
| 1509 | *recv = ipa_init(rx_region->constituents[0].address); |
| 1510 | *send = ipa_init(tx_region->constituents[0].address); |
| 1511 | *page_count = rx_region->constituents[0].page_count; |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1512 | |
| 1513 | vm_unlock(&vm_locked); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1514 | } else { |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1515 | *owner_vm_id = current_vm->id; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1516 | } |
| 1517 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1518 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1519 | * 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] | 1520 | * must not be shared. Locking of the page tables combined with a local memory |
| 1521 | * pool ensures there will always be enough memory to recover from any errors |
| 1522 | * that arise. The stage-1 page tables must be locked so memory cannot be taken |
| 1523 | * by another core which could result in this transaction being unable to roll |
| 1524 | * back in the case of an error. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1525 | * |
| 1526 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1527 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1528 | * aligned, are the same or have invalid attributes. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1529 | * - 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] | 1530 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1531 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1532 | * - FFA_SUCCESS on success if no further action is needed. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1533 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1534 | 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] | 1535 | uint32_t page_count, struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1536 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1537 | struct ffa_value ret; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1538 | struct vm_locked owner_vm_locked; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1539 | struct mm_stage1_locked mm_stage1_locked; |
| 1540 | struct mpool local_page_pool; |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1541 | ffa_vm_id_t owner_vm_id; |
| 1542 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1543 | /* |
| 1544 | * Get the original buffer addresses and VM ID in case of forwarded |
| 1545 | * message. |
| 1546 | */ |
J-Alves | 28ad9b4 | 2022-12-08 12:19:43 +0000 | [diff] [blame] | 1547 | api_get_rxtx_description(current->vm, &send, &recv, &page_count, |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1548 | &owner_vm_id); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1549 | |
| 1550 | owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id); |
| 1551 | if (owner_vm_locked.vm == NULL) { |
| 1552 | dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n", |
| 1553 | owner_vm_id); |
| 1554 | return ffa_error(FFA_DENIED); |
| 1555 | } |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1556 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1557 | /* |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1558 | * Create a local pool so any freed memory can't be used by another |
| 1559 | * thread. This is to ensure the original mapping can be restored if any |
| 1560 | * stage of the process fails. |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1561 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1562 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1563 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1564 | mm_stage1_locked = mm_lock_stage1(); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1565 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1566 | ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send, |
| 1567 | recv, page_count, &local_page_pool); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1568 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1569 | goto exit; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1570 | } |
| 1571 | |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1572 | /* Forward buffer mapping to SPMC if coming from a VM. */ |
| 1573 | plat_ffa_rxtx_map_forward(owner_vm_locked); |
| 1574 | |
Federico Recanati | 9f1b653 | 2022-04-14 13:15:28 +0200 | [diff] [blame] | 1575 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1576 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1577 | exit: |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1578 | mpool_fini(&local_page_pool); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1579 | mm_unlock_stage1(&mm_stage1_locked); |
Federico Recanati | 8d8b1cf | 2022-04-14 13:16:00 +0200 | [diff] [blame] | 1580 | vm_unlock(&owner_vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1581 | |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1586 | * Unmaps the RX/TX buffer pair with a partition or partition manager from the |
| 1587 | * translation regime of the caller. Unmap the region for the hypervisor and |
| 1588 | * set the memory region to owned and exclusive for the component. Since the |
| 1589 | * memory region mapped in the page table, when the buffers were originally |
| 1590 | * created we can safely remap it. |
| 1591 | * |
| 1592 | * Returns: |
| 1593 | * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on |
| 1594 | * behalf of the caller. |
| 1595 | * - FFA_SUCCESS on success if no further action is needed. |
| 1596 | */ |
| 1597 | struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id, |
| 1598 | struct vcpu *current) |
| 1599 | { |
| 1600 | struct vm *vm = current->vm; |
| 1601 | struct vm_locked vm_locked; |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1602 | ffa_vm_id_t owner_vm_id; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1603 | struct mm_stage1_locked mm_stage1_locked; |
| 1604 | paddr_t send_pa_begin; |
| 1605 | paddr_t send_pa_end; |
| 1606 | paddr_t recv_pa_begin; |
| 1607 | paddr_t recv_pa_end; |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1608 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1609 | |
J-Alves | 9bd324a | 2023-01-12 10:52:52 +0000 | [diff] [blame] | 1610 | if (vm->id == HF_HYPERVISOR_VM_ID && !plat_ffa_is_vm_id(allocator_id)) { |
| 1611 | dlog_error( |
| 1612 | "The Hypervisor must specify a valid VM ID in register " |
| 1613 | "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n"); |
| 1614 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1615 | } |
| 1616 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1617 | /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */ |
| 1618 | if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) { |
J-Alves | 9bd324a | 2023-01-12 10:52:52 +0000 | [diff] [blame] | 1619 | dlog_error( |
| 1620 | "The register W1 (containing ID) must be 0 at virtual " |
| 1621 | "instances.\n"); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1622 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1623 | } |
| 1624 | |
| 1625 | /* VM ID of which buffers have to be unmapped. */ |
| 1626 | owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id; |
| 1627 | |
| 1628 | vm_locked = plat_ffa_vm_find_locked(owner_vm_id); |
| 1629 | vm = vm_locked.vm; |
| 1630 | if (vm == NULL) { |
| 1631 | dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n", |
| 1632 | owner_vm_id); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1633 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1634 | } |
| 1635 | |
| 1636 | /* Get send and receive buffers. */ |
| 1637 | if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) { |
Olivier Deprez | 86d87ae | 2021-08-19 14:27:46 +0200 | [diff] [blame] | 1638 | dlog_verbose( |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1639 | "No buffer pair registered on behalf of the caller.\n"); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1640 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1641 | goto out; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* Currently a mailbox size of 1 page is assumed. */ |
| 1645 | send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send)); |
| 1646 | send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE); |
| 1647 | recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv)); |
| 1648 | recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE); |
| 1649 | |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1650 | mm_stage1_locked = mm_lock_stage1(); |
| 1651 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1652 | /* Reset stage 2 mapping only for virtual FF-A instances. */ |
| 1653 | if (vm_id_is_current_world(owner_vm_id)) { |
| 1654 | /* |
| 1655 | * Set the memory region of the buffers back to the default mode |
| 1656 | * for the VM. Since this memory region was already mapped for |
| 1657 | * the RXTX buffers we can safely remap them. |
| 1658 | */ |
| 1659 | CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end, |
| 1660 | MM_MODE_R | MM_MODE_W | MM_MODE_X, |
| 1661 | &api_page_pool, NULL)); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1662 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1663 | CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end, |
| 1664 | MM_MODE_R | MM_MODE_W | MM_MODE_X, |
| 1665 | &api_page_pool, NULL)); |
| 1666 | } |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1667 | |
| 1668 | /* Unmap the buffers in the partition manager. */ |
| 1669 | CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end, |
| 1670 | &api_page_pool)); |
| 1671 | CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end, |
| 1672 | &api_page_pool)); |
| 1673 | |
| 1674 | vm->mailbox.send = NULL; |
| 1675 | vm->mailbox.recv = NULL; |
Federico Recanati | 10bd06c | 2022-02-23 17:32:59 +0100 | [diff] [blame] | 1676 | plat_ffa_vm_destroy(vm_locked); |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1677 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1678 | /* Forward buffer unmapping to SPMC if coming from a VM. */ |
J-Alves | 7007993 | 2022-12-07 17:32:20 +0000 | [diff] [blame] | 1679 | plat_ffa_rxtx_unmap_forward(vm_locked); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1680 | |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1681 | mm_unlock_stage1(&mm_stage1_locked); |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1682 | |
| 1683 | out: |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1684 | vm_unlock(&vm_locked); |
| 1685 | |
Federico Recanati | 8da9e33 | 2022-02-10 11:00:17 +0100 | [diff] [blame] | 1686 | return ret; |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /** |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1690 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 1691 | * and notifies the receiver. |
| 1692 | */ |
| 1693 | struct ffa_value api_ffa_msg_send2(ffa_vm_id_t sender_vm_id, uint32_t flags, |
| 1694 | struct vcpu *current) |
| 1695 | { |
| 1696 | struct vm *from = current->vm; |
| 1697 | struct vm *to; |
| 1698 | struct vm_locked to_locked; |
| 1699 | ffa_vm_id_t msg_sender_id; |
| 1700 | struct vm_locked sender_locked; |
| 1701 | const void *from_msg; |
| 1702 | struct ffa_value ret; |
| 1703 | struct ffa_partition_rxtx_header header; |
| 1704 | ffa_vm_id_t sender_id; |
| 1705 | ffa_vm_id_t receiver_id; |
| 1706 | uint32_t msg_size; |
| 1707 | ffa_notifications_bitmap_t rx_buffer_full; |
| 1708 | |
| 1709 | /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */ |
| 1710 | if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) { |
| 1711 | dlog_error("Sender VM ID must be zero.\n"); |
| 1712 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1713 | } |
| 1714 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1715 | /* |
| 1716 | * Get message sender's mailbox, which can be different to the `from` vm |
| 1717 | * when the message is forwarded. |
| 1718 | */ |
| 1719 | msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id; |
| 1720 | sender_locked = plat_ffa_vm_find_locked(msg_sender_id); |
| 1721 | if (sender_locked.vm == NULL) { |
| 1722 | dlog_error("Cannot send message from VM ID %#x, not found.\n", |
| 1723 | msg_sender_id); |
| 1724 | return ffa_error(FFA_DENIED); |
| 1725 | } |
| 1726 | |
| 1727 | from_msg = sender_locked.vm->mailbox.send; |
| 1728 | if (from_msg == NULL) { |
| 1729 | dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n", |
| 1730 | msg_sender_id); |
| 1731 | ret = ffa_error(FFA_DENIED); |
| 1732 | goto out_unlock_sender; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * Copy message header as safety measure to avoid multiple accesses to |
| 1737 | * unsafe memory which could be 'corrupted' between safety checks and |
| 1738 | * final buffer copy. |
| 1739 | */ |
| 1740 | memcpy_s(&header, FFA_RXTX_HEADER_SIZE, from_msg, FFA_RXTX_HEADER_SIZE); |
| 1741 | sender_id = ffa_rxtx_header_sender(&header); |
| 1742 | receiver_id = ffa_rxtx_header_receiver(&header); |
| 1743 | |
| 1744 | /* Ensure Sender IDs from API and from message header match. */ |
| 1745 | if (msg_sender_id != sender_id) { |
| 1746 | dlog_error( |
| 1747 | "Message sender VM ID (%#x) doesn't match header's VM " |
| 1748 | "ID (%#x).\n", |
| 1749 | msg_sender_id, sender_id); |
| 1750 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1751 | goto out_unlock_sender; |
| 1752 | } |
| 1753 | |
| 1754 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 1755 | if (receiver_id == sender_id) { |
| 1756 | dlog_error("Sender and receive VM IDs must be different.\n"); |
| 1757 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1758 | goto out_unlock_sender; |
| 1759 | } |
| 1760 | |
Federico Recanati | 7b39ff2 | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1761 | /* `flags` can be set only at secure virtual FF-A instances. */ |
| 1762 | if (plat_ffa_is_vm_id(sender_id) && (flags != 0)) { |
| 1763 | dlog_error("flags must be zero.\n"); |
| 1764 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1765 | } |
| 1766 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1767 | /* |
| 1768 | * Check if the message has to be forwarded to the SPMC, in |
| 1769 | * this case return, the SPMC will handle the buffer copy. |
| 1770 | */ |
| 1771 | if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) { |
| 1772 | goto out_unlock_sender; |
| 1773 | } |
| 1774 | |
| 1775 | /* Ensure the receiver VM exists. */ |
| 1776 | to_locked = plat_ffa_vm_find_locked(receiver_id); |
| 1777 | to = to_locked.vm; |
| 1778 | |
| 1779 | if (to == NULL) { |
| 1780 | dlog_error("Cannot deliver message to VM %#x, not found.\n", |
| 1781 | receiver_id); |
| 1782 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1783 | goto out_unlock_sender; |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * Check sender and receiver can use indirect messages. |
| 1788 | * Sender is the VM/SP who originally sent the message, not the |
| 1789 | * hypervisor possibly relaying it. |
| 1790 | */ |
| 1791 | if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) { |
| 1792 | dlog_verbose("VM %#x doesn't support indirect message\n", |
| 1793 | sender_id); |
| 1794 | ret = ffa_error(FFA_DENIED); |
| 1795 | goto out; |
| 1796 | } |
| 1797 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1798 | if (vm_is_mailbox_busy(to_locked)) { |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1799 | dlog_error( |
| 1800 | "Cannot deliver message to VM %#x, RX buffer not " |
| 1801 | "ready.\n", |
| 1802 | receiver_id); |
| 1803 | ret = ffa_error(FFA_BUSY); |
| 1804 | goto out; |
| 1805 | } |
| 1806 | |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 1807 | /* Acquire receiver's RX buffer. */ |
| 1808 | if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) { |
| 1809 | dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id); |
| 1810 | goto out; |
| 1811 | } |
| 1812 | |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1813 | /* Check the size of transfer. */ |
| 1814 | msg_size = FFA_RXTX_HEADER_SIZE + header.size; |
| 1815 | if ((msg_size > FFA_PARTITION_MSG_PAYLOAD_MAX) || |
| 1816 | (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) { |
| 1817 | dlog_error("Message is too big.\n"); |
| 1818 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1819 | goto out; |
| 1820 | } |
| 1821 | |
| 1822 | /* Copy data. */ |
| 1823 | memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, msg_size); |
| 1824 | to->mailbox.recv_size = msg_size; |
| 1825 | to->mailbox.recv_sender = sender_id; |
| 1826 | to->mailbox.recv_func = FFA_MSG_SEND2_32; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1827 | to->mailbox.state = MAILBOX_STATE_FULL; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1828 | |
| 1829 | rx_buffer_full = plat_ffa_is_vm_id(sender_id) |
| 1830 | ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK |
| 1831 | : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK; |
| 1832 | vm_notifications_framework_set_pending(to_locked, rx_buffer_full); |
| 1833 | |
| 1834 | if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) { |
| 1835 | dlog_verbose("SRI was NOT delayed. vcpu: %u!\n", |
| 1836 | vcpu_index(current)); |
| 1837 | plat_ffa_sri_trigger_not_delayed(current->cpu); |
| 1838 | } else { |
| 1839 | plat_ffa_sri_state_set(DELAYED); |
| 1840 | } |
| 1841 | |
| 1842 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 1843 | |
| 1844 | out: |
| 1845 | vm_unlock(&to_locked); |
| 1846 | |
| 1847 | out_unlock_sender: |
| 1848 | vm_unlock(&sender_locked); |
| 1849 | |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
| 1853 | /** |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1854 | * Checks whether the vCPU's attempt to block for a message has already been |
| 1855 | * interrupted or whether it is allowed to block. |
| 1856 | */ |
Olivier Deprez | d8e1888 | 2022-07-15 14:46:41 +0200 | [diff] [blame] | 1857 | static bool api_ffa_msg_recv_block_interrupted(struct vcpu *current) |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1858 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1859 | struct vcpu_locked current_locked; |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1860 | bool interrupted; |
| 1861 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1862 | current_locked = vcpu_lock(current); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1863 | |
| 1864 | /* |
| 1865 | * Don't block if there are enabled and pending interrupts, to match |
| 1866 | * behaviour of wait_for_interrupt. |
| 1867 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1868 | interrupted = (vcpu_interrupt_count_get(current_locked) > 0); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1869 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1870 | vcpu_unlock(¤t_locked); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1871 | |
| 1872 | return interrupted; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1876 | * Receives a message from the mailbox. If one isn't available, this function |
| 1877 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1878 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1879 | * No new messages can be received until the mailbox has been cleared. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1880 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1881 | struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current, |
| 1882 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1883 | { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1884 | bool is_direct_request_ongoing; |
| 1885 | struct vcpu_locked current_locked; |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1886 | struct vm *vm = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1887 | struct ffa_value return_code; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1888 | bool is_from_secure_world = |
| 1889 | (current->vm->id & HF_VM_ID_WORLD_MASK) != 0; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1890 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1891 | /* |
| 1892 | * The primary VM will receive messages as a status code from running |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1893 | * vCPUs and must not call this function. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1894 | */ |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1895 | if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1896 | return ffa_error(FFA_NOT_SUPPORTED); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1899 | /* |
| 1900 | * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ |
| 1901 | * invocation. |
| 1902 | */ |
| 1903 | current_locked = vcpu_lock(current); |
| 1904 | is_direct_request_ongoing = |
| 1905 | is_ffa_direct_msg_request_ongoing(current_locked); |
| 1906 | vcpu_unlock(¤t_locked); |
| 1907 | |
| 1908 | if (is_direct_request_ongoing) { |
| 1909 | return ffa_error(FFA_DENIED); |
| 1910 | } |
| 1911 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1912 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1913 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1914 | /* Return pending messages without blocking. */ |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1915 | if (vm->mailbox.state == MAILBOX_STATE_FULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1916 | return_code = ffa_msg_recv_return(vm); |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1917 | if (return_code.func == FFA_MSG_SEND_32) { |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 1918 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 1919 | } |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1920 | goto out; |
| 1921 | } |
| 1922 | |
| 1923 | /* No pending message so fail if not allowed to block. */ |
| 1924 | if (!block) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1925 | return_code = ffa_error(FFA_RETRY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1926 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1927 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1928 | |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1929 | /* |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1930 | * From this point onward this call can only be interrupted or a message |
| 1931 | * received. If a message is received the return value will be set at |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1932 | * that time to FFA_SUCCESS. |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1933 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1934 | return_code = ffa_error(FFA_INTERRUPTED); |
| 1935 | if (api_ffa_msg_recv_block_interrupted(current)) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1936 | goto out; |
| 1937 | } |
| 1938 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1939 | if (is_from_secure_world) { |
| 1940 | /* Return to other world if caller is a SP. */ |
| 1941 | *next = api_switch_to_other_world( |
| 1942 | current, (struct ffa_value){.func = FFA_MSG_WAIT_32}, |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1943 | VCPU_STATE_WAITING); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1944 | } else { |
| 1945 | /* Switch back to primary VM to block. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1946 | struct ffa_value run_return = { |
| 1947 | .func = FFA_MSG_WAIT_32, |
| 1948 | .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)), |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1949 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1950 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1951 | *next = api_switch_to_primary(current, run_return, |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 1952 | VCPU_STATE_WAITING); |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1953 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1954 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1955 | sl_unlock(&vm->lock); |
| 1956 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1957 | return return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1961 | * Retrieves the next VM whose mailbox became writable. For a VM to be notified |
| 1962 | * by this function, the caller must have called api_mailbox_send before with |
| 1963 | * the notify argument set to true, and this call must have failed because the |
| 1964 | * mailbox was not available. |
| 1965 | * |
| 1966 | * It should be called repeatedly to retrieve a list of VMs. |
| 1967 | * |
| 1968 | * Returns -1 if no VM became writable, or the id of the VM whose mailbox |
| 1969 | * became writable. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1970 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1971 | int64_t api_mailbox_writable_get(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1972 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1973 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1974 | struct wait_entry *entry; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 1975 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1976 | |
| 1977 | sl_lock(&vm->lock); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1978 | if (list_empty(&vm->mailbox.ready_list)) { |
| 1979 | ret = -1; |
| 1980 | goto exit; |
| 1981 | } |
| 1982 | |
| 1983 | entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry, |
| 1984 | ready_links); |
| 1985 | list_remove(&entry->ready_links); |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 1986 | ret = vm_id_for_wait_entry(vm, entry); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1987 | |
| 1988 | exit: |
| 1989 | sl_unlock(&vm->lock); |
| 1990 | return ret; |
| 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * Retrieves the next VM waiting to be notified that the mailbox of the |
| 1995 | * specified VM became writable. Only primary VMs are allowed to call this. |
| 1996 | * |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 1997 | * Returns -1 on failure or if there are no waiters; the VM id of the next |
| 1998 | * waiter otherwise. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1999 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2000 | int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2001 | { |
| 2002 | struct vm *vm; |
| 2003 | struct vm_locked locked; |
| 2004 | struct wait_entry *entry; |
| 2005 | struct vm *waiting_vm; |
| 2006 | |
| 2007 | /* Only primary VMs are allowed to call this function. */ |
| 2008 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 2009 | return -1; |
| 2010 | } |
| 2011 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 2012 | vm = vm_find(vm_id); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2013 | if (vm == NULL) { |
| 2014 | return -1; |
| 2015 | } |
| 2016 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 2017 | /* Check if there are outstanding notifications from given VM. */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 2018 | locked = vm_lock(vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2019 | entry = api_fetch_waiter(locked); |
| 2020 | vm_unlock(&locked); |
| 2021 | |
| 2022 | if (entry == NULL) { |
| 2023 | return -1; |
| 2024 | } |
| 2025 | |
| 2026 | /* Enqueue notification to waiting VM. */ |
| 2027 | waiting_vm = entry->waiting_vm; |
| 2028 | |
| 2029 | sl_lock(&waiting_vm->lock); |
| 2030 | if (list_empty(&entry->ready_links)) { |
| 2031 | list_append(&waiting_vm->mailbox.ready_list, |
| 2032 | &entry->ready_links); |
| 2033 | } |
| 2034 | sl_unlock(&waiting_vm->lock); |
| 2035 | |
| 2036 | return waiting_vm->id; |
| 2037 | } |
| 2038 | |
| 2039 | /** |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 2040 | * Releases the caller's mailbox so that a new message can be received. The |
| 2041 | * caller must have copied out all data they wish to preserve as new messages |
| 2042 | * will overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2043 | * |
| 2044 | * Returns: |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2045 | * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but |
| 2046 | * there's no buffer pair mapped. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2047 | * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read. |
| 2048 | * - FFA_SUCCESS on success if no further action is needed. |
| 2049 | * - 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] | 2050 | * 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] | 2051 | * hf_mailbox_waiter_get. |
| 2052 | */ |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2053 | struct ffa_value api_ffa_rx_release(ffa_vm_id_t receiver_id, |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2054 | struct vcpu *current) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2055 | { |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2056 | struct vm *current_vm = current->vm; |
| 2057 | struct vm *vm; |
| 2058 | struct vm_locked vm_locked; |
| 2059 | ffa_vm_id_t current_vm_id = current_vm->id; |
| 2060 | ffa_vm_id_t release_vm_id; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2061 | struct ffa_value ret; |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2062 | int32_t error_code; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 2063 | |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2064 | /* `receiver_id` can be set only at Non-Secure Physical interface. */ |
| 2065 | if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) { |
| 2066 | dlog_error("Invalid `receiver_id`, must be zero.\n"); |
| 2067 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * VM ID to be released: `receiver_id` if message has been forwarded by |
| 2072 | * Hypervisor to release a VM's buffer, current VM ID otherwise. |
| 2073 | */ |
| 2074 | if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) { |
| 2075 | release_vm_id = current_vm_id; |
| 2076 | } else { |
| 2077 | release_vm_id = receiver_id; |
| 2078 | } |
| 2079 | |
| 2080 | vm_locked = plat_ffa_vm_find_locked(release_vm_id); |
| 2081 | vm = vm_locked.vm; |
| 2082 | if (vm == NULL) { |
| 2083 | dlog_error("No buffer registered for VM ID %#x.\n", |
| 2084 | release_vm_id); |
| 2085 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2086 | } |
| 2087 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2088 | if (plat_ffa_rx_release_forward(vm_locked, &ret)) { |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2089 | goto out; |
| 2090 | } |
| 2091 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2092 | if (!api_release_mailbox(vm_locked, &error_code)) { |
| 2093 | ret = ffa_error(error_code); |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2094 | goto out; |
| 2095 | } |
| 2096 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2097 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Federico Recanati | 7bef0b9 | 2022-03-17 14:56:22 +0100 | [diff] [blame] | 2098 | |
| 2099 | out: |
| 2100 | vm_unlock(&vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 2101 | |
| 2102 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 2103 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2104 | |
| 2105 | /** |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2106 | * Acquire ownership of an RX buffer before writing to it. Both |
| 2107 | * Hypervisor and SPMC are producers of VM's RX buffer, and they |
| 2108 | * could contend for the same buffer. SPMC owns VM's RX buffer after |
| 2109 | * it's mapped in its translation regime. This ABI should be |
| 2110 | * used by the Hypervisor to get the ownership of a VM's RX buffer |
| 2111 | * from the SPMC solving the aforementioned possible contention. |
| 2112 | * |
| 2113 | * Returns: |
| 2114 | * - FFA_DENIED: callee cannot relinquish ownership of RX buffer. |
| 2115 | * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM. |
| 2116 | * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance. |
| 2117 | */ |
| 2118 | struct ffa_value api_ffa_rx_acquire(ffa_vm_id_t receiver_id, |
| 2119 | struct vcpu *current) |
| 2120 | { |
| 2121 | struct vm_locked receiver_locked; |
| 2122 | struct vm *receiver; |
| 2123 | struct ffa_value ret; |
| 2124 | |
| 2125 | if ((current->vm->id != HF_HYPERVISOR_VM_ID) || |
| 2126 | !plat_ffa_is_vm_id(receiver_id)) { |
| 2127 | dlog_error( |
| 2128 | "FFA_RX_ACQUIRE not supported at this FF-A " |
| 2129 | "instance.\n"); |
| 2130 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2131 | } |
| 2132 | |
| 2133 | receiver_locked = plat_ffa_vm_find_locked(receiver_id); |
| 2134 | receiver = receiver_locked.vm; |
| 2135 | |
| 2136 | if (receiver == NULL || receiver->mailbox.recv == NULL) { |
| 2137 | dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n", |
| 2138 | receiver_id); |
| 2139 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2140 | goto out; |
| 2141 | } |
| 2142 | |
| 2143 | if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) { |
| 2144 | dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id); |
| 2145 | ret = ffa_error(FFA_DENIED); |
| 2146 | goto out; |
| 2147 | } |
| 2148 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 2149 | receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED; |
Federico Recanati | 644f046 | 2022-03-17 12:04:00 +0100 | [diff] [blame] | 2150 | |
| 2151 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2152 | |
| 2153 | out: |
| 2154 | vm_unlock(&receiver_locked); |
| 2155 | |
| 2156 | return ret; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2160 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 2161 | * |
| 2162 | * Returns 0 on success, or -1 if the intid is invalid. |
| 2163 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2164 | int64_t api_interrupt_enable(uint32_t intid, bool enable, |
| 2165 | enum interrupt_type type, struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2166 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2167 | struct vcpu_locked current_locked; |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2168 | struct interrupts *interrupts = ¤t->interrupts; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2169 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2170 | if (intid >= HF_NUM_INTIDS) { |
| 2171 | return -1; |
| 2172 | } |
| 2173 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2174 | current_locked = vcpu_lock(current); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2175 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2176 | /* |
| 2177 | * If it is pending and was not enabled before, increment the |
| 2178 | * count. |
| 2179 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2180 | if (vcpu_is_virt_interrupt_pending(interrupts, intid) && |
| 2181 | !vcpu_is_virt_interrupt_enabled(interrupts, intid)) { |
| 2182 | vcpu_interrupt_count_increment(current_locked, |
| 2183 | interrupts, intid); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2184 | } |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2185 | vcpu_virt_interrupt_set_enabled(interrupts, intid); |
| 2186 | vcpu_virt_interrupt_set_type(interrupts, intid, type); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2187 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2188 | /* |
| 2189 | * If it is pending and was enabled before, decrement the count. |
| 2190 | */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2191 | if (vcpu_is_virt_interrupt_pending(interrupts, intid) && |
| 2192 | vcpu_is_virt_interrupt_enabled(interrupts, intid)) { |
| 2193 | vcpu_interrupt_count_decrement(current_locked, |
| 2194 | interrupts, intid); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2195 | } |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2196 | vcpu_virt_interrupt_clear_enabled(interrupts, intid); |
| 2197 | vcpu_virt_interrupt_set_type(interrupts, intid, |
| 2198 | INTERRUPT_TYPE_IRQ); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2201 | vcpu_unlock(¤t_locked); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2202 | return 0; |
| 2203 | } |
| 2204 | |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 2205 | static void api_interrupt_clear_decrement(struct vcpu_locked locked_vcpu, |
| 2206 | struct interrupts *interrupts, |
| 2207 | uint32_t intid) |
| 2208 | { |
| 2209 | vcpu_virt_interrupt_clear_pending(interrupts, intid); |
| 2210 | vcpu_interrupt_count_decrement(locked_vcpu, interrupts, intid); |
| 2211 | } |
| 2212 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2213 | /** |
| 2214 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 2215 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 2216 | * HF_INVALID_INTID if there are no pending interrupts. |
| 2217 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 2218 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2219 | { |
Raghu Krishnamurthy | 61a025b | 2022-07-20 08:37:04 -0700 | [diff] [blame] | 2220 | uint32_t i; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2221 | uint32_t first_interrupt = HF_INVALID_INTID; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2222 | struct vcpu_locked current_locked; |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2223 | struct interrupts *interrupts = ¤t->interrupts; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2224 | |
| 2225 | /* |
| 2226 | * Find the first enabled and pending interrupt ID, return it, and |
| 2227 | * deactivate it. |
| 2228 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2229 | current_locked = vcpu_lock(current); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2230 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 2231 | uint32_t enabled_and_pending = |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2232 | interrupts->interrupt_enabled.bitmap[i] & |
| 2233 | interrupts->interrupt_pending.bitmap[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2234 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2235 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2236 | uint8_t bit_index = ctz(enabled_and_pending); |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2237 | |
| 2238 | first_interrupt = |
| 2239 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2240 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2241 | /* |
| 2242 | * Mark it as no longer pending and decrement the count. |
| 2243 | */ |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 2244 | api_interrupt_clear_decrement( |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 2245 | current_locked, interrupts, first_interrupt); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2246 | break; |
| 2247 | } |
| 2248 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2249 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2250 | vcpu_unlock(¤t_locked); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2251 | return first_interrupt; |
| 2252 | } |
| 2253 | |
| 2254 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 2255 | * Returns whether the current vCPU is allowed to inject an interrupt into the |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2256 | * given VM and vCPU. |
| 2257 | */ |
| 2258 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 2259 | struct vcpu *current) |
| 2260 | { |
| 2261 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2262 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2263 | /* |
| 2264 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 2265 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 2266 | */ |
| 2267 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 2268 | current_vm_id == target_vm_id; |
| 2269 | } |
| 2270 | |
| 2271 | /** |
| 2272 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 2273 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 2274 | * when the vCPU is next run, which is up to the scheduler. |
| 2275 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 2276 | * Returns: |
| 2277 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 2278 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 2279 | * the target VM. |
| 2280 | * - 0 on success if no further action is needed. |
| 2281 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 2282 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2283 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2284 | int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id, |
| 2285 | ffa_vcpu_index_t target_vcpu_idx, uint32_t intid, |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 2286 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2287 | { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2288 | struct vcpu *target_vcpu; |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 2289 | struct vm *target_vm = vm_find(target_vm_id); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2290 | |
| 2291 | if (intid >= HF_NUM_INTIDS) { |
| 2292 | return -1; |
| 2293 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2294 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2295 | if (target_vm == NULL) { |
| 2296 | return -1; |
| 2297 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2298 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2299 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 2300 | /* The requested vCPU must exist. */ |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2301 | return -1; |
| 2302 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2303 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2304 | if (!is_injection_allowed(target_vm_id, current)) { |
| 2305 | return -1; |
| 2306 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 2307 | |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 2308 | target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2309 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 2310 | dlog_verbose( |
| 2311 | "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU " |
| 2312 | "%u\n", |
| 2313 | intid, target_vm_id, target_vcpu_idx, current->vm->id, |
| 2314 | vcpu_index(current)); |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 2315 | return internal_interrupt_inject(target_vcpu, intid, current, next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 2316 | } |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 2317 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2318 | /** Returns the version of the implemented FF-A specification. */ |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2319 | struct ffa_value api_ffa_version(struct vcpu *current, |
| 2320 | uint32_t requested_version) |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2321 | { |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2322 | struct vm_locked current_vm_locked; |
| 2323 | |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2324 | /* |
| 2325 | * Ensure that both major and minor revision representation occupies at |
| 2326 | * most 15 bits. |
| 2327 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2328 | static_assert(0x8000 > FFA_VERSION_MAJOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 2329 | "Major revision representation takes more than 15 bits."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2330 | static_assert(0x10000 > FFA_VERSION_MINOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 2331 | "Minor revision representation takes more than 16 bits."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2332 | if (requested_version & FFA_VERSION_RESERVED_BIT) { |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 2333 | /* Invalid encoding, return an error. */ |
J-Alves | 13318e3 | 2021-02-22 17:21:00 +0000 | [diff] [blame] | 2334 | return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED}; |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 2335 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2336 | |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 2337 | current_vm_locked = vm_lock(current->vm); |
| 2338 | current_vm_locked.vm->ffa_version = requested_version; |
| 2339 | vm_unlock(¤t_vm_locked); |
| 2340 | |
Daniel Boulby | 6e32c61 | 2021-02-17 15:09:41 +0000 | [diff] [blame] | 2341 | return ((struct ffa_value){.func = FFA_VERSION_COMPILED}); |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 2342 | } |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 2343 | |
| 2344 | int64_t api_debug_log(char c, struct vcpu *current) |
| 2345 | { |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 2346 | bool flush; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 2347 | struct vm *vm = current->vm; |
| 2348 | struct vm_locked vm_locked = vm_lock(vm); |
| 2349 | |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 2350 | if (c == '\n' || c == '\0') { |
| 2351 | flush = true; |
| 2352 | } else { |
| 2353 | vm->log_buffer[vm->log_buffer_length++] = c; |
| 2354 | flush = (vm->log_buffer_length == sizeof(vm->log_buffer)); |
| 2355 | } |
| 2356 | |
| 2357 | if (flush) { |
Andrew Walbran | 7f904bf | 2019-07-12 16:38:38 +0100 | [diff] [blame] | 2358 | dlog_flush_vm_buffer(vm->id, vm->log_buffer, |
| 2359 | vm->log_buffer_length); |
| 2360 | vm->log_buffer_length = 0; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | vm_unlock(&vm_locked); |
| 2364 | |
| 2365 | return 0; |
| 2366 | } |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2367 | |
| 2368 | /** |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2369 | * Helper for success return of FFA_FEATURES, for when it is used to query |
| 2370 | * an interrupt ID. |
| 2371 | */ |
| 2372 | struct ffa_value api_ffa_feature_success(uint32_t arg2) |
| 2373 | { |
| 2374 | return (struct ffa_value){ |
| 2375 | .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2}; |
| 2376 | } |
| 2377 | |
| 2378 | /** |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2379 | * Discovery function returning information about the implementation of optional |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2380 | * FF-A interfaces. |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2381 | */ |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2382 | struct ffa_value api_ffa_features(uint32_t feature_function_id, |
| 2383 | uint32_t input_property, uint32_t ffa_version) |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2384 | { |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2385 | /* |
| 2386 | * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ |
| 2387 | * if using a feature ID. |
| 2388 | */ |
| 2389 | if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U && |
J-Alves | ff676c1 | 2022-05-13 17:25:33 +0100 | [diff] [blame] | 2390 | (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) { |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2391 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2392 | } |
| 2393 | |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2394 | if (feature_function_id != FFA_MEM_RETRIEVE_REQ_32 && |
| 2395 | input_property != 0U) { |
| 2396 | dlog_verbose( |
| 2397 | "input_property must be zero.\ninput_property = %u.\n", |
| 2398 | input_property); |
| 2399 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2400 | } |
| 2401 | |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2402 | switch (feature_function_id) { |
| 2403 | /* Check support of the given Function ID. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2404 | case FFA_ERROR_32: |
| 2405 | case FFA_SUCCESS_32: |
| 2406 | case FFA_INTERRUPT_32: |
| 2407 | case FFA_VERSION_32: |
| 2408 | case FFA_FEATURES_32: |
| 2409 | case FFA_RX_RELEASE_32: |
| 2410 | case FFA_RXTX_MAP_64: |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 2411 | case FFA_RXTX_UNMAP_32: |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 2412 | case FFA_PARTITION_INFO_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2413 | case FFA_ID_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2414 | case FFA_MSG_WAIT_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2415 | case FFA_RUN_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2416 | case FFA_MEM_DONATE_32: |
| 2417 | case FFA_MEM_LEND_32: |
| 2418 | case FFA_MEM_SHARE_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2419 | case FFA_MEM_RETRIEVE_RESP_32: |
| 2420 | case FFA_MEM_RELINQUISH_32: |
| 2421 | case FFA_MEM_RECLAIM_32: |
J-Alves | ff676c1 | 2022-05-13 17:25:33 +0100 | [diff] [blame] | 2422 | case FFA_MEM_FRAG_RX_32: |
| 2423 | case FFA_MEM_FRAG_TX_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 2424 | case FFA_MSG_SEND_DIRECT_RESP_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2425 | case FFA_MSG_SEND_DIRECT_RESP_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 2426 | case FFA_MSG_SEND_DIRECT_REQ_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2427 | case FFA_MSG_SEND_DIRECT_REQ_32: |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 2428 | #if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 2429 | /* FF-A v1.1 features. */ |
| 2430 | case FFA_SPM_ID_GET_32: |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2431 | case FFA_NOTIFICATION_BITMAP_CREATE_32: |
| 2432 | case FFA_NOTIFICATION_BITMAP_DESTROY_32: |
| 2433 | case FFA_NOTIFICATION_BIND_32: |
| 2434 | case FFA_NOTIFICATION_UNBIND_32: |
| 2435 | case FFA_NOTIFICATION_SET_32: |
| 2436 | case FFA_NOTIFICATION_GET_32: |
| 2437 | case FFA_NOTIFICATION_INFO_GET_64: |
Raghu Krishnamurthy | 6764b07 | 2021-10-18 12:54:24 -0700 | [diff] [blame] | 2438 | case FFA_MEM_PERM_GET_32: |
| 2439 | case FFA_MEM_PERM_SET_32: |
| 2440 | case FFA_MEM_PERM_GET_64: |
| 2441 | case FFA_MEM_PERM_SET_64: |
Federico Recanati | 25053ee | 2022-03-14 15:01:53 +0100 | [diff] [blame] | 2442 | case FFA_MSG_SEND2_32: |
Raghu Krishnamurthy | 7592bcb | 2022-12-25 13:09:00 -0800 | [diff] [blame] | 2443 | case FFA_PARTITION_INFO_GET_REGS_64: |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 2444 | #endif |
| 2445 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Karl Meakin | 34b8ae9 | 2023-01-13 13:33:07 +0000 | [diff] [blame] | 2446 | case FFA_MEM_RETRIEVE_REQ_32: |
| 2447 | if ((input_property & FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK) != |
| 2448 | 0U) { |
| 2449 | dlog_verbose( |
| 2450 | "Bits[31:2] and Bit[0] of input_property must " |
| 2451 | "be zero.\ninput_property = %u.\n", |
| 2452 | input_property); |
| 2453 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2454 | } |
| 2455 | |
| 2456 | if (ffa_version >= MAKE_FFA_VERSION(1, 1)) { |
| 2457 | if ((input_property & |
| 2458 | FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) { |
| 2459 | dlog_verbose("NS bit support must be 1.\n"); |
| 2460 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | return api_ffa_feature_success( |
| 2465 | FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | |
| 2466 | (input_property & |
| 2467 | FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) | |
| 2468 | FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT); |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2469 | |
| 2470 | #if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) |
| 2471 | /* Check support of a feature provided respective feature ID. */ |
| 2472 | case FFA_FEATURE_NPI: |
| 2473 | return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID); |
| 2474 | case FFA_FEATURE_SRI: |
| 2475 | return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID); |
| 2476 | #endif |
| 2477 | /* Platform specific feature support. */ |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2478 | default: |
J-Alves | 6f72ca8 | 2021-11-01 12:34:58 +0000 | [diff] [blame] | 2479 | return arch_ffa_features(feature_function_id); |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 2480 | } |
| 2481 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2482 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2483 | /** |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2484 | * FF-A specification states that x2/w2 Must Be Zero for direct messaging |
| 2485 | * interfaces. |
| 2486 | */ |
| 2487 | static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args) |
| 2488 | { |
| 2489 | return args.arg2 == 0U; |
| 2490 | } |
| 2491 | |
| 2492 | /** |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2493 | * Limits size of arguments in ffa_value structure to 32-bit. |
| 2494 | */ |
| 2495 | static struct ffa_value api_ffa_value_copy32(struct ffa_value args) |
| 2496 | { |
| 2497 | return (struct ffa_value){ |
| 2498 | .func = (uint32_t)args.func, |
| 2499 | .arg1 = (uint32_t)args.arg1, |
| 2500 | .arg2 = (uint32_t)0, |
| 2501 | .arg3 = (uint32_t)args.arg3, |
| 2502 | .arg4 = (uint32_t)args.arg4, |
| 2503 | .arg5 = (uint32_t)args.arg5, |
| 2504 | .arg6 = (uint32_t)args.arg6, |
| 2505 | .arg7 = (uint32_t)args.arg7, |
| 2506 | }; |
| 2507 | } |
| 2508 | |
| 2509 | /** |
| 2510 | * Helper to copy direct message payload, depending on SMC used and expected |
| 2511 | * registers size. |
| 2512 | */ |
| 2513 | static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args) |
| 2514 | { |
| 2515 | if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 || |
| 2516 | args.func == FFA_MSG_SEND_DIRECT_RESP_32) { |
| 2517 | return api_ffa_value_copy32(args); |
| 2518 | } |
| 2519 | |
| 2520 | return (struct ffa_value){ |
| 2521 | .func = args.func, |
| 2522 | .arg1 = args.arg1, |
| 2523 | .arg2 = 0, |
| 2524 | .arg3 = args.arg3, |
| 2525 | .arg4 = args.arg4, |
| 2526 | .arg5 = args.arg5, |
| 2527 | .arg6 = args.arg6, |
| 2528 | .arg7 = args.arg7, |
| 2529 | }; |
| 2530 | } |
| 2531 | |
| 2532 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2533 | * Send an FF-A direct message request. |
| 2534 | */ |
| 2535 | struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id, |
| 2536 | ffa_vm_id_t receiver_vm_id, |
| 2537 | struct ffa_value args, |
| 2538 | struct vcpu *current, |
| 2539 | struct vcpu **next) |
| 2540 | { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 2541 | struct ffa_value ret; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2542 | struct vm *receiver_vm; |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2543 | struct vm_locked receiver_locked; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2544 | struct vcpu *receiver_vcpu; |
| 2545 | struct two_vcpu_locked vcpus_locked; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2546 | enum vcpu_state next_state = VCPU_STATE_BLOCKED; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2547 | |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2548 | if (!api_ffa_dir_msg_is_arg2_zero(args)) { |
| 2549 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2550 | } |
| 2551 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2552 | if (!plat_ffa_is_direct_request_valid(current, sender_vm_id, |
| 2553 | receiver_vm_id)) { |
J-Alves | e0caac3 | 2022-12-19 14:37:08 +0000 | [diff] [blame] | 2554 | dlog_verbose("Invalid direct message request.\n"); |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 2555 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2558 | if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 2559 | return ret; |
| 2560 | } |
| 2561 | |
| 2562 | ret = (struct ffa_value){.func = FFA_INTERRUPT_32}; |
| 2563 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2564 | receiver_vm = vm_find(receiver_vm_id); |
| 2565 | if (receiver_vm == NULL) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2566 | dlog_verbose("Invalid Receiver!\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2567 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2568 | } |
| 2569 | |
| 2570 | /* |
J-Alves | 439ac97 | 2021-11-18 17:32:03 +0000 | [diff] [blame] | 2571 | * Check if sender supports sending direct message req, and if |
| 2572 | * receiver supports receipt of direct message requests. |
| 2573 | */ |
| 2574 | if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) { |
| 2575 | return ffa_error(FFA_DENIED); |
| 2576 | } |
| 2577 | |
| 2578 | /* |
Olivier Deprez | c13a869 | 2022-04-08 17:47:14 +0200 | [diff] [blame] | 2579 | * 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] | 2580 | * UP (migratable) or MP partitions with a number of vCPUs matching the |
| 2581 | * number of PEs in the system. It further states that MP partitions |
| 2582 | * accepting direct request messages cannot migrate. |
| 2583 | */ |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 2584 | receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2585 | if (receiver_vcpu == NULL) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2586 | dlog_verbose("Invalid vCPU!\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2587 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2588 | } |
| 2589 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2590 | if (!plat_ffa_check_runtime_state_transition( |
| 2591 | current, sender_vm_id, HF_INVALID_VM_ID, receiver_vcpu, |
| 2592 | args.func, &next_state)) { |
| 2593 | return ffa_error(FFA_DENIED); |
| 2594 | } |
| 2595 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2596 | receiver_locked = vm_lock(receiver_vm); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2597 | vcpus_locked = vcpu_lock_both(receiver_vcpu, current); |
| 2598 | |
| 2599 | /* |
| 2600 | * If destination vCPU is executing or already received an |
| 2601 | * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is |
| 2602 | * busy. There is a brief period of time where the vCPU state has |
| 2603 | * changed but regs_available is still false thus consider this case as |
| 2604 | * the vCPU not yet ready to receive a direct message request. |
| 2605 | */ |
| 2606 | if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) || |
| 2607 | receiver_vcpu->state == VCPU_STATE_RUNNING || |
| 2608 | !receiver_vcpu->regs_available) { |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2609 | dlog_verbose("Receiver is busy with another request.\n"); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2610 | ret = ffa_error(FFA_BUSY); |
| 2611 | goto out; |
| 2612 | } |
| 2613 | |
| 2614 | if (atomic_load_explicit(&receiver_vcpu->vm->aborting, |
| 2615 | memory_order_relaxed)) { |
| 2616 | if (receiver_vcpu->state != VCPU_STATE_ABORTED) { |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 2617 | dlog_notice("Aborting VM %#x vCPU %u\n", |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2618 | receiver_vcpu->vm->id, |
| 2619 | vcpu_index(receiver_vcpu)); |
| 2620 | receiver_vcpu->state = VCPU_STATE_ABORTED; |
| 2621 | } |
| 2622 | |
| 2623 | ret = ffa_error(FFA_ABORTED); |
| 2624 | goto out; |
| 2625 | } |
| 2626 | |
| 2627 | switch (receiver_vcpu->state) { |
| 2628 | case VCPU_STATE_OFF: |
| 2629 | case VCPU_STATE_RUNNING: |
| 2630 | case VCPU_STATE_ABORTED: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2631 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2632 | case VCPU_STATE_BLOCKED: |
| 2633 | case VCPU_STATE_PREEMPTED: |
J-Alves | 88a1354 | 2021-12-14 15:39:52 +0000 | [diff] [blame] | 2634 | dlog_verbose("Receiver's vCPU can't receive request (%u)!\n", |
| 2635 | vcpu_index(receiver_vcpu)); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2636 | ret = ffa_error(FFA_BUSY); |
| 2637 | goto out; |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2638 | case VCPU_STATE_WAITING: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2639 | /* |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2640 | * We expect target vCPU to be in WAITING state after either |
| 2641 | * having called ffa_msg_wait or sent a direct message response. |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2642 | */ |
| 2643 | break; |
| 2644 | } |
| 2645 | |
| 2646 | /* Inject timer interrupt if any pending */ |
| 2647 | if (arch_timer_pending(&receiver_vcpu->regs)) { |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2648 | api_interrupt_inject_locked(vcpus_locked.vcpu1, |
| 2649 | HF_VIRTUAL_TIMER_INTID, current, |
| 2650 | NULL); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2651 | |
| 2652 | arch_timer_mask(&receiver_vcpu->regs); |
| 2653 | } |
| 2654 | |
| 2655 | /* The receiver vCPU runs upon direct message invocation */ |
| 2656 | receiver_vcpu->cpu = current->cpu; |
| 2657 | receiver_vcpu->state = VCPU_STATE_RUNNING; |
| 2658 | receiver_vcpu->regs_available = false; |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2659 | receiver_vcpu->direct_request_origin_vm_id = sender_vm_id; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2660 | |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2661 | 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] | 2662 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2663 | assert(next_state == VCPU_STATE_BLOCKED); |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 2664 | current->state = VCPU_STATE_BLOCKED; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2665 | |
Madhukar Pappireddy | 49fe670 | 2022-06-21 17:52:23 -0500 | [diff] [blame] | 2666 | plat_ffa_wind_call_chain_ffa_direct_req(vcpus_locked.vcpu2, |
| 2667 | vcpus_locked.vcpu1); |
| 2668 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2669 | /* Switch to receiver vCPU targeted to by direct msg request */ |
| 2670 | *next = receiver_vcpu; |
| 2671 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2672 | if (!receiver_locked.vm->el0_partition) { |
| 2673 | /* |
| 2674 | * If the scheduler in the system is giving CPU cycles to the |
| 2675 | * receiver, due to pending notifications, inject the NPI |
| 2676 | * interrupt. Following call assumes that '*next' has been set |
| 2677 | * to receiver_vcpu. |
| 2678 | */ |
| 2679 | plat_ffa_inject_notification_pending_interrupt( |
| 2680 | vcpus_locked.vcpu1.vcpu == receiver_vcpu |
| 2681 | ? vcpus_locked.vcpu1 |
| 2682 | : vcpus_locked.vcpu2, |
| 2683 | current, receiver_locked); |
| 2684 | } |
| 2685 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2686 | /* |
| 2687 | * Since this flow will lead to a VM switch, the return value will not |
| 2688 | * be applied to current vCPU. |
| 2689 | */ |
| 2690 | |
| 2691 | out: |
| 2692 | sl_unlock(&receiver_vcpu->lock); |
| 2693 | sl_unlock(¤t->lock); |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 2694 | vm_unlock(&receiver_locked); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2695 | |
| 2696 | return ret; |
| 2697 | } |
| 2698 | |
| 2699 | /** |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 2700 | * Resume the target vCPU after the current vCPU sent a direct response. |
| 2701 | * Current vCPU moves to waiting state. |
| 2702 | */ |
| 2703 | void api_ffa_resume_direct_resp_target(struct vcpu *current, struct vcpu **next, |
| 2704 | ffa_vm_id_t receiver_vm_id, |
| 2705 | struct ffa_value to_ret, |
| 2706 | bool is_nwd_call_chain) |
| 2707 | { |
| 2708 | if (!vm_id_is_current_world(receiver_vm_id)) { |
| 2709 | *next = api_switch_to_other_world(current, to_ret, |
| 2710 | VCPU_STATE_WAITING); |
| 2711 | |
| 2712 | /* End of NWd scheduled call chain. */ |
| 2713 | assert(!is_nwd_call_chain || |
| 2714 | (current->call_chain.prev_node == NULL)); |
| 2715 | } else if (receiver_vm_id == HF_PRIMARY_VM_ID) { |
| 2716 | *next = api_switch_to_primary(current, to_ret, |
| 2717 | VCPU_STATE_WAITING); |
| 2718 | |
| 2719 | /* Removing a node from NWd scheduled call chain. */ |
| 2720 | if (is_nwd_call_chain) { |
| 2721 | vcpu_call_chain_remove_node(current, *next); |
| 2722 | } |
| 2723 | } else if (vm_id_is_current_world(receiver_vm_id)) { |
| 2724 | /* |
| 2725 | * It is expected the receiver_vm_id to be from an SP, otherwise |
| 2726 | * 'plat_ffa_is_direct_response_valid' should have |
| 2727 | * made function return error before getting to this point. |
| 2728 | */ |
| 2729 | *next = api_switch_to_vm(current, to_ret, VCPU_STATE_WAITING, |
| 2730 | receiver_vm_id); |
| 2731 | } else { |
| 2732 | panic("Invalid direct message response invocation"); |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2737 | * Send an FF-A direct message response. |
| 2738 | */ |
| 2739 | struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id, |
| 2740 | ffa_vm_id_t receiver_vm_id, |
| 2741 | struct ffa_value args, |
| 2742 | struct vcpu *current, |
| 2743 | struct vcpu **next) |
| 2744 | { |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2745 | struct vcpu_locked current_locked; |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2746 | enum vcpu_state next_state = VCPU_STATE_WAITING; |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 2747 | struct ffa_value signal_interrupt = |
| 2748 | (struct ffa_value){.func = FFA_INTERRUPT_32}; |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2749 | |
| 2750 | if (!api_ffa_dir_msg_is_arg2_zero(args)) { |
| 2751 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2752 | } |
| 2753 | |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2754 | struct ffa_value to_ret = api_ffa_dir_msg_value(args); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2755 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2756 | if (!plat_ffa_is_direct_response_valid(current, sender_vm_id, |
| 2757 | receiver_vm_id)) { |
J-Alves | e0caac3 | 2022-12-19 14:37:08 +0000 | [diff] [blame] | 2758 | dlog_verbose("Invalid direct response call.\n"); |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 2759 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2762 | if (!plat_ffa_check_runtime_state_transition(current, sender_vm_id, |
| 2763 | receiver_vm_id, NULL, |
| 2764 | args.func, &next_state)) { |
| 2765 | return ffa_error(FFA_DENIED); |
| 2766 | } |
| 2767 | |
Madhukar Pappireddy | 5fd3248 | 2022-01-07 14:53:26 -0600 | [diff] [blame] | 2768 | if (plat_ffa_is_direct_response_interrupted(current)) { |
| 2769 | return ffa_error(FFA_INTERRUPTED); |
| 2770 | } |
| 2771 | |
Madhukar Pappireddy | c857ac2 | 2022-06-21 17:37:53 -0500 | [diff] [blame] | 2772 | assert(next_state == VCPU_STATE_WAITING); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2773 | current_locked = vcpu_lock(current); |
Madhukar Pappireddy | 469fa71 | 2022-06-21 18:53:33 -0500 | [diff] [blame] | 2774 | |
| 2775 | /* |
| 2776 | * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a |
| 2777 | * defined originator. |
| 2778 | */ |
| 2779 | if (!is_ffa_direct_msg_request_ongoing(current_locked)) { |
| 2780 | /* |
| 2781 | * Sending direct response but direct request origin |
| 2782 | * vCPU is not set. |
| 2783 | */ |
| 2784 | vcpu_unlock(¤t_locked); |
| 2785 | return ffa_error(FFA_DENIED); |
| 2786 | } |
| 2787 | |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2788 | if (api_ffa_is_managed_exit_ongoing(current_locked)) { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2789 | /* |
Madhukar Pappireddy | 469fa71 | 2022-06-21 18:53:33 -0500 | [diff] [blame] | 2790 | * 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] | 2791 | * a secure interrupt to a SP that is performing managed exit. |
| 2792 | * We have taken a implementation defined choice to not allow |
| 2793 | * Managed exit while a SP is processing a secure interrupt. |
| 2794 | */ |
| 2795 | CHECK(!current->processing_secure_interrupt); |
| 2796 | |
Madhukar Pappireddy | dd6fdfb | 2021-12-14 12:30:36 -0600 | [diff] [blame] | 2797 | plat_interrupts_set_priority_mask(current->priority_mask); |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 2798 | /* |
| 2799 | * A SP may be signaled a managed exit but actually not trap |
| 2800 | * the virtual interrupt, probably because it has virtual |
| 2801 | * interrupts masked, and emit direct resp. In this case the |
| 2802 | * managed exit operation is considered completed and it would |
| 2803 | * also need to clear the pending managed exit flag for the SP |
| 2804 | * vCPU. |
| 2805 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2806 | current->processing_managed_exit = false; |
Madhukar Pappireddy | a101911 | 2022-06-21 18:57:44 -0500 | [diff] [blame] | 2807 | struct interrupts *interrupts = ¤t->interrupts; |
| 2808 | |
| 2809 | if (vcpu_is_virt_interrupt_pending(interrupts, |
| 2810 | HF_MANAGED_EXIT_INTID)) { |
| 2811 | api_interrupt_clear_decrement(current_locked, |
| 2812 | interrupts, |
| 2813 | HF_MANAGED_EXIT_INTID); |
| 2814 | } |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 2817 | if (plat_ffa_intercept_direct_response(current_locked, next, to_ret, |
| 2818 | &signal_interrupt)) { |
| 2819 | vcpu_unlock(¤t_locked); |
| 2820 | return signal_interrupt; |
| 2821 | } |
| 2822 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2823 | /* Clear direct request origin for the caller. */ |
| 2824 | current->direct_request_origin_vm_id = HF_INVALID_VM_ID; |
| 2825 | |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2826 | vcpu_unlock(¤t_locked); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2827 | |
Madhukar Pappireddy | 2f76e49 | 2022-09-06 15:21:59 -0500 | [diff] [blame] | 2828 | api_ffa_resume_direct_resp_target(current, next, receiver_vm_id, to_ret, |
| 2829 | false); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2830 | |
Madhukar Pappireddy | c0fb87e | 2022-06-21 17:59:15 -0500 | [diff] [blame] | 2831 | plat_ffa_unwind_call_chain_ffa_direct_resp(current, *next); |
| 2832 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2833 | return (struct ffa_value){.func = FFA_INTERRUPT_32}; |
| 2834 | } |
| 2835 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 2836 | static bool api_memory_region_check_flags( |
| 2837 | struct ffa_memory_region *memory_region, uint32_t share_func) |
| 2838 | { |
| 2839 | switch (share_func) { |
| 2840 | case FFA_MEM_SHARE_32: |
| 2841 | if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != |
| 2842 | 0U) { |
| 2843 | return false; |
| 2844 | } |
| 2845 | /* Intentional fall-through */ |
| 2846 | case FFA_MEM_LEND_32: |
| 2847 | case FFA_MEM_DONATE_32: { |
| 2848 | /* Bits 31:2 Must Be Zero. */ |
| 2849 | ffa_memory_receiver_flags_t to_mask = |
| 2850 | ~(FFA_MEMORY_REGION_FLAG_CLEAR | |
| 2851 | FFA_MEMORY_REGION_FLAG_TIME_SLICE); |
| 2852 | |
| 2853 | if ((memory_region->flags & to_mask) != 0U) { |
| 2854 | return false; |
| 2855 | } |
| 2856 | break; |
| 2857 | } |
| 2858 | default: |
| 2859 | panic("Check for mem send calls only.\n"); |
| 2860 | } |
| 2861 | |
| 2862 | /* Last check reserved values are 0 */ |
| 2863 | return true; |
| 2864 | } |
| 2865 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2866 | struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length, |
| 2867 | uint32_t fragment_length, ipaddr_t address, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2868 | uint32_t page_count, struct vcpu *current) |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2869 | { |
| 2870 | struct vm *from = current->vm; |
| 2871 | struct vm *to; |
| 2872 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2873 | struct ffa_memory_region *memory_region; |
| 2874 | struct ffa_value ret; |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 2875 | bool targets_other_world = false; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2876 | |
| 2877 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 2878 | /* |
| 2879 | * Hafnium only supports passing the descriptor in the TX |
| 2880 | * mailbox. |
| 2881 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2882 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2885 | if (fragment_length > length) { |
| 2886 | dlog_verbose( |
| 2887 | "Fragment length %d greater than total length %d.\n", |
| 2888 | fragment_length, length); |
| 2889 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2890 | } |
| 2891 | if (fragment_length < sizeof(struct ffa_memory_region) + |
| 2892 | sizeof(struct ffa_memory_access)) { |
| 2893 | dlog_verbose( |
| 2894 | "Initial fragment length %d smaller than header size " |
| 2895 | "%d.\n", |
| 2896 | fragment_length, |
| 2897 | sizeof(struct ffa_memory_region) + |
| 2898 | sizeof(struct ffa_memory_access)); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2899 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Check that the sender has configured its send buffer. If the TX |
| 2904 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 2905 | * be safely accessed after releasing the lock since the TX mailbox |
| 2906 | * address can only be configured once. |
| 2907 | */ |
| 2908 | sl_lock(&from->lock); |
| 2909 | from_msg = from->mailbox.send; |
| 2910 | sl_unlock(&from->lock); |
| 2911 | |
| 2912 | if (from_msg == NULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2913 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2917 | * Copy the memory region descriptor to a fresh page from the memory |
| 2918 | * pool. This prevents the sender from changing it underneath us, and |
| 2919 | * 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] | 2920 | */ |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2921 | if (fragment_length > HF_MAILBOX_SIZE || |
| 2922 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2923 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2924 | } |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2925 | memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2926 | if (memory_region == NULL) { |
| 2927 | dlog_verbose("Failed to allocate memory region copy.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2928 | return ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2929 | } |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2930 | memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2931 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 2932 | if (!api_memory_region_check_flags(memory_region, share_func)) { |
| 2933 | dlog_verbose( |
| 2934 | "Memory region reserved arguments must be zero.\n"); |
| 2935 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2936 | goto out; |
| 2937 | } |
| 2938 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 2939 | if (memory_region->receiver_count == 0U) { |
| 2940 | dlog_verbose("Receiver count can't be 0.\n"); |
| 2941 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2942 | goto out; |
| 2943 | } |
| 2944 | |
| 2945 | if (share_func == FFA_MEM_DONATE_32 && |
| 2946 | memory_region->receiver_count != 1U) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2947 | dlog_verbose( |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 2948 | "FFA_MEM_DONATE only supports one recipient. " |
| 2949 | "Specified %u\n", |
| 2950 | memory_region->receiver_count); |
| 2951 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2952 | goto out; |
| 2953 | } |
| 2954 | |
| 2955 | if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) { |
| 2956 | dlog_verbose( |
| 2957 | "Max number of recipients supported is %u " |
| 2958 | "specified %u\n", |
| 2959 | MAX_MEM_SHARE_RECIPIENTS, |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2960 | memory_region->receiver_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2961 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2962 | goto out; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * 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] | 2967 | * If there is a receiver from the other world, track it for later |
| 2968 | * forwarding if needed. |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2969 | */ |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 2970 | for (uint32_t i = 0U; i < memory_region->receiver_count; i++) { |
| 2971 | ffa_vm_id_t receiver_id = |
| 2972 | memory_region->receivers[i] |
| 2973 | .receiver_permissions.receiver; |
| 2974 | to = vm_find(receiver_id); |
| 2975 | |
| 2976 | if (vm_id_is_current_world(receiver_id) && |
| 2977 | (to == NULL || to == from)) { |
| 2978 | dlog_verbose("%s: invalid receiver.\n", __func__); |
| 2979 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2980 | goto out; |
| 2981 | } |
| 2982 | |
| 2983 | if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) { |
| 2984 | ret = ffa_error(FFA_DENIED); |
| 2985 | goto out; |
| 2986 | } |
| 2987 | |
| 2988 | /* Capture if any of the receivers is from the other world. */ |
| 2989 | if (!targets_other_world) { |
| 2990 | targets_other_world = |
| 2991 | !vm_id_is_current_world(receiver_id); |
| 2992 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 2995 | if (targets_other_world) { |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 2996 | ret = plat_ffa_other_world_mem_send( |
| 2997 | from, share_func, &memory_region, length, |
| 2998 | fragment_length, &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2999 | } else { |
| 3000 | struct vm_locked from_locked = vm_lock(from); |
| 3001 | |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3002 | ret = ffa_memory_send(from_locked, memory_region, length, |
| 3003 | fragment_length, share_func, |
| 3004 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3005 | /* |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3006 | * ffa_memory_send takes ownership of the memory_region, so |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3007 | * make sure we don't free it. |
| 3008 | */ |
| 3009 | memory_region = NULL; |
| 3010 | |
| 3011 | vm_unlock(&from_locked); |
| 3012 | } |
| 3013 | |
| 3014 | out: |
| 3015 | if (memory_region != NULL) { |
| 3016 | mpool_free(&api_page_pool, memory_region); |
| 3017 | } |
| 3018 | |
| 3019 | return ret; |
| 3020 | } |
| 3021 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3022 | struct ffa_value api_ffa_mem_retrieve_req(uint32_t length, |
| 3023 | uint32_t fragment_length, |
| 3024 | ipaddr_t address, uint32_t page_count, |
| 3025 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3026 | { |
| 3027 | struct vm *to = current->vm; |
| 3028 | struct vm_locked to_locked; |
| 3029 | const void *to_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3030 | struct ffa_memory_region *retrieve_request; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3031 | uint32_t message_buffer_size; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3032 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3033 | |
| 3034 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 3035 | /* |
| 3036 | * Hafnium only supports passing the descriptor in the TX |
| 3037 | * mailbox. |
| 3038 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3039 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 3042 | if (fragment_length != length) { |
| 3043 | dlog_verbose("Fragmentation not yet supported.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3044 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3045 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3046 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3047 | retrieve_request = |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3048 | (struct ffa_memory_region *)cpu_get_buffer(current->cpu); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3049 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 3050 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 3051 | dlog_verbose("Retrieve request too long.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3052 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | to_locked = vm_lock(to); |
| 3056 | to_msg = to->mailbox.send; |
| 3057 | |
| 3058 | if (to_msg == NULL) { |
| 3059 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3060 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3061 | goto out; |
| 3062 | } |
| 3063 | |
| 3064 | /* |
| 3065 | * Copy the retrieve request descriptor to an internal buffer, so that |
| 3066 | * the caller can't change it underneath us. |
| 3067 | */ |
| 3068 | memcpy_s(retrieve_request, message_buffer_size, to_msg, length); |
| 3069 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 3070 | if ((vm_is_mailbox_other_world_owned(to_locked) && |
| 3071 | !plat_ffa_acquire_receiver_rx(to_locked, &ret)) || |
| 3072 | vm_is_mailbox_busy(to_locked)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3073 | /* |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 3074 | * Can't retrieve memory information if the mailbox is |
| 3075 | * not available. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3076 | */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3077 | dlog_verbose("%s: RX buffer not ready.\n", __func__); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3078 | ret = ffa_error(FFA_BUSY); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3079 | goto out; |
| 3080 | } |
| 3081 | |
J-Alves | b5084cf | 2022-07-06 14:20:12 +0100 | [diff] [blame] | 3082 | if (plat_ffa_memory_handle_allocated_by_current_world( |
| 3083 | retrieve_request->handle)) { |
| 3084 | ret = ffa_memory_retrieve(to_locked, retrieve_request, length, |
| 3085 | &api_page_pool); |
| 3086 | } else { |
| 3087 | ret = plat_ffa_other_world_mem_retrieve( |
| 3088 | to_locked, retrieve_request, length, &api_page_pool); |
| 3089 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3090 | out: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3091 | vm_unlock(&to_locked); |
| 3092 | return ret; |
| 3093 | } |
| 3094 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3095 | struct ffa_value api_ffa_mem_relinquish(struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3096 | { |
| 3097 | struct vm *from = current->vm; |
| 3098 | struct vm_locked from_locked; |
| 3099 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3100 | struct ffa_mem_relinquish *relinquish_request; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3101 | uint32_t message_buffer_size; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3102 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3103 | uint32_t length; |
| 3104 | |
| 3105 | from_locked = vm_lock(from); |
| 3106 | from_msg = from->mailbox.send; |
| 3107 | |
| 3108 | if (from_msg == NULL) { |
| 3109 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3110 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3111 | goto out; |
| 3112 | } |
| 3113 | |
| 3114 | /* |
| 3115 | * Calculate length from relinquish descriptor before copying. We will |
| 3116 | * check again later to make sure it hasn't changed. |
| 3117 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3118 | length = sizeof(struct ffa_mem_relinquish) + |
| 3119 | ((struct ffa_mem_relinquish *)from_msg)->endpoint_count * |
| 3120 | sizeof(ffa_vm_id_t); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3121 | /* |
| 3122 | * Copy the relinquish descriptor to an internal buffer, so that the |
| 3123 | * caller can't change it underneath us. |
| 3124 | */ |
| 3125 | relinquish_request = |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3126 | (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3127 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 3128 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 3129 | dlog_verbose("Relinquish message too long.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3130 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3131 | goto out; |
| 3132 | } |
| 3133 | memcpy_s(relinquish_request, message_buffer_size, from_msg, length); |
| 3134 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3135 | if (sizeof(struct ffa_mem_relinquish) + |
| 3136 | relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) != |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3137 | length) { |
| 3138 | dlog_verbose( |
| 3139 | "Endpoint count changed while copying to internal " |
| 3140 | "buffer.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3141 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3142 | goto out; |
| 3143 | } |
| 3144 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3145 | ret = ffa_memory_relinquish(from_locked, relinquish_request, |
| 3146 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3147 | |
| 3148 | out: |
| 3149 | vm_unlock(&from_locked); |
| 3150 | return ret; |
| 3151 | } |
| 3152 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3153 | struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle, |
| 3154 | ffa_memory_region_flags_t flags, |
| 3155 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3156 | { |
| 3157 | struct vm *to = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3158 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3159 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 3160 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3161 | struct vm_locked to_locked = vm_lock(to); |
| 3162 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3163 | ret = ffa_memory_reclaim(to_locked, handle, flags, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3164 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3165 | |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3166 | vm_unlock(&to_locked); |
| 3167 | } else { |
J-Alves | fc19b37 | 2022-07-06 12:17:35 +0100 | [diff] [blame] | 3168 | ret = plat_ffa_other_world_mem_reclaim(to, handle, flags, |
| 3169 | &api_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | return ret; |
| 3173 | } |
| 3174 | |
| 3175 | struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle, |
| 3176 | uint32_t fragment_offset, |
| 3177 | ffa_vm_id_t sender_vm_id, |
| 3178 | struct vcpu *current) |
| 3179 | { |
| 3180 | struct vm *to = current->vm; |
| 3181 | struct vm_locked to_locked; |
| 3182 | struct ffa_value ret; |
| 3183 | |
| 3184 | /* Sender ID MBZ at virtual instance. */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3185 | if (vm_id_is_current_world(to->id)) { |
| 3186 | if (sender_vm_id != 0) { |
| 3187 | dlog_verbose("%s: Invalid sender.\n", __func__); |
| 3188 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3189 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | to_locked = vm_lock(to); |
| 3193 | |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 3194 | if (vm_is_mailbox_busy(to_locked)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3195 | /* |
| 3196 | * Can't retrieve memory information if the mailbox is not |
| 3197 | * available. |
| 3198 | */ |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3199 | dlog_verbose("%s: RX buffer not ready partition %x.\n", |
| 3200 | __func__, to_locked.vm->id); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3201 | ret = ffa_error(FFA_BUSY); |
| 3202 | goto out; |
| 3203 | } |
| 3204 | |
| 3205 | ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset, |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3206 | sender_vm_id, &api_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3207 | out: |
| 3208 | vm_unlock(&to_locked); |
| 3209 | return ret; |
| 3210 | } |
| 3211 | |
| 3212 | struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle, |
| 3213 | uint32_t fragment_length, |
| 3214 | ffa_vm_id_t sender_vm_id, |
| 3215 | struct vcpu *current) |
| 3216 | { |
| 3217 | struct vm *from = current->vm; |
| 3218 | const void *from_msg; |
| 3219 | void *fragment_copy; |
| 3220 | struct ffa_value ret; |
| 3221 | |
| 3222 | /* Sender ID MBZ at virtual instance. */ |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 3223 | if (vm_id_is_current_world(from->id) && sender_vm_id != 0) { |
| 3224 | dlog_verbose("Invalid sender."); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3225 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * Check that the sender has configured its send buffer. If the TX |
| 3230 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 3231 | * be safely accessed after releasing the lock since the TX mailbox |
| 3232 | * address can only be configured once. |
| 3233 | */ |
| 3234 | sl_lock(&from->lock); |
| 3235 | from_msg = from->mailbox.send; |
| 3236 | sl_unlock(&from->lock); |
| 3237 | |
| 3238 | if (from_msg == NULL) { |
J-Alves | 59ed004 | 2022-07-28 18:26:41 +0100 | [diff] [blame] | 3239 | dlog_verbose("Mailbox from %x is not set.\n", from->id); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3240 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * Copy the fragment to a fresh page from the memory pool. This prevents |
| 3245 | * the sender from changing it underneath us, and also lets us keep it |
| 3246 | * around in the share state table if needed. |
| 3247 | */ |
| 3248 | if (fragment_length > HF_MAILBOX_SIZE || |
| 3249 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
| 3250 | dlog_verbose( |
| 3251 | "Fragment length %d larger than mailbox size %d.\n", |
| 3252 | fragment_length, HF_MAILBOX_SIZE); |
| 3253 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3254 | } |
| 3255 | if (fragment_length < sizeof(struct ffa_memory_region_constituent) || |
| 3256 | fragment_length % sizeof(struct ffa_memory_region_constituent) != |
| 3257 | 0) { |
| 3258 | dlog_verbose("Invalid fragment length %d.\n", fragment_length); |
| 3259 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3260 | } |
| 3261 | fragment_copy = mpool_alloc(&api_page_pool); |
| 3262 | if (fragment_copy == NULL) { |
| 3263 | dlog_verbose("Failed to allocate fragment copy.\n"); |
| 3264 | return ffa_error(FFA_NO_MEMORY); |
| 3265 | } |
| 3266 | memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length); |
| 3267 | |
| 3268 | /* |
| 3269 | * Hafnium doesn't support fragmentation of memory retrieve requests |
| 3270 | * (because it doesn't support caller-specified mappings, so a request |
| 3271 | * will never be larger than a single page), so this must be part of a |
| 3272 | * memory send (i.e. donate, lend or share) request. |
| 3273 | * |
| 3274 | * 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] | 3275 | * other world or not. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3276 | */ |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 3277 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3278 | struct vm_locked from_locked = vm_lock(from); |
| 3279 | |
| 3280 | ret = ffa_memory_send_continue(from_locked, fragment_copy, |
| 3281 | fragment_length, handle, |
| 3282 | &api_page_pool); |
| 3283 | /* |
| 3284 | * `ffa_memory_send_continue` takes ownership of the |
| 3285 | * fragment_copy, so we don't need to free it here. |
| 3286 | */ |
| 3287 | vm_unlock(&from_locked); |
| 3288 | } else { |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 3289 | ret = plat_ffa_other_world_mem_send_continue( |
| 3290 | from, fragment_copy, fragment_length, handle, |
| 3291 | &api_page_pool); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3292 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 3293 | |
| 3294 | return ret; |
| 3295 | } |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 3296 | |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 3297 | /** |
| 3298 | * Register an entry point for a vCPU in warm boot cases. |
| 3299 | * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER. |
| 3300 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 3301 | struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point, |
| 3302 | struct vcpu *current) |
| 3303 | { |
| 3304 | struct vm_locked vm_locked; |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 3305 | struct vcpu_locked current_locked; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 3306 | |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 3307 | /* |
| 3308 | * Reject if interface is not supported at this FF-A instance |
| 3309 | * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP. |
| 3310 | */ |
| 3311 | if (!plat_ffa_is_secondary_ep_register_supported() || |
| 3312 | current->vm->vcpu_count == 1) { |
| 3313 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3314 | } |
| 3315 | |
| 3316 | /* |
| 3317 | * No further check is made on the address validity |
| 3318 | * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known |
| 3319 | * from the VM or vCPU structure. |
| 3320 | * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1: |
| 3321 | * For each SP [...] the Framework assumes that the same entry point |
| 3322 | * address is used for initializing any execution context during a |
| 3323 | * secondary cold boot. |
| 3324 | * If this function is invoked multiple times, then the entry point |
| 3325 | * address specified in the last valid invocation must be used by the |
| 3326 | * callee. |
| 3327 | */ |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 3328 | current_locked = vcpu_lock(current); |
| 3329 | if (current->rt_model != RTM_SP_INIT) { |
| 3330 | dlog_error( |
| 3331 | "FFA_SECONDARY_EP_REGISTER can only be called while " |
| 3332 | "vCPU in run-time state for initialization.\n"); |
| 3333 | vcpu_unlock(¤t_locked); |
| 3334 | return ffa_error(FFA_DENIED); |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 3335 | } |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 3336 | vcpu_unlock(¤t_locked); |
Olivier Deprez | d614d32 | 2021-06-18 15:21:00 +0200 | [diff] [blame] | 3337 | |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 3338 | vm_locked = vm_lock(current->vm); |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 3339 | vm_locked.vm->secondary_ep = entry_point; |
| 3340 | vm_unlock(&vm_locked); |
| 3341 | |
Olivier Deprez | b280833 | 2023-02-02 15:25:40 +0100 | [diff] [blame] | 3342 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 3343 | } |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 3344 | |
| 3345 | struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id, |
| 3346 | ffa_vcpu_count_t vcpu_count, |
| 3347 | struct vcpu *current) |
| 3348 | { |
| 3349 | if (!plat_ffa_is_notifications_create_valid(current, vm_id)) { |
| 3350 | dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n", |
| 3351 | vm_id); |
| 3352 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3353 | } |
| 3354 | |
| 3355 | return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count); |
| 3356 | } |
| 3357 | |
| 3358 | struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id, |
| 3359 | struct vcpu *current) |
| 3360 | { |
| 3361 | /* |
| 3362 | * Validity of use of this interface is the same as for bitmap create. |
| 3363 | */ |
| 3364 | if (!plat_ffa_is_notifications_create_valid(current, vm_id)) { |
| 3365 | dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n", |
| 3366 | vm_id); |
| 3367 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3368 | } |
| 3369 | |
| 3370 | return plat_ffa_notifications_bitmap_destroy(vm_id); |
| 3371 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 3372 | |
| 3373 | struct ffa_value api_ffa_notification_update_bindings( |
| 3374 | ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags, |
| 3375 | ffa_notifications_bitmap_t notifications, bool is_bind, |
| 3376 | struct vcpu *current) |
| 3377 | { |
| 3378 | struct ffa_value ret = {.func = FFA_SUCCESS_32}; |
| 3379 | struct vm_locked receiver_locked; |
| 3380 | const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
| 3381 | const ffa_vm_id_t id_to_update = |
| 3382 | is_bind ? sender_vm_id : HF_INVALID_VM_ID; |
| 3383 | const ffa_vm_id_t id_to_validate = |
| 3384 | is_bind ? HF_INVALID_VM_ID : sender_vm_id; |
J-Alves | 1daeaea | 2022-09-06 17:41:24 +0100 | [diff] [blame] | 3385 | const uint32_t flags_mbz = |
| 3386 | is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U; |
| 3387 | |
| 3388 | if ((flags_mbz & flags) != 0U) { |
| 3389 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3390 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 3391 | |
| 3392 | if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id, |
| 3393 | receiver_vm_id)) { |
| 3394 | dlog_verbose("Invalid use of notifications bind interface.\n"); |
| 3395 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3396 | } |
| 3397 | |
J-Alves | b15e940 | 2021-09-08 11:44:42 +0100 | [diff] [blame] | 3398 | if (plat_ffa_notifications_update_bindings_forward( |
| 3399 | receiver_vm_id, sender_vm_id, flags, notifications, is_bind, |
| 3400 | &ret)) { |
J-Alves | b15e940 | 2021-09-08 11:44:42 +0100 | [diff] [blame] | 3401 | return ret; |
| 3402 | } |
| 3403 | |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 3404 | if (notifications == 0U) { |
| 3405 | dlog_verbose("No notifications have been specified.\n"); |
| 3406 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3407 | } |
| 3408 | |
| 3409 | /** |
| 3410 | * This check assumes receiver is the current VM, and has been enforced |
| 3411 | * by 'plat_ffa_is_notifications_bind_valid'. |
| 3412 | */ |
| 3413 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 3414 | |
| 3415 | if (receiver_locked.vm == NULL) { |
| 3416 | dlog_verbose("Receiver doesn't exist!\n"); |
| 3417 | return ffa_error(FFA_DENIED); |
| 3418 | } |
| 3419 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 3420 | if (!vm_locked_are_notifications_enabled(receiver_locked)) { |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 3421 | dlog_verbose("Notifications are not enabled.\n"); |
| 3422 | ret = ffa_error(FFA_NOT_SUPPORTED); |
| 3423 | goto out; |
| 3424 | } |
| 3425 | |
| 3426 | if (is_bind && vm_id_is_current_world(sender_vm_id) && |
| 3427 | vm_find(sender_vm_id) == NULL) { |
| 3428 | dlog_verbose("Sender VM does not exist!\n"); |
| 3429 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3430 | goto out; |
| 3431 | } |
| 3432 | |
| 3433 | /* |
| 3434 | * Can't bind/unbind notifications if at least one is bound to a |
| 3435 | * different sender. |
| 3436 | */ |
| 3437 | if (!vm_notifications_validate_bound_sender( |
| 3438 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), |
| 3439 | id_to_validate, notifications)) { |
| 3440 | dlog_verbose("Notifications are bound to other sender.\n"); |
| 3441 | ret = ffa_error(FFA_DENIED); |
| 3442 | goto out; |
| 3443 | } |
| 3444 | |
| 3445 | /** |
| 3446 | * Check if there is a pending notification within those specified in |
| 3447 | * the bitmap. |
| 3448 | */ |
| 3449 | if (vm_are_notifications_pending(receiver_locked, |
| 3450 | plat_ffa_is_vm_id(sender_vm_id), |
| 3451 | notifications)) { |
| 3452 | dlog_verbose("Notifications within '%x' pending.\n", |
| 3453 | notifications); |
| 3454 | ret = ffa_error(FFA_DENIED); |
| 3455 | goto out; |
| 3456 | } |
| 3457 | |
| 3458 | vm_notifications_update_bindings( |
| 3459 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update, |
| 3460 | notifications, is_per_vcpu && is_bind); |
| 3461 | |
| 3462 | out: |
| 3463 | vm_unlock(&receiver_locked); |
| 3464 | return ret; |
| 3465 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3466 | |
| 3467 | struct ffa_value api_ffa_notification_set( |
| 3468 | ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags, |
| 3469 | ffa_notifications_bitmap_t notifications, struct vcpu *current) |
| 3470 | { |
| 3471 | struct ffa_value ret; |
| 3472 | struct vm_locked receiver_locked; |
| 3473 | |
| 3474 | /* |
| 3475 | * Check if is per-vCPU or global, and extracting vCPU ID according |
| 3476 | * to table 17.19 of the FF-A v1.1 Beta 0 spec. |
| 3477 | */ |
| 3478 | bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
| 3479 | ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16); |
| 3480 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3481 | if (!plat_ffa_is_notification_set_valid(current, sender_vm_id, |
| 3482 | receiver_vm_id)) { |
| 3483 | dlog_verbose("Invalid use of notifications set interface.\n"); |
| 3484 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3485 | } |
| 3486 | |
| 3487 | if (notifications == 0U) { |
| 3488 | dlog_verbose("No notifications have been specified.\n"); |
| 3489 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3490 | } |
| 3491 | |
J-Alves | de7bd2f | 2021-09-09 19:54:35 +0100 | [diff] [blame] | 3492 | if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id, |
| 3493 | flags, notifications, &ret)) { |
| 3494 | return ret; |
| 3495 | } |
| 3496 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3497 | /* |
| 3498 | * This check assumes receiver is the current VM, and has been enforced |
| 3499 | * by 'plat_ffa_is_notification_set_valid'. |
| 3500 | */ |
| 3501 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 3502 | |
| 3503 | if (receiver_locked.vm == NULL) { |
| 3504 | dlog_verbose("Receiver ID is not valid.\n"); |
| 3505 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3506 | } |
| 3507 | |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 3508 | if (!vm_locked_are_notifications_enabled(receiver_locked)) { |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3509 | dlog_verbose("Receiver's notifications not enabled.\n"); |
| 3510 | ret = ffa_error(FFA_DENIED); |
| 3511 | goto out; |
| 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | * If notifications are not bound to the sender, they wouldn't be |
| 3516 | * enabled either for the receiver. |
| 3517 | */ |
| 3518 | if (!vm_notifications_validate_binding( |
| 3519 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), |
| 3520 | sender_vm_id, notifications, is_per_vcpu)) { |
| 3521 | dlog_verbose("Notifications bindings not valid.\n"); |
| 3522 | ret = ffa_error(FFA_DENIED); |
| 3523 | goto out; |
| 3524 | } |
| 3525 | |
| 3526 | if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) { |
| 3527 | dlog_verbose("Invalid VCPU ID!\n"); |
| 3528 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3529 | goto out; |
| 3530 | } |
| 3531 | |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 3532 | /* Set notifications pending. */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 3533 | vm_notifications_partition_set_pending( |
| 3534 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications, |
| 3535 | vcpu_id, is_per_vcpu); |
| 3536 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3537 | dlog_verbose("Set the notifications: %x.\n", notifications); |
| 3538 | |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 3539 | if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) { |
| 3540 | dlog_verbose("SRI was NOT delayed. vcpu: %u!\n", |
| 3541 | vcpu_index(current)); |
| 3542 | plat_ffa_sri_trigger_not_delayed(current->cpu); |
| 3543 | } else { |
| 3544 | plat_ffa_sri_state_set(DELAYED); |
| 3545 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3546 | |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 3547 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3548 | out: |
| 3549 | vm_unlock(&receiver_locked); |
| 3550 | |
| 3551 | return ret; |
| 3552 | } |
| 3553 | |
| 3554 | static struct ffa_value api_ffa_notification_get_success_return( |
| 3555 | ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm, |
| 3556 | ffa_notifications_bitmap_t from_framework) |
| 3557 | { |
| 3558 | return (struct ffa_value){ |
| 3559 | .func = FFA_SUCCESS_32, |
| 3560 | .arg1 = 0U, |
| 3561 | .arg2 = (uint32_t)from_sp, |
| 3562 | .arg3 = (uint32_t)(from_sp >> 32), |
| 3563 | .arg4 = (uint32_t)from_vm, |
| 3564 | .arg5 = (uint32_t)(from_vm >> 32), |
| 3565 | .arg6 = (uint32_t)from_framework, |
| 3566 | .arg7 = (uint32_t)(from_framework >> 32), |
| 3567 | }; |
| 3568 | } |
| 3569 | |
| 3570 | struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id, |
| 3571 | ffa_vcpu_index_t vcpu_id, |
| 3572 | uint32_t flags, struct vcpu *current) |
| 3573 | { |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 3574 | ffa_notifications_bitmap_t framework_notifications = 0; |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3575 | ffa_notifications_bitmap_t sp_notifications = 0; |
| 3576 | ffa_notifications_bitmap_t vm_notifications = 0; |
| 3577 | struct vm_locked receiver_locked; |
| 3578 | struct ffa_value ret; |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 3579 | const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP | |
| 3580 | FFA_NOTIFICATION_FLAG_BITMAP_SPM | |
| 3581 | FFA_NOTIFICATION_FLAG_BITMAP_SP | |
| 3582 | FFA_NOTIFICATION_FLAG_BITMAP_VM); |
| 3583 | |
| 3584 | /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */ |
| 3585 | if ((flags & flags_mbz) != 0U) { |
| 3586 | dlog_verbose( |
| 3587 | "Invalid flags bit(s) set in notifications get. [31:4] " |
| 3588 | "MBZ(%x)\n", |
| 3589 | flags); |
| 3590 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3591 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3592 | |
| 3593 | /* |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 3594 | * Following check should capture wrong uses of the interface, |
| 3595 | * depending on whether Hafnium is SPMC or hypervisor. On the |
| 3596 | * rest of the function it is assumed this condition is met. |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3597 | */ |
J-Alves | fc95a30 | 2022-04-22 14:18:23 +0100 | [diff] [blame] | 3598 | if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id, |
| 3599 | flags)) { |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3600 | dlog_verbose("Invalid use of notifications get interface.\n"); |
| 3601 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3602 | } |
| 3603 | |
| 3604 | /* |
| 3605 | * This check assumes receiver is the current VM, and has been enforced |
| 3606 | * by `plat_ffa_is_notifications_get_valid`. |
| 3607 | */ |
| 3608 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 3609 | |
| 3610 | /* |
| 3611 | * `plat_ffa_is_notifications_get_valid` ensures following is never |
| 3612 | * true. |
| 3613 | */ |
| 3614 | CHECK(receiver_locked.vm != NULL); |
| 3615 | |
| 3616 | if (receiver_locked.vm->vcpu_count <= vcpu_id || |
| 3617 | (receiver_locked.vm->vcpu_count != 1 && |
| 3618 | cpu_index(current->cpu) != vcpu_id)) { |
J-Alves | 1abb334 | 2022-01-05 11:59:10 +0000 | [diff] [blame] | 3619 | dlog_verbose( |
| 3620 | "Invalid VCPU ID %u. vcpu count %u current core: %u!\n", |
| 3621 | vcpu_id, receiver_locked.vm->vcpu_count, |
| 3622 | cpu_index(current->cpu)); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3623 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3624 | goto out; |
| 3625 | } |
| 3626 | |
| 3627 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) { |
J-Alves | 98ff956 | 2021-09-09 14:39:41 +0100 | [diff] [blame] | 3628 | if (!plat_ffa_notifications_get_from_sp( |
| 3629 | receiver_locked, vcpu_id, &sp_notifications, |
| 3630 | &ret)) { |
| 3631 | dlog_verbose("Failed to get notifications from sps."); |
| 3632 | goto out; |
| 3633 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) { |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 3637 | vm_notifications = vm_notifications_partition_get_pending( |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3638 | receiver_locked, true, vcpu_id); |
| 3639 | } |
| 3640 | |
J-Alves | d605a09 | 2022-03-28 14:20:48 +0100 | [diff] [blame] | 3641 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U || |
| 3642 | (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) { |
| 3643 | if (!plat_ffa_notifications_get_framework_notifications( |
| 3644 | receiver_locked, &framework_notifications, flags, |
| 3645 | vcpu_id, &ret)) { |
| 3646 | dlog_verbose( |
| 3647 | "Failed to get notifications from " |
| 3648 | "framework.\n"); |
| 3649 | goto out; |
| 3650 | } |
| 3651 | } |
| 3652 | |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 3653 | ret = api_ffa_notification_get_success_return( |
| 3654 | sp_notifications, vm_notifications, framework_notifications); |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3655 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 3656 | /* |
| 3657 | * If there are no more pending notifications, change `sri_state` to |
| 3658 | * handled. |
| 3659 | */ |
| 3660 | if (vm_is_notifications_pending_count_zero()) { |
| 3661 | plat_ffa_sri_state_set(HANDLED); |
| 3662 | } |
| 3663 | |
J-Alves | 6e2abc6 | 2021-12-02 14:58:56 +0000 | [diff] [blame] | 3664 | if (!receiver_locked.vm->el0_partition && |
| 3665 | !vm_are_global_notifications_pending(receiver_locked)) { |
| 3666 | vm_notifications_set_npi_injected(receiver_locked, false); |
| 3667 | } |
| 3668 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 3669 | out: |
| 3670 | vm_unlock(&receiver_locked); |
| 3671 | |
| 3672 | return ret; |
| 3673 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3674 | |
| 3675 | /** |
| 3676 | * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by |
| 3677 | * the section 17.7.1 of the FF-A v1.1 Beta0 specification. |
| 3678 | */ |
| 3679 | static struct ffa_value api_ffa_notification_info_get_success_return( |
| 3680 | 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] | 3681 | uint32_t lists_count) |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3682 | { |
| 3683 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64}; |
| 3684 | |
| 3685 | /* |
| 3686 | * Copying content of ids into ret structure. Use 5 registers (x3-x7) to |
| 3687 | * hold the list of ids. |
| 3688 | */ |
| 3689 | memcpy_s(&ret.arg3, |
| 3690 | sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids, |
| 3691 | sizeof(ids[0]) * ids_count); |
| 3692 | |
| 3693 | /* |
| 3694 | * According to the spec x2 should have: |
| 3695 | * - Bit flagging if there are more notifications pending; |
| 3696 | * - The total number of elements (i.e. total list size); |
| 3697 | * - The number of VCPU IDs within each VM specific list. |
| 3698 | */ |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 3699 | ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler() |
| 3700 | ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING |
| 3701 | : 0; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3702 | |
| 3703 | ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK) |
| 3704 | << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT; |
| 3705 | |
| 3706 | for (unsigned int i = 0; i < lists_count; i++) { |
| 3707 | ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK) |
| 3708 | << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1); |
| 3709 | } |
| 3710 | |
| 3711 | return ret; |
| 3712 | } |
| 3713 | |
| 3714 | struct ffa_value api_ffa_notification_info_get(struct vcpu *current) |
| 3715 | { |
| 3716 | /* |
| 3717 | * Following set of variables should be populated with the return info. |
| 3718 | * At a successfull handling of this interface, they should be used |
| 3719 | * to populate the 'ret' structure in accordance to the table 17.29 |
| 3720 | * of the FF-A v1.1 Beta0 specification. |
| 3721 | */ |
| 3722 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS]; |
| 3723 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 3724 | uint32_t lists_count = 0; |
| 3725 | uint32_t ids_count = 0; |
| 3726 | bool list_is_full = false; |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 3727 | struct ffa_value result; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3728 | |
| 3729 | /* |
| 3730 | * This interface can only be called at NS virtual/physical FF-A |
| 3731 | * instance by the endpoint implementing the primary scheduler and the |
| 3732 | * Hypervisor/OS kernel. |
| 3733 | * In the SPM, following check passes if call has been forwarded from |
| 3734 | * the hypervisor. |
| 3735 | */ |
| 3736 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 3737 | dlog_verbose( |
| 3738 | "Only the receiver's scheduler can use this " |
| 3739 | "interface\n"); |
| 3740 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3741 | } |
| 3742 | |
J-Alves | ca058c2 | 2021-09-10 14:02:07 +0100 | [diff] [blame] | 3743 | /* |
| 3744 | * Forward call to the other world, and fill the arrays used to assemble |
| 3745 | * return. |
| 3746 | */ |
| 3747 | plat_ffa_notification_info_get_forward( |
| 3748 | ids, &ids_count, lists_sizes, &lists_count, |
| 3749 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 3750 | |
| 3751 | list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS; |
| 3752 | |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3753 | /* Get notifications' info from this world */ |
| 3754 | for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full; |
| 3755 | ++index) { |
| 3756 | struct vm_locked vm_locked = vm_lock(vm_find_index(index)); |
| 3757 | |
| 3758 | list_is_full = vm_notifications_info_get( |
| 3759 | vm_locked, ids, &ids_count, lists_sizes, &lists_count, |
| 3760 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 3761 | |
| 3762 | vm_unlock(&vm_locked); |
| 3763 | } |
| 3764 | |
| 3765 | if (!list_is_full) { |
| 3766 | /* Grab notifications info from other world */ |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 3767 | plat_ffa_vm_notifications_info_get( |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3768 | ids, &ids_count, lists_sizes, &lists_count, |
| 3769 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 3770 | } |
| 3771 | |
| 3772 | if (ids_count == 0) { |
J-Alves | ca058c2 | 2021-09-10 14:02:07 +0100 | [diff] [blame] | 3773 | dlog_verbose( |
| 3774 | "Notification info get has no data to retrieve.\n"); |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 3775 | result = ffa_error(FFA_NO_DATA); |
| 3776 | } else { |
| 3777 | result = api_ffa_notification_info_get_success_return( |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 3778 | ids, ids_count, lists_sizes, lists_count); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 3781 | plat_ffa_sri_state_set(HANDLED); |
| 3782 | |
J-Alves | 1339402 | 2021-06-30 13:48:49 +0100 | [diff] [blame] | 3783 | return result; |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 3784 | } |
Raghu Krishnamurthy | ea6d25f | 2021-09-14 15:27:06 -0700 | [diff] [blame] | 3785 | |
| 3786 | struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current) |
| 3787 | { |
| 3788 | struct vm_locked vm_locked; |
| 3789 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3790 | bool mode_ret = false; |
| 3791 | uint32_t mode = 0; |
| 3792 | |
| 3793 | if (!plat_ffa_is_mem_perm_get_valid(current)) { |
| 3794 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3795 | } |
| 3796 | |
| 3797 | if (!(current->vm->el0_partition)) { |
| 3798 | return ffa_error(FFA_DENIED); |
| 3799 | } |
| 3800 | |
| 3801 | vm_locked = vm_lock(current->vm); |
| 3802 | |
| 3803 | /* |
| 3804 | * mm_get_mode is used to check if the given base_addr page is already |
| 3805 | * mapped. If the page is unmapped, return error. If the page is mapped |
| 3806 | * appropriate attributes are returned to the caller. Note that |
| 3807 | * mm_get_mode returns true if the address is in the valid VA range as |
| 3808 | * supported by the architecture and MMU configurations, as opposed to |
| 3809 | * whether a page is mapped or not. For a page to be known as mapped, |
| 3810 | * the API must return true AND the returned mode must not have |
| 3811 | * MM_MODE_INVALID set. |
| 3812 | */ |
| 3813 | mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr, |
| 3814 | va_add(base_addr, PAGE_SIZE), &mode); |
| 3815 | if (!mode_ret || (mode & MM_MODE_INVALID)) { |
| 3816 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3817 | goto out; |
| 3818 | } |
| 3819 | |
| 3820 | /* No memory should be marked RWX */ |
| 3821 | CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) != |
| 3822 | (MM_MODE_R | MM_MODE_W | MM_MODE_X)); |
| 3823 | |
| 3824 | /* |
| 3825 | * S-EL0 partitions are expected to have all their pages marked as |
| 3826 | * non-global. |
| 3827 | */ |
| 3828 | CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) == |
| 3829 | (MM_MODE_NG | MM_MODE_USER)); |
| 3830 | |
| 3831 | if (mode & MM_MODE_W) { |
| 3832 | /* No memory should be writeable but not readable. */ |
| 3833 | CHECK(mode & MM_MODE_R); |
| 3834 | ret = (struct ffa_value){.func = FFA_SUCCESS_32, |
| 3835 | .arg2 = (uint32_t)(FFA_MEM_PERM_RW)}; |
| 3836 | } else if (mode & MM_MODE_R) { |
| 3837 | ret = (struct ffa_value){.func = FFA_SUCCESS_32, |
| 3838 | .arg2 = (uint32_t)(FFA_MEM_PERM_RX)}; |
| 3839 | if (!(mode & MM_MODE_X)) { |
| 3840 | ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO); |
| 3841 | } |
| 3842 | } |
| 3843 | out: |
| 3844 | vm_unlock(&vm_locked); |
| 3845 | return ret; |
| 3846 | } |
| 3847 | |
| 3848 | struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count, |
| 3849 | uint32_t mem_perm, struct vcpu *current) |
| 3850 | { |
| 3851 | struct vm_locked vm_locked; |
| 3852 | struct ffa_value ret; |
| 3853 | bool mode_ret = false; |
| 3854 | uint32_t original_mode; |
| 3855 | uint32_t new_mode; |
| 3856 | struct mpool local_page_pool; |
| 3857 | |
| 3858 | if (!plat_ffa_is_mem_perm_set_valid(current)) { |
| 3859 | return ffa_error(FFA_NOT_SUPPORTED); |
| 3860 | } |
| 3861 | |
| 3862 | if (!(current->vm->el0_partition)) { |
| 3863 | return ffa_error(FFA_DENIED); |
| 3864 | } |
| 3865 | |
| 3866 | if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) { |
| 3867 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3868 | } |
| 3869 | |
| 3870 | if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) && |
| 3871 | (mem_perm != FFA_MEM_PERM_RX)) { |
| 3872 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3873 | } |
| 3874 | |
| 3875 | /* |
| 3876 | * Create a local pool so any freed memory can't be used by another |
| 3877 | * thread. This is to ensure the original mapping can be restored if any |
| 3878 | * stage of the process fails. |
| 3879 | */ |
| 3880 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 3881 | |
| 3882 | vm_locked = vm_lock(current->vm); |
| 3883 | |
| 3884 | /* |
| 3885 | * All regions accessible by the partition are mapped during boot. If we |
| 3886 | * cannot get a successful translation for the page range, the request |
| 3887 | * to change permissions is rejected. |
| 3888 | * mm_get_mode is used to check if the given address range is already |
| 3889 | * mapped. If the range is unmapped, return error. If the range is |
| 3890 | * mapped appropriate attributes are returned to the caller. Note that |
| 3891 | * mm_get_mode returns true if the address is in the valid VA range as |
| 3892 | * supported by the architecture and MMU configurations, as opposed to |
| 3893 | * whether a page is mapped or not. For a page to be known as mapped, |
| 3894 | * the API must return true AND the returned mode must not have |
| 3895 | * MM_MODE_INVALID set. |
| 3896 | */ |
| 3897 | |
| 3898 | mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr, |
| 3899 | va_add(base_addr, page_count * PAGE_SIZE), |
| 3900 | &original_mode); |
| 3901 | if (!mode_ret || (original_mode & MM_MODE_INVALID)) { |
| 3902 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3903 | goto out; |
| 3904 | } |
| 3905 | |
| 3906 | /* Device memory cannot be marked as executable */ |
| 3907 | if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) { |
| 3908 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3909 | goto out; |
| 3910 | } |
| 3911 | |
| 3912 | new_mode = MM_MODE_USER | MM_MODE_NG; |
| 3913 | |
| 3914 | if (mem_perm == FFA_MEM_PERM_RW) { |
| 3915 | new_mode |= MM_MODE_R | MM_MODE_W; |
| 3916 | } else if (mem_perm == FFA_MEM_PERM_RX) { |
| 3917 | new_mode |= MM_MODE_R | MM_MODE_X; |
| 3918 | } else if (mem_perm == FFA_MEM_PERM_RO) { |
| 3919 | new_mode |= MM_MODE_R; |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * Safe to re-map memory, since we know the requested permissions are |
| 3924 | * valid, and the memory requested to be re-mapped is also valid. |
| 3925 | */ |
| 3926 | if (!mm_identity_prepare( |
| 3927 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 3928 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 3929 | new_mode, &local_page_pool)) { |
| 3930 | /* |
| 3931 | * Defrag the table into the local page pool. |
| 3932 | * mm_identity_prepare could have allocated or freed pages to |
| 3933 | * split blocks or tables etc. |
| 3934 | */ |
| 3935 | mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool); |
| 3936 | |
| 3937 | /* |
| 3938 | * Guaranteed to succeed mapping with old mode since the mapping |
| 3939 | * with old mode already existed and we have a local page pool |
| 3940 | * that should have sufficient memory to go back to the original |
| 3941 | * state. |
| 3942 | */ |
| 3943 | CHECK(mm_identity_prepare( |
| 3944 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 3945 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 3946 | original_mode, &local_page_pool)); |
| 3947 | mm_identity_commit( |
| 3948 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 3949 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), |
| 3950 | original_mode, &local_page_pool); |
| 3951 | |
| 3952 | mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool); |
| 3953 | ret = ffa_error(FFA_NO_MEMORY); |
| 3954 | goto out; |
| 3955 | } |
| 3956 | |
| 3957 | mm_identity_commit( |
| 3958 | &vm_locked.vm->ptable, pa_from_va(base_addr), |
| 3959 | pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode, |
| 3960 | &local_page_pool); |
| 3961 | |
| 3962 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 3963 | |
| 3964 | out: |
| 3965 | mpool_fini(&local_page_pool); |
| 3966 | vm_unlock(&vm_locked); |
| 3967 | |
| 3968 | return ret; |
| 3969 | } |
Maksims Svecovs | 71b7670 | 2022-05-20 15:32:58 +0100 | [diff] [blame] | 3970 | |
| 3971 | /** |
| 3972 | * Helper function for FFA_CONSOLE_LOG ABI. |
| 3973 | * Writes number of characters to a given VM buffer. |
| 3974 | */ |
| 3975 | static rsize_t arg_to_char_helper(struct vm_locked from_locked, |
| 3976 | const uint64_t src, rsize_t src_size, |
| 3977 | rsize_t to_write) |
| 3978 | { |
| 3979 | bool flush = false; |
| 3980 | char c; |
| 3981 | rsize_t size = src_size < to_write ? src_size : to_write; |
| 3982 | rsize_t written = 0; |
| 3983 | |
| 3984 | if (size == 0) { |
| 3985 | return 0; |
| 3986 | } |
| 3987 | |
| 3988 | while (written < size) { |
| 3989 | c = ((char *)&src)[written++]; |
| 3990 | if (c == '\n' || c == '\0') { |
| 3991 | flush = true; |
| 3992 | } else { |
| 3993 | from_locked.vm->log_buffer |
| 3994 | [from_locked.vm->log_buffer_length++] = c; |
| 3995 | flush = (from_locked.vm->log_buffer_length == |
| 3996 | LOG_BUFFER_SIZE); |
| 3997 | } |
| 3998 | |
| 3999 | if (flush) { |
| 4000 | dlog_flush_vm_buffer(from_locked.vm->id, |
| 4001 | from_locked.vm->log_buffer, |
| 4002 | from_locked.vm->log_buffer_length); |
| 4003 | from_locked.vm->log_buffer_length = 0; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | return written; |
| 4008 | } |
| 4009 | |
| 4010 | /** |
| 4011 | * Implements FFA_CONSOLE_LOG buffered logging. |
| 4012 | */ |
| 4013 | struct ffa_value api_ffa_console_log(const struct ffa_value args, |
| 4014 | struct vcpu *current) |
| 4015 | { |
| 4016 | struct vm *vm = current->vm; |
| 4017 | struct vm_locked vm_locked; |
| 4018 | size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32 |
| 4019 | ? sizeof(uint32_t) |
| 4020 | : sizeof(uint64_t); |
| 4021 | size_t total_to_write = args.arg1; |
| 4022 | |
| 4023 | if (total_to_write == 0 || total_to_write > chars_in_param * 6) { |
| 4024 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 4025 | } |
| 4026 | |
| 4027 | vm_locked = vm_lock(vm); |
| 4028 | |
| 4029 | total_to_write -= arg_to_char_helper(vm_locked, args.arg2, |
| 4030 | chars_in_param, total_to_write); |
| 4031 | total_to_write -= arg_to_char_helper(vm_locked, args.arg3, |
| 4032 | chars_in_param, total_to_write); |
| 4033 | total_to_write -= arg_to_char_helper(vm_locked, args.arg4, |
| 4034 | chars_in_param, total_to_write); |
| 4035 | total_to_write -= arg_to_char_helper(vm_locked, args.arg5, |
| 4036 | chars_in_param, total_to_write); |
| 4037 | total_to_write -= arg_to_char_helper(vm_locked, args.arg6, |
| 4038 | chars_in_param, total_to_write); |
| 4039 | arg_to_char_helper(vm_locked, args.arg7, chars_in_param, |
| 4040 | total_to_write); |
| 4041 | |
| 4042 | vm_unlock(&vm_locked); |
| 4043 | |
| 4044 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 4045 | } |