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