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 | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 41 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 42 | * 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] | 43 | * |
| 44 | * This triggers the scheduling logic to run. Run in the context of secondary VM |
| 45 | * to cause HF_VCPU_RUN to return and the primary VM to regain control of the |
| 46 | * cpu. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 47 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 48 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 49 | struct hf_vcpu_run_return primary_ret, |
| 50 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 51 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 52 | struct vm *primary = vm_get(HF_PRIMARY_VM_ID); |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 53 | struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)]; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 54 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 55 | /* 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] | 56 | arch_regs_set_retval(&next->regs, |
| 57 | hf_vcpu_run_return_encode(primary_ret)); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 58 | |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 59 | /* Mark the current vcpu as waiting. */ |
| 60 | sl_lock(¤t->lock); |
| 61 | current->state = secondary_state; |
| 62 | sl_unlock(¤t->lock); |
| 63 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 64 | return next; |
| 65 | } |
| 66 | |
| 67 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 68 | * Returns to the primary vm leaving the current vcpu ready to be scheduled |
| 69 | * again. |
| 70 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 71 | struct vcpu *api_yield(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 72 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 73 | struct hf_vcpu_run_return ret = { |
| 74 | .code = HF_VCPU_RUN_YIELD, |
| 75 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 76 | return api_switch_to_primary(current, ret, vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Puts the current vcpu in wait for interrupt mode, and returns to the primary |
| 81 | * vm. |
| 82 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 83 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 84 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 85 | struct hf_vcpu_run_return ret = { |
| 86 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 87 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 88 | return api_switch_to_primary(current, ret, |
| 89 | vcpu_state_blocked_interrupt); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 93 | * Returns the number of VMs configured to run. |
| 94 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 95 | int64_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 96 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 97 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the number of vcpus configured in the given VM. |
| 102 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 103 | 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] | 104 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 105 | struct vm *vm; |
| 106 | |
| 107 | /* Only the primary VM needs to know about vcpus for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 108 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 109 | return -1; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 110 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 111 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 112 | vm = vm_get(vm_id); |
| 113 | if (vm == NULL) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Runs the given vcpu of the given vm. |
| 122 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 123 | 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] | 124 | const struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 125 | struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 126 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 127 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 128 | struct vcpu *vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 129 | struct hf_vcpu_run_return ret = { |
| 130 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 131 | }; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 132 | |
| 133 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 134 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 135 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 136 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 137 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 138 | /* Only secondary VM vcpus can be run. */ |
| 139 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 140 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 141 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 142 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 143 | /* The requested VM must exist. */ |
| 144 | vm = vm_get(vm_id); |
| 145 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 146 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 150 | if (vcpu_idx >= vm->vcpu_count) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 151 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 152 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 153 | |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 154 | vcpu = &vm->vcpus[vcpu_idx]; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 155 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 156 | sl_lock(&vcpu->lock); |
| 157 | if (vcpu->state != vcpu_state_ready) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 158 | ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 159 | } else { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 160 | vcpu->cpu = current->cpu; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 161 | vcpu->state = vcpu_state_running; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 162 | *next = vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 163 | ret.code = HF_VCPU_RUN_YIELD; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 164 | } |
| 165 | sl_unlock(&vcpu->lock); |
| 166 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 167 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 168 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 172 | * Configures the VM to send/receive data through the specified pages. The pages |
| 173 | * must not be shared. |
| 174 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 175 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, |
| 176 | const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 177 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 178 | struct vm *vm = current->vm; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 179 | paddr_t pa_send_begin; |
| 180 | paddr_t pa_send_end; |
| 181 | paddr_t pa_recv_begin; |
| 182 | paddr_t pa_recv_end; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 183 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 184 | |
| 185 | /* Fail if addresses are not page-aligned. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 186 | if ((ipa_addr(send) & (PAGE_SIZE - 1)) || |
| 187 | (ipa_addr(recv) & (PAGE_SIZE - 1))) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | sl_lock(&vm->lock); |
| 192 | |
| 193 | /* We only allow these to be setup once. */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 194 | if (vm->mailbox.send || vm->mailbox.recv) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 195 | ret = -1; |
| 196 | goto exit; |
| 197 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 198 | |
| 199 | /* |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 200 | * TODO: Once memory sharing is implemented, we need to make sure that |
| 201 | * these pages aren't and won't be shared. |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 202 | */ |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 203 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 204 | /* |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 205 | * Convert the intermediate physical addresses to physical address |
| 206 | * provided the address was acessible from the VM which ensures that the |
| 207 | * caller isn't trying to use another VM's memory. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 208 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 209 | if (!mm_vm_translate(&vm->ptable, send, &pa_send_begin) || |
| 210 | !mm_vm_translate(&vm->ptable, recv, &pa_recv_begin)) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 211 | ret = -1; |
| 212 | goto exit; |
| 213 | } |
| 214 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 215 | /* Fail if the same page is used for the send and receive pages. */ |
| 216 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
| 217 | ret = -1; |
| 218 | goto exit; |
| 219 | } |
| 220 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 221 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 222 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 223 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 224 | /* Map the send page as read-only in the hypervisor address space. */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 225 | vm->mailbox.send = |
| 226 | mm_identity_map(pa_send_begin, pa_send_end, MM_MODE_R); |
| 227 | if (!vm->mailbox.send) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 228 | ret = -1; |
| 229 | goto exit; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Map the receive page as writable in the hypervisor address space. On |
| 234 | * failure, unmap the send page before returning. |
| 235 | */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 236 | vm->mailbox.recv = |
| 237 | mm_identity_map(pa_recv_begin, pa_recv_end, MM_MODE_W); |
| 238 | if (!vm->mailbox.recv) { |
| 239 | vm->mailbox.send = NULL; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 240 | mm_unmap(pa_send_begin, pa_send_end, 0); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 241 | ret = -1; |
| 242 | goto exit; |
| 243 | } |
| 244 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 245 | /* TODO: Notify any waiters. */ |
| 246 | |
| 247 | ret = 0; |
| 248 | exit: |
| 249 | sl_unlock(&vm->lock); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 255 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 256 | * and notifies the recipient. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 257 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 258 | int64_t api_mailbox_send(uint32_t vm_id, size_t size, struct vcpu *current, |
| 259 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 260 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 261 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 262 | struct vm *to; |
| 263 | const void *from_buf; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 264 | uint16_t vcpu; |
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 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 267 | /* Limit the size of transfer. */ |
| 268 | if (size > HF_MAILBOX_SIZE) { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 273 | if (vm_id == from->id) { |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | /* Ensure the target VM exists. */ |
| 278 | to = vm_get(vm_id); |
| 279 | if (to == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 283 | /* |
| 284 | * Check that the sender has configured its send buffer. It is safe to |
| 285 | * use from_buf after releasing the lock because the buffer cannot be |
| 286 | * modified once it's configured. |
| 287 | */ |
| 288 | sl_lock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 289 | from_buf = from->mailbox.send; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 290 | sl_unlock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 291 | if (from_buf == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 295 | sl_lock(&to->lock); |
| 296 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 297 | if (to->mailbox.state != mailbox_state_empty || |
| 298 | to->mailbox.recv == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 299 | /* Fail if the target isn't currently ready to receive data. */ |
| 300 | ret = -1; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 301 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 304 | /* Copy data. */ |
| 305 | memcpy(to->mailbox.recv, from_buf, size); |
| 306 | to->mailbox.recv_bytes = size; |
| 307 | to->mailbox.recv_from_id = from->id; |
| 308 | to->mailbox.state = mailbox_state_read; |
| 309 | |
| 310 | /* Messages for the primary VM are delivered directly. */ |
| 311 | if (to->id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 312 | struct hf_vcpu_run_return primary_ret = { |
| 313 | .code = HF_VCPU_RUN_MESSAGE, |
| 314 | .message.size = size, |
| 315 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 316 | *next = api_switch_to_primary(current, primary_ret, |
| 317 | vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 318 | ret = 0; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Try to find a vcpu to handle the message and tell the scheduler to |
| 324 | * run it. |
| 325 | */ |
| 326 | if (to->mailbox.recv_waiter == NULL) { |
| 327 | /* |
| 328 | * The scheduler must choose a vcpu to interrupt so it can |
| 329 | * handle the message. |
| 330 | */ |
| 331 | to->mailbox.state = mailbox_state_received; |
| 332 | vcpu = HF_INVALID_VCPU; |
| 333 | } else { |
| 334 | struct vcpu *to_vcpu = to->mailbox.recv_waiter; |
| 335 | |
| 336 | /* |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 337 | * Take target vcpu out of waiter list and mark it as ready to |
| 338 | * run again. |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 339 | */ |
| 340 | sl_lock(&to_vcpu->lock); |
| 341 | to->mailbox.recv_waiter = to_vcpu->mailbox_next; |
| 342 | to_vcpu->state = vcpu_state_ready; |
| 343 | |
| 344 | /* Return from HF_MAILBOX_RECEIVE. */ |
| 345 | arch_regs_set_retval(&to_vcpu->regs, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 346 | hf_mailbox_receive_return_encode(( |
| 347 | struct hf_mailbox_receive_return){ |
| 348 | .vm_id = to->mailbox.recv_from_id, |
| 349 | .size = size, |
| 350 | })); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 351 | |
| 352 | sl_unlock(&to_vcpu->lock); |
| 353 | |
| 354 | vcpu = to_vcpu - to->vcpus; |
| 355 | } |
| 356 | |
| 357 | /* Return to the primary VM directly or with a switch. */ |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 358 | if (from->id == HF_PRIMARY_VM_ID) { |
| 359 | ret = vcpu; |
| 360 | } else { |
| 361 | struct hf_vcpu_run_return primary_ret = { |
| 362 | .code = HF_VCPU_RUN_WAKE_UP, |
| 363 | .wake_up.vm_id = to->id, |
| 364 | .wake_up.vcpu = vcpu, |
| 365 | }; |
Wedson Almeida Filho | ba641ef | 2018-12-03 04:19:44 +0000 | [diff] [blame] | 366 | *next = api_switch_to_primary(current, primary_ret, |
| 367 | vcpu_state_ready); |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 368 | ret = 0; |
| 369 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 370 | |
| 371 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 372 | sl_unlock(&to->lock); |
| 373 | |
Wedson Almeida Filho | 80eb4a3 | 2018-11-30 17:11:15 +0000 | [diff] [blame] | 374 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 378 | * Receives a message from the mailbox. If one isn't available, this function |
| 379 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 380 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 381 | * 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] | 382 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 383 | struct hf_mailbox_receive_return api_mailbox_receive(bool block, |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 384 | struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 385 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 386 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 387 | struct vm *vm = current->vm; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 388 | struct hf_mailbox_receive_return ret = { |
| 389 | .vm_id = HF_INVALID_VM_ID, |
| 390 | }; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 391 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 392 | /* |
| 393 | * The primary VM will receive messages as a status code from running |
| 394 | * vcpus and must not call this function. |
| 395 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 396 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 397 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | sl_lock(&vm->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 401 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 402 | /* Return pending messages without blocking. */ |
| 403 | if (vm->mailbox.state == mailbox_state_received) { |
| 404 | vm->mailbox.state = mailbox_state_read; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 405 | ret.vm_id = vm->mailbox.recv_from_id; |
| 406 | ret.size = vm->mailbox.recv_bytes; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 407 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 408 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 409 | |
| 410 | /* No pending message so fail if not allowed to block. */ |
| 411 | if (!block) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 412 | goto out; |
| 413 | } |
| 414 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 415 | sl_lock(¤t->lock); |
| 416 | current->state = vcpu_state_blocked_mailbox; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 417 | |
| 418 | /* Push vcpu into waiter list. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 419 | current->mailbox_next = vm->mailbox.recv_waiter; |
| 420 | vm->mailbox.recv_waiter = current; |
| 421 | sl_unlock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 422 | |
| 423 | /* Switch back to primary vm to block. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 424 | *next = api_wait_for_interrupt(current); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 425 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 426 | sl_unlock(&vm->lock); |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 432 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 433 | * must have copied out all data they wish to preserve as new messages will |
| 434 | * overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 435 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 436 | int64_t api_mailbox_clear(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 437 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 438 | struct vm *vm = current->vm; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 439 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 440 | |
| 441 | sl_lock(&vm->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 442 | if (vm->mailbox.state == mailbox_state_read) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 443 | ret = 0; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 444 | vm->mailbox.state = mailbox_state_empty; |
| 445 | } else { |
| 446 | ret = -1; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 447 | } |
| 448 | sl_unlock(&vm->lock); |
| 449 | |
| 450 | if (ret == 0) { |
| 451 | /* TODO: Notify waiters, if any. */ |
| 452 | } |
| 453 | |
| 454 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 455 | } |
Andrew Walbran | 318f573 | 2018-11-20 16:23:42 +0000 | [diff] [blame^] | 456 | |
| 457 | /** |
| 458 | * Enables or disables a given interrupt ID for the calling vCPU. |
| 459 | * |
| 460 | * Returns 0 on success, or -1 if the intid is invalid. |
| 461 | */ |
| 462 | int64_t api_enable_interrupt(uint32_t intid, bool enable, struct vcpu *current) |
| 463 | { |
| 464 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 465 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
| 466 | if (intid >= HF_NUM_INTIDS) { |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | sl_lock(¤t->lock); |
| 471 | if (enable) { |
| 472 | current->interrupts.interrupt_enabled[intid_index] |= |
| 473 | intid_mask; |
| 474 | /* If it is pending, change state and trigger a virtual IRQ. */ |
| 475 | if (current->interrupts.interrupt_pending[intid_index] & |
| 476 | intid_mask) { |
| 477 | arch_regs_set_virtual_interrupt(¤t->regs, true); |
| 478 | } |
| 479 | } else { |
| 480 | current->interrupts.interrupt_enabled[intid_index] &= |
| 481 | ~intid_mask; |
| 482 | } |
| 483 | |
| 484 | sl_unlock(¤t->lock); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Returns the ID of the next pending interrupt for the calling vCPU, and |
| 490 | * acknowledges it (i.e. marks it as no longer pending). Returns |
| 491 | * HF_INVALID_INTID if there are no pending interrupts. |
| 492 | */ |
| 493 | uint32_t api_get_and_acknowledge_interrupt(struct vcpu *current) |
| 494 | { |
| 495 | uint8_t i; |
| 496 | uint32_t first_interrupt = HF_INVALID_INTID; |
| 497 | bool interrupts_remain = false; |
| 498 | |
| 499 | /* |
| 500 | * Find the first enabled and pending interrupt ID, return it, and |
| 501 | * deactivate it. |
| 502 | */ |
| 503 | sl_lock(¤t->lock); |
| 504 | for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) { |
| 505 | uint32_t enabled_and_pending = |
| 506 | current->interrupts.interrupt_enabled[i] & |
| 507 | current->interrupts.interrupt_pending[i]; |
| 508 | if (enabled_and_pending == 0) { |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | if (first_interrupt != HF_INVALID_INTID) { |
| 513 | interrupts_remain = true; |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | uint8_t bit_index = ctz(enabled_and_pending); |
| 518 | /* Mark it as no longer pending. */ |
| 519 | current->interrupts.interrupt_pending[i] &= ~(1u << bit_index); |
| 520 | first_interrupt = i * INTERRUPT_REGISTER_BITS + bit_index; |
| 521 | |
| 522 | enabled_and_pending = current->interrupts.interrupt_enabled[i] & |
| 523 | current->interrupts.interrupt_pending[i]; |
| 524 | if (enabled_and_pending != 0) { |
| 525 | interrupts_remain = true; |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | /* |
| 530 | * If there are no more enabled and pending interrupts left, clear the |
| 531 | * VI bit. |
| 532 | */ |
| 533 | arch_regs_set_virtual_interrupt(¤t->regs, interrupts_remain); |
| 534 | |
| 535 | sl_unlock(¤t->lock); |
| 536 | return first_interrupt; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Return wheher the current vCPU is allowed to inject an interrupt into the |
| 541 | * given VM and vCPU. |
| 542 | */ |
| 543 | static inline bool is_injection_allowed(uint32_t target_vm_id, |
| 544 | struct vcpu *current) |
| 545 | { |
| 546 | uint32_t current_vm_id = current->vm->id; |
| 547 | /* |
| 548 | * The primary VM is allowed to inject interrupts into any VM. Secondary |
| 549 | * VMs are only allowed to inject interrupts into their own vCPUs. |
| 550 | */ |
| 551 | return current_vm_id == HF_PRIMARY_VM_ID || |
| 552 | current_vm_id == target_vm_id; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Injects a virtual interrupt of the given ID into the given target vCPU. |
| 557 | * This doesn't cause the vCPU to actually be run immediately; it will be taken |
| 558 | * when the vCPU is next run, which is up to the scheduler. |
| 559 | * |
| 560 | * Returns 0 on success, or -1 if the target VM or vCPU doesn't exist, the |
| 561 | * interrupt ID is invalid, or the current VM is not allowed to inject |
| 562 | * interrupts to the target VM. |
| 563 | */ |
| 564 | int64_t api_inject_interrupt(uint32_t target_vm_id, uint32_t target_vcpu_idx, |
| 565 | uint32_t intid, struct vcpu *current, |
| 566 | struct vcpu **next) |
| 567 | { |
| 568 | uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS; |
| 569 | uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS); |
| 570 | struct vcpu *target_vcpu; |
| 571 | struct vm *target_vm = vm_get(target_vm_id); |
| 572 | |
| 573 | if (intid >= HF_NUM_INTIDS) { |
| 574 | return -1; |
| 575 | } |
| 576 | if (target_vm == NULL) { |
| 577 | return -1; |
| 578 | } |
| 579 | if (target_vcpu_idx >= target_vm->vcpu_count) { |
| 580 | /* The requested vcpu must exist. */ |
| 581 | return -1; |
| 582 | } |
| 583 | if (!is_injection_allowed(target_vm_id, current)) { |
| 584 | return -1; |
| 585 | } |
| 586 | target_vcpu = &target_vm->vcpus[target_vcpu_idx]; |
| 587 | |
| 588 | dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid, |
| 589 | target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id); |
| 590 | |
| 591 | sl_lock(&target_vcpu->lock); |
| 592 | |
| 593 | /* Make it pending. */ |
| 594 | target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask; |
| 595 | |
| 596 | /* If it is enabled, change state and trigger a virtual IRQ. */ |
| 597 | if (target_vcpu->interrupts.interrupt_enabled[intid_index] & |
| 598 | intid_mask) { |
| 599 | dlog("IRQ %d is enabled for VM %d VCPU %d, setting VI.\n", |
| 600 | intid, target_vm_id, target_vcpu_idx); |
| 601 | arch_regs_set_virtual_interrupt(&target_vcpu->regs, true); |
| 602 | |
| 603 | if (target_vcpu->state == vcpu_state_blocked_interrupt) { |
| 604 | dlog("Changing state from blocked_interrupt to " |
| 605 | "ready.\n"); |
| 606 | target_vcpu->state = vcpu_state_ready; |
| 607 | } |
| 608 | |
| 609 | if (current->vm->id != HF_PRIMARY_VM_ID && |
| 610 | current != target_vcpu) { |
| 611 | /* |
| 612 | * Switch to the primary so that it can switch to the |
| 613 | * target. |
| 614 | */ |
| 615 | struct hf_vcpu_run_return ret = { |
| 616 | .code = HF_VCPU_RUN_WAKE_UP, |
| 617 | .wake_up.vm_id = target_vm_id, |
| 618 | .wake_up.vcpu = target_vcpu_idx, |
| 619 | }; |
| 620 | *next = api_switch_to_primary(current, ret, |
| 621 | vcpu_state_ready); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | sl_unlock(&target_vcpu->lock); |
| 626 | |
| 627 | return 0; |
| 628 | } |