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