blob: 46d8a881722ab95e13c6d5d17e7d500291635e98 [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"
Olivier Deprez96a2a262020-06-11 17:21:38 +020012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000014#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020015#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000016
Andrew Scull877ae4b2019-07-02 12:52:33 +010017#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
20#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000021#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010022#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010024#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010025#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/vm.h"
27
Andrew Scullf35a5c92018-08-07 18:09:46 +010028#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010029#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010030
Fuad Tabbae4efcc32020-07-16 15:37:27 +010031static_assert(sizeof(struct ffa_partition_info) == 8,
32 "Partition information descriptor size doesn't match the one in "
33 "the FF-A 1.0 EAC specification, Table 82.");
34
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000035/*
36 * To eliminate the risk of deadlocks, we define a partial order for the
37 * acquisition of locks held concurrently by the same physical CPU. Our current
38 * ordering requirements are as follows:
39 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010040 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000041 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010042 * Locks of the same kind require the lock of lowest address to be locked first,
43 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000044 */
45
Andrew Scullaa039b32018-10-04 15:02:26 +010046static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010047 "Currently, a page is mapped for the send and receive buffers so "
48 "the maximum request is the size of a page.");
49
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000050static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
51 "The page pool entry size must be at least as big as the mailbox "
52 "size, so that memory region descriptors can be copied from the "
53 "mailbox for memory sharing.");
54
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000055static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000056
57/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000058 * Initialises the API page pool by taking ownership of the contents of the
59 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000060 */
61void api_init(struct mpool *ppool)
62{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000063 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000064}
65
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010066/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000067 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010068 *
69 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010070 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010071 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010072static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010073 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000074 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010075{
Andrew Walbran42347a92019-05-09 13:59:03 +010076 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010077 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010078
Andrew Walbran508e63c2018-12-20 17:02:37 +000079 /*
80 * If the secondary is blocked but has a timer running, sleep until the
81 * timer fires rather than indefinitely.
82 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010083 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010084 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
85 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010086 if (arch_timer_enabled_current()) {
87 uint64_t remaining_ns =
88 arch_timer_remaining_ns_current();
89
90 if (remaining_ns == 0) {
91 /*
92 * Timer is pending, so the current vCPU should
93 * be run again right away.
94 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010095 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010096 /*
97 * primary_ret.arg1 should already be set to the
98 * current VM ID and vCPU ID.
99 */
100 primary_ret.arg2 = 0;
101 } else {
102 primary_ret.arg2 = remaining_ns;
103 }
104 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100105 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100106 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000107 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100108 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000109
110 default:
111 /* Do nothing. */
112 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000113 }
114
Andrew Walbrana0e30b02020-10-22 15:55:45 +0100115 /* Set the return value for the primary VM's call to FFA_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100116 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100117
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000118 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000119 sl_lock(&current->lock);
120 current->state = secondary_state;
121 sl_unlock(&current->lock);
122
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100123 return next;
124}
125
126/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200127 * Choose next vCPU to run to be the counterpart vCPU in the other
128 * world (run the normal world if currently running in the secure
129 * world). Set current vCPU state to the given vcpu_state parameter.
130 * Set FF-A return values to the target vCPU in the other world.
131 *
132 * Called in context of a direct message response from a secure
133 * partition to a VM.
134 */
135static struct vcpu *api_switch_to_other_world(struct vcpu *current,
136 struct ffa_value other_world_ret,
137 enum vcpu_state vcpu_state)
138{
139 struct vcpu *next = vcpu_get_other_world_counterpart(current);
140
141 CHECK(next != NULL);
142
143 /* Set the return value for the other world's VM. */
144 arch_regs_set_retval(&next->regs, other_world_ret);
145
146 /* Set the current vCPU state. */
147 sl_lock(&current->lock);
148 current->state = vcpu_state;
149 sl_unlock(&current->lock);
150
151 return next;
152}
153
154/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100155 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
156 * registers the `from` VM to be notified when it becomes available.
157 */
158static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
159{
160 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
161 to.vm->mailbox.recv == NULL) {
162 /*
163 * Fail if the receiver isn't currently ready to receive data,
164 * setting up for notification if requested.
165 */
166 if (notify) {
167 struct wait_entry *entry =
168 vm_get_wait_entry(from, to.vm->id);
169
170 /* Append waiter only if it's not there yet. */
171 if (list_empty(&entry->wait_links)) {
172 list_append(&to.vm->mailbox.waiter_list,
173 &entry->wait_links);
174 }
175 }
176
177 return true;
178 }
179
180 return false;
181}
182
183/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000184 * Returns true if the given vCPU is executing in context of an
185 * FFA_MSG_SEND_DIRECT_REQ invocation.
186 */
187static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
188{
189 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
190}
191
192/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000193 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000194 */
195struct vcpu *api_preempt(struct vcpu *current)
196{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100197 struct ffa_value ret = {
198 .func = FFA_INTERRUPT_32,
199 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000200 };
201
Andrew Sculld6ee1102019-04-05 22:12:42 +0100202 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000203}
204
205/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000206 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000207 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100208 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100209struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100210{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100211 struct ffa_value ret = {
212 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
213 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100214 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000215
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000216 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100217 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100218}
219
220/**
Andrew Walbran33645652019-04-15 12:29:31 +0100221 * Puts the current vCPU in off mode, and returns to the primary VM.
222 */
223struct vcpu *api_vcpu_off(struct vcpu *current)
224{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100225 struct ffa_value ret = {
226 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
227 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100228 };
229
230 /*
231 * Disable the timer, so the scheduler doesn't get told to call back
232 * based on it.
233 */
234 arch_timer_disable_current();
235
236 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
237}
238
239/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000240 * Returns to the primary VM to allow this CPU to be used for other tasks as the
241 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100242 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000243 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000244struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000245{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000246 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
247 struct vcpu_locked current_locked;
248 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000249
250 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000251 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000252 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000253 }
254
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000255 current_locked = vcpu_lock(current);
256 is_direct_request_ongoing =
257 is_ffa_direct_msg_request_ongoing(current_locked);
258 vcpu_unlock(&current_locked);
259
260 if (is_direct_request_ongoing) {
261 return ffa_error(FFA_DENIED);
262 }
263
264 *next = api_switch_to_primary(
265 current,
266 (struct ffa_value){.func = FFA_YIELD_32,
267 .arg1 = ffa_vm_vcpu(current->vm->id,
268 vcpu_index(current))},
269 VCPU_STATE_READY);
270
271 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000272}
273
274/**
Andrew Walbran33645652019-04-15 12:29:31 +0100275 * Switches to the primary so that it can switch to the target, or kick it if it
276 * is already running on a different physical CPU.
277 */
278struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
279{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100280 struct ffa_value ret = {
281 .func = HF_FFA_RUN_WAKE_UP,
282 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
283 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100284 };
285 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
286}
287
288/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000289 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000290 */
291struct vcpu *api_abort(struct vcpu *current)
292{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100293 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000294
Andrew Walbran17eebf92020-02-05 16:35:49 +0000295 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
296 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000297
298 if (current->vm->id == HF_PRIMARY_VM_ID) {
299 /* TODO: what to do when the primary aborts? */
300 for (;;) {
301 /* Do nothing. */
302 }
303 }
304
305 atomic_store_explicit(&current->vm->aborting, true,
306 memory_order_relaxed);
307
308 /* TODO: free resources once all vCPUs abort. */
309
Andrew Sculld6ee1102019-04-05 22:12:42 +0100310 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000311}
312
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100313struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
314 const struct ffa_uuid *uuid)
315{
316 struct vm *current_vm = current->vm;
317 struct vm_locked current_vm_locked;
318 ffa_vm_count_t vm_count = 0;
319 bool uuid_is_null = ffa_uuid_is_null(uuid);
320 struct ffa_value ret;
321 uint32_t size;
322 struct ffa_partition_info partitions[MAX_VMS];
323
324 /*
325 * Iterate through the VMs to find the ones with a matching UUID.
326 * A Null UUID retrieves information for all VMs.
327 */
328 for (uint16_t index = 0; index < vm_get_count(); ++index) {
329 const struct vm *vm = vm_find_index(index);
330
331 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
332 partitions[vm_count].vm_id = vm->id;
333 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100334 partitions[vm_count].properties =
Olivier Deprez764fd2e2020-07-29 15:14:09 +0200335 arch_vm_partition_properties(vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100336
337 ++vm_count;
338 }
339 }
340
341 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
342 if (vm_count == 0) {
343 return ffa_error(FFA_INVALID_PARAMETERS);
344 }
345
346 size = vm_count * sizeof(partitions[0]);
347 if (size > FFA_MSG_PAYLOAD_MAX) {
348 dlog_error(
349 "Partition information does not fit in the VM's RX "
350 "buffer.\n");
351 return ffa_error(FFA_NO_MEMORY);
352 }
353
354 /*
355 * Partition information is returned in the VM's RX buffer, which is why
356 * the lock is needed.
357 */
358 current_vm_locked = vm_lock(current_vm);
359
360 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
361 /*
362 * Can't retrieve memory information if the mailbox is not
363 * available.
364 */
365 dlog_verbose("RX buffer not ready.\n");
366 ret = ffa_error(FFA_BUSY);
367 goto out_unlock;
368 }
369
370 /* Populate the VM's RX buffer with the partition information. */
371 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
372 size);
373 current_vm->mailbox.recv_size = size;
374 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
375 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
376 current_vm->mailbox.state = MAILBOX_STATE_READ;
377
378 /* Return the count of partition information descriptors in w2. */
379 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
380
381out_unlock:
382 vm_unlock(&current_vm_locked);
383
384 return ret;
385}
386
Andrew Scull9726c252019-01-23 13:44:19 +0000387/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000388 * Returns the ID of the VM.
389 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100390struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000391{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100392 return (struct ffa_value){.func = FFA_SUCCESS_32,
393 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000394}
395
396/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000397 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000398 * function to indicate that register state for the given vCPU has been saved
399 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000400 */
401void api_regs_state_saved(struct vcpu *vcpu)
402{
403 sl_lock(&vcpu->lock);
404 vcpu->regs_available = true;
405 sl_unlock(&vcpu->lock);
406}
407
408/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000409 * Retrieves the next waiter and removes it from the wait list if the VM's
410 * mailbox is in a writable state.
411 */
412static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
413{
414 struct wait_entry *entry;
415 struct vm *vm = locked_vm.vm;
416
Andrew Sculld6ee1102019-04-05 22:12:42 +0100417 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000418 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
419 /* The mailbox is not writable or there are no waiters. */
420 return NULL;
421 }
422
423 /* Remove waiter from the wait list. */
424 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
425 wait_links);
426 list_remove(&entry->wait_links);
427 return entry;
428}
429
430/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000431 * Assuming that the arguments have already been checked by the caller, injects
432 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
433 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
434 * is next run, which is up to the scheduler.
435 *
436 * Returns:
437 * - 0 on success if no further action is needed.
438 * - 1 if it was called by the primary VM and the primary VM now needs to wake
439 * up or kick the target vCPU.
440 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200441static int64_t internal_interrupt_inject_locked(
442 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
443 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000444{
445 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100446 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000447 int64_t ret = 0;
448
Andrew Walbran508e63c2018-12-20 17:02:37 +0000449 /*
450 * We only need to change state and (maybe) trigger a virtual IRQ if it
451 * is enabled and was not previously pending. Otherwise we can skip
452 * everything except setting the pending bit.
453 *
454 * If you change this logic make sure to update the need_vm_lock logic
455 * above to match.
456 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200457 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
458 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000459 intid_mask)) {
460 goto out;
461 }
462
463 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200464 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000465
466 /*
467 * Only need to update state if there was not already an
468 * interrupt enabled and pending.
469 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200470 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000471 goto out;
472 }
473
Andrew Walbran508e63c2018-12-20 17:02:37 +0000474 if (current->vm->id == HF_PRIMARY_VM_ID) {
475 /*
476 * If the call came from the primary VM, let it know that it
477 * should run or kick the target vCPU.
478 */
479 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200480 } else if (current != target_locked.vcpu && next != NULL) {
481 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000482 }
483
484out:
485 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200486 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
487 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000488
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200489 return ret;
490}
491
492/* Wrapper to internal_interrupt_inject with locking of target vCPU */
493static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
494 uint32_t intid, struct vcpu *current,
495 struct vcpu **next)
496{
497 int64_t ret;
498 struct vcpu_locked target_locked;
499
500 target_locked = vcpu_lock(target_vcpu);
501 ret = internal_interrupt_inject_locked(target_locked, intid, current,
502 next);
503 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000504
505 return ret;
506}
507
508/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100509 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
510 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100511 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100513{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000514 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 case FFA_MSG_SEND_32:
516 return (struct ffa_value){
517 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000518 .arg1 = (receiver->mailbox.recv_sender << 16) |
519 receiver->id,
520 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000521 default:
522 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000523 dlog_error("Tried to return an invalid message function %#x\n",
524 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000526 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100527}
528
529/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000530 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000531 * value needs to be forced onto the vCPU.
532 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000533static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100534 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000535{
Andrew Scullb06d1752019-02-04 10:15:48 +0000536 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000537 bool ret;
538
Andrew Scullb06d1752019-02-04 10:15:48 +0000539 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000540 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000541 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100542 * The VM lock is not needed in the common case so it must only be taken
543 * when it is going to be needed. This ensures there are no inter-vCPU
544 * dependencies in the common run case meaning the sensitive context
545 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000546 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000547 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000548
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000549 /* The VM needs to be locked to deliver mailbox messages. */
550 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
551 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000552 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000553 sl_lock(&vcpu->vm->lock);
554 sl_lock(&vcpu->lock);
555 }
556
557 /*
558 * If the vCPU is already running somewhere then we can't run it here
559 * simultaneously. While it is actually running then the state should be
560 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
561 * stops running but while Hafnium is in the process of switching back
562 * to the primary there will be a brief period while the state has been
563 * updated but `regs_available` is still false (until
564 * `api_regs_state_saved` is called). We can't start running it again
565 * until this has finished, so count this state as still running for the
566 * purposes of this check.
567 */
568 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
569 /*
570 * vCPU is running on another pCPU.
571 *
572 * It's okay not to return the sleep duration here because the
573 * other physical CPU that is currently running this vCPU will
574 * return the sleep duration if needed.
575 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000577 ret = false;
578 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000579 }
Andrew Scull9726c252019-01-23 13:44:19 +0000580
581 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100582 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000583 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
584 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100585 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000586 }
587 ret = false;
588 goto out;
589 }
590
Andrew Walbran508e63c2018-12-20 17:02:37 +0000591 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100592 case VCPU_STATE_RUNNING:
593 case VCPU_STATE_OFF:
594 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000595 ret = false;
596 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000597
Andrew Sculld6ee1102019-04-05 22:12:42 +0100598 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000599 /*
600 * A pending message allows the vCPU to run so the message can
601 * be delivered directly.
602 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100603 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100604 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100606 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000607 break;
608 }
609 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100610 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000611 /* Allow virtual interrupts to be delivered. */
612 if (vcpu->interrupts.enabled_and_pending_count > 0) {
613 break;
614 }
615
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100616 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
617
Andrew Walbran508e63c2018-12-20 17:02:37 +0000618 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100619 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000620 arch_timer_remaining_ns(&vcpu->regs);
621
622 /*
623 * The timer expired so allow the interrupt to be
624 * delivered.
625 */
626 if (timer_remaining_ns == 0) {
627 break;
628 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100629 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000630
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100631 /*
632 * The vCPU is not ready to run, return the appropriate code to
633 * the primary which called vcpu_run.
634 */
635 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100636 ? FFA_MSG_WAIT_32
637 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100638 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
639 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000640
641 ret = false;
642 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000643
Andrew Sculld6ee1102019-04-05 22:12:42 +0100644 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000645 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000646 }
647
Andrew Scullb06d1752019-02-04 10:15:48 +0000648 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000649 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100650 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000651
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000652 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000653 * Mark the registers as unavailable now that we're about to reflect
654 * them onto the real registers. This will also prevent another physical
655 * CPU from trying to read these registers.
656 */
657 vcpu->regs_available = false;
658
659 ret = true;
660
661out:
662 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000663 if (need_vm_lock) {
664 sl_unlock(&vcpu->vm->lock);
665 }
666
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000667 return ret;
668}
669
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100670struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
671 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100672{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100673 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100674 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100675 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100676
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000677 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100678 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100679 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100680 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100681 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100682
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000683 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100684 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100685 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100686 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100687
Andrew Scull19503262018-09-20 14:48:39 +0100688 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100689 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100690 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100691 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100692 }
693
Fuad Tabbaed294af2019-12-20 10:43:01 +0000694 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100695 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100696 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100697 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100698
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000699 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100700 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000701 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000702 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100703 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000704
Andrew Walbran508e63c2018-12-20 17:02:37 +0000705 /*
706 * Inject timer interrupt if timer has expired. It's safe to access
707 * vcpu->regs here because api_vcpu_prepare_run already made sure that
708 * regs_available was true (and then set it to false) before returning
709 * true.
710 */
711 if (arch_timer_pending(&vcpu->regs)) {
712 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100713 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
714 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000715
716 /*
717 * Set the mask bit so the hardware interrupt doesn't fire
718 * again. Ideally we wouldn't do this because it affects what
719 * the secondary vCPU sees, but if we don't then we end up with
720 * a loop of the interrupt firing each time we try to return to
721 * the secondary vCPU.
722 */
723 arch_timer_mask(&vcpu->regs);
724 }
725
Fuad Tabbaed294af2019-12-20 10:43:01 +0000726 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000727 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000728
Andrew Scull33fecd32019-01-08 14:48:27 +0000729 /*
730 * Set a placeholder return code to the scheduler. This will be
731 * overwritten when the switch back to the primary occurs.
732 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100733 ret.func = FFA_INTERRUPT_32;
734 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100735 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000736
Andrew Scull6d2db332018-10-10 15:28:17 +0100737out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100738 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100739}
740
741/**
Andrew Scull81e85092018-12-12 12:56:20 +0000742 * Check that the mode indicates memory that is valid, owned and exclusive.
743 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100744static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000745{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100746 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
747 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000748}
749
750/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100751 * Determines the value to be returned by api_ffa_rxtx_map and
752 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
753 * there are waiters, it also switches back to the primary VM for it to wake
754 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000755 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100756static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
757 struct vcpu *current,
758 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000759{
760 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000761
762 if (list_empty(&vm->mailbox.waiter_list)) {
763 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100764 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000765 }
766
767 if (vm->id == HF_PRIMARY_VM_ID) {
768 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100769 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000770 }
771
772 /*
773 * Switch back to the primary VM, informing it that there are waiters
774 * that need to be notified.
775 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000776 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100777 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000778 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000779
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100780 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000781}
782
783/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100784 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100785 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100786static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
787 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100788 paddr_t pa_send_begin, paddr_t pa_send_end,
789 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200790 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100791 struct mpool *local_page_pool)
792{
793 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100794
795 /* Map the send page as read-only in the hypervisor address space. */
796 vm_locked.vm->mailbox.send =
797 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200798 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100799 if (!vm_locked.vm->mailbox.send) {
800 /* TODO: partial defrag of failed range. */
801 /* Recover any memory consumed in failed mapping. */
802 mm_defrag(mm_stage1_locked, local_page_pool);
803 goto fail;
804 }
805
806 /*
807 * Map the receive page as writable in the hypervisor address space. On
808 * failure, unmap the send page before returning.
809 */
810 vm_locked.vm->mailbox.recv =
811 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200812 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100813 if (!vm_locked.vm->mailbox.recv) {
814 /* TODO: partial defrag of failed range. */
815 /* Recover any memory consumed in failed mapping. */
816 mm_defrag(mm_stage1_locked, local_page_pool);
817 goto fail_undo_send;
818 }
819
820 ret = true;
821 goto out;
822
823 /*
824 * The following mappings will not require more memory than is available
825 * in the local pool.
826 */
827fail_undo_send:
828 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100829 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
830 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100831
832fail:
833 ret = false;
834
835out:
Andrew Sculle1322792019-07-01 17:46:10 +0100836 return ret;
837}
838
839/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100840 * Sanity checks and configures the send and receive pages in the VM stage-2
841 * and hypervisor stage-1 page tables.
842 *
843 * Returns:
844 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
845 * aligned or are the same.
846 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
847 * due to insuffient page table memory.
848 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
849 * the caller.
850 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100851 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100852
853struct ffa_value api_vm_configure_pages(
854 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
855 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
856 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100857{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100858 struct ffa_value ret;
859 paddr_t pa_send_begin;
860 paddr_t pa_send_end;
861 paddr_t pa_recv_begin;
862 paddr_t pa_recv_end;
863 uint32_t orig_send_mode;
864 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200865 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100866
867 /* We only allow these to be setup once. */
868 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
869 ret = ffa_error(FFA_DENIED);
870 goto out;
871 }
872
873 /* Hafnium only supports a fixed size of RX/TX buffers. */
874 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
875 ret = ffa_error(FFA_INVALID_PARAMETERS);
876 goto out;
877 }
878
879 /* Fail if addresses are not page-aligned. */
880 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
881 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
882 ret = ffa_error(FFA_INVALID_PARAMETERS);
883 goto out;
884 }
885
886 /* Convert to physical addresses. */
887 pa_send_begin = pa_from_ipa(send);
888 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
889 pa_recv_begin = pa_from_ipa(recv);
890 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
891
892 /* Fail if the same page is used for the send and receive pages. */
893 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
894 ret = ffa_error(FFA_INVALID_PARAMETERS);
895 goto out;
896 }
Andrew Sculle1322792019-07-01 17:46:10 +0100897
898 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100899 * Ensure the pages are valid, owned and exclusive to the VM and that
900 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100901 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100902 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
903 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
904 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
905 (orig_send_mode & MM_MODE_R) == 0 ||
906 (orig_send_mode & MM_MODE_W) == 0) {
907 ret = ffa_error(FFA_DENIED);
908 goto out;
909 }
910
911 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
912 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
913 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
914 (orig_recv_mode & MM_MODE_R) == 0) {
915 ret = ffa_error(FFA_DENIED);
916 goto out;
917 }
Andrew Sculle1322792019-07-01 17:46:10 +0100918
919 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000920 if (!vm_identity_map(
921 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100922 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100923 local_page_pool, NULL)) {
924 ret = ffa_error(FFA_NO_MEMORY);
925 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100926 }
927
Andrew Scull3c257452019-11-26 13:32:50 +0000928 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
929 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100930 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100931 /* TODO: partial defrag of failed range. */
932 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100933 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100934 goto fail_undo_send;
935 }
936
Olivier Deprez96a2a262020-06-11 17:21:38 +0200937 /* Get extra send/recv pages mapping attributes for the given VM ID. */
938 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
939
Manish Pandeyd34f8892020-06-19 17:41:07 +0100940 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
941 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200942 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100943 goto fail_undo_send_and_recv;
944 }
945
Manish Pandeyd34f8892020-06-19 17:41:07 +0100946 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100947 goto out;
948
Andrew Sculle1322792019-07-01 17:46:10 +0100949fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000950 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100951 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100952
953fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000954 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100955 orig_send_mode, local_page_pool, NULL));
956 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +0100957
958out:
Andrew Sculle1322792019-07-01 17:46:10 +0100959 return ret;
960}
961
962/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100963 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +0100964 * must not be shared. Locking of the page tables combined with a local memory
965 * pool ensures there will always be enough memory to recover from any errors
966 * that arise. The stage-1 page tables must be locked so memory cannot be taken
967 * by another core which could result in this transaction being unable to roll
968 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000969 *
970 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100971 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000972 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000974 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100975 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000976 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 * - FFA_SUCCESS on success if no further action is needed.
978 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000979 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100980 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
982 uint32_t page_count, struct vcpu *current,
983 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100984{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100985 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100987 struct vm_locked vm_locked;
988 struct mm_stage1_locked mm_stage1_locked;
989 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +0000990
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100991 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100992 * Create a local pool so any freed memory can't be used by another
993 * thread. This is to ensure the original mapping can be restored if any
994 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100995 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100996 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
997
Andrew Sculle1322792019-07-01 17:46:10 +0100998 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +0100999 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001000
Manish Pandeyd34f8892020-06-19 17:41:07 +01001001 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1002 page_count, &local_page_pool);
1003 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001004 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001005 }
1006
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001007 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001008 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001009
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001010exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001011 mpool_fini(&local_page_pool);
1012
1013 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001014 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001015
1016 return ret;
1017}
1018
1019/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001020 * Notifies the `to` VM about the message currently in its mailbox, possibly
1021 * with the help of the primary VM.
1022 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1024 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001025{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1027 struct ffa_value primary_ret = {
1028 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001029 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001030 };
1031
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001032 /* Messages for the primary VM are delivered directly. */
1033 if (to.vm->id == HF_PRIMARY_VM_ID) {
1034 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001035 * Only tell the primary VM the size and other details if the
1036 * message is for it, to avoid leaking data about messages for
1037 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001038 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001039 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001040
1041 to.vm->mailbox.state = MAILBOX_STATE_READ;
1042 *next = api_switch_to_primary(current, primary_ret,
1043 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001044 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001045 }
1046
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001047 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1048
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001049 /* Messages for the TEE are sent on via the dispatcher. */
1050 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001052
Olivier Deprez112d2b52020-09-30 07:39:23 +02001053 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001054 /*
1055 * After the call to the TEE completes it must have finished
1056 * reading its RX buffer, so it is ready for another message.
1057 */
1058 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001059 /*
1060 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001061 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001062 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001063 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001064 }
1065
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001066 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001067 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001068 *next = api_switch_to_primary(current, primary_ret,
1069 VCPU_STATE_READY);
1070 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001071
1072 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001073}
1074
1075/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001076 * Copies data from the sender's send buffer to the recipient's receive buffer
1077 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001078 *
1079 * If the recipient's receive buffer is busy, it can optionally register the
1080 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001081 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1083 ffa_vm_id_t receiver_vm_id, uint32_t size,
1084 uint32_t attributes, struct vcpu *current,
1085 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001086{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001087 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001088 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001089 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001090 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001091 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001092 struct vcpu_locked current_locked;
1093 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001094 bool notify =
1095 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001096
Andrew Walbran70bc8622019-10-07 14:15:58 +01001097 /* Ensure sender VM ID corresponds to the current VM. */
1098 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001099 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001100 }
1101
1102 /* Disallow reflexive requests as this suggests an error in the VM. */
1103 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001104 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001105 }
1106
1107 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001108 if (size > FFA_MSG_PAYLOAD_MAX) {
1109 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001110 }
1111
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001112 /*
1113 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1114 * invocation.
1115 */
1116 current_locked = vcpu_lock(current);
1117 is_direct_request_ongoing =
1118 is_ffa_direct_msg_request_ongoing(current_locked);
1119 vcpu_unlock(&current_locked);
1120
1121 if (is_direct_request_ongoing) {
1122 return ffa_error(FFA_DENIED);
1123 }
1124
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001125 /* Ensure the receiver VM exists. */
1126 to = vm_find(receiver_vm_id);
1127 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001129 }
1130
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001131 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001132 * Check that the sender has configured its send buffer. If the tx
1133 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1134 * be safely accessed after releasing the lock since the tx mailbox
1135 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001136 */
1137 sl_lock(&from->lock);
1138 from_msg = from->mailbox.send;
1139 sl_unlock(&from->lock);
1140
1141 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001142 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001143 }
1144
Andrew Walbran82d6d152019-12-24 15:02:06 +00001145 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001146
Andrew Walbran82d6d152019-12-24 15:02:06 +00001147 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001148 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001149 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001150 }
1151
Andrew Walbran82d6d152019-12-24 15:02:06 +00001152 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001153 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001154 to->mailbox.recv_size = size;
1155 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001156 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001157 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001158
1159out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001160 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001161
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001162 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001163}
1164
1165/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001166 * Checks whether the vCPU's attempt to block for a message has already been
1167 * interrupted or whether it is allowed to block.
1168 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001169bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001170{
1171 bool interrupted;
1172
1173 sl_lock(&current->lock);
1174
1175 /*
1176 * Don't block if there are enabled and pending interrupts, to match
1177 * behaviour of wait_for_interrupt.
1178 */
1179 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1180
1181 sl_unlock(&current->lock);
1182
1183 return interrupted;
1184}
1185
1186/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001187 * Receives a message from the mailbox. If one isn't available, this function
1188 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001189 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001190 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001191 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001192struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1193 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001194{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001195 bool is_direct_request_ongoing;
1196 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001197 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001198 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001199 bool is_from_secure_world =
1200 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001201
Andrew Scullaa039b32018-10-04 15:02:26 +01001202 /*
1203 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001204 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001205 */
J-Alvesb37fd082020-10-22 12:29:21 +01001206 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001207 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001208 }
1209
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001210 /*
1211 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1212 * invocation.
1213 */
1214 current_locked = vcpu_lock(current);
1215 is_direct_request_ongoing =
1216 is_ffa_direct_msg_request_ongoing(current_locked);
1217 vcpu_unlock(&current_locked);
1218
1219 if (is_direct_request_ongoing) {
1220 return ffa_error(FFA_DENIED);
1221 }
1222
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001223 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001224
Andrew Scullaa039b32018-10-04 15:02:26 +01001225 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001226 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1227 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001228 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001229 goto out;
1230 }
1231
1232 /* No pending message so fail if not allowed to block. */
1233 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001235 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001236 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001237
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001238 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001239 * From this point onward this call can only be interrupted or a message
1240 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001242 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001243 return_code = ffa_error(FFA_INTERRUPTED);
1244 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001245 goto out;
1246 }
1247
J-Alvesb37fd082020-10-22 12:29:21 +01001248 if (is_from_secure_world) {
1249 /* Return to other world if caller is a SP. */
1250 *next = api_switch_to_other_world(
1251 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1252 VCPU_STATE_BLOCKED_MAILBOX);
1253 } else {
1254 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 struct ffa_value run_return = {
1256 .func = FFA_MSG_WAIT_32,
1257 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001258 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001259
Andrew Walbranb4816552018-12-05 17:35:42 +00001260 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001261 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001262 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001263out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001264 sl_unlock(&vm->lock);
1265
Jose Marinho3e2442f2019-03-12 13:30:37 +00001266 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001267}
1268
1269/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001270 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1271 * by this function, the caller must have called api_mailbox_send before with
1272 * the notify argument set to true, and this call must have failed because the
1273 * mailbox was not available.
1274 *
1275 * It should be called repeatedly to retrieve a list of VMs.
1276 *
1277 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1278 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001279 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001280int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001281{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001282 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001283 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001284 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001285
1286 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001287 if (list_empty(&vm->mailbox.ready_list)) {
1288 ret = -1;
1289 goto exit;
1290 }
1291
1292 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1293 ready_links);
1294 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001295 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001296
1297exit:
1298 sl_unlock(&vm->lock);
1299 return ret;
1300}
1301
1302/**
1303 * Retrieves the next VM waiting to be notified that the mailbox of the
1304 * specified VM became writable. Only primary VMs are allowed to call this.
1305 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001306 * Returns -1 on failure or if there are no waiters; the VM id of the next
1307 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001308 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001309int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001310{
1311 struct vm *vm;
1312 struct vm_locked locked;
1313 struct wait_entry *entry;
1314 struct vm *waiting_vm;
1315
1316 /* Only primary VMs are allowed to call this function. */
1317 if (current->vm->id != HF_PRIMARY_VM_ID) {
1318 return -1;
1319 }
1320
Andrew Walbran42347a92019-05-09 13:59:03 +01001321 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001322 if (vm == NULL) {
1323 return -1;
1324 }
1325
Fuad Tabbaed294af2019-12-20 10:43:01 +00001326 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001327 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001328 entry = api_fetch_waiter(locked);
1329 vm_unlock(&locked);
1330
1331 if (entry == NULL) {
1332 return -1;
1333 }
1334
1335 /* Enqueue notification to waiting VM. */
1336 waiting_vm = entry->waiting_vm;
1337
1338 sl_lock(&waiting_vm->lock);
1339 if (list_empty(&entry->ready_links)) {
1340 list_append(&waiting_vm->mailbox.ready_list,
1341 &entry->ready_links);
1342 }
1343 sl_unlock(&waiting_vm->lock);
1344
1345 return waiting_vm->id;
1346}
1347
1348/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001349 * Releases the caller's mailbox so that a new message can be received. The
1350 * caller must have copied out all data they wish to preserve as new messages
1351 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001352 *
1353 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1355 * - FFA_SUCCESS on success if no further action is needed.
1356 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001357 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001358 * hf_mailbox_waiter_get.
1359 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001360struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001361{
1362 struct vm *vm = current->vm;
1363 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001364 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001365
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001366 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001367 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001368 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001369 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001370 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001371 break;
1372
Andrew Sculld6ee1102019-04-05 22:12:42 +01001373 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001374 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001375 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001376 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001377 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001378 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001379
1380 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001381}
Andrew Walbran318f5732018-11-20 16:23:42 +00001382
1383/**
1384 * Enables or disables a given interrupt ID for the calling vCPU.
1385 *
1386 * Returns 0 on success, or -1 if the intid is invalid.
1387 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001388int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001389{
1390 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001391 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001392
Andrew Walbran318f5732018-11-20 16:23:42 +00001393 if (intid >= HF_NUM_INTIDS) {
1394 return -1;
1395 }
1396
1397 sl_lock(&current->lock);
1398 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001399 /*
1400 * If it is pending and was not enabled before, increment the
1401 * count.
1402 */
1403 if (current->interrupts.interrupt_pending[intid_index] &
1404 ~current->interrupts.interrupt_enabled[intid_index] &
1405 intid_mask) {
1406 current->interrupts.enabled_and_pending_count++;
1407 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001408 current->interrupts.interrupt_enabled[intid_index] |=
1409 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001410 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001411 /*
1412 * If it is pending and was enabled before, decrement the count.
1413 */
1414 if (current->interrupts.interrupt_pending[intid_index] &
1415 current->interrupts.interrupt_enabled[intid_index] &
1416 intid_mask) {
1417 current->interrupts.enabled_and_pending_count--;
1418 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001419 current->interrupts.interrupt_enabled[intid_index] &=
1420 ~intid_mask;
1421 }
1422
1423 sl_unlock(&current->lock);
1424 return 0;
1425}
1426
1427/**
1428 * Returns the ID of the next pending interrupt for the calling vCPU, and
1429 * acknowledges it (i.e. marks it as no longer pending). Returns
1430 * HF_INVALID_INTID if there are no pending interrupts.
1431 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001432uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001433{
1434 uint8_t i;
1435 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001436
1437 /*
1438 * Find the first enabled and pending interrupt ID, return it, and
1439 * deactivate it.
1440 */
1441 sl_lock(&current->lock);
1442 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1443 uint32_t enabled_and_pending =
1444 current->interrupts.interrupt_enabled[i] &
1445 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001446
Andrew Walbran318f5732018-11-20 16:23:42 +00001447 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001448 uint8_t bit_index = ctz(enabled_and_pending);
1449 /*
1450 * Mark it as no longer pending and decrement the count.
1451 */
1452 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001453 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001454 current->interrupts.enabled_and_pending_count--;
1455 first_interrupt =
1456 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001457 break;
1458 }
1459 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001460
1461 sl_unlock(&current->lock);
1462 return first_interrupt;
1463}
1464
1465/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001466 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001467 * given VM and vCPU.
1468 */
1469static inline bool is_injection_allowed(uint32_t target_vm_id,
1470 struct vcpu *current)
1471{
1472 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001473
Andrew Walbran318f5732018-11-20 16:23:42 +00001474 /*
1475 * The primary VM is allowed to inject interrupts into any VM. Secondary
1476 * VMs are only allowed to inject interrupts into their own vCPUs.
1477 */
1478 return current_vm_id == HF_PRIMARY_VM_ID ||
1479 current_vm_id == target_vm_id;
1480}
1481
1482/**
1483 * Injects a virtual interrupt of the given ID into the given target vCPU.
1484 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1485 * when the vCPU is next run, which is up to the scheduler.
1486 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001487 * Returns:
1488 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1489 * ID is invalid, or the current VM is not allowed to inject interrupts to
1490 * the target VM.
1491 * - 0 on success if no further action is needed.
1492 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1493 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001494 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001495int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1496 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001497 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001498{
Andrew Walbran318f5732018-11-20 16:23:42 +00001499 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001500 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001501
1502 if (intid >= HF_NUM_INTIDS) {
1503 return -1;
1504 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001505
Andrew Walbran318f5732018-11-20 16:23:42 +00001506 if (target_vm == NULL) {
1507 return -1;
1508 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001509
Andrew Walbran318f5732018-11-20 16:23:42 +00001510 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001511 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001512 return -1;
1513 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001514
Andrew Walbran318f5732018-11-20 16:23:42 +00001515 if (!is_injection_allowed(target_vm_id, current)) {
1516 return -1;
1517 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001518
Andrew Walbrane1310df2019-04-29 17:28:28 +01001519 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001520
Andrew Walbran17eebf92020-02-05 16:35:49 +00001521 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1522 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1523 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001524 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001525}
Andrew Scull6386f252018-12-06 13:29:10 +00001526
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001527/** Returns the version of the implemented FF-A specification. */
1528struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001529{
1530 /*
1531 * Ensure that both major and minor revision representation occupies at
1532 * most 15 bits.
1533 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001535 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001537 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001539 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001541 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001542
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001543 return (struct ffa_value){
1544 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1545 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001546}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001547
1548int64_t api_debug_log(char c, struct vcpu *current)
1549{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001550 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001551 struct vm *vm = current->vm;
1552 struct vm_locked vm_locked = vm_lock(vm);
1553
Andrew Sculld54e1be2019-08-20 11:09:42 +01001554 if (c == '\n' || c == '\0') {
1555 flush = true;
1556 } else {
1557 vm->log_buffer[vm->log_buffer_length++] = c;
1558 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1559 }
1560
1561 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001562 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1563 vm->log_buffer_length);
1564 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001565 }
1566
1567 vm_unlock(&vm_locked);
1568
1569 return 0;
1570}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001571
1572/**
1573 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001574 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001575 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001576struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001577{
1578 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001579 case FFA_ERROR_32:
1580 case FFA_SUCCESS_32:
1581 case FFA_INTERRUPT_32:
1582 case FFA_VERSION_32:
1583 case FFA_FEATURES_32:
1584 case FFA_RX_RELEASE_32:
1585 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001586 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001587 case FFA_ID_GET_32:
1588 case FFA_MSG_POLL_32:
1589 case FFA_MSG_WAIT_32:
1590 case FFA_YIELD_32:
1591 case FFA_RUN_32:
1592 case FFA_MSG_SEND_32:
1593 case FFA_MEM_DONATE_32:
1594 case FFA_MEM_LEND_32:
1595 case FFA_MEM_SHARE_32:
1596 case FFA_MEM_RETRIEVE_REQ_32:
1597 case FFA_MEM_RETRIEVE_RESP_32:
1598 case FFA_MEM_RELINQUISH_32:
1599 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001600 case FFA_MSG_SEND_DIRECT_RESP_32:
1601 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001603 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001604 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001605 }
1606}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001607
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001608/**
1609 * Get target VM vCPU for direct messaging request.
1610 * If VM is UP then return first vCPU.
1611 * If VM is MP then return vCPU whose index matches current CPU index.
1612 */
1613static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1614 struct vm *vm, struct vcpu *current)
1615{
1616 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1617 struct vcpu *vcpu = NULL;
1618
1619 if (vm->vcpu_count == 1) {
1620 vcpu = vm_get_vcpu(vm, 0);
1621 } else if (current_cpu_index < vm->vcpu_count) {
1622 vcpu = vm_get_vcpu(vm, current_cpu_index);
1623 }
1624
1625 return vcpu;
1626}
1627
1628/**
1629 * Send an FF-A direct message request.
1630 */
1631struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1632 ffa_vm_id_t receiver_vm_id,
1633 struct ffa_value args,
1634 struct vcpu *current,
1635 struct vcpu **next)
1636{
1637 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001638 struct vm *receiver_vm;
1639 struct vcpu *receiver_vcpu;
1640 struct two_vcpu_locked vcpus_locked;
1641
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001642 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1643 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001644 return ffa_error(FFA_NOT_SUPPORTED);
1645 }
1646
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001647 receiver_vm = vm_find(receiver_vm_id);
1648 if (receiver_vm == NULL) {
1649 return ffa_error(FFA_INVALID_PARAMETERS);
1650 }
1651
1652 /*
1653 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1654 * UP (migratable) or MP partitions with a number of vCPUs matching the
1655 * number of PEs in the system. It further states that MP partitions
1656 * accepting direct request messages cannot migrate.
1657 */
1658 receiver_vcpu =
1659 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1660 if (receiver_vcpu == NULL) {
1661 return ffa_error(FFA_INVALID_PARAMETERS);
1662 }
1663
1664 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1665
1666 /*
1667 * If destination vCPU is executing or already received an
1668 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1669 * busy. There is a brief period of time where the vCPU state has
1670 * changed but regs_available is still false thus consider this case as
1671 * the vCPU not yet ready to receive a direct message request.
1672 */
1673 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1674 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1675 !receiver_vcpu->regs_available) {
1676 ret = ffa_error(FFA_BUSY);
1677 goto out;
1678 }
1679
1680 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1681 memory_order_relaxed)) {
1682 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
1683 dlog_notice("Aborting VM %u vCPU %u\n",
1684 receiver_vcpu->vm->id,
1685 vcpu_index(receiver_vcpu));
1686 receiver_vcpu->state = VCPU_STATE_ABORTED;
1687 }
1688
1689 ret = ffa_error(FFA_ABORTED);
1690 goto out;
1691 }
1692
1693 switch (receiver_vcpu->state) {
1694 case VCPU_STATE_OFF:
1695 case VCPU_STATE_RUNNING:
1696 case VCPU_STATE_ABORTED:
1697 case VCPU_STATE_READY:
1698 case VCPU_STATE_BLOCKED_INTERRUPT:
1699 ret = ffa_error(FFA_BUSY);
1700 goto out;
1701 case VCPU_STATE_BLOCKED_MAILBOX:
1702 /*
1703 * Expect target vCPU to be blocked after having called
1704 * ffa_msg_wait or sent a direct message response.
1705 */
1706 break;
1707 }
1708
1709 /* Inject timer interrupt if any pending */
1710 if (arch_timer_pending(&receiver_vcpu->regs)) {
1711 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1712 HF_VIRTUAL_TIMER_INTID,
1713 current, NULL);
1714
1715 arch_timer_mask(&receiver_vcpu->regs);
1716 }
1717
1718 /* The receiver vCPU runs upon direct message invocation */
1719 receiver_vcpu->cpu = current->cpu;
1720 receiver_vcpu->state = VCPU_STATE_RUNNING;
1721 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001722 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001723
1724 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1725 .func = args.func,
1726 .arg1 = args.arg1,
1727 .arg2 = 0,
1728 .arg3 = args.arg3,
1729 .arg4 = args.arg4,
1730 .arg5 = args.arg5,
1731 .arg6 = args.arg6,
1732 .arg7 = args.arg7,
1733 });
1734
1735 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1736
1737 /* Switch to receiver vCPU targeted to by direct msg request */
1738 *next = receiver_vcpu;
1739
1740 /*
1741 * Since this flow will lead to a VM switch, the return value will not
1742 * be applied to current vCPU.
1743 */
1744
1745out:
1746 sl_unlock(&receiver_vcpu->lock);
1747 sl_unlock(&current->lock);
1748
1749 return ret;
1750}
1751
1752/**
1753 * Send an FF-A direct message response.
1754 */
1755struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1756 ffa_vm_id_t receiver_vm_id,
1757 struct ffa_value args,
1758 struct vcpu *current,
1759 struct vcpu **next)
1760{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001761 struct vcpu_locked current_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001762
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001763 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1764 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001765 return ffa_error(FFA_NOT_SUPPORTED);
1766 }
1767
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001768 current_locked = vcpu_lock(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001769
1770 /*
1771 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1772 * defined originator.
1773 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001774 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001775 /*
1776 * Sending direct response but direct request origin vCPU is
1777 * not set.
1778 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001779 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001780 return ffa_error(FFA_DENIED);
1781 }
1782
1783 if (current->direct_request_origin_vm_id != receiver_vm_id) {
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001784 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001785 return ffa_error(FFA_DENIED);
1786 }
1787
1788 /* Clear direct request origin for the caller. */
1789 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1790
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001791 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001792
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001793 if (!vm_id_is_current_world(receiver_vm_id)) {
1794 *next = api_switch_to_other_world(current,
1795 (struct ffa_value){
1796 .func = args.func,
1797 .arg1 = args.arg1,
1798 .arg2 = 0,
1799 .arg3 = args.arg3,
1800 .arg4 = args.arg4,
1801 .arg5 = args.arg5,
1802 .arg6 = args.arg6,
1803 .arg7 = args.arg7,
1804 },
1805 VCPU_STATE_BLOCKED_MAILBOX);
1806 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
1807 *next = api_switch_to_primary(current,
1808 (struct ffa_value){
1809 .func = args.func,
1810 .arg1 = args.arg1,
1811 .arg2 = 0,
1812 .arg3 = args.arg3,
1813 .arg4 = args.arg4,
1814 .arg5 = args.arg5,
1815 .arg6 = args.arg6,
1816 .arg7 = args.arg7,
1817 },
1818 VCPU_STATE_BLOCKED_MAILBOX);
1819 } else {
1820 panic("Invalid direct message response invocation");
1821 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001822
1823 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1824}
1825
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001826struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1827 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001828 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001829{
1830 struct vm *from = current->vm;
1831 struct vm *to;
1832 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001833 struct ffa_memory_region *memory_region;
1834 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001835
1836 if (ipa_addr(address) != 0 || page_count != 0) {
1837 /*
1838 * Hafnium only supports passing the descriptor in the TX
1839 * mailbox.
1840 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001841 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001842 }
1843
Andrew Walbranca808b12020-05-15 17:22:28 +01001844 if (fragment_length > length) {
1845 dlog_verbose(
1846 "Fragment length %d greater than total length %d.\n",
1847 fragment_length, length);
1848 return ffa_error(FFA_INVALID_PARAMETERS);
1849 }
1850 if (fragment_length < sizeof(struct ffa_memory_region) +
1851 sizeof(struct ffa_memory_access)) {
1852 dlog_verbose(
1853 "Initial fragment length %d smaller than header size "
1854 "%d.\n",
1855 fragment_length,
1856 sizeof(struct ffa_memory_region) +
1857 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001858 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001859 }
1860
1861 /*
1862 * Check that the sender has configured its send buffer. If the TX
1863 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1864 * be safely accessed after releasing the lock since the TX mailbox
1865 * address can only be configured once.
1866 */
1867 sl_lock(&from->lock);
1868 from_msg = from->mailbox.send;
1869 sl_unlock(&from->lock);
1870
1871 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001872 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001873 }
1874
1875 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001876 * Copy the memory region descriptor to a fresh page from the memory
1877 * pool. This prevents the sender from changing it underneath us, and
1878 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001879 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001880 if (fragment_length > HF_MAILBOX_SIZE ||
1881 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001882 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001883 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001884 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001885 if (memory_region == NULL) {
1886 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001887 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001888 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001889 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001890
1891 /* The sender must match the caller. */
1892 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001893 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001894 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001895 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001896 }
1897
Andrew Walbrana65a1322020-04-06 19:32:32 +01001898 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001899 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001900 dlog_verbose(
1901 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001902 "endpoint memory access descriptors, expected 1).\n",
1903 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001904 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001905 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001906 }
1907
1908 /*
1909 * Ensure that the receiver VM exists and isn't the same as the sender.
1910 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001911 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001912 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001913 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001914 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001915 goto out;
1916 }
1917
1918 if (to->id == HF_TEE_VM_ID) {
1919 /*
1920 * The 'to' VM lock is only needed in the case that it is the
1921 * TEE VM.
1922 */
1923 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1924
1925 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001926 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001927 goto out_unlock;
1928 }
1929
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001930 ret = ffa_memory_tee_send(
1931 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1932 length, fragment_length, share_func, &api_page_pool);
1933 /*
1934 * ffa_tee_memory_send takes ownership of the memory_region, so
1935 * make sure we don't free it.
1936 */
1937 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001938
1939 out_unlock:
1940 vm_unlock(&vm_to_from_lock.vm1);
1941 vm_unlock(&vm_to_from_lock.vm2);
1942 } else {
1943 struct vm_locked from_locked = vm_lock(from);
1944
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001945 ret = ffa_memory_send(from_locked, memory_region, length,
1946 fragment_length, share_func,
1947 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001948 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001949 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001950 * make sure we don't free it.
1951 */
1952 memory_region = NULL;
1953
1954 vm_unlock(&from_locked);
1955 }
1956
1957out:
1958 if (memory_region != NULL) {
1959 mpool_free(&api_page_pool, memory_region);
1960 }
1961
1962 return ret;
1963}
1964
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001965struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1966 uint32_t fragment_length,
1967 ipaddr_t address, uint32_t page_count,
1968 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001969{
1970 struct vm *to = current->vm;
1971 struct vm_locked to_locked;
1972 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001973 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001974 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001975 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001976
1977 if (ipa_addr(address) != 0 || page_count != 0) {
1978 /*
1979 * Hafnium only supports passing the descriptor in the TX
1980 * mailbox.
1981 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001982 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001983 }
1984
Andrew Walbrana65a1322020-04-06 19:32:32 +01001985 if (fragment_length != length) {
1986 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001988 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001989
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001990 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001991 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001992 message_buffer_size = cpu_get_buffer_size(current->cpu);
1993 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1994 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001995 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001996 }
1997
1998 to_locked = vm_lock(to);
1999 to_msg = to->mailbox.send;
2000
2001 if (to_msg == NULL) {
2002 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002003 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002004 goto out;
2005 }
2006
2007 /*
2008 * Copy the retrieve request descriptor to an internal buffer, so that
2009 * the caller can't change it underneath us.
2010 */
2011 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2012
2013 if (msg_receiver_busy(to_locked, NULL, false)) {
2014 /*
2015 * Can't retrieve memory information if the mailbox is not
2016 * available.
2017 */
2018 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002019 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002020 goto out;
2021 }
2022
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002023 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2024 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002025
2026out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002027 vm_unlock(&to_locked);
2028 return ret;
2029}
2030
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002031struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002032{
2033 struct vm *from = current->vm;
2034 struct vm_locked from_locked;
2035 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002036 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002037 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002038 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002039 uint32_t length;
2040
2041 from_locked = vm_lock(from);
2042 from_msg = from->mailbox.send;
2043
2044 if (from_msg == NULL) {
2045 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002046 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002047 goto out;
2048 }
2049
2050 /*
2051 * Calculate length from relinquish descriptor before copying. We will
2052 * check again later to make sure it hasn't changed.
2053 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002054 length = sizeof(struct ffa_mem_relinquish) +
2055 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2056 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002057 /*
2058 * Copy the relinquish descriptor to an internal buffer, so that the
2059 * caller can't change it underneath us.
2060 */
2061 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002062 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002063 message_buffer_size = cpu_get_buffer_size(current->cpu);
2064 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2065 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002066 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002067 goto out;
2068 }
2069 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2070
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002071 if (sizeof(struct ffa_mem_relinquish) +
2072 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002073 length) {
2074 dlog_verbose(
2075 "Endpoint count changed while copying to internal "
2076 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002078 goto out;
2079 }
2080
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002081 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2082 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002083
2084out:
2085 vm_unlock(&from_locked);
2086 return ret;
2087}
2088
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2090 ffa_memory_region_flags_t flags,
2091 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002092{
2093 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002094 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002095
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002096 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2097 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002098 struct vm_locked to_locked = vm_lock(to);
2099
Andrew Walbranca808b12020-05-15 17:22:28 +01002100 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002101 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002102
Andrew Walbran290b0c92020-02-03 16:37:14 +00002103 vm_unlock(&to_locked);
2104 } else {
2105 struct vm *from = vm_find(HF_TEE_VM_ID);
2106 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2107
Andrew Walbranca808b12020-05-15 17:22:28 +01002108 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2109 vm_to_from_lock.vm2, handle, flags,
2110 &api_page_pool);
2111
2112 vm_unlock(&vm_to_from_lock.vm1);
2113 vm_unlock(&vm_to_from_lock.vm2);
2114 }
2115
2116 return ret;
2117}
2118
2119struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2120 uint32_t fragment_offset,
2121 ffa_vm_id_t sender_vm_id,
2122 struct vcpu *current)
2123{
2124 struct vm *to = current->vm;
2125 struct vm_locked to_locked;
2126 struct ffa_value ret;
2127
2128 /* Sender ID MBZ at virtual instance. */
2129 if (sender_vm_id != 0) {
2130 return ffa_error(FFA_INVALID_PARAMETERS);
2131 }
2132
2133 to_locked = vm_lock(to);
2134
2135 if (msg_receiver_busy(to_locked, NULL, false)) {
2136 /*
2137 * Can't retrieve memory information if the mailbox is not
2138 * available.
2139 */
2140 dlog_verbose("RX buffer not ready.\n");
2141 ret = ffa_error(FFA_BUSY);
2142 goto out;
2143 }
2144
2145 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2146 &api_page_pool);
2147
2148out:
2149 vm_unlock(&to_locked);
2150 return ret;
2151}
2152
2153struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2154 uint32_t fragment_length,
2155 ffa_vm_id_t sender_vm_id,
2156 struct vcpu *current)
2157{
2158 struct vm *from = current->vm;
2159 const void *from_msg;
2160 void *fragment_copy;
2161 struct ffa_value ret;
2162
2163 /* Sender ID MBZ at virtual instance. */
2164 if (sender_vm_id != 0) {
2165 return ffa_error(FFA_INVALID_PARAMETERS);
2166 }
2167
2168 /*
2169 * Check that the sender has configured its send buffer. If the TX
2170 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2171 * be safely accessed after releasing the lock since the TX mailbox
2172 * address can only be configured once.
2173 */
2174 sl_lock(&from->lock);
2175 from_msg = from->mailbox.send;
2176 sl_unlock(&from->lock);
2177
2178 if (from_msg == NULL) {
2179 return ffa_error(FFA_INVALID_PARAMETERS);
2180 }
2181
2182 /*
2183 * Copy the fragment to a fresh page from the memory pool. This prevents
2184 * the sender from changing it underneath us, and also lets us keep it
2185 * around in the share state table if needed.
2186 */
2187 if (fragment_length > HF_MAILBOX_SIZE ||
2188 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2189 dlog_verbose(
2190 "Fragment length %d larger than mailbox size %d.\n",
2191 fragment_length, HF_MAILBOX_SIZE);
2192 return ffa_error(FFA_INVALID_PARAMETERS);
2193 }
2194 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2195 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2196 0) {
2197 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2198 return ffa_error(FFA_INVALID_PARAMETERS);
2199 }
2200 fragment_copy = mpool_alloc(&api_page_pool);
2201 if (fragment_copy == NULL) {
2202 dlog_verbose("Failed to allocate fragment copy.\n");
2203 return ffa_error(FFA_NO_MEMORY);
2204 }
2205 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2206
2207 /*
2208 * Hafnium doesn't support fragmentation of memory retrieve requests
2209 * (because it doesn't support caller-specified mappings, so a request
2210 * will never be larger than a single page), so this must be part of a
2211 * memory send (i.e. donate, lend or share) request.
2212 *
2213 * We can tell from the handle whether the memory transaction is for the
2214 * TEE or not.
2215 */
2216 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2217 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2218 struct vm_locked from_locked = vm_lock(from);
2219
2220 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2221 fragment_length, handle,
2222 &api_page_pool);
2223 /*
2224 * `ffa_memory_send_continue` takes ownership of the
2225 * fragment_copy, so we don't need to free it here.
2226 */
2227 vm_unlock(&from_locked);
2228 } else {
2229 struct vm *to = vm_find(HF_TEE_VM_ID);
2230 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2231
2232 /*
2233 * The TEE RX buffer state is checked in
2234 * `ffa_memory_tee_send_continue` rather than here, as we need
2235 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2236 * than FFA_ERROR FFA_BUSY in case it is busy.
2237 */
2238
2239 ret = ffa_memory_tee_send_continue(
2240 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2241 fragment_length, handle, &api_page_pool);
2242 /*
2243 * `ffa_memory_tee_send_continue` takes ownership of the
2244 * fragment_copy, so we don't need to free it here.
2245 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002246
2247 vm_unlock(&vm_to_from_lock.vm1);
2248 vm_unlock(&vm_to_from_lock.vm2);
2249 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002250
2251 return ret;
2252}