blob: 16620a1a011d1496d2602455bf273a1a2d69573d [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"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000031#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010032
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000033/*
34 * To eliminate the risk of deadlocks, we define a partial order for the
35 * acquisition of locks held concurrently by the same physical CPU. Our current
36 * ordering requirements are as follows:
37 *
38 * vm::lock -> vcpu::lock
Andrew Scull6386f252018-12-06 13:29:10 +000039 *
40 * Locks of the same kind require the lock of lowest address to be locked first,
41 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000042 */
43
Andrew Scullaa039b32018-10-04 15:02:26 +010044static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010045 "Currently, a page is mapped for the send and receive buffers so "
46 "the maximum request is the size of a page.");
47
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000048static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000049
50/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000051 * Initialises the API page pool by taking ownership of the contents of the
52 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000053 */
54void api_init(struct mpool *ppool)
55{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000056 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000057}
58
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010059/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010060 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010061 *
62 * This triggers the scheduling logic to run. Run in the context of secondary VM
63 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
64 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010065 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010066static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000067 struct hf_vcpu_run_return primary_ret,
68 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069{
Andrew Scull19503262018-09-20 14:48:39 +010070 struct vm *primary = vm_get(HF_PRIMARY_VM_ID);
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071 struct vcpu *next = &primary->vcpus[cpu_index(current->cpu)];
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010072
Andrew Walbran508e63c2018-12-20 17:02:37 +000073 /*
74 * If the secondary is blocked but has a timer running, sleep until the
75 * timer fires rather than indefinitely.
76 */
Andrew Scullb06d1752019-02-04 10:15:48 +000077 switch (primary_ret.code) {
78 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
79 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
80 primary_ret.sleep.ns =
81 arch_timer_enabled_current()
82 ? arch_timer_remaining_ns_current()
83 : HF_SLEEP_INDEFINITE;
84 break;
85
86 default:
87 /* Do nothing. */
88 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +000089 }
90
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010091 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010092 arch_regs_set_retval(&next->regs,
93 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010094
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000095 /* Mark the current vcpu as waiting. */
96 sl_lock(&current->lock);
97 current->state = secondary_state;
98 sl_unlock(&current->lock);
99
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100100 return next;
101}
102
103/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000104 * Returns to the primary vm and signals that the vcpu still has work to do so.
105 */
106struct vcpu *api_preempt(struct vcpu *current)
107{
108 struct hf_vcpu_run_return ret = {
109 .code = HF_VCPU_RUN_PREEMPTED,
110 };
111
112 return api_switch_to_primary(current, ret, vcpu_state_ready);
113}
114
115/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100116 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
117 * vm.
118 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100119struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100120{
Andrew Scull6d2db332018-10-10 15:28:17 +0100121 struct hf_vcpu_run_return ret = {
122 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
123 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000124
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000125 return api_switch_to_primary(current, ret,
126 vcpu_state_blocked_interrupt);
Andrew Scullaa039b32018-10-04 15:02:26 +0100127}
128
129/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000130 * Returns to the primary vm to allow this cpu to be used for other tasks as the
131 * vcpu does not have work to do at this moment. The current vcpu is marked as
132 * ready to be scheduled again.
133 */
134struct vcpu *api_yield(struct vcpu *current)
135{
136 struct hf_vcpu_run_return ret = {
137 .code = HF_VCPU_RUN_YIELD,
138 };
139
140 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000141 /* Noop on the primary as it makes the scheduling decisions. */
Andrew Scull66d62bf2019-02-01 13:54:10 +0000142 return NULL;
143 }
144
145 return api_switch_to_primary(current, ret, vcpu_state_ready);
146}
147
148/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000149 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000150 */
151struct vcpu *api_abort(struct vcpu *current)
152{
153 struct hf_vcpu_run_return ret = {
154 .code = HF_VCPU_RUN_ABORTED,
155 };
156
157 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
158
159 if (current->vm->id == HF_PRIMARY_VM_ID) {
160 /* TODO: what to do when the primary aborts? */
161 for (;;) {
162 /* Do nothing. */
163 }
164 }
165
166 atomic_store_explicit(&current->vm->aborting, true,
167 memory_order_relaxed);
168
169 /* TODO: free resources once all vCPUs abort. */
170
171 return api_switch_to_primary(current, ret, vcpu_state_aborted);
172}
173
174/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000175 * Returns the ID of the VM.
176 */
177int64_t api_vm_get_id(const struct vcpu *current)
178{
179 return current->vm->id;
180}
181
182/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100183 * Returns the number of VMs configured to run.
184 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100185int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100186{
Andrew Scull19503262018-09-20 14:48:39 +0100187 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100188}
189
190/**
191 * Returns the number of vcpus configured in the given VM.
192 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100193int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100194{
Andrew Scull19503262018-09-20 14:48:39 +0100195 struct vm *vm;
196
197 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100198 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100199 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100200 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100201
Andrew Scull19503262018-09-20 14:48:39 +0100202 vm = vm_get(vm_id);
203 if (vm == NULL) {
204 return -1;
205 }
206
207 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100208}
209
210/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000211 * This function is called by the architecture-specific context switching
212 * function to indicate that register state for the given vcpu has been saved
213 * and can therefore be used by other pcpus.
214 */
215void api_regs_state_saved(struct vcpu *vcpu)
216{
217 sl_lock(&vcpu->lock);
218 vcpu->regs_available = true;
219 sl_unlock(&vcpu->lock);
220}
221
222/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000223 * Retrieves the next waiter and removes it from the wait list if the VM's
224 * mailbox is in a writable state.
225 */
226static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
227{
228 struct wait_entry *entry;
229 struct vm *vm = locked_vm.vm;
230
231 if (vm->mailbox.state != mailbox_state_empty ||
232 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
233 /* The mailbox is not writable or there are no waiters. */
234 return NULL;
235 }
236
237 /* Remove waiter from the wait list. */
238 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
239 wait_links);
240 list_remove(&entry->wait_links);
241 return entry;
242}
243
244/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000245 * Assuming that the arguments have already been checked by the caller, injects
246 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
247 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
248 * is next run, which is up to the scheduler.
249 *
250 * Returns:
251 * - 0 on success if no further action is needed.
252 * - 1 if it was called by the primary VM and the primary VM now needs to wake
253 * up or kick the target vCPU.
254 */
255static int64_t internal_interrupt_inject(struct vm *target_vm,
256 struct vcpu *target_vcpu,
257 uint32_t intid, struct vcpu *current,
258 struct vcpu **next)
259{
260 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
261 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000262 int64_t ret = 0;
263
264 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000265
266 /*
267 * We only need to change state and (maybe) trigger a virtual IRQ if it
268 * is enabled and was not previously pending. Otherwise we can skip
269 * everything except setting the pending bit.
270 *
271 * If you change this logic make sure to update the need_vm_lock logic
272 * above to match.
273 */
274 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
275 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
276 intid_mask)) {
277 goto out;
278 }
279
280 /* Increment the count. */
281 target_vcpu->interrupts.enabled_and_pending_count++;
282
283 /*
284 * Only need to update state if there was not already an
285 * interrupt enabled and pending.
286 */
287 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
288 goto out;
289 }
290
Andrew Walbran508e63c2018-12-20 17:02:37 +0000291 if (current->vm->id == HF_PRIMARY_VM_ID) {
292 /*
293 * If the call came from the primary VM, let it know that it
294 * should run or kick the target vCPU.
295 */
296 ret = 1;
297 } else if (current != target_vcpu && next != NULL) {
298 /*
299 * Switch to the primary so that it can switch to the target, or
300 * kick it if it is already running on a different physical CPU.
301 */
302 struct hf_vcpu_run_return ret = {
303 .code = HF_VCPU_RUN_WAKE_UP,
304 .wake_up.vm_id = target_vm->id,
305 .wake_up.vcpu = target_vcpu - target_vm->vcpus,
306 };
307 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
308 }
309
310out:
311 /* Either way, make it pending. */
312 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
313
314 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000315
316 return ret;
317}
318
319/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000320 * Prepares the vcpu to run by updating its state and fetching whether a return
321 * value needs to be forced onto the vCPU.
322 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000323static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000324 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000325{
Andrew Scullb06d1752019-02-04 10:15:48 +0000326 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000327 bool ret;
328
Andrew Scullb06d1752019-02-04 10:15:48 +0000329 /*
330 * Wait until the registers become available. All locks must be
331 * released between iterations of this loop to avoid potential deadlocks
332 * if, on any path, a lock needs to be taken after taking the decision
333 * to switch context but before the registers have been saved.
334 *
335 * The VM lock is not needed in the common case so it must only be taken
336 * when it is going to be needed. This ensures there are no inter-vCPU
337 * dependencies in the common run case meaning the sensitive context
338 * switch performance is consistent.
339 */
340 for (;;) {
341 sl_lock(&vcpu->lock);
342
343 /* The VM needs to be locked to deliver mailbox messages. */
344 need_vm_lock = vcpu->state == vcpu_state_blocked_mailbox;
345 if (need_vm_lock) {
346 sl_unlock(&vcpu->lock);
347 sl_lock(&vcpu->vm->lock);
348 sl_lock(&vcpu->lock);
349 }
350
351 if (vcpu->regs_available) {
352 break;
353 }
354
355 if (vcpu->state == vcpu_state_running) {
356 /*
357 * vCPU is running on another pCPU.
358 *
359 * It's ok to not return the sleep duration here because
360 * the other physical CPU that is currently running this
361 * vCPU will return sleep duration if neeed. The default
362 * return value is HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no
363 * need to set it explicitly.
364 */
365 ret = false;
366 goto out;
367 }
368
369 sl_unlock(&vcpu->lock);
370 if (need_vm_lock) {
371 sl_unlock(&vcpu->vm->lock);
372 }
373 }
Andrew Scull9726c252019-01-23 13:44:19 +0000374
375 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
376 if (vcpu->state != vcpu_state_aborted) {
Andrew Scull82331282019-01-25 10:29:34 +0000377 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
378 vcpu_index(vcpu));
Andrew Scull9726c252019-01-23 13:44:19 +0000379 vcpu->state = vcpu_state_aborted;
380 }
381 ret = false;
382 goto out;
383 }
384
Andrew Walbran508e63c2018-12-20 17:02:37 +0000385 switch (vcpu->state) {
386 case vcpu_state_running:
387 case vcpu_state_off:
388 case vcpu_state_aborted:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000389 ret = false;
390 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000391
Andrew Walbran508e63c2018-12-20 17:02:37 +0000392 case vcpu_state_blocked_mailbox:
Andrew Scullb06d1752019-02-04 10:15:48 +0000393 /*
394 * A pending message allows the vCPU to run so the message can
395 * be delivered directly.
396 */
397 if (vcpu->vm->mailbox.state == mailbox_state_received) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000398 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Scullb06d1752019-02-04 10:15:48 +0000399 vcpu->vm->mailbox.state = mailbox_state_read;
400 break;
401 }
402 /* Fall through. */
403 case vcpu_state_blocked_interrupt:
404 /* Allow virtual interrupts to be delivered. */
405 if (vcpu->interrupts.enabled_and_pending_count > 0) {
406 break;
407 }
408
409 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000410 if (arch_timer_pending(&vcpu->regs)) {
411 break;
412 }
413
414 /*
415 * The vCPU is not ready to run, return the appropriate code to
416 * the primary which called vcpu_run.
417 */
418 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000419 run_ret->code =
420 vcpu->state == vcpu_state_blocked_mailbox
421 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
422 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000423 run_ret->sleep.ns =
424 arch_timer_remaining_ns(&vcpu->regs);
425 }
426
427 ret = false;
428 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000429
Andrew Walbran508e63c2018-12-20 17:02:37 +0000430 case vcpu_state_ready:
431 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000432 }
433
Andrew Scullb06d1752019-02-04 10:15:48 +0000434 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000435 vcpu->cpu = current->cpu;
436 vcpu->state = vcpu_state_running;
437
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000438 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000439 * Mark the registers as unavailable now that we're about to reflect
440 * them onto the real registers. This will also prevent another physical
441 * CPU from trying to read these registers.
442 */
443 vcpu->regs_available = false;
444
445 ret = true;
446
447out:
448 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000449 if (need_vm_lock) {
450 sl_unlock(&vcpu->vm->lock);
451 }
452
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000453 return ret;
454}
455
456/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100457 * Runs the given vcpu of the given vm.
458 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100459struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000460 const struct vcpu *current,
461 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100462{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100463 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100464 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100465 struct hf_vcpu_run_return ret = {
466 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000467 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100468 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100469
470 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100471 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100472 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100473 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100474
Andrew Scull19503262018-09-20 14:48:39 +0100475 /* Only secondary VM vcpus can be run. */
476 if (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 /* The requested VM must exist. */
481 vm = vm_get(vm_id);
482 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100483 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100484 }
485
486 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100487 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100488 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100489 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100490
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000491 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100492 vcpu = &vm->vcpus[vcpu_idx];
Andrew Scullb06d1752019-02-04 10:15:48 +0000493 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000494 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100495 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000496
Andrew Walbran508e63c2018-12-20 17:02:37 +0000497 /*
498 * Inject timer interrupt if timer has expired. It's safe to access
499 * vcpu->regs here because api_vcpu_prepare_run already made sure that
500 * regs_available was true (and then set it to false) before returning
501 * true.
502 */
503 if (arch_timer_pending(&vcpu->regs)) {
504 /* Make virtual timer interrupt pending. */
505 internal_interrupt_inject(vm, vcpu, HF_VIRTUAL_TIMER_INTID,
506 vcpu, NULL);
507
508 /*
509 * Set the mask bit so the hardware interrupt doesn't fire
510 * again. Ideally we wouldn't do this because it affects what
511 * the secondary vCPU sees, but if we don't then we end up with
512 * a loop of the interrupt firing each time we try to return to
513 * the secondary vCPU.
514 */
515 arch_timer_mask(&vcpu->regs);
516 }
517
Andrew Scull33fecd32019-01-08 14:48:27 +0000518 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000519 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000520
Andrew Scull33fecd32019-01-08 14:48:27 +0000521 /*
522 * Set a placeholder return code to the scheduler. This will be
523 * overwritten when the switch back to the primary occurs.
524 */
525 ret.code = HF_VCPU_RUN_PREEMPTED;
526
Andrew Scull6d2db332018-10-10 15:28:17 +0100527out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100528 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100529}
530
531/**
Andrew Scull81e85092018-12-12 12:56:20 +0000532 * Check that the mode indicates memory that is valid, owned and exclusive.
533 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000534static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000535{
536 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
537 0;
538}
539
540/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000541 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
542 * after they've succeeded. If a secondary VM is running and there are waiters,
543 * it also switches back to the primary VM for it to wake waiters up.
544 */
545static int64_t api_waiter_result(struct vm_locked locked_vm,
546 struct vcpu *current, struct vcpu **next)
547{
548 struct vm *vm = locked_vm.vm;
549 struct hf_vcpu_run_return ret = {
550 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
551 };
552
553 if (list_empty(&vm->mailbox.waiter_list)) {
554 /* No waiters, nothing else to do. */
555 return 0;
556 }
557
558 if (vm->id == HF_PRIMARY_VM_ID) {
559 /* The caller is the primary VM. Tell it to wake up waiters. */
560 return 1;
561 }
562
563 /*
564 * Switch back to the primary VM, informing it that there are waiters
565 * that need to be notified.
566 */
567 *next = api_switch_to_primary(current, ret, vcpu_state_ready);
568
569 return 0;
570}
571
572/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100573 * Configures the VM to send/receive data through the specified pages. The pages
574 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000575 *
576 * Returns:
577 * - -1 on failure.
578 * - 0 on success if no further action is needed.
579 * - 1 if it was called by the primary VM and the primary VM now needs to wake
580 * up or kick waiters. Waiters should be retrieved by calling
581 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100582 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000583int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
584 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100585{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100586 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000587 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100588 paddr_t pa_send_begin;
589 paddr_t pa_send_end;
590 paddr_t pa_recv_begin;
591 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000592 int orig_send_mode;
593 int orig_recv_mode;
594 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100595 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100596
597 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000598 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
599 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100600 return -1;
601 }
602
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000603 /* Convert to physical addresses. */
604 pa_send_begin = pa_from_ipa(send);
605 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
606
607 pa_recv_begin = pa_from_ipa(recv);
608 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
609
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100610 /* Fail if the same page is used for the send and receive pages. */
611 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000612 return -1;
613 }
614
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000615 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000616
617 /* We only allow these to be setup once. */
618 if (vm->mailbox.send || vm->mailbox.recv) {
619 goto fail;
620 }
621
622 /*
623 * Ensure the pages are valid, owned and exclusive to the VM and that
624 * the VM has the required access to the memory.
625 */
626 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
627 &orig_send_mode) ||
628 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
629 (orig_send_mode & MM_MODE_R) == 0 ||
630 (orig_send_mode & MM_MODE_W) == 0) {
631 goto fail;
632 }
633
634 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
635 &orig_recv_mode) ||
636 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
637 (orig_recv_mode & MM_MODE_R) == 0) {
638 goto fail;
639 }
640
641 /*
642 * Create a local pool so any freed memory can't be used by another
643 * thread. This is to ensure the original mapping can be restored if any
644 * stage of the process fails.
645 */
646 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
647
648 /* Take memory ownership away from the VM and mark as shared. */
649 if (!mm_vm_identity_map(
650 &vm->ptable, pa_send_begin, pa_send_end,
651 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
652 NULL, &local_page_pool)) {
653 goto fail_free_pool;
654 }
655
656 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
657 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
658 NULL, &local_page_pool)) {
659 /* TODO: partial defrag of failed range. */
660 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000661 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000662 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100663 }
664
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100665 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000666 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000667 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100668 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000669 /* TODO: partial defrag of failed range. */
670 /* Recover any memory consumed in failed mapping. */
671 mm_defrag(&local_page_pool);
672 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100673 }
674
675 /*
676 * Map the receive page as writable in the hypervisor address space. On
677 * failure, unmap the send page before returning.
678 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000679 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000680 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100681 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000682 /* TODO: partial defrag of failed range. */
683 /* Recover any memory consumed in failed mapping. */
684 mm_defrag(&local_page_pool);
685 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100686 }
687
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000688 /* Tell caller about waiters, if any. */
689 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000690 goto exit;
691
692 /*
693 * The following mappings will not require more memory than is available
694 * in the local pool.
695 */
696fail_undo_all:
697 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000698 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000699
700fail_undo_send_and_recv:
701 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
702 orig_recv_mode, NULL, &local_page_pool);
703
704fail_undo_send:
705 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
706 orig_send_mode, NULL, &local_page_pool);
707
708fail_free_pool:
709 mpool_fini(&local_page_pool);
710
711fail:
712 ret = -1;
713
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100714exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000715 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100716
717 return ret;
718}
719
720/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100721 * Copies data from the sender's send buffer to the recipient's receive buffer
722 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000723 *
724 * If the recipient's receive buffer is busy, it can optionally register the
725 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100726 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000727int32_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
728 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100729{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100730 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100731 struct vm *to;
Andrew Scullb06d1752019-02-04 10:15:48 +0000732 struct hf_vcpu_run_return primary_ret = {
733 .code = HF_VCPU_RUN_MESSAGE,
734 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000735 struct spci_message from_msg_replica;
736 struct spci_message *to_msg;
737 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100738
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000739 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100740
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000741 int64_t ret;
742 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
743 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100744
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000745 /*
746 * Check that the sender has configured its send buffer. Copy the
747 * message header. If the tx mailbox at from_msg is configured (i.e.
748 * from_msg != NULL) then it can be safely accessed after releasing the
749 * lock since the tx mailbox address can only be configured once.
750 */
751 sl_lock(&from->lock);
752 from_msg = from->mailbox.send;
753 sl_unlock(&from->lock);
754
755 if (from_msg == NULL) {
756 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100757 }
758
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100759 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000760 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100761 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000762 from_msg_replica = *from_msg;
763
764 /* Ensure source VM id corresponds to the current VM. */
765 if (from_msg_replica.source_vm_id != from->id) {
766 return SPCI_INVALID_PARAMETERS;
767 }
768
769 size = from_msg_replica.length;
770 /* Limit the size of transfer. */
771 if (size > HF_MAILBOX_SIZE - sizeof(struct spci_message)) {
772 return SPCI_INVALID_PARAMETERS;
773 }
774
775 /* Disallow reflexive requests as this suggests an error in the VM. */
776 if (from_msg_replica.target_vm_id == from->id) {
777 return SPCI_INVALID_PARAMETERS;
778 }
779
780 /* Ensure the target VM exists. */
781 to = vm_get(from_msg_replica.target_vm_id);
782 if (to == NULL) {
783 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100784 }
785
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100786 sl_lock(&to->lock);
787
Andrew Scullaa039b32018-10-04 15:02:26 +0100788 if (to->mailbox.state != mailbox_state_empty ||
789 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000790 /*
791 * Fail if the target isn't currently ready to receive data,
792 * setting up for notification if requested.
793 */
794 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000795 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000796 &current->vm->wait_entries
797 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000798
799 /* Append waiter only if it's not there yet. */
800 if (list_empty(&entry->wait_links)) {
801 list_append(&to->mailbox.waiter_list,
802 &entry->wait_links);
803 }
804 }
805
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000806 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100807 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100808 }
809
Andrew Scullaa039b32018-10-04 15:02:26 +0100810 /* Copy data. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000811 to_msg = to->mailbox.recv;
812 *to_msg = from_msg_replica;
813 memcpy(to_msg->payload, from->mailbox.send->payload, size);
Andrew Scullb06d1752019-02-04 10:15:48 +0000814 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000815 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100816
817 /* Messages for the primary VM are delivered directly. */
818 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000819 primary_ret.message.size = size,
820 to->mailbox.state = mailbox_state_read;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000821 *next = api_switch_to_primary(current, primary_ret,
822 vcpu_state_ready);
Andrew Scullaa039b32018-10-04 15:02:26 +0100823 goto out;
824 }
825
Andrew Scullb06d1752019-02-04 10:15:48 +0000826 to->mailbox.state = mailbox_state_received;
Andrew Scullaa039b32018-10-04 15:02:26 +0100827
828 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000829 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000830 *next = api_switch_to_primary(current, primary_ret,
831 vcpu_state_ready);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000832 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100833
834out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100835 sl_unlock(&to->lock);
836
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000837 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100838}
839
840/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100841 * Receives a message from the mailbox. If one isn't available, this function
842 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100843 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100844 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100845 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000846int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current,
847 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100848{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100849 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000850 int32_t return_code;
851 bool block =
852 (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100853
Andrew Scullaa039b32018-10-04 15:02:26 +0100854 /*
855 * The primary VM will receive messages as a status code from running
856 * vcpus and must not call this function.
857 */
Andrew Scull19503262018-09-20 14:48:39 +0100858 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000859 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100860 }
861
862 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100863
Andrew Scullaa039b32018-10-04 15:02:26 +0100864 /* Return pending messages without blocking. */
865 if (vm->mailbox.state == mailbox_state_received) {
866 vm->mailbox.state = mailbox_state_read;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000867 return_code = SPCI_SUCCESS;
868 goto out;
869 }
870
871 /* No pending message so fail if not allowed to block. */
872 if (!block) {
873 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100874 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100875 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100876
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000877 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +0000878 * From this point onward this call can only be interrupted or a message
879 * received. If a message is received the return value will be set at
880 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000881 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000882 return_code = SPCI_INTERRUPTED;
883
884 /*
885 * Don't block if there are enabled and pending interrupts, to match
886 * behaviour of wait_for_interrupt.
887 */
888 if (current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100889 goto out;
890 }
891
Andrew Scullaa039b32018-10-04 15:02:26 +0100892 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000893 {
894 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +0000895 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +0000896 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000897
Andrew Walbranb4816552018-12-05 17:35:42 +0000898 *next = api_switch_to_primary(current, run_return,
899 vcpu_state_blocked_mailbox);
900 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100901out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100902 sl_unlock(&vm->lock);
903
Jose Marinho3e2442f2019-03-12 13:30:37 +0000904 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100905}
906
907/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000908 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
909 * by this function, the caller must have called api_mailbox_send before with
910 * the notify argument set to true, and this call must have failed because the
911 * mailbox was not available.
912 *
913 * It should be called repeatedly to retrieve a list of VMs.
914 *
915 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
916 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100917 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000918int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100919{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100920 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000921 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100922 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100923
924 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000925 if (list_empty(&vm->mailbox.ready_list)) {
926 ret = -1;
927 goto exit;
928 }
929
930 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
931 ready_links);
932 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000933 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000934
935exit:
936 sl_unlock(&vm->lock);
937 return ret;
938}
939
940/**
941 * Retrieves the next VM waiting to be notified that the mailbox of the
942 * specified VM became writable. Only primary VMs are allowed to call this.
943 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000944 * Returns -1 on failure or if there are no waiters; the VM id of the next
945 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000946 */
947int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
948{
949 struct vm *vm;
950 struct vm_locked locked;
951 struct wait_entry *entry;
952 struct vm *waiting_vm;
953
954 /* Only primary VMs are allowed to call this function. */
955 if (current->vm->id != HF_PRIMARY_VM_ID) {
956 return -1;
957 }
958
959 vm = vm_get(vm_id);
960 if (vm == NULL) {
961 return -1;
962 }
963
964 /* Check if there are outstanding notifications from given vm. */
965 vm_lock(vm, &locked);
966 entry = api_fetch_waiter(locked);
967 vm_unlock(&locked);
968
969 if (entry == NULL) {
970 return -1;
971 }
972
973 /* Enqueue notification to waiting VM. */
974 waiting_vm = entry->waiting_vm;
975
976 sl_lock(&waiting_vm->lock);
977 if (list_empty(&entry->ready_links)) {
978 list_append(&waiting_vm->mailbox.ready_list,
979 &entry->ready_links);
980 }
981 sl_unlock(&waiting_vm->lock);
982
983 return waiting_vm->id;
984}
985
986/**
987 * Clears the caller's mailbox so that a new message can be received. The caller
988 * must have copied out all data they wish to preserve as new messages will
989 * overwrite the old and will arrive asynchronously.
990 *
991 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000992 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000993 * - 0 on success if no further action is needed.
994 * - 1 if it was called by the primary VM and the primary VM now needs to wake
995 * up or kick waiters. Waiters should be retrieved by calling
996 * hf_mailbox_waiter_get.
997 */
998int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
999{
1000 struct vm *vm = current->vm;
1001 struct vm_locked locked;
1002 int64_t ret;
1003
1004 vm_lock(vm, &locked);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001005 switch (vm->mailbox.state) {
1006 case mailbox_state_empty:
1007 ret = 0;
1008 break;
1009
1010 case mailbox_state_received:
1011 ret = -1;
1012 break;
1013
1014 case mailbox_state_read:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001015 ret = api_waiter_result(locked, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001016 vm->mailbox.state = mailbox_state_empty;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001017 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001018 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001019 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001020
1021 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001022}
Andrew Walbran318f5732018-11-20 16:23:42 +00001023
1024/**
1025 * Enables or disables a given interrupt ID for the calling vCPU.
1026 *
1027 * Returns 0 on success, or -1 if the intid is invalid.
1028 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001029int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001030{
1031 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1032 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001033
Andrew Walbran318f5732018-11-20 16:23:42 +00001034 if (intid >= HF_NUM_INTIDS) {
1035 return -1;
1036 }
1037
1038 sl_lock(&current->lock);
1039 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001040 /*
1041 * If it is pending and was not enabled before, increment the
1042 * count.
1043 */
1044 if (current->interrupts.interrupt_pending[intid_index] &
1045 ~current->interrupts.interrupt_enabled[intid_index] &
1046 intid_mask) {
1047 current->interrupts.enabled_and_pending_count++;
1048 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001049 current->interrupts.interrupt_enabled[intid_index] |=
1050 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001051 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001052 /*
1053 * If it is pending and was enabled before, decrement the count.
1054 */
1055 if (current->interrupts.interrupt_pending[intid_index] &
1056 current->interrupts.interrupt_enabled[intid_index] &
1057 intid_mask) {
1058 current->interrupts.enabled_and_pending_count--;
1059 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001060 current->interrupts.interrupt_enabled[intid_index] &=
1061 ~intid_mask;
1062 }
1063
1064 sl_unlock(&current->lock);
1065 return 0;
1066}
1067
1068/**
1069 * Returns the ID of the next pending interrupt for the calling vCPU, and
1070 * acknowledges it (i.e. marks it as no longer pending). Returns
1071 * HF_INVALID_INTID if there are no pending interrupts.
1072 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001073uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001074{
1075 uint8_t i;
1076 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001077
1078 /*
1079 * Find the first enabled and pending interrupt ID, return it, and
1080 * deactivate it.
1081 */
1082 sl_lock(&current->lock);
1083 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1084 uint32_t enabled_and_pending =
1085 current->interrupts.interrupt_enabled[i] &
1086 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001087
Andrew Walbran318f5732018-11-20 16:23:42 +00001088 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001089 uint8_t bit_index = ctz(enabled_and_pending);
1090 /*
1091 * Mark it as no longer pending and decrement the count.
1092 */
1093 current->interrupts.interrupt_pending[i] &=
1094 ~(1u << bit_index);
1095 current->interrupts.enabled_and_pending_count--;
1096 first_interrupt =
1097 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001098 break;
1099 }
1100 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001101
1102 sl_unlock(&current->lock);
1103 return first_interrupt;
1104}
1105
1106/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001107 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001108 * given VM and vCPU.
1109 */
1110static inline bool is_injection_allowed(uint32_t target_vm_id,
1111 struct vcpu *current)
1112{
1113 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001114
Andrew Walbran318f5732018-11-20 16:23:42 +00001115 /*
1116 * The primary VM is allowed to inject interrupts into any VM. Secondary
1117 * VMs are only allowed to inject interrupts into their own vCPUs.
1118 */
1119 return current_vm_id == HF_PRIMARY_VM_ID ||
1120 current_vm_id == target_vm_id;
1121}
1122
1123/**
1124 * Injects a virtual interrupt of the given ID into the given target vCPU.
1125 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1126 * when the vCPU is next run, which is up to the scheduler.
1127 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001128 * Returns:
1129 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1130 * ID is invalid, or the current VM is not allowed to inject interrupts to
1131 * the target VM.
1132 * - 0 on success if no further action is needed.
1133 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1134 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001135 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001136int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +00001137 uint32_t intid, struct vcpu *current,
1138 struct vcpu **next)
1139{
Andrew Walbran318f5732018-11-20 16:23:42 +00001140 struct vcpu *target_vcpu;
1141 struct vm *target_vm = vm_get(target_vm_id);
1142
1143 if (intid >= HF_NUM_INTIDS) {
1144 return -1;
1145 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001146
Andrew Walbran318f5732018-11-20 16:23:42 +00001147 if (target_vm == NULL) {
1148 return -1;
1149 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001150
Andrew Walbran318f5732018-11-20 16:23:42 +00001151 if (target_vcpu_idx >= target_vm->vcpu_count) {
1152 /* The requested vcpu must exist. */
1153 return -1;
1154 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001155
Andrew Walbran318f5732018-11-20 16:23:42 +00001156 if (!is_injection_allowed(target_vm_id, current)) {
1157 return -1;
1158 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001159
Andrew Walbran318f5732018-11-20 16:23:42 +00001160 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
1161
1162 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1163 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001164 return internal_interrupt_inject(target_vm, target_vcpu, intid, current,
1165 next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001166}
Andrew Scull6386f252018-12-06 13:29:10 +00001167
1168/**
1169 * Clears a region of physical memory by overwriting it with zeros. The data is
1170 * flushed from the cache so the memory has been cleared across the system.
1171 */
1172static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1173{
1174 /*
1175 * TODO: change this to a cpu local single page window rather than a
1176 * global mapping of the whole range. Such an approach will limit
1177 * the changes to stage-1 tables and will allow only local
1178 * invalidation.
1179 */
1180 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
1181 size_t size = pa_addr(end) - pa_addr(begin);
1182
1183 if (!ptr) {
1184 /* TODO: partial defrag of failed range. */
1185 /* Recover any memory consumed in failed mapping. */
1186 mm_defrag(ppool);
1187 return false;
1188 }
1189
1190 memset(ptr, 0, size);
1191 arch_mm_write_back_dcache(ptr, size);
1192 mm_unmap(begin, end, ppool);
1193
1194 return true;
1195}
1196
1197/**
1198 * Shares memory from the calling VM with another. The memory can be shared in
1199 * different modes.
1200 *
1201 * TODO: the interface for sharing memory will need to be enhanced to allow
1202 * sharing with different modes e.g. read-only, informing the recipient
1203 * of the memory they have been given, opting to not wipe the memory and
1204 * possibly allowing multiple blocks to be transferred. What this will
1205 * look like is TBD.
1206 */
1207int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1208 enum hf_share share, struct vcpu *current)
1209{
1210 struct vm *from = current->vm;
1211 struct vm *to;
1212 int orig_from_mode;
1213 int from_mode;
1214 int to_mode;
1215 ipaddr_t begin;
1216 ipaddr_t end;
1217 paddr_t pa_begin;
1218 paddr_t pa_end;
1219 struct mpool local_page_pool;
1220 int64_t ret;
1221
1222 /* Disallow reflexive shares as this suggests an error in the VM. */
1223 if (vm_id == from->id) {
1224 return -1;
1225 }
1226
1227 /* Ensure the target VM exists. */
1228 to = vm_get(vm_id);
1229 if (to == NULL) {
1230 return -1;
1231 }
1232
1233 begin = addr;
1234 end = ipa_add(addr, size);
1235
1236 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001237 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1238 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001239 return -1;
1240 }
1241
1242 /* Convert the sharing request to memory management modes. */
1243 switch (share) {
1244 case HF_MEMORY_GIVE:
1245 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1246 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1247 break;
1248
1249 case HF_MEMORY_LEND:
1250 from_mode = MM_MODE_INVALID;
1251 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1252 break;
1253
1254 case HF_MEMORY_SHARE:
1255 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1256 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1257 MM_MODE_SHARED;
1258 break;
1259
1260 default:
1261 /* The input is untrusted so might not be a valid value. */
1262 return -1;
1263 }
1264
1265 /*
1266 * Create a local pool so any freed memory can't be used by another
1267 * thread. This is to ensure the original mapping can be restored if any
1268 * stage of the process fails.
1269 */
1270 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1271
1272 sl_lock_both(&from->lock, &to->lock);
1273
1274 /*
1275 * Ensure that the memory range is mapped with the same mode so that
1276 * changes can be reverted if the process fails.
1277 */
1278 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1279 goto fail;
1280 }
1281
1282 /*
1283 * Ensure the memory range is valid for the sender. If it isn't, the
1284 * sender has either shared it with another VM already or has no claim
1285 * to the memory.
1286 */
1287 if (orig_from_mode & MM_MODE_INVALID) {
1288 goto fail;
1289 }
1290
1291 /*
1292 * The sender must own the memory and have exclusive access to it in
1293 * order to share it. Alternatively, it is giving memory back to the
1294 * owning VM.
1295 */
1296 if (orig_from_mode & MM_MODE_UNOWNED) {
1297 int orig_to_mode;
1298
1299 if (share != HF_MEMORY_GIVE ||
1300 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1301 orig_to_mode & MM_MODE_UNOWNED) {
1302 goto fail;
1303 }
1304 } else if (orig_from_mode & MM_MODE_SHARED) {
1305 goto fail;
1306 }
1307
1308 pa_begin = pa_from_ipa(begin);
1309 pa_end = pa_from_ipa(end);
1310
1311 /*
1312 * First update the mapping for the sender so there is not overlap with
1313 * the recipient.
1314 */
1315 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1316 NULL, &local_page_pool)) {
1317 goto fail;
1318 }
1319
1320 /* Clear the memory so no VM or device can see the previous contents. */
1321 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1322 goto fail_return_to_sender;
1323 }
1324
1325 /* Complete the transfer by mapping the memory into the recipient. */
1326 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1327 &local_page_pool)) {
1328 /* TODO: partial defrag of failed range. */
1329 /* Recover any memory consumed in failed mapping. */
1330 mm_vm_defrag(&from->ptable, &local_page_pool);
1331 goto fail_return_to_sender;
1332 }
1333
1334 ret = 0;
1335 goto out;
1336
1337fail_return_to_sender:
1338 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1339 NULL, &local_page_pool);
1340
1341fail:
1342 ret = -1;
1343
1344out:
1345 sl_unlock(&from->lock);
1346 sl_unlock(&to->lock);
1347
1348 mpool_fini(&local_page_pool);
1349
1350 return ret;
1351}