blob: ecc45b3cb227cb00b870d32ac9bacc9c8595cf66 [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 Walbran318f5732018-11-20 16:23:42 +000019#include "hf/arch/cpu.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000020#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000021
Andrew Scull5c496a32019-04-04 11:57:33 +010022#include "hf/assert.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000023#include "hf/dlog.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/mm.h"
25#include "hf/spinlock.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000030#include "vmapi/hf/spci.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
Andrew Sculld6ee1102019-04-05 22:12:42 +0100111 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000112}
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,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100125 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
Jose Marinho135dff32019-02-28 10:25:57 +0000131 * ready to be scheduled again. This SPCI function always returns SPCI_SUCCESS.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000132 */
Jose Marinho135dff32019-02-28 10:25:57 +0000133int32_t api_spci_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000134{
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. */
Jose Marinho135dff32019-02-28 10:25:57 +0000141 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000142 }
143
Jose Marinho135dff32019-02-28 10:25:57 +0000144 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
145
146 /* SPCI_YIELD always returns SPCI_SUCCESS. */
147 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000148}
149
150/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000151 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000152 */
153struct vcpu *api_abort(struct vcpu *current)
154{
155 struct hf_vcpu_run_return ret = {
156 .code = HF_VCPU_RUN_ABORTED,
157 };
158
159 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
160
161 if (current->vm->id == HF_PRIMARY_VM_ID) {
162 /* TODO: what to do when the primary aborts? */
163 for (;;) {
164 /* Do nothing. */
165 }
166 }
167
168 atomic_store_explicit(&current->vm->aborting, true,
169 memory_order_relaxed);
170
171 /* TODO: free resources once all vCPUs abort. */
172
Andrew Sculld6ee1102019-04-05 22:12:42 +0100173 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000174}
175
176/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000177 * Returns the ID of the VM.
178 */
179int64_t api_vm_get_id(const struct vcpu *current)
180{
181 return current->vm->id;
182}
183
184/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100185 * Returns the number of VMs configured to run.
186 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100187int64_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100188{
Andrew Scull19503262018-09-20 14:48:39 +0100189 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100190}
191
192/**
193 * Returns the number of vcpus configured in the given VM.
194 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100195int64_t api_vcpu_get_count(uint32_t vm_id, const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100196{
Andrew Scull19503262018-09-20 14:48:39 +0100197 struct vm *vm;
198
199 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100200 if (current->vm->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100201 return -1;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100202 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100203
Andrew Scull19503262018-09-20 14:48:39 +0100204 vm = vm_get(vm_id);
205 if (vm == NULL) {
206 return -1;
207 }
208
209 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100210}
211
212/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000213 * This function is called by the architecture-specific context switching
214 * function to indicate that register state for the given vcpu has been saved
215 * and can therefore be used by other pcpus.
216 */
217void api_regs_state_saved(struct vcpu *vcpu)
218{
219 sl_lock(&vcpu->lock);
220 vcpu->regs_available = true;
221 sl_unlock(&vcpu->lock);
222}
223
224/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000225 * Retrieves the next waiter and removes it from the wait list if the VM's
226 * mailbox is in a writable state.
227 */
228static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
229{
230 struct wait_entry *entry;
231 struct vm *vm = locked_vm.vm;
232
Andrew Sculld6ee1102019-04-05 22:12:42 +0100233 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000234 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
235 /* The mailbox is not writable or there are no waiters. */
236 return NULL;
237 }
238
239 /* Remove waiter from the wait list. */
240 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
241 wait_links);
242 list_remove(&entry->wait_links);
243 return entry;
244}
245
246/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000247 * Assuming that the arguments have already been checked by the caller, injects
248 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
249 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
250 * is next run, which is up to the scheduler.
251 *
252 * Returns:
253 * - 0 on success if no further action is needed.
254 * - 1 if it was called by the primary VM and the primary VM now needs to wake
255 * up or kick the target vCPU.
256 */
257static int64_t internal_interrupt_inject(struct vm *target_vm,
258 struct vcpu *target_vcpu,
259 uint32_t intid, struct vcpu *current,
260 struct vcpu **next)
261{
262 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
263 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000264 int64_t ret = 0;
265
266 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000267
268 /*
269 * We only need to change state and (maybe) trigger a virtual IRQ if it
270 * is enabled and was not previously pending. Otherwise we can skip
271 * everything except setting the pending bit.
272 *
273 * If you change this logic make sure to update the need_vm_lock logic
274 * above to match.
275 */
276 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
277 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
278 intid_mask)) {
279 goto out;
280 }
281
282 /* Increment the count. */
283 target_vcpu->interrupts.enabled_and_pending_count++;
284
285 /*
286 * Only need to update state if there was not already an
287 * interrupt enabled and pending.
288 */
289 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
290 goto out;
291 }
292
Andrew Walbran508e63c2018-12-20 17:02:37 +0000293 if (current->vm->id == HF_PRIMARY_VM_ID) {
294 /*
295 * If the call came from the primary VM, let it know that it
296 * should run or kick the target vCPU.
297 */
298 ret = 1;
299 } else if (current != target_vcpu && next != NULL) {
300 /*
301 * Switch to the primary so that it can switch to the target, or
302 * kick it if it is already running on a different physical CPU.
303 */
304 struct hf_vcpu_run_return ret = {
305 .code = HF_VCPU_RUN_WAKE_UP,
306 .wake_up.vm_id = target_vm->id,
307 .wake_up.vcpu = target_vcpu - target_vm->vcpus,
308 };
Andrew Sculld6ee1102019-04-05 22:12:42 +0100309 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000310 }
311
312out:
313 /* Either way, make it pending. */
314 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
315
316 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000317
318 return ret;
319}
320
321/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000322 * Prepares the vcpu to run by updating its state and fetching whether a return
323 * value needs to be forced onto the vCPU.
324 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000325static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000326 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000327{
Andrew Scullb06d1752019-02-04 10:15:48 +0000328 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000329 bool ret;
330
Andrew Scullb06d1752019-02-04 10:15:48 +0000331 /*
332 * Wait until the registers become available. All locks must be
333 * released between iterations of this loop to avoid potential deadlocks
334 * if, on any path, a lock needs to be taken after taking the decision
335 * to switch context but before the registers have been saved.
336 *
337 * The VM lock is not needed in the common case so it must only be taken
338 * when it is going to be needed. This ensures there are no inter-vCPU
339 * dependencies in the common run case meaning the sensitive context
340 * switch performance is consistent.
341 */
342 for (;;) {
343 sl_lock(&vcpu->lock);
344
345 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100346 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000347 if (need_vm_lock) {
348 sl_unlock(&vcpu->lock);
349 sl_lock(&vcpu->vm->lock);
350 sl_lock(&vcpu->lock);
351 }
352
353 if (vcpu->regs_available) {
354 break;
355 }
356
Andrew Sculld6ee1102019-04-05 22:12:42 +0100357 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000358 /*
359 * vCPU is running on another pCPU.
360 *
361 * It's ok to not return the sleep duration here because
362 * the other physical CPU that is currently running this
363 * vCPU will return sleep duration if neeed. The default
364 * return value is HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no
365 * need to set it explicitly.
366 */
367 ret = false;
368 goto out;
369 }
370
371 sl_unlock(&vcpu->lock);
372 if (need_vm_lock) {
373 sl_unlock(&vcpu->vm->lock);
374 }
375 }
Andrew Scull9726c252019-01-23 13:44:19 +0000376
377 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100378 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000379 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
380 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100381 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000382 }
383 ret = false;
384 goto out;
385 }
386
Andrew Walbran508e63c2018-12-20 17:02:37 +0000387 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100388 case VCPU_STATE_RUNNING:
389 case VCPU_STATE_OFF:
390 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000391 ret = false;
392 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000393
Andrew Sculld6ee1102019-04-05 22:12:42 +0100394 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000395 /*
396 * A pending message allows the vCPU to run so the message can
397 * be delivered directly.
398 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100399 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000400 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100401 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000402 break;
403 }
404 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100405 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000406 /* Allow virtual interrupts to be delivered. */
407 if (vcpu->interrupts.enabled_and_pending_count > 0) {
408 break;
409 }
410
411 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000412 if (arch_timer_pending(&vcpu->regs)) {
413 break;
414 }
415
416 /*
417 * The vCPU is not ready to run, return the appropriate code to
418 * the primary which called vcpu_run.
419 */
420 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000421 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100422 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000423 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
424 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000425 run_ret->sleep.ns =
426 arch_timer_remaining_ns(&vcpu->regs);
427 }
428
429 ret = false;
430 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000431
Andrew Sculld6ee1102019-04-05 22:12:42 +0100432 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000433 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000434 }
435
Andrew Scullb06d1752019-02-04 10:15:48 +0000436 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000437 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100438 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000439
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000440 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000441 * Mark the registers as unavailable now that we're about to reflect
442 * them onto the real registers. This will also prevent another physical
443 * CPU from trying to read these registers.
444 */
445 vcpu->regs_available = false;
446
447 ret = true;
448
449out:
450 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000451 if (need_vm_lock) {
452 sl_unlock(&vcpu->vm->lock);
453 }
454
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000455 return ret;
456}
457
458/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100459 * Runs the given vcpu of the given vm.
460 */
Andrew Scull6d2db332018-10-10 15:28:17 +0100461struct hf_vcpu_run_return api_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000462 const struct vcpu *current,
463 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100464{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100465 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100466 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100467 struct hf_vcpu_run_return ret = {
468 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000469 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100470 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100471
472 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100473 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100474 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100475 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100476
Andrew Scull19503262018-09-20 14:48:39 +0100477 /* Only secondary VM vcpus can be run. */
478 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100479 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100480 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100481
Andrew Scull19503262018-09-20 14:48:39 +0100482 /* The requested VM must exist. */
483 vm = vm_get(vm_id);
484 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100485 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100486 }
487
488 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100489 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100490 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100491 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100492
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000493 /* Update state if allowed. */
Andrew Scullf3d45592018-09-20 14:30:22 +0100494 vcpu = &vm->vcpus[vcpu_idx];
Andrew Scullb06d1752019-02-04 10:15:48 +0000495 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000496 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100497 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000498
Andrew Walbran508e63c2018-12-20 17:02:37 +0000499 /*
500 * Inject timer interrupt if timer has expired. It's safe to access
501 * vcpu->regs here because api_vcpu_prepare_run already made sure that
502 * regs_available was true (and then set it to false) before returning
503 * true.
504 */
505 if (arch_timer_pending(&vcpu->regs)) {
506 /* Make virtual timer interrupt pending. */
507 internal_interrupt_inject(vm, vcpu, HF_VIRTUAL_TIMER_INTID,
508 vcpu, NULL);
509
510 /*
511 * Set the mask bit so the hardware interrupt doesn't fire
512 * again. Ideally we wouldn't do this because it affects what
513 * the secondary vCPU sees, but if we don't then we end up with
514 * a loop of the interrupt firing each time we try to return to
515 * the secondary vCPU.
516 */
517 arch_timer_mask(&vcpu->regs);
518 }
519
Andrew Scull33fecd32019-01-08 14:48:27 +0000520 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000521 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000522
Andrew Scull33fecd32019-01-08 14:48:27 +0000523 /*
524 * Set a placeholder return code to the scheduler. This will be
525 * overwritten when the switch back to the primary occurs.
526 */
527 ret.code = HF_VCPU_RUN_PREEMPTED;
528
Andrew Scull6d2db332018-10-10 15:28:17 +0100529out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100530 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100531}
532
533/**
Andrew Scull81e85092018-12-12 12:56:20 +0000534 * Check that the mode indicates memory that is valid, owned and exclusive.
535 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000536static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000537{
538 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
539 0;
540}
541
542/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000543 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
544 * after they've succeeded. If a secondary VM is running and there are waiters,
545 * it also switches back to the primary VM for it to wake waiters up.
546 */
547static int64_t api_waiter_result(struct vm_locked locked_vm,
548 struct vcpu *current, struct vcpu **next)
549{
550 struct vm *vm = locked_vm.vm;
551 struct hf_vcpu_run_return ret = {
552 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
553 };
554
555 if (list_empty(&vm->mailbox.waiter_list)) {
556 /* No waiters, nothing else to do. */
557 return 0;
558 }
559
560 if (vm->id == HF_PRIMARY_VM_ID) {
561 /* The caller is the primary VM. Tell it to wake up waiters. */
562 return 1;
563 }
564
565 /*
566 * Switch back to the primary VM, informing it that there are waiters
567 * that need to be notified.
568 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100569 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000570
571 return 0;
572}
573
574/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100575 * Configures the VM to send/receive data through the specified pages. The pages
576 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000577 *
578 * Returns:
579 * - -1 on failure.
580 * - 0 on success if no further action is needed.
581 * - 1 if it was called by the primary VM and the primary VM now needs to wake
582 * up or kick waiters. Waiters should be retrieved by calling
583 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100584 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000585int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
586 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100587{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100588 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000589 struct vm_locked locked;
Andrew Scull80871322018-08-06 12:04:09 +0100590 paddr_t pa_send_begin;
591 paddr_t pa_send_end;
592 paddr_t pa_recv_begin;
593 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000594 int orig_send_mode;
595 int orig_recv_mode;
596 struct mpool local_page_pool;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100597 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100598
599 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000600 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
601 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100602 return -1;
603 }
604
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000605 /* Convert to physical addresses. */
606 pa_send_begin = pa_from_ipa(send);
607 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
608
609 pa_recv_begin = pa_from_ipa(recv);
610 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
611
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100612 /* Fail if the same page is used for the send and receive pages. */
613 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000614 return -1;
615 }
616
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000617 vm_lock(vm, &locked);
Andrew Scull220e6212018-12-21 18:09:00 +0000618
619 /* We only allow these to be setup once. */
620 if (vm->mailbox.send || vm->mailbox.recv) {
621 goto fail;
622 }
623
624 /*
625 * Ensure the pages are valid, owned and exclusive to the VM and that
626 * the VM has the required access to the memory.
627 */
628 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
629 &orig_send_mode) ||
630 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
631 (orig_send_mode & MM_MODE_R) == 0 ||
632 (orig_send_mode & MM_MODE_W) == 0) {
633 goto fail;
634 }
635
636 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
637 &orig_recv_mode) ||
638 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
639 (orig_recv_mode & MM_MODE_R) == 0) {
640 goto fail;
641 }
642
643 /*
644 * Create a local pool so any freed memory can't be used by another
645 * thread. This is to ensure the original mapping can be restored if any
646 * stage of the process fails.
647 */
648 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
649
650 /* Take memory ownership away from the VM and mark as shared. */
651 if (!mm_vm_identity_map(
652 &vm->ptable, pa_send_begin, pa_send_end,
653 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
654 NULL, &local_page_pool)) {
655 goto fail_free_pool;
656 }
657
658 if (!mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
659 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
660 NULL, &local_page_pool)) {
661 /* TODO: partial defrag of failed range. */
662 /* Recover any memory consumed in failed mapping. */
Andrew Scullda3df7f2019-01-05 17:49:27 +0000663 mm_vm_defrag(&vm->ptable, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000664 goto fail_undo_send;
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100665 }
666
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100667 /* Map the send page as read-only in the hypervisor address space. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000668 vm->mailbox.send = mm_identity_map(pa_send_begin, pa_send_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000669 MM_MODE_R, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100670 if (!vm->mailbox.send) {
Andrew Scull220e6212018-12-21 18:09:00 +0000671 /* TODO: partial defrag of failed range. */
672 /* Recover any memory consumed in failed mapping. */
673 mm_defrag(&local_page_pool);
674 goto fail_undo_send_and_recv;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100675 }
676
677 /*
678 * Map the receive page as writable in the hypervisor address space. On
679 * failure, unmap the send page before returning.
680 */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000681 vm->mailbox.recv = mm_identity_map(pa_recv_begin, pa_recv_end,
Andrew Scull220e6212018-12-21 18:09:00 +0000682 MM_MODE_W, &local_page_pool);
Andrew Scullaa039b32018-10-04 15:02:26 +0100683 if (!vm->mailbox.recv) {
Andrew Scull220e6212018-12-21 18:09:00 +0000684 /* TODO: partial defrag of failed range. */
685 /* Recover any memory consumed in failed mapping. */
686 mm_defrag(&local_page_pool);
687 goto fail_undo_all;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100688 }
689
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000690 /* Tell caller about waiters, if any. */
691 ret = api_waiter_result(locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000692 goto exit;
693
694 /*
695 * The following mappings will not require more memory than is available
696 * in the local pool.
697 */
698fail_undo_all:
699 vm->mailbox.send = NULL;
Andrew Scullda241972019-01-05 18:17:48 +0000700 mm_unmap(pa_send_begin, pa_send_end, &local_page_pool);
Andrew Scull220e6212018-12-21 18:09:00 +0000701
702fail_undo_send_and_recv:
703 mm_vm_identity_map(&vm->ptable, pa_recv_begin, pa_recv_end,
704 orig_recv_mode, NULL, &local_page_pool);
705
706fail_undo_send:
707 mm_vm_identity_map(&vm->ptable, pa_send_begin, pa_send_end,
708 orig_send_mode, NULL, &local_page_pool);
709
710fail_free_pool:
711 mpool_fini(&local_page_pool);
712
713fail:
714 ret = -1;
715
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100716exit:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000717 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100718
719 return ret;
720}
721
722/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100723 * Copies data from the sender's send buffer to the recipient's receive buffer
724 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000725 *
726 * If the recipient's receive buffer is busy, it can optionally register the
727 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100728 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000729int32_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
730 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100731{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100732 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100733 struct vm *to;
Andrew Scullb06d1752019-02-04 10:15:48 +0000734 struct hf_vcpu_run_return primary_ret = {
735 .code = HF_VCPU_RUN_MESSAGE,
736 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000737 struct spci_message from_msg_replica;
738 struct spci_message *to_msg;
739 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100740
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000741 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100742
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000743 int64_t ret;
744 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
745 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100746
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000747 /*
748 * Check that the sender has configured its send buffer. Copy the
749 * message header. If the tx mailbox at from_msg is configured (i.e.
750 * from_msg != NULL) then it can be safely accessed after releasing the
751 * lock since the tx mailbox address can only be configured once.
752 */
753 sl_lock(&from->lock);
754 from_msg = from->mailbox.send;
755 sl_unlock(&from->lock);
756
757 if (from_msg == NULL) {
758 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100759 }
760
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100761 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000762 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100763 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000764 from_msg_replica = *from_msg;
765
766 /* Ensure source VM id corresponds to the current VM. */
767 if (from_msg_replica.source_vm_id != from->id) {
768 return SPCI_INVALID_PARAMETERS;
769 }
770
771 size = from_msg_replica.length;
772 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100773 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000774 return SPCI_INVALID_PARAMETERS;
775 }
776
777 /* Disallow reflexive requests as this suggests an error in the VM. */
778 if (from_msg_replica.target_vm_id == from->id) {
779 return SPCI_INVALID_PARAMETERS;
780 }
781
782 /* Ensure the target VM exists. */
783 to = vm_get(from_msg_replica.target_vm_id);
784 if (to == NULL) {
785 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100786 }
787
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100788 sl_lock(&to->lock);
789
Andrew Sculld6ee1102019-04-05 22:12:42 +0100790 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100791 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000792 /*
793 * Fail if the target isn't currently ready to receive data,
794 * setting up for notification if requested.
795 */
796 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000797 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000798 &current->vm->wait_entries
799 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000800
801 /* Append waiter only if it's not there yet. */
802 if (list_empty(&entry->wait_links)) {
803 list_append(&to->mailbox.waiter_list,
804 &entry->wait_links);
805 }
806 }
807
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000808 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100809 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100810 }
811
Andrew Scullaa039b32018-10-04 15:02:26 +0100812 /* Copy data. */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000813 to_msg = to->mailbox.recv;
814 *to_msg = from_msg_replica;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100815 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
816 from->mailbox.send->payload, size);
Andrew Scullb06d1752019-02-04 10:15:48 +0000817 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000818 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100819
820 /* Messages for the primary VM are delivered directly. */
821 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100822 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000823 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100824 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100825 goto out;
826 }
827
Andrew Sculld6ee1102019-04-05 22:12:42 +0100828 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +0100829
830 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +0000831 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000832 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100833 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000834 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100835
836out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100837 sl_unlock(&to->lock);
838
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +0000839 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100840}
841
842/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100843 * Receives a message from the mailbox. If one isn't available, this function
844 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100845 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100846 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100847 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000848int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current,
849 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100850{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100851 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000852 int32_t return_code;
853 bool block =
854 (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100855
Andrew Scullaa039b32018-10-04 15:02:26 +0100856 /*
857 * The primary VM will receive messages as a status code from running
858 * vcpus and must not call this function.
859 */
Andrew Scull19503262018-09-20 14:48:39 +0100860 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000861 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100862 }
863
864 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100865
Andrew Scullaa039b32018-10-04 15:02:26 +0100866 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100867 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
868 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +0000869 return_code = SPCI_SUCCESS;
870 goto out;
871 }
872
873 /* No pending message so fail if not allowed to block. */
874 if (!block) {
875 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100876 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100877 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100878
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000879 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +0000880 * From this point onward this call can only be interrupted or a message
881 * received. If a message is received the return value will be set at
882 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000883 */
Jose Marinho3e2442f2019-03-12 13:30:37 +0000884 return_code = SPCI_INTERRUPTED;
885
886 /*
887 * Don't block if there are enabled and pending interrupts, to match
888 * behaviour of wait_for_interrupt.
889 */
890 if (current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +0100891 goto out;
892 }
893
Andrew Scullaa039b32018-10-04 15:02:26 +0100894 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +0000895 {
896 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +0000897 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +0000898 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000899
Andrew Walbranb4816552018-12-05 17:35:42 +0000900 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100901 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +0000902 }
Andrew Scullaa039b32018-10-04 15:02:26 +0100903out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100904 sl_unlock(&vm->lock);
905
Jose Marinho3e2442f2019-03-12 13:30:37 +0000906 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100907}
908
909/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000910 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
911 * by this function, the caller must have called api_mailbox_send before with
912 * the notify argument set to true, and this call must have failed because the
913 * mailbox was not available.
914 *
915 * It should be called repeatedly to retrieve a list of VMs.
916 *
917 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
918 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100919 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000920int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100921{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100922 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000923 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100924 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100925
926 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000927 if (list_empty(&vm->mailbox.ready_list)) {
928 ret = -1;
929 goto exit;
930 }
931
932 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
933 ready_links);
934 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000935 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000936
937exit:
938 sl_unlock(&vm->lock);
939 return ret;
940}
941
942/**
943 * Retrieves the next VM waiting to be notified that the mailbox of the
944 * specified VM became writable. Only primary VMs are allowed to call this.
945 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000946 * Returns -1 on failure or if there are no waiters; the VM id of the next
947 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000948 */
949int64_t api_mailbox_waiter_get(uint32_t vm_id, const struct vcpu *current)
950{
951 struct vm *vm;
952 struct vm_locked locked;
953 struct wait_entry *entry;
954 struct vm *waiting_vm;
955
956 /* Only primary VMs are allowed to call this function. */
957 if (current->vm->id != HF_PRIMARY_VM_ID) {
958 return -1;
959 }
960
961 vm = vm_get(vm_id);
962 if (vm == NULL) {
963 return -1;
964 }
965
966 /* Check if there are outstanding notifications from given vm. */
967 vm_lock(vm, &locked);
968 entry = api_fetch_waiter(locked);
969 vm_unlock(&locked);
970
971 if (entry == NULL) {
972 return -1;
973 }
974
975 /* Enqueue notification to waiting VM. */
976 waiting_vm = entry->waiting_vm;
977
978 sl_lock(&waiting_vm->lock);
979 if (list_empty(&entry->ready_links)) {
980 list_append(&waiting_vm->mailbox.ready_list,
981 &entry->ready_links);
982 }
983 sl_unlock(&waiting_vm->lock);
984
985 return waiting_vm->id;
986}
987
988/**
989 * Clears the caller's mailbox so that a new message can be received. The caller
990 * must have copied out all data they wish to preserve as new messages will
991 * overwrite the old and will arrive asynchronously.
992 *
993 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000994 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000995 * - 0 on success if no further action is needed.
996 * - 1 if it was called by the primary VM and the primary VM now needs to wake
997 * up or kick waiters. Waiters should be retrieved by calling
998 * hf_mailbox_waiter_get.
999 */
1000int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1001{
1002 struct vm *vm = current->vm;
1003 struct vm_locked locked;
1004 int64_t ret;
1005
1006 vm_lock(vm, &locked);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001007 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001008 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001009 ret = 0;
1010 break;
1011
Andrew Sculld6ee1102019-04-05 22:12:42 +01001012 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001013 ret = -1;
1014 break;
1015
Andrew Sculld6ee1102019-04-05 22:12:42 +01001016 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001017 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001018 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001019 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001020 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001021 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001022
1023 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001024}
Andrew Walbran318f5732018-11-20 16:23:42 +00001025
1026/**
1027 * Enables or disables a given interrupt ID for the calling vCPU.
1028 *
1029 * Returns 0 on success, or -1 if the intid is invalid.
1030 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001031int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001032{
1033 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1034 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001035
Andrew Walbran318f5732018-11-20 16:23:42 +00001036 if (intid >= HF_NUM_INTIDS) {
1037 return -1;
1038 }
1039
1040 sl_lock(&current->lock);
1041 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001042 /*
1043 * If it is pending and was not enabled before, increment the
1044 * count.
1045 */
1046 if (current->interrupts.interrupt_pending[intid_index] &
1047 ~current->interrupts.interrupt_enabled[intid_index] &
1048 intid_mask) {
1049 current->interrupts.enabled_and_pending_count++;
1050 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001051 current->interrupts.interrupt_enabled[intid_index] |=
1052 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001053 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001054 /*
1055 * If it is pending and was enabled before, decrement the count.
1056 */
1057 if (current->interrupts.interrupt_pending[intid_index] &
1058 current->interrupts.interrupt_enabled[intid_index] &
1059 intid_mask) {
1060 current->interrupts.enabled_and_pending_count--;
1061 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001062 current->interrupts.interrupt_enabled[intid_index] &=
1063 ~intid_mask;
1064 }
1065
1066 sl_unlock(&current->lock);
1067 return 0;
1068}
1069
1070/**
1071 * Returns the ID of the next pending interrupt for the calling vCPU, and
1072 * acknowledges it (i.e. marks it as no longer pending). Returns
1073 * HF_INVALID_INTID if there are no pending interrupts.
1074 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001075uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001076{
1077 uint8_t i;
1078 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001079
1080 /*
1081 * Find the first enabled and pending interrupt ID, return it, and
1082 * deactivate it.
1083 */
1084 sl_lock(&current->lock);
1085 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1086 uint32_t enabled_and_pending =
1087 current->interrupts.interrupt_enabled[i] &
1088 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001089
Andrew Walbran318f5732018-11-20 16:23:42 +00001090 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001091 uint8_t bit_index = ctz(enabled_and_pending);
1092 /*
1093 * Mark it as no longer pending and decrement the count.
1094 */
1095 current->interrupts.interrupt_pending[i] &=
1096 ~(1u << bit_index);
1097 current->interrupts.enabled_and_pending_count--;
1098 first_interrupt =
1099 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001100 break;
1101 }
1102 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001103
1104 sl_unlock(&current->lock);
1105 return first_interrupt;
1106}
1107
1108/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001109 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001110 * given VM and vCPU.
1111 */
1112static inline bool is_injection_allowed(uint32_t target_vm_id,
1113 struct vcpu *current)
1114{
1115 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001116
Andrew Walbran318f5732018-11-20 16:23:42 +00001117 /*
1118 * The primary VM is allowed to inject interrupts into any VM. Secondary
1119 * VMs are only allowed to inject interrupts into their own vCPUs.
1120 */
1121 return current_vm_id == HF_PRIMARY_VM_ID ||
1122 current_vm_id == target_vm_id;
1123}
1124
1125/**
1126 * Injects a virtual interrupt of the given ID into the given target vCPU.
1127 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1128 * when the vCPU is next run, which is up to the scheduler.
1129 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001130 * Returns:
1131 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1132 * ID is invalid, or the current VM is not allowed to inject interrupts to
1133 * the target VM.
1134 * - 0 on success if no further action is needed.
1135 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1136 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001137 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001138int64_t api_interrupt_inject(uint32_t target_vm_id, uint32_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +00001139 uint32_t intid, struct vcpu *current,
1140 struct vcpu **next)
1141{
Andrew Walbran318f5732018-11-20 16:23:42 +00001142 struct vcpu *target_vcpu;
1143 struct vm *target_vm = vm_get(target_vm_id);
1144
1145 if (intid >= HF_NUM_INTIDS) {
1146 return -1;
1147 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001148
Andrew Walbran318f5732018-11-20 16:23:42 +00001149 if (target_vm == NULL) {
1150 return -1;
1151 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001152
Andrew Walbran318f5732018-11-20 16:23:42 +00001153 if (target_vcpu_idx >= target_vm->vcpu_count) {
1154 /* The requested vcpu must exist. */
1155 return -1;
1156 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001157
Andrew Walbran318f5732018-11-20 16:23:42 +00001158 if (!is_injection_allowed(target_vm_id, current)) {
1159 return -1;
1160 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001161
Andrew Walbran318f5732018-11-20 16:23:42 +00001162 target_vcpu = &target_vm->vcpus[target_vcpu_idx];
1163
1164 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1165 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001166 return internal_interrupt_inject(target_vm, target_vcpu, intid, current,
1167 next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001168}
Andrew Scull6386f252018-12-06 13:29:10 +00001169
1170/**
1171 * Clears a region of physical memory by overwriting it with zeros. The data is
1172 * flushed from the cache so the memory has been cleared across the system.
1173 */
1174static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1175{
1176 /*
1177 * TODO: change this to a cpu local single page window rather than a
1178 * global mapping of the whole range. Such an approach will limit
1179 * the changes to stage-1 tables and will allow only local
1180 * invalidation.
1181 */
1182 void *ptr = mm_identity_map(begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001183 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001184
1185 if (!ptr) {
1186 /* TODO: partial defrag of failed range. */
1187 /* Recover any memory consumed in failed mapping. */
1188 mm_defrag(ppool);
1189 return false;
1190 }
1191
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001192 memset_s(ptr, size, 0, size);
Andrew Scull6386f252018-12-06 13:29:10 +00001193 arch_mm_write_back_dcache(ptr, size);
1194 mm_unmap(begin, end, ppool);
1195
1196 return true;
1197}
1198
1199/**
1200 * Shares memory from the calling VM with another. The memory can be shared in
1201 * different modes.
1202 *
1203 * TODO: the interface for sharing memory will need to be enhanced to allow
1204 * sharing with different modes e.g. read-only, informing the recipient
1205 * of the memory they have been given, opting to not wipe the memory and
1206 * possibly allowing multiple blocks to be transferred. What this will
1207 * look like is TBD.
1208 */
1209int64_t api_share_memory(uint32_t vm_id, ipaddr_t addr, size_t size,
1210 enum hf_share share, struct vcpu *current)
1211{
1212 struct vm *from = current->vm;
1213 struct vm *to;
1214 int orig_from_mode;
1215 int from_mode;
1216 int to_mode;
1217 ipaddr_t begin;
1218 ipaddr_t end;
1219 paddr_t pa_begin;
1220 paddr_t pa_end;
1221 struct mpool local_page_pool;
1222 int64_t ret;
1223
1224 /* Disallow reflexive shares as this suggests an error in the VM. */
1225 if (vm_id == from->id) {
1226 return -1;
1227 }
1228
1229 /* Ensure the target VM exists. */
1230 to = vm_get(vm_id);
1231 if (to == NULL) {
1232 return -1;
1233 }
1234
1235 begin = addr;
1236 end = ipa_add(addr, size);
1237
1238 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001239 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1240 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001241 return -1;
1242 }
1243
1244 /* Convert the sharing request to memory management modes. */
1245 switch (share) {
1246 case HF_MEMORY_GIVE:
1247 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1248 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1249 break;
1250
1251 case HF_MEMORY_LEND:
1252 from_mode = MM_MODE_INVALID;
1253 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1254 break;
1255
1256 case HF_MEMORY_SHARE:
1257 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1258 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1259 MM_MODE_SHARED;
1260 break;
1261
1262 default:
1263 /* The input is untrusted so might not be a valid value. */
1264 return -1;
1265 }
1266
1267 /*
1268 * Create a local pool so any freed memory can't be used by another
1269 * thread. This is to ensure the original mapping can be restored if any
1270 * stage of the process fails.
1271 */
1272 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1273
1274 sl_lock_both(&from->lock, &to->lock);
1275
1276 /*
1277 * Ensure that the memory range is mapped with the same mode so that
1278 * changes can be reverted if the process fails.
1279 */
1280 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1281 goto fail;
1282 }
1283
1284 /*
1285 * Ensure the memory range is valid for the sender. If it isn't, the
1286 * sender has either shared it with another VM already or has no claim
1287 * to the memory.
1288 */
1289 if (orig_from_mode & MM_MODE_INVALID) {
1290 goto fail;
1291 }
1292
1293 /*
1294 * The sender must own the memory and have exclusive access to it in
1295 * order to share it. Alternatively, it is giving memory back to the
1296 * owning VM.
1297 */
1298 if (orig_from_mode & MM_MODE_UNOWNED) {
1299 int orig_to_mode;
1300
1301 if (share != HF_MEMORY_GIVE ||
1302 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1303 orig_to_mode & MM_MODE_UNOWNED) {
1304 goto fail;
1305 }
1306 } else if (orig_from_mode & MM_MODE_SHARED) {
1307 goto fail;
1308 }
1309
1310 pa_begin = pa_from_ipa(begin);
1311 pa_end = pa_from_ipa(end);
1312
1313 /*
1314 * First update the mapping for the sender so there is not overlap with
1315 * the recipient.
1316 */
1317 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1318 NULL, &local_page_pool)) {
1319 goto fail;
1320 }
1321
1322 /* Clear the memory so no VM or device can see the previous contents. */
1323 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1324 goto fail_return_to_sender;
1325 }
1326
1327 /* Complete the transfer by mapping the memory into the recipient. */
1328 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1329 &local_page_pool)) {
1330 /* TODO: partial defrag of failed range. */
1331 /* Recover any memory consumed in failed mapping. */
1332 mm_vm_defrag(&from->ptable, &local_page_pool);
1333 goto fail_return_to_sender;
1334 }
1335
1336 ret = 0;
1337 goto out;
1338
1339fail_return_to_sender:
1340 mm_vm_identity_map(&from->ptable, pa_begin, pa_end, orig_from_mode,
1341 NULL, &local_page_pool);
1342
1343fail:
1344 ret = -1;
1345
1346out:
1347 sl_unlock(&from->lock);
1348 sl_unlock(&to->lock);
1349
1350 mpool_fini(&local_page_pool);
1351
1352 return ret;
1353}