blob: 938d978ad346410b3b2f46da2394323bc0a5acbe [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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"
Andrew Walbran4a53ba62019-03-05 17:26:12 +000022#include "hf/arch/std.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000023#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000024
25#include "hf/dlog.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/mm.h"
27#include "hf/spinlock.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/vm.h"
29
Andrew Scullf35a5c92018-08-07 18:09:46 +010030#include "vmapi/hf/call.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010031
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000032/*
33 * To eliminate the risk of deadlocks, we define a partial order for the
34 * acquisition of locks held concurrently by the same physical CPU. Our current
35 * ordering requirements are as follows:
36 *
37 * vm::lock -> vcpu::lock
Andrew Scull6386f252018-12-06 13:29:10 +000038 *
39 * Locks of the same kind require the lock of lowest address to be locked first,
40 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000041 */
42
Andrew Scullaa039b32018-10-04 15:02:26 +010043static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010044 "Currently, a page is mapped for the send and receive buffers so "
45 "the maximum request is the size of a page.");
46
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000047static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000048
49/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000050 * Initialises the API page pool by taking ownership of the contents of the
51 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000052 */
53void api_init(struct mpool *ppool)
54{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000055 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000056}
57
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010058/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010059 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010060 *
61 * This triggers the scheduling logic to run. Run in the context of secondary VM
62 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
63 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010064 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010065static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000066 struct hf_vcpu_run_return primary_ret,
67 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010068{
Andrew Scull19503262018-09-20 14:48:39 +010069 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010070 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010071
Andrew Walbran508e63c2018-12-20 17:02:37 +000072 /*
73 * If the secondary is blocked but has a timer running, sleep until the
74 * timer fires rather than indefinitely.
75 */
Andrew Scullb06d1752019-02-04 10:15:48 +000076 switch (primary_ret.code) {
77 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
78 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
79 primary_ret.sleep.ns =
80 arch_timer_enabled_current()
81 ? arch_timer_remaining_ns_current()
82 : HF_SLEEP_INDEFINITE;
83 break;
84
85 default:
86 /* Do nothing. */
87 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +000088 }
89
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010090 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010091 arch_regs_set_retval(&next->regs,
92 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010093
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000094 /* Mark the current vcpu as waiting. */
95 sl_lock(&current->lock);
96 current->state = secondary_state;
97 sl_unlock(&current->lock);
98
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010099 return next;
100}
101
102/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000103 * Returns to the primary vm and signals that the vcpu still has work to do so.
104 */
105struct vcpu *api_preempt(struct vcpu *current)
106{
107 struct hf_vcpu_run_return ret = {
108 .code = HF_VCPU_RUN_PREEMPTED,
109 };
110
111 return api_switch_to_primary(current, ret, vcpu_state_ready);
112}
113
114/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100115 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
116 * vm.
117 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100118struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100119{
Andrew Scull6d2db332018-10-10 15:28:17 +0100120 struct hf_vcpu_run_return ret = {
121 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
122 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000123
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000124 return api_switch_to_primary(current, ret,
125 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100126}
127
128/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000129 * Returns to the primary vm to allow this cpu to be used for other tasks as the
130 * vcpu does not have work to do at this moment. The current vcpu is marked as
131 * ready to be scheduled again.
132 */
133struct vcpu *api_yield(struct vcpu *current)
134{
135 struct hf_vcpu_run_return ret = {
136 .code = HF_VCPU_RUN_YIELD,
137 };
138
139 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000140 /* Noop on the primary as it makes the scheduling decisions. */
Andrew Scull66d62bf2019-02-01 13:54:10 +0000141 return NULL;
142 }
143
144 return api_switch_to_primary(current, ret, vcpu_state_ready);
145}
146
147/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000148 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000149 */
150struct vcpu *api_abort(struct vcpu *current)
151{
152 struct hf_vcpu_run_return ret = {
153 .code = HF_VCPU_RUN_ABORTED,
154 };
155
156 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
157
158 if (current->vm->id == HF_PRIMARY_VM_ID) {
159 /* TODO: what to do when the primary aborts? */
160 for (;;) {
161 /* Do nothing. */
162 }
163 }
164
165 atomic_store_explicit(&current->vm->aborting, true,
166 memory_order_relaxed);
167
168 /* TODO: free resources once all vCPUs abort. */
169
170 return api_switch_to_primary(current, ret, vcpu_state_aborted);
171}
172
173/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000174 * Returns the ID of the VM.
175 */
176int64_t api_vm_get_id(const struct vcpu *current)
177{
178 return current->vm->id;
179}
180
181/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100182 * Returns the number of VMs configured to run.
183 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100184int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100185{
Andrew Scull19503262018-09-20 14:48:39 +0100186 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100187}
188
189/**
190 * Returns the number of vcpus configured in the given VM.
191 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100192int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100193{
Andrew Scull19503262018-09-20 14:48:39 +0100194 struct vm *vm;
195
196 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100197 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100198 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100199 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100200
Andrew Scull19503262018-09-20 14:48:39 +0100201 vm = vm_get(vm_id);
202 if (vm == NULL) {
203 return -1;
204 }
205
206 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100207}
208
209/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000210 * This function is called by the architecture-specific context switching
211 * function to indicate that register state for the given vcpu has been saved
212 * and can therefore be used by other pcpus.
213 */
214void api_regs_state_saved(struct vcpu *vcpu)
215{
216 sl_lock(&vcpu->lock);
217 vcpu->regs_available = true;
218 sl_unlock(&vcpu->lock);
219}
220
221/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000222 * Retrieves the next waiter and removes it from the wait list if the VM's
223 * mailbox is in a writable state.
224 */
225static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
226{
227 struct wait_entry *entry;
228 struct vm *vm = locked_vm.vm;
229
230 if (vm->mailbox.state != mailbox_state_empty ||
231 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
232 /* The mailbox is not writable or there are no waiters. */
233 return NULL;
234 }
235
236 /* Remove waiter from the wait list. */
237 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
238 wait_links);
239 list_remove(&entry->wait_links);
240 return entry;
241}
242
243/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000244 * Assuming that the arguments have already been checked by the caller, injects
245 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
246 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
247 * is next run, which is up to the scheduler.
248 *
249 * Returns:
250 * - 0 on success if no further action is needed.
251 * - 1 if it was called by the primary VM and the primary VM now needs to wake
252 * up or kick the target vCPU.
253 */
254static int64_t internal_interrupt_inject(struct vm *target_vm,
255 struct vcpu *target_vcpu,
256 uint32_t intid, struct vcpu *current,
257 struct vcpu **next)
258{
259 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
260 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000261 int64_t ret = 0;
262
263 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000264
265 /*
266 * We only need to change state and (maybe) trigger a virtual IRQ if it
267 * is enabled and was not previously pending. Otherwise we can skip
268 * everything except setting the pending bit.
269 *
270 * If you change this logic make sure to update the need_vm_lock logic
271 * above to match.
272 */
273 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
274 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
275 intid_mask)) {
276 goto out;
277 }
278
279 /* Increment the count. */
280 target_vcpu->interrupts.enabled_and_pending_count++;
281
282 /*
283 * Only need to update state if there was not already an
284 * interrupt enabled and pending.
285 */
286 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
287 goto out;
288 }
289
Andrew Walbran508e63c2018-12-20 17:02:37 +0000290 if (current->vm->id == HF_PRIMARY_VM_ID) {
291 /*
292 * If the call came from the primary VM, let it know that it
293 * should run or kick the target vCPU.
294 */
295 ret = 1;
296 } else if (current != target_vcpu && next != NULL) {
297 /*
298 * Switch to the primary so that it can switch to the target, or
299 * kick it if it is already running on a different physical CPU.
300 */
301 struct hf_vcpu_run_return ret = {
302 .code = HF_VCPU_RUN_WAKE_UP,
303 .wake_up.vm_id = target_vm->id,
304 .wake_up.vcpu = target_vcpu - target_vm->vcpus,
305 };
306 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
307 }
308
309out:
310 /* Either way, make it pending. */
311 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
312
313 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000314
315 return ret;
316}
317
318/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000319 * Prepares the vcpu to run by updating its state and fetching whether a return
320 * value needs to be forced onto the vCPU.
321 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000322static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000323 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000324{
Andrew Scullb06d1752019-02-04 10:15:48 +0000325 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000326 bool ret;
327
Andrew Scullb06d1752019-02-04 10:15:48 +0000328 /*
329 * Wait until the registers become available. All locks must be
330 * released between iterations of this loop to avoid potential deadlocks
331 * if, on any path, a lock needs to be taken after taking the decision
332 * to switch context but before the registers have been saved.
333 *
334 * The VM lock is not needed in the common case so it must only be taken
335 * when it is going to be needed. This ensures there are no inter-vCPU
336 * dependencies in the common run case meaning the sensitive context
337 * switch performance is consistent.
338 */
339 for (;;) {
340 sl_lock(&vcpu->lock);
341
342 /* The VM needs to be locked to deliver mailbox messages. */
343 need_vm_lock = vcpu->state == vcpu_state_blocked_mailbox;
344 if (need_vm_lock) {
345 sl_unlock(&vcpu->lock);
346 sl_lock(&vcpu->vm->lock);
347 sl_lock(&vcpu->lock);
348 }
349
350 if (vcpu->regs_available) {
351 break;
352 }
353
354 if (vcpu->state == vcpu_state_running) {
355 /*
356 * vCPU is running on another pCPU.
357 *
358 * It's ok to not return the sleep duration here because
359 * the other physical CPU that is currently running this
360 * vCPU will return sleep duration if neeed. The default
361 * return value is HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no
362 * need to set it explicitly.
363 */
364 ret = false;
365 goto out;
366 }
367
368 sl_unlock(&vcpu->lock);
369 if (need_vm_lock) {
370 sl_unlock(&vcpu->vm->lock);
371 }
372 }
Andrew Scull9726c252019-01-23 13:44:19 +0000373
374 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
375 if (vcpu->state != vcpu_state_aborted) {
Andrew Scull82331282019-01-25 10:29:34 +0000376 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
377 vcpu_index(vcpu));
Andrew Scull9726c252019-01-23 13:44:19 +0000378 vcpu->state = vcpu_state_aborted;
379 }
380 ret = false;
381 goto out;
382 }
383
Andrew Walbran508e63c2018-12-20 17:02:37 +0000384 switch (vcpu->state) {
385 case vcpu_state_running:
386 case vcpu_state_off:
387 case vcpu_state_aborted:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000388 ret = false;
389 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000390
Andrew Walbran508e63c2018-12-20 17:02:37 +0000391 case vcpu_state_blocked_mailbox:
Andrew Scullb06d1752019-02-04 10:15:48 +0000392 /*
393 * A pending message allows the vCPU to run so the message can
394 * be delivered directly.
395 */
396 if (vcpu->vm->mailbox.state == mailbox_state_received) {
397 arch_regs_set_retval(
398 &vcpu->regs,
399 hf_mailbox_receive_return_encode((
400 struct hf_mailbox_receive_return){
401 .vm_id = vcpu->vm->mailbox.recv_from_id,
402 .size = vcpu->vm->mailbox.recv_bytes,
403 }));
404 vcpu->vm->mailbox.state = mailbox_state_read;
405 break;
406 }
407 /* Fall through. */
408 case vcpu_state_blocked_interrupt:
409 /* Allow virtual interrupts to be delivered. */
410 if (vcpu->interrupts.enabled_and_pending_count > 0) {
411 break;
412 }
413
414 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000415 if (arch_timer_pending(&vcpu->regs)) {
416 break;
417 }
418
419 /*
420 * The vCPU is not ready to run, return the appropriate code to
421 * the primary which called vcpu_run.
422 */
423 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000424 run_ret->code =
425 vcpu->state == vcpu_state_blocked_mailbox
426 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
427 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000428 run_ret->sleep.ns =
429 arch_timer_remaining_ns(&vcpu->regs);
430 }
431
432 ret = false;
433 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000434
Andrew Walbran508e63c2018-12-20 17:02:37 +0000435 case vcpu_state_ready:
436 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000437 }
438
Andrew Scullb06d1752019-02-04 10:15:48 +0000439 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000440 vcpu->cpu = current->cpu;
441 vcpu->state = vcpu_state_running;
442
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000443 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000444 * Mark the registers as unavailable now that we're about to reflect
445 * them onto the real registers. This will also prevent another physical
446 * CPU from trying to read these registers.
447 */
448 vcpu->regs_available = false;
449
450 ret = true;
451
452out:
453 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000454 if (need_vm_lock) {
455 sl_unlock(&vcpu->vm->lock);
456 }
457
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000458 return ret;
459}
460
461/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100462 * Runs the given vcpu of the given vm.
463 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100464struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000465 const struct vcpu *current,
466 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100467{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100468 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100469 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100470 struct hf_vcpu_run_return ret = {
471 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000472 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100473 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100474
475 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100476 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100477 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100478 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100479
Andrew Scull19503262018-09-20 14:48:39 +0100480 /* Only secondary VM vcpus can be run. */
481 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100482 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100483 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100484
Andrew Scull19503262018-09-20 14:48:39 +0100485 /* The requested VM must exist. */
486 vm = vm_get(vm_id);
487 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100488 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100489 }
490
491 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100492 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100493 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100494 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100495
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000496 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100497 vcpu = &vm->vcpus[vcpu_idx];
Andrew Scullb06d1752019-02-04 10:15:48 +0000498 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000499 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100500 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000501
Andrew Walbran508e63c2018-12-20 17:02:37 +0000502 /*
503 * Inject timer interrupt if timer has expired. It's safe to access
504 * vcpu->regs here because api_vcpu_prepare_run already made sure that
505 * regs_available was true (and then set it to false) before returning
506 * true.
507 */
508 if (arch_timer_pending(&vcpu->regs)) {
509 /* Make virtual timer interrupt pending. */
510 internal_interrupt_inject(vm, vcpu, HF_VIRTUAL_TIMER_INTID,
511 vcpu, NULL);
512
513 /*
514 * Set the mask bit so the hardware interrupt doesn't fire
515 * again. Ideally we wouldn't do this because it affects what
516 * the secondary vCPU sees, but if we don't then we end up with
517 * a loop of the interrupt firing each time we try to return to
518 * the secondary vCPU.
519 */
520 arch_timer_mask(&vcpu->regs);
521 }
522
Andrew Scull33fecd32019-01-08 14:48:27 +0000523 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000524 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000525
Andrew Scull33fecd32019-01-08 14:48:27 +0000526 /*
527 * Set a placeholder return code to the scheduler. This will be
528 * overwritten when the switch back to the primary occurs.
529 */
530 ret.code = HF_VCPU_RUN_PREEMPTED;
531
Andrew Scull6d2db332018-10-10 15:28:17 +0100532out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100533 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100534}
535
536/**
Andrew Scull81e85092018-12-12 12:56:20 +0000537 * Check that the mode indicates memory that is valid, owned and exclusive.
538 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000539static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000540{
541 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
542 0;
543}
544
545/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000546 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
547 * after they've succeeded. If a secondary VM is running and there are waiters,
548 * it also switches back to the primary VM for it to wake waiters up.
549 */
550static int64_t api_waiter_result(struct vm_locked locked_vm,
551 struct vcpu *current, struct vcpu **next)
552{
553 struct vm *vm = locked_vm.vm;
554 struct hf_vcpu_run_return ret = {
555 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
556 };
557
558 if (list_empty(&vm->mailbox.waiter_list)) {
559 /* No waiters, nothing else to do. */
560 return 0;
561 }
562
563 if (vm->id == HF_PRIMARY_VM_ID) {
564 /* The caller is the primary VM. Tell it to wake up waiters. */
565 return 1;
566 }
567
568 /*
569 * Switch back to the primary VM, informing it that there are waiters
570 * that need to be notified.
571 */
572 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
573
574 return 0;
575}
576
577/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100578 * Configures the VM to send/receive data through the specified pages. The pages
579 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000580 *
581 * Returns:
582 * - -1 on failure.
583 * - 0 on success if no further action is needed.
584 * - 1 if it was called by the primary VM and the primary VM now needs to wake
585 * up or kick waiters. Waiters should be retrieved by calling
586 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100587 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000588int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
589 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100590{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100591 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000592 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100593 paddr_t pa_send_begin;
594 paddr_t pa_send_end;
595 paddr_t pa_recv_begin;
596 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000597 int orig_send_mode;
598 int orig_recv_mode;
599 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100600 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100601
602 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000603 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
604 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100605 return -1;
606 }
607
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000608 /* Convert to physical addresses. */
609 pa_send_begin = pa_from_ipa(send);
610 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
611
612 pa_recv_begin = pa_from_ipa(recv);
613 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
614
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100615 /* Fail if the same page is used for the send and receive pages. */
616 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000617 return -1;
618 }
619
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000620 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000621
622 /* We only allow these to be setup once. */
623 if (vm->mailbox.send || vm->mailbox.recv) {
624 goto fail;
625 }
626
627 /*
628 * Ensure the pages are valid, owned and exclusive to the VM and that
629 * the VM has the required access to the memory.
630 */
631 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
632 &orig_send_mode) ||
633 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
634 (orig_send_mode & MM_MODE_R) == 0 ||
635 (orig_send_mode & MM_MODE_W) == 0) {
636 goto fail;
637 }
638
639 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
640 &orig_recv_mode) ||
641 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
642 (orig_recv_mode & MM_MODE_R) == 0) {
643 goto fail;
644 }
645
646 /*
647 * Create a local pool so any freed memory can't be used by another
648 * thread. This is to ensure the original mapping can be restored if any
649 * stage of the process fails.
650 */
651 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
652
653 /* Take memory ownership away from the VM and mark as shared. */
654 if (!mm_vm_identity_map(
655 &vm->ptable, pa_send_begin, pa_send_end,
656 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
657 NULL, &local_page_pool)) {
658 goto fail_free_pool;
659 }
660
661 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
662 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
663 NULL, &local_page_pool)) {
664 /* TODO: partial defrag of failed range. */
665 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000666 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000667 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100668 }
669
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100670 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000671 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000672 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100673 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000674 /* TODO: partial defrag of failed range. */
675 /* Recover any memory consumed in failed mapping. */
676 mm_defrag(&local_page_pool);
677 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100678 }
679
680 /*
681 * Map the receive page as writable in the hypervisor address space. On
682 * failure, unmap the send page before returning.
683 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000684 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000685 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100686 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000687 /* TODO: partial defrag of failed range. */
688 /* Recover any memory consumed in failed mapping. */
689 mm_defrag(&local_page_pool);
690 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100691 }
692
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000693 /* Tell caller about waiters, if any. */
694 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000695 goto exit;
696
697 /*
698 * The following mappings will not require more memory than is available
699 * in the local pool.
700 */
701fail_undo_all:
702 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000703 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000704
705fail_undo_send_and_recv:
706 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
707 orig_recv_mode, NULL, &local_page_pool);
708
709fail_undo_send:
710 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
711 orig_send_mode, NULL, &local_page_pool);
712
713fail_free_pool:
714 mpool_fini(&local_page_pool);
715
716fail:
717 ret = -1;
718
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100719exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000720 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100721
722 return ret;
723}
724
725/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100726 * Copies data from the sender's send buffer to the recipient's receive buffer
727 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000728 *
729 * If the recipient's receive buffer is busy, it can optionally register the
730 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100731 */
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000732int64_t api_mailbox_send(uint32_t vm_id, size_t size, bool notify,
733 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100734{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100735 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100736 struct vm *to;
737 const void *from_buf;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100738 int64_t ret;
Andrew Scullb06d1752019-02-04 10:15:48 +0000739 struct hf_vcpu_run_return primary_ret = {
740 .code = HF_VCPU_RUN_MESSAGE,
741 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100742
Andrew Scullaa039b32018-10-04 15:02:26 +0100743 /* Limit the size of transfer. */
744 if (size > HF_MAILBOX_SIZE) {
Andrew Scull19503262018-09-20 14:48:39 +0100745 return -1;
746 }
747
748 /* Disallow reflexive requests as this suggests an error in the VM. */
749 if (vm_id == from->id) {
750 return -1;
751 }
752
753 /* Ensure the target VM exists. */
754 to = vm_get(vm_id);
755 if (to == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100756 return -1;
757 }
758
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100759 /*
760 * Check that the sender has configured its send buffer. It is safe to
761 * use from_buf after releasing the lock because the buffer cannot be
762 * modified once it's configured.
763 */
764 sl_lock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100765 from_buf = from->mailbox.send;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100766 sl_unlock(&from->lock);
Andrew Scullaa039b32018-10-04 15:02:26 +0100767 if (from_buf == NULL) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100768 return -1;
769 }
770
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100771 sl_lock(&to->lock);
772
Andrew Scullaa039b32018-10-04 15:02:26 +0100773 if (to->mailbox.state != mailbox_state_empty ||
774 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000775 /*
776 * Fail if the target isn't currently ready to receive data,
777 * setting up for notification if requested.
778 */
779 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000780 struct wait_entry *entry =
781 &current->vm->wait_entries[vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000782
783 /* Append waiter only if it's not there yet. */
784 if (list_empty(&entry->wait_links)) {
785 list_append(&to->mailbox.waiter_list,
786 &entry->wait_links);
787 }
788 }
789
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100790 ret = -1;
Andrew Scullaa039b32018-10-04 15:02:26 +0100791 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100792 }
793
Andrew Scullaa039b32018-10-04 15:02:26 +0100794 /* Copy data. */
795 memcpy(to->mailbox.recv, from_buf, size);
796 to->mailbox.recv_bytes = size;
797 to->mailbox.recv_from_id = from->id;
Andrew Scullb06d1752019-02-04 10:15:48 +0000798 primary_ret.message.vm_id = to->id;
799 ret = 0;
Andrew Scullaa039b32018-10-04 15:02:26 +0100800
801 /* Messages for the primary VM are delivered directly. */
802 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000803 primary_ret.message.size = size,
804 to->mailbox.state = mailbox_state_read;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000805 *next = api_switch_to_primary(current, primary_ret,
806 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100807 goto out;
808 }
809
Andrew Scullb06d1752019-02-04 10:15:48 +0000810 to->mailbox.state = mailbox_state_received;
Andrew Scullaa039b32018-10-04 15:02:26 +0100811
812 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000813 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000814 *next = api_switch_to_primary(current, primary_ret,
815 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000816 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100817
818out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100819 sl_unlock(&to->lock);
820
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000821 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100822}
823
824/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100825 * Receives a message from the mailbox. If one isn't available, this function
826 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100827 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100828 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100829 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100830struct hf_mailbox_receive_return api_mailbox_receive(bool block,
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100831 struct vcpu *current,
Andrew Scull6d2db332018-10-10 15:28:17 +0100832 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100833{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100834 struct vm *vm = current->vm;
Andrew Scull6d2db332018-10-10 15:28:17 +0100835 struct hf_mailbox_receive_return ret = {
836 .vm_id = HF_INVALID_VM_ID,
837 };
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100838
Andrew Scullaa039b32018-10-04 15:02:26 +0100839 /*
840 * The primary VM will receive messages as a status code from running
841 * vcpus and must not call this function.
842 */
Andrew Scull19503262018-09-20 14:48:39 +0100843 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100844 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100845 }
846
847 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100848
Andrew Scullaa039b32018-10-04 15:02:26 +0100849 /* Return pending messages without blocking. */
850 if (vm->mailbox.state == mailbox_state_received) {
851 vm->mailbox.state = mailbox_state_read;
Andrew Scull6d2db332018-10-10 15:28:17 +0100852 ret.vm_id = vm->mailbox.recv_from_id;
853 ret.size = vm->mailbox.recv_bytes;
Andrew Scullaa039b32018-10-04 15:02:26 +0100854 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100855 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100856
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000857 /*
858 * No pending message so fail if not allowed to block. Don't block if
859 * there are enabled and pending interrupts, to match behaviour of
860 * wait_for_interrupt.
861 */
862 if (!block || current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100863 goto out;
864 }
865
Andrew Scullaa039b32018-10-04 15:02:26 +0100866 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000867 {
868 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +0000869 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +0000870 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000871
Andrew Walbranb4816552018-12-05 17:35:42 +0000872 *next = api_switch_to_primary(current, run_return,
873 vcpu_state_blocked_mailbox);
874 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100875out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100876 sl_unlock(&vm->lock);
877
878 return ret;
879}
880
881/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000882 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
883 * by this function, the caller must have called api_mailbox_send before with
884 * the notify argument set to true, and this call must have failed because the
885 * mailbox was not available.
886 *
887 * It should be called repeatedly to retrieve a list of VMs.
888 *
889 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
890 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100891 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000892int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100893{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100894 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000895 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100896 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100897
898 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000899 if (list_empty(&vm->mailbox.ready_list)) {
900 ret = -1;
901 goto exit;
902 }
903
904 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
905 ready_links);
906 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000907 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000908
909exit:
910 sl_unlock(&vm->lock);
911 return ret;
912}
913
914/**
915 * Retrieves the next VM waiting to be notified that the mailbox of the
916 * specified VM became writable. Only primary VMs are allowed to call this.
917 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000918 * Returns -1 on failure or if there are no waiters; the VM id of the next
919 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000920 */
921int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
922{
923 struct vm *vm;
924 struct vm_locked locked;
925 struct wait_entry *entry;
926 struct vm *waiting_vm;
927
928 /* Only primary VMs are allowed to call this function. */
929 if (current->vm->id != HF_PRIMARY_VM_ID) {
930 return -1;
931 }
932
933 vm = vm_get(vm_id);
934 if (vm == NULL) {
935 return -1;
936 }
937
938 /* Check if there are outstanding notifications from given vm. */
939 vm_lock(vm, &locked);
940 entry = api_fetch_waiter(locked);
941 vm_unlock(&locked);
942
943 if (entry == NULL) {
944 return -1;
945 }
946
947 /* Enqueue notification to waiting VM. */
948 waiting_vm = entry->waiting_vm;
949
950 sl_lock(&waiting_vm->lock);
951 if (list_empty(&entry->ready_links)) {
952 list_append(&waiting_vm->mailbox.ready_list,
953 &entry->ready_links);
954 }
955 sl_unlock(&waiting_vm->lock);
956
957 return waiting_vm->id;
958}
959
960/**
961 * Clears the caller's mailbox so that a new message can be received. The caller
962 * must have copied out all data they wish to preserve as new messages will
963 * overwrite the old and will arrive asynchronously.
964 *
965 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000966 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000967 * - 0 on success if no further action is needed.
968 * - 1 if it was called by the primary VM and the primary VM now needs to wake
969 * up or kick waiters. Waiters should be retrieved by calling
970 * hf_mailbox_waiter_get.
971 */
972int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
973{
974 struct vm *vm = current->vm;
975 struct vm_locked locked;
976 int64_t ret;
977
978 vm_lock(vm, &locked);
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000979 switch (vm->mailbox.state) {
980 case mailbox_state_empty:
981 ret = 0;
982 break;
983
984 case mailbox_state_received:
985 ret = -1;
986 break;
987
988 case mailbox_state_read:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000989 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100990 vm->mailbox.state = mailbox_state_empty;
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000991 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100992 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000993 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100994
995 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100996}
Andrew Walbran318f5732018-11-20 16:23:42 +0000997
998/**
999 * Enables or disables a given interrupt ID for the calling vCPU.
1000 *
1001 * Returns 0 on success, or -1 if the intid is invalid.
1002 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001003int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001004{
1005 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1006 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001007
Andrew Walbran318f5732018-11-20 16:23:42 +00001008 if (intid >= HF_NUM_INTIDS) {
1009 return -1;
1010 }
1011
1012 sl_lock(&current->lock);
1013 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001014 /*
1015 * If it is pending and was not enabled before, increment the
1016 * count.
1017 */
1018 if (current->interrupts.interrupt_pending[intid_index] &
1019 ~current->interrupts.interrupt_enabled[intid_index] &
1020 intid_mask) {
1021 current->interrupts.enabled_and_pending_count++;
1022 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001023 current->interrupts.interrupt_enabled[intid_index] |=
1024 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001025 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001026 /*
1027 * If it is pending and was enabled before, decrement the count.
1028 */
1029 if (current->interrupts.interrupt_pending[intid_index] &
1030 current->interrupts.interrupt_enabled[intid_index] &
1031 intid_mask) {
1032 current->interrupts.enabled_and_pending_count--;
1033 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001034 current->interrupts.interrupt_enabled[intid_index] &=
1035 ~intid_mask;
1036 }
1037
1038 sl_unlock(&current->lock);
1039 return 0;
1040}
1041
1042/**
1043 * Returns the ID of the next pending interrupt for the calling vCPU, and
1044 * acknowledges it (i.e. marks it as no longer pending). Returns
1045 * HF_INVALID_INTID if there are no pending interrupts.
1046 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001047uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001048{
1049 uint8_t i;
1050 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001051
1052 /*
1053 * Find the first enabled and pending interrupt ID, return it, and
1054 * deactivate it.
1055 */
1056 sl_lock(&current->lock);
1057 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1058 uint32_t enabled_and_pending =
1059 current->interrupts.interrupt_enabled[i] &
1060 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001061
Andrew Walbran318f5732018-11-20 16:23:42 +00001062 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001063 uint8_t bit_index = ctz(enabled_and_pending);
1064 /*
1065 * Mark it as no longer pending and decrement the count.
1066 */
1067 current->interrupts.interrupt_pending[i] &=
1068 ~(1u << bit_index);
1069 current->interrupts.enabled_and_pending_count--;
1070 first_interrupt =
1071 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001072 break;
1073 }
1074 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001075
1076 sl_unlock(&current->lock);
1077 return first_interrupt;
1078}
1079
1080/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001081 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001082 * given VM and vCPU.
1083 */
1084static inline bool is_injection_allowed(uint32_t target_vm_id,
1085 struct vcpu *current)
1086{
1087 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001088
Andrew Walbran318f5732018-11-20 16:23:42 +00001089 /*
1090 * The primary VM is allowed to inject interrupts into any VM. Secondary
1091 * VMs are only allowed to inject interrupts into their own vCPUs.
1092 */
1093 return current_vm_id == HF_PRIMARY_VM_ID ||
1094 current_vm_id == target_vm_id;
1095}
1096
1097/**
1098 * Injects a virtual interrupt of the given ID into the given target vCPU.
1099 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1100 * when the vCPU is next run, which is up to the scheduler.
1101 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001102 * Returns:
1103 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1104 * ID is invalid, or the current VM is not allowed to inject interrupts to
1105 * the target VM.
1106 * - 0 on success if no further action is needed.
1107 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1108 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001109 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001110int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +00001111 uint32_t intid, struct vcpu *current,
1112 struct vcpu **next)
1113{
Andrew Walbran318f5732018-11-20 16:23:42 +00001114 struct vcpu *target_vcpu;
1115 struct vm *target_vm = vm_get(target_vm_id);
1116
1117 if (intid >= HF_NUM_INTIDS) {
1118 return -1;
1119 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001120
Andrew Walbran318f5732018-11-20 16:23:42 +00001121 if (target_vm == NULL) {
1122 return -1;
1123 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001124
Andrew Walbran318f5732018-11-20 16:23:42 +00001125 if (target_vcpu_idx >= target_vm->vcpu_count) {
1126 /* The requested vcpu must exist. */
1127 return -1;
1128 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001129
Andrew Walbran318f5732018-11-20 16:23:42 +00001130 if (!is_injection_allowed(target_vm_id, current)) {
1131 return -1;
1132 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001133
Andrew Walbran318f5732018-11-20 16:23:42 +00001134 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
1135
1136 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1137 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001138 return internal_interrupt_inject(target_vm, target_vcpu, intid, current,
1139 next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001140}
Andrew Scull6386f252018-12-06 13:29:10 +00001141
1142/**
1143 * Clears a region of physical memory by overwriting it with zeros. The data is
1144 * flushed from the cache so the memory has been cleared across the system.
1145 */
1146static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1147{
1148 /*
1149 * TODO: change this to a cpu local single page window rather than a
1150 * global mapping of the whole range. Such an approach will limit
1151 * the changes to stage-1 tables and will allow only local
1152 * invalidation.
1153 */
1154 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
1155 size_t size = pa_addr(end) - pa_addr(begin);
1156
1157 if (!ptr) {
1158 /* TODO: partial defrag of failed range. */
1159 /* Recover any memory consumed in failed mapping. */
1160 mm_defrag(ppool);
1161 return false;
1162 }
1163
1164 memset(ptr, 0, size);
1165 arch_mm_write_back_dcache(ptr, size);
1166 mm_unmap(begin, end, ppool);
1167
1168 return true;
1169}
1170
1171/**
1172 * Shares memory from the calling VM with another. The memory can be shared in
1173 * different modes.
1174 *
1175 * TODO: the interface for sharing memory will need to be enhanced to allow
1176 * sharing with different modes e.g. read-only, informing the recipient
1177 * of the memory they have been given, opting to not wipe the memory and
1178 * possibly allowing multiple blocks to be transferred. What this will
1179 * look like is TBD.
1180 */
1181int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1182 enum hf_share share, struct vcpu *current)
1183{
1184 struct vm *from = current->vm;
1185 struct vm *to;
1186 int orig_from_mode;
1187 int from_mode;
1188 int to_mode;
1189 ipaddr_t begin;
1190 ipaddr_t end;
1191 paddr_t pa_begin;
1192 paddr_t pa_end;
1193 struct mpool local_page_pool;
1194 int64_t ret;
1195
1196 /* Disallow reflexive shares as this suggests an error in the VM. */
1197 if (vm_id == from->id) {
1198 return -1;
1199 }
1200
1201 /* Ensure the target VM exists. */
1202 to = vm_get(vm_id);
1203 if (to == NULL) {
1204 return -1;
1205 }
1206
1207 begin = addr;
1208 end = ipa_add(addr, size);
1209
1210 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001211 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1212 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001213 return -1;
1214 }
1215
1216 /* Convert the sharing request to memory management modes. */
1217 switch (share) {
1218 case HF_MEMORY_GIVE:
1219 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1220 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1221 break;
1222
1223 case HF_MEMORY_LEND:
1224 from_mode = MM_MODE_INVALID;
1225 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1226 break;
1227
1228 case HF_MEMORY_SHARE:
1229 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1230 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1231 MM_MODE_SHARED;
1232 break;
1233
1234 default:
1235 /* The input is untrusted so might not be a valid value. */
1236 return -1;
1237 }
1238
1239 /*
1240 * Create a local pool so any freed memory can't be used by another
1241 * thread. This is to ensure the original mapping can be restored if any
1242 * stage of the process fails.
1243 */
1244 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1245
1246 sl_lock_both(&from->lock, &to->lock);
1247
1248 /*
1249 * Ensure that the memory range is mapped with the same mode so that
1250 * changes can be reverted if the process fails.
1251 */
1252 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1253 goto fail;
1254 }
1255
1256 /*
1257 * Ensure the memory range is valid for the sender. If it isn't, the
1258 * sender has either shared it with another VM already or has no claim
1259 * to the memory.
1260 */
1261 if (orig_from_mode & MM_MODE_INVALID) {
1262 goto fail;
1263 }
1264
1265 /*
1266 * The sender must own the memory and have exclusive access to it in
1267 * order to share it. Alternatively, it is giving memory back to the
1268 * owning VM.
1269 */
1270 if (orig_from_mode & MM_MODE_UNOWNED) {
1271 int orig_to_mode;
1272
1273 if (share != HF_MEMORY_GIVE ||
1274 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1275 orig_to_mode & MM_MODE_UNOWNED) {
1276 goto fail;
1277 }
1278 } else if (orig_from_mode & MM_MODE_SHARED) {
1279 goto fail;
1280 }
1281
1282 pa_begin = pa_from_ipa(begin);
1283 pa_end = pa_from_ipa(end);
1284
1285 /*
1286 * First update the mapping for the sender so there is not overlap with
1287 * the recipient.
1288 */
1289 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1290 NULL, &local_page_pool)) {
1291 goto fail;
1292 }
1293
1294 /* Clear the memory so no VM or device can see the previous contents. */
1295 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1296 goto fail_return_to_sender;
1297 }
1298
1299 /* Complete the transfer by mapping the memory into the recipient. */
1300 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1301 &local_page_pool)) {
1302 /* TODO: partial defrag of failed range. */
1303 /* Recover any memory consumed in failed mapping. */
1304 mm_vm_defrag(&from->ptable, &local_page_pool);
1305 goto fail_return_to_sender;
1306 }
1307
1308 ret = 0;
1309 goto out;
1310
1311fail_return_to_sender:
1312 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1313 NULL, &local_page_pool);
1314
1315fail:
1316 ret = -1;
1317
1318out:
1319 sl_unlock(&from->lock);
1320 sl_unlock(&to->lock);
1321
1322 mpool_fini(&local_page_pool);
1323
1324 return ret;
1325}