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