blob: 11df107d282a611f3e1fc7d60611dd611aeeaf8c [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;
303 goto out;
304 }
305
306 /*
307 * Try to find a vcpu to handle the message and tell the scheduler to
308 * run it.
309 */
310 if (to->mailbox.recv_waiter == NULL) {
311 /*
312 * The scheduler must choose a vcpu to interrupt so it can
313 * handle the message.
314 */
315 to->mailbox.state = mailbox_state_received;
316 vcpu = HF_INVALID_VCPU;
317 } else {
318 struct vcpu *to_vcpu = to->mailbox.recv_waiter;
319
320 /*
321 * Take target vcpu out of waiter list and mark as ready
322 * to run again.
323 */
324 sl_lock(&to_vcpu->lock);
325 to->mailbox.recv_waiter = to_vcpu->mailbox_next;
326 to_vcpu->state = vcpu_state_ready;
327
328 /* Return from HF_MAILBOX_RECEIVE. */
329 arch_regs_set_retval(&to_vcpu->regs,
330 HF_MAILBOX_RECEIVE_RESPONSE(
331 to->mailbox.recv_from_id, size));
332
333 sl_unlock(&to_vcpu->lock);
334
335 vcpu = to_vcpu - to->vcpus;
336 }
337
338 /* Return to the primary VM directly or with a switch. */
339 primary_ret = HF_VCPU_RUN_RESPONSE(HF_VCPU_RUN_WAKE_UP, to->id, vcpu);
340 ret = 0;
341
342out:
343 /*
344 * Unlock before routing the return values as switching to the primary
345 * will acquire more locks and nesting the locks is avoidable.
346 */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100347 sl_unlock(&to->lock);
348
Andrew Scullaa039b32018-10-04 15:02:26 +0100349 /* Report errors to the sender. */
350 if (ret != 0) {
351 return ret;
352 }
353
354 /* If the sender is the primary, return the vcpu to schedule. */
355 if (from->id == HF_PRIMARY_VM_ID) {
356 return vcpu;
357 }
358
359 /* Switch to primary for scheduling and return success to the sender. */
360 *next = api_switch_to_primary(primary_ret, vcpu_state_ready);
361 return 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100362}
363
364/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100365 * Receives a message from the mailbox. If one isn't available, this function
366 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100367 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100368 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100369 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100370int64_t api_mailbox_receive(bool block, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100371{
372 struct vcpu *vcpu = cpu()->current;
373 struct vm *vm = vcpu->vm;
Andrew Scullaa039b32018-10-04 15:02:26 +0100374 int64_t ret = 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100375
Andrew Scullaa039b32018-10-04 15:02:26 +0100376 /*
377 * The primary VM will receive messages as a status code from running
378 * vcpus and must not call this function.
379 */
Andrew Scull19503262018-09-20 14:48:39 +0100380 if (vm->id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100381 return -1;
382 }
383
384 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100385
Andrew Scullaa039b32018-10-04 15:02:26 +0100386 /* Return pending messages without blocking. */
387 if (vm->mailbox.state == mailbox_state_received) {
388 vm->mailbox.state = mailbox_state_read;
389 block = false;
390 ret = HF_MAILBOX_RECEIVE_RESPONSE(vm->mailbox.recv_from_id,
391 vm->mailbox.recv_bytes);
392 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100393 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100394
395 /* No pending message so fail if not allowed to block. */
396 if (!block) {
397 ret = -1;
398 goto out;
399 }
400
401 sl_lock(&vcpu->lock);
402 vcpu->state = vcpu_state_blocked_mailbox;
403
404 /* Push vcpu into waiter list. */
405 vcpu->mailbox_next = vm->mailbox.recv_waiter;
406 vm->mailbox.recv_waiter = vcpu;
407 sl_unlock(&vcpu->lock);
408
409 /* Switch back to primary vm to block. */
410 *next = api_wait_for_interrupt();
411
412out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100413 sl_unlock(&vm->lock);
414
415 return ret;
416}
417
418/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100419 * Clears the caller's mailbox so that a new message can be received. The caller
420 * must have copied out all data they wish to preserve as new messages will
421 * overwrite the old and will arrive asynchronously.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100422 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100423int64_t api_mailbox_clear(void)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100424{
425 struct vm *vm = cpu()->current->vm;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100426 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100427
428 sl_lock(&vm->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100429 if (vm->mailbox.state == mailbox_state_read) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100430 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100431 vm->mailbox.state = mailbox_state_empty;
432 } else {
433 ret = -1;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100434 }
435 sl_unlock(&vm->lock);
436
437 if (ret == 0) {
438 /* TODO: Notify waiters, if any. */
439 }
440
441 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100442}