blob: c32446f070509ef02b607e85de0f37678aa65fbc [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"
Jose Marinho40d55f32019-07-01 15:41:54 +010025#include "hf/spci_internal.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000032#include "vmapi/hf/spci.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000034/*
35 * To eliminate the risk of deadlocks, we define a partial order for the
36 * acquisition of locks held concurrently by the same physical CPU. Our current
37 * ordering requirements are as follows:
38 *
Andrew Scullba79b0a2019-07-03 11:26:53 +010039 * vm::lock -> vcpu::lock -> mm_stage1_lock
Andrew Scull6386f252018-12-06 13:29:10 +000040 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010041 * Locks of the same kind require the lock of lowest address to be locked first,
42 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000043 */
44
Andrew Scullaa039b32018-10-04 15:02:26 +010045static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010046 "Currently, a page is mapped for the send and receive buffers so "
47 "the maximum request is the size of a page.");
48
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000049static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000050
51/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000052 * Initialises the API page pool by taking ownership of the contents of the
53 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000054 */
55void api_init(struct mpool *ppool)
56{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000057 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000058}
59
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010060/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010061 * Switches the physical CPU back to the corresponding vcpu of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010062 *
63 * This triggers the scheduling logic to run. Run in the context of secondary VM
64 * to cause HF_VCPU_RUN to return and the primary VM to regain control of the
65 * cpu.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010066 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010067static struct vcpu *api_switch_to_primary(struct vcpu *current,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000068 struct hf_vcpu_run_return primary_ret,
69 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070{
Andrew Walbran42347a92019-05-09 13:59:03 +010071 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010072 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010073
Andrew Walbran508e63c2018-12-20 17:02:37 +000074 /*
75 * If the secondary is blocked but has a timer running, sleep until the
76 * timer fires rather than indefinitely.
77 */
Andrew Scullb06d1752019-02-04 10:15:48 +000078 switch (primary_ret.code) {
79 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
80 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
81 primary_ret.sleep.ns =
82 arch_timer_enabled_current()
83 ? arch_timer_remaining_ns_current()
84 : HF_SLEEP_INDEFINITE;
85 break;
86
87 default:
88 /* Do nothing. */
89 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +000090 }
91
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010092 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Scull6d2db332018-10-10 15:28:17 +010093 arch_regs_set_retval(&next->regs,
94 hf_vcpu_run_return_encode(primary_ret));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010095
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000096 /* Mark the current vcpu as waiting. */
97 sl_lock(&current->lock);
98 current->state = secondary_state;
99 sl_unlock(&current->lock);
100
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100101 return next;
102}
103
104/**
Andrew Scull33fecd32019-01-08 14:48:27 +0000105 * Returns to the primary vm and signals that the vcpu still has work to do so.
106 */
107struct vcpu *api_preempt(struct vcpu *current)
108{
109 struct hf_vcpu_run_return ret = {
110 .code = HF_VCPU_RUN_PREEMPTED,
111 };
112
Andrew Sculld6ee1102019-04-05 22:12:42 +0100113 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000114}
115
116/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100117 * Puts the current vcpu in wait for interrupt mode, and returns to the primary
118 * vm.
119 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100120struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100121{
Andrew Scull6d2db332018-10-10 15:28:17 +0100122 struct hf_vcpu_run_return ret = {
123 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
124 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000125
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000126 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100127 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100128}
129
130/**
Andrew Walbran33645652019-04-15 12:29:31 +0100131 * Puts the current vCPU in off mode, and returns to the primary VM.
132 */
133struct vcpu *api_vcpu_off(struct vcpu *current)
134{
135 struct hf_vcpu_run_return ret = {
136 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
137 };
138
139 /*
140 * Disable the timer, so the scheduler doesn't get told to call back
141 * based on it.
142 */
143 arch_timer_disable_current();
144
145 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
146}
147
148/**
Andrew Scull66d62bf2019-02-01 13:54:10 +0000149 * Returns to the primary vm to allow this cpu to be used for other tasks as the
150 * vcpu does not have work to do at this moment. The current vcpu is marked as
Jose Marinho135dff32019-02-28 10:25:57 +0000151 * ready to be scheduled again. This SPCI function always returns SPCI_SUCCESS.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000152 */
Jose Marinho135dff32019-02-28 10:25:57 +0000153int32_t api_spci_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000154{
155 struct hf_vcpu_run_return ret = {
156 .code = HF_VCPU_RUN_YIELD,
157 };
158
159 if (current->vm->id == HF_PRIMARY_VM_ID) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000160 /* Noop on the primary as it makes the scheduling decisions. */
Jose Marinho135dff32019-02-28 10:25:57 +0000161 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000162 }
163
Jose Marinho135dff32019-02-28 10:25:57 +0000164 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
165
166 /* SPCI_YIELD always returns SPCI_SUCCESS. */
167 return SPCI_SUCCESS;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000168}
169
170/**
Andrew Walbran33645652019-04-15 12:29:31 +0100171 * Switches to the primary so that it can switch to the target, or kick it if it
172 * is already running on a different physical CPU.
173 */
174struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
175{
176 struct hf_vcpu_run_return ret = {
177 .code = HF_VCPU_RUN_WAKE_UP,
178 .wake_up.vm_id = target_vcpu->vm->id,
179 .wake_up.vcpu = vcpu_index(target_vcpu),
180 };
181 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
182}
183
184/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000185 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000186 */
187struct vcpu *api_abort(struct vcpu *current)
188{
189 struct hf_vcpu_run_return ret = {
190 .code = HF_VCPU_RUN_ABORTED,
191 };
192
193 dlog("Aborting VM %u vCPU %u\n", current->vm->id, vcpu_index(current));
194
195 if (current->vm->id == HF_PRIMARY_VM_ID) {
196 /* TODO: what to do when the primary aborts? */
197 for (;;) {
198 /* Do nothing. */
199 }
200 }
201
202 atomic_store_explicit(&current->vm->aborting, true,
203 memory_order_relaxed);
204
205 /* TODO: free resources once all vCPUs abort. */
206
Andrew Sculld6ee1102019-04-05 22:12:42 +0100207 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000208}
209
210/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000211 * Returns the ID of the VM.
212 */
Andrew Walbranfc6cd9d2019-06-25 15:23:27 +0100213spci_vm_id_t api_vm_get_id(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000214{
215 return current->vm->id;
216}
217
218/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100219 * Returns the number of VMs configured to run.
220 */
Andrew Walbran52d99672019-06-25 15:51:11 +0100221spci_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100222{
Andrew Scull19503262018-09-20 14:48:39 +0100223 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100224}
225
226/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100227 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
228 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100229 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100230spci_vcpu_count_t api_vcpu_get_count(spci_vm_id_t vm_id,
231 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100232{
Andrew Scull19503262018-09-20 14:48:39 +0100233 struct vm *vm;
234
235 /* Only the primary VM needs to know about vcpus for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100236 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100237 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100238 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100239
Andrew Walbran42347a92019-05-09 13:59:03 +0100240 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100241 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100242 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100243 }
244
245 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100246}
247
248/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000249 * This function is called by the architecture-specific context switching
250 * function to indicate that register state for the given vcpu has been saved
251 * and can therefore be used by other pcpus.
252 */
253void api_regs_state_saved(struct vcpu *vcpu)
254{
255 sl_lock(&vcpu->lock);
256 vcpu->regs_available = true;
257 sl_unlock(&vcpu->lock);
258}
259
260/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000261 * Retrieves the next waiter and removes it from the wait list if the VM's
262 * mailbox is in a writable state.
263 */
264static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
265{
266 struct wait_entry *entry;
267 struct vm *vm = locked_vm.vm;
268
Andrew Sculld6ee1102019-04-05 22:12:42 +0100269 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000270 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
271 /* The mailbox is not writable or there are no waiters. */
272 return NULL;
273 }
274
275 /* Remove waiter from the wait list. */
276 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
277 wait_links);
278 list_remove(&entry->wait_links);
279 return entry;
280}
281
282/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000283 * Assuming that the arguments have already been checked by the caller, injects
284 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
285 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
286 * is next run, which is up to the scheduler.
287 *
288 * Returns:
289 * - 0 on success if no further action is needed.
290 * - 1 if it was called by the primary VM and the primary VM now needs to wake
291 * up or kick the target vCPU.
292 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100293static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000294 uint32_t intid, struct vcpu *current,
295 struct vcpu **next)
296{
297 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
298 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000299 int64_t ret = 0;
300
301 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000302
303 /*
304 * We only need to change state and (maybe) trigger a virtual IRQ if it
305 * is enabled and was not previously pending. Otherwise we can skip
306 * everything except setting the pending bit.
307 *
308 * If you change this logic make sure to update the need_vm_lock logic
309 * above to match.
310 */
311 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
312 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
313 intid_mask)) {
314 goto out;
315 }
316
317 /* Increment the count. */
318 target_vcpu->interrupts.enabled_and_pending_count++;
319
320 /*
321 * Only need to update state if there was not already an
322 * interrupt enabled and pending.
323 */
324 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
325 goto out;
326 }
327
Andrew Walbran508e63c2018-12-20 17:02:37 +0000328 if (current->vm->id == HF_PRIMARY_VM_ID) {
329 /*
330 * If the call came from the primary VM, let it know that it
331 * should run or kick the target vCPU.
332 */
333 ret = 1;
334 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100335 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000336 }
337
338out:
339 /* Either way, make it pending. */
340 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
341
342 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000343
344 return ret;
345}
346
347/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000348 * Prepares the vcpu to run by updating its state and fetching whether a return
349 * value needs to be forced onto the vCPU.
350 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000351static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000352 struct hf_vcpu_run_return *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000353{
Andrew Scullb06d1752019-02-04 10:15:48 +0000354 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000355 bool ret;
356
Andrew Scullb06d1752019-02-04 10:15:48 +0000357 /*
Andrew Scull4caadaf2019-07-03 13:13:47 +0100358 * Wait until the registers become available. All locks must be released
359 * between iterations of this loop to avoid potential deadlocks if, on
360 * any path, a lock needs to be taken after taking the decision to
361 * switch context but before the registers have been saved.
Andrew Scullb06d1752019-02-04 10:15:48 +0000362 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100363 * The VM lock is not needed in the common case so it must only be taken
364 * when it is going to be needed. This ensures there are no inter-vCPU
365 * dependencies in the common run case meaning the sensitive context
366 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000367 */
368 for (;;) {
369 sl_lock(&vcpu->lock);
370
371 /* The VM needs to be locked to deliver mailbox messages. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100372 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
Andrew Scullb06d1752019-02-04 10:15:48 +0000373 if (need_vm_lock) {
374 sl_unlock(&vcpu->lock);
375 sl_lock(&vcpu->vm->lock);
376 sl_lock(&vcpu->lock);
377 }
378
379 if (vcpu->regs_available) {
380 break;
381 }
382
Andrew Sculld6ee1102019-04-05 22:12:42 +0100383 if (vcpu->state == VCPU_STATE_RUNNING) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000384 /*
385 * vCPU is running on another pCPU.
386 *
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100387 * It's ok not to return the sleep duration here because
Andrew Scullb06d1752019-02-04 10:15:48 +0000388 * the other physical CPU that is currently running this
Andrew Walbranabf88fb2019-06-21 12:17:47 +0100389 * vCPU will return the sleep duration if needed. The
390 * default return value is
391 * HF_VCPU_RUN_WAIT_FOR_INTERRUPT, so no need to set it
392 * explicitly.
Andrew Scullb06d1752019-02-04 10:15:48 +0000393 */
394 ret = false;
395 goto out;
396 }
397
398 sl_unlock(&vcpu->lock);
399 if (need_vm_lock) {
400 sl_unlock(&vcpu->vm->lock);
401 }
402 }
Andrew Scull9726c252019-01-23 13:44:19 +0000403
404 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100405 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Scull82331282019-01-25 10:29:34 +0000406 dlog("Aborting VM %u vCPU %u\n", vcpu->vm->id,
407 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100408 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000409 }
410 ret = false;
411 goto out;
412 }
413
Andrew Walbran508e63c2018-12-20 17:02:37 +0000414 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100415 case VCPU_STATE_RUNNING:
416 case VCPU_STATE_OFF:
417 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000418 ret = false;
419 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000420
Andrew Sculld6ee1102019-04-05 22:12:42 +0100421 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000422 /*
423 * A pending message allows the vCPU to run so the message can
424 * be delivered directly.
425 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100426 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Jose Marinho3e2442f2019-03-12 13:30:37 +0000427 arch_regs_set_retval(&vcpu->regs, SPCI_SUCCESS);
Andrew Sculld6ee1102019-04-05 22:12:42 +0100428 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000429 break;
430 }
431 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100432 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000433 /* Allow virtual interrupts to be delivered. */
434 if (vcpu->interrupts.enabled_and_pending_count > 0) {
435 break;
436 }
437
438 /* The timer expired so allow the interrupt to be delivered. */
Andrew Walbran508e63c2018-12-20 17:02:37 +0000439 if (arch_timer_pending(&vcpu->regs)) {
440 break;
441 }
442
443 /*
444 * The vCPU is not ready to run, return the appropriate code to
445 * the primary which called vcpu_run.
446 */
447 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000448 run_ret->code =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100449 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Scullb06d1752019-02-04 10:15:48 +0000450 ? HF_VCPU_RUN_WAIT_FOR_MESSAGE
451 : HF_VCPU_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000452 run_ret->sleep.ns =
453 arch_timer_remaining_ns(&vcpu->regs);
454 }
455
456 ret = false;
457 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000458
Andrew Sculld6ee1102019-04-05 22:12:42 +0100459 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000460 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000461 }
462
Andrew Scullb06d1752019-02-04 10:15:48 +0000463 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000464 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100465 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000466
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000467 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000468 * Mark the registers as unavailable now that we're about to reflect
469 * them onto the real registers. This will also prevent another physical
470 * CPU from trying to read these registers.
471 */
472 vcpu->regs_available = false;
473
474 ret = true;
475
476out:
477 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000478 if (need_vm_lock) {
479 sl_unlock(&vcpu->vm->lock);
480 }
481
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000482 return ret;
483}
484
485/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100486 * Runs the given vcpu of the given vm.
487 */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100488struct hf_vcpu_run_return api_vcpu_run(spci_vm_id_t vm_id,
489 spci_vcpu_index_t vcpu_idx,
Andrew Scull38772ab2019-01-24 15:16:50 +0000490 const struct vcpu *current,
491 struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100492{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100493 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100494 struct vcpu *vcpu;
Andrew Scull6d2db332018-10-10 15:28:17 +0100495 struct hf_vcpu_run_return ret = {
496 .code = HF_VCPU_RUN_WAIT_FOR_INTERRUPT,
Andrew Scullb06d1752019-02-04 10:15:48 +0000497 .sleep.ns = HF_SLEEP_INDEFINITE,
Andrew Scull6d2db332018-10-10 15:28:17 +0100498 };
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100499
500 /* Only the primary VM can switch vcpus. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100501 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100502 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100503 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100504
Andrew Scull19503262018-09-20 14:48:39 +0100505 /* Only secondary VM vcpus can be run. */
506 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100507 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100508 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100509
Andrew Scull19503262018-09-20 14:48:39 +0100510 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100511 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100512 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100513 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100514 }
515
516 /* The requested vcpu must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100517 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100518 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100519 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100520
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000521 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100522 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000523 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000524 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100525 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000526
Andrew Walbran508e63c2018-12-20 17:02:37 +0000527 /*
528 * Inject timer interrupt if timer has expired. It's safe to access
529 * vcpu->regs here because api_vcpu_prepare_run already made sure that
530 * regs_available was true (and then set it to false) before returning
531 * true.
532 */
533 if (arch_timer_pending(&vcpu->regs)) {
534 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100535 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
536 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000537
538 /*
539 * Set the mask bit so the hardware interrupt doesn't fire
540 * again. Ideally we wouldn't do this because it affects what
541 * the secondary vCPU sees, but if we don't then we end up with
542 * a loop of the interrupt firing each time we try to return to
543 * the secondary vCPU.
544 */
545 arch_timer_mask(&vcpu->regs);
546 }
547
Andrew Scull33fecd32019-01-08 14:48:27 +0000548 /* Switch to the vcpu. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000549 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000550
Andrew Scull33fecd32019-01-08 14:48:27 +0000551 /*
552 * Set a placeholder return code to the scheduler. This will be
553 * overwritten when the switch back to the primary occurs.
554 */
555 ret.code = HF_VCPU_RUN_PREEMPTED;
556
Andrew Scull6d2db332018-10-10 15:28:17 +0100557out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100558 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100559}
560
561/**
Andrew Scull81e85092018-12-12 12:56:20 +0000562 * Check that the mode indicates memory that is valid, owned and exclusive.
563 */
Andrew Scullcbefbdb2019-01-11 16:36:26 +0000564static bool api_mode_valid_owned_and_exclusive(int mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000565{
566 return (mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED)) ==
567 0;
568}
569
570/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000571 * Determines the value to be returned by api_vm_configure and api_mailbox_clear
572 * after they've succeeded. If a secondary VM is running and there are waiters,
573 * it also switches back to the primary VM for it to wake waiters up.
574 */
575static int64_t api_waiter_result(struct vm_locked locked_vm,
576 struct vcpu *current, struct vcpu **next)
577{
578 struct vm *vm = locked_vm.vm;
579 struct hf_vcpu_run_return ret = {
580 .code = HF_VCPU_RUN_NOTIFY_WAITERS,
581 };
582
583 if (list_empty(&vm->mailbox.waiter_list)) {
584 /* No waiters, nothing else to do. */
585 return 0;
586 }
587
588 if (vm->id == HF_PRIMARY_VM_ID) {
589 /* The caller is the primary VM. Tell it to wake up waiters. */
590 return 1;
591 }
592
593 /*
594 * Switch back to the primary VM, informing it that there are waiters
595 * that need to be notified.
596 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100597 *next = api_switch_to_primary(current, ret, VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000598
599 return 0;
600}
601
602/**
Andrew Sculle1322792019-07-01 17:46:10 +0100603 * Configures the hypervisor's stage-1 view of the send and receive pages. The
604 * stage-1 page tables must be locked so memory cannot be taken by another core
605 * which could result in this transaction being unable to roll back in the case
606 * of an error.
607 */
608static bool api_vm_configure_stage1(struct vm_locked vm_locked,
609 paddr_t pa_send_begin, paddr_t pa_send_end,
610 paddr_t pa_recv_begin, paddr_t pa_recv_end,
611 struct mpool *local_page_pool)
612{
613 bool ret;
614 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
615
616 /* Map the send page as read-only in the hypervisor address space. */
617 vm_locked.vm->mailbox.send =
618 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
619 MM_MODE_R, local_page_pool);
620 if (!vm_locked.vm->mailbox.send) {
621 /* TODO: partial defrag of failed range. */
622 /* Recover any memory consumed in failed mapping. */
623 mm_defrag(mm_stage1_locked, local_page_pool);
624 goto fail;
625 }
626
627 /*
628 * Map the receive page as writable in the hypervisor address space. On
629 * failure, unmap the send page before returning.
630 */
631 vm_locked.vm->mailbox.recv =
632 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
633 MM_MODE_W, local_page_pool);
634 if (!vm_locked.vm->mailbox.recv) {
635 /* TODO: partial defrag of failed range. */
636 /* Recover any memory consumed in failed mapping. */
637 mm_defrag(mm_stage1_locked, local_page_pool);
638 goto fail_undo_send;
639 }
640
641 ret = true;
642 goto out;
643
644 /*
645 * The following mappings will not require more memory than is available
646 * in the local pool.
647 */
648fail_undo_send:
649 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100650 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
651 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100652
653fail:
654 ret = false;
655
656out:
657 mm_unlock_stage1(&mm_stage1_locked);
658
659 return ret;
660}
661
662/**
663 * Configures the send and receive pages in the VM stage-2 and hypervisor
664 * stage-1 page tables. Locking of the page tables combined with a local memory
665 * pool ensures there will always be enough memory to recover from any errors
666 * that arise.
667 */
668static bool api_vm_configure_pages(struct vm_locked vm_locked,
669 paddr_t pa_send_begin, paddr_t pa_send_end,
670 int orig_send_mode, paddr_t pa_recv_begin,
671 paddr_t pa_recv_end, int orig_recv_mode)
672{
673 bool ret;
674 struct mpool local_page_pool;
675
676 /*
677 * Create a local pool so any freed memory can't be used by another
678 * thread. This is to ensure the original mapping can be restored if any
679 * stage of the process fails.
680 */
681 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
682
683 /* Take memory ownership away from the VM and mark as shared. */
684 if (!mm_vm_identity_map(
685 &vm_locked.vm->ptable, pa_send_begin, pa_send_end,
686 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
687 NULL, &local_page_pool)) {
688 goto fail;
689 }
690
691 if (!mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
692 pa_recv_end,
693 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
694 NULL, &local_page_pool)) {
695 /* TODO: partial defrag of failed range. */
696 /* Recover any memory consumed in failed mapping. */
697 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
698 goto fail_undo_send;
699 }
700
701 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
702 pa_recv_begin, pa_recv_end,
703 &local_page_pool)) {
704 goto fail_undo_send_and_recv;
705 }
706
707 ret = true;
708 goto out;
709
710 /*
711 * The following mappings will not require more memory than is available
712 * in the local pool.
713 */
714fail_undo_send_and_recv:
Andrew Scull7e8de322019-07-02 13:00:56 +0100715 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_recv_begin,
716 pa_recv_end, orig_recv_mode, NULL,
717 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100718
719fail_undo_send:
Andrew Scull7e8de322019-07-02 13:00:56 +0100720 CHECK(mm_vm_identity_map(&vm_locked.vm->ptable, pa_send_begin,
721 pa_send_end, orig_send_mode, NULL,
722 &local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100723
724fail:
725 ret = false;
726
727out:
728 mpool_fini(&local_page_pool);
729
730 return ret;
731}
732
733/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100734 * Configures the VM to send/receive data through the specified pages. The pages
735 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000736 *
737 * Returns:
738 * - -1 on failure.
739 * - 0 on success if no further action is needed.
740 * - 1 if it was called by the primary VM and the primary VM now needs to wake
741 * up or kick waiters. Waiters should be retrieved by calling
742 * hf_mailbox_waiter_get.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100743 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000744int64_t api_vm_configure(ipaddr_t send, ipaddr_t recv, struct vcpu *current,
745 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100746{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100747 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100748 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100749 paddr_t pa_send_begin;
750 paddr_t pa_send_end;
751 paddr_t pa_recv_begin;
752 paddr_t pa_recv_end;
Andrew Scull220e6212018-12-21 18:09:00 +0000753 int orig_send_mode;
754 int orig_recv_mode;
Andrew Scullc0e569a2018-10-02 18:05:21 +0100755 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100756
757 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000758 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
759 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100760 return -1;
761 }
762
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000763 /* Convert to physical addresses. */
764 pa_send_begin = pa_from_ipa(send);
765 pa_send_end = pa_add(pa_send_begin, PAGE_SIZE);
766
767 pa_recv_begin = pa_from_ipa(recv);
768 pa_recv_end = pa_add(pa_recv_begin, PAGE_SIZE);
769
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100770 /* Fail if the same page is used for the send and receive pages. */
771 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Scull220e6212018-12-21 18:09:00 +0000772 return -1;
773 }
774
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100775 /*
776 * The hypervisor's memory map must be locked for the duration of this
777 * operation to ensure there will be sufficient memory to recover from
778 * any failures.
779 *
780 * TODO: the scope of the can be reduced but will require restructuring
781 * to keep a single unlock point.
782 */
Andrew Sculle1322792019-07-01 17:46:10 +0100783 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000784
785 /* We only allow these to be setup once. */
786 if (vm->mailbox.send || vm->mailbox.recv) {
787 goto fail;
788 }
789
790 /*
791 * Ensure the pages are valid, owned and exclusive to the VM and that
792 * the VM has the required access to the memory.
793 */
794 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
795 &orig_send_mode) ||
796 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
797 (orig_send_mode & MM_MODE_R) == 0 ||
798 (orig_send_mode & MM_MODE_W) == 0) {
799 goto fail;
800 }
801
802 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
803 &orig_recv_mode) ||
804 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
805 (orig_recv_mode & MM_MODE_R) == 0) {
806 goto fail;
807 }
808
Andrew Sculle1322792019-07-01 17:46:10 +0100809 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
810 orig_send_mode, pa_recv_begin, pa_recv_end,
811 orig_recv_mode)) {
812 goto fail;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100813 }
814
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000815 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100816 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000817 goto exit;
818
Andrew Scull220e6212018-12-21 18:09:00 +0000819fail:
820 ret = -1;
821
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100822exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100823 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100824
825 return ret;
826}
827
828/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100829 * Copies data from the sender's send buffer to the recipient's receive buffer
830 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000831 *
832 * If the recipient's receive buffer is busy, it can optionally register the
833 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100834 */
Jose Marinho75509b42019-04-09 09:34:59 +0100835spci_return_t api_spci_msg_send(uint32_t attributes, struct vcpu *current,
836 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100837{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100838 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100839 struct vm *to;
Jose Marinho75509b42019-04-09 09:34:59 +0100840
841 struct two_vm_locked vm_from_to_lock;
842
Andrew Scullb06d1752019-02-04 10:15:48 +0000843 struct hf_vcpu_run_return primary_ret = {
844 .code = HF_VCPU_RUN_MESSAGE,
845 };
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000846 struct spci_message from_msg_replica;
847 struct spci_message *to_msg;
848 const struct spci_message *from_msg;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100849
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000850 uint32_t size;
Andrew Scull19503262018-09-20 14:48:39 +0100851
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000852 int64_t ret;
853 bool notify = (attributes & SPCI_MSG_SEND_NOTIFY_MASK) ==
854 SPCI_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100855
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000856 /*
857 * Check that the sender has configured its send buffer. Copy the
858 * message header. If the tx mailbox at from_msg is configured (i.e.
859 * from_msg != NULL) then it can be safely accessed after releasing the
860 * lock since the tx mailbox address can only be configured once.
861 */
862 sl_lock(&from->lock);
863 from_msg = from->mailbox.send;
864 sl_unlock(&from->lock);
865
866 if (from_msg == NULL) {
867 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100868 }
869
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100870 /*
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000871 * Note that the payload is not copied when the message header is.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100872 */
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000873 from_msg_replica = *from_msg;
874
875 /* Ensure source VM id corresponds to the current VM. */
876 if (from_msg_replica.source_vm_id != from->id) {
877 return SPCI_INVALID_PARAMETERS;
878 }
879
880 size = from_msg_replica.length;
881 /* Limit the size of transfer. */
Andrew Scull1262ac22019-04-05 12:44:26 +0100882 if (size > SPCI_MSG_PAYLOAD_MAX) {
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000883 return SPCI_INVALID_PARAMETERS;
884 }
885
886 /* Disallow reflexive requests as this suggests an error in the VM. */
887 if (from_msg_replica.target_vm_id == from->id) {
888 return SPCI_INVALID_PARAMETERS;
889 }
890
891 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100892 to = vm_find(from_msg_replica.target_vm_id);
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000893 if (to == NULL) {
894 return SPCI_INVALID_PARAMETERS;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100895 }
896
Jose Marinho75509b42019-04-09 09:34:59 +0100897 /*
898 * Hf needs to hold the lock on <to> before the mailbox state is
899 * checked. The lock on <to> must be held until the information is
900 * copied to <to> Rx buffer. Since in
901 * spci_msg_handle_architected_message we may call api_spci_share_memory
902 * which must hold the <from> lock, we must hold the <from> lock at this
903 * point to prevent a deadlock scenario.
904 */
905 vm_from_to_lock = vm_lock_both(to, from);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100906
Andrew Sculld6ee1102019-04-05 22:12:42 +0100907 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
Andrew Scullaa039b32018-10-04 15:02:26 +0100908 to->mailbox.recv == NULL) {
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000909 /*
910 * Fail if the target isn't currently ready to receive data,
911 * setting up for notification if requested.
912 */
913 if (notify) {
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000914 struct wait_entry *entry =
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000915 &current->vm->wait_entries
916 [from_msg_replica.target_vm_id];
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000917
918 /* Append waiter only if it's not there yet. */
919 if (list_empty(&entry->wait_links)) {
920 list_append(&to->mailbox.waiter_list,
921 &entry->wait_links);
922 }
923 }
924
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000925 ret = SPCI_BUSY;
Andrew Scullaa039b32018-10-04 15:02:26 +0100926 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100927 }
928
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000929 to_msg = to->mailbox.recv;
Jose Marinho75509b42019-04-09 09:34:59 +0100930
931 /* Handle architected messages. */
932 if ((from_msg_replica.flags & SPCI_MESSAGE_IMPDEF_MASK) !=
933 SPCI_MESSAGE_IMPDEF) {
934 /*
935 * Buffer holding the internal copy of the shared memory
936 * regions.
937 */
938 /* TODO: Buffer is temporarily in the stack. */
939 uint8_t message_buffer
940 [sizeof(struct spci_architected_message_header) +
941 sizeof(struct spci_memory_region_constituent) +
942 sizeof(struct spci_memory_region)];
943
944 struct spci_architected_message_header *architected_header =
945 spci_get_architected_message_header(from->mailbox.send);
946
947 const struct spci_architected_message_header
948 *architected_message_replica;
949
950 if (from_msg_replica.length > sizeof(message_buffer)) {
951 ret = SPCI_INVALID_PARAMETERS;
952 goto out;
953 }
954
955 if (from_msg_replica.length <
956 sizeof(struct spci_architected_message_header)) {
957 ret = SPCI_INVALID_PARAMETERS;
958 goto out;
959 }
960
961 /* Copy the architected message into an internal buffer. */
962 memcpy_s(message_buffer, sizeof(message_buffer),
963 architected_header, from_msg_replica.length);
964
965 architected_message_replica =
966 (struct spci_architected_message_header *)
967 message_buffer;
968
969 /*
970 * Note that message_buffer is passed as the third parameter to
971 * spci_msg_handle_architected_message. The execution flow
972 * commencing at spci_msg_handle_architected_message will make
973 * several accesses to fields in message_buffer. The memory area
974 * message_buffer must be exclusively owned by Hf so that TOCTOU
975 * issues do not arise.
976 */
977 ret = spci_msg_handle_architected_message(
978 vm_from_to_lock.vm1, vm_from_to_lock.vm2,
979 architected_message_replica, &from_msg_replica, to_msg);
980
981 if (ret != SPCI_SUCCESS) {
982 goto out;
983 }
984 } else {
985 /* Copy data. */
986 memcpy_s(to_msg->payload, SPCI_MSG_PAYLOAD_MAX,
987 from->mailbox.send->payload, size);
988 *to_msg = from_msg_replica;
989 }
990
Andrew Scullb06d1752019-02-04 10:15:48 +0000991 primary_ret.message.vm_id = to->id;
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000992 ret = SPCI_SUCCESS;
Andrew Scullaa039b32018-10-04 15:02:26 +0100993
994 /* Messages for the primary VM are delivered directly. */
995 if (to->id == HF_PRIMARY_VM_ID) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100996 to->mailbox.state = MAILBOX_STATE_READ;
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000997 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100998 VCPU_STATE_READY);
Andrew Scullaa039b32018-10-04 15:02:26 +0100999 goto out;
1000 }
1001
Andrew Sculld6ee1102019-04-05 22:12:42 +01001002 to->mailbox.state = MAILBOX_STATE_RECEIVED;
Andrew Scullaa039b32018-10-04 15:02:26 +01001003
1004 /* Return to the primary VM directly or with a switch. */
Andrew Scullb06d1752019-02-04 10:15:48 +00001005 if (from->id != HF_PRIMARY_VM_ID) {
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +00001006 *next = api_switch_to_primary(current, primary_ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001007 VCPU_STATE_READY);
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001008 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001009
1010out:
Jose Marinho75509b42019-04-09 09:34:59 +01001011 vm_unlock(&vm_from_to_lock.vm1);
1012 vm_unlock(&vm_from_to_lock.vm2);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001013
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001014 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001015}
1016
1017/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001018 * Receives a message from the mailbox. If one isn't available, this function
1019 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001020 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001021 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001022 */
Jose Marinho3e2442f2019-03-12 13:30:37 +00001023int32_t api_spci_msg_recv(uint32_t attributes, struct vcpu *current,
1024 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001025{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001026 struct vm *vm = current->vm;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001027 int32_t return_code;
1028 bool block =
1029 (attributes & SPCI_MSG_RECV_BLOCK_MASK) == SPCI_MSG_RECV_BLOCK;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001030
Andrew Scullaa039b32018-10-04 15:02:26 +01001031 /*
1032 * The primary VM will receive messages as a status code from running
1033 * vcpus and must not call this function.
1034 */
Andrew Scull19503262018-09-20 14:48:39 +01001035 if (vm->id == HF_PRIMARY_VM_ID) {
Jose Marinho3e2442f2019-03-12 13:30:37 +00001036 return SPCI_INTERRUPTED;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001037 }
1038
1039 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001040
Andrew Scullaa039b32018-10-04 15:02:26 +01001041 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001042 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1043 vm->mailbox.state = MAILBOX_STATE_READ;
Jose Marinho3e2442f2019-03-12 13:30:37 +00001044 return_code = SPCI_SUCCESS;
1045 goto out;
1046 }
1047
1048 /* No pending message so fail if not allowed to block. */
1049 if (!block) {
1050 return_code = SPCI_RETRY;
Andrew Scullaa039b32018-10-04 15:02:26 +01001051 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001052 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001053
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001054 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001055 * From this point onward this call can only be interrupted or a message
1056 * received. If a message is received the return value will be set at
1057 * that time to SPCI_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001058 */
Jose Marinho3e2442f2019-03-12 13:30:37 +00001059 return_code = SPCI_INTERRUPTED;
1060
1061 /*
1062 * Don't block if there are enabled and pending interrupts, to match
1063 * behaviour of wait_for_interrupt.
1064 */
1065 if (current->interrupts.enabled_and_pending_count > 0) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001066 goto out;
1067 }
1068
Andrew Scullaa039b32018-10-04 15:02:26 +01001069 /* Switch back to primary vm to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001070 {
1071 struct hf_vcpu_run_return run_return = {
Andrew Scullb06d1752019-02-04 10:15:48 +00001072 .code = HF_VCPU_RUN_WAIT_FOR_MESSAGE,
Andrew Walbranb4816552018-12-05 17:35:42 +00001073 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001074
Andrew Walbranb4816552018-12-05 17:35:42 +00001075 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001076 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001077 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001078out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001079 sl_unlock(&vm->lock);
1080
Jose Marinho3e2442f2019-03-12 13:30:37 +00001081 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001082}
1083
1084/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001085 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1086 * by this function, the caller must have called api_mailbox_send before with
1087 * the notify argument set to true, and this call must have failed because the
1088 * mailbox was not available.
1089 *
1090 * It should be called repeatedly to retrieve a list of VMs.
1091 *
1092 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1093 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001094 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001095int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001096{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001097 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001098 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001099 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001100
1101 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001102 if (list_empty(&vm->mailbox.ready_list)) {
1103 ret = -1;
1104 goto exit;
1105 }
1106
1107 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1108 ready_links);
1109 list_remove(&entry->ready_links);
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001110 ret = entry - vm->wait_entries;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001111
1112exit:
1113 sl_unlock(&vm->lock);
1114 return ret;
1115}
1116
1117/**
1118 * Retrieves the next VM waiting to be notified that the mailbox of the
1119 * specified VM became writable. Only primary VMs are allowed to call this.
1120 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001121 * Returns -1 on failure or if there are no waiters; the VM id of the next
1122 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001123 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001124int64_t api_mailbox_waiter_get(spci_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001125{
1126 struct vm *vm;
1127 struct vm_locked locked;
1128 struct wait_entry *entry;
1129 struct vm *waiting_vm;
1130
1131 /* Only primary VMs are allowed to call this function. */
1132 if (current->vm->id != HF_PRIMARY_VM_ID) {
1133 return -1;
1134 }
1135
Andrew Walbran42347a92019-05-09 13:59:03 +01001136 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001137 if (vm == NULL) {
1138 return -1;
1139 }
1140
1141 /* Check if there are outstanding notifications from given vm. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001142 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001143 entry = api_fetch_waiter(locked);
1144 vm_unlock(&locked);
1145
1146 if (entry == NULL) {
1147 return -1;
1148 }
1149
1150 /* Enqueue notification to waiting VM. */
1151 waiting_vm = entry->waiting_vm;
1152
1153 sl_lock(&waiting_vm->lock);
1154 if (list_empty(&entry->ready_links)) {
1155 list_append(&waiting_vm->mailbox.ready_list,
1156 &entry->ready_links);
1157 }
1158 sl_unlock(&waiting_vm->lock);
1159
1160 return waiting_vm->id;
1161}
1162
1163/**
1164 * Clears the caller's mailbox so that a new message can be received. The caller
1165 * must have copied out all data they wish to preserve as new messages will
1166 * overwrite the old and will arrive asynchronously.
1167 *
1168 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001169 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001170 * - 0 on success if no further action is needed.
1171 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1172 * up or kick waiters. Waiters should be retrieved by calling
1173 * hf_mailbox_waiter_get.
1174 */
1175int64_t api_mailbox_clear(struct vcpu *current, struct vcpu **next)
1176{
1177 struct vm *vm = current->vm;
1178 struct vm_locked locked;
1179 int64_t ret;
1180
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001181 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001182 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001183 case MAILBOX_STATE_EMPTY:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001184 ret = 0;
1185 break;
1186
Andrew Sculld6ee1102019-04-05 22:12:42 +01001187 case MAILBOX_STATE_RECEIVED:
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001188 ret = -1;
1189 break;
1190
Andrew Sculld6ee1102019-04-05 22:12:42 +01001191 case MAILBOX_STATE_READ:
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001192 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001193 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001194 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001195 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001196 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001197
1198 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001199}
Andrew Walbran318f5732018-11-20 16:23:42 +00001200
1201/**
1202 * Enables or disables a given interrupt ID for the calling vCPU.
1203 *
1204 * Returns 0 on success, or -1 if the intid is invalid.
1205 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001206int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001207{
1208 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
1209 uint32_t intid_mask = 1u << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001210
Andrew Walbran318f5732018-11-20 16:23:42 +00001211 if (intid >= HF_NUM_INTIDS) {
1212 return -1;
1213 }
1214
1215 sl_lock(&current->lock);
1216 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001217 /*
1218 * If it is pending and was not enabled before, increment the
1219 * count.
1220 */
1221 if (current->interrupts.interrupt_pending[intid_index] &
1222 ~current->interrupts.interrupt_enabled[intid_index] &
1223 intid_mask) {
1224 current->interrupts.enabled_and_pending_count++;
1225 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001226 current->interrupts.interrupt_enabled[intid_index] |=
1227 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001228 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001229 /*
1230 * If it is pending and was enabled before, decrement the count.
1231 */
1232 if (current->interrupts.interrupt_pending[intid_index] &
1233 current->interrupts.interrupt_enabled[intid_index] &
1234 intid_mask) {
1235 current->interrupts.enabled_and_pending_count--;
1236 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001237 current->interrupts.interrupt_enabled[intid_index] &=
1238 ~intid_mask;
1239 }
1240
1241 sl_unlock(&current->lock);
1242 return 0;
1243}
1244
1245/**
1246 * Returns the ID of the next pending interrupt for the calling vCPU, and
1247 * acknowledges it (i.e. marks it as no longer pending). Returns
1248 * HF_INVALID_INTID if there are no pending interrupts.
1249 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001250uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001251{
1252 uint8_t i;
1253 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001254
1255 /*
1256 * Find the first enabled and pending interrupt ID, return it, and
1257 * deactivate it.
1258 */
1259 sl_lock(&current->lock);
1260 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1261 uint32_t enabled_and_pending =
1262 current->interrupts.interrupt_enabled[i] &
1263 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001264
Andrew Walbran318f5732018-11-20 16:23:42 +00001265 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001266 uint8_t bit_index = ctz(enabled_and_pending);
1267 /*
1268 * Mark it as no longer pending and decrement the count.
1269 */
1270 current->interrupts.interrupt_pending[i] &=
1271 ~(1u << bit_index);
1272 current->interrupts.enabled_and_pending_count--;
1273 first_interrupt =
1274 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001275 break;
1276 }
1277 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001278
1279 sl_unlock(&current->lock);
1280 return first_interrupt;
1281}
1282
1283/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001284 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001285 * given VM and vCPU.
1286 */
1287static inline bool is_injection_allowed(uint32_t target_vm_id,
1288 struct vcpu *current)
1289{
1290 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001291
Andrew Walbran318f5732018-11-20 16:23:42 +00001292 /*
1293 * The primary VM is allowed to inject interrupts into any VM. Secondary
1294 * VMs are only allowed to inject interrupts into their own vCPUs.
1295 */
1296 return current_vm_id == HF_PRIMARY_VM_ID ||
1297 current_vm_id == target_vm_id;
1298}
1299
1300/**
1301 * Injects a virtual interrupt of the given ID into the given target vCPU.
1302 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1303 * when the vCPU is next run, which is up to the scheduler.
1304 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001305 * Returns:
1306 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1307 * ID is invalid, or the current VM is not allowed to inject interrupts to
1308 * the target VM.
1309 * - 0 on success if no further action is needed.
1310 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1311 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001312 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001313int64_t api_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +01001314 spci_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001315 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001316{
Andrew Walbran318f5732018-11-20 16:23:42 +00001317 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001318 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001319
1320 if (intid >= HF_NUM_INTIDS) {
1321 return -1;
1322 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001323
Andrew Walbran318f5732018-11-20 16:23:42 +00001324 if (target_vm == NULL) {
1325 return -1;
1326 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001327
Andrew Walbran318f5732018-11-20 16:23:42 +00001328 if (target_vcpu_idx >= target_vm->vcpu_count) {
1329 /* The requested vcpu must exist. */
1330 return -1;
1331 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001332
Andrew Walbran318f5732018-11-20 16:23:42 +00001333 if (!is_injection_allowed(target_vm_id, current)) {
1334 return -1;
1335 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001336
Andrew Walbrane1310df2019-04-29 17:28:28 +01001337 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001338
1339 dlog("Injecting IRQ %d for VM %d VCPU %d from VM %d VCPU %d\n", intid,
1340 target_vm_id, target_vcpu_idx, current->vm->id, current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001341 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001342}
Andrew Scull6386f252018-12-06 13:29:10 +00001343
1344/**
1345 * Clears a region of physical memory by overwriting it with zeros. The data is
1346 * flushed from the cache so the memory has been cleared across the system.
1347 */
1348static bool api_clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
1349{
1350 /*
1351 * TODO: change this to a cpu local single page window rather than a
1352 * global mapping of the whole range. Such an approach will limit
1353 * the changes to stage-1 tables and will allow only local
1354 * invalidation.
1355 */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001356 bool ret;
1357 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
1358 void *ptr =
1359 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
Andrew Walbran2cb43392019-04-17 12:52:45 +01001360 size_t size = pa_difference(begin, end);
Andrew Scull6386f252018-12-06 13:29:10 +00001361
1362 if (!ptr) {
1363 /* TODO: partial defrag of failed range. */
1364 /* Recover any memory consumed in failed mapping. */
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001365 mm_defrag(stage1_locked, ppool);
1366 goto fail;
Andrew Scull6386f252018-12-06 13:29:10 +00001367 }
1368
Andrew Scull2b5fbad2019-04-05 13:55:56 +01001369 memset_s(ptr, size, 0, size);
Andrew Scull6386f252018-12-06 13:29:10 +00001370 arch_mm_write_back_dcache(ptr, size);
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001371 mm_unmap(stage1_locked, begin, end, ppool);
Andrew Scull6386f252018-12-06 13:29:10 +00001372
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001373 ret = true;
1374 goto out;
1375
1376fail:
1377 ret = false;
1378
1379out:
1380 mm_unlock_stage1(&stage1_locked);
1381
1382 return ret;
Andrew Scull6386f252018-12-06 13:29:10 +00001383}
1384
Jose Marinho75509b42019-04-09 09:34:59 +01001385/** TODO: Move function to spci_architectted_message.c. */
1386/**
1387 * Shares memory from the calling VM with another. The memory can be shared in
1388 * different modes.
1389 *
1390 * This function requires the calling context to hold the <to> and <from> locks.
1391 *
1392 * Returns:
1393 * In case of error one of the following values is returned:
1394 * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were
1395 * erroneous;
1396 * 2) SPCI_NO_MEMORY - Hf did not have sufficient memory to complete
1397 * the request.
1398 * Success is indicated by SPCI_SUCCESS.
1399 */
1400spci_return_t api_spci_share_memory(struct vm_locked to_locked,
1401 struct vm_locked from_locked,
1402 struct spci_memory_region *memory_region,
1403 uint32_t memory_to_attributes,
1404 enum spci_memory_share share)
1405{
1406 struct vm *to = to_locked.vm;
1407 struct vm *from = from_locked.vm;
1408 int orig_from_mode;
1409 int from_mode;
1410 int to_mode;
1411 struct mpool local_page_pool;
1412 int64_t ret;
1413 paddr_t pa_begin;
1414 paddr_t pa_end;
1415 ipaddr_t begin;
1416 ipaddr_t end;
1417
1418 size_t size;
1419
1420 /* Disallow reflexive shares as this suggests an error in the VM. */
1421 if (to == from) {
1422 return SPCI_INVALID_PARAMETERS;
1423 }
1424
1425 /*
1426 * Create a local pool so any freed memory can't be used by another
1427 * thread. This is to ensure the original mapping can be restored if any
1428 * stage of the process fails.
1429 */
1430 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1431
1432 /* Obtain the single contiguous set of pages from the memory_region. */
1433 /* TODO: Add support for multiple constituent regions. */
1434 size = memory_region->constituents[0].page_count * PAGE_SIZE;
1435 begin = ipa_init(memory_region->constituents[0].address);
1436 end = ipa_add(begin, size);
1437
1438 /*
1439 * Check if the state transition is lawful for both VMs involved
1440 * in the memory exchange, ensure that all constituents of a memory
1441 * region being shared are at the same state.
1442 */
1443 if (!spci_msg_check_transition(to, from, share, &orig_from_mode, begin,
1444 end, memory_to_attributes, &from_mode,
1445 &to_mode)) {
1446 return SPCI_INVALID_PARAMETERS;
1447 }
1448
1449 pa_begin = pa_from_ipa(begin);
1450 pa_end = pa_from_ipa(end);
1451
1452 /*
1453 * First update the mapping for the sender so there is not overlap with
1454 * the recipient.
1455 */
1456 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1457 NULL, &local_page_pool)) {
1458 ret = SPCI_NO_MEMORY;
1459 goto out;
1460 }
1461
1462 /* Complete the transfer by mapping the memory into the recipient. */
1463 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1464 &local_page_pool)) {
1465 /* TODO: partial defrag of failed range. */
1466 /* Recover any memory consumed in failed mapping. */
1467 mm_vm_defrag(&from->ptable, &local_page_pool);
1468
1469 ret = SPCI_NO_MEMORY;
1470
1471 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1472 orig_from_mode, NULL,
1473 &local_page_pool));
1474
1475 goto out;
1476 }
1477
1478 ret = SPCI_SUCCESS;
1479
1480out:
1481
1482 mpool_fini(&local_page_pool);
1483
1484 return ret;
1485}
1486
Andrew Scull6386f252018-12-06 13:29:10 +00001487/**
1488 * Shares memory from the calling VM with another. The memory can be shared in
1489 * different modes.
1490 *
1491 * TODO: the interface for sharing memory will need to be enhanced to allow
1492 * sharing with different modes e.g. read-only, informing the recipient
1493 * of the memory they have been given, opting to not wipe the memory and
1494 * possibly allowing multiple blocks to be transferred. What this will
1495 * look like is TBD.
1496 */
Andrew Walbran42347a92019-05-09 13:59:03 +01001497int64_t api_share_memory(spci_vm_id_t vm_id, ipaddr_t addr, size_t size,
Andrew Scull6386f252018-12-06 13:29:10 +00001498 enum hf_share share, struct vcpu *current)
1499{
1500 struct vm *from = current->vm;
1501 struct vm *to;
1502 int orig_from_mode;
1503 int from_mode;
1504 int to_mode;
1505 ipaddr_t begin;
1506 ipaddr_t end;
1507 paddr_t pa_begin;
1508 paddr_t pa_end;
1509 struct mpool local_page_pool;
1510 int64_t ret;
1511
1512 /* Disallow reflexive shares as this suggests an error in the VM. */
1513 if (vm_id == from->id) {
1514 return -1;
1515 }
1516
1517 /* Ensure the target VM exists. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001518 to = vm_find(vm_id);
Andrew Scull6386f252018-12-06 13:29:10 +00001519 if (to == NULL) {
1520 return -1;
1521 }
1522
1523 begin = addr;
1524 end = ipa_add(addr, size);
1525
1526 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +00001527 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
1528 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Scull6386f252018-12-06 13:29:10 +00001529 return -1;
1530 }
1531
1532 /* Convert the sharing request to memory management modes. */
1533 switch (share) {
1534 case HF_MEMORY_GIVE:
1535 from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED;
1536 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X;
1537 break;
1538
1539 case HF_MEMORY_LEND:
1540 from_mode = MM_MODE_INVALID;
1541 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED;
1542 break;
1543
1544 case HF_MEMORY_SHARE:
1545 from_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_SHARED;
1546 to_mode = MM_MODE_R | MM_MODE_W | MM_MODE_X | MM_MODE_UNOWNED |
1547 MM_MODE_SHARED;
1548 break;
1549
1550 default:
1551 /* The input is untrusted so might not be a valid value. */
1552 return -1;
1553 }
1554
1555 /*
1556 * Create a local pool so any freed memory can't be used by another
1557 * thread. This is to ensure the original mapping can be restored if any
1558 * stage of the process fails.
1559 */
1560 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1561
1562 sl_lock_both(&from->lock, &to->lock);
1563
1564 /*
1565 * Ensure that the memory range is mapped with the same mode so that
1566 * changes can be reverted if the process fails.
1567 */
1568 if (!mm_vm_get_mode(&from->ptable, begin, end, &orig_from_mode)) {
1569 goto fail;
1570 }
1571
1572 /*
1573 * Ensure the memory range is valid for the sender. If it isn't, the
1574 * sender has either shared it with another VM already or has no claim
1575 * to the memory.
1576 */
1577 if (orig_from_mode & MM_MODE_INVALID) {
1578 goto fail;
1579 }
1580
1581 /*
1582 * The sender must own the memory and have exclusive access to it in
1583 * order to share it. Alternatively, it is giving memory back to the
1584 * owning VM.
1585 */
1586 if (orig_from_mode & MM_MODE_UNOWNED) {
1587 int orig_to_mode;
1588
1589 if (share != HF_MEMORY_GIVE ||
1590 !mm_vm_get_mode(&to->ptable, begin, end, &orig_to_mode) ||
1591 orig_to_mode & MM_MODE_UNOWNED) {
1592 goto fail;
1593 }
1594 } else if (orig_from_mode & MM_MODE_SHARED) {
1595 goto fail;
1596 }
1597
1598 pa_begin = pa_from_ipa(begin);
1599 pa_end = pa_from_ipa(end);
1600
1601 /*
1602 * First update the mapping for the sender so there is not overlap with
1603 * the recipient.
1604 */
1605 if (!mm_vm_identity_map(&from->ptable, pa_begin, pa_end, from_mode,
1606 NULL, &local_page_pool)) {
1607 goto fail;
1608 }
1609
1610 /* Clear the memory so no VM or device can see the previous contents. */
1611 if (!api_clear_memory(pa_begin, pa_end, &local_page_pool)) {
1612 goto fail_return_to_sender;
1613 }
1614
1615 /* Complete the transfer by mapping the memory into the recipient. */
1616 if (!mm_vm_identity_map(&to->ptable, pa_begin, pa_end, to_mode, NULL,
1617 &local_page_pool)) {
1618 /* TODO: partial defrag of failed range. */
1619 /* Recover any memory consumed in failed mapping. */
1620 mm_vm_defrag(&from->ptable, &local_page_pool);
1621 goto fail_return_to_sender;
1622 }
1623
1624 ret = 0;
1625 goto out;
1626
1627fail_return_to_sender:
Andrew Scull7e8de322019-07-02 13:00:56 +01001628 CHECK(mm_vm_identity_map(&from->ptable, pa_begin, pa_end,
1629 orig_from_mode, NULL, &local_page_pool));
Andrew Scull6386f252018-12-06 13:29:10 +00001630
1631fail:
1632 ret = -1;
1633
1634out:
1635 sl_unlock(&from->lock);
1636 sl_unlock(&to->lock);
1637
1638 mpool_fini(&local_page_pool);
1639
1640 return ret;
1641}
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001642
1643/** Returns the version of the implemented SPCI specification. */
1644int32_t api_spci_version(void)
1645{
1646 /*
1647 * Ensure that both major and minor revision representation occupies at
1648 * most 15 bits.
1649 */
1650 static_assert(0x8000 > SPCI_VERSION_MAJOR,
1651 "Major revision representation take more than 15 bits.");
1652 static_assert(0x10000 > SPCI_VERSION_MINOR,
1653 "Minor revision representation take more than 16 bits.");
1654
1655 return (SPCI_VERSION_MAJOR << SPCI_VERSION_MAJOR_OFFSET) |
1656 SPCI_VERSION_MINOR;
1657}