blob: 1cba24f7c18fc286b51154a6f428263862a360fd [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010018
Andrew Scull13652af2018-09-17 14:49:08 +010019#include <assert.h>
20
Andrew Walbran318f5732018-11-20 16:23:42 +000021#include "hf/arch/cpu.h"
22
23#include "hf/dlog.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/std.h"
25#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000029/*
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 Scullaa039b32018-10-04 15:02:26 +010037static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010038 "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 Filho3fcbcff2018-07-10 23:53:39 +010041/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010042 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010043 *
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 Filho2f94ec12018-07-26 16:00:48 +010047 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010048static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000049 struct hf_vcpu_run_return primary_ret,
50 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010051{
Andrew Scull19503262018-09-20 14:48:39 +010052 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010053 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010054
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010055 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010056 arch_regs_set_retval(&next->regs,
57 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010058
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000059 /* Mark the current vcpu as waiting. */
60 sl_lock(&current->lock);
61 current->state = secondary_state;
62 sl_unlock(&current->lock);
63
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010064 return next;
65}
66
67/**
Andrew Scullaa039b32018-10-04 15:02:26 +010068 * Returns to the primary vm leaving the current vcpu ready to be scheduled
69 * again.
70 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071struct vcpu *api_yield(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +010072{
Andrew Scull6d2db332018-10-10 15:28:17 +010073 struct hf_vcpu_run_return ret = {
74 .code = HF_VCPU_RUN_YIELD,
75 };
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000076 return api_switch_to_primary(current, ret, vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +010077}
78
79/**
80 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
81 * vm.
82 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010083struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +010084{
Andrew Scull6d2db332018-10-10 15:28:17 +010085 struct hf_vcpu_run_return ret = {
86 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
87 };
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000088 return api_switch_to_primary(current, ret,
89 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +010090}
91
92/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010093 * Returns the number of VMs configured to run.
94 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010095int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010096{
Andrew Scull19503262018-09-20 14:48:39 +010097 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010098}
99
100/**
101 * Returns the number of vcpus configured in the given VM.
102 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100103int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100104{
Andrew Scull19503262018-09-20 14:48:39 +0100105 struct vm *vm;
106
107 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100108 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100109 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100110 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100111
Andrew Scull19503262018-09-20 14:48:39 +0100112 vm = vm_get(vm_id);
113 if (vm == NULL) {
114 return -1;
115 }
116
117 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100118}
119
120/**
121 * Runs the given vcpu of the given vm.
122 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100123struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100124 const struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100125 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100126{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100127 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100128 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100129 struct hf_vcpu_run_return ret = {
130 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
131 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100132
133 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100134 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100135 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100136 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100137
Andrew Scull19503262018-09-20 14:48:39 +0100138 /* Only secondary VM vcpus can be run. */
139 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100140 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100141 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100142
Andrew Scull19503262018-09-20 14:48:39 +0100143 /* The requested VM must exist. */
144 vm = vm_get(vm_id);
145 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100146 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100147 }
148
149 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100150 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100151 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100152 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100153
Andrew Scullf3d45592018-09-20 14:30:22 +0100154 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100155
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100156 sl_lock(&vcpu->lock);
157 if (vcpu->state != vcpu_state_ready) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100158 ret.code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100159 } else {
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100160 vcpu->cpu = current->cpu;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100161 vcpu->state = vcpu_state_running;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100162 *next = vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100163 ret.code = HF_VCPU_RUN_YIELD;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100164 }
165 sl_unlock(&vcpu->lock);
166
Andrew Scull6d2db332018-10-10 15:28:17 +0100167out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100168 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100169}
170
171/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100172 * Configures the VM to send/receive data through the specified pages. The pages
173 * must not be shared.
174 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100175int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv,
176 const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100177{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100178 struct vm *vm = current->vm;
Andrew Scull80871322018-08-06 12:04:09 +0100179 paddr_t pa_send_begin;
180 paddr_t pa_send_end;
181 paddr_t pa_recv_begin;
182 paddr_t pa_recv_end;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100183 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100184
185 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100186 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
187 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100188 return -1;
189 }
190
191 sl_lock(&vm->lock);
192
193 /* We only allow these to be setup once. */
Andrew Scullaa039b32018-10-04 15:02:26 +0100194 if (vm->mailbox.send || vm->mailbox.recv) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100195 ret = -1;
196 goto exit;
197 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100198
199 /*
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100200 * TODO: Once memory sharing is implemented, we need to make sure that
201 * these pages aren't and won't be shared.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100202 */
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100203
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100204 /*
Andrew Scull265ada92018-07-30 15:19:01 +0100205 * 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 Filho2f94ec12018-07-26 16:00:48 +0100208 */
Andrew Scull80871322018-08-06 12:04:09 +0100209 if (!mm_vm_translate(&vm->ptable, send, &pa_send_begin) ||
210 !mm_vm_translate(&vm->ptable, recv, &pa_recv_begin)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100211 ret = -1;
212 goto exit;
213 }
214
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100215 /* 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 Scull80871322018-08-06 12:04:09 +0100221 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
222 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
Andrew Scull265ada92018-07-30 15:19:01 +0100223
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100224 /* Map the send page as read-only in the hypervisor address space. */
Andrew Scullaa039b32018-10-04 15:02:26 +0100225 vm->mailbox.send =
226 mm_identity_map(pa_send_begin, pa_send_end, MM_MODE_R);
227 if (!vm->mailbox.send) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100228 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 Scullaa039b32018-10-04 15:02:26 +0100236 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 Scull80871322018-08-06 12:04:09 +0100240 mm_unmap(pa_send_begin, pa_send_end, 0);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100241 ret = -1;
242 goto exit;
243 }
244
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100245 /* TODO: Notify any waiters. */
246
247 ret = 0;
248exit:
249 sl_unlock(&vm->lock);
250
251 return ret;
252}
253
254/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100255 * Copies data from the sender's send buffer to the recipient's receive buffer
256 * and notifies the recipient.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100257 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100258int64_t api_mailbox_send(uint32_t vm_id, size_t size, struct vcpu *current,
259 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100260{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100261 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100262 struct vm *to;
263 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100264 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100265 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100266
Andrew Scullaa039b32018-10-04 15:02:26 +0100267 /* Limit the size of transfer. */
268 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100269 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 Filho2f94ec12018-07-26 16:00:48 +0100280 return -1;
281 }
282
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100283 /*
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 Scullaa039b32018-10-04 15:02:26 +0100289 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100290 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100291 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100292 return -1;
293 }
294
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100295 sl_lock(&to->lock);
296
Andrew Scullaa039b32018-10-04 15:02:26 +0100297 if (to->mailbox.state != mailbox_state_empty ||
298 to->mailbox.recv == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100299 /* Fail if the target isn't currently ready to receive data. */
300 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100301 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100302 }
303
Andrew Scullaa039b32018-10-04 15:02:26 +0100304 /* 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 Filho80eb4a32018-11-30 17:11:15 +0000312 struct hf_vcpu_run_return primary_ret = {
313 .code = HF_VCPU_RUN_MESSAGE,
314 .message.size = size,
315 };
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000316 *next = api_switch_to_primary(current, primary_ret,
317 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100318 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 Filho80eb4a32018-11-30 17:11:15 +0000337 * Take target vcpu out of waiter list and mark it as ready to
338 * run again.
Andrew Scullaa039b32018-10-04 15:02:26 +0100339 */
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 Scull6d2db332018-10-10 15:28:17 +0100346 hf_mailbox_receive_return_encode((
347 struct hf_mailbox_receive_return){
348 .vm_id = to->mailbox.recv_from_id,
349 .size = size,
350 }));
Andrew Scullaa039b32018-10-04 15:02:26 +0100351
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 Filho80eb4a32018-11-30 17:11:15 +0000358 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 Filhoba641ef2018-12-03 04:19:44 +0000366 *next = api_switch_to_primary(current, primary_ret,
367 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000368 ret = 0;
369 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100370
371out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100372 sl_unlock(&to->lock);
373
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000374 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100375}
376
377/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100378 * 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 Filho2f94ec12018-07-26 16:00:48 +0100380 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100381 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100382 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100383struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100384 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100385 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100386{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100387 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100388 struct hf_mailbox_receive_return ret = {
389 .vm_id = HF_INVALID_VM_ID,
390 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100391
Andrew Scullaa039b32018-10-04 15:02:26 +0100392 /*
393 * The primary VM will receive messages as a status code from running
394 * vcpus and must not call this function.
395 */
Andrew Scull19503262018-09-20 14:48:39 +0100396 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100397 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100398 }
399
400 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100401
Andrew Scullaa039b32018-10-04 15:02:26 +0100402 /* Return pending messages without blocking. */
403 if (vm->mailbox.state == mailbox_state_received) {
404 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100405 ret.vm_id = vm->mailbox.recv_from_id;
406 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100407 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100408 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100409
410 /* No pending message so fail if not allowed to block. */
411 if (!block) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100412 goto out;
413 }
414
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100415 sl_lock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100416
417 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100418 current->mailbox_next = vm->mailbox.recv_waiter;
419 vm->mailbox.recv_waiter = current;
420 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100421
422 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000423 {
424 struct hf_vcpu_run_return run_return = {
425 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
426 };
427 *next = api_switch_to_primary(current, run_return,
428 vcpu_state_blocked_mailbox);
429 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100430out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100431 sl_unlock(&vm->lock);
432
433 return ret;
434}
435
436/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100437 * Clears the caller's mailbox so that a new message can be received. The caller
438 * must have copied out all data they wish to preserve as new messages will
439 * overwrite the old and will arrive asynchronously.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100440 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100441int64_t api_mailbox_clear(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100442{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100443 struct vm *vm = current->vm;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100444 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100445
446 sl_lock(&vm->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100447 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100448 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100449 vm->mailbox.state = mailbox_state_empty;
450 } else {
451 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100452 }
453 sl_unlock(&vm->lock);
454
455 if (ret == 0) {
456 /* TODO: Notify waiters, if any. */
457 }
458
459 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100460}
Andrew Walbran318f5732018-11-20 16:23:42 +0000461
462/**
463 * Enables or disables a given interrupt ID for the calling vCPU.
464 *
465 * Returns 0 on success, or -1 if the intid is invalid.
466 */
467int64_t api_enable_interrupt(uint32_t intid, bool enable, struct vcpu *current)
468{
469 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
470 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
471 if (intid >= HF_NUM_INTIDS) {
472 return -1;
473 }
474
475 sl_lock(&current->lock);
476 if (enable) {
477 current->interrupts.interrupt_enabled[intid_index] |=
478 intid_mask;
479 /* If it is pending, change state and trigger a virtual IRQ. */
480 if (current->interrupts.interrupt_pending[intid_index] &
481 intid_mask) {
482 arch_regs_set_virtual_interrupt(&current->regs, true);
483 }
484 } else {
485 current->interrupts.interrupt_enabled[intid_index] &=
486 ~intid_mask;
487 }
488
489 sl_unlock(&current->lock);
490 return 0;
491}
492
493/**
494 * Returns the ID of the next pending interrupt for the calling vCPU, and
495 * acknowledges it (i.e. marks it as no longer pending). Returns
496 * HF_INVALID_INTID if there are no pending interrupts.
497 */
498uint32_t api_get_and_acknowledge_interrupt(struct vcpu *current)
499{
500 uint8_t i;
501 uint32_t first_interrupt = HF_INVALID_INTID;
502 bool interrupts_remain = false;
503
504 /*
505 * Find the first enabled and pending interrupt ID, return it, and
506 * deactivate it.
507 */
508 sl_lock(&current->lock);
509 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
510 uint32_t enabled_and_pending =
511 current->interrupts.interrupt_enabled[i] &
512 current->interrupts.interrupt_pending[i];
513 if (enabled_and_pending == 0) {
514 continue;
515 }
516
517 if (first_interrupt != HF_INVALID_INTID) {
518 interrupts_remain = true;
519 break;
520 }
521
522 uint8_t bit_index = ctz(enabled_and_pending);
523 /* Mark it as no longer pending. */
524 current->interrupts.interrupt_pending[i] &= ~(1u << bit_index);
525 first_interrupt = i * INTERRUPT_REGISTER_BITS + bit_index;
526
527 enabled_and_pending = current->interrupts.interrupt_enabled[i] &
528 current->interrupts.interrupt_pending[i];
529 if (enabled_and_pending != 0) {
530 interrupts_remain = true;
531 break;
532 }
533 }
534 /*
535 * If there are no more enabled and pending interrupts left, clear the
536 * VI bit.
537 */
538 arch_regs_set_virtual_interrupt(&current->regs, interrupts_remain);
539
540 sl_unlock(&current->lock);
541 return first_interrupt;
542}
543
544/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +0000545 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +0000546 * given VM and vCPU.
547 */
548static inline bool is_injection_allowed(uint32_t target_vm_id,
549 struct vcpu *current)
550{
551 uint32_t current_vm_id = current->vm->id;
552 /*
553 * The primary VM is allowed to inject interrupts into any VM. Secondary
554 * VMs are only allowed to inject interrupts into their own vCPUs.
555 */
556 return current_vm_id == HF_PRIMARY_VM_ID ||
557 current_vm_id == target_vm_id;
558}
559
560/**
561 * Injects a virtual interrupt of the given ID into the given target vCPU.
562 * This doesn't cause the vCPU to actually be run immediately; it will be taken
563 * when the vCPU is next run, which is up to the scheduler.
564 *
565 * Returns 0 on success, or -1 if the target VM or vCPU doesn't exist, the
566 * interrupt ID is invalid, or the current VM is not allowed to inject
567 * interrupts to the target VM.
568 */
569int64_t api_inject_interrupt(uint32_t target_vm_id, uint32_t target_vcpu_idx,
570 uint32_t intid, struct vcpu *current,
571 struct vcpu **next)
572{
573 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
574 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
575 struct vcpu *target_vcpu;
576 struct vm *target_vm = vm_get(target_vm_id);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000577 bool need_vm_lock;
Andrew Walbran318f5732018-11-20 16:23:42 +0000578
579 if (intid >= HF_NUM_INTIDS) {
580 return -1;
581 }
582 if (target_vm == NULL) {
583 return -1;
584 }
585 if (target_vcpu_idx >= target_vm->vcpu_count) {
586 /* The requested vcpu must exist. */
587 return -1;
588 }
589 if (!is_injection_allowed(target_vm_id, current)) {
590 return -1;
591 }
592 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
593
594 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
595 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
596
597 sl_lock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000598 /*
599 * If we need the target_vm lock we need to release the target_vcpu lock
600 * first to maintain the correct order of locks. In-between releasing
601 * and acquiring it again the state of the vCPU could change in such a
602 * way that we don't actually need to touch the target_vm after all, but
603 * that's alright: we'll take the target_vm lock anyway, but it's safe,
604 * just perhaps a little slow in this unusual case. The reverse is not
605 * possible: if need_vm_lock is false, we don't release the target_vcpu
606 * lock until we are done, so nothing should change in such as way that
607 * we need the VM lock after all.
608 */
609 need_vm_lock = (target_vcpu->interrupts.interrupt_enabled[intid_index] &
610 intid_mask) &&
611 target_vcpu->state == vcpu_state_blocked_mailbox;
612 if (need_vm_lock) {
613 sl_unlock(&target_vcpu->lock);
614 sl_lock(&target_vm->lock);
615 sl_lock(&target_vcpu->lock);
616 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000617
618 /* Make it pending. */
619 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
620
Andrew Walbran69520dc2018-12-06 11:39:38 +0000621 /*
622 * If it is enabled, change state and trigger a virtual IRQ. If you
623 * change this logic make sure to update the need_vm_lock logic above to
624 * match.
625 */
Andrew Walbran318f5732018-11-20 16:23:42 +0000626 if (target_vcpu->interrupts.interrupt_enabled[intid_index] &
627 intid_mask) {
628 dlog("IRQ %d is enabled for VM %d VCPU %d, setting VI.\n",
629 intid, target_vm_id, target_vcpu_idx);
630 arch_regs_set_virtual_interrupt(&target_vcpu->regs, true);
631
632 if (target_vcpu->state == vcpu_state_blocked_interrupt) {
Andrew Walbran318f5732018-11-20 16:23:42 +0000633 target_vcpu->state = vcpu_state_ready;
Andrew Walbran69520dc2018-12-06 11:39:38 +0000634 } else if (target_vcpu->state == vcpu_state_blocked_mailbox) {
635 /*
636 * If you change this logic make sure to update the
637 * need_vm_lock logic above to match.
638 */
639 target_vcpu->state = vcpu_state_ready;
640
641 /* Take target vCPU out of mailbox recv_waiter list. */
642 /*
Andrew Walbrana793ea02018-12-12 13:21:47 +0000643 * TODO: Consider using a doubly-linked list for the
Andrew Walbran69520dc2018-12-06 11:39:38 +0000644 * receive waiter list to avoid the linear search here.
645 */
646 struct vcpu **previous_next_pointer =
647 &target_vm->mailbox.recv_waiter;
648 while (*previous_next_pointer != NULL &&
649 *previous_next_pointer != target_vcpu) {
650 /*
651 * TODO(qwandor): Do we need to lock the vCPUs
652 * somehow while we walk the linked list, or is
653 * the VM lock enough?
654 */
655 previous_next_pointer =
656 &(*previous_next_pointer)->mailbox_next;
657 }
658 if (*previous_next_pointer == NULL) {
659 dlog("Target VCPU state is "
660 "vcpu_state_blocked_mailbox but is not in "
661 "VM mailbox waiter list. This should "
662 "never happen.\n");
663 } else {
664 *previous_next_pointer =
665 target_vcpu->mailbox_next;
666 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000667 }
668
669 if (current->vm->id != HF_PRIMARY_VM_ID &&
670 current != target_vcpu) {
671 /*
672 * Switch to the primary so that it can switch to the
673 * target.
674 */
675 struct hf_vcpu_run_return ret = {
676 .code = HF_VCPU_RUN_WAKE_UP,
677 .wake_up.vm_id = target_vm_id,
678 .wake_up.vcpu = target_vcpu_idx,
679 };
680 *next = api_switch_to_primary(current, ret,
681 vcpu_state_ready);
682 }
683 }
684
685 sl_unlock(&target_vcpu->lock);
Andrew Walbran69520dc2018-12-06 11:39:38 +0000686 if (need_vm_lock) {
687 sl_unlock(&target_vm->lock);
688 }
Andrew Walbran318f5732018-11-20 16:23:42 +0000689
690 return 0;
691}