blob: 61e8698136b5c5470c9762d4a0395951fe2a5d21 [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Andrew Walbran2619e0a2020-01-10 16:37:50 +000012#include "hf/arch/tee.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000013#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000014
Andrew Scull877ae4b2019-07-02 12:52:33 +010015#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000016#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "hf/ffa_internal.h"
18#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000019#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010020#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000021#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010022#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/vm.h"
25
Andrew Scullf35a5c92018-08-07 18:09:46 +010026#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010027#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000029/*
30 * To eliminate the risk of deadlocks, we define a partial order for the
31 * acquisition of locks held concurrently by the same physical CPU. Our current
32 * ordering requirements are as follows:
33 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010034 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000035 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010036 * Locks of the same kind require the lock of lowest address to be locked first,
37 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000038 */
39
Andrew Scullaa039b32018-10-04 15:02:26 +010040static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010041 "Currently, a page is mapped for the send and receive buffers so "
42 "the maximum request is the size of a page.");
43
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000044static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
45 "The page pool entry size must be at least as big as the mailbox "
46 "size, so that memory region descriptors can be copied from the "
47 "mailbox for memory sharing.");
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/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000061 * 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
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010064 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010065 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010066static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010067 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000068 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069{
Andrew Walbran42347a92019-05-09 13:59:03 +010070 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010071 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010072
Andrew Walbran508e63c2018-12-20 17:02:37 +000073 /*
74 * If the secondary is blocked but has a timer running, sleep until the
75 * timer fires rather than indefinitely.
76 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010077 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010078 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
79 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010080 if (arch_timer_enabled_current()) {
81 uint64_t remaining_ns =
82 arch_timer_remaining_ns_current();
83
84 if (remaining_ns == 0) {
85 /*
86 * Timer is pending, so the current vCPU should
87 * be run again right away.
88 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010089 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010090 /*
91 * primary_ret.arg1 should already be set to the
92 * current VM ID and vCPU ID.
93 */
94 primary_ret.arg2 = 0;
95 } else {
96 primary_ret.arg2 = remaining_ns;
97 }
98 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100100 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000101 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100102 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000103
104 default:
105 /* Do nothing. */
106 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000107 }
108
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100109 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100110 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100111
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000112 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000113 sl_lock(&current->lock);
114 current->state = secondary_state;
115 sl_unlock(&current->lock);
116
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100117 return next;
118}
119
120/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000121 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000122 */
123struct vcpu *api_preempt(struct vcpu *current)
124{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100125 struct ffa_value ret = {
126 .func = FFA_INTERRUPT_32,
127 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000128 };
129
Andrew Sculld6ee1102019-04-05 22:12:42 +0100130 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000131}
132
133/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000134 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000135 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100136 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100137struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100138{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100139 struct ffa_value ret = {
140 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
141 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100142 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000143
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000144 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100145 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100146}
147
148/**
Andrew Walbran33645652019-04-15 12:29:31 +0100149 * Puts the current vCPU in off mode, and returns to the primary VM.
150 */
151struct vcpu *api_vcpu_off(struct vcpu *current)
152{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_value ret = {
154 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
155 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100156 };
157
158 /*
159 * Disable the timer, so the scheduler doesn't get told to call back
160 * based on it.
161 */
162 arch_timer_disable_current();
163
164 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
165}
166
167/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000168 * Returns to the primary VM to allow this CPU to be used for other tasks as the
169 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100170 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000171 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100172void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000173{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100174 struct ffa_value primary_ret = {
175 .func = FFA_YIELD_32,
176 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull66d62bf2019-02-01 13:54:10 +0000177 };
178
179 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000180 /* NOOP on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100181 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000182 }
183
Andrew Walbran16075b62019-09-03 17:11:07 +0100184 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000185}
186
187/**
Andrew Walbran33645652019-04-15 12:29:31 +0100188 * Switches to the primary so that it can switch to the target, or kick it if it
189 * is already running on a different physical CPU.
190 */
191struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
192{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100193 struct ffa_value ret = {
194 .func = HF_FFA_RUN_WAKE_UP,
195 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
196 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100197 };
198 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
199}
200
201/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000202 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000203 */
204struct vcpu *api_abort(struct vcpu *current)
205{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100206 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000207
Andrew Walbran17eebf92020-02-05 16:35:49 +0000208 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
209 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000210
211 if (current->vm->id == HF_PRIMARY_VM_ID) {
212 /* TODO: what to do when the primary aborts? */
213 for (;;) {
214 /* Do nothing. */
215 }
216 }
217
218 atomic_store_explicit(&current->vm->aborting, true,
219 memory_order_relaxed);
220
221 /* TODO: free resources once all vCPUs abort. */
222
Andrew Sculld6ee1102019-04-05 22:12:42 +0100223 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000224}
225
226/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000227 * Returns the ID of the VM.
228 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100229struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000230{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100231 return (struct ffa_value){.func = FFA_SUCCESS_32,
232 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000233}
234
235/**
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100236 * Returns the number of VMs configured to run.
237 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100238ffa_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100239{
Andrew Scull19503262018-09-20 14:48:39 +0100240 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100241}
242
243/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100244 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
245 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100246 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100247ffa_vcpu_count_t api_vcpu_get_count(ffa_vm_id_t vm_id,
248 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100249{
Andrew Scull19503262018-09-20 14:48:39 +0100250 struct vm *vm;
251
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000252 /* Only the primary VM needs to know about vCPUs for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100253 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100254 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100255 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100256
Andrew Walbran42347a92019-05-09 13:59:03 +0100257 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100258 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100259 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100260 }
261
262 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100263}
264
265/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000266 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000267 * function to indicate that register state for the given vCPU has been saved
268 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000269 */
270void api_regs_state_saved(struct vcpu *vcpu)
271{
272 sl_lock(&vcpu->lock);
273 vcpu->regs_available = true;
274 sl_unlock(&vcpu->lock);
275}
276
277/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000278 * Retrieves the next waiter and removes it from the wait list if the VM's
279 * mailbox is in a writable state.
280 */
281static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
282{
283 struct wait_entry *entry;
284 struct vm *vm = locked_vm.vm;
285
Andrew Sculld6ee1102019-04-05 22:12:42 +0100286 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000287 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
288 /* The mailbox is not writable or there are no waiters. */
289 return NULL;
290 }
291
292 /* Remove waiter from the wait list. */
293 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
294 wait_links);
295 list_remove(&entry->wait_links);
296 return entry;
297}
298
299/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000300 * Assuming that the arguments have already been checked by the caller, injects
301 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
302 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
303 * is next run, which is up to the scheduler.
304 *
305 * Returns:
306 * - 0 on success if no further action is needed.
307 * - 1 if it was called by the primary VM and the primary VM now needs to wake
308 * up or kick the target vCPU.
309 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100310static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000311 uint32_t intid, struct vcpu *current,
312 struct vcpu **next)
313{
314 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100315 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000316 int64_t ret = 0;
317
318 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000319
320 /*
321 * We only need to change state and (maybe) trigger a virtual IRQ if it
322 * is enabled and was not previously pending. Otherwise we can skip
323 * everything except setting the pending bit.
324 *
325 * If you change this logic make sure to update the need_vm_lock logic
326 * above to match.
327 */
328 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
329 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
330 intid_mask)) {
331 goto out;
332 }
333
334 /* Increment the count. */
335 target_vcpu->interrupts.enabled_and_pending_count++;
336
337 /*
338 * Only need to update state if there was not already an
339 * interrupt enabled and pending.
340 */
341 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
342 goto out;
343 }
344
Andrew Walbran508e63c2018-12-20 17:02:37 +0000345 if (current->vm->id == HF_PRIMARY_VM_ID) {
346 /*
347 * If the call came from the primary VM, let it know that it
348 * should run or kick the target vCPU.
349 */
350 ret = 1;
351 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100352 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000353 }
354
355out:
356 /* Either way, make it pending. */
357 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
358
359 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000360
361 return ret;
362}
363
364/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100365 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
366 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100367 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100368static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100369{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000370 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 case FFA_MSG_SEND_32:
372 return (struct ffa_value){
373 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000374 .arg1 = (receiver->mailbox.recv_sender << 16) |
375 receiver->id,
376 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000377 default:
378 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000379 dlog_error("Tried to return an invalid message function %#x\n",
380 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000382 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100383}
384
385/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000386 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000387 * value needs to be forced onto the vCPU.
388 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000389static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100390 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000391{
Andrew Scullb06d1752019-02-04 10:15:48 +0000392 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000393 bool ret;
394
Andrew Scullb06d1752019-02-04 10:15:48 +0000395 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000396 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000397 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100398 * The VM lock is not needed in the common case so it must only be taken
399 * when it is going to be needed. This ensures there are no inter-vCPU
400 * dependencies in the common run case meaning the sensitive context
401 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000402 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000403 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000404
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000405 /* The VM needs to be locked to deliver mailbox messages. */
406 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
407 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000408 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000409 sl_lock(&vcpu->vm->lock);
410 sl_lock(&vcpu->lock);
411 }
412
413 /*
414 * If the vCPU is already running somewhere then we can't run it here
415 * simultaneously. While it is actually running then the state should be
416 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
417 * stops running but while Hafnium is in the process of switching back
418 * to the primary there will be a brief period while the state has been
419 * updated but `regs_available` is still false (until
420 * `api_regs_state_saved` is called). We can't start running it again
421 * until this has finished, so count this state as still running for the
422 * purposes of this check.
423 */
424 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
425 /*
426 * vCPU is running on another pCPU.
427 *
428 * It's okay not to return the sleep duration here because the
429 * other physical CPU that is currently running this vCPU will
430 * return the sleep duration if needed.
431 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000433 ret = false;
434 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000435 }
Andrew Scull9726c252019-01-23 13:44:19 +0000436
437 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100438 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000439 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
440 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100441 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000442 }
443 ret = false;
444 goto out;
445 }
446
Andrew Walbran508e63c2018-12-20 17:02:37 +0000447 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100448 case VCPU_STATE_RUNNING:
449 case VCPU_STATE_OFF:
450 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000451 ret = false;
452 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000453
Andrew Sculld6ee1102019-04-05 22:12:42 +0100454 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000455 /*
456 * A pending message allows the vCPU to run so the message can
457 * be delivered directly.
458 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100459 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100460 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100461 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100462 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000463 break;
464 }
465 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100466 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000467 /* Allow virtual interrupts to be delivered. */
468 if (vcpu->interrupts.enabled_and_pending_count > 0) {
469 break;
470 }
471
Andrew Walbran508e63c2018-12-20 17:02:37 +0000472 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000473 uint64_t timer_remaining_ns =
474 arch_timer_remaining_ns(&vcpu->regs);
475
476 /*
477 * The timer expired so allow the interrupt to be
478 * delivered.
479 */
480 if (timer_remaining_ns == 0) {
481 break;
482 }
483
484 /*
485 * The vCPU is not ready to run, return the appropriate
486 * code to the primary which called vcpu_run.
487 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100488 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100489 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490 ? FFA_MSG_WAIT_32
491 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000492 run_ret->arg1 =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100493 ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000494 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000495 }
496
497 ret = false;
498 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000499
Andrew Sculld6ee1102019-04-05 22:12:42 +0100500 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000501 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000502 }
503
Andrew Scullb06d1752019-02-04 10:15:48 +0000504 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000505 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100506 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000507
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000508 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000509 * Mark the registers as unavailable now that we're about to reflect
510 * them onto the real registers. This will also prevent another physical
511 * CPU from trying to read these registers.
512 */
513 vcpu->regs_available = false;
514
515 ret = true;
516
517out:
518 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000519 if (need_vm_lock) {
520 sl_unlock(&vcpu->vm->lock);
521 }
522
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000523 return ret;
524}
525
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
527 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100528{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100529 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100530 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100531 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100532
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000533 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100534 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100536 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100537 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100538
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000539 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100540 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100541 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100542 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100543
Andrew Scull19503262018-09-20 14:48:39 +0100544 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100545 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100546 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100547 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100548 }
549
Fuad Tabbaed294af2019-12-20 10:43:01 +0000550 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100551 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100552 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100553 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100554
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000555 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100556 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000557 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000558 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100559 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000560
Andrew Walbran508e63c2018-12-20 17:02:37 +0000561 /*
562 * Inject timer interrupt if timer has expired. It's safe to access
563 * vcpu->regs here because api_vcpu_prepare_run already made sure that
564 * regs_available was true (and then set it to false) before returning
565 * true.
566 */
567 if (arch_timer_pending(&vcpu->regs)) {
568 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100569 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
570 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000571
572 /*
573 * Set the mask bit so the hardware interrupt doesn't fire
574 * again. Ideally we wouldn't do this because it affects what
575 * the secondary vCPU sees, but if we don't then we end up with
576 * a loop of the interrupt firing each time we try to return to
577 * the secondary vCPU.
578 */
579 arch_timer_mask(&vcpu->regs);
580 }
581
Fuad Tabbaed294af2019-12-20 10:43:01 +0000582 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000583 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000584
Andrew Scull33fecd32019-01-08 14:48:27 +0000585 /*
586 * Set a placeholder return code to the scheduler. This will be
587 * overwritten when the switch back to the primary occurs.
588 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589 ret.func = FFA_INTERRUPT_32;
590 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100591 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000592
Andrew Scull6d2db332018-10-10 15:28:17 +0100593out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100594 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100595}
596
597/**
Andrew Scull81e85092018-12-12 12:56:20 +0000598 * Check that the mode indicates memory that is valid, owned and exclusive.
599 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100600static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000601{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100602 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
603 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000604}
605
606/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100607 * Determines the value to be returned by api_vm_configure and ffa_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000608 * after they've succeeded. If a secondary VM is running and there are waiters,
609 * it also switches back to the primary VM for it to wake waiters up.
610 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100611static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
612 struct vcpu *current,
613 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000614{
615 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000616
617 if (list_empty(&vm->mailbox.waiter_list)) {
618 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100619 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000620 }
621
622 if (vm->id == HF_PRIMARY_VM_ID) {
623 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000625 }
626
627 /*
628 * Switch back to the primary VM, informing it that there are waiters
629 * that need to be notified.
630 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000631 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000633 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000636}
637
638/**
Andrew Sculle1322792019-07-01 17:46:10 +0100639 * Configures the hypervisor's stage-1 view of the send and receive pages. The
640 * stage-1 page tables must be locked so memory cannot be taken by another core
641 * which could result in this transaction being unable to roll back in the case
642 * of an error.
643 */
644static bool api_vm_configure_stage1(struct vm_locked vm_locked,
645 paddr_t pa_send_begin, paddr_t pa_send_end,
646 paddr_t pa_recv_begin, paddr_t pa_recv_end,
647 struct mpool *local_page_pool)
648{
649 bool ret;
650 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
651
652 /* Map the send page as read-only in the hypervisor address space. */
653 vm_locked.vm->mailbox.send =
654 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
655 MM_MODE_R, local_page_pool);
656 if (!vm_locked.vm->mailbox.send) {
657 /* TODO: partial defrag of failed range. */
658 /* Recover any memory consumed in failed mapping. */
659 mm_defrag(mm_stage1_locked, local_page_pool);
660 goto fail;
661 }
662
663 /*
664 * Map the receive page as writable in the hypervisor address space. On
665 * failure, unmap the send page before returning.
666 */
667 vm_locked.vm->mailbox.recv =
668 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
669 MM_MODE_W, local_page_pool);
670 if (!vm_locked.vm->mailbox.recv) {
671 /* TODO: partial defrag of failed range. */
672 /* Recover any memory consumed in failed mapping. */
673 mm_defrag(mm_stage1_locked, local_page_pool);
674 goto fail_undo_send;
675 }
676
677 ret = true;
678 goto out;
679
680 /*
681 * The following mappings will not require more memory than is available
682 * in the local pool.
683 */
684fail_undo_send:
685 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100686 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
687 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100688
689fail:
690 ret = false;
691
692out:
693 mm_unlock_stage1(&mm_stage1_locked);
694
695 return ret;
696}
697
698/**
699 * Configures the send and receive pages in the VM stage-2 and hypervisor
700 * stage-1 page tables. Locking of the page tables combined with a local memory
701 * pool ensures there will always be enough memory to recover from any errors
702 * that arise.
703 */
704static bool api_vm_configure_pages(struct vm_locked vm_locked,
705 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100706 uint32_t orig_send_mode,
707 paddr_t pa_recv_begin, paddr_t pa_recv_end,
708 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100709{
710 bool ret;
711 struct mpool local_page_pool;
712
713 /*
714 * Create a local pool so any freed memory can't be used by another
715 * thread. This is to ensure the original mapping can be restored if any
716 * stage of the process fails.
717 */
718 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
719
720 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000721 if (!vm_identity_map(
722 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100723 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000724 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100725 goto fail;
726 }
727
Andrew Scull3c257452019-11-26 13:32:50 +0000728 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
729 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
730 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100731 /* TODO: partial defrag of failed range. */
732 /* Recover any memory consumed in failed mapping. */
733 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
734 goto fail_undo_send;
735 }
736
737 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
738 pa_recv_begin, pa_recv_end,
739 &local_page_pool)) {
740 goto fail_undo_send_and_recv;
741 }
742
743 ret = true;
744 goto out;
745
746 /*
747 * The following mappings will not require more memory than is available
748 * in the local pool.
749 */
750fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000751 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
752 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100753
754fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000755 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
756 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100757
758fail:
759 ret = false;
760
761out:
762 mpool_fini(&local_page_pool);
763
764 return ret;
765}
766
767/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100768 * Configures the VM to send/receive data through the specified pages. The pages
769 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000770 *
771 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100772 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000773 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100774 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000775 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100776 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000777 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100778 * - FFA_SUCCESS on success if no further action is needed.
779 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000780 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100781 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100782struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
783 uint32_t page_count, struct vcpu *current,
784 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100785{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100786 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100787 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100788 paddr_t pa_send_begin;
789 paddr_t pa_send_end;
790 paddr_t pa_recv_begin;
791 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100792 uint32_t orig_send_mode;
793 uint32_t orig_recv_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100794 struct ffa_value ret;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000795
796 /* Hafnium only supports a fixed size of RX/TX buffers. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
798 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000799 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100800
801 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000802 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
803 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100804 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100805 }
806
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000807 /* Convert to physical addresses. */
808 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000809 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000810
811 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000812 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000813
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100814 /* Fail if the same page is used for the send and receive pages. */
815 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100816 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000817 }
818
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100819 /*
820 * The hypervisor's memory map must be locked for the duration of this
821 * operation to ensure there will be sufficient memory to recover from
822 * any failures.
823 *
824 * TODO: the scope of the can be reduced but will require restructuring
825 * to keep a single unlock point.
826 */
Andrew Sculle1322792019-07-01 17:46:10 +0100827 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000828
829 /* We only allow these to be setup once. */
830 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100831 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000832 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000833 }
834
835 /*
836 * Ensure the pages are valid, owned and exclusive to the VM and that
837 * the VM has the required access to the memory.
838 */
839 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
840 &orig_send_mode) ||
841 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
842 (orig_send_mode & MM_MODE_R) == 0 ||
843 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100844 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000845 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000846 }
847
848 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
849 &orig_recv_mode) ||
850 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
851 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100852 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000853 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000854 }
855
Andrew Sculle1322792019-07-01 17:46:10 +0100856 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
857 orig_send_mode, pa_recv_begin, pa_recv_end,
858 orig_recv_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100859 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000860 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100861 }
862
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000863 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100864 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000865
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100866exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100867 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100868
869 return ret;
870}
871
872/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100873 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
874 * registers the `from` VM to be notified when it becomes available.
875 */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000876static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100877{
878 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
879 to.vm->mailbox.recv == NULL) {
880 /*
881 * Fail if the receiver isn't currently ready to receive data,
882 * setting up for notification if requested.
883 */
884 if (notify) {
885 struct wait_entry *entry =
Andrew Walbranaad8f982019-12-04 10:56:39 +0000886 vm_get_wait_entry(from, to.vm->id);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100887
888 /* Append waiter only if it's not there yet. */
889 if (list_empty(&entry->wait_links)) {
890 list_append(&to.vm->mailbox.waiter_list,
891 &entry->wait_links);
892 }
893 }
894
895 return true;
896 }
897
898 return false;
899}
900
901/**
902 * Notifies the `to` VM about the message currently in its mailbox, possibly
903 * with the help of the primary VM.
904 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100905static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
906 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100907{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
909 struct ffa_value primary_ret = {
910 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000911 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100912 };
913
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100914 /* Messages for the primary VM are delivered directly. */
915 if (to.vm->id == HF_PRIMARY_VM_ID) {
916 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000917 * Only tell the primary VM the size and other details if the
918 * message is for it, to avoid leaking data about messages for
919 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100920 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100922
923 to.vm->mailbox.state = MAILBOX_STATE_READ;
924 *next = api_switch_to_primary(current, primary_ret,
925 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000926 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100927 }
928
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000929 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
930
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000931 /* Messages for the TEE are sent on via the dispatcher. */
932 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100933 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000934
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000935 ret = arch_tee_call(call);
936 /*
937 * After the call to the TEE completes it must have finished
938 * reading its RX buffer, so it is ready for another message.
939 */
940 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000941 /*
942 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100943 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000944 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000945 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000946 }
947
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100948 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +0000949 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100950 *next = api_switch_to_primary(current, primary_ret,
951 VCPU_STATE_READY);
952 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000953
954 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100955}
956
957/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100958 * Copies data from the sender's send buffer to the recipient's receive buffer
959 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000960 *
961 * If the recipient's receive buffer is busy, it can optionally register the
962 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100963 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100964struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
965 ffa_vm_id_t receiver_vm_id, uint32_t size,
966 uint32_t attributes, struct vcpu *current,
967 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100968{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100969 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100970 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +0000971 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +0100972 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 struct ffa_value ret;
974 bool notify =
975 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +0100976
Andrew Walbran70bc8622019-10-07 14:15:58 +0100977 /* Ensure sender VM ID corresponds to the current VM. */
978 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100979 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100980 }
981
982 /* Disallow reflexive requests as this suggests an error in the VM. */
983 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100984 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100985 }
986
987 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 if (size > FFA_MSG_PAYLOAD_MAX) {
989 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100990 }
991
Andrew Walbran0b60c4f2019-12-10 17:05:29 +0000992 /* Ensure the receiver VM exists. */
993 to = vm_find(receiver_vm_id);
994 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100995 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +0000996 }
997
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000998 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +0100999 * Check that the sender has configured its send buffer. If the tx
1000 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1001 * be safely accessed after releasing the lock since the tx mailbox
1002 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001003 */
1004 sl_lock(&from->lock);
1005 from_msg = from->mailbox.send;
1006 sl_unlock(&from->lock);
1007
1008 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001010 }
1011
Andrew Walbran82d6d152019-12-24 15:02:06 +00001012 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001013
Andrew Walbran82d6d152019-12-24 15:02:06 +00001014 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001015 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001016 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001017 }
1018
Andrew Walbran82d6d152019-12-24 15:02:06 +00001019 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001021 to->mailbox.recv_size = size;
1022 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001024 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001025
1026out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001027 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001028
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001029 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001030}
1031
1032/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001033 * Checks whether the vCPU's attempt to block for a message has already been
1034 * interrupted or whether it is allowed to block.
1035 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001037{
1038 bool interrupted;
1039
1040 sl_lock(&current->lock);
1041
1042 /*
1043 * Don't block if there are enabled and pending interrupts, to match
1044 * behaviour of wait_for_interrupt.
1045 */
1046 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1047
1048 sl_unlock(&current->lock);
1049
1050 return interrupted;
1051}
1052
1053/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001054 * Receives a message from the mailbox. If one isn't available, this function
1055 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001056 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001057 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001058 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001059struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1060 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001062 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001063 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001064
Andrew Scullaa039b32018-10-04 15:02:26 +01001065 /*
1066 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001067 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001068 */
Andrew Scull19503262018-09-20 14:48:39 +01001069 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001071 }
1072
1073 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001074
Andrew Scullaa039b32018-10-04 15:02:26 +01001075 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001076 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1077 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001079 goto out;
1080 }
1081
1082 /* No pending message so fail if not allowed to block. */
1083 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001084 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001085 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001086 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001087
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001088 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001089 * From this point onward this call can only be interrupted or a message
1090 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001091 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001092 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001093 return_code = ffa_error(FFA_INTERRUPTED);
1094 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001095 goto out;
1096 }
1097
Fuad Tabbaed294af2019-12-20 10:43:01 +00001098 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001099 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001100 struct ffa_value run_return = {
1101 .func = FFA_MSG_WAIT_32,
1102 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001103 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001104
Andrew Walbranb4816552018-12-05 17:35:42 +00001105 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001106 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001107 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001108out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001109 sl_unlock(&vm->lock);
1110
Jose Marinho3e2442f2019-03-12 13:30:37 +00001111 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001112}
1113
1114/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001115 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1116 * by this function, the caller must have called api_mailbox_send before with
1117 * the notify argument set to true, and this call must have failed because the
1118 * mailbox was not available.
1119 *
1120 * It should be called repeatedly to retrieve a list of VMs.
1121 *
1122 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1123 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001124 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001125int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001126{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001127 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001128 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001129 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001130
1131 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001132 if (list_empty(&vm->mailbox.ready_list)) {
1133 ret = -1;
1134 goto exit;
1135 }
1136
1137 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1138 ready_links);
1139 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001140 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001141
1142exit:
1143 sl_unlock(&vm->lock);
1144 return ret;
1145}
1146
1147/**
1148 * Retrieves the next VM waiting to be notified that the mailbox of the
1149 * specified VM became writable. Only primary VMs are allowed to call this.
1150 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001151 * Returns -1 on failure or if there are no waiters; the VM id of the next
1152 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001153 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001155{
1156 struct vm *vm;
1157 struct vm_locked locked;
1158 struct wait_entry *entry;
1159 struct vm *waiting_vm;
1160
1161 /* Only primary VMs are allowed to call this function. */
1162 if (current->vm->id != HF_PRIMARY_VM_ID) {
1163 return -1;
1164 }
1165
Andrew Walbran42347a92019-05-09 13:59:03 +01001166 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001167 if (vm == NULL) {
1168 return -1;
1169 }
1170
Fuad Tabbaed294af2019-12-20 10:43:01 +00001171 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001172 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001173 entry = api_fetch_waiter(locked);
1174 vm_unlock(&locked);
1175
1176 if (entry == NULL) {
1177 return -1;
1178 }
1179
1180 /* Enqueue notification to waiting VM. */
1181 waiting_vm = entry->waiting_vm;
1182
1183 sl_lock(&waiting_vm->lock);
1184 if (list_empty(&entry->ready_links)) {
1185 list_append(&waiting_vm->mailbox.ready_list,
1186 &entry->ready_links);
1187 }
1188 sl_unlock(&waiting_vm->lock);
1189
1190 return waiting_vm->id;
1191}
1192
1193/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001194 * Releases the caller's mailbox so that a new message can be received. The
1195 * caller must have copied out all data they wish to preserve as new messages
1196 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001197 *
1198 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1200 * - FFA_SUCCESS on success if no further action is needed.
1201 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001202 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001203 * hf_mailbox_waiter_get.
1204 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001205struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001206{
1207 struct vm *vm = current->vm;
1208 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001210
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001211 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001212 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001213 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001214 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001215 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001216 break;
1217
Andrew Sculld6ee1102019-04-05 22:12:42 +01001218 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001219 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001220 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001221 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001222 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001223 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001224
1225 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001226}
Andrew Walbran318f5732018-11-20 16:23:42 +00001227
1228/**
1229 * Enables or disables a given interrupt ID for the calling vCPU.
1230 *
1231 * Returns 0 on success, or -1 if the intid is invalid.
1232 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001233int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001234{
1235 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001236 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001237
Andrew Walbran318f5732018-11-20 16:23:42 +00001238 if (intid >= HF_NUM_INTIDS) {
1239 return -1;
1240 }
1241
1242 sl_lock(&current->lock);
1243 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001244 /*
1245 * If it is pending and was not enabled before, increment the
1246 * count.
1247 */
1248 if (current->interrupts.interrupt_pending[intid_index] &
1249 ~current->interrupts.interrupt_enabled[intid_index] &
1250 intid_mask) {
1251 current->interrupts.enabled_and_pending_count++;
1252 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001253 current->interrupts.interrupt_enabled[intid_index] |=
1254 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001255 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001256 /*
1257 * If it is pending and was enabled before, decrement the count.
1258 */
1259 if (current->interrupts.interrupt_pending[intid_index] &
1260 current->interrupts.interrupt_enabled[intid_index] &
1261 intid_mask) {
1262 current->interrupts.enabled_and_pending_count--;
1263 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001264 current->interrupts.interrupt_enabled[intid_index] &=
1265 ~intid_mask;
1266 }
1267
1268 sl_unlock(&current->lock);
1269 return 0;
1270}
1271
1272/**
1273 * Returns the ID of the next pending interrupt for the calling vCPU, and
1274 * acknowledges it (i.e. marks it as no longer pending). Returns
1275 * HF_INVALID_INTID if there are no pending interrupts.
1276 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001277uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001278{
1279 uint8_t i;
1280 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001281
1282 /*
1283 * Find the first enabled and pending interrupt ID, return it, and
1284 * deactivate it.
1285 */
1286 sl_lock(&current->lock);
1287 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1288 uint32_t enabled_and_pending =
1289 current->interrupts.interrupt_enabled[i] &
1290 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001291
Andrew Walbran318f5732018-11-20 16:23:42 +00001292 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001293 uint8_t bit_index = ctz(enabled_and_pending);
1294 /*
1295 * Mark it as no longer pending and decrement the count.
1296 */
1297 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001298 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001299 current->interrupts.enabled_and_pending_count--;
1300 first_interrupt =
1301 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001302 break;
1303 }
1304 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001305
1306 sl_unlock(&current->lock);
1307 return first_interrupt;
1308}
1309
1310/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001311 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001312 * given VM and vCPU.
1313 */
1314static inline bool is_injection_allowed(uint32_t target_vm_id,
1315 struct vcpu *current)
1316{
1317 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001318
Andrew Walbran318f5732018-11-20 16:23:42 +00001319 /*
1320 * The primary VM is allowed to inject interrupts into any VM. Secondary
1321 * VMs are only allowed to inject interrupts into their own vCPUs.
1322 */
1323 return current_vm_id == HF_PRIMARY_VM_ID ||
1324 current_vm_id == target_vm_id;
1325}
1326
1327/**
1328 * Injects a virtual interrupt of the given ID into the given target vCPU.
1329 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1330 * when the vCPU is next run, which is up to the scheduler.
1331 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001332 * Returns:
1333 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1334 * ID is invalid, or the current VM is not allowed to inject interrupts to
1335 * the target VM.
1336 * - 0 on success if no further action is needed.
1337 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1338 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001339 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1341 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001342 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001343{
Andrew Walbran318f5732018-11-20 16:23:42 +00001344 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001345 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001346
1347 if (intid >= HF_NUM_INTIDS) {
1348 return -1;
1349 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001350
Andrew Walbran318f5732018-11-20 16:23:42 +00001351 if (target_vm == NULL) {
1352 return -1;
1353 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001354
Andrew Walbran318f5732018-11-20 16:23:42 +00001355 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001356 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001357 return -1;
1358 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001359
Andrew Walbran318f5732018-11-20 16:23:42 +00001360 if (!is_injection_allowed(target_vm_id, current)) {
1361 return -1;
1362 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001363
Andrew Walbrane1310df2019-04-29 17:28:28 +01001364 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001365
Andrew Walbran17eebf92020-02-05 16:35:49 +00001366 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1367 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1368 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001369 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001370}
Andrew Scull6386f252018-12-06 13:29:10 +00001371
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001372/** Returns the version of the implemented FF-A specification. */
1373struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001374{
1375 /*
1376 * Ensure that both major and minor revision representation occupies at
1377 * most 15 bits.
1378 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001379 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001380 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001382 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001383 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001384 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001385 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001386 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001387
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 return (struct ffa_value){
1389 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1390 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001391}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001392
1393int64_t api_debug_log(char c, struct vcpu *current)
1394{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001395 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001396 struct vm *vm = current->vm;
1397 struct vm_locked vm_locked = vm_lock(vm);
1398
Andrew Sculld54e1be2019-08-20 11:09:42 +01001399 if (c == '\n' || c == '\0') {
1400 flush = true;
1401 } else {
1402 vm->log_buffer[vm->log_buffer_length++] = c;
1403 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1404 }
1405
1406 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001407 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1408 vm->log_buffer_length);
1409 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001410 }
1411
1412 vm_unlock(&vm_locked);
1413
1414 return 0;
1415}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001416
1417/**
1418 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001419 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001420 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001422{
1423 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 case FFA_ERROR_32:
1425 case FFA_SUCCESS_32:
1426 case FFA_INTERRUPT_32:
1427 case FFA_VERSION_32:
1428 case FFA_FEATURES_32:
1429 case FFA_RX_RELEASE_32:
1430 case FFA_RXTX_MAP_64:
1431 case FFA_ID_GET_32:
1432 case FFA_MSG_POLL_32:
1433 case FFA_MSG_WAIT_32:
1434 case FFA_YIELD_32:
1435 case FFA_RUN_32:
1436 case FFA_MSG_SEND_32:
1437 case FFA_MEM_DONATE_32:
1438 case FFA_MEM_LEND_32:
1439 case FFA_MEM_SHARE_32:
1440 case FFA_MEM_RETRIEVE_REQ_32:
1441 case FFA_MEM_RETRIEVE_RESP_32:
1442 case FFA_MEM_RELINQUISH_32:
1443 case FFA_MEM_RECLAIM_32:
1444 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001445 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001447 }
1448}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001449
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1451 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001452 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001453{
1454 struct vm *from = current->vm;
1455 struct vm *to;
1456 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001457 struct ffa_memory_region *memory_region;
1458 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001459
1460 if (ipa_addr(address) != 0 || page_count != 0) {
1461 /*
1462 * Hafnium only supports passing the descriptor in the TX
1463 * mailbox.
1464 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001465 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001466 }
1467
Andrew Walbranca808b12020-05-15 17:22:28 +01001468 if (fragment_length > length) {
1469 dlog_verbose(
1470 "Fragment length %d greater than total length %d.\n",
1471 fragment_length, length);
1472 return ffa_error(FFA_INVALID_PARAMETERS);
1473 }
1474 if (fragment_length < sizeof(struct ffa_memory_region) +
1475 sizeof(struct ffa_memory_access)) {
1476 dlog_verbose(
1477 "Initial fragment length %d smaller than header size "
1478 "%d.\n",
1479 fragment_length,
1480 sizeof(struct ffa_memory_region) +
1481 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001483 }
1484
1485 /*
1486 * Check that the sender has configured its send buffer. If the TX
1487 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1488 * be safely accessed after releasing the lock since the TX mailbox
1489 * address can only be configured once.
1490 */
1491 sl_lock(&from->lock);
1492 from_msg = from->mailbox.send;
1493 sl_unlock(&from->lock);
1494
1495 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001496 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001497 }
1498
1499 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001500 * Copy the memory region descriptor to a fresh page from the memory
1501 * pool. This prevents the sender from changing it underneath us, and
1502 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001503 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001504 if (fragment_length > HF_MAILBOX_SIZE ||
1505 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001506 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001507 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001508 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001509 if (memory_region == NULL) {
1510 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001511 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001512 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001513 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001514
1515 /* The sender must match the caller. */
1516 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001517 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001519 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001520 }
1521
Andrew Walbrana65a1322020-04-06 19:32:32 +01001522 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001523 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001524 dlog_verbose(
1525 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001526 "endpoint memory access descriptors, expected 1).\n",
1527 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001528 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001529 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001530 }
1531
1532 /*
1533 * Ensure that the receiver VM exists and isn't the same as the sender.
1534 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001536 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001537 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001539 goto out;
1540 }
1541
1542 if (to->id == HF_TEE_VM_ID) {
1543 /*
1544 * The 'to' VM lock is only needed in the case that it is the
1545 * TEE VM.
1546 */
1547 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1548
1549 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001551 goto out_unlock;
1552 }
1553
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001554 ret = ffa_memory_tee_send(
1555 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1556 length, fragment_length, share_func, &api_page_pool);
1557 /*
1558 * ffa_tee_memory_send takes ownership of the memory_region, so
1559 * make sure we don't free it.
1560 */
1561 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562
1563 out_unlock:
1564 vm_unlock(&vm_to_from_lock.vm1);
1565 vm_unlock(&vm_to_from_lock.vm2);
1566 } else {
1567 struct vm_locked from_locked = vm_lock(from);
1568
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001569 ret = ffa_memory_send(from_locked, memory_region, length,
1570 fragment_length, share_func,
1571 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001572 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001573 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001574 * make sure we don't free it.
1575 */
1576 memory_region = NULL;
1577
1578 vm_unlock(&from_locked);
1579 }
1580
1581out:
1582 if (memory_region != NULL) {
1583 mpool_free(&api_page_pool, memory_region);
1584 }
1585
1586 return ret;
1587}
1588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001589struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1590 uint32_t fragment_length,
1591 ipaddr_t address, uint32_t page_count,
1592 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001593{
1594 struct vm *to = current->vm;
1595 struct vm_locked to_locked;
1596 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001597 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001598 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001600
1601 if (ipa_addr(address) != 0 || page_count != 0) {
1602 /*
1603 * Hafnium only supports passing the descriptor in the TX
1604 * mailbox.
1605 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001607 }
1608
Andrew Walbrana65a1322020-04-06 19:32:32 +01001609 if (fragment_length != length) {
1610 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001611 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001612 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001613
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001614 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001616 message_buffer_size = cpu_get_buffer_size(current->cpu);
1617 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1618 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001619 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001620 }
1621
1622 to_locked = vm_lock(to);
1623 to_msg = to->mailbox.send;
1624
1625 if (to_msg == NULL) {
1626 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001627 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001628 goto out;
1629 }
1630
1631 /*
1632 * Copy the retrieve request descriptor to an internal buffer, so that
1633 * the caller can't change it underneath us.
1634 */
1635 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1636
1637 if (msg_receiver_busy(to_locked, NULL, false)) {
1638 /*
1639 * Can't retrieve memory information if the mailbox is not
1640 * available.
1641 */
1642 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001643 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001644 goto out;
1645 }
1646
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1648 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001649
1650out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001651 vm_unlock(&to_locked);
1652 return ret;
1653}
1654
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001655struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656{
1657 struct vm *from = current->vm;
1658 struct vm_locked from_locked;
1659 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001660 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001661 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001662 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001663 uint32_t length;
1664
1665 from_locked = vm_lock(from);
1666 from_msg = from->mailbox.send;
1667
1668 if (from_msg == NULL) {
1669 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001671 goto out;
1672 }
1673
1674 /*
1675 * Calculate length from relinquish descriptor before copying. We will
1676 * check again later to make sure it hasn't changed.
1677 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001678 length = sizeof(struct ffa_mem_relinquish) +
1679 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
1680 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001681 /*
1682 * Copy the relinquish descriptor to an internal buffer, so that the
1683 * caller can't change it underneath us.
1684 */
1685 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001686 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687 message_buffer_size = cpu_get_buffer_size(current->cpu);
1688 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1689 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001690 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001691 goto out;
1692 }
1693 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
1694
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001695 if (sizeof(struct ffa_mem_relinquish) +
1696 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001697 length) {
1698 dlog_verbose(
1699 "Endpoint count changed while copying to internal "
1700 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001701 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001702 goto out;
1703 }
1704
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001705 ret = ffa_memory_relinquish(from_locked, relinquish_request,
1706 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707
1708out:
1709 vm_unlock(&from_locked);
1710 return ret;
1711}
1712
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001713struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
1714 ffa_memory_region_flags_t flags,
1715 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001716{
1717 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001718 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001719
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001720 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1721 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001722 struct vm_locked to_locked = vm_lock(to);
1723
Andrew Walbranca808b12020-05-15 17:22:28 +01001724 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001725 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001726
Andrew Walbran290b0c92020-02-03 16:37:14 +00001727 vm_unlock(&to_locked);
1728 } else {
1729 struct vm *from = vm_find(HF_TEE_VM_ID);
1730 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1731
Andrew Walbranca808b12020-05-15 17:22:28 +01001732 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
1733 vm_to_from_lock.vm2, handle, flags,
1734 &api_page_pool);
1735
1736 vm_unlock(&vm_to_from_lock.vm1);
1737 vm_unlock(&vm_to_from_lock.vm2);
1738 }
1739
1740 return ret;
1741}
1742
1743struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
1744 uint32_t fragment_offset,
1745 ffa_vm_id_t sender_vm_id,
1746 struct vcpu *current)
1747{
1748 struct vm *to = current->vm;
1749 struct vm_locked to_locked;
1750 struct ffa_value ret;
1751
1752 /* Sender ID MBZ at virtual instance. */
1753 if (sender_vm_id != 0) {
1754 return ffa_error(FFA_INVALID_PARAMETERS);
1755 }
1756
1757 to_locked = vm_lock(to);
1758
1759 if (msg_receiver_busy(to_locked, NULL, false)) {
1760 /*
1761 * Can't retrieve memory information if the mailbox is not
1762 * available.
1763 */
1764 dlog_verbose("RX buffer not ready.\n");
1765 ret = ffa_error(FFA_BUSY);
1766 goto out;
1767 }
1768
1769 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
1770 &api_page_pool);
1771
1772out:
1773 vm_unlock(&to_locked);
1774 return ret;
1775}
1776
1777struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
1778 uint32_t fragment_length,
1779 ffa_vm_id_t sender_vm_id,
1780 struct vcpu *current)
1781{
1782 struct vm *from = current->vm;
1783 const void *from_msg;
1784 void *fragment_copy;
1785 struct ffa_value ret;
1786
1787 /* Sender ID MBZ at virtual instance. */
1788 if (sender_vm_id != 0) {
1789 return ffa_error(FFA_INVALID_PARAMETERS);
1790 }
1791
1792 /*
1793 * Check that the sender has configured its send buffer. If the TX
1794 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1795 * be safely accessed after releasing the lock since the TX mailbox
1796 * address can only be configured once.
1797 */
1798 sl_lock(&from->lock);
1799 from_msg = from->mailbox.send;
1800 sl_unlock(&from->lock);
1801
1802 if (from_msg == NULL) {
1803 return ffa_error(FFA_INVALID_PARAMETERS);
1804 }
1805
1806 /*
1807 * Copy the fragment to a fresh page from the memory pool. This prevents
1808 * the sender from changing it underneath us, and also lets us keep it
1809 * around in the share state table if needed.
1810 */
1811 if (fragment_length > HF_MAILBOX_SIZE ||
1812 fragment_length > MM_PPOOL_ENTRY_SIZE) {
1813 dlog_verbose(
1814 "Fragment length %d larger than mailbox size %d.\n",
1815 fragment_length, HF_MAILBOX_SIZE);
1816 return ffa_error(FFA_INVALID_PARAMETERS);
1817 }
1818 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
1819 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1820 0) {
1821 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
1822 return ffa_error(FFA_INVALID_PARAMETERS);
1823 }
1824 fragment_copy = mpool_alloc(&api_page_pool);
1825 if (fragment_copy == NULL) {
1826 dlog_verbose("Failed to allocate fragment copy.\n");
1827 return ffa_error(FFA_NO_MEMORY);
1828 }
1829 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
1830
1831 /*
1832 * Hafnium doesn't support fragmentation of memory retrieve requests
1833 * (because it doesn't support caller-specified mappings, so a request
1834 * will never be larger than a single page), so this must be part of a
1835 * memory send (i.e. donate, lend or share) request.
1836 *
1837 * We can tell from the handle whether the memory transaction is for the
1838 * TEE or not.
1839 */
1840 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1841 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
1842 struct vm_locked from_locked = vm_lock(from);
1843
1844 ret = ffa_memory_send_continue(from_locked, fragment_copy,
1845 fragment_length, handle,
1846 &api_page_pool);
1847 /*
1848 * `ffa_memory_send_continue` takes ownership of the
1849 * fragment_copy, so we don't need to free it here.
1850 */
1851 vm_unlock(&from_locked);
1852 } else {
1853 struct vm *to = vm_find(HF_TEE_VM_ID);
1854 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1855
1856 /*
1857 * The TEE RX buffer state is checked in
1858 * `ffa_memory_tee_send_continue` rather than here, as we need
1859 * to return `FFA_MEM_FRAG_RX` with the current offset rather
1860 * than FFA_ERROR FFA_BUSY in case it is busy.
1861 */
1862
1863 ret = ffa_memory_tee_send_continue(
1864 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
1865 fragment_length, handle, &api_page_pool);
1866 /*
1867 * `ffa_memory_tee_send_continue` takes ownership of the
1868 * fragment_copy, so we don't need to free it here.
1869 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00001870
1871 vm_unlock(&vm_to_from_lock.vm1);
1872 vm_unlock(&vm_to_from_lock.vm2);
1873 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001874
1875 return ret;
1876}