blob: dac53889986afff138dd7f7056da81f9a415eb15 [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);
416 current->state = vcpu_state_blocked_mailbox;
Andrew Scullaa039b32018-10-04 15:02:26 +0100417
418 /* Push vcpu into waiter list. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100419 current->mailbox_next = vm->mailbox.recv_waiter;
420 vm->mailbox.recv_waiter = current;
421 sl_unlock(&current->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100422
423 /* Switch back to primary vm to block. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100424 *next = api_wait_for_interrupt(current);
Andrew Scullaa039b32018-10-04 15:02:26 +0100425out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100426 sl_unlock(&vm->lock);
427
428 return ret;
429}
430
431/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100432 * 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 Filho2f94ec12018-07-26 16:00:48 +0100435 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100436int64_t api_mailbox_clear(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100437{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100438 struct vm *vm = current->vm;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100439 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100440
441 sl_lock(&vm->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100442 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100443 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100444 vm->mailbox.state = mailbox_state_empty;
445 } else {
446 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100447 }
448 sl_unlock(&vm->lock);
449
450 if (ret == 0) {
451 /* TODO: Notify waiters, if any. */
452 }
453
454 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100455}
Andrew Walbran318f5732018-11-20 16:23:42 +0000456
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 */
462int64_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(&current->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(&current->regs, true);
478 }
479 } else {
480 current->interrupts.interrupt_enabled[intid_index] &=
481 ~intid_mask;
482 }
483
484 sl_unlock(&current->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 */
493uint32_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(&current->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(&current->regs, interrupts_remain);
534
535 sl_unlock(&current->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 */
543static 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 */
564int64_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}