Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/api.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 18 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 19 | #include "hf/arch/cpu.h" |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 20 | #include "hf/arch/tee.h" |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 21 | #include "hf/arch/timer.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 22 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 23 | #include "hf/check.h" |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 24 | #include "hf/dlog.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 25 | #include "hf/mm.h" |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 26 | #include "hf/plat/console.h" |
Jose Marinho | 40d55f3 | 2019-07-01 15:41:54 +0100 | [diff] [blame] | 27 | #include "hf/spci_internal.h" |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 28 | #include "hf/spci_memory.h" |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 29 | #include "hf/spinlock.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 30 | #include "hf/static_assert.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 31 | #include "hf/std.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 32 | #include "hf/vm.h" |
| 33 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 34 | #include "vmapi/hf/call.h" |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 35 | #include "vmapi/hf/spci.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 36 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 37 | /* |
| 38 | * To eliminate the risk of deadlocks, we define a partial order for the |
| 39 | * acquisition of locks held concurrently by the same physical CPU. Our current |
| 40 | * ordering requirements are as follows: |
| 41 | * |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 42 | * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 43 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 44 | * Locks of the same kind require the lock of lowest address to be locked first, |
| 45 | * see `sl_lock_both()`. |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 48 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 49 | "Currently, a page is mapped for the send and receive buffers so " |
| 50 | "the maximum request is the size of a page."); |
| 51 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 52 | static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE, |
| 53 | "The page pool entry size must be at least as big as the mailbox " |
| 54 | "size, so that memory region descriptors can be copied from the " |
| 55 | "mailbox for memory sharing."); |
| 56 | |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 57 | static struct mpool api_page_pool; |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 60 | * Initialises the API page pool by taking ownership of the contents of the |
| 61 | * given page pool. |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 62 | */ |
| 63 | void api_init(struct mpool *ppool) |
| 64 | { |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 65 | mpool_init_from(&api_page_pool, ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 68 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 69 | * 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] | 70 | * |
| 71 | * This triggers the scheduling logic to run. Run in the context of secondary VM |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 72 | * to cause SPCI_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] | 73 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 74 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 75 | struct spci_value primary_ret, |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 76 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 77 | { |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 78 | struct vm *primary = vm_find(HF_PRIMARY_VM_ID); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 79 | struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 80 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 81 | /* |
| 82 | * If the secondary is blocked but has a timer running, sleep until the |
| 83 | * timer fires rather than indefinitely. |
| 84 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 85 | switch (primary_ret.func) { |
| 86 | case HF_SPCI_RUN_WAIT_FOR_INTERRUPT: |
| 87 | case SPCI_MSG_WAIT_32: { |
| 88 | if (arch_timer_enabled_current()) { |
| 89 | uint64_t remaining_ns = |
| 90 | arch_timer_remaining_ns_current(); |
| 91 | |
| 92 | if (remaining_ns == 0) { |
| 93 | /* |
| 94 | * Timer is pending, so the current vCPU should |
| 95 | * be run again right away. |
| 96 | */ |
| 97 | primary_ret.func = SPCI_INTERRUPT_32; |
| 98 | /* |
| 99 | * primary_ret.arg1 should already be set to the |
| 100 | * current VM ID and vCPU ID. |
| 101 | */ |
| 102 | primary_ret.arg2 = 0; |
| 103 | } else { |
| 104 | primary_ret.arg2 = remaining_ns; |
| 105 | } |
| 106 | } else { |
| 107 | primary_ret.arg2 = SPCI_SLEEP_INDEFINITE; |
| 108 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 109 | break; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 110 | } |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 111 | |
| 112 | default: |
| 113 | /* Do nothing. */ |
| 114 | break; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 117 | /* Set the return value for the primary VM's call to HF_VCPU_RUN. */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 118 | arch_regs_set_retval(&next->regs, primary_ret); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 119 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 120 | /* Mark the current vCPU as waiting. */ |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 121 | sl_lock(¤t->lock); |
| 122 | current->state = secondary_state; |
| 123 | sl_unlock(¤t->lock); |
| 124 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 125 | return next; |
| 126 | } |
| 127 | |
| 128 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 129 | * 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] | 130 | */ |
| 131 | struct vcpu *api_preempt(struct vcpu *current) |
| 132 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 133 | struct spci_value ret = { |
| 134 | .func = SPCI_INTERRUPT_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 135 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 138 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 142 | * 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] | 143 | * VM. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 144 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 145 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 146 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 147 | struct spci_value ret = { |
| 148 | .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 149 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 150 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 151 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 152 | return api_switch_to_primary(current, ret, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 153 | VCPU_STATE_BLOCKED_INTERRUPT); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 157 | * Puts the current vCPU in off mode, and returns to the primary VM. |
| 158 | */ |
| 159 | struct vcpu *api_vcpu_off(struct vcpu *current) |
| 160 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 161 | struct spci_value ret = { |
| 162 | .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 163 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * Disable the timer, so the scheduler doesn't get told to call back |
| 168 | * based on it. |
| 169 | */ |
| 170 | arch_timer_disable_current(); |
| 171 | |
| 172 | return api_switch_to_primary(current, ret, VCPU_STATE_OFF); |
| 173 | } |
| 174 | |
| 175 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 176 | * Returns to the primary VM to allow this CPU to be used for other tasks as the |
| 177 | * vCPU does not have work to do at this moment. The current vCPU is marked as |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 178 | * ready to be scheduled again. |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 179 | */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 180 | void api_yield(struct vcpu *current, struct vcpu **next) |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 181 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 182 | struct spci_value primary_ret = { |
| 183 | .func = SPCI_YIELD_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 184 | .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)), |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 188 | /* NOOP on the primary as it makes the scheduling decisions. */ |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 189 | return; |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Andrew Walbran | 16075b6 | 2019-09-03 17:11:07 +0100 | [diff] [blame] | 192 | *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY); |
Andrew Scull | 66d62bf | 2019-02-01 13:54:10 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 196 | * Switches to the primary so that it can switch to the target, or kick it if it |
| 197 | * is already running on a different physical CPU. |
| 198 | */ |
| 199 | struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu) |
| 200 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 201 | struct spci_value ret = { |
| 202 | .func = HF_SPCI_RUN_WAKE_UP, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 203 | .arg1 = spci_vm_vcpu(target_vcpu->vm->id, |
| 204 | vcpu_index(target_vcpu)), |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 205 | }; |
| 206 | return api_switch_to_primary(current, ret, VCPU_STATE_READY); |
| 207 | } |
| 208 | |
| 209 | /** |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 210 | * Aborts the vCPU and triggers its VM to abort fully. |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 211 | */ |
| 212 | struct vcpu *api_abort(struct vcpu *current) |
| 213 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 214 | struct spci_value ret = spci_error(SPCI_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 215 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 216 | dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id, |
| 217 | vcpu_index(current)); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 218 | |
| 219 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 220 | /* TODO: what to do when the primary aborts? */ |
| 221 | for (;;) { |
| 222 | /* Do nothing. */ |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | atomic_store_explicit(¤t->vm->aborting, true, |
| 227 | memory_order_relaxed); |
| 228 | |
| 229 | /* TODO: free resources once all vCPUs abort. */ |
| 230 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 231 | return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED); |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 235 | * Returns the ID of the VM. |
| 236 | */ |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame] | 237 | struct spci_value api_spci_id_get(const struct vcpu *current) |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 238 | { |
Andrew Walbran | d230f66 | 2019-10-07 18:03:36 +0100 | [diff] [blame] | 239 | return (struct spci_value){.func = SPCI_SUCCESS_32, |
| 240 | .arg2 = current->vm->id}; |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 244 | * Returns the number of VMs configured to run. |
| 245 | */ |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 246 | spci_vm_count_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 247 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 248 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 252 | * Returns the number of vCPUs configured in the given VM, or 0 if there is no |
| 253 | * such VM or the caller is not the primary VM. |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 254 | */ |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 255 | spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id, |
| 256 | const struct vcpu *current) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 257 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 258 | struct vm *vm; |
| 259 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 260 | /* Only the primary VM needs to know about vCPUs for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 261 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 262 | return 0; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 263 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 264 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 265 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 266 | if (vm == NULL) { |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 267 | return 0; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 274 | * This function is called by the architecture-specific context switching |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 275 | * function to indicate that register state for the given vCPU has been saved |
| 276 | * and can therefore be used by other pCPUs. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 277 | */ |
| 278 | void api_regs_state_saved(struct vcpu *vcpu) |
| 279 | { |
| 280 | sl_lock(&vcpu->lock); |
| 281 | vcpu->regs_available = true; |
| 282 | sl_unlock(&vcpu->lock); |
| 283 | } |
| 284 | |
| 285 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 286 | * Retrieves the next waiter and removes it from the wait list if the VM's |
| 287 | * mailbox is in a writable state. |
| 288 | */ |
| 289 | static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm) |
| 290 | { |
| 291 | struct wait_entry *entry; |
| 292 | struct vm *vm = locked_vm.vm; |
| 293 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 294 | if (vm->mailbox.state != MAILBOX_STATE_EMPTY || |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 295 | vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) { |
| 296 | /* The mailbox is not writable or there are no waiters. */ |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | /* Remove waiter from the wait list. */ |
| 301 | entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry, |
| 302 | wait_links); |
| 303 | list_remove(&entry->wait_links); |
| 304 | return entry; |
| 305 | } |
| 306 | |
| 307 | /** |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 308 | * Assuming that the arguments have already been checked by the caller, injects |
| 309 | * a virtual interrupt of the given ID into the given target vCPU. This doesn't |
| 310 | * cause the vCPU to actually be run immediately; it will be taken when the vCPU |
| 311 | * is next run, which is up to the scheduler. |
| 312 | * |
| 313 | * Returns: |
| 314 | * - 0 on success if no further action is needed. |
| 315 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 316 | * up or kick the target vCPU. |
| 317 | */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 318 | static int64_t internal_interrupt_inject(struct vcpu *target_vcpu, |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 319 | uint32_t intid, struct vcpu *current, |
| 320 | struct vcpu **next) |
| 321 | { |
| 322 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 323 | uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 324 | int64_t ret = 0; |
| 325 | |
| 326 | sl_lock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * We only need to change state and (maybe) trigger a virtual IRQ if it |
| 330 | * is enabled and was not previously pending. Otherwise we can skip |
| 331 | * everything except setting the pending bit. |
| 332 | * |
| 333 | * If you change this logic make sure to update the need_vm_lock logic |
| 334 | * above to match. |
| 335 | */ |
| 336 | if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 337 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
| 338 | intid_mask)) { |
| 339 | goto out; |
| 340 | } |
| 341 | |
| 342 | /* Increment the count. */ |
| 343 | target_vcpu->interrupts.enabled_and_pending_count++; |
| 344 | |
| 345 | /* |
| 346 | * Only need to update state if there was not already an |
| 347 | * interrupt enabled and pending. |
| 348 | */ |
| 349 | if (target_vcpu->interrupts.enabled_and_pending_count != 1) { |
| 350 | goto out; |
| 351 | } |
| 352 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 353 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 354 | /* |
| 355 | * If the call came from the primary VM, let it know that it |
| 356 | * should run or kick the target vCPU. |
| 357 | */ |
| 358 | ret = 1; |
| 359 | } else if (current != target_vcpu && next != NULL) { |
Andrew Walbran | 3364565 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 360 | *next = api_wake_up(current, target_vcpu); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | out: |
| 364 | /* Either way, make it pending. */ |
| 365 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 366 | |
| 367 | sl_unlock(&target_vcpu->lock); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | /** |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 373 | * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL |
| 374 | * or SPCI_MSG_WAIT call. |
| 375 | */ |
| 376 | static struct spci_value spci_msg_recv_return(const struct vm *receiver) |
| 377 | { |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 378 | switch (receiver->mailbox.recv_func) { |
| 379 | case SPCI_MSG_SEND_32: |
| 380 | return (struct spci_value){ |
| 381 | .func = SPCI_MSG_SEND_32, |
| 382 | .arg1 = (receiver->mailbox.recv_sender << 16) | |
| 383 | receiver->id, |
| 384 | .arg3 = receiver->mailbox.recv_size}; |
| 385 | case SPCI_MEM_DONATE_32: |
| 386 | case SPCI_MEM_LEND_32: |
| 387 | case SPCI_MEM_SHARE_32: |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 388 | return (struct spci_value){.func = receiver->mailbox.recv_func, |
Andrew Walbran | 0f6cede | 2020-01-10 15:38:09 +0000 | [diff] [blame] | 389 | .arg3 = receiver->mailbox.recv_size, |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 390 | .arg4 = receiver->mailbox.recv_size}; |
| 391 | default: |
| 392 | /* This should never be reached, but return an error in case. */ |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 393 | dlog_error("Tried to return an invalid message function %#x\n", |
| 394 | receiver->mailbox.recv_func); |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 395 | return spci_error(SPCI_DENIED); |
| 396 | } |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 400 | * 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] | 401 | * value needs to be forced onto the vCPU. |
| 402 | */ |
Andrew Scull | 38772ab | 2019-01-24 15:16:50 +0000 | [diff] [blame] | 403 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 404 | struct spci_value *run_ret) |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 405 | { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 406 | bool need_vm_lock; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 407 | bool ret; |
| 408 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 409 | /* |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 410 | * 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] | 411 | * |
Andrew Scull | 4caadaf | 2019-07-03 13:13:47 +0100 | [diff] [blame] | 412 | * The VM lock is not needed in the common case so it must only be taken |
| 413 | * when it is going to be needed. This ensures there are no inter-vCPU |
| 414 | * dependencies in the common run case meaning the sensitive context |
| 415 | * switch performance is consistent. |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 416 | */ |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 417 | sl_lock(&vcpu->lock); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 418 | |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 419 | /* The VM needs to be locked to deliver mailbox messages. */ |
| 420 | need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX; |
| 421 | if (need_vm_lock) { |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 422 | sl_unlock(&vcpu->lock); |
Andrew Walbran | d81c7d8 | 2019-11-27 18:34:46 +0000 | [diff] [blame] | 423 | sl_lock(&vcpu->vm->lock); |
| 424 | sl_lock(&vcpu->lock); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * If the vCPU is already running somewhere then we can't run it here |
| 429 | * simultaneously. While it is actually running then the state should be |
| 430 | * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it |
| 431 | * stops running but while Hafnium is in the process of switching back |
| 432 | * to the primary there will be a brief period while the state has been |
| 433 | * updated but `regs_available` is still false (until |
| 434 | * `api_regs_state_saved` is called). We can't start running it again |
| 435 | * until this has finished, so count this state as still running for the |
| 436 | * purposes of this check. |
| 437 | */ |
| 438 | if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) { |
| 439 | /* |
| 440 | * vCPU is running on another pCPU. |
| 441 | * |
| 442 | * It's okay not to return the sleep duration here because the |
| 443 | * other physical CPU that is currently running this vCPU will |
| 444 | * return the sleep duration if needed. |
| 445 | */ |
| 446 | *run_ret = spci_error(SPCI_BUSY); |
| 447 | ret = false; |
| 448 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 449 | } |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 450 | |
| 451 | if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 452 | if (vcpu->state != VCPU_STATE_ABORTED) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 453 | dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id, |
| 454 | vcpu_index(vcpu)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 455 | vcpu->state = VCPU_STATE_ABORTED; |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 456 | } |
| 457 | ret = false; |
| 458 | goto out; |
| 459 | } |
| 460 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 461 | switch (vcpu->state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 462 | case VCPU_STATE_RUNNING: |
| 463 | case VCPU_STATE_OFF: |
| 464 | case VCPU_STATE_ABORTED: |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 465 | ret = false; |
| 466 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 467 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 468 | case VCPU_STATE_BLOCKED_MAILBOX: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 469 | /* |
| 470 | * A pending message allows the vCPU to run so the message can |
| 471 | * be delivered directly. |
| 472 | */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 473 | if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 474 | arch_regs_set_retval(&vcpu->regs, |
| 475 | spci_msg_recv_return(vcpu->vm)); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 476 | vcpu->vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | /* Fall through. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 480 | case VCPU_STATE_BLOCKED_INTERRUPT: |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 481 | /* Allow virtual interrupts to be delivered. */ |
| 482 | if (vcpu->interrupts.enabled_and_pending_count > 0) { |
| 483 | break; |
| 484 | } |
| 485 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 486 | if (arch_timer_enabled(&vcpu->regs)) { |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 487 | uint64_t timer_remaining_ns = |
| 488 | arch_timer_remaining_ns(&vcpu->regs); |
| 489 | |
| 490 | /* |
| 491 | * The timer expired so allow the interrupt to be |
| 492 | * delivered. |
| 493 | */ |
| 494 | if (timer_remaining_ns == 0) { |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * The vCPU is not ready to run, return the appropriate |
| 500 | * code to the primary which called vcpu_run. |
| 501 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 502 | run_ret->func = |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 503 | vcpu->state == VCPU_STATE_BLOCKED_MAILBOX |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 504 | ? SPCI_MSG_WAIT_32 |
| 505 | : HF_SPCI_RUN_WAIT_FOR_INTERRUPT; |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 506 | run_ret->arg1 = |
| 507 | spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu)); |
Andrew Walbran | 2fc856a | 2019-11-04 15:17:24 +0000 | [diff] [blame] | 508 | run_ret->arg2 = timer_remaining_ns; |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | ret = false; |
| 512 | goto out; |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 513 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 514 | case VCPU_STATE_READY: |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 515 | break; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 518 | /* It has been decided that the vCPU should be run. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 519 | vcpu->cpu = current->cpu; |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 520 | vcpu->state = VCPU_STATE_RUNNING; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 521 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 522 | /* |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 523 | * Mark the registers as unavailable now that we're about to reflect |
| 524 | * them onto the real registers. This will also prevent another physical |
| 525 | * CPU from trying to read these registers. |
| 526 | */ |
| 527 | vcpu->regs_available = false; |
| 528 | |
| 529 | ret = true; |
| 530 | |
| 531 | out: |
| 532 | sl_unlock(&vcpu->lock); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 533 | if (need_vm_lock) { |
| 534 | sl_unlock(&vcpu->vm->lock); |
| 535 | } |
| 536 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 540 | struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx, |
| 541 | const struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 542 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 543 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 544 | struct vcpu *vcpu; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 545 | struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 546 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 547 | /* Only the primary VM can switch vCPUs. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 548 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 549 | ret.arg2 = SPCI_DENIED; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 550 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 551 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 552 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 553 | /* Only secondary VM vCPUs can be run. */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 554 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 555 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 556 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 557 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 558 | /* The requested VM must exist. */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 559 | vm = vm_find(vm_id); |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 560 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 561 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 564 | /* The requested vCPU must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 565 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 566 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 567 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 568 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 569 | /* Update state if allowed. */ |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 570 | vcpu = vm_get_vcpu(vm, vcpu_idx); |
Andrew Scull | b06d175 | 2019-02-04 10:15:48 +0000 | [diff] [blame] | 571 | if (!api_vcpu_prepare_run(current, vcpu, &ret)) { |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 572 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 573 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 574 | |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 575 | /* |
| 576 | * Inject timer interrupt if timer has expired. It's safe to access |
| 577 | * vcpu->regs here because api_vcpu_prepare_run already made sure that |
| 578 | * regs_available was true (and then set it to false) before returning |
| 579 | * true. |
| 580 | */ |
| 581 | if (arch_timer_pending(&vcpu->regs)) { |
| 582 | /* Make virtual timer interrupt pending. */ |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 583 | internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu, |
| 584 | NULL); |
Andrew Walbran | 508e63c | 2018-12-20 17:02:37 +0000 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * Set the mask bit so the hardware interrupt doesn't fire |
| 588 | * again. Ideally we wouldn't do this because it affects what |
| 589 | * the secondary vCPU sees, but if we don't then we end up with |
| 590 | * a loop of the interrupt firing each time we try to return to |
| 591 | * the secondary vCPU. |
| 592 | */ |
| 593 | arch_timer_mask(&vcpu->regs); |
| 594 | } |
| 595 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 596 | /* Switch to the vCPU. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 597 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 598 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 599 | /* |
| 600 | * Set a placeholder return code to the scheduler. This will be |
| 601 | * overwritten when the switch back to the primary occurs. |
| 602 | */ |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 603 | ret.func = SPCI_INTERRUPT_32; |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 604 | ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx); |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 605 | ret.arg2 = 0; |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 606 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 607 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 608 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 612 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 613 | */ |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 614 | static bool api_mode_valid_owned_and_exclusive(uint32_t mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 615 | { |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 616 | return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 617 | MM_MODE_SHARED)) == 0; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /** |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 621 | * Determines the value to be returned by api_vm_configure and spci_rx_release |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 622 | * after they've succeeded. If a secondary VM is running and there are waiters, |
| 623 | * it also switches back to the primary VM for it to wake waiters up. |
| 624 | */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 625 | static struct spci_value api_waiter_result(struct vm_locked locked_vm, |
| 626 | struct vcpu *current, |
| 627 | struct vcpu **next) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 628 | { |
| 629 | struct vm *vm = locked_vm.vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 630 | |
| 631 | if (list_empty(&vm->mailbox.waiter_list)) { |
| 632 | /* No waiters, nothing else to do. */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 633 | return (struct spci_value){.func = SPCI_SUCCESS_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (vm->id == HF_PRIMARY_VM_ID) { |
| 637 | /* The caller is the primary VM. Tell it to wake up waiters. */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 638 | return (struct spci_value){.func = SPCI_RX_RELEASE_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Switch back to the primary VM, informing it that there are waiters |
| 643 | * that need to be notified. |
| 644 | */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 645 | *next = api_switch_to_primary( |
| 646 | current, (struct spci_value){.func = SPCI_RX_RELEASE_32}, |
| 647 | VCPU_STATE_READY); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 648 | |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 649 | return (struct spci_value){.func = SPCI_SUCCESS_32}; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /** |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 653 | * Configures the hypervisor's stage-1 view of the send and receive pages. The |
| 654 | * stage-1 page tables must be locked so memory cannot be taken by another core |
| 655 | * which could result in this transaction being unable to roll back in the case |
| 656 | * of an error. |
| 657 | */ |
| 658 | static bool api_vm_configure_stage1(struct vm_locked vm_locked, |
| 659 | paddr_t pa_send_begin, paddr_t pa_send_end, |
| 660 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
| 661 | struct mpool *local_page_pool) |
| 662 | { |
| 663 | bool ret; |
| 664 | struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1(); |
| 665 | |
| 666 | /* Map the send page as read-only in the hypervisor address space. */ |
| 667 | vm_locked.vm->mailbox.send = |
| 668 | mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 669 | MM_MODE_R, local_page_pool); |
| 670 | if (!vm_locked.vm->mailbox.send) { |
| 671 | /* TODO: partial defrag of failed range. */ |
| 672 | /* Recover any memory consumed in failed mapping. */ |
| 673 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 674 | goto fail; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Map the receive page as writable in the hypervisor address space. On |
| 679 | * failure, unmap the send page before returning. |
| 680 | */ |
| 681 | vm_locked.vm->mailbox.recv = |
| 682 | mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end, |
| 683 | MM_MODE_W, local_page_pool); |
| 684 | if (!vm_locked.vm->mailbox.recv) { |
| 685 | /* TODO: partial defrag of failed range. */ |
| 686 | /* Recover any memory consumed in failed mapping. */ |
| 687 | mm_defrag(mm_stage1_locked, local_page_pool); |
| 688 | goto fail_undo_send; |
| 689 | } |
| 690 | |
| 691 | ret = true; |
| 692 | goto out; |
| 693 | |
| 694 | /* |
| 695 | * The following mappings will not require more memory than is available |
| 696 | * in the local pool. |
| 697 | */ |
| 698 | fail_undo_send: |
| 699 | vm_locked.vm->mailbox.send = NULL; |
Andrew Scull | 7e8de32 | 2019-07-02 13:00:56 +0100 | [diff] [blame] | 700 | CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end, |
| 701 | local_page_pool)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 702 | |
| 703 | fail: |
| 704 | ret = false; |
| 705 | |
| 706 | out: |
| 707 | mm_unlock_stage1(&mm_stage1_locked); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Configures the send and receive pages in the VM stage-2 and hypervisor |
| 714 | * stage-1 page tables. Locking of the page tables combined with a local memory |
| 715 | * pool ensures there will always be enough memory to recover from any errors |
| 716 | * that arise. |
| 717 | */ |
| 718 | static bool api_vm_configure_pages(struct vm_locked vm_locked, |
| 719 | paddr_t pa_send_begin, paddr_t pa_send_end, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 720 | uint32_t orig_send_mode, |
| 721 | paddr_t pa_recv_begin, paddr_t pa_recv_end, |
| 722 | uint32_t orig_recv_mode) |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 723 | { |
| 724 | bool ret; |
| 725 | struct mpool local_page_pool; |
| 726 | |
| 727 | /* |
| 728 | * Create a local pool so any freed memory can't be used by another |
| 729 | * thread. This is to ensure the original mapping can be restored if any |
| 730 | * stage of the process fails. |
| 731 | */ |
| 732 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 733 | |
| 734 | /* Take memory ownership away from the VM and mark as shared. */ |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 735 | if (!vm_identity_map( |
| 736 | vm_locked, pa_send_begin, pa_send_end, |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 737 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W, |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 738 | &local_page_pool, NULL)) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 739 | goto fail; |
| 740 | } |
| 741 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 742 | if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
| 743 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R, |
| 744 | &local_page_pool, NULL)) { |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 745 | /* TODO: partial defrag of failed range. */ |
| 746 | /* Recover any memory consumed in failed mapping. */ |
| 747 | mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool); |
| 748 | goto fail_undo_send; |
| 749 | } |
| 750 | |
| 751 | if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end, |
| 752 | pa_recv_begin, pa_recv_end, |
| 753 | &local_page_pool)) { |
| 754 | goto fail_undo_send_and_recv; |
| 755 | } |
| 756 | |
| 757 | ret = true; |
| 758 | goto out; |
| 759 | |
| 760 | /* |
| 761 | * The following mappings will not require more memory than is available |
| 762 | * in the local pool. |
| 763 | */ |
| 764 | fail_undo_send_and_recv: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 765 | CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, |
| 766 | orig_recv_mode, &local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 767 | |
| 768 | fail_undo_send: |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 769 | CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end, |
| 770 | orig_send_mode, &local_page_pool, NULL)); |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 771 | |
| 772 | fail: |
| 773 | ret = false; |
| 774 | |
| 775 | out: |
| 776 | mpool_fini(&local_page_pool); |
| 777 | |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 782 | * Configures the VM to send/receive data through the specified pages. The pages |
| 783 | * must not be shared. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 784 | * |
| 785 | * Returns: |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 786 | * - SPCI_ERROR SPCI_INVALID_PARAMETERS if the given addresses are not properly |
| 787 | * aligned or are the same. |
| 788 | * - SPCI_ERROR SPCI_NO_MEMORY if the hypervisor was unable to map the buffers |
| 789 | * due to insuffient page table memory. |
| 790 | * - SPCI_ERROR SPCI_DENIED if the pages are already mapped or are not owned by |
| 791 | * the caller. |
| 792 | * - SPCI_SUCCESS on success if no further action is needed. |
| 793 | * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now |
| 794 | * needs to wake up or kick waiters. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 795 | */ |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 796 | struct spci_value api_spci_rxtx_map(ipaddr_t send, ipaddr_t recv, |
| 797 | uint32_t page_count, struct vcpu *current, |
| 798 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 799 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 800 | struct vm *vm = current->vm; |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 801 | struct vm_locked vm_locked; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 802 | paddr_t pa_send_begin; |
| 803 | paddr_t pa_send_end; |
| 804 | paddr_t pa_recv_begin; |
| 805 | paddr_t pa_recv_end; |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 806 | uint32_t orig_send_mode; |
| 807 | uint32_t orig_recv_mode; |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 808 | struct spci_value ret; |
| 809 | |
| 810 | /* Hafnium only supports a fixed size of RX/TX buffers. */ |
| 811 | if (page_count != HF_MAILBOX_SIZE / SPCI_PAGE_SIZE) { |
| 812 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 813 | } |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 814 | |
| 815 | /* Fail if addresses are not page-aligned. */ |
Alfredo Mazzinghi | eb1997c | 2019-02-07 18:00:01 +0000 | [diff] [blame] | 816 | if (!is_aligned(ipa_addr(send), PAGE_SIZE) || |
| 817 | !is_aligned(ipa_addr(recv), PAGE_SIZE)) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 818 | return spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 821 | /* Convert to physical addresses. */ |
| 822 | pa_send_begin = pa_from_ipa(send); |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 823 | pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE); |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 824 | |
| 825 | pa_recv_begin = pa_from_ipa(recv); |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 826 | pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE); |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 827 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 828 | /* Fail if the same page is used for the send and receive pages. */ |
| 829 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 830 | return spci_error(SPCI_INVALID_PARAMETERS); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 833 | /* |
| 834 | * The hypervisor's memory map must be locked for the duration of this |
| 835 | * operation to ensure there will be sufficient memory to recover from |
| 836 | * any failures. |
| 837 | * |
| 838 | * TODO: the scope of the can be reduced but will require restructuring |
| 839 | * to keep a single unlock point. |
| 840 | */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 841 | vm_locked = vm_lock(vm); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 842 | |
| 843 | /* We only allow these to be setup once. */ |
| 844 | if (vm->mailbox.send || vm->mailbox.recv) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 845 | ret = spci_error(SPCI_DENIED); |
| 846 | goto exit; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Ensure the pages are valid, owned and exclusive to the VM and that |
| 851 | * the VM has the required access to the memory. |
| 852 | */ |
| 853 | if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE), |
| 854 | &orig_send_mode) || |
| 855 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 856 | (orig_send_mode & MM_MODE_R) == 0 || |
| 857 | (orig_send_mode & MM_MODE_W) == 0) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 858 | ret = spci_error(SPCI_DENIED); |
| 859 | goto exit; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE), |
| 863 | &orig_recv_mode) || |
| 864 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 865 | (orig_recv_mode & MM_MODE_R) == 0) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 866 | ret = spci_error(SPCI_DENIED); |
| 867 | goto exit; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 870 | if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end, |
| 871 | orig_send_mode, pa_recv_begin, pa_recv_end, |
| 872 | orig_recv_mode)) { |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 873 | ret = spci_error(SPCI_NO_MEMORY); |
| 874 | goto exit; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 877 | /* Tell caller about waiters, if any. */ |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 878 | ret = api_waiter_result(vm_locked, current, next); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 879 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 880 | exit: |
Andrew Scull | e132279 | 2019-07-01 17:46:10 +0100 | [diff] [blame] | 881 | vm_unlock(&vm_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | /** |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 887 | * Checks whether the given `to` VM's mailbox is currently busy, and optionally |
| 888 | * registers the `from` VM to be notified when it becomes available. |
| 889 | */ |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 890 | static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify) |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 891 | { |
| 892 | if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 893 | to.vm->mailbox.recv == NULL) { |
| 894 | /* |
| 895 | * Fail if the receiver isn't currently ready to receive data, |
| 896 | * setting up for notification if requested. |
| 897 | */ |
| 898 | if (notify) { |
| 899 | struct wait_entry *entry = |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 900 | vm_get_wait_entry(from, to.vm->id); |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 901 | |
| 902 | /* Append waiter only if it's not there yet. */ |
| 903 | if (list_empty(&entry->wait_links)) { |
| 904 | list_append(&to.vm->mailbox.waiter_list, |
| 905 | &entry->wait_links); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Notifies the `to` VM about the message currently in its mailbox, possibly |
| 917 | * with the help of the primary VM. |
| 918 | */ |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 919 | static struct spci_value deliver_msg(struct vm_locked to, spci_vm_id_t from_id, |
| 920 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 921 | { |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 922 | struct spci_value ret = (struct spci_value){.func = SPCI_SUCCESS_32}; |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 923 | struct spci_value primary_ret = { |
| 924 | .func = SPCI_MSG_SEND_32, |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 925 | .arg1 = ((uint32_t)from_id << 16) | to.vm->id, |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 926 | }; |
| 927 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 928 | /* Messages for the primary VM are delivered directly. */ |
| 929 | if (to.vm->id == HF_PRIMARY_VM_ID) { |
| 930 | /* |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 931 | * Only tell the primary VM the size and other details if the |
| 932 | * message is for it, to avoid leaking data about messages for |
| 933 | * other VMs. |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 934 | */ |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 935 | primary_ret = spci_msg_recv_return(to.vm); |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 936 | |
| 937 | to.vm->mailbox.state = MAILBOX_STATE_READ; |
| 938 | *next = api_switch_to_primary(current, primary_ret, |
| 939 | VCPU_STATE_READY); |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 940 | return ret; |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 943 | to.vm->mailbox.state = MAILBOX_STATE_RECEIVED; |
| 944 | |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 945 | /* Messages for the TEE are sent on via the dispatcher. */ |
| 946 | if (to.vm->id == HF_TEE_VM_ID) { |
| 947 | struct spci_value call = spci_msg_recv_return(to.vm); |
| 948 | |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 949 | ret = arch_tee_call(call); |
| 950 | /* |
| 951 | * After the call to the TEE completes it must have finished |
| 952 | * reading its RX buffer, so it is ready for another message. |
| 953 | */ |
| 954 | to.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 955 | /* |
| 956 | * Don't return to the primary VM in this case, as the TEE is |
| 957 | * not (yet) scheduled via SPCI. |
| 958 | */ |
Andrew Walbran | 11cff3a | 2020-02-28 11:33:17 +0000 | [diff] [blame] | 959 | return ret; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 962 | /* Return to the primary VM directly or with a switch. */ |
Andrew Walbran | f76f575 | 2019-12-03 18:33:08 +0000 | [diff] [blame] | 963 | if (from_id != HF_PRIMARY_VM_ID) { |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 964 | *next = api_switch_to_primary(current, primary_ret, |
| 965 | VCPU_STATE_READY); |
| 966 | } |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 967 | |
| 968 | return ret; |
Andrew Walbran | e0f575f | 2019-10-16 16:00:12 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 972 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 973 | * and notifies the recipient. |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 974 | * |
| 975 | * If the recipient's receive buffer is busy, it can optionally register the |
| 976 | * 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] | 977 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 978 | struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id, |
| 979 | spci_vm_id_t receiver_vm_id, uint32_t size, |
| 980 | uint32_t attributes, struct vcpu *current, |
| 981 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 982 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 983 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 984 | struct vm *to; |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 985 | struct vm_locked to_locked; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 986 | const void *from_msg; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 987 | struct spci_value ret; |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 988 | bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) == |
| 989 | SPCI_MSG_SEND_NOTIFY; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 990 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 991 | /* Ensure sender VM ID corresponds to the current VM. */ |
| 992 | if (sender_vm_id != from->id) { |
| 993 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 994 | } |
| 995 | |
| 996 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 997 | if (receiver_vm_id == from->id) { |
| 998 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 999 | } |
| 1000 | |
| 1001 | /* Limit the size of transfer. */ |
| 1002 | if (size > SPCI_MSG_PAYLOAD_MAX) { |
| 1003 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1004 | } |
| 1005 | |
Andrew Walbran | 0b60c4f | 2019-12-10 17:05:29 +0000 | [diff] [blame] | 1006 | /* Ensure the receiver VM exists. */ |
| 1007 | to = vm_find(receiver_vm_id); |
| 1008 | if (to == NULL) { |
| 1009 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1010 | } |
| 1011 | |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 1012 | /* |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1013 | * Check that the sender has configured its send buffer. If the tx |
| 1014 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 1015 | * be safely accessed after releasing the lock since the tx mailbox |
| 1016 | * address can only be configured once. |
Jose Marinho | a1dfeda | 2019-02-27 16:46:03 +0000 | [diff] [blame] | 1017 | */ |
| 1018 | sl_lock(&from->lock); |
| 1019 | from_msg = from->mailbox.send; |
| 1020 | sl_unlock(&from->lock); |
| 1021 | |
| 1022 | if (from_msg == NULL) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1023 | return spci_error(SPCI_INVALID_PARAMETERS); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1026 | to_locked = vm_lock(to); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1027 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1028 | if (msg_receiver_busy(to_locked, from, notify)) { |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 1029 | ret = spci_error(SPCI_BUSY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1030 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1033 | /* Copy data. */ |
| 1034 | memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg, size); |
| 1035 | to->mailbox.recv_size = size; |
| 1036 | to->mailbox.recv_sender = sender_vm_id; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1037 | to->mailbox.recv_func = SPCI_MSG_SEND_32; |
Andrew Walbran | 2619e0a | 2020-01-10 16:37:50 +0000 | [diff] [blame] | 1038 | ret = deliver_msg(to_locked, sender_vm_id, current, next); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1039 | |
| 1040 | out: |
Andrew Walbran | 82d6d15 | 2019-12-24 15:02:06 +0000 | [diff] [blame] | 1041 | vm_unlock(&to_locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1042 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 1043 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /** |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1047 | * Checks whether the vCPU's attempt to block for a message has already been |
| 1048 | * interrupted or whether it is allowed to block. |
| 1049 | */ |
| 1050 | bool api_spci_msg_recv_block_interrupted(struct vcpu *current) |
| 1051 | { |
| 1052 | bool interrupted; |
| 1053 | |
| 1054 | sl_lock(¤t->lock); |
| 1055 | |
| 1056 | /* |
| 1057 | * Don't block if there are enabled and pending interrupts, to match |
| 1058 | * behaviour of wait_for_interrupt. |
| 1059 | */ |
| 1060 | interrupted = (current->interrupts.enabled_and_pending_count > 0); |
| 1061 | |
| 1062 | sl_unlock(¤t->lock); |
| 1063 | |
| 1064 | return interrupted; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1068 | * Receives a message from the mailbox. If one isn't available, this function |
| 1069 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1070 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1071 | * 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] | 1072 | */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1073 | struct spci_value api_spci_msg_recv(bool block, struct vcpu *current, |
| 1074 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1075 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1076 | struct vm *vm = current->vm; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1077 | struct spci_value return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1078 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1079 | /* |
| 1080 | * The primary VM will receive messages as a status code from running |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1081 | * vCPUs and must not call this function. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1082 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 1083 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1084 | return spci_error(SPCI_NOT_SUPPORTED); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1088 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1089 | /* Return pending messages without blocking. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1090 | if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) { |
| 1091 | vm->mailbox.state = MAILBOX_STATE_READ; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1092 | return_code = spci_msg_recv_return(vm); |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1093 | goto out; |
| 1094 | } |
| 1095 | |
| 1096 | /* No pending message so fail if not allowed to block. */ |
| 1097 | if (!block) { |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1098 | return_code = spci_error(SPCI_RETRY); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1099 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1100 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1101 | |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1102 | /* |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1103 | * From this point onward this call can only be interrupted or a message |
| 1104 | * received. If a message is received the return value will be set at |
| 1105 | * that time to SPCI_SUCCESS. |
Andrew Walbran | 9311c9a | 2019-03-12 16:59:04 +0000 | [diff] [blame] | 1106 | */ |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 1107 | return_code = spci_error(SPCI_INTERRUPTED); |
Andrew Scull | ec52ddf | 2019-08-20 10:41:01 +0100 | [diff] [blame] | 1108 | if (api_spci_msg_recv_block_interrupted(current)) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1109 | goto out; |
| 1110 | } |
| 1111 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1112 | /* Switch back to primary VM to block. */ |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1113 | { |
Andrew Walbran | 7a1ea0b | 2019-10-02 18:18:44 +0100 | [diff] [blame] | 1114 | struct spci_value run_return = { |
| 1115 | .func = SPCI_MSG_WAIT_32, |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 1116 | .arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)), |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1117 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1118 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1119 | *next = api_switch_to_primary(current, run_return, |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1120 | VCPU_STATE_BLOCKED_MAILBOX); |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 1121 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 1122 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1123 | sl_unlock(&vm->lock); |
| 1124 | |
Jose Marinho | 3e2442f | 2019-03-12 13:30:37 +0000 | [diff] [blame] | 1125 | return return_code; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /** |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1129 | * Retrieves the next VM whose mailbox became writable. For a VM to be notified |
| 1130 | * by this function, the caller must have called api_mailbox_send before with |
| 1131 | * the notify argument set to true, and this call must have failed because the |
| 1132 | * mailbox was not available. |
| 1133 | * |
| 1134 | * It should be called repeatedly to retrieve a list of VMs. |
| 1135 | * |
| 1136 | * Returns -1 if no VM became writable, or the id of the VM whose mailbox |
| 1137 | * became writable. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1138 | */ |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1139 | int64_t api_mailbox_writable_get(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1140 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 1141 | struct vm *vm = current->vm; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1142 | struct wait_entry *entry; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 1143 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1144 | |
| 1145 | sl_lock(&vm->lock); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1146 | if (list_empty(&vm->mailbox.ready_list)) { |
| 1147 | ret = -1; |
| 1148 | goto exit; |
| 1149 | } |
| 1150 | |
| 1151 | entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry, |
| 1152 | ready_links); |
| 1153 | list_remove(&entry->ready_links); |
Andrew Walbran | aad8f98 | 2019-12-04 10:56:39 +0000 | [diff] [blame] | 1154 | ret = vm_id_for_wait_entry(vm, entry); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1155 | |
| 1156 | exit: |
| 1157 | sl_unlock(&vm->lock); |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
| 1161 | /** |
| 1162 | * Retrieves the next VM waiting to be notified that the mailbox of the |
| 1163 | * specified VM became writable. Only primary VMs are allowed to call this. |
| 1164 | * |
Wedson Almeida Filho | b790f65 | 2019-01-22 23:41:56 +0000 | [diff] [blame] | 1165 | * Returns -1 on failure or if there are no waiters; the VM id of the next |
| 1166 | * waiter otherwise. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1167 | */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1168 | int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1169 | { |
| 1170 | struct vm *vm; |
| 1171 | struct vm_locked locked; |
| 1172 | struct wait_entry *entry; |
| 1173 | struct vm *waiting_vm; |
| 1174 | |
| 1175 | /* Only primary VMs are allowed to call this function. */ |
| 1176 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
| 1177 | return -1; |
| 1178 | } |
| 1179 | |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1180 | vm = vm_find(vm_id); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1181 | if (vm == NULL) { |
| 1182 | return -1; |
| 1183 | } |
| 1184 | |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 1185 | /* Check if there are outstanding notifications from given VM. */ |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1186 | locked = vm_lock(vm); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1187 | entry = api_fetch_waiter(locked); |
| 1188 | vm_unlock(&locked); |
| 1189 | |
| 1190 | if (entry == NULL) { |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | /* Enqueue notification to waiting VM. */ |
| 1195 | waiting_vm = entry->waiting_vm; |
| 1196 | |
| 1197 | sl_lock(&waiting_vm->lock); |
| 1198 | if (list_empty(&entry->ready_links)) { |
| 1199 | list_append(&waiting_vm->mailbox.ready_list, |
| 1200 | &entry->ready_links); |
| 1201 | } |
| 1202 | sl_unlock(&waiting_vm->lock); |
| 1203 | |
| 1204 | return waiting_vm->id; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1208 | * Releases the caller's mailbox so that a new message can be received. The |
| 1209 | * caller must have copied out all data they wish to preserve as new messages |
| 1210 | * will overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1211 | * |
| 1212 | * Returns: |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1213 | * - SPCI_ERROR SPCI_DENIED on failure, if the mailbox hasn't been read. |
| 1214 | * - SPCI_SUCCESS on success if no further action is needed. |
| 1215 | * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now |
| 1216 | * 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] | 1217 | * hf_mailbox_waiter_get. |
| 1218 | */ |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1219 | struct spci_value api_spci_rx_release(struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1220 | { |
| 1221 | struct vm *vm = current->vm; |
| 1222 | struct vm_locked locked; |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1223 | struct spci_value ret; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1224 | |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 1225 | locked = vm_lock(vm); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1226 | switch (vm->mailbox.state) { |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1227 | case MAILBOX_STATE_EMPTY: |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1228 | case MAILBOX_STATE_RECEIVED: |
Andrew Walbran | 8a0f5ca | 2019-11-05 13:12:23 +0000 | [diff] [blame] | 1229 | ret = spci_error(SPCI_DENIED); |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1230 | break; |
| 1231 | |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1232 | case MAILBOX_STATE_READ: |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 1233 | ret = api_waiter_result(locked, current, next); |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 1234 | vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Scull | aa7db8e | 2019-02-01 14:12:19 +0000 | [diff] [blame] | 1235 | break; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1236 | } |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 1237 | vm_unlock(&locked); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 1238 | |
| 1239 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1240 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1241 | |
| 1242 | /** |
| 1243 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 1244 | * |
| 1245 | * Returns 0 on success, or -1 if the intid is invalid. |
| 1246 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1247 | int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1248 | { |
| 1249 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 1250 | uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1251 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1252 | if (intid >= HF_NUM_INTIDS) { |
| 1253 | return -1; |
| 1254 | } |
| 1255 | |
| 1256 | sl_lock(¤t->lock); |
| 1257 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1258 | /* |
| 1259 | * If it is pending and was not enabled before, increment the |
| 1260 | * count. |
| 1261 | */ |
| 1262 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1263 | ~current->interrupts.interrupt_enabled[intid_index] & |
| 1264 | intid_mask) { |
| 1265 | current->interrupts.enabled_and_pending_count++; |
| 1266 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1267 | current->interrupts.interrupt_enabled[intid_index] |= |
| 1268 | intid_mask; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1269 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1270 | /* |
| 1271 | * If it is pending and was enabled before, decrement the count. |
| 1272 | */ |
| 1273 | if (current->interrupts.interrupt_pending[intid_index] & |
| 1274 | current->interrupts.interrupt_enabled[intid_index] & |
| 1275 | intid_mask) { |
| 1276 | current->interrupts.enabled_and_pending_count--; |
| 1277 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1278 | current->interrupts.interrupt_enabled[intid_index] &= |
| 1279 | ~intid_mask; |
| 1280 | } |
| 1281 | |
| 1282 | sl_unlock(¤t->lock); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 1288 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 1289 | * HF_INVALID_INTID if there are no pending interrupts. |
| 1290 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 1291 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1292 | { |
| 1293 | uint8_t i; |
| 1294 | uint32_t first_interrupt = HF_INVALID_INTID; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* |
| 1297 | * Find the first enabled and pending interrupt ID, return it, and |
| 1298 | * deactivate it. |
| 1299 | */ |
| 1300 | sl_lock(¤t->lock); |
| 1301 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 1302 | uint32_t enabled_and_pending = |
| 1303 | current->interrupts.interrupt_enabled[i] & |
| 1304 | current->interrupts.interrupt_pending[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1305 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1306 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1307 | uint8_t bit_index = ctz(enabled_and_pending); |
| 1308 | /* |
| 1309 | * Mark it as no longer pending and decrement the count. |
| 1310 | */ |
| 1311 | current->interrupts.interrupt_pending[i] &= |
Andrew Walbran | e52006c | 2019-10-22 18:01:28 +0100 | [diff] [blame] | 1312 | ~(1U << bit_index); |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1313 | current->interrupts.enabled_and_pending_count--; |
| 1314 | first_interrupt = |
| 1315 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1319 | |
| 1320 | sl_unlock(¤t->lock); |
| 1321 | return first_interrupt; |
| 1322 | } |
| 1323 | |
| 1324 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 1325 | * 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] | 1326 | * given VM and vCPU. |
| 1327 | */ |
| 1328 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 1329 | struct vcpu *current) |
| 1330 | { |
| 1331 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1332 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1333 | /* |
| 1334 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 1335 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 1336 | */ |
| 1337 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 1338 | current_vm_id == target_vm_id; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 1343 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 1344 | * when the vCPU is next run, which is up to the scheduler. |
| 1345 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 1346 | * Returns: |
| 1347 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 1348 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 1349 | * the target VM. |
| 1350 | * - 0 on success if no further action is needed. |
| 1351 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 1352 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1353 | */ |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1354 | int64_t api_interrupt_inject(spci_vm_id_t target_vm_id, |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 1355 | spci_vcpu_index_t target_vcpu_idx, uint32_t intid, |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1356 | struct vcpu *current, struct vcpu **next) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1357 | { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1358 | struct vcpu *target_vcpu; |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 1359 | struct vm *target_vm = vm_find(target_vm_id); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (intid >= HF_NUM_INTIDS) { |
| 1362 | return -1; |
| 1363 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1364 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1365 | if (target_vm == NULL) { |
| 1366 | return -1; |
| 1367 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1368 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1369 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 1370 | /* The requested vCPU must exist. */ |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1371 | return -1; |
| 1372 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1373 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1374 | if (!is_injection_allowed(target_vm_id, current)) { |
| 1375 | return -1; |
| 1376 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 1377 | |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 1378 | target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1379 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1380 | dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n", |
| 1381 | intid, target_vm_id, target_vcpu_idx, current->vm->id, |
| 1382 | current->cpu->id); |
Andrew Walbran | fc9d438 | 2019-05-10 18:07:21 +0100 | [diff] [blame] | 1383 | return internal_interrupt_inject(target_vcpu, intid, current, next); |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 1384 | } |
Andrew Scull | 6386f25 | 2018-12-06 13:29:10 +0000 | [diff] [blame] | 1385 | |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1386 | /** Returns the version of the implemented SPCI specification. */ |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1387 | struct spci_value api_spci_version(uint32_t requested_version) |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1388 | { |
| 1389 | /* |
| 1390 | * Ensure that both major and minor revision representation occupies at |
| 1391 | * most 15 bits. |
| 1392 | */ |
| 1393 | static_assert(0x8000 > SPCI_VERSION_MAJOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1394 | "Major revision representation takes more than 15 bits."); |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1395 | static_assert(0x10000 > SPCI_VERSION_MINOR, |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1396 | "Minor revision representation takes more than 16 bits."); |
| 1397 | if (requested_version & SPCI_VERSION_RESERVED_BIT) { |
| 1398 | /* Invalid encoding, return an error. */ |
| 1399 | return (struct spci_value){.func = SPCI_NOT_SUPPORTED}; |
| 1400 | } |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1401 | |
Andrew Walbran | 9fd2907 | 2020-04-22 12:12:14 +0100 | [diff] [blame] | 1402 | return (struct spci_value){ |
| 1403 | .func = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) | |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 1404 | SPCI_VERSION_MINOR}; |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1405 | } |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1406 | |
| 1407 | int64_t api_debug_log(char c, struct vcpu *current) |
| 1408 | { |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1409 | bool flush; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1410 | struct vm *vm = current->vm; |
| 1411 | struct vm_locked vm_locked = vm_lock(vm); |
| 1412 | |
Andrew Scull | d54e1be | 2019-08-20 11:09:42 +0100 | [diff] [blame] | 1413 | if (c == '\n' || c == '\0') { |
| 1414 | flush = true; |
| 1415 | } else { |
| 1416 | vm->log_buffer[vm->log_buffer_length++] = c; |
| 1417 | flush = (vm->log_buffer_length == sizeof(vm->log_buffer)); |
| 1418 | } |
| 1419 | |
| 1420 | if (flush) { |
Andrew Walbran | 7f904bf | 2019-07-12 16:38:38 +0100 | [diff] [blame] | 1421 | dlog_flush_vm_buffer(vm->id, vm->log_buffer, |
| 1422 | vm->log_buffer_length); |
| 1423 | vm->log_buffer_length = 0; |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | vm_unlock(&vm_locked); |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1430 | |
| 1431 | /** |
| 1432 | * Discovery function returning information about the implementation of optional |
| 1433 | * SPCI interfaces. |
| 1434 | */ |
| 1435 | struct spci_value api_spci_features(uint32_t function_id) |
| 1436 | { |
| 1437 | switch (function_id) { |
| 1438 | case SPCI_ERROR_32: |
| 1439 | case SPCI_SUCCESS_32: |
Andrew Walbran | 49b4f23 | 2020-04-23 16:06:00 +0100 | [diff] [blame^] | 1440 | case SPCI_INTERRUPT_32: |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1441 | case SPCI_VERSION_32: |
| 1442 | case SPCI_FEATURES_32: |
Andrew Walbran | 49b4f23 | 2020-04-23 16:06:00 +0100 | [diff] [blame^] | 1443 | case SPCI_RX_RELEASE_32: |
| 1444 | case SPCI_RXTX_MAP_64: |
| 1445 | case SPCI_ID_GET_32: |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1446 | case SPCI_MSG_POLL_32: |
| 1447 | case SPCI_MSG_WAIT_32: |
Andrew Walbran | 49b4f23 | 2020-04-23 16:06:00 +0100 | [diff] [blame^] | 1448 | case SPCI_YIELD_32: |
| 1449 | case SPCI_RUN_32: |
| 1450 | case SPCI_MSG_SEND_32: |
| 1451 | case SPCI_MEM_DONATE_32: |
| 1452 | case SPCI_MEM_LEND_32: |
| 1453 | case SPCI_MEM_SHARE_32: |
| 1454 | case SPCI_MEM_RETRIEVE_REQ_32: |
| 1455 | case SPCI_MEM_RETRIEVE_RESP_32: |
| 1456 | case SPCI_MEM_RELINQUISH_32: |
| 1457 | case SPCI_MEM_RECLAIM_32: |
Jose Marinho | c0f4ff2 | 2019-10-09 10:37:42 +0100 | [diff] [blame] | 1458 | return (struct spci_value){.func = SPCI_SUCCESS_32}; |
| 1459 | default: |
| 1460 | return spci_error(SPCI_NOT_SUPPORTED); |
| 1461 | } |
| 1462 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1463 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 1464 | struct spci_value api_spci_mem_send(uint32_t share_func, ipaddr_t address, |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1465 | uint32_t page_count, |
Andrew Walbran | 0f6cede | 2020-01-10 15:38:09 +0000 | [diff] [blame] | 1466 | uint32_t fragment_length, uint32_t length, |
Andrew Walbran | 382e918 | 2020-03-04 11:27:27 +0000 | [diff] [blame] | 1467 | spci_cookie_t cookie, struct vcpu *current, |
Andrew Walbran | 0f6cede | 2020-01-10 15:38:09 +0000 | [diff] [blame] | 1468 | struct vcpu **next) |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1469 | { |
| 1470 | struct vm *from = current->vm; |
| 1471 | struct vm *to; |
| 1472 | const void *from_msg; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1473 | struct spci_memory_region *memory_region; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1474 | struct spci_value ret; |
| 1475 | |
| 1476 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 1477 | /* |
| 1478 | * Hafnium only supports passing the descriptor in the TX |
| 1479 | * mailbox. |
| 1480 | */ |
| 1481 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1482 | } |
| 1483 | |
Andrew Walbran | 0f6cede | 2020-01-10 15:38:09 +0000 | [diff] [blame] | 1484 | if ((cookie == 0) != (fragment_length == length)) { |
| 1485 | /* Cookie is required iff there are multiple fragments. */ |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1486 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * Check that the sender has configured its send buffer. If the TX |
| 1491 | * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can |
| 1492 | * be safely accessed after releasing the lock since the TX mailbox |
| 1493 | * address can only be configured once. |
| 1494 | */ |
| 1495 | sl_lock(&from->lock); |
| 1496 | from_msg = from->mailbox.send; |
| 1497 | sl_unlock(&from->lock); |
| 1498 | |
| 1499 | if (from_msg == NULL) { |
| 1500 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1501 | } |
| 1502 | |
| 1503 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1504 | * Copy the memory region descriptor to a fresh page from the memory |
| 1505 | * pool. This prevents the sender from changing it underneath us, and |
| 1506 | * 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] | 1507 | */ |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1508 | if (length > HF_MAILBOX_SIZE || length > MM_PPOOL_ENTRY_SIZE) { |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1509 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1510 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1511 | memory_region = |
| 1512 | (struct spci_memory_region *)mpool_alloc(&api_page_pool); |
| 1513 | if (memory_region == NULL) { |
| 1514 | dlog_verbose("Failed to allocate memory region copy.\n"); |
| 1515 | return spci_error(SPCI_NO_MEMORY); |
| 1516 | } |
| 1517 | memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, length); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1518 | |
| 1519 | /* The sender must match the caller. */ |
| 1520 | if (memory_region->sender != from->id) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1521 | dlog_verbose("Memory region sender doesn't match caller.\n"); |
| 1522 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1523 | goto out; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | if (memory_region->attribute_count != 1) { |
| 1527 | /* Hafnium doesn't support multi-way memory sharing for now. */ |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1528 | dlog_verbose( |
| 1529 | "Multi-way memory sharing not supported (got %d " |
| 1530 | "attribute descriptors, expected 0).\n", |
| 1531 | memory_region->attribute_count); |
| 1532 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1533 | goto out; |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * Ensure that the receiver VM exists and isn't the same as the sender. |
| 1538 | */ |
| 1539 | to = vm_find(memory_region->attributes[0].receiver); |
| 1540 | if (to == NULL || to == from) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1541 | dlog_verbose("Invalid receiver.\n"); |
| 1542 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1543 | goto out; |
| 1544 | } |
| 1545 | |
| 1546 | if (to->id == HF_TEE_VM_ID) { |
| 1547 | /* |
| 1548 | * The 'to' VM lock is only needed in the case that it is the |
| 1549 | * TEE VM. |
| 1550 | */ |
| 1551 | struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from); |
| 1552 | |
| 1553 | if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) { |
| 1554 | ret = spci_error(SPCI_BUSY); |
| 1555 | goto out_unlock; |
| 1556 | } |
| 1557 | |
| 1558 | ret = spci_memory_send(to, vm_to_from_lock.vm2, memory_region, |
| 1559 | length, share_func, &api_page_pool); |
| 1560 | /* |
| 1561 | * spci_memory_send takes ownership of the memory_region, so |
| 1562 | * make sure we don't free it. |
| 1563 | */ |
| 1564 | memory_region = NULL; |
| 1565 | |
| 1566 | if (ret.func == SPCI_SUCCESS_32 && to->id == HF_TEE_VM_ID) { |
| 1567 | /* Forward memory send message on to TEE. */ |
| 1568 | memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, |
| 1569 | memory_region, length); |
| 1570 | mpool_free(&api_page_pool, memory_region); |
| 1571 | memory_region = NULL; |
| 1572 | to->mailbox.recv_size = length; |
| 1573 | to->mailbox.recv_sender = from->id; |
| 1574 | to->mailbox.recv_func = share_func; |
| 1575 | ret = deliver_msg(vm_to_from_lock.vm1, from->id, |
| 1576 | current, next); |
| 1577 | } |
| 1578 | |
| 1579 | out_unlock: |
| 1580 | vm_unlock(&vm_to_from_lock.vm1); |
| 1581 | vm_unlock(&vm_to_from_lock.vm2); |
| 1582 | } else { |
| 1583 | struct vm_locked from_locked = vm_lock(from); |
| 1584 | |
| 1585 | ret = spci_memory_send(to, from_locked, memory_region, length, |
| 1586 | share_func, &api_page_pool); |
| 1587 | /* |
| 1588 | * spci_memory_send takes ownership of the memory_region, so |
| 1589 | * make sure we don't free it. |
| 1590 | */ |
| 1591 | memory_region = NULL; |
| 1592 | |
| 1593 | vm_unlock(&from_locked); |
| 1594 | } |
| 1595 | |
| 1596 | out: |
| 1597 | if (memory_region != NULL) { |
| 1598 | mpool_free(&api_page_pool, memory_region); |
| 1599 | } |
| 1600 | |
| 1601 | return ret; |
| 1602 | } |
| 1603 | |
Andrew Walbran | 382e918 | 2020-03-04 11:27:27 +0000 | [diff] [blame] | 1604 | struct spci_value api_spci_mem_retrieve_req( |
| 1605 | ipaddr_t address, uint32_t page_count, uint32_t fragment_length, |
| 1606 | uint32_t length, spci_cookie_t cookie, struct vcpu *current) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1607 | { |
| 1608 | struct vm *to = current->vm; |
| 1609 | struct vm_locked to_locked; |
| 1610 | const void *to_msg; |
| 1611 | struct spci_memory_retrieve_request *retrieve_request; |
| 1612 | uint32_t message_buffer_size; |
| 1613 | struct spci_value ret; |
| 1614 | |
| 1615 | if (ipa_addr(address) != 0 || page_count != 0) { |
| 1616 | /* |
| 1617 | * Hafnium only supports passing the descriptor in the TX |
| 1618 | * mailbox. |
| 1619 | */ |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1620 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1621 | } |
| 1622 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1623 | if (fragment_length == length && cookie != 0) { |
| 1624 | /* Cookie is only allowed if there are multiple fragments. */ |
| 1625 | dlog_verbose("Unexpected cookie %d.\n", cookie); |
| 1626 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1627 | } |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1628 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1629 | retrieve_request = |
| 1630 | (struct spci_memory_retrieve_request *)cpu_get_buffer( |
| 1631 | current->cpu); |
| 1632 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 1633 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 1634 | dlog_verbose("Retrieve request too long.\n"); |
| 1635 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 1636 | } |
| 1637 | |
| 1638 | to_locked = vm_lock(to); |
| 1639 | to_msg = to->mailbox.send; |
| 1640 | |
| 1641 | if (to_msg == NULL) { |
| 1642 | dlog_verbose("TX buffer not setup.\n"); |
| 1643 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1644 | goto out; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * Copy the retrieve request descriptor to an internal buffer, so that |
| 1649 | * the caller can't change it underneath us. |
| 1650 | */ |
| 1651 | memcpy_s(retrieve_request, message_buffer_size, to_msg, length); |
| 1652 | |
| 1653 | if (msg_receiver_busy(to_locked, NULL, false)) { |
| 1654 | /* |
| 1655 | * Can't retrieve memory information if the mailbox is not |
| 1656 | * available. |
| 1657 | */ |
| 1658 | dlog_verbose("RX buffer not ready.\n"); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1659 | ret = spci_error(SPCI_BUSY); |
| 1660 | goto out; |
| 1661 | } |
| 1662 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1663 | ret = spci_memory_retrieve(to_locked, retrieve_request, length, |
| 1664 | &api_page_pool); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1665 | |
| 1666 | out: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1667 | vm_unlock(&to_locked); |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | struct spci_value api_spci_mem_relinquish(struct vcpu *current) |
| 1672 | { |
| 1673 | struct vm *from = current->vm; |
| 1674 | struct vm_locked from_locked; |
| 1675 | const void *from_msg; |
| 1676 | struct spci_mem_relinquish *relinquish_request; |
| 1677 | uint32_t message_buffer_size; |
| 1678 | struct spci_value ret; |
| 1679 | uint32_t length; |
| 1680 | |
| 1681 | from_locked = vm_lock(from); |
| 1682 | from_msg = from->mailbox.send; |
| 1683 | |
| 1684 | if (from_msg == NULL) { |
| 1685 | dlog_verbose("TX buffer not setup.\n"); |
| 1686 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1687 | goto out; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Calculate length from relinquish descriptor before copying. We will |
| 1692 | * check again later to make sure it hasn't changed. |
| 1693 | */ |
| 1694 | length = sizeof(struct spci_mem_relinquish) + |
| 1695 | ((struct spci_mem_relinquish *)from_msg)->endpoint_count * |
| 1696 | sizeof(spci_vm_id_t); |
| 1697 | /* |
| 1698 | * Copy the relinquish descriptor to an internal buffer, so that the |
| 1699 | * caller can't change it underneath us. |
| 1700 | */ |
| 1701 | relinquish_request = |
| 1702 | (struct spci_mem_relinquish *)cpu_get_buffer(current->cpu); |
| 1703 | message_buffer_size = cpu_get_buffer_size(current->cpu); |
| 1704 | if (length > HF_MAILBOX_SIZE || length > message_buffer_size) { |
| 1705 | dlog_verbose("Relinquish message too long.\n"); |
| 1706 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1707 | goto out; |
| 1708 | } |
| 1709 | memcpy_s(relinquish_request, message_buffer_size, from_msg, length); |
| 1710 | |
| 1711 | if (sizeof(struct spci_mem_relinquish) + |
| 1712 | relinquish_request->endpoint_count * sizeof(spci_vm_id_t) != |
| 1713 | length) { |
| 1714 | dlog_verbose( |
| 1715 | "Endpoint count changed while copying to internal " |
| 1716 | "buffer.\n"); |
| 1717 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1718 | goto out; |
| 1719 | } |
| 1720 | |
| 1721 | ret = spci_memory_relinquish(from_locked, relinquish_request, |
| 1722 | &api_page_pool); |
| 1723 | |
| 1724 | out: |
| 1725 | vm_unlock(&from_locked); |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
| 1729 | struct spci_value api_spci_mem_reclaim(uint32_t handle, uint32_t flags, |
| 1730 | struct vcpu *current) |
| 1731 | { |
| 1732 | struct vm *to = current->vm; |
| 1733 | struct vm_locked to_locked; |
| 1734 | struct spci_value ret; |
| 1735 | |
| 1736 | to_locked = vm_lock(to); |
| 1737 | |
| 1738 | if ((handle & SPCI_MEMORY_HANDLE_ALLOCATOR_MASK) == |
| 1739 | SPCI_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) { |
| 1740 | ret = spci_memory_reclaim(to_locked, handle, |
| 1741 | flags & SPCI_MEM_RECLAIM_CLEAR, |
| 1742 | &api_page_pool); |
| 1743 | } else { |
| 1744 | dlog_verbose( |
| 1745 | "Tried to reclaim handle %#x not allocated by " |
| 1746 | "hypervisor.\n", |
| 1747 | handle); |
| 1748 | ret = spci_error(SPCI_INVALID_PARAMETERS); |
| 1749 | } |
| 1750 | |
| 1751 | vm_unlock(&to_locked); |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 1752 | |
| 1753 | return ret; |
| 1754 | } |