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