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