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