blob: c33d9eb8add7aaf436c8e4741ec9b0f9e1f5e247 [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"
Andrew Walbran2619e0a2020-01-10 16:37:50 +000013#include "hf/arch/tee.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000014#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000015
Andrew Scull877ae4b2019-07-02 12:52:33 +010016#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "hf/ffa_internal.h"
19#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000020#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010021#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000022#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010023#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010024#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010028#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010029
Fuad Tabbae4efcc32020-07-16 15:37:27 +010030static_assert(sizeof(struct ffa_partition_info) == 8,
31 "Partition information descriptor size doesn't match the one in "
32 "the FF-A 1.0 EAC specification, Table 82.");
33
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000034/*
35 * To eliminate the risk of deadlocks, we define a partial order for the
36 * acquisition of locks held concurrently by the same physical CPU. Our current
37 * ordering requirements are as follows:
38 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010039 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000040 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010041 * Locks of the same kind require the lock of lowest address to be locked first,
42 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000043 */
44
Andrew Scullaa039b32018-10-04 15:02:26 +010045static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010046 "Currently, a page is mapped for the send and receive buffers so "
47 "the maximum request is the size of a page.");
48
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000049static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
50 "The page pool entry size must be at least as big as the mailbox "
51 "size, so that memory region descriptors can be copied from the "
52 "mailbox for memory sharing.");
53
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000054static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000055
56/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000057 * Initialises the API page pool by taking ownership of the contents of the
58 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000059 */
60void api_init(struct mpool *ppool)
61{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000062 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000063}
64
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010065/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000066 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010067 *
68 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010069 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000073 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074{
Andrew Walbran42347a92019-05-09 13:59:03 +010075 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010076 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010077
Andrew Walbran508e63c2018-12-20 17:02:37 +000078 /*
79 * If the secondary is blocked but has a timer running, sleep until the
80 * timer fires rather than indefinitely.
81 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010082 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010083 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
84 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010085 if (arch_timer_enabled_current()) {
86 uint64_t remaining_ns =
87 arch_timer_remaining_ns_current();
88
89 if (remaining_ns == 0) {
90 /*
91 * Timer is pending, so the current vCPU should
92 * be run again right away.
93 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010094 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010095 /*
96 * primary_ret.arg1 should already be set to the
97 * current VM ID and vCPU ID.
98 */
99 primary_ret.arg2 = 0;
100 } else {
101 primary_ret.arg2 = remaining_ns;
102 }
103 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100104 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100105 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000106 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100107 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000108
109 default:
110 /* Do nothing. */
111 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000112 }
113
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100114 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100115 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100116
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000117 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000118 sl_lock(&current->lock);
119 current->state = secondary_state;
120 sl_unlock(&current->lock);
121
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100122 return next;
123}
124
125/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100126 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
127 * registers the `from` VM to be notified when it becomes available.
128 */
129static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
130{
131 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
132 to.vm->mailbox.recv == NULL) {
133 /*
134 * Fail if the receiver isn't currently ready to receive data,
135 * setting up for notification if requested.
136 */
137 if (notify) {
138 struct wait_entry *entry =
139 vm_get_wait_entry(from, to.vm->id);
140
141 /* Append waiter only if it's not there yet. */
142 if (list_empty(&entry->wait_links)) {
143 list_append(&to.vm->mailbox.waiter_list,
144 &entry->wait_links);
145 }
146 }
147
148 return true;
149 }
150
151 return false;
152}
153
154/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000155 * Returns true if the given vCPU is executing in context of an
156 * FFA_MSG_SEND_DIRECT_REQ invocation.
157 */
158static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
159{
160 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
161}
162
163/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000164 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000165 */
166struct vcpu *api_preempt(struct vcpu *current)
167{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100168 struct ffa_value ret = {
169 .func = FFA_INTERRUPT_32,
170 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000171 };
172
Andrew Sculld6ee1102019-04-05 22:12:42 +0100173 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000174}
175
176/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000177 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000178 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100179 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100180struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100181{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100182 struct ffa_value ret = {
183 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
184 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100185 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000186
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000187 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100188 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100189}
190
191/**
Andrew Walbran33645652019-04-15 12:29:31 +0100192 * Puts the current vCPU in off mode, and returns to the primary VM.
193 */
194struct vcpu *api_vcpu_off(struct vcpu *current)
195{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100196 struct ffa_value ret = {
197 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
198 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100199 };
200
201 /*
202 * Disable the timer, so the scheduler doesn't get told to call back
203 * based on it.
204 */
205 arch_timer_disable_current();
206
207 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
208}
209
210/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000211 * Returns to the primary VM to allow this CPU to be used for other tasks as the
212 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100213 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000214 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000215struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000216{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000217 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
218 struct vcpu_locked current_locked;
219 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000220
221 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000222 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000223 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000224 }
225
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000226 current_locked = vcpu_lock(current);
227 is_direct_request_ongoing =
228 is_ffa_direct_msg_request_ongoing(current_locked);
229 vcpu_unlock(&current_locked);
230
231 if (is_direct_request_ongoing) {
232 return ffa_error(FFA_DENIED);
233 }
234
235 *next = api_switch_to_primary(
236 current,
237 (struct ffa_value){.func = FFA_YIELD_32,
238 .arg1 = ffa_vm_vcpu(current->vm->id,
239 vcpu_index(current))},
240 VCPU_STATE_READY);
241
242 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000243}
244
245/**
Andrew Walbran33645652019-04-15 12:29:31 +0100246 * Switches to the primary so that it can switch to the target, or kick it if it
247 * is already running on a different physical CPU.
248 */
249struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
250{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100251 struct ffa_value ret = {
252 .func = HF_FFA_RUN_WAKE_UP,
253 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
254 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100255 };
256 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
257}
258
259/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000260 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000261 */
262struct vcpu *api_abort(struct vcpu *current)
263{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100264 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000265
Andrew Walbran17eebf92020-02-05 16:35:49 +0000266 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
267 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000268
269 if (current->vm->id == HF_PRIMARY_VM_ID) {
270 /* TODO: what to do when the primary aborts? */
271 for (;;) {
272 /* Do nothing. */
273 }
274 }
275
276 atomic_store_explicit(&current->vm->aborting, true,
277 memory_order_relaxed);
278
279 /* TODO: free resources once all vCPUs abort. */
280
Andrew Sculld6ee1102019-04-05 22:12:42 +0100281 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000282}
283
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100284struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
285 const struct ffa_uuid *uuid)
286{
287 struct vm *current_vm = current->vm;
288 struct vm_locked current_vm_locked;
289 ffa_vm_count_t vm_count = 0;
290 bool uuid_is_null = ffa_uuid_is_null(uuid);
291 struct ffa_value ret;
292 uint32_t size;
293 struct ffa_partition_info partitions[MAX_VMS];
294
295 /*
296 * Iterate through the VMs to find the ones with a matching UUID.
297 * A Null UUID retrieves information for all VMs.
298 */
299 for (uint16_t index = 0; index < vm_get_count(); ++index) {
300 const struct vm *vm = vm_find_index(index);
301
302 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
303 partitions[vm_count].vm_id = vm->id;
304 partitions[vm_count].vcpu_count = vm->vcpu_count;
305
306 /* Hafnium only supports indirect messaging. */
307 partitions[vm_count].properties =
308 FFA_PARTITION_INDIRECT_MSG;
309
310 ++vm_count;
311 }
312 }
313
314 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
315 if (vm_count == 0) {
316 return ffa_error(FFA_INVALID_PARAMETERS);
317 }
318
319 size = vm_count * sizeof(partitions[0]);
320 if (size > FFA_MSG_PAYLOAD_MAX) {
321 dlog_error(
322 "Partition information does not fit in the VM's RX "
323 "buffer.\n");
324 return ffa_error(FFA_NO_MEMORY);
325 }
326
327 /*
328 * Partition information is returned in the VM's RX buffer, which is why
329 * the lock is needed.
330 */
331 current_vm_locked = vm_lock(current_vm);
332
333 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
334 /*
335 * Can't retrieve memory information if the mailbox is not
336 * available.
337 */
338 dlog_verbose("RX buffer not ready.\n");
339 ret = ffa_error(FFA_BUSY);
340 goto out_unlock;
341 }
342
343 /* Populate the VM's RX buffer with the partition information. */
344 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
345 size);
346 current_vm->mailbox.recv_size = size;
347 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
348 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
349 current_vm->mailbox.state = MAILBOX_STATE_READ;
350
351 /* Return the count of partition information descriptors in w2. */
352 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
353
354out_unlock:
355 vm_unlock(&current_vm_locked);
356
357 return ret;
358}
359
Andrew Scull9726c252019-01-23 13:44:19 +0000360/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000361 * Returns the ID of the VM.
362 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100363struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000364{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100365 return (struct ffa_value){.func = FFA_SUCCESS_32,
366 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000367}
368
369/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000370 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000371 * function to indicate that register state for the given vCPU has been saved
372 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000373 */
374void api_regs_state_saved(struct vcpu *vcpu)
375{
376 sl_lock(&vcpu->lock);
377 vcpu->regs_available = true;
378 sl_unlock(&vcpu->lock);
379}
380
381/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000382 * Retrieves the next waiter and removes it from the wait list if the VM's
383 * mailbox is in a writable state.
384 */
385static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
386{
387 struct wait_entry *entry;
388 struct vm *vm = locked_vm.vm;
389
Andrew Sculld6ee1102019-04-05 22:12:42 +0100390 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000391 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
392 /* The mailbox is not writable or there are no waiters. */
393 return NULL;
394 }
395
396 /* Remove waiter from the wait list. */
397 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
398 wait_links);
399 list_remove(&entry->wait_links);
400 return entry;
401}
402
403/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000404 * Assuming that the arguments have already been checked by the caller, injects
405 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
406 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
407 * is next run, which is up to the scheduler.
408 *
409 * Returns:
410 * - 0 on success if no further action is needed.
411 * - 1 if it was called by the primary VM and the primary VM now needs to wake
412 * up or kick the target vCPU.
413 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200414static int64_t internal_interrupt_inject_locked(
415 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
416 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000417{
418 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100419 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000420 int64_t ret = 0;
421
Andrew Walbran508e63c2018-12-20 17:02:37 +0000422 /*
423 * We only need to change state and (maybe) trigger a virtual IRQ if it
424 * is enabled and was not previously pending. Otherwise we can skip
425 * everything except setting the pending bit.
426 *
427 * If you change this logic make sure to update the need_vm_lock logic
428 * above to match.
429 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200430 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
431 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000432 intid_mask)) {
433 goto out;
434 }
435
436 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200437 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000438
439 /*
440 * Only need to update state if there was not already an
441 * interrupt enabled and pending.
442 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200443 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000444 goto out;
445 }
446
Andrew Walbran508e63c2018-12-20 17:02:37 +0000447 if (current->vm->id == HF_PRIMARY_VM_ID) {
448 /*
449 * If the call came from the primary VM, let it know that it
450 * should run or kick the target vCPU.
451 */
452 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200453 } else if (current != target_locked.vcpu && next != NULL) {
454 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000455 }
456
457out:
458 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200459 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
460 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000461
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200462 return ret;
463}
464
465/* Wrapper to internal_interrupt_inject with locking of target vCPU */
466static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
467 uint32_t intid, struct vcpu *current,
468 struct vcpu **next)
469{
470 int64_t ret;
471 struct vcpu_locked target_locked;
472
473 target_locked = vcpu_lock(target_vcpu);
474 ret = internal_interrupt_inject_locked(target_locked, intid, current,
475 next);
476 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000477
478 return ret;
479}
480
481/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100482 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
483 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100484 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100485static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100486{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000487 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100488 case FFA_MSG_SEND_32:
489 return (struct ffa_value){
490 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000491 .arg1 = (receiver->mailbox.recv_sender << 16) |
492 receiver->id,
493 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000494 default:
495 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000496 dlog_error("Tried to return an invalid message function %#x\n",
497 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000499 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100500}
501
502/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000503 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000504 * value needs to be forced onto the vCPU.
505 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000506static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100507 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000508{
Andrew Scullb06d1752019-02-04 10:15:48 +0000509 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000510 bool ret;
511
Andrew Scullb06d1752019-02-04 10:15:48 +0000512 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000513 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000514 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100515 * The VM lock is not needed in the common case so it must only be taken
516 * when it is going to be needed. This ensures there are no inter-vCPU
517 * dependencies in the common run case meaning the sensitive context
518 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000519 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000520 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000521
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000522 /* The VM needs to be locked to deliver mailbox messages. */
523 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
524 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000525 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000526 sl_lock(&vcpu->vm->lock);
527 sl_lock(&vcpu->lock);
528 }
529
530 /*
531 * If the vCPU is already running somewhere then we can't run it here
532 * simultaneously. While it is actually running then the state should be
533 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
534 * stops running but while Hafnium is in the process of switching back
535 * to the primary there will be a brief period while the state has been
536 * updated but `regs_available` is still false (until
537 * `api_regs_state_saved` is called). We can't start running it again
538 * until this has finished, so count this state as still running for the
539 * purposes of this check.
540 */
541 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
542 /*
543 * vCPU is running on another pCPU.
544 *
545 * It's okay not to return the sleep duration here because the
546 * other physical CPU that is currently running this vCPU will
547 * return the sleep duration if needed.
548 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100549 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000550 ret = false;
551 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000552 }
Andrew Scull9726c252019-01-23 13:44:19 +0000553
554 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100555 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000556 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
557 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100558 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000559 }
560 ret = false;
561 goto out;
562 }
563
Andrew Walbran508e63c2018-12-20 17:02:37 +0000564 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100565 case VCPU_STATE_RUNNING:
566 case VCPU_STATE_OFF:
567 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000568 ret = false;
569 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000570
Andrew Sculld6ee1102019-04-05 22:12:42 +0100571 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000572 /*
573 * A pending message allows the vCPU to run so the message can
574 * be delivered directly.
575 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100576 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100577 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100579 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000580 break;
581 }
582 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100583 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000584 /* Allow virtual interrupts to be delivered. */
585 if (vcpu->interrupts.enabled_and_pending_count > 0) {
586 break;
587 }
588
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100589 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
590
Andrew Walbran508e63c2018-12-20 17:02:37 +0000591 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100592 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000593 arch_timer_remaining_ns(&vcpu->regs);
594
595 /*
596 * The timer expired so allow the interrupt to be
597 * delivered.
598 */
599 if (timer_remaining_ns == 0) {
600 break;
601 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100602 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000603
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100604 /*
605 * The vCPU is not ready to run, return the appropriate code to
606 * the primary which called vcpu_run.
607 */
608 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100609 ? FFA_MSG_WAIT_32
610 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100611 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
612 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000613
614 ret = false;
615 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000616
Andrew Sculld6ee1102019-04-05 22:12:42 +0100617 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000618 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000619 }
620
Andrew Scullb06d1752019-02-04 10:15:48 +0000621 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000622 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100623 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000624
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000625 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000626 * Mark the registers as unavailable now that we're about to reflect
627 * them onto the real registers. This will also prevent another physical
628 * CPU from trying to read these registers.
629 */
630 vcpu->regs_available = false;
631
632 ret = true;
633
634out:
635 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000636 if (need_vm_lock) {
637 sl_unlock(&vcpu->vm->lock);
638 }
639
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000640 return ret;
641}
642
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
644 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100645{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100646 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100647 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100649
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000650 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100651 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100652 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100653 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100654 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100655
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000656 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100657 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100658 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100659 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100660
Andrew Scull19503262018-09-20 14:48:39 +0100661 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100662 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100663 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100664 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100665 }
666
Fuad Tabbaed294af2019-12-20 10:43:01 +0000667 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100668 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100669 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100670 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100671
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000672 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100673 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000674 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000675 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100676 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000677
Andrew Walbran508e63c2018-12-20 17:02:37 +0000678 /*
679 * Inject timer interrupt if timer has expired. It's safe to access
680 * vcpu->regs here because api_vcpu_prepare_run already made sure that
681 * regs_available was true (and then set it to false) before returning
682 * true.
683 */
684 if (arch_timer_pending(&vcpu->regs)) {
685 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100686 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
687 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000688
689 /*
690 * Set the mask bit so the hardware interrupt doesn't fire
691 * again. Ideally we wouldn't do this because it affects what
692 * the secondary vCPU sees, but if we don't then we end up with
693 * a loop of the interrupt firing each time we try to return to
694 * the secondary vCPU.
695 */
696 arch_timer_mask(&vcpu->regs);
697 }
698
Fuad Tabbaed294af2019-12-20 10:43:01 +0000699 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000700 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000701
Andrew Scull33fecd32019-01-08 14:48:27 +0000702 /*
703 * Set a placeholder return code to the scheduler. This will be
704 * overwritten when the switch back to the primary occurs.
705 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100706 ret.func = FFA_INTERRUPT_32;
707 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100708 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000709
Andrew Scull6d2db332018-10-10 15:28:17 +0100710out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100711 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100712}
713
714/**
Andrew Scull81e85092018-12-12 12:56:20 +0000715 * Check that the mode indicates memory that is valid, owned and exclusive.
716 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100717static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000718{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100719 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
720 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000721}
722
723/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100724 * Determines the value to be returned by api_ffa_rxtx_map and
725 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
726 * there are waiters, it also switches back to the primary VM for it to wake
727 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000728 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100729static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
730 struct vcpu *current,
731 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000732{
733 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000734
735 if (list_empty(&vm->mailbox.waiter_list)) {
736 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100737 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000738 }
739
740 if (vm->id == HF_PRIMARY_VM_ID) {
741 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100742 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000743 }
744
745 /*
746 * Switch back to the primary VM, informing it that there are waiters
747 * that need to be notified.
748 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000749 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100750 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000751 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000752
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100753 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000754}
755
756/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100757 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100758 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100759static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
760 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100761 paddr_t pa_send_begin, paddr_t pa_send_end,
762 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200763 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100764 struct mpool *local_page_pool)
765{
766 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100767
768 /* Map the send page as read-only in the hypervisor address space. */
769 vm_locked.vm->mailbox.send =
770 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200771 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100772 if (!vm_locked.vm->mailbox.send) {
773 /* TODO: partial defrag of failed range. */
774 /* Recover any memory consumed in failed mapping. */
775 mm_defrag(mm_stage1_locked, local_page_pool);
776 goto fail;
777 }
778
779 /*
780 * Map the receive page as writable in the hypervisor address space. On
781 * failure, unmap the send page before returning.
782 */
783 vm_locked.vm->mailbox.recv =
784 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200785 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100786 if (!vm_locked.vm->mailbox.recv) {
787 /* TODO: partial defrag of failed range. */
788 /* Recover any memory consumed in failed mapping. */
789 mm_defrag(mm_stage1_locked, local_page_pool);
790 goto fail_undo_send;
791 }
792
793 ret = true;
794 goto out;
795
796 /*
797 * The following mappings will not require more memory than is available
798 * in the local pool.
799 */
800fail_undo_send:
801 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100802 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
803 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100804
805fail:
806 ret = false;
807
808out:
Andrew Sculle1322792019-07-01 17:46:10 +0100809 return ret;
810}
811
812/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100813 * Sanity checks and configures the send and receive pages in the VM stage-2
814 * and hypervisor stage-1 page tables.
815 *
816 * Returns:
817 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
818 * aligned or are the same.
819 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
820 * due to insuffient page table memory.
821 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
822 * the caller.
823 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100824 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100825
826struct ffa_value api_vm_configure_pages(
827 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
828 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
829 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100830{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100831 struct ffa_value ret;
832 paddr_t pa_send_begin;
833 paddr_t pa_send_end;
834 paddr_t pa_recv_begin;
835 paddr_t pa_recv_end;
836 uint32_t orig_send_mode;
837 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200838 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100839
840 /* We only allow these to be setup once. */
841 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
842 ret = ffa_error(FFA_DENIED);
843 goto out;
844 }
845
846 /* Hafnium only supports a fixed size of RX/TX buffers. */
847 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
848 ret = ffa_error(FFA_INVALID_PARAMETERS);
849 goto out;
850 }
851
852 /* Fail if addresses are not page-aligned. */
853 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
854 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
855 ret = ffa_error(FFA_INVALID_PARAMETERS);
856 goto out;
857 }
858
859 /* Convert to physical addresses. */
860 pa_send_begin = pa_from_ipa(send);
861 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
862 pa_recv_begin = pa_from_ipa(recv);
863 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
864
865 /* Fail if the same page is used for the send and receive pages. */
866 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
867 ret = ffa_error(FFA_INVALID_PARAMETERS);
868 goto out;
869 }
Andrew Sculle1322792019-07-01 17:46:10 +0100870
871 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100872 * Ensure the pages are valid, owned and exclusive to the VM and that
873 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100874 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100875 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
876 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
877 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
878 (orig_send_mode & MM_MODE_R) == 0 ||
879 (orig_send_mode & MM_MODE_W) == 0) {
880 ret = ffa_error(FFA_DENIED);
881 goto out;
882 }
883
884 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
885 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
886 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
887 (orig_recv_mode & MM_MODE_R) == 0) {
888 ret = ffa_error(FFA_DENIED);
889 goto out;
890 }
Andrew Sculle1322792019-07-01 17:46:10 +0100891
892 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000893 if (!vm_identity_map(
894 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100895 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100896 local_page_pool, NULL)) {
897 ret = ffa_error(FFA_NO_MEMORY);
898 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100899 }
900
Andrew Scull3c257452019-11-26 13:32:50 +0000901 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
902 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100903 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100904 /* TODO: partial defrag of failed range. */
905 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100906 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100907 goto fail_undo_send;
908 }
909
Olivier Deprez96a2a262020-06-11 17:21:38 +0200910 /* Get extra send/recv pages mapping attributes for the given VM ID. */
911 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
912
Manish Pandeyd34f8892020-06-19 17:41:07 +0100913 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
914 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200915 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100916 goto fail_undo_send_and_recv;
917 }
918
Manish Pandeyd34f8892020-06-19 17:41:07 +0100919 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100920 goto out;
921
Andrew Sculle1322792019-07-01 17:46:10 +0100922fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000923 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100924 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100925
926fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000927 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100928 orig_send_mode, local_page_pool, NULL));
929 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +0100930
931out:
Andrew Sculle1322792019-07-01 17:46:10 +0100932 return ret;
933}
934
935/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100936 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +0100937 * must not be shared. Locking of the page tables combined with a local memory
938 * pool ensures there will always be enough memory to recover from any errors
939 * that arise. The stage-1 page tables must be locked so memory cannot be taken
940 * by another core which could result in this transaction being unable to roll
941 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000942 *
943 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000945 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000947 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000949 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 * - FFA_SUCCESS on success if no further action is needed.
951 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000952 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100953 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100954struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
955 uint32_t page_count, struct vcpu *current,
956 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100957{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100958 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100959 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100960 struct vm_locked vm_locked;
961 struct mm_stage1_locked mm_stage1_locked;
962 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +0000963
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100964 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100965 * Create a local pool so any freed memory can't be used by another
966 * thread. This is to ensure the original mapping can be restored if any
967 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100968 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100969 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
970
Andrew Sculle1322792019-07-01 17:46:10 +0100971 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +0100972 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +0000973
Manish Pandeyd34f8892020-06-19 17:41:07 +0100974 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
975 page_count, &local_page_pool);
976 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000977 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100978 }
979
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000980 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100981 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000982
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100983exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +0100984 mpool_fini(&local_page_pool);
985
986 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +0100987 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100988
989 return ret;
990}
991
992/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100993 * Notifies the `to` VM about the message currently in its mailbox, possibly
994 * with the help of the primary VM.
995 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
997 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100998{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100999 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1000 struct ffa_value primary_ret = {
1001 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001002 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001003 };
1004
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001005 /* Messages for the primary VM are delivered directly. */
1006 if (to.vm->id == HF_PRIMARY_VM_ID) {
1007 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001008 * Only tell the primary VM the size and other details if the
1009 * message is for it, to avoid leaking data about messages for
1010 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001011 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001012 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001013
1014 to.vm->mailbox.state = MAILBOX_STATE_READ;
1015 *next = api_switch_to_primary(current, primary_ret,
1016 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001017 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001018 }
1019
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001020 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1021
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001022 /* Messages for the TEE are sent on via the dispatcher. */
1023 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001025
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001026 ret = arch_tee_call(call);
1027 /*
1028 * After the call to the TEE completes it must have finished
1029 * reading its RX buffer, so it is ready for another message.
1030 */
1031 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001032 /*
1033 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001035 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001036 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001037 }
1038
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001039 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001040 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001041 *next = api_switch_to_primary(current, primary_ret,
1042 VCPU_STATE_READY);
1043 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001044
1045 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001046}
1047
1048/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001049 * Copies data from the sender's send buffer to the recipient's receive buffer
1050 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001051 *
1052 * If the recipient's receive buffer is busy, it can optionally register the
1053 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001054 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1056 ffa_vm_id_t receiver_vm_id, uint32_t size,
1057 uint32_t attributes, struct vcpu *current,
1058 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001059{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001060 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001062 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001063 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001064 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001065 struct vcpu_locked current_locked;
1066 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001067 bool notify =
1068 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001069
Andrew Walbran70bc8622019-10-07 14:15:58 +01001070 /* Ensure sender VM ID corresponds to the current VM. */
1071 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001073 }
1074
1075 /* Disallow reflexive requests as this suggests an error in the VM. */
1076 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001077 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001078 }
1079
1080 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001081 if (size > FFA_MSG_PAYLOAD_MAX) {
1082 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001083 }
1084
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001085 /*
1086 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1087 * invocation.
1088 */
1089 current_locked = vcpu_lock(current);
1090 is_direct_request_ongoing =
1091 is_ffa_direct_msg_request_ongoing(current_locked);
1092 vcpu_unlock(&current_locked);
1093
1094 if (is_direct_request_ongoing) {
1095 return ffa_error(FFA_DENIED);
1096 }
1097
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001098 /* Ensure the receiver VM exists. */
1099 to = vm_find(receiver_vm_id);
1100 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001101 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001102 }
1103
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001104 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001105 * Check that the sender has configured its send buffer. If the tx
1106 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1107 * be safely accessed after releasing the lock since the tx mailbox
1108 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001109 */
1110 sl_lock(&from->lock);
1111 from_msg = from->mailbox.send;
1112 sl_unlock(&from->lock);
1113
1114 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001116 }
1117
Andrew Walbran82d6d152019-12-24 15:02:06 +00001118 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001119
Andrew Walbran82d6d152019-12-24 15:02:06 +00001120 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001121 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001122 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001123 }
1124
Andrew Walbran82d6d152019-12-24 15:02:06 +00001125 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001126 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001127 to->mailbox.recv_size = size;
1128 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001130 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001131
1132out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001133 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001134
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001135 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001136}
1137
1138/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001139 * Checks whether the vCPU's attempt to block for a message has already been
1140 * interrupted or whether it is allowed to block.
1141 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001142bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001143{
1144 bool interrupted;
1145
1146 sl_lock(&current->lock);
1147
1148 /*
1149 * Don't block if there are enabled and pending interrupts, to match
1150 * behaviour of wait_for_interrupt.
1151 */
1152 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1153
1154 sl_unlock(&current->lock);
1155
1156 return interrupted;
1157}
1158
1159/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001160 * Receives a message from the mailbox. If one isn't available, this function
1161 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001162 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001163 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001164 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001165struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1166 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001167{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001168 bool is_direct_request_ongoing;
1169 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001170 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001171 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001172
Andrew Scullaa039b32018-10-04 15:02:26 +01001173 /*
1174 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001175 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001176 */
Andrew Scull19503262018-09-20 14:48:39 +01001177 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001179 }
1180
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001181 /*
1182 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1183 * invocation.
1184 */
1185 current_locked = vcpu_lock(current);
1186 is_direct_request_ongoing =
1187 is_ffa_direct_msg_request_ongoing(current_locked);
1188 vcpu_unlock(&current_locked);
1189
1190 if (is_direct_request_ongoing) {
1191 return ffa_error(FFA_DENIED);
1192 }
1193
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001194 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001195
Andrew Scullaa039b32018-10-04 15:02:26 +01001196 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001197 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1198 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001200 goto out;
1201 }
1202
1203 /* No pending message so fail if not allowed to block. */
1204 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001205 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001206 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001207 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001208
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001209 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001210 * From this point onward this call can only be interrupted or a message
1211 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001213 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214 return_code = ffa_error(FFA_INTERRUPTED);
1215 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001216 goto out;
1217 }
1218
Fuad Tabbaed294af2019-12-20 10:43:01 +00001219 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001220 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001221 struct ffa_value run_return = {
1222 .func = FFA_MSG_WAIT_32,
1223 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001224 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001225
Andrew Walbranb4816552018-12-05 17:35:42 +00001226 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001227 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001228 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001229out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001230 sl_unlock(&vm->lock);
1231
Jose Marinho3e2442f2019-03-12 13:30:37 +00001232 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001233}
1234
1235/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001236 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1237 * by this function, the caller must have called api_mailbox_send before with
1238 * the notify argument set to true, and this call must have failed because the
1239 * mailbox was not available.
1240 *
1241 * It should be called repeatedly to retrieve a list of VMs.
1242 *
1243 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1244 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001245 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001246int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001247{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001248 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001249 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001250 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001251
1252 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001253 if (list_empty(&vm->mailbox.ready_list)) {
1254 ret = -1;
1255 goto exit;
1256 }
1257
1258 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1259 ready_links);
1260 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001261 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001262
1263exit:
1264 sl_unlock(&vm->lock);
1265 return ret;
1266}
1267
1268/**
1269 * Retrieves the next VM waiting to be notified that the mailbox of the
1270 * specified VM became writable. Only primary VMs are allowed to call this.
1271 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001272 * Returns -1 on failure or if there are no waiters; the VM id of the next
1273 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001274 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001275int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001276{
1277 struct vm *vm;
1278 struct vm_locked locked;
1279 struct wait_entry *entry;
1280 struct vm *waiting_vm;
1281
1282 /* Only primary VMs are allowed to call this function. */
1283 if (current->vm->id != HF_PRIMARY_VM_ID) {
1284 return -1;
1285 }
1286
Andrew Walbran42347a92019-05-09 13:59:03 +01001287 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001288 if (vm == NULL) {
1289 return -1;
1290 }
1291
Fuad Tabbaed294af2019-12-20 10:43:01 +00001292 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001293 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001294 entry = api_fetch_waiter(locked);
1295 vm_unlock(&locked);
1296
1297 if (entry == NULL) {
1298 return -1;
1299 }
1300
1301 /* Enqueue notification to waiting VM. */
1302 waiting_vm = entry->waiting_vm;
1303
1304 sl_lock(&waiting_vm->lock);
1305 if (list_empty(&entry->ready_links)) {
1306 list_append(&waiting_vm->mailbox.ready_list,
1307 &entry->ready_links);
1308 }
1309 sl_unlock(&waiting_vm->lock);
1310
1311 return waiting_vm->id;
1312}
1313
1314/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001315 * Releases the caller's mailbox so that a new message can be received. The
1316 * caller must have copied out all data they wish to preserve as new messages
1317 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001318 *
1319 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001320 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1321 * - FFA_SUCCESS on success if no further action is needed.
1322 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001323 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001324 * hf_mailbox_waiter_get.
1325 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001326struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001327{
1328 struct vm *vm = current->vm;
1329 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001331
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001332 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001333 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001334 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001335 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001336 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001337 break;
1338
Andrew Sculld6ee1102019-04-05 22:12:42 +01001339 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001340 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001341 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001342 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001343 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001344 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001345
1346 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001347}
Andrew Walbran318f5732018-11-20 16:23:42 +00001348
1349/**
1350 * Enables or disables a given interrupt ID for the calling vCPU.
1351 *
1352 * Returns 0 on success, or -1 if the intid is invalid.
1353 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001354int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001355{
1356 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001357 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001358
Andrew Walbran318f5732018-11-20 16:23:42 +00001359 if (intid >= HF_NUM_INTIDS) {
1360 return -1;
1361 }
1362
1363 sl_lock(&current->lock);
1364 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001365 /*
1366 * If it is pending and was not enabled before, increment the
1367 * count.
1368 */
1369 if (current->interrupts.interrupt_pending[intid_index] &
1370 ~current->interrupts.interrupt_enabled[intid_index] &
1371 intid_mask) {
1372 current->interrupts.enabled_and_pending_count++;
1373 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001374 current->interrupts.interrupt_enabled[intid_index] |=
1375 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001376 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001377 /*
1378 * If it is pending and was enabled before, decrement the count.
1379 */
1380 if (current->interrupts.interrupt_pending[intid_index] &
1381 current->interrupts.interrupt_enabled[intid_index] &
1382 intid_mask) {
1383 current->interrupts.enabled_and_pending_count--;
1384 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001385 current->interrupts.interrupt_enabled[intid_index] &=
1386 ~intid_mask;
1387 }
1388
1389 sl_unlock(&current->lock);
1390 return 0;
1391}
1392
1393/**
1394 * Returns the ID of the next pending interrupt for the calling vCPU, and
1395 * acknowledges it (i.e. marks it as no longer pending). Returns
1396 * HF_INVALID_INTID if there are no pending interrupts.
1397 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001398uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001399{
1400 uint8_t i;
1401 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001402
1403 /*
1404 * Find the first enabled and pending interrupt ID, return it, and
1405 * deactivate it.
1406 */
1407 sl_lock(&current->lock);
1408 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1409 uint32_t enabled_and_pending =
1410 current->interrupts.interrupt_enabled[i] &
1411 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001412
Andrew Walbran318f5732018-11-20 16:23:42 +00001413 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001414 uint8_t bit_index = ctz(enabled_and_pending);
1415 /*
1416 * Mark it as no longer pending and decrement the count.
1417 */
1418 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001419 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001420 current->interrupts.enabled_and_pending_count--;
1421 first_interrupt =
1422 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001423 break;
1424 }
1425 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001426
1427 sl_unlock(&current->lock);
1428 return first_interrupt;
1429}
1430
1431/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001432 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001433 * given VM and vCPU.
1434 */
1435static inline bool is_injection_allowed(uint32_t target_vm_id,
1436 struct vcpu *current)
1437{
1438 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001439
Andrew Walbran318f5732018-11-20 16:23:42 +00001440 /*
1441 * The primary VM is allowed to inject interrupts into any VM. Secondary
1442 * VMs are only allowed to inject interrupts into their own vCPUs.
1443 */
1444 return current_vm_id == HF_PRIMARY_VM_ID ||
1445 current_vm_id == target_vm_id;
1446}
1447
1448/**
1449 * Injects a virtual interrupt of the given ID into the given target vCPU.
1450 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1451 * when the vCPU is next run, which is up to the scheduler.
1452 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001453 * Returns:
1454 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1455 * ID is invalid, or the current VM is not allowed to inject interrupts to
1456 * the target VM.
1457 * - 0 on success if no further action is needed.
1458 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1459 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001460 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1462 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001463 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001464{
Andrew Walbran318f5732018-11-20 16:23:42 +00001465 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001466 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001467
1468 if (intid >= HF_NUM_INTIDS) {
1469 return -1;
1470 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001471
Andrew Walbran318f5732018-11-20 16:23:42 +00001472 if (target_vm == NULL) {
1473 return -1;
1474 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001475
Andrew Walbran318f5732018-11-20 16:23:42 +00001476 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001477 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001478 return -1;
1479 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001480
Andrew Walbran318f5732018-11-20 16:23:42 +00001481 if (!is_injection_allowed(target_vm_id, current)) {
1482 return -1;
1483 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001484
Andrew Walbrane1310df2019-04-29 17:28:28 +01001485 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001486
Andrew Walbran17eebf92020-02-05 16:35:49 +00001487 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1488 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1489 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001490 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001491}
Andrew Scull6386f252018-12-06 13:29:10 +00001492
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001493/** Returns the version of the implemented FF-A specification. */
1494struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001495{
1496 /*
1497 * Ensure that both major and minor revision representation occupies at
1498 * most 15 bits.
1499 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001500 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001501 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001502 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001503 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001504 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001505 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001506 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001507 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001508
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001509 return (struct ffa_value){
1510 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1511 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001512}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001513
1514int64_t api_debug_log(char c, struct vcpu *current)
1515{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001516 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001517 struct vm *vm = current->vm;
1518 struct vm_locked vm_locked = vm_lock(vm);
1519
Andrew Sculld54e1be2019-08-20 11:09:42 +01001520 if (c == '\n' || c == '\0') {
1521 flush = true;
1522 } else {
1523 vm->log_buffer[vm->log_buffer_length++] = c;
1524 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1525 }
1526
1527 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001528 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1529 vm->log_buffer_length);
1530 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001531 }
1532
1533 vm_unlock(&vm_locked);
1534
1535 return 0;
1536}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001537
1538/**
1539 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001541 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001543{
1544 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001545 case FFA_ERROR_32:
1546 case FFA_SUCCESS_32:
1547 case FFA_INTERRUPT_32:
1548 case FFA_VERSION_32:
1549 case FFA_FEATURES_32:
1550 case FFA_RX_RELEASE_32:
1551 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001552 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001553 case FFA_ID_GET_32:
1554 case FFA_MSG_POLL_32:
1555 case FFA_MSG_WAIT_32:
1556 case FFA_YIELD_32:
1557 case FFA_RUN_32:
1558 case FFA_MSG_SEND_32:
1559 case FFA_MEM_DONATE_32:
1560 case FFA_MEM_LEND_32:
1561 case FFA_MEM_SHARE_32:
1562 case FFA_MEM_RETRIEVE_REQ_32:
1563 case FFA_MEM_RETRIEVE_RESP_32:
1564 case FFA_MEM_RELINQUISH_32:
1565 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001566 case FFA_MSG_SEND_DIRECT_RESP_32:
1567 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001568 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001569 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001570 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001571 }
1572}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001573
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001574/**
1575 * Get target VM vCPU for direct messaging request.
1576 * If VM is UP then return first vCPU.
1577 * If VM is MP then return vCPU whose index matches current CPU index.
1578 */
1579static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1580 struct vm *vm, struct vcpu *current)
1581{
1582 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1583 struct vcpu *vcpu = NULL;
1584
1585 if (vm->vcpu_count == 1) {
1586 vcpu = vm_get_vcpu(vm, 0);
1587 } else if (current_cpu_index < vm->vcpu_count) {
1588 vcpu = vm_get_vcpu(vm, current_cpu_index);
1589 }
1590
1591 return vcpu;
1592}
1593
1594/**
1595 * Send an FF-A direct message request.
1596 */
1597struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1598 ffa_vm_id_t receiver_vm_id,
1599 struct ffa_value args,
1600 struct vcpu *current,
1601 struct vcpu **next)
1602{
1603 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
1604 ffa_vm_id_t current_vm_id = current->vm->id;
1605 struct vm *receiver_vm;
1606 struct vcpu *receiver_vcpu;
1607 struct two_vcpu_locked vcpus_locked;
1608
1609 /* Only allow primary VM to send direct message requests. */
1610 if (current_vm_id != HF_PRIMARY_VM_ID) {
1611 return ffa_error(FFA_NOT_SUPPORTED);
1612 }
1613
1614 /* Prevent sender_vm_id spoofing. */
1615 if (current_vm_id != sender_vm_id) {
1616 return ffa_error(FFA_INVALID_PARAMETERS);
1617 }
1618
1619 /* Prevent a VM from sending messages to itself. */
1620 if (current_vm_id == receiver_vm_id) {
1621 return ffa_error(FFA_INVALID_PARAMETERS);
1622 }
1623
1624 receiver_vm = vm_find(receiver_vm_id);
1625 if (receiver_vm == NULL) {
1626 return ffa_error(FFA_INVALID_PARAMETERS);
1627 }
1628
1629 /*
1630 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1631 * UP (migratable) or MP partitions with a number of vCPUs matching the
1632 * number of PEs in the system. It further states that MP partitions
1633 * accepting direct request messages cannot migrate.
1634 */
1635 receiver_vcpu =
1636 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1637 if (receiver_vcpu == NULL) {
1638 return ffa_error(FFA_INVALID_PARAMETERS);
1639 }
1640
1641 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1642
1643 /*
1644 * If destination vCPU is executing or already received an
1645 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1646 * busy. There is a brief period of time where the vCPU state has
1647 * changed but regs_available is still false thus consider this case as
1648 * the vCPU not yet ready to receive a direct message request.
1649 */
1650 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1651 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1652 !receiver_vcpu->regs_available) {
1653 ret = ffa_error(FFA_BUSY);
1654 goto out;
1655 }
1656
1657 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1658 memory_order_relaxed)) {
1659 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
1660 dlog_notice("Aborting VM %u vCPU %u\n",
1661 receiver_vcpu->vm->id,
1662 vcpu_index(receiver_vcpu));
1663 receiver_vcpu->state = VCPU_STATE_ABORTED;
1664 }
1665
1666 ret = ffa_error(FFA_ABORTED);
1667 goto out;
1668 }
1669
1670 switch (receiver_vcpu->state) {
1671 case VCPU_STATE_OFF:
1672 case VCPU_STATE_RUNNING:
1673 case VCPU_STATE_ABORTED:
1674 case VCPU_STATE_READY:
1675 case VCPU_STATE_BLOCKED_INTERRUPT:
1676 ret = ffa_error(FFA_BUSY);
1677 goto out;
1678 case VCPU_STATE_BLOCKED_MAILBOX:
1679 /*
1680 * Expect target vCPU to be blocked after having called
1681 * ffa_msg_wait or sent a direct message response.
1682 */
1683 break;
1684 }
1685
1686 /* Inject timer interrupt if any pending */
1687 if (arch_timer_pending(&receiver_vcpu->regs)) {
1688 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1689 HF_VIRTUAL_TIMER_INTID,
1690 current, NULL);
1691
1692 arch_timer_mask(&receiver_vcpu->regs);
1693 }
1694
1695 /* The receiver vCPU runs upon direct message invocation */
1696 receiver_vcpu->cpu = current->cpu;
1697 receiver_vcpu->state = VCPU_STATE_RUNNING;
1698 receiver_vcpu->regs_available = false;
1699 receiver_vcpu->direct_request_origin_vm_id = current_vm_id;
1700
1701 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1702 .func = args.func,
1703 .arg1 = args.arg1,
1704 .arg2 = 0,
1705 .arg3 = args.arg3,
1706 .arg4 = args.arg4,
1707 .arg5 = args.arg5,
1708 .arg6 = args.arg6,
1709 .arg7 = args.arg7,
1710 });
1711
1712 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1713
1714 /* Switch to receiver vCPU targeted to by direct msg request */
1715 *next = receiver_vcpu;
1716
1717 /*
1718 * Since this flow will lead to a VM switch, the return value will not
1719 * be applied to current vCPU.
1720 */
1721
1722out:
1723 sl_unlock(&receiver_vcpu->lock);
1724 sl_unlock(&current->lock);
1725
1726 return ret;
1727}
1728
1729/**
1730 * Send an FF-A direct message response.
1731 */
1732struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1733 ffa_vm_id_t receiver_vm_id,
1734 struct ffa_value args,
1735 struct vcpu *current,
1736 struct vcpu **next)
1737{
1738 ffa_vm_id_t current_vm_id = current->vm->id;
1739 struct vcpu_locked vcpu_locked;
1740
1741 /* Only allow secondary VMs to send direct message responses. */
1742 if (current_vm_id == HF_PRIMARY_VM_ID) {
1743 return ffa_error(FFA_NOT_SUPPORTED);
1744 }
1745
1746 /* Prevent sender_vm_id spoofing. */
1747 if (current_vm_id != sender_vm_id) {
1748 return ffa_error(FFA_INVALID_PARAMETERS);
1749 }
1750
1751 /* Prevent a VM from sending messages to itself. */
1752 if (current_vm_id == receiver_vm_id) {
1753 return ffa_error(FFA_INVALID_PARAMETERS);
1754 }
1755
1756 vcpu_locked = vcpu_lock(current);
1757
1758 /*
1759 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1760 * defined originator.
1761 */
1762 if (!is_ffa_direct_msg_request_ongoing(vcpu_locked)) {
1763 /*
1764 * Sending direct response but direct request origin vCPU is
1765 * not set.
1766 */
1767 vcpu_unlock(&vcpu_locked);
1768 return ffa_error(FFA_DENIED);
1769 }
1770
1771 if (current->direct_request_origin_vm_id != receiver_vm_id) {
1772 vcpu_unlock(&vcpu_locked);
1773 return ffa_error(FFA_DENIED);
1774 }
1775
1776 /* Clear direct request origin for the caller. */
1777 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1778
1779 vcpu_unlock(&vcpu_locked);
1780
1781 *next = api_switch_to_primary(current,
1782 (struct ffa_value){
1783 .func = args.func,
1784 .arg1 = args.arg1,
1785 .arg2 = 0,
1786 .arg3 = args.arg3,
1787 .arg4 = args.arg4,
1788 .arg5 = args.arg5,
1789 .arg6 = args.arg6,
1790 .arg7 = args.arg7,
1791 },
1792 VCPU_STATE_BLOCKED_MAILBOX);
1793
1794 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1795}
1796
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001797struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1798 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001799 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001800{
1801 struct vm *from = current->vm;
1802 struct vm *to;
1803 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001804 struct ffa_memory_region *memory_region;
1805 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001806
1807 if (ipa_addr(address) != 0 || page_count != 0) {
1808 /*
1809 * Hafnium only supports passing the descriptor in the TX
1810 * mailbox.
1811 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001812 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001813 }
1814
Andrew Walbranca808b12020-05-15 17:22:28 +01001815 if (fragment_length > length) {
1816 dlog_verbose(
1817 "Fragment length %d greater than total length %d.\n",
1818 fragment_length, length);
1819 return ffa_error(FFA_INVALID_PARAMETERS);
1820 }
1821 if (fragment_length < sizeof(struct ffa_memory_region) +
1822 sizeof(struct ffa_memory_access)) {
1823 dlog_verbose(
1824 "Initial fragment length %d smaller than header size "
1825 "%d.\n",
1826 fragment_length,
1827 sizeof(struct ffa_memory_region) +
1828 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001829 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001830 }
1831
1832 /*
1833 * Check that the sender has configured its send buffer. If the TX
1834 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1835 * be safely accessed after releasing the lock since the TX mailbox
1836 * address can only be configured once.
1837 */
1838 sl_lock(&from->lock);
1839 from_msg = from->mailbox.send;
1840 sl_unlock(&from->lock);
1841
1842 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001843 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001844 }
1845
1846 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001847 * Copy the memory region descriptor to a fresh page from the memory
1848 * pool. This prevents the sender from changing it underneath us, and
1849 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001850 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001851 if (fragment_length > HF_MAILBOX_SIZE ||
1852 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001853 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001854 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001855 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001856 if (memory_region == NULL) {
1857 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001858 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001859 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001860 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001861
1862 /* The sender must match the caller. */
1863 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001864 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001865 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001866 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001867 }
1868
Andrew Walbrana65a1322020-04-06 19:32:32 +01001869 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001870 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001871 dlog_verbose(
1872 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001873 "endpoint memory access descriptors, expected 1).\n",
1874 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001875 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001876 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001877 }
1878
1879 /*
1880 * Ensure that the receiver VM exists and isn't the same as the sender.
1881 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001882 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001883 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001884 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001885 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001886 goto out;
1887 }
1888
1889 if (to->id == HF_TEE_VM_ID) {
1890 /*
1891 * The 'to' VM lock is only needed in the case that it is the
1892 * TEE VM.
1893 */
1894 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1895
1896 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001897 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001898 goto out_unlock;
1899 }
1900
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001901 ret = ffa_memory_tee_send(
1902 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1903 length, fragment_length, share_func, &api_page_pool);
1904 /*
1905 * ffa_tee_memory_send takes ownership of the memory_region, so
1906 * make sure we don't free it.
1907 */
1908 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001909
1910 out_unlock:
1911 vm_unlock(&vm_to_from_lock.vm1);
1912 vm_unlock(&vm_to_from_lock.vm2);
1913 } else {
1914 struct vm_locked from_locked = vm_lock(from);
1915
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001916 ret = ffa_memory_send(from_locked, memory_region, length,
1917 fragment_length, share_func,
1918 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001919 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001920 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001921 * make sure we don't free it.
1922 */
1923 memory_region = NULL;
1924
1925 vm_unlock(&from_locked);
1926 }
1927
1928out:
1929 if (memory_region != NULL) {
1930 mpool_free(&api_page_pool, memory_region);
1931 }
1932
1933 return ret;
1934}
1935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001936struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1937 uint32_t fragment_length,
1938 ipaddr_t address, uint32_t page_count,
1939 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001940{
1941 struct vm *to = current->vm;
1942 struct vm_locked to_locked;
1943 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001944 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001945 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001946 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001947
1948 if (ipa_addr(address) != 0 || page_count != 0) {
1949 /*
1950 * Hafnium only supports passing the descriptor in the TX
1951 * mailbox.
1952 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001953 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001954 }
1955
Andrew Walbrana65a1322020-04-06 19:32:32 +01001956 if (fragment_length != length) {
1957 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001958 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001959 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001960
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001961 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001962 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001963 message_buffer_size = cpu_get_buffer_size(current->cpu);
1964 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1965 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001966 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001967 }
1968
1969 to_locked = vm_lock(to);
1970 to_msg = to->mailbox.send;
1971
1972 if (to_msg == NULL) {
1973 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001974 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001975 goto out;
1976 }
1977
1978 /*
1979 * Copy the retrieve request descriptor to an internal buffer, so that
1980 * the caller can't change it underneath us.
1981 */
1982 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1983
1984 if (msg_receiver_busy(to_locked, NULL, false)) {
1985 /*
1986 * Can't retrieve memory information if the mailbox is not
1987 * available.
1988 */
1989 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001990 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001991 goto out;
1992 }
1993
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001994 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1995 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001996
1997out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001998 vm_unlock(&to_locked);
1999 return ret;
2000}
2001
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002002struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002003{
2004 struct vm *from = current->vm;
2005 struct vm_locked from_locked;
2006 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002007 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002008 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002009 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002010 uint32_t length;
2011
2012 from_locked = vm_lock(from);
2013 from_msg = from->mailbox.send;
2014
2015 if (from_msg == NULL) {
2016 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002017 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002018 goto out;
2019 }
2020
2021 /*
2022 * Calculate length from relinquish descriptor before copying. We will
2023 * check again later to make sure it hasn't changed.
2024 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002025 length = sizeof(struct ffa_mem_relinquish) +
2026 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2027 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002028 /*
2029 * Copy the relinquish descriptor to an internal buffer, so that the
2030 * caller can't change it underneath us.
2031 */
2032 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002033 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002034 message_buffer_size = cpu_get_buffer_size(current->cpu);
2035 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2036 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002037 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002038 goto out;
2039 }
2040 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2041
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002042 if (sizeof(struct ffa_mem_relinquish) +
2043 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002044 length) {
2045 dlog_verbose(
2046 "Endpoint count changed while copying to internal "
2047 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002048 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002049 goto out;
2050 }
2051
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002052 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2053 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002054
2055out:
2056 vm_unlock(&from_locked);
2057 return ret;
2058}
2059
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002060struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2061 ffa_memory_region_flags_t flags,
2062 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002063{
2064 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002065 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002066
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002067 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2068 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002069 struct vm_locked to_locked = vm_lock(to);
2070
Andrew Walbranca808b12020-05-15 17:22:28 +01002071 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002072 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002073
Andrew Walbran290b0c92020-02-03 16:37:14 +00002074 vm_unlock(&to_locked);
2075 } else {
2076 struct vm *from = vm_find(HF_TEE_VM_ID);
2077 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2078
Andrew Walbranca808b12020-05-15 17:22:28 +01002079 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2080 vm_to_from_lock.vm2, handle, flags,
2081 &api_page_pool);
2082
2083 vm_unlock(&vm_to_from_lock.vm1);
2084 vm_unlock(&vm_to_from_lock.vm2);
2085 }
2086
2087 return ret;
2088}
2089
2090struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2091 uint32_t fragment_offset,
2092 ffa_vm_id_t sender_vm_id,
2093 struct vcpu *current)
2094{
2095 struct vm *to = current->vm;
2096 struct vm_locked to_locked;
2097 struct ffa_value ret;
2098
2099 /* Sender ID MBZ at virtual instance. */
2100 if (sender_vm_id != 0) {
2101 return ffa_error(FFA_INVALID_PARAMETERS);
2102 }
2103
2104 to_locked = vm_lock(to);
2105
2106 if (msg_receiver_busy(to_locked, NULL, false)) {
2107 /*
2108 * Can't retrieve memory information if the mailbox is not
2109 * available.
2110 */
2111 dlog_verbose("RX buffer not ready.\n");
2112 ret = ffa_error(FFA_BUSY);
2113 goto out;
2114 }
2115
2116 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2117 &api_page_pool);
2118
2119out:
2120 vm_unlock(&to_locked);
2121 return ret;
2122}
2123
2124struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2125 uint32_t fragment_length,
2126 ffa_vm_id_t sender_vm_id,
2127 struct vcpu *current)
2128{
2129 struct vm *from = current->vm;
2130 const void *from_msg;
2131 void *fragment_copy;
2132 struct ffa_value ret;
2133
2134 /* Sender ID MBZ at virtual instance. */
2135 if (sender_vm_id != 0) {
2136 return ffa_error(FFA_INVALID_PARAMETERS);
2137 }
2138
2139 /*
2140 * Check that the sender has configured its send buffer. If the TX
2141 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2142 * be safely accessed after releasing the lock since the TX mailbox
2143 * address can only be configured once.
2144 */
2145 sl_lock(&from->lock);
2146 from_msg = from->mailbox.send;
2147 sl_unlock(&from->lock);
2148
2149 if (from_msg == NULL) {
2150 return ffa_error(FFA_INVALID_PARAMETERS);
2151 }
2152
2153 /*
2154 * Copy the fragment to a fresh page from the memory pool. This prevents
2155 * the sender from changing it underneath us, and also lets us keep it
2156 * around in the share state table if needed.
2157 */
2158 if (fragment_length > HF_MAILBOX_SIZE ||
2159 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2160 dlog_verbose(
2161 "Fragment length %d larger than mailbox size %d.\n",
2162 fragment_length, HF_MAILBOX_SIZE);
2163 return ffa_error(FFA_INVALID_PARAMETERS);
2164 }
2165 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2166 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2167 0) {
2168 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2169 return ffa_error(FFA_INVALID_PARAMETERS);
2170 }
2171 fragment_copy = mpool_alloc(&api_page_pool);
2172 if (fragment_copy == NULL) {
2173 dlog_verbose("Failed to allocate fragment copy.\n");
2174 return ffa_error(FFA_NO_MEMORY);
2175 }
2176 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2177
2178 /*
2179 * Hafnium doesn't support fragmentation of memory retrieve requests
2180 * (because it doesn't support caller-specified mappings, so a request
2181 * will never be larger than a single page), so this must be part of a
2182 * memory send (i.e. donate, lend or share) request.
2183 *
2184 * We can tell from the handle whether the memory transaction is for the
2185 * TEE or not.
2186 */
2187 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2188 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2189 struct vm_locked from_locked = vm_lock(from);
2190
2191 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2192 fragment_length, handle,
2193 &api_page_pool);
2194 /*
2195 * `ffa_memory_send_continue` takes ownership of the
2196 * fragment_copy, so we don't need to free it here.
2197 */
2198 vm_unlock(&from_locked);
2199 } else {
2200 struct vm *to = vm_find(HF_TEE_VM_ID);
2201 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2202
2203 /*
2204 * The TEE RX buffer state is checked in
2205 * `ffa_memory_tee_send_continue` rather than here, as we need
2206 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2207 * than FFA_ERROR FFA_BUSY in case it is busy.
2208 */
2209
2210 ret = ffa_memory_tee_send_continue(
2211 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2212 fragment_length, handle, &api_page_pool);
2213 /*
2214 * `ffa_memory_tee_send_continue` takes ownership of the
2215 * fragment_copy, so we don't need to free it here.
2216 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002217
2218 vm_unlock(&vm_to_from_lock.vm1);
2219 vm_unlock(&vm_to_from_lock.vm2);
2220 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002221
2222 return ret;
2223}