blob: 1d44691a65f73a642af5c9319ff4d1edda772ffd [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 Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/std.h"
22#include "hf/vm.h"
23
Andrew Scullf35a5c92018-08-07 18:09:46 +010024#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010025
Andrew Scullaa039b32018-10-04 15:02:26 +010026static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010027 "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 Filho3fcbcff2018-07-10 23:53:39 +010030/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010031 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010032 *
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 Filho2f94ec12018-07-26 16:00:48 +010036 */
Andrew Scull6bca35e2018-10-02 12:05:32 +010037static struct vcpu *api_switch_to_primary(size_t primary_retval,
38 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010039{
40 struct vcpu *vcpu = cpu()->current;
Andrew Scull19503262018-09-20 14:48:39 +010041 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
42 struct vcpu *next = &primary->vcpus[cpu_index(cpu())];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010043
44 /* Switch back to primary VM. */
Andrew Scull19503262018-09-20 14:48:39 +010045 vm_set_current(primary);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010046
47 /*
Andrew Scullaa039b32018-10-04 15:02:26 +010048 * Set the return valuefor the primary VM's call to HF_VCPU_RUN.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010049 */
50 arch_regs_set_retval(&next->regs, primary_retval);
51
52 /* Mark the vcpu as waiting. */
53 sl_lock(&vcpu->lock);
54 vcpu->state = secondary_state;
55 sl_unlock(&vcpu->lock);
56
57 return next;
58}
59
60/**
Andrew Scullaa039b32018-10-04 15:02:26 +010061 * Returns to the primary vm leaving the current vcpu ready to be scheduled
62 * again.
63 */
64struct vcpu *api_yield(void)
65{
66 return api_switch_to_primary(
67 HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_YIELD, 0, 0),
68 vcpu_state_ready);
69}
70
71/**
72 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
73 * vm.
74 */
75struct vcpu *api_wait_for_interrupt(void)
76{
77 return api_switch_to_primary(
78 HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0, 0),
79 vcpu_state_blocked_interrupt);
80}
81
82/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010083 * Returns the number of VMs configured to run.
84 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010085int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010086{
Andrew Scull19503262018-09-20 14:48:39 +010087 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010088}
89
90/**
91 * Returns the number of vcpus configured in the given VM.
92 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010093int64_t api_vcpu_get_count(uint32_t vm_id)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010094{
Andrew Scull19503262018-09-20 14:48:39 +010095 struct vm *vm;
96
97 /* Only the primary VM needs to know about vcpus for scheduling. */
98 if (cpu()->current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010099 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100100 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100101
Andrew Scull19503262018-09-20 14:48:39 +0100102 vm = vm_get(vm_id);
103 if (vm == NULL) {
104 return -1;
105 }
106
107 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100108}
109
110/**
111 * Runs the given vcpu of the given vm.
112 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100113int64_t api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100114{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100115 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100116 struct vcpu *vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100117 int64_t ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100118
119 /* Only the primary VM can switch vcpus. */
Andrew Scull19503262018-09-20 14:48:39 +0100120 if (cpu()->current->vm->id != HF_PRIMARY_VM_ID) {
121 goto fail;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100122 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100123
Andrew Scull19503262018-09-20 14:48:39 +0100124 /* Only secondary VM vcpus can be run. */
125 if (vm_id == HF_PRIMARY_VM_ID) {
126 goto fail;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100127 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100128
Andrew Scull19503262018-09-20 14:48:39 +0100129 /* The requested VM must exist. */
130 vm = vm_get(vm_id);
131 if (vm == NULL) {
132 goto fail;
133 }
134
135 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100136 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull19503262018-09-20 14:48:39 +0100137 goto fail;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100138 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100139
Andrew Scullf3d45592018-09-20 14:30:22 +0100140 vcpu = &vm->vcpus[vcpu_idx];
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100141
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100142 sl_lock(&vcpu->lock);
143 if (vcpu->state != vcpu_state_ready) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100144 ret = HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0,
145 0);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100146 } else {
147 vcpu->state = vcpu_state_running;
148 vm_set_current(vm);
149 *next = vcpu;
Andrew Scullaa039b32018-10-04 15:02:26 +0100150 ret = HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_YIELD, 0, 0);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100151 }
152 sl_unlock(&vcpu->lock);
153
154 return ret;
Andrew Scull19503262018-09-20 14:48:39 +0100155
156fail:
Andrew Scullaa039b32018-10-04 15:02:26 +0100157 return HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAIT_FOR_INTERRUPT, 0, 0);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100158}
159
160/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100161 * Configures the VM to send/receive data through the specified pages. The pages
162 * must not be shared.
163 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100164int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100165{
166 struct vm *vm = cpu()->current->vm;
Andrew Scull80871322018-08-06 12:04:09 +0100167 paddr_t pa_send_begin;
168 paddr_t pa_send_end;
169 paddr_t pa_recv_begin;
170 paddr_t pa_recv_end;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100171 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100172
173 /* Fail if addresses are not page-aligned. */
Andrew Scull265ada92018-07-30 15:19:01 +0100174 if ((ipa_addr(send) & (PAGE_SIZE - 1)) ||
175 (ipa_addr(recv) & (PAGE_SIZE - 1))) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100176 return -1;
177 }
178
179 sl_lock(&vm->lock);
180
181 /* We only allow these to be setup once. */
Andrew Scullaa039b32018-10-04 15:02:26 +0100182 if (vm->mailbox.send || vm->mailbox.recv) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100183 ret = -1;
184 goto exit;
185 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100186
187 /*
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100188 * TODO: Once memory sharing is implemented, we need to make sure that
189 * these pages aren't and won't be shared.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100190 */
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100191
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100192 /*
Andrew Scull265ada92018-07-30 15:19:01 +0100193 * Convert the intermediate physical addresses to physical address
194 * provided the address was acessible from the VM which ensures that the
195 * caller isn't trying to use another VM's memory.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100196 */
Andrew Scull80871322018-08-06 12:04:09 +0100197 if (!mm_vm_translate(&vm->ptable, send, &pa_send_begin) ||
198 !mm_vm_translate(&vm->ptable, recv, &pa_recv_begin)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100199 ret = -1;
200 goto exit;
201 }
202
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100203 /* Fail if the same page is used for the send and receive pages. */
204 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
205 ret = -1;
206 goto exit;
207 }
208
Andrew Scull80871322018-08-06 12:04:09 +0100209 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
210 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
Andrew Scull265ada92018-07-30 15:19:01 +0100211
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100212 /* Map the send page as read-only in the hypervisor address space. */
Andrew Scullaa039b32018-10-04 15:02:26 +0100213 vm->mailbox.send =
214 mm_identity_map(pa_send_begin, pa_send_end, MM_MODE_R);
215 if (!vm->mailbox.send) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100216 ret = -1;
217 goto exit;
218 }
219
220 /*
221 * Map the receive page as writable in the hypervisor address space. On
222 * failure, unmap the send page before returning.
223 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100224 vm->mailbox.recv =
225 mm_identity_map(pa_recv_begin, pa_recv_end, MM_MODE_W);
226 if (!vm->mailbox.recv) {
227 vm->mailbox.send = NULL;
Andrew Scull80871322018-08-06 12:04:09 +0100228 mm_unmap(pa_send_begin, pa_send_end, 0);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100229 ret = -1;
230 goto exit;
231 }
232
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100233 /* TODO: Notify any waiters. */
234
235 ret = 0;
236exit:
237 sl_unlock(&vm->lock);
238
239 return ret;
240}
241
242/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100243 * Copies data from the sender's send buffer to the recipient's receive buffer
244 * and notifies the recipient.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100245 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100246int64_t api_mailbox_send(uint32_t vm_id, size_t size, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100247{
248 struct vm *from = cpu()->current->vm;
249 struct vm *to;
250 const void *from_buf;
Andrew Scullaa039b32018-10-04 15:02:26 +0100251 uint16_t vcpu;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100252 int64_t ret;
Andrew Scullaa039b32018-10-04 15:02:26 +0100253 int64_t primary_ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100254
Andrew Scullaa039b32018-10-04 15:02:26 +0100255 /* Limit the size of transfer. */
256 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100257 return -1;
258 }
259
260 /* Disallow reflexive requests as this suggests an error in the VM. */
261 if (vm_id == from->id) {
262 return -1;
263 }
264
265 /* Ensure the target VM exists. */
266 to = vm_get(vm_id);
267 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100268 return -1;
269 }
270
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100271 /*
272 * Check that the sender has configured its send buffer. It is safe to
273 * use from_buf after releasing the lock because the buffer cannot be
274 * modified once it's configured.
275 */
276 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100277 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100278 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100279 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100280 return -1;
281 }
282
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100283 sl_lock(&to->lock);
284
Andrew Scullaa039b32018-10-04 15:02:26 +0100285 if (to->mailbox.state != mailbox_state_empty ||
286 to->mailbox.recv == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100287 /* Fail if the target isn't currently ready to receive data. */
288 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100289 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100290 }
291
Andrew Scullaa039b32018-10-04 15:02:26 +0100292 /* Copy data. */
293 memcpy(to->mailbox.recv, from_buf, size);
294 to->mailbox.recv_bytes = size;
295 to->mailbox.recv_from_id = from->id;
296 to->mailbox.state = mailbox_state_read;
297
298 /* Messages for the primary VM are delivered directly. */
299 if (to->id == HF_PRIMARY_VM_ID) {
300 primary_ret =
301 HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_MESSAGE, 0, size);
302 ret = 0;
Andrew Scull4a496572018-10-11 18:44:08 +0100303 /*
304 * clang-tidy isn't able to prove that
305 * `from->id != HF_PRIMARY_VM_ID` so cover that specific case
306 * explicitly so as not to hide other possible bugs.
307 */
308 if (from->id == HF_PRIMARY_VM_ID) {
309 vcpu = 0;
310 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100311 goto out;
312 }
313
314 /*
315 * Try to find a vcpu to handle the message and tell the scheduler to
316 * run it.
317 */
318 if (to->mailbox.recv_waiter == NULL) {
319 /*
320 * The scheduler must choose a vcpu to interrupt so it can
321 * handle the message.
322 */
323 to->mailbox.state = mailbox_state_received;
324 vcpu = HF_INVALID_VCPU;
325 } else {
326 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
327
328 /*
329 * Take target vcpu out of waiter list and mark as ready
330 * to run again.
331 */
332 sl_lock(&to_vcpu->lock);
333 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
334 to_vcpu->state = vcpu_state_ready;
335
336 /* Return from HF_MAILBOX_RECEIVE. */
337 arch_regs_set_retval(&to_vcpu->regs,
338 HF_MAILBOX_RECEIVE_RESPONSE(
339 to->mailbox.recv_from_id, size));
340
341 sl_unlock(&to_vcpu->lock);
342
343 vcpu = to_vcpu - to->vcpus;
344 }
345
346 /* Return to the primary VM directly or with a switch. */
347 primary_ret = HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAKE_UP, to->id, vcpu);
348 ret = 0;
349
350out:
351 /*
352 * Unlock before routing the return values as switching to the primary
353 * will acquire more locks and nesting the locks is avoidable.
354 */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100355 sl_unlock(&to->lock);
356
Andrew Scullaa039b32018-10-04 15:02:26 +0100357 /* Report errors to the sender. */
358 if (ret != 0) {
359 return ret;
360 }
361
362 /* If the sender is the primary, return the vcpu to schedule. */
363 if (from->id == HF_PRIMARY_VM_ID) {
364 return vcpu;
365 }
366
367 /* Switch to primary for scheduling and return success to the sender. */
368 *next = api_switch_to_primary(primary_ret, vcpu_state_ready);
369 return 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100370}
371
372/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100373 * Receives a message from the mailbox. If one isn't available, this function
374 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100375 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100376 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100377 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100378int64_t api_mailbox_receive(bool block, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100379{
380 struct vcpu *vcpu = cpu()->current;
381 struct vm *vm = vcpu->vm;
Andrew Scullaa039b32018-10-04 15:02:26 +0100382 int64_t ret = 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100383
Andrew Scullaa039b32018-10-04 15:02:26 +0100384 /*
385 * The primary VM will receive messages as a status code from running
386 * vcpus and must not call this function.
387 */
Andrew Scull19503262018-09-20 14:48:39 +0100388 if (vm->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100389 return -1;
390 }
391
392 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100393
Andrew Scullaa039b32018-10-04 15:02:26 +0100394 /* Return pending messages without blocking. */
395 if (vm->mailbox.state == mailbox_state_received) {
396 vm->mailbox.state = mailbox_state_read;
Andrew Scullaa039b32018-10-04 15:02:26 +0100397 ret = HF_MAILBOX_RECEIVE_RESPONSE(vm->mailbox.recv_from_id,
398 vm->mailbox.recv_bytes);
399 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100400 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100401
402 /* No pending message so fail if not allowed to block. */
403 if (!block) {
404 ret = -1;
405 goto out;
406 }
407
408 sl_lock(&vcpu->lock);
409 vcpu->state = vcpu_state_blocked_mailbox;
410
411 /* Push vcpu into waiter list. */
412 vcpu->mailbox_next = vm->mailbox.recv_waiter;
413 vm->mailbox.recv_waiter = vcpu;
414 sl_unlock(&vcpu->lock);
415
416 /* Switch back to primary vm to block. */
417 *next = api_wait_for_interrupt();
418
419out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100420 sl_unlock(&vm->lock);
421
422 return ret;
423}
424
425/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100426 * Clears the caller's mailbox so that a new message can be received. The caller
427 * must have copied out all data they wish to preserve as new messages will
428 * overwrite the old and will arrive asynchronously.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100429 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100430int64_t api_mailbox_clear(void)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100431{
432 struct vm *vm = cpu()->current->vm;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100433 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100434
435 sl_lock(&vm->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100436 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100437 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100438 vm->mailbox.state = mailbox_state_empty;
439 } else {
440 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100441 }
442 sl_unlock(&vm->lock);
443
444 if (ret == 0) {
445 /* TODO: Notify waiters, if any. */
446 }
447
448 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100449}