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