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