blob: 6f118a2ba08133119890d8c69fc0c91aac4e0ccd [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/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010062 * 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
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000113 /* Mark the current vcpu as waiting. */
114 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/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000122 * Returns to the primary vm and signals that the vcpu still has work to do so.
123 */
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,
128 .arg1 = ((uint32_t)current->vm->id << 16) | 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/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100135 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
136 * vm.
137 */
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,
142 .arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
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,
156 .arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
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/**
Andrew Scull66d62bf2019-02-01 13:54:10 +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,
177 .arg1 = ((uint32_t)vcpu_index(current) << 16) | current->vm->id,
Andrew Scull66d62bf2019-02-01 13:54:10 +0000178 };
179
180 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +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,
196 .arg1 = ((uint32_t)vcpu_index(target_vcpu) << 16) |
197 target_vcpu->vm->id,
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
252 /* 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
267 * function to indicate that register state for the given vcpu has been saved
268 * and can therefore be used by other pcpus.
269 */
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/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000378 * Prepares the vcpu to run by updating its state and fetching whether a return
379 * 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 Scull4caadaf2019-07-03 13:13:47 +0100388 * Wait until the registers become available. All locks must be released
389 * between iterations of this loop to avoid potential deadlocks if, on
390 * any path, a lock needs to be taken after taking the decision to
391 * switch context but before the registers have been saved.
Andrew Scullb06d1752019-02-04 10:15:48 +0000392 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100393 * The VM lock is not needed in the common case so it must only be taken
394 * when it is going to be needed. This ensures there are no inter-vCPU
395 * dependencies in the common run case meaning the sensitive context
396 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000397 */
398 for (;;) {
399 sl_lock(&vcpu->lock);
400
401 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100402 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000403 if (need_vm_lock) {
404 sl_unlock(&vcpu->lock);
405 sl_lock(&vcpu->vm->lock);
406 sl_lock(&vcpu->lock);
407 }
408
409 if (vcpu->regs_available) {
410 break;
411 }
412
Andrew Sculld6ee1102019-04-05 22:12:42 +0100413 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000414 /*
415 * vCPU is running on another pCPU.
416 *
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100417 * It's okay not to return the sleep duration here
418 * because the other physical CPU that is currently
419 * running this vCPU will return the sleep duration if
420 * needed.
Andrew Scullb06d1752019-02-04 10:15:48 +0000421 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100422 *run_ret = spci_error(SPCI_BUSY);
Andrew Scullb06d1752019-02-04 10:15:48 +0000423 ret = false;
424 goto out;
425 }
426
427 sl_unlock(&vcpu->lock);
428 if (need_vm_lock) {
429 sl_unlock(&vcpu->vm->lock);
430 }
431 }
Andrew Scull9726c252019-01-23 13:44:19 +0000432
433 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100434 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000435 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
436 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100437 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000438 }
439 ret = false;
440 goto out;
441 }
442
Andrew Walbran508e63c2018-12-20 17:02:37 +0000443 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100444 case VCPU_STATE_RUNNING:
445 case VCPU_STATE_OFF:
446 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000447 ret = false;
448 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000449
Andrew Sculld6ee1102019-04-05 22:12:42 +0100450 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000451 /*
452 * A pending message allows the vCPU to run so the message can
453 * be delivered directly.
454 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100455 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100456 arch_regs_set_retval(&vcpu->regs,
457 spci_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100458 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000459 break;
460 }
461 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100462 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000463 /* Allow virtual interrupts to be delivered. */
464 if (vcpu->interrupts.enabled_and_pending_count > 0) {
465 break;
466 }
467
468 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000469 if (arch_timer_pending(&vcpu->regs)) {
470 break;
471 }
472
473 /*
474 * The vCPU is not ready to run, return the appropriate code to
475 * the primary which called vcpu_run.
476 */
477 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100478 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100479 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100480 ? SPCI_MSG_WAIT_32
481 : HF_SPCI_RUN_WAIT_FOR_INTERRUPT;
482 run_ret->arg1 = ((uint32_t)vcpu_index(vcpu) << 16) |
483 vcpu->vm->id;
484 /*
485 * arch_timer_remaining_ns should never return 0,
486 * because if it would then arch_timer_pending would
487 * have returned true before and so we won't get here.
488 */
489 run_ret->arg2 = arch_timer_remaining_ns(&vcpu->regs);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000490 }
491
492 ret = false;
493 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000494
Andrew Sculld6ee1102019-04-05 22:12:42 +0100495 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000496 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000497 }
498
Andrew Scullb06d1752019-02-04 10:15:48 +0000499 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000500 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100501 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000502
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000503 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000504 * Mark the registers as unavailable now that we're about to reflect
505 * them onto the real registers. This will also prevent another physical
506 * CPU from trying to read these registers.
507 */
508 vcpu->regs_available = false;
509
510 ret = true;
511
512out:
513 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000514 if (need_vm_lock) {
515 sl_unlock(&vcpu->vm->lock);
516 }
517
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000518 return ret;
519}
520
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100521struct spci_value api_spci_run(spci_vm_id_t vm_id, spci_vcpu_index_t vcpu_idx,
522 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100523{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100524 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100525 struct vcpu *vcpu;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100526 struct spci_value ret = spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100527
528 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100529 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100530 ret.arg2 = SPCI_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100531 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100532 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100533
Andrew Scull19503262018-09-20 14:48:39 +0100534 /* Only secondary VM vcpus can be run. */
535 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100536 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100537 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100538
Andrew Scull19503262018-09-20 14:48:39 +0100539 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100540 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100541 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100542 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100543 }
544
545 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100546 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100547 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100548 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100549
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000550 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100551 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000552 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000553 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100554 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000555
Andrew Walbran508e63c2018-12-20 17:02:37 +0000556 /*
557 * Inject timer interrupt if timer has expired. It's safe to access
558 * vcpu->regs here because api_vcpu_prepare_run already made sure that
559 * regs_available was true (and then set it to false) before returning
560 * true.
561 */
562 if (arch_timer_pending(&vcpu->regs)) {
563 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100564 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
565 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000566
567 /*
568 * Set the mask bit so the hardware interrupt doesn't fire
569 * again. Ideally we wouldn't do this because it affects what
570 * the secondary vCPU sees, but if we don't then we end up with
571 * a loop of the interrupt firing each time we try to return to
572 * the secondary vCPU.
573 */
574 arch_timer_mask(&vcpu->regs);
575 }
576
Andrew Scull33fecd32019-01-08 14:48:27 +0000577 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000578 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000579
Andrew Scull33fecd32019-01-08 14:48:27 +0000580 /*
581 * Set a placeholder return code to the scheduler. This will be
582 * overwritten when the switch back to the primary occurs.
583 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100584 ret.func = SPCI_INTERRUPT_32;
585 ret.arg1 = ((uint32_t)vm_id << 16) | vcpu_idx;
586 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000587
Andrew Scull6d2db332018-10-10 15:28:17 +0100588out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100589 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100590}
591
592/**
Andrew Scull81e85092018-12-12 12:56:20 +0000593 * Check that the mode indicates memory that is valid, owned and exclusive.
594 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100595static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000596{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100597 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
598 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000599}
600
601/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000602 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
603 * after they've succeeded. If a secondary VM is running and there are waiters,
604 * it also switches back to the primary VM for it to wake waiters up.
605 */
606static int64_t api_waiter_result(struct vm_locked locked_vm,
607 struct vcpu *current, struct vcpu **next)
608{
609 struct vm *vm = locked_vm.vm;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100610 struct spci_value ret = {
611 .func = SPCI_RX_RELEASE_32,
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000612 };
613
614 if (list_empty(&vm->mailbox.waiter_list)) {
615 /* No waiters, nothing else to do. */
616 return 0;
617 }
618
619 if (vm->id == HF_PRIMARY_VM_ID) {
620 /* The caller is the primary VM. Tell it to wake up waiters. */
621 return 1;
622 }
623
624 /*
625 * Switch back to the primary VM, informing it that there are waiters
626 * that need to be notified.
627 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100628 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000629
630 return 0;
631}
632
633/**
Andrew Sculle1322792019-07-01 17:46:10 +0100634 * Configures the hypervisor's stage-1 view of the send and receive pages. The
635 * stage-1 page tables must be locked so memory cannot be taken by another core
636 * which could result in this transaction being unable to roll back in the case
637 * of an error.
638 */
639static bool api_vm_configure_stage1(struct vm_locked vm_locked,
640 paddr_t pa_send_begin, paddr_t pa_send_end,
641 paddr_t pa_recv_begin, paddr_t pa_recv_end,
642 struct mpool *local_page_pool)
643{
644 bool ret;
645 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
646
647 /* Map the send page as read-only in the hypervisor address space. */
648 vm_locked.vm->mailbox.send =
649 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
650 MM_MODE_R, local_page_pool);
651 if (!vm_locked.vm->mailbox.send) {
652 /* TODO: partial defrag of failed range. */
653 /* Recover any memory consumed in failed mapping. */
654 mm_defrag(mm_stage1_locked, local_page_pool);
655 goto fail;
656 }
657
658 /*
659 * Map the receive page as writable in the hypervisor address space. On
660 * failure, unmap the send page before returning.
661 */
662 vm_locked.vm->mailbox.recv =
663 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
664 MM_MODE_W, local_page_pool);
665 if (!vm_locked.vm->mailbox.recv) {
666 /* TODO: partial defrag of failed range. */
667 /* Recover any memory consumed in failed mapping. */
668 mm_defrag(mm_stage1_locked, local_page_pool);
669 goto fail_undo_send;
670 }
671
672 ret = true;
673 goto out;
674
675 /*
676 * The following mappings will not require more memory than is available
677 * in the local pool.
678 */
679fail_undo_send:
680 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100681 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
682 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100683
684fail:
685 ret = false;
686
687out:
688 mm_unlock_stage1(&mm_stage1_locked);
689
690 return ret;
691}
692
693/**
694 * Configures the send and receive pages in the VM stage-2 and hypervisor
695 * stage-1 page tables. Locking of the page tables combined with a local memory
696 * pool ensures there will always be enough memory to recover from any errors
697 * that arise.
698 */
699static bool api_vm_configure_pages(struct vm_locked vm_locked,
700 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100701 uint32_t orig_send_mode,
702 paddr_t pa_recv_begin, paddr_t pa_recv_end,
703 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100704{
705 bool ret;
706 struct mpool local_page_pool;
707
708 /*
709 * Create a local pool so any freed memory can't be used by another
710 * thread. This is to ensure the original mapping can be restored if any
711 * stage of the process fails.
712 */
713 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
714
715 /* Take memory ownership away from the VM and mark as shared. */
716 if (!mm_vm_identity_map(
717 &vm_locked.vm->ptable, pa_send_begin, pa_send_end,
718 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
719 NULL, &local_page_pool)) {
720 goto fail;
721 }
722
723 if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
724 pa_recv_end,
725 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
726 NULL, &local_page_pool)) {
727 /* TODO: partial defrag of failed range. */
728 /* Recover any memory consumed in failed mapping. */
729 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
730 goto fail_undo_send;
731 }
732
733 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
734 pa_recv_begin, pa_recv_end,
735 &local_page_pool)) {
736 goto fail_undo_send_and_recv;
737 }
738
739 ret = true;
740 goto out;
741
742 /*
743 * The following mappings will not require more memory than is available
744 * in the local pool.
745 */
746fail_undo_send_and_recv:
Andrew Scull7e8de322019-07-02 13:00:56 +0100747 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
748 pa_recv_end, orig_recv_mode, NULL,
749 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100750
751fail_undo_send:
Andrew Scull7e8de322019-07-02 13:00:56 +0100752 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin,
753 pa_send_end, orig_send_mode, NULL,
754 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100755
756fail:
757 ret = false;
758
759out:
760 mpool_fini(&local_page_pool);
761
762 return ret;
763}
764
765/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100766 * Configures the VM to send/receive data through the specified pages. The pages
767 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000768 *
769 * Returns:
770 * - -1 on failure.
771 * - 0 on success if no further action is needed.
772 * - 1 if it was called by the primary VM and the primary VM now needs to wake
773 * up or kick waiters. Waiters should be retrieved by calling
774 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100775 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000776int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
777 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100778{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100779 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100780 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100781 paddr_t pa_send_begin;
782 paddr_t pa_send_end;
783 paddr_t pa_recv_begin;
784 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100785 uint32_t orig_send_mode;
786 uint32_t orig_recv_mode;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100787 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100788
789 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000790 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
791 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100792 return -1;
793 }
794
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000795 /* Convert to physical addresses. */
796 pa_send_begin = pa_from_ipa(send);
797 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
798
799 pa_recv_begin = pa_from_ipa(recv);
800 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
801
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100802 /* Fail if the same page is used for the send and receive pages. */
803 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000804 return -1;
805 }
806
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100807 /*
808 * The hypervisor's memory map must be locked for the duration of this
809 * operation to ensure there will be sufficient memory to recover from
810 * any failures.
811 *
812 * TODO: the scope of the can be reduced but will require restructuring
813 * to keep a single unlock point.
814 */
Andrew Sculle1322792019-07-01 17:46:10 +0100815 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000816
817 /* We only allow these to be setup once. */
818 if (vm->mailbox.send || vm->mailbox.recv) {
819 goto fail;
820 }
821
822 /*
823 * Ensure the pages are valid, owned and exclusive to the VM and that
824 * the VM has the required access to the memory.
825 */
826 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
827 &orig_send_mode) ||
828 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
829 (orig_send_mode & MM_MODE_R) == 0 ||
830 (orig_send_mode & MM_MODE_W) == 0) {
831 goto fail;
832 }
833
834 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
835 &orig_recv_mode) ||
836 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
837 (orig_recv_mode & MM_MODE_R) == 0) {
838 goto fail;
839 }
840
Andrew Sculle1322792019-07-01 17:46:10 +0100841 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
842 orig_send_mode, pa_recv_begin, pa_recv_end,
843 orig_recv_mode)) {
844 goto fail;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100845 }
846
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000847 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100848 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000849 goto exit;
850
Andrew Scull220e6212018-12-21 18:09:00 +0000851fail:
852 ret = -1;
853
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100854exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100855 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100856
857 return ret;
858}
859
860/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100861 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
862 * registers the `from` VM to be notified when it becomes available.
863 */
864static bool msg_receiver_busy(struct vm_locked to, struct vm_locked from,
865 bool notify)
866{
867 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
868 to.vm->mailbox.recv == NULL) {
869 /*
870 * Fail if the receiver isn't currently ready to receive data,
871 * setting up for notification if requested.
872 */
873 if (notify) {
874 struct wait_entry *entry =
875 &from.vm->wait_entries[to.vm->id];
876
877 /* Append waiter only if it's not there yet. */
878 if (list_empty(&entry->wait_links)) {
879 list_append(&to.vm->mailbox.waiter_list,
880 &entry->wait_links);
881 }
882 }
883
884 return true;
885 }
886
887 return false;
888}
889
890/**
891 * Notifies the `to` VM about the message currently in its mailbox, possibly
892 * with the help of the primary VM.
893 */
894static void deliver_msg(struct vm_locked to, struct vm_locked from,
895 uint32_t size, struct vcpu *current, struct vcpu **next)
896{
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100897 struct spci_value primary_ret = {
898 .func = SPCI_MSG_SEND_32,
899 .arg1 = ((uint32_t)from.vm->id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100900 };
901
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100902 /* Messages for the primary VM are delivered directly. */
903 if (to.vm->id == HF_PRIMARY_VM_ID) {
904 /*
905 * Only tell the primary VM the size if the message is for it,
906 * to avoid leaking data about messages for other VMs.
907 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100908 primary_ret.arg3 = size;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100909
910 to.vm->mailbox.state = MAILBOX_STATE_READ;
911 *next = api_switch_to_primary(current, primary_ret,
912 VCPU_STATE_READY);
913 return;
914 }
915
916 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
917
918 /* Return to the primary VM directly or with a switch. */
919 if (from.vm->id != HF_PRIMARY_VM_ID) {
920 *next = api_switch_to_primary(current, primary_ret,
921 VCPU_STATE_READY);
922 }
923}
924
925/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100926 * Copies data from the sender's send buffer to the recipient's receive buffer
927 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000928 *
929 * If the recipient's receive buffer is busy, it can optionally register the
930 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100931 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100932struct spci_value api_spci_msg_send(spci_vm_id_t sender_vm_id,
933 spci_vm_id_t receiver_vm_id, uint32_t size,
934 uint32_t attributes, struct vcpu *current,
935 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100936{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100937 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100938 struct vm *to;
Jose Marinho75509b42019-04-09 09:34:59 +0100939
Andrew Walbranf6595962019-10-15 16:47:56 +0100940 struct two_vm_locked vm_to_from_lock;
Jose Marinho75509b42019-04-09 09:34:59 +0100941
Andrew Walbran70bc8622019-10-07 14:15:58 +0100942 const void *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100943
Andrew Walbran70bc8622019-10-07 14:15:58 +0100944 struct spci_value ret;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000945 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
946 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100947
Andrew Walbran70bc8622019-10-07 14:15:58 +0100948 /* Ensure sender VM ID corresponds to the current VM. */
949 if (sender_vm_id != from->id) {
950 return spci_error(SPCI_INVALID_PARAMETERS);
951 }
952
953 /* Disallow reflexive requests as this suggests an error in the VM. */
954 if (receiver_vm_id == from->id) {
955 return spci_error(SPCI_INVALID_PARAMETERS);
956 }
957
958 /* Limit the size of transfer. */
959 if (size > SPCI_MSG_PAYLOAD_MAX) {
960 return spci_error(SPCI_INVALID_PARAMETERS);
961 }
962
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000963 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +0100964 * Check that the sender has configured its send buffer. If the tx
965 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
966 * be safely accessed after releasing the lock since the tx mailbox
967 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000968 */
969 sl_lock(&from->lock);
970 from_msg = from->mailbox.send;
971 sl_unlock(&from->lock);
972
973 if (from_msg == NULL) {
Andrew Walbran70bc8622019-10-07 14:15:58 +0100974 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100975 }
976
Andrew Walbran70bc8622019-10-07 14:15:58 +0100977 /* Ensure the receiver VM exists. */
978 to = vm_find(receiver_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000979 if (to == NULL) {
Andrew Walbran70bc8622019-10-07 14:15:58 +0100980 return spci_error(SPCI_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100981 }
982
Jose Marinho75509b42019-04-09 09:34:59 +0100983 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +0100984 * Hafnium needs to hold the lock on <to> before the mailbox state is
Jose Marinho75509b42019-04-09 09:34:59 +0100985 * checked. The lock on <to> must be held until the information is
986 * copied to <to> Rx buffer. Since in
987 * spci_msg_handle_architected_message we may call api_spci_share_memory
988 * which must hold the <from> lock, we must hold the <from> lock at this
989 * point to prevent a deadlock scenario.
990 */
Andrew Walbranf6595962019-10-15 16:47:56 +0100991 vm_to_from_lock = vm_lock_both(to, from);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100992
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100993 if (msg_receiver_busy(vm_to_from_lock.vm1, vm_to_from_lock.vm2,
994 notify)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +0100995 ret = spci_error(SPCI_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100996 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100997 }
998
Andrew Walbran70bc8622019-10-07 14:15:58 +0100999 /* Handle legacy memory sharing messages. */
1000 if ((attributes & SPCI_MSG_SEND_LEGACY_MEMORY_MASK) ==
1001 SPCI_MSG_SEND_LEGACY_MEMORY) {
Jose Marinho75509b42019-04-09 09:34:59 +01001002 /*
1003 * Buffer holding the internal copy of the shared memory
1004 * regions.
1005 */
Andrew Walbran70bc8622019-10-07 14:15:58 +01001006 struct spci_architected_message_header
1007 *architected_message_replica =
1008 (struct spci_architected_message_header *)
1009 cpu_get_buffer(current->cpu->id);
Jose Marinho20713fa2019-08-07 15:42:07 +01001010 uint32_t message_buffer_size =
1011 cpu_get_buffer_size(current->cpu->id);
Jose Marinho75509b42019-04-09 09:34:59 +01001012
1013 struct spci_architected_message_header *architected_header =
Andrew Walbran70bc8622019-10-07 14:15:58 +01001014 (struct spci_architected_message_header *)from_msg;
Jose Marinho75509b42019-04-09 09:34:59 +01001015
Andrew Walbran70bc8622019-10-07 14:15:58 +01001016 if (size > message_buffer_size) {
1017 ret = spci_error(SPCI_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +01001018 goto out;
1019 }
1020
Andrew Walbran70bc8622019-10-07 14:15:58 +01001021 if (size < sizeof(struct spci_architected_message_header)) {
1022 ret = spci_error(SPCI_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +01001023 goto out;
1024 }
1025
Andrew Walbran70bc8622019-10-07 14:15:58 +01001026 /* Copy the architected message into the internal buffer. */
1027 memcpy_s(architected_message_replica, message_buffer_size,
1028 architected_header, size);
Jose Marinho75509b42019-04-09 09:34:59 +01001029
1030 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001031 * Note that architected_message_replica is passed as the third
1032 * parameter to spci_msg_handle_architected_message. The
1033 * execution flow commencing at
1034 * spci_msg_handle_architected_message will make several
1035 * accesses to fields in architected_message_replica. The memory
1036 * area architected_message_replica must be exclusively owned by
1037 * Hafnium so that TOCTOU issues do not arise.
Jose Marinho75509b42019-04-09 09:34:59 +01001038 */
1039 ret = spci_msg_handle_architected_message(
Andrew Walbranf6595962019-10-15 16:47:56 +01001040 vm_to_from_lock.vm1, vm_to_from_lock.vm2,
Andrew Walbran70bc8622019-10-07 14:15:58 +01001041 architected_message_replica, size);
Jose Marinho75509b42019-04-09 09:34:59 +01001042
Andrew Walbran70bc8622019-10-07 14:15:58 +01001043 if (ret.func != SPCI_SUCCESS_32) {
Jose Marinho75509b42019-04-09 09:34:59 +01001044 goto out;
1045 }
1046 } else {
1047 /* Copy data. */
Andrew Walbran70bc8622019-10-07 14:15:58 +01001048 memcpy_s(to->mailbox.recv, SPCI_MSG_PAYLOAD_MAX, from_msg,
1049 size);
1050 to->mailbox.recv_size = size;
1051 to->mailbox.recv_sender = sender_vm_id;
1052 to->mailbox.recv_attributes = 0;
1053 ret = (struct spci_value){.func = SPCI_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001054 }
1055
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001056 deliver_msg(vm_to_from_lock.vm1, vm_to_from_lock.vm2, size, current,
1057 next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001058
1059out:
Andrew Walbranf6595962019-10-15 16:47:56 +01001060 vm_unlock(&vm_to_from_lock.vm1);
1061 vm_unlock(&vm_to_from_lock.vm2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001062
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001063 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001064}
1065
1066/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001067 * Checks whether the vCPU's attempt to block for a message has already been
1068 * interrupted or whether it is allowed to block.
1069 */
1070bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1071{
1072 bool interrupted;
1073
1074 sl_lock(&current->lock);
1075
1076 /*
1077 * Don't block if there are enabled and pending interrupts, to match
1078 * behaviour of wait_for_interrupt.
1079 */
1080 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1081
1082 sl_unlock(&current->lock);
1083
1084 return interrupted;
1085}
1086
1087/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001088 * Receives a message from the mailbox. If one isn't available, this function
1089 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001090 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001091 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001092 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001093struct spci_value api_spci_msg_recv(bool block, struct vcpu *current,
1094 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001095{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001096 struct vm *vm = current->vm;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001097 struct spci_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001098
Andrew Scullaa039b32018-10-04 15:02:26 +01001099 /*
1100 * The primary VM will receive messages as a status code from running
1101 * vcpus and must not call this function.
1102 */
Andrew Scull19503262018-09-20 14:48:39 +01001103 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001104 return spci_error(SPCI_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001105 }
1106
1107 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001108
Andrew Scullaa039b32018-10-04 15:02:26 +01001109 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001110 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1111 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001112 return_code = spci_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001113 goto out;
1114 }
1115
1116 /* No pending message so fail if not allowed to block. */
1117 if (!block) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001118 return_code = spci_error(SPCI_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001119 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001120 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001121
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001122 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001123 * From this point onward this call can only be interrupted or a message
1124 * received. If a message is received the return value will be set at
1125 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001126 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001127 return_code = spci_error(SPCI_INTERRUPTED);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001128 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001129 goto out;
1130 }
1131
Andrew Scullaa039b32018-10-04 15:02:26 +01001132 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001133 {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001134 struct spci_value run_return = {
1135 .func = SPCI_MSG_WAIT_32,
1136 .arg1 = ((uint32_t)vcpu_index(current) << 16) | vm->id,
Andrew Walbranb4816552018-12-05 17:35:42 +00001137 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001138
Andrew Walbranb4816552018-12-05 17:35:42 +00001139 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001140 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001141 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001142out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001143 sl_unlock(&vm->lock);
1144
Jose Marinho3e2442f2019-03-12 13:30:37 +00001145 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001146}
1147
1148/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001149 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1150 * by this function, the caller must have called api_mailbox_send before with
1151 * the notify argument set to true, and this call must have failed because the
1152 * mailbox was not available.
1153 *
1154 * It should be called repeatedly to retrieve a list of VMs.
1155 *
1156 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1157 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001158 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001159int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001160{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001161 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001162 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001163 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001164
1165 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001166 if (list_empty(&vm->mailbox.ready_list)) {
1167 ret = -1;
1168 goto exit;
1169 }
1170
1171 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1172 ready_links);
1173 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001174 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001175
1176exit:
1177 sl_unlock(&vm->lock);
1178 return ret;
1179}
1180
1181/**
1182 * Retrieves the next VM waiting to be notified that the mailbox of the
1183 * specified VM became writable. Only primary VMs are allowed to call this.
1184 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001185 * Returns -1 on failure or if there are no waiters; the VM id of the next
1186 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001187 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001188int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001189{
1190 struct vm *vm;
1191 struct vm_locked locked;
1192 struct wait_entry *entry;
1193 struct vm *waiting_vm;
1194
1195 /* Only primary VMs are allowed to call this function. */
1196 if (current->vm->id != HF_PRIMARY_VM_ID) {
1197 return -1;
1198 }
1199
Andrew Walbran42347a92019-05-09 13:59:03 +01001200 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001201 if (vm == NULL) {
1202 return -1;
1203 }
1204
1205 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001206 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001207 entry = api_fetch_waiter(locked);
1208 vm_unlock(&locked);
1209
1210 if (entry == NULL) {
1211 return -1;
1212 }
1213
1214 /* Enqueue notification to waiting VM. */
1215 waiting_vm = entry->waiting_vm;
1216
1217 sl_lock(&waiting_vm->lock);
1218 if (list_empty(&entry->ready_links)) {
1219 list_append(&waiting_vm->mailbox.ready_list,
1220 &entry->ready_links);
1221 }
1222 sl_unlock(&waiting_vm->lock);
1223
1224 return waiting_vm->id;
1225}
1226
1227/**
1228 * Clears the caller's mailbox so that a new message can be received. The caller
1229 * must have copied out all data they wish to preserve as new messages will
1230 * overwrite the old and will arrive asynchronously.
1231 *
1232 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001233 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001234 * - 0 on success if no further action is needed.
1235 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1236 * up or kick waiters. Waiters should be retrieved by calling
1237 * hf_mailbox_waiter_get.
1238 */
1239int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1240{
1241 struct vm *vm = current->vm;
1242 struct vm_locked locked;
1243 int64_t ret;
1244
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001245 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001246 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001247 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001248 ret = 0;
1249 break;
1250
Andrew Sculld6ee1102019-04-05 22:12:42 +01001251 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001252 ret = -1;
1253 break;
1254
Andrew Sculld6ee1102019-04-05 22:12:42 +01001255 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001256 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001257 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001258 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001259 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001260 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001261
1262 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001263}
Andrew Walbran318f5732018-11-20 16:23:42 +00001264
1265/**
1266 * Enables or disables a given interrupt ID for the calling vCPU.
1267 *
1268 * Returns 0 on success, or -1 if the intid is invalid.
1269 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001270int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001271{
1272 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001273 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001274
Andrew Walbran318f5732018-11-20 16:23:42 +00001275 if (intid >= HF_NUM_INTIDS) {
1276 return -1;
1277 }
1278
1279 sl_lock(&current->lock);
1280 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001281 /*
1282 * If it is pending and was not enabled before, increment the
1283 * count.
1284 */
1285 if (current->interrupts.interrupt_pending[intid_index] &
1286 ~current->interrupts.interrupt_enabled[intid_index] &
1287 intid_mask) {
1288 current->interrupts.enabled_and_pending_count++;
1289 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001290 current->interrupts.interrupt_enabled[intid_index] |=
1291 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001292 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001293 /*
1294 * If it is pending and was enabled before, decrement the count.
1295 */
1296 if (current->interrupts.interrupt_pending[intid_index] &
1297 current->interrupts.interrupt_enabled[intid_index] &
1298 intid_mask) {
1299 current->interrupts.enabled_and_pending_count--;
1300 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001301 current->interrupts.interrupt_enabled[intid_index] &=
1302 ~intid_mask;
1303 }
1304
1305 sl_unlock(&current->lock);
1306 return 0;
1307}
1308
1309/**
1310 * Returns the ID of the next pending interrupt for the calling vCPU, and
1311 * acknowledges it (i.e. marks it as no longer pending). Returns
1312 * HF_INVALID_INTID if there are no pending interrupts.
1313 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001314uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001315{
1316 uint8_t i;
1317 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001318
1319 /*
1320 * Find the first enabled and pending interrupt ID, return it, and
1321 * deactivate it.
1322 */
1323 sl_lock(&current->lock);
1324 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1325 uint32_t enabled_and_pending =
1326 current->interrupts.interrupt_enabled[i] &
1327 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001328
Andrew Walbran318f5732018-11-20 16:23:42 +00001329 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001330 uint8_t bit_index = ctz(enabled_and_pending);
1331 /*
1332 * Mark it as no longer pending and decrement the count.
1333 */
1334 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001335 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001336 current->interrupts.enabled_and_pending_count--;
1337 first_interrupt =
1338 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001339 break;
1340 }
1341 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001342
1343 sl_unlock(&current->lock);
1344 return first_interrupt;
1345}
1346
1347/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001348 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001349 * given VM and vCPU.
1350 */
1351static inline bool is_injection_allowed(uint32_t target_vm_id,
1352 struct vcpu *current)
1353{
1354 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001355
Andrew Walbran318f5732018-11-20 16:23:42 +00001356 /*
1357 * The primary VM is allowed to inject interrupts into any VM. Secondary
1358 * VMs are only allowed to inject interrupts into their own vCPUs.
1359 */
1360 return current_vm_id == HF_PRIMARY_VM_ID ||
1361 current_vm_id == target_vm_id;
1362}
1363
1364/**
1365 * Injects a virtual interrupt of the given ID into the given target vCPU.
1366 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1367 * when the vCPU is next run, which is up to the scheduler.
1368 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001369 * Returns:
1370 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1371 * ID is invalid, or the current VM is not allowed to inject interrupts to
1372 * the target VM.
1373 * - 0 on success if no further action is needed.
1374 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1375 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001376 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001377int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001378 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001379 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001380{
Andrew Walbran318f5732018-11-20 16:23:42 +00001381 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001382 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001383
1384 if (intid >= HF_NUM_INTIDS) {
1385 return -1;
1386 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001387
Andrew Walbran318f5732018-11-20 16:23:42 +00001388 if (target_vm == NULL) {
1389 return -1;
1390 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001391
Andrew Walbran318f5732018-11-20 16:23:42 +00001392 if (target_vcpu_idx >= target_vm->vcpu_count) {
1393 /* The requested vcpu must exist. */
1394 return -1;
1395 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001396
Andrew Walbran318f5732018-11-20 16:23:42 +00001397 if (!is_injection_allowed(target_vm_id, current)) {
1398 return -1;
1399 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001400
Andrew Walbrane1310df2019-04-29 17:28:28 +01001401 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001402
1403 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1404 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001405 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001406}
Andrew Scull6386f252018-12-06 13:29:10 +00001407
1408/**
1409 * Clears a region of physical memory by overwriting it with zeros. The data is
1410 * flushed from the cache so the memory has been cleared across the system.
1411 */
1412static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1413{
1414 /*
1415 * TODO: change this to a cpu local single page window rather than a
1416 * global mapping of the whole range. Such an approach will limit
1417 * the changes to stage-1 tables and will allow only local
1418 * invalidation.
1419 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001420 bool ret;
1421 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
1422 void *ptr =
1423 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001424 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001425
1426 if (!ptr) {
1427 /* TODO: partial defrag of failed range. */
1428 /* Recover any memory consumed in failed mapping. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001429 mm_defrag(stage1_locked, ppool);
1430 goto fail;
Andrew Scull6386f252018-12-06 13:29:10 +00001431 }
1432
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001433 memset_s(ptr, size, 0, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +01001434 arch_mm_flush_dcache(ptr, size);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001435 mm_unmap(stage1_locked, begin, end, ppool);
Andrew Scull6386f252018-12-06 13:29:10 +00001436
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001437 ret = true;
1438 goto out;
1439
1440fail:
1441 ret = false;
1442
1443out:
1444 mm_unlock_stage1(&stage1_locked);
1445
1446 return ret;
Andrew Scull6386f252018-12-06 13:29:10 +00001447}
1448
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001449/** TODO: Move function to spci_architected_message.c. */
Jose Marinho75509b42019-04-09 09:34:59 +01001450/**
1451 * Shares memory from the calling VM with another. The memory can be shared in
1452 * different modes.
1453 *
1454 * This function requires the calling context to hold the <to> and <from> locks.
1455 *
1456 * Returns:
1457 * In case of error one of the following values is returned:
1458 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
1459 * erroneous;
Andrew Walbran379aa722019-10-07 14:16:34 +01001460 * 2) SPCI_NO_MEMORY - Hafnium did not have sufficient memory to complete
Jose Marinho75509b42019-04-09 09:34:59 +01001461 * the request.
1462 * Success is indicated by SPCI_SUCCESS.
1463 */
Andrew Walbran70bc8622019-10-07 14:15:58 +01001464struct spci_value api_spci_share_memory(
1465 struct vm_locked to_locked, struct vm_locked from_locked,
1466 struct spci_memory_region *memory_region, uint32_t memory_to_attributes,
1467 enum spci_memory_share share)
Jose Marinho75509b42019-04-09 09:34:59 +01001468{
1469 struct vm *to = to_locked.vm;
1470 struct vm *from = from_locked.vm;
Andrew Walbran1281ed42019-10-22 17:23:40 +01001471 uint32_t orig_from_mode;
1472 uint32_t from_mode;
1473 uint32_t to_mode;
Jose Marinho75509b42019-04-09 09:34:59 +01001474 struct mpool local_page_pool;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001475 struct spci_value ret;
Jose Marinho75509b42019-04-09 09:34:59 +01001476 paddr_t pa_begin;
1477 paddr_t pa_end;
1478 ipaddr_t begin;
1479 ipaddr_t end;
Andrew Walbranf5972182019-10-15 15:41:26 +01001480 struct spci_memory_region_constituent *constituents =
1481 spci_memory_region_get_constituents(memory_region);
Jose Marinho75509b42019-04-09 09:34:59 +01001482
1483 size_t size;
1484
1485 /* Disallow reflexive shares as this suggests an error in the VM. */
1486 if (to == from) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001487 return spci_error(SPCI_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +01001488 }
1489
1490 /*
1491 * Create a local pool so any freed memory can't be used by another
1492 * thread. This is to ensure the original mapping can be restored if any
1493 * stage of the process fails.
1494 */
1495 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1496
1497 /* Obtain the single contiguous set of pages from the memory_region. */
1498 /* TODO: Add support for multiple constituent regions. */
Andrew Walbranf5972182019-10-15 15:41:26 +01001499 size = constituents[0].page_count * PAGE_SIZE;
1500 begin = ipa_init(constituents[0].address);
Jose Marinho75509b42019-04-09 09:34:59 +01001501 end = ipa_add(begin, size);
1502
1503 /*
1504 * Check if the state transition is lawful for both VMs involved
1505 * in the memory exchange, ensure that all constituents of a memory
1506 * region being shared are at the same state.
1507 */
1508 if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin,
1509 end, memory_to_attributes, &from_mode,
1510 &to_mode)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001511 return spci_error(SPCI_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +01001512 }
1513
1514 pa_begin = pa_from_ipa(begin);
1515 pa_end = pa_from_ipa(end);
1516
1517 /*
1518 * First update the mapping for the sender so there is not overlap with
1519 * the recipient.
1520 */
1521 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1522 NULL, &local_page_pool)) {
Andrew Walbran70bc8622019-10-07 14:15:58 +01001523 ret = spci_error(SPCI_NO_MEMORY);
Jose Marinho75509b42019-04-09 09:34:59 +01001524 goto out;
1525 }
1526
1527 /* Complete the transfer by mapping the memory into the recipient. */
1528 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1529 &local_page_pool)) {
1530 /* TODO: partial defrag of failed range. */
1531 /* Recover any memory consumed in failed mapping. */
1532 mm_vm_defrag(&from->ptable, &local_page_pool);
1533
Andrew Walbran70bc8622019-10-07 14:15:58 +01001534 ret = spci_error(SPCI_NO_MEMORY);
Jose Marinho75509b42019-04-09 09:34:59 +01001535
1536 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1537 orig_from_mode, NULL,
1538 &local_page_pool));
1539
1540 goto out;
1541 }
1542
Andrew Walbran70bc8622019-10-07 14:15:58 +01001543 ret = (struct spci_value){.func = SPCI_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001544
1545out:
Jose Marinho75509b42019-04-09 09:34:59 +01001546 mpool_fini(&local_page_pool);
1547
1548 return ret;
1549}
1550
Andrew Scull6386f252018-12-06 13:29:10 +00001551/**
1552 * Shares memory from the calling VM with another. The memory can be shared in
1553 * different modes.
1554 *
1555 * TODO: the interface for sharing memory will need to be enhanced to allow
1556 * sharing with different modes e.g. read-only, informing the recipient
1557 * of the memory they have been given, opting to not wipe the memory and
1558 * possibly allowing multiple blocks to be transferred. What this will
1559 * look like is TBD.
1560 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001561int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001562 enum hf_share share, struct vcpu *current)
1563{
1564 struct vm *from = current->vm;
1565 struct vm *to;
Andrew Walbran1281ed42019-10-22 17:23:40 +01001566 uint32_t orig_from_mode;
1567 uint32_t from_mode;
1568 uint32_t to_mode;
Andrew Scull6386f252018-12-06 13:29:10 +00001569 ipaddr_t begin;
1570 ipaddr_t end;
1571 paddr_t pa_begin;
1572 paddr_t pa_end;
1573 struct mpool local_page_pool;
1574 int64_t ret;
1575
1576 /* Disallow reflexive shares as this suggests an error in the VM. */
1577 if (vm_id == from->id) {
1578 return -1;
1579 }
1580
1581 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001582 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001583 if (to == NULL) {
1584 return -1;
1585 }
1586
1587 begin = addr;
1588 end = ipa_add(addr, size);
1589
1590 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001591 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1592 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001593 return -1;
1594 }
1595
1596 /* Convert the sharing request to memory management modes. */
1597 switch (share) {
1598 case HF_MEMORY_GIVE:
1599 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1600 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1601 break;
1602
1603 case HF_MEMORY_LEND:
1604 from_mode = MM_MODE_INVALID;
1605 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1606 break;
1607
1608 case HF_MEMORY_SHARE:
1609 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1610 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1611 MM_MODE_SHARED;
1612 break;
1613
1614 default:
1615 /* The input is untrusted so might not be a valid value. */
1616 return -1;
1617 }
1618
1619 /*
1620 * Create a local pool so any freed memory can't be used by another
1621 * thread. This is to ensure the original mapping can be restored if any
1622 * stage of the process fails.
1623 */
1624 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1625
1626 sl_lock_both(&from->lock, &to->lock);
1627
1628 /*
1629 * Ensure that the memory range is mapped with the same mode so that
1630 * changes can be reverted if the process fails.
1631 */
1632 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1633 goto fail;
1634 }
1635
Andrew Scullb5f49e02019-10-02 13:20:47 +01001636 /* Ensure the address range is normal memory and not a device. */
1637 if (orig_from_mode & MM_MODE_D) {
1638 goto fail;
1639 }
1640
Andrew Scull6386f252018-12-06 13:29:10 +00001641 /*
1642 * Ensure the memory range is valid for the sender. If it isn't, the
1643 * sender has either shared it with another VM already or has no claim
1644 * to the memory.
1645 */
1646 if (orig_from_mode & MM_MODE_INVALID) {
1647 goto fail;
1648 }
1649
1650 /*
1651 * The sender must own the memory and have exclusive access to it in
1652 * order to share it. Alternatively, it is giving memory back to the
1653 * owning VM.
1654 */
1655 if (orig_from_mode & MM_MODE_UNOWNED) {
Andrew Walbran1281ed42019-10-22 17:23:40 +01001656 uint32_t orig_to_mode;
Andrew Scull6386f252018-12-06 13:29:10 +00001657
1658 if (share != HF_MEMORY_GIVE ||
1659 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1660 orig_to_mode & MM_MODE_UNOWNED) {
1661 goto fail;
1662 }
1663 } else if (orig_from_mode & MM_MODE_SHARED) {
1664 goto fail;
1665 }
1666
1667 pa_begin = pa_from_ipa(begin);
1668 pa_end = pa_from_ipa(end);
1669
1670 /*
1671 * First update the mapping for the sender so there is not overlap with
1672 * the recipient.
1673 */
1674 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1675 NULL, &local_page_pool)) {
1676 goto fail;
1677 }
1678
1679 /* Clear the memory so no VM or device can see the previous contents. */
1680 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1681 goto fail_return_to_sender;
1682 }
1683
1684 /* Complete the transfer by mapping the memory into the recipient. */
1685 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1686 &local_page_pool)) {
1687 /* TODO: partial defrag of failed range. */
1688 /* Recover any memory consumed in failed mapping. */
1689 mm_vm_defrag(&from->ptable, &local_page_pool);
1690 goto fail_return_to_sender;
1691 }
1692
1693 ret = 0;
1694 goto out;
1695
1696fail_return_to_sender:
Andrew Scull7e8de322019-07-02 13:00:56 +01001697 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1698 orig_from_mode, NULL, &local_page_pool));
Andrew Scull6386f252018-12-06 13:29:10 +00001699
1700fail:
1701 ret = -1;
1702
1703out:
1704 sl_unlock(&from->lock);
1705 sl_unlock(&to->lock);
1706
1707 mpool_fini(&local_page_pool);
1708
1709 return ret;
1710}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001711
1712/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001713struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001714{
1715 /*
1716 * Ensure that both major and minor revision representation occupies at
1717 * most 15 bits.
1718 */
1719 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1720 "Major revision representation take more than 15 bits.");
1721 static_assert(0x10000 > SPCI_VERSION_MINOR,
1722 "Minor revision representation take more than 16 bits.");
1723
Andrew Walbran7f920af2019-09-03 17:09:30 +01001724 struct spci_value ret = {
1725 .func = SPCI_SUCCESS_32,
Andrew Walbran455c53a2019-10-10 13:56:19 +01001726 .arg2 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
Andrew Walbran7f920af2019-09-03 17:09:30 +01001727 SPCI_VERSION_MINOR};
1728 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001729}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001730
1731int64_t api_debug_log(char c, struct vcpu *current)
1732{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001733 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001734 struct vm *vm = current->vm;
1735 struct vm_locked vm_locked = vm_lock(vm);
1736
Andrew Sculld54e1be2019-08-20 11:09:42 +01001737 if (c == '\n' || c == '\0') {
1738 flush = true;
1739 } else {
1740 vm->log_buffer[vm->log_buffer_length++] = c;
1741 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1742 }
1743
1744 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001745 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1746 vm->log_buffer_length);
1747 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001748 }
1749
1750 vm_unlock(&vm_locked);
1751
1752 return 0;
1753}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001754
1755/**
1756 * Discovery function returning information about the implementation of optional
1757 * SPCI interfaces.
1758 */
1759struct spci_value api_spci_features(uint32_t function_id)
1760{
1761 switch (function_id) {
1762 case SPCI_ERROR_32:
1763 case SPCI_SUCCESS_32:
1764 case SPCI_ID_GET_32:
1765 case SPCI_YIELD_32:
1766 case SPCI_VERSION_32:
1767 case SPCI_FEATURES_32:
1768 case SPCI_MSG_SEND_32:
1769 case SPCI_MSG_POLL_32:
1770 case SPCI_MSG_WAIT_32:
1771 return (struct spci_value){.func = SPCI_SUCCESS_32};
1772 default:
1773 return spci_error(SPCI_NOT_SUPPORTED);
1774 }
1775}