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 Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
| 22 | #include "hf/vm.h" |
| 23 | |
Andrew Scull | f35a5c9 | 2018-08-07 18:09:46 +0100 | [diff] [blame] | 24 | #include "vmapi/hf/call.h" |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 25 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 26 | static_assert(HF_MAILBOX_SIZE == PAGE_SIZE, |
Andrew Scull | 13652af | 2018-09-17 14:49:08 +0100 | [diff] [blame] | 27 | "Currently, a page is mapped for the send and receive buffers so " |
| 28 | "the maximum request is the size of a page."); |
| 29 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 30 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 31 | * 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] | 32 | * |
| 33 | * This triggers the scheduling logic to run. Run in the context of secondary VM |
| 34 | * to cause HF_VCPU_RUN to return and the primary VM to regain control of the |
| 35 | * cpu. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 36 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 37 | static struct vcpu *api_switch_to_primary(struct vcpu *current, |
| 38 | struct hf_vcpu_run_return primary_ret, |
Andrew Scull | 6bca35e | 2018-10-02 12:05:32 +0100 | [diff] [blame] | 39 | enum vcpu_state secondary_state) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 40 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 41 | struct vm *primary = vm_get(HF_PRIMARY_VM_ID); |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 42 | struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)]; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 43 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 44 | /* 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] | 45 | arch_regs_set_retval(&next->regs, |
| 46 | hf_vcpu_run_return_encode(primary_ret)); |
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 | /* Mark the current vcpu as waiting. */ |
| 49 | sl_lock(¤t->lock); |
| 50 | current->state = secondary_state; |
| 51 | sl_unlock(¤t->lock); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 52 | |
| 53 | return next; |
| 54 | } |
| 55 | |
| 56 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 57 | * Returns to the primary vm leaving the current vcpu ready to be scheduled |
| 58 | * again. |
| 59 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 60 | struct vcpu *api_yield(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 61 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 62 | struct hf_vcpu_run_return ret = { |
| 63 | .code = HF_VCPU_RUN_YIELD, |
| 64 | }; |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 65 | return api_switch_to_primary(current, ret, vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Puts the current vcpu in wait for interrupt mode, and returns to the primary |
| 70 | * vm. |
| 71 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 72 | struct vcpu *api_wait_for_interrupt(struct vcpu *current) |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 73 | { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 74 | struct hf_vcpu_run_return ret = { |
| 75 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 76 | }; |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 77 | return api_switch_to_primary(current, ret, |
| 78 | vcpu_state_blocked_interrupt); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 82 | * Returns the number of VMs configured to run. |
| 83 | */ |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 84 | int64_t api_vm_get_count(void) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 85 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 86 | return vm_get_count(); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the number of vcpus configured in the given VM. |
| 91 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 92 | 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] | 93 | { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 94 | struct vm *vm; |
| 95 | |
| 96 | /* Only the primary VM needs to know about vcpus for scheduling. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 97 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 98 | return -1; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 99 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 100 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 101 | vm = vm_get(vm_id); |
| 102 | if (vm == NULL) { |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | return vm->vcpu_count; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Runs the given vcpu of the given vm. |
| 111 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 112 | 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] | 113 | const struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 114 | struct vcpu **next) |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 115 | { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 116 | struct vm *vm; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 117 | struct vcpu *vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 118 | struct hf_vcpu_run_return ret = { |
| 119 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 120 | }; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 121 | |
| 122 | /* Only the primary VM can switch vcpus. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 123 | if (current->vm->id != HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 124 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 125 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 126 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 127 | /* Only secondary VM vcpus can be run. */ |
| 128 | if (vm_id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 129 | goto out; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 130 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 131 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 132 | /* The requested VM must exist. */ |
| 133 | vm = vm_get(vm_id); |
| 134 | if (vm == NULL) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 135 | goto out; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* The requested vcpu must exist. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 139 | if (vcpu_idx >= vm->vcpu_count) { |
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 | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 143 | vcpu = &vm->vcpus[vcpu_idx]; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 144 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 145 | sl_lock(&vcpu->lock); |
| 146 | if (vcpu->state != vcpu_state_ready) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 147 | ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 148 | } else { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 149 | vcpu->cpu = current->cpu; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 150 | vcpu->state = vcpu_state_running; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 151 | *next = vcpu; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 152 | ret.code = HF_VCPU_RUN_YIELD; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 153 | } |
| 154 | sl_unlock(&vcpu->lock); |
| 155 | |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 156 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 157 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 161 | * Configures the VM to send/receive data through the specified pages. The pages |
| 162 | * must not be shared. |
| 163 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 164 | int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, |
| 165 | const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 166 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 167 | struct vm *vm = current->vm; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 168 | paddr_t pa_send_begin; |
| 169 | paddr_t pa_send_end; |
| 170 | paddr_t pa_recv_begin; |
| 171 | paddr_t pa_recv_end; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 172 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 173 | |
| 174 | /* Fail if addresses are not page-aligned. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 175 | if ((ipa_addr(send) & (PAGE_SIZE - 1)) || |
| 176 | (ipa_addr(recv) & (PAGE_SIZE - 1))) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | sl_lock(&vm->lock); |
| 181 | |
| 182 | /* We only allow these to be setup once. */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 183 | if (vm->mailbox.send || vm->mailbox.recv) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 184 | ret = -1; |
| 185 | goto exit; |
| 186 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 187 | |
| 188 | /* |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 189 | * TODO: Once memory sharing is implemented, we need to make sure that |
| 190 | * these pages aren't and won't be shared. |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 191 | */ |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 192 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 193 | /* |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 194 | * Convert the intermediate physical addresses to physical address |
| 195 | * provided the address was acessible from the VM which ensures that the |
| 196 | * caller isn't trying to use another VM's memory. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 197 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 198 | if (!mm_vm_translate(&vm->ptable, send, &pa_send_begin) || |
| 199 | !mm_vm_translate(&vm->ptable, recv, &pa_recv_begin)) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 200 | ret = -1; |
| 201 | goto exit; |
| 202 | } |
| 203 | |
Andrew Scull | c9ccb3f | 2018-08-13 15:27:12 +0100 | [diff] [blame] | 204 | /* Fail if the same page is used for the send and receive pages. */ |
| 205 | if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) { |
| 206 | ret = -1; |
| 207 | goto exit; |
| 208 | } |
| 209 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 210 | pa_send_end = pa_add(pa_send_begin, PAGE_SIZE); |
| 211 | pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE); |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 212 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 213 | /* Map the send page as read-only in the hypervisor address space. */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 214 | vm->mailbox.send = |
| 215 | mm_identity_map(pa_send_begin, pa_send_end, MM_MODE_R); |
| 216 | if (!vm->mailbox.send) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 217 | ret = -1; |
| 218 | goto exit; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Map the receive page as writable in the hypervisor address space. On |
| 223 | * failure, unmap the send page before returning. |
| 224 | */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 225 | vm->mailbox.recv = |
| 226 | mm_identity_map(pa_recv_begin, pa_recv_end, MM_MODE_W); |
| 227 | if (!vm->mailbox.recv) { |
| 228 | vm->mailbox.send = NULL; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 229 | mm_unmap(pa_send_begin, pa_send_end, 0); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 230 | ret = -1; |
| 231 | goto exit; |
| 232 | } |
| 233 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 234 | /* TODO: Notify any waiters. */ |
| 235 | |
| 236 | ret = 0; |
| 237 | exit: |
| 238 | sl_unlock(&vm->lock); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 244 | * Copies data from the sender's send buffer to the recipient's receive buffer |
| 245 | * and notifies the recipient. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 246 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 247 | int64_t api_mailbox_send(uint32_t vm_id, size_t size, struct vcpu *current, |
| 248 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 249 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 250 | struct vm *from = current->vm; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 251 | struct vm *to; |
| 252 | const void *from_buf; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 253 | uint16_t vcpu; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 254 | int64_t ret; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 255 | struct hf_vcpu_run_return primary_ret = { |
| 256 | .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT, |
| 257 | }; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 258 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 259 | /* Limit the size of transfer. */ |
| 260 | if (size > HF_MAILBOX_SIZE) { |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | /* Disallow reflexive requests as this suggests an error in the VM. */ |
| 265 | if (vm_id == from->id) { |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | /* Ensure the target VM exists. */ |
| 270 | to = vm_get(vm_id); |
| 271 | if (to == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 275 | /* |
| 276 | * Check that the sender has configured its send buffer. It is safe to |
| 277 | * use from_buf after releasing the lock because the buffer cannot be |
| 278 | * modified once it's configured. |
| 279 | */ |
| 280 | sl_lock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 281 | from_buf = from->mailbox.send; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 282 | sl_unlock(&from->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 283 | if (from_buf == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 287 | sl_lock(&to->lock); |
| 288 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 289 | if (to->mailbox.state != mailbox_state_empty || |
| 290 | to->mailbox.recv == NULL) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 291 | /* Fail if the target isn't currently ready to receive data. */ |
| 292 | ret = -1; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 293 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 296 | /* Copy data. */ |
| 297 | memcpy(to->mailbox.recv, from_buf, size); |
| 298 | to->mailbox.recv_bytes = size; |
| 299 | to->mailbox.recv_from_id = from->id; |
| 300 | to->mailbox.state = mailbox_state_read; |
| 301 | |
| 302 | /* Messages for the primary VM are delivered directly. */ |
| 303 | if (to->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 304 | primary_ret.code = HF_VCPU_RUN_MESSAGE; |
| 305 | primary_ret.message.size = size; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 306 | ret = 0; |
Andrew Scull | 4a49657 | 2018-10-11 18:44:08 +0100 | [diff] [blame] | 307 | /* |
| 308 | * clang-tidy isn't able to prove that |
| 309 | * `from->id != HF_PRIMARY_VM_ID` so cover that specific case |
Andrew Scull | a1317a1 | 2018-10-12 18:15:20 +0100 | [diff] [blame] | 310 | * explicitly so as not to hide other possible bugs. clang-check |
| 311 | * is more clever and finds that this is dead code so we also |
| 312 | * pretend to use the new value. |
Andrew Scull | 4a49657 | 2018-10-11 18:44:08 +0100 | [diff] [blame] | 313 | */ |
| 314 | if (from->id == HF_PRIMARY_VM_ID) { |
| 315 | vcpu = 0; |
Andrew Scull | a1317a1 | 2018-10-12 18:15:20 +0100 | [diff] [blame] | 316 | (void)vcpu; |
Andrew Scull | 4a49657 | 2018-10-11 18:44:08 +0100 | [diff] [blame] | 317 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Try to find a vcpu to handle the message and tell the scheduler to |
| 323 | * run it. |
| 324 | */ |
| 325 | if (to->mailbox.recv_waiter == NULL) { |
| 326 | /* |
| 327 | * The scheduler must choose a vcpu to interrupt so it can |
| 328 | * handle the message. |
| 329 | */ |
| 330 | to->mailbox.state = mailbox_state_received; |
| 331 | vcpu = HF_INVALID_VCPU; |
| 332 | } else { |
| 333 | struct vcpu *to_vcpu = to->mailbox.recv_waiter; |
| 334 | |
| 335 | /* |
| 336 | * Take target vcpu out of waiter list and mark as ready |
| 337 | * to run again. |
| 338 | */ |
| 339 | sl_lock(&to_vcpu->lock); |
| 340 | to->mailbox.recv_waiter = to_vcpu->mailbox_next; |
| 341 | to_vcpu->state = vcpu_state_ready; |
| 342 | |
| 343 | /* Return from HF_MAILBOX_RECEIVE. */ |
| 344 | arch_regs_set_retval(&to_vcpu->regs, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 345 | hf_mailbox_receive_return_encode(( |
| 346 | struct hf_mailbox_receive_return){ |
| 347 | .vm_id = to->mailbox.recv_from_id, |
| 348 | .size = size, |
| 349 | })); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 350 | |
| 351 | sl_unlock(&to_vcpu->lock); |
| 352 | |
| 353 | vcpu = to_vcpu - to->vcpus; |
| 354 | } |
| 355 | |
| 356 | /* Return to the primary VM directly or with a switch. */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 357 | primary_ret.code = HF_VCPU_RUN_WAKE_UP; |
| 358 | primary_ret.wake_up.vm_id = to->id; |
| 359 | primary_ret.wake_up.vcpu = vcpu; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 360 | ret = 0; |
| 361 | |
| 362 | out: |
| 363 | /* |
| 364 | * Unlock before routing the return values as switching to the primary |
| 365 | * will acquire more locks and nesting the locks is avoidable. |
| 366 | */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 367 | sl_unlock(&to->lock); |
| 368 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 369 | /* Report errors to the sender. */ |
| 370 | if (ret != 0) { |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | /* If the sender is the primary, return the vcpu to schedule. */ |
| 375 | if (from->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 376 | return primary_ret.wake_up.vcpu; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Switch to primary for scheduling and return success to the sender. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 380 | *next = api_switch_to_primary(current, primary_ret, vcpu_state_ready); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 381 | return 0; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 385 | * Receives a message from the mailbox. If one isn't available, this function |
| 386 | * can optionally block the caller until one becomes available. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 387 | * |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 388 | * 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] | 389 | */ |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 390 | struct hf_mailbox_receive_return api_mailbox_receive(bool block, |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 391 | struct vcpu *current, |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 392 | struct vcpu **next) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 393 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 394 | struct vm *vm = current->vm; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 395 | struct hf_mailbox_receive_return ret = { |
| 396 | .vm_id = HF_INVALID_VM_ID, |
| 397 | }; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 398 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 399 | /* |
| 400 | * The primary VM will receive messages as a status code from running |
| 401 | * vcpus and must not call this function. |
| 402 | */ |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 403 | if (vm->id == HF_PRIMARY_VM_ID) { |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 404 | return ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | sl_lock(&vm->lock); |
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 | /* Return pending messages without blocking. */ |
| 410 | if (vm->mailbox.state == mailbox_state_received) { |
| 411 | vm->mailbox.state = mailbox_state_read; |
Andrew Scull | 6d2db33 | 2018-10-10 15:28:17 +0100 | [diff] [blame] | 412 | ret.vm_id = vm->mailbox.recv_from_id; |
| 413 | ret.size = vm->mailbox.recv_bytes; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 414 | goto out; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 415 | } |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 416 | |
| 417 | /* No pending message so fail if not allowed to block. */ |
| 418 | if (!block) { |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 419 | goto out; |
| 420 | } |
| 421 | |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 422 | sl_lock(¤t->lock); |
| 423 | current->state = vcpu_state_blocked_mailbox; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 424 | |
| 425 | /* Push vcpu into waiter list. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 426 | current->mailbox_next = vm->mailbox.recv_waiter; |
| 427 | vm->mailbox.recv_waiter = current; |
| 428 | sl_unlock(¤t->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 429 | |
| 430 | /* Switch back to primary vm to block. */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 431 | *next = api_wait_for_interrupt(current); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 432 | out: |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 433 | sl_unlock(&vm->lock); |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | /** |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 439 | * Clears the caller's mailbox so that a new message can be received. The caller |
| 440 | * must have copied out all data they wish to preserve as new messages will |
| 441 | * overwrite the old and will arrive asynchronously. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 442 | */ |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 443 | int64_t api_mailbox_clear(const struct vcpu *current) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 444 | { |
Wedson Almeida Filho | 00df6c7 | 2018-10-18 11:19:24 +0100 | [diff] [blame] | 445 | struct vm *vm = current->vm; |
Andrew Scull | c0e569a | 2018-10-02 18:05:21 +0100 | [diff] [blame] | 446 | int64_t ret; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 447 | |
| 448 | sl_lock(&vm->lock); |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 449 | if (vm->mailbox.state == mailbox_state_read) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 450 | ret = 0; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 451 | vm->mailbox.state = mailbox_state_empty; |
| 452 | } else { |
| 453 | ret = -1; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 454 | } |
| 455 | sl_unlock(&vm->lock); |
| 456 | |
| 457 | if (ret == 0) { |
| 458 | /* TODO: Notify waiters, if any. */ |
| 459 | } |
| 460 | |
| 461 | return ret; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 462 | } |