blob: 7b1fa0ad441faec0bb5aea4f8908d7ea0917f743 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Federico Recanati25053ee2022-03-14 15:01:53 +01002 * Copyright 2022 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"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020017#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
22#include "hf/ffa_memory.h"
J-Alves33c47bf2022-09-29 11:36:20 +010023#include "hf/ffa_v1_0.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010025#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010026#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000027#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010028#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010029#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010030#include "hf/vm.h"
31
Andrew Scullf35a5c92018-08-07 18:09:46 +010032#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010033#include "vmapi/hf/ffa.h"
J-Alves126ab502022-09-29 11:37:33 +010034#include "vmapi/hf/ffa_v1_0.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010035
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000036static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010037 "Partition information descriptor size doesn't match the one in "
38 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000039static_assert(sizeof(struct ffa_partition_info) == 24,
40 "Partition information descriptor size doesn't match the one in "
41 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -080042static_assert((sizeof(struct ffa_partition_info) & 7) == 0,
43 "Partition information descriptor must be a multiple of 8 bytes"
44 " for ffa_partition_info_get_regs to work correctly. Information"
45 " from this structure are returned in 8 byte registers and the"
46 " count of 8 byte registers is returned by the ABI.");
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000047/*
48 * To eliminate the risk of deadlocks, we define a partial order for the
49 * acquisition of locks held concurrently by the same physical CPU. Our current
50 * ordering requirements are as follows:
51 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010052 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000053 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010054 * Locks of the same kind require the lock of lowest address to be locked first,
55 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000056 */
57
Andrew Scullaa039b32018-10-04 15:02:26 +010058static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010059 "Currently, a page is mapped for the send and receive buffers so "
60 "the maximum request is the size of a page.");
61
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000062static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
63 "The page pool entry size must be at least as big as the mailbox "
64 "size, so that memory region descriptors can be copied from the "
65 "mailbox for memory sharing.");
66
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -080067/*
68 * Maximum ffa_partition_info entries that can be returned by an invocation
69 * of FFA_PARTITION_INFO_GET_REGS_64 is size in bytes, of available
70 * registers/args in struct ffa_value divided by size of struct
71 * ffa_partition_info. For this ABI, arg3-arg17 in ffa_value can be used, i.e.
72 * 15 uint64_t fields. For FF-A v1.1, this value should be 5.
73 */
74#define MAX_INFO_REGS_ENTRIES_PER_CALL \
75 ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info))
76static_assert(MAX_INFO_REGS_ENTRIES_PER_CALL == 5,
77 "FF-A v1.1 supports no more than 5 entries"
78 " per FFA_PARTITION_INFO_GET_REGS64 calls");
79
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000080static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000081
82/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000083 * Initialises the API page pool by taking ownership of the contents of the
84 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000085 */
86void api_init(struct mpool *ppool)
87{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000088 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000089}
90
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010091/**
J-Alvesad6a0432021-04-09 16:06:21 +010092 * Get target VM vCPU:
93 * If VM is UP then return first vCPU.
94 * If VM is MP then return vCPU whose index matches current CPU index.
95 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050096struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010097{
98 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
99 struct vcpu *vcpu = NULL;
100
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500101 CHECK((vm != NULL) && (current != NULL));
102
J-Alvesad6a0432021-04-09 16:06:21 +0100103 if (vm->vcpu_count == 1) {
104 vcpu = vm_get_vcpu(vm, 0);
105 } else if (current_cpu_index < vm->vcpu_count) {
106 vcpu = vm_get_vcpu(vm, current_cpu_index);
107 }
108
109 return vcpu;
110}
111
112/**
J-Alvesfe7f7372020-11-09 11:32:12 +0000113 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
114 * is given as argument of the function.
115 *
116 * Called to change the context between SPs for direct messaging (when Hafnium
117 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
118 *
119 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100120 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000121 * - MP with pinned Execution Contexts.
122 */
Madhukar Pappireddyb3fb8712023-05-23 17:10:34 -0500123struct vcpu *api_switch_to_vm(struct vcpu *current, struct ffa_value to_ret,
124 enum vcpu_state vcpu_state, ffa_vm_id_t to_id)
J-Alvesfe7f7372020-11-09 11:32:12 +0000125{
126 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100127 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000128
129 CHECK(next != NULL);
130
131 /* Set the return value for the target VM. */
132 arch_regs_set_retval(&next->regs, to_ret);
133
134 /* Set the current vCPU state. */
135 sl_lock(&current->lock);
136 current->state = vcpu_state;
137 sl_unlock(&current->lock);
138
139 return next;
140}
141
142/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000143 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100144 *
145 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100146 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100147 */
J-Alves27b71962022-12-12 15:29:58 +0000148struct vcpu *api_switch_to_primary(struct vcpu *current,
149 struct ffa_value primary_ret,
150 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100151{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000152 /*
153 * If the secondary is blocked but has a timer running, sleep until the
154 * timer fires rather than indefinitely.
155 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100156 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100157 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
158 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 if (arch_timer_enabled_current()) {
160 uint64_t remaining_ns =
161 arch_timer_remaining_ns_current();
162
163 if (remaining_ns == 0) {
164 /*
165 * Timer is pending, so the current vCPU should
166 * be run again right away.
167 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100168 primary_ret = (struct ffa_value){
169 .func = FFA_INTERRUPT_32};
170
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100171 } else {
172 primary_ret.arg2 = remaining_ns;
173 }
174 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100175 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100176 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000177 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100178 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000179
180 default:
181 /* Do nothing. */
182 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000183 }
184
J-Alvesfe7f7372020-11-09 11:32:12 +0000185 return api_switch_to_vm(current, primary_ret, secondary_state,
186 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100187}
188
189/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200190 * Choose next vCPU to run to be the counterpart vCPU in the other
191 * world (run the normal world if currently running in the secure
192 * world). Set current vCPU state to the given vcpu_state parameter.
193 * Set FF-A return values to the target vCPU in the other world.
194 *
195 * Called in context of a direct message response from a secure
196 * partition to a VM.
197 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100198struct vcpu *api_switch_to_other_world(struct vcpu *current,
199 struct ffa_value other_world_ret,
200 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200201{
J-Alvesfe7f7372020-11-09 11:32:12 +0000202 return api_switch_to_vm(current, other_world_ret, vcpu_state,
203 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200204}
205
206/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000207 * Returns true if the given vCPU is executing in context of an
208 * FFA_MSG_SEND_DIRECT_REQ invocation.
209 */
J-Alves27b71962022-12-12 15:29:58 +0000210bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000211{
212 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
213}
214
215/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100216 * Returns true if the VM owning the given vCPU is supporting managed exit and
217 * the vCPU is currently processing a managed exit.
218 */
219static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
220{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100221 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100222 vcpu_locked.vcpu->processing_managed_exit);
223}
224
225/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000226 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000227 */
228struct vcpu *api_preempt(struct vcpu *current)
229{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100230 struct ffa_value ret = {
231 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200232 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000233 };
234
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500235 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000236}
237
238/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000239 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000240 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100241 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100242struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100243{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100244 struct ffa_value ret = {
245 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
246 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100247 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000248
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000249 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100250 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100251}
252
253/**
Andrew Walbran33645652019-04-15 12:29:31 +0100254 * Puts the current vCPU in off mode, and returns to the primary VM.
255 */
256struct vcpu *api_vcpu_off(struct vcpu *current)
257{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100258 struct ffa_value ret = {
259 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
260 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100261 };
262
263 /*
264 * Disable the timer, so the scheduler doesn't get told to call back
265 * based on it.
266 */
267 arch_timer_disable_current();
268
269 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
270}
271
272/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500273 * The current vCPU is blocked on some resource and needs to relinquish
274 * control back to the execution context of the endpoint that originally
275 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000276 */
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500277struct ffa_value api_yield(struct vcpu *current, struct vcpu **next,
278 struct ffa_value *args)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000279{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000280 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
281 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500282 bool transition_allowed;
283 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500284 uint32_t timeout_low = 0;
285 uint32_t timeout_high = 0;
286
287 if (args != NULL) {
288 if (args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
289 args->arg7 != 0U) {
290 dlog_error(
291 "Parameters passed through registers X4-X7 "
292 "must be zero\n");
293 return ffa_error(FFA_INVALID_PARAMETERS);
294 }
295 timeout_low = (uint32_t)args->arg2 & 0xFFFFFFFF;
296 timeout_high = (uint32_t)args->arg3 & 0xFFFFFFFF;
297 }
Andrew Scull66d62bf2019-02-01 13:54:10 +0000298
299 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000300 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000301 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000302 }
303
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000304 current_locked = vcpu_lock(current);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500305 transition_allowed = plat_ffa_check_runtime_state_transition(
306 current, current->vm->id, HF_INVALID_VM_ID, NULL, FFA_YIELD_32,
307 &next_state);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000308 vcpu_unlock(&current_locked);
309
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500310 if (!transition_allowed) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000311 return ffa_error(FFA_DENIED);
312 }
313
Madhukar Pappireddy65764072023-05-23 17:18:58 -0500314 /*
315 * The current vCPU is expected to move to BLOCKED state. However,
316 * under certain circumstances, it is allowed for the current vCPU
317 * to be resumed immediately without ever moving to BLOCKED state. One
318 * such scenario occurs when an SP's execution context attempts to
319 * yield cycles while handling secure interrupt. Refer to the comments
320 * in the SPMC variant of the plat_ffa_yield_prepare function.
321 */
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500322 assert(next_state == VCPU_STATE_BLOCKED);
323
Madhukar Pappireddy184501c2023-05-23 17:24:06 -0500324 return plat_ffa_yield_prepare(current, next, timeout_low, timeout_high);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000325}
326
327/**
Andrew Walbran33645652019-04-15 12:29:31 +0100328 * Switches to the primary so that it can switch to the target, or kick it if it
329 * is already running on a different physical CPU.
330 */
331struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
332{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100333 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100334 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
336 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100337 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500338 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100339}
340
341/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000342 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000343 */
344struct vcpu *api_abort(struct vcpu *current)
345{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100346 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000347
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100348 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000349 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000350
351 if (current->vm->id == HF_PRIMARY_VM_ID) {
352 /* TODO: what to do when the primary aborts? */
353 for (;;) {
354 /* Do nothing. */
355 }
356 }
357
358 atomic_store_explicit(&current->vm->aborting, true,
359 memory_order_relaxed);
360
361 /* TODO: free resources once all vCPUs abort. */
362
Andrew Sculld6ee1102019-04-05 22:12:42 +0100363 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000364}
365
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000366/*
367 * Format the partition info descriptors according to the version supported
368 * by the endpoint and return the size of the array created.
369 */
370static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000371 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000372 uint32_t vm_count)
373{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000374 struct vm *vm = vm_locked.vm;
375 uint32_t version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100376 uint32_t partition_info_size;
377 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100378 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000379
J-Alvese8c8c2b2022-12-16 15:34:48 +0000380 /* Acquire receiver's RX buffer. */
381 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
382 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
383 return ret;
384 }
385
J-Alves122f1a12022-12-12 15:55:42 +0000386 if (vm_is_mailbox_busy(vm_locked)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000387 /*
388 * Can't retrieve memory information if the mailbox is not
389 * available.
390 */
391 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000392 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000393 }
394
Daniel Boulby191b5f02022-02-17 17:26:14 +0000395 if (version == MAKE_FFA_VERSION(1, 0)) {
396 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
397
Daniel Boulby835e1592022-05-30 17:38:51 +0100398 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
399 buffer_size = partition_info_size * vm_count;
400 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000401 dlog_error(
402 "Partition information does not fit in the "
403 "VM's RX "
404 "buffer.\n");
405 return ffa_error(FFA_NO_MEMORY);
406 }
407
408 for (uint32_t i = 0; i < vm_count; i++) {
409 /*
410 * Populate the VM's RX buffer with the partition
Kathleen Capella402fa852022-11-09 16:16:51 -0500411 * information. Clear properties bits that must be zero
412 * according to DEN0077A FF-A v1.0 REL Table 8.25.
Daniel Boulby191b5f02022-02-17 17:26:14 +0000413 */
414 recv_mailbox[i].vm_id = partitions[i].vm_id;
415 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
Kathleen Capella402fa852022-11-09 16:16:51 -0500416 recv_mailbox[i].properties =
417 partitions[i].properties &
418 ~FFA_PARTITION_v1_0_RES_MASK;
Daniel Boulby191b5f02022-02-17 17:26:14 +0000419 }
420
421 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100422 partition_info_size = sizeof(struct ffa_partition_info);
423 buffer_size = partition_info_size * vm_count;
424 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000425 dlog_error(
426 "Partition information does not fit in the "
427 "VM's RX "
428 "buffer.\n");
429 return ffa_error(FFA_NO_MEMORY);
430 }
431
432 /* Populate the VM's RX buffer with the partition information.
433 */
Daniel Boulby835e1592022-05-30 17:38:51 +0100434 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions,
435 buffer_size);
Daniel Boulby191b5f02022-02-17 17:26:14 +0000436 }
437
Daniel Boulby835e1592022-05-30 17:38:51 +0100438 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000439
440 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000441 vm->mailbox.recv_sender = HF_VM_ID_BASE;
442 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000443 vm->mailbox.state = MAILBOX_STATE_FULL;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000444
Daniel Boulby835e1592022-05-30 17:38:51 +0100445 /*
446 * Return the count of partition information descriptors in w2
447 * and the size of the descriptors in w3.
448 */
449 return (struct ffa_value){.func = FFA_SUCCESS_32,
450 .arg2 = vm_count,
451 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000452}
453
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800454static void api_ffa_fill_partitions_info_array(
455 struct ffa_partition_info *partitions, size_t partitions_len,
456 const struct ffa_uuid *uuid, bool count_flag, ffa_vm_id_t vm_id,
457 ffa_vm_count_t *vm_count_out)
458{
459 ffa_vm_count_t vm_count = 0;
460 bool uuid_is_null = ffa_uuid_is_null(uuid);
461
462 assert(vm_get_count() <= partitions_len);
463
464 /*
465 * Iterate through the VMs to find the ones with a matching
466 * UUID. A Null UUID retrieves information for all VMs.
467 */
468 for (ffa_vm_count_t index = 0; index < vm_get_count(); ++index) {
469 struct vm *vm = vm_find_index(index);
470
471 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
472 uint16_t array_index = vm_count;
473
474 ++vm_count;
475 if (count_flag) {
476 continue;
477 }
478
479 partitions[array_index].vm_id = vm->id;
480 partitions[array_index].vcpu_count = vm->vcpu_count;
481 partitions[array_index].properties =
482 plat_ffa_partition_properties(vm_id, vm);
483 partitions[array_index].properties |=
484 vm_are_notifications_enabled(vm)
485 ? FFA_PARTITION_NOTIFICATION
486 : 0;
487 partitions[array_index].properties |=
488 FFA_PARTITION_AARCH64_EXEC;
489 if (uuid_is_null) {
490 partitions[array_index].uuid = vm->uuid;
491 }
492 }
493 }
494
495 *vm_count_out = vm_count;
496}
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800497
498static inline void api_ffa_pack_vmid_count_props(
499 uint64_t *xn, ffa_vm_id_t vm_id, ffa_vcpu_count_t vcpu_count,
500 ffa_partition_properties_t properties)
501{
502 *xn = (uint64_t)vm_id;
503 *xn |= (uint64_t)vcpu_count << 16;
504 *xn |= (uint64_t)properties << 32;
505}
506
507static inline void api_ffa_pack_uuid(uint64_t *xn_1, uint64_t *xn_2,
508 struct ffa_uuid *uuid)
509{
510 *xn_1 = (uint64_t)uuid->uuid[0];
511 *xn_1 |= (uint64_t)uuid->uuid[1] << 32;
512 *xn_2 = (uint64_t)uuid->uuid[2];
513 *xn_2 |= (uint64_t)uuid->uuid[3] << 32;
514}
515
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800516struct ffa_value api_ffa_partition_info_get_regs(struct vcpu *current,
517 const struct ffa_uuid *uuid,
518 const uint16_t start_index,
519 const uint16_t tag)
520{
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800521 struct vm *current_vm = current->vm;
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800522 static struct ffa_partition_info partitions[2 * MAX_VMS];
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800523 bool uuid_is_null = ffa_uuid_is_null(uuid);
524 ffa_vm_count_t vm_count = 0;
525 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
526 uint16_t max_idx = 0;
527 uint16_t curr_idx = 0;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800528 uint8_t num_entries_to_ret = 0;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800529 uint8_t arg_idx = 3;
530
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800531 /* list of pointers to args in return value */
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800532 uint64_t *arg_ptrs[] = {
533 &(ret).func,
534 &(ret).arg1,
535 &(ret).arg2,
536 &(ret).arg3,
537 &(ret).arg4,
538 &(ret).arg5,
539 &(ret).arg6,
540 &(ret).arg7,
541 &(ret).extended_val.arg8,
542 &(ret).extended_val.arg9,
543 &(ret).extended_val.arg10,
544 &(ret).extended_val.arg11,
545 &(ret).extended_val.arg12,
546 &(ret).extended_val.arg13,
547 &(ret).extended_val.arg14,
548 &(ret).extended_val.arg15,
549 &(ret).extended_val.arg16,
550 &(ret).extended_val.arg17,
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800551 };
552
553 /* TODO: Add support for using tags */
554 if (tag != 0) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800555 dlog_error("Tag not 0. Unsupported tag. %d\n", tag);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800556 return ffa_error(FFA_RETRY);
557 }
558
559 memset_s(&partitions, sizeof(partitions), 0, sizeof(partitions));
560
561 api_ffa_fill_partitions_info_array(partitions, ARRAY_SIZE(partitions),
562 uuid, false, current_vm->id,
563 &vm_count);
564
565 /* If UUID is Null vm_count must not be zero at this stage. */
566 CHECK(!uuid_is_null || vm_count != 0);
567
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800568 /*
569 * When running the Hypervisor:
570 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
571 * it to fill with secure partitions information.
572 * TODO: Note that for this ABI, forwarding on every invocation when
573 * uuid is Null is inefficient,and if performance becomes a problem,
574 * this would be a good place to optimize using strategies such as
575 * caching info etc. For now, assuming this inefficiency is not a major
576 * issue.
577 * - If UUID is non-Null vm_count may be zero because the UUID matches
578 * a secure partition and the query is forwarded to the SPMC.
579 * When running the SPMC:
580 * - If UUID is non-Null and vm_count is zero it means there is no such
581 * partition identified in the system.
582 */
583 if (!plat_ffa_partition_info_get_regs_forward(
584 uuid, start_index, tag, partitions, ARRAY_SIZE(partitions),
585 &vm_count)) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800586 dlog_error("Failed to forward ffa_partition_info_get_regs.\n");
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800587 return ffa_error(FFA_DENIED);
588 }
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800589
590 /*
591 * Unrecognized UUID: does not match any of the VMs (or SPs)
592 * and is not Null.
593 */
Raghu Krishnamurthyef432cb2022-12-29 06:56:32 -0800594 if (vm_count == 0 || vm_count > ARRAY_SIZE(partitions)) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800595 dlog_error(
596 "Invalid parameters. vm_count = %d (must not be zero "
597 "or > %d)\n",
598 vm_count, ARRAY_SIZE(partitions));
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800599 return ffa_error(FFA_INVALID_PARAMETERS);
600 }
601
602 if (start_index >= vm_count) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800603 dlog_error(
604 "start index = %d vm_count = %d (start_index must be "
605 "less than vm_count)\n",
606 start_index, vm_count);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800607 return ffa_error(FFA_INVALID_PARAMETERS);
608 }
609
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800610 max_idx = vm_count - 1;
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800611 num_entries_to_ret = (max_idx - start_index) + 1;
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800612 num_entries_to_ret =
613 MIN(num_entries_to_ret, MAX_INFO_REGS_ENTRIES_PER_CALL);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800614 curr_idx = start_index + num_entries_to_ret - 1;
615 assert(curr_idx <= max_idx);
616
617 ret.func = FFA_SUCCESS_64;
618 ret.arg2 = (sizeof(struct ffa_partition_info) & 0xFFFF) << 48;
619 ret.arg2 |= curr_idx << 16;
620 ret.arg2 |= max_idx;
621
622 if (num_entries_to_ret > 1) {
623 ret.extended_val.valid = 1;
624 }
625
626 for (uint16_t idx = start_index; idx <= curr_idx; ++idx) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800627 uint64_t *xn_0 = arg_ptrs[arg_idx++];
628 uint64_t *xn_1 = arg_ptrs[arg_idx++];
629 uint64_t *xn_2 = arg_ptrs[arg_idx++];
630
631 api_ffa_pack_vmid_count_props(xn_0, partitions[idx].vm_id,
632 partitions[idx].vcpu_count,
633 partitions[idx].properties);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800634 if (uuid_is_null) {
Raghu Krishnamurthybefdcc82023-02-24 06:41:56 -0800635 api_ffa_pack_uuid(xn_1, xn_2, &partitions[idx].uuid);
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800636 }
637 assert(arg_idx < ARRAY_SIZE(arg_ptrs));
638 }
639
640 return ret;
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800641}
642
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100643struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000644 const struct ffa_uuid *uuid,
645 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100646{
647 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100648 ffa_vm_count_t vm_count = 0;
Olivier Deprez013f4d62022-11-21 15:46:20 +0100649 bool count_flag = (flags & FFA_PARTITION_COUNT_FLAG_MASK) ==
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000650 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100651 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100652 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000653 struct vm_locked vm_locked;
654 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100655
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000656 /* Bits 31:1 Must Be Zero */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100657 if ((flags & ~FFA_PARTITION_COUNT_FLAG_MASK) != 0) {
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000658 return ffa_error(FFA_INVALID_PARAMETERS);
659 }
660
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100661 /*
Kathleen Capella402fa852022-11-09 16:16:51 -0500662 * No need to count if we are returning the number of partitions as we
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000663 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100664 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000665 if (uuid_is_null && count_flag) {
666 vm_count = vm_get_count();
667 } else {
Raghu Krishnamurthyf67776f2022-12-26 10:10:05 -0800668 api_ffa_fill_partitions_info_array(
669 partitions, ARRAY_SIZE(partitions), uuid, count_flag,
670 current_vm->id, &vm_count);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100671 }
672
Olivier Depreze562e542020-06-11 17:31:54 +0200673 /* If UUID is Null vm_count must not be zero at this stage. */
674 CHECK(!uuid_is_null || vm_count != 0);
675
676 /*
677 * When running the Hypervisor:
678 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
679 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000680 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200681 * a secure partition and the query is forwarded to the SPMC.
682 * When running the SPMC:
683 * - If UUID is non-Null and vm_count is zero it means there is no such
684 * partition identified in the system.
685 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000686 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200687
688 /*
689 * Unrecognized UUID: does not match any of the VMs (or SPs)
690 * and is not Null.
691 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100692 if (vm_count == 0) {
693 return ffa_error(FFA_INVALID_PARAMETERS);
694 }
695
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000696 /*
697 * If the count flag is set we don't need to return the partition info
698 * descriptors.
699 */
700 if (count_flag) {
701 return (struct ffa_value){.func = FFA_SUCCESS_32,
702 .arg2 = vm_count};
703 }
704
Daniel Boulby191b5f02022-02-17 17:26:14 +0000705 vm_locked = vm_lock(current_vm);
706 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
707 vm_count);
708 vm_unlock(&vm_locked);
709 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100710}
711
Andrew Scull9726c252019-01-23 13:44:19 +0000712/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000713 * Returns the ID of the VM.
714 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100715struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000716{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100717 return (struct ffa_value){.func = FFA_SUCCESS_32,
718 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000719}
720
721/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200722 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
723 * FF-A instances.
724 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000725 */
726struct ffa_value api_ffa_spm_id_get(void)
727{
J-Alves3829fc02021-03-18 12:49:18 +0000728#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
729 /*
730 * Return the SPMC ID that was fetched during FF-A
731 * initialization.
732 */
733 return (struct ffa_value){.func = FFA_SUCCESS_32,
734 .arg2 = arch_ffa_spmc_id_get()};
735#else
736 return ffa_error(FFA_NOT_SUPPORTED);
737#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000738}
739
740/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000741 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000742 * function to indicate that register state for the given vCPU has been saved
743 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000744 */
745void api_regs_state_saved(struct vcpu *vcpu)
746{
747 sl_lock(&vcpu->lock);
748 vcpu->regs_available = true;
749 sl_unlock(&vcpu->lock);
750}
751
752/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000753 * Retrieves the next waiter and removes it from the wait list if the VM's
754 * mailbox is in a writable state.
755 */
756static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
757{
758 struct wait_entry *entry;
759 struct vm *vm = locked_vm.vm;
760
Andrew Sculld6ee1102019-04-05 22:12:42 +0100761 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000762 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
763 /* The mailbox is not writable or there are no waiters. */
764 return NULL;
765 }
766
767 /* Remove waiter from the wait list. */
768 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
769 wait_links);
770 list_remove(&entry->wait_links);
771 return entry;
772}
773
774/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000775 * Assuming that the arguments have already been checked by the caller, injects
776 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
777 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
778 * is next run, which is up to the scheduler.
779 *
780 * Returns:
781 * - 0 on success if no further action is needed.
782 * - 1 if it was called by the primary VM and the primary VM now needs to wake
783 * up or kick the target vCPU.
784 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100785int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
786 uint32_t intid, struct vcpu *current,
787 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000788{
Manish Pandey35e452f2021-02-18 21:36:34 +0000789 struct vcpu *target_vcpu = target_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100790 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000791 int64_t ret = 0;
792
Andrew Walbran508e63c2018-12-20 17:02:37 +0000793 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000794 * We only need to change state and (maybe) trigger a virtual interrupt
795 * if it is enabled and was not previously pending. Otherwise we can
796 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000797 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100798 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
799 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000800 goto out;
801 }
802
803 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100804 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000805
806 /*
807 * Only need to update state if there was not already an
808 * interrupt enabled and pending.
809 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000810 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000811 goto out;
812 }
813
Andrew Walbran508e63c2018-12-20 17:02:37 +0000814 if (current->vm->id == HF_PRIMARY_VM_ID) {
815 /*
816 * If the call came from the primary VM, let it know that it
817 * should run or kick the target vCPU.
818 */
819 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000820 } else if (current != target_vcpu && next != NULL) {
821 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000822 }
823
824out:
825 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100826 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000827
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200828 return ret;
829}
830
831/* Wrapper to internal_interrupt_inject with locking of target vCPU */
832static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
833 uint32_t intid, struct vcpu *current,
834 struct vcpu **next)
835{
836 int64_t ret;
837 struct vcpu_locked target_locked;
838
839 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100840 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200841 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000842
843 return ret;
844}
845
846/**
Federico Recanati25053ee2022-03-14 15:01:53 +0100847 * Constructs the return value from a successful FFA_MSG_POLL or
848 * FFA_MSG_WAIT call.
849 *
850 * Note: FFA_MSG_POLL is deprecated in FF-A v1.1 and should not be used with
851 * FFA_MSG_SEND2; no check is done on mixing FF-A v1.0 and FF-A v1.1 indirect
852 * message protocols.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100853 */
J-Alves27b71962022-12-12 15:29:58 +0000854struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100855{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000856 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100857 case FFA_MSG_SEND_32:
858 return (struct ffa_value){
859 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000860 .arg1 = (receiver->mailbox.recv_sender << 16) |
861 receiver->id,
862 .arg3 = receiver->mailbox.recv_size};
Federico Recanati25053ee2022-03-14 15:01:53 +0100863 case FFA_MSG_SEND2_32:
864 return (struct ffa_value){
865 .func = FFA_RUN_32,
866 /*
867 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
868 * Retrieving vCPU requires a rework of the function,
869 * while receiver ID must be set because it's checked by
870 * other APIs (eg: FFA_NOTIFICATION_GET).
871 */
872 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000873 default:
874 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000875 dlog_error("Tried to return an invalid message function %#x\n",
876 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100877 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000878 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100879}
880
J-Alvese8c8c2b2022-12-16 15:34:48 +0000881/**
882 * Change the state of mailbox to empty, such that the ownership is given to the
883 * Partition manager.
884 * Returns true if the mailbox was reset successfully, false otherwise.
885 */
886static bool api_release_mailbox(struct vm_locked vm_locked, int32_t *error_code)
887{
888 ffa_vm_id_t vm_id = vm_locked.vm->id;
889 int32_t error_code_to_ret = 0;
890 bool ret = false;
891
892 switch (vm_locked.vm->mailbox.state) {
893 case MAILBOX_STATE_EMPTY:
894 dlog_verbose("Mailbox of %x is empty.\n", vm_id);
895 error_code_to_ret = FFA_DENIED;
896 goto out;
897 case MAILBOX_STATE_FULL:
898 /* Check it doesn't have pending RX full notifications. */
899 if (vm_are_fwk_notifications_pending(vm_locked)) {
900 dlog_verbose(
901 "Mailbox of endpoint %x has pending "
902 "messages.\n",
903 vm_id);
904 error_code_to_ret = FFA_DENIED;
905 goto out;
906 }
907 break;
908 case MAILBOX_STATE_OTHER_WORLD_OWNED:
909 /*
910 * The SPMC shouldn't let SP's mailbox get into this state.
911 * For the Hypervisor, the VM may call FFA_RX_RELEASE, whilst
912 * the mailbox is in this state. In that case, we should report
913 * error.
914 */
915 if (vm_id_is_current_world(vm_id)) {
916 dlog_verbose(
917 "Mailbox of endpoint %x in a wrongful state.\n",
918 vm_id);
919 error_code_to_ret = FFA_ABORTED;
920 goto out;
921 }
922 break;
923 }
924
925 vm_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
926 ret = true;
927out:
928 if (error_code != NULL) {
929 *error_code = error_code_to_ret;
930 }
931
932 return ret;
933}
934
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600935struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
936 struct ffa_value *args)
937{
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500938 enum vcpu_state next_state = VCPU_STATE_WAITING;
J-Alvese8c8c2b2022-12-16 15:34:48 +0000939 struct ffa_value ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600940
941 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
942 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
943 args->arg7 != 0U) {
944 return ffa_error(FFA_INVALID_PARAMETERS);
945 }
946
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -0500947 if (!plat_ffa_check_runtime_state_transition(
948 current, current->vm->id, HF_INVALID_VM_ID, NULL,
949 FFA_MSG_WAIT_32, &next_state)) {
950 return ffa_error(FFA_DENIED);
951 }
952
953 assert(next_state == VCPU_STATE_WAITING);
954
J-Alvese8c8c2b2022-12-16 15:34:48 +0000955 ret = plat_ffa_msg_wait_prepare(current, next);
956
957 if (ret.func != FFA_ERROR_32) {
958 struct vm_locked vm_locked = vm_lock(current->vm);
959
960 api_release_mailbox(vm_locked, NULL);
961 vm_unlock(&vm_locked);
962 }
963
964 return ret;
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600965}
966
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100967/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000968 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000969 * value needs to be forced onto the vCPU.
970 */
J-Alves6e2abc62021-12-02 14:58:56 +0000971static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100972 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000973{
Max Shvetsov40108e72020-08-27 12:39:50 +0100974 struct vcpu_locked vcpu_locked;
975 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000976 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500977 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
Olivier Deprez04168ed2022-09-26 09:20:00 +0200978 bool vcpu_was_init_state = false;
J-Alves6e2abc62021-12-02 14:58:56 +0000979 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000980
Andrew Scullb06d1752019-02-04 10:15:48 +0000981 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000982 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000983 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100984 * The VM lock is not needed in the common case so it must only be taken
985 * when it is going to be needed. This ensures there are no inter-vCPU
986 * dependencies in the common run case meaning the sensitive context
987 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000988 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100989 vcpu_locked = vcpu_lock(vcpu);
990
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000991 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000992 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
993 (!vcpu->vm->el0_partition &&
994 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
995 vcpu->state == VCPU_STATE_BLOCKED ||
996 vcpu->state == VCPU_STATE_PREEMPTED));
997
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000998 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100999 vcpu_unlock(&vcpu_locked);
1000 vm_locked = vm_lock(vcpu->vm);
1001 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001002 }
1003
1004 /*
1005 * If the vCPU is already running somewhere then we can't run it here
1006 * simultaneously. While it is actually running then the state should be
1007 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
1008 * stops running but while Hafnium is in the process of switching back
1009 * to the primary there will be a brief period while the state has been
1010 * updated but `regs_available` is still false (until
1011 * `api_regs_state_saved` is called). We can't start running it again
1012 * until this has finished, so count this state as still running for the
1013 * purposes of this check.
1014 */
1015 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
1016 /*
1017 * vCPU is running on another pCPU.
1018 *
1019 * It's okay not to return the sleep duration here because the
1020 * other physical CPU that is currently running this vCPU will
1021 * return the sleep duration if needed.
1022 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +00001024 ret = false;
1025 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001026 }
Andrew Scull9726c252019-01-23 13:44:19 +00001027
1028 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001029 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001030 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +00001031 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +01001032 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +00001033 }
1034 ret = false;
1035 goto out;
1036 }
1037
Andrew Walbran508e63c2018-12-20 17:02:37 +00001038 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001039 case VCPU_STATE_RUNNING:
1040 case VCPU_STATE_OFF:
1041 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001042 ret = false;
1043 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001044
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001045 case VCPU_STATE_WAITING:
1046 /*
Olivier Deprezb2808332023-02-02 15:25:40 +01001047 * An initial FFA_RUN is necessary for SP's secondary vCPUs to
1048 * reach the message wait loop.
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001049 */
Olivier Deprezb2808332023-02-02 15:25:40 +01001050 if (vcpu->rt_model == RTM_SP_INIT) {
1051 /*
1052 * TODO: this should be removed, but omitting it makes
1053 * normal world arch gicv3 tests failing.
1054 */
1055 vcpu->rt_model = RTM_NONE;
Olivier Deprez04168ed2022-09-26 09:20:00 +02001056
1057 vcpu_was_init_state = true;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001058 break;
1059 }
1060
J-Alves6e2abc62021-12-02 14:58:56 +00001061 assert(need_vm_lock == true);
1062 if (!vm_locked.vm->el0_partition &&
1063 plat_ffa_inject_notification_pending_interrupt(
1064 vcpu_locked, current, vm_locked)) {
Federico Recanati9dccc4b2022-04-27 12:52:36 +02001065 /* TODO: setting a return value to override
1066 * the placeholder (FFA_ERROR(INTERRUPTED))
1067 * set by FFA_MSG_WAIT. FF-A v1.1 allows
1068 * FFA_MSG_WAIT to successfully return even if
1069 * it didn't receive a message. TFTF tests are
1070 * still expecting an FFA_ERROR instead,
1071 * should be fixed?
1072 */
1073 arch_regs_set_retval(
1074 &vcpu->regs,
1075 (struct ffa_value){.func = FFA_RUN_32,
1076 // TODO: does it make sense
1077 // to set vCPU/receiver?
1078 .arg1 = 0});
J-Alves6e2abc62021-12-02 14:58:56 +00001079 break;
1080 }
1081
Andrew Scullb06d1752019-02-04 10:15:48 +00001082 /*
1083 * A pending message allows the vCPU to run so the message can
1084 * be delivered directly.
1085 */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001086 if (vcpu->vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +01001087 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001088 ffa_msg_recv_return(vcpu->vm));
Andrew Scullb06d1752019-02-04 10:15:48 +00001089 break;
1090 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001091
1092 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
1093 break;
1094 }
1095
1096 if (arch_timer_enabled(&vcpu->regs)) {
1097 timer_remaining_ns =
1098 arch_timer_remaining_ns(&vcpu->regs);
1099 if (timer_remaining_ns == 0) {
1100 break;
1101 }
1102 } else {
1103 dlog_verbose("Timer disabled\n");
1104 }
1105 run_ret->func = FFA_MSG_WAIT_32;
1106 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1107 run_ret->arg2 = timer_remaining_ns;
1108 ret = false;
1109 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001110 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +00001111 if (need_vm_lock &&
1112 plat_ffa_inject_notification_pending_interrupt(
1113 vcpu_locked, current, vm_locked)) {
1114 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
1115 break;
1116 }
1117
Andrew Scullb06d1752019-02-04 10:15:48 +00001118 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +00001119 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +00001120 break;
1121 }
1122
Andrew Walbran508e63c2018-12-20 17:02:37 +00001123 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001124 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001125 arch_timer_remaining_ns(&vcpu->regs);
1126
1127 /*
1128 * The timer expired so allow the interrupt to be
1129 * delivered.
1130 */
1131 if (timer_remaining_ns == 0) {
1132 break;
1133 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001134 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +00001135
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001136 /*
1137 * The vCPU is not ready to run, return the appropriate code to
1138 * the primary which called vcpu_run.
1139 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001140 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +01001141 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
1142 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +00001143
1144 ret = false;
1145 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +00001146
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001147 case VCPU_STATE_BLOCKED:
1148 /* A blocked vCPU is run unconditionally. Fall through. */
1149 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +00001150 /* Check NPI is to be injected here. */
1151 if (need_vm_lock) {
1152 plat_ffa_inject_notification_pending_interrupt(
1153 vcpu_locked, current, vm_locked);
1154 }
Andrew Walbran508e63c2018-12-20 17:02:37 +00001155 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001156 default:
1157 /*
1158 * Execution not expected to reach here. Deny the request
1159 * gracefully.
1160 */
1161 *run_ret = ffa_error(FFA_DENIED);
1162 ret = false;
1163 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001164 }
1165
Madhukar Pappireddy1480fce2022-06-21 18:09:25 -05001166 plat_ffa_init_schedule_mode_ffa_run(current, vcpu_locked);
1167
Andrew Scullb06d1752019-02-04 10:15:48 +00001168 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001169 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +01001170 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001171
Olivier Deprez04168ed2022-09-26 09:20:00 +02001172 if (vcpu_was_init_state) {
1173 vcpu_set_phys_core_idx(vcpu);
1174 vcpu_set_boot_info_gp_reg(vcpu);
J-Alves7ac49052022-02-08 17:20:53 +00001175 }
J-Alves7ac49052022-02-08 17:20:53 +00001176
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001177 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001178 * Mark the registers as unavailable now that we're about to reflect
1179 * them onto the real registers. This will also prevent another physical
1180 * CPU from trying to read these registers.
1181 */
1182 vcpu->regs_available = false;
1183
1184 ret = true;
1185
1186out:
Max Shvetsov40108e72020-08-27 12:39:50 +01001187 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001188 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +01001189 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +00001190 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001191 return ret;
1192}
1193
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001194struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001195 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001196{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001197 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001198 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001200 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
1201 struct vcpu_locked current_locked;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001202
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -08001203 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001204 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001205 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001206
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -07001207 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
1208 return ret;
1209 }
1210
Andrew Scull19503262018-09-20 14:48:39 +01001211 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +01001212 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +01001213 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001214 goto out;
Andrew Scull19503262018-09-20 14:48:39 +01001215 }
1216
Fuad Tabbaed294af2019-12-20 10:43:01 +00001217 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001218 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +01001219 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +01001220 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001221
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001222 /*
1223 * Refer Figure 8.13 Scenario 1 of the FF-A v1.1 EAC spec. SPMC
1224 * bypasses the intermediate execution contexts and resumes the
1225 * SP execution context that was originally preempted.
1226 */
1227 if (*next != NULL) {
1228 vcpu = *next;
1229 } else {
1230 vcpu = vm_get_vcpu(vm, vcpu_idx);
1231 }
1232
1233 if (!plat_ffa_check_runtime_state_transition(current, current->vm->id,
1234 HF_INVALID_VM_ID, vcpu,
1235 FFA_RUN_32, &next_state)) {
1236 return ffa_error(FFA_DENIED);
1237 }
1238
Andrew Scullb06d1752019-02-04 10:15:48 +00001239 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001240 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001241 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001242
Andrew Walbran508e63c2018-12-20 17:02:37 +00001243 /*
1244 * Inject timer interrupt if timer has expired. It's safe to access
1245 * vcpu->regs here because api_vcpu_prepare_run already made sure that
1246 * regs_available was true (and then set it to false) before returning
1247 * true.
1248 */
1249 if (arch_timer_pending(&vcpu->regs)) {
1250 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001251 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
1252 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +00001253
1254 /*
1255 * Set the mask bit so the hardware interrupt doesn't fire
1256 * again. Ideally we wouldn't do this because it affects what
1257 * the secondary vCPU sees, but if we don't then we end up with
1258 * a loop of the interrupt firing each time we try to return to
1259 * the secondary vCPU.
1260 */
1261 arch_timer_mask(&vcpu->regs);
1262 }
1263
Fuad Tabbaed294af2019-12-20 10:43:01 +00001264 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001265 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +00001266
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05001267 assert(next_state == VCPU_STATE_BLOCKED);
1268 current_locked = vcpu_lock(current);
1269 current->state = VCPU_STATE_BLOCKED;
1270 vcpu_unlock(&current_locked);
1271
Andrew Scull33fecd32019-01-08 14:48:27 +00001272 /*
1273 * Set a placeholder return code to the scheduler. This will be
1274 * overwritten when the switch back to the primary occurs.
1275 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001276 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +01001277 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001278 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +00001279
Andrew Scull6d2db332018-10-10 15:28:17 +01001280out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001281 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001282}
1283
1284/**
Andrew Scull81e85092018-12-12 12:56:20 +00001285 * Check that the mode indicates memory that is valid, owned and exclusive.
1286 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001287static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001288{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001289 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1290 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001291}
1292
1293/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001294 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001295 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001296static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1297 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001298 paddr_t pa_send_begin, paddr_t pa_send_end,
1299 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001300 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001301 struct mpool *local_page_pool)
1302{
1303 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001304
1305 /* Map the send page as read-only in the hypervisor address space. */
1306 vm_locked.vm->mailbox.send =
1307 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001308 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001309 if (!vm_locked.vm->mailbox.send) {
Andrew Sculle1322792019-07-01 17:46:10 +01001310 goto fail;
1311 }
1312
1313 /*
1314 * Map the receive page as writable in the hypervisor address space. On
1315 * failure, unmap the send page before returning.
1316 */
1317 vm_locked.vm->mailbox.recv =
1318 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001319 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001320 if (!vm_locked.vm->mailbox.recv) {
Andrew Sculle1322792019-07-01 17:46:10 +01001321 goto fail_undo_send;
1322 }
1323
1324 ret = true;
1325 goto out;
1326
1327 /*
1328 * The following mappings will not require more memory than is available
1329 * in the local pool.
1330 */
1331fail_undo_send:
1332 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001333 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1334 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001335
1336fail:
1337 ret = false;
1338
1339out:
Andrew Sculle1322792019-07-01 17:46:10 +01001340 return ret;
1341}
1342
1343/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001344 * Sanity checks and configures the send and receive pages in the VM stage-2
1345 * and hypervisor stage-1 page tables.
1346 *
1347 * Returns:
1348 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001349 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001350 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1351 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001352 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001353 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001354 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001355
1356struct ffa_value api_vm_configure_pages(
1357 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1358 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1359 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001360{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001361 struct ffa_value ret;
1362 paddr_t pa_send_begin;
1363 paddr_t pa_send_end;
1364 paddr_t pa_recv_begin;
1365 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001366 uint32_t orig_send_mode = 0;
1367 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001368 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001369
1370 /* We only allow these to be setup once. */
1371 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1372 ret = ffa_error(FFA_DENIED);
1373 goto out;
1374 }
1375
1376 /* Hafnium only supports a fixed size of RX/TX buffers. */
1377 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1378 ret = ffa_error(FFA_INVALID_PARAMETERS);
1379 goto out;
1380 }
1381
1382 /* Fail if addresses are not page-aligned. */
1383 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1384 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1385 ret = ffa_error(FFA_INVALID_PARAMETERS);
1386 goto out;
1387 }
1388
1389 /* Convert to physical addresses. */
1390 pa_send_begin = pa_from_ipa(send);
1391 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1392 pa_recv_begin = pa_from_ipa(recv);
1393 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1394
1395 /* Fail if the same page is used for the send and receive pages. */
1396 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1397 ret = ffa_error(FFA_INVALID_PARAMETERS);
1398 goto out;
1399 }
Andrew Sculle1322792019-07-01 17:46:10 +01001400
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001401 /* Set stage 2 translation tables only for virtual FF-A instances. */
1402 if (vm_id_is_current_world(vm_locked.vm->id)) {
1403 /*
1404 * Ensure the pages are valid, owned and exclusive to the VM and
1405 * that the VM has the required access to the memory.
1406 */
1407 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1408 &orig_send_mode) ||
1409 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1410 (orig_send_mode & MM_MODE_R) == 0 ||
1411 (orig_send_mode & MM_MODE_W) == 0) {
1412 dlog_error(
1413 "VM doesn't have required access rights to map "
1414 "TX buffer in stage 2.\n");
1415 ret = ffa_error(FFA_INVALID_PARAMETERS);
1416 goto out;
1417 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001418
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001419 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1420 &orig_recv_mode) ||
1421 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1422 (orig_recv_mode & MM_MODE_R) == 0) {
1423 dlog_error(
1424 "VM doesn't have required access rights to map "
1425 "RX buffer in stage 2.\n");
1426 ret = ffa_error(FFA_INVALID_PARAMETERS);
1427 goto out;
1428 }
Andrew Sculle1322792019-07-01 17:46:10 +01001429
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001430 /* Take memory ownership away from the VM and mark as shared. */
1431 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1432 MM_MODE_W;
1433 if (vm_locked.vm->el0_partition) {
1434 mode |= MM_MODE_USER | MM_MODE_NG;
1435 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001436
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001437 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1438 mode, local_page_pool, NULL)) {
1439 dlog_error(
1440 "Cannot allocate a new entry in stage 2 "
1441 "translation table.\n");
1442 ret = ffa_error(FFA_NO_MEMORY);
1443 goto out;
1444 }
Andrew Sculle1322792019-07-01 17:46:10 +01001445
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001446 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1447 if (vm_locked.vm->el0_partition) {
1448 mode |= MM_MODE_USER | MM_MODE_NG;
1449 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001450
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001451 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1452 mode, local_page_pool, NULL)) {
1453 /* TODO: partial defrag of failed range. */
1454 /* Recover any memory consumed in failed mapping. */
1455 vm_ptable_defrag(vm_locked, local_page_pool);
1456 goto fail_undo_send;
1457 }
Andrew Sculle1322792019-07-01 17:46:10 +01001458 }
1459
Olivier Deprez96a2a262020-06-11 17:21:38 +02001460 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1461 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1462
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001463 /*
1464 * For EL0 partitions, since both the partition and the hypervisor code
1465 * use the EL2&0 translation regime, it is critical to mark the mappings
1466 * of the send and recv buffers as non-global in the TLB. For one, if we
1467 * dont mark it as non-global, it would cause TLB conflicts since there
1468 * would be an identity mapping with non-global attribute in the
1469 * partitions page tables, but another identity mapping in the
1470 * hypervisor page tables with the global attribute. The other issue is
1471 * one of security, we dont want other partitions to be able to access
1472 * other partitions buffers through cached translations.
1473 */
1474 if (vm_locked.vm->el0_partition) {
1475 extra_attributes |= MM_MODE_NG;
1476 }
1477
Manish Pandeyd34f8892020-06-19 17:41:07 +01001478 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1479 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001480 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001481 goto fail_undo_send_and_recv;
1482 }
1483
Manish Pandeyd34f8892020-06-19 17:41:07 +01001484 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001485 goto out;
1486
Andrew Sculle1322792019-07-01 17:46:10 +01001487fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001488 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001489 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001490
1491fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001492 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001493 orig_send_mode, local_page_pool, NULL));
1494 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001495
1496out:
Andrew Sculle1322792019-07-01 17:46:10 +01001497 return ret;
1498}
1499
J-Alves28ad9b42022-12-08 12:19:43 +00001500static void api_get_rxtx_description(struct vm *current_vm, ipaddr_t *send,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001501 ipaddr_t *recv, uint32_t *page_count,
1502 ffa_vm_id_t *owner_vm_id)
1503{
1504 /*
1505 * If the message has been forwarded the effective addresses are in
1506 * hypervisor's TX buffer.
1507 */
J-Alves28ad9b42022-12-08 12:19:43 +00001508 bool forwarded = (current_vm->id == HF_OTHER_WORLD_ID) &&
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001509 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1510 (*page_count == 0);
1511
1512 if (forwarded) {
J-Alves28ad9b42022-12-08 12:19:43 +00001513 struct vm_locked vm_locked = vm_lock(current_vm);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001514 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1515 (struct ffa_endpoint_rx_tx_descriptor *)
1516 vm_locked.vm->mailbox.send;
1517 struct ffa_composite_memory_region *rx_region =
1518 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1519 struct ffa_composite_memory_region *tx_region =
1520 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1521
1522 *owner_vm_id = endpoint_desc->endpoint_id;
1523 *recv = ipa_init(rx_region->constituents[0].address);
1524 *send = ipa_init(tx_region->constituents[0].address);
1525 *page_count = rx_region->constituents[0].page_count;
J-Alves28ad9b42022-12-08 12:19:43 +00001526
1527 vm_unlock(&vm_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001528 } else {
J-Alves28ad9b42022-12-08 12:19:43 +00001529 *owner_vm_id = current_vm->id;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001530 }
1531}
Andrew Sculle1322792019-07-01 17:46:10 +01001532/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001533 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001534 * must not be shared. Locking of the page tables combined with a local memory
1535 * pool ensures there will always be enough memory to recover from any errors
1536 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1537 * by another core which could result in this transaction being unable to roll
1538 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001539 *
1540 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001541 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001542 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001543 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001544 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001545 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001547 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001548struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001549 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001550{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 struct ffa_value ret;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001552 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001553 struct mm_stage1_locked mm_stage1_locked;
1554 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001555 ffa_vm_id_t owner_vm_id;
1556
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001557 /*
1558 * Get the original buffer addresses and VM ID in case of forwarded
1559 * message.
1560 */
J-Alves28ad9b42022-12-08 12:19:43 +00001561 api_get_rxtx_description(current->vm, &send, &recv, &page_count,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001562 &owner_vm_id);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001563
1564 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1565 if (owner_vm_locked.vm == NULL) {
1566 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1567 owner_vm_id);
1568 return ffa_error(FFA_DENIED);
1569 }
Andrew Scull220e6212018-12-21 18:09:00 +00001570
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001571 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001572 * Create a local pool so any freed memory can't be used by another
1573 * thread. This is to ensure the original mapping can be restored if any
1574 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001575 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001576 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1577
Manish Pandeyd34f8892020-06-19 17:41:07 +01001578 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001579
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001580 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1581 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001582 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001583 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001584 }
1585
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001586 /* Forward buffer mapping to SPMC if coming from a VM. */
1587 plat_ffa_rxtx_map_forward(owner_vm_locked);
1588
Federico Recanati9f1b6532022-04-14 13:15:28 +02001589 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001590
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001591exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001592 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001593 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001594 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001595
1596 return ret;
1597}
1598
1599/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001600 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1601 * translation regime of the caller. Unmap the region for the hypervisor and
1602 * set the memory region to owned and exclusive for the component. Since the
1603 * memory region mapped in the page table, when the buffers were originally
1604 * created we can safely remap it.
1605 *
1606 * Returns:
1607 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1608 * behalf of the caller.
1609 * - FFA_SUCCESS on success if no further action is needed.
1610 */
1611struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1612 struct vcpu *current)
1613{
1614 struct vm *vm = current->vm;
1615 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001616 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001617 struct mm_stage1_locked mm_stage1_locked;
1618 paddr_t send_pa_begin;
1619 paddr_t send_pa_end;
1620 paddr_t recv_pa_begin;
1621 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001622 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001623
J-Alves9bd324a2023-01-12 10:52:52 +00001624 if (vm->id == HF_HYPERVISOR_VM_ID && !plat_ffa_is_vm_id(allocator_id)) {
1625 dlog_error(
1626 "The Hypervisor must specify a valid VM ID in register "
1627 "W1, if FFA_RXTX_UNMAP call forwarded to SPM.\n");
1628 return ffa_error(FFA_INVALID_PARAMETERS);
1629 }
1630
Federico Recanati8da9e332022-02-10 11:00:17 +01001631 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1632 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
J-Alves9bd324a2023-01-12 10:52:52 +00001633 dlog_error(
1634 "The register W1 (containing ID) must be 0 at virtual "
1635 "instances.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001636 return ffa_error(FFA_INVALID_PARAMETERS);
1637 }
1638
1639 /* VM ID of which buffers have to be unmapped. */
1640 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1641
1642 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1643 vm = vm_locked.vm;
1644 if (vm == NULL) {
1645 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1646 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001647 return ffa_error(FFA_INVALID_PARAMETERS);
1648 }
1649
1650 /* Get send and receive buffers. */
1651 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001652 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001653 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001654 ret = ffa_error(FFA_INVALID_PARAMETERS);
1655 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001656 }
1657
1658 /* Currently a mailbox size of 1 page is assumed. */
1659 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1660 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1661 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1662 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1663
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001664 mm_stage1_locked = mm_lock_stage1();
1665
Federico Recanati8da9e332022-02-10 11:00:17 +01001666 /* Reset stage 2 mapping only for virtual FF-A instances. */
1667 if (vm_id_is_current_world(owner_vm_id)) {
1668 /*
1669 * Set the memory region of the buffers back to the default mode
1670 * for the VM. Since this memory region was already mapped for
1671 * the RXTX buffers we can safely remap them.
1672 */
1673 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1674 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1675 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001676
Federico Recanati8da9e332022-02-10 11:00:17 +01001677 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1678 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1679 &api_page_pool, NULL));
1680 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001681
1682 /* Unmap the buffers in the partition manager. */
1683 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1684 &api_page_pool));
1685 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1686 &api_page_pool));
1687
1688 vm->mailbox.send = NULL;
1689 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001690 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001691
Federico Recanati8da9e332022-02-10 11:00:17 +01001692 /* Forward buffer unmapping to SPMC if coming from a VM. */
J-Alves70079932022-12-07 17:32:20 +00001693 plat_ffa_rxtx_unmap_forward(vm_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001694
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001695 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001696
1697out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001698 vm_unlock(&vm_locked);
1699
Federico Recanati8da9e332022-02-10 11:00:17 +01001700 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001701}
1702
1703/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001704 * Copies data from the sender's send buffer to the recipient's receive buffer
1705 * and notifies the receiver.
1706 */
1707struct ffa_value api_ffa_msg_send2(ffa_vm_id_t sender_vm_id, uint32_t flags,
1708 struct vcpu *current)
1709{
1710 struct vm *from = current->vm;
1711 struct vm *to;
1712 struct vm_locked to_locked;
1713 ffa_vm_id_t msg_sender_id;
1714 struct vm_locked sender_locked;
1715 const void *from_msg;
1716 struct ffa_value ret;
1717 struct ffa_partition_rxtx_header header;
1718 ffa_vm_id_t sender_id;
1719 ffa_vm_id_t receiver_id;
1720 uint32_t msg_size;
1721 ffa_notifications_bitmap_t rx_buffer_full;
1722
1723 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1724 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1725 dlog_error("Sender VM ID must be zero.\n");
1726 return ffa_error(FFA_INVALID_PARAMETERS);
1727 }
1728
Federico Recanati25053ee2022-03-14 15:01:53 +01001729 /*
1730 * Get message sender's mailbox, which can be different to the `from` vm
1731 * when the message is forwarded.
1732 */
1733 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1734 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1735 if (sender_locked.vm == NULL) {
1736 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1737 msg_sender_id);
1738 return ffa_error(FFA_DENIED);
1739 }
1740
1741 from_msg = sender_locked.vm->mailbox.send;
1742 if (from_msg == NULL) {
1743 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1744 msg_sender_id);
1745 ret = ffa_error(FFA_DENIED);
1746 goto out_unlock_sender;
1747 }
1748
1749 /*
1750 * Copy message header as safety measure to avoid multiple accesses to
1751 * unsafe memory which could be 'corrupted' between safety checks and
1752 * final buffer copy.
1753 */
1754 memcpy_s(&header, FFA_RXTX_HEADER_SIZE, from_msg, FFA_RXTX_HEADER_SIZE);
1755 sender_id = ffa_rxtx_header_sender(&header);
1756 receiver_id = ffa_rxtx_header_receiver(&header);
1757
1758 /* Ensure Sender IDs from API and from message header match. */
1759 if (msg_sender_id != sender_id) {
1760 dlog_error(
1761 "Message sender VM ID (%#x) doesn't match header's VM "
1762 "ID (%#x).\n",
1763 msg_sender_id, sender_id);
1764 ret = ffa_error(FFA_INVALID_PARAMETERS);
1765 goto out_unlock_sender;
1766 }
1767
1768 /* Disallow reflexive requests as this suggests an error in the VM. */
1769 if (receiver_id == sender_id) {
1770 dlog_error("Sender and receive VM IDs must be different.\n");
1771 ret = ffa_error(FFA_INVALID_PARAMETERS);
1772 goto out_unlock_sender;
1773 }
1774
Federico Recanati7b39ff22022-03-14 15:01:53 +01001775 /* `flags` can be set only at secure virtual FF-A instances. */
1776 if (plat_ffa_is_vm_id(sender_id) && (flags != 0)) {
1777 dlog_error("flags must be zero.\n");
1778 return ffa_error(FFA_INVALID_PARAMETERS);
1779 }
1780
Federico Recanati25053ee2022-03-14 15:01:53 +01001781 /*
1782 * Check if the message has to be forwarded to the SPMC, in
1783 * this case return, the SPMC will handle the buffer copy.
1784 */
1785 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
1786 goto out_unlock_sender;
1787 }
1788
1789 /* Ensure the receiver VM exists. */
1790 to_locked = plat_ffa_vm_find_locked(receiver_id);
1791 to = to_locked.vm;
1792
1793 if (to == NULL) {
1794 dlog_error("Cannot deliver message to VM %#x, not found.\n",
1795 receiver_id);
1796 ret = ffa_error(FFA_INVALID_PARAMETERS);
1797 goto out_unlock_sender;
1798 }
1799
1800 /*
1801 * Check sender and receiver can use indirect messages.
1802 * Sender is the VM/SP who originally sent the message, not the
1803 * hypervisor possibly relaying it.
1804 */
1805 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
1806 dlog_verbose("VM %#x doesn't support indirect message\n",
1807 sender_id);
1808 ret = ffa_error(FFA_DENIED);
1809 goto out;
1810 }
1811
J-Alvese8c8c2b2022-12-16 15:34:48 +00001812 if (vm_is_mailbox_busy(to_locked)) {
Federico Recanati25053ee2022-03-14 15:01:53 +01001813 dlog_error(
1814 "Cannot deliver message to VM %#x, RX buffer not "
1815 "ready.\n",
1816 receiver_id);
1817 ret = ffa_error(FFA_BUSY);
1818 goto out;
1819 }
1820
Federico Recanati644f0462022-03-17 12:04:00 +01001821 /* Acquire receiver's RX buffer. */
1822 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
1823 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
1824 goto out;
1825 }
1826
Federico Recanati25053ee2022-03-14 15:01:53 +01001827 /* Check the size of transfer. */
1828 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
1829 if ((msg_size > FFA_PARTITION_MSG_PAYLOAD_MAX) ||
1830 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
1831 dlog_error("Message is too big.\n");
1832 ret = ffa_error(FFA_INVALID_PARAMETERS);
1833 goto out;
1834 }
1835
1836 /* Copy data. */
1837 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, msg_size);
1838 to->mailbox.recv_size = msg_size;
1839 to->mailbox.recv_sender = sender_id;
1840 to->mailbox.recv_func = FFA_MSG_SEND2_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00001841 to->mailbox.state = MAILBOX_STATE_FULL;
Federico Recanati25053ee2022-03-14 15:01:53 +01001842
1843 rx_buffer_full = plat_ffa_is_vm_id(sender_id)
1844 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
1845 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
1846 vm_notifications_framework_set_pending(to_locked, rx_buffer_full);
1847
1848 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
1849 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
1850 vcpu_index(current));
1851 plat_ffa_sri_trigger_not_delayed(current->cpu);
1852 } else {
1853 plat_ffa_sri_state_set(DELAYED);
1854 }
1855
1856 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1857
1858out:
1859 vm_unlock(&to_locked);
1860
1861out_unlock_sender:
1862 vm_unlock(&sender_locked);
1863
1864 return ret;
1865}
1866
1867/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001868 * Checks whether the vCPU's attempt to block for a message has already been
1869 * interrupted or whether it is allowed to block.
1870 */
Olivier Deprezd8e18882022-07-15 14:46:41 +02001871static bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001872{
Manish Pandey35e452f2021-02-18 21:36:34 +00001873 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001874 bool interrupted;
1875
Manish Pandey35e452f2021-02-18 21:36:34 +00001876 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001877
1878 /*
1879 * Don't block if there are enabled and pending interrupts, to match
1880 * behaviour of wait_for_interrupt.
1881 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001882 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001883
Manish Pandey35e452f2021-02-18 21:36:34 +00001884 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001885
1886 return interrupted;
1887}
1888
1889/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001890 * Receives a message from the mailbox. If one isn't available, this function
1891 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001892 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001893 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001894 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001895struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1896 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001897{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001898 bool is_direct_request_ongoing;
1899 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001900 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001901 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001902 bool is_from_secure_world =
1903 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001904
Andrew Scullaa039b32018-10-04 15:02:26 +01001905 /*
1906 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001907 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001908 */
J-Alvesb37fd082020-10-22 12:29:21 +01001909 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001910 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001911 }
1912
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001913 /*
1914 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1915 * invocation.
1916 */
1917 current_locked = vcpu_lock(current);
1918 is_direct_request_ongoing =
1919 is_ffa_direct_msg_request_ongoing(current_locked);
1920 vcpu_unlock(&current_locked);
1921
1922 if (is_direct_request_ongoing) {
1923 return ffa_error(FFA_DENIED);
1924 }
1925
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001926 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001927
Andrew Scullaa039b32018-10-04 15:02:26 +01001928 /* Return pending messages without blocking. */
J-Alvese8c8c2b2022-12-16 15:34:48 +00001929 if (vm->mailbox.state == MAILBOX_STATE_FULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001930 return_code = ffa_msg_recv_return(vm);
Federico Recanati25053ee2022-03-14 15:01:53 +01001931 if (return_code.func == FFA_MSG_SEND_32) {
J-Alvese8c8c2b2022-12-16 15:34:48 +00001932 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Federico Recanati25053ee2022-03-14 15:01:53 +01001933 }
Jose Marinho3e2442f2019-03-12 13:30:37 +00001934 goto out;
1935 }
1936
1937 /* No pending message so fail if not allowed to block. */
1938 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001939 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001940 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001941 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001942
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001943 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001944 * From this point onward this call can only be interrupted or a message
1945 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001946 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001947 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001948 return_code = ffa_error(FFA_INTERRUPTED);
1949 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001950 goto out;
1951 }
1952
J-Alvesb37fd082020-10-22 12:29:21 +01001953 if (is_from_secure_world) {
1954 /* Return to other world if caller is a SP. */
1955 *next = api_switch_to_other_world(
1956 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001957 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001958 } else {
1959 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001960 struct ffa_value run_return = {
1961 .func = FFA_MSG_WAIT_32,
1962 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001963 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001964
Andrew Walbranb4816552018-12-05 17:35:42 +00001965 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001966 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001967 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001968out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001969 sl_unlock(&vm->lock);
1970
Jose Marinho3e2442f2019-03-12 13:30:37 +00001971 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001972}
1973
1974/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001975 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1976 * by this function, the caller must have called api_mailbox_send before with
1977 * the notify argument set to true, and this call must have failed because the
1978 * mailbox was not available.
1979 *
1980 * It should be called repeatedly to retrieve a list of VMs.
1981 *
1982 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1983 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001984 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001985int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001986{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001987 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001988 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001989 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001990
1991 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001992 if (list_empty(&vm->mailbox.ready_list)) {
1993 ret = -1;
1994 goto exit;
1995 }
1996
1997 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1998 ready_links);
1999 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00002000 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002001
2002exit:
2003 sl_unlock(&vm->lock);
2004 return ret;
2005}
2006
2007/**
2008 * Retrieves the next VM waiting to be notified that the mailbox of the
2009 * specified VM became writable. Only primary VMs are allowed to call this.
2010 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00002011 * Returns -1 on failure or if there are no waiters; the VM id of the next
2012 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002013 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002014int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002015{
2016 struct vm *vm;
2017 struct vm_locked locked;
2018 struct wait_entry *entry;
2019 struct vm *waiting_vm;
2020
2021 /* Only primary VMs are allowed to call this function. */
2022 if (current->vm->id != HF_PRIMARY_VM_ID) {
2023 return -1;
2024 }
2025
Andrew Walbran42347a92019-05-09 13:59:03 +01002026 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002027 if (vm == NULL) {
2028 return -1;
2029 }
2030
Fuad Tabbaed294af2019-12-20 10:43:01 +00002031 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01002032 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002033 entry = api_fetch_waiter(locked);
2034 vm_unlock(&locked);
2035
2036 if (entry == NULL) {
2037 return -1;
2038 }
2039
2040 /* Enqueue notification to waiting VM. */
2041 waiting_vm = entry->waiting_vm;
2042
2043 sl_lock(&waiting_vm->lock);
2044 if (list_empty(&entry->ready_links)) {
2045 list_append(&waiting_vm->mailbox.ready_list,
2046 &entry->ready_links);
2047 }
2048 sl_unlock(&waiting_vm->lock);
2049
2050 return waiting_vm->id;
2051}
2052
2053/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002054 * Releases the caller's mailbox so that a new message can be received. The
2055 * caller must have copied out all data they wish to preserve as new messages
2056 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002057 *
2058 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002059 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
2060 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002061 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
2062 * - FFA_SUCCESS on success if no further action is needed.
2063 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00002064 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002065 * hf_mailbox_waiter_get.
2066 */
Federico Recanati7bef0b92022-03-17 14:56:22 +01002067struct ffa_value api_ffa_rx_release(ffa_vm_id_t receiver_id,
J-Alvese8c8c2b2022-12-16 15:34:48 +00002068 struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002069{
Federico Recanati7bef0b92022-03-17 14:56:22 +01002070 struct vm *current_vm = current->vm;
2071 struct vm *vm;
2072 struct vm_locked vm_locked;
2073 ffa_vm_id_t current_vm_id = current_vm->id;
2074 ffa_vm_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002075 struct ffa_value ret;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002076 int32_t error_code;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00002077
Federico Recanati7bef0b92022-03-17 14:56:22 +01002078 /* `receiver_id` can be set only at Non-Secure Physical interface. */
2079 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
2080 dlog_error("Invalid `receiver_id`, must be zero.\n");
2081 return ffa_error(FFA_INVALID_PARAMETERS);
2082 }
2083
2084 /*
2085 * VM ID to be released: `receiver_id` if message has been forwarded by
2086 * Hypervisor to release a VM's buffer, current VM ID otherwise.
2087 */
2088 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
2089 release_vm_id = current_vm_id;
2090 } else {
2091 release_vm_id = receiver_id;
2092 }
2093
2094 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2095 vm = vm_locked.vm;
2096 if (vm == NULL) {
2097 dlog_error("No buffer registered for VM ID %#x.\n",
2098 release_vm_id);
2099 return ffa_error(FFA_INVALID_PARAMETERS);
2100 }
2101
J-Alvese8c8c2b2022-12-16 15:34:48 +00002102 if (plat_ffa_rx_release_forward(vm_locked, &ret)) {
Federico Recanati7bef0b92022-03-17 14:56:22 +01002103 goto out;
2104 }
2105
J-Alvese8c8c2b2022-12-16 15:34:48 +00002106 if (!api_release_mailbox(vm_locked, &error_code)) {
2107 ret = ffa_error(error_code);
Federico Recanati7bef0b92022-03-17 14:56:22 +01002108 goto out;
2109 }
2110
J-Alvese8c8c2b2022-12-16 15:34:48 +00002111 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Federico Recanati7bef0b92022-03-17 14:56:22 +01002112
2113out:
2114 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002115
2116 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002117}
Andrew Walbran318f5732018-11-20 16:23:42 +00002118
2119/**
Federico Recanati644f0462022-03-17 12:04:00 +01002120 * Acquire ownership of an RX buffer before writing to it. Both
2121 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2122 * could contend for the same buffer. SPMC owns VM's RX buffer after
2123 * it's mapped in its translation regime. This ABI should be
2124 * used by the Hypervisor to get the ownership of a VM's RX buffer
2125 * from the SPMC solving the aforementioned possible contention.
2126 *
2127 * Returns:
2128 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2129 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2130 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2131 */
2132struct ffa_value api_ffa_rx_acquire(ffa_vm_id_t receiver_id,
2133 struct vcpu *current)
2134{
2135 struct vm_locked receiver_locked;
2136 struct vm *receiver;
2137 struct ffa_value ret;
2138
2139 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
2140 !plat_ffa_is_vm_id(receiver_id)) {
2141 dlog_error(
2142 "FFA_RX_ACQUIRE not supported at this FF-A "
2143 "instance.\n");
2144 return ffa_error(FFA_NOT_SUPPORTED);
2145 }
2146
2147 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2148 receiver = receiver_locked.vm;
2149
2150 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2151 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2152 receiver_id);
2153 ret = ffa_error(FFA_INVALID_PARAMETERS);
2154 goto out;
2155 }
2156
2157 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2158 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2159 ret = ffa_error(FFA_DENIED);
2160 goto out;
2161 }
2162
J-Alvese8c8c2b2022-12-16 15:34:48 +00002163 receiver->mailbox.state = MAILBOX_STATE_OTHER_WORLD_OWNED;
Federico Recanati644f0462022-03-17 12:04:00 +01002164
2165 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2166
2167out:
2168 vm_unlock(&receiver_locked);
2169
2170 return ret;
2171}
2172
2173/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002174 * Enables or disables a given interrupt ID for the calling vCPU.
2175 *
2176 * Returns 0 on success, or -1 if the intid is invalid.
2177 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002178int64_t api_interrupt_enable(uint32_t intid, bool enable,
2179 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002180{
Manish Pandey35e452f2021-02-18 21:36:34 +00002181 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002182 struct interrupts *interrupts = &current->interrupts;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002183
Andrew Walbran318f5732018-11-20 16:23:42 +00002184 if (intid >= HF_NUM_INTIDS) {
2185 return -1;
2186 }
2187
Manish Pandey35e452f2021-02-18 21:36:34 +00002188 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002189 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002190 /*
2191 * If it is pending and was not enabled before, increment the
2192 * count.
2193 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002194 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2195 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2196 vcpu_interrupt_count_increment(current_locked,
2197 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002198 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002199 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2200 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002201 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002202 /*
2203 * If it is pending and was enabled before, decrement the count.
2204 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002205 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2206 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2207 vcpu_interrupt_count_decrement(current_locked,
2208 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002209 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002210 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2211 vcpu_virt_interrupt_set_type(interrupts, intid,
2212 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002213 }
2214
Manish Pandey35e452f2021-02-18 21:36:34 +00002215 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002216 return 0;
2217}
2218
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002219static void api_interrupt_clear_decrement(struct vcpu_locked locked_vcpu,
2220 struct interrupts *interrupts,
2221 uint32_t intid)
2222{
2223 vcpu_virt_interrupt_clear_pending(interrupts, intid);
2224 vcpu_interrupt_count_decrement(locked_vcpu, interrupts, intid);
2225}
2226
Andrew Walbran318f5732018-11-20 16:23:42 +00002227/**
2228 * Returns the ID of the next pending interrupt for the calling vCPU, and
2229 * acknowledges it (i.e. marks it as no longer pending). Returns
2230 * HF_INVALID_INTID if there are no pending interrupts.
2231 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00002232uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002233{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002234 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002235 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00002236 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002237 struct interrupts *interrupts = &current->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002238
2239 /*
2240 * Find the first enabled and pending interrupt ID, return it, and
2241 * deactivate it.
2242 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002243 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002244 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2245 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002246 interrupts->interrupt_enabled.bitmap[i] &
2247 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002248
Andrew Walbran318f5732018-11-20 16:23:42 +00002249 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002250 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002251
2252 first_interrupt =
2253 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002254
Andrew Walbran3d84a262018-12-13 14:41:19 +00002255 /*
2256 * Mark it as no longer pending and decrement the count.
2257 */
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002258 api_interrupt_clear_decrement(
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002259 current_locked, interrupts, first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002260 break;
2261 }
2262 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002263
Manish Pandey35e452f2021-02-18 21:36:34 +00002264 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002265 return first_interrupt;
2266}
2267
2268/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00002269 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00002270 * given VM and vCPU.
2271 */
2272static inline bool is_injection_allowed(uint32_t target_vm_id,
2273 struct vcpu *current)
2274{
2275 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002276
Andrew Walbran318f5732018-11-20 16:23:42 +00002277 /*
2278 * The primary VM is allowed to inject interrupts into any VM. Secondary
2279 * VMs are only allowed to inject interrupts into their own vCPUs.
2280 */
2281 return current_vm_id == HF_PRIMARY_VM_ID ||
2282 current_vm_id == target_vm_id;
2283}
2284
2285/**
2286 * Injects a virtual interrupt of the given ID into the given target vCPU.
2287 * This doesn't cause the vCPU to actually be run immediately; it will be taken
2288 * when the vCPU is next run, which is up to the scheduler.
2289 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00002290 * Returns:
2291 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
2292 * ID is invalid, or the current VM is not allowed to inject interrupts to
2293 * the target VM.
2294 * - 0 on success if no further action is needed.
2295 * - 1 if it was called by the primary VM and the primary VM now needs to wake
2296 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00002297 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002298int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
2299 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01002300 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00002301{
Andrew Walbran318f5732018-11-20 16:23:42 +00002302 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01002303 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00002304
2305 if (intid >= HF_NUM_INTIDS) {
2306 return -1;
2307 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002308
Andrew Walbran318f5732018-11-20 16:23:42 +00002309 if (target_vm == NULL) {
2310 return -1;
2311 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002312
Andrew Walbran318f5732018-11-20 16:23:42 +00002313 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00002314 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00002315 return -1;
2316 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002317
Andrew Walbran318f5732018-11-20 16:23:42 +00002318 if (!is_injection_allowed(target_vm_id, current)) {
2319 return -1;
2320 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002321
Andrew Walbrane1310df2019-04-29 17:28:28 +01002322 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00002323
Manish Pandey35e452f2021-02-18 21:36:34 +00002324 dlog_verbose(
2325 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
2326 "%u\n",
2327 intid, target_vm_id, target_vcpu_idx, current->vm->id,
2328 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01002329 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00002330}
Andrew Scull6386f252018-12-06 13:29:10 +00002331
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002332/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002333struct ffa_value api_ffa_version(struct vcpu *current,
2334 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002335{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002336 struct vm_locked current_vm_locked;
2337
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002338 /*
2339 * Ensure that both major and minor revision representation occupies at
2340 * most 15 bits.
2341 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002342 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002343 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002344 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002345 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002346 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002347 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002348 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002349 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002350
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002351 current_vm_locked = vm_lock(current->vm);
2352 current_vm_locked.vm->ffa_version = requested_version;
2353 vm_unlock(&current_vm_locked);
2354
Daniel Boulby6e32c612021-02-17 15:09:41 +00002355 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002356}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002357
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002358/**
J-Alves6f72ca82021-11-01 12:34:58 +00002359 * Helper for success return of FFA_FEATURES, for when it is used to query
2360 * an interrupt ID.
2361 */
2362struct ffa_value api_ffa_feature_success(uint32_t arg2)
2363{
2364 return (struct ffa_value){
2365 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2366}
2367
2368/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002369 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002370 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002371 */
Karl Meakin34b8ae92023-01-13 13:33:07 +00002372struct ffa_value api_ffa_features(uint32_t feature_function_id,
2373 uint32_t input_property, uint32_t ffa_version)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002374{
J-Alves6f72ca82021-11-01 12:34:58 +00002375 /*
2376 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2377 * if using a feature ID.
2378 */
2379 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002380 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002381 return ffa_error(FFA_NOT_SUPPORTED);
2382 }
2383
Karl Meakin34b8ae92023-01-13 13:33:07 +00002384 if (feature_function_id != FFA_MEM_RETRIEVE_REQ_32 &&
2385 input_property != 0U) {
2386 dlog_verbose(
2387 "input_property must be zero.\ninput_property = %u.\n",
2388 input_property);
2389 return ffa_error(FFA_INVALID_PARAMETERS);
2390 }
2391
J-Alves6f72ca82021-11-01 12:34:58 +00002392 switch (feature_function_id) {
2393 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002394 case FFA_ERROR_32:
2395 case FFA_SUCCESS_32:
2396 case FFA_INTERRUPT_32:
2397 case FFA_VERSION_32:
2398 case FFA_FEATURES_32:
2399 case FFA_RX_RELEASE_32:
2400 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002401 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002402 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002403 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002404 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002405 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002406 case FFA_MEM_DONATE_32:
2407 case FFA_MEM_LEND_32:
2408 case FFA_MEM_SHARE_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002409 case FFA_MEM_RETRIEVE_RESP_32:
2410 case FFA_MEM_RELINQUISH_32:
2411 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002412 case FFA_MEM_FRAG_RX_32:
2413 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002414 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002415 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002416 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002417 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002418#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002419 /* FF-A v1.1 features. */
2420 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002421 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2422 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2423 case FFA_NOTIFICATION_BIND_32:
2424 case FFA_NOTIFICATION_UNBIND_32:
2425 case FFA_NOTIFICATION_SET_32:
2426 case FFA_NOTIFICATION_GET_32:
2427 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002428 case FFA_MEM_PERM_GET_32:
2429 case FFA_MEM_PERM_SET_32:
2430 case FFA_MEM_PERM_GET_64:
2431 case FFA_MEM_PERM_SET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002432 case FFA_MSG_SEND2_32:
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -08002433 case FFA_PARTITION_INFO_GET_REGS_64:
J-Alves3829fc02021-03-18 12:49:18 +00002434#endif
2435 return (struct ffa_value){.func = FFA_SUCCESS_32};
Karl Meakin34b8ae92023-01-13 13:33:07 +00002436 case FFA_MEM_RETRIEVE_REQ_32:
2437 if ((input_property & FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK) !=
2438 0U) {
2439 dlog_verbose(
2440 "Bits[31:2] and Bit[0] of input_property must "
2441 "be zero.\ninput_property = %u.\n",
2442 input_property);
2443 return ffa_error(FFA_INVALID_PARAMETERS);
2444 }
2445
2446 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
2447 if ((input_property &
2448 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) == 0U) {
2449 dlog_verbose("NS bit support must be 1.\n");
2450 return ffa_error(FFA_INVALID_PARAMETERS);
2451 }
2452 }
2453
2454 return api_ffa_feature_success(
2455 FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT |
2456 (input_property &
2457 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT) |
2458 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT);
J-Alves6f72ca82021-11-01 12:34:58 +00002459
2460#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2461 /* Check support of a feature provided respective feature ID. */
2462 case FFA_FEATURE_NPI:
2463 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2464 case FFA_FEATURE_SRI:
2465 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2466#endif
2467 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002468 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002469 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002470 }
2471}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002472
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002473/**
J-Alves645eabe2021-02-22 16:08:27 +00002474 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2475 * interfaces.
2476 */
2477static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2478{
2479 return args.arg2 == 0U;
2480}
2481
2482/**
J-Alves76d99af2021-03-10 17:42:11 +00002483 * Limits size of arguments in ffa_value structure to 32-bit.
2484 */
2485static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2486{
2487 return (struct ffa_value){
2488 .func = (uint32_t)args.func,
2489 .arg1 = (uint32_t)args.arg1,
2490 .arg2 = (uint32_t)0,
2491 .arg3 = (uint32_t)args.arg3,
2492 .arg4 = (uint32_t)args.arg4,
2493 .arg5 = (uint32_t)args.arg5,
2494 .arg6 = (uint32_t)args.arg6,
2495 .arg7 = (uint32_t)args.arg7,
2496 };
2497}
2498
2499/**
2500 * Helper to copy direct message payload, depending on SMC used and expected
2501 * registers size.
2502 */
2503static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2504{
2505 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2506 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2507 return api_ffa_value_copy32(args);
2508 }
2509
2510 return (struct ffa_value){
2511 .func = args.func,
2512 .arg1 = args.arg1,
2513 .arg2 = 0,
2514 .arg3 = args.arg3,
2515 .arg4 = args.arg4,
2516 .arg5 = args.arg5,
2517 .arg6 = args.arg6,
2518 .arg7 = args.arg7,
2519 };
2520}
2521
2522/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002523 * Send an FF-A direct message request.
2524 */
2525struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2526 ffa_vm_id_t receiver_vm_id,
2527 struct ffa_value args,
2528 struct vcpu *current,
2529 struct vcpu **next)
2530{
J-Alves17228f72021-04-20 17:13:19 +01002531 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002532 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002533 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002534 struct vcpu *receiver_vcpu;
2535 struct two_vcpu_locked vcpus_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002536 enum vcpu_state next_state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002537
J-Alves645eabe2021-02-22 16:08:27 +00002538 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2539 return ffa_error(FFA_INVALID_PARAMETERS);
2540 }
2541
Olivier Deprez55a189e2021-06-09 15:45:27 +02002542 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2543 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002544 dlog_verbose("Invalid direct message request.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002545 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002546 }
2547
Olivier Deprez55a189e2021-06-09 15:45:27 +02002548 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002549 return ret;
2550 }
2551
2552 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2553
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002554 receiver_vm = vm_find(receiver_vm_id);
2555 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002556 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002557 return ffa_error(FFA_INVALID_PARAMETERS);
2558 }
2559
2560 /*
J-Alves439ac972021-11-18 17:32:03 +00002561 * Check if sender supports sending direct message req, and if
2562 * receiver supports receipt of direct message requests.
2563 */
2564 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2565 return ffa_error(FFA_DENIED);
2566 }
2567
2568 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002569 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002570 * UP (migratable) or MP partitions with a number of vCPUs matching the
2571 * number of PEs in the system. It further states that MP partitions
2572 * accepting direct request messages cannot migrate.
2573 */
J-Alvesad6a0432021-04-09 16:06:21 +01002574 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002575 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002576 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002577 return ffa_error(FFA_INVALID_PARAMETERS);
2578 }
2579
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002580 if (!plat_ffa_check_runtime_state_transition(
2581 current, sender_vm_id, HF_INVALID_VM_ID, receiver_vcpu,
2582 args.func, &next_state)) {
2583 return ffa_error(FFA_DENIED);
2584 }
2585
J-Alves6e2abc62021-12-02 14:58:56 +00002586 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002587 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2588
2589 /*
2590 * If destination vCPU is executing or already received an
2591 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2592 * busy. There is a brief period of time where the vCPU state has
2593 * changed but regs_available is still false thus consider this case as
2594 * the vCPU not yet ready to receive a direct message request.
2595 */
2596 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2597 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2598 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002599 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002600 ret = ffa_error(FFA_BUSY);
2601 goto out;
2602 }
2603
2604 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2605 memory_order_relaxed)) {
2606 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002607 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002608 receiver_vcpu->vm->id,
2609 vcpu_index(receiver_vcpu));
2610 receiver_vcpu->state = VCPU_STATE_ABORTED;
2611 }
2612
2613 ret = ffa_error(FFA_ABORTED);
2614 goto out;
2615 }
2616
2617 switch (receiver_vcpu->state) {
2618 case VCPU_STATE_OFF:
2619 case VCPU_STATE_RUNNING:
2620 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002621 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002622 case VCPU_STATE_BLOCKED:
2623 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002624 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2625 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002626 ret = ffa_error(FFA_BUSY);
2627 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002628 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002629 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002630 * We expect target vCPU to be in WAITING state after either
2631 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002632 */
2633 break;
2634 }
2635
2636 /* Inject timer interrupt if any pending */
2637 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002638 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2639 HF_VIRTUAL_TIMER_INTID, current,
2640 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002641
2642 arch_timer_mask(&receiver_vcpu->regs);
2643 }
2644
2645 /* The receiver vCPU runs upon direct message invocation */
2646 receiver_vcpu->cpu = current->cpu;
2647 receiver_vcpu->state = VCPU_STATE_RUNNING;
2648 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002649 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002650
J-Alves76d99af2021-03-10 17:42:11 +00002651 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002652
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002653 assert(next_state == VCPU_STATE_BLOCKED);
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002654 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002655
Madhukar Pappireddy49fe6702022-06-21 17:52:23 -05002656 plat_ffa_wind_call_chain_ffa_direct_req(vcpus_locked.vcpu2,
2657 vcpus_locked.vcpu1);
2658
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002659 /* Switch to receiver vCPU targeted to by direct msg request */
2660 *next = receiver_vcpu;
2661
J-Alves6e2abc62021-12-02 14:58:56 +00002662 if (!receiver_locked.vm->el0_partition) {
2663 /*
2664 * If the scheduler in the system is giving CPU cycles to the
2665 * receiver, due to pending notifications, inject the NPI
2666 * interrupt. Following call assumes that '*next' has been set
2667 * to receiver_vcpu.
2668 */
2669 plat_ffa_inject_notification_pending_interrupt(
2670 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2671 ? vcpus_locked.vcpu1
2672 : vcpus_locked.vcpu2,
2673 current, receiver_locked);
2674 }
2675
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002676 /*
2677 * Since this flow will lead to a VM switch, the return value will not
2678 * be applied to current vCPU.
2679 */
2680
2681out:
2682 sl_unlock(&receiver_vcpu->lock);
2683 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002684 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002685
2686 return ret;
2687}
2688
2689/**
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002690 * Resume the target vCPU after the current vCPU sent a direct response.
2691 * Current vCPU moves to waiting state.
2692 */
2693void api_ffa_resume_direct_resp_target(struct vcpu *current, struct vcpu **next,
2694 ffa_vm_id_t receiver_vm_id,
2695 struct ffa_value to_ret,
2696 bool is_nwd_call_chain)
2697{
2698 if (!vm_id_is_current_world(receiver_vm_id)) {
2699 *next = api_switch_to_other_world(current, to_ret,
2700 VCPU_STATE_WAITING);
2701
2702 /* End of NWd scheduled call chain. */
2703 assert(!is_nwd_call_chain ||
2704 (current->call_chain.prev_node == NULL));
2705 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
2706 *next = api_switch_to_primary(current, to_ret,
2707 VCPU_STATE_WAITING);
2708
2709 /* Removing a node from NWd scheduled call chain. */
2710 if (is_nwd_call_chain) {
2711 vcpu_call_chain_remove_node(current, *next);
2712 }
2713 } else if (vm_id_is_current_world(receiver_vm_id)) {
2714 /*
2715 * It is expected the receiver_vm_id to be from an SP, otherwise
2716 * 'plat_ffa_is_direct_response_valid' should have
2717 * made function return error before getting to this point.
2718 */
2719 *next = api_switch_to_vm(current, to_ret, VCPU_STATE_WAITING,
2720 receiver_vm_id);
2721 } else {
2722 panic("Invalid direct message response invocation");
2723 }
2724}
2725
2726/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002727 * Send an FF-A direct message response.
2728 */
2729struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2730 ffa_vm_id_t receiver_vm_id,
2731 struct ffa_value args,
2732 struct vcpu *current,
2733 struct vcpu **next)
2734{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002735 struct vcpu_locked current_locked;
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002736 enum vcpu_state next_state = VCPU_STATE_WAITING;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002737 struct ffa_value signal_interrupt =
2738 (struct ffa_value){.func = FFA_INTERRUPT_32};
J-Alves645eabe2021-02-22 16:08:27 +00002739
2740 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2741 return ffa_error(FFA_INVALID_PARAMETERS);
2742 }
2743
J-Alves76d99af2021-03-10 17:42:11 +00002744 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002745
Olivier Deprez55a189e2021-06-09 15:45:27 +02002746 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2747 receiver_vm_id)) {
J-Alvese0caac32022-12-19 14:37:08 +00002748 dlog_verbose("Invalid direct response call.\n");
J-Alvesaa336102021-03-01 13:02:45 +00002749 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002750 }
2751
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002752 if (!plat_ffa_check_runtime_state_transition(current, sender_vm_id,
2753 receiver_vm_id, NULL,
2754 args.func, &next_state)) {
2755 return ffa_error(FFA_DENIED);
2756 }
2757
Madhukar Pappireddy5fd32482022-01-07 14:53:26 -06002758 if (plat_ffa_is_direct_response_interrupted(current)) {
2759 return ffa_error(FFA_INTERRUPTED);
2760 }
2761
Madhukar Pappireddyc857ac22022-06-21 17:37:53 -05002762 assert(next_state == VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002763 current_locked = vcpu_lock(current);
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05002764
2765 /*
2766 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2767 * defined originator.
2768 */
2769 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2770 /*
2771 * Sending direct response but direct request origin
2772 * vCPU is not set.
2773 */
2774 vcpu_unlock(&current_locked);
2775 return ffa_error(FFA_DENIED);
2776 }
2777
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002778 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002779 /*
Madhukar Pappireddy469fa712022-06-21 18:53:33 -05002780 * Per FF-A v1.1 EAC0 section 8.3.1.2.1 rule 6, SPMC can signal
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002781 * a secure interrupt to a SP that is performing managed exit.
2782 * We have taken a implementation defined choice to not allow
2783 * Managed exit while a SP is processing a secure interrupt.
2784 */
2785 CHECK(!current->processing_secure_interrupt);
2786
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002787 plat_interrupts_set_priority_mask(current->priority_mask);
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002788 /*
2789 * A SP may be signaled a managed exit but actually not trap
2790 * the virtual interrupt, probably because it has virtual
2791 * interrupts masked, and emit direct resp. In this case the
2792 * managed exit operation is considered completed and it would
2793 * also need to clear the pending managed exit flag for the SP
2794 * vCPU.
2795 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002796 current->processing_managed_exit = false;
Madhukar Pappireddya1019112022-06-21 18:57:44 -05002797 struct interrupts *interrupts = &current->interrupts;
2798
2799 if (vcpu_is_virt_interrupt_pending(interrupts,
2800 HF_MANAGED_EXIT_INTID)) {
2801 api_interrupt_clear_decrement(current_locked,
2802 interrupts,
2803 HF_MANAGED_EXIT_INTID);
2804 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002805 }
2806
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002807 if (plat_ffa_intercept_direct_response(current_locked, next, to_ret,
2808 &signal_interrupt)) {
2809 vcpu_unlock(&current_locked);
2810 return signal_interrupt;
2811 }
2812
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002813 /* Clear direct request origin for the caller. */
2814 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2815
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002816 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002817
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -05002818 api_ffa_resume_direct_resp_target(current, next, receiver_vm_id, to_ret,
2819 false);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002820
Madhukar Pappireddyc0fb87e2022-06-21 17:59:15 -05002821 plat_ffa_unwind_call_chain_ffa_direct_resp(current, *next);
2822
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002823 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2824}
2825
J-Alves84658fc2021-06-17 14:37:32 +01002826static bool api_memory_region_check_flags(
2827 struct ffa_memory_region *memory_region, uint32_t share_func)
2828{
2829 switch (share_func) {
2830 case FFA_MEM_SHARE_32:
2831 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2832 0U) {
2833 return false;
2834 }
2835 /* Intentional fall-through */
2836 case FFA_MEM_LEND_32:
2837 case FFA_MEM_DONATE_32: {
2838 /* Bits 31:2 Must Be Zero. */
2839 ffa_memory_receiver_flags_t to_mask =
2840 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2841 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2842
2843 if ((memory_region->flags & to_mask) != 0U) {
2844 return false;
2845 }
2846 break;
2847 }
2848 default:
2849 panic("Check for mem send calls only.\n");
2850 }
2851
2852 /* Last check reserved values are 0 */
2853 return true;
2854}
2855
J-Alves33c47bf2022-09-29 11:36:20 +01002856/*
2857 * Convert memory transaction descriptor from FF-A v1.0 to FF-A v1.1 EAC0.
2858 */
2859static void api_ffa_memory_region_v1_1_from_v1_0(
2860 struct ffa_memory_region_v1_0 *memory_region_v1_0,
2861 struct ffa_memory_region *memory_region_v1_1)
2862{
2863 memory_region_v1_1->sender = memory_region_v1_0->sender;
J-Alves00847062022-10-03 17:08:44 +01002864 memory_region_v1_1->handle = memory_region_v1_0->handle;
J-Alves33c47bf2022-09-29 11:36:20 +01002865 memory_region_v1_1->attributes = memory_region_v1_0->attributes;
2866 memory_region_v1_1->flags = memory_region_v1_0->flags;
2867 memory_region_v1_1->tag = memory_region_v1_0->tag;
2868 memory_region_v1_1->memory_access_desc_size =
2869 sizeof(struct ffa_memory_access);
2870 memory_region_v1_1->receiver_count = memory_region_v1_0->receiver_count;
2871 memory_region_v1_1->receivers_offset =
2872 offsetof(struct ffa_memory_region, receivers);
2873
2874 /* Zero reserved fields. */
2875 for (uint32_t i = 0; i < 3U; i++) {
2876 memory_region_v1_1->reserved[i] = 0U;
2877 }
2878}
2879
2880/*
2881 * Checks the FF-A version of the lender and makes necessary updates.
2882 */
2883static struct ffa_value api_ffa_memory_send_per_ffa_version(
2884 void *allocated, struct ffa_memory_region **out_v1_1,
2885 uint32_t *fragment_length, uint32_t *total_length, uint32_t ffa_version)
2886{
2887 struct ffa_memory_region_v1_0 *memory_region_v1_0;
2888 struct ffa_memory_region *memory_region_v1_1 = NULL;
2889 struct ffa_composite_memory_region *composite_v1_0;
2890 struct ffa_composite_memory_region *composite_v1_1;
2891 size_t receivers_length;
2892 size_t space_left;
2893 size_t composite_offset_v1_1;
2894 size_t composite_offset_v1_0;
2895 size_t fragment_constituents_size;
2896 size_t fragment_length_v1_1;
2897
2898 assert(out_v1_1 != NULL);
2899 assert(fragment_length != NULL);
2900 assert(total_length != NULL);
2901
2902 *out_v1_1 = (struct ffa_memory_region *)allocated;
2903
2904 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2905 return (struct ffa_value){.func = FFA_SUCCESS_32};
2906 }
2907
2908 if (ffa_version != MAKE_FFA_VERSION(1, 0)) {
2909 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
2910 ffa_version);
2911 return ffa_error(FFA_NOT_SUPPORTED);
2912 }
2913
2914 dlog_verbose("FF-A v1.0 memory transaction descriptor.\n");
2915
2916 memory_region_v1_0 = (struct ffa_memory_region_v1_0 *)allocated;
2917
J-Alves33c47bf2022-09-29 11:36:20 +01002918 if (memory_region_v1_0->reserved_0 != 0U ||
2919 memory_region_v1_0->reserved_1 != 0U) {
2920 return ffa_error(FFA_INVALID_PARAMETERS);
2921 }
2922
2923 /* This should also prevent over flows. */
2924 if (memory_region_v1_0->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
2925 return ffa_error(FFA_INVALID_PARAMETERS);
2926 }
2927
2928 receivers_length = sizeof(struct ffa_memory_access) *
2929 memory_region_v1_0->receiver_count;
2930
2931 /*
2932 * Check the specified composite offset of v1.0 descriptor, and that all
2933 * receivers were configured with the same offset.
2934 */
2935 composite_offset_v1_0 =
2936 memory_region_v1_0->receivers[0].composite_memory_region_offset;
2937
2938 if (composite_offset_v1_0 == 0U ||
2939 composite_offset_v1_0 <
2940 sizeof(struct ffa_memory_region_v1_0) + receivers_length ||
2941 composite_offset_v1_0 + sizeof(struct ffa_composite_memory_region) >
2942 *fragment_length ||
2943 composite_offset_v1_0 > *fragment_length) {
2944 dlog_verbose(
2945 "Invalid composite memory region descriptor offset "
2946 "%d.\n",
2947 composite_offset_v1_0);
2948 return ffa_error(FFA_INVALID_PARAMETERS);
2949 }
2950
2951 for (uint32_t i = 1; i < memory_region_v1_0->receiver_count; i++) {
2952 const uint32_t current_offset =
2953 memory_region_v1_0->receivers[i]
2954 .composite_memory_region_offset;
2955
2956 if (current_offset != composite_offset_v1_0) {
2957 dlog_verbose(
2958 "Composite offset %x differs from %x in index "
2959 "%u\n",
2960 composite_offset_v1_0, current_offset, i);
2961 return ffa_error(FFA_INVALID_PARAMETERS);
2962 }
2963 }
2964
2965 fragment_constituents_size = *fragment_length - composite_offset_v1_0 -
2966 sizeof(struct ffa_composite_memory_region);
2967
2968 /* Determine the composite offset for v1.1 descriptor. */
2969 composite_offset_v1_1 =
2970 sizeof(struct ffa_memory_region) + receivers_length;
2971
2972 /* Determine final size of the v1.1 descriptor. */
2973 fragment_length_v1_1 = composite_offset_v1_1 +
2974 sizeof(struct ffa_composite_memory_region) +
2975 fragment_constituents_size;
2976
2977 /*
2978 * Currently only support the simpler cases: memory transaction
2979 * in a single fragment that fits in a MM_PPOOL_ENTRY_SIZE.
2980 * TODO: allocate the entries needed for this fragment_length_v1_1.
2981 * - Check corner when v1.1 descriptor converted size surpasses
2982 * the size of the entry.
2983 */
2984 if (fragment_length_v1_1 > MM_PPOOL_ENTRY_SIZE) {
2985 dlog_verbose(
2986 "Translation of FF-A v1.0 descriptors for over %u is "
2987 "unsupported.",
2988 MM_PPOOL_ENTRY_SIZE);
2989 return ffa_error(FFA_NOT_SUPPORTED);
2990 }
2991
2992 space_left = fragment_length_v1_1;
2993
2994 memory_region_v1_1 =
2995 (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
2996 if (memory_region_v1_1 == NULL) {
2997 return ffa_error(FFA_NO_MEMORY);
2998 }
2999
3000 /* Translate header from v1.0 to v1.1. */
3001 api_ffa_memory_region_v1_1_from_v1_0(memory_region_v1_0,
3002 memory_region_v1_1);
3003
3004 space_left -= sizeof(struct ffa_memory_region);
3005
3006 /* Copy memory access information. */
3007 memcpy_s(memory_region_v1_1->receivers, space_left,
3008 memory_region_v1_0->receivers, receivers_length);
3009
3010 /* Initialize the memory access descriptors with composite offset. */
3011 for (uint32_t i = 0; i < memory_region_v1_1->receiver_count; i++) {
3012 struct ffa_memory_access *receiver =
3013 &memory_region_v1_1->receivers[i];
3014
3015 receiver->composite_memory_region_offset =
3016 composite_offset_v1_1;
3017 }
3018
3019 space_left -= receivers_length;
3020
3021 /* Init v1.1 composite. */
3022 composite_v1_1 = (struct ffa_composite_memory_region
3023 *)((uint8_t *)memory_region_v1_1 +
3024 composite_offset_v1_1);
3025
3026 composite_v1_0 =
3027 ffa_memory_region_get_composite_v1_0(memory_region_v1_0, 0);
3028 composite_v1_1->constituent_count = composite_v1_0->constituent_count;
3029 composite_v1_1->page_count = composite_v1_0->page_count;
3030
3031 space_left -= sizeof(struct ffa_composite_memory_region);
3032
3033 /* Initialize v1.1 constituents. */
3034 memcpy_s(composite_v1_1->constituents, space_left,
3035 composite_v1_0->constituents, fragment_constituents_size);
3036
3037 space_left -= fragment_constituents_size;
3038 assert(space_left == 0U);
3039
3040 *out_v1_1 = memory_region_v1_1;
3041
3042 /*
3043 * Remove the v1.0 fragment size, and resultant size of v1.1 fragment.
3044 */
3045 *total_length = *total_length - *fragment_length + fragment_length_v1_1;
3046 *fragment_length = fragment_length_v1_1;
3047
3048 /*
3049 * After successfully convert to v1.1 free memory containing v1.0
3050 * descriptor.
3051 */
3052 mpool_free(&api_page_pool, allocated);
3053
3054 return (struct ffa_value){.func = FFA_SUCCESS_32};
3055}
3056
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003057struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
3058 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003059 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003060{
3061 struct vm *from = current->vm;
3062 struct vm *to;
3063 const void *from_msg;
J-Alves33c47bf2022-09-29 11:36:20 +01003064 void *allocated_entry;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003065 struct ffa_memory_region *memory_region;
3066 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01003067 bool targets_other_world = false;
J-Alves33c47bf2022-09-29 11:36:20 +01003068 uint32_t ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003069
3070 if (ipa_addr(address) != 0 || page_count != 0) {
3071 /*
3072 * Hafnium only supports passing the descriptor in the TX
3073 * mailbox.
3074 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003075 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003076 }
3077
Andrew Walbranca808b12020-05-15 17:22:28 +01003078 if (fragment_length > length) {
3079 dlog_verbose(
3080 "Fragment length %d greater than total length %d.\n",
3081 fragment_length, length);
3082 return ffa_error(FFA_INVALID_PARAMETERS);
3083 }
J-Alves33c47bf2022-09-29 11:36:20 +01003084
3085 if (fragment_length > HF_MAILBOX_SIZE ||
3086 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003087 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003088 }
3089
3090 /*
3091 * Check that the sender has configured its send buffer. If the TX
3092 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3093 * be safely accessed after releasing the lock since the TX mailbox
3094 * address can only be configured once.
3095 */
3096 sl_lock(&from->lock);
3097 from_msg = from->mailbox.send;
J-Alves33c47bf2022-09-29 11:36:20 +01003098 ffa_version = from->ffa_version;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003099 sl_unlock(&from->lock);
3100
3101 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003102 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003103 }
3104
3105 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003106 * Copy the memory region descriptor to a fresh page from the memory
3107 * pool. This prevents the sender from changing it underneath us, and
3108 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003109 */
J-Alves33c47bf2022-09-29 11:36:20 +01003110 allocated_entry = mpool_alloc(&api_page_pool);
3111 if (allocated_entry == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003112 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003113 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003114 }
J-Alves33c47bf2022-09-29 11:36:20 +01003115
3116 memcpy_s(allocated_entry, MM_PPOOL_ENTRY_SIZE, from_msg,
3117 fragment_length);
3118
3119 ret = api_ffa_memory_send_per_ffa_version(
3120 allocated_entry, &memory_region, &fragment_length, &length,
3121 ffa_version);
3122 if (ret.func != FFA_SUCCESS_32) {
3123 goto out;
3124 }
3125
3126 if (fragment_length < sizeof(struct ffa_memory_region) +
3127 sizeof(struct ffa_memory_access)) {
3128 dlog_verbose(
3129 "Initial fragment length %d smaller than header size "
3130 "%d.\n",
3131 fragment_length,
3132 sizeof(struct ffa_memory_region) +
3133 sizeof(struct ffa_memory_access));
3134 return ffa_error(FFA_INVALID_PARAMETERS);
3135 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003136
J-Alves84658fc2021-06-17 14:37:32 +01003137 if (!api_memory_region_check_flags(memory_region, share_func)) {
3138 dlog_verbose(
3139 "Memory region reserved arguments must be zero.\n");
3140 ret = ffa_error(FFA_INVALID_PARAMETERS);
3141 goto out;
3142 }
3143
J-Alves363f5722022-04-25 17:37:37 +01003144 if (memory_region->receiver_count == 0U) {
3145 dlog_verbose("Receiver count can't be 0.\n");
3146 ret = ffa_error(FFA_INVALID_PARAMETERS);
3147 goto out;
3148 }
3149
3150 if (share_func == FFA_MEM_DONATE_32 &&
3151 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003152 dlog_verbose(
J-Alves363f5722022-04-25 17:37:37 +01003153 "FFA_MEM_DONATE only supports one recipient. "
3154 "Specified %u\n",
3155 memory_region->receiver_count);
3156 ret = ffa_error(FFA_INVALID_PARAMETERS);
3157 goto out;
3158 }
3159
3160 if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
3161 dlog_verbose(
3162 "Max number of recipients supported is %u "
3163 "specified %u\n",
3164 MAX_MEM_SHARE_RECIPIENTS,
Andrew Walbrana65a1322020-04-06 19:32:32 +01003165 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003166 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003167 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003168 }
3169
3170 /*
3171 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01003172 * If there is a receiver from the other world, track it for later
3173 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003174 */
J-Alves363f5722022-04-25 17:37:37 +01003175 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
3176 ffa_vm_id_t receiver_id =
3177 memory_region->receivers[i]
3178 .receiver_permissions.receiver;
3179 to = vm_find(receiver_id);
3180
3181 if (vm_id_is_current_world(receiver_id) &&
3182 (to == NULL || to == from)) {
3183 dlog_verbose("%s: invalid receiver.\n", __func__);
3184 ret = ffa_error(FFA_INVALID_PARAMETERS);
3185 goto out;
3186 }
3187
3188 if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) {
3189 ret = ffa_error(FFA_DENIED);
3190 goto out;
3191 }
3192
3193 /* Capture if any of the receivers is from the other world. */
3194 if (!targets_other_world) {
3195 targets_other_world =
3196 !vm_id_is_current_world(receiver_id);
3197 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003198 }
3199
J-Alves363f5722022-04-25 17:37:37 +01003200 if (targets_other_world) {
J-Alves66652252022-07-06 09:49:51 +01003201 ret = plat_ffa_other_world_mem_send(
3202 from, share_func, &memory_region, length,
3203 fragment_length, &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003204 } else {
3205 struct vm_locked from_locked = vm_lock(from);
3206
Andrew Walbran1a86aa92020-05-15 17:22:28 +01003207 ret = ffa_memory_send(from_locked, memory_region, length,
3208 fragment_length, share_func,
3209 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003210 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003211 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003212 * make sure we don't free it.
3213 */
3214 memory_region = NULL;
3215
3216 vm_unlock(&from_locked);
3217 }
3218
3219out:
3220 if (memory_region != NULL) {
3221 mpool_free(&api_page_pool, memory_region);
3222 }
3223
3224 return ret;
3225}
3226
J-Alves00847062022-10-03 17:08:44 +01003227static struct ffa_value api_ffa_mem_retrieve_req_version_update(
3228 void *retrieve_msg, uint32_t retrieve_msg_buffer_size,
3229 struct ffa_memory_region **out_v1_1, uint32_t *fragment_length,
3230 uint32_t ffa_version)
3231{
3232 struct ffa_memory_region_v1_0 *retrieve_request_v1_0;
3233 struct ffa_memory_region *retrieve_request_v1_1;
3234 size_t fragment_length_v1_1;
3235 uint32_t expected_retrieve_request_length_v1_0;
3236 size_t space_left = retrieve_msg_buffer_size;
3237 size_t receivers_length;
3238
3239 assert(out_v1_1 != NULL);
3240 assert(fragment_length != NULL);
3241 assert(retrieve_msg != NULL);
3242
3243 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
3244 *out_v1_1 = (struct ffa_memory_region *)retrieve_msg;
3245 return (struct ffa_value){.func = FFA_SUCCESS_32};
3246 }
3247 if (ffa_version != MAKE_FFA_VERSION(1, 0)) {
3248 dlog_verbose("%s: Unsupported FF-A version %x\n", __func__,
3249 ffa_version);
3250 return ffa_error(FFA_NOT_SUPPORTED);
3251 }
3252
3253 retrieve_request_v1_0 = (struct ffa_memory_region_v1_0 *)retrieve_msg;
3254
3255 if (retrieve_request_v1_0->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
3256 dlog_verbose(
3257 "Specified more than expected maximum receivers.\n");
3258 return ffa_error(FFA_INVALID_PARAMETERS);
3259 }
3260
3261 receivers_length = retrieve_request_v1_0->receiver_count *
3262 sizeof(struct ffa_memory_access);
3263
3264 expected_retrieve_request_length_v1_0 =
3265 sizeof(struct ffa_memory_region_v1_0) + receivers_length;
3266
3267 if (*fragment_length != expected_retrieve_request_length_v1_0) {
3268 dlog_verbose("Retrieve request size is not as expected.\n");
3269 return ffa_error(FFA_INVALID_PARAMETERS);
3270 }
3271
3272 /* Determine expected v1.1 retrieve request size. */
3273 fragment_length_v1_1 = sizeof(struct ffa_memory_region) +
3274 retrieve_request_v1_0->receiver_count *
3275 sizeof(struct ffa_memory_access);
3276
3277 /*
3278 * At this point there is the assumption that the retrieve request has
3279 * been copied to an internal buffer to prevent TOCTOU attacks.
3280 * The translation of the resultant v1.1 transaction descriptor will be
3281 * written to that same buffer. That said, the referred buffer needs
3282 * space to accommodate both v1.0 and v1.1 descriptors simultaneously.
3283 */
3284 assert(fragment_length_v1_1 + expected_retrieve_request_length_v1_0 <=
3285 retrieve_msg_buffer_size);
3286
3287 space_left -= expected_retrieve_request_length_v1_0;
3288
3289 /*
3290 * Prepare to write the resultant FF-A v1.1 retrieve request in an
3291 * offset following the FF-A v1.0 within the same buffer.
3292 */
3293 retrieve_request_v1_1 =
3294 // NOLINTNEXTLINE(performance-no-int-to-ptr)
3295 (struct ffa_memory_region *)((uintptr_t)retrieve_msg +
3296 *fragment_length);
3297
3298 api_ffa_memory_region_v1_1_from_v1_0(retrieve_request_v1_0,
3299 retrieve_request_v1_1);
3300
3301 space_left -= sizeof(struct ffa_memory_region);
3302
3303 /* Copy memory access information. */
3304 memcpy_s(retrieve_request_v1_1->receivers, space_left,
3305 retrieve_request_v1_0->receivers, receivers_length);
3306
3307 /* Initialize the memory access descriptors with composite offset. */
3308 for (uint32_t i = 0; i < retrieve_request_v1_1->receiver_count; i++) {
3309 struct ffa_memory_access *receiver =
3310 &retrieve_request_v1_1->receivers[i];
3311
3312 receiver->composite_memory_region_offset = 0U;
3313 }
3314
3315 *fragment_length = fragment_length_v1_1;
3316 *out_v1_1 = retrieve_request_v1_1;
3317
3318 return (struct ffa_value){.func = FFA_SUCCESS_32};
3319}
3320
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003321struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
3322 uint32_t fragment_length,
3323 ipaddr_t address, uint32_t page_count,
3324 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003325{
3326 struct vm *to = current->vm;
3327 struct vm_locked to_locked;
3328 const void *to_msg;
J-Alves00847062022-10-03 17:08:44 +01003329 void *retrieve_msg;
3330 struct ffa_memory_region *retrieve_request = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003331 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003332 struct ffa_value ret;
J-Alves00847062022-10-03 17:08:44 +01003333 uint32_t ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003334
3335 if (ipa_addr(address) != 0 || page_count != 0) {
3336 /*
3337 * Hafnium only supports passing the descriptor in the TX
3338 * mailbox.
3339 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003340 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003341 }
3342
Andrew Walbrana65a1322020-04-06 19:32:32 +01003343 if (fragment_length != length) {
J-Alves33c47bf2022-09-29 11:36:20 +01003344 dlog_verbose("Fragmentation not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003345 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003346 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003347
J-Alves00847062022-10-03 17:08:44 +01003348 retrieve_msg = cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003349 message_buffer_size = cpu_get_buffer_size(current->cpu);
3350 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3351 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003352 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003353 }
3354
3355 to_locked = vm_lock(to);
3356 to_msg = to->mailbox.send;
J-Alves00847062022-10-03 17:08:44 +01003357 ffa_version = to->ffa_version;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003358
3359 if (to_msg == NULL) {
3360 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003361 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003362 goto out;
3363 }
3364
3365 /*
3366 * Copy the retrieve request descriptor to an internal buffer, so that
3367 * the caller can't change it underneath us.
3368 */
J-Alves00847062022-10-03 17:08:44 +01003369 memcpy_s(retrieve_msg, message_buffer_size, to_msg, length);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003370
J-Alvese8c8c2b2022-12-16 15:34:48 +00003371 if ((vm_is_mailbox_other_world_owned(to_locked) &&
3372 !plat_ffa_acquire_receiver_rx(to_locked, &ret)) ||
3373 vm_is_mailbox_busy(to_locked)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003374 /*
J-Alvese8c8c2b2022-12-16 15:34:48 +00003375 * Can't retrieve memory information if the mailbox is
3376 * not available.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003377 */
J-Alves59ed0042022-07-28 18:26:41 +01003378 dlog_verbose("%s: RX buffer not ready.\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003379 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003380 goto out;
3381 }
3382
J-Alves00847062022-10-03 17:08:44 +01003383 /*
3384 * If required, transform the retrieve request to FF-A v1.1.
3385 */
3386 ret = api_ffa_mem_retrieve_req_version_update(
3387 retrieve_msg, message_buffer_size, &retrieve_request, &length,
3388 ffa_version);
3389 assert(retrieve_request != NULL);
3390
3391 if (ret.func != FFA_SUCCESS_32) {
3392 goto out;
3393 }
3394
J-Alvesb5084cf2022-07-06 14:20:12 +01003395 if (plat_ffa_memory_handle_allocated_by_current_world(
3396 retrieve_request->handle)) {
3397 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
3398 &api_page_pool);
3399 } else {
3400 ret = plat_ffa_other_world_mem_retrieve(
3401 to_locked, retrieve_request, length, &api_page_pool);
3402 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003403out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003404 vm_unlock(&to_locked);
3405 return ret;
3406}
3407
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003408struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003409{
3410 struct vm *from = current->vm;
3411 struct vm_locked from_locked;
3412 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003413 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003414 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003415 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003416 uint32_t length;
3417
3418 from_locked = vm_lock(from);
3419 from_msg = from->mailbox.send;
3420
3421 if (from_msg == NULL) {
3422 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003423 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003424 goto out;
3425 }
3426
3427 /*
3428 * Calculate length from relinquish descriptor before copying. We will
3429 * check again later to make sure it hasn't changed.
3430 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003431 length = sizeof(struct ffa_mem_relinquish) +
3432 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
3433 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003434 /*
3435 * Copy the relinquish descriptor to an internal buffer, so that the
3436 * caller can't change it underneath us.
3437 */
3438 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003439 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003440 message_buffer_size = cpu_get_buffer_size(current->cpu);
3441 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3442 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003443 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003444 goto out;
3445 }
3446 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
3447
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003448 if (sizeof(struct ffa_mem_relinquish) +
3449 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003450 length) {
3451 dlog_verbose(
3452 "Endpoint count changed while copying to internal "
3453 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003454 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003455 goto out;
3456 }
3457
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003458 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3459 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003460
3461out:
3462 vm_unlock(&from_locked);
3463 return ret;
3464}
3465
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003466struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3467 ffa_memory_region_flags_t flags,
3468 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003469{
3470 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003471 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003472
Olivier Deprez55a189e2021-06-09 15:45:27 +02003473 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003474 struct vm_locked to_locked = vm_lock(to);
3475
Andrew Walbranca808b12020-05-15 17:22:28 +01003476 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003477 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003478
Andrew Walbran290b0c92020-02-03 16:37:14 +00003479 vm_unlock(&to_locked);
3480 } else {
J-Alvesfc19b372022-07-06 12:17:35 +01003481 ret = plat_ffa_other_world_mem_reclaim(to, handle, flags,
3482 &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003483 }
3484
3485 return ret;
3486}
3487
3488struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3489 uint32_t fragment_offset,
3490 ffa_vm_id_t sender_vm_id,
3491 struct vcpu *current)
3492{
3493 struct vm *to = current->vm;
3494 struct vm_locked to_locked;
3495 struct ffa_value ret;
3496
3497 /* Sender ID MBZ at virtual instance. */
J-Alves59ed0042022-07-28 18:26:41 +01003498 if (vm_id_is_current_world(to->id)) {
3499 if (sender_vm_id != 0) {
3500 dlog_verbose("%s: Invalid sender.\n", __func__);
3501 return ffa_error(FFA_INVALID_PARAMETERS);
3502 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003503 }
3504
3505 to_locked = vm_lock(to);
3506
J-Alves122f1a12022-12-12 15:55:42 +00003507 if (vm_is_mailbox_busy(to_locked)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003508 /*
3509 * Can't retrieve memory information if the mailbox is not
3510 * available.
3511 */
J-Alves59ed0042022-07-28 18:26:41 +01003512 dlog_verbose("%s: RX buffer not ready partition %x.\n",
3513 __func__, to_locked.vm->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003514 ret = ffa_error(FFA_BUSY);
3515 goto out;
3516 }
3517
3518 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01003519 sender_vm_id, &api_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01003520out:
3521 vm_unlock(&to_locked);
3522 return ret;
3523}
3524
3525struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3526 uint32_t fragment_length,
3527 ffa_vm_id_t sender_vm_id,
3528 struct vcpu *current)
3529{
3530 struct vm *from = current->vm;
3531 const void *from_msg;
3532 void *fragment_copy;
3533 struct ffa_value ret;
3534
3535 /* Sender ID MBZ at virtual instance. */
J-Alvesfdd29272022-07-19 13:16:31 +01003536 if (vm_id_is_current_world(from->id) && sender_vm_id != 0) {
3537 dlog_verbose("Invalid sender.");
Andrew Walbranca808b12020-05-15 17:22:28 +01003538 return ffa_error(FFA_INVALID_PARAMETERS);
3539 }
3540
3541 /*
3542 * Check that the sender has configured its send buffer. If the TX
3543 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3544 * be safely accessed after releasing the lock since the TX mailbox
3545 * address can only be configured once.
3546 */
3547 sl_lock(&from->lock);
3548 from_msg = from->mailbox.send;
3549 sl_unlock(&from->lock);
3550
3551 if (from_msg == NULL) {
J-Alves59ed0042022-07-28 18:26:41 +01003552 dlog_verbose("Mailbox from %x is not set.\n", from->id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003553 return ffa_error(FFA_INVALID_PARAMETERS);
3554 }
3555
3556 /*
3557 * Copy the fragment to a fresh page from the memory pool. This prevents
3558 * the sender from changing it underneath us, and also lets us keep it
3559 * around in the share state table if needed.
3560 */
3561 if (fragment_length > HF_MAILBOX_SIZE ||
3562 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3563 dlog_verbose(
3564 "Fragment length %d larger than mailbox size %d.\n",
3565 fragment_length, HF_MAILBOX_SIZE);
3566 return ffa_error(FFA_INVALID_PARAMETERS);
3567 }
3568 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
3569 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
3570 0) {
3571 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
3572 return ffa_error(FFA_INVALID_PARAMETERS);
3573 }
3574 fragment_copy = mpool_alloc(&api_page_pool);
3575 if (fragment_copy == NULL) {
3576 dlog_verbose("Failed to allocate fragment copy.\n");
3577 return ffa_error(FFA_NO_MEMORY);
3578 }
3579 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
3580
3581 /*
3582 * Hafnium doesn't support fragmentation of memory retrieve requests
3583 * (because it doesn't support caller-specified mappings, so a request
3584 * will never be larger than a single page), so this must be part of a
3585 * memory send (i.e. donate, lend or share) request.
3586 *
3587 * We can tell from the handle whether the memory transaction is for the
J-Alvesfdd29272022-07-19 13:16:31 +01003588 * other world or not.
Andrew Walbranca808b12020-05-15 17:22:28 +01003589 */
J-Alvesfdd29272022-07-19 13:16:31 +01003590 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003591 struct vm_locked from_locked = vm_lock(from);
3592
3593 ret = ffa_memory_send_continue(from_locked, fragment_copy,
3594 fragment_length, handle,
3595 &api_page_pool);
3596 /*
3597 * `ffa_memory_send_continue` takes ownership of the
3598 * fragment_copy, so we don't need to free it here.
3599 */
3600 vm_unlock(&from_locked);
3601 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01003602 ret = plat_ffa_other_world_mem_send_continue(
3603 from, fragment_copy, fragment_length, handle,
3604 &api_page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003605 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003606
3607 return ret;
3608}
Max Shvetsov40108e72020-08-27 12:39:50 +01003609
Olivier Deprezd614d322021-06-18 15:21:00 +02003610/**
3611 * Register an entry point for a vCPU in warm boot cases.
3612 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
3613 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003614struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
3615 struct vcpu *current)
3616{
3617 struct vm_locked vm_locked;
Olivier Deprezb2808332023-02-02 15:25:40 +01003618 struct vcpu_locked current_locked;
Max Shvetsov40108e72020-08-27 12:39:50 +01003619
Olivier Deprezd614d322021-06-18 15:21:00 +02003620 /*
3621 * Reject if interface is not supported at this FF-A instance
3622 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
3623 */
3624 if (!plat_ffa_is_secondary_ep_register_supported() ||
3625 current->vm->vcpu_count == 1) {
3626 return ffa_error(FFA_NOT_SUPPORTED);
3627 }
3628
3629 /*
3630 * No further check is made on the address validity
3631 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
3632 * from the VM or vCPU structure.
3633 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
3634 * For each SP [...] the Framework assumes that the same entry point
3635 * address is used for initializing any execution context during a
3636 * secondary cold boot.
3637 * If this function is invoked multiple times, then the entry point
3638 * address specified in the last valid invocation must be used by the
3639 * callee.
3640 */
Olivier Deprezb2808332023-02-02 15:25:40 +01003641 current_locked = vcpu_lock(current);
3642 if (current->rt_model != RTM_SP_INIT) {
3643 dlog_error(
3644 "FFA_SECONDARY_EP_REGISTER can only be called while "
3645 "vCPU in run-time state for initialization.\n");
3646 vcpu_unlock(&current_locked);
3647 return ffa_error(FFA_DENIED);
Olivier Deprezd614d322021-06-18 15:21:00 +02003648 }
Olivier Deprezb2808332023-02-02 15:25:40 +01003649 vcpu_unlock(&current_locked);
Olivier Deprezd614d322021-06-18 15:21:00 +02003650
Olivier Deprezb2808332023-02-02 15:25:40 +01003651 vm_locked = vm_lock(current->vm);
Max Shvetsov40108e72020-08-27 12:39:50 +01003652 vm_locked.vm->secondary_ep = entry_point;
3653 vm_unlock(&vm_locked);
3654
Olivier Deprezb2808332023-02-02 15:25:40 +01003655 return (struct ffa_value){.func = FFA_SUCCESS_32};
Max Shvetsov40108e72020-08-27 12:39:50 +01003656}
J-Alvesa0f317d2021-06-09 13:31:59 +01003657
3658struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
3659 ffa_vcpu_count_t vcpu_count,
3660 struct vcpu *current)
3661{
3662 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3663 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
3664 vm_id);
3665 return ffa_error(FFA_NOT_SUPPORTED);
3666 }
3667
3668 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
3669}
3670
3671struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
3672 struct vcpu *current)
3673{
3674 /*
3675 * Validity of use of this interface is the same as for bitmap create.
3676 */
3677 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3678 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
3679 vm_id);
3680 return ffa_error(FFA_NOT_SUPPORTED);
3681 }
3682
3683 return plat_ffa_notifications_bitmap_destroy(vm_id);
3684}
J-Alvesc003a7a2021-03-18 13:06:53 +00003685
3686struct ffa_value api_ffa_notification_update_bindings(
3687 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3688 ffa_notifications_bitmap_t notifications, bool is_bind,
3689 struct vcpu *current)
3690{
3691 struct ffa_value ret = {.func = FFA_SUCCESS_32};
3692 struct vm_locked receiver_locked;
3693 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3694 const ffa_vm_id_t id_to_update =
3695 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
3696 const ffa_vm_id_t id_to_validate =
3697 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
J-Alves1daeaea2022-09-06 17:41:24 +01003698 const uint32_t flags_mbz =
3699 is_bind ? ~FFA_NOTIFICATIONS_FLAG_PER_VCPU : ~0U;
3700
3701 if ((flags_mbz & flags) != 0U) {
3702 return ffa_error(FFA_INVALID_PARAMETERS);
3703 }
J-Alvesc003a7a2021-03-18 13:06:53 +00003704
3705 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
3706 receiver_vm_id)) {
3707 dlog_verbose("Invalid use of notifications bind interface.\n");
3708 return ffa_error(FFA_INVALID_PARAMETERS);
3709 }
3710
J-Alvesb15e9402021-09-08 11:44:42 +01003711 if (plat_ffa_notifications_update_bindings_forward(
3712 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
3713 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01003714 return ret;
3715 }
3716
J-Alvesc003a7a2021-03-18 13:06:53 +00003717 if (notifications == 0U) {
3718 dlog_verbose("No notifications have been specified.\n");
3719 return ffa_error(FFA_INVALID_PARAMETERS);
3720 }
3721
3722 /**
3723 * This check assumes receiver is the current VM, and has been enforced
3724 * by 'plat_ffa_is_notifications_bind_valid'.
3725 */
3726 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3727
3728 if (receiver_locked.vm == NULL) {
3729 dlog_verbose("Receiver doesn't exist!\n");
3730 return ffa_error(FFA_DENIED);
3731 }
3732
J-Alves09ff9d82021-11-02 11:55:20 +00003733 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003734 dlog_verbose("Notifications are not enabled.\n");
3735 ret = ffa_error(FFA_NOT_SUPPORTED);
3736 goto out;
3737 }
3738
3739 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3740 vm_find(sender_vm_id) == NULL) {
3741 dlog_verbose("Sender VM does not exist!\n");
3742 ret = ffa_error(FFA_INVALID_PARAMETERS);
3743 goto out;
3744 }
3745
3746 /*
3747 * Can't bind/unbind notifications if at least one is bound to a
3748 * different sender.
3749 */
3750 if (!vm_notifications_validate_bound_sender(
3751 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3752 id_to_validate, notifications)) {
3753 dlog_verbose("Notifications are bound to other sender.\n");
3754 ret = ffa_error(FFA_DENIED);
3755 goto out;
3756 }
3757
3758 /**
3759 * Check if there is a pending notification within those specified in
3760 * the bitmap.
3761 */
3762 if (vm_are_notifications_pending(receiver_locked,
3763 plat_ffa_is_vm_id(sender_vm_id),
3764 notifications)) {
3765 dlog_verbose("Notifications within '%x' pending.\n",
3766 notifications);
3767 ret = ffa_error(FFA_DENIED);
3768 goto out;
3769 }
3770
3771 vm_notifications_update_bindings(
3772 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3773 notifications, is_per_vcpu && is_bind);
3774
3775out:
3776 vm_unlock(&receiver_locked);
3777 return ret;
3778}
J-Alvesaa79c012021-07-09 14:29:45 +01003779
3780struct ffa_value api_ffa_notification_set(
3781 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3782 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3783{
3784 struct ffa_value ret;
3785 struct vm_locked receiver_locked;
3786
3787 /*
3788 * Check if is per-vCPU or global, and extracting vCPU ID according
3789 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3790 */
3791 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3792 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3793
J-Alvesaa79c012021-07-09 14:29:45 +01003794 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3795 receiver_vm_id)) {
3796 dlog_verbose("Invalid use of notifications set interface.\n");
3797 return ffa_error(FFA_INVALID_PARAMETERS);
3798 }
3799
3800 if (notifications == 0U) {
3801 dlog_verbose("No notifications have been specified.\n");
3802 return ffa_error(FFA_INVALID_PARAMETERS);
3803 }
3804
J-Alvesde7bd2f2021-09-09 19:54:35 +01003805 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3806 flags, notifications, &ret)) {
3807 return ret;
3808 }
3809
J-Alvesaa79c012021-07-09 14:29:45 +01003810 /*
3811 * This check assumes receiver is the current VM, and has been enforced
3812 * by 'plat_ffa_is_notification_set_valid'.
3813 */
3814 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3815
3816 if (receiver_locked.vm == NULL) {
3817 dlog_verbose("Receiver ID is not valid.\n");
3818 return ffa_error(FFA_INVALID_PARAMETERS);
3819 }
3820
J-Alves09ff9d82021-11-02 11:55:20 +00003821 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003822 dlog_verbose("Receiver's notifications not enabled.\n");
3823 ret = ffa_error(FFA_DENIED);
3824 goto out;
3825 }
3826
3827 /*
3828 * If notifications are not bound to the sender, they wouldn't be
3829 * enabled either for the receiver.
3830 */
3831 if (!vm_notifications_validate_binding(
3832 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3833 sender_vm_id, notifications, is_per_vcpu)) {
3834 dlog_verbose("Notifications bindings not valid.\n");
3835 ret = ffa_error(FFA_DENIED);
3836 goto out;
3837 }
3838
3839 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3840 dlog_verbose("Invalid VCPU ID!\n");
3841 ret = ffa_error(FFA_INVALID_PARAMETERS);
3842 goto out;
3843 }
3844
J-Alves7461ef22021-10-18 17:21:33 +01003845 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003846 vm_notifications_partition_set_pending(
3847 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3848 vcpu_id, is_per_vcpu);
3849
J-Alvesaa79c012021-07-09 14:29:45 +01003850 dlog_verbose("Set the notifications: %x.\n", notifications);
3851
J-Alves13394022021-06-30 13:48:49 +01003852 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3853 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3854 vcpu_index(current));
3855 plat_ffa_sri_trigger_not_delayed(current->cpu);
3856 } else {
3857 plat_ffa_sri_state_set(DELAYED);
3858 }
J-Alvesaa79c012021-07-09 14:29:45 +01003859
J-Alves13394022021-06-30 13:48:49 +01003860 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003861out:
3862 vm_unlock(&receiver_locked);
3863
3864 return ret;
3865}
3866
3867static struct ffa_value api_ffa_notification_get_success_return(
3868 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3869 ffa_notifications_bitmap_t from_framework)
3870{
3871 return (struct ffa_value){
3872 .func = FFA_SUCCESS_32,
3873 .arg1 = 0U,
3874 .arg2 = (uint32_t)from_sp,
3875 .arg3 = (uint32_t)(from_sp >> 32),
3876 .arg4 = (uint32_t)from_vm,
3877 .arg5 = (uint32_t)(from_vm >> 32),
3878 .arg6 = (uint32_t)from_framework,
3879 .arg7 = (uint32_t)(from_framework >> 32),
3880 };
3881}
3882
3883struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3884 ffa_vcpu_index_t vcpu_id,
3885 uint32_t flags, struct vcpu *current)
3886{
J-Alves663682a2022-03-25 13:56:51 +00003887 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003888 ffa_notifications_bitmap_t sp_notifications = 0;
3889 ffa_notifications_bitmap_t vm_notifications = 0;
3890 struct vm_locked receiver_locked;
3891 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003892 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3893 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3894 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3895 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3896
3897 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3898 if ((flags & flags_mbz) != 0U) {
3899 dlog_verbose(
3900 "Invalid flags bit(s) set in notifications get. [31:4] "
3901 "MBZ(%x)\n",
3902 flags);
3903 return ffa_error(FFA_INVALID_PARAMETERS);
3904 }
J-Alvesaa79c012021-07-09 14:29:45 +01003905
3906 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003907 * Following check should capture wrong uses of the interface,
3908 * depending on whether Hafnium is SPMC or hypervisor. On the
3909 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003910 */
J-Alvesfc95a302022-04-22 14:18:23 +01003911 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3912 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003913 dlog_verbose("Invalid use of notifications get interface.\n");
3914 return ffa_error(FFA_INVALID_PARAMETERS);
3915 }
3916
3917 /*
3918 * This check assumes receiver is the current VM, and has been enforced
3919 * by `plat_ffa_is_notifications_get_valid`.
3920 */
3921 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3922
3923 /*
3924 * `plat_ffa_is_notifications_get_valid` ensures following is never
3925 * true.
3926 */
3927 CHECK(receiver_locked.vm != NULL);
3928
3929 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3930 (receiver_locked.vm->vcpu_count != 1 &&
3931 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003932 dlog_verbose(
3933 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3934 vcpu_id, receiver_locked.vm->vcpu_count,
3935 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003936 ret = ffa_error(FFA_INVALID_PARAMETERS);
3937 goto out;
3938 }
3939
3940 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003941 if (!plat_ffa_notifications_get_from_sp(
3942 receiver_locked, vcpu_id, &sp_notifications,
3943 &ret)) {
3944 dlog_verbose("Failed to get notifications from sps.");
3945 goto out;
3946 }
J-Alvesaa79c012021-07-09 14:29:45 +01003947 }
3948
3949 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003950 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003951 receiver_locked, true, vcpu_id);
3952 }
3953
J-Alvesd605a092022-03-28 14:20:48 +01003954 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3955 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3956 if (!plat_ffa_notifications_get_framework_notifications(
3957 receiver_locked, &framework_notifications, flags,
3958 vcpu_id, &ret)) {
3959 dlog_verbose(
3960 "Failed to get notifications from "
3961 "framework.\n");
3962 goto out;
3963 }
3964 }
3965
J-Alves663682a2022-03-25 13:56:51 +00003966 ret = api_ffa_notification_get_success_return(
3967 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003968
J-Alvesfe23ebe2021-10-13 16:07:07 +01003969 /*
3970 * If there are no more pending notifications, change `sri_state` to
3971 * handled.
3972 */
3973 if (vm_is_notifications_pending_count_zero()) {
3974 plat_ffa_sri_state_set(HANDLED);
3975 }
3976
J-Alves6e2abc62021-12-02 14:58:56 +00003977 if (!receiver_locked.vm->el0_partition &&
3978 !vm_are_global_notifications_pending(receiver_locked)) {
3979 vm_notifications_set_npi_injected(receiver_locked, false);
3980 }
3981
J-Alvesaa79c012021-07-09 14:29:45 +01003982out:
3983 vm_unlock(&receiver_locked);
3984
3985 return ret;
3986}
J-Alvesc8e8a222021-06-08 17:33:52 +01003987
3988/**
3989 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3990 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3991 */
3992static struct ffa_value api_ffa_notification_info_get_success_return(
3993 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003994 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003995{
3996 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3997
3998 /*
3999 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
4000 * hold the list of ids.
4001 */
4002 memcpy_s(&ret.arg3,
4003 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
4004 sizeof(ids[0]) * ids_count);
4005
4006 /*
4007 * According to the spec x2 should have:
4008 * - Bit flagging if there are more notifications pending;
4009 * - The total number of elements (i.e. total list size);
4010 * - The number of VCPU IDs within each VM specific list.
4011 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004012 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
4013 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
4014 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01004015
4016 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
4017 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
4018
4019 for (unsigned int i = 0; i < lists_count; i++) {
4020 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
4021 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
4022 }
4023
4024 return ret;
4025}
4026
4027struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
4028{
4029 /*
4030 * Following set of variables should be populated with the return info.
4031 * At a successfull handling of this interface, they should be used
4032 * to populate the 'ret' structure in accordance to the table 17.29
4033 * of the FF-A v1.1 Beta0 specification.
4034 */
4035 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
4036 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
4037 uint32_t lists_count = 0;
4038 uint32_t ids_count = 0;
4039 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01004040 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004041
4042 /*
4043 * This interface can only be called at NS virtual/physical FF-A
4044 * instance by the endpoint implementing the primary scheduler and the
4045 * Hypervisor/OS kernel.
4046 * In the SPM, following check passes if call has been forwarded from
4047 * the hypervisor.
4048 */
4049 if (current->vm->id != HF_PRIMARY_VM_ID) {
4050 dlog_verbose(
4051 "Only the receiver's scheduler can use this "
4052 "interface\n");
4053 return ffa_error(FFA_NOT_SUPPORTED);
4054 }
4055
J-Alvesca058c22021-09-10 14:02:07 +01004056 /*
4057 * Forward call to the other world, and fill the arrays used to assemble
4058 * return.
4059 */
4060 plat_ffa_notification_info_get_forward(
4061 ids, &ids_count, lists_sizes, &lists_count,
4062 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4063
4064 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
4065
J-Alvesc8e8a222021-06-08 17:33:52 +01004066 /* Get notifications' info from this world */
4067 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
4068 ++index) {
4069 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
4070
4071 list_is_full = vm_notifications_info_get(
4072 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
4073 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4074
4075 vm_unlock(&vm_locked);
4076 }
4077
4078 if (!list_is_full) {
4079 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01004080 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01004081 ids, &ids_count, lists_sizes, &lists_count,
4082 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
4083 }
4084
4085 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01004086 dlog_verbose(
4087 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01004088 result = ffa_error(FFA_NO_DATA);
4089 } else {
4090 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01004091 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01004092 }
4093
J-Alvesfe23ebe2021-10-13 16:07:07 +01004094 plat_ffa_sri_state_set(HANDLED);
4095
J-Alves13394022021-06-30 13:48:49 +01004096 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01004097}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07004098
4099struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
4100{
4101 struct vm_locked vm_locked;
4102 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
4103 bool mode_ret = false;
4104 uint32_t mode = 0;
4105
4106 if (!plat_ffa_is_mem_perm_get_valid(current)) {
4107 return ffa_error(FFA_NOT_SUPPORTED);
4108 }
4109
4110 if (!(current->vm->el0_partition)) {
4111 return ffa_error(FFA_DENIED);
4112 }
4113
4114 vm_locked = vm_lock(current->vm);
4115
4116 /*
4117 * mm_get_mode is used to check if the given base_addr page is already
4118 * mapped. If the page is unmapped, return error. If the page is mapped
4119 * appropriate attributes are returned to the caller. Note that
4120 * mm_get_mode returns true if the address is in the valid VA range as
4121 * supported by the architecture and MMU configurations, as opposed to
4122 * whether a page is mapped or not. For a page to be known as mapped,
4123 * the API must return true AND the returned mode must not have
4124 * MM_MODE_INVALID set.
4125 */
4126 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4127 va_add(base_addr, PAGE_SIZE), &mode);
4128 if (!mode_ret || (mode & MM_MODE_INVALID)) {
4129 ret = ffa_error(FFA_INVALID_PARAMETERS);
4130 goto out;
4131 }
4132
4133 /* No memory should be marked RWX */
4134 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
4135 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
4136
4137 /*
4138 * S-EL0 partitions are expected to have all their pages marked as
4139 * non-global.
4140 */
4141 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
4142 (MM_MODE_NG | MM_MODE_USER));
4143
4144 if (mode & MM_MODE_W) {
4145 /* No memory should be writeable but not readable. */
4146 CHECK(mode & MM_MODE_R);
4147 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4148 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
4149 } else if (mode & MM_MODE_R) {
4150 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
4151 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
4152 if (!(mode & MM_MODE_X)) {
4153 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
4154 }
4155 }
4156out:
4157 vm_unlock(&vm_locked);
4158 return ret;
4159}
4160
4161struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
4162 uint32_t mem_perm, struct vcpu *current)
4163{
4164 struct vm_locked vm_locked;
4165 struct ffa_value ret;
4166 bool mode_ret = false;
4167 uint32_t original_mode;
4168 uint32_t new_mode;
4169 struct mpool local_page_pool;
4170
4171 if (!plat_ffa_is_mem_perm_set_valid(current)) {
4172 return ffa_error(FFA_NOT_SUPPORTED);
4173 }
4174
4175 if (!(current->vm->el0_partition)) {
4176 return ffa_error(FFA_DENIED);
4177 }
4178
4179 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
4180 return ffa_error(FFA_INVALID_PARAMETERS);
4181 }
4182
4183 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
4184 (mem_perm != FFA_MEM_PERM_RX)) {
4185 return ffa_error(FFA_INVALID_PARAMETERS);
4186 }
4187
4188 /*
4189 * Create a local pool so any freed memory can't be used by another
4190 * thread. This is to ensure the original mapping can be restored if any
4191 * stage of the process fails.
4192 */
4193 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
4194
4195 vm_locked = vm_lock(current->vm);
4196
4197 /*
4198 * All regions accessible by the partition are mapped during boot. If we
4199 * cannot get a successful translation for the page range, the request
4200 * to change permissions is rejected.
4201 * mm_get_mode is used to check if the given address range is already
4202 * mapped. If the range is unmapped, return error. If the range is
4203 * mapped appropriate attributes are returned to the caller. Note that
4204 * mm_get_mode returns true if the address is in the valid VA range as
4205 * supported by the architecture and MMU configurations, as opposed to
4206 * whether a page is mapped or not. For a page to be known as mapped,
4207 * the API must return true AND the returned mode must not have
4208 * MM_MODE_INVALID set.
4209 */
4210
4211 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
4212 va_add(base_addr, page_count * PAGE_SIZE),
4213 &original_mode);
4214 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
4215 ret = ffa_error(FFA_INVALID_PARAMETERS);
4216 goto out;
4217 }
4218
4219 /* Device memory cannot be marked as executable */
4220 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
4221 ret = ffa_error(FFA_INVALID_PARAMETERS);
4222 goto out;
4223 }
4224
4225 new_mode = MM_MODE_USER | MM_MODE_NG;
4226
4227 if (mem_perm == FFA_MEM_PERM_RW) {
4228 new_mode |= MM_MODE_R | MM_MODE_W;
4229 } else if (mem_perm == FFA_MEM_PERM_RX) {
4230 new_mode |= MM_MODE_R | MM_MODE_X;
4231 } else if (mem_perm == FFA_MEM_PERM_RO) {
4232 new_mode |= MM_MODE_R;
4233 }
4234
4235 /*
4236 * Safe to re-map memory, since we know the requested permissions are
4237 * valid, and the memory requested to be re-mapped is also valid.
4238 */
4239 if (!mm_identity_prepare(
4240 &vm_locked.vm->ptable, pa_from_va(base_addr),
4241 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4242 new_mode, &local_page_pool)) {
4243 /*
4244 * Defrag the table into the local page pool.
4245 * mm_identity_prepare could have allocated or freed pages to
4246 * split blocks or tables etc.
4247 */
4248 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
4249
4250 /*
4251 * Guaranteed to succeed mapping with old mode since the mapping
4252 * with old mode already existed and we have a local page pool
4253 * that should have sufficient memory to go back to the original
4254 * state.
4255 */
4256 CHECK(mm_identity_prepare(
4257 &vm_locked.vm->ptable, pa_from_va(base_addr),
4258 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4259 original_mode, &local_page_pool));
4260 mm_identity_commit(
4261 &vm_locked.vm->ptable, pa_from_va(base_addr),
4262 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
4263 original_mode, &local_page_pool);
4264
4265 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
4266 ret = ffa_error(FFA_NO_MEMORY);
4267 goto out;
4268 }
4269
4270 mm_identity_commit(
4271 &vm_locked.vm->ptable, pa_from_va(base_addr),
4272 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
4273 &local_page_pool);
4274
4275 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
4276
4277out:
4278 mpool_fini(&local_page_pool);
4279 vm_unlock(&vm_locked);
4280
4281 return ret;
4282}
Maksims Svecovs71b76702022-05-20 15:32:58 +01004283
4284/**
4285 * Helper function for FFA_CONSOLE_LOG ABI.
4286 * Writes number of characters to a given VM buffer.
4287 */
4288static rsize_t arg_to_char_helper(struct vm_locked from_locked,
4289 const uint64_t src, rsize_t src_size,
4290 rsize_t to_write)
4291{
4292 bool flush = false;
4293 char c;
4294 rsize_t size = src_size < to_write ? src_size : to_write;
4295 rsize_t written = 0;
4296
4297 if (size == 0) {
4298 return 0;
4299 }
4300
4301 while (written < size) {
4302 c = ((char *)&src)[written++];
4303 if (c == '\n' || c == '\0') {
4304 flush = true;
4305 } else {
4306 from_locked.vm->log_buffer
4307 [from_locked.vm->log_buffer_length++] = c;
4308 flush = (from_locked.vm->log_buffer_length ==
4309 LOG_BUFFER_SIZE);
4310 }
4311
4312 if (flush) {
4313 dlog_flush_vm_buffer(from_locked.vm->id,
4314 from_locked.vm->log_buffer,
4315 from_locked.vm->log_buffer_length);
4316 from_locked.vm->log_buffer_length = 0;
4317 }
4318 }
4319
4320 return written;
4321}
4322
4323/**
4324 * Implements FFA_CONSOLE_LOG buffered logging.
4325 */
4326struct ffa_value api_ffa_console_log(const struct ffa_value args,
4327 struct vcpu *current)
4328{
4329 struct vm *vm = current->vm;
4330 struct vm_locked vm_locked;
4331 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
4332 ? sizeof(uint32_t)
4333 : sizeof(uint64_t);
4334 size_t total_to_write = args.arg1;
4335
4336 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
4337 return ffa_error(FFA_INVALID_PARAMETERS);
4338 }
4339
4340 vm_locked = vm_lock(vm);
4341
4342 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
4343 chars_in_param, total_to_write);
4344 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
4345 chars_in_param, total_to_write);
4346 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
4347 chars_in_param, total_to_write);
4348 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
4349 chars_in_param, total_to_write);
4350 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
4351 chars_in_param, total_to_write);
4352 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
4353 total_to_write);
4354
4355 vm_unlock(&vm_locked);
4356
4357 return (struct ffa_value){.func = FFA_SUCCESS_32};
4358}