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