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