blob: 0db632a4cc494b3b09496b9fe60364c7e5d30f06 [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 Scull6d2db332018-10-10 15:28:17 +010094 arch_regs_set_retval(&next->regs,
95 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010096
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000097 /* Mark the current vcpu as waiting. */
98 sl_lock(&current->lock);
99 current->state = secondary_state;
100 sl_unlock(&current->lock);
101
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100102 return next;
103}
104
105/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000106 * Returns to the primary vm and signals that the vcpu still has work to do so.
107 */
108struct vcpu *api_preempt(struct vcpu *current)
109{
110 struct hf_vcpu_run_return ret = {
111 .code = HF_VCPU_RUN_PREEMPTED,
112 };
113
Andrew Sculld6ee1102019-04-05 22:12:42 +0100114 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000115}
116
117/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100118 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
119 * vm.
120 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100121struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100122{
Andrew Scull6d2db332018-10-10 15:28:17 +0100123 struct hf_vcpu_run_return ret = {
124 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
125 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000126
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000127 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100128 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100129}
130
131/**
Andrew Walbran33645652019-04-15 12:29:31 +0100132 * Puts the current vCPU in off mode, and returns to the primary VM.
133 */
134struct vcpu *api_vcpu_off(struct vcpu *current)
135{
136 struct hf_vcpu_run_return ret = {
137 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
138 };
139
140 /*
141 * Disable the timer, so the scheduler doesn't get told to call back
142 * based on it.
143 */
144 arch_timer_disable_current();
145
146 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
147}
148
149/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000150 * Returns to the primary vm to allow this cpu to be used for other tasks as the
151 * vcpu does not have work to do at this moment. The current vcpu is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100152 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000153 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100154void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000155{
Andrew Walbran16075b62019-09-03 17:11:07 +0100156 struct hf_vcpu_run_return primary_ret = {
Andrew Scull66d62bf2019-02-01 13:54:10 +0000157 .code = HF_VCPU_RUN_YIELD,
158 };
159
160 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000161 /* Noop on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100162 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000163 }
164
Andrew Walbran16075b62019-09-03 17:11:07 +0100165 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000166}
167
168/**
Andrew Walbran33645652019-04-15 12:29:31 +0100169 * Switches to the primary so that it can switch to the target, or kick it if it
170 * is already running on a different physical CPU.
171 */
172struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
173{
174 struct hf_vcpu_run_return ret = {
175 .code = HF_VCPU_RUN_WAKE_UP,
176 .wake_up.vm_id = target_vcpu->vm->id,
177 .wake_up.vcpu = vcpu_index(target_vcpu),
178 };
179 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
180}
181
182/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000183 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000184 */
185struct vcpu *api_abort(struct vcpu *current)
186{
187 struct hf_vcpu_run_return ret = {
188 .code = HF_VCPU_RUN_ABORTED,
189 };
190
191 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
192
193 if (current->vm->id == HF_PRIMARY_VM_ID) {
194 /* TODO: what to do when the primary aborts? */
195 for (;;) {
196 /* Do nothing. */
197 }
198 }
199
200 atomic_store_explicit(&current->vm->aborting, true,
201 memory_order_relaxed);
202
203 /* TODO: free resources once all vCPUs abort. */
204
Andrew Sculld6ee1102019-04-05 22:12:42 +0100205 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000206}
207
208/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000209 * Returns the ID of the VM.
210 */
Andrew Walbranfc6cd9d2019-06-25 15:23:27 +0100211spci_vm_id_t api_vm_get_id(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000212{
213 return current->vm->id;
214}
215
216/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100217 * Returns the number of VMs configured to run.
218 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100219spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100220{
Andrew Scull19503262018-09-20 14:48:39 +0100221 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100222}
223
224/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100225 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
226 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100227 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100228spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
229 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100230{
Andrew Scull19503262018-09-20 14:48:39 +0100231 struct vm *vm;
232
233 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100234 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100235 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100236 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100237
Andrew Walbran42347a92019-05-09 13:59:03 +0100238 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100239 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100240 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100241 }
242
243 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100244}
245
246/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000247 * This function is called by the architecture-specific context switching
248 * function to indicate that register state for the given vcpu has been saved
249 * and can therefore be used by other pcpus.
250 */
251void api_regs_state_saved(struct vcpu *vcpu)
252{
253 sl_lock(&vcpu->lock);
254 vcpu->regs_available = true;
255 sl_unlock(&vcpu->lock);
256}
257
258/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000259 * Retrieves the next waiter and removes it from the wait list if the VM's
260 * mailbox is in a writable state.
261 */
262static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
263{
264 struct wait_entry *entry;
265 struct vm *vm = locked_vm.vm;
266
Andrew Sculld6ee1102019-04-05 22:12:42 +0100267 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000268 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
269 /* The mailbox is not writable or there are no waiters. */
270 return NULL;
271 }
272
273 /* Remove waiter from the wait list. */
274 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
275 wait_links);
276 list_remove(&entry->wait_links);
277 return entry;
278}
279
280/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000281 * Assuming that the arguments have already been checked by the caller, injects
282 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
283 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
284 * is next run, which is up to the scheduler.
285 *
286 * Returns:
287 * - 0 on success if no further action is needed.
288 * - 1 if it was called by the primary VM and the primary VM now needs to wake
289 * up or kick the target vCPU.
290 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100291static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000292 uint32_t intid, struct vcpu *current,
293 struct vcpu **next)
294{
295 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
296 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000297 int64_t ret = 0;
298
299 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000300
301 /*
302 * We only need to change state and (maybe) trigger a virtual IRQ if it
303 * is enabled and was not previously pending. Otherwise we can skip
304 * everything except setting the pending bit.
305 *
306 * If you change this logic make sure to update the need_vm_lock logic
307 * above to match.
308 */
309 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
310 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
311 intid_mask)) {
312 goto out;
313 }
314
315 /* Increment the count. */
316 target_vcpu->interrupts.enabled_and_pending_count++;
317
318 /*
319 * Only need to update state if there was not already an
320 * interrupt enabled and pending.
321 */
322 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
323 goto out;
324 }
325
Andrew Walbran508e63c2018-12-20 17:02:37 +0000326 if (current->vm->id == HF_PRIMARY_VM_ID) {
327 /*
328 * If the call came from the primary VM, let it know that it
329 * should run or kick the target vCPU.
330 */
331 ret = 1;
332 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100333 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000334 }
335
336out:
337 /* Either way, make it pending. */
338 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
339
340 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000341
342 return ret;
343}
344
345/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000346 * Prepares the vcpu to run by updating its state and fetching whether a return
347 * value needs to be forced onto the vCPU.
348 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000349static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000350 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000351{
Andrew Scullb06d1752019-02-04 10:15:48 +0000352 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000353 bool ret;
354
Andrew Scullb06d1752019-02-04 10:15:48 +0000355 /*
Andrew Scull4caadaf2019-07-03 13:13:47 +0100356 * Wait until the registers become available. All locks must be released
357 * between iterations of this loop to avoid potential deadlocks if, on
358 * any path, a lock needs to be taken after taking the decision to
359 * switch context but before the registers have been saved.
Andrew Scullb06d1752019-02-04 10:15:48 +0000360 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100361 * The VM lock is not needed in the common case so it must only be taken
362 * when it is going to be needed. This ensures there are no inter-vCPU
363 * dependencies in the common run case meaning the sensitive context
364 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000365 */
366 for (;;) {
367 sl_lock(&vcpu->lock);
368
369 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100370 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000371 if (need_vm_lock) {
372 sl_unlock(&vcpu->lock);
373 sl_lock(&vcpu->vm->lock);
374 sl_lock(&vcpu->lock);
375 }
376
377 if (vcpu->regs_available) {
378 break;
379 }
380
Andrew Sculld6ee1102019-04-05 22:12:42 +0100381 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000382 /*
383 * vCPU is running on another pCPU.
384 *
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100385 * It's ok not to return the sleep duration here because
Andrew Scullb06d1752019-02-04 10:15:48 +0000386 * the other physical CPU that is currently running this
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100387 * vCPU will return the sleep duration if needed. The
388 * default return value is
389 * HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no need to set it
390 * explicitly.
Andrew Scullb06d1752019-02-04 10:15:48 +0000391 */
392 ret = false;
393 goto out;
394 }
395
396 sl_unlock(&vcpu->lock);
397 if (need_vm_lock) {
398 sl_unlock(&vcpu->vm->lock);
399 }
400 }
Andrew Scull9726c252019-01-23 13:44:19 +0000401
402 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100403 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000404 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
405 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100406 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000407 }
408 ret = false;
409 goto out;
410 }
411
Andrew Walbran508e63c2018-12-20 17:02:37 +0000412 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100413 case VCPU_STATE_RUNNING:
414 case VCPU_STATE_OFF:
415 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000416 ret = false;
417 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000418
Andrew Sculld6ee1102019-04-05 22:12:42 +0100419 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000420 /*
421 * A pending message allows the vCPU to run so the message can
422 * be delivered directly.
423 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100424 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000425 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100426 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000427 break;
428 }
429 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100430 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000431 /* Allow virtual interrupts to be delivered. */
432 if (vcpu->interrupts.enabled_and_pending_count > 0) {
433 break;
434 }
435
436 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000437 if (arch_timer_pending(&vcpu->regs)) {
438 break;
439 }
440
441 /*
442 * The vCPU is not ready to run, return the appropriate code to
443 * the primary which called vcpu_run.
444 */
445 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000446 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100447 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000448 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
449 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000450 run_ret->sleep.ns =
451 arch_timer_remaining_ns(&vcpu->regs);
452 }
453
454 ret = false;
455 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000456
Andrew Sculld6ee1102019-04-05 22:12:42 +0100457 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000458 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000459 }
460
Andrew Scullb06d1752019-02-04 10:15:48 +0000461 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000462 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100463 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000464
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000465 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000466 * Mark the registers as unavailable now that we're about to reflect
467 * them onto the real registers. This will also prevent another physical
468 * CPU from trying to read these registers.
469 */
470 vcpu->regs_available = false;
471
472 ret = true;
473
474out:
475 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000476 if (need_vm_lock) {
477 sl_unlock(&vcpu->vm->lock);
478 }
479
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000480 return ret;
481}
482
483/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100484 * Runs the given vcpu of the given vm.
485 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100486struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id,
487 spci_vcpu_index_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000488 const struct vcpu *current,
489 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100490{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100491 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100492 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100493 struct hf_vcpu_run_return ret = {
494 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000495 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100496 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100497
498 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100499 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100500 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100501 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100502
Andrew Scull19503262018-09-20 14:48:39 +0100503 /* Only secondary VM vcpus can be run. */
504 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100505 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100506 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100507
Andrew Scull19503262018-09-20 14:48:39 +0100508 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100509 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100510 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100511 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100512 }
513
514 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100515 if (vcpu_idx >= vm->vcpu_count) {
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
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000519 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100520 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000521 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000522 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100523 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000524
Andrew Walbran508e63c2018-12-20 17:02:37 +0000525 /*
526 * Inject timer interrupt if timer has expired. It's safe to access
527 * vcpu->regs here because api_vcpu_prepare_run already made sure that
528 * regs_available was true (and then set it to false) before returning
529 * true.
530 */
531 if (arch_timer_pending(&vcpu->regs)) {
532 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100533 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
534 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000535
536 /*
537 * Set the mask bit so the hardware interrupt doesn't fire
538 * again. Ideally we wouldn't do this because it affects what
539 * the secondary vCPU sees, but if we don't then we end up with
540 * a loop of the interrupt firing each time we try to return to
541 * the secondary vCPU.
542 */
543 arch_timer_mask(&vcpu->regs);
544 }
545
Andrew Scull33fecd32019-01-08 14:48:27 +0000546 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000547 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000548
Andrew Scull33fecd32019-01-08 14:48:27 +0000549 /*
550 * Set a placeholder return code to the scheduler. This will be
551 * overwritten when the switch back to the primary occurs.
552 */
553 ret.code = HF_VCPU_RUN_PREEMPTED;
554
Andrew Scull6d2db332018-10-10 15:28:17 +0100555out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100556 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100557}
558
559/**
Andrew Scull81e85092018-12-12 12:56:20 +0000560 * Check that the mode indicates memory that is valid, owned and exclusive.
561 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000562static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000563{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100564 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
565 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000566}
567
568/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000569 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
570 * after they've succeeded. If a secondary VM is running and there are waiters,
571 * it also switches back to the primary VM for it to wake waiters up.
572 */
573static int64_t api_waiter_result(struct vm_locked locked_vm,
574 struct vcpu *current, struct vcpu **next)
575{
576 struct vm *vm = locked_vm.vm;
577 struct hf_vcpu_run_return ret = {
578 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
579 };
580
581 if (list_empty(&vm->mailbox.waiter_list)) {
582 /* No waiters, nothing else to do. */
583 return 0;
584 }
585
586 if (vm->id == HF_PRIMARY_VM_ID) {
587 /* The caller is the primary VM. Tell it to wake up waiters. */
588 return 1;
589 }
590
591 /*
592 * Switch back to the primary VM, informing it that there are waiters
593 * that need to be notified.
594 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100595 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000596
597 return 0;
598}
599
600/**
Andrew Sculle1322792019-07-01 17:46:10 +0100601 * Configures the hypervisor's stage-1 view of the send and receive pages. The
602 * stage-1 page tables must be locked so memory cannot be taken by another core
603 * which could result in this transaction being unable to roll back in the case
604 * of an error.
605 */
606static bool api_vm_configure_stage1(struct vm_locked vm_locked,
607 paddr_t pa_send_begin, paddr_t pa_send_end,
608 paddr_t pa_recv_begin, paddr_t pa_recv_end,
609 struct mpool *local_page_pool)
610{
611 bool ret;
612 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
613
614 /* Map the send page as read-only in the hypervisor address space. */
615 vm_locked.vm->mailbox.send =
616 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
617 MM_MODE_R, local_page_pool);
618 if (!vm_locked.vm->mailbox.send) {
619 /* TODO: partial defrag of failed range. */
620 /* Recover any memory consumed in failed mapping. */
621 mm_defrag(mm_stage1_locked, local_page_pool);
622 goto fail;
623 }
624
625 /*
626 * Map the receive page as writable in the hypervisor address space. On
627 * failure, unmap the send page before returning.
628 */
629 vm_locked.vm->mailbox.recv =
630 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
631 MM_MODE_W, local_page_pool);
632 if (!vm_locked.vm->mailbox.recv) {
633 /* TODO: partial defrag of failed range. */
634 /* Recover any memory consumed in failed mapping. */
635 mm_defrag(mm_stage1_locked, local_page_pool);
636 goto fail_undo_send;
637 }
638
639 ret = true;
640 goto out;
641
642 /*
643 * The following mappings will not require more memory than is available
644 * in the local pool.
645 */
646fail_undo_send:
647 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100648 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
649 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100650
651fail:
652 ret = false;
653
654out:
655 mm_unlock_stage1(&mm_stage1_locked);
656
657 return ret;
658}
659
660/**
661 * Configures the send and receive pages in the VM stage-2 and hypervisor
662 * stage-1 page tables. Locking of the page tables combined with a local memory
663 * pool ensures there will always be enough memory to recover from any errors
664 * that arise.
665 */
666static bool api_vm_configure_pages(struct vm_locked vm_locked,
667 paddr_t pa_send_begin, paddr_t pa_send_end,
668 int orig_send_mode, paddr_t pa_recv_begin,
669 paddr_t pa_recv_end, int orig_recv_mode)
670{
671 bool ret;
672 struct mpool local_page_pool;
673
674 /*
675 * Create a local pool so any freed memory can't be used by another
676 * thread. This is to ensure the original mapping can be restored if any
677 * stage of the process fails.
678 */
679 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
680
681 /* Take memory ownership away from the VM and mark as shared. */
682 if (!mm_vm_identity_map(
683 &vm_locked.vm->ptable, pa_send_begin, pa_send_end,
684 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
685 NULL, &local_page_pool)) {
686 goto fail;
687 }
688
689 if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
690 pa_recv_end,
691 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
692 NULL, &local_page_pool)) {
693 /* TODO: partial defrag of failed range. */
694 /* Recover any memory consumed in failed mapping. */
695 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
696 goto fail_undo_send;
697 }
698
699 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
700 pa_recv_begin, pa_recv_end,
701 &local_page_pool)) {
702 goto fail_undo_send_and_recv;
703 }
704
705 ret = true;
706 goto out;
707
708 /*
709 * The following mappings will not require more memory than is available
710 * in the local pool.
711 */
712fail_undo_send_and_recv:
Andrew Scull7e8de322019-07-02 13:00:56 +0100713 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
714 pa_recv_end, orig_recv_mode, NULL,
715 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100716
717fail_undo_send:
Andrew Scull7e8de322019-07-02 13:00:56 +0100718 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin,
719 pa_send_end, orig_send_mode, NULL,
720 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100721
722fail:
723 ret = false;
724
725out:
726 mpool_fini(&local_page_pool);
727
728 return ret;
729}
730
731/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100732 * Configures the VM to send/receive data through the specified pages. The pages
733 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000734 *
735 * Returns:
736 * - -1 on failure.
737 * - 0 on success if no further action is needed.
738 * - 1 if it was called by the primary VM and the primary VM now needs to wake
739 * up or kick waiters. Waiters should be retrieved by calling
740 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100741 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000742int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
743 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100744{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100745 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100746 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100747 paddr_t pa_send_begin;
748 paddr_t pa_send_end;
749 paddr_t pa_recv_begin;
750 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000751 int orig_send_mode;
752 int orig_recv_mode;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100753 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100754
755 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000756 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
757 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100758 return -1;
759 }
760
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000761 /* Convert to physical addresses. */
762 pa_send_begin = pa_from_ipa(send);
763 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
764
765 pa_recv_begin = pa_from_ipa(recv);
766 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
767
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100768 /* Fail if the same page is used for the send and receive pages. */
769 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000770 return -1;
771 }
772
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100773 /*
774 * The hypervisor's memory map must be locked for the duration of this
775 * operation to ensure there will be sufficient memory to recover from
776 * any failures.
777 *
778 * TODO: the scope of the can be reduced but will require restructuring
779 * to keep a single unlock point.
780 */
Andrew Sculle1322792019-07-01 17:46:10 +0100781 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000782
783 /* We only allow these to be setup once. */
784 if (vm->mailbox.send || vm->mailbox.recv) {
785 goto fail;
786 }
787
788 /*
789 * Ensure the pages are valid, owned and exclusive to the VM and that
790 * the VM has the required access to the memory.
791 */
792 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
793 &orig_send_mode) ||
794 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
795 (orig_send_mode & MM_MODE_R) == 0 ||
796 (orig_send_mode & MM_MODE_W) == 0) {
797 goto fail;
798 }
799
800 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
801 &orig_recv_mode) ||
802 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
803 (orig_recv_mode & MM_MODE_R) == 0) {
804 goto fail;
805 }
806
Andrew Sculle1322792019-07-01 17:46:10 +0100807 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
808 orig_send_mode, pa_recv_begin, pa_recv_end,
809 orig_recv_mode)) {
810 goto fail;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100811 }
812
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000813 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100814 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000815 goto exit;
816
Andrew Scull220e6212018-12-21 18:09:00 +0000817fail:
818 ret = -1;
819
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100820exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100821 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100822
823 return ret;
824}
825
826/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100827 * Copies data from the sender's send buffer to the recipient's receive buffer
828 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000829 *
830 * If the recipient's receive buffer is busy, it can optionally register the
831 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100832 */
Jose Marinho75509b42019-04-09 09:34:59 +0100833spci_return_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
834 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100835{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100836 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100837 struct vm *to;
Jose Marinho75509b42019-04-09 09:34:59 +0100838
839 struct two_vm_locked vm_from_to_lock;
840
Andrew Scullb06d1752019-02-04 10:15:48 +0000841 struct hf_vcpu_run_return primary_ret = {
842 .code = HF_VCPU_RUN_MESSAGE,
843 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000844 struct spci_message from_msg_replica;
845 struct spci_message *to_msg;
846 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100847
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000848 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100849
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000850 int64_t ret;
851 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
852 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100853
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000854 /*
855 * Check that the sender has configured its send buffer. Copy the
856 * message header. If the tx mailbox at from_msg is configured (i.e.
857 * from_msg != NULL) then it can be safely accessed after releasing the
858 * lock since the tx mailbox address can only be configured once.
859 */
860 sl_lock(&from->lock);
861 from_msg = from->mailbox.send;
862 sl_unlock(&from->lock);
863
864 if (from_msg == NULL) {
865 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100866 }
867
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100868 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000869 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100870 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000871 from_msg_replica = *from_msg;
872
873 /* Ensure source VM id corresponds to the current VM. */
874 if (from_msg_replica.source_vm_id != from->id) {
875 return SPCI_INVALID_PARAMETERS;
876 }
877
878 size = from_msg_replica.length;
879 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100880 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000881 return SPCI_INVALID_PARAMETERS;
882 }
883
884 /* Disallow reflexive requests as this suggests an error in the VM. */
885 if (from_msg_replica.target_vm_id == from->id) {
886 return SPCI_INVALID_PARAMETERS;
887 }
888
889 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100890 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000891 if (to == NULL) {
892 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100893 }
894
Jose Marinho75509b42019-04-09 09:34:59 +0100895 /*
896 * Hf needs to hold the lock on <to> before the mailbox state is
897 * checked. The lock on <to> must be held until the information is
898 * copied to <to> Rx buffer. Since in
899 * spci_msg_handle_architected_message we may call api_spci_share_memory
900 * which must hold the <from> lock, we must hold the <from> lock at this
901 * point to prevent a deadlock scenario.
902 */
903 vm_from_to_lock = vm_lock_both(to, from);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100904
Andrew Sculld6ee1102019-04-05 22:12:42 +0100905 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100906 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000907 /*
908 * Fail if the target isn't currently ready to receive data,
909 * setting up for notification if requested.
910 */
911 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000912 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000913 &current->vm->wait_entries
914 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000915
916 /* Append waiter only if it's not there yet. */
917 if (list_empty(&entry->wait_links)) {
918 list_append(&to->mailbox.waiter_list,
919 &entry->wait_links);
920 }
921 }
922
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000923 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100924 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100925 }
926
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000927 to_msg = to->mailbox.recv;
Jose Marinho75509b42019-04-09 09:34:59 +0100928
929 /* Handle architected messages. */
930 if ((from_msg_replica.flags & SPCI_MESSAGE_IMPDEF_MASK) !=
931 SPCI_MESSAGE_IMPDEF) {
932 /*
933 * Buffer holding the internal copy of the shared memory
934 * regions.
935 */
Jose Marinho20713fa2019-08-07 15:42:07 +0100936 uint8_t *message_buffer = cpu_get_buffer(current->cpu->id);
937 uint32_t message_buffer_size =
938 cpu_get_buffer_size(current->cpu->id);
Jose Marinho75509b42019-04-09 09:34:59 +0100939
940 struct spci_architected_message_header *architected_header =
941 spci_get_architected_message_header(from->mailbox.send);
942
943 const struct spci_architected_message_header
944 *architected_message_replica;
945
Jose Marinho20713fa2019-08-07 15:42:07 +0100946 if (from_msg_replica.length > message_buffer_size) {
Jose Marinho75509b42019-04-09 09:34:59 +0100947 ret = SPCI_INVALID_PARAMETERS;
948 goto out;
949 }
950
951 if (from_msg_replica.length <
952 sizeof(struct spci_architected_message_header)) {
953 ret = SPCI_INVALID_PARAMETERS;
954 goto out;
955 }
956
957 /* Copy the architected message into an internal buffer. */
Jose Marinho20713fa2019-08-07 15:42:07 +0100958 memcpy_s(message_buffer, message_buffer_size,
Jose Marinho75509b42019-04-09 09:34:59 +0100959 architected_header, from_msg_replica.length);
960
961 architected_message_replica =
962 (struct spci_architected_message_header *)
963 message_buffer;
964
965 /*
966 * Note that message_buffer is passed as the third parameter to
967 * spci_msg_handle_architected_message. The execution flow
968 * commencing at spci_msg_handle_architected_message will make
969 * several accesses to fields in message_buffer. The memory area
970 * message_buffer must be exclusively owned by Hf so that TOCTOU
971 * issues do not arise.
972 */
973 ret = spci_msg_handle_architected_message(
974 vm_from_to_lock.vm1, vm_from_to_lock.vm2,
975 architected_message_replica, &from_msg_replica, to_msg);
976
977 if (ret != SPCI_SUCCESS) {
978 goto out;
979 }
980 } else {
981 /* Copy data. */
982 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
983 from->mailbox.send->payload, size);
984 *to_msg = from_msg_replica;
985 }
986
Andrew Scullb06d1752019-02-04 10:15:48 +0000987 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000988 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100989
990 /* Messages for the primary VM are delivered directly. */
991 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100992 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000993 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100994 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100995 goto out;
996 }
997
Andrew Sculld6ee1102019-04-05 22:12:42 +0100998 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +0100999
1000 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +00001001 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +00001002 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001003 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001004 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001005
1006out:
Jose Marinho75509b42019-04-09 09:34:59 +01001007 vm_unlock(&vm_from_to_lock.vm1);
1008 vm_unlock(&vm_from_to_lock.vm2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001009
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001010 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001011}
1012
1013/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001014 * Checks whether the vCPU's attempt to block for a message has already been
1015 * interrupted or whether it is allowed to block.
1016 */
1017bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1018{
1019 bool interrupted;
1020
1021 sl_lock(&current->lock);
1022
1023 /*
1024 * Don't block if there are enabled and pending interrupts, to match
1025 * behaviour of wait_for_interrupt.
1026 */
1027 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1028
1029 sl_unlock(&current->lock);
1030
1031 return interrupted;
1032}
1033
1034/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001035 * Receives a message from the mailbox. If one isn't available, this function
1036 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001037 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001038 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001039 */
Andrew Walbran0de4f162019-09-03 16:44:20 +01001040int32_t api_spci_msg_recv(bool block, struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001041{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001042 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001043 int32_t return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001044
Andrew Scullaa039b32018-10-04 15:02:26 +01001045 /*
1046 * The primary VM will receive messages as a status code from running
1047 * vcpus and must not call this function.
1048 */
Andrew Scull19503262018-09-20 14:48:39 +01001049 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +00001050 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001051 }
1052
1053 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001054
Andrew Scullaa039b32018-10-04 15:02:26 +01001055 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001056 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1057 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001058 return_code = SPCI_SUCCESS;
1059 goto out;
1060 }
1061
1062 /* No pending message so fail if not allowed to block. */
1063 if (!block) {
1064 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +01001065 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001066 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001067
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001068 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001069 * From this point onward this call can only be interrupted or a message
1070 * received. If a message is received the return value will be set at
1071 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001072 */
Jose Marinho3e2442f2019-03-12 13:30:37 +00001073 return_code = SPCI_INTERRUPTED;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001074 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001075 goto out;
1076 }
1077
Andrew Scullaa039b32018-10-04 15:02:26 +01001078 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001079 {
1080 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +00001081 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +00001082 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001083
Andrew Walbranb4816552018-12-05 17:35:42 +00001084 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001085 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001086 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001087out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001088 sl_unlock(&vm->lock);
1089
Jose Marinho3e2442f2019-03-12 13:30:37 +00001090 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001091}
1092
1093/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001094 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1095 * by this function, the caller must have called api_mailbox_send before with
1096 * the notify argument set to true, and this call must have failed because the
1097 * mailbox was not available.
1098 *
1099 * It should be called repeatedly to retrieve a list of VMs.
1100 *
1101 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1102 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001103 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001104int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001105{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001106 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001107 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001108 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001109
1110 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001111 if (list_empty(&vm->mailbox.ready_list)) {
1112 ret = -1;
1113 goto exit;
1114 }
1115
1116 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1117 ready_links);
1118 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001119 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001120
1121exit:
1122 sl_unlock(&vm->lock);
1123 return ret;
1124}
1125
1126/**
1127 * Retrieves the next VM waiting to be notified that the mailbox of the
1128 * specified VM became writable. Only primary VMs are allowed to call this.
1129 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001130 * Returns -1 on failure or if there are no waiters; the VM id of the next
1131 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001132 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001133int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001134{
1135 struct vm *vm;
1136 struct vm_locked locked;
1137 struct wait_entry *entry;
1138 struct vm *waiting_vm;
1139
1140 /* Only primary VMs are allowed to call this function. */
1141 if (current->vm->id != HF_PRIMARY_VM_ID) {
1142 return -1;
1143 }
1144
Andrew Walbran42347a92019-05-09 13:59:03 +01001145 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001146 if (vm == NULL) {
1147 return -1;
1148 }
1149
1150 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001151 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001152 entry = api_fetch_waiter(locked);
1153 vm_unlock(&locked);
1154
1155 if (entry == NULL) {
1156 return -1;
1157 }
1158
1159 /* Enqueue notification to waiting VM. */
1160 waiting_vm = entry->waiting_vm;
1161
1162 sl_lock(&waiting_vm->lock);
1163 if (list_empty(&entry->ready_links)) {
1164 list_append(&waiting_vm->mailbox.ready_list,
1165 &entry->ready_links);
1166 }
1167 sl_unlock(&waiting_vm->lock);
1168
1169 return waiting_vm->id;
1170}
1171
1172/**
1173 * Clears the caller's mailbox so that a new message can be received. The caller
1174 * must have copied out all data they wish to preserve as new messages will
1175 * overwrite the old and will arrive asynchronously.
1176 *
1177 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001178 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001179 * - 0 on success if no further action is needed.
1180 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1181 * up or kick waiters. Waiters should be retrieved by calling
1182 * hf_mailbox_waiter_get.
1183 */
1184int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1185{
1186 struct vm *vm = current->vm;
1187 struct vm_locked locked;
1188 int64_t ret;
1189
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001190 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001191 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001192 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001193 ret = 0;
1194 break;
1195
Andrew Sculld6ee1102019-04-05 22:12:42 +01001196 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001197 ret = -1;
1198 break;
1199
Andrew Sculld6ee1102019-04-05 22:12:42 +01001200 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001201 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001202 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001203 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001204 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001205 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001206
1207 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001208}
Andrew Walbran318f5732018-11-20 16:23:42 +00001209
1210/**
1211 * Enables or disables a given interrupt ID for the calling vCPU.
1212 *
1213 * Returns 0 on success, or -1 if the intid is invalid.
1214 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001215int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001216{
1217 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1218 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001219
Andrew Walbran318f5732018-11-20 16:23:42 +00001220 if (intid >= HF_NUM_INTIDS) {
1221 return -1;
1222 }
1223
1224 sl_lock(&current->lock);
1225 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001226 /*
1227 * If it is pending and was not enabled before, increment the
1228 * count.
1229 */
1230 if (current->interrupts.interrupt_pending[intid_index] &
1231 ~current->interrupts.interrupt_enabled[intid_index] &
1232 intid_mask) {
1233 current->interrupts.enabled_and_pending_count++;
1234 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001235 current->interrupts.interrupt_enabled[intid_index] |=
1236 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001237 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001238 /*
1239 * If it is pending and was enabled before, decrement the count.
1240 */
1241 if (current->interrupts.interrupt_pending[intid_index] &
1242 current->interrupts.interrupt_enabled[intid_index] &
1243 intid_mask) {
1244 current->interrupts.enabled_and_pending_count--;
1245 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001246 current->interrupts.interrupt_enabled[intid_index] &=
1247 ~intid_mask;
1248 }
1249
1250 sl_unlock(&current->lock);
1251 return 0;
1252}
1253
1254/**
1255 * Returns the ID of the next pending interrupt for the calling vCPU, and
1256 * acknowledges it (i.e. marks it as no longer pending). Returns
1257 * HF_INVALID_INTID if there are no pending interrupts.
1258 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001259uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001260{
1261 uint8_t i;
1262 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001263
1264 /*
1265 * Find the first enabled and pending interrupt ID, return it, and
1266 * deactivate it.
1267 */
1268 sl_lock(&current->lock);
1269 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1270 uint32_t enabled_and_pending =
1271 current->interrupts.interrupt_enabled[i] &
1272 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001273
Andrew Walbran318f5732018-11-20 16:23:42 +00001274 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001275 uint8_t bit_index = ctz(enabled_and_pending);
1276 /*
1277 * Mark it as no longer pending and decrement the count.
1278 */
1279 current->interrupts.interrupt_pending[i] &=
1280 ~(1u << bit_index);
1281 current->interrupts.enabled_and_pending_count--;
1282 first_interrupt =
1283 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001284 break;
1285 }
1286 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001287
1288 sl_unlock(&current->lock);
1289 return first_interrupt;
1290}
1291
1292/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001293 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001294 * given VM and vCPU.
1295 */
1296static inline bool is_injection_allowed(uint32_t target_vm_id,
1297 struct vcpu *current)
1298{
1299 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001300
Andrew Walbran318f5732018-11-20 16:23:42 +00001301 /*
1302 * The primary VM is allowed to inject interrupts into any VM. Secondary
1303 * VMs are only allowed to inject interrupts into their own vCPUs.
1304 */
1305 return current_vm_id == HF_PRIMARY_VM_ID ||
1306 current_vm_id == target_vm_id;
1307}
1308
1309/**
1310 * Injects a virtual interrupt of the given ID into the given target vCPU.
1311 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1312 * when the vCPU is next run, which is up to the scheduler.
1313 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001314 * Returns:
1315 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1316 * ID is invalid, or the current VM is not allowed to inject interrupts to
1317 * the target VM.
1318 * - 0 on success if no further action is needed.
1319 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1320 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001321 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001322int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001323 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001324 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001325{
Andrew Walbran318f5732018-11-20 16:23:42 +00001326 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001327 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001328
1329 if (intid >= HF_NUM_INTIDS) {
1330 return -1;
1331 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001332
Andrew Walbran318f5732018-11-20 16:23:42 +00001333 if (target_vm == NULL) {
1334 return -1;
1335 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001336
Andrew Walbran318f5732018-11-20 16:23:42 +00001337 if (target_vcpu_idx >= target_vm->vcpu_count) {
1338 /* The requested vcpu must exist. */
1339 return -1;
1340 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001341
Andrew Walbran318f5732018-11-20 16:23:42 +00001342 if (!is_injection_allowed(target_vm_id, current)) {
1343 return -1;
1344 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001345
Andrew Walbrane1310df2019-04-29 17:28:28 +01001346 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001347
1348 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1349 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001350 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001351}
Andrew Scull6386f252018-12-06 13:29:10 +00001352
1353/**
1354 * Clears a region of physical memory by overwriting it with zeros. The data is
1355 * flushed from the cache so the memory has been cleared across the system.
1356 */
1357static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1358{
1359 /*
1360 * TODO: change this to a cpu local single page window rather than a
1361 * global mapping of the whole range. Such an approach will limit
1362 * the changes to stage-1 tables and will allow only local
1363 * invalidation.
1364 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001365 bool ret;
1366 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
1367 void *ptr =
1368 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001369 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001370
1371 if (!ptr) {
1372 /* TODO: partial defrag of failed range. */
1373 /* Recover any memory consumed in failed mapping. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001374 mm_defrag(stage1_locked, ppool);
1375 goto fail;
Andrew Scull6386f252018-12-06 13:29:10 +00001376 }
1377
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001378 memset_s(ptr, size, 0, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +01001379 arch_mm_flush_dcache(ptr, size);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001380 mm_unmap(stage1_locked, begin, end, ppool);
Andrew Scull6386f252018-12-06 13:29:10 +00001381
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001382 ret = true;
1383 goto out;
1384
1385fail:
1386 ret = false;
1387
1388out:
1389 mm_unlock_stage1(&stage1_locked);
1390
1391 return ret;
Andrew Scull6386f252018-12-06 13:29:10 +00001392}
1393
Jose Marinho75509b42019-04-09 09:34:59 +01001394/** TODO: Move function to spci_architectted_message.c. */
1395/**
1396 * Shares memory from the calling VM with another. The memory can be shared in
1397 * different modes.
1398 *
1399 * This function requires the calling context to hold the <to> and <from> locks.
1400 *
1401 * Returns:
1402 * In case of error one of the following values is returned:
1403 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
1404 * erroneous;
1405 * 2) SPCI_NO_MEMORY - Hf did not have sufficient memory to complete
1406 * the request.
1407 * Success is indicated by SPCI_SUCCESS.
1408 */
1409spci_return_t api_spci_share_memory(struct vm_locked to_locked,
1410 struct vm_locked from_locked,
1411 struct spci_memory_region *memory_region,
1412 uint32_t memory_to_attributes,
1413 enum spci_memory_share share)
1414{
1415 struct vm *to = to_locked.vm;
1416 struct vm *from = from_locked.vm;
1417 int orig_from_mode;
1418 int from_mode;
1419 int to_mode;
1420 struct mpool local_page_pool;
1421 int64_t ret;
1422 paddr_t pa_begin;
1423 paddr_t pa_end;
1424 ipaddr_t begin;
1425 ipaddr_t end;
1426
1427 size_t size;
1428
1429 /* Disallow reflexive shares as this suggests an error in the VM. */
1430 if (to == from) {
1431 return SPCI_INVALID_PARAMETERS;
1432 }
1433
1434 /*
1435 * Create a local pool so any freed memory can't be used by another
1436 * thread. This is to ensure the original mapping can be restored if any
1437 * stage of the process fails.
1438 */
1439 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1440
1441 /* Obtain the single contiguous set of pages from the memory_region. */
1442 /* TODO: Add support for multiple constituent regions. */
1443 size = memory_region->constituents[0].page_count * PAGE_SIZE;
1444 begin = ipa_init(memory_region->constituents[0].address);
1445 end = ipa_add(begin, size);
1446
1447 /*
1448 * Check if the state transition is lawful for both VMs involved
1449 * in the memory exchange, ensure that all constituents of a memory
1450 * region being shared are at the same state.
1451 */
1452 if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin,
1453 end, memory_to_attributes, &from_mode,
1454 &to_mode)) {
1455 return SPCI_INVALID_PARAMETERS;
1456 }
1457
1458 pa_begin = pa_from_ipa(begin);
1459 pa_end = pa_from_ipa(end);
1460
1461 /*
1462 * First update the mapping for the sender so there is not overlap with
1463 * the recipient.
1464 */
1465 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1466 NULL, &local_page_pool)) {
1467 ret = SPCI_NO_MEMORY;
1468 goto out;
1469 }
1470
1471 /* Complete the transfer by mapping the memory into the recipient. */
1472 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1473 &local_page_pool)) {
1474 /* TODO: partial defrag of failed range. */
1475 /* Recover any memory consumed in failed mapping. */
1476 mm_vm_defrag(&from->ptable, &local_page_pool);
1477
1478 ret = SPCI_NO_MEMORY;
1479
1480 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1481 orig_from_mode, NULL,
1482 &local_page_pool));
1483
1484 goto out;
1485 }
1486
1487 ret = SPCI_SUCCESS;
1488
1489out:
1490
1491 mpool_fini(&local_page_pool);
1492
1493 return ret;
1494}
1495
Andrew Scull6386f252018-12-06 13:29:10 +00001496/**
1497 * Shares memory from the calling VM with another. The memory can be shared in
1498 * different modes.
1499 *
1500 * TODO: the interface for sharing memory will need to be enhanced to allow
1501 * sharing with different modes e.g. read-only, informing the recipient
1502 * of the memory they have been given, opting to not wipe the memory and
1503 * possibly allowing multiple blocks to be transferred. What this will
1504 * look like is TBD.
1505 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001506int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001507 enum hf_share share, struct vcpu *current)
1508{
1509 struct vm *from = current->vm;
1510 struct vm *to;
1511 int orig_from_mode;
1512 int from_mode;
1513 int to_mode;
1514 ipaddr_t begin;
1515 ipaddr_t end;
1516 paddr_t pa_begin;
1517 paddr_t pa_end;
1518 struct mpool local_page_pool;
1519 int64_t ret;
1520
1521 /* Disallow reflexive shares as this suggests an error in the VM. */
1522 if (vm_id == from->id) {
1523 return -1;
1524 }
1525
1526 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001527 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001528 if (to == NULL) {
1529 return -1;
1530 }
1531
1532 begin = addr;
1533 end = ipa_add(addr, size);
1534
1535 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001536 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1537 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001538 return -1;
1539 }
1540
1541 /* Convert the sharing request to memory management modes. */
1542 switch (share) {
1543 case HF_MEMORY_GIVE:
1544 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1545 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1546 break;
1547
1548 case HF_MEMORY_LEND:
1549 from_mode = MM_MODE_INVALID;
1550 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1551 break;
1552
1553 case HF_MEMORY_SHARE:
1554 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1555 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1556 MM_MODE_SHARED;
1557 break;
1558
1559 default:
1560 /* The input is untrusted so might not be a valid value. */
1561 return -1;
1562 }
1563
1564 /*
1565 * Create a local pool so any freed memory can't be used by another
1566 * thread. This is to ensure the original mapping can be restored if any
1567 * stage of the process fails.
1568 */
1569 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1570
1571 sl_lock_both(&from->lock, &to->lock);
1572
1573 /*
1574 * Ensure that the memory range is mapped with the same mode so that
1575 * changes can be reverted if the process fails.
1576 */
1577 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1578 goto fail;
1579 }
1580
Andrew Scullb5f49e02019-10-02 13:20:47 +01001581 /* Ensure the address range is normal memory and not a device. */
1582 if (orig_from_mode & MM_MODE_D) {
1583 goto fail;
1584 }
1585
Andrew Scull6386f252018-12-06 13:29:10 +00001586 /*
1587 * Ensure the memory range is valid for the sender. If it isn't, the
1588 * sender has either shared it with another VM already or has no claim
1589 * to the memory.
1590 */
1591 if (orig_from_mode & MM_MODE_INVALID) {
1592 goto fail;
1593 }
1594
1595 /*
1596 * The sender must own the memory and have exclusive access to it in
1597 * order to share it. Alternatively, it is giving memory back to the
1598 * owning VM.
1599 */
1600 if (orig_from_mode & MM_MODE_UNOWNED) {
1601 int orig_to_mode;
1602
1603 if (share != HF_MEMORY_GIVE ||
1604 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1605 orig_to_mode & MM_MODE_UNOWNED) {
1606 goto fail;
1607 }
1608 } else if (orig_from_mode & MM_MODE_SHARED) {
1609 goto fail;
1610 }
1611
1612 pa_begin = pa_from_ipa(begin);
1613 pa_end = pa_from_ipa(end);
1614
1615 /*
1616 * First update the mapping for the sender so there is not overlap with
1617 * the recipient.
1618 */
1619 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1620 NULL, &local_page_pool)) {
1621 goto fail;
1622 }
1623
1624 /* Clear the memory so no VM or device can see the previous contents. */
1625 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1626 goto fail_return_to_sender;
1627 }
1628
1629 /* Complete the transfer by mapping the memory into the recipient. */
1630 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1631 &local_page_pool)) {
1632 /* TODO: partial defrag of failed range. */
1633 /* Recover any memory consumed in failed mapping. */
1634 mm_vm_defrag(&from->ptable, &local_page_pool);
1635 goto fail_return_to_sender;
1636 }
1637
1638 ret = 0;
1639 goto out;
1640
1641fail_return_to_sender:
Andrew Scull7e8de322019-07-02 13:00:56 +01001642 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1643 orig_from_mode, NULL, &local_page_pool));
Andrew Scull6386f252018-12-06 13:29:10 +00001644
1645fail:
1646 ret = -1;
1647
1648out:
1649 sl_unlock(&from->lock);
1650 sl_unlock(&to->lock);
1651
1652 mpool_fini(&local_page_pool);
1653
1654 return ret;
1655}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001656
1657/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001658struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001659{
1660 /*
1661 * Ensure that both major and minor revision representation occupies at
1662 * most 15 bits.
1663 */
1664 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1665 "Major revision representation take more than 15 bits.");
1666 static_assert(0x10000 > SPCI_VERSION_MINOR,
1667 "Minor revision representation take more than 16 bits.");
1668
Andrew Walbran7f920af2019-09-03 17:09:30 +01001669 struct spci_value ret = {
1670 .func = SPCI_SUCCESS_32,
1671 .arg1 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1672 SPCI_VERSION_MINOR};
1673 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001674}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001675
1676int64_t api_debug_log(char c, struct vcpu *current)
1677{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001678 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001679 struct vm *vm = current->vm;
1680 struct vm_locked vm_locked = vm_lock(vm);
1681
Andrew Sculld54e1be2019-08-20 11:09:42 +01001682 if (c == '\n' || c == '\0') {
1683 flush = true;
1684 } else {
1685 vm->log_buffer[vm->log_buffer_length++] = c;
1686 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1687 }
1688
1689 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001690 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1691 vm->log_buffer_length);
1692 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001693 }
1694
1695 vm_unlock(&vm_locked);
1696
1697 return 0;
1698}