blob: 39da4d8050f30916732c3fd7719c7d9181abdf73 [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 Walbran475c1452020-02-07 13:22:22 +000028#include "hf/spci_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000029#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010030#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010031#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010032#include "hf/vm.h"
33
Andrew Scullf35a5c92018-08-07 18:09:46 +010034#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000035#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010036
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000037/*
38 * To eliminate the risk of deadlocks, we define a partial order for the
39 * acquisition of locks held concurrently by the same physical CPU. Our current
40 * ordering requirements are as follows:
41 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010042 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000043 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010044 * Locks of the same kind require the lock of lowest address to be locked first,
45 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000046 */
47
Andrew Scullaa039b32018-10-04 15:02:26 +010048static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010049 "Currently, a page is mapped for the send and receive buffers so "
50 "the maximum request is the size of a page.");
51
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000052static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000053
54/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000055 * Initialises the API page pool by taking ownership of the contents of the
56 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000057 */
58void api_init(struct mpool *ppool)
59{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000060 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000061}
62
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010063/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000064 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010065 *
66 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranf0c314d2019-10-02 14:24:26 +010067 * to cause SPCI_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010068 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010069static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010070 struct spci_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000071 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010072{
Andrew Walbran42347a92019-05-09 13:59:03 +010073 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010074 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010075
Andrew Walbran508e63c2018-12-20 17:02:37 +000076 /*
77 * If the secondary is blocked but has a timer running, sleep until the
78 * timer fires rather than indefinitely.
79 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010080 switch (primary_ret.func) {
81 case HF_SPCI_RUN_WAIT_FOR_INTERRUPT:
82 case SPCI_MSG_WAIT_32: {
83 if (arch_timer_enabled_current()) {
84 uint64_t remaining_ns =
85 arch_timer_remaining_ns_current();
86
87 if (remaining_ns == 0) {
88 /*
89 * Timer is pending, so the current vCPU should
90 * be run again right away.
91 */
92 primary_ret.func = SPCI_INTERRUPT_32;
93 /*
94 * primary_ret.arg1 should already be set to the
95 * current VM ID and vCPU ID.
96 */
97 primary_ret.arg2 = 0;
98 } else {
99 primary_ret.arg2 = remaining_ns;
100 }
101 } else {
102 primary_ret.arg2 = SPCI_SLEEP_INDEFINITE;
103 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000104 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100105 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000106
107 default:
108 /* Do nothing. */
109 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000110 }
111
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100112 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100113 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100114
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000115 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000116 sl_lock(&current->lock);
117 current->state = secondary_state;
118 sl_unlock(&current->lock);
119
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100120 return next;
121}
122
123/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000124 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000125 */
126struct vcpu *api_preempt(struct vcpu *current)
127{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100128 struct spci_value ret = {
129 .func = SPCI_INTERRUPT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000130 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000131 };
132
Andrew Sculld6ee1102019-04-05 22:12:42 +0100133 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000134}
135
136/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000137 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000138 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100139 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100140struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100141{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100142 struct spci_value ret = {
143 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000144 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100145 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000146
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000147 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100148 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100149}
150
151/**
Andrew Walbran33645652019-04-15 12:29:31 +0100152 * Puts the current vCPU in off mode, and returns to the primary VM.
153 */
154struct vcpu *api_vcpu_off(struct vcpu *current)
155{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100156 struct spci_value ret = {
157 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000158 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100159 };
160
161 /*
162 * Disable the timer, so the scheduler doesn't get told to call back
163 * based on it.
164 */
165 arch_timer_disable_current();
166
167 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
168}
169
170/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000171 * Returns to the primary VM to allow this CPU to be used for other tasks as the
172 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100173 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000174 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100175void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000176{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100177 struct spci_value primary_ret = {
178 .func = SPCI_YIELD_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000179 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull66d62bf2019-02-01 13:54:10 +0000180 };
181
182 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000183 /* NOOP on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100184 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000185 }
186
Andrew Walbran16075b62019-09-03 17:11:07 +0100187 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000188}
189
190/**
Andrew Walbran33645652019-04-15 12:29:31 +0100191 * Switches to the primary so that it can switch to the target, or kick it if it
192 * is already running on a different physical CPU.
193 */
194struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
195{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100196 struct spci_value ret = {
197 .func = HF_SPCI_RUN_WAKE_UP,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000198 .arg1 = spci_vm_vcpu(target_vcpu->vm->id,
199 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100200 };
201 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
202}
203
204/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000205 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000206 */
207struct vcpu *api_abort(struct vcpu *current)
208{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100209 struct spci_value ret = spci_error(SPCI_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000210
Andrew Walbran17eebf92020-02-05 16:35:49 +0000211 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
212 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000213
214 if (current->vm->id == HF_PRIMARY_VM_ID) {
215 /* TODO: what to do when the primary aborts? */
216 for (;;) {
217 /* Do nothing. */
218 }
219 }
220
221 atomic_store_explicit(&current->vm->aborting, true,
222 memory_order_relaxed);
223
224 /* TODO: free resources once all vCPUs abort. */
225
Andrew Sculld6ee1102019-04-05 22:12:42 +0100226 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000227}
228
229/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000230 * Returns the ID of the VM.
231 */
Andrew Walbrand230f662019-10-07 18:03:36 +0100232struct spci_value api_spci_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000233{
Andrew Walbrand230f662019-10-07 18:03:36 +0100234 return (struct spci_value){.func = SPCI_SUCCESS_32,
235 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000236}
237
238/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100239 * Returns the number of VMs configured to run.
240 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100241spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100242{
Andrew Scull19503262018-09-20 14:48:39 +0100243 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100244}
245
246/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100247 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
248 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100249 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100250spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
251 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100252{
Andrew Scull19503262018-09-20 14:48:39 +0100253 struct vm *vm;
254
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000255 /* Only the primary VM needs to know about vCPUs for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100256 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100257 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100258 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100259
Andrew Walbran42347a92019-05-09 13:59:03 +0100260 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100261 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100262 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100263 }
264
265 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100266}
267
268/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000269 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000270 * function to indicate that register state for the given vCPU has been saved
271 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000272 */
273void api_regs_state_saved(struct vcpu *vcpu)
274{
275 sl_lock(&vcpu->lock);
276 vcpu->regs_available = true;
277 sl_unlock(&vcpu->lock);
278}
279
280/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000281 * Retrieves the next waiter and removes it from the wait list if the VM's
282 * mailbox is in a writable state.
283 */
284static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
285{
286 struct wait_entry *entry;
287 struct vm *vm = locked_vm.vm;
288
Andrew Sculld6ee1102019-04-05 22:12:42 +0100289 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000290 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
291 /* The mailbox is not writable or there are no waiters. */
292 return NULL;
293 }
294
295 /* Remove waiter from the wait list. */
296 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
297 wait_links);
298 list_remove(&entry->wait_links);
299 return entry;
300}
301
302/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000303 * Assuming that the arguments have already been checked by the caller, injects
304 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
305 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
306 * is next run, which is up to the scheduler.
307 *
308 * Returns:
309 * - 0 on success if no further action is needed.
310 * - 1 if it was called by the primary VM and the primary VM now needs to wake
311 * up or kick the target vCPU.
312 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100313static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000314 uint32_t intid, struct vcpu *current,
315 struct vcpu **next)
316{
317 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100318 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000319 int64_t ret = 0;
320
321 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000322
323 /*
324 * We only need to change state and (maybe) trigger a virtual IRQ if it
325 * is enabled and was not previously pending. Otherwise we can skip
326 * everything except setting the pending bit.
327 *
328 * If you change this logic make sure to update the need_vm_lock logic
329 * above to match.
330 */
331 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
332 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
333 intid_mask)) {
334 goto out;
335 }
336
337 /* Increment the count. */
338 target_vcpu->interrupts.enabled_and_pending_count++;
339
340 /*
341 * Only need to update state if there was not already an
342 * interrupt enabled and pending.
343 */
344 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
345 goto out;
346 }
347
Andrew Walbran508e63c2018-12-20 17:02:37 +0000348 if (current->vm->id == HF_PRIMARY_VM_ID) {
349 /*
350 * If the call came from the primary VM, let it know that it
351 * should run or kick the target vCPU.
352 */
353 ret = 1;
354 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100355 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000356 }
357
358out:
359 /* Either way, make it pending. */
360 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
361
362 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000363
364 return ret;
365}
366
367/**
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100368 * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL
369 * or SPCI_MSG_WAIT call.
370 */
371static struct spci_value spci_msg_recv_return(const struct vm *receiver)
372{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000373 switch (receiver->mailbox.recv_func) {
374 case SPCI_MSG_SEND_32:
375 return (struct spci_value){
376 .func = SPCI_MSG_SEND_32,
377 .arg1 = (receiver->mailbox.recv_sender << 16) |
378 receiver->id,
379 .arg3 = receiver->mailbox.recv_size};
380 case SPCI_MEM_DONATE_32:
381 case SPCI_MEM_LEND_32:
382 case SPCI_MEM_SHARE_32:
383 case HF_SPCI_MEM_RELINQUISH:
384 return (struct spci_value){.func = receiver->mailbox.recv_func,
Andrew Walbran0f6cede2020-01-10 15:38:09 +0000385 .arg3 = receiver->mailbox.recv_size,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000386 .arg4 = receiver->mailbox.recv_size};
387 default:
388 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000389 dlog_error("Tried to return an invalid message function %#x\n",
390 receiver->mailbox.recv_func);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000391 return spci_error(SPCI_DENIED);
392 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100393}
394
395/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000396 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000397 * value needs to be forced onto the vCPU.
398 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000399static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100400 struct spci_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000401{
Andrew Scullb06d1752019-02-04 10:15:48 +0000402 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000403 bool ret;
404
Andrew Scullb06d1752019-02-04 10:15:48 +0000405 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000406 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000407 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100408 * The VM lock is not needed in the common case so it must only be taken
409 * when it is going to be needed. This ensures there are no inter-vCPU
410 * dependencies in the common run case meaning the sensitive context
411 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000412 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000413 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000414
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000415 /* The VM needs to be locked to deliver mailbox messages. */
416 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
417 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000418 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000419 sl_lock(&vcpu->vm->lock);
420 sl_lock(&vcpu->lock);
421 }
422
423 /*
424 * If the vCPU is already running somewhere then we can't run it here
425 * simultaneously. While it is actually running then the state should be
426 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
427 * stops running but while Hafnium is in the process of switching back
428 * to the primary there will be a brief period while the state has been
429 * updated but `regs_available` is still false (until
430 * `api_regs_state_saved` is called). We can't start running it again
431 * until this has finished, so count this state as still running for the
432 * purposes of this check.
433 */
434 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
435 /*
436 * vCPU is running on another pCPU.
437 *
438 * It's okay not to return the sleep duration here because the
439 * other physical CPU that is currently running this vCPU will
440 * return the sleep duration if needed.
441 */
442 *run_ret = spci_error(SPCI_BUSY);
443 ret = false;
444 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000445 }
Andrew Scull9726c252019-01-23 13:44:19 +0000446
447 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100448 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000449 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
450 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100451 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000452 }
453 ret = false;
454 goto out;
455 }
456
Andrew Walbran508e63c2018-12-20 17:02:37 +0000457 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100458 case VCPU_STATE_RUNNING:
459 case VCPU_STATE_OFF:
460 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000461 ret = false;
462 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000463
Andrew Sculld6ee1102019-04-05 22:12:42 +0100464 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000465 /*
466 * A pending message allows the vCPU to run so the message can
467 * be delivered directly.
468 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100469 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100470 arch_regs_set_retval(&vcpu->regs,
471 spci_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100472 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000473 break;
474 }
475 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100476 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000477 /* Allow virtual interrupts to be delivered. */
478 if (vcpu->interrupts.enabled_and_pending_count > 0) {
479 break;
480 }
481
Andrew Walbran508e63c2018-12-20 17:02:37 +0000482 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000483 uint64_t timer_remaining_ns =
484 arch_timer_remaining_ns(&vcpu->regs);
485
486 /*
487 * The timer expired so allow the interrupt to be
488 * delivered.
489 */
490 if (timer_remaining_ns == 0) {
491 break;
492 }
493
494 /*
495 * The vCPU is not ready to run, return the appropriate
496 * code to the primary which called vcpu_run.
497 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100498 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100499 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100500 ? SPCI_MSG_WAIT_32
501 : HF_SPCI_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000502 run_ret->arg1 =
503 spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000504 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000505 }
506
507 ret = false;
508 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000509
Andrew Sculld6ee1102019-04-05 22:12:42 +0100510 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000511 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000512 }
513
Andrew Scullb06d1752019-02-04 10:15:48 +0000514 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000515 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100516 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000517
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000518 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000519 * Mark the registers as unavailable now that we're about to reflect
520 * them onto the real registers. This will also prevent another physical
521 * CPU from trying to read these registers.
522 */
523 vcpu->regs_available = false;
524
525 ret = true;
526
527out:
528 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000529 if (need_vm_lock) {
530 sl_unlock(&vcpu->vm->lock);
531 }
532
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000533 return ret;
534}
535
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100536struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx,
537 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100538{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100539 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100540 struct vcpu *vcpu;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100541 struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100542
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000543 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100544 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100545 ret.arg2 = SPCI_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100546 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100547 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100548
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000549 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100550 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100551 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100552 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100553
Andrew Scull19503262018-09-20 14:48:39 +0100554 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100555 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100556 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100557 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100558 }
559
Fuad Tabbaed294af2019-12-20 10:43:01 +0000560 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100561 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100562 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100563 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100564
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000565 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100566 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000567 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000568 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100569 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000570
Andrew Walbran508e63c2018-12-20 17:02:37 +0000571 /*
572 * Inject timer interrupt if timer has expired. It's safe to access
573 * vcpu->regs here because api_vcpu_prepare_run already made sure that
574 * regs_available was true (and then set it to false) before returning
575 * true.
576 */
577 if (arch_timer_pending(&vcpu->regs)) {
578 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100579 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
580 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000581
582 /*
583 * Set the mask bit so the hardware interrupt doesn't fire
584 * again. Ideally we wouldn't do this because it affects what
585 * the secondary vCPU sees, but if we don't then we end up with
586 * a loop of the interrupt firing each time we try to return to
587 * the secondary vCPU.
588 */
589 arch_timer_mask(&vcpu->regs);
590 }
591
Fuad Tabbaed294af2019-12-20 10:43:01 +0000592 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000593 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000594
Andrew Scull33fecd32019-01-08 14:48:27 +0000595 /*
596 * Set a placeholder return code to the scheduler. This will be
597 * overwritten when the switch back to the primary occurs.
598 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100599 ret.func = SPCI_INTERRUPT_32;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000600 ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100601 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000602
Andrew Scull6d2db332018-10-10 15:28:17 +0100603out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100604 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100605}
606
607/**
Andrew Scull81e85092018-12-12 12:56:20 +0000608 * Check that the mode indicates memory that is valid, owned and exclusive.
609 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100610static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000611{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100612 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
613 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000614}
615
616/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000617 * Determines the value to be returned by api_vm_configure and spci_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000618 * after they've succeeded. If a secondary VM is running and there are waiters,
619 * it also switches back to the primary VM for it to wake waiters up.
620 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000621static struct spci_value api_waiter_result(struct vm_locked locked_vm,
622 struct vcpu *current,
623 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000624{
625 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000626
627 if (list_empty(&vm->mailbox.waiter_list)) {
628 /* No waiters, nothing else to do. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000629 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000630 }
631
632 if (vm->id == HF_PRIMARY_VM_ID) {
633 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000634 return (struct spci_value){.func = SPCI_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000635 }
636
637 /*
638 * Switch back to the primary VM, informing it that there are waiters
639 * that need to be notified.
640 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000641 *next = api_switch_to_primary(
642 current, (struct spci_value){.func = SPCI_RX_RELEASE_32},
643 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000644
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000645 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000646}
647
648/**
Andrew Sculle1322792019-07-01 17:46:10 +0100649 * Configures the hypervisor's stage-1 view of the send and receive pages. The
650 * stage-1 page tables must be locked so memory cannot be taken by another core
651 * which could result in this transaction being unable to roll back in the case
652 * of an error.
653 */
654static bool api_vm_configure_stage1(struct vm_locked vm_locked,
655 paddr_t pa_send_begin, paddr_t pa_send_end,
656 paddr_t pa_recv_begin, paddr_t pa_recv_end,
657 struct mpool *local_page_pool)
658{
659 bool ret;
660 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
661
662 /* Map the send page as read-only in the hypervisor address space. */
663 vm_locked.vm->mailbox.send =
664 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
665 MM_MODE_R, local_page_pool);
666 if (!vm_locked.vm->mailbox.send) {
667 /* TODO: partial defrag of failed range. */
668 /* Recover any memory consumed in failed mapping. */
669 mm_defrag(mm_stage1_locked, local_page_pool);
670 goto fail;
671 }
672
673 /*
674 * Map the receive page as writable in the hypervisor address space. On
675 * failure, unmap the send page before returning.
676 */
677 vm_locked.vm->mailbox.recv =
678 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
679 MM_MODE_W, local_page_pool);
680 if (!vm_locked.vm->mailbox.recv) {
681 /* TODO: partial defrag of failed range. */
682 /* Recover any memory consumed in failed mapping. */
683 mm_defrag(mm_stage1_locked, local_page_pool);
684 goto fail_undo_send;
685 }
686
687 ret = true;
688 goto out;
689
690 /*
691 * The following mappings will not require more memory than is available
692 * in the local pool.
693 */
694fail_undo_send:
695 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100696 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
697 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100698
699fail:
700 ret = false;
701
702out:
703 mm_unlock_stage1(&mm_stage1_locked);
704
705 return ret;
706}
707
708/**
709 * Configures the send and receive pages in the VM stage-2 and hypervisor
710 * stage-1 page tables. Locking of the page tables combined with a local memory
711 * pool ensures there will always be enough memory to recover from any errors
712 * that arise.
713 */
714static bool api_vm_configure_pages(struct vm_locked vm_locked,
715 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100716 uint32_t orig_send_mode,
717 paddr_t pa_recv_begin, paddr_t pa_recv_end,
718 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100719{
720 bool ret;
721 struct mpool local_page_pool;
722
723 /*
724 * Create a local pool so any freed memory can't be used by another
725 * thread. This is to ensure the original mapping can be restored if any
726 * stage of the process fails.
727 */
728 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
729
730 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000731 if (!vm_identity_map(
732 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100733 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000734 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100735 goto fail;
736 }
737
Andrew Scull3c257452019-11-26 13:32:50 +0000738 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
739 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
740 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100741 /* TODO: partial defrag of failed range. */
742 /* Recover any memory consumed in failed mapping. */
743 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
744 goto fail_undo_send;
745 }
746
747 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
748 pa_recv_begin, pa_recv_end,
749 &local_page_pool)) {
750 goto fail_undo_send_and_recv;
751 }
752
753 ret = true;
754 goto out;
755
756 /*
757 * The following mappings will not require more memory than is available
758 * in the local pool.
759 */
760fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000761 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
762 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100763
764fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000765 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
766 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100767
768fail:
769 ret = false;
770
771out:
772 mpool_fini(&local_page_pool);
773
774 return ret;
775}
776
777/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100778 * Configures the VM to send/receive data through the specified pages. The pages
779 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000780 *
781 * Returns:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000782 * - SPCI_ERROR SPCI_INVALID_PARAMETERS if the given addresses are not properly
783 * aligned or are the same.
784 * - SPCI_ERROR SPCI_NO_MEMORY if the hypervisor was unable to map the buffers
785 * due to insuffient page table memory.
786 * - SPCI_ERROR SPCI_DENIED if the pages are already mapped or are not owned by
787 * the caller.
788 * - SPCI_SUCCESS on success if no further action is needed.
789 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
790 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100791 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000792struct spci_value api_spci_rxtx_map(ipaddr_t send, ipaddr_t recv,
793 uint32_t page_count, struct vcpu *current,
794 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100795{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100796 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100797 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100798 paddr_t pa_send_begin;
799 paddr_t pa_send_end;
800 paddr_t pa_recv_begin;
801 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100802 uint32_t orig_send_mode;
803 uint32_t orig_recv_mode;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000804 struct spci_value ret;
805
806 /* Hafnium only supports a fixed size of RX/TX buffers. */
807 if (page_count != HF_MAILBOX_SIZE / SPCI_PAGE_SIZE) {
808 return spci_error(SPCI_INVALID_PARAMETERS);
809 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100810
811 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000812 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
813 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000814 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100815 }
816
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000817 /* Convert to physical addresses. */
818 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000819 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000820
821 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000822 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000823
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100824 /* Fail if the same page is used for the send and receive pages. */
825 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000826 return spci_error(SPCI_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000827 }
828
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100829 /*
830 * The hypervisor's memory map must be locked for the duration of this
831 * operation to ensure there will be sufficient memory to recover from
832 * any failures.
833 *
834 * TODO: the scope of the can be reduced but will require restructuring
835 * to keep a single unlock point.
836 */
Andrew Sculle1322792019-07-01 17:46:10 +0100837 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000838
839 /* We only allow these to be setup once. */
840 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000841 ret = spci_error(SPCI_DENIED);
842 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000843 }
844
845 /*
846 * Ensure the pages are valid, owned and exclusive to the VM and that
847 * the VM has the required access to the memory.
848 */
849 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
850 &orig_send_mode) ||
851 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
852 (orig_send_mode & MM_MODE_R) == 0 ||
853 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000854 ret = spci_error(SPCI_DENIED);
855 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000856 }
857
858 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
859 &orig_recv_mode) ||
860 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
861 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000862 ret = spci_error(SPCI_DENIED);
863 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000864 }
865
Andrew Sculle1322792019-07-01 17:46:10 +0100866 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
867 orig_send_mode, pa_recv_begin, pa_recv_end,
868 orig_recv_mode)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000869 ret = spci_error(SPCI_NO_MEMORY);
870 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100871 }
872
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000873 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100874 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000875
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100876exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100877 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100878
879 return ret;
880}
881
882/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100883 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
884 * registers the `from` VM to be notified when it becomes available.
885 */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000886static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100887{
888 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
889 to.vm->mailbox.recv == NULL) {
890 /*
891 * Fail if the receiver isn't currently ready to receive data,
892 * setting up for notification if requested.
893 */
894 if (notify) {
895 struct wait_entry *entry =
Andrew Walbranaad8f982019-12-04 10:56:39 +0000896 vm_get_wait_entry(from, to.vm->id);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100897
898 /* Append waiter only if it's not there yet. */
899 if (list_empty(&entry->wait_links)) {
900 list_append(&to.vm->mailbox.waiter_list,
901 &entry->wait_links);
902 }
903 }
904
905 return true;
906 }
907
908 return false;
909}
910
911/**
912 * Notifies the `to` VM about the message currently in its mailbox, possibly
913 * with the help of the primary VM.
914 */
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000915static struct spci_value deliver_msg(struct vm_locked to, spci_vm_id_t from_id,
916 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100917{
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000918 struct spci_value ret = (struct spci_value){.func = SPCI_SUCCESS_32};
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100919 struct spci_value primary_ret = {
920 .func = SPCI_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000921 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100922 };
923
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100924 /* Messages for the primary VM are delivered directly. */
925 if (to.vm->id == HF_PRIMARY_VM_ID) {
926 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000927 * Only tell the primary VM the size and other details if the
928 * message is for it, to avoid leaking data about messages for
929 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100930 */
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000931 primary_ret = spci_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100932
933 to.vm->mailbox.state = MAILBOX_STATE_READ;
934 *next = api_switch_to_primary(current, primary_ret,
935 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000936 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100937 }
938
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000939 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
940
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000941 /* Messages for the TEE are sent on via the dispatcher. */
942 if (to.vm->id == HF_TEE_VM_ID) {
943 struct spci_value call = spci_msg_recv_return(to.vm);
944
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000945 ret = arch_tee_call(call);
946 /*
947 * After the call to the TEE completes it must have finished
948 * reading its RX buffer, so it is ready for another message.
949 */
950 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000951 /*
952 * Don't return to the primary VM in this case, as the TEE is
953 * not (yet) scheduled via SPCI.
954 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000955 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000956 }
957
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100958 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000959 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100960 *next = api_switch_to_primary(current, primary_ret,
961 VCPU_STATE_READY);
962 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000963
964 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100965}
966
967/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100968 * Copies data from the sender's send buffer to the recipient's receive buffer
969 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000970 *
971 * If the recipient's receive buffer is busy, it can optionally register the
972 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100973 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100974struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id,
975 spci_vm_id_t receiver_vm_id, uint32_t size,
976 uint32_t attributes, struct vcpu *current,
977 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100978{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100979 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100980 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +0000981 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100982 const void *from_msg;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100983 struct spci_value ret;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000984 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
985 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100986
Andrew Walbran70bc8622019-10-07 14:15:58 +0100987 /* Ensure sender VM ID corresponds to the current VM. */
988 if (sender_vm_id != from->id) {
989 return spci_error(SPCI_INVALID_PARAMETERS);
990 }
991
992 /* Disallow reflexive requests as this suggests an error in the VM. */
993 if (receiver_vm_id == from->id) {
994 return spci_error(SPCI_INVALID_PARAMETERS);
995 }
996
997 /* Limit the size of transfer. */
998 if (size > SPCI_MSG_PAYLOAD_MAX) {
999 return spci_error(SPCI_INVALID_PARAMETERS);
1000 }
1001
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001002 /* Ensure the receiver VM exists. */
1003 to = vm_find(receiver_vm_id);
1004 if (to == NULL) {
1005 return spci_error(SPCI_INVALID_PARAMETERS);
1006 }
1007
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001008 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001009 * Check that the sender has configured its send buffer. If the tx
1010 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1011 * be safely accessed after releasing the lock since the tx mailbox
1012 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001013 */
1014 sl_lock(&from->lock);
1015 from_msg = from->mailbox.send;
1016 sl_unlock(&from->lock);
1017
1018 if (from_msg == NULL) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001019 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001020 }
1021
Andrew Walbran82d6d152019-12-24 15:02:06 +00001022 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001023
Andrew Walbran82d6d152019-12-24 15:02:06 +00001024 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001025 ret = spci_error(SPCI_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001026 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001027 }
1028
Andrew Walbran82d6d152019-12-24 15:02:06 +00001029 /* Copy data. */
1030 memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg, size);
1031 to->mailbox.recv_size = size;
1032 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001033 to->mailbox.recv_func = SPCI_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001034 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001035
1036out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001037 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001038
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001039 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001040}
1041
1042/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001043 * Checks whether the vCPU's attempt to block for a message has already been
1044 * interrupted or whether it is allowed to block.
1045 */
1046bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1047{
1048 bool interrupted;
1049
1050 sl_lock(&current->lock);
1051
1052 /*
1053 * Don't block if there are enabled and pending interrupts, to match
1054 * behaviour of wait_for_interrupt.
1055 */
1056 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1057
1058 sl_unlock(&current->lock);
1059
1060 return interrupted;
1061}
1062
1063/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001064 * Receives a message from the mailbox. If one isn't available, this function
1065 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001066 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001067 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001068 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001069struct spci_value api_spci_msg_recv(bool block, struct vcpu *current,
1070 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001071{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001072 struct vm *vm = current->vm;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001073 struct spci_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001074
Andrew Scullaa039b32018-10-04 15:02:26 +01001075 /*
1076 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001077 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001078 */
Andrew Scull19503262018-09-20 14:48:39 +01001079 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001080 return spci_error(SPCI_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001081 }
1082
1083 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001084
Andrew Scullaa039b32018-10-04 15:02:26 +01001085 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001086 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1087 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001088 return_code = spci_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001089 goto out;
1090 }
1091
1092 /* No pending message so fail if not allowed to block. */
1093 if (!block) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001094 return_code = spci_error(SPCI_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001095 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001096 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001097
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001098 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001099 * From this point onward this call can only be interrupted or a message
1100 * received. If a message is received the return value will be set at
1101 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001102 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001103 return_code = spci_error(SPCI_INTERRUPTED);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001104 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001105 goto out;
1106 }
1107
Fuad Tabbaed294af2019-12-20 10:43:01 +00001108 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001109 {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001110 struct spci_value run_return = {
1111 .func = SPCI_MSG_WAIT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +00001112 .arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001113 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001114
Andrew Walbranb4816552018-12-05 17:35:42 +00001115 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001116 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001117 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001118out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001119 sl_unlock(&vm->lock);
1120
Jose Marinho3e2442f2019-03-12 13:30:37 +00001121 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001122}
1123
1124/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001125 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1126 * by this function, the caller must have called api_mailbox_send before with
1127 * the notify argument set to true, and this call must have failed because the
1128 * mailbox was not available.
1129 *
1130 * It should be called repeatedly to retrieve a list of VMs.
1131 *
1132 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1133 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001134 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001135int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001136{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001137 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001138 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001139 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001140
1141 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001142 if (list_empty(&vm->mailbox.ready_list)) {
1143 ret = -1;
1144 goto exit;
1145 }
1146
1147 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1148 ready_links);
1149 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001150 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001151
1152exit:
1153 sl_unlock(&vm->lock);
1154 return ret;
1155}
1156
1157/**
1158 * Retrieves the next VM waiting to be notified that the mailbox of the
1159 * specified VM became writable. Only primary VMs are allowed to call this.
1160 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001161 * Returns -1 on failure or if there are no waiters; the VM id of the next
1162 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001163 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001164int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001165{
1166 struct vm *vm;
1167 struct vm_locked locked;
1168 struct wait_entry *entry;
1169 struct vm *waiting_vm;
1170
1171 /* Only primary VMs are allowed to call this function. */
1172 if (current->vm->id != HF_PRIMARY_VM_ID) {
1173 return -1;
1174 }
1175
Andrew Walbran42347a92019-05-09 13:59:03 +01001176 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001177 if (vm == NULL) {
1178 return -1;
1179 }
1180
Fuad Tabbaed294af2019-12-20 10:43:01 +00001181 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001182 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001183 entry = api_fetch_waiter(locked);
1184 vm_unlock(&locked);
1185
1186 if (entry == NULL) {
1187 return -1;
1188 }
1189
1190 /* Enqueue notification to waiting VM. */
1191 waiting_vm = entry->waiting_vm;
1192
1193 sl_lock(&waiting_vm->lock);
1194 if (list_empty(&entry->ready_links)) {
1195 list_append(&waiting_vm->mailbox.ready_list,
1196 &entry->ready_links);
1197 }
1198 sl_unlock(&waiting_vm->lock);
1199
1200 return waiting_vm->id;
1201}
1202
1203/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001204 * Releases the caller's mailbox so that a new message can be received. The
1205 * caller must have copied out all data they wish to preserve as new messages
1206 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001207 *
1208 * Returns:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001209 * - SPCI_ERROR SPCI_DENIED on failure, if the mailbox hasn't been read.
1210 * - SPCI_SUCCESS on success if no further action is needed.
1211 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
1212 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001213 * hf_mailbox_waiter_get.
1214 */
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001215struct spci_value api_spci_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001216{
1217 struct vm *vm = current->vm;
1218 struct vm_locked locked;
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001219 struct spci_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001220
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001221 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001222 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001223 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001224 case MAILBOX_STATE_RECEIVED:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001225 ret = spci_error(SPCI_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001226 break;
1227
Andrew Sculld6ee1102019-04-05 22:12:42 +01001228 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001229 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001230 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001231 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001232 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001233 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001234
1235 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001236}
Andrew Walbran318f5732018-11-20 16:23:42 +00001237
1238/**
1239 * Enables or disables a given interrupt ID for the calling vCPU.
1240 *
1241 * Returns 0 on success, or -1 if the intid is invalid.
1242 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001243int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001244{
1245 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001246 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001247
Andrew Walbran318f5732018-11-20 16:23:42 +00001248 if (intid >= HF_NUM_INTIDS) {
1249 return -1;
1250 }
1251
1252 sl_lock(&current->lock);
1253 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001254 /*
1255 * If it is pending and was not enabled before, increment the
1256 * count.
1257 */
1258 if (current->interrupts.interrupt_pending[intid_index] &
1259 ~current->interrupts.interrupt_enabled[intid_index] &
1260 intid_mask) {
1261 current->interrupts.enabled_and_pending_count++;
1262 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001263 current->interrupts.interrupt_enabled[intid_index] |=
1264 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001265 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001266 /*
1267 * If it is pending and was enabled before, decrement the count.
1268 */
1269 if (current->interrupts.interrupt_pending[intid_index] &
1270 current->interrupts.interrupt_enabled[intid_index] &
1271 intid_mask) {
1272 current->interrupts.enabled_and_pending_count--;
1273 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001274 current->interrupts.interrupt_enabled[intid_index] &=
1275 ~intid_mask;
1276 }
1277
1278 sl_unlock(&current->lock);
1279 return 0;
1280}
1281
1282/**
1283 * Returns the ID of the next pending interrupt for the calling vCPU, and
1284 * acknowledges it (i.e. marks it as no longer pending). Returns
1285 * HF_INVALID_INTID if there are no pending interrupts.
1286 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001287uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001288{
1289 uint8_t i;
1290 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001291
1292 /*
1293 * Find the first enabled and pending interrupt ID, return it, and
1294 * deactivate it.
1295 */
1296 sl_lock(&current->lock);
1297 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1298 uint32_t enabled_and_pending =
1299 current->interrupts.interrupt_enabled[i] &
1300 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001301
Andrew Walbran318f5732018-11-20 16:23:42 +00001302 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001303 uint8_t bit_index = ctz(enabled_and_pending);
1304 /*
1305 * Mark it as no longer pending and decrement the count.
1306 */
1307 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001308 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001309 current->interrupts.enabled_and_pending_count--;
1310 first_interrupt =
1311 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001312 break;
1313 }
1314 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001315
1316 sl_unlock(&current->lock);
1317 return first_interrupt;
1318}
1319
1320/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001321 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001322 * given VM and vCPU.
1323 */
1324static inline bool is_injection_allowed(uint32_t target_vm_id,
1325 struct vcpu *current)
1326{
1327 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001328
Andrew Walbran318f5732018-11-20 16:23:42 +00001329 /*
1330 * The primary VM is allowed to inject interrupts into any VM. Secondary
1331 * VMs are only allowed to inject interrupts into their own vCPUs.
1332 */
1333 return current_vm_id == HF_PRIMARY_VM_ID ||
1334 current_vm_id == target_vm_id;
1335}
1336
1337/**
1338 * Injects a virtual interrupt of the given ID into the given target vCPU.
1339 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1340 * when the vCPU is next run, which is up to the scheduler.
1341 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001342 * Returns:
1343 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1344 * ID is invalid, or the current VM is not allowed to inject interrupts to
1345 * the target VM.
1346 * - 0 on success if no further action is needed.
1347 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1348 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001349 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001350int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001351 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001352 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001353{
Andrew Walbran318f5732018-11-20 16:23:42 +00001354 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001355 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001356
1357 if (intid >= HF_NUM_INTIDS) {
1358 return -1;
1359 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001360
Andrew Walbran318f5732018-11-20 16:23:42 +00001361 if (target_vm == NULL) {
1362 return -1;
1363 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001364
Andrew Walbran318f5732018-11-20 16:23:42 +00001365 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001366 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001367 return -1;
1368 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001369
Andrew Walbran318f5732018-11-20 16:23:42 +00001370 if (!is_injection_allowed(target_vm_id, current)) {
1371 return -1;
1372 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001373
Andrew Walbrane1310df2019-04-29 17:28:28 +01001374 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001375
Andrew Walbran17eebf92020-02-05 16:35:49 +00001376 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1377 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1378 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001379 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001380}
Andrew Scull6386f252018-12-06 13:29:10 +00001381
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001382/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001383struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001384{
1385 /*
1386 * Ensure that both major and minor revision representation occupies at
1387 * most 15 bits.
1388 */
1389 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1390 "Major revision representation take more than 15 bits.");
1391 static_assert(0x10000 > SPCI_VERSION_MINOR,
1392 "Minor revision representation take more than 16 bits.");
1393
Andrew Walbran7f920af2019-09-03 17:09:30 +01001394 struct spci_value ret = {
1395 .func = SPCI_SUCCESS_32,
Andrew Walbran455c53a2019-10-10 13:56:19 +01001396 .arg2 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
Andrew Walbran7f920af2019-09-03 17:09:30 +01001397 SPCI_VERSION_MINOR};
1398 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001399}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001400
1401int64_t api_debug_log(char c, struct vcpu *current)
1402{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001403 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001404 struct vm *vm = current->vm;
1405 struct vm_locked vm_locked = vm_lock(vm);
1406
Andrew Sculld54e1be2019-08-20 11:09:42 +01001407 if (c == '\n' || c == '\0') {
1408 flush = true;
1409 } else {
1410 vm->log_buffer[vm->log_buffer_length++] = c;
1411 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1412 }
1413
1414 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001415 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1416 vm->log_buffer_length);
1417 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001418 }
1419
1420 vm_unlock(&vm_locked);
1421
1422 return 0;
1423}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001424
1425/**
1426 * Discovery function returning information about the implementation of optional
1427 * SPCI interfaces.
1428 */
1429struct spci_value api_spci_features(uint32_t function_id)
1430{
1431 switch (function_id) {
1432 case SPCI_ERROR_32:
1433 case SPCI_SUCCESS_32:
1434 case SPCI_ID_GET_32:
1435 case SPCI_YIELD_32:
1436 case SPCI_VERSION_32:
1437 case SPCI_FEATURES_32:
1438 case SPCI_MSG_SEND_32:
1439 case SPCI_MSG_POLL_32:
1440 case SPCI_MSG_WAIT_32:
1441 return (struct spci_value){.func = SPCI_SUCCESS_32};
1442 default:
1443 return spci_error(SPCI_NOT_SUPPORTED);
1444 }
1445}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001446
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001447struct spci_value api_spci_mem_send(uint32_t share_func, ipaddr_t address,
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001448 uint32_t page_count,
Andrew Walbran0f6cede2020-01-10 15:38:09 +00001449 uint32_t fragment_length, uint32_t length,
1450 uint32_t cookie, struct vcpu *current,
1451 struct vcpu **next)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001452{
1453 struct vm *from = current->vm;
1454 struct vm *to;
1455 const void *from_msg;
1456 uint32_t message_buffer_size;
1457 struct spci_memory_region *memory_region;
1458 struct two_vm_locked vm_to_from_lock;
1459 struct spci_value ret;
1460
1461 if (ipa_addr(address) != 0 || page_count != 0) {
1462 /*
1463 * Hafnium only supports passing the descriptor in the TX
1464 * mailbox.
1465 */
1466 return spci_error(SPCI_INVALID_PARAMETERS);
1467 }
1468
Andrew Walbran0f6cede2020-01-10 15:38:09 +00001469 if ((cookie == 0) != (fragment_length == length)) {
1470 /* Cookie is required iff there are multiple fragments. */
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001471 return spci_error(SPCI_INVALID_PARAMETERS);
1472 }
1473
1474 /*
1475 * Check that the sender has configured its send buffer. If the TX
1476 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1477 * be safely accessed after releasing the lock since the TX mailbox
1478 * address can only be configured once.
1479 */
1480 sl_lock(&from->lock);
1481 from_msg = from->mailbox.send;
1482 sl_unlock(&from->lock);
1483
1484 if (from_msg == NULL) {
1485 return spci_error(SPCI_INVALID_PARAMETERS);
1486 }
1487
1488 /*
1489 * Copy the memory region descriptor to an internal buffer, so that the
1490 * sender can't change it underneath us.
1491 */
1492 memory_region =
Mahesh Bireddy8ca57862020-01-07 13:43:21 +05301493 (struct spci_memory_region *)cpu_get_buffer(current->cpu);
1494 message_buffer_size = cpu_get_buffer_size(current->cpu);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001495 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1496 return spci_error(SPCI_INVALID_PARAMETERS);
1497 }
1498 memcpy_s(memory_region, message_buffer_size, from_msg, length);
1499
1500 /* The sender must match the caller. */
1501 if (memory_region->sender != from->id) {
1502 return spci_error(SPCI_INVALID_PARAMETERS);
1503 }
1504
1505 if (memory_region->attribute_count != 1) {
1506 /* Hafnium doesn't support multi-way memory sharing for now. */
1507 return spci_error(SPCI_NOT_SUPPORTED);
1508 }
1509
1510 /*
1511 * Ensure that the receiver VM exists and isn't the same as the sender.
1512 */
1513 to = vm_find(memory_region->attributes[0].receiver);
1514 if (to == NULL || to == from) {
1515 return spci_error(SPCI_INVALID_PARAMETERS);
1516 }
1517
1518 vm_to_from_lock = vm_lock_both(to, from);
1519
1520 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
1521 ret = spci_error(SPCI_BUSY);
1522 goto out;
1523 }
1524
Andrew Walbran475c1452020-02-07 13:22:22 +00001525 ret = spci_memory_send(vm_to_from_lock.vm1, vm_to_from_lock.vm2,
1526 memory_region, length, share_func,
1527 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001528
1529 if (ret.func == SPCI_SUCCESS_32) {
Andrew Walbran475c1452020-02-07 13:22:22 +00001530 /* Copy data to the destination Rx. */
1531 /*
1532 * TODO: Translate the <from> IPA addresses to <to> IPA
1533 * addresses. Currently we assume identity mapping of the stage
1534 * 2 translation. Removing this assumption relies on a mechanism
1535 * to handle scenarios where the memory region fits in the
1536 * source Tx buffer but cannot fit in the destination Rx buffer.
1537 * This mechanism will be defined at the spec level.
1538 */
1539 memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, memory_region,
1540 length);
1541 to->mailbox.recv_size = length;
1542 to->mailbox.recv_sender = from->id;
1543 to->mailbox.recv_func = share_func;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001544 ret = deliver_msg(vm_to_from_lock.vm1, from->id, current, next);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001545 }
1546
1547out:
1548 vm_unlock(&vm_to_from_lock.vm1);
1549 vm_unlock(&vm_to_from_lock.vm2);
1550
1551 return ret;
1552}