Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
J-Alves | 13318e3 | 2021-02-22 17:21:00 +0000 | [diff] [blame] | 2 | * Copyright 2021 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" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 33 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 34 | static_assert(sizeof(struct ffa_partition_info) == 8, |
| 35 | "Partition information descriptor size doesn't match the one in " |
| 36 | "the FF-A 1.0 EAC specification, Table 82."); |
| 37 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 38 | /* |
| 39 | * To eliminate the risk of deadlocks, we define a partial order for the |
| 40 | * acquisition of locks held concurrently by the same physical CPU. Our current |
| 41 | * ordering requirements are as follows: |
| 42 | * |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 43 | * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 44 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 45 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 46 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 49 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 50 | "Currently, a page is mapped for the send and receive buffers so " |
| 51 | "the maximum request is the size of a page."); |
| 52 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 53 | static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE, |
| 54 | "The page pool entry size must be at least as big as the mailbox " |
| 55 | "size, so that memory region descriptors can be copied from the " |
| 56 | "mailbox for memory sharing."); |
| 57 | |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 58 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 61 | * Initialises the API page pool by taking ownership of the contents of the |
| 62 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 63 | */ |
| 64 | void api_init(struct mpool *ppool) |
| 65 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 66 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 69 | /** |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 70 | * Get target VM vCPU: |
| 71 | * If VM is UP then return first vCPU. |
| 72 | * If VM is MP then return vCPU whose index matches current CPU index. |
| 73 | */ |
| 74 | static struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current) |
| 75 | { |
| 76 | ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu); |
| 77 | struct vcpu *vcpu = NULL; |
| 78 | |
| 79 | if (vm->vcpu_count == 1) { |
| 80 | vcpu = vm_get_vcpu(vm, 0); |
| 81 | } else if (current_cpu_index < vm->vcpu_count) { |
| 82 | vcpu = vm_get_vcpu(vm, current_cpu_index); |
| 83 | } |
| 84 | |
| 85 | return vcpu; |
| 86 | } |
| 87 | |
| 88 | /** |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 89 | * Switches the physical CPU back to the corresponding vCPU of the VM whose ID |
| 90 | * is given as argument of the function. |
| 91 | * |
| 92 | * Called to change the context between SPs for direct messaging (when Hafnium |
| 93 | * is SPMC), and on the context of the remaining 'api_switch_to_*' functions. |
| 94 | * |
| 95 | * This function works for partitions that are: |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 96 | * - UP migratable. |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 97 | * - MP with pinned Execution Contexts. |
| 98 | */ |
| 99 | static struct vcpu *api_switch_to_vm(struct vcpu *current, |
| 100 | struct ffa_value to_ret, |
| 101 | enum vcpu_state vcpu_state, |
| 102 | ffa_vm_id_t to_id) |
| 103 | { |
| 104 | struct vm *to_vm = vm_find(to_id); |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 105 | struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current); |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 106 | |
| 107 | CHECK(next != NULL); |
| 108 | |
| 109 | /* Set the return value for the target VM. */ |
| 110 | arch_regs_set_retval(&next->regs, to_ret); |
| 111 | |
| 112 | /* Set the current vCPU state. */ |
| 113 | sl_lock(¤t->lock); |
| 114 | current->state = vcpu_state; |
| 115 | sl_unlock(¤t->lock); |
| 116 | |
| 117 | return next; |
| 118 | } |
| 119 | |
| 120 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 121 | * 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] | 122 | * |
| 123 | * 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] | 124 | * 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] | 125 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 126 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 127 | struct ffa_value primary_ret, |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 128 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 129 | { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 130 | /* |
| 131 | * If the secondary is blocked but has a timer running, sleep until the |
| 132 | * timer fires rather than indefinitely. |
| 133 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 134 | switch (primary_ret.func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 135 | case HF_FFA_RUN_WAIT_FOR_INTERRUPT: |
| 136 | case FFA_MSG_WAIT_32: { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 137 | if (arch_timer_enabled_current()) { |
| 138 | uint64_t remaining_ns = |
| 139 | arch_timer_remaining_ns_current(); |
| 140 | |
| 141 | if (remaining_ns == 0) { |
| 142 | /* |
| 143 | * Timer is pending, so the current vCPU should |
| 144 | * be run again right away. |
| 145 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 146 | primary_ret.func = FFA_INTERRUPT_32; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 147 | /* |
| 148 | * primary_ret.arg1 should already be set to the |
| 149 | * current VM ID and vCPU ID. |
| 150 | */ |
| 151 | primary_ret.arg2 = 0; |
| 152 | } else { |
| 153 | primary_ret.arg2 = remaining_ns; |
| 154 | } |
| 155 | } else { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 156 | primary_ret.arg2 = FFA_SLEEP_INDEFINITE; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 157 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 158 | break; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 159 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 160 | |
| 161 | default: |
| 162 | /* Do nothing. */ |
| 163 | break; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 166 | return api_switch_to_vm(current, primary_ret, secondary_state, |
| 167 | HF_PRIMARY_VM_ID); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 171 | * Choose next vCPU to run to be the counterpart vCPU in the other |
| 172 | * world (run the normal world if currently running in the secure |
| 173 | * world). Set current vCPU state to the given vcpu_state parameter. |
| 174 | * Set FF-A return values to the target vCPU in the other world. |
| 175 | * |
| 176 | * Called in context of a direct message response from a secure |
| 177 | * partition to a VM. |
| 178 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 179 | struct vcpu *api_switch_to_other_world(struct vcpu *current, |
| 180 | struct ffa_value other_world_ret, |
| 181 | enum vcpu_state vcpu_state) |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 182 | { |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 183 | return api_switch_to_vm(current, other_world_ret, vcpu_state, |
| 184 | HF_OTHER_WORLD_ID); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 188 | * Checks whether the given `to` VM's mailbox is currently busy, and optionally |
| 189 | * registers the `from` VM to be notified when it becomes available. |
| 190 | */ |
| 191 | static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify) |
| 192 | { |
| 193 | if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 194 | to.vm->mailbox.recv == NULL) { |
| 195 | /* |
| 196 | * Fail if the receiver isn't currently ready to receive data, |
| 197 | * setting up for notification if requested. |
| 198 | */ |
| 199 | if (notify) { |
| 200 | struct wait_entry *entry = |
| 201 | vm_get_wait_entry(from, to.vm->id); |
| 202 | |
| 203 | /* Append waiter only if it's not there yet. */ |
| 204 | if (list_empty(&entry->wait_links)) { |
| 205 | list_append(&to.vm->mailbox.waiter_list, |
| 206 | &entry->wait_links); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 217 | * Returns true if the given vCPU is executing in context of an |
| 218 | * FFA_MSG_SEND_DIRECT_REQ invocation. |
| 219 | */ |
| 220 | static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked) |
| 221 | { |
| 222 | return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID; |
| 223 | } |
| 224 | |
| 225 | /** |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 226 | * Returns true if the VM owning the given vCPU is supporting managed exit and |
| 227 | * the vCPU is currently processing a managed exit. |
| 228 | */ |
| 229 | static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked) |
| 230 | { |
Maksims Svecovs | 9ddf86a | 2021-05-06 17:17:21 +0100 | [diff] [blame] | 231 | return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) && |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 232 | vcpu_locked.vcpu->processing_managed_exit); |
| 233 | } |
| 234 | |
| 235 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 236 | * 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] | 237 | */ |
| 238 | struct vcpu *api_preempt(struct vcpu *current) |
| 239 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 240 | struct ffa_value ret = { |
| 241 | .func = FFA_INTERRUPT_32, |
| 242 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 245 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 249 | * 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] | 250 | * VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 251 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 252 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 253 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 254 | struct ffa_value ret = { |
| 255 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 256 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 257 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 258 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 259 | return api_switch_to_primary(current, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 260 | VCPU_STATE_BLOCKED_INTERRUPT); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 264 | * Puts the current vCPU in off mode, and returns to the primary VM. |
| 265 | */ |
| 266 | struct vcpu *api_vcpu_off(struct vcpu *current) |
| 267 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 268 | struct ffa_value ret = { |
| 269 | .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT, |
| 270 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | /* |
| 274 | * Disable the timer, so the scheduler doesn't get told to call back |
| 275 | * based on it. |
| 276 | */ |
| 277 | arch_timer_disable_current(); |
| 278 | |
| 279 | return api_switch_to_primary(current, ret, VCPU_STATE_OFF); |
| 280 | } |
| 281 | |
| 282 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 283 | * Returns to the primary VM to allow this CPU to be used for other tasks as the |
| 284 | * vCPU does not have work to do at this moment. The current vCPU is marked as |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 285 | * ready to be scheduled again. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 286 | */ |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 287 | struct ffa_value api_yield(struct vcpu *current, struct vcpu **next) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 288 | { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 289 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 290 | struct vcpu_locked current_locked; |
| 291 | bool is_direct_request_ongoing; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 292 | |
| 293 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 294 | /* NOOP on the primary as it makes the scheduling decisions. */ |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 295 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 298 | current_locked = vcpu_lock(current); |
| 299 | is_direct_request_ongoing = |
| 300 | is_ffa_direct_msg_request_ongoing(current_locked); |
| 301 | vcpu_unlock(¤t_locked); |
| 302 | |
| 303 | if (is_direct_request_ongoing) { |
| 304 | return ffa_error(FFA_DENIED); |
| 305 | } |
| 306 | |
| 307 | *next = api_switch_to_primary( |
| 308 | current, |
| 309 | (struct ffa_value){.func = FFA_YIELD_32, |
| 310 | .arg1 = ffa_vm_vcpu(current->vm->id, |
| 311 | vcpu_index(current))}, |
| 312 | VCPU_STATE_READY); |
| 313 | |
| 314 | return ret; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 318 | * Switches to the primary so that it can switch to the target, or kick it if it |
| 319 | * is already running on a different physical CPU. |
| 320 | */ |
| 321 | struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu) |
| 322 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 323 | struct ffa_value ret = { |
| 324 | .func = HF_FFA_RUN_WAKE_UP, |
| 325 | .arg1 = ffa_vm_vcpu(target_vcpu->vm->id, |
| 326 | vcpu_index(target_vcpu)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 327 | }; |
| 328 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
| 329 | } |
| 330 | |
| 331 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 332 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 333 | */ |
| 334 | struct vcpu *api_abort(struct vcpu *current) |
| 335 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 336 | struct ffa_value ret = ffa_error(FFA_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 337 | |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 338 | dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 339 | vcpu_index(current)); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 340 | |
| 341 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 342 | /* TODO: what to do when the primary aborts? */ |
| 343 | for (;;) { |
| 344 | /* Do nothing. */ |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | atomic_store_explicit(¤t->vm->aborting, true, |
| 349 | memory_order_relaxed); |
| 350 | |
| 351 | /* TODO: free resources once all vCPUs abort. */ |
| 352 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 353 | return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 356 | struct ffa_value api_ffa_partition_info_get(struct vcpu *current, |
| 357 | const struct ffa_uuid *uuid) |
| 358 | { |
| 359 | struct vm *current_vm = current->vm; |
| 360 | struct vm_locked current_vm_locked; |
| 361 | ffa_vm_count_t vm_count = 0; |
| 362 | bool uuid_is_null = ffa_uuid_is_null(uuid); |
| 363 | struct ffa_value ret; |
| 364 | uint32_t size; |
| 365 | struct ffa_partition_info partitions[MAX_VMS]; |
| 366 | |
| 367 | /* |
| 368 | * Iterate through the VMs to find the ones with a matching UUID. |
| 369 | * A Null UUID retrieves information for all VMs. |
| 370 | */ |
| 371 | for (uint16_t index = 0; index < vm_get_count(); ++index) { |
| 372 | const struct vm *vm = vm_find_index(index); |
| 373 | |
| 374 | if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) { |
| 375 | partitions[vm_count].vm_id = vm->id; |
| 376 | partitions[vm_count].vcpu_count = vm->vcpu_count; |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 377 | partitions[vm_count].properties = |
Maksims Svecovs | b596eab | 2021-04-27 00:52:27 +0100 | [diff] [blame] | 378 | plat_ffa_partition_properties(current_vm->id, |
| 379 | vm); |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 380 | |
| 381 | ++vm_count; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* Unrecognized UUID: does not match any of the VMs and is not Null. */ |
| 386 | if (vm_count == 0) { |
| 387 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 388 | } |
| 389 | |
| 390 | size = vm_count * sizeof(partitions[0]); |
| 391 | if (size > FFA_MSG_PAYLOAD_MAX) { |
| 392 | dlog_error( |
| 393 | "Partition information does not fit in the VM's RX " |
| 394 | "buffer.\n"); |
| 395 | return ffa_error(FFA_NO_MEMORY); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Partition information is returned in the VM's RX buffer, which is why |
| 400 | * the lock is needed. |
| 401 | */ |
| 402 | current_vm_locked = vm_lock(current_vm); |
| 403 | |
| 404 | if (msg_receiver_busy(current_vm_locked, NULL, false)) { |
| 405 | /* |
| 406 | * Can't retrieve memory information if the mailbox is not |
| 407 | * available. |
| 408 | */ |
| 409 | dlog_verbose("RX buffer not ready.\n"); |
| 410 | ret = ffa_error(FFA_BUSY); |
| 411 | goto out_unlock; |
| 412 | } |
| 413 | |
| 414 | /* Populate the VM's RX buffer with the partition information. */ |
| 415 | memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions, |
| 416 | size); |
| 417 | current_vm->mailbox.recv_size = size; |
| 418 | current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID; |
| 419 | current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32; |
| 420 | current_vm->mailbox.state = MAILBOX_STATE_READ; |
| 421 | |
| 422 | /* Return the count of partition information descriptors in w2. */ |
| 423 | ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count}; |
| 424 | |
| 425 | out_unlock: |
| 426 | vm_unlock(¤t_vm_locked); |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 431 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 432 | * Returns the ID of the VM. |
| 433 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 434 | struct ffa_value api_ffa_id_get(const struct vcpu *current) |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 435 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 436 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 437 | .arg2 = current->vm->id}; |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 441 | * Returns the ID of the SPMC. |
| 442 | */ |
| 443 | struct ffa_value api_ffa_spm_id_get(void) |
| 444 | { |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 445 | #if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) |
| 446 | /* |
| 447 | * Return the SPMC ID that was fetched during FF-A |
| 448 | * initialization. |
| 449 | */ |
| 450 | return (struct ffa_value){.func = FFA_SUCCESS_32, |
| 451 | .arg2 = arch_ffa_spmc_id_get()}; |
| 452 | #else |
| 453 | return ffa_error(FFA_NOT_SUPPORTED); |
| 454 | #endif |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 458 | * This function is called by the architecture-specific context switching |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 459 | * function to indicate that register state for the given vCPU has been saved |
| 460 | * and can therefore be used by other pCPUs. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 461 | */ |
| 462 | void api_regs_state_saved(struct vcpu *vcpu) |
| 463 | { |
| 464 | sl_lock(&vcpu->lock); |
| 465 | vcpu->regs_available = true; |
| 466 | sl_unlock(&vcpu->lock); |
| 467 | } |
| 468 | |
| 469 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 470 | * Retrieves the next waiter and removes it from the wait list if the VM's |
| 471 | * mailbox is in a writable state. |
| 472 | */ |
| 473 | static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm) |
| 474 | { |
| 475 | struct wait_entry *entry; |
| 476 | struct vm *vm = locked_vm.vm; |
| 477 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 478 | if (vm->mailbox.state != MAILBOX_STATE_EMPTY || |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 479 | vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) { |
| 480 | /* The mailbox is not writable or there are no waiters. */ |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | /* Remove waiter from the wait list. */ |
| 485 | entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry, |
| 486 | wait_links); |
| 487 | list_remove(&entry->wait_links); |
| 488 | return entry; |
| 489 | } |
| 490 | |
| 491 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 492 | * Assuming that the arguments have already been checked by the caller, injects |
| 493 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 494 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 495 | * is next run, which is up to the scheduler. |
| 496 | * |
| 497 | * Returns: |
| 498 | * - 0 on success if no further action is needed. |
| 499 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 500 | * up or kick the target vCPU. |
| 501 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 502 | int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked, |
| 503 | uint32_t intid, struct vcpu *current, |
| 504 | struct vcpu **next) |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 505 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 506 | struct vcpu *target_vcpu = target_locked.vcpu; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 507 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 508 | uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS; |
| 509 | uint32_t intid_mask = 1U << intid_shift; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 510 | int64_t ret = 0; |
| 511 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 512 | /* |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 513 | * We only need to change state and (maybe) trigger a virtual interrupt |
| 514 | * if it is enabled and was not previously pending. Otherwise we can |
| 515 | * skip everything except setting the pending bit. |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 516 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 517 | if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 518 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 519 | intid_mask)) { |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | /* Increment the count. */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 524 | if ((target_vcpu->interrupts.interrupt_type[intid_index] & |
| 525 | intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) { |
| 526 | vcpu_irq_count_increment(target_locked); |
| 527 | } else { |
| 528 | vcpu_fiq_count_increment(target_locked); |
| 529 | } |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | * Only need to update state if there was not already an |
| 533 | * interrupt enabled and pending. |
| 534 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 535 | if (vcpu_interrupt_count_get(target_locked) != 1) { |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 536 | goto out; |
| 537 | } |
| 538 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 539 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 540 | /* |
| 541 | * If the call came from the primary VM, let it know that it |
| 542 | * should run or kick the target vCPU. |
| 543 | */ |
| 544 | ret = 1; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 545 | } else if (current != target_vcpu && next != NULL) { |
| 546 | *next = api_wake_up(current, target_vcpu); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | out: |
| 550 | /* Either way, make it pending. */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 551 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 552 | |
Olivier Deprez | c19a6ea | 2020-08-06 11:16:07 +0200 | [diff] [blame] | 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | /* Wrapper to internal_interrupt_inject with locking of target vCPU */ |
| 557 | static int64_t internal_interrupt_inject(struct vcpu *target_vcpu, |
| 558 | uint32_t intid, struct vcpu *current, |
| 559 | struct vcpu **next) |
| 560 | { |
| 561 | int64_t ret; |
| 562 | struct vcpu_locked target_locked; |
| 563 | |
| 564 | target_locked = vcpu_lock(target_vcpu); |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 565 | ret = api_interrupt_inject_locked(target_locked, intid, current, next); |
Olivier Deprez | c19a6ea | 2020-08-06 11:16:07 +0200 | [diff] [blame] | 566 | vcpu_unlock(&target_locked); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 567 | |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 572 | * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL |
| 573 | * or FFA_MSG_WAIT call. |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 574 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 575 | static struct ffa_value ffa_msg_recv_return(const struct vm *receiver) |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 576 | { |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 577 | switch (receiver->mailbox.recv_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 578 | case FFA_MSG_SEND_32: |
| 579 | return (struct ffa_value){ |
| 580 | .func = FFA_MSG_SEND_32, |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 581 | .arg1 = (receiver->mailbox.recv_sender << 16) | |
| 582 | receiver->id, |
| 583 | .arg3 = receiver->mailbox.recv_size}; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 584 | default: |
| 585 | /* This should never be reached, but return an error in case. */ |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 586 | dlog_error("Tried to return an invalid message function %#x\n", |
| 587 | receiver->mailbox.recv_func); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 588 | return ffa_error(FFA_DENIED); |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 589 | } |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 593 | * 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] | 594 | * value needs to be forced onto the vCPU. |
| 595 | */ |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 596 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 597 | struct ffa_value *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 598 | { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 599 | struct vcpu_locked vcpu_locked; |
| 600 | struct vm_locked vm_locked; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 601 | bool need_vm_lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 602 | bool ret; |
| 603 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 604 | /* |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 605 | * 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] | 606 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 607 | * The VM lock is not needed in the common case so it must only be taken |
| 608 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 609 | * dependencies in the common run case meaning the sensitive context |
| 610 | * switch performance is consistent. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 611 | */ |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 612 | vcpu_locked = vcpu_lock(vcpu); |
| 613 | |
| 614 | #if SECURE_WORLD == 1 |
| 615 | |
| 616 | if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep, |
| 617 | 0)) { |
| 618 | dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n", |
| 619 | __func__, vcpu->vm->id, current->cpu->id); |
| 620 | } |
| 621 | |
| 622 | #endif |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 623 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 624 | /* The VM needs to be locked to deliver mailbox messages. */ |
| 625 | need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX; |
| 626 | if (need_vm_lock) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 627 | vcpu_unlock(&vcpu_locked); |
| 628 | vm_locked = vm_lock(vcpu->vm); |
| 629 | vcpu_locked = vcpu_lock(vcpu); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
| 633 | * If the vCPU is already running somewhere then we can't run it here |
| 634 | * simultaneously. While it is actually running then the state should be |
| 635 | * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it |
| 636 | * stops running but while Hafnium is in the process of switching back |
| 637 | * to the primary there will be a brief period while the state has been |
| 638 | * updated but `regs_available` is still false (until |
| 639 | * `api_regs_state_saved` is called). We can't start running it again |
| 640 | * until this has finished, so count this state as still running for the |
| 641 | * purposes of this check. |
| 642 | */ |
| 643 | if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) { |
| 644 | /* |
| 645 | * vCPU is running on another pCPU. |
| 646 | * |
| 647 | * It's okay not to return the sleep duration here because the |
| 648 | * other physical CPU that is currently running this vCPU will |
| 649 | * return the sleep duration if needed. |
| 650 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 651 | *run_ret = ffa_error(FFA_BUSY); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 652 | ret = false; |
| 653 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 654 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 655 | |
| 656 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 657 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 658 | dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id, |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 659 | vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 660 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 661 | } |
| 662 | ret = false; |
| 663 | goto out; |
| 664 | } |
| 665 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 666 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 667 | case VCPU_STATE_RUNNING: |
| 668 | case VCPU_STATE_OFF: |
| 669 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 670 | ret = false; |
| 671 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 672 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 673 | case VCPU_STATE_BLOCKED_MAILBOX: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 674 | /* |
| 675 | * A pending message allows the vCPU to run so the message can |
| 676 | * be delivered directly. |
| 677 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 678 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 679 | arch_regs_set_retval(&vcpu->regs, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 680 | ffa_msg_recv_return(vcpu->vm)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 681 | vcpu->vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | /* Fall through. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 685 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 686 | /* Allow virtual interrupts to be delivered. */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 687 | if (vcpu_interrupt_count_get(vcpu_locked) > 0) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 691 | uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE; |
| 692 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 693 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 694 | timer_remaining_ns = |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 695 | arch_timer_remaining_ns(&vcpu->regs); |
| 696 | |
| 697 | /* |
| 698 | * The timer expired so allow the interrupt to be |
| 699 | * delivered. |
| 700 | */ |
| 701 | if (timer_remaining_ns == 0) { |
| 702 | break; |
| 703 | } |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 704 | } |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 705 | |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 706 | /* |
| 707 | * The vCPU is not ready to run, return the appropriate code to |
| 708 | * the primary which called vcpu_run. |
| 709 | */ |
| 710 | run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 711 | ? FFA_MSG_WAIT_32 |
| 712 | : HF_FFA_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 4692a3a | 2020-08-07 12:42:01 +0100 | [diff] [blame] | 713 | run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
| 714 | run_ret->arg2 = timer_remaining_ns; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 715 | |
| 716 | ret = false; |
| 717 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 718 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 719 | case VCPU_STATE_READY: |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 720 | break; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 723 | /* It has been decided that the vCPU should be run. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 724 | vcpu->cpu = current->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 725 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 726 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 727 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 728 | * Mark the registers as unavailable now that we're about to reflect |
| 729 | * them onto the real registers. This will also prevent another physical |
| 730 | * CPU from trying to read these registers. |
| 731 | */ |
| 732 | vcpu->regs_available = false; |
| 733 | |
| 734 | ret = true; |
| 735 | |
| 736 | out: |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 737 | vcpu_unlock(&vcpu_locked); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 738 | if (need_vm_lock) { |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 739 | vm_unlock(&vm_locked); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 742 | return ret; |
| 743 | } |
| 744 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 745 | struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx, |
| 746 | const struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 747 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 748 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 749 | struct vcpu *vcpu; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 750 | struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 751 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 752 | /* Only the primary VM can switch vCPUs. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 753 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 754 | ret.arg2 = FFA_DENIED; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 755 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 756 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 757 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 758 | /* Only secondary VM vCPUs can be run. */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 759 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 760 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 761 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 762 | |
Raghu Krishnamurthy | 62f97a7 | 2021-07-27 02:14:59 -0700 | [diff] [blame] | 763 | if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) { |
| 764 | return ret; |
| 765 | } |
| 766 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 767 | /* The requested VM must exist. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 768 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 769 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 770 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 773 | /* The requested vCPU must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 774 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 775 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 776 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 777 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 778 | /* Update state if allowed. */ |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 779 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 780 | if (!api_vcpu_prepare_run(current, vcpu, &ret)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 781 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 782 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 783 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Inject timer interrupt if timer has expired. It's safe to access |
| 786 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 787 | * regs_available was true (and then set it to false) before returning |
| 788 | * true. |
| 789 | */ |
| 790 | if (arch_timer_pending(&vcpu->regs)) { |
| 791 | /* Make virtual timer interrupt pending. */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 792 | internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu, |
| 793 | NULL); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * Set the mask bit so the hardware interrupt doesn't fire |
| 797 | * again. Ideally we wouldn't do this because it affects what |
| 798 | * the secondary vCPU sees, but if we don't then we end up with |
| 799 | * a loop of the interrupt firing each time we try to return to |
| 800 | * the secondary vCPU. |
| 801 | */ |
| 802 | arch_timer_mask(&vcpu->regs); |
| 803 | } |
| 804 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 805 | /* Switch to the vCPU. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 806 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 807 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 808 | /* |
| 809 | * Set a placeholder return code to the scheduler. This will be |
| 810 | * overwritten when the switch back to the primary occurs. |
| 811 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 812 | ret.func = FFA_INTERRUPT_32; |
| 813 | ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx); |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 814 | ret.arg2 = 0; |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 815 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 816 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 817 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 821 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 822 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 823 | static bool api_mode_valid_owned_and_exclusive(uint32_t mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 824 | { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 825 | return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 826 | MM_MODE_SHARED)) == 0; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
Andrew Walbran | c8a0197 | 2020-09-22 11:23:30 +0100 | [diff] [blame] | 830 | * Determines the value to be returned by api_ffa_rxtx_map and |
| 831 | * api_ffa_rx_release after they've succeeded. If a secondary VM is running and |
| 832 | * there are waiters, it also switches back to the primary VM for it to wake |
| 833 | * waiters up. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 834 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 835 | static struct ffa_value api_waiter_result(struct vm_locked locked_vm, |
| 836 | struct vcpu *current, |
| 837 | struct vcpu **next) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 838 | { |
| 839 | struct vm *vm = locked_vm.vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 840 | |
| 841 | if (list_empty(&vm->mailbox.waiter_list)) { |
| 842 | /* No waiters, nothing else to do. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 843 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (vm->id == HF_PRIMARY_VM_ID) { |
| 847 | /* The caller is the primary VM. Tell it to wake up waiters. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 848 | return (struct ffa_value){.func = FFA_RX_RELEASE_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Switch back to the primary VM, informing it that there are waiters |
| 853 | * that need to be notified. |
| 854 | */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 855 | *next = api_switch_to_primary( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 856 | current, (struct ffa_value){.func = FFA_RX_RELEASE_32}, |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 857 | VCPU_STATE_READY); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 858 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 859 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 863 | * 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] | 864 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 865 | static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked, |
| 866 | struct vm_locked vm_locked, |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 867 | paddr_t pa_send_begin, paddr_t pa_send_end, |
| 868 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 869 | uint32_t extra_attributes, |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 870 | struct mpool *local_page_pool) |
| 871 | { |
| 872 | bool ret; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 873 | |
| 874 | /* Map the send page as read-only in the hypervisor address space. */ |
| 875 | vm_locked.vm->mailbox.send = |
| 876 | mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 877 | MM_MODE_R | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 878 | if (!vm_locked.vm->mailbox.send) { |
| 879 | /* TODO: partial defrag of failed range. */ |
| 880 | /* Recover any memory consumed in failed mapping. */ |
| 881 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 882 | goto fail; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Map the receive page as writable in the hypervisor address space. On |
| 887 | * failure, unmap the send page before returning. |
| 888 | */ |
| 889 | vm_locked.vm->mailbox.recv = |
| 890 | mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 891 | MM_MODE_W | extra_attributes, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 892 | if (!vm_locked.vm->mailbox.recv) { |
| 893 | /* TODO: partial defrag of failed range. */ |
| 894 | /* Recover any memory consumed in failed mapping. */ |
| 895 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 896 | goto fail_undo_send; |
| 897 | } |
| 898 | |
| 899 | ret = true; |
| 900 | goto out; |
| 901 | |
| 902 | /* |
| 903 | * The following mappings will not require more memory than is available |
| 904 | * in the local pool. |
| 905 | */ |
| 906 | fail_undo_send: |
| 907 | vm_locked.vm->mailbox.send = NULL; |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 908 | CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 909 | local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 910 | |
| 911 | fail: |
| 912 | ret = false; |
| 913 | |
| 914 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | /** |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 919 | * Sanity checks and configures the send and receive pages in the VM stage-2 |
| 920 | * and hypervisor stage-1 page tables. |
| 921 | * |
| 922 | * Returns: |
| 923 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 924 | * aligned, are the same or have invalid attributes. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 925 | * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers |
| 926 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 927 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 928 | * - FFA_SUCCESS on success if no further action is needed. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 929 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 930 | |
| 931 | struct ffa_value api_vm_configure_pages( |
| 932 | struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked, |
| 933 | ipaddr_t send, ipaddr_t recv, uint32_t page_count, |
| 934 | struct mpool *local_page_pool) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 935 | { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 936 | struct ffa_value ret; |
| 937 | paddr_t pa_send_begin; |
| 938 | paddr_t pa_send_end; |
| 939 | paddr_t pa_recv_begin; |
| 940 | paddr_t pa_recv_end; |
| 941 | uint32_t orig_send_mode; |
| 942 | uint32_t orig_recv_mode; |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 943 | uint32_t extra_attributes; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 944 | |
| 945 | /* We only allow these to be setup once. */ |
| 946 | if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) { |
| 947 | ret = ffa_error(FFA_DENIED); |
| 948 | goto out; |
| 949 | } |
| 950 | |
| 951 | /* Hafnium only supports a fixed size of RX/TX buffers. */ |
| 952 | if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) { |
| 953 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 954 | goto out; |
| 955 | } |
| 956 | |
| 957 | /* Fail if addresses are not page-aligned. */ |
| 958 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 959 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
| 960 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | /* Convert to physical addresses. */ |
| 965 | pa_send_begin = pa_from_ipa(send); |
| 966 | pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE); |
| 967 | pa_recv_begin = pa_from_ipa(recv); |
| 968 | pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE); |
| 969 | |
| 970 | /* Fail if the same page is used for the send and receive pages. */ |
| 971 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
| 972 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 973 | goto out; |
| 974 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 975 | |
| 976 | /* |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 977 | * Ensure the pages are valid, owned and exclusive to the VM and that |
| 978 | * the VM has the required access to the memory. |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 979 | */ |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 980 | if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE), |
| 981 | &orig_send_mode) || |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 982 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 983 | (orig_send_mode & MM_MODE_R) == 0 || |
| 984 | (orig_send_mode & MM_MODE_W) == 0) { |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 985 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 986 | goto out; |
| 987 | } |
| 988 | |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 989 | if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE), |
| 990 | &orig_recv_mode) || |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 991 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 992 | (orig_recv_mode & MM_MODE_R) == 0) { |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 993 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 994 | goto out; |
| 995 | } |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 996 | |
| 997 | /* Take memory ownership away from the VM and mark as shared. */ |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 998 | uint32_t mode = |
| 999 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W; |
| 1000 | if (vm_locked.vm->el0_partition) { |
| 1001 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1002 | } |
| 1003 | |
| 1004 | if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode, |
| 1005 | local_page_pool, NULL)) { |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1006 | ret = ffa_error(FFA_NO_MEMORY); |
| 1007 | goto out; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1010 | mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R; |
| 1011 | if (vm_locked.vm->el0_partition) { |
| 1012 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 1013 | } |
| 1014 | |
| 1015 | if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1016 | local_page_pool, NULL)) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1017 | /* TODO: partial defrag of failed range. */ |
| 1018 | /* Recover any memory consumed in failed mapping. */ |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1019 | vm_ptable_defrag(vm_locked, local_page_pool); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1020 | goto fail_undo_send; |
| 1021 | } |
| 1022 | |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1023 | /* Get extra send/recv pages mapping attributes for the given VM ID. */ |
| 1024 | extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id); |
| 1025 | |
Raghu Krishnamurthy | 95c3a27 | 2021-02-26 18:09:41 -0800 | [diff] [blame] | 1026 | /* |
| 1027 | * For EL0 partitions, since both the partition and the hypervisor code |
| 1028 | * use the EL2&0 translation regime, it is critical to mark the mappings |
| 1029 | * of the send and recv buffers as non-global in the TLB. For one, if we |
| 1030 | * dont mark it as non-global, it would cause TLB conflicts since there |
| 1031 | * would be an identity mapping with non-global attribute in the |
| 1032 | * partitions page tables, but another identity mapping in the |
| 1033 | * hypervisor page tables with the global attribute. The other issue is |
| 1034 | * one of security, we dont want other partitions to be able to access |
| 1035 | * other partitions buffers through cached translations. |
| 1036 | */ |
| 1037 | if (vm_locked.vm->el0_partition) { |
| 1038 | extra_attributes |= MM_MODE_NG; |
| 1039 | } |
| 1040 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1041 | if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin, |
| 1042 | pa_send_end, pa_recv_begin, pa_recv_end, |
Olivier Deprez | 96a2a26 | 2020-06-11 17:21:38 +0200 | [diff] [blame] | 1043 | extra_attributes, local_page_pool)) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1044 | goto fail_undo_send_and_recv; |
| 1045 | } |
| 1046 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1047 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1048 | goto out; |
| 1049 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1050 | fail_undo_send_and_recv: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1051 | CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1052 | orig_send_mode, local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1053 | |
| 1054 | fail_undo_send: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 1055 | CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1056 | orig_send_mode, local_page_pool, NULL)); |
| 1057 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1058 | |
| 1059 | out: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1064 | * 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] | 1065 | * must not be shared. Locking of the page tables combined with a local memory |
| 1066 | * pool ensures there will always be enough memory to recover from any errors |
| 1067 | * that arise. The stage-1 page tables must be locked so memory cannot be taken |
| 1068 | * by another core which could result in this transaction being unable to roll |
| 1069 | * back in the case of an error. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1070 | * |
| 1071 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1072 | * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1073 | * aligned, are the same or have invalid attributes. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1074 | * - 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] | 1075 | * due to insuffient page table memory. |
Daniel Boulby | 6f8941e | 2021-06-14 18:27:18 +0100 | [diff] [blame] | 1076 | * - FFA_ERROR FFA_DENIED if the pages are already mapped. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1077 | * - FFA_SUCCESS on success if no further action is needed. |
| 1078 | * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1079 | * needs to wake up or kick waiters. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1080 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1081 | struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv, |
| 1082 | uint32_t page_count, struct vcpu *current, |
| 1083 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1084 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1085 | struct vm *vm = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1086 | struct ffa_value ret; |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1087 | struct vm_locked vm_locked; |
| 1088 | struct mm_stage1_locked mm_stage1_locked; |
| 1089 | struct mpool local_page_pool; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1090 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1091 | /* |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1092 | * Create a local pool so any freed memory can't be used by another |
| 1093 | * thread. This is to ensure the original mapping can be restored if any |
| 1094 | * stage of the process fails. |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1095 | */ |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1096 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 1097 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1098 | vm_locked = vm_lock(vm); |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1099 | mm_stage1_locked = mm_lock_stage1(); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1100 | |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1101 | ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv, |
| 1102 | page_count, &local_page_pool); |
| 1103 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1104 | goto exit; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1107 | /* Tell caller about waiters, if any. */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1108 | ret = api_waiter_result(vm_locked, current, next); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 1109 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1110 | exit: |
Manish Pandey | d34f889 | 2020-06-19 17:41:07 +0100 | [diff] [blame] | 1111 | mpool_fini(&local_page_pool); |
| 1112 | |
| 1113 | mm_unlock_stage1(&mm_stage1_locked); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 1114 | vm_unlock(&vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1115 | |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | /** |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1120 | * Unmaps the RX/TX buffer pair with a partition or partition manager from the |
| 1121 | * translation regime of the caller. Unmap the region for the hypervisor and |
| 1122 | * set the memory region to owned and exclusive for the component. Since the |
| 1123 | * memory region mapped in the page table, when the buffers were originally |
| 1124 | * created we can safely remap it. |
| 1125 | * |
| 1126 | * Returns: |
| 1127 | * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on |
| 1128 | * behalf of the caller. |
| 1129 | * - FFA_SUCCESS on success if no further action is needed. |
| 1130 | */ |
| 1131 | struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id, |
| 1132 | struct vcpu *current) |
| 1133 | { |
| 1134 | struct vm *vm = current->vm; |
| 1135 | struct vm_locked vm_locked; |
| 1136 | struct mm_stage1_locked mm_stage1_locked; |
| 1137 | paddr_t send_pa_begin; |
| 1138 | paddr_t send_pa_end; |
| 1139 | paddr_t recv_pa_begin; |
| 1140 | paddr_t recv_pa_end; |
| 1141 | |
| 1142 | /* |
| 1143 | * Check there is a buffer pair registered on behalf of the caller. |
| 1144 | * Since forwarding is not yet supported the allocator ID MBZ. |
| 1145 | */ |
| 1146 | if (allocator_id != 0) { |
| 1147 | dlog_error( |
| 1148 | "Forwarding MAP/UNMAP from the hypervisor is not yet " |
| 1149 | "supported so vm id must be zero.\n"); |
| 1150 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1151 | } |
| 1152 | |
| 1153 | /* Get send and receive buffers. */ |
| 1154 | if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) { |
Olivier Deprez | 86d87ae | 2021-08-19 14:27:46 +0200 | [diff] [blame] | 1155 | dlog_verbose( |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1156 | "No buffer pair registered on behalf of the caller.\n"); |
| 1157 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1158 | } |
| 1159 | |
| 1160 | /* Currently a mailbox size of 1 page is assumed. */ |
| 1161 | send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send)); |
| 1162 | send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE); |
| 1163 | recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv)); |
| 1164 | recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE); |
| 1165 | |
| 1166 | vm_locked = vm_lock(vm); |
| 1167 | mm_stage1_locked = mm_lock_stage1(); |
| 1168 | |
| 1169 | /* |
| 1170 | * Set the memory region of the buffers back to the default mode |
| 1171 | * for the VM. Since this memory region was already mapped for the |
| 1172 | * RXTX buffers we can safely remap them. |
| 1173 | */ |
| 1174 | CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end, |
| 1175 | MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool, |
| 1176 | NULL)); |
| 1177 | |
| 1178 | CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end, |
| 1179 | MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool, |
| 1180 | NULL)); |
| 1181 | |
| 1182 | /* Unmap the buffers in the partition manager. */ |
| 1183 | CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end, |
| 1184 | &api_page_pool)); |
| 1185 | CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end, |
| 1186 | &api_page_pool)); |
| 1187 | |
| 1188 | vm->mailbox.send = NULL; |
| 1189 | vm->mailbox.recv = NULL; |
| 1190 | |
| 1191 | mm_unlock_stage1(&mm_stage1_locked); |
| 1192 | vm_unlock(&vm_locked); |
| 1193 | |
| 1194 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 1195 | } |
| 1196 | |
| 1197 | /** |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1198 | * Notifies the `to` VM about the message currently in its mailbox, possibly |
| 1199 | * with the help of the primary VM. |
| 1200 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1201 | static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id, |
| 1202 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1203 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1204 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 1205 | struct ffa_value primary_ret = { |
| 1206 | .func = FFA_MSG_SEND_32, |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 1207 | .arg1 = ((uint32_t)from_id << 16) | to.vm->id, |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1208 | }; |
| 1209 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1210 | /* Messages for the primary VM are delivered directly. */ |
| 1211 | if (to.vm->id == HF_PRIMARY_VM_ID) { |
| 1212 | /* |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1213 | * Only tell the primary VM the size and other details if the |
| 1214 | * message is for it, to avoid leaking data about messages for |
| 1215 | * other VMs. |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1216 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1217 | primary_ret = ffa_msg_recv_return(to.vm); |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1218 | |
| 1219 | to.vm->mailbox.state = MAILBOX_STATE_READ; |
| 1220 | *next = api_switch_to_primary(current, primary_ret, |
| 1221 | VCPU_STATE_READY); |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1222 | return ret; |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 1225 | to.vm->mailbox.state = MAILBOX_STATE_RECEIVED; |
| 1226 | |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1227 | /* Messages for the TEE are sent on via the dispatcher. */ |
| 1228 | if (to.vm->id == HF_TEE_VM_ID) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1229 | struct ffa_value call = ffa_msg_recv_return(to.vm); |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1230 | |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 1231 | ret = arch_other_world_call(call); |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 1232 | /* |
| 1233 | * After the call to the TEE completes it must have finished |
| 1234 | * reading its RX buffer, so it is ready for another message. |
| 1235 | */ |
| 1236 | to.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1237 | /* |
| 1238 | * Don't return to the primary VM in this case, as the TEE is |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1239 | * not (yet) scheduled via FF-A. |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1240 | */ |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 1241 | return ret; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1244 | /* Return to the primary VM directly or with a switch. */ |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 1245 | if (from_id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1246 | *next = api_switch_to_primary(current, primary_ret, |
| 1247 | VCPU_STATE_READY); |
| 1248 | } |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1249 | |
| 1250 | return ret; |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1254 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 1255 | * and notifies the recipient. |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 1256 | * |
| 1257 | * If the recipient's receive buffer is busy, it can optionally register the |
| 1258 | * caller to be notified when the recipient's receive buffer becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1259 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1260 | struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id, |
| 1261 | ffa_vm_id_t receiver_vm_id, uint32_t size, |
| 1262 | uint32_t attributes, struct vcpu *current, |
| 1263 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1264 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1265 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1266 | struct vm *to; |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1267 | struct vm_locked to_locked; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1268 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1269 | struct ffa_value ret; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1270 | struct vcpu_locked current_locked; |
| 1271 | bool is_direct_request_ongoing; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1272 | bool notify = |
| 1273 | (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1274 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1275 | /* Ensure sender VM ID corresponds to the current VM. */ |
| 1276 | if (sender_vm_id != from->id) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1277 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 1281 | if (receiver_vm_id == from->id) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1282 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* Limit the size of transfer. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1286 | if (size > FFA_MSG_PAYLOAD_MAX) { |
| 1287 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ |
| 1292 | * invocation. |
| 1293 | */ |
| 1294 | current_locked = vcpu_lock(current); |
| 1295 | is_direct_request_ongoing = |
| 1296 | is_ffa_direct_msg_request_ongoing(current_locked); |
| 1297 | vcpu_unlock(¤t_locked); |
| 1298 | |
| 1299 | if (is_direct_request_ongoing) { |
| 1300 | return ffa_error(FFA_DENIED); |
| 1301 | } |
| 1302 | |
Andrew Walbran | 0b60c4f | 2019-12-10 17:05:29 +0000 | [diff] [blame] | 1303 | /* Ensure the receiver VM exists. */ |
| 1304 | to = vm_find(receiver_vm_id); |
| 1305 | if (to == NULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1306 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 0b60c4f | 2019-12-10 17:05:29 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 1309 | /* |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1310 | * Check that the sender has configured its send buffer. If the tx |
| 1311 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 1312 | * be safely accessed after releasing the lock since the tx mailbox |
| 1313 | * address can only be configured once. |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 1314 | */ |
| 1315 | sl_lock(&from->lock); |
| 1316 | from_msg = from->mailbox.send; |
| 1317 | sl_unlock(&from->lock); |
| 1318 | |
| 1319 | if (from_msg == NULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1320 | return ffa_error(FFA_INVALID_PARAMETERS); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1323 | to_locked = vm_lock(to); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1324 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1325 | if (msg_receiver_busy(to_locked, from, notify)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1326 | ret = ffa_error(FFA_BUSY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1327 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1330 | /* Copy data. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1331 | memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size); |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1332 | to->mailbox.recv_size = size; |
| 1333 | to->mailbox.recv_sender = sender_vm_id; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1334 | to->mailbox.recv_func = FFA_MSG_SEND_32; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1335 | ret = deliver_msg(to_locked, sender_vm_id, current, next); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1336 | |
| 1337 | out: |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1338 | vm_unlock(&to_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1339 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 1340 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | /** |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1344 | * Checks whether the vCPU's attempt to block for a message has already been |
| 1345 | * interrupted or whether it is allowed to block. |
| 1346 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1347 | bool api_ffa_msg_recv_block_interrupted(struct vcpu *current) |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1348 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1349 | struct vcpu_locked current_locked; |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1350 | bool interrupted; |
| 1351 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1352 | current_locked = vcpu_lock(current); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* |
| 1355 | * Don't block if there are enabled and pending interrupts, to match |
| 1356 | * behaviour of wait_for_interrupt. |
| 1357 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1358 | interrupted = (vcpu_interrupt_count_get(current_locked) > 0); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1359 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1360 | vcpu_unlock(¤t_locked); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1361 | |
| 1362 | return interrupted; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1366 | * Receives a message from the mailbox. If one isn't available, this function |
| 1367 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1368 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1369 | * 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] | 1370 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1371 | struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current, |
| 1372 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1373 | { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1374 | bool is_direct_request_ongoing; |
| 1375 | struct vcpu_locked current_locked; |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1376 | struct vm *vm = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1377 | struct ffa_value return_code; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1378 | bool is_from_secure_world = |
| 1379 | (current->vm->id & HF_VM_ID_WORLD_MASK) != 0; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1380 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1381 | /* |
| 1382 | * The primary VM will receive messages as a status code from running |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1383 | * vCPUs and must not call this function. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1384 | */ |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1385 | if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1386 | return ffa_error(FFA_NOT_SUPPORTED); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ |
| 1391 | * invocation. |
| 1392 | */ |
| 1393 | current_locked = vcpu_lock(current); |
| 1394 | is_direct_request_ongoing = |
| 1395 | is_ffa_direct_msg_request_ongoing(current_locked); |
| 1396 | vcpu_unlock(¤t_locked); |
| 1397 | |
| 1398 | if (is_direct_request_ongoing) { |
| 1399 | return ffa_error(FFA_DENIED); |
| 1400 | } |
| 1401 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1402 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1403 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1404 | /* Return pending messages without blocking. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1405 | if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
| 1406 | vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1407 | return_code = ffa_msg_recv_return(vm); |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1408 | goto out; |
| 1409 | } |
| 1410 | |
| 1411 | /* No pending message so fail if not allowed to block. */ |
| 1412 | if (!block) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1413 | return_code = ffa_error(FFA_RETRY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1414 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1415 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1416 | |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1417 | /* |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1418 | * From this point onward this call can only be interrupted or a message |
| 1419 | * 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] | 1420 | * that time to FFA_SUCCESS. |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1421 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1422 | return_code = ffa_error(FFA_INTERRUPTED); |
| 1423 | if (api_ffa_msg_recv_block_interrupted(current)) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1424 | goto out; |
| 1425 | } |
| 1426 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 1427 | if (is_from_secure_world) { |
| 1428 | /* Return to other world if caller is a SP. */ |
| 1429 | *next = api_switch_to_other_world( |
| 1430 | current, (struct ffa_value){.func = FFA_MSG_WAIT_32}, |
| 1431 | VCPU_STATE_BLOCKED_MAILBOX); |
| 1432 | } else { |
| 1433 | /* Switch back to primary VM to block. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1434 | struct ffa_value run_return = { |
| 1435 | .func = FFA_MSG_WAIT_32, |
| 1436 | .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)), |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1437 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1438 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1439 | *next = api_switch_to_primary(current, run_return, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1440 | VCPU_STATE_BLOCKED_MAILBOX); |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1441 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1442 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1443 | sl_unlock(&vm->lock); |
| 1444 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1445 | return return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1449 | * Retrieves the next VM whose mailbox became writable. For a VM to be notified |
| 1450 | * by this function, the caller must have called api_mailbox_send before with |
| 1451 | * the notify argument set to true, and this call must have failed because the |
| 1452 | * mailbox was not available. |
| 1453 | * |
| 1454 | * It should be called repeatedly to retrieve a list of VMs. |
| 1455 | * |
| 1456 | * Returns -1 if no VM became writable, or the id of the VM whose mailbox |
| 1457 | * became writable. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1458 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1459 | int64_t api_mailbox_writable_get(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1460 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1461 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1462 | struct wait_entry *entry; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 1463 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1464 | |
| 1465 | sl_lock(&vm->lock); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1466 | if (list_empty(&vm->mailbox.ready_list)) { |
| 1467 | ret = -1; |
| 1468 | goto exit; |
| 1469 | } |
| 1470 | |
| 1471 | entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry, |
| 1472 | ready_links); |
| 1473 | list_remove(&entry->ready_links); |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 1474 | ret = vm_id_for_wait_entry(vm, entry); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1475 | |
| 1476 | exit: |
| 1477 | sl_unlock(&vm->lock); |
| 1478 | return ret; |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * Retrieves the next VM waiting to be notified that the mailbox of the |
| 1483 | * specified VM became writable. Only primary VMs are allowed to call this. |
| 1484 | * |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 1485 | * Returns -1 on failure or if there are no waiters; the VM id of the next |
| 1486 | * waiter otherwise. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1487 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1488 | 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] | 1489 | { |
| 1490 | struct vm *vm; |
| 1491 | struct vm_locked locked; |
| 1492 | struct wait_entry *entry; |
| 1493 | struct vm *waiting_vm; |
| 1494 | |
| 1495 | /* Only primary VMs are allowed to call this function. */ |
| 1496 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1500 | vm = vm_find(vm_id); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1501 | if (vm == NULL) { |
| 1502 | return -1; |
| 1503 | } |
| 1504 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1505 | /* Check if there are outstanding notifications from given VM. */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1506 | locked = vm_lock(vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1507 | entry = api_fetch_waiter(locked); |
| 1508 | vm_unlock(&locked); |
| 1509 | |
| 1510 | if (entry == NULL) { |
| 1511 | return -1; |
| 1512 | } |
| 1513 | |
| 1514 | /* Enqueue notification to waiting VM. */ |
| 1515 | waiting_vm = entry->waiting_vm; |
| 1516 | |
| 1517 | sl_lock(&waiting_vm->lock); |
| 1518 | if (list_empty(&entry->ready_links)) { |
| 1519 | list_append(&waiting_vm->mailbox.ready_list, |
| 1520 | &entry->ready_links); |
| 1521 | } |
| 1522 | sl_unlock(&waiting_vm->lock); |
| 1523 | |
| 1524 | return waiting_vm->id; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1528 | * Releases the caller's mailbox so that a new message can be received. The |
| 1529 | * caller must have copied out all data they wish to preserve as new messages |
| 1530 | * will overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1531 | * |
| 1532 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1533 | * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read. |
| 1534 | * - FFA_SUCCESS on success if no further action is needed. |
| 1535 | * - 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] | 1536 | * 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] | 1537 | * hf_mailbox_waiter_get. |
| 1538 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1539 | struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1540 | { |
| 1541 | struct vm *vm = current->vm; |
| 1542 | struct vm_locked locked; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1543 | struct ffa_value ret; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1544 | |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1545 | locked = vm_lock(vm); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1546 | switch (vm->mailbox.state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1547 | case MAILBOX_STATE_EMPTY: |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1548 | case MAILBOX_STATE_RECEIVED: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1549 | ret = ffa_error(FFA_DENIED); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1552 | case MAILBOX_STATE_READ: |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1553 | ret = api_waiter_result(locked, current, next); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1554 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1555 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1556 | } |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1557 | vm_unlock(&locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1558 | |
| 1559 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1560 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1561 | |
| 1562 | /** |
| 1563 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 1564 | * |
| 1565 | * Returns 0 on success, or -1 if the intid is invalid. |
| 1566 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1567 | int64_t api_interrupt_enable(uint32_t intid, bool enable, |
| 1568 | enum interrupt_type type, struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1569 | { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1570 | struct vcpu_locked current_locked; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1571 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1572 | uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS; |
| 1573 | uint32_t intid_mask = 1U << intid_shift; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1574 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1575 | if (intid >= HF_NUM_INTIDS) { |
| 1576 | return -1; |
| 1577 | } |
| 1578 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1579 | current_locked = vcpu_lock(current); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1580 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1581 | /* |
| 1582 | * If it is pending and was not enabled before, increment the |
| 1583 | * count. |
| 1584 | */ |
| 1585 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1586 | ~current->interrupts.interrupt_enabled[intid_index] & |
| 1587 | intid_mask) { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1588 | if ((current->interrupts.interrupt_type[intid_index] & |
| 1589 | intid_mask) == |
| 1590 | (INTERRUPT_TYPE_IRQ << intid_shift)) { |
| 1591 | vcpu_irq_count_increment(current_locked); |
| 1592 | } else { |
| 1593 | vcpu_fiq_count_increment(current_locked); |
| 1594 | } |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1595 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1596 | current->interrupts.interrupt_enabled[intid_index] |= |
| 1597 | intid_mask; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (type == INTERRUPT_TYPE_IRQ) { |
| 1600 | current->interrupts.interrupt_type[intid_index] &= |
| 1601 | ~intid_mask; |
| 1602 | } else if (type == INTERRUPT_TYPE_FIQ) { |
| 1603 | current->interrupts.interrupt_type[intid_index] |= |
| 1604 | intid_mask; |
| 1605 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1606 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1607 | /* |
| 1608 | * If it is pending and was enabled before, decrement the count. |
| 1609 | */ |
| 1610 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1611 | current->interrupts.interrupt_enabled[intid_index] & |
| 1612 | intid_mask) { |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1613 | if ((current->interrupts.interrupt_type[intid_index] & |
| 1614 | intid_mask) == |
| 1615 | (INTERRUPT_TYPE_IRQ << intid_shift)) { |
| 1616 | vcpu_irq_count_decrement(current_locked); |
| 1617 | } else { |
| 1618 | vcpu_fiq_count_decrement(current_locked); |
| 1619 | } |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1620 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1621 | current->interrupts.interrupt_enabled[intid_index] &= |
| 1622 | ~intid_mask; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1623 | current->interrupts.interrupt_type[intid_index] &= ~intid_mask; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1626 | vcpu_unlock(¤t_locked); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 1632 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 1633 | * HF_INVALID_INTID if there are no pending interrupts. |
| 1634 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1635 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1636 | { |
| 1637 | uint8_t i; |
| 1638 | uint32_t first_interrupt = HF_INVALID_INTID; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1639 | struct vcpu_locked current_locked; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* |
| 1642 | * Find the first enabled and pending interrupt ID, return it, and |
| 1643 | * deactivate it. |
| 1644 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1645 | current_locked = vcpu_lock(current); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1646 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 1647 | uint32_t enabled_and_pending = |
| 1648 | current->interrupts.interrupt_enabled[i] & |
| 1649 | current->interrupts.interrupt_pending[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1650 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1651 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1652 | uint8_t bit_index = ctz(enabled_and_pending); |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1653 | uint32_t intid_mask = 1U << bit_index; |
| 1654 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1655 | /* |
| 1656 | * Mark it as no longer pending and decrement the count. |
| 1657 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1658 | current->interrupts.interrupt_pending[i] &= ~intid_mask; |
| 1659 | |
| 1660 | if ((current->interrupts.interrupt_type[i] & |
| 1661 | intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) { |
| 1662 | vcpu_irq_count_decrement(current_locked); |
| 1663 | } else { |
| 1664 | vcpu_fiq_count_decrement(current_locked); |
| 1665 | } |
| 1666 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1667 | first_interrupt = |
| 1668 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | } |
| 1671 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1672 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1673 | vcpu_unlock(¤t_locked); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1674 | return first_interrupt; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 1678 | * 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] | 1679 | * given VM and vCPU. |
| 1680 | */ |
| 1681 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 1682 | struct vcpu *current) |
| 1683 | { |
| 1684 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1685 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1686 | /* |
| 1687 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 1688 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 1689 | */ |
| 1690 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 1691 | current_vm_id == target_vm_id; |
| 1692 | } |
| 1693 | |
| 1694 | /** |
| 1695 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 1696 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 1697 | * when the vCPU is next run, which is up to the scheduler. |
| 1698 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1699 | * Returns: |
| 1700 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 1701 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 1702 | * the target VM. |
| 1703 | * - 0 on success if no further action is needed. |
| 1704 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 1705 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1706 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1707 | int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id, |
| 1708 | ffa_vcpu_index_t target_vcpu_idx, uint32_t intid, |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1709 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1710 | { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1711 | struct vcpu *target_vcpu; |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1712 | struct vm *target_vm = vm_find(target_vm_id); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1713 | |
| 1714 | if (intid >= HF_NUM_INTIDS) { |
| 1715 | return -1; |
| 1716 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1717 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1718 | if (target_vm == NULL) { |
| 1719 | return -1; |
| 1720 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1721 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1722 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1723 | /* The requested vCPU must exist. */ |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1724 | return -1; |
| 1725 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1726 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1727 | if (!is_injection_allowed(target_vm_id, current)) { |
| 1728 | return -1; |
| 1729 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1730 | |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 1731 | target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1732 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 1733 | dlog_verbose( |
| 1734 | "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU " |
| 1735 | "%u\n", |
| 1736 | intid, target_vm_id, target_vcpu_idx, current->vm->id, |
| 1737 | vcpu_index(current)); |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 1738 | return internal_interrupt_inject(target_vcpu, intid, current, next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1739 | } |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1740 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1741 | /** Returns the version of the implemented FF-A specification. */ |
| 1742 | struct ffa_value api_ffa_version(uint32_t requested_version) |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1743 | { |
| 1744 | /* |
| 1745 | * Ensure that both major and minor revision representation occupies at |
| 1746 | * most 15 bits. |
| 1747 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1748 | static_assert(0x8000 > FFA_VERSION_MAJOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1749 | "Major revision representation takes more than 15 bits."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1750 | static_assert(0x10000 > FFA_VERSION_MINOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1751 | "Minor revision representation takes more than 16 bits."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1752 | if (requested_version & FFA_VERSION_RESERVED_BIT) { |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1753 | /* Invalid encoding, return an error. */ |
J-Alves | 13318e3 | 2021-02-22 17:21:00 +0000 | [diff] [blame] | 1754 | return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED}; |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1755 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1756 | |
Daniel Boulby | 6e32c61 | 2021-02-17 15:09:41 +0000 | [diff] [blame] | 1757 | return ((struct ffa_value){.func = FFA_VERSION_COMPILED}); |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1758 | } |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1759 | |
| 1760 | int64_t api_debug_log(char c, struct vcpu *current) |
| 1761 | { |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1762 | bool flush; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1763 | struct vm *vm = current->vm; |
| 1764 | struct vm_locked vm_locked = vm_lock(vm); |
| 1765 | |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1766 | if (c == '\n' || c == '\0') { |
| 1767 | flush = true; |
| 1768 | } else { |
| 1769 | vm->log_buffer[vm->log_buffer_length++] = c; |
| 1770 | flush = (vm->log_buffer_length == sizeof(vm->log_buffer)); |
| 1771 | } |
| 1772 | |
| 1773 | if (flush) { |
Andrew Walbran | 7f904bf | 2019-07-12 16:38:38 +0100 | [diff] [blame] | 1774 | dlog_flush_vm_buffer(vm->id, vm->log_buffer, |
| 1775 | vm->log_buffer_length); |
| 1776 | vm->log_buffer_length = 0; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | vm_unlock(&vm_locked); |
| 1780 | |
| 1781 | return 0; |
| 1782 | } |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1783 | |
| 1784 | /** |
| 1785 | * Discovery function returning information about the implementation of optional |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1786 | * FF-A interfaces. |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1787 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1788 | struct ffa_value api_ffa_features(uint32_t function_id) |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1789 | { |
| 1790 | switch (function_id) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1791 | case FFA_ERROR_32: |
| 1792 | case FFA_SUCCESS_32: |
| 1793 | case FFA_INTERRUPT_32: |
| 1794 | case FFA_VERSION_32: |
| 1795 | case FFA_FEATURES_32: |
| 1796 | case FFA_RX_RELEASE_32: |
| 1797 | case FFA_RXTX_MAP_64: |
Daniel Boulby | 9e420ca | 2021-07-07 15:03:49 +0100 | [diff] [blame] | 1798 | case FFA_RXTX_UNMAP_32: |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 1799 | case FFA_PARTITION_INFO_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1800 | case FFA_ID_GET_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1801 | case FFA_MSG_WAIT_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1802 | case FFA_RUN_32: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1803 | case FFA_MEM_DONATE_32: |
| 1804 | case FFA_MEM_LEND_32: |
| 1805 | case FFA_MEM_SHARE_32: |
| 1806 | case FFA_MEM_RETRIEVE_REQ_32: |
| 1807 | case FFA_MEM_RETRIEVE_RESP_32: |
| 1808 | case FFA_MEM_RELINQUISH_32: |
| 1809 | case FFA_MEM_RECLAIM_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 1810 | case FFA_MSG_SEND_DIRECT_RESP_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1811 | case FFA_MSG_SEND_DIRECT_RESP_32: |
J-Alves | bc3de8b | 2020-12-07 14:32:04 +0000 | [diff] [blame] | 1812 | case FFA_MSG_SEND_DIRECT_REQ_64: |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1813 | case FFA_MSG_SEND_DIRECT_REQ_32: |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 1814 | #if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED) |
Daniel Boulby | b2fb80e | 2021-02-03 15:09:23 +0000 | [diff] [blame] | 1815 | /* FF-A v1.1 features. */ |
| 1816 | case FFA_SPM_ID_GET_32: |
J-Alves | 3829fc0 | 2021-03-18 12:49:18 +0000 | [diff] [blame] | 1817 | #endif |
| 1818 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1819 | default: |
Daniel Boulby | 87b2dc8 | 2021-08-04 14:07:43 +0100 | [diff] [blame] | 1820 | return arch_ffa_features(function_id); |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1821 | } |
| 1822 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1823 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1824 | /** |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 1825 | * FF-A specification states that x2/w2 Must Be Zero for direct messaging |
| 1826 | * interfaces. |
| 1827 | */ |
| 1828 | static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args) |
| 1829 | { |
| 1830 | return args.arg2 == 0U; |
| 1831 | } |
| 1832 | |
| 1833 | /** |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 1834 | * Limits size of arguments in ffa_value structure to 32-bit. |
| 1835 | */ |
| 1836 | static struct ffa_value api_ffa_value_copy32(struct ffa_value args) |
| 1837 | { |
| 1838 | return (struct ffa_value){ |
| 1839 | .func = (uint32_t)args.func, |
| 1840 | .arg1 = (uint32_t)args.arg1, |
| 1841 | .arg2 = (uint32_t)0, |
| 1842 | .arg3 = (uint32_t)args.arg3, |
| 1843 | .arg4 = (uint32_t)args.arg4, |
| 1844 | .arg5 = (uint32_t)args.arg5, |
| 1845 | .arg6 = (uint32_t)args.arg6, |
| 1846 | .arg7 = (uint32_t)args.arg7, |
| 1847 | }; |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * Helper to copy direct message payload, depending on SMC used and expected |
| 1852 | * registers size. |
| 1853 | */ |
| 1854 | static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args) |
| 1855 | { |
| 1856 | if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 || |
| 1857 | args.func == FFA_MSG_SEND_DIRECT_RESP_32) { |
| 1858 | return api_ffa_value_copy32(args); |
| 1859 | } |
| 1860 | |
| 1861 | return (struct ffa_value){ |
| 1862 | .func = args.func, |
| 1863 | .arg1 = args.arg1, |
| 1864 | .arg2 = 0, |
| 1865 | .arg3 = args.arg3, |
| 1866 | .arg4 = args.arg4, |
| 1867 | .arg5 = args.arg5, |
| 1868 | .arg6 = args.arg6, |
| 1869 | .arg7 = args.arg7, |
| 1870 | }; |
| 1871 | } |
| 1872 | |
| 1873 | /** |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1874 | * Send an FF-A direct message request. |
| 1875 | */ |
| 1876 | struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id, |
| 1877 | ffa_vm_id_t receiver_vm_id, |
| 1878 | struct ffa_value args, |
| 1879 | struct vcpu *current, |
| 1880 | struct vcpu **next) |
| 1881 | { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 1882 | struct ffa_value ret; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1883 | struct vm *receiver_vm; |
| 1884 | struct vcpu *receiver_vcpu; |
| 1885 | struct two_vcpu_locked vcpus_locked; |
| 1886 | |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 1887 | if (!api_ffa_dir_msg_is_arg2_zero(args)) { |
| 1888 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1889 | } |
| 1890 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 1891 | if (!plat_ffa_is_direct_request_valid(current, sender_vm_id, |
| 1892 | receiver_vm_id)) { |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 1893 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 1896 | if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) { |
J-Alves | 17228f7 | 2021-04-20 17:13:19 +0100 | [diff] [blame] | 1897 | return ret; |
| 1898 | } |
| 1899 | |
| 1900 | ret = (struct ffa_value){.func = FFA_INTERRUPT_32}; |
| 1901 | |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1902 | receiver_vm = vm_find(receiver_vm_id); |
| 1903 | if (receiver_vm == NULL) { |
| 1904 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports |
| 1909 | * UP (migratable) or MP partitions with a number of vCPUs matching the |
| 1910 | * number of PEs in the system. It further states that MP partitions |
| 1911 | * accepting direct request messages cannot migrate. |
| 1912 | */ |
J-Alves | ad6a043 | 2021-04-09 16:06:21 +0100 | [diff] [blame] | 1913 | receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1914 | if (receiver_vcpu == NULL) { |
| 1915 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1916 | } |
| 1917 | |
| 1918 | vcpus_locked = vcpu_lock_both(receiver_vcpu, current); |
| 1919 | |
| 1920 | /* |
| 1921 | * If destination vCPU is executing or already received an |
| 1922 | * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is |
| 1923 | * busy. There is a brief period of time where the vCPU state has |
| 1924 | * changed but regs_available is still false thus consider this case as |
| 1925 | * the vCPU not yet ready to receive a direct message request. |
| 1926 | */ |
| 1927 | if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) || |
| 1928 | receiver_vcpu->state == VCPU_STATE_RUNNING || |
| 1929 | !receiver_vcpu->regs_available) { |
| 1930 | ret = ffa_error(FFA_BUSY); |
| 1931 | goto out; |
| 1932 | } |
| 1933 | |
| 1934 | if (atomic_load_explicit(&receiver_vcpu->vm->aborting, |
| 1935 | memory_order_relaxed)) { |
| 1936 | if (receiver_vcpu->state != VCPU_STATE_ABORTED) { |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 1937 | dlog_notice("Aborting VM %#x vCPU %u\n", |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1938 | receiver_vcpu->vm->id, |
| 1939 | vcpu_index(receiver_vcpu)); |
| 1940 | receiver_vcpu->state = VCPU_STATE_ABORTED; |
| 1941 | } |
| 1942 | |
| 1943 | ret = ffa_error(FFA_ABORTED); |
| 1944 | goto out; |
| 1945 | } |
| 1946 | |
| 1947 | switch (receiver_vcpu->state) { |
| 1948 | case VCPU_STATE_OFF: |
| 1949 | case VCPU_STATE_RUNNING: |
| 1950 | case VCPU_STATE_ABORTED: |
| 1951 | case VCPU_STATE_READY: |
| 1952 | case VCPU_STATE_BLOCKED_INTERRUPT: |
| 1953 | ret = ffa_error(FFA_BUSY); |
| 1954 | goto out; |
| 1955 | case VCPU_STATE_BLOCKED_MAILBOX: |
| 1956 | /* |
| 1957 | * Expect target vCPU to be blocked after having called |
| 1958 | * ffa_msg_wait or sent a direct message response. |
| 1959 | */ |
| 1960 | break; |
| 1961 | } |
| 1962 | |
| 1963 | /* Inject timer interrupt if any pending */ |
| 1964 | if (arch_timer_pending(&receiver_vcpu->regs)) { |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 1965 | api_interrupt_inject_locked(vcpus_locked.vcpu1, |
| 1966 | HF_VIRTUAL_TIMER_INTID, current, |
| 1967 | NULL); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1968 | |
| 1969 | arch_timer_mask(&receiver_vcpu->regs); |
| 1970 | } |
| 1971 | |
| 1972 | /* The receiver vCPU runs upon direct message invocation */ |
| 1973 | receiver_vcpu->cpu = current->cpu; |
| 1974 | receiver_vcpu->state = VCPU_STATE_RUNNING; |
| 1975 | receiver_vcpu->regs_available = false; |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 1976 | receiver_vcpu->direct_request_origin_vm_id = sender_vm_id; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 1977 | |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 1978 | 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] | 1979 | |
| 1980 | current->state = VCPU_STATE_BLOCKED_MAILBOX; |
| 1981 | |
| 1982 | /* Switch to receiver vCPU targeted to by direct msg request */ |
| 1983 | *next = receiver_vcpu; |
| 1984 | |
| 1985 | /* |
| 1986 | * Since this flow will lead to a VM switch, the return value will not |
| 1987 | * be applied to current vCPU. |
| 1988 | */ |
| 1989 | |
| 1990 | out: |
| 1991 | sl_unlock(&receiver_vcpu->lock); |
| 1992 | sl_unlock(¤t->lock); |
| 1993 | |
| 1994 | return ret; |
| 1995 | } |
| 1996 | |
| 1997 | /** |
| 1998 | * Send an FF-A direct message response. |
| 1999 | */ |
| 2000 | struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id, |
| 2001 | ffa_vm_id_t receiver_vm_id, |
| 2002 | struct ffa_value args, |
| 2003 | struct vcpu *current, |
| 2004 | struct vcpu **next) |
| 2005 | { |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2006 | struct vcpu_locked current_locked; |
J-Alves | 645eabe | 2021-02-22 16:08:27 +0000 | [diff] [blame] | 2007 | |
| 2008 | if (!api_ffa_dir_msg_is_arg2_zero(args)) { |
| 2009 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2010 | } |
| 2011 | |
J-Alves | 76d99af | 2021-03-10 17:42:11 +0000 | [diff] [blame] | 2012 | struct ffa_value to_ret = api_ffa_dir_msg_value(args); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2013 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2014 | if (!plat_ffa_is_direct_response_valid(current, sender_vm_id, |
| 2015 | receiver_vm_id)) { |
J-Alves | aa33610 | 2021-03-01 13:02:45 +0000 | [diff] [blame] | 2016 | return ffa_error(FFA_INVALID_PARAMETERS); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2019 | current_locked = vcpu_lock(current); |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2020 | if (api_ffa_is_managed_exit_ongoing(current_locked)) { |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2021 | /* |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2022 | * No need for REQ/RESP state management as managed exit does |
| 2023 | * not have corresponding REQ pair. |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2024 | */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2025 | if (receiver_vm_id != HF_PRIMARY_VM_ID) { |
| 2026 | vcpu_unlock(¤t_locked); |
| 2027 | return ffa_error(FFA_DENIED); |
| 2028 | } |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2029 | |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 2030 | plat_interrupts_set_priority_mask(0xff); |
| 2031 | current->processing_managed_exit = false; |
| 2032 | } else { |
| 2033 | /* |
| 2034 | * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a |
| 2035 | * defined originator. |
| 2036 | */ |
| 2037 | if (!is_ffa_direct_msg_request_ongoing(current_locked)) { |
| 2038 | /* |
| 2039 | * Sending direct response but direct request origin |
| 2040 | * vCPU is not set. |
| 2041 | */ |
| 2042 | vcpu_unlock(¤t_locked); |
| 2043 | return ffa_error(FFA_DENIED); |
| 2044 | } |
| 2045 | |
| 2046 | if (current->direct_request_origin_vm_id != receiver_vm_id) { |
| 2047 | vcpu_unlock(¤t_locked); |
| 2048 | return ffa_error(FFA_DENIED); |
| 2049 | } |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* Clear direct request origin for the caller. */ |
| 2053 | current->direct_request_origin_vm_id = HF_INVALID_VM_ID; |
| 2054 | |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2055 | vcpu_unlock(¤t_locked); |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2056 | |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2057 | if (!vm_id_is_current_world(receiver_vm_id)) { |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 2058 | *next = api_switch_to_other_world(current, to_ret, |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2059 | VCPU_STATE_BLOCKED_MAILBOX); |
| 2060 | } else if (receiver_vm_id == HF_PRIMARY_VM_ID) { |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 2061 | *next = api_switch_to_primary(current, to_ret, |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2062 | VCPU_STATE_BLOCKED_MAILBOX); |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 2063 | } else if (vm_id_is_current_world(receiver_vm_id)) { |
| 2064 | /* |
| 2065 | * It is expected the receiver_vm_id to be from an SP, otherwise |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 2066 | * 'plat_ffa_is_direct_response_valid' should have |
J-Alves | fe7f737 | 2020-11-09 11:32:12 +0000 | [diff] [blame] | 2067 | * made function return error before getting to this point. |
| 2068 | */ |
| 2069 | *next = api_switch_to_vm(current, to_ret, |
| 2070 | VCPU_STATE_BLOCKED_MAILBOX, |
| 2071 | receiver_vm_id); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 2072 | } else { |
| 2073 | panic("Invalid direct message response invocation"); |
| 2074 | } |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 2075 | |
| 2076 | return (struct ffa_value){.func = FFA_INTERRUPT_32}; |
| 2077 | } |
| 2078 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 2079 | static bool api_memory_region_check_flags( |
| 2080 | struct ffa_memory_region *memory_region, uint32_t share_func) |
| 2081 | { |
| 2082 | switch (share_func) { |
| 2083 | case FFA_MEM_SHARE_32: |
| 2084 | if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != |
| 2085 | 0U) { |
| 2086 | return false; |
| 2087 | } |
| 2088 | /* Intentional fall-through */ |
| 2089 | case FFA_MEM_LEND_32: |
| 2090 | case FFA_MEM_DONATE_32: { |
| 2091 | /* Bits 31:2 Must Be Zero. */ |
| 2092 | ffa_memory_receiver_flags_t to_mask = |
| 2093 | ~(FFA_MEMORY_REGION_FLAG_CLEAR | |
| 2094 | FFA_MEMORY_REGION_FLAG_TIME_SLICE); |
| 2095 | |
| 2096 | if ((memory_region->flags & to_mask) != 0U) { |
| 2097 | return false; |
| 2098 | } |
| 2099 | break; |
| 2100 | } |
| 2101 | default: |
| 2102 | panic("Check for mem send calls only.\n"); |
| 2103 | } |
| 2104 | |
| 2105 | /* Last check reserved values are 0 */ |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2109 | struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length, |
| 2110 | uint32_t fragment_length, ipaddr_t address, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2111 | uint32_t page_count, struct vcpu *current) |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2112 | { |
| 2113 | struct vm *from = current->vm; |
| 2114 | struct vm *to; |
| 2115 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2116 | struct ffa_memory_region *memory_region; |
| 2117 | struct ffa_value ret; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2118 | |
| 2119 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 2120 | /* |
| 2121 | * Hafnium only supports passing the descriptor in the TX |
| 2122 | * mailbox. |
| 2123 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2124 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2127 | if (fragment_length > length) { |
| 2128 | dlog_verbose( |
| 2129 | "Fragment length %d greater than total length %d.\n", |
| 2130 | fragment_length, length); |
| 2131 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2132 | } |
| 2133 | if (fragment_length < sizeof(struct ffa_memory_region) + |
| 2134 | sizeof(struct ffa_memory_access)) { |
| 2135 | dlog_verbose( |
| 2136 | "Initial fragment length %d smaller than header size " |
| 2137 | "%d.\n", |
| 2138 | fragment_length, |
| 2139 | sizeof(struct ffa_memory_region) + |
| 2140 | sizeof(struct ffa_memory_access)); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2141 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * Check that the sender has configured its send buffer. If the TX |
| 2146 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 2147 | * be safely accessed after releasing the lock since the TX mailbox |
| 2148 | * address can only be configured once. |
| 2149 | */ |
| 2150 | sl_lock(&from->lock); |
| 2151 | from_msg = from->mailbox.send; |
| 2152 | sl_unlock(&from->lock); |
| 2153 | |
| 2154 | if (from_msg == NULL) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2155 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2159 | * Copy the memory region descriptor to a fresh page from the memory |
| 2160 | * pool. This prevents the sender from changing it underneath us, and |
| 2161 | * 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] | 2162 | */ |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2163 | if (fragment_length > HF_MAILBOX_SIZE || |
| 2164 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2165 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2166 | } |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2167 | memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2168 | if (memory_region == NULL) { |
| 2169 | dlog_verbose("Failed to allocate memory region copy.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2170 | return ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2171 | } |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2172 | memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2173 | |
| 2174 | /* The sender must match the caller. */ |
| 2175 | if (memory_region->sender != from->id) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2176 | dlog_verbose("Memory region sender doesn't match caller.\n"); |
J-Alves | 9994866 | 2021-07-28 18:07:04 +0100 | [diff] [blame] | 2177 | ret = ffa_error(FFA_DENIED); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2178 | goto out; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
J-Alves | 84658fc | 2021-06-17 14:37:32 +0100 | [diff] [blame] | 2181 | if (!api_memory_region_check_flags(memory_region, share_func)) { |
| 2182 | dlog_verbose( |
| 2183 | "Memory region reserved arguments must be zero.\n"); |
| 2184 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2185 | goto out; |
| 2186 | } |
| 2187 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2188 | if (memory_region->receiver_count != 1) { |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2189 | /* Hafnium doesn't support multi-way memory sharing for now. */ |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2190 | dlog_verbose( |
| 2191 | "Multi-way memory sharing not supported (got %d " |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2192 | "endpoint memory access descriptors, expected 1).\n", |
| 2193 | memory_region->receiver_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2194 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2195 | goto out; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /* |
| 2199 | * Ensure that the receiver VM exists and isn't the same as the sender. |
| 2200 | */ |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2201 | to = vm_find(memory_region->receivers[0].receiver_permissions.receiver); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2202 | if (to == NULL || to == from) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2203 | dlog_verbose("Invalid receiver.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2204 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2205 | goto out; |
| 2206 | } |
| 2207 | |
| 2208 | if (to->id == HF_TEE_VM_ID) { |
| 2209 | /* |
| 2210 | * The 'to' VM lock is only needed in the case that it is the |
| 2211 | * TEE VM. |
| 2212 | */ |
| 2213 | struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from); |
| 2214 | |
| 2215 | if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2216 | ret = ffa_error(FFA_BUSY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2217 | goto out_unlock; |
| 2218 | } |
| 2219 | |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2220 | ret = ffa_memory_tee_send( |
| 2221 | vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region, |
| 2222 | length, fragment_length, share_func, &api_page_pool); |
| 2223 | /* |
| 2224 | * ffa_tee_memory_send takes ownership of the memory_region, so |
| 2225 | * make sure we don't free it. |
| 2226 | */ |
| 2227 | memory_region = NULL; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2228 | |
| 2229 | out_unlock: |
| 2230 | vm_unlock(&vm_to_from_lock.vm1); |
| 2231 | vm_unlock(&vm_to_from_lock.vm2); |
| 2232 | } else { |
| 2233 | struct vm_locked from_locked = vm_lock(from); |
| 2234 | |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2235 | ret = ffa_memory_send(from_locked, memory_region, length, |
| 2236 | fragment_length, share_func, |
| 2237 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2238 | /* |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2239 | * ffa_memory_send takes ownership of the memory_region, so |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2240 | * make sure we don't free it. |
| 2241 | */ |
| 2242 | memory_region = NULL; |
| 2243 | |
| 2244 | vm_unlock(&from_locked); |
| 2245 | } |
| 2246 | |
| 2247 | out: |
| 2248 | if (memory_region != NULL) { |
| 2249 | mpool_free(&api_page_pool, memory_region); |
| 2250 | } |
| 2251 | |
| 2252 | return ret; |
| 2253 | } |
| 2254 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2255 | struct ffa_value api_ffa_mem_retrieve_req(uint32_t length, |
| 2256 | uint32_t fragment_length, |
| 2257 | ipaddr_t address, uint32_t page_count, |
| 2258 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2259 | { |
| 2260 | struct vm *to = current->vm; |
| 2261 | struct vm_locked to_locked; |
| 2262 | const void *to_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2263 | struct ffa_memory_region *retrieve_request; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2264 | uint32_t message_buffer_size; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2265 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 2268 | /* |
| 2269 | * Hafnium only supports passing the descriptor in the TX |
| 2270 | * mailbox. |
| 2271 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2272 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2275 | if (fragment_length != length) { |
| 2276 | dlog_verbose("Fragmentation not yet supported.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2277 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2278 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2279 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2280 | retrieve_request = |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2281 | (struct ffa_memory_region *)cpu_get_buffer(current->cpu); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2282 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 2283 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 2284 | dlog_verbose("Retrieve request too long.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2285 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | to_locked = vm_lock(to); |
| 2289 | to_msg = to->mailbox.send; |
| 2290 | |
| 2291 | if (to_msg == NULL) { |
| 2292 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2293 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2294 | goto out; |
| 2295 | } |
| 2296 | |
| 2297 | /* |
| 2298 | * Copy the retrieve request descriptor to an internal buffer, so that |
| 2299 | * the caller can't change it underneath us. |
| 2300 | */ |
| 2301 | memcpy_s(retrieve_request, message_buffer_size, to_msg, length); |
| 2302 | |
| 2303 | if (msg_receiver_busy(to_locked, NULL, false)) { |
| 2304 | /* |
| 2305 | * Can't retrieve memory information if the mailbox is not |
| 2306 | * available. |
| 2307 | */ |
| 2308 | dlog_verbose("RX buffer not ready.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2309 | ret = ffa_error(FFA_BUSY); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2310 | goto out; |
| 2311 | } |
| 2312 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2313 | ret = ffa_memory_retrieve(to_locked, retrieve_request, length, |
| 2314 | &api_page_pool); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2315 | |
| 2316 | out: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2317 | vm_unlock(&to_locked); |
| 2318 | return ret; |
| 2319 | } |
| 2320 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2321 | struct ffa_value api_ffa_mem_relinquish(struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2322 | { |
| 2323 | struct vm *from = current->vm; |
| 2324 | struct vm_locked from_locked; |
| 2325 | const void *from_msg; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2326 | struct ffa_mem_relinquish *relinquish_request; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2327 | uint32_t message_buffer_size; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2328 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2329 | uint32_t length; |
| 2330 | |
| 2331 | from_locked = vm_lock(from); |
| 2332 | from_msg = from->mailbox.send; |
| 2333 | |
| 2334 | if (from_msg == NULL) { |
| 2335 | dlog_verbose("TX buffer not setup.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2336 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2337 | goto out; |
| 2338 | } |
| 2339 | |
| 2340 | /* |
| 2341 | * Calculate length from relinquish descriptor before copying. We will |
| 2342 | * check again later to make sure it hasn't changed. |
| 2343 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2344 | length = sizeof(struct ffa_mem_relinquish) + |
| 2345 | ((struct ffa_mem_relinquish *)from_msg)->endpoint_count * |
| 2346 | sizeof(ffa_vm_id_t); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2347 | /* |
| 2348 | * Copy the relinquish descriptor to an internal buffer, so that the |
| 2349 | * caller can't change it underneath us. |
| 2350 | */ |
| 2351 | relinquish_request = |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2352 | (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2353 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 2354 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 2355 | dlog_verbose("Relinquish message too long.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2356 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2357 | goto out; |
| 2358 | } |
| 2359 | memcpy_s(relinquish_request, message_buffer_size, from_msg, length); |
| 2360 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2361 | if (sizeof(struct ffa_mem_relinquish) + |
| 2362 | relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) != |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2363 | length) { |
| 2364 | dlog_verbose( |
| 2365 | "Endpoint count changed while copying to internal " |
| 2366 | "buffer.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2367 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2368 | goto out; |
| 2369 | } |
| 2370 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2371 | ret = ffa_memory_relinquish(from_locked, relinquish_request, |
| 2372 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2373 | |
| 2374 | out: |
| 2375 | vm_unlock(&from_locked); |
| 2376 | return ret; |
| 2377 | } |
| 2378 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2379 | struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle, |
| 2380 | ffa_memory_region_flags_t flags, |
| 2381 | struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2382 | { |
| 2383 | struct vm *to = current->vm; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2384 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2385 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 2386 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 2387 | struct vm_locked to_locked = vm_lock(to); |
| 2388 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2389 | ret = ffa_memory_reclaim(to_locked, handle, flags, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2390 | &api_page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2391 | |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 2392 | vm_unlock(&to_locked); |
| 2393 | } else { |
| 2394 | struct vm *from = vm_find(HF_TEE_VM_ID); |
| 2395 | struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from); |
| 2396 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2397 | ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1, |
| 2398 | vm_to_from_lock.vm2, handle, flags, |
| 2399 | &api_page_pool); |
| 2400 | |
| 2401 | vm_unlock(&vm_to_from_lock.vm1); |
| 2402 | vm_unlock(&vm_to_from_lock.vm2); |
| 2403 | } |
| 2404 | |
| 2405 | return ret; |
| 2406 | } |
| 2407 | |
| 2408 | struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle, |
| 2409 | uint32_t fragment_offset, |
| 2410 | ffa_vm_id_t sender_vm_id, |
| 2411 | struct vcpu *current) |
| 2412 | { |
| 2413 | struct vm *to = current->vm; |
| 2414 | struct vm_locked to_locked; |
| 2415 | struct ffa_value ret; |
| 2416 | |
| 2417 | /* Sender ID MBZ at virtual instance. */ |
| 2418 | if (sender_vm_id != 0) { |
| 2419 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2420 | } |
| 2421 | |
| 2422 | to_locked = vm_lock(to); |
| 2423 | |
| 2424 | if (msg_receiver_busy(to_locked, NULL, false)) { |
| 2425 | /* |
| 2426 | * Can't retrieve memory information if the mailbox is not |
| 2427 | * available. |
| 2428 | */ |
| 2429 | dlog_verbose("RX buffer not ready.\n"); |
| 2430 | ret = ffa_error(FFA_BUSY); |
| 2431 | goto out; |
| 2432 | } |
| 2433 | |
| 2434 | ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset, |
| 2435 | &api_page_pool); |
| 2436 | |
| 2437 | out: |
| 2438 | vm_unlock(&to_locked); |
| 2439 | return ret; |
| 2440 | } |
| 2441 | |
| 2442 | struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle, |
| 2443 | uint32_t fragment_length, |
| 2444 | ffa_vm_id_t sender_vm_id, |
| 2445 | struct vcpu *current) |
| 2446 | { |
| 2447 | struct vm *from = current->vm; |
| 2448 | const void *from_msg; |
| 2449 | void *fragment_copy; |
| 2450 | struct ffa_value ret; |
| 2451 | |
| 2452 | /* Sender ID MBZ at virtual instance. */ |
| 2453 | if (sender_vm_id != 0) { |
| 2454 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | * Check that the sender has configured its send buffer. If the TX |
| 2459 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 2460 | * be safely accessed after releasing the lock since the TX mailbox |
| 2461 | * address can only be configured once. |
| 2462 | */ |
| 2463 | sl_lock(&from->lock); |
| 2464 | from_msg = from->mailbox.send; |
| 2465 | sl_unlock(&from->lock); |
| 2466 | |
| 2467 | if (from_msg == NULL) { |
| 2468 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * Copy the fragment to a fresh page from the memory pool. This prevents |
| 2473 | * the sender from changing it underneath us, and also lets us keep it |
| 2474 | * around in the share state table if needed. |
| 2475 | */ |
| 2476 | if (fragment_length > HF_MAILBOX_SIZE || |
| 2477 | fragment_length > MM_PPOOL_ENTRY_SIZE) { |
| 2478 | dlog_verbose( |
| 2479 | "Fragment length %d larger than mailbox size %d.\n", |
| 2480 | fragment_length, HF_MAILBOX_SIZE); |
| 2481 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2482 | } |
| 2483 | if (fragment_length < sizeof(struct ffa_memory_region_constituent) || |
| 2484 | fragment_length % sizeof(struct ffa_memory_region_constituent) != |
| 2485 | 0) { |
| 2486 | dlog_verbose("Invalid fragment length %d.\n", fragment_length); |
| 2487 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2488 | } |
| 2489 | fragment_copy = mpool_alloc(&api_page_pool); |
| 2490 | if (fragment_copy == NULL) { |
| 2491 | dlog_verbose("Failed to allocate fragment copy.\n"); |
| 2492 | return ffa_error(FFA_NO_MEMORY); |
| 2493 | } |
| 2494 | memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length); |
| 2495 | |
| 2496 | /* |
| 2497 | * Hafnium doesn't support fragmentation of memory retrieve requests |
| 2498 | * (because it doesn't support caller-specified mappings, so a request |
| 2499 | * will never be larger than a single page), so this must be part of a |
| 2500 | * memory send (i.e. donate, lend or share) request. |
| 2501 | * |
| 2502 | * We can tell from the handle whether the memory transaction is for the |
| 2503 | * TEE or not. |
| 2504 | */ |
| 2505 | if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) == |
| 2506 | FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) { |
| 2507 | struct vm_locked from_locked = vm_lock(from); |
| 2508 | |
| 2509 | ret = ffa_memory_send_continue(from_locked, fragment_copy, |
| 2510 | fragment_length, handle, |
| 2511 | &api_page_pool); |
| 2512 | /* |
| 2513 | * `ffa_memory_send_continue` takes ownership of the |
| 2514 | * fragment_copy, so we don't need to free it here. |
| 2515 | */ |
| 2516 | vm_unlock(&from_locked); |
| 2517 | } else { |
| 2518 | struct vm *to = vm_find(HF_TEE_VM_ID); |
| 2519 | struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from); |
| 2520 | |
| 2521 | /* |
| 2522 | * The TEE RX buffer state is checked in |
| 2523 | * `ffa_memory_tee_send_continue` rather than here, as we need |
| 2524 | * to return `FFA_MEM_FRAG_RX` with the current offset rather |
| 2525 | * than FFA_ERROR FFA_BUSY in case it is busy. |
| 2526 | */ |
| 2527 | |
| 2528 | ret = ffa_memory_tee_send_continue( |
| 2529 | vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy, |
| 2530 | fragment_length, handle, &api_page_pool); |
| 2531 | /* |
| 2532 | * `ffa_memory_tee_send_continue` takes ownership of the |
| 2533 | * fragment_copy, so we don't need to free it here. |
| 2534 | */ |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 2535 | |
| 2536 | vm_unlock(&vm_to_from_lock.vm1); |
| 2537 | vm_unlock(&vm_to_from_lock.vm2); |
| 2538 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 2539 | |
| 2540 | return ret; |
| 2541 | } |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 2542 | |
| 2543 | struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point, |
| 2544 | struct vcpu *current) |
| 2545 | { |
| 2546 | struct vm_locked vm_locked; |
| 2547 | |
| 2548 | vm_locked = vm_lock(current->vm); |
| 2549 | vm_locked.vm->secondary_ep = entry_point; |
| 2550 | vm_unlock(&vm_locked); |
| 2551 | |
| 2552 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2553 | } |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 2554 | |
| 2555 | struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id, |
| 2556 | ffa_vcpu_count_t vcpu_count, |
| 2557 | struct vcpu *current) |
| 2558 | { |
| 2559 | if (!plat_ffa_is_notifications_create_valid(current, vm_id)) { |
| 2560 | dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n", |
| 2561 | vm_id); |
| 2562 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2563 | } |
| 2564 | |
| 2565 | return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count); |
| 2566 | } |
| 2567 | |
| 2568 | struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id, |
| 2569 | struct vcpu *current) |
| 2570 | { |
| 2571 | /* |
| 2572 | * Validity of use of this interface is the same as for bitmap create. |
| 2573 | */ |
| 2574 | if (!plat_ffa_is_notifications_create_valid(current, vm_id)) { |
| 2575 | dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n", |
| 2576 | vm_id); |
| 2577 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2578 | } |
| 2579 | |
| 2580 | return plat_ffa_notifications_bitmap_destroy(vm_id); |
| 2581 | } |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 2582 | |
| 2583 | struct ffa_value api_ffa_notification_update_bindings( |
| 2584 | ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags, |
| 2585 | ffa_notifications_bitmap_t notifications, bool is_bind, |
| 2586 | struct vcpu *current) |
| 2587 | { |
| 2588 | struct ffa_value ret = {.func = FFA_SUCCESS_32}; |
| 2589 | struct vm_locked receiver_locked; |
| 2590 | const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
| 2591 | const ffa_vm_id_t id_to_update = |
| 2592 | is_bind ? sender_vm_id : HF_INVALID_VM_ID; |
| 2593 | const ffa_vm_id_t id_to_validate = |
| 2594 | is_bind ? HF_INVALID_VM_ID : sender_vm_id; |
| 2595 | |
| 2596 | if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id, |
| 2597 | receiver_vm_id)) { |
| 2598 | dlog_verbose("Invalid use of notifications bind interface.\n"); |
| 2599 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2600 | } |
| 2601 | |
J-Alves | b15e940 | 2021-09-08 11:44:42 +0100 | [diff] [blame] | 2602 | if (plat_ffa_notifications_update_bindings_forward( |
| 2603 | receiver_vm_id, sender_vm_id, flags, notifications, is_bind, |
| 2604 | &ret)) { |
| 2605 | dlog_verbose("Forwarding call to other world.\n"); |
| 2606 | return ret; |
| 2607 | } |
| 2608 | |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 2609 | if (notifications == 0U) { |
| 2610 | dlog_verbose("No notifications have been specified.\n"); |
| 2611 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2612 | } |
| 2613 | |
| 2614 | /** |
| 2615 | * This check assumes receiver is the current VM, and has been enforced |
| 2616 | * by 'plat_ffa_is_notifications_bind_valid'. |
| 2617 | */ |
| 2618 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 2619 | |
| 2620 | if (receiver_locked.vm == NULL) { |
| 2621 | dlog_verbose("Receiver doesn't exist!\n"); |
| 2622 | return ffa_error(FFA_DENIED); |
| 2623 | } |
| 2624 | |
| 2625 | if (!vm_are_notifications_enabled(receiver_locked)) { |
| 2626 | dlog_verbose("Notifications are not enabled.\n"); |
| 2627 | ret = ffa_error(FFA_NOT_SUPPORTED); |
| 2628 | goto out; |
| 2629 | } |
| 2630 | |
| 2631 | if (is_bind && vm_id_is_current_world(sender_vm_id) && |
| 2632 | vm_find(sender_vm_id) == NULL) { |
| 2633 | dlog_verbose("Sender VM does not exist!\n"); |
| 2634 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2635 | goto out; |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * Can't bind/unbind notifications if at least one is bound to a |
| 2640 | * different sender. |
| 2641 | */ |
| 2642 | if (!vm_notifications_validate_bound_sender( |
| 2643 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), |
| 2644 | id_to_validate, notifications)) { |
| 2645 | dlog_verbose("Notifications are bound to other sender.\n"); |
| 2646 | ret = ffa_error(FFA_DENIED); |
| 2647 | goto out; |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | * Check if there is a pending notification within those specified in |
| 2652 | * the bitmap. |
| 2653 | */ |
| 2654 | if (vm_are_notifications_pending(receiver_locked, |
| 2655 | plat_ffa_is_vm_id(sender_vm_id), |
| 2656 | notifications)) { |
| 2657 | dlog_verbose("Notifications within '%x' pending.\n", |
| 2658 | notifications); |
| 2659 | ret = ffa_error(FFA_DENIED); |
| 2660 | goto out; |
| 2661 | } |
| 2662 | |
| 2663 | vm_notifications_update_bindings( |
| 2664 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update, |
| 2665 | notifications, is_per_vcpu && is_bind); |
| 2666 | |
| 2667 | out: |
| 2668 | vm_unlock(&receiver_locked); |
| 2669 | return ret; |
| 2670 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 2671 | |
| 2672 | struct ffa_value api_ffa_notification_set( |
| 2673 | ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags, |
| 2674 | ffa_notifications_bitmap_t notifications, struct vcpu *current) |
| 2675 | { |
| 2676 | struct ffa_value ret; |
| 2677 | struct vm_locked receiver_locked; |
| 2678 | |
| 2679 | /* |
| 2680 | * Check if is per-vCPU or global, and extracting vCPU ID according |
| 2681 | * to table 17.19 of the FF-A v1.1 Beta 0 spec. |
| 2682 | */ |
| 2683 | bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U; |
| 2684 | ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16); |
| 2685 | |
| 2686 | /* |
| 2687 | * TODO: cater for the delay_schedule_receiver flag when dealing with |
| 2688 | * schedule receiver interrupt. |
| 2689 | */ |
| 2690 | |
| 2691 | if (!plat_ffa_is_notification_set_valid(current, sender_vm_id, |
| 2692 | receiver_vm_id)) { |
| 2693 | dlog_verbose("Invalid use of notifications set interface.\n"); |
| 2694 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2695 | } |
| 2696 | |
| 2697 | if (notifications == 0U) { |
| 2698 | dlog_verbose("No notifications have been specified.\n"); |
| 2699 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2700 | } |
| 2701 | |
J-Alves | de7bd2f | 2021-09-09 19:54:35 +0100 | [diff] [blame] | 2702 | if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id, |
| 2703 | flags, notifications, &ret)) { |
| 2704 | return ret; |
| 2705 | } |
| 2706 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 2707 | /* |
| 2708 | * This check assumes receiver is the current VM, and has been enforced |
| 2709 | * by 'plat_ffa_is_notification_set_valid'. |
| 2710 | */ |
| 2711 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 2712 | |
| 2713 | if (receiver_locked.vm == NULL) { |
| 2714 | dlog_verbose("Receiver ID is not valid.\n"); |
| 2715 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2716 | } |
| 2717 | |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 2718 | if (!vm_are_notifications_enabled(receiver_locked)) { |
| 2719 | dlog_verbose("Receiver's notifications not enabled.\n"); |
| 2720 | ret = ffa_error(FFA_DENIED); |
| 2721 | goto out; |
| 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * If notifications are not bound to the sender, they wouldn't be |
| 2726 | * enabled either for the receiver. |
| 2727 | */ |
| 2728 | if (!vm_notifications_validate_binding( |
| 2729 | receiver_locked, plat_ffa_is_vm_id(sender_vm_id), |
| 2730 | sender_vm_id, notifications, is_per_vcpu)) { |
| 2731 | dlog_verbose("Notifications bindings not valid.\n"); |
| 2732 | ret = ffa_error(FFA_DENIED); |
| 2733 | goto out; |
| 2734 | } |
| 2735 | |
| 2736 | if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) { |
| 2737 | dlog_verbose("Invalid VCPU ID!\n"); |
| 2738 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2739 | goto out; |
| 2740 | } |
| 2741 | |
| 2742 | /* Set notifications pending */ |
| 2743 | vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id), |
| 2744 | notifications, vcpu_id, is_per_vcpu); |
| 2745 | dlog_verbose("Set the notifications: %x.\n", notifications); |
| 2746 | |
| 2747 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2748 | |
| 2749 | out: |
| 2750 | vm_unlock(&receiver_locked); |
| 2751 | |
| 2752 | return ret; |
| 2753 | } |
| 2754 | |
| 2755 | static struct ffa_value api_ffa_notification_get_success_return( |
| 2756 | ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm, |
| 2757 | ffa_notifications_bitmap_t from_framework) |
| 2758 | { |
| 2759 | return (struct ffa_value){ |
| 2760 | .func = FFA_SUCCESS_32, |
| 2761 | .arg1 = 0U, |
| 2762 | .arg2 = (uint32_t)from_sp, |
| 2763 | .arg3 = (uint32_t)(from_sp >> 32), |
| 2764 | .arg4 = (uint32_t)from_vm, |
| 2765 | .arg5 = (uint32_t)(from_vm >> 32), |
| 2766 | .arg6 = (uint32_t)from_framework, |
| 2767 | .arg7 = (uint32_t)(from_framework >> 32), |
| 2768 | }; |
| 2769 | } |
| 2770 | |
| 2771 | struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id, |
| 2772 | ffa_vcpu_index_t vcpu_id, |
| 2773 | uint32_t flags, struct vcpu *current) |
| 2774 | { |
| 2775 | /* TODO: get framework notifications, when these are supported. */ |
| 2776 | ffa_notifications_bitmap_t sp_notifications = 0; |
| 2777 | ffa_notifications_bitmap_t vm_notifications = 0; |
| 2778 | struct vm_locked receiver_locked; |
| 2779 | struct ffa_value ret; |
| 2780 | |
| 2781 | /* |
| 2782 | * Following check should capture wrong uses of the interface, depending |
| 2783 | * on whether Hafnium is SPMC or hypervisor. |
| 2784 | * On the rest of the function it is assumed this condition is met. |
| 2785 | */ |
| 2786 | if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) { |
| 2787 | dlog_verbose("Invalid use of notifications get interface.\n"); |
| 2788 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2789 | } |
| 2790 | |
| 2791 | /* |
| 2792 | * This check assumes receiver is the current VM, and has been enforced |
| 2793 | * by `plat_ffa_is_notifications_get_valid`. |
| 2794 | */ |
| 2795 | receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id); |
| 2796 | |
| 2797 | /* |
| 2798 | * `plat_ffa_is_notifications_get_valid` ensures following is never |
| 2799 | * true. |
| 2800 | */ |
| 2801 | CHECK(receiver_locked.vm != NULL); |
| 2802 | |
| 2803 | if (receiver_locked.vm->vcpu_count <= vcpu_id || |
| 2804 | (receiver_locked.vm->vcpu_count != 1 && |
| 2805 | cpu_index(current->cpu) != vcpu_id)) { |
| 2806 | dlog_verbose("Invalid VCPU ID!\n"); |
| 2807 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2808 | goto out; |
| 2809 | } |
| 2810 | |
| 2811 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) { |
J-Alves | 98ff956 | 2021-09-09 14:39:41 +0100 | [diff] [blame] | 2812 | if (!plat_ffa_notifications_get_from_sp( |
| 2813 | receiver_locked, vcpu_id, &sp_notifications, |
| 2814 | &ret)) { |
| 2815 | dlog_verbose("Failed to get notifications from sps."); |
| 2816 | goto out; |
| 2817 | } |
J-Alves | aa79c01 | 2021-07-09 14:29:45 +0100 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) { |
| 2821 | vm_notifications = vm_notifications_get_pending_and_clear( |
| 2822 | receiver_locked, true, vcpu_id); |
| 2823 | } |
| 2824 | |
| 2825 | ret = api_ffa_notification_get_success_return(sp_notifications, |
| 2826 | vm_notifications, 0); |
| 2827 | |
| 2828 | out: |
| 2829 | vm_unlock(&receiver_locked); |
| 2830 | |
| 2831 | return ret; |
| 2832 | } |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 2833 | |
| 2834 | /** |
| 2835 | * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by |
| 2836 | * the section 17.7.1 of the FF-A v1.1 Beta0 specification. |
| 2837 | */ |
| 2838 | static struct ffa_value api_ffa_notification_info_get_success_return( |
| 2839 | const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes, |
| 2840 | uint32_t lists_count, bool list_is_full) |
| 2841 | { |
| 2842 | struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64}; |
| 2843 | |
| 2844 | /* |
| 2845 | * Copying content of ids into ret structure. Use 5 registers (x3-x7) to |
| 2846 | * hold the list of ids. |
| 2847 | */ |
| 2848 | memcpy_s(&ret.arg3, |
| 2849 | sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids, |
| 2850 | sizeof(ids[0]) * ids_count); |
| 2851 | |
| 2852 | /* |
| 2853 | * According to the spec x2 should have: |
| 2854 | * - Bit flagging if there are more notifications pending; |
| 2855 | * - The total number of elements (i.e. total list size); |
| 2856 | * - The number of VCPU IDs within each VM specific list. |
| 2857 | */ |
| 2858 | ret.arg2 = |
| 2859 | list_is_full ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING : 0; |
| 2860 | |
| 2861 | ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK) |
| 2862 | << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT; |
| 2863 | |
| 2864 | for (unsigned int i = 0; i < lists_count; i++) { |
| 2865 | ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK) |
| 2866 | << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1); |
| 2867 | } |
| 2868 | |
| 2869 | return ret; |
| 2870 | } |
| 2871 | |
| 2872 | struct ffa_value api_ffa_notification_info_get(struct vcpu *current) |
| 2873 | { |
| 2874 | /* |
| 2875 | * Following set of variables should be populated with the return info. |
| 2876 | * At a successfull handling of this interface, they should be used |
| 2877 | * to populate the 'ret' structure in accordance to the table 17.29 |
| 2878 | * of the FF-A v1.1 Beta0 specification. |
| 2879 | */ |
| 2880 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS]; |
| 2881 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 2882 | uint32_t lists_count = 0; |
| 2883 | uint32_t ids_count = 0; |
| 2884 | bool list_is_full = false; |
| 2885 | |
| 2886 | /* |
| 2887 | * This interface can only be called at NS virtual/physical FF-A |
| 2888 | * instance by the endpoint implementing the primary scheduler and the |
| 2889 | * Hypervisor/OS kernel. |
| 2890 | * In the SPM, following check passes if call has been forwarded from |
| 2891 | * the hypervisor. |
| 2892 | */ |
| 2893 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 2894 | dlog_verbose( |
| 2895 | "Only the receiver's scheduler can use this " |
| 2896 | "interface\n"); |
| 2897 | return ffa_error(FFA_NOT_SUPPORTED); |
| 2898 | } |
| 2899 | |
| 2900 | /* Get notifications' info from this world */ |
| 2901 | for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full; |
| 2902 | ++index) { |
| 2903 | struct vm_locked vm_locked = vm_lock(vm_find_index(index)); |
| 2904 | |
| 2905 | list_is_full = vm_notifications_info_get( |
| 2906 | vm_locked, ids, &ids_count, lists_sizes, &lists_count, |
| 2907 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 2908 | |
| 2909 | vm_unlock(&vm_locked); |
| 2910 | } |
| 2911 | |
| 2912 | if (!list_is_full) { |
| 2913 | /* Grab notifications info from other world */ |
| 2914 | list_is_full = plat_ffa_vm_notifications_info_get( |
| 2915 | ids, &ids_count, lists_sizes, &lists_count, |
| 2916 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 2917 | } |
| 2918 | |
| 2919 | if (ids_count == 0) { |
| 2920 | return ffa_error(FFA_NO_DATA); |
| 2921 | } |
| 2922 | |
| 2923 | return api_ffa_notification_info_get_success_return( |
| 2924 | ids, ids_count, lists_sizes, lists_count, list_is_full); |
| 2925 | } |