blob: 143a624ad78bac5154cec0c5371c2b3ae1005ce7 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010018
Andrew Walbran318f5732018-11-20 16:23:42 +000019#include "hf/arch/cpu.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000020#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000021
Andrew Scull877ae4b2019-07-02 12:52:33 +010022#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000023#include "hf/dlog.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010025#include "hf/plat/console.h"
Jose Marinho40d55f32019-07-01 15:41:54 +010026#include "hf/spci_internal.h"
Andrew Scull6386f252018-12-06 13:29:10 +000027#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010028#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010029#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
31
Andrew Scullf35a5c92018-08-07 18:09:46 +010032#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000033#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010034
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000035/*
36 * To eliminate the risk of deadlocks, we define a partial order for the
37 * acquisition of locks held concurrently by the same physical CPU. Our current
38 * ordering requirements are as follows:
39 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010040 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000041 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010042 * Locks of the same kind require the lock of lowest address to be locked first,
43 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000044 */
45
Andrew Scullaa039b32018-10-04 15:02:26 +010046static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010047 "Currently, a page is mapped for the send and receive buffers so "
48 "the maximum request is the size of a page.");
49
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000050static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000051
52/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000053 * Initialises the API page pool by taking ownership of the contents of the
54 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000055 */
56void api_init(struct mpool *ppool)
57{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000058 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000059}
60
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010061/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000062 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010063 *
64 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranf0c314d2019-10-02 14:24:26 +010065 * to cause SPCI_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010066 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010067static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010068 struct spci_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000069 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070{
Andrew Walbran42347a92019-05-09 13:59:03 +010071 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010072 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010073
Andrew Walbran508e63c2018-12-20 17:02:37 +000074 /*
75 * If the secondary is blocked but has a timer running, sleep until the
76 * timer fires rather than indefinitely.
77 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010078 switch (primary_ret.func) {
79 case HF_SPCI_RUN_WAIT_FOR_INTERRUPT:
80 case SPCI_MSG_WAIT_32: {
81 if (arch_timer_enabled_current()) {
82 uint64_t remaining_ns =
83 arch_timer_remaining_ns_current();
84
85 if (remaining_ns == 0) {
86 /*
87 * Timer is pending, so the current vCPU should
88 * be run again right away.
89 */
90 primary_ret.func = SPCI_INTERRUPT_32;
91 /*
92 * primary_ret.arg1 should already be set to the
93 * current VM ID and vCPU ID.
94 */
95 primary_ret.arg2 = 0;
96 } else {
97 primary_ret.arg2 = remaining_ns;
98 }
99 } else {
100 primary_ret.arg2 = SPCI_SLEEP_INDEFINITE;
101 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000102 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100103 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000104
105 default:
106 /* Do nothing. */
107 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000108 }
109
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100110 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100111 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100112
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000113 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000114 sl_lock(&current->lock);
115 current->state = secondary_state;
116 sl_unlock(&current->lock);
117
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100118 return next;
119}
120
121/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000122 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000123 */
124struct vcpu *api_preempt(struct vcpu *current)
125{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100126 struct spci_value ret = {
127 .func = SPCI_INTERRUPT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000128 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000129 };
130
Andrew Sculld6ee1102019-04-05 22:12:42 +0100131 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000132}
133
134/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000135 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000136 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100137 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100138struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100139{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100140 struct spci_value ret = {
141 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000142 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100143 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000144
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000145 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100146 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100147}
148
149/**
Andrew Walbran33645652019-04-15 12:29:31 +0100150 * Puts the current vCPU in off mode, and returns to the primary VM.
151 */
152struct vcpu *api_vcpu_off(struct vcpu *current)
153{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100154 struct spci_value ret = {
155 .func = HF_SPCI_RUN_WAIT_FOR_INTERRUPT,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000156 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100157 };
158
159 /*
160 * Disable the timer, so the scheduler doesn't get told to call back
161 * based on it.
162 */
163 arch_timer_disable_current();
164
165 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
166}
167
168/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000169 * Returns to the primary VM to allow this CPU to be used for other tasks as the
170 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100171 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000172 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100173void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000174{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100175 struct spci_value primary_ret = {
176 .func = SPCI_YIELD_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000177 .arg1 = spci_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull66d62bf2019-02-01 13:54:10 +0000178 };
179
180 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000181 /* NOOP on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100182 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000183 }
184
Andrew Walbran16075b62019-09-03 17:11:07 +0100185 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000186}
187
188/**
Andrew Walbran33645652019-04-15 12:29:31 +0100189 * Switches to the primary so that it can switch to the target, or kick it if it
190 * is already running on a different physical CPU.
191 */
192struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
193{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100194 struct spci_value ret = {
195 .func = HF_SPCI_RUN_WAKE_UP,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000196 .arg1 = spci_vm_vcpu(target_vcpu->vm->id,
197 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100198 };
199 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
200}
201
202/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000203 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000204 */
205struct vcpu *api_abort(struct vcpu *current)
206{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100207 struct spci_value ret = spci_error(SPCI_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000208
209 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
210
211 if (current->vm->id == HF_PRIMARY_VM_ID) {
212 /* TODO: what to do when the primary aborts? */
213 for (;;) {
214 /* Do nothing. */
215 }
216 }
217
218 atomic_store_explicit(&current->vm->aborting, true,
219 memory_order_relaxed);
220
221 /* TODO: free resources once all vCPUs abort. */
222
Andrew Sculld6ee1102019-04-05 22:12:42 +0100223 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000224}
225
226/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000227 * Returns the ID of the VM.
228 */
Andrew Walbrand230f662019-10-07 18:03:36 +0100229struct spci_value api_spci_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000230{
Andrew Walbrand230f662019-10-07 18:03:36 +0100231 return (struct spci_value){.func = SPCI_SUCCESS_32,
232 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000233}
234
235/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100236 * Returns the number of VMs configured to run.
237 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100238spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100239{
Andrew Scull19503262018-09-20 14:48:39 +0100240 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100241}
242
243/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100244 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
245 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100246 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100247spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
248 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100249{
Andrew Scull19503262018-09-20 14:48:39 +0100250 struct vm *vm;
251
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000252 /* Only the primary VM needs to know about vCPUs for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100253 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100254 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100255 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100256
Andrew Walbran42347a92019-05-09 13:59:03 +0100257 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100258 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100259 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100260 }
261
262 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100263}
264
265/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000266 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000267 * function to indicate that register state for the given vCPU has been saved
268 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000269 */
270void api_regs_state_saved(struct vcpu *vcpu)
271{
272 sl_lock(&vcpu->lock);
273 vcpu->regs_available = true;
274 sl_unlock(&vcpu->lock);
275}
276
277/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000278 * Retrieves the next waiter and removes it from the wait list if the VM's
279 * mailbox is in a writable state.
280 */
281static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
282{
283 struct wait_entry *entry;
284 struct vm *vm = locked_vm.vm;
285
Andrew Sculld6ee1102019-04-05 22:12:42 +0100286 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000287 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
288 /* The mailbox is not writable or there are no waiters. */
289 return NULL;
290 }
291
292 /* Remove waiter from the wait list. */
293 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
294 wait_links);
295 list_remove(&entry->wait_links);
296 return entry;
297}
298
299/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000300 * Assuming that the arguments have already been checked by the caller, injects
301 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
302 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
303 * is next run, which is up to the scheduler.
304 *
305 * Returns:
306 * - 0 on success if no further action is needed.
307 * - 1 if it was called by the primary VM and the primary VM now needs to wake
308 * up or kick the target vCPU.
309 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100310static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000311 uint32_t intid, struct vcpu *current,
312 struct vcpu **next)
313{
314 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100315 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000316 int64_t ret = 0;
317
318 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000319
320 /*
321 * We only need to change state and (maybe) trigger a virtual IRQ if it
322 * is enabled and was not previously pending. Otherwise we can skip
323 * everything except setting the pending bit.
324 *
325 * If you change this logic make sure to update the need_vm_lock logic
326 * above to match.
327 */
328 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
329 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
330 intid_mask)) {
331 goto out;
332 }
333
334 /* Increment the count. */
335 target_vcpu->interrupts.enabled_and_pending_count++;
336
337 /*
338 * Only need to update state if there was not already an
339 * interrupt enabled and pending.
340 */
341 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
342 goto out;
343 }
344
Andrew Walbran508e63c2018-12-20 17:02:37 +0000345 if (current->vm->id == HF_PRIMARY_VM_ID) {
346 /*
347 * If the call came from the primary VM, let it know that it
348 * should run or kick the target vCPU.
349 */
350 ret = 1;
351 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100352 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000353 }
354
355out:
356 /* Either way, make it pending. */
357 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
358
359 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000360
361 return ret;
362}
363
364/**
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100365 * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL
366 * or SPCI_MSG_WAIT call.
367 */
368static struct spci_value spci_msg_recv_return(const struct vm *receiver)
369{
370 return (struct spci_value){
371 .func = SPCI_MSG_SEND_32,
Andrew Walbran70bc8622019-10-07 14:15:58 +0100372 .arg1 = (receiver->mailbox.recv_sender << 16) | receiver->id,
373 .arg3 = receiver->mailbox.recv_size,
374 .arg4 = receiver->mailbox.recv_attributes};
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100375}
376
377/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000378 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000379 * value needs to be forced onto the vCPU.
380 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000381static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100382 struct spci_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000383{
Andrew Scullb06d1752019-02-04 10:15:48 +0000384 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000385 bool ret;
386
Andrew Scullb06d1752019-02-04 10:15:48 +0000387 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000388 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000389 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100390 * The VM lock is not needed in the common case so it must only be taken
391 * when it is going to be needed. This ensures there are no inter-vCPU
392 * dependencies in the common run case meaning the sensitive context
393 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000394 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000395 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000396
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000397 /* The VM needs to be locked to deliver mailbox messages. */
398 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
399 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000400 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000401 sl_lock(&vcpu->vm->lock);
402 sl_lock(&vcpu->lock);
403 }
404
405 /*
406 * If the vCPU is already running somewhere then we can't run it here
407 * simultaneously. While it is actually running then the state should be
408 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
409 * stops running but while Hafnium is in the process of switching back
410 * to the primary there will be a brief period while the state has been
411 * updated but `regs_available` is still false (until
412 * `api_regs_state_saved` is called). We can't start running it again
413 * until this has finished, so count this state as still running for the
414 * purposes of this check.
415 */
416 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
417 /*
418 * vCPU is running on another pCPU.
419 *
420 * It's okay not to return the sleep duration here because the
421 * other physical CPU that is currently running this vCPU will
422 * return the sleep duration if needed.
423 */
424 *run_ret = spci_error(SPCI_BUSY);
425 ret = false;
426 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000427 }
Andrew Scull9726c252019-01-23 13:44:19 +0000428
429 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100430 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000431 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
432 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100433 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000434 }
435 ret = false;
436 goto out;
437 }
438
Andrew Walbran508e63c2018-12-20 17:02:37 +0000439 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100440 case VCPU_STATE_RUNNING:
441 case VCPU_STATE_OFF:
442 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000443 ret = false;
444 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000445
Andrew Sculld6ee1102019-04-05 22:12:42 +0100446 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000447 /*
448 * A pending message allows the vCPU to run so the message can
449 * be delivered directly.
450 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100451 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100452 arch_regs_set_retval(&vcpu->regs,
453 spci_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100454 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000455 break;
456 }
457 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100458 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000459 /* Allow virtual interrupts to be delivered. */
460 if (vcpu->interrupts.enabled_and_pending_count > 0) {
461 break;
462 }
463
Andrew Walbran508e63c2018-12-20 17:02:37 +0000464 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000465 uint64_t timer_remaining_ns =
466 arch_timer_remaining_ns(&vcpu->regs);
467
468 /*
469 * The timer expired so allow the interrupt to be
470 * delivered.
471 */
472 if (timer_remaining_ns == 0) {
473 break;
474 }
475
476 /*
477 * The vCPU is not ready to run, return the appropriate
478 * code to the primary which called vcpu_run.
479 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100480 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100481 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100482 ? SPCI_MSG_WAIT_32
483 : HF_SPCI_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000484 run_ret->arg1 =
485 spci_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000486 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000487 }
488
489 ret = false;
490 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000491
Andrew Sculld6ee1102019-04-05 22:12:42 +0100492 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000493 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000494 }
495
Andrew Scullb06d1752019-02-04 10:15:48 +0000496 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000497 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100498 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000499
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000500 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000501 * Mark the registers as unavailable now that we're about to reflect
502 * them onto the real registers. This will also prevent another physical
503 * CPU from trying to read these registers.
504 */
505 vcpu->regs_available = false;
506
507 ret = true;
508
509out:
510 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000511 if (need_vm_lock) {
512 sl_unlock(&vcpu->vm->lock);
513 }
514
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000515 return ret;
516}
517
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100518struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx,
519 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100520{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100521 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100522 struct vcpu *vcpu;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100523 struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100524
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000525 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100526 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100527 ret.arg2 = SPCI_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100528 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100529 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100530
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000531 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100532 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100533 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100534 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100535
Andrew Scull19503262018-09-20 14:48:39 +0100536 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100537 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100538 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100539 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100540 }
541
Fuad Tabbaed294af2019-12-20 10:43:01 +0000542 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100543 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100544 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100545 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100546
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000547 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100548 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000549 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000550 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100551 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000552
Andrew Walbran508e63c2018-12-20 17:02:37 +0000553 /*
554 * Inject timer interrupt if timer has expired. It's safe to access
555 * vcpu->regs here because api_vcpu_prepare_run already made sure that
556 * regs_available was true (and then set it to false) before returning
557 * true.
558 */
559 if (arch_timer_pending(&vcpu->regs)) {
560 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100561 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
562 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000563
564 /*
565 * Set the mask bit so the hardware interrupt doesn't fire
566 * again. Ideally we wouldn't do this because it affects what
567 * the secondary vCPU sees, but if we don't then we end up with
568 * a loop of the interrupt firing each time we try to return to
569 * the secondary vCPU.
570 */
571 arch_timer_mask(&vcpu->regs);
572 }
573
Fuad Tabbaed294af2019-12-20 10:43:01 +0000574 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000575 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000576
Andrew Scull33fecd32019-01-08 14:48:27 +0000577 /*
578 * Set a placeholder return code to the scheduler. This will be
579 * overwritten when the switch back to the primary occurs.
580 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100581 ret.func = SPCI_INTERRUPT_32;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000582 ret.arg1 = spci_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100583 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000584
Andrew Scull6d2db332018-10-10 15:28:17 +0100585out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100586 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100587}
588
589/**
Andrew Scull81e85092018-12-12 12:56:20 +0000590 * Check that the mode indicates memory that is valid, owned and exclusive.
591 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100592static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000593{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100594 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
595 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000596}
597
598/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +0000599 * Determines the value to be returned by api_vm_configure and spci_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000600 * after they've succeeded. If a secondary VM is running and there are waiters,
601 * it also switches back to the primary VM for it to wake waiters up.
602 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000603static struct spci_value api_waiter_result(struct vm_locked locked_vm,
604 struct vcpu *current,
605 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000606{
607 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000608
609 if (list_empty(&vm->mailbox.waiter_list)) {
610 /* No waiters, nothing else to do. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000611 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000612 }
613
614 if (vm->id == HF_PRIMARY_VM_ID) {
615 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000616 return (struct spci_value){.func = SPCI_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000617 }
618
619 /*
620 * Switch back to the primary VM, informing it that there are waiters
621 * that need to be notified.
622 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000623 *next = api_switch_to_primary(
624 current, (struct spci_value){.func = SPCI_RX_RELEASE_32},
625 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000626
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000627 return (struct spci_value){.func = SPCI_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000628}
629
630/**
Andrew Sculle1322792019-07-01 17:46:10 +0100631 * Configures the hypervisor's stage-1 view of the send and receive pages. The
632 * stage-1 page tables must be locked so memory cannot be taken by another core
633 * which could result in this transaction being unable to roll back in the case
634 * of an error.
635 */
636static bool api_vm_configure_stage1(struct vm_locked vm_locked,
637 paddr_t pa_send_begin, paddr_t pa_send_end,
638 paddr_t pa_recv_begin, paddr_t pa_recv_end,
639 struct mpool *local_page_pool)
640{
641 bool ret;
642 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
643
644 /* Map the send page as read-only in the hypervisor address space. */
645 vm_locked.vm->mailbox.send =
646 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
647 MM_MODE_R, local_page_pool);
648 if (!vm_locked.vm->mailbox.send) {
649 /* TODO: partial defrag of failed range. */
650 /* Recover any memory consumed in failed mapping. */
651 mm_defrag(mm_stage1_locked, local_page_pool);
652 goto fail;
653 }
654
655 /*
656 * Map the receive page as writable in the hypervisor address space. On
657 * failure, unmap the send page before returning.
658 */
659 vm_locked.vm->mailbox.recv =
660 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
661 MM_MODE_W, local_page_pool);
662 if (!vm_locked.vm->mailbox.recv) {
663 /* TODO: partial defrag of failed range. */
664 /* Recover any memory consumed in failed mapping. */
665 mm_defrag(mm_stage1_locked, local_page_pool);
666 goto fail_undo_send;
667 }
668
669 ret = true;
670 goto out;
671
672 /*
673 * The following mappings will not require more memory than is available
674 * in the local pool.
675 */
676fail_undo_send:
677 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100678 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
679 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100680
681fail:
682 ret = false;
683
684out:
685 mm_unlock_stage1(&mm_stage1_locked);
686
687 return ret;
688}
689
690/**
691 * Configures the send and receive pages in the VM stage-2 and hypervisor
692 * stage-1 page tables. Locking of the page tables combined with a local memory
693 * pool ensures there will always be enough memory to recover from any errors
694 * that arise.
695 */
696static bool api_vm_configure_pages(struct vm_locked vm_locked,
697 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100698 uint32_t orig_send_mode,
699 paddr_t pa_recv_begin, paddr_t pa_recv_end,
700 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100701{
702 bool ret;
703 struct mpool local_page_pool;
704
705 /*
706 * Create a local pool so any freed memory can't be used by another
707 * thread. This is to ensure the original mapping can be restored if any
708 * stage of the process fails.
709 */
710 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
711
712 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000713 if (!vm_identity_map(
714 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100715 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000716 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100717 goto fail;
718 }
719
Andrew Scull3c257452019-11-26 13:32:50 +0000720 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
721 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
722 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100723 /* TODO: partial defrag of failed range. */
724 /* Recover any memory consumed in failed mapping. */
725 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
726 goto fail_undo_send;
727 }
728
729 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
730 pa_recv_begin, pa_recv_end,
731 &local_page_pool)) {
732 goto fail_undo_send_and_recv;
733 }
734
735 ret = true;
736 goto out;
737
738 /*
739 * The following mappings will not require more memory than is available
740 * in the local pool.
741 */
742fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000743 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
744 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100745
746fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000747 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
748 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100749
750fail:
751 ret = false;
752
753out:
754 mpool_fini(&local_page_pool);
755
756 return ret;
757}
758
759/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100760 * Configures the VM to send/receive data through the specified pages. The pages
761 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000762 *
763 * Returns:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000764 * - SPCI_ERROR SPCI_INVALID_PARAMETERS if the given addresses are not properly
765 * aligned or are the same.
766 * - SPCI_ERROR SPCI_NO_MEMORY if the hypervisor was unable to map the buffers
767 * due to insuffient page table memory.
768 * - SPCI_ERROR SPCI_DENIED if the pages are already mapped or are not owned by
769 * the caller.
770 * - SPCI_SUCCESS on success if no further action is needed.
771 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
772 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100773 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000774struct spci_value api_spci_rxtx_map(ipaddr_t send, ipaddr_t recv,
775 uint32_t page_count, struct vcpu *current,
776 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100777{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100778 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100779 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100780 paddr_t pa_send_begin;
781 paddr_t pa_send_end;
782 paddr_t pa_recv_begin;
783 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100784 uint32_t orig_send_mode;
785 uint32_t orig_recv_mode;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000786 struct spci_value ret;
787
788 /* Hafnium only supports a fixed size of RX/TX buffers. */
789 if (page_count != HF_MAILBOX_SIZE / SPCI_PAGE_SIZE) {
790 return spci_error(SPCI_INVALID_PARAMETERS);
791 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100792
793 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000794 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
795 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000796 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100797 }
798
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000799 /* Convert to physical addresses. */
800 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000801 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000802
803 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000804 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000805
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100806 /* Fail if the same page is used for the send and receive pages. */
807 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000808 return spci_error(SPCI_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000809 }
810
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100811 /*
812 * The hypervisor's memory map must be locked for the duration of this
813 * operation to ensure there will be sufficient memory to recover from
814 * any failures.
815 *
816 * TODO: the scope of the can be reduced but will require restructuring
817 * to keep a single unlock point.
818 */
Andrew Sculle1322792019-07-01 17:46:10 +0100819 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000820
821 /* We only allow these to be setup once. */
822 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000823 ret = spci_error(SPCI_DENIED);
824 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000825 }
826
827 /*
828 * Ensure the pages are valid, owned and exclusive to the VM and that
829 * the VM has the required access to the memory.
830 */
831 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
832 &orig_send_mode) ||
833 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
834 (orig_send_mode & MM_MODE_R) == 0 ||
835 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000836 ret = spci_error(SPCI_DENIED);
837 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000838 }
839
840 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
841 &orig_recv_mode) ||
842 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
843 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000844 ret = spci_error(SPCI_DENIED);
845 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000846 }
847
Andrew Sculle1322792019-07-01 17:46:10 +0100848 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
849 orig_send_mode, pa_recv_begin, pa_recv_end,
850 orig_recv_mode)) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000851 ret = spci_error(SPCI_NO_MEMORY);
852 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100853 }
854
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000855 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100856 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000857
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100858exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100859 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100860
861 return ret;
862}
863
864/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100865 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
866 * registers the `from` VM to be notified when it becomes available.
867 */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000868static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100869{
870 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
871 to.vm->mailbox.recv == NULL) {
872 /*
873 * Fail if the receiver isn't currently ready to receive data,
874 * setting up for notification if requested.
875 */
876 if (notify) {
877 struct wait_entry *entry =
Andrew Walbranaad8f982019-12-04 10:56:39 +0000878 vm_get_wait_entry(from, to.vm->id);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100879
880 /* Append waiter only if it's not there yet. */
881 if (list_empty(&entry->wait_links)) {
882 list_append(&to.vm->mailbox.waiter_list,
883 &entry->wait_links);
884 }
885 }
886
887 return true;
888 }
889
890 return false;
891}
892
893/**
894 * Notifies the `to` VM about the message currently in its mailbox, possibly
895 * with the help of the primary VM.
896 */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000897static void deliver_msg(struct vm_locked to, spci_vm_id_t from_id,
Andrew Walbran7cff8fc2019-11-21 19:05:30 +0000898 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100899{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100900 struct spci_value primary_ret = {
901 .func = SPCI_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000902 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100903 };
904
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100905 /* Messages for the primary VM are delivered directly. */
906 if (to.vm->id == HF_PRIMARY_VM_ID) {
907 /*
908 * Only tell the primary VM the size if the message is for it,
909 * to avoid leaking data about messages for other VMs.
910 */
Andrew Walbran7cff8fc2019-11-21 19:05:30 +0000911 primary_ret.arg3 = to.vm->mailbox.recv_size;
912 primary_ret.arg4 = to.vm->mailbox.recv_attributes;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100913
914 to.vm->mailbox.state = MAILBOX_STATE_READ;
915 *next = api_switch_to_primary(current, primary_ret,
916 VCPU_STATE_READY);
917 return;
918 }
919
920 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
921
922 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000923 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100924 *next = api_switch_to_primary(current, primary_ret,
925 VCPU_STATE_READY);
926 }
927}
928
929/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100930 * Copies data from the sender's send buffer to the recipient's receive buffer
931 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000932 *
933 * If the recipient's receive buffer is busy, it can optionally register the
934 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100935 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100936struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id,
937 spci_vm_id_t receiver_vm_id, uint32_t size,
938 uint32_t attributes, struct vcpu *current,
939 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100940{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100941 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100942 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +0000943 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100944 const void *from_msg;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100945 struct spci_value ret;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000946 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
947 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100948
Andrew Walbran70bc8622019-10-07 14:15:58 +0100949 /* Ensure sender VM ID corresponds to the current VM. */
950 if (sender_vm_id != from->id) {
951 return spci_error(SPCI_INVALID_PARAMETERS);
952 }
953
954 /* Disallow reflexive requests as this suggests an error in the VM. */
955 if (receiver_vm_id == from->id) {
956 return spci_error(SPCI_INVALID_PARAMETERS);
957 }
958
959 /* Limit the size of transfer. */
960 if (size > SPCI_MSG_PAYLOAD_MAX) {
961 return spci_error(SPCI_INVALID_PARAMETERS);
962 }
963
Andrew Walbran0b60c4f2019-12-10 17:05:29 +0000964 /* Ensure the receiver VM exists. */
965 to = vm_find(receiver_vm_id);
966 if (to == NULL) {
967 return spci_error(SPCI_INVALID_PARAMETERS);
968 }
969
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000970 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +0100971 * Check that the sender has configured its send buffer. If the tx
972 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
973 * be safely accessed after releasing the lock since the tx mailbox
974 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000975 */
976 sl_lock(&from->lock);
977 from_msg = from->mailbox.send;
978 sl_unlock(&from->lock);
979
980 if (from_msg == NULL) {
Andrew Walbran70bc8622019-10-07 14:15:58 +0100981 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100982 }
983
Andrew Walbran82d6d152019-12-24 15:02:06 +0000984 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100985
Andrew Walbran82d6d152019-12-24 15:02:06 +0000986 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +0100987 ret = spci_error(SPCI_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100988 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100989 }
990
Andrew Walbran82d6d152019-12-24 15:02:06 +0000991 /* Copy data. */
992 memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg, size);
993 to->mailbox.recv_size = size;
994 to->mailbox.recv_sender = sender_vm_id;
995 to->mailbox.recv_attributes = 0;
996 ret = (struct spci_value){.func = SPCI_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100997
Andrew Walbran82d6d152019-12-24 15:02:06 +0000998 deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +0100999
1000out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001001 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001002
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001003 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001004}
1005
1006/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001007 * Checks whether the vCPU's attempt to block for a message has already been
1008 * interrupted or whether it is allowed to block.
1009 */
1010bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1011{
1012 bool interrupted;
1013
1014 sl_lock(&current->lock);
1015
1016 /*
1017 * Don't block if there are enabled and pending interrupts, to match
1018 * behaviour of wait_for_interrupt.
1019 */
1020 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1021
1022 sl_unlock(&current->lock);
1023
1024 return interrupted;
1025}
1026
1027/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001028 * Receives a message from the mailbox. If one isn't available, this function
1029 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001030 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001031 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001032 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001033struct spci_value api_spci_msg_recv(bool block, struct vcpu *current,
1034 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001035{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001036 struct vm *vm = current->vm;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001037 struct spci_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001038
Andrew Scullaa039b32018-10-04 15:02:26 +01001039 /*
1040 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001041 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001042 */
Andrew Scull19503262018-09-20 14:48:39 +01001043 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001044 return spci_error(SPCI_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001045 }
1046
1047 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001048
Andrew Scullaa039b32018-10-04 15:02:26 +01001049 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001050 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1051 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001052 return_code = spci_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001053 goto out;
1054 }
1055
1056 /* No pending message so fail if not allowed to block. */
1057 if (!block) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001058 return_code = spci_error(SPCI_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001059 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001060 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001061
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001062 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001063 * From this point onward this call can only be interrupted or a message
1064 * received. If a message is received the return value will be set at
1065 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001066 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001067 return_code = spci_error(SPCI_INTERRUPTED);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001068 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001069 goto out;
1070 }
1071
Fuad Tabbaed294af2019-12-20 10:43:01 +00001072 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001073 {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001074 struct spci_value run_return = {
1075 .func = SPCI_MSG_WAIT_32,
Andrew Walbran4db5f3a2019-11-04 11:42:42 +00001076 .arg1 = spci_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001077 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001078
Andrew Walbranb4816552018-12-05 17:35:42 +00001079 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001080 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001081 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001082out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001083 sl_unlock(&vm->lock);
1084
Jose Marinho3e2442f2019-03-12 13:30:37 +00001085 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001086}
1087
1088/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001089 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1090 * by this function, the caller must have called api_mailbox_send before with
1091 * the notify argument set to true, and this call must have failed because the
1092 * mailbox was not available.
1093 *
1094 * It should be called repeatedly to retrieve a list of VMs.
1095 *
1096 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1097 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001098 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001099int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001100{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001101 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001102 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001103 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001104
1105 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001106 if (list_empty(&vm->mailbox.ready_list)) {
1107 ret = -1;
1108 goto exit;
1109 }
1110
1111 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1112 ready_links);
1113 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001114 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001115
1116exit:
1117 sl_unlock(&vm->lock);
1118 return ret;
1119}
1120
1121/**
1122 * Retrieves the next VM waiting to be notified that the mailbox of the
1123 * specified VM became writable. Only primary VMs are allowed to call this.
1124 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001125 * Returns -1 on failure or if there are no waiters; the VM id of the next
1126 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001127 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001128int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001129{
1130 struct vm *vm;
1131 struct vm_locked locked;
1132 struct wait_entry *entry;
1133 struct vm *waiting_vm;
1134
1135 /* Only primary VMs are allowed to call this function. */
1136 if (current->vm->id != HF_PRIMARY_VM_ID) {
1137 return -1;
1138 }
1139
Andrew Walbran42347a92019-05-09 13:59:03 +01001140 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001141 if (vm == NULL) {
1142 return -1;
1143 }
1144
Fuad Tabbaed294af2019-12-20 10:43:01 +00001145 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001146 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001147 entry = api_fetch_waiter(locked);
1148 vm_unlock(&locked);
1149
1150 if (entry == NULL) {
1151 return -1;
1152 }
1153
1154 /* Enqueue notification to waiting VM. */
1155 waiting_vm = entry->waiting_vm;
1156
1157 sl_lock(&waiting_vm->lock);
1158 if (list_empty(&entry->ready_links)) {
1159 list_append(&waiting_vm->mailbox.ready_list,
1160 &entry->ready_links);
1161 }
1162 sl_unlock(&waiting_vm->lock);
1163
1164 return waiting_vm->id;
1165}
1166
1167/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001168 * Releases the caller's mailbox so that a new message can be received. The
1169 * caller must have copied out all data they wish to preserve as new messages
1170 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001171 *
1172 * Returns:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001173 * - SPCI_ERROR SPCI_DENIED on failure, if the mailbox hasn't been read.
1174 * - SPCI_SUCCESS on success if no further action is needed.
1175 * - SPCI_RX_RELEASE if it was called by the primary VM and the primary VM now
1176 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001177 * hf_mailbox_waiter_get.
1178 */
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001179struct spci_value api_spci_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001180{
1181 struct vm *vm = current->vm;
1182 struct vm_locked locked;
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001183 struct spci_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001184
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001185 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001186 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001187 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001188 case MAILBOX_STATE_RECEIVED:
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001189 ret = spci_error(SPCI_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001190 break;
1191
Andrew Sculld6ee1102019-04-05 22:12:42 +01001192 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001193 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001194 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001195 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001196 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001197 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001198
1199 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001200}
Andrew Walbran318f5732018-11-20 16:23:42 +00001201
1202/**
1203 * Enables or disables a given interrupt ID for the calling vCPU.
1204 *
1205 * Returns 0 on success, or -1 if the intid is invalid.
1206 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001207int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001208{
1209 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001210 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001211
Andrew Walbran318f5732018-11-20 16:23:42 +00001212 if (intid >= HF_NUM_INTIDS) {
1213 return -1;
1214 }
1215
1216 sl_lock(&current->lock);
1217 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001218 /*
1219 * If it is pending and was not enabled before, increment the
1220 * count.
1221 */
1222 if (current->interrupts.interrupt_pending[intid_index] &
1223 ~current->interrupts.interrupt_enabled[intid_index] &
1224 intid_mask) {
1225 current->interrupts.enabled_and_pending_count++;
1226 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001227 current->interrupts.interrupt_enabled[intid_index] |=
1228 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001229 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001230 /*
1231 * If it is pending and was enabled before, decrement the count.
1232 */
1233 if (current->interrupts.interrupt_pending[intid_index] &
1234 current->interrupts.interrupt_enabled[intid_index] &
1235 intid_mask) {
1236 current->interrupts.enabled_and_pending_count--;
1237 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001238 current->interrupts.interrupt_enabled[intid_index] &=
1239 ~intid_mask;
1240 }
1241
1242 sl_unlock(&current->lock);
1243 return 0;
1244}
1245
1246/**
1247 * Returns the ID of the next pending interrupt for the calling vCPU, and
1248 * acknowledges it (i.e. marks it as no longer pending). Returns
1249 * HF_INVALID_INTID if there are no pending interrupts.
1250 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001251uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001252{
1253 uint8_t i;
1254 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001255
1256 /*
1257 * Find the first enabled and pending interrupt ID, return it, and
1258 * deactivate it.
1259 */
1260 sl_lock(&current->lock);
1261 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1262 uint32_t enabled_and_pending =
1263 current->interrupts.interrupt_enabled[i] &
1264 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001265
Andrew Walbran318f5732018-11-20 16:23:42 +00001266 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001267 uint8_t bit_index = ctz(enabled_and_pending);
1268 /*
1269 * Mark it as no longer pending and decrement the count.
1270 */
1271 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001272 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001273 current->interrupts.enabled_and_pending_count--;
1274 first_interrupt =
1275 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001276 break;
1277 }
1278 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001279
1280 sl_unlock(&current->lock);
1281 return first_interrupt;
1282}
1283
1284/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001285 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001286 * given VM and vCPU.
1287 */
1288static inline bool is_injection_allowed(uint32_t target_vm_id,
1289 struct vcpu *current)
1290{
1291 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001292
Andrew Walbran318f5732018-11-20 16:23:42 +00001293 /*
1294 * The primary VM is allowed to inject interrupts into any VM. Secondary
1295 * VMs are only allowed to inject interrupts into their own vCPUs.
1296 */
1297 return current_vm_id == HF_PRIMARY_VM_ID ||
1298 current_vm_id == target_vm_id;
1299}
1300
1301/**
1302 * Injects a virtual interrupt of the given ID into the given target vCPU.
1303 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1304 * when the vCPU is next run, which is up to the scheduler.
1305 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001306 * Returns:
1307 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1308 * ID is invalid, or the current VM is not allowed to inject interrupts to
1309 * the target VM.
1310 * - 0 on success if no further action is needed.
1311 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1312 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001313 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001314int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001315 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001316 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001317{
Andrew Walbran318f5732018-11-20 16:23:42 +00001318 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001319 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001320
1321 if (intid >= HF_NUM_INTIDS) {
1322 return -1;
1323 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001324
Andrew Walbran318f5732018-11-20 16:23:42 +00001325 if (target_vm == NULL) {
1326 return -1;
1327 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001328
Andrew Walbran318f5732018-11-20 16:23:42 +00001329 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001330 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001331 return -1;
1332 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001333
Andrew Walbran318f5732018-11-20 16:23:42 +00001334 if (!is_injection_allowed(target_vm_id, current)) {
1335 return -1;
1336 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001337
Andrew Walbrane1310df2019-04-29 17:28:28 +01001338 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001339
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001340 dlog("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n", intid,
Andrew Walbran318f5732018-11-20 16:23:42 +00001341 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001342 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001343}
Andrew Scull6386f252018-12-06 13:29:10 +00001344
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001345/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001346struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001347{
1348 /*
1349 * Ensure that both major and minor revision representation occupies at
1350 * most 15 bits.
1351 */
1352 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1353 "Major revision representation take more than 15 bits.");
1354 static_assert(0x10000 > SPCI_VERSION_MINOR,
1355 "Minor revision representation take more than 16 bits.");
1356
Andrew Walbran7f920af2019-09-03 17:09:30 +01001357 struct spci_value ret = {
1358 .func = SPCI_SUCCESS_32,
Andrew Walbran455c53a2019-10-10 13:56:19 +01001359 .arg2 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
Andrew Walbran7f920af2019-09-03 17:09:30 +01001360 SPCI_VERSION_MINOR};
1361 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001362}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001363
1364int64_t api_debug_log(char c, struct vcpu *current)
1365{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001366 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001367 struct vm *vm = current->vm;
1368 struct vm_locked vm_locked = vm_lock(vm);
1369
Andrew Sculld54e1be2019-08-20 11:09:42 +01001370 if (c == '\n' || c == '\0') {
1371 flush = true;
1372 } else {
1373 vm->log_buffer[vm->log_buffer_length++] = c;
1374 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1375 }
1376
1377 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001378 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1379 vm->log_buffer_length);
1380 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001381 }
1382
1383 vm_unlock(&vm_locked);
1384
1385 return 0;
1386}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001387
1388/**
1389 * Discovery function returning information about the implementation of optional
1390 * SPCI interfaces.
1391 */
1392struct spci_value api_spci_features(uint32_t function_id)
1393{
1394 switch (function_id) {
1395 case SPCI_ERROR_32:
1396 case SPCI_SUCCESS_32:
1397 case SPCI_ID_GET_32:
1398 case SPCI_YIELD_32:
1399 case SPCI_VERSION_32:
1400 case SPCI_FEATURES_32:
1401 case SPCI_MSG_SEND_32:
1402 case SPCI_MSG_POLL_32:
1403 case SPCI_MSG_WAIT_32:
1404 return (struct spci_value){.func = SPCI_SUCCESS_32};
1405 default:
1406 return spci_error(SPCI_NOT_SUPPORTED);
1407 }
1408}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001409
1410struct spci_value api_spci_mem_send(uint32_t share_type, ipaddr_t address,
1411 uint32_t page_count,
1412 uint32_t remaining_fragment_count,
1413 uint32_t length, uint32_t handle,
1414 struct vcpu *current, struct vcpu **next)
1415{
1416 struct vm *from = current->vm;
1417 struct vm *to;
1418 const void *from_msg;
1419 uint32_t message_buffer_size;
1420 struct spci_memory_region *memory_region;
1421 struct two_vm_locked vm_to_from_lock;
1422 struct spci_value ret;
1423
1424 if (ipa_addr(address) != 0 || page_count != 0) {
1425 /*
1426 * Hafnium only supports passing the descriptor in the TX
1427 * mailbox.
1428 */
1429 return spci_error(SPCI_INVALID_PARAMETERS);
1430 }
1431
1432 if (handle == 0 && remaining_fragment_count != 0) {
1433 /* Handle is required if there are multiple fragments. */
1434 return spci_error(SPCI_INVALID_PARAMETERS);
1435 }
1436
1437 /*
1438 * Check that the sender has configured its send buffer. If the TX
1439 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1440 * be safely accessed after releasing the lock since the TX mailbox
1441 * address can only be configured once.
1442 */
1443 sl_lock(&from->lock);
1444 from_msg = from->mailbox.send;
1445 sl_unlock(&from->lock);
1446
1447 if (from_msg == NULL) {
1448 return spci_error(SPCI_INVALID_PARAMETERS);
1449 }
1450
1451 /*
1452 * Copy the memory region descriptor to an internal buffer, so that the
1453 * sender can't change it underneath us.
1454 */
1455 memory_region =
1456 (struct spci_memory_region *)cpu_get_buffer(current->cpu->id);
1457 message_buffer_size = cpu_get_buffer_size(current->cpu->id);
1458 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1459 return spci_error(SPCI_INVALID_PARAMETERS);
1460 }
1461 memcpy_s(memory_region, message_buffer_size, from_msg, length);
1462
1463 /* The sender must match the caller. */
1464 if (memory_region->sender != from->id) {
1465 return spci_error(SPCI_INVALID_PARAMETERS);
1466 }
1467
1468 if (memory_region->attribute_count != 1) {
1469 /* Hafnium doesn't support multi-way memory sharing for now. */
1470 return spci_error(SPCI_NOT_SUPPORTED);
1471 }
1472
1473 /*
1474 * Ensure that the receiver VM exists and isn't the same as the sender.
1475 */
1476 to = vm_find(memory_region->attributes[0].receiver);
1477 if (to == NULL || to == from) {
1478 return spci_error(SPCI_INVALID_PARAMETERS);
1479 }
1480
1481 vm_to_from_lock = vm_lock_both(to, from);
1482
1483 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
1484 ret = spci_error(SPCI_BUSY);
1485 goto out;
1486 }
1487
1488 ret = spci_msg_handle_architected_message(
1489 vm_to_from_lock.vm1, vm_to_from_lock.vm2, memory_region, length,
1490 share_type, &api_page_pool);
1491
1492 if (ret.func == SPCI_SUCCESS_32) {
1493 deliver_msg(vm_to_from_lock.vm1, from->id, current, next);
1494 }
1495
1496out:
1497 vm_unlock(&vm_to_from_lock.vm1);
1498 vm_unlock(&vm_to_from_lock.vm2);
1499
1500 return ret;
1501}