blob: aa6c7c12ca75b43f0512105a308bcd021a231850 [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 Walbran2619e0a2020-01-10 16:37:50 +000020#include "hf/arch/tee.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000021#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000022
Andrew Scull877ae4b2019-07-02 12:52:33 +010023#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000024#include "hf/dlog.h"
Andrew Scull6386f252018-12-06 13:29:10 +000025#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010026#include "hf/plat/console.h"
Jose Marinho40d55f32019-07-01 15:41:54 +010027#include "hf/spci_internal.h"
Andrew Scull6386f252018-12-06 13:29:10 +000028#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010029#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010030#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010031#include "hf/vm.h"
32
Andrew Scullf35a5c92018-08-07 18:09:46 +010033#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000034#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010035
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000036/*
37 * To eliminate the risk of deadlocks, we define a partial order for the
38 * acquisition of locks held concurrently by the same physical CPU. Our current
39 * ordering requirements are as follows:
40 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010041 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000042 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010043 * Locks of the same kind require the lock of lowest address to be locked first,
44 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000045 */
46
Andrew Scullaa039b32018-10-04 15:02:26 +010047static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010048 "Currently, a page is mapped for the send and receive buffers so "
49 "the maximum request is the size of a page.");
50
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000051static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000052
53/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000054 * Initialises the API page pool by taking ownership of the contents of the
55 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000056 */
57void api_init(struct mpool *ppool)
58{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000059 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000060}
61
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010062/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000063 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010064 *
65 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranf0c314d2019-10-02 14:24:26 +010066 * to cause SPCI_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010067 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010068static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010069 struct spci_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000070 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010071{
Andrew Walbran42347a92019-05-09 13:59:03 +010072 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010073 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074
Andrew Walbran508e63c2018-12-20 17:02:37 +000075 /*
76 * If the secondary is blocked but has a timer running, sleep until the
77 * timer fires rather than indefinitely.
78 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010079 switch (primary_ret.func) {
80 case HF_SPCI_RUN_WAIT_FOR_INTERRUPT:
81 case SPCI_MSG_WAIT_32: {
82 if (arch_timer_enabled_current()) {
83 uint64_t remaining_ns =
84 arch_timer_remaining_ns_current();
85
86 if (remaining_ns == 0) {
87 /*
88 * Timer is pending, so the current vCPU should
89 * be run again right away.
90 */
91 primary_ret.func = SPCI_INTERRUPT_32;
92 /*
93 * primary_ret.arg1 should already be set to the
94 * current VM ID and vCPU ID.
95 */
96 primary_ret.arg2 = 0;
97 } else {
98 primary_ret.arg2 = remaining_ns;
99 }
100 } else {
101 primary_ret.arg2 = SPCI_SLEEP_INDEFINITE;
102 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000103 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100104 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000105
106 default:
107 /* Do nothing. */
108 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000109 }
110
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100111 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100112 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100113
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000114 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000115 sl_lock(&current->lock);
116 current->state = secondary_state;
117 sl_unlock(&current->lock);
118
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100119 return next;
120}
121
122/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000123 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000124 */
125struct vcpu *api_preempt(struct vcpu *current)
126{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100127 struct spci_value ret = {
128 .func = SPCI_INTERRUPT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000129 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000130 };
131
Andrew Sculld6ee1102019-04-05 22:12:42 +0100132 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000133}
134
135/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000136 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000137 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100138 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100139struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100140{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100141 struct spci_value ret = {
142 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000143 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100144 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000145
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000146 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100147 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100148}
149
150/**
Andrew Walbran33645652019-04-15 12:29:31 +0100151 * Puts the current vCPU in off mode, and returns to the primary VM.
152 */
153struct vcpu *api_vcpu_off(struct vcpu *current)
154{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100155 struct spci_value ret = {
156 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000157 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100158 };
159
160 /*
161 * Disable the timer, so the scheduler doesn't get told to call back
162 * based on it.
163 */
164 arch_timer_disable_current();
165
166 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
167}
168
169/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000170 * Returns to the primary VM to allow this CPU to be used for other tasks as the
171 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100172 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000173 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100174void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000175{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100176 struct spci_value primary_ret = {
177 .func = SPCI_YIELD_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000178 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull66d62bf2019-02-01 13:54:10 +0000179 };
180
181 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000182 /* NOOP on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100183 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000184 }
185
Andrew Walbran16075b62019-09-03 17:11:07 +0100186 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000187}
188
189/**
Andrew Walbran33645652019-04-15 12:29:31 +0100190 * Switches to the primary so that it can switch to the target, or kick it if it
191 * is already running on a different physical CPU.
192 */
193struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
194{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100195 struct spci_value ret = {
196 .func = HF_SPCI_RUN_WAKE_UP,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000197 .arg1 = spci_vm_vcpu(target_vcpu->vm->id,
198 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100199 };
200 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
201}
202
203/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000204 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000205 */
206struct vcpu *api_abort(struct vcpu *current)
207{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100208 struct spci_value ret = spci_error(SPCI_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000209
Andrew Walbran17eebf92020-02-05 16:35:49 +0000210 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
211 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000212
213 if (current->vm->id == HF_PRIMARY_VM_ID) {
214 /* TODO: what to do when the primary aborts? */
215 for (;;) {
216 /* Do nothing. */
217 }
218 }
219
220 atomic_store_explicit(&current->vm->aborting, true,
221 memory_order_relaxed);
222
223 /* TODO: free resources once all vCPUs abort. */
224
Andrew Sculld6ee1102019-04-05 22:12:42 +0100225 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000226}
227
228/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000229 * Returns the ID of the VM.
230 */
Andrew Walbrand230f662019-10-07 18:03:36 +0100231struct spci_value api_spci_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000232{
Andrew Walbrand230f662019-10-07 18:03:36 +0100233 return (struct spci_value){.func = SPCI_SUCCESS_32,
234 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000235}
236
237/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100238 * Returns the number of VMs configured to run.
239 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100240spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100241{
Andrew Scull19503262018-09-20 14:48:39 +0100242 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100243}
244
245/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100246 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
247 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100248 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100249spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
250 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100251{
Andrew Scull19503262018-09-20 14:48:39 +0100252 struct vm *vm;
253
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000254 /* Only the primary VM needs to know about vCPUs for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100255 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100256 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100257 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100258
Andrew Walbran42347a92019-05-09 13:59:03 +0100259 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100260 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100261 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100262 }
263
264 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100265}
266
267/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000268 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000269 * function to indicate that register state for the given vCPU has been saved
270 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000271 */
272void api_regs_state_saved(struct vcpu *vcpu)
273{
274 sl_lock(&vcpu->lock);
275 vcpu->regs_available = true;
276 sl_unlock(&vcpu->lock);
277}
278
279/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000280 * Retrieves the next waiter and removes it from the wait list if the VM's
281 * mailbox is in a writable state.
282 */
283static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
284{
285 struct wait_entry *entry;
286 struct vm *vm = locked_vm.vm;
287
Andrew Sculld6ee1102019-04-05 22:12:42 +0100288 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000289 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
290 /* The mailbox is not writable or there are no waiters. */
291 return NULL;
292 }
293
294 /* Remove waiter from the wait list. */
295 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
296 wait_links);
297 list_remove(&entry->wait_links);
298 return entry;
299}
300
301/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000302 * Assuming that the arguments have already been checked by the caller, injects
303 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
304 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
305 * is next run, which is up to the scheduler.
306 *
307 * Returns:
308 * - 0 on success if no further action is needed.
309 * - 1 if it was called by the primary VM and the primary VM now needs to wake
310 * up or kick the target vCPU.
311 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100312static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000313 uint32_t intid, struct vcpu *current,
314 struct vcpu **next)
315{
316 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100317 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000318 int64_t ret = 0;
319
320 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000321
322 /*
323 * We only need to change state and (maybe) trigger a virtual IRQ if it
324 * is enabled and was not previously pending. Otherwise we can skip
325 * everything except setting the pending bit.
326 *
327 * If you change this logic make sure to update the need_vm_lock logic
328 * above to match.
329 */
330 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
331 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
332 intid_mask)) {
333 goto out;
334 }
335
336 /* Increment the count. */
337 target_vcpu->interrupts.enabled_and_pending_count++;
338
339 /*
340 * Only need to update state if there was not already an
341 * interrupt enabled and pending.
342 */
343 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
344 goto out;
345 }
346
Andrew Walbran508e63c2018-12-20 17:02:37 +0000347 if (current->vm->id == HF_PRIMARY_VM_ID) {
348 /*
349 * If the call came from the primary VM, let it know that it
350 * should run or kick the target vCPU.
351 */
352 ret = 1;
353 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100354 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000355 }
356
357out:
358 /* Either way, make it pending. */
359 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
360
361 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000362
363 return ret;
364}
365
366/**
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100367 * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL
368 * or SPCI_MSG_WAIT call.
369 */
370static struct spci_value spci_msg_recv_return(const struct vm *receiver)
371{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000372 switch (receiver->mailbox.recv_func) {
373 case SPCI_MSG_SEND_32:
374 return (struct spci_value){
375 .func = SPCI_MSG_SEND_32,
376 .arg1 = (receiver->mailbox.recv_sender << 16) |
377 receiver->id,
378 .arg3 = receiver->mailbox.recv_size};
379 case SPCI_MEM_DONATE_32:
380 case SPCI_MEM_LEND_32:
381 case SPCI_MEM_SHARE_32:
382 case HF_SPCI_MEM_RELINQUISH:
383 return (struct spci_value){.func = receiver->mailbox.recv_func,
Andrew Walbran0f6cede2020-01-10 15:38:09 +0000384 .arg3 = receiver->mailbox.recv_size,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000385 .arg4 = receiver->mailbox.recv_size};
386 default:
387 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000388 dlog_error("Tried to return an invalid message function %#x\n",
389 receiver->mailbox.recv_func);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000390 return spci_error(SPCI_DENIED);
391 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100392}
393
394/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000395 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000396 * value needs to be forced onto the vCPU.
397 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000398static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100399 struct spci_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000400{
Andrew Scullb06d1752019-02-04 10:15:48 +0000401 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000402 bool ret;
403
Andrew Scullb06d1752019-02-04 10:15:48 +0000404 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000405 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000406 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100407 * The VM lock is not needed in the common case so it must only be taken
408 * when it is going to be needed. This ensures there are no inter-vCPU
409 * dependencies in the common run case meaning the sensitive context
410 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000411 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000412 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000413
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000414 /* The VM needs to be locked to deliver mailbox messages. */
415 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
416 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000417 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000418 sl_lock(&vcpu->vm->lock);
419 sl_lock(&vcpu->lock);
420 }
421
422 /*
423 * If the vCPU is already running somewhere then we can't run it here
424 * simultaneously. While it is actually running then the state should be
425 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
426 * stops running but while Hafnium is in the process of switching back
427 * to the primary there will be a brief period while the state has been
428 * updated but `regs_available` is still false (until
429 * `api_regs_state_saved` is called). We can't start running it again
430 * until this has finished, so count this state as still running for the
431 * purposes of this check.
432 */
433 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
434 /*
435 * vCPU is running on another pCPU.
436 *
437 * It's okay not to return the sleep duration here because the
438 * other physical CPU that is currently running this vCPU will
439 * return the sleep duration if needed.
440 */
441 *run_ret = spci_error(SPCI_BUSY);
442 ret = false;
443 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000444 }
Andrew Scull9726c252019-01-23 13:44:19 +0000445
446 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100447 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000448 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
449 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100450 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000451 }
452 ret = false;
453 goto out;
454 }
455
Andrew Walbran508e63c2018-12-20 17:02:37 +0000456 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100457 case VCPU_STATE_RUNNING:
458 case VCPU_STATE_OFF:
459 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000460 ret = false;
461 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000462
Andrew Sculld6ee1102019-04-05 22:12:42 +0100463 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000464 /*
465 * A pending message allows the vCPU to run so the message can
466 * be delivered directly.
467 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100468 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100469 arch_regs_set_retval(&vcpu->regs,
470 spci_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100471 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000472 break;
473 }
474 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100475 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000476 /* Allow virtual interrupts to be delivered. */
477 if (vcpu->interrupts.enabled_and_pending_count > 0) {
478 break;
479 }
480
Andrew Walbran508e63c2018-12-20 17:02:37 +0000481 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000482 uint64_t timer_remaining_ns =
483 arch_timer_remaining_ns(&vcpu->regs);
484
485 /*
486 * The timer expired so allow the interrupt to be
487 * delivered.
488 */
489 if (timer_remaining_ns == 0) {
490 break;
491 }
492
493 /*
494 * The vCPU is not ready to run, return the appropriate
495 * code to the primary which called vcpu_run.
496 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100497 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100498 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100499 ? SPCI_MSG_WAIT_32
500 : HF_SPCI_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000501 run_ret->arg1 =
502 spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000503 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000504 }
505
506 ret = false;
507 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000508
Andrew Sculld6ee1102019-04-05 22:12:42 +0100509 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000510 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000511 }
512
Andrew Scullb06d1752019-02-04 10:15:48 +0000513 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000514 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100515 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000516
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000517 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000518 * Mark the registers as unavailable now that we're about to reflect
519 * them onto the real registers. This will also prevent another physical
520 * CPU from trying to read these registers.
521 */
522 vcpu->regs_available = false;
523
524 ret = true;
525
526out:
527 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000528 if (need_vm_lock) {
529 sl_unlock(&vcpu->vm->lock);
530 }
531
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000532 return ret;
533}
534
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100535struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx,
536 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100537{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100538 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100539 struct vcpu *vcpu;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100540 struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100541
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000542 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100543 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100544 ret.arg2 = SPCI_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100545 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100546 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100547
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000548 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100549 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100550 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100551 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100552
Andrew Scull19503262018-09-20 14:48:39 +0100553 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100554 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100555 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100556 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100557 }
558
Fuad Tabbaed294af2019-12-20 10:43:01 +0000559 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100560 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100561 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100562 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100563
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000564 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100565 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000566 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000567 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100568 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000569
Andrew Walbran508e63c2018-12-20 17:02:37 +0000570 /*
571 * Inject timer interrupt if timer has expired. It's safe to access
572 * vcpu->regs here because api_vcpu_prepare_run already made sure that
573 * regs_available was true (and then set it to false) before returning
574 * true.
575 */
576 if (arch_timer_pending(&vcpu->regs)) {
577 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100578 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
579 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000580
581 /*
582 * Set the mask bit so the hardware interrupt doesn't fire
583 * again. Ideally we wouldn't do this because it affects what
584 * the secondary vCPU sees, but if we don't then we end up with
585 * a loop of the interrupt firing each time we try to return to
586 * the secondary vCPU.
587 */
588 arch_timer_mask(&vcpu->regs);
589 }
590
Fuad Tabbaed294af2019-12-20 10:43:01 +0000591 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000592 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000593
Andrew Scull33fecd32019-01-08 14:48:27 +0000594 /*
595 * Set a placeholder return code to the scheduler. This will be
596 * overwritten when the switch back to the primary occurs.
597 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100598 ret.func = SPCI_INTERRUPT_32;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000599 ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100600 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000601
Andrew Scull6d2db332018-10-10 15:28:17 +0100602out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100603 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100604}
605
606/**
Andrew Scull81e85092018-12-12 12:56:20 +0000607 * Check that the mode indicates memory that is valid, owned and exclusive.
608 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100609static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000610{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100611 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
612 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000613}
614
615/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000616 * Determines the value to be returned by api_vm_configure and spci_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000617 * after they've succeeded. If a secondary VM is running and there are waiters,
618 * it also switches back to the primary VM for it to wake waiters up.
619 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000620static struct spci_value api_waiter_result(struct vm_locked locked_vm,
621 struct vcpu *current,
622 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000623{
624 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000625
626 if (list_empty(&vm->mailbox.waiter_list)) {
627 /* No waiters, nothing else to do. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000628 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000629 }
630
631 if (vm->id == HF_PRIMARY_VM_ID) {
632 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000633 return (struct spci_value){.func = SPCI_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000634 }
635
636 /*
637 * Switch back to the primary VM, informing it that there are waiters
638 * that need to be notified.
639 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000640 *next = api_switch_to_primary(
641 current, (struct spci_value){.func = SPCI_RX_RELEASE_32},
642 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000643
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000644 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000645}
646
647/**
Andrew Sculle1322792019-07-01 17:46:10 +0100648 * Configures the hypervisor's stage-1 view of the send and receive pages. The
649 * stage-1 page tables must be locked so memory cannot be taken by another core
650 * which could result in this transaction being unable to roll back in the case
651 * of an error.
652 */
653static bool api_vm_configure_stage1(struct vm_locked vm_locked,
654 paddr_t pa_send_begin, paddr_t pa_send_end,
655 paddr_t pa_recv_begin, paddr_t pa_recv_end,
656 struct mpool *local_page_pool)
657{
658 bool ret;
659 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
660
661 /* Map the send page as read-only in the hypervisor address space. */
662 vm_locked.vm->mailbox.send =
663 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
664 MM_MODE_R, local_page_pool);
665 if (!vm_locked.vm->mailbox.send) {
666 /* TODO: partial defrag of failed range. */
667 /* Recover any memory consumed in failed mapping. */
668 mm_defrag(mm_stage1_locked, local_page_pool);
669 goto fail;
670 }
671
672 /*
673 * Map the receive page as writable in the hypervisor address space. On
674 * failure, unmap the send page before returning.
675 */
676 vm_locked.vm->mailbox.recv =
677 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
678 MM_MODE_W, local_page_pool);
679 if (!vm_locked.vm->mailbox.recv) {
680 /* TODO: partial defrag of failed range. */
681 /* Recover any memory consumed in failed mapping. */
682 mm_defrag(mm_stage1_locked, local_page_pool);
683 goto fail_undo_send;
684 }
685
686 ret = true;
687 goto out;
688
689 /*
690 * The following mappings will not require more memory than is available
691 * in the local pool.
692 */
693fail_undo_send:
694 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100695 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
696 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100697
698fail:
699 ret = false;
700
701out:
702 mm_unlock_stage1(&mm_stage1_locked);
703
704 return ret;
705}
706
707/**
708 * Configures the send and receive pages in the VM stage-2 and hypervisor
709 * stage-1 page tables. Locking of the page tables combined with a local memory
710 * pool ensures there will always be enough memory to recover from any errors
711 * that arise.
712 */
713static bool api_vm_configure_pages(struct vm_locked vm_locked,
714 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100715 uint32_t orig_send_mode,
716 paddr_t pa_recv_begin, paddr_t pa_recv_end,
717 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100718{
719 bool ret;
720 struct mpool local_page_pool;
721
722 /*
723 * Create a local pool so any freed memory can't be used by another
724 * thread. This is to ensure the original mapping can be restored if any
725 * stage of the process fails.
726 */
727 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
728
729 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000730 if (!vm_identity_map(
731 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100732 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000733 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100734 goto fail;
735 }
736
Andrew Scull3c257452019-11-26 13:32:50 +0000737 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
738 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
739 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100740 /* TODO: partial defrag of failed range. */
741 /* Recover any memory consumed in failed mapping. */
742 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
743 goto fail_undo_send;
744 }
745
746 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
747 pa_recv_begin, pa_recv_end,
748 &local_page_pool)) {
749 goto fail_undo_send_and_recv;
750 }
751
752 ret = true;
753 goto out;
754
755 /*
756 * The following mappings will not require more memory than is available
757 * in the local pool.
758 */
759fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000760 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
761 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100762
763fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000764 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
765 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100766
767fail:
768 ret = false;
769
770out:
771 mpool_fini(&local_page_pool);
772
773 return ret;
774}
775
776/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100777 * Configures the VM to send/receive data through the specified pages. The pages
778 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000779 *
780 * Returns:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000781 * - SPCI_ERROR SPCI_INVALID_PARAMETERS if the given addresses are not properly
782 * aligned or are the same.
783 * - SPCI_ERROR SPCI_NO_MEMORY if the hypervisor was unable to map the buffers
784 * due to insuffient page table memory.
785 * - SPCI_ERROR SPCI_DENIED if the pages are already mapped or are not owned by
786 * the caller.
787 * - SPCI_SUCCESS on success if no further action is needed.
788 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
789 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100790 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000791struct spci_value api_spci_rxtx_map(ipaddr_t send, ipaddr_t recv,
792 uint32_t page_count, struct vcpu *current,
793 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100794{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100795 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100796 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100797 paddr_t pa_send_begin;
798 paddr_t pa_send_end;
799 paddr_t pa_recv_begin;
800 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100801 uint32_t orig_send_mode;
802 uint32_t orig_recv_mode;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000803 struct spci_value ret;
804
805 /* Hafnium only supports a fixed size of RX/TX buffers. */
806 if (page_count != HF_MAILBOX_SIZE / SPCI_PAGE_SIZE) {
807 return spci_error(SPCI_INVALID_PARAMETERS);
808 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100809
810 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000811 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
812 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000813 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100814 }
815
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000816 /* Convert to physical addresses. */
817 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000818 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000819
820 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000821 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000822
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100823 /* Fail if the same page is used for the send and receive pages. */
824 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000825 return spci_error(SPCI_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000826 }
827
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100828 /*
829 * The hypervisor's memory map must be locked for the duration of this
830 * operation to ensure there will be sufficient memory to recover from
831 * any failures.
832 *
833 * TODO: the scope of the can be reduced but will require restructuring
834 * to keep a single unlock point.
835 */
Andrew Sculle1322792019-07-01 17:46:10 +0100836 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000837
838 /* We only allow these to be setup once. */
839 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000840 ret = spci_error(SPCI_DENIED);
841 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000842 }
843
844 /*
845 * Ensure the pages are valid, owned and exclusive to the VM and that
846 * the VM has the required access to the memory.
847 */
848 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
849 &orig_send_mode) ||
850 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
851 (orig_send_mode & MM_MODE_R) == 0 ||
852 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000853 ret = spci_error(SPCI_DENIED);
854 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000855 }
856
857 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
858 &orig_recv_mode) ||
859 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
860 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000861 ret = spci_error(SPCI_DENIED);
862 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000863 }
864
Andrew Sculle1322792019-07-01 17:46:10 +0100865 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
866 orig_send_mode, pa_recv_begin, pa_recv_end,
867 orig_recv_mode)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000868 ret = spci_error(SPCI_NO_MEMORY);
869 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100870 }
871
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000872 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100873 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000874
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100875exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100876 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100877
878 return ret;
879}
880
881/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100882 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
883 * registers the `from` VM to be notified when it becomes available.
884 */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000885static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100886{
887 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
888 to.vm->mailbox.recv == NULL) {
889 /*
890 * Fail if the receiver isn't currently ready to receive data,
891 * setting up for notification if requested.
892 */
893 if (notify) {
894 struct wait_entry *entry =
Andrew Walbranaad8f982019-12-04 10:56:39 +0000895 vm_get_wait_entry(from, to.vm->id);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100896
897 /* Append waiter only if it's not there yet. */
898 if (list_empty(&entry->wait_links)) {
899 list_append(&to.vm->mailbox.waiter_list,
900 &entry->wait_links);
901 }
902 }
903
904 return true;
905 }
906
907 return false;
908}
909
910/**
911 * Notifies the `to` VM about the message currently in its mailbox, possibly
912 * with the help of the primary VM.
913 */
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000914static struct spci_value deliver_msg(struct vm_locked to, spci_vm_id_t from_id,
915 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100916{
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000917 struct spci_value ret = (struct spci_value){.func = SPCI_SUCCESS_32};
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100918 struct spci_value primary_ret = {
919 .func = SPCI_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000920 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100921 };
922
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100923 /* Messages for the primary VM are delivered directly. */
924 if (to.vm->id == HF_PRIMARY_VM_ID) {
925 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000926 * Only tell the primary VM the size and other details if the
927 * message is for it, to avoid leaking data about messages for
928 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100929 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000930 primary_ret = spci_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100931
932 to.vm->mailbox.state = MAILBOX_STATE_READ;
933 *next = api_switch_to_primary(current, primary_ret,
934 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000935 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100936 }
937
938 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
939
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000940 /* Messages for the TEE are sent on via the dispatcher. */
941 if (to.vm->id == HF_TEE_VM_ID) {
942 struct spci_value call = spci_msg_recv_return(to.vm);
943
944 return arch_tee_call(call);
945 /*
946 * Don't return to the primary VM in this case, as the TEE is
947 * not (yet) scheduled via SPCI.
948 */
949 }
950
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100951 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000952 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100953 *next = api_switch_to_primary(current, primary_ret,
954 VCPU_STATE_READY);
955 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000956
957 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100958}
959
960/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100961 * Copies data from the sender's send buffer to the recipient's receive buffer
962 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000963 *
964 * If the recipient's receive buffer is busy, it can optionally register the
965 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100966 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100967struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id,
968 spci_vm_id_t receiver_vm_id, uint32_t size,
969 uint32_t attributes, struct vcpu *current,
970 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100971{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100972 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100973 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +0000974 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100975 const void *from_msg;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100976 struct spci_value ret;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000977 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
978 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100979
Andrew Walbran70bc8622019-10-07 14:15:58 +0100980 /* Ensure sender VM ID corresponds to the current VM. */
981 if (sender_vm_id != from->id) {
982 return spci_error(SPCI_INVALID_PARAMETERS);
983 }
984
985 /* Disallow reflexive requests as this suggests an error in the VM. */
986 if (receiver_vm_id == from->id) {
987 return spci_error(SPCI_INVALID_PARAMETERS);
988 }
989
990 /* Limit the size of transfer. */
991 if (size > SPCI_MSG_PAYLOAD_MAX) {
992 return spci_error(SPCI_INVALID_PARAMETERS);
993 }
994
Andrew Walbran0b60c4f2019-12-10 17:05:29 +0000995 /* Ensure the receiver VM exists. */
996 to = vm_find(receiver_vm_id);
997 if (to == NULL) {
998 return spci_error(SPCI_INVALID_PARAMETERS);
999 }
1000
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001001 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001002 * Check that the sender has configured its send buffer. If the tx
1003 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1004 * be safely accessed after releasing the lock since the tx mailbox
1005 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001006 */
1007 sl_lock(&from->lock);
1008 from_msg = from->mailbox.send;
1009 sl_unlock(&from->lock);
1010
1011 if (from_msg == NULL) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001012 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001013 }
1014
Andrew Walbran82d6d152019-12-24 15:02:06 +00001015 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001016
Andrew Walbran82d6d152019-12-24 15:02:06 +00001017 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001018 ret = spci_error(SPCI_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001019 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001020 }
1021
Andrew Walbran82d6d152019-12-24 15:02:06 +00001022 /* Copy data. */
1023 memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg, size);
1024 to->mailbox.recv_size = size;
1025 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001026 to->mailbox.recv_func = SPCI_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001027 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001028
1029out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001030 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001031
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001032 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001033}
1034
1035/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001036 * Checks whether the vCPU's attempt to block for a message has already been
1037 * interrupted or whether it is allowed to block.
1038 */
1039bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1040{
1041 bool interrupted;
1042
1043 sl_lock(&current->lock);
1044
1045 /*
1046 * Don't block if there are enabled and pending interrupts, to match
1047 * behaviour of wait_for_interrupt.
1048 */
1049 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1050
1051 sl_unlock(&current->lock);
1052
1053 return interrupted;
1054}
1055
1056/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001057 * Receives a message from the mailbox. If one isn't available, this function
1058 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001059 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001060 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001062struct spci_value api_spci_msg_recv(bool block, struct vcpu *current,
1063 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001064{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001065 struct vm *vm = current->vm;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001066 struct spci_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001067
Andrew Scullaa039b32018-10-04 15:02:26 +01001068 /*
1069 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001070 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001071 */
Andrew Scull19503262018-09-20 14:48:39 +01001072 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001073 return spci_error(SPCI_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001074 }
1075
1076 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001077
Andrew Scullaa039b32018-10-04 15:02:26 +01001078 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001079 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1080 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001081 return_code = spci_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001082 goto out;
1083 }
1084
1085 /* No pending message so fail if not allowed to block. */
1086 if (!block) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001087 return_code = spci_error(SPCI_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001088 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001089 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001090
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001091 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001092 * From this point onward this call can only be interrupted or a message
1093 * received. If a message is received the return value will be set at
1094 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001095 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001096 return_code = spci_error(SPCI_INTERRUPTED);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001097 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001098 goto out;
1099 }
1100
Fuad Tabbaed294af2019-12-20 10:43:01 +00001101 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001102 {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001103 struct spci_value run_return = {
1104 .func = SPCI_MSG_WAIT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +00001105 .arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001106 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001107
Andrew Walbranb4816552018-12-05 17:35:42 +00001108 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001109 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001110 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001111out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001112 sl_unlock(&vm->lock);
1113
Jose Marinho3e2442f2019-03-12 13:30:37 +00001114 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001115}
1116
1117/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001118 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1119 * by this function, the caller must have called api_mailbox_send before with
1120 * the notify argument set to true, and this call must have failed because the
1121 * mailbox was not available.
1122 *
1123 * It should be called repeatedly to retrieve a list of VMs.
1124 *
1125 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1126 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001127 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001128int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001129{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001130 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001131 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001132 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001133
1134 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001135 if (list_empty(&vm->mailbox.ready_list)) {
1136 ret = -1;
1137 goto exit;
1138 }
1139
1140 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1141 ready_links);
1142 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001143 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001144
1145exit:
1146 sl_unlock(&vm->lock);
1147 return ret;
1148}
1149
1150/**
1151 * Retrieves the next VM waiting to be notified that the mailbox of the
1152 * specified VM became writable. Only primary VMs are allowed to call this.
1153 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001154 * Returns -1 on failure or if there are no waiters; the VM id of the next
1155 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001156 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001157int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001158{
1159 struct vm *vm;
1160 struct vm_locked locked;
1161 struct wait_entry *entry;
1162 struct vm *waiting_vm;
1163
1164 /* Only primary VMs are allowed to call this function. */
1165 if (current->vm->id != HF_PRIMARY_VM_ID) {
1166 return -1;
1167 }
1168
Andrew Walbran42347a92019-05-09 13:59:03 +01001169 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001170 if (vm == NULL) {
1171 return -1;
1172 }
1173
Fuad Tabbaed294af2019-12-20 10:43:01 +00001174 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001175 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001176 entry = api_fetch_waiter(locked);
1177 vm_unlock(&locked);
1178
1179 if (entry == NULL) {
1180 return -1;
1181 }
1182
1183 /* Enqueue notification to waiting VM. */
1184 waiting_vm = entry->waiting_vm;
1185
1186 sl_lock(&waiting_vm->lock);
1187 if (list_empty(&entry->ready_links)) {
1188 list_append(&waiting_vm->mailbox.ready_list,
1189 &entry->ready_links);
1190 }
1191 sl_unlock(&waiting_vm->lock);
1192
1193 return waiting_vm->id;
1194}
1195
1196/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001197 * Releases the caller's mailbox so that a new message can be received. The
1198 * caller must have copied out all data they wish to preserve as new messages
1199 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001200 *
1201 * Returns:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001202 * - SPCI_ERROR SPCI_DENIED on failure, if the mailbox hasn't been read.
1203 * - SPCI_SUCCESS on success if no further action is needed.
1204 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
1205 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001206 * hf_mailbox_waiter_get.
1207 */
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001208struct spci_value api_spci_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001209{
1210 struct vm *vm = current->vm;
1211 struct vm_locked locked;
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001212 struct spci_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001213
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001214 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001215 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001216 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001217 case MAILBOX_STATE_RECEIVED:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001218 ret = spci_error(SPCI_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001219 break;
1220
Andrew Sculld6ee1102019-04-05 22:12:42 +01001221 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001222 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001223 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001224 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001225 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001226 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001227
1228 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001229}
Andrew Walbran318f5732018-11-20 16:23:42 +00001230
1231/**
1232 * Enables or disables a given interrupt ID for the calling vCPU.
1233 *
1234 * Returns 0 on success, or -1 if the intid is invalid.
1235 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001236int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001237{
1238 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001239 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001240
Andrew Walbran318f5732018-11-20 16:23:42 +00001241 if (intid >= HF_NUM_INTIDS) {
1242 return -1;
1243 }
1244
1245 sl_lock(&current->lock);
1246 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001247 /*
1248 * If it is pending and was not enabled before, increment the
1249 * count.
1250 */
1251 if (current->interrupts.interrupt_pending[intid_index] &
1252 ~current->interrupts.interrupt_enabled[intid_index] &
1253 intid_mask) {
1254 current->interrupts.enabled_and_pending_count++;
1255 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001256 current->interrupts.interrupt_enabled[intid_index] |=
1257 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001258 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001259 /*
1260 * If it is pending and was enabled before, decrement the count.
1261 */
1262 if (current->interrupts.interrupt_pending[intid_index] &
1263 current->interrupts.interrupt_enabled[intid_index] &
1264 intid_mask) {
1265 current->interrupts.enabled_and_pending_count--;
1266 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001267 current->interrupts.interrupt_enabled[intid_index] &=
1268 ~intid_mask;
1269 }
1270
1271 sl_unlock(&current->lock);
1272 return 0;
1273}
1274
1275/**
1276 * Returns the ID of the next pending interrupt for the calling vCPU, and
1277 * acknowledges it (i.e. marks it as no longer pending). Returns
1278 * HF_INVALID_INTID if there are no pending interrupts.
1279 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001280uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001281{
1282 uint8_t i;
1283 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001284
1285 /*
1286 * Find the first enabled and pending interrupt ID, return it, and
1287 * deactivate it.
1288 */
1289 sl_lock(&current->lock);
1290 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1291 uint32_t enabled_and_pending =
1292 current->interrupts.interrupt_enabled[i] &
1293 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001294
Andrew Walbran318f5732018-11-20 16:23:42 +00001295 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001296 uint8_t bit_index = ctz(enabled_and_pending);
1297 /*
1298 * Mark it as no longer pending and decrement the count.
1299 */
1300 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001301 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001302 current->interrupts.enabled_and_pending_count--;
1303 first_interrupt =
1304 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001305 break;
1306 }
1307 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001308
1309 sl_unlock(&current->lock);
1310 return first_interrupt;
1311}
1312
1313/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001314 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001315 * given VM and vCPU.
1316 */
1317static inline bool is_injection_allowed(uint32_t target_vm_id,
1318 struct vcpu *current)
1319{
1320 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001321
Andrew Walbran318f5732018-11-20 16:23:42 +00001322 /*
1323 * The primary VM is allowed to inject interrupts into any VM. Secondary
1324 * VMs are only allowed to inject interrupts into their own vCPUs.
1325 */
1326 return current_vm_id == HF_PRIMARY_VM_ID ||
1327 current_vm_id == target_vm_id;
1328}
1329
1330/**
1331 * Injects a virtual interrupt of the given ID into the given target vCPU.
1332 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1333 * when the vCPU is next run, which is up to the scheduler.
1334 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001335 * Returns:
1336 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1337 * ID is invalid, or the current VM is not allowed to inject interrupts to
1338 * the target VM.
1339 * - 0 on success if no further action is needed.
1340 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1341 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001342 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001343int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001344 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001345 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001346{
Andrew Walbran318f5732018-11-20 16:23:42 +00001347 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001348 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001349
1350 if (intid >= HF_NUM_INTIDS) {
1351 return -1;
1352 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001353
Andrew Walbran318f5732018-11-20 16:23:42 +00001354 if (target_vm == NULL) {
1355 return -1;
1356 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001357
Andrew Walbran318f5732018-11-20 16:23:42 +00001358 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001359 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001360 return -1;
1361 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001362
Andrew Walbran318f5732018-11-20 16:23:42 +00001363 if (!is_injection_allowed(target_vm_id, current)) {
1364 return -1;
1365 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001366
Andrew Walbrane1310df2019-04-29 17:28:28 +01001367 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001368
Andrew Walbran17eebf92020-02-05 16:35:49 +00001369 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1370 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1371 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001372 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001373}
Andrew Scull6386f252018-12-06 13:29:10 +00001374
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001375/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001376struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001377{
1378 /*
1379 * Ensure that both major and minor revision representation occupies at
1380 * most 15 bits.
1381 */
1382 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1383 "Major revision representation take more than 15 bits.");
1384 static_assert(0x10000 > SPCI_VERSION_MINOR,
1385 "Minor revision representation take more than 16 bits.");
1386
Andrew Walbran7f920af2019-09-03 17:09:30 +01001387 struct spci_value ret = {
1388 .func = SPCI_SUCCESS_32,
Andrew Walbran455c53a2019-10-10 13:56:19 +01001389 .arg2 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
Andrew Walbran7f920af2019-09-03 17:09:30 +01001390 SPCI_VERSION_MINOR};
1391 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001392}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001393
1394int64_t api_debug_log(char c, struct vcpu *current)
1395{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001396 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001397 struct vm *vm = current->vm;
1398 struct vm_locked vm_locked = vm_lock(vm);
1399
Andrew Sculld54e1be2019-08-20 11:09:42 +01001400 if (c == '\n' || c == '\0') {
1401 flush = true;
1402 } else {
1403 vm->log_buffer[vm->log_buffer_length++] = c;
1404 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1405 }
1406
1407 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001408 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1409 vm->log_buffer_length);
1410 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001411 }
1412
1413 vm_unlock(&vm_locked);
1414
1415 return 0;
1416}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001417
1418/**
1419 * Discovery function returning information about the implementation of optional
1420 * SPCI interfaces.
1421 */
1422struct spci_value api_spci_features(uint32_t function_id)
1423{
1424 switch (function_id) {
1425 case SPCI_ERROR_32:
1426 case SPCI_SUCCESS_32:
1427 case SPCI_ID_GET_32:
1428 case SPCI_YIELD_32:
1429 case SPCI_VERSION_32:
1430 case SPCI_FEATURES_32:
1431 case SPCI_MSG_SEND_32:
1432 case SPCI_MSG_POLL_32:
1433 case SPCI_MSG_WAIT_32:
1434 return (struct spci_value){.func = SPCI_SUCCESS_32};
1435 default:
1436 return spci_error(SPCI_NOT_SUPPORTED);
1437 }
1438}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001439
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001440struct spci_value api_spci_mem_send(uint32_t share_func, ipaddr_t address,
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001441 uint32_t page_count,
Andrew Walbran0f6cede2020-01-10 15:38:09 +00001442 uint32_t fragment_length, uint32_t length,
1443 uint32_t cookie, struct vcpu *current,
1444 struct vcpu **next)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001445{
1446 struct vm *from = current->vm;
1447 struct vm *to;
1448 const void *from_msg;
1449 uint32_t message_buffer_size;
1450 struct spci_memory_region *memory_region;
1451 struct two_vm_locked vm_to_from_lock;
1452 struct spci_value ret;
1453
1454 if (ipa_addr(address) != 0 || page_count != 0) {
1455 /*
1456 * Hafnium only supports passing the descriptor in the TX
1457 * mailbox.
1458 */
1459 return spci_error(SPCI_INVALID_PARAMETERS);
1460 }
1461
Andrew Walbran0f6cede2020-01-10 15:38:09 +00001462 if ((cookie == 0) != (fragment_length == length)) {
1463 /* Cookie is required iff there are multiple fragments. */
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001464 return spci_error(SPCI_INVALID_PARAMETERS);
1465 }
1466
1467 /*
1468 * Check that the sender has configured its send buffer. If the TX
1469 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1470 * be safely accessed after releasing the lock since the TX mailbox
1471 * address can only be configured once.
1472 */
1473 sl_lock(&from->lock);
1474 from_msg = from->mailbox.send;
1475 sl_unlock(&from->lock);
1476
1477 if (from_msg == NULL) {
1478 return spci_error(SPCI_INVALID_PARAMETERS);
1479 }
1480
1481 /*
1482 * Copy the memory region descriptor to an internal buffer, so that the
1483 * sender can't change it underneath us.
1484 */
1485 memory_region =
Mahesh Bireddy8ca57862020-01-07 13:43:21 +05301486 (struct spci_memory_region *)cpu_get_buffer(current->cpu);
1487 message_buffer_size = cpu_get_buffer_size(current->cpu);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001488 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1489 return spci_error(SPCI_INVALID_PARAMETERS);
1490 }
1491 memcpy_s(memory_region, message_buffer_size, from_msg, length);
1492
1493 /* The sender must match the caller. */
1494 if (memory_region->sender != from->id) {
1495 return spci_error(SPCI_INVALID_PARAMETERS);
1496 }
1497
1498 if (memory_region->attribute_count != 1) {
1499 /* Hafnium doesn't support multi-way memory sharing for now. */
1500 return spci_error(SPCI_NOT_SUPPORTED);
1501 }
1502
1503 /*
1504 * Ensure that the receiver VM exists and isn't the same as the sender.
1505 */
1506 to = vm_find(memory_region->attributes[0].receiver);
1507 if (to == NULL || to == from) {
1508 return spci_error(SPCI_INVALID_PARAMETERS);
1509 }
1510
1511 vm_to_from_lock = vm_lock_both(to, from);
1512
1513 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
1514 ret = spci_error(SPCI_BUSY);
1515 goto out;
1516 }
1517
1518 ret = spci_msg_handle_architected_message(
1519 vm_to_from_lock.vm1, vm_to_from_lock.vm2, memory_region, length,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001520 share_func, &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001521
1522 if (ret.func == SPCI_SUCCESS_32) {
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001523 ret = deliver_msg(vm_to_from_lock.vm1, from->id, current, next);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001524 }
1525
1526out:
1527 vm_unlock(&vm_to_from_lock.vm1);
1528 vm_unlock(&vm_to_from_lock.vm2);
1529
1530 return ret;
1531}