blob: a605ac4fe57a74e7d4f086cfd7f45e3210353b60 [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
Jose Marinho135dff32019-02-28 10:25:57 +0000152 * ready to be scheduled again. This SPCI function always returns SPCI_SUCCESS.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000153 */
Jose Marinho135dff32019-02-28 10:25:57 +0000154int32_t api_spci_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000155{
156 struct hf_vcpu_run_return ret = {
157 .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. */
Jose Marinho135dff32019-02-28 10:25:57 +0000162 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000163 }
164
Jose Marinho135dff32019-02-28 10:25:57 +0000165 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
166
167 /* SPCI_YIELD always returns SPCI_SUCCESS. */
168 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000169}
170
171/**
Andrew Walbran33645652019-04-15 12:29:31 +0100172 * Switches to the primary so that it can switch to the target, or kick it if it
173 * is already running on a different physical CPU.
174 */
175struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
176{
177 struct hf_vcpu_run_return ret = {
178 .code = HF_VCPU_RUN_WAKE_UP,
179 .wake_up.vm_id = target_vcpu->vm->id,
180 .wake_up.vcpu = vcpu_index(target_vcpu),
181 };
182 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
183}
184
185/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000186 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000187 */
188struct vcpu *api_abort(struct vcpu *current)
189{
190 struct hf_vcpu_run_return ret = {
191 .code = HF_VCPU_RUN_ABORTED,
192 };
193
194 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
195
196 if (current->vm->id == HF_PRIMARY_VM_ID) {
197 /* TODO: what to do when the primary aborts? */
198 for (;;) {
199 /* Do nothing. */
200 }
201 }
202
203 atomic_store_explicit(&current->vm->aborting, true,
204 memory_order_relaxed);
205
206 /* TODO: free resources once all vCPUs abort. */
207
Andrew Sculld6ee1102019-04-05 22:12:42 +0100208 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000209}
210
211/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000212 * Returns the ID of the VM.
213 */
Andrew Walbranfc6cd9d2019-06-25 15:23:27 +0100214spci_vm_id_t api_vm_get_id(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000215{
216 return current->vm->id;
217}
218
219/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100220 * Returns the number of VMs configured to run.
221 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100222spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100223{
Andrew Scull19503262018-09-20 14:48:39 +0100224 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100225}
226
227/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100228 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
229 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100230 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100231spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
232 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100233{
Andrew Scull19503262018-09-20 14:48:39 +0100234 struct vm *vm;
235
236 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100237 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100238 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100239 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100240
Andrew Walbran42347a92019-05-09 13:59:03 +0100241 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100242 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100243 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100244 }
245
246 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100247}
248
249/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000250 * This function is called by the architecture-specific context switching
251 * function to indicate that register state for the given vcpu has been saved
252 * and can therefore be used by other pcpus.
253 */
254void api_regs_state_saved(struct vcpu *vcpu)
255{
256 sl_lock(&vcpu->lock);
257 vcpu->regs_available = true;
258 sl_unlock(&vcpu->lock);
259}
260
261/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000262 * Retrieves the next waiter and removes it from the wait list if the VM's
263 * mailbox is in a writable state.
264 */
265static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
266{
267 struct wait_entry *entry;
268 struct vm *vm = locked_vm.vm;
269
Andrew Sculld6ee1102019-04-05 22:12:42 +0100270 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000271 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
272 /* The mailbox is not writable or there are no waiters. */
273 return NULL;
274 }
275
276 /* Remove waiter from the wait list. */
277 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
278 wait_links);
279 list_remove(&entry->wait_links);
280 return entry;
281}
282
283/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000284 * Assuming that the arguments have already been checked by the caller, injects
285 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
286 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
287 * is next run, which is up to the scheduler.
288 *
289 * Returns:
290 * - 0 on success if no further action is needed.
291 * - 1 if it was called by the primary VM and the primary VM now needs to wake
292 * up or kick the target vCPU.
293 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100294static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000295 uint32_t intid, struct vcpu *current,
296 struct vcpu **next)
297{
298 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
299 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000300 int64_t ret = 0;
301
302 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000303
304 /*
305 * We only need to change state and (maybe) trigger a virtual IRQ if it
306 * is enabled and was not previously pending. Otherwise we can skip
307 * everything except setting the pending bit.
308 *
309 * If you change this logic make sure to update the need_vm_lock logic
310 * above to match.
311 */
312 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
313 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
314 intid_mask)) {
315 goto out;
316 }
317
318 /* Increment the count. */
319 target_vcpu->interrupts.enabled_and_pending_count++;
320
321 /*
322 * Only need to update state if there was not already an
323 * interrupt enabled and pending.
324 */
325 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
326 goto out;
327 }
328
Andrew Walbran508e63c2018-12-20 17:02:37 +0000329 if (current->vm->id == HF_PRIMARY_VM_ID) {
330 /*
331 * If the call came from the primary VM, let it know that it
332 * should run or kick the target vCPU.
333 */
334 ret = 1;
335 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100336 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000337 }
338
339out:
340 /* Either way, make it pending. */
341 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
342
343 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000344
345 return ret;
346}
347
348/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000349 * Prepares the vcpu to run by updating its state and fetching whether a return
350 * value needs to be forced onto the vCPU.
351 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000352static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000353 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000354{
Andrew Scullb06d1752019-02-04 10:15:48 +0000355 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000356 bool ret;
357
Andrew Scullb06d1752019-02-04 10:15:48 +0000358 /*
Andrew Scull4caadaf2019-07-03 13:13:47 +0100359 * Wait until the registers become available. All locks must be released
360 * between iterations of this loop to avoid potential deadlocks if, on
361 * any path, a lock needs to be taken after taking the decision to
362 * switch context but before the registers have been saved.
Andrew Scullb06d1752019-02-04 10:15:48 +0000363 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100364 * The VM lock is not needed in the common case so it must only be taken
365 * when it is going to be needed. This ensures there are no inter-vCPU
366 * dependencies in the common run case meaning the sensitive context
367 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000368 */
369 for (;;) {
370 sl_lock(&vcpu->lock);
371
372 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100373 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000374 if (need_vm_lock) {
375 sl_unlock(&vcpu->lock);
376 sl_lock(&vcpu->vm->lock);
377 sl_lock(&vcpu->lock);
378 }
379
380 if (vcpu->regs_available) {
381 break;
382 }
383
Andrew Sculld6ee1102019-04-05 22:12:42 +0100384 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000385 /*
386 * vCPU is running on another pCPU.
387 *
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100388 * It's ok not to return the sleep duration here because
Andrew Scullb06d1752019-02-04 10:15:48 +0000389 * the other physical CPU that is currently running this
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100390 * vCPU will return the sleep duration if needed. The
391 * default return value is
392 * HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no need to set it
393 * explicitly.
Andrew Scullb06d1752019-02-04 10:15:48 +0000394 */
395 ret = false;
396 goto out;
397 }
398
399 sl_unlock(&vcpu->lock);
400 if (need_vm_lock) {
401 sl_unlock(&vcpu->vm->lock);
402 }
403 }
Andrew Scull9726c252019-01-23 13:44:19 +0000404
405 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100406 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000407 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
408 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100409 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000410 }
411 ret = false;
412 goto out;
413 }
414
Andrew Walbran508e63c2018-12-20 17:02:37 +0000415 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100416 case VCPU_STATE_RUNNING:
417 case VCPU_STATE_OFF:
418 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000419 ret = false;
420 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000421
Andrew Sculld6ee1102019-04-05 22:12:42 +0100422 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000423 /*
424 * A pending message allows the vCPU to run so the message can
425 * be delivered directly.
426 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100427 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000428 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100429 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000430 break;
431 }
432 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100433 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000434 /* Allow virtual interrupts to be delivered. */
435 if (vcpu->interrupts.enabled_and_pending_count > 0) {
436 break;
437 }
438
439 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000440 if (arch_timer_pending(&vcpu->regs)) {
441 break;
442 }
443
444 /*
445 * The vCPU is not ready to run, return the appropriate code to
446 * the primary which called vcpu_run.
447 */
448 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000449 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100450 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000451 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
452 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000453 run_ret->sleep.ns =
454 arch_timer_remaining_ns(&vcpu->regs);
455 }
456
457 ret = false;
458 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000459
Andrew Sculld6ee1102019-04-05 22:12:42 +0100460 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000461 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000462 }
463
Andrew Scullb06d1752019-02-04 10:15:48 +0000464 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000465 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100466 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000467
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000468 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000469 * Mark the registers as unavailable now that we're about to reflect
470 * them onto the real registers. This will also prevent another physical
471 * CPU from trying to read these registers.
472 */
473 vcpu->regs_available = false;
474
475 ret = true;
476
477out:
478 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000479 if (need_vm_lock) {
480 sl_unlock(&vcpu->vm->lock);
481 }
482
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000483 return ret;
484}
485
486/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100487 * Runs the given vcpu of the given vm.
488 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100489struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id,
490 spci_vcpu_index_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000491 const struct vcpu *current,
492 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100493{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100494 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100495 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100496 struct hf_vcpu_run_return ret = {
497 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000498 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100499 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100500
501 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100502 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100503 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100504 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100505
Andrew Scull19503262018-09-20 14:48:39 +0100506 /* Only secondary VM vcpus can be run. */
507 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100508 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100509 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100510
Andrew Scull19503262018-09-20 14:48:39 +0100511 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100512 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100513 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100514 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100515 }
516
517 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100518 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100519 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100520 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100521
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000522 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100523 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000524 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000525 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100526 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000527
Andrew Walbran508e63c2018-12-20 17:02:37 +0000528 /*
529 * Inject timer interrupt if timer has expired. It's safe to access
530 * vcpu->regs here because api_vcpu_prepare_run already made sure that
531 * regs_available was true (and then set it to false) before returning
532 * true.
533 */
534 if (arch_timer_pending(&vcpu->regs)) {
535 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100536 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
537 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000538
539 /*
540 * Set the mask bit so the hardware interrupt doesn't fire
541 * again. Ideally we wouldn't do this because it affects what
542 * the secondary vCPU sees, but if we don't then we end up with
543 * a loop of the interrupt firing each time we try to return to
544 * the secondary vCPU.
545 */
546 arch_timer_mask(&vcpu->regs);
547 }
548
Andrew Scull33fecd32019-01-08 14:48:27 +0000549 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000550 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000551
Andrew Scull33fecd32019-01-08 14:48:27 +0000552 /*
553 * Set a placeholder return code to the scheduler. This will be
554 * overwritten when the switch back to the primary occurs.
555 */
556 ret.code = HF_VCPU_RUN_PREEMPTED;
557
Andrew Scull6d2db332018-10-10 15:28:17 +0100558out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100559 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100560}
561
562/**
Andrew Scull81e85092018-12-12 12:56:20 +0000563 * Check that the mode indicates memory that is valid, owned and exclusive.
564 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000565static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000566{
567 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
568 0;
569}
570
571/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000572 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
573 * after they've succeeded. If a secondary VM is running and there are waiters,
574 * it also switches back to the primary VM for it to wake waiters up.
575 */
576static int64_t api_waiter_result(struct vm_locked locked_vm,
577 struct vcpu *current, struct vcpu **next)
578{
579 struct vm *vm = locked_vm.vm;
580 struct hf_vcpu_run_return ret = {
581 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
582 };
583
584 if (list_empty(&vm->mailbox.waiter_list)) {
585 /* No waiters, nothing else to do. */
586 return 0;
587 }
588
589 if (vm->id == HF_PRIMARY_VM_ID) {
590 /* The caller is the primary VM. Tell it to wake up waiters. */
591 return 1;
592 }
593
594 /*
595 * Switch back to the primary VM, informing it that there are waiters
596 * that need to be notified.
597 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100598 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000599
600 return 0;
601}
602
603/**
Andrew Sculle1322792019-07-01 17:46:10 +0100604 * Configures the hypervisor's stage-1 view of the send and receive pages. The
605 * stage-1 page tables must be locked so memory cannot be taken by another core
606 * which could result in this transaction being unable to roll back in the case
607 * of an error.
608 */
609static bool api_vm_configure_stage1(struct vm_locked vm_locked,
610 paddr_t pa_send_begin, paddr_t pa_send_end,
611 paddr_t pa_recv_begin, paddr_t pa_recv_end,
612 struct mpool *local_page_pool)
613{
614 bool ret;
615 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
616
617 /* Map the send page as read-only in the hypervisor address space. */
618 vm_locked.vm->mailbox.send =
619 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
620 MM_MODE_R, local_page_pool);
621 if (!vm_locked.vm->mailbox.send) {
622 /* TODO: partial defrag of failed range. */
623 /* Recover any memory consumed in failed mapping. */
624 mm_defrag(mm_stage1_locked, local_page_pool);
625 goto fail;
626 }
627
628 /*
629 * Map the receive page as writable in the hypervisor address space. On
630 * failure, unmap the send page before returning.
631 */
632 vm_locked.vm->mailbox.recv =
633 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
634 MM_MODE_W, local_page_pool);
635 if (!vm_locked.vm->mailbox.recv) {
636 /* TODO: partial defrag of failed range. */
637 /* Recover any memory consumed in failed mapping. */
638 mm_defrag(mm_stage1_locked, local_page_pool);
639 goto fail_undo_send;
640 }
641
642 ret = true;
643 goto out;
644
645 /*
646 * The following mappings will not require more memory than is available
647 * in the local pool.
648 */
649fail_undo_send:
650 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100651 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
652 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100653
654fail:
655 ret = false;
656
657out:
658 mm_unlock_stage1(&mm_stage1_locked);
659
660 return ret;
661}
662
663/**
664 * Configures the send and receive pages in the VM stage-2 and hypervisor
665 * stage-1 page tables. Locking of the page tables combined with a local memory
666 * pool ensures there will always be enough memory to recover from any errors
667 * that arise.
668 */
669static bool api_vm_configure_pages(struct vm_locked vm_locked,
670 paddr_t pa_send_begin, paddr_t pa_send_end,
671 int orig_send_mode, paddr_t pa_recv_begin,
672 paddr_t pa_recv_end, int orig_recv_mode)
673{
674 bool ret;
675 struct mpool local_page_pool;
676
677 /*
678 * Create a local pool so any freed memory can't be used by another
679 * thread. This is to ensure the original mapping can be restored if any
680 * stage of the process fails.
681 */
682 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
683
684 /* Take memory ownership away from the VM and mark as shared. */
685 if (!mm_vm_identity_map(
686 &vm_locked.vm->ptable, pa_send_begin, pa_send_end,
687 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
688 NULL, &local_page_pool)) {
689 goto fail;
690 }
691
692 if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
693 pa_recv_end,
694 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
695 NULL, &local_page_pool)) {
696 /* TODO: partial defrag of failed range. */
697 /* Recover any memory consumed in failed mapping. */
698 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
699 goto fail_undo_send;
700 }
701
702 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
703 pa_recv_begin, pa_recv_end,
704 &local_page_pool)) {
705 goto fail_undo_send_and_recv;
706 }
707
708 ret = true;
709 goto out;
710
711 /*
712 * The following mappings will not require more memory than is available
713 * in the local pool.
714 */
715fail_undo_send_and_recv:
Andrew Scull7e8de322019-07-02 13:00:56 +0100716 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
717 pa_recv_end, orig_recv_mode, NULL,
718 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100719
720fail_undo_send:
Andrew Scull7e8de322019-07-02 13:00:56 +0100721 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin,
722 pa_send_end, orig_send_mode, NULL,
723 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100724
725fail:
726 ret = false;
727
728out:
729 mpool_fini(&local_page_pool);
730
731 return ret;
732}
733
734/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100735 * Configures the VM to send/receive data through the specified pages. The pages
736 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000737 *
738 * Returns:
739 * - -1 on failure.
740 * - 0 on success if no further action is needed.
741 * - 1 if it was called by the primary VM and the primary VM now needs to wake
742 * up or kick waiters. Waiters should be retrieved by calling
743 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100744 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000745int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
746 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100747{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100748 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100749 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100750 paddr_t pa_send_begin;
751 paddr_t pa_send_end;
752 paddr_t pa_recv_begin;
753 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000754 int orig_send_mode;
755 int orig_recv_mode;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100756 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100757
758 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000759 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
760 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100761 return -1;
762 }
763
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000764 /* Convert to physical addresses. */
765 pa_send_begin = pa_from_ipa(send);
766 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
767
768 pa_recv_begin = pa_from_ipa(recv);
769 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
770
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100771 /* Fail if the same page is used for the send and receive pages. */
772 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000773 return -1;
774 }
775
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100776 /*
777 * The hypervisor's memory map must be locked for the duration of this
778 * operation to ensure there will be sufficient memory to recover from
779 * any failures.
780 *
781 * TODO: the scope of the can be reduced but will require restructuring
782 * to keep a single unlock point.
783 */
Andrew Sculle1322792019-07-01 17:46:10 +0100784 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000785
786 /* We only allow these to be setup once. */
787 if (vm->mailbox.send || vm->mailbox.recv) {
788 goto fail;
789 }
790
791 /*
792 * Ensure the pages are valid, owned and exclusive to the VM and that
793 * the VM has the required access to the memory.
794 */
795 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
796 &orig_send_mode) ||
797 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
798 (orig_send_mode & MM_MODE_R) == 0 ||
799 (orig_send_mode & MM_MODE_W) == 0) {
800 goto fail;
801 }
802
803 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
804 &orig_recv_mode) ||
805 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
806 (orig_recv_mode & MM_MODE_R) == 0) {
807 goto fail;
808 }
809
Andrew Sculle1322792019-07-01 17:46:10 +0100810 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
811 orig_send_mode, pa_recv_begin, pa_recv_end,
812 orig_recv_mode)) {
813 goto fail;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100814 }
815
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000816 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100817 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000818 goto exit;
819
Andrew Scull220e6212018-12-21 18:09:00 +0000820fail:
821 ret = -1;
822
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100823exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100824 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100825
826 return ret;
827}
828
829/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100830 * Copies data from the sender's send buffer to the recipient's receive buffer
831 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000832 *
833 * If the recipient's receive buffer is busy, it can optionally register the
834 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100835 */
Jose Marinho75509b42019-04-09 09:34:59 +0100836spci_return_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
837 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100838{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100839 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100840 struct vm *to;
Jose Marinho75509b42019-04-09 09:34:59 +0100841
842 struct two_vm_locked vm_from_to_lock;
843
Andrew Scullb06d1752019-02-04 10:15:48 +0000844 struct hf_vcpu_run_return primary_ret = {
845 .code = HF_VCPU_RUN_MESSAGE,
846 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000847 struct spci_message from_msg_replica;
848 struct spci_message *to_msg;
849 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100850
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000851 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100852
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000853 int64_t ret;
854 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
855 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100856
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000857 /*
858 * Check that the sender has configured its send buffer. Copy the
859 * message header. If the tx mailbox at from_msg is configured (i.e.
860 * from_msg != NULL) then it can be safely accessed after releasing the
861 * lock since the tx mailbox address can only be configured once.
862 */
863 sl_lock(&from->lock);
864 from_msg = from->mailbox.send;
865 sl_unlock(&from->lock);
866
867 if (from_msg == NULL) {
868 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100869 }
870
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100871 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000872 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100873 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000874 from_msg_replica = *from_msg;
875
876 /* Ensure source VM id corresponds to the current VM. */
877 if (from_msg_replica.source_vm_id != from->id) {
878 return SPCI_INVALID_PARAMETERS;
879 }
880
881 size = from_msg_replica.length;
882 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100883 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000884 return SPCI_INVALID_PARAMETERS;
885 }
886
887 /* Disallow reflexive requests as this suggests an error in the VM. */
888 if (from_msg_replica.target_vm_id == from->id) {
889 return SPCI_INVALID_PARAMETERS;
890 }
891
892 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100893 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000894 if (to == NULL) {
895 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100896 }
897
Jose Marinho75509b42019-04-09 09:34:59 +0100898 /*
899 * Hf needs to hold the lock on <to> before the mailbox state is
900 * checked. The lock on <to> must be held until the information is
901 * copied to <to> Rx buffer. Since in
902 * spci_msg_handle_architected_message we may call api_spci_share_memory
903 * which must hold the <from> lock, we must hold the <from> lock at this
904 * point to prevent a deadlock scenario.
905 */
906 vm_from_to_lock = vm_lock_both(to, from);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100907
Andrew Sculld6ee1102019-04-05 22:12:42 +0100908 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100909 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000910 /*
911 * Fail if the target isn't currently ready to receive data,
912 * setting up for notification if requested.
913 */
914 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000915 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000916 &current->vm->wait_entries
917 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000918
919 /* Append waiter only if it's not there yet. */
920 if (list_empty(&entry->wait_links)) {
921 list_append(&to->mailbox.waiter_list,
922 &entry->wait_links);
923 }
924 }
925
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000926 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100927 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100928 }
929
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000930 to_msg = to->mailbox.recv;
Jose Marinho75509b42019-04-09 09:34:59 +0100931
932 /* Handle architected messages. */
933 if ((from_msg_replica.flags & SPCI_MESSAGE_IMPDEF_MASK) !=
934 SPCI_MESSAGE_IMPDEF) {
935 /*
936 * Buffer holding the internal copy of the shared memory
937 * regions.
938 */
Jose Marinho20713fa2019-08-07 15:42:07 +0100939 uint8_t *message_buffer = cpu_get_buffer(current->cpu->id);
940 uint32_t message_buffer_size =
941 cpu_get_buffer_size(current->cpu->id);
Jose Marinho75509b42019-04-09 09:34:59 +0100942
943 struct spci_architected_message_header *architected_header =
944 spci_get_architected_message_header(from->mailbox.send);
945
946 const struct spci_architected_message_header
947 *architected_message_replica;
948
Jose Marinho20713fa2019-08-07 15:42:07 +0100949 if (from_msg_replica.length > message_buffer_size) {
Jose Marinho75509b42019-04-09 09:34:59 +0100950 ret = SPCI_INVALID_PARAMETERS;
951 goto out;
952 }
953
954 if (from_msg_replica.length <
955 sizeof(struct spci_architected_message_header)) {
956 ret = SPCI_INVALID_PARAMETERS;
957 goto out;
958 }
959
960 /* Copy the architected message into an internal buffer. */
Jose Marinho20713fa2019-08-07 15:42:07 +0100961 memcpy_s(message_buffer, message_buffer_size,
Jose Marinho75509b42019-04-09 09:34:59 +0100962 architected_header, from_msg_replica.length);
963
964 architected_message_replica =
965 (struct spci_architected_message_header *)
966 message_buffer;
967
968 /*
969 * Note that message_buffer is passed as the third parameter to
970 * spci_msg_handle_architected_message. The execution flow
971 * commencing at spci_msg_handle_architected_message will make
972 * several accesses to fields in message_buffer. The memory area
973 * message_buffer must be exclusively owned by Hf so that TOCTOU
974 * issues do not arise.
975 */
976 ret = spci_msg_handle_architected_message(
977 vm_from_to_lock.vm1, vm_from_to_lock.vm2,
978 architected_message_replica, &from_msg_replica, to_msg);
979
980 if (ret != SPCI_SUCCESS) {
981 goto out;
982 }
983 } else {
984 /* Copy data. */
985 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
986 from->mailbox.send->payload, size);
987 *to_msg = from_msg_replica;
988 }
989
Andrew Scullb06d1752019-02-04 10:15:48 +0000990 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000991 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100992
993 /* Messages for the primary VM are delivered directly. */
994 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100995 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000996 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100997 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100998 goto out;
999 }
1000
Andrew Sculld6ee1102019-04-05 22:12:42 +01001001 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +01001002
1003 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +00001004 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +00001005 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001006 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001007 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001008
1009out:
Jose Marinho75509b42019-04-09 09:34:59 +01001010 vm_unlock(&vm_from_to_lock.vm1);
1011 vm_unlock(&vm_from_to_lock.vm2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001012
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001013 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001014}
1015
1016/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001017 * Checks whether the vCPU's attempt to block for a message has already been
1018 * interrupted or whether it is allowed to block.
1019 */
1020bool api_spci_msg_recv_block_interrupted(struct vcpu *current)
1021{
1022 bool interrupted;
1023
1024 sl_lock(&current->lock);
1025
1026 /*
1027 * Don't block if there are enabled and pending interrupts, to match
1028 * behaviour of wait_for_interrupt.
1029 */
1030 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1031
1032 sl_unlock(&current->lock);
1033
1034 return interrupted;
1035}
1036
1037/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001038 * Receives a message from the mailbox. If one isn't available, this function
1039 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001040 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001041 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001042 */
Andrew Walbran0de4f162019-09-03 16:44:20 +01001043int32_t api_spci_msg_recv(bool block, struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001044{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001045 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001046 int32_t return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001047
Andrew Scullaa039b32018-10-04 15:02:26 +01001048 /*
1049 * The primary VM will receive messages as a status code from running
1050 * vcpus and must not call this function.
1051 */
Andrew Scull19503262018-09-20 14:48:39 +01001052 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +00001053 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001054 }
1055
1056 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001057
Andrew Scullaa039b32018-10-04 15:02:26 +01001058 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001059 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1060 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001061 return_code = SPCI_SUCCESS;
1062 goto out;
1063 }
1064
1065 /* No pending message so fail if not allowed to block. */
1066 if (!block) {
1067 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +01001068 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001069 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001070
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001071 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001072 * From this point onward this call can only be interrupted or a message
1073 * received. If a message is received the return value will be set at
1074 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001075 */
Jose Marinho3e2442f2019-03-12 13:30:37 +00001076 return_code = SPCI_INTERRUPTED;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001077 if (api_spci_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001078 goto out;
1079 }
1080
Andrew Scullaa039b32018-10-04 15:02:26 +01001081 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001082 {
1083 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +00001084 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +00001085 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001086
Andrew Walbranb4816552018-12-05 17:35:42 +00001087 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001088 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001089 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001090out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001091 sl_unlock(&vm->lock);
1092
Jose Marinho3e2442f2019-03-12 13:30:37 +00001093 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001094}
1095
1096/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001097 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1098 * by this function, the caller must have called api_mailbox_send before with
1099 * the notify argument set to true, and this call must have failed because the
1100 * mailbox was not available.
1101 *
1102 * It should be called repeatedly to retrieve a list of VMs.
1103 *
1104 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1105 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001106 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001107int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001108{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001109 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001110 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001111 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001112
1113 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001114 if (list_empty(&vm->mailbox.ready_list)) {
1115 ret = -1;
1116 goto exit;
1117 }
1118
1119 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1120 ready_links);
1121 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001122 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001123
1124exit:
1125 sl_unlock(&vm->lock);
1126 return ret;
1127}
1128
1129/**
1130 * Retrieves the next VM waiting to be notified that the mailbox of the
1131 * specified VM became writable. Only primary VMs are allowed to call this.
1132 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001133 * Returns -1 on failure or if there are no waiters; the VM id of the next
1134 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001135 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001136int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001137{
1138 struct vm *vm;
1139 struct vm_locked locked;
1140 struct wait_entry *entry;
1141 struct vm *waiting_vm;
1142
1143 /* Only primary VMs are allowed to call this function. */
1144 if (current->vm->id != HF_PRIMARY_VM_ID) {
1145 return -1;
1146 }
1147
Andrew Walbran42347a92019-05-09 13:59:03 +01001148 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001149 if (vm == NULL) {
1150 return -1;
1151 }
1152
1153 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001154 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001155 entry = api_fetch_waiter(locked);
1156 vm_unlock(&locked);
1157
1158 if (entry == NULL) {
1159 return -1;
1160 }
1161
1162 /* Enqueue notification to waiting VM. */
1163 waiting_vm = entry->waiting_vm;
1164
1165 sl_lock(&waiting_vm->lock);
1166 if (list_empty(&entry->ready_links)) {
1167 list_append(&waiting_vm->mailbox.ready_list,
1168 &entry->ready_links);
1169 }
1170 sl_unlock(&waiting_vm->lock);
1171
1172 return waiting_vm->id;
1173}
1174
1175/**
1176 * Clears the caller's mailbox so that a new message can be received. The caller
1177 * must have copied out all data they wish to preserve as new messages will
1178 * overwrite the old and will arrive asynchronously.
1179 *
1180 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001181 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001182 * - 0 on success if no further action is needed.
1183 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1184 * up or kick waiters. Waiters should be retrieved by calling
1185 * hf_mailbox_waiter_get.
1186 */
1187int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1188{
1189 struct vm *vm = current->vm;
1190 struct vm_locked locked;
1191 int64_t ret;
1192
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001193 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001194 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001195 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001196 ret = 0;
1197 break;
1198
Andrew Sculld6ee1102019-04-05 22:12:42 +01001199 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001200 ret = -1;
1201 break;
1202
Andrew Sculld6ee1102019-04-05 22:12:42 +01001203 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001204 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001205 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001206 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001207 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001208 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001209
1210 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001211}
Andrew Walbran318f5732018-11-20 16:23:42 +00001212
1213/**
1214 * Enables or disables a given interrupt ID for the calling vCPU.
1215 *
1216 * Returns 0 on success, or -1 if the intid is invalid.
1217 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001218int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001219{
1220 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1221 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001222
Andrew Walbran318f5732018-11-20 16:23:42 +00001223 if (intid >= HF_NUM_INTIDS) {
1224 return -1;
1225 }
1226
1227 sl_lock(&current->lock);
1228 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001229 /*
1230 * If it is pending and was not enabled before, increment the
1231 * count.
1232 */
1233 if (current->interrupts.interrupt_pending[intid_index] &
1234 ~current->interrupts.interrupt_enabled[intid_index] &
1235 intid_mask) {
1236 current->interrupts.enabled_and_pending_count++;
1237 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001238 current->interrupts.interrupt_enabled[intid_index] |=
1239 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001240 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001241 /*
1242 * If it is pending and was enabled before, decrement the count.
1243 */
1244 if (current->interrupts.interrupt_pending[intid_index] &
1245 current->interrupts.interrupt_enabled[intid_index] &
1246 intid_mask) {
1247 current->interrupts.enabled_and_pending_count--;
1248 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001249 current->interrupts.interrupt_enabled[intid_index] &=
1250 ~intid_mask;
1251 }
1252
1253 sl_unlock(&current->lock);
1254 return 0;
1255}
1256
1257/**
1258 * Returns the ID of the next pending interrupt for the calling vCPU, and
1259 * acknowledges it (i.e. marks it as no longer pending). Returns
1260 * HF_INVALID_INTID if there are no pending interrupts.
1261 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001262uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001263{
1264 uint8_t i;
1265 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001266
1267 /*
1268 * Find the first enabled and pending interrupt ID, return it, and
1269 * deactivate it.
1270 */
1271 sl_lock(&current->lock);
1272 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1273 uint32_t enabled_and_pending =
1274 current->interrupts.interrupt_enabled[i] &
1275 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001276
Andrew Walbran318f5732018-11-20 16:23:42 +00001277 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001278 uint8_t bit_index = ctz(enabled_and_pending);
1279 /*
1280 * Mark it as no longer pending and decrement the count.
1281 */
1282 current->interrupts.interrupt_pending[i] &=
1283 ~(1u << bit_index);
1284 current->interrupts.enabled_and_pending_count--;
1285 first_interrupt =
1286 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001287 break;
1288 }
1289 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001290
1291 sl_unlock(&current->lock);
1292 return first_interrupt;
1293}
1294
1295/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001296 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001297 * given VM and vCPU.
1298 */
1299static inline bool is_injection_allowed(uint32_t target_vm_id,
1300 struct vcpu *current)
1301{
1302 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001303
Andrew Walbran318f5732018-11-20 16:23:42 +00001304 /*
1305 * The primary VM is allowed to inject interrupts into any VM. Secondary
1306 * VMs are only allowed to inject interrupts into their own vCPUs.
1307 */
1308 return current_vm_id == HF_PRIMARY_VM_ID ||
1309 current_vm_id == target_vm_id;
1310}
1311
1312/**
1313 * Injects a virtual interrupt of the given ID into the given target vCPU.
1314 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1315 * when the vCPU is next run, which is up to the scheduler.
1316 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001317 * Returns:
1318 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1319 * ID is invalid, or the current VM is not allowed to inject interrupts to
1320 * the target VM.
1321 * - 0 on success if no further action is needed.
1322 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1323 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001324 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001325int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001326 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001327 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001328{
Andrew Walbran318f5732018-11-20 16:23:42 +00001329 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001330 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001331
1332 if (intid >= HF_NUM_INTIDS) {
1333 return -1;
1334 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001335
Andrew Walbran318f5732018-11-20 16:23:42 +00001336 if (target_vm == NULL) {
1337 return -1;
1338 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001339
Andrew Walbran318f5732018-11-20 16:23:42 +00001340 if (target_vcpu_idx >= target_vm->vcpu_count) {
1341 /* The requested vcpu must exist. */
1342 return -1;
1343 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001344
Andrew Walbran318f5732018-11-20 16:23:42 +00001345 if (!is_injection_allowed(target_vm_id, current)) {
1346 return -1;
1347 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001348
Andrew Walbrane1310df2019-04-29 17:28:28 +01001349 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001350
1351 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1352 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001353 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001354}
Andrew Scull6386f252018-12-06 13:29:10 +00001355
1356/**
1357 * Clears a region of physical memory by overwriting it with zeros. The data is
1358 * flushed from the cache so the memory has been cleared across the system.
1359 */
1360static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1361{
1362 /*
1363 * TODO: change this to a cpu local single page window rather than a
1364 * global mapping of the whole range. Such an approach will limit
1365 * the changes to stage-1 tables and will allow only local
1366 * invalidation.
1367 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001368 bool ret;
1369 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
1370 void *ptr =
1371 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001372 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001373
1374 if (!ptr) {
1375 /* TODO: partial defrag of failed range. */
1376 /* Recover any memory consumed in failed mapping. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001377 mm_defrag(stage1_locked, ppool);
1378 goto fail;
Andrew Scull6386f252018-12-06 13:29:10 +00001379 }
1380
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001381 memset_s(ptr, size, 0, size);
Andrew Scullc059fbe2019-09-12 12:58:40 +01001382 arch_mm_flush_dcache(ptr, size);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001383 mm_unmap(stage1_locked, begin, end, ppool);
Andrew Scull6386f252018-12-06 13:29:10 +00001384
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001385 ret = true;
1386 goto out;
1387
1388fail:
1389 ret = false;
1390
1391out:
1392 mm_unlock_stage1(&stage1_locked);
1393
1394 return ret;
Andrew Scull6386f252018-12-06 13:29:10 +00001395}
1396
Jose Marinho75509b42019-04-09 09:34:59 +01001397/** TODO: Move function to spci_architectted_message.c. */
1398/**
1399 * Shares memory from the calling VM with another. The memory can be shared in
1400 * different modes.
1401 *
1402 * This function requires the calling context to hold the <to> and <from> locks.
1403 *
1404 * Returns:
1405 * In case of error one of the following values is returned:
1406 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
1407 * erroneous;
1408 * 2) SPCI_NO_MEMORY - Hf did not have sufficient memory to complete
1409 * the request.
1410 * Success is indicated by SPCI_SUCCESS.
1411 */
1412spci_return_t api_spci_share_memory(struct vm_locked to_locked,
1413 struct vm_locked from_locked,
1414 struct spci_memory_region *memory_region,
1415 uint32_t memory_to_attributes,
1416 enum spci_memory_share share)
1417{
1418 struct vm *to = to_locked.vm;
1419 struct vm *from = from_locked.vm;
1420 int orig_from_mode;
1421 int from_mode;
1422 int to_mode;
1423 struct mpool local_page_pool;
1424 int64_t ret;
1425 paddr_t pa_begin;
1426 paddr_t pa_end;
1427 ipaddr_t begin;
1428 ipaddr_t end;
1429
1430 size_t size;
1431
1432 /* Disallow reflexive shares as this suggests an error in the VM. */
1433 if (to == from) {
1434 return SPCI_INVALID_PARAMETERS;
1435 }
1436
1437 /*
1438 * Create a local pool so any freed memory can't be used by another
1439 * thread. This is to ensure the original mapping can be restored if any
1440 * stage of the process fails.
1441 */
1442 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1443
1444 /* Obtain the single contiguous set of pages from the memory_region. */
1445 /* TODO: Add support for multiple constituent regions. */
1446 size = memory_region->constituents[0].page_count * PAGE_SIZE;
1447 begin = ipa_init(memory_region->constituents[0].address);
1448 end = ipa_add(begin, size);
1449
1450 /*
1451 * Check if the state transition is lawful for both VMs involved
1452 * in the memory exchange, ensure that all constituents of a memory
1453 * region being shared are at the same state.
1454 */
1455 if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin,
1456 end, memory_to_attributes, &from_mode,
1457 &to_mode)) {
1458 return SPCI_INVALID_PARAMETERS;
1459 }
1460
1461 pa_begin = pa_from_ipa(begin);
1462 pa_end = pa_from_ipa(end);
1463
1464 /*
1465 * First update the mapping for the sender so there is not overlap with
1466 * the recipient.
1467 */
1468 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1469 NULL, &local_page_pool)) {
1470 ret = SPCI_NO_MEMORY;
1471 goto out;
1472 }
1473
1474 /* Complete the transfer by mapping the memory into the recipient. */
1475 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1476 &local_page_pool)) {
1477 /* TODO: partial defrag of failed range. */
1478 /* Recover any memory consumed in failed mapping. */
1479 mm_vm_defrag(&from->ptable, &local_page_pool);
1480
1481 ret = SPCI_NO_MEMORY;
1482
1483 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1484 orig_from_mode, NULL,
1485 &local_page_pool));
1486
1487 goto out;
1488 }
1489
1490 ret = SPCI_SUCCESS;
1491
1492out:
1493
1494 mpool_fini(&local_page_pool);
1495
1496 return ret;
1497}
1498
Andrew Scull6386f252018-12-06 13:29:10 +00001499/**
1500 * Shares memory from the calling VM with another. The memory can be shared in
1501 * different modes.
1502 *
1503 * TODO: the interface for sharing memory will need to be enhanced to allow
1504 * sharing with different modes e.g. read-only, informing the recipient
1505 * of the memory they have been given, opting to not wipe the memory and
1506 * possibly allowing multiple blocks to be transferred. What this will
1507 * look like is TBD.
1508 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001509int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001510 enum hf_share share, struct vcpu *current)
1511{
1512 struct vm *from = current->vm;
1513 struct vm *to;
1514 int orig_from_mode;
1515 int from_mode;
1516 int to_mode;
1517 ipaddr_t begin;
1518 ipaddr_t end;
1519 paddr_t pa_begin;
1520 paddr_t pa_end;
1521 struct mpool local_page_pool;
1522 int64_t ret;
1523
1524 /* Disallow reflexive shares as this suggests an error in the VM. */
1525 if (vm_id == from->id) {
1526 return -1;
1527 }
1528
1529 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001530 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001531 if (to == NULL) {
1532 return -1;
1533 }
1534
1535 begin = addr;
1536 end = ipa_add(addr, size);
1537
1538 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001539 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1540 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001541 return -1;
1542 }
1543
1544 /* Convert the sharing request to memory management modes. */
1545 switch (share) {
1546 case HF_MEMORY_GIVE:
1547 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1548 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1549 break;
1550
1551 case HF_MEMORY_LEND:
1552 from_mode = MM_MODE_INVALID;
1553 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1554 break;
1555
1556 case HF_MEMORY_SHARE:
1557 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1558 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1559 MM_MODE_SHARED;
1560 break;
1561
1562 default:
1563 /* The input is untrusted so might not be a valid value. */
1564 return -1;
1565 }
1566
1567 /*
1568 * Create a local pool so any freed memory can't be used by another
1569 * thread. This is to ensure the original mapping can be restored if any
1570 * stage of the process fails.
1571 */
1572 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1573
1574 sl_lock_both(&from->lock, &to->lock);
1575
1576 /*
1577 * Ensure that the memory range is mapped with the same mode so that
1578 * changes can be reverted if the process fails.
1579 */
1580 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1581 goto fail;
1582 }
1583
1584 /*
1585 * Ensure the memory range is valid for the sender. If it isn't, the
1586 * sender has either shared it with another VM already or has no claim
1587 * to the memory.
1588 */
1589 if (orig_from_mode & MM_MODE_INVALID) {
1590 goto fail;
1591 }
1592
1593 /*
1594 * The sender must own the memory and have exclusive access to it in
1595 * order to share it. Alternatively, it is giving memory back to the
1596 * owning VM.
1597 */
1598 if (orig_from_mode & MM_MODE_UNOWNED) {
1599 int orig_to_mode;
1600
1601 if (share != HF_MEMORY_GIVE ||
1602 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1603 orig_to_mode & MM_MODE_UNOWNED) {
1604 goto fail;
1605 }
1606 } else if (orig_from_mode & MM_MODE_SHARED) {
1607 goto fail;
1608 }
1609
1610 pa_begin = pa_from_ipa(begin);
1611 pa_end = pa_from_ipa(end);
1612
1613 /*
1614 * First update the mapping for the sender so there is not overlap with
1615 * the recipient.
1616 */
1617 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1618 NULL, &local_page_pool)) {
1619 goto fail;
1620 }
1621
1622 /* Clear the memory so no VM or device can see the previous contents. */
1623 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1624 goto fail_return_to_sender;
1625 }
1626
1627 /* Complete the transfer by mapping the memory into the recipient. */
1628 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1629 &local_page_pool)) {
1630 /* TODO: partial defrag of failed range. */
1631 /* Recover any memory consumed in failed mapping. */
1632 mm_vm_defrag(&from->ptable, &local_page_pool);
1633 goto fail_return_to_sender;
1634 }
1635
1636 ret = 0;
1637 goto out;
1638
1639fail_return_to_sender:
Andrew Scull7e8de322019-07-02 13:00:56 +01001640 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1641 orig_from_mode, NULL, &local_page_pool));
Andrew Scull6386f252018-12-06 13:29:10 +00001642
1643fail:
1644 ret = -1;
1645
1646out:
1647 sl_unlock(&from->lock);
1648 sl_unlock(&to->lock);
1649
1650 mpool_fini(&local_page_pool);
1651
1652 return ret;
1653}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001654
1655/** Returns the version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +01001656struct spci_value api_spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001657{
1658 /*
1659 * Ensure that both major and minor revision representation occupies at
1660 * most 15 bits.
1661 */
1662 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1663 "Major revision representation take more than 15 bits.");
1664 static_assert(0x10000 > SPCI_VERSION_MINOR,
1665 "Minor revision representation take more than 16 bits.");
1666
Andrew Walbran7f920af2019-09-03 17:09:30 +01001667 struct spci_value ret = {
1668 .func = SPCI_SUCCESS_32,
1669 .arg1 = (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1670 SPCI_VERSION_MINOR};
1671 return ret;
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001672}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001673
1674int64_t api_debug_log(char c, struct vcpu *current)
1675{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001676 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001677 struct vm *vm = current->vm;
1678 struct vm_locked vm_locked = vm_lock(vm);
1679
Andrew Sculld54e1be2019-08-20 11:09:42 +01001680 if (c == '\n' || c == '\0') {
1681 flush = true;
1682 } else {
1683 vm->log_buffer[vm->log_buffer_length++] = c;
1684 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1685 }
1686
1687 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001688 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1689 vm->log_buffer_length);
1690 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001691 }
1692
1693 vm_unlock(&vm_locked);
1694
1695 return 0;
1696}