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 | /** |
| 44 | * Initialies the API page pool by taking ownership of the contents of the given |
| 45 | * page pool. |
| 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 | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 79 | * Returns to the primary vm leaving the current vcpu ready to be scheduled |
| 80 | * again. |
| 81 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 82 | struct vcpu *api_yield(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 83 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 84 | struct hf_vcpu_run_return ret = { |
| 85 | .code = HF_VCPU_RUN_YIELD, |
| 86 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 87 | return api_switch_to_primary(current, ret, vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Puts the current vcpu in wait for interrupt mode, and returns to the primary |
| 92 | * vm. |
| 93 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 94 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 95 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 96 | struct hf_vcpu_run_return ret = { |
| 97 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 98 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 99 | return api_switch_to_primary(current, ret, |
| 100 | vcpu_state_blocked_interrupt); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Andrew Scull | 55c4d8b | 2018-12-18 18:50:18 +0000 | [diff] [blame^] | 104 | * Returns the ID of the VM. |
| 105 | */ |
| 106 | int64_t api_vm_get_id(const struct vcpu *current) |
| 107 | { |
| 108 | return current->vm->id; |
| 109 | } |
| 110 | |
| 111 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 112 | * Returns the number of VMs configured to run. |
| 113 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 114 | int64_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 115 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 116 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the number of vcpus configured in the given VM. |
| 121 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 122 | 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] | 123 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 124 | struct vm *vm; |
| 125 | |
| 126 | /* Only the primary VM needs to know about vcpus for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 127 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 128 | return -1; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 129 | } |
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 | vm = vm_get(vm_id); |
| 132 | if (vm == NULL) { |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 140 | * This function is called by the architecture-specific context switching |
| 141 | * function to indicate that register state for the given vcpu has been saved |
| 142 | * and can therefore be used by other pcpus. |
| 143 | */ |
| 144 | void api_regs_state_saved(struct vcpu *vcpu) |
| 145 | { |
| 146 | sl_lock(&vcpu->lock); |
| 147 | vcpu->regs_available = true; |
| 148 | sl_unlock(&vcpu->lock); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Prepares the vcpu to run by updating its state and fetching whether a return |
| 153 | * value needs to be forced onto the vCPU. |
| 154 | */ |
| 155 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
| 156 | struct retval_state *vcpu_retval) |
| 157 | { |
| 158 | bool ret; |
| 159 | |
| 160 | sl_lock(&vcpu->lock); |
| 161 | if (vcpu->state != vcpu_state_ready) { |
| 162 | ret = false; |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | vcpu->cpu = current->cpu; |
| 167 | vcpu->state = vcpu_state_running; |
| 168 | |
| 169 | /* Fetch return value to inject into vCPU if there is one. */ |
| 170 | *vcpu_retval = vcpu->retval; |
| 171 | if (vcpu_retval->force) { |
| 172 | vcpu->retval.force = false; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Wait until the registers become available. Care must be taken when |
| 177 | * looping on this: it shouldn't be done while holding other locks |
| 178 | * to avoid deadlocks. |
| 179 | */ |
| 180 | while (!vcpu->regs_available) { |
| 181 | sl_unlock(&vcpu->lock); |
| 182 | sl_lock(&vcpu->lock); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Mark the registers as unavailable now that we're about to reflect |
| 187 | * them onto the real registers. This will also prevent another physical |
| 188 | * CPU from trying to read these registers. |
| 189 | */ |
| 190 | vcpu->regs_available = false; |
| 191 | |
| 192 | ret = true; |
| 193 | |
| 194 | out: |
| 195 | sl_unlock(&vcpu->lock); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 200 | * Runs the given vcpu of the given vm. |
| 201 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 202 | 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] | 203 | const struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 204 | struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 205 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 206 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 207 | struct vcpu *vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 208 | struct retval_state vcpu_retval; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 209 | struct hf_vcpu_run_return ret = { |
| 210 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 211 | }; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 212 | |
| 213 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 214 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 215 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 216 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 217 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 218 | /* Only secondary VM vcpus can be run. */ |
| 219 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 220 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 221 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 222 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 223 | /* The requested VM must exist. */ |
| 224 | vm = vm_get(vm_id); |
| 225 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 226 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 230 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 231 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 232 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 233 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 234 | /* Update state if allowed. */ |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 235 | vcpu = &vm->vcpus[vcpu_idx]; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 236 | if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 237 | ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 238 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 239 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 240 | |
| 241 | *next = vcpu; |
| 242 | ret.code = HF_VCPU_RUN_YIELD; |
| 243 | |
| 244 | /* Update return value if one was injected. */ |
| 245 | if (vcpu_retval.force) { |
| 246 | arch_regs_set_retval(&vcpu->regs, vcpu_retval.value); |
| 247 | } |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 248 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 249 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 250 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 254 | * Configures the VM to send/receive data through the specified pages. The pages |
| 255 | * must not be shared. |
| 256 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 257 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, |
| 258 | const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 259 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 260 | struct vm *vm = current->vm; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 261 | paddr_t pa_send_begin; |
| 262 | paddr_t pa_send_end; |
| 263 | paddr_t pa_recv_begin; |
| 264 | paddr_t pa_recv_end; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 265 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 266 | |
| 267 | /* Fail if addresses are not page-aligned. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 268 | if ((ipa_addr(send) & (PAGE_SIZE - 1)) || |
| 269 | (ipa_addr(recv) & (PAGE_SIZE - 1))) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | sl_lock(&vm->lock); |
| 274 | |
| 275 | /* We only allow these to be setup once. */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 276 | if (vm->mailbox.send || vm->mailbox.recv) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 277 | ret = -1; |
| 278 | goto exit; |
| 279 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 280 | |
| 281 | /* |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 282 | * TODO: Once memory sharing is implemented, we need to make sure that |
| 283 | * these pages aren't and won't be shared. |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 284 | */ |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 285 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 286 | /* Ensure the pages are accessible from the VM. */ |
| 287 | if (!mm_vm_is_mapped(&vm->ptable, send, 0) || |
| 288 | !mm_vm_is_mapped(&vm->ptable, recv, 0)) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 289 | ret = -1; |
| 290 | goto exit; |
| 291 | } |
| 292 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 293 | /* Convert to physical addresses. */ |
| 294 | pa_send_begin = pa_from_ipa(send); |
| 295 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 296 | |
| 297 | pa_recv_begin = pa_from_ipa(recv); |
| 298 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
| 299 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 300 | /* Fail if the same page is used for the send and receive pages. */ |
| 301 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
| 302 | ret = -1; |
| 303 | goto exit; |
| 304 | } |
| 305 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 306 | /* 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] | 307 | vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end, |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 308 | MM_MODE_R, &api_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 309 | if (!vm->mailbox.send) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 310 | ret = -1; |
| 311 | goto exit; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Map the receive page as writable in the hypervisor address space. On |
| 316 | * failure, unmap the send page before returning. |
| 317 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 318 | vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end, |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 319 | MM_MODE_W, &api_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 320 | if (!vm->mailbox.recv) { |
| 321 | vm->mailbox.send = NULL; |
Wedson Almeida Filho | 9ed8da5 | 2018-12-17 16:09:11 +0000 | [diff] [blame] | 322 | mm_unmap(pa_send_begin, pa_send_end, 0, &api_page_pool); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 323 | ret = -1; |
| 324 | goto exit; |
| 325 | } |
| 326 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 327 | /* TODO: Notify any waiters. */ |
| 328 | |
| 329 | ret = 0; |
| 330 | exit: |
| 331 | sl_unlock(&vm->lock); |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 337 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 338 | * and notifies the recipient. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 339 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 340 | int64_t api_mailbox_send(uint32_t vm_id, size_t size, struct vcpu *current, |
| 341 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 342 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 343 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 344 | struct vm *to; |
| 345 | const void *from_buf; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 346 | uint16_t vcpu; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 347 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 348 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 349 | /* Limit the size of transfer. */ |
| 350 | if (size > HF_MAILBOX_SIZE) { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 355 | if (vm_id == from->id) { |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | /* Ensure the target VM exists. */ |
| 360 | to = vm_get(vm_id); |
| 361 | if (to == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 362 | return -1; |
| 363 | } |
| 364 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 365 | /* |
| 366 | * Check that the sender has configured its send buffer. It is safe to |
| 367 | * use from_buf after releasing the lock because the buffer cannot be |
| 368 | * modified once it's configured. |
| 369 | */ |
| 370 | sl_lock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 371 | from_buf = from->mailbox.send; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 372 | sl_unlock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 373 | if (from_buf == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 374 | return -1; |
| 375 | } |
| 376 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 377 | sl_lock(&to->lock); |
| 378 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 379 | if (to->mailbox.state != mailbox_state_empty || |
| 380 | to->mailbox.recv == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 381 | /* Fail if the target isn't currently ready to receive data. */ |
| 382 | ret = -1; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 383 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 386 | /* Copy data. */ |
| 387 | memcpy(to->mailbox.recv, from_buf, size); |
| 388 | to->mailbox.recv_bytes = size; |
| 389 | to->mailbox.recv_from_id = from->id; |
| 390 | to->mailbox.state = mailbox_state_read; |
| 391 | |
| 392 | /* Messages for the primary VM are delivered directly. */ |
| 393 | if (to->id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 394 | struct hf_vcpu_run_return primary_ret = { |
| 395 | .code = HF_VCPU_RUN_MESSAGE, |
| 396 | .message.size = size, |
| 397 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 398 | *next = api_switch_to_primary(current, primary_ret, |
| 399 | vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 400 | ret = 0; |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Try to find a vcpu to handle the message and tell the scheduler to |
| 406 | * run it. |
| 407 | */ |
| 408 | if (to->mailbox.recv_waiter == NULL) { |
| 409 | /* |
| 410 | * The scheduler must choose a vcpu to interrupt so it can |
| 411 | * handle the message. |
| 412 | */ |
| 413 | to->mailbox.state = mailbox_state_received; |
| 414 | vcpu = HF_INVALID_VCPU; |
| 415 | } else { |
| 416 | struct vcpu *to_vcpu = to->mailbox.recv_waiter; |
| 417 | |
| 418 | /* |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 419 | * Take target vcpu out of waiter list and mark it as ready to |
| 420 | * run again. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 421 | */ |
| 422 | sl_lock(&to_vcpu->lock); |
| 423 | to->mailbox.recv_waiter = to_vcpu->mailbox_next; |
| 424 | to_vcpu->state = vcpu_state_ready; |
| 425 | |
| 426 | /* Return from HF_MAILBOX_RECEIVE. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 427 | to_vcpu->retval.force = true; |
| 428 | to_vcpu->retval.value = hf_mailbox_receive_return_encode( |
| 429 | (struct hf_mailbox_receive_return){ |
| 430 | .vm_id = to->mailbox.recv_from_id, |
| 431 | .size = size, |
| 432 | }); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 433 | |
| 434 | sl_unlock(&to_vcpu->lock); |
| 435 | |
| 436 | vcpu = to_vcpu - to->vcpus; |
| 437 | } |
| 438 | |
| 439 | /* Return to the primary VM directly or with a switch. */ |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 440 | if (from->id == HF_PRIMARY_VM_ID) { |
| 441 | ret = vcpu; |
| 442 | } else { |
| 443 | struct hf_vcpu_run_return primary_ret = { |
| 444 | .code = HF_VCPU_RUN_WAKE_UP, |
| 445 | .wake_up.vm_id = to->id, |
| 446 | .wake_up.vcpu = vcpu, |
| 447 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 448 | *next = api_switch_to_primary(current, primary_ret, |
| 449 | vcpu_state_ready); |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 450 | ret = 0; |
| 451 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 452 | |
| 453 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 454 | sl_unlock(&to->lock); |
| 455 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 456 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 460 | * Receives a message from the mailbox. If one isn't available, this function |
| 461 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 462 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 463 | * 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] | 464 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 465 | struct hf_mailbox_receive_return api_mailbox_receive(bool block, |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 466 | struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 467 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 468 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 469 | struct vm *vm = current->vm; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 470 | struct hf_mailbox_receive_return ret = { |
| 471 | .vm_id = HF_INVALID_VM_ID, |
| 472 | }; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 473 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 474 | /* |
| 475 | * The primary VM will receive messages as a status code from running |
| 476 | * vcpus and must not call this function. |
| 477 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 478 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 479 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 483 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 484 | /* Return pending messages without blocking. */ |
| 485 | if (vm->mailbox.state == mailbox_state_received) { |
| 486 | vm->mailbox.state = mailbox_state_read; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 487 | ret.vm_id = vm->mailbox.recv_from_id; |
| 488 | ret.size = vm->mailbox.recv_bytes; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 489 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 490 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 491 | |
| 492 | /* No pending message so fail if not allowed to block. */ |
| 493 | if (!block) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 494 | goto out; |
| 495 | } |
| 496 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 497 | sl_lock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 498 | |
| 499 | /* Push vcpu into waiter list. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 500 | current->mailbox_next = vm->mailbox.recv_waiter; |
| 501 | vm->mailbox.recv_waiter = current; |
| 502 | sl_unlock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 503 | |
| 504 | /* Switch back to primary vm to block. */ |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 505 | { |
| 506 | struct hf_vcpu_run_return run_return = { |
| 507 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 508 | }; |
| 509 | *next = api_switch_to_primary(current, run_return, |
| 510 | vcpu_state_blocked_mailbox); |
| 511 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 512 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 513 | sl_unlock(&vm->lock); |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 519 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 520 | * must have copied out all data they wish to preserve as new messages will |
| 521 | * overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 522 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 523 | int64_t api_mailbox_clear(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 524 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 525 | struct vm *vm = current->vm; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 526 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 527 | |
| 528 | sl_lock(&vm->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 529 | if (vm->mailbox.state == mailbox_state_read) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 530 | ret = 0; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 531 | vm->mailbox.state = mailbox_state_empty; |
| 532 | } else { |
| 533 | ret = -1; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 534 | } |
| 535 | sl_unlock(&vm->lock); |
| 536 | |
| 537 | if (ret == 0) { |
| 538 | /* TODO: Notify waiters, if any. */ |
| 539 | } |
| 540 | |
| 541 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 542 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 543 | |
| 544 | /** |
| 545 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 546 | * |
| 547 | * Returns 0 on success, or -1 if the intid is invalid. |
| 548 | */ |
| 549 | int64_t api_enable_interrupt(uint32_t intid, bool enable, struct vcpu *current) |
| 550 | { |
| 551 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 552 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
| 553 | if (intid >= HF_NUM_INTIDS) { |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | sl_lock(¤t->lock); |
| 558 | if (enable) { |
| 559 | current->interrupts.interrupt_enabled[intid_index] |= |
| 560 | intid_mask; |
| 561 | /* If it is pending, change state and trigger a virtual IRQ. */ |
| 562 | if (current->interrupts.interrupt_pending[intid_index] & |
| 563 | intid_mask) { |
| 564 | arch_regs_set_virtual_interrupt(¤t->regs, true); |
| 565 | } |
| 566 | } else { |
| 567 | current->interrupts.interrupt_enabled[intid_index] &= |
| 568 | ~intid_mask; |
| 569 | } |
| 570 | |
| 571 | sl_unlock(¤t->lock); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 577 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 578 | * HF_INVALID_INTID if there are no pending interrupts. |
| 579 | */ |
| 580 | uint32_t api_get_and_acknowledge_interrupt(struct vcpu *current) |
| 581 | { |
| 582 | uint8_t i; |
| 583 | uint32_t first_interrupt = HF_INVALID_INTID; |
| 584 | bool interrupts_remain = false; |
| 585 | |
| 586 | /* |
| 587 | * Find the first enabled and pending interrupt ID, return it, and |
| 588 | * deactivate it. |
| 589 | */ |
| 590 | sl_lock(¤t->lock); |
| 591 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 592 | uint32_t enabled_and_pending = |
| 593 | current->interrupts.interrupt_enabled[i] & |
| 594 | current->interrupts.interrupt_pending[i]; |
| 595 | if (enabled_and_pending == 0) { |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | if (first_interrupt != HF_INVALID_INTID) { |
| 600 | interrupts_remain = true; |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | uint8_t bit_index = ctz(enabled_and_pending); |
| 605 | /* Mark it as no longer pending. */ |
| 606 | current->interrupts.interrupt_pending[i] &= ~(1u << bit_index); |
| 607 | first_interrupt = i * INTERRUPT_REGISTER_BITS + bit_index; |
| 608 | |
| 609 | enabled_and_pending = current->interrupts.interrupt_enabled[i] & |
| 610 | current->interrupts.interrupt_pending[i]; |
| 611 | if (enabled_and_pending != 0) { |
| 612 | interrupts_remain = true; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | /* |
| 617 | * If there are no more enabled and pending interrupts left, clear the |
| 618 | * VI bit. |
| 619 | */ |
| 620 | arch_regs_set_virtual_interrupt(¤t->regs, interrupts_remain); |
| 621 | |
| 622 | sl_unlock(¤t->lock); |
| 623 | return first_interrupt; |
| 624 | } |
| 625 | |
| 626 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 627 | * 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] | 628 | * given VM and vCPU. |
| 629 | */ |
| 630 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 631 | struct vcpu *current) |
| 632 | { |
| 633 | uint32_t current_vm_id = current->vm->id; |
| 634 | /* |
| 635 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 636 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 637 | */ |
| 638 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 639 | current_vm_id == target_vm_id; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 644 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 645 | * when the vCPU is next run, which is up to the scheduler. |
| 646 | * |
| 647 | * Returns 0 on success, or -1 if the target VM or vCPU doesn't exist, the |
| 648 | * interrupt ID is invalid, or the current VM is not allowed to inject |
| 649 | * interrupts to the target VM. |
| 650 | */ |
| 651 | int64_t api_inject_interrupt(uint32_t target_vm_id, uint32_t target_vcpu_idx, |
| 652 | uint32_t intid, struct vcpu *current, |
| 653 | struct vcpu **next) |
| 654 | { |
| 655 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 656 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
| 657 | struct vcpu *target_vcpu; |
| 658 | struct vm *target_vm = vm_get(target_vm_id); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 659 | bool need_vm_lock; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 660 | |
| 661 | if (intid >= HF_NUM_INTIDS) { |
| 662 | return -1; |
| 663 | } |
| 664 | if (target_vm == NULL) { |
| 665 | return -1; |
| 666 | } |
| 667 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
| 668 | /* The requested vcpu must exist. */ |
| 669 | return -1; |
| 670 | } |
| 671 | if (!is_injection_allowed(target_vm_id, current)) { |
| 672 | return -1; |
| 673 | } |
| 674 | target_vcpu = &target_vm->vcpus[target_vcpu_idx]; |
| 675 | |
| 676 | dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid, |
| 677 | target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id); |
| 678 | |
| 679 | sl_lock(&target_vcpu->lock); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 680 | /* |
| 681 | * If we need the target_vm lock we need to release the target_vcpu lock |
| 682 | * first to maintain the correct order of locks. In-between releasing |
| 683 | * and acquiring it again the state of the vCPU could change in such a |
| 684 | * way that we don't actually need to touch the target_vm after all, but |
| 685 | * that's alright: we'll take the target_vm lock anyway, but it's safe, |
| 686 | * just perhaps a little slow in this unusual case. The reverse is not |
| 687 | * possible: if need_vm_lock is false, we don't release the target_vcpu |
| 688 | * lock until we are done, so nothing should change in such as way that |
| 689 | * we need the VM lock after all. |
| 690 | */ |
| 691 | need_vm_lock = (target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 692 | intid_mask) && |
| 693 | target_vcpu->state == vcpu_state_blocked_mailbox; |
| 694 | if (need_vm_lock) { |
| 695 | sl_unlock(&target_vcpu->lock); |
| 696 | sl_lock(&target_vm->lock); |
| 697 | sl_lock(&target_vcpu->lock); |
| 698 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 699 | |
| 700 | /* Make it pending. */ |
| 701 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 702 | |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 703 | /* |
| 704 | * If it is enabled, change state and trigger a virtual IRQ. If you |
| 705 | * change this logic make sure to update the need_vm_lock logic above to |
| 706 | * match. |
| 707 | */ |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 708 | if (target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 709 | intid_mask) { |
| 710 | dlog("IRQ %d is enabled for VM %d VCPU %d, setting VI.\n", |
| 711 | intid, target_vm_id, target_vcpu_idx); |
| 712 | arch_regs_set_virtual_interrupt(&target_vcpu->regs, true); |
| 713 | |
| 714 | if (target_vcpu->state == vcpu_state_blocked_interrupt) { |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 715 | target_vcpu->state = vcpu_state_ready; |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 716 | } else if (target_vcpu->state == vcpu_state_blocked_mailbox) { |
| 717 | /* |
| 718 | * If you change this logic make sure to update the |
| 719 | * need_vm_lock logic above to match. |
| 720 | */ |
| 721 | target_vcpu->state = vcpu_state_ready; |
| 722 | |
| 723 | /* Take target vCPU out of mailbox recv_waiter list. */ |
| 724 | /* |
Andrew Walbran | a793ea0 | 2018-12-12 13:21:47 +0000 | [diff] [blame] | 725 | * TODO: Consider using a doubly-linked list for the |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 726 | * receive waiter list to avoid the linear search here. |
| 727 | */ |
| 728 | struct vcpu **previous_next_pointer = |
| 729 | &target_vm->mailbox.recv_waiter; |
| 730 | while (*previous_next_pointer != NULL && |
| 731 | *previous_next_pointer != target_vcpu) { |
| 732 | /* |
| 733 | * TODO(qwandor): Do we need to lock the vCPUs |
| 734 | * somehow while we walk the linked list, or is |
| 735 | * the VM lock enough? |
| 736 | */ |
| 737 | previous_next_pointer = |
| 738 | &(*previous_next_pointer)->mailbox_next; |
| 739 | } |
| 740 | if (*previous_next_pointer == NULL) { |
| 741 | dlog("Target VCPU state is " |
| 742 | "vcpu_state_blocked_mailbox but is not in " |
| 743 | "VM mailbox waiter list. This should " |
| 744 | "never happen.\n"); |
| 745 | } else { |
| 746 | *previous_next_pointer = |
| 747 | target_vcpu->mailbox_next; |
| 748 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | if (current->vm->id != HF_PRIMARY_VM_ID && |
| 752 | current != target_vcpu) { |
| 753 | /* |
| 754 | * Switch to the primary so that it can switch to the |
| 755 | * target. |
| 756 | */ |
| 757 | struct hf_vcpu_run_return ret = { |
| 758 | .code = HF_VCPU_RUN_WAKE_UP, |
| 759 | .wake_up.vm_id = target_vm_id, |
| 760 | .wake_up.vcpu = target_vcpu_idx, |
| 761 | }; |
| 762 | *next = api_switch_to_primary(current, ret, |
| 763 | vcpu_state_ready); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | sl_unlock(&target_vcpu->lock); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 768 | if (need_vm_lock) { |
| 769 | sl_unlock(&target_vm->lock); |
| 770 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | } |