blob: 2034ae91cf6b1c67e7acaee385376b21d0823d7a [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
65 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
66 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010067 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010068static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000069 struct hf_vcpu_run_return primary_ret,
70 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010071{
Andrew Walbran42347a92019-05-09 13:59:03 +010072 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010073 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074
Andrew Walbran508e63c2018-12-20 17:02:37 +000075 /*
76 * If the secondary is blocked but has a timer running, sleep until the
77 * timer fires rather than indefinitely.
78 */
Andrew Scullb06d1752019-02-04 10:15:48 +000079 switch (primary_ret.code) {
80 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
81 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
82 primary_ret.sleep.ns =
83 arch_timer_enabled_current()
84 ? arch_timer_remaining_ns_current()
85 : HF_SLEEP_INDEFINITE;
86 break;
87
88 default:
89 /* Do nothing. */
90 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +000091 }
92
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010093 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +010094 arch_regs_set_retval(
95 &next->regs,
96 (struct spci_value){
97 .func = hf_vcpu_run_return_encode(primary_ret)});
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010098
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000099 /* Mark the current vcpu as waiting. */
100 sl_lock(&current->lock);
101 current->state = secondary_state;
102 sl_unlock(&current->lock);
103
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100104 return next;
105}
106
107/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000108 * Returns to the primary vm and signals that the vcpu still has work to do so.
109 */
110struct vcpu *api_preempt(struct vcpu *current)
111{
112 struct hf_vcpu_run_return ret = {
113 .code = HF_VCPU_RUN_PREEMPTED,
114 };
115
Andrew Sculld6ee1102019-04-05 22:12:42 +0100116 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000117}
118
119/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100120 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
121 * vm.
122 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100123struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100124{
Andrew Scull6d2db332018-10-10 15:28:17 +0100125 struct hf_vcpu_run_return ret = {
126 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
127 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000128
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000129 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100130 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100131}
132
133/**
Andrew Walbran33645652019-04-15 12:29:31 +0100134 * Puts the current vCPU in off mode, and returns to the primary VM.
135 */
136struct vcpu *api_vcpu_off(struct vcpu *current)
137{
138 struct hf_vcpu_run_return ret = {
139 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
140 };
141
142 /*
143 * Disable the timer, so the scheduler doesn't get told to call back
144 * based on it.
145 */
146 arch_timer_disable_current();
147
148 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
149}
150
151/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000152 * Returns to the primary vm to allow this cpu to be used for other tasks as the
153 * vcpu does not have work to do at this moment. The current vcpu is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100154 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000155 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100156void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000157{
Andrew Walbran16075b62019-09-03 17:11:07 +0100158 struct hf_vcpu_run_return primary_ret = {
Andrew Scull66d62bf2019-02-01 13:54:10 +0000159 .code = HF_VCPU_RUN_YIELD,
160 };
161
162 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000163 /* Noop on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100164 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000165 }
166
Andrew Walbran16075b62019-09-03 17:11:07 +0100167 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000168}
169
170/**
Andrew Walbran33645652019-04-15 12:29:31 +0100171 * Switches to the primary so that it can switch to the target, or kick it if it
172 * is already running on a different physical CPU.
173 */
174struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
175{
176 struct hf_vcpu_run_return ret = {
177 .code = HF_VCPU_RUN_WAKE_UP,
178 .wake_up.vm_id = target_vcpu->vm->id,
179 .wake_up.vcpu = vcpu_index(target_vcpu),
180 };
181 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
182}
183
184/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000185 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000186 */
187struct vcpu *api_abort(struct vcpu *current)
188{
189 struct hf_vcpu_run_return ret = {
190 .code = HF_VCPU_RUN_ABORTED,
191 };
192
193 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
194
195 if (current->vm->id == HF_PRIMARY_VM_ID) {
196 /* TODO: what to do when the primary aborts? */
197 for (;;) {
198 /* Do nothing. */
199 }
200 }
201
202 atomic_store_explicit(&current->vm->aborting, true,
203 memory_order_relaxed);
204
205 /* TODO: free resources once all vCPUs abort. */
206
Andrew Sculld6ee1102019-04-05 22:12:42 +0100207 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000208}
209
210/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000211 * Returns the ID of the VM.
212 */
Andrew Walbranfc6cd9d2019-06-25 15:23:27 +0100213spci_vm_id_t api_vm_get_id(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000214{
215 return current->vm->id;
216}
217
218/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100219 * Returns the number of VMs configured to run.
220 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100221spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100222{
Andrew Scull19503262018-09-20 14:48:39 +0100223 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100224}
225
226/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100227 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
228 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100229 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100230spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
231 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100232{
Andrew Scull19503262018-09-20 14:48:39 +0100233 struct vm *vm;
234
235 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100236 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100237 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100238 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100239
Andrew Walbran42347a92019-05-09 13:59:03 +0100240 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100241 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100242 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100243 }
244
245 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100246}
247
248/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000249 * This function is called by the architecture-specific context switching
250 * function to indicate that register state for the given vcpu has been saved
251 * and can therefore be used by other pcpus.
252 */
253void api_regs_state_saved(struct vcpu *vcpu)
254{
255 sl_lock(&vcpu->lock);
256 vcpu->regs_available = true;
257 sl_unlock(&vcpu->lock);
258}
259
260/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000261 * Retrieves the next waiter and removes it from the wait list if the VM's
262 * mailbox is in a writable state.
263 */
264static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
265{
266 struct wait_entry *entry;
267 struct vm *vm = locked_vm.vm;
268
Andrew Sculld6ee1102019-04-05 22:12:42 +0100269 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000270 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
271 /* The mailbox is not writable or there are no waiters. */
272 return NULL;
273 }
274
275 /* Remove waiter from the wait list. */
276 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
277 wait_links);
278 list_remove(&entry->wait_links);
279 return entry;
280}
281
282/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000283 * Assuming that the arguments have already been checked by the caller, injects
284 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
285 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
286 * is next run, which is up to the scheduler.
287 *
288 * Returns:
289 * - 0 on success if no further action is needed.
290 * - 1 if it was called by the primary VM and the primary VM now needs to wake
291 * up or kick the target vCPU.
292 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100293static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000294 uint32_t intid, struct vcpu *current,
295 struct vcpu **next)
296{
297 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
298 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000299 int64_t ret = 0;
300
301 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000302
303 /*
304 * We only need to change state and (maybe) trigger a virtual IRQ if it
305 * is enabled and was not previously pending. Otherwise we can skip
306 * everything except setting the pending bit.
307 *
308 * If you change this logic make sure to update the need_vm_lock logic
309 * above to match.
310 */
311 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
312 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
313 intid_mask)) {
314 goto out;
315 }
316
317 /* Increment the count. */
318 target_vcpu->interrupts.enabled_and_pending_count++;
319
320 /*
321 * Only need to update state if there was not already an
322 * interrupt enabled and pending.
323 */
324 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
325 goto out;
326 }
327
Andrew Walbran508e63c2018-12-20 17:02:37 +0000328 if (current->vm->id == HF_PRIMARY_VM_ID) {
329 /*
330 * If the call came from the primary VM, let it know that it
331 * should run or kick the target vCPU.
332 */
333 ret = 1;
334 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100335 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000336 }
337
338out:
339 /* Either way, make it pending. */
340 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
341
342 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000343
344 return ret;
345}
346
347/**
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100348 * Constructs an SPCI_MSG_SEND value to return from a successful SPCI_MSG_POLL
349 * or SPCI_MSG_WAIT call.
350 */
351static struct spci_value spci_msg_recv_return(const struct vm *receiver)
352{
353 return (struct spci_value){
354 .func = SPCI_MSG_SEND_32,
355 .arg1 = receiver->mailbox.recv->source_vm_id << 16 |
356 receiver->id,
357 .arg3 = receiver->mailbox.recv->length};
358}
359
360/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000361 * Prepares the vcpu to run by updating its state and fetching whether a return
362 * value needs to be forced onto the vCPU.
363 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000364static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000365 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000366{
Andrew Scullb06d1752019-02-04 10:15:48 +0000367 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000368 bool ret;
369
Andrew Scullb06d1752019-02-04 10:15:48 +0000370 /*
Andrew Scull4caadaf2019-07-03 13:13:47 +0100371 * Wait until the registers become available. All locks must be released
372 * between iterations of this loop to avoid potential deadlocks if, on
373 * any path, a lock needs to be taken after taking the decision to
374 * switch context but before the registers have been saved.
Andrew Scullb06d1752019-02-04 10:15:48 +0000375 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100376 * The VM lock is not needed in the common case so it must only be taken
377 * when it is going to be needed. This ensures there are no inter-vCPU
378 * dependencies in the common run case meaning the sensitive context
379 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000380 */
381 for (;;) {
382 sl_lock(&vcpu->lock);
383
384 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100385 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000386 if (need_vm_lock) {
387 sl_unlock(&vcpu->lock);
388 sl_lock(&vcpu->vm->lock);
389 sl_lock(&vcpu->lock);
390 }
391
392 if (vcpu->regs_available) {
393 break;
394 }
395
Andrew Sculld6ee1102019-04-05 22:12:42 +0100396 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000397 /*
398 * vCPU is running on another pCPU.
399 *
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100400 * It's ok not to return the sleep duration here because
Andrew Scullb06d1752019-02-04 10:15:48 +0000401 * the other physical CPU that is currently running this
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100402 * vCPU will return the sleep duration if needed. The
403 * default return value is
404 * HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no need to set it
405 * explicitly.
Andrew Scullb06d1752019-02-04 10:15:48 +0000406 */
407 ret = false;
408 goto out;
409 }
410
411 sl_unlock(&vcpu->lock);
412 if (need_vm_lock) {
413 sl_unlock(&vcpu->vm->lock);
414 }
415 }
Andrew Scull9726c252019-01-23 13:44:19 +0000416
417 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100418 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000419 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
420 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100421 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000422 }
423 ret = false;
424 goto out;
425 }
426
Andrew Walbran508e63c2018-12-20 17:02:37 +0000427 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100428 case VCPU_STATE_RUNNING:
429 case VCPU_STATE_OFF:
430 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000431 ret = false;
432 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000433
Andrew Sculld6ee1102019-04-05 22:12:42 +0100434 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000435 /*
436 * A pending message allows the vCPU to run so the message can
437 * be delivered directly.
438 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100439 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100440 arch_regs_set_retval(&vcpu->regs,
441 spci_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100442 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000443 break;
444 }
445 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100446 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000447 /* Allow virtual interrupts to be delivered. */
448 if (vcpu->interrupts.enabled_and_pending_count > 0) {
449 break;
450 }
451
452 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000453 if (arch_timer_pending(&vcpu->regs)) {
454 break;
455 }
456
457 /*
458 * The vCPU is not ready to run, return the appropriate code to
459 * the primary which called vcpu_run.
460 */
461 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000462 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100463 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000464 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
465 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000466 run_ret->sleep.ns =
467 arch_timer_remaining_ns(&vcpu->regs);
468 }
469
470 ret = false;
471 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000472
Andrew Sculld6ee1102019-04-05 22:12:42 +0100473 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000474 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000475 }
476
Andrew Scullb06d1752019-02-04 10:15:48 +0000477 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000478 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100479 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000480
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000481 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000482 * Mark the registers as unavailable now that we're about to reflect
483 * them onto the real registers. This will also prevent another physical
484 * CPU from trying to read these registers.
485 */
486 vcpu->regs_available = false;
487
488 ret = true;
489
490out:
491 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000492 if (need_vm_lock) {
493 sl_unlock(&vcpu->vm->lock);
494 }
495
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000496 return ret;
497}
498
499/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100500 * Runs the given vcpu of the given vm.
501 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100502struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id,
503 spci_vcpu_index_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000504 const struct vcpu *current,
505 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100506{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100507 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100508 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100509 struct hf_vcpu_run_return ret = {
510 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000511 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100512 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100513
514 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100515 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100516 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100517 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100518
Andrew Scull19503262018-09-20 14:48:39 +0100519 /* Only secondary VM vcpus can be run. */
520 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100521 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100522 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100523
Andrew Scull19503262018-09-20 14:48:39 +0100524 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100525 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100526 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100527 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100528 }
529
530 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100531 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100532 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100533 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100534
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000535 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100536 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000537 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000538 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100539 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000540
Andrew Walbran508e63c2018-12-20 17:02:37 +0000541 /*
542 * Inject timer interrupt if timer has expired. It's safe to access
543 * vcpu->regs here because api_vcpu_prepare_run already made sure that
544 * regs_available was true (and then set it to false) before returning
545 * true.
546 */
547 if (arch_timer_pending(&vcpu->regs)) {
548 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100549 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
550 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000551
552 /*
553 * Set the mask bit so the hardware interrupt doesn't fire
554 * again. Ideally we wouldn't do this because it affects what
555 * the secondary vCPU sees, but if we don't then we end up with
556 * a loop of the interrupt firing each time we try to return to
557 * the secondary vCPU.
558 */
559 arch_timer_mask(&vcpu->regs);
560 }
561
Andrew Scull33fecd32019-01-08 14:48:27 +0000562 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000563 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000564
Andrew Scull33fecd32019-01-08 14:48:27 +0000565 /*
566 * Set a placeholder return code to the scheduler. This will be
567 * overwritten when the switch back to the primary occurs.
568 */
569 ret.code = HF_VCPU_RUN_PREEMPTED;
570
Andrew Scull6d2db332018-10-10 15:28:17 +0100571out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100572 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100573}
574
575/**
Andrew Scull81e85092018-12-12 12:56:20 +0000576 * Check that the mode indicates memory that is valid, owned and exclusive.
577 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000578static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000579{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100580 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
581 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000582}
583
584/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000585 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
586 * after they've succeeded. If a secondary VM is running and there are waiters,
587 * it also switches back to the primary VM for it to wake waiters up.
588 */
589static int64_t api_waiter_result(struct vm_locked locked_vm,
590 struct vcpu *current, struct vcpu **next)
591{
592 struct vm *vm = locked_vm.vm;
593 struct hf_vcpu_run_return ret = {
594 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
595 };
596
597 if (list_empty(&vm->mailbox.waiter_list)) {
598 /* No waiters, nothing else to do. */
599 return 0;
600 }
601
602 if (vm->id == HF_PRIMARY_VM_ID) {
603 /* The caller is the primary VM. Tell it to wake up waiters. */
604 return 1;
605 }
606
607 /*
608 * Switch back to the primary VM, informing it that there are waiters
609 * that need to be notified.
610 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100611 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000612
613 return 0;
614}
615
616/**
Andrew Sculle1322792019-07-01 17:46:10 +0100617 * Configures the hypervisor's stage-1 view of the send and receive pages. The
618 * stage-1 page tables must be locked so memory cannot be taken by another core
619 * which could result in this transaction being unable to roll back in the case
620 * of an error.
621 */
622static bool api_vm_configure_stage1(struct vm_locked vm_locked,
623 paddr_t pa_send_begin, paddr_t pa_send_end,
624 paddr_t pa_recv_begin, paddr_t pa_recv_end,
625 struct mpool *local_page_pool)
626{
627 bool ret;
628 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
629
630 /* Map the send page as read-only in the hypervisor address space. */
631 vm_locked.vm->mailbox.send =
632 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
633 MM_MODE_R, local_page_pool);
634 if (!vm_locked.vm->mailbox.send) {
635 /* TODO: partial defrag of failed range. */
636 /* Recover any memory consumed in failed mapping. */
637 mm_defrag(mm_stage1_locked, local_page_pool);
638 goto fail;
639 }
640
641 /*
642 * Map the receive page as writable in the hypervisor address space. On
643 * failure, unmap the send page before returning.
644 */
645 vm_locked.vm->mailbox.recv =
646 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
647 MM_MODE_W, local_page_pool);
648 if (!vm_locked.vm->mailbox.recv) {
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_undo_send;
653 }
654
655 ret = true;
656 goto out;
657
658 /*
659 * The following mappings will not require more memory than is available
660 * in the local pool.
661 */
662fail_undo_send:
663 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100664 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
665 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100666
667fail:
668 ret = false;
669
670out:
671 mm_unlock_stage1(&mm_stage1_locked);
672
673 return ret;
674}
675
676/**
677 * Configures the send and receive pages in the VM stage-2 and hypervisor
678 * stage-1 page tables. Locking of the page tables combined with a local memory
679 * pool ensures there will always be enough memory to recover from any errors
680 * that arise.
681 */
682static bool api_vm_configure_pages(struct vm_locked vm_locked,
683 paddr_t pa_send_begin, paddr_t pa_send_end,
684 int orig_send_mode, paddr_t pa_recv_begin,
685 paddr_t pa_recv_end, int orig_recv_mode)
686{
687 bool ret;
688 struct mpool local_page_pool;
689
690 /*
691 * Create a local pool so any freed memory can't be used by another
692 * thread. This is to ensure the original mapping can be restored if any
693 * stage of the process fails.
694 */
695 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
696
697 /* Take memory ownership away from the VM and mark as shared. */
698 if (!mm_vm_identity_map(
699 &vm_locked.vm->ptable, pa_send_begin, pa_send_end,
700 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
701 NULL, &local_page_pool)) {
702 goto fail;
703 }
704
705 if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
706 pa_recv_end,
707 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
708 NULL, &local_page_pool)) {
709 /* TODO: partial defrag of failed range. */
710 /* Recover any memory consumed in failed mapping. */
711 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
712 goto fail_undo_send;
713 }
714
715 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
716 pa_recv_begin, pa_recv_end,
717 &local_page_pool)) {
718 goto fail_undo_send_and_recv;
719 }
720
721 ret = true;
722 goto out;
723
724 /*
725 * The following mappings will not require more memory than is available
726 * in the local pool.
727 */
728fail_undo_send_and_recv:
Andrew Scull7e8de322019-07-02 13:00:56 +0100729 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
730 pa_recv_end, orig_recv_mode, NULL,
731 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100732
733fail_undo_send:
Andrew Scull7e8de322019-07-02 13:00:56 +0100734 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin,
735 pa_send_end, orig_send_mode, NULL,
736 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100737
738fail:
739 ret = false;
740
741out:
742 mpool_fini(&local_page_pool);
743
744 return ret;
745}
746
747/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100748 * Configures the VM to send/receive data through the specified pages. The pages
749 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000750 *
751 * Returns:
752 * - -1 on failure.
753 * - 0 on success if no further action is needed.
754 * - 1 if it was called by the primary VM and the primary VM now needs to wake
755 * up or kick waiters. Waiters should be retrieved by calling
756 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100757 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000758int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
759 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100760{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100761 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100762 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100763 paddr_t pa_send_begin;
764 paddr_t pa_send_end;
765 paddr_t pa_recv_begin;
766 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000767 int orig_send_mode;
768 int orig_recv_mode;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100769 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100770
771 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000772 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
773 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100774 return -1;
775 }
776
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000777 /* Convert to physical addresses. */
778 pa_send_begin = pa_from_ipa(send);
779 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
780
781 pa_recv_begin = pa_from_ipa(recv);
782 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
783
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100784 /* Fail if the same page is used for the send and receive pages. */
785 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000786 return -1;
787 }
788
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100789 /*
790 * The hypervisor's memory map must be locked for the duration of this
791 * operation to ensure there will be sufficient memory to recover from
792 * any failures.
793 *
794 * TODO: the scope of the can be reduced but will require restructuring
795 * to keep a single unlock point.
796 */
Andrew Sculle1322792019-07-01 17:46:10 +0100797 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000798
799 /* We only allow these to be setup once. */
800 if (vm->mailbox.send || vm->mailbox.recv) {
801 goto fail;
802 }
803
804 /*
805 * Ensure the pages are valid, owned and exclusive to the VM and that
806 * the VM has the required access to the memory.
807 */
808 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
809 &orig_send_mode) ||
810 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
811 (orig_send_mode & MM_MODE_R) == 0 ||
812 (orig_send_mode & MM_MODE_W) == 0) {
813 goto fail;
814 }
815
816 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
817 &orig_recv_mode) ||
818 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
819 (orig_recv_mode & MM_MODE_R) == 0) {
820 goto fail;
821 }
822
Andrew Sculle1322792019-07-01 17:46:10 +0100823 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
824 orig_send_mode, pa_recv_begin, pa_recv_end,
825 orig_recv_mode)) {
826 goto fail;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100827 }
828
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000829 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100830 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000831 goto exit;
832
Andrew Scull220e6212018-12-21 18:09:00 +0000833fail:
834 ret = -1;
835
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100836exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100837 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100838
839 return ret;
840}
841
842/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100843 * Copies data from the sender's send buffer to the recipient's receive buffer
844 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000845 *
846 * If the recipient's receive buffer is busy, it can optionally register the
847 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100848 */
Jose Marinho75509b42019-04-09 09:34:59 +0100849spci_return_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
850 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100851{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100852 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100853 struct vm *to;
Jose Marinho75509b42019-04-09 09:34:59 +0100854
855 struct two_vm_locked vm_from_to_lock;
856
Andrew Scullb06d1752019-02-04 10:15:48 +0000857 struct hf_vcpu_run_return primary_ret = {
858 .code = HF_VCPU_RUN_MESSAGE,
859 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000860 struct spci_message from_msg_replica;
861 struct spci_message *to_msg;
862 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100863
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000864 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100865
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000866 int64_t ret;
867 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
868 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100869
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000870 /*
871 * Check that the sender has configured its send buffer. Copy the
872 * message header. If the tx mailbox at from_msg is configured (i.e.
873 * from_msg != NULL) then it can be safely accessed after releasing the
874 * lock since the tx mailbox address can only be configured once.
875 */
876 sl_lock(&from->lock);
877 from_msg = from->mailbox.send;
878 sl_unlock(&from->lock);
879
880 if (from_msg == NULL) {
881 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100882 }
883
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100884 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000885 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100886 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000887 from_msg_replica = *from_msg;
888
889 /* Ensure source VM id corresponds to the current VM. */
890 if (from_msg_replica.source_vm_id != from->id) {
891 return SPCI_INVALID_PARAMETERS;
892 }
893
894 size = from_msg_replica.length;
895 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100896 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000897 return SPCI_INVALID_PARAMETERS;
898 }
899
900 /* Disallow reflexive requests as this suggests an error in the VM. */
901 if (from_msg_replica.target_vm_id == from->id) {
902 return SPCI_INVALID_PARAMETERS;
903 }
904
905 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100906 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000907 if (to == NULL) {
908 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100909 }
910
Jose Marinho75509b42019-04-09 09:34:59 +0100911 /*
912 * Hf needs to hold the lock on <to> before the mailbox state is
913 * checked. The lock on <to> must be held until the information is
914 * copied to <to> Rx buffer. Since in
915 * spci_msg_handle_architected_message we may call api_spci_share_memory
916 * which must hold the <from> lock, we must hold the <from> lock at this
917 * point to prevent a deadlock scenario.
918 */
919 vm_from_to_lock = vm_lock_both(to, from);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100920
Andrew Sculld6ee1102019-04-05 22:12:42 +0100921 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100922 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000923 /*
924 * Fail if the target isn't currently ready to receive data,
925 * setting up for notification if requested.
926 */
927 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000928 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000929 &current->vm->wait_entries
930 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000931
932 /* Append waiter only if it's not there yet. */
933 if (list_empty(&entry->wait_links)) {
934 list_append(&to->mailbox.waiter_list,
935 &entry->wait_links);
936 }
937 }
938
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000939 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100940 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100941 }
942
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000943 to_msg = to->mailbox.recv;
Jose Marinho75509b42019-04-09 09:34:59 +0100944
945 /* Handle architected messages. */
946 if ((from_msg_replica.flags & SPCI_MESSAGE_IMPDEF_MASK) !=
947 SPCI_MESSAGE_IMPDEF) {
948 /*
949 * Buffer holding the internal copy of the shared memory
950 * regions.
951 */
Jose Marinho20713fa2019-08-07 15:42:07 +0100952 uint8_t *message_buffer = cpu_get_buffer(current->cpu->id);
953 uint32_t message_buffer_size =
954 cpu_get_buffer_size(current->cpu->id);
Jose Marinho75509b42019-04-09 09:34:59 +0100955
956 struct spci_architected_message_header *architected_header =
957 spci_get_architected_message_header(from->mailbox.send);
958
959 const struct spci_architected_message_header
960 *architected_message_replica;
961
Jose Marinho20713fa2019-08-07 15:42:07 +0100962 if (from_msg_replica.length > message_buffer_size) {
Jose Marinho75509b42019-04-09 09:34:59 +0100963 ret = SPCI_INVALID_PARAMETERS;
964 goto out;
965 }
966
967 if (from_msg_replica.length <
968 sizeof(struct spci_architected_message_header)) {
969 ret = SPCI_INVALID_PARAMETERS;
970 goto out;
971 }
972
973 /* Copy the architected message into an internal buffer. */
Jose Marinho20713fa2019-08-07 15:42:07 +0100974 memcpy_s(message_buffer, message_buffer_size,
Jose Marinho75509b42019-04-09 09:34:59 +0100975 architected_header, from_msg_replica.length);
976
977 architected_message_replica =
978 (struct spci_architected_message_header *)
979 message_buffer;
980
981 /*
982 * Note that message_buffer is passed as the third parameter to
983 * spci_msg_handle_architected_message. The execution flow
984 * commencing at spci_msg_handle_architected_message will make
985 * several accesses to fields in message_buffer. The memory area
986 * message_buffer must be exclusively owned by Hf so that TOCTOU
987 * issues do not arise.
988 */
989 ret = spci_msg_handle_architected_message(
990 vm_from_to_lock.vm1, vm_from_to_lock.vm2,
991 architected_message_replica, &from_msg_replica, to_msg);
992
993 if (ret != SPCI_SUCCESS) {
994 goto out;
995 }
996 } else {
997 /* Copy data. */
998 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
999 from->mailbox.send->payload, size);
1000 *to_msg = from_msg_replica;
1001 }
1002
Andrew Scullb06d1752019-02-04 10:15:48 +00001003 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001004 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +01001005
1006 /* Messages for the primary VM are delivered directly. */
1007 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Walbranf1bd6322019-10-03 16:45:11 +01001008 /*
1009 * Only tell the primary VM the size if the message is for it,
1010 * to avoid leaking data about messages for other VMs.
1011 */
1012 primary_ret.message.size = size;
1013
Andrew Sculld6ee1102019-04-05 22:12:42 +01001014 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +00001015 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001016 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001017 goto out;
1018 }
1019
Andrew Sculld6ee1102019-04-05 22:12:42 +01001020 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +01001021
1022 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +00001023 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +00001024 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001025 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001026 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001027
1028out:
Jose Marinho75509b42019-04-09 09:34:59 +01001029 vm_unlock(&vm_from_to_lock.vm1);
1030 vm_unlock(&vm_from_to_lock.vm2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001031
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001032 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001033}
1034
1035/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001036 * Checks whether the vCPU's attempt to block for a message has already been
1037 * interrupted or whether it is allowed to block.
1038 */
1039bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1040{
1041 bool interrupted;
1042
1043 sl_lock(&current->lock);
1044
1045 /*
1046 * Don't block if there are enabled and pending interrupts, to match
1047 * behaviour of wait_for_interrupt.
1048 */
1049 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1050
1051 sl_unlock(&current->lock);
1052
1053 return interrupted;
1054}
1055
1056/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001057 * Receives a message from the mailbox. If one isn't available, this function
1058 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001059 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001060 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001062struct spci_value api_spci_msg_recv(bool block, struct vcpu *current,
1063 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001064{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001065 struct vm *vm = current->vm;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001066 struct spci_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001067
Andrew Scullaa039b32018-10-04 15:02:26 +01001068 /*
1069 * The primary VM will receive messages as a status code from running
1070 * vcpus and must not call this function.
1071 */
Andrew Scull19503262018-09-20 14:48:39 +01001072 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001073 return spci_error(SPCI_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001074 }
1075
1076 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001077
Andrew Scullaa039b32018-10-04 15:02:26 +01001078 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001079 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1080 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001081 return_code = spci_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001082 goto out;
1083 }
1084
1085 /* No pending message so fail if not allowed to block. */
1086 if (!block) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001087 return_code = spci_error(SPCI_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001088 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001089 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001090
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001091 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001092 * From this point onward this call can only be interrupted or a message
1093 * received. If a message is received the return value will be set at
1094 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001095 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001096 return_code = spci_error(SPCI_INTERRUPTED);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001097 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001098 goto out;
1099 }
1100
Andrew Scullaa039b32018-10-04 15:02:26 +01001101 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001102 {
1103 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +00001104 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +00001105 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001106
Andrew Walbranb4816552018-12-05 17:35:42 +00001107 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001108 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001109 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001110out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001111 sl_unlock(&vm->lock);
1112
Jose Marinho3e2442f2019-03-12 13:30:37 +00001113 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001114}
1115
1116/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001117 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1118 * by this function, the caller must have called api_mailbox_send before with
1119 * the notify argument set to true, and this call must have failed because the
1120 * mailbox was not available.
1121 *
1122 * It should be called repeatedly to retrieve a list of VMs.
1123 *
1124 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1125 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001126 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001127int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001128{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001129 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001130 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001131 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001132
1133 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001134 if (list_empty(&vm->mailbox.ready_list)) {
1135 ret = -1;
1136 goto exit;
1137 }
1138
1139 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1140 ready_links);
1141 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001142 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001143
1144exit:
1145 sl_unlock(&vm->lock);
1146 return ret;
1147}
1148
1149/**
1150 * Retrieves the next VM waiting to be notified that the mailbox of the
1151 * specified VM became writable. Only primary VMs are allowed to call this.
1152 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001153 * Returns -1 on failure or if there are no waiters; the VM id of the next
1154 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001155 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001156int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001157{
1158 struct vm *vm;
1159 struct vm_locked locked;
1160 struct wait_entry *entry;
1161 struct vm *waiting_vm;
1162
1163 /* Only primary VMs are allowed to call this function. */
1164 if (current->vm->id != HF_PRIMARY_VM_ID) {
1165 return -1;
1166 }
1167
Andrew Walbran42347a92019-05-09 13:59:03 +01001168 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001169 if (vm == NULL) {
1170 return -1;
1171 }
1172
1173 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001174 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001175 entry = api_fetch_waiter(locked);
1176 vm_unlock(&locked);
1177
1178 if (entry == NULL) {
1179 return -1;
1180 }
1181
1182 /* Enqueue notification to waiting VM. */
1183 waiting_vm = entry->waiting_vm;
1184
1185 sl_lock(&waiting_vm->lock);
1186 if (list_empty(&entry->ready_links)) {
1187 list_append(&waiting_vm->mailbox.ready_list,
1188 &entry->ready_links);
1189 }
1190 sl_unlock(&waiting_vm->lock);
1191
1192 return waiting_vm->id;
1193}
1194
1195/**
1196 * Clears the caller's mailbox so that a new message can be received. The caller
1197 * must have copied out all data they wish to preserve as new messages will
1198 * overwrite the old and will arrive asynchronously.
1199 *
1200 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001201 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001202 * - 0 on success if no further action is needed.
1203 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1204 * up or kick waiters. Waiters should be retrieved by calling
1205 * hf_mailbox_waiter_get.
1206 */
1207int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1208{
1209 struct vm *vm = current->vm;
1210 struct vm_locked locked;
1211 int64_t ret;
1212
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001213 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001214 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001215 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001216 ret = 0;
1217 break;
1218
Andrew Sculld6ee1102019-04-05 22:12:42 +01001219 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001220 ret = -1;
1221 break;
1222
Andrew Sculld6ee1102019-04-05 22:12:42 +01001223 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001224 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001225 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001226 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001227 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001228 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001229
1230 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001231}
Andrew Walbran318f5732018-11-20 16:23:42 +00001232
1233/**
1234 * Enables or disables a given interrupt ID for the calling vCPU.
1235 *
1236 * Returns 0 on success, or -1 if the intid is invalid.
1237 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001238int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001239{
1240 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1241 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001242
Andrew Walbran318f5732018-11-20 16:23:42 +00001243 if (intid >= HF_NUM_INTIDS) {
1244 return -1;
1245 }
1246
1247 sl_lock(&current->lock);
1248 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001249 /*
1250 * If it is pending and was not enabled before, increment the
1251 * count.
1252 */
1253 if (current->interrupts.interrupt_pending[intid_index] &
1254 ~current->interrupts.interrupt_enabled[intid_index] &
1255 intid_mask) {
1256 current->interrupts.enabled_and_pending_count++;
1257 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001258 current->interrupts.interrupt_enabled[intid_index] |=
1259 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001260 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001261 /*
1262 * If it is pending and was enabled before, decrement the count.
1263 */
1264 if (current->interrupts.interrupt_pending[intid_index] &
1265 current->interrupts.interrupt_enabled[intid_index] &
1266 intid_mask) {
1267 current->interrupts.enabled_and_pending_count--;
1268 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001269 current->interrupts.interrupt_enabled[intid_index] &=
1270 ~intid_mask;
1271 }
1272
1273 sl_unlock(&current->lock);
1274 return 0;
1275}
1276
1277/**
1278 * Returns the ID of the next pending interrupt for the calling vCPU, and
1279 * acknowledges it (i.e. marks it as no longer pending). Returns
1280 * HF_INVALID_INTID if there are no pending interrupts.
1281 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001282uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001283{
1284 uint8_t i;
1285 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001286
1287 /*
1288 * Find the first enabled and pending interrupt ID, return it, and
1289 * deactivate it.
1290 */
1291 sl_lock(&current->lock);
1292 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1293 uint32_t enabled_and_pending =
1294 current->interrupts.interrupt_enabled[i] &
1295 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001296
Andrew Walbran318f5732018-11-20 16:23:42 +00001297 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001298 uint8_t bit_index = ctz(enabled_and_pending);
1299 /*
1300 * Mark it as no longer pending and decrement the count.
1301 */
1302 current->interrupts.interrupt_pending[i] &=
1303 ~(1u << bit_index);
1304 current->interrupts.enabled_and_pending_count--;
1305 first_interrupt =
1306 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001307 break;
1308 }
1309 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001310
1311 sl_unlock(&current->lock);
1312 return first_interrupt;
1313}
1314
1315/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001316 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001317 * given VM and vCPU.
1318 */
1319static inline bool is_injection_allowed(uint32_t target_vm_id,
1320 struct vcpu *current)
1321{
1322 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001323
Andrew Walbran318f5732018-11-20 16:23:42 +00001324 /*
1325 * The primary VM is allowed to inject interrupts into any VM. Secondary
1326 * VMs are only allowed to inject interrupts into their own vCPUs.
1327 */
1328 return current_vm_id == HF_PRIMARY_VM_ID ||
1329 current_vm_id == target_vm_id;
1330}
1331
1332/**
1333 * Injects a virtual interrupt of the given ID into the given target vCPU.
1334 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1335 * when the vCPU is next run, which is up to the scheduler.
1336 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001337 * Returns:
1338 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1339 * ID is invalid, or the current VM is not allowed to inject interrupts to
1340 * the target VM.
1341 * - 0 on success if no further action is needed.
1342 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1343 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001344 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001345int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001346 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001347 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001348{
Andrew Walbran318f5732018-11-20 16:23:42 +00001349 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001350 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001351
1352 if (intid >= HF_NUM_INTIDS) {
1353 return -1;
1354 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001355
Andrew Walbran318f5732018-11-20 16:23:42 +00001356 if (target_vm == NULL) {
1357 return -1;
1358 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001359
Andrew Walbran318f5732018-11-20 16:23:42 +00001360 if (target_vcpu_idx >= target_vm->vcpu_count) {
1361 /* The requested vcpu must exist. */
1362 return -1;
1363 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001364
Andrew Walbran318f5732018-11-20 16:23:42 +00001365 if (!is_injection_allowed(target_vm_id, current)) {
1366 return -1;
1367 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001368
Andrew Walbrane1310df2019-04-29 17:28:28 +01001369 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001370
1371 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1372 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001373 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001374}
Andrew Scull6386f252018-12-06 13:29:10 +00001375
1376/**
1377 * Clears a region of physical memory by overwriting it with zeros. The data is
1378 * flushed from the cache so the memory has been cleared across the system.
1379 */
1380static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1381{
1382 /*
1383 * TODO: change this to a cpu local single page window rather than a
1384 * global mapping of the whole range. Such an approach will limit
1385 * the changes to stage-1 tables and will allow only local
1386 * invalidation.
1387 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001388 bool ret;
1389 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
1390 void *ptr =
1391 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001392 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001393
1394 if (!ptr) {
1395 /* TODO: partial defrag of failed range. */
1396 /* Recover any memory consumed in failed mapping. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001397 mm_defrag(stage1_locked, ppool);
1398 goto fail;
Andrew Scull6386f252018-12-06 13:29:10 +00001399 }
1400
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001401 memset_s(ptr, size, 0, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +01001402 arch_mm_flush_dcache(ptr, size);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001403 mm_unmap(stage1_locked, begin, end, ppool);
Andrew Scull6386f252018-12-06 13:29:10 +00001404
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001405 ret = true;
1406 goto out;
1407
1408fail:
1409 ret = false;
1410
1411out:
1412 mm_unlock_stage1(&stage1_locked);
1413
1414 return ret;
Andrew Scull6386f252018-12-06 13:29:10 +00001415}
1416
Jose Marinho75509b42019-04-09 09:34:59 +01001417/** TODO: Move function to spci_architectted_message.c. */
1418/**
1419 * Shares memory from the calling VM with another. The memory can be shared in
1420 * different modes.
1421 *
1422 * This function requires the calling context to hold the <to> and <from> locks.
1423 *
1424 * Returns:
1425 * In case of error one of the following values is returned:
1426 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
1427 * erroneous;
Andrew Walbran379aa722019-10-07 14:16:34 +01001428 * 2) SPCI_NO_MEMORY - Hafnium did not have sufficient memory to complete
Jose Marinho75509b42019-04-09 09:34:59 +01001429 * the request.
1430 * Success is indicated by SPCI_SUCCESS.
1431 */
1432spci_return_t api_spci_share_memory(struct vm_locked to_locked,
1433 struct vm_locked from_locked,
1434 struct spci_memory_region *memory_region,
1435 uint32_t memory_to_attributes,
1436 enum spci_memory_share share)
1437{
1438 struct vm *to = to_locked.vm;
1439 struct vm *from = from_locked.vm;
1440 int orig_from_mode;
1441 int from_mode;
1442 int to_mode;
1443 struct mpool local_page_pool;
1444 int64_t ret;
1445 paddr_t pa_begin;
1446 paddr_t pa_end;
1447 ipaddr_t begin;
1448 ipaddr_t end;
1449
1450 size_t size;
1451
1452 /* Disallow reflexive shares as this suggests an error in the VM. */
1453 if (to == from) {
1454 return SPCI_INVALID_PARAMETERS;
1455 }
1456
1457 /*
1458 * Create a local pool so any freed memory can't be used by another
1459 * thread. This is to ensure the original mapping can be restored if any
1460 * stage of the process fails.
1461 */
1462 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1463
1464 /* Obtain the single contiguous set of pages from the memory_region. */
1465 /* TODO: Add support for multiple constituent regions. */
1466 size = memory_region->constituents[0].page_count * PAGE_SIZE;
1467 begin = ipa_init(memory_region->constituents[0].address);
1468 end = ipa_add(begin, size);
1469
1470 /*
1471 * Check if the state transition is lawful for both VMs involved
1472 * in the memory exchange, ensure that all constituents of a memory
1473 * region being shared are at the same state.
1474 */
1475 if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin,
1476 end, memory_to_attributes, &from_mode,
1477 &to_mode)) {
1478 return SPCI_INVALID_PARAMETERS;
1479 }
1480
1481 pa_begin = pa_from_ipa(begin);
1482 pa_end = pa_from_ipa(end);
1483
1484 /*
1485 * First update the mapping for the sender so there is not overlap with
1486 * the recipient.
1487 */
1488 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1489 NULL, &local_page_pool)) {
1490 ret = SPCI_NO_MEMORY;
1491 goto out;
1492 }
1493
1494 /* Complete the transfer by mapping the memory into the recipient. */
1495 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1496 &local_page_pool)) {
1497 /* TODO: partial defrag of failed range. */
1498 /* Recover any memory consumed in failed mapping. */
1499 mm_vm_defrag(&from->ptable, &local_page_pool);
1500
1501 ret = SPCI_NO_MEMORY;
1502
1503 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1504 orig_from_mode, NULL,
1505 &local_page_pool));
1506
1507 goto out;
1508 }
1509
1510 ret = SPCI_SUCCESS;
1511
1512out:
1513
1514 mpool_fini(&local_page_pool);
1515
1516 return ret;
1517}
1518
Andrew Scull6386f252018-12-06 13:29:10 +00001519/**
1520 * Shares memory from the calling VM with another. The memory can be shared in
1521 * different modes.
1522 *
1523 * TODO: the interface for sharing memory will need to be enhanced to allow
1524 * sharing with different modes e.g. read-only, informing the recipient
1525 * of the memory they have been given, opting to not wipe the memory and
1526 * possibly allowing multiple blocks to be transferred. What this will
1527 * look like is TBD.
1528 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001529int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001530 enum hf_share share, struct vcpu *current)
1531{
1532 struct vm *from = current->vm;
1533 struct vm *to;
1534 int orig_from_mode;
1535 int from_mode;
1536 int to_mode;
1537 ipaddr_t begin;
1538 ipaddr_t end;
1539 paddr_t pa_begin;
1540 paddr_t pa_end;
1541 struct mpool local_page_pool;
1542 int64_t ret;
1543
1544 /* Disallow reflexive shares as this suggests an error in the VM. */
1545 if (vm_id == from->id) {
1546 return -1;
1547 }
1548
1549 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001550 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001551 if (to == NULL) {
1552 return -1;
1553 }
1554
1555 begin = addr;
1556 end = ipa_add(addr, size);
1557
1558 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001559 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1560 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001561 return -1;
1562 }
1563
1564 /* Convert the sharing request to memory management modes. */
1565 switch (share) {
1566 case HF_MEMORY_GIVE:
1567 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1568 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1569 break;
1570
1571 case HF_MEMORY_LEND:
1572 from_mode = MM_MODE_INVALID;
1573 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1574 break;
1575
1576 case HF_MEMORY_SHARE:
1577 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1578 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1579 MM_MODE_SHARED;
1580 break;
1581
1582 default:
1583 /* The input is untrusted so might not be a valid value. */
1584 return -1;
1585 }
1586
1587 /*
1588 * Create a local pool so any freed memory can't be used by another
1589 * thread. This is to ensure the original mapping can be restored if any
1590 * stage of the process fails.
1591 */
1592 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1593
1594 sl_lock_both(&from->lock, &to->lock);
1595
1596 /*
1597 * Ensure that the memory range is mapped with the same mode so that
1598 * changes can be reverted if the process fails.
1599 */
1600 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1601 goto fail;
1602 }
1603
Andrew Scullb5f49e02019-10-02 13:20:47 +01001604 /* Ensure the address range is normal memory and not a device. */
1605 if (orig_from_mode & MM_MODE_D) {
1606 goto fail;
1607 }
1608
Andrew Scull6386f252018-12-06 13:29:10 +00001609 /*
1610 * Ensure the memory range is valid for the sender. If it isn't, the
1611 * sender has either shared it with another VM already or has no claim
1612 * to the memory.
1613 */
1614 if (orig_from_mode & MM_MODE_INVALID) {
1615 goto fail;
1616 }
1617
1618 /*
1619 * The sender must own the memory and have exclusive access to it in
1620 * order to share it. Alternatively, it is giving memory back to the
1621 * owning VM.
1622 */
1623 if (orig_from_mode & MM_MODE_UNOWNED) {
1624 int orig_to_mode;
1625
1626 if (share != HF_MEMORY_GIVE ||
1627 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1628 orig_to_mode & MM_MODE_UNOWNED) {
1629 goto fail;
1630 }
1631 } else if (orig_from_mode & MM_MODE_SHARED) {
1632 goto fail;
1633 }
1634
1635 pa_begin = pa_from_ipa(begin);
1636 pa_end = pa_from_ipa(end);
1637
1638 /*
1639 * First update the mapping for the sender so there is not overlap with
1640 * the recipient.
1641 */
1642 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1643 NULL, &local_page_pool)) {
1644 goto fail;
1645 }
1646
1647 /* Clear the memory so no VM or device can see the previous contents. */
1648 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1649 goto fail_return_to_sender;
1650 }
1651
1652 /* Complete the transfer by mapping the memory into the recipient. */
1653 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1654 &local_page_pool)) {
1655 /* TODO: partial defrag of failed range. */
1656 /* Recover any memory consumed in failed mapping. */
1657 mm_vm_defrag(&from->ptable, &local_page_pool);
1658 goto fail_return_to_sender;
1659 }
1660
1661 ret = 0;
1662 goto out;
1663
1664fail_return_to_sender:
Andrew Scull7e8de322019-07-02 13:00:56 +01001665 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1666 orig_from_mode, NULL, &local_page_pool));
Andrew Scull6386f252018-12-06 13:29:10 +00001667
1668fail:
1669 ret = -1;
1670
1671out:
1672 sl_unlock(&from->lock);
1673 sl_unlock(&to->lock);
1674
1675 mpool_fini(&local_page_pool);
1676
1677 return ret;
1678}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001679
1680/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001681struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001682{
1683 /*
1684 * Ensure that both major and minor revision representation occupies at
1685 * most 15 bits.
1686 */
1687 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1688 "Major revision representation take more than 15 bits.");
1689 static_assert(0x10000 > SPCI_VERSION_MINOR,
1690 "Minor revision representation take more than 16 bits.");
1691
Andrew Walbran7f920af2019-09-03 17:09:30 +01001692 struct spci_value ret = {
1693 .func = SPCI_SUCCESS_32,
1694 .arg1 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1695 SPCI_VERSION_MINOR};
1696 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001697}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001698
1699int64_t api_debug_log(char c, struct vcpu *current)
1700{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001701 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001702 struct vm *vm = current->vm;
1703 struct vm_locked vm_locked = vm_lock(vm);
1704
Andrew Sculld54e1be2019-08-20 11:09:42 +01001705 if (c == '\n' || c == '\0') {
1706 flush = true;
1707 } else {
1708 vm->log_buffer[vm->log_buffer_length++] = c;
1709 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1710 }
1711
1712 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001713 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1714 vm->log_buffer_length);
1715 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001716 }
1717
1718 vm_unlock(&vm_locked);
1719
1720 return 0;
1721}