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 | /** |
| 167 | * Prepares the vcpu to run by updating its state and fetching whether a return |
| 168 | * value needs to be forced onto the vCPU. |
| 169 | */ |
| 170 | static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu, |
| 171 | struct retval_state *vcpu_retval) |
| 172 | { |
| 173 | bool ret; |
| 174 | |
| 175 | sl_lock(&vcpu->lock); |
| 176 | if (vcpu->state != vcpu_state_ready) { |
| 177 | ret = false; |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | vcpu->cpu = current->cpu; |
| 182 | vcpu->state = vcpu_state_running; |
| 183 | |
| 184 | /* Fetch return value to inject into vCPU if there is one. */ |
| 185 | *vcpu_retval = vcpu->retval; |
| 186 | if (vcpu_retval->force) { |
| 187 | vcpu->retval.force = false; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Wait until the registers become available. Care must be taken when |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 192 | * looping on this: it shouldn't be done while holding other locks to |
| 193 | * avoid deadlocks. |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 194 | */ |
| 195 | while (!vcpu->regs_available) { |
| 196 | sl_unlock(&vcpu->lock); |
| 197 | sl_lock(&vcpu->lock); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Mark the registers as unavailable now that we're about to reflect |
| 202 | * them onto the real registers. This will also prevent another physical |
| 203 | * CPU from trying to read these registers. |
| 204 | */ |
| 205 | vcpu->regs_available = false; |
| 206 | |
| 207 | ret = true; |
| 208 | |
| 209 | out: |
| 210 | sl_unlock(&vcpu->lock); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 215 | * Runs the given vcpu of the given vm. |
| 216 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 217 | 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] | 218 | const struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 219 | struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 220 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 221 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 222 | struct vcpu *vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 223 | struct retval_state vcpu_retval; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 224 | struct hf_vcpu_run_return ret = { |
| 225 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 226 | }; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 227 | |
| 228 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 229 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 230 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 231 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 232 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 233 | /* Only secondary VM vcpus can be run. */ |
| 234 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 235 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 236 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 237 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 238 | /* The requested VM must exist. */ |
| 239 | vm = vm_get(vm_id); |
| 240 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 241 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 245 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 246 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 247 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 248 | |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 249 | /* Update state if allowed. */ |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 250 | vcpu = &vm->vcpus[vcpu_idx]; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 251 | if (!api_vcpu_prepare_run(current, vcpu, &vcpu_retval)) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 252 | ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 253 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 254 | } |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 255 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 256 | /* Switch to the vcpu. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 257 | *next = vcpu; |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 258 | |
Andrew Scull | 33fecd3 | 2019-01-08 14:48:27 +0000 | [diff] [blame] | 259 | /* |
| 260 | * Set a placeholder return code to the scheduler. This will be |
| 261 | * overwritten when the switch back to the primary occurs. |
| 262 | */ |
| 263 | ret.code = HF_VCPU_RUN_PREEMPTED; |
| 264 | |
| 265 | /* Update return value for the next vcpu if one was injected. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 266 | if (vcpu_retval.force) { |
| 267 | arch_regs_set_retval(&vcpu->regs, vcpu_retval.value); |
| 268 | } |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 269 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 270 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 271 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 275 | * Check that the mode indicates memory that is valid, owned and exclusive. |
| 276 | */ |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame] | 277 | static bool api_mode_valid_owned_and_exclusive(int mode) |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 278 | { |
| 279 | return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) == |
| 280 | 0; |
| 281 | } |
| 282 | |
| 283 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 284 | * Configures the VM to send/receive data through the specified pages. The pages |
| 285 | * must not be shared. |
| 286 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 287 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, |
| 288 | const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 289 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 290 | struct vm *vm = current->vm; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 291 | paddr_t pa_send_begin; |
| 292 | paddr_t pa_send_end; |
| 293 | paddr_t pa_recv_begin; |
| 294 | paddr_t pa_recv_end; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 295 | int orig_send_mode; |
| 296 | int orig_recv_mode; |
| 297 | struct mpool local_page_pool; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 298 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 299 | |
| 300 | /* Fail if addresses are not page-aligned. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 301 | if ((ipa_addr(send) & (PAGE_SIZE - 1)) || |
| 302 | (ipa_addr(recv) & (PAGE_SIZE - 1))) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 303 | return -1; |
| 304 | } |
| 305 | |
Andrew Scull | c2eb6a3 | 2018-12-13 16:54:24 +0000 | [diff] [blame] | 306 | /* Convert to physical addresses. */ |
| 307 | pa_send_begin = pa_from_ipa(send); |
| 308 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 309 | |
| 310 | pa_recv_begin = pa_from_ipa(recv); |
| 311 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
| 312 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 313 | /* Fail if the same page is used for the send and receive pages. */ |
| 314 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | sl_lock(&vm->lock); |
| 319 | |
| 320 | /* We only allow these to be setup once. */ |
| 321 | if (vm->mailbox.send || vm->mailbox.recv) { |
| 322 | goto fail; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Ensure the pages are valid, owned and exclusive to the VM and that |
| 327 | * the VM has the required access to the memory. |
| 328 | */ |
| 329 | if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE), |
| 330 | &orig_send_mode) || |
| 331 | !api_mode_valid_owned_and_exclusive(orig_send_mode) || |
| 332 | (orig_send_mode & MM_MODE_R) == 0 || |
| 333 | (orig_send_mode & MM_MODE_W) == 0) { |
| 334 | goto fail; |
| 335 | } |
| 336 | |
| 337 | if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE), |
| 338 | &orig_recv_mode) || |
| 339 | !api_mode_valid_owned_and_exclusive(orig_recv_mode) || |
| 340 | (orig_recv_mode & MM_MODE_R) == 0) { |
| 341 | goto fail; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Create a local pool so any freed memory can't be used by another |
| 346 | * thread. This is to ensure the original mapping can be restored if any |
| 347 | * stage of the process fails. |
| 348 | */ |
| 349 | mpool_init_with_fallback(&local_page_pool, &api_page_pool); |
| 350 | |
| 351 | /* Take memory ownership away from the VM and mark as shared. */ |
| 352 | if (!mm_vm_identity_map( |
| 353 | &vm->ptable, pa_send_begin, pa_send_end, |
| 354 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W, |
| 355 | NULL, &local_page_pool)) { |
| 356 | goto fail_free_pool; |
| 357 | } |
| 358 | |
| 359 | if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end, |
| 360 | MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R, |
| 361 | NULL, &local_page_pool)) { |
| 362 | /* TODO: partial defrag of failed range. */ |
| 363 | /* Recover any memory consumed in failed mapping. */ |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 364 | mm_vm_defrag(&vm->ptable, &local_page_pool); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 365 | goto fail_undo_send; |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 368 | /* 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] | 369 | vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end, |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 370 | MM_MODE_R, &local_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 371 | if (!vm->mailbox.send) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 372 | /* TODO: partial defrag of failed range. */ |
| 373 | /* Recover any memory consumed in failed mapping. */ |
| 374 | mm_defrag(&local_page_pool); |
| 375 | goto fail_undo_send_and_recv; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Map the receive page as writable in the hypervisor address space. On |
| 380 | * failure, unmap the send page before returning. |
| 381 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 382 | vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end, |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 383 | MM_MODE_W, &local_page_pool); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 384 | if (!vm->mailbox.recv) { |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 385 | /* TODO: partial defrag of failed range. */ |
| 386 | /* Recover any memory consumed in failed mapping. */ |
| 387 | mm_defrag(&local_page_pool); |
| 388 | goto fail_undo_all; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 391 | /* TODO: Notify any waiters. */ |
| 392 | |
| 393 | ret = 0; |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 394 | goto exit; |
| 395 | |
| 396 | /* |
| 397 | * The following mappings will not require more memory than is available |
| 398 | * in the local pool. |
| 399 | */ |
| 400 | fail_undo_all: |
| 401 | vm->mailbox.send = NULL; |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 402 | mm_unmap(pa_send_begin, pa_send_end, &local_page_pool); |
Andrew Scull | 220e621 | 2018-12-21 18:09:00 +0000 | [diff] [blame] | 403 | |
| 404 | fail_undo_send_and_recv: |
| 405 | mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end, |
| 406 | orig_recv_mode, NULL, &local_page_pool); |
| 407 | |
| 408 | fail_undo_send: |
| 409 | mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end, |
| 410 | orig_send_mode, NULL, &local_page_pool); |
| 411 | |
| 412 | fail_free_pool: |
| 413 | mpool_fini(&local_page_pool); |
| 414 | |
| 415 | fail: |
| 416 | ret = -1; |
| 417 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 418 | exit: |
| 419 | sl_unlock(&vm->lock); |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 425 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 426 | * and notifies the recipient. |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 427 | * |
| 428 | * If the recipient's receive buffer is busy, it can optionally register the |
| 429 | * 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] | 430 | */ |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 431 | int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify, |
| 432 | struct vcpu *current, struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 433 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 434 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 435 | struct vm *to; |
| 436 | const void *from_buf; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 437 | uint16_t vcpu; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 438 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 439 | |
Wedson Almeida Filho | 17c997f | 2019-01-09 18:50:09 +0000 | [diff] [blame] | 440 | (void)notify; |
| 441 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 442 | /* Limit the size of transfer. */ |
| 443 | if (size > HF_MAILBOX_SIZE) { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 448 | if (vm_id == from->id) { |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | /* Ensure the target VM exists. */ |
| 453 | to = vm_get(vm_id); |
| 454 | if (to == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 458 | /* |
| 459 | * Check that the sender has configured its send buffer. It is safe to |
| 460 | * use from_buf after releasing the lock because the buffer cannot be |
| 461 | * modified once it's configured. |
| 462 | */ |
| 463 | sl_lock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 464 | from_buf = from->mailbox.send; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 465 | sl_unlock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 466 | if (from_buf == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 467 | return -1; |
| 468 | } |
| 469 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 470 | sl_lock(&to->lock); |
| 471 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 472 | if (to->mailbox.state != mailbox_state_empty || |
| 473 | to->mailbox.recv == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 474 | /* Fail if the target isn't currently ready to receive data. */ |
| 475 | ret = -1; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 476 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 479 | /* Copy data. */ |
| 480 | memcpy(to->mailbox.recv, from_buf, size); |
| 481 | to->mailbox.recv_bytes = size; |
| 482 | to->mailbox.recv_from_id = from->id; |
| 483 | to->mailbox.state = mailbox_state_read; |
| 484 | |
| 485 | /* Messages for the primary VM are delivered directly. */ |
| 486 | if (to->id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 487 | struct hf_vcpu_run_return primary_ret = { |
| 488 | .code = HF_VCPU_RUN_MESSAGE, |
| 489 | .message.size = size, |
| 490 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 491 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 492 | *next = api_switch_to_primary(current, primary_ret, |
| 493 | vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 494 | ret = 0; |
| 495 | goto out; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Try to find a vcpu to handle the message and tell the scheduler to |
| 500 | * run it. |
| 501 | */ |
| 502 | if (to->mailbox.recv_waiter == NULL) { |
| 503 | /* |
| 504 | * The scheduler must choose a vcpu to interrupt so it can |
| 505 | * handle the message. |
| 506 | */ |
| 507 | to->mailbox.state = mailbox_state_received; |
| 508 | vcpu = HF_INVALID_VCPU; |
| 509 | } else { |
| 510 | struct vcpu *to_vcpu = to->mailbox.recv_waiter; |
| 511 | |
| 512 | /* |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 513 | * Take target vcpu out of waiter list and mark it as ready to |
| 514 | * run again. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 515 | */ |
| 516 | sl_lock(&to_vcpu->lock); |
| 517 | to->mailbox.recv_waiter = to_vcpu->mailbox_next; |
| 518 | to_vcpu->state = vcpu_state_ready; |
| 519 | |
| 520 | /* Return from HF_MAILBOX_RECEIVE. */ |
Wedson Almeida Filho | 0330611 | 2018-11-26 00:08:03 +0000 | [diff] [blame] | 521 | to_vcpu->retval.force = true; |
| 522 | to_vcpu->retval.value = hf_mailbox_receive_return_encode( |
| 523 | (struct hf_mailbox_receive_return){ |
| 524 | .vm_id = to->mailbox.recv_from_id, |
| 525 | .size = size, |
| 526 | }); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 527 | |
| 528 | sl_unlock(&to_vcpu->lock); |
| 529 | |
| 530 | vcpu = to_vcpu - to->vcpus; |
| 531 | } |
| 532 | |
| 533 | /* Return to the primary VM directly or with a switch. */ |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 534 | if (from->id == HF_PRIMARY_VM_ID) { |
| 535 | ret = vcpu; |
| 536 | } else { |
| 537 | struct hf_vcpu_run_return primary_ret = { |
| 538 | .code = HF_VCPU_RUN_WAKE_UP, |
| 539 | .wake_up.vm_id = to->id, |
| 540 | .wake_up.vcpu = vcpu, |
| 541 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 542 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 543 | *next = api_switch_to_primary(current, primary_ret, |
| 544 | vcpu_state_ready); |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 545 | ret = 0; |
| 546 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 547 | |
| 548 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 549 | sl_unlock(&to->lock); |
| 550 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 551 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 555 | * Receives a message from the mailbox. If one isn't available, this function |
| 556 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 557 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 558 | * 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] | 559 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 560 | struct hf_mailbox_receive_return api_mailbox_receive(bool block, |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 561 | struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 562 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 563 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 564 | struct vm *vm = current->vm; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 565 | struct hf_mailbox_receive_return ret = { |
| 566 | .vm_id = HF_INVALID_VM_ID, |
| 567 | }; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 568 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 569 | /* |
| 570 | * The primary VM will receive messages as a status code from running |
| 571 | * vcpus and must not call this function. |
| 572 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 573 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 574 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 578 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 579 | /* Return pending messages without blocking. */ |
| 580 | if (vm->mailbox.state == mailbox_state_received) { |
| 581 | vm->mailbox.state = mailbox_state_read; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 582 | ret.vm_id = vm->mailbox.recv_from_id; |
| 583 | ret.size = vm->mailbox.recv_bytes; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 584 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 585 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 586 | |
| 587 | /* No pending message so fail if not allowed to block. */ |
| 588 | if (!block) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 589 | goto out; |
| 590 | } |
| 591 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 592 | sl_lock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 593 | |
| 594 | /* Push vcpu into waiter list. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 595 | current->mailbox_next = vm->mailbox.recv_waiter; |
| 596 | vm->mailbox.recv_waiter = current; |
| 597 | sl_unlock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 598 | |
| 599 | /* Switch back to primary vm to block. */ |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 600 | { |
| 601 | struct hf_vcpu_run_return run_return = { |
| 602 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 603 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 604 | |
Andrew Walbran | b481655 | 2018-12-05 17:35:42 +0000 | [diff] [blame] | 605 | *next = api_switch_to_primary(current, run_return, |
| 606 | vcpu_state_blocked_mailbox); |
| 607 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 608 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 609 | sl_unlock(&vm->lock); |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 615 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 616 | * must have copied out all data they wish to preserve as new messages will |
| 617 | * overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 618 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 619 | int64_t api_mailbox_clear(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 620 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 621 | struct vm *vm = current->vm; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 622 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 623 | |
| 624 | sl_lock(&vm->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 625 | if (vm->mailbox.state == mailbox_state_read) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 626 | ret = 0; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 627 | vm->mailbox.state = mailbox_state_empty; |
| 628 | } else { |
| 629 | ret = -1; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 630 | } |
| 631 | sl_unlock(&vm->lock); |
| 632 | |
| 633 | if (ret == 0) { |
| 634 | /* TODO: Notify waiters, if any. */ |
| 635 | } |
| 636 | |
| 637 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 638 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 639 | |
| 640 | /** |
| 641 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 642 | * |
| 643 | * Returns 0 on success, or -1 if the intid is invalid. |
| 644 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 645 | 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] | 646 | { |
| 647 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 648 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 649 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 650 | if (intid >= HF_NUM_INTIDS) { |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | sl_lock(¤t->lock); |
| 655 | if (enable) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 656 | /* |
| 657 | * If it is pending and was not enabled before, increment the |
| 658 | * count. |
| 659 | */ |
| 660 | if (current->interrupts.interrupt_pending[intid_index] & |
| 661 | ~current->interrupts.interrupt_enabled[intid_index] & |
| 662 | intid_mask) { |
| 663 | current->interrupts.enabled_and_pending_count++; |
| 664 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 665 | current->interrupts.interrupt_enabled[intid_index] |= |
| 666 | intid_mask; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 667 | } else { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 668 | /* |
| 669 | * If it is pending and was enabled before, decrement the count. |
| 670 | */ |
| 671 | if (current->interrupts.interrupt_pending[intid_index] & |
| 672 | current->interrupts.interrupt_enabled[intid_index] & |
| 673 | intid_mask) { |
| 674 | current->interrupts.enabled_and_pending_count--; |
| 675 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 676 | current->interrupts.interrupt_enabled[intid_index] &= |
| 677 | ~intid_mask; |
| 678 | } |
| 679 | |
| 680 | sl_unlock(¤t->lock); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 686 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 687 | * HF_INVALID_INTID if there are no pending interrupts. |
| 688 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 689 | uint32_t api_interrupt_get(struct vcpu *current) |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 690 | { |
| 691 | uint8_t i; |
| 692 | uint32_t first_interrupt = HF_INVALID_INTID; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Find the first enabled and pending interrupt ID, return it, and |
| 696 | * deactivate it. |
| 697 | */ |
| 698 | sl_lock(¤t->lock); |
| 699 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 700 | uint32_t enabled_and_pending = |
| 701 | current->interrupts.interrupt_enabled[i] & |
| 702 | current->interrupts.interrupt_pending[i]; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 703 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 704 | if (enabled_and_pending != 0) { |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 705 | uint8_t bit_index = ctz(enabled_and_pending); |
| 706 | /* |
| 707 | * Mark it as no longer pending and decrement the count. |
| 708 | */ |
| 709 | current->interrupts.interrupt_pending[i] &= |
| 710 | ~(1u << bit_index); |
| 711 | current->interrupts.enabled_and_pending_count--; |
| 712 | first_interrupt = |
| 713 | i * INTERRUPT_REGISTER_BITS + bit_index; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 717 | |
| 718 | sl_unlock(¤t->lock); |
| 719 | return first_interrupt; |
| 720 | } |
| 721 | |
| 722 | /** |
Andrew Walbran | 4cf217a | 2018-12-14 15:24:50 +0000 | [diff] [blame] | 723 | * 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] | 724 | * given VM and vCPU. |
| 725 | */ |
| 726 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 727 | struct vcpu *current) |
| 728 | { |
| 729 | uint32_t current_vm_id = current->vm->id; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 730 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 731 | /* |
| 732 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 733 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 734 | */ |
| 735 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 736 | current_vm_id == target_vm_id; |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 741 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 742 | * when the vCPU is next run, which is up to the scheduler. |
| 743 | * |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 744 | * Returns: |
| 745 | * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt |
| 746 | * ID is invalid, or the current VM is not allowed to inject interrupts to |
| 747 | * the target VM. |
| 748 | * - 0 on success if no further action is needed. |
| 749 | * - 1 if it was called by the primary VM and the primary VM now needs to wake |
| 750 | * up or kick the target vCPU. |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 751 | */ |
Wedson Almeida Filho | c559d13 | 2019-01-09 19:33:40 +0000 | [diff] [blame] | 752 | 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] | 753 | uint32_t intid, struct vcpu *current, |
| 754 | struct vcpu **next) |
| 755 | { |
| 756 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 757 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
| 758 | struct vcpu *target_vcpu; |
| 759 | struct vm *target_vm = vm_get(target_vm_id); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 760 | bool need_vm_lock; |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 761 | int64_t ret = 0; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 762 | |
| 763 | if (intid >= HF_NUM_INTIDS) { |
| 764 | return -1; |
| 765 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 766 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 767 | if (target_vm == NULL) { |
| 768 | return -1; |
| 769 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 770 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 771 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
| 772 | /* The requested vcpu must exist. */ |
| 773 | return -1; |
| 774 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 775 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 776 | if (!is_injection_allowed(target_vm_id, current)) { |
| 777 | return -1; |
| 778 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 779 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 780 | target_vcpu = &target_vm->vcpus[target_vcpu_idx]; |
| 781 | |
| 782 | dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid, |
| 783 | target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id); |
| 784 | |
| 785 | sl_lock(&target_vcpu->lock); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 786 | /* |
| 787 | * If we need the target_vm lock we need to release the target_vcpu lock |
| 788 | * first to maintain the correct order of locks. In-between releasing |
| 789 | * and acquiring it again the state of the vCPU could change in such a |
| 790 | * way that we don't actually need to touch the target_vm after all, but |
| 791 | * that's alright: we'll take the target_vm lock anyway, but it's safe, |
| 792 | * just perhaps a little slow in this unusual case. The reverse is not |
| 793 | * possible: if need_vm_lock is false, we don't release the target_vcpu |
| 794 | * lock until we are done, so nothing should change in such as way that |
| 795 | * we need the VM lock after all. |
| 796 | */ |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 797 | need_vm_lock = |
| 798 | (target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 799 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
| 800 | intid_mask) && |
| 801 | target_vcpu->state == vcpu_state_blocked_mailbox; |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 802 | if (need_vm_lock) { |
| 803 | sl_unlock(&target_vcpu->lock); |
| 804 | sl_lock(&target_vm->lock); |
| 805 | sl_lock(&target_vcpu->lock); |
| 806 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 807 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 808 | /* |
| 809 | * We only need to change state and (maybe) trigger a virtual IRQ if it |
| 810 | * is enabled and was not previously pending. Otherwise we can skip |
| 811 | * everything except setting the pending bit. |
| 812 | * |
| 813 | * If you change this logic make sure to update the need_vm_lock logic |
| 814 | * above to match. |
| 815 | */ |
| 816 | if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 817 | ~target_vcpu->interrupts.interrupt_pending[intid_index] & |
| 818 | intid_mask)) { |
| 819 | goto out; |
| 820 | } |
| 821 | |
| 822 | /* Increment the count. */ |
| 823 | target_vcpu->interrupts.enabled_and_pending_count++; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 824 | |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 825 | /* |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 826 | * Only need to update state if there was not already an |
| 827 | * interrupt enabled and pending. |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 828 | */ |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 829 | if (target_vcpu->interrupts.enabled_and_pending_count != 1) { |
| 830 | goto out; |
| 831 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 832 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 833 | if (target_vcpu->state == vcpu_state_blocked_interrupt) { |
| 834 | target_vcpu->state = vcpu_state_ready; |
| 835 | } else if (target_vcpu->state == vcpu_state_blocked_mailbox) { |
| 836 | /* |
| 837 | * If you change this logic make sure to update the need_vm_lock |
| 838 | * logic above to match. |
| 839 | */ |
| 840 | target_vcpu->state = vcpu_state_ready; |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 841 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 842 | /* Take target vCPU out of mailbox recv_waiter list. */ |
| 843 | /* |
| 844 | * TODO: Consider using a doubly-linked list for |
| 845 | * the receive waiter list to avoid the linear |
| 846 | * search here. |
| 847 | */ |
| 848 | struct vcpu **previous_next_pointer = |
| 849 | &target_vm->mailbox.recv_waiter; |
| 850 | while (*previous_next_pointer != NULL && |
| 851 | *previous_next_pointer != target_vcpu) { |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 852 | /* |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 853 | * TODO(qwandor): Do we need to lock the vCPUs somehow |
| 854 | * while we walk the linked list, or is the VM lock |
| 855 | * enough? |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 856 | */ |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 857 | previous_next_pointer = |
| 858 | &(*previous_next_pointer)->mailbox_next; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 859 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 860 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 861 | if (*previous_next_pointer == NULL) { |
| 862 | dlog("Target VCPU state is vcpu_state_blocked_mailbox " |
| 863 | "but is not in VM mailbox waiter list. This " |
| 864 | "should never happen.\n"); |
| 865 | } else { |
| 866 | *previous_next_pointer = target_vcpu->mailbox_next; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 870 | if (current->vm->id == HF_PRIMARY_VM_ID) { |
| 871 | /* |
| 872 | * If the call came from the primary VM, let it know that it |
| 873 | * should run or kick the target vCPU. |
| 874 | */ |
| 875 | ret = 1; |
| 876 | } else if (current != target_vcpu) { |
| 877 | /* |
| 878 | * Switch to the primary so that it can switch to the target, or |
| 879 | * kick it if it is already running on a different physical CPU. |
| 880 | */ |
| 881 | struct hf_vcpu_run_return ret = { |
| 882 | .code = HF_VCPU_RUN_WAKE_UP, |
| 883 | .wake_up.vm_id = target_vm_id, |
| 884 | .wake_up.vcpu = target_vcpu_idx, |
| 885 | }; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 886 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 887 | *next = api_switch_to_primary(current, ret, vcpu_state_ready); |
| 888 | } |
| 889 | |
| 890 | out: |
| 891 | /* Either way, make it pending. */ |
| 892 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 893 | |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 894 | sl_unlock(&target_vcpu->lock); |
Andrew Walbran | 69520dc | 2018-12-06 11:39:38 +0000 | [diff] [blame] | 895 | if (need_vm_lock) { |
| 896 | sl_unlock(&target_vm->lock); |
| 897 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 898 | |
Andrew Walbran | 3d84a26 | 2018-12-13 14:41:19 +0000 | [diff] [blame] | 899 | return ret; |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame] | 900 | } |