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