blob: 35ae4c2a554c48885f04d4771cc4cd4a4aec172b [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"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010024#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010025#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000034static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010035 "Partition information descriptor size doesn't match the one in "
36 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000037static_assert(sizeof(struct ffa_partition_info) == 24,
38 "Partition information descriptor size doesn't match the one in "
39 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Fuad Tabbae4efcc32020-07-16 15:37:27 +010040
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000041/*
42 * To eliminate the risk of deadlocks, we define a partial order for the
43 * acquisition of locks held concurrently by the same physical CPU. Our current
44 * ordering requirements are as follows:
45 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010046 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000047 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010048 * Locks of the same kind require the lock of lowest address to be locked first,
49 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000050 */
51
Andrew Scullaa039b32018-10-04 15:02:26 +010052static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010053 "Currently, a page is mapped for the send and receive buffers so "
54 "the maximum request is the size of a page.");
55
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
57 "The page pool entry size must be at least as big as the mailbox "
58 "size, so that memory region descriptors can be copied from the "
59 "mailbox for memory sharing.");
60
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000061static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062
63/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000064 * Initialises the API page pool by taking ownership of the contents of the
65 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000066 */
67void api_init(struct mpool *ppool)
68{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000069 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000070}
71
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010072/**
J-Alvesad6a0432021-04-09 16:06:21 +010073 * Get target VM vCPU:
74 * If VM is UP then return first vCPU.
75 * If VM is MP then return vCPU whose index matches current CPU index.
76 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050077struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010078{
79 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
80 struct vcpu *vcpu = NULL;
81
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050082 CHECK((vm != NULL) && (current != NULL));
83
J-Alvesad6a0432021-04-09 16:06:21 +010084 if (vm->vcpu_count == 1) {
85 vcpu = vm_get_vcpu(vm, 0);
86 } else if (current_cpu_index < vm->vcpu_count) {
87 vcpu = vm_get_vcpu(vm, current_cpu_index);
88 }
89
90 return vcpu;
91}
92
93/**
J-Alvesfe7f7372020-11-09 11:32:12 +000094 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
95 * is given as argument of the function.
96 *
97 * Called to change the context between SPs for direct messaging (when Hafnium
98 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
99 *
100 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100101 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000102 * - MP with pinned Execution Contexts.
103 */
104static struct vcpu *api_switch_to_vm(struct vcpu *current,
105 struct ffa_value to_ret,
106 enum vcpu_state vcpu_state,
107 ffa_vm_id_t to_id)
108{
109 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100110 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000111
112 CHECK(next != NULL);
113
114 /* Set the return value for the target VM. */
115 arch_regs_set_retval(&next->regs, to_ret);
116
117 /* Set the current vCPU state. */
118 sl_lock(&current->lock);
119 current->state = vcpu_state;
120 sl_unlock(&current->lock);
121
122 return next;
123}
124
125/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000126 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100127 *
128 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100130 */
Olivier Deprezd8e18882022-07-15 14:46:41 +0200131static struct vcpu *api_switch_to_primary(struct vcpu *current,
132 struct ffa_value primary_ret,
133 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100134{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000135 /*
136 * If the secondary is blocked but has a timer running, sleep until the
137 * timer fires rather than indefinitely.
138 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100139 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100140 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
141 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100142 if (arch_timer_enabled_current()) {
143 uint64_t remaining_ns =
144 arch_timer_remaining_ns_current();
145
146 if (remaining_ns == 0) {
147 /*
148 * Timer is pending, so the current vCPU should
149 * be run again right away.
150 */
Andrew Walbran7e824602020-10-22 16:51:40 +0100151 primary_ret = (struct ffa_value){
152 .func = FFA_INTERRUPT_32};
153
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100154 } else {
155 primary_ret.arg2 = remaining_ns;
156 }
157 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000160 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100161 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000162
163 default:
164 /* Do nothing. */
165 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000166 }
167
J-Alvesfe7f7372020-11-09 11:32:12 +0000168 return api_switch_to_vm(current, primary_ret, secondary_state,
169 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100170}
171
172/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200173 * Choose next vCPU to run to be the counterpart vCPU in the other
174 * world (run the normal world if currently running in the secure
175 * world). Set current vCPU state to the given vcpu_state parameter.
176 * Set FF-A return values to the target vCPU in the other world.
177 *
178 * Called in context of a direct message response from a secure
179 * partition to a VM.
180 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100181struct vcpu *api_switch_to_other_world(struct vcpu *current,
182 struct ffa_value other_world_ret,
183 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200184{
J-Alvesfe7f7372020-11-09 11:32:12 +0000185 return api_switch_to_vm(current, other_world_ret, vcpu_state,
186 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187}
188
189/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100190 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
191 * registers the `from` VM to be notified when it becomes available.
192 */
193static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
194{
195 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
196 to.vm->mailbox.recv == NULL) {
197 /*
198 * Fail if the receiver isn't currently ready to receive data,
199 * setting up for notification if requested.
200 */
201 if (notify) {
202 struct wait_entry *entry =
203 vm_get_wait_entry(from, to.vm->id);
204
205 /* Append waiter only if it's not there yet. */
206 if (list_empty(&entry->wait_links)) {
207 list_append(&to.vm->mailbox.waiter_list,
208 &entry->wait_links);
209 }
210 }
211
212 return true;
213 }
214
215 return false;
216}
217
218/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000219 * Returns true if the given vCPU is executing in context of an
220 * FFA_MSG_SEND_DIRECT_REQ invocation.
221 */
222static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
223{
224 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
225}
226
227/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100228 * Returns true if the VM owning the given vCPU is supporting managed exit and
229 * the vCPU is currently processing a managed exit.
230 */
231static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
232{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100233 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100234 vcpu_locked.vcpu->processing_managed_exit);
235}
236
237/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000238 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000239 */
240struct vcpu *api_preempt(struct vcpu *current)
241{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = FFA_INTERRUPT_32,
Olivier Deprez0d725f52022-07-06 14:20:27 +0200244 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000245 };
246
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500247 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000248}
249
250/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000251 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000252 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100253 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100254struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100255{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100256 struct ffa_value ret = {
257 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
258 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100259 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000260
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000261 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100262 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100263}
264
265/**
Andrew Walbran33645652019-04-15 12:29:31 +0100266 * Puts the current vCPU in off mode, and returns to the primary VM.
267 */
268struct vcpu *api_vcpu_off(struct vcpu *current)
269{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100270 struct ffa_value ret = {
271 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
272 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100273 };
274
275 /*
276 * Disable the timer, so the scheduler doesn't get told to call back
277 * based on it.
278 */
279 arch_timer_disable_current();
280
281 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
282}
283
284/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500285 * The current vCPU is blocked on some resource and needs to relinquish
286 * control back to the execution context of the endpoint that originally
287 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000288 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000289struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000290{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000291 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
292 struct vcpu_locked current_locked;
293 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000294
295 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000296 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000298 }
299
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 current_locked = vcpu_lock(current);
301 is_direct_request_ongoing =
302 is_ffa_direct_msg_request_ongoing(current_locked);
303 vcpu_unlock(&current_locked);
304
305 if (is_direct_request_ongoing) {
306 return ffa_error(FFA_DENIED);
307 }
308
309 *next = api_switch_to_primary(
310 current,
311 (struct ffa_value){.func = FFA_YIELD_32,
312 .arg1 = ffa_vm_vcpu(current->vm->id,
313 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500314 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000315
316 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000317}
318
319/**
Andrew Walbran33645652019-04-15 12:29:31 +0100320 * Switches to the primary so that it can switch to the target, or kick it if it
321 * is already running on a different physical CPU.
322 */
323struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
324{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100326 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
328 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100329 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500330 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100331}
332
333/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000334 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000335 */
336struct vcpu *api_abort(struct vcpu *current)
337{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000339
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100340 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000341 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000342
343 if (current->vm->id == HF_PRIMARY_VM_ID) {
344 /* TODO: what to do when the primary aborts? */
345 for (;;) {
346 /* Do nothing. */
347 }
348 }
349
350 atomic_store_explicit(&current->vm->aborting, true,
351 memory_order_relaxed);
352
353 /* TODO: free resources once all vCPUs abort. */
354
Andrew Sculld6ee1102019-04-05 22:12:42 +0100355 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000356}
357
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000358/*
359 * Format the partition info descriptors according to the version supported
360 * by the endpoint and return the size of the array created.
361 */
362static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000363 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000364 uint32_t vm_count)
365{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000366 struct vm *vm = vm_locked.vm;
367 uint32_t version = vm->ffa_version;
Daniel Boulby835e1592022-05-30 17:38:51 +0100368 uint32_t partition_info_size;
369 uint32_t buffer_size;
Federico Recanati644f0462022-03-17 12:04:00 +0100370 struct ffa_value ret;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000371
Daniel Boulby191b5f02022-02-17 17:26:14 +0000372 if (msg_receiver_busy(vm_locked, NULL, false)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000373 /*
374 * Can't retrieve memory information if the mailbox is not
375 * available.
376 */
377 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000378 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000379 }
380
Federico Recanati644f0462022-03-17 12:04:00 +0100381 /* Acquire receiver's RX buffer. */
382 if (!plat_ffa_acquire_receiver_rx(vm_locked, &ret)) {
383 dlog_verbose("Failed to acquire RX buffer for VM %x\n", vm->id);
384 return ret;
385 }
386
Daniel Boulby191b5f02022-02-17 17:26:14 +0000387 if (version == MAKE_FFA_VERSION(1, 0)) {
388 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
389
Daniel Boulby835e1592022-05-30 17:38:51 +0100390 partition_info_size = sizeof(struct ffa_partition_info_v1_0);
391 buffer_size = partition_info_size * vm_count;
392 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000393 dlog_error(
394 "Partition information does not fit in the "
395 "VM's RX "
396 "buffer.\n");
397 return ffa_error(FFA_NO_MEMORY);
398 }
399
400 for (uint32_t i = 0; i < vm_count; i++) {
401 /*
402 * Populate the VM's RX buffer with the partition
403 * information.
404 */
405 recv_mailbox[i].vm_id = partitions[i].vm_id;
406 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
407 recv_mailbox[i].properties = partitions[i].properties;
408 }
409
410 } else {
Daniel Boulby835e1592022-05-30 17:38:51 +0100411 partition_info_size = sizeof(struct ffa_partition_info);
412 buffer_size = partition_info_size * vm_count;
413 if (buffer_size > HF_MAILBOX_SIZE) {
Daniel Boulby191b5f02022-02-17 17:26:14 +0000414 dlog_error(
415 "Partition information does not fit in the "
416 "VM's RX "
417 "buffer.\n");
418 return ffa_error(FFA_NO_MEMORY);
419 }
420
421 /* Populate the VM's RX buffer with the partition information.
422 */
Daniel Boulby835e1592022-05-30 17:38:51 +0100423 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions,
424 buffer_size);
Daniel Boulby191b5f02022-02-17 17:26:14 +0000425 }
426
Daniel Boulby835e1592022-05-30 17:38:51 +0100427 vm->mailbox.recv_size = buffer_size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000428
429 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000430 vm->mailbox.recv_sender = HF_VM_ID_BASE;
431 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
432 vm->mailbox.state = MAILBOX_STATE_READ;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000433
Daniel Boulby835e1592022-05-30 17:38:51 +0100434 /*
435 * Return the count of partition information descriptors in w2
436 * and the size of the descriptors in w3.
437 */
438 return (struct ffa_value){.func = FFA_SUCCESS_32,
439 .arg2 = vm_count,
440 .arg3 = partition_info_size};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000441}
442
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100443struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000444 const struct ffa_uuid *uuid,
445 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100446{
447 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100448 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000449 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
450 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100451 bool uuid_is_null = ffa_uuid_is_null(uuid);
Daniel Boulbyce541842022-05-31 15:54:31 +0100452 struct ffa_partition_info partitions[2 * MAX_VMS] = {0};
Daniel Boulby191b5f02022-02-17 17:26:14 +0000453 struct vm_locked vm_locked;
454 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100455
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000456 /* Bits 31:1 Must Be Zero */
457 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
458 return ffa_error(FFA_INVALID_PARAMETERS);
459 }
460
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100461 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000462 * No need to count if we are returning the number of paritions as we
463 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100464 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000465 if (uuid_is_null && count_flag) {
466 vm_count = vm_get_count();
467 } else {
468 /*
469 * Iterate through the VMs to find the ones with a matching
470 * UUID. A Null UUID retrieves information for all VMs.
471 */
472 for (uint16_t index = 0; index < vm_get_count(); ++index) {
473 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100474
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000475 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
476 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100477
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000478 ++vm_count;
479 if (count_flag) {
480 continue;
481 }
482
483 partitions[array_index].vm_id = vm->id;
484 partitions[array_index].vcpu_count =
485 vm->vcpu_count;
486 partitions[array_index].properties =
487 plat_ffa_partition_properties(
488 current_vm->id, vm);
489 partitions[array_index].properties |=
490 vm_are_notifications_enabled(vm)
491 ? FFA_PARTITION_NOTIFICATION
492 : 0;
Daniel Boulbyce541842022-05-31 15:54:31 +0100493 if (uuid_is_null) {
494 partitions[array_index].uuid = vm->uuid;
495 }
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000496 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100497 }
498 }
499
Olivier Depreze562e542020-06-11 17:31:54 +0200500 /* If UUID is Null vm_count must not be zero at this stage. */
501 CHECK(!uuid_is_null || vm_count != 0);
502
503 /*
504 * When running the Hypervisor:
505 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
506 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000507 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200508 * a secure partition and the query is forwarded to the SPMC.
509 * When running the SPMC:
510 * - If UUID is non-Null and vm_count is zero it means there is no such
511 * partition identified in the system.
512 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000513 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200514
515 /*
516 * Unrecognized UUID: does not match any of the VMs (or SPs)
517 * and is not Null.
518 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100519 if (vm_count == 0) {
520 return ffa_error(FFA_INVALID_PARAMETERS);
521 }
522
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000523 /*
524 * If the count flag is set we don't need to return the partition info
525 * descriptors.
526 */
527 if (count_flag) {
528 return (struct ffa_value){.func = FFA_SUCCESS_32,
529 .arg2 = vm_count};
530 }
531
Daniel Boulby191b5f02022-02-17 17:26:14 +0000532 vm_locked = vm_lock(current_vm);
533 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
534 vm_count);
535 vm_unlock(&vm_locked);
536 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100537}
538
Andrew Scull9726c252019-01-23 13:44:19 +0000539/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000540 * Returns the ID of the VM.
541 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100542struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000543{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return (struct ffa_value){.func = FFA_SUCCESS_32,
545 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000546}
547
548/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200549 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
550 * FF-A instances.
551 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000552 */
553struct ffa_value api_ffa_spm_id_get(void)
554{
J-Alves3829fc02021-03-18 12:49:18 +0000555#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
556 /*
557 * Return the SPMC ID that was fetched during FF-A
558 * initialization.
559 */
560 return (struct ffa_value){.func = FFA_SUCCESS_32,
561 .arg2 = arch_ffa_spmc_id_get()};
562#else
563 return ffa_error(FFA_NOT_SUPPORTED);
564#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000565}
566
567/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000568 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000569 * function to indicate that register state for the given vCPU has been saved
570 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000571 */
572void api_regs_state_saved(struct vcpu *vcpu)
573{
574 sl_lock(&vcpu->lock);
575 vcpu->regs_available = true;
576 sl_unlock(&vcpu->lock);
577}
578
579/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000580 * Retrieves the next waiter and removes it from the wait list if the VM's
581 * mailbox is in a writable state.
582 */
583static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
584{
585 struct wait_entry *entry;
586 struct vm *vm = locked_vm.vm;
587
Andrew Sculld6ee1102019-04-05 22:12:42 +0100588 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000589 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
590 /* The mailbox is not writable or there are no waiters. */
591 return NULL;
592 }
593
594 /* Remove waiter from the wait list. */
595 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
596 wait_links);
597 list_remove(&entry->wait_links);
598 return entry;
599}
600
601/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000602 * Assuming that the arguments have already been checked by the caller, injects
603 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
604 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
605 * is next run, which is up to the scheduler.
606 *
607 * Returns:
608 * - 0 on success if no further action is needed.
609 * - 1 if it was called by the primary VM and the primary VM now needs to wake
610 * up or kick the target vCPU.
611 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100612int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
613 uint32_t intid, struct vcpu *current,
614 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000615{
Manish Pandey35e452f2021-02-18 21:36:34 +0000616 struct vcpu *target_vcpu = target_locked.vcpu;
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100617 struct interrupts *interrupts = &target_vcpu->interrupts;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000618 int64_t ret = 0;
619
Andrew Walbran508e63c2018-12-20 17:02:37 +0000620 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000621 * We only need to change state and (maybe) trigger a virtual interrupt
622 * if it is enabled and was not previously pending. Otherwise we can
623 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000624 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100625 if (!(vcpu_is_virt_interrupt_enabled(interrupts, intid) &&
626 !vcpu_is_virt_interrupt_pending(interrupts, intid))) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000627 goto out;
628 }
629
630 /* Increment the count. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100631 vcpu_interrupt_count_increment(target_locked, interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000632
633 /*
634 * Only need to update state if there was not already an
635 * interrupt enabled and pending.
636 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000637 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000638 goto out;
639 }
640
Andrew Walbran508e63c2018-12-20 17:02:37 +0000641 if (current->vm->id == HF_PRIMARY_VM_ID) {
642 /*
643 * If the call came from the primary VM, let it know that it
644 * should run or kick the target vCPU.
645 */
646 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000647 } else if (current != target_vcpu && next != NULL) {
648 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000649 }
650
651out:
652 /* Either way, make it pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100653 vcpu_virt_interrupt_set_pending(interrupts, intid);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000654
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200655 return ret;
656}
657
658/* Wrapper to internal_interrupt_inject with locking of target vCPU */
659static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
660 uint32_t intid, struct vcpu *current,
661 struct vcpu **next)
662{
663 int64_t ret;
664 struct vcpu_locked target_locked;
665
666 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100667 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200668 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000669
670 return ret;
671}
672
673/**
Federico Recanati25053ee2022-03-14 15:01:53 +0100674 * Constructs the return value from a successful FFA_MSG_POLL or
675 * FFA_MSG_WAIT call.
676 *
677 * Note: FFA_MSG_POLL is deprecated in FF-A v1.1 and should not be used with
678 * FFA_MSG_SEND2; no check is done on mixing FF-A v1.0 and FF-A v1.1 indirect
679 * message protocols.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100680 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100682{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000683 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100684 case FFA_MSG_SEND_32:
685 return (struct ffa_value){
686 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000687 .arg1 = (receiver->mailbox.recv_sender << 16) |
688 receiver->id,
689 .arg3 = receiver->mailbox.recv_size};
Federico Recanati25053ee2022-03-14 15:01:53 +0100690 case FFA_MSG_SEND2_32:
691 return (struct ffa_value){
692 .func = FFA_RUN_32,
693 /*
694 * TODO: FFA_RUN should return vCPU and VM ID in arg1.
695 * Retrieving vCPU requires a rework of the function,
696 * while receiver ID must be set because it's checked by
697 * other APIs (eg: FFA_NOTIFICATION_GET).
698 */
699 .arg1 = receiver->id};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000700 default:
701 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000702 dlog_error("Tried to return an invalid message function %#x\n",
703 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100704 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000705 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100706}
707
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600708struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
709 struct ffa_value *args)
710{
711 struct ffa_value ret;
712
713 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
714 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
715 args->arg7 != 0U) {
716 return ffa_error(FFA_INVALID_PARAMETERS);
717 }
718
719 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
720 return ret;
721 }
722
723 return api_ffa_msg_recv(true, current, next);
724}
725
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100726/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000727 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000728 * value needs to be forced onto the vCPU.
729 */
J-Alves6e2abc62021-12-02 14:58:56 +0000730static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000732{
Max Shvetsov40108e72020-08-27 12:39:50 +0100733 struct vcpu_locked vcpu_locked;
734 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000735 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500736 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000737 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000738
Andrew Scullb06d1752019-02-04 10:15:48 +0000739 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000740 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000741 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100742 * The VM lock is not needed in the common case so it must only be taken
743 * when it is going to be needed. This ensures there are no inter-vCPU
744 * dependencies in the common run case meaning the sensitive context
745 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000746 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100747 vcpu_locked = vcpu_lock(vcpu);
748
749#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000750 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
751 vcpu_locked, vcpu->vm->secondary_ep, 0);
752 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100753 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
754 __func__, vcpu->vm->id, current->cpu->id);
755 }
756
757#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000758 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000759 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
760 (!vcpu->vm->el0_partition &&
761 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
762 vcpu->state == VCPU_STATE_BLOCKED ||
763 vcpu->state == VCPU_STATE_PREEMPTED));
764
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000765 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100766 vcpu_unlock(&vcpu_locked);
767 vm_locked = vm_lock(vcpu->vm);
768 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000769 }
770
771 /*
772 * If the vCPU is already running somewhere then we can't run it here
773 * simultaneously. While it is actually running then the state should be
774 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
775 * stops running but while Hafnium is in the process of switching back
776 * to the primary there will be a brief period while the state has been
777 * updated but `regs_available` is still false (until
778 * `api_regs_state_saved` is called). We can't start running it again
779 * until this has finished, so count this state as still running for the
780 * purposes of this check.
781 */
782 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
783 /*
784 * vCPU is running on another pCPU.
785 *
786 * It's okay not to return the sleep duration here because the
787 * other physical CPU that is currently running this vCPU will
788 * return the sleep duration if needed.
789 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100790 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000791 ret = false;
792 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000793 }
Andrew Scull9726c252019-01-23 13:44:19 +0000794
795 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100796 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100797 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000798 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100799 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000800 }
801 ret = false;
802 goto out;
803 }
804
Andrew Walbran508e63c2018-12-20 17:02:37 +0000805 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100806 case VCPU_STATE_RUNNING:
807 case VCPU_STATE_OFF:
808 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000809 ret = false;
810 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000811
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500812 case VCPU_STATE_WAITING:
813 /*
814 * An initial FFA_RUN is necessary for secondary VM/SP to reach
815 * the message wait loop.
816 */
817 if (!vcpu->is_bootstrapped) {
818 vcpu->is_bootstrapped = true;
819 break;
820 }
821
J-Alves6e2abc62021-12-02 14:58:56 +0000822 assert(need_vm_lock == true);
823 if (!vm_locked.vm->el0_partition &&
824 plat_ffa_inject_notification_pending_interrupt(
825 vcpu_locked, current, vm_locked)) {
826 break;
827 }
828
Andrew Scullb06d1752019-02-04 10:15:48 +0000829 /*
830 * A pending message allows the vCPU to run so the message can
831 * be delivered directly.
832 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100833 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100834 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 ffa_msg_recv_return(vcpu->vm));
Federico Recanati25053ee2022-03-14 15:01:53 +0100836 if (vcpu->vm->mailbox.recv_func == FFA_MSG_SEND_32) {
837 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
838 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000839 break;
840 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500841
842 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
843 break;
844 }
845
846 if (arch_timer_enabled(&vcpu->regs)) {
847 timer_remaining_ns =
848 arch_timer_remaining_ns(&vcpu->regs);
849 if (timer_remaining_ns == 0) {
850 break;
851 }
852 } else {
853 dlog_verbose("Timer disabled\n");
854 }
855 run_ret->func = FFA_MSG_WAIT_32;
856 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
857 run_ret->arg2 = timer_remaining_ns;
858 ret = false;
859 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100860 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000861 if (need_vm_lock &&
862 plat_ffa_inject_notification_pending_interrupt(
863 vcpu_locked, current, vm_locked)) {
864 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
865 break;
866 }
867
Andrew Scullb06d1752019-02-04 10:15:48 +0000868 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000869 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000870 break;
871 }
872
Andrew Walbran508e63c2018-12-20 17:02:37 +0000873 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100874 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000875 arch_timer_remaining_ns(&vcpu->regs);
876
877 /*
878 * The timer expired so allow the interrupt to be
879 * delivered.
880 */
881 if (timer_remaining_ns == 0) {
882 break;
883 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100884 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000885
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100886 /*
887 * The vCPU is not ready to run, return the appropriate code to
888 * the primary which called vcpu_run.
889 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500890 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100891 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
892 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000893
894 ret = false;
895 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000896
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500897 case VCPU_STATE_BLOCKED:
898 /* A blocked vCPU is run unconditionally. Fall through. */
899 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000900 /* Check NPI is to be injected here. */
901 if (need_vm_lock) {
902 plat_ffa_inject_notification_pending_interrupt(
903 vcpu_locked, current, vm_locked);
904 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000905 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500906 default:
907 /*
908 * Execution not expected to reach here. Deny the request
909 * gracefully.
910 */
911 *run_ret = ffa_error(FFA_DENIED);
912 ret = false;
913 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000914 }
915
Andrew Scullb06d1752019-02-04 10:15:48 +0000916 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000917 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100918 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000919
J-Alves7ac49052022-02-08 17:20:53 +0000920#if SECURE_WORLD == 1
921 /* Set the designated GP register with the vCPU ID. */
922 if (is_vcpu_reset_and_start) {
923 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
924 }
925#endif
926
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000927 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000928 * Mark the registers as unavailable now that we're about to reflect
929 * them onto the real registers. This will also prevent another physical
930 * CPU from trying to read these registers.
931 */
932 vcpu->regs_available = false;
933
934 ret = true;
935
936out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100937 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000938 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100939 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000940 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000941 return ret;
942}
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500945 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100946{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100947 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100948 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100950
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800951 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500952 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100953 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100954
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700955 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
956 return ret;
957 }
958
Andrew Scull19503262018-09-20 14:48:39 +0100959 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100960 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100961 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100962 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100963 }
964
Fuad Tabbaed294af2019-12-20 10:43:01 +0000965 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100966 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100967 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100968 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100969
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000970 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100971 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000972 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000973 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100974 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000975
Andrew Walbran508e63c2018-12-20 17:02:37 +0000976 /*
977 * Inject timer interrupt if timer has expired. It's safe to access
978 * vcpu->regs here because api_vcpu_prepare_run already made sure that
979 * regs_available was true (and then set it to false) before returning
980 * true.
981 */
982 if (arch_timer_pending(&vcpu->regs)) {
983 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100984 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
985 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000986
987 /*
988 * Set the mask bit so the hardware interrupt doesn't fire
989 * again. Ideally we wouldn't do this because it affects what
990 * the secondary vCPU sees, but if we don't then we end up with
991 * a loop of the interrupt firing each time we try to return to
992 * the secondary vCPU.
993 */
994 arch_timer_mask(&vcpu->regs);
995 }
996
Fuad Tabbaed294af2019-12-20 10:43:01 +0000997 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000998 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000999
Andrew Scull33fecd32019-01-08 14:48:27 +00001000 /*
1001 * Set a placeholder return code to the scheduler. This will be
1002 * overwritten when the switch back to the primary occurs.
1003 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +01001005 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +01001006 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +00001007
Andrew Scull6d2db332018-10-10 15:28:17 +01001008out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001009 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001010}
1011
1012/**
Andrew Scull81e85092018-12-12 12:56:20 +00001013 * Check that the mode indicates memory that is valid, owned and exclusive.
1014 */
Andrew Walbran1281ed42019-10-22 17:23:40 +01001015static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +00001016{
Andrew Scullb5f49e02019-10-02 13:20:47 +01001017 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
1018 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +00001019}
1020
1021/**
Andrew Walbranc8a01972020-09-22 11:23:30 +01001022 * Determines the value to be returned by api_ffa_rxtx_map and
1023 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
1024 * there are waiters, it also switches back to the primary VM for it to wake
1025 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001026 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1028 struct vcpu *current,
1029 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001030{
1031 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001032
1033 if (list_empty(&vm->mailbox.waiter_list)) {
1034 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001035 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001036 }
1037
1038 if (vm->id == HF_PRIMARY_VM_ID) {
1039 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001041 }
1042
1043 /*
1044 * Switch back to the primary VM, informing it that there are waiters
1045 * that need to be notified.
1046 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001047 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001049 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001050
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001052}
1053
1054/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001055 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001056 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001057static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1058 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001059 paddr_t pa_send_begin, paddr_t pa_send_end,
1060 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001061 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001062 struct mpool *local_page_pool)
1063{
1064 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001065
1066 /* Map the send page as read-only in the hypervisor address space. */
1067 vm_locked.vm->mailbox.send =
1068 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001069 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001070 if (!vm_locked.vm->mailbox.send) {
1071 /* TODO: partial defrag of failed range. */
1072 /* Recover any memory consumed in failed mapping. */
1073 mm_defrag(mm_stage1_locked, local_page_pool);
1074 goto fail;
1075 }
1076
1077 /*
1078 * Map the receive page as writable in the hypervisor address space. On
1079 * failure, unmap the send page before returning.
1080 */
1081 vm_locked.vm->mailbox.recv =
1082 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001083 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001084 if (!vm_locked.vm->mailbox.recv) {
1085 /* TODO: partial defrag of failed range. */
1086 /* Recover any memory consumed in failed mapping. */
1087 mm_defrag(mm_stage1_locked, local_page_pool);
1088 goto fail_undo_send;
1089 }
1090
1091 ret = true;
1092 goto out;
1093
1094 /*
1095 * The following mappings will not require more memory than is available
1096 * in the local pool.
1097 */
1098fail_undo_send:
1099 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001100 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1101 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001102
1103fail:
1104 ret = false;
1105
1106out:
Andrew Sculle1322792019-07-01 17:46:10 +01001107 return ret;
1108}
1109
1110/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001111 * Sanity checks and configures the send and receive pages in the VM stage-2
1112 * and hypervisor stage-1 page tables.
1113 *
1114 * Returns:
1115 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001116 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001117 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1118 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001119 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001121 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001122
1123struct ffa_value api_vm_configure_pages(
1124 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1125 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1126 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001127{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001128 struct ffa_value ret;
1129 paddr_t pa_send_begin;
1130 paddr_t pa_send_end;
1131 paddr_t pa_recv_begin;
1132 paddr_t pa_recv_end;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001133 uint32_t orig_send_mode = 0;
1134 uint32_t orig_recv_mode = 0;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001135 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001136
1137 /* We only allow these to be setup once. */
1138 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1139 ret = ffa_error(FFA_DENIED);
1140 goto out;
1141 }
1142
1143 /* Hafnium only supports a fixed size of RX/TX buffers. */
1144 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1145 ret = ffa_error(FFA_INVALID_PARAMETERS);
1146 goto out;
1147 }
1148
1149 /* Fail if addresses are not page-aligned. */
1150 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1151 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1152 ret = ffa_error(FFA_INVALID_PARAMETERS);
1153 goto out;
1154 }
1155
1156 /* Convert to physical addresses. */
1157 pa_send_begin = pa_from_ipa(send);
1158 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1159 pa_recv_begin = pa_from_ipa(recv);
1160 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1161
1162 /* Fail if the same page is used for the send and receive pages. */
1163 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1164 ret = ffa_error(FFA_INVALID_PARAMETERS);
1165 goto out;
1166 }
Andrew Sculle1322792019-07-01 17:46:10 +01001167
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001168 /* Set stage 2 translation tables only for virtual FF-A instances. */
1169 if (vm_id_is_current_world(vm_locked.vm->id)) {
1170 /*
1171 * Ensure the pages are valid, owned and exclusive to the VM and
1172 * that the VM has the required access to the memory.
1173 */
1174 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1175 &orig_send_mode) ||
1176 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1177 (orig_send_mode & MM_MODE_R) == 0 ||
1178 (orig_send_mode & MM_MODE_W) == 0) {
1179 dlog_error(
1180 "VM doesn't have required access rights to map "
1181 "TX buffer in stage 2.\n");
1182 ret = ffa_error(FFA_INVALID_PARAMETERS);
1183 goto out;
1184 }
Manish Pandeyd34f8892020-06-19 17:41:07 +01001185
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001186 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1187 &orig_recv_mode) ||
1188 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1189 (orig_recv_mode & MM_MODE_R) == 0) {
1190 dlog_error(
1191 "VM doesn't have required access rights to map "
1192 "RX buffer in stage 2.\n");
1193 ret = ffa_error(FFA_INVALID_PARAMETERS);
1194 goto out;
1195 }
Andrew Sculle1322792019-07-01 17:46:10 +01001196
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001197 /* Take memory ownership away from the VM and mark as shared. */
1198 uint32_t mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R |
1199 MM_MODE_W;
1200 if (vm_locked.vm->el0_partition) {
1201 mode |= MM_MODE_USER | MM_MODE_NG;
1202 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001203
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001204 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
1205 mode, local_page_pool, NULL)) {
1206 dlog_error(
1207 "Cannot allocate a new entry in stage 2 "
1208 "translation table.\n");
1209 ret = ffa_error(FFA_NO_MEMORY);
1210 goto out;
1211 }
Andrew Sculle1322792019-07-01 17:46:10 +01001212
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001213 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1214 if (vm_locked.vm->el0_partition) {
1215 mode |= MM_MODE_USER | MM_MODE_NG;
1216 }
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001217
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001218 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
1219 mode, local_page_pool, NULL)) {
1220 /* TODO: partial defrag of failed range. */
1221 /* Recover any memory consumed in failed mapping. */
1222 vm_ptable_defrag(vm_locked, local_page_pool);
1223 goto fail_undo_send;
1224 }
Andrew Sculle1322792019-07-01 17:46:10 +01001225 }
1226
Olivier Deprez96a2a262020-06-11 17:21:38 +02001227 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1228 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1229
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001230 /*
1231 * For EL0 partitions, since both the partition and the hypervisor code
1232 * use the EL2&0 translation regime, it is critical to mark the mappings
1233 * of the send and recv buffers as non-global in the TLB. For one, if we
1234 * dont mark it as non-global, it would cause TLB conflicts since there
1235 * would be an identity mapping with non-global attribute in the
1236 * partitions page tables, but another identity mapping in the
1237 * hypervisor page tables with the global attribute. The other issue is
1238 * one of security, we dont want other partitions to be able to access
1239 * other partitions buffers through cached translations.
1240 */
1241 if (vm_locked.vm->el0_partition) {
1242 extra_attributes |= MM_MODE_NG;
1243 }
1244
Manish Pandeyd34f8892020-06-19 17:41:07 +01001245 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1246 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001247 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001248 goto fail_undo_send_and_recv;
1249 }
1250
Manish Pandeyd34f8892020-06-19 17:41:07 +01001251 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001252 goto out;
1253
Andrew Sculle1322792019-07-01 17:46:10 +01001254fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001255 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001256 orig_recv_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001257
1258fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001259 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001260 orig_send_mode, local_page_pool, NULL));
1261 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001262
1263out:
Andrew Sculle1322792019-07-01 17:46:10 +01001264 return ret;
1265}
1266
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001267static void api_get_rxtx_description(struct vm_locked vm_locked, ipaddr_t *send,
1268 ipaddr_t *recv, uint32_t *page_count,
1269 ffa_vm_id_t *owner_vm_id)
1270{
1271 /*
1272 * If the message has been forwarded the effective addresses are in
1273 * hypervisor's TX buffer.
1274 */
1275 bool forwarded = (vm_locked.vm->id == HF_OTHER_WORLD_ID) &&
1276 (ipa_addr(*send) == 0) && (ipa_addr(*recv) == 0) &&
1277 (*page_count == 0);
1278
1279 if (forwarded) {
1280 struct ffa_endpoint_rx_tx_descriptor *endpoint_desc =
1281 (struct ffa_endpoint_rx_tx_descriptor *)
1282 vm_locked.vm->mailbox.send;
1283 struct ffa_composite_memory_region *rx_region =
1284 ffa_enpoint_get_rx_memory_region(endpoint_desc);
1285 struct ffa_composite_memory_region *tx_region =
1286 ffa_enpoint_get_tx_memory_region(endpoint_desc);
1287
1288 *owner_vm_id = endpoint_desc->endpoint_id;
1289 *recv = ipa_init(rx_region->constituents[0].address);
1290 *send = ipa_init(tx_region->constituents[0].address);
1291 *page_count = rx_region->constituents[0].page_count;
1292 } else {
1293 *owner_vm_id = vm_locked.vm->id;
1294 }
1295}
Andrew Sculle1322792019-07-01 17:46:10 +01001296/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001297 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001298 * must not be shared. Locking of the page tables combined with a local memory
1299 * pool ensures there will always be enough memory to recover from any errors
1300 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1301 * by another core which could result in this transaction being unable to roll
1302 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001303 *
1304 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001306 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001307 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001308 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001309 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001310 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001311 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001313 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001314{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001315 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001316 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001317 struct vm_locked vm_locked;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001318 struct vm_locked owner_vm_locked;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001319 struct mm_stage1_locked mm_stage1_locked;
1320 struct mpool local_page_pool;
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001321 ffa_vm_id_t owner_vm_id;
1322
1323 vm_locked = vm_lock(vm);
1324 /*
1325 * Get the original buffer addresses and VM ID in case of forwarded
1326 * message.
1327 */
1328 api_get_rxtx_description(vm_locked, &send, &recv, &page_count,
1329 &owner_vm_id);
1330 vm_unlock(&vm_locked);
1331
1332 owner_vm_locked = plat_ffa_vm_find_locked_create(owner_vm_id);
1333 if (owner_vm_locked.vm == NULL) {
1334 dlog_error("Cannot map RX/TX for VM ID %#x, not found.\n",
1335 owner_vm_id);
1336 return ffa_error(FFA_DENIED);
1337 }
Andrew Scull220e6212018-12-21 18:09:00 +00001338
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001339 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001340 * Create a local pool so any freed memory can't be used by another
1341 * thread. This is to ensure the original mapping can be restored if any
1342 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001343 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001344 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1345
Manish Pandeyd34f8892020-06-19 17:41:07 +01001346 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001347
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001348 ret = api_vm_configure_pages(mm_stage1_locked, owner_vm_locked, send,
1349 recv, page_count, &local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001350 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001351 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001352 }
1353
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001354 /* Forward buffer mapping to SPMC if coming from a VM. */
1355 plat_ffa_rxtx_map_forward(owner_vm_locked);
1356
Federico Recanati9f1b6532022-04-14 13:15:28 +02001357 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001358
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001359exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001360 mpool_fini(&local_page_pool);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001361 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8d8b1cf2022-04-14 13:16:00 +02001362 vm_unlock(&owner_vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001363
1364 return ret;
1365}
1366
1367/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001368 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1369 * translation regime of the caller. Unmap the region for the hypervisor and
1370 * set the memory region to owned and exclusive for the component. Since the
1371 * memory region mapped in the page table, when the buffers were originally
1372 * created we can safely remap it.
1373 *
1374 * Returns:
1375 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1376 * behalf of the caller.
1377 * - FFA_SUCCESS on success if no further action is needed.
1378 */
1379struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1380 struct vcpu *current)
1381{
1382 struct vm *vm = current->vm;
1383 struct vm_locked vm_locked;
Federico Recanati8da9e332022-02-10 11:00:17 +01001384 ffa_vm_id_t owner_vm_id;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001385 struct mm_stage1_locked mm_stage1_locked;
1386 paddr_t send_pa_begin;
1387 paddr_t send_pa_end;
1388 paddr_t recv_pa_begin;
1389 paddr_t recv_pa_end;
Federico Recanati8da9e332022-02-10 11:00:17 +01001390 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001391
Federico Recanati8da9e332022-02-10 11:00:17 +01001392 /* Ensure `allocator_id` is set only at Non-Secure Physical instance. */
1393 if (vm_id_is_current_world(vm->id) && (allocator_id != 0)) {
1394 dlog_error("`allocator_id` must be 0 at virtual instances.\n");
1395 return ffa_error(FFA_INVALID_PARAMETERS);
1396 }
1397
1398 /* VM ID of which buffers have to be unmapped. */
1399 owner_vm_id = (allocator_id != 0) ? allocator_id : vm->id;
1400
1401 vm_locked = plat_ffa_vm_find_locked(owner_vm_id);
1402 vm = vm_locked.vm;
1403 if (vm == NULL) {
1404 dlog_error("Cannot unmap RX/TX for VM ID %#x, not found.\n",
1405 owner_vm_id);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001406 return ffa_error(FFA_INVALID_PARAMETERS);
1407 }
1408
1409 /* Get send and receive buffers. */
1410 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001411 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001412 "No buffer pair registered on behalf of the caller.\n");
Federico Recanati8da9e332022-02-10 11:00:17 +01001413 ret = ffa_error(FFA_INVALID_PARAMETERS);
1414 goto out;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001415 }
1416
1417 /* Currently a mailbox size of 1 page is assumed. */
1418 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1419 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1420 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1421 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1422
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001423 mm_stage1_locked = mm_lock_stage1();
1424
Federico Recanati8da9e332022-02-10 11:00:17 +01001425 /* Reset stage 2 mapping only for virtual FF-A instances. */
1426 if (vm_id_is_current_world(owner_vm_id)) {
1427 /*
1428 * Set the memory region of the buffers back to the default mode
1429 * for the VM. Since this memory region was already mapped for
1430 * the RXTX buffers we can safely remap them.
1431 */
1432 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1433 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1434 &api_page_pool, NULL));
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001435
Federico Recanati8da9e332022-02-10 11:00:17 +01001436 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1437 MM_MODE_R | MM_MODE_W | MM_MODE_X,
1438 &api_page_pool, NULL));
1439 }
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001440
1441 /* Unmap the buffers in the partition manager. */
1442 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1443 &api_page_pool));
1444 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1445 &api_page_pool));
1446
1447 vm->mailbox.send = NULL;
1448 vm->mailbox.recv = NULL;
Federico Recanati10bd06c2022-02-23 17:32:59 +01001449 plat_ffa_vm_destroy(vm_locked);
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001450
Federico Recanati8da9e332022-02-10 11:00:17 +01001451 /* Forward buffer unmapping to SPMC if coming from a VM. */
1452 plat_ffa_rxtx_unmap_forward(owner_vm_id);
1453
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001454 mm_unlock_stage1(&mm_stage1_locked);
Federico Recanati8da9e332022-02-10 11:00:17 +01001455
1456out:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001457 vm_unlock(&vm_locked);
1458
Federico Recanati8da9e332022-02-10 11:00:17 +01001459 return ret;
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001460}
1461
1462/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001463 * Notifies the `to` VM about the message currently in its mailbox, possibly
1464 * with the help of the primary VM.
1465 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001466static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1467 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001468{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1470 struct ffa_value primary_ret = {
1471 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001472 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001473 };
1474
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001475 /* Messages for the primary VM are delivered directly. */
1476 if (to.vm->id == HF_PRIMARY_VM_ID) {
1477 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001478 * Only tell the primary VM the size and other details if the
1479 * message is for it, to avoid leaking data about messages for
1480 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001481 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001483
1484 to.vm->mailbox.state = MAILBOX_STATE_READ;
1485 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001486 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001487 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001488 }
1489
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001490 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1491
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001492 /* Messages for the TEE are sent on via the dispatcher. */
1493 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001494 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001495
Olivier Deprez112d2b52020-09-30 07:39:23 +02001496 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001497 /*
1498 * After the call to the TEE completes it must have finished
1499 * reading its RX buffer, so it is ready for another message.
1500 */
1501 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001502 /*
1503 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001504 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001505 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001506 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001507 }
1508
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001509 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001510 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001511 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001512 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001513 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001514
1515 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001516}
1517
1518/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001519 * Copies data from the sender's send buffer to the recipient's receive buffer
1520 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001521 *
1522 * If the recipient's receive buffer is busy, it can optionally register the
1523 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001524 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001525struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1526 ffa_vm_id_t receiver_vm_id, uint32_t size,
1527 uint32_t attributes, struct vcpu *current,
1528 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001529{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001530 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001531 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001532 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001533 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001535 struct vcpu_locked current_locked;
1536 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 bool notify =
1538 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001539
Andrew Walbran70bc8622019-10-07 14:15:58 +01001540 /* Ensure sender VM ID corresponds to the current VM. */
1541 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001543 }
1544
1545 /* Disallow reflexive requests as this suggests an error in the VM. */
1546 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001547 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001548 }
1549
1550 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 if (size > FFA_MSG_PAYLOAD_MAX) {
1552 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001553 }
1554
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001555 /*
1556 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1557 * invocation.
1558 */
1559 current_locked = vcpu_lock(current);
1560 is_direct_request_ongoing =
1561 is_ffa_direct_msg_request_ongoing(current_locked);
1562 vcpu_unlock(&current_locked);
1563
1564 if (is_direct_request_ongoing) {
1565 return ffa_error(FFA_DENIED);
1566 }
1567
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001568 /* Ensure the receiver VM exists. */
1569 to = vm_find(receiver_vm_id);
1570 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001571 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001572 }
1573
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001574 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001575 * Check that the sender has configured its send buffer. If the tx
1576 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1577 * be safely accessed after releasing the lock since the tx mailbox
1578 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001579 */
1580 sl_lock(&from->lock);
1581 from_msg = from->mailbox.send;
1582 sl_unlock(&from->lock);
1583
1584 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001586 }
1587
Andrew Walbran82d6d152019-12-24 15:02:06 +00001588 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001589
Andrew Walbran82d6d152019-12-24 15:02:06 +00001590 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001592 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001593 }
1594
Andrew Walbran82d6d152019-12-24 15:02:06 +00001595 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001597 to->mailbox.recv_size = size;
1598 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001600 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001601
1602out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001603 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001604
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001605 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001606}
1607
1608/**
Federico Recanati25053ee2022-03-14 15:01:53 +01001609 * Copies data from the sender's send buffer to the recipient's receive buffer
1610 * and notifies the receiver.
1611 */
1612struct ffa_value api_ffa_msg_send2(ffa_vm_id_t sender_vm_id, uint32_t flags,
1613 struct vcpu *current)
1614{
1615 struct vm *from = current->vm;
1616 struct vm *to;
1617 struct vm_locked to_locked;
1618 ffa_vm_id_t msg_sender_id;
1619 struct vm_locked sender_locked;
1620 const void *from_msg;
1621 struct ffa_value ret;
1622 struct ffa_partition_rxtx_header header;
1623 ffa_vm_id_t sender_id;
1624 ffa_vm_id_t receiver_id;
1625 uint32_t msg_size;
1626 ffa_notifications_bitmap_t rx_buffer_full;
1627
1628 /* Only Hypervisor can set `sender_vm_id` when forwarding messages. */
1629 if (from->id != HF_HYPERVISOR_VM_ID && sender_vm_id != 0) {
1630 dlog_error("Sender VM ID must be zero.\n");
1631 return ffa_error(FFA_INVALID_PARAMETERS);
1632 }
1633
Federico Recanati25053ee2022-03-14 15:01:53 +01001634 /*
1635 * Get message sender's mailbox, which can be different to the `from` vm
1636 * when the message is forwarded.
1637 */
1638 msg_sender_id = (sender_vm_id != 0) ? sender_vm_id : from->id;
1639 sender_locked = plat_ffa_vm_find_locked(msg_sender_id);
1640 if (sender_locked.vm == NULL) {
1641 dlog_error("Cannot send message from VM ID %#x, not found.\n",
1642 msg_sender_id);
1643 return ffa_error(FFA_DENIED);
1644 }
1645
1646 from_msg = sender_locked.vm->mailbox.send;
1647 if (from_msg == NULL) {
1648 dlog_error("Cannot retrieve TX buffer for VM ID %#x.\n",
1649 msg_sender_id);
1650 ret = ffa_error(FFA_DENIED);
1651 goto out_unlock_sender;
1652 }
1653
1654 /*
1655 * Copy message header as safety measure to avoid multiple accesses to
1656 * unsafe memory which could be 'corrupted' between safety checks and
1657 * final buffer copy.
1658 */
1659 memcpy_s(&header, FFA_RXTX_HEADER_SIZE, from_msg, FFA_RXTX_HEADER_SIZE);
1660 sender_id = ffa_rxtx_header_sender(&header);
1661 receiver_id = ffa_rxtx_header_receiver(&header);
1662
1663 /* Ensure Sender IDs from API and from message header match. */
1664 if (msg_sender_id != sender_id) {
1665 dlog_error(
1666 "Message sender VM ID (%#x) doesn't match header's VM "
1667 "ID (%#x).\n",
1668 msg_sender_id, sender_id);
1669 ret = ffa_error(FFA_INVALID_PARAMETERS);
1670 goto out_unlock_sender;
1671 }
1672
1673 /* Disallow reflexive requests as this suggests an error in the VM. */
1674 if (receiver_id == sender_id) {
1675 dlog_error("Sender and receive VM IDs must be different.\n");
1676 ret = ffa_error(FFA_INVALID_PARAMETERS);
1677 goto out_unlock_sender;
1678 }
1679
Federico Recanati7b39ff22022-03-14 15:01:53 +01001680 /* `flags` can be set only at secure virtual FF-A instances. */
1681 if (plat_ffa_is_vm_id(sender_id) && (flags != 0)) {
1682 dlog_error("flags must be zero.\n");
1683 return ffa_error(FFA_INVALID_PARAMETERS);
1684 }
1685
Federico Recanati25053ee2022-03-14 15:01:53 +01001686 /*
1687 * Check if the message has to be forwarded to the SPMC, in
1688 * this case return, the SPMC will handle the buffer copy.
1689 */
1690 if (plat_ffa_msg_send2_forward(receiver_id, sender_id, &ret)) {
1691 goto out_unlock_sender;
1692 }
1693
1694 /* Ensure the receiver VM exists. */
1695 to_locked = plat_ffa_vm_find_locked(receiver_id);
1696 to = to_locked.vm;
1697
1698 if (to == NULL) {
1699 dlog_error("Cannot deliver message to VM %#x, not found.\n",
1700 receiver_id);
1701 ret = ffa_error(FFA_INVALID_PARAMETERS);
1702 goto out_unlock_sender;
1703 }
1704
1705 /*
1706 * Check sender and receiver can use indirect messages.
1707 * Sender is the VM/SP who originally sent the message, not the
1708 * hypervisor possibly relaying it.
1709 */
1710 if (!plat_ffa_is_indirect_msg_supported(sender_locked, to_locked)) {
1711 dlog_verbose("VM %#x doesn't support indirect message\n",
1712 sender_id);
1713 ret = ffa_error(FFA_DENIED);
1714 goto out;
1715 }
1716
1717 if (to->mailbox.state != MAILBOX_STATE_EMPTY ||
1718 to->mailbox.recv == NULL) {
1719 dlog_error(
1720 "Cannot deliver message to VM %#x, RX buffer not "
1721 "ready.\n",
1722 receiver_id);
1723 ret = ffa_error(FFA_BUSY);
1724 goto out;
1725 }
1726
Federico Recanati644f0462022-03-17 12:04:00 +01001727 /* Acquire receiver's RX buffer. */
1728 if (!plat_ffa_acquire_receiver_rx(to_locked, &ret)) {
1729 dlog_error("Failed to acquire RX buffer for VM %#x\n", to->id);
1730 goto out;
1731 }
1732
Federico Recanati25053ee2022-03-14 15:01:53 +01001733 /* Check the size of transfer. */
1734 msg_size = FFA_RXTX_HEADER_SIZE + header.size;
1735 if ((msg_size > FFA_PARTITION_MSG_PAYLOAD_MAX) ||
1736 (header.size > FFA_PARTITION_MSG_PAYLOAD_MAX)) {
1737 dlog_error("Message is too big.\n");
1738 ret = ffa_error(FFA_INVALID_PARAMETERS);
1739 goto out;
1740 }
1741
1742 /* Copy data. */
1743 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, msg_size);
1744 to->mailbox.recv_size = msg_size;
1745 to->mailbox.recv_sender = sender_id;
1746 to->mailbox.recv_func = FFA_MSG_SEND2_32;
1747 to->mailbox.state = MAILBOX_STATE_RECEIVED;
1748
1749 rx_buffer_full = plat_ffa_is_vm_id(sender_id)
1750 ? FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK
1751 : FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK;
1752 vm_notifications_framework_set_pending(to_locked, rx_buffer_full);
1753
1754 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
1755 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
1756 vcpu_index(current));
1757 plat_ffa_sri_trigger_not_delayed(current->cpu);
1758 } else {
1759 plat_ffa_sri_state_set(DELAYED);
1760 }
1761
1762 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1763
1764out:
1765 vm_unlock(&to_locked);
1766
1767out_unlock_sender:
1768 vm_unlock(&sender_locked);
1769
1770 return ret;
1771}
1772
1773/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001774 * Checks whether the vCPU's attempt to block for a message has already been
1775 * interrupted or whether it is allowed to block.
1776 */
Olivier Deprezd8e18882022-07-15 14:46:41 +02001777static bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001778{
Manish Pandey35e452f2021-02-18 21:36:34 +00001779 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001780 bool interrupted;
1781
Manish Pandey35e452f2021-02-18 21:36:34 +00001782 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001783
1784 /*
1785 * Don't block if there are enabled and pending interrupts, to match
1786 * behaviour of wait_for_interrupt.
1787 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001788 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001789
Manish Pandey35e452f2021-02-18 21:36:34 +00001790 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001791
1792 return interrupted;
1793}
1794
1795/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001796 * Receives a message from the mailbox. If one isn't available, this function
1797 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001798 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001799 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001800 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1802 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001803{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001804 bool is_direct_request_ongoing;
1805 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001806 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001807 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001808 bool is_from_secure_world =
1809 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001810
Andrew Scullaa039b32018-10-04 15:02:26 +01001811 /*
1812 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001813 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001814 */
J-Alvesb37fd082020-10-22 12:29:21 +01001815 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001816 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001817 }
1818
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001819 /*
1820 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1821 * invocation.
1822 */
1823 current_locked = vcpu_lock(current);
1824 is_direct_request_ongoing =
1825 is_ffa_direct_msg_request_ongoing(current_locked);
1826 vcpu_unlock(&current_locked);
1827
1828 if (is_direct_request_ongoing) {
1829 return ffa_error(FFA_DENIED);
1830 }
1831
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001832 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001833
Andrew Scullaa039b32018-10-04 15:02:26 +01001834 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001835 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001836 return_code = ffa_msg_recv_return(vm);
Federico Recanati25053ee2022-03-14 15:01:53 +01001837 if (return_code.func == FFA_MSG_SEND_32) {
1838 vm->mailbox.state = MAILBOX_STATE_READ;
1839 }
Jose Marinho3e2442f2019-03-12 13:30:37 +00001840 goto out;
1841 }
1842
1843 /* No pending message so fail if not allowed to block. */
1844 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001845 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001846 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001847 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001848
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001849 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001850 * From this point onward this call can only be interrupted or a message
1851 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001853 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001854 return_code = ffa_error(FFA_INTERRUPTED);
1855 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001856 goto out;
1857 }
1858
J-Alvesb37fd082020-10-22 12:29:21 +01001859 if (is_from_secure_world) {
1860 /* Return to other world if caller is a SP. */
1861 *next = api_switch_to_other_world(
1862 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001863 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001864 } else {
1865 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001866 struct ffa_value run_return = {
1867 .func = FFA_MSG_WAIT_32,
1868 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001869 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001870
Andrew Walbranb4816552018-12-05 17:35:42 +00001871 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001872 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001873 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001874out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001875 sl_unlock(&vm->lock);
1876
Jose Marinho3e2442f2019-03-12 13:30:37 +00001877 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001878}
1879
1880/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001881 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1882 * by this function, the caller must have called api_mailbox_send before with
1883 * the notify argument set to true, and this call must have failed because the
1884 * mailbox was not available.
1885 *
1886 * It should be called repeatedly to retrieve a list of VMs.
1887 *
1888 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1889 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001890 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001891int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001892{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001893 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001894 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001895 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001896
1897 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001898 if (list_empty(&vm->mailbox.ready_list)) {
1899 ret = -1;
1900 goto exit;
1901 }
1902
1903 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1904 ready_links);
1905 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001906 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001907
1908exit:
1909 sl_unlock(&vm->lock);
1910 return ret;
1911}
1912
1913/**
1914 * Retrieves the next VM waiting to be notified that the mailbox of the
1915 * specified VM became writable. Only primary VMs are allowed to call this.
1916 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001917 * Returns -1 on failure or if there are no waiters; the VM id of the next
1918 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001919 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001920int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001921{
1922 struct vm *vm;
1923 struct vm_locked locked;
1924 struct wait_entry *entry;
1925 struct vm *waiting_vm;
1926
1927 /* Only primary VMs are allowed to call this function. */
1928 if (current->vm->id != HF_PRIMARY_VM_ID) {
1929 return -1;
1930 }
1931
Andrew Walbran42347a92019-05-09 13:59:03 +01001932 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001933 if (vm == NULL) {
1934 return -1;
1935 }
1936
Fuad Tabbaed294af2019-12-20 10:43:01 +00001937 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001938 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001939 entry = api_fetch_waiter(locked);
1940 vm_unlock(&locked);
1941
1942 if (entry == NULL) {
1943 return -1;
1944 }
1945
1946 /* Enqueue notification to waiting VM. */
1947 waiting_vm = entry->waiting_vm;
1948
1949 sl_lock(&waiting_vm->lock);
1950 if (list_empty(&entry->ready_links)) {
1951 list_append(&waiting_vm->mailbox.ready_list,
1952 &entry->ready_links);
1953 }
1954 sl_unlock(&waiting_vm->lock);
1955
1956 return waiting_vm->id;
1957}
1958
1959/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001960 * Releases the caller's mailbox so that a new message can be received. The
1961 * caller must have copied out all data they wish to preserve as new messages
1962 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001963 *
1964 * Returns:
Federico Recanati7bef0b92022-03-17 14:56:22 +01001965 * - FFA_ERROR FFA_INVALID_PARAMETERS if message is forwarded to SPMC but
1966 * there's no buffer pair mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001967 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1968 * - FFA_SUCCESS on success if no further action is needed.
1969 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001970 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001971 * hf_mailbox_waiter_get.
1972 */
Federico Recanati7bef0b92022-03-17 14:56:22 +01001973struct ffa_value api_ffa_rx_release(ffa_vm_id_t receiver_id,
1974 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001975{
Federico Recanati7bef0b92022-03-17 14:56:22 +01001976 struct vm *current_vm = current->vm;
1977 struct vm *vm;
1978 struct vm_locked vm_locked;
1979 ffa_vm_id_t current_vm_id = current_vm->id;
1980 ffa_vm_id_t release_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001981 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001982
Federico Recanati7bef0b92022-03-17 14:56:22 +01001983 /* `receiver_id` can be set only at Non-Secure Physical interface. */
1984 if (vm_id_is_current_world(current_vm_id) && (receiver_id != 0)) {
1985 dlog_error("Invalid `receiver_id`, must be zero.\n");
1986 return ffa_error(FFA_INVALID_PARAMETERS);
1987 }
1988
1989 /*
1990 * VM ID to be released: `receiver_id` if message has been forwarded by
1991 * Hypervisor to release a VM's buffer, current VM ID otherwise.
1992 */
1993 if (vm_id_is_current_world(current_vm_id) || (receiver_id == 0)) {
1994 release_vm_id = current_vm_id;
1995 } else {
1996 release_vm_id = receiver_id;
1997 }
1998
1999 vm_locked = plat_ffa_vm_find_locked(release_vm_id);
2000 vm = vm_locked.vm;
2001 if (vm == NULL) {
2002 dlog_error("No buffer registered for VM ID %#x.\n",
2003 release_vm_id);
2004 return ffa_error(FFA_INVALID_PARAMETERS);
2005 }
2006
2007 if (!plat_ffa_rx_release_forward(vm_locked, &ret)) {
2008 dlog_verbose("RX_RELEASE forward failed for VM ID %#x.\n",
2009 release_vm_id);
2010 goto out;
2011 }
2012
2013 /*
2014 * When SPMC owns a VM's RX buffer, the Hypervisor's view can be out of
2015 * sync: reset it to empty and exit.
2016 */
2017 if (plat_ffa_rx_release_forwarded(vm_locked)) {
2018 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2019 goto out;
2020 }
2021
Andrew Scullaa7db8e2019-02-01 14:12:19 +00002022 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01002023 case MAILBOX_STATE_EMPTY:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002024 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00002025 break;
2026
Federico Recanati7bef0b92022-03-17 14:56:22 +01002027 case MAILBOX_STATE_RECEIVED:
2028 if (release_vm_id == current_vm_id) {
2029 /*
2030 * VM requesting to release its own RX buffer,
2031 * must be in READ state.
2032 */
2033 ret = ffa_error(FFA_DENIED);
2034 } else {
2035 /*
2036 * Forwarded message from Hypervisor to release a VM
2037 * RX buffer, SPMC's mailbox view can be still in
2038 * RECEIVED state.
2039 */
2040 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2041 vm->mailbox.state = MAILBOX_STATE_EMPTY;
2042 }
2043 break;
2044
Andrew Sculld6ee1102019-04-05 22:12:42 +01002045 case MAILBOX_STATE_READ:
Federico Recanati7bef0b92022-03-17 14:56:22 +01002046 ret = api_waiter_result(vm_locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01002047 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00002048 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002049 }
Federico Recanati7bef0b92022-03-17 14:56:22 +01002050
2051out:
2052 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01002053
2054 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01002055}
Andrew Walbran318f5732018-11-20 16:23:42 +00002056
2057/**
Federico Recanati644f0462022-03-17 12:04:00 +01002058 * Acquire ownership of an RX buffer before writing to it. Both
2059 * Hypervisor and SPMC are producers of VM's RX buffer, and they
2060 * could contend for the same buffer. SPMC owns VM's RX buffer after
2061 * it's mapped in its translation regime. This ABI should be
2062 * used by the Hypervisor to get the ownership of a VM's RX buffer
2063 * from the SPMC solving the aforementioned possible contention.
2064 *
2065 * Returns:
2066 * - FFA_DENIED: callee cannot relinquish ownership of RX buffer.
2067 * - FFA_INVALID_PARAMETERS: there is no buffer pair registered for the VM.
2068 * - FFA_NOT_SUPPORTED: function not implemented at the FF-A instance.
2069 */
2070struct ffa_value api_ffa_rx_acquire(ffa_vm_id_t receiver_id,
2071 struct vcpu *current)
2072{
2073 struct vm_locked receiver_locked;
2074 struct vm *receiver;
2075 struct ffa_value ret;
2076
2077 if ((current->vm->id != HF_HYPERVISOR_VM_ID) ||
2078 !plat_ffa_is_vm_id(receiver_id)) {
2079 dlog_error(
2080 "FFA_RX_ACQUIRE not supported at this FF-A "
2081 "instance.\n");
2082 return ffa_error(FFA_NOT_SUPPORTED);
2083 }
2084
2085 receiver_locked = plat_ffa_vm_find_locked(receiver_id);
2086 receiver = receiver_locked.vm;
2087
2088 if (receiver == NULL || receiver->mailbox.recv == NULL) {
2089 dlog_error("Cannot retrieve RX buffer for VM ID %#x.\n",
2090 receiver_id);
2091 ret = ffa_error(FFA_INVALID_PARAMETERS);
2092 goto out;
2093 }
2094
2095 if (receiver->mailbox.state != MAILBOX_STATE_EMPTY) {
2096 dlog_error("Mailbox busy for VM ID %#x.\n", receiver_id);
2097 ret = ffa_error(FFA_DENIED);
2098 goto out;
2099 }
2100
2101 receiver->mailbox.state = MAILBOX_STATE_RECEIVED;
2102
2103 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2104
2105out:
2106 vm_unlock(&receiver_locked);
2107
2108 return ret;
2109}
2110
2111/**
Andrew Walbran318f5732018-11-20 16:23:42 +00002112 * Enables or disables a given interrupt ID for the calling vCPU.
2113 *
2114 * Returns 0 on success, or -1 if the intid is invalid.
2115 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002116int64_t api_interrupt_enable(uint32_t intid, bool enable,
2117 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002118{
Manish Pandey35e452f2021-02-18 21:36:34 +00002119 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002120 struct interrupts *interrupts = &current->interrupts;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002121
Andrew Walbran318f5732018-11-20 16:23:42 +00002122 if (intid >= HF_NUM_INTIDS) {
2123 return -1;
2124 }
2125
Manish Pandey35e452f2021-02-18 21:36:34 +00002126 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002127 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002128 /*
2129 * If it is pending and was not enabled before, increment the
2130 * count.
2131 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002132 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2133 !vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2134 vcpu_interrupt_count_increment(current_locked,
2135 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002136 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002137 vcpu_virt_interrupt_set_enabled(interrupts, intid);
2138 vcpu_virt_interrupt_set_type(interrupts, intid, type);
Andrew Walbran318f5732018-11-20 16:23:42 +00002139 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002140 /*
2141 * If it is pending and was enabled before, decrement the count.
2142 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002143 if (vcpu_is_virt_interrupt_pending(interrupts, intid) &&
2144 vcpu_is_virt_interrupt_enabled(interrupts, intid)) {
2145 vcpu_interrupt_count_decrement(current_locked,
2146 interrupts, intid);
Andrew Walbran3d84a262018-12-13 14:41:19 +00002147 }
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002148 vcpu_virt_interrupt_clear_enabled(interrupts, intid);
2149 vcpu_virt_interrupt_set_type(interrupts, intid,
2150 INTERRUPT_TYPE_IRQ);
Andrew Walbran318f5732018-11-20 16:23:42 +00002151 }
2152
Manish Pandey35e452f2021-02-18 21:36:34 +00002153 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002154 return 0;
2155}
2156
2157/**
2158 * Returns the ID of the next pending interrupt for the calling vCPU, and
2159 * acknowledges it (i.e. marks it as no longer pending). Returns
2160 * HF_INVALID_INTID if there are no pending interrupts.
2161 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00002162uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00002163{
Raghu Krishnamurthy61a025b2022-07-20 08:37:04 -07002164 uint32_t i;
Andrew Walbran318f5732018-11-20 16:23:42 +00002165 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00002166 struct vcpu_locked current_locked;
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002167 struct interrupts *interrupts = &current->interrupts;
Andrew Walbran318f5732018-11-20 16:23:42 +00002168
2169 /*
2170 * Find the first enabled and pending interrupt ID, return it, and
2171 * deactivate it.
2172 */
Manish Pandey35e452f2021-02-18 21:36:34 +00002173 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00002174 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
2175 uint32_t enabled_and_pending =
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002176 interrupts->interrupt_enabled.bitmap[i] &
2177 interrupts->interrupt_pending.bitmap[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002178
Andrew Walbran318f5732018-11-20 16:23:42 +00002179 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00002180 uint8_t bit_index = ctz(enabled_and_pending);
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002181
2182 first_interrupt =
2183 i * INTERRUPT_REGISTER_BITS + bit_index;
Manish Pandey35e452f2021-02-18 21:36:34 +00002184
Andrew Walbran3d84a262018-12-13 14:41:19 +00002185 /*
2186 * Mark it as no longer pending and decrement the count.
2187 */
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002188 vcpu_virt_interrupt_clear_pending(interrupts,
2189 first_interrupt);
Manish Pandey35e452f2021-02-18 21:36:34 +00002190
Daniel Boulby4ca50f02022-07-29 18:29:34 +01002191 vcpu_interrupt_count_decrement(
2192 current_locked, interrupts, first_interrupt);
Andrew Walbran318f5732018-11-20 16:23:42 +00002193 break;
2194 }
2195 }
Andrew Walbran318f5732018-11-20 16:23:42 +00002196
Manish Pandey35e452f2021-02-18 21:36:34 +00002197 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00002198 return first_interrupt;
2199}
2200
2201/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00002202 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00002203 * given VM and vCPU.
2204 */
2205static inline bool is_injection_allowed(uint32_t target_vm_id,
2206 struct vcpu *current)
2207{
2208 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002209
Andrew Walbran318f5732018-11-20 16:23:42 +00002210 /*
2211 * The primary VM is allowed to inject interrupts into any VM. Secondary
2212 * VMs are only allowed to inject interrupts into their own vCPUs.
2213 */
2214 return current_vm_id == HF_PRIMARY_VM_ID ||
2215 current_vm_id == target_vm_id;
2216}
2217
2218/**
2219 * Injects a virtual interrupt of the given ID into the given target vCPU.
2220 * This doesn't cause the vCPU to actually be run immediately; it will be taken
2221 * when the vCPU is next run, which is up to the scheduler.
2222 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00002223 * Returns:
2224 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
2225 * ID is invalid, or the current VM is not allowed to inject interrupts to
2226 * the target VM.
2227 * - 0 on success if no further action is needed.
2228 * - 1 if it was called by the primary VM and the primary VM now needs to wake
2229 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00002230 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002231int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
2232 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01002233 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00002234{
Andrew Walbran318f5732018-11-20 16:23:42 +00002235 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01002236 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00002237
2238 if (intid >= HF_NUM_INTIDS) {
2239 return -1;
2240 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002241
Andrew Walbran318f5732018-11-20 16:23:42 +00002242 if (target_vm == NULL) {
2243 return -1;
2244 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002245
Andrew Walbran318f5732018-11-20 16:23:42 +00002246 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00002247 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00002248 return -1;
2249 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002250
Andrew Walbran318f5732018-11-20 16:23:42 +00002251 if (!is_injection_allowed(target_vm_id, current)) {
2252 return -1;
2253 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00002254
Andrew Walbrane1310df2019-04-29 17:28:28 +01002255 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00002256
Manish Pandey35e452f2021-02-18 21:36:34 +00002257 dlog_verbose(
2258 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
2259 "%u\n",
2260 intid, target_vm_id, target_vcpu_idx, current->vm->id,
2261 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01002262 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00002263}
Andrew Scull6386f252018-12-06 13:29:10 +00002264
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002265/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002266struct ffa_value api_ffa_version(struct vcpu *current,
2267 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002268{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002269 struct vm_locked current_vm_locked;
2270
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002271 /*
2272 * Ensure that both major and minor revision representation occupies at
2273 * most 15 bits.
2274 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002275 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002276 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002277 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01002278 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002279 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01002280 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00002281 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01002282 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002283
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00002284 current_vm_locked = vm_lock(current->vm);
2285 current_vm_locked.vm->ffa_version = requested_version;
2286 vm_unlock(&current_vm_locked);
2287
Daniel Boulby6e32c612021-02-17 15:09:41 +00002288 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01002289}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002290
2291int64_t api_debug_log(char c, struct vcpu *current)
2292{
Andrew Sculld54e1be2019-08-20 11:09:42 +01002293 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002294 struct vm *vm = current->vm;
2295 struct vm_locked vm_locked = vm_lock(vm);
2296
Andrew Sculld54e1be2019-08-20 11:09:42 +01002297 if (c == '\n' || c == '\0') {
2298 flush = true;
2299 } else {
2300 vm->log_buffer[vm->log_buffer_length++] = c;
2301 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
2302 }
2303
2304 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01002305 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
2306 vm->log_buffer_length);
2307 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01002308 }
2309
2310 vm_unlock(&vm_locked);
2311
2312 return 0;
2313}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002314
2315/**
J-Alves6f72ca82021-11-01 12:34:58 +00002316 * Helper for success return of FFA_FEATURES, for when it is used to query
2317 * an interrupt ID.
2318 */
2319struct ffa_value api_ffa_feature_success(uint32_t arg2)
2320{
2321 return (struct ffa_value){
2322 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
2323}
2324
2325/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002326 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002327 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002328 */
J-Alves6f72ca82021-11-01 12:34:58 +00002329struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002330{
J-Alves6f72ca82021-11-01 12:34:58 +00002331 /*
2332 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
2333 * if using a feature ID.
2334 */
2335 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
J-Alvesff676c12022-05-13 17:25:33 +01002336 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0U) {
J-Alves6f72ca82021-11-01 12:34:58 +00002337 return ffa_error(FFA_NOT_SUPPORTED);
2338 }
2339
2340 switch (feature_function_id) {
2341 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002342 case FFA_ERROR_32:
2343 case FFA_SUCCESS_32:
2344 case FFA_INTERRUPT_32:
2345 case FFA_VERSION_32:
2346 case FFA_FEATURES_32:
2347 case FFA_RX_RELEASE_32:
2348 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01002349 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01002350 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002351 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002352 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002353 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002354 case FFA_MEM_DONATE_32:
2355 case FFA_MEM_LEND_32:
2356 case FFA_MEM_SHARE_32:
2357 case FFA_MEM_RETRIEVE_REQ_32:
2358 case FFA_MEM_RETRIEVE_RESP_32:
2359 case FFA_MEM_RELINQUISH_32:
2360 case FFA_MEM_RECLAIM_32:
J-Alvesff676c12022-05-13 17:25:33 +01002361 case FFA_MEM_FRAG_RX_32:
2362 case FFA_MEM_FRAG_TX_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002363 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002364 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002365 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002366 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002367#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002368 /* FF-A v1.1 features. */
2369 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002370 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2371 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2372 case FFA_NOTIFICATION_BIND_32:
2373 case FFA_NOTIFICATION_UNBIND_32:
2374 case FFA_NOTIFICATION_SET_32:
2375 case FFA_NOTIFICATION_GET_32:
2376 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002377 case FFA_MEM_PERM_GET_32:
2378 case FFA_MEM_PERM_SET_32:
2379 case FFA_MEM_PERM_GET_64:
2380 case FFA_MEM_PERM_SET_64:
Federico Recanati25053ee2022-03-14 15:01:53 +01002381 case FFA_MSG_SEND2_32:
J-Alves3829fc02021-03-18 12:49:18 +00002382#endif
2383 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002384
2385#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2386 /* Check support of a feature provided respective feature ID. */
2387 case FFA_FEATURE_NPI:
2388 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2389 case FFA_FEATURE_SRI:
2390 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2391#endif
2392 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002393 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002394 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002395 }
2396}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002397
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002398/**
J-Alves645eabe2021-02-22 16:08:27 +00002399 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2400 * interfaces.
2401 */
2402static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2403{
2404 return args.arg2 == 0U;
2405}
2406
2407/**
J-Alves76d99af2021-03-10 17:42:11 +00002408 * Limits size of arguments in ffa_value structure to 32-bit.
2409 */
2410static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2411{
2412 return (struct ffa_value){
2413 .func = (uint32_t)args.func,
2414 .arg1 = (uint32_t)args.arg1,
2415 .arg2 = (uint32_t)0,
2416 .arg3 = (uint32_t)args.arg3,
2417 .arg4 = (uint32_t)args.arg4,
2418 .arg5 = (uint32_t)args.arg5,
2419 .arg6 = (uint32_t)args.arg6,
2420 .arg7 = (uint32_t)args.arg7,
2421 };
2422}
2423
2424/**
2425 * Helper to copy direct message payload, depending on SMC used and expected
2426 * registers size.
2427 */
2428static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2429{
2430 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2431 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2432 return api_ffa_value_copy32(args);
2433 }
2434
2435 return (struct ffa_value){
2436 .func = args.func,
2437 .arg1 = args.arg1,
2438 .arg2 = 0,
2439 .arg3 = args.arg3,
2440 .arg4 = args.arg4,
2441 .arg5 = args.arg5,
2442 .arg6 = args.arg6,
2443 .arg7 = args.arg7,
2444 };
2445}
2446
2447/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002448 * Send an FF-A direct message request.
2449 */
2450struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2451 ffa_vm_id_t receiver_vm_id,
2452 struct ffa_value args,
2453 struct vcpu *current,
2454 struct vcpu **next)
2455{
J-Alves17228f72021-04-20 17:13:19 +01002456 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002457 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002458 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002459 struct vcpu *receiver_vcpu;
2460 struct two_vcpu_locked vcpus_locked;
2461
J-Alves645eabe2021-02-22 16:08:27 +00002462 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2463 return ffa_error(FFA_INVALID_PARAMETERS);
2464 }
2465
Olivier Deprez55a189e2021-06-09 15:45:27 +02002466 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2467 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002468 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002469 }
2470
Olivier Deprez55a189e2021-06-09 15:45:27 +02002471 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002472 return ret;
2473 }
2474
2475 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2476
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002477 receiver_vm = vm_find(receiver_vm_id);
2478 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002479 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002480 return ffa_error(FFA_INVALID_PARAMETERS);
2481 }
2482
2483 /*
J-Alves439ac972021-11-18 17:32:03 +00002484 * Check if sender supports sending direct message req, and if
2485 * receiver supports receipt of direct message requests.
2486 */
2487 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2488 return ffa_error(FFA_DENIED);
2489 }
2490
2491 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002492 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002493 * UP (migratable) or MP partitions with a number of vCPUs matching the
2494 * number of PEs in the system. It further states that MP partitions
2495 * accepting direct request messages cannot migrate.
2496 */
J-Alvesad6a0432021-04-09 16:06:21 +01002497 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002498 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002499 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002500 return ffa_error(FFA_INVALID_PARAMETERS);
2501 }
2502
J-Alves6e2abc62021-12-02 14:58:56 +00002503 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002504 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2505
2506 /*
2507 * If destination vCPU is executing or already received an
2508 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2509 * busy. There is a brief period of time where the vCPU state has
2510 * changed but regs_available is still false thus consider this case as
2511 * the vCPU not yet ready to receive a direct message request.
2512 */
2513 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2514 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2515 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002516 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002517 ret = ffa_error(FFA_BUSY);
2518 goto out;
2519 }
2520
2521 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2522 memory_order_relaxed)) {
2523 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002524 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002525 receiver_vcpu->vm->id,
2526 vcpu_index(receiver_vcpu));
2527 receiver_vcpu->state = VCPU_STATE_ABORTED;
2528 }
2529
2530 ret = ffa_error(FFA_ABORTED);
2531 goto out;
2532 }
2533
2534 switch (receiver_vcpu->state) {
2535 case VCPU_STATE_OFF:
2536 case VCPU_STATE_RUNNING:
2537 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002538 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002539 case VCPU_STATE_BLOCKED:
2540 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002541 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2542 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002543 ret = ffa_error(FFA_BUSY);
2544 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002545 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002546 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002547 * We expect target vCPU to be in WAITING state after either
2548 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002549 */
2550 break;
2551 }
2552
2553 /* Inject timer interrupt if any pending */
2554 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002555 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2556 HF_VIRTUAL_TIMER_INTID, current,
2557 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002558
2559 arch_timer_mask(&receiver_vcpu->regs);
2560 }
2561
2562 /* The receiver vCPU runs upon direct message invocation */
2563 receiver_vcpu->cpu = current->cpu;
2564 receiver_vcpu->state = VCPU_STATE_RUNNING;
2565 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002566 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002567
J-Alves76d99af2021-03-10 17:42:11 +00002568 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002569
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002570 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002571
2572 /* Switch to receiver vCPU targeted to by direct msg request */
2573 *next = receiver_vcpu;
2574
J-Alves6e2abc62021-12-02 14:58:56 +00002575 if (!receiver_locked.vm->el0_partition) {
2576 /*
2577 * If the scheduler in the system is giving CPU cycles to the
2578 * receiver, due to pending notifications, inject the NPI
2579 * interrupt. Following call assumes that '*next' has been set
2580 * to receiver_vcpu.
2581 */
2582 plat_ffa_inject_notification_pending_interrupt(
2583 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2584 ? vcpus_locked.vcpu1
2585 : vcpus_locked.vcpu2,
2586 current, receiver_locked);
2587 }
2588
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002589 /*
2590 * Since this flow will lead to a VM switch, the return value will not
2591 * be applied to current vCPU.
2592 */
2593
2594out:
2595 sl_unlock(&receiver_vcpu->lock);
2596 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002597 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002598
2599 return ret;
2600}
2601
2602/**
2603 * Send an FF-A direct message response.
2604 */
2605struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2606 ffa_vm_id_t receiver_vm_id,
2607 struct ffa_value args,
2608 struct vcpu *current,
2609 struct vcpu **next)
2610{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002611 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002612
2613 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2614 return ffa_error(FFA_INVALID_PARAMETERS);
2615 }
2616
J-Alves76d99af2021-03-10 17:42:11 +00002617 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002618
Olivier Deprez55a189e2021-06-09 15:45:27 +02002619 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2620 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002621 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002622 }
2623
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002624 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002625 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002626 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002627 * No need for REQ/RESP state management as managed exit does
2628 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002629 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002630 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2631 vcpu_unlock(&current_locked);
2632 return ffa_error(FFA_DENIED);
2633 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002634
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002635 /*
2636 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2637 * a secure interrupt to a SP that is performing managed exit.
2638 * We have taken a implementation defined choice to not allow
2639 * Managed exit while a SP is processing a secure interrupt.
2640 */
2641 CHECK(!current->processing_secure_interrupt);
2642
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002643 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002644 current->processing_managed_exit = false;
2645 } else {
2646 /*
2647 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2648 * defined originator.
2649 */
2650 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2651 /*
2652 * Sending direct response but direct request origin
2653 * vCPU is not set.
2654 */
2655 vcpu_unlock(&current_locked);
2656 return ffa_error(FFA_DENIED);
2657 }
2658
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002659 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002660 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2661 vcpu_unlock(&current_locked);
2662 return ffa_error(FFA_DENIED);
2663 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002664 }
2665
2666 /* Clear direct request origin for the caller. */
2667 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2668
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002669 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002670
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002671 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002672 *next = api_switch_to_other_world(
2673 current, to_ret,
2674 /*
2675 * Current vcpu sent a direct response. It moves to
2676 * waiting state.
2677 */
2678 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002679 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002680 *next = api_switch_to_primary(
2681 current, to_ret,
2682 /*
2683 * Current vcpu sent a direct response. It moves to
2684 * waiting state.
2685 */
2686 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002687 } else if (vm_id_is_current_world(receiver_vm_id)) {
2688 /*
2689 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002690 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002691 * made function return error before getting to this point.
2692 */
2693 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002694 /*
2695 * current vcpu sent a direct response.
2696 * It moves to waiting state.
2697 */
2698 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002699 } else {
2700 panic("Invalid direct message response invocation");
2701 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002702
2703 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2704}
2705
J-Alves84658fc2021-06-17 14:37:32 +01002706static bool api_memory_region_check_flags(
2707 struct ffa_memory_region *memory_region, uint32_t share_func)
2708{
2709 switch (share_func) {
2710 case FFA_MEM_SHARE_32:
2711 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2712 0U) {
2713 return false;
2714 }
2715 /* Intentional fall-through */
2716 case FFA_MEM_LEND_32:
2717 case FFA_MEM_DONATE_32: {
2718 /* Bits 31:2 Must Be Zero. */
2719 ffa_memory_receiver_flags_t to_mask =
2720 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2721 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2722
2723 if ((memory_region->flags & to_mask) != 0U) {
2724 return false;
2725 }
2726 break;
2727 }
2728 default:
2729 panic("Check for mem send calls only.\n");
2730 }
2731
2732 /* Last check reserved values are 0 */
2733 return true;
2734}
2735
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002736struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2737 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002738 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002739{
2740 struct vm *from = current->vm;
2741 struct vm *to;
2742 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002743 struct ffa_memory_region *memory_region;
2744 struct ffa_value ret;
J-Alves363f5722022-04-25 17:37:37 +01002745 bool targets_other_world = false;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002746
2747 if (ipa_addr(address) != 0 || page_count != 0) {
2748 /*
2749 * Hafnium only supports passing the descriptor in the TX
2750 * mailbox.
2751 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002752 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002753 }
2754
Andrew Walbranca808b12020-05-15 17:22:28 +01002755 if (fragment_length > length) {
2756 dlog_verbose(
2757 "Fragment length %d greater than total length %d.\n",
2758 fragment_length, length);
2759 return ffa_error(FFA_INVALID_PARAMETERS);
2760 }
2761 if (fragment_length < sizeof(struct ffa_memory_region) +
2762 sizeof(struct ffa_memory_access)) {
2763 dlog_verbose(
2764 "Initial fragment length %d smaller than header size "
2765 "%d.\n",
2766 fragment_length,
2767 sizeof(struct ffa_memory_region) +
2768 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002769 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002770 }
2771
2772 /*
2773 * Check that the sender has configured its send buffer. If the TX
2774 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2775 * be safely accessed after releasing the lock since the TX mailbox
2776 * address can only be configured once.
2777 */
2778 sl_lock(&from->lock);
2779 from_msg = from->mailbox.send;
2780 sl_unlock(&from->lock);
2781
2782 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002783 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002784 }
2785
2786 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002787 * Copy the memory region descriptor to a fresh page from the memory
2788 * pool. This prevents the sender from changing it underneath us, and
2789 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002790 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002791 if (fragment_length > HF_MAILBOX_SIZE ||
2792 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002793 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002794 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002795 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002796 if (memory_region == NULL) {
2797 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002798 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002799 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002800 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002801
2802 /* The sender must match the caller. */
2803 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002804 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002805 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002806 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002807 }
2808
J-Alves84658fc2021-06-17 14:37:32 +01002809 if (!api_memory_region_check_flags(memory_region, share_func)) {
2810 dlog_verbose(
2811 "Memory region reserved arguments must be zero.\n");
2812 ret = ffa_error(FFA_INVALID_PARAMETERS);
2813 goto out;
2814 }
2815
J-Alves363f5722022-04-25 17:37:37 +01002816 if (memory_region->receiver_count == 0U) {
2817 dlog_verbose("Receiver count can't be 0.\n");
2818 ret = ffa_error(FFA_INVALID_PARAMETERS);
2819 goto out;
2820 }
2821
2822 if (share_func == FFA_MEM_DONATE_32 &&
2823 memory_region->receiver_count != 1U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002824 dlog_verbose(
J-Alves363f5722022-04-25 17:37:37 +01002825 "FFA_MEM_DONATE only supports one recipient. "
2826 "Specified %u\n",
2827 memory_region->receiver_count);
2828 ret = ffa_error(FFA_INVALID_PARAMETERS);
2829 goto out;
2830 }
2831
2832 if (memory_region->receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
2833 dlog_verbose(
2834 "Max number of recipients supported is %u "
2835 "specified %u\n",
2836 MAX_MEM_SHARE_RECIPIENTS,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002837 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002838 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002839 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002840 }
2841
2842 /*
2843 * Ensure that the receiver VM exists and isn't the same as the sender.
J-Alves363f5722022-04-25 17:37:37 +01002844 * If there is a receiver from the other world, track it for later
2845 * forwarding if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002846 */
J-Alves363f5722022-04-25 17:37:37 +01002847 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
2848 ffa_vm_id_t receiver_id =
2849 memory_region->receivers[i]
2850 .receiver_permissions.receiver;
2851 to = vm_find(receiver_id);
2852
2853 if (vm_id_is_current_world(receiver_id) &&
2854 (to == NULL || to == from)) {
2855 dlog_verbose("%s: invalid receiver.\n", __func__);
2856 ret = ffa_error(FFA_INVALID_PARAMETERS);
2857 goto out;
2858 }
2859
2860 if (!plat_ffa_is_memory_send_valid(receiver_id, share_func)) {
2861 ret = ffa_error(FFA_DENIED);
2862 goto out;
2863 }
2864
2865 /* Capture if any of the receivers is from the other world. */
2866 if (!targets_other_world) {
2867 targets_other_world =
2868 !vm_id_is_current_world(receiver_id);
2869 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002870 }
2871
J-Alves363f5722022-04-25 17:37:37 +01002872 /* Allow for one memory region to be shared to the TEE. */
2873 if (targets_other_world) {
2874 assert(memory_region->receiver_count == 1 &&
2875 to->id == HF_TEE_VM_ID);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002876 /*
2877 * The 'to' VM lock is only needed in the case that it is the
2878 * TEE VM.
2879 */
2880 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2881
2882 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002883 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002884 goto out_unlock;
2885 }
2886
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002887 ret = ffa_memory_tee_send(
2888 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2889 length, fragment_length, share_func, &api_page_pool);
2890 /*
2891 * ffa_tee_memory_send takes ownership of the memory_region, so
2892 * make sure we don't free it.
2893 */
2894 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002895
2896 out_unlock:
2897 vm_unlock(&vm_to_from_lock.vm1);
2898 vm_unlock(&vm_to_from_lock.vm2);
2899 } else {
2900 struct vm_locked from_locked = vm_lock(from);
2901
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002902 ret = ffa_memory_send(from_locked, memory_region, length,
2903 fragment_length, share_func,
2904 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002905 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002906 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002907 * make sure we don't free it.
2908 */
2909 memory_region = NULL;
2910
2911 vm_unlock(&from_locked);
2912 }
2913
2914out:
2915 if (memory_region != NULL) {
2916 mpool_free(&api_page_pool, memory_region);
2917 }
2918
2919 return ret;
2920}
2921
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002922struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2923 uint32_t fragment_length,
2924 ipaddr_t address, uint32_t page_count,
2925 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002926{
2927 struct vm *to = current->vm;
2928 struct vm_locked to_locked;
2929 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002930 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002931 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002932 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002933
2934 if (ipa_addr(address) != 0 || page_count != 0) {
2935 /*
2936 * Hafnium only supports passing the descriptor in the TX
2937 * mailbox.
2938 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002939 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002940 }
2941
Andrew Walbrana65a1322020-04-06 19:32:32 +01002942 if (fragment_length != length) {
2943 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002944 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002945 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002946
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002947 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002948 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002949 message_buffer_size = cpu_get_buffer_size(current->cpu);
2950 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2951 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002952 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002953 }
2954
2955 to_locked = vm_lock(to);
2956 to_msg = to->mailbox.send;
2957
2958 if (to_msg == NULL) {
2959 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002960 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002961 goto out;
2962 }
2963
2964 /*
2965 * Copy the retrieve request descriptor to an internal buffer, so that
2966 * the caller can't change it underneath us.
2967 */
2968 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2969
2970 if (msg_receiver_busy(to_locked, NULL, false)) {
2971 /*
2972 * Can't retrieve memory information if the mailbox is not
2973 * available.
2974 */
2975 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002976 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002977 goto out;
2978 }
2979
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002980 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2981 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002982
2983out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002984 vm_unlock(&to_locked);
2985 return ret;
2986}
2987
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002988struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002989{
2990 struct vm *from = current->vm;
2991 struct vm_locked from_locked;
2992 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002993 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002994 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002995 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002996 uint32_t length;
2997
2998 from_locked = vm_lock(from);
2999 from_msg = from->mailbox.send;
3000
3001 if (from_msg == NULL) {
3002 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003003 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003004 goto out;
3005 }
3006
3007 /*
3008 * Calculate length from relinquish descriptor before copying. We will
3009 * check again later to make sure it hasn't changed.
3010 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003011 length = sizeof(struct ffa_mem_relinquish) +
3012 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
3013 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003014 /*
3015 * Copy the relinquish descriptor to an internal buffer, so that the
3016 * caller can't change it underneath us.
3017 */
3018 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003019 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003020 message_buffer_size = cpu_get_buffer_size(current->cpu);
3021 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
3022 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003023 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003024 goto out;
3025 }
3026 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
3027
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003028 if (sizeof(struct ffa_mem_relinquish) +
3029 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003030 length) {
3031 dlog_verbose(
3032 "Endpoint count changed while copying to internal "
3033 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003034 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003035 goto out;
3036 }
3037
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003038 ret = ffa_memory_relinquish(from_locked, relinquish_request,
3039 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003040
3041out:
3042 vm_unlock(&from_locked);
3043 return ret;
3044}
3045
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003046struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
3047 ffa_memory_region_flags_t flags,
3048 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003049{
3050 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003051 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003052
Olivier Deprez55a189e2021-06-09 15:45:27 +02003053 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00003054 struct vm_locked to_locked = vm_lock(to);
3055
Andrew Walbranca808b12020-05-15 17:22:28 +01003056 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003057 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003058
Andrew Walbran290b0c92020-02-03 16:37:14 +00003059 vm_unlock(&to_locked);
3060 } else {
3061 struct vm *from = vm_find(HF_TEE_VM_ID);
3062 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
3063
Andrew Walbranca808b12020-05-15 17:22:28 +01003064 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
3065 vm_to_from_lock.vm2, handle, flags,
3066 &api_page_pool);
3067
3068 vm_unlock(&vm_to_from_lock.vm1);
3069 vm_unlock(&vm_to_from_lock.vm2);
3070 }
3071
3072 return ret;
3073}
3074
3075struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
3076 uint32_t fragment_offset,
3077 ffa_vm_id_t sender_vm_id,
3078 struct vcpu *current)
3079{
3080 struct vm *to = current->vm;
3081 struct vm_locked to_locked;
3082 struct ffa_value ret;
3083
3084 /* Sender ID MBZ at virtual instance. */
3085 if (sender_vm_id != 0) {
3086 return ffa_error(FFA_INVALID_PARAMETERS);
3087 }
3088
3089 to_locked = vm_lock(to);
3090
3091 if (msg_receiver_busy(to_locked, NULL, false)) {
3092 /*
3093 * Can't retrieve memory information if the mailbox is not
3094 * available.
3095 */
3096 dlog_verbose("RX buffer not ready.\n");
3097 ret = ffa_error(FFA_BUSY);
3098 goto out;
3099 }
3100
3101 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
3102 &api_page_pool);
3103
3104out:
3105 vm_unlock(&to_locked);
3106 return ret;
3107}
3108
3109struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
3110 uint32_t fragment_length,
3111 ffa_vm_id_t sender_vm_id,
3112 struct vcpu *current)
3113{
3114 struct vm *from = current->vm;
3115 const void *from_msg;
3116 void *fragment_copy;
3117 struct ffa_value ret;
3118
3119 /* Sender ID MBZ at virtual instance. */
3120 if (sender_vm_id != 0) {
3121 return ffa_error(FFA_INVALID_PARAMETERS);
3122 }
3123
3124 /*
3125 * Check that the sender has configured its send buffer. If the TX
3126 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
3127 * be safely accessed after releasing the lock since the TX mailbox
3128 * address can only be configured once.
3129 */
3130 sl_lock(&from->lock);
3131 from_msg = from->mailbox.send;
3132 sl_unlock(&from->lock);
3133
3134 if (from_msg == NULL) {
3135 return ffa_error(FFA_INVALID_PARAMETERS);
3136 }
3137
3138 /*
3139 * Copy the fragment to a fresh page from the memory pool. This prevents
3140 * the sender from changing it underneath us, and also lets us keep it
3141 * around in the share state table if needed.
3142 */
3143 if (fragment_length > HF_MAILBOX_SIZE ||
3144 fragment_length > MM_PPOOL_ENTRY_SIZE) {
3145 dlog_verbose(
3146 "Fragment length %d larger than mailbox size %d.\n",
3147 fragment_length, HF_MAILBOX_SIZE);
3148 return ffa_error(FFA_INVALID_PARAMETERS);
3149 }
3150 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
3151 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
3152 0) {
3153 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
3154 return ffa_error(FFA_INVALID_PARAMETERS);
3155 }
3156 fragment_copy = mpool_alloc(&api_page_pool);
3157 if (fragment_copy == NULL) {
3158 dlog_verbose("Failed to allocate fragment copy.\n");
3159 return ffa_error(FFA_NO_MEMORY);
3160 }
3161 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
3162
3163 /*
3164 * Hafnium doesn't support fragmentation of memory retrieve requests
3165 * (because it doesn't support caller-specified mappings, so a request
3166 * will never be larger than a single page), so this must be part of a
3167 * memory send (i.e. donate, lend or share) request.
3168 *
3169 * We can tell from the handle whether the memory transaction is for the
3170 * TEE or not.
3171 */
3172 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
3173 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
3174 struct vm_locked from_locked = vm_lock(from);
3175
3176 ret = ffa_memory_send_continue(from_locked, fragment_copy,
3177 fragment_length, handle,
3178 &api_page_pool);
3179 /*
3180 * `ffa_memory_send_continue` takes ownership of the
3181 * fragment_copy, so we don't need to free it here.
3182 */
3183 vm_unlock(&from_locked);
3184 } else {
3185 struct vm *to = vm_find(HF_TEE_VM_ID);
3186 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
3187
3188 /*
3189 * The TEE RX buffer state is checked in
3190 * `ffa_memory_tee_send_continue` rather than here, as we need
3191 * to return `FFA_MEM_FRAG_RX` with the current offset rather
3192 * than FFA_ERROR FFA_BUSY in case it is busy.
3193 */
3194
3195 ret = ffa_memory_tee_send_continue(
3196 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
3197 fragment_length, handle, &api_page_pool);
3198 /*
3199 * `ffa_memory_tee_send_continue` takes ownership of the
3200 * fragment_copy, so we don't need to free it here.
3201 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00003202
3203 vm_unlock(&vm_to_from_lock.vm1);
3204 vm_unlock(&vm_to_from_lock.vm2);
3205 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00003206
3207 return ret;
3208}
Max Shvetsov40108e72020-08-27 12:39:50 +01003209
Olivier Deprezd614d322021-06-18 15:21:00 +02003210/**
3211 * Register an entry point for a vCPU in warm boot cases.
3212 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
3213 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003214struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
3215 struct vcpu *current)
3216{
3217 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02003218 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01003219
Olivier Deprezd614d322021-06-18 15:21:00 +02003220 /*
3221 * Reject if interface is not supported at this FF-A instance
3222 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
3223 */
3224 if (!plat_ffa_is_secondary_ep_register_supported() ||
3225 current->vm->vcpu_count == 1) {
3226 return ffa_error(FFA_NOT_SUPPORTED);
3227 }
3228
3229 /*
3230 * No further check is made on the address validity
3231 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
3232 * from the VM or vCPU structure.
3233 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
3234 * For each SP [...] the Framework assumes that the same entry point
3235 * address is used for initializing any execution context during a
3236 * secondary cold boot.
3237 * If this function is invoked multiple times, then the entry point
3238 * address specified in the last valid invocation must be used by the
3239 * callee.
3240 */
Max Shvetsov40108e72020-08-27 12:39:50 +01003241 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02003242 if (vm_locked.vm->initialized) {
3243 goto out;
3244 }
3245
Max Shvetsov40108e72020-08-27 12:39:50 +01003246 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02003247
3248 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3249
3250out:
Max Shvetsov40108e72020-08-27 12:39:50 +01003251 vm_unlock(&vm_locked);
3252
Olivier Deprezd614d322021-06-18 15:21:00 +02003253 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01003254}
J-Alvesa0f317d2021-06-09 13:31:59 +01003255
3256struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
3257 ffa_vcpu_count_t vcpu_count,
3258 struct vcpu *current)
3259{
3260 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3261 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
3262 vm_id);
3263 return ffa_error(FFA_NOT_SUPPORTED);
3264 }
3265
3266 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
3267}
3268
3269struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
3270 struct vcpu *current)
3271{
3272 /*
3273 * Validity of use of this interface is the same as for bitmap create.
3274 */
3275 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
3276 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
3277 vm_id);
3278 return ffa_error(FFA_NOT_SUPPORTED);
3279 }
3280
3281 return plat_ffa_notifications_bitmap_destroy(vm_id);
3282}
J-Alvesc003a7a2021-03-18 13:06:53 +00003283
3284struct ffa_value api_ffa_notification_update_bindings(
3285 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3286 ffa_notifications_bitmap_t notifications, bool is_bind,
3287 struct vcpu *current)
3288{
3289 struct ffa_value ret = {.func = FFA_SUCCESS_32};
3290 struct vm_locked receiver_locked;
3291 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3292 const ffa_vm_id_t id_to_update =
3293 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
3294 const ffa_vm_id_t id_to_validate =
3295 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
3296
3297 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
3298 receiver_vm_id)) {
3299 dlog_verbose("Invalid use of notifications bind interface.\n");
3300 return ffa_error(FFA_INVALID_PARAMETERS);
3301 }
3302
J-Alvesb15e9402021-09-08 11:44:42 +01003303 if (plat_ffa_notifications_update_bindings_forward(
3304 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
3305 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01003306 return ret;
3307 }
3308
J-Alvesc003a7a2021-03-18 13:06:53 +00003309 if (notifications == 0U) {
3310 dlog_verbose("No notifications have been specified.\n");
3311 return ffa_error(FFA_INVALID_PARAMETERS);
3312 }
3313
3314 /**
3315 * This check assumes receiver is the current VM, and has been enforced
3316 * by 'plat_ffa_is_notifications_bind_valid'.
3317 */
3318 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3319
3320 if (receiver_locked.vm == NULL) {
3321 dlog_verbose("Receiver doesn't exist!\n");
3322 return ffa_error(FFA_DENIED);
3323 }
3324
J-Alves09ff9d82021-11-02 11:55:20 +00003325 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00003326 dlog_verbose("Notifications are not enabled.\n");
3327 ret = ffa_error(FFA_NOT_SUPPORTED);
3328 goto out;
3329 }
3330
3331 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
3332 vm_find(sender_vm_id) == NULL) {
3333 dlog_verbose("Sender VM does not exist!\n");
3334 ret = ffa_error(FFA_INVALID_PARAMETERS);
3335 goto out;
3336 }
3337
3338 /*
3339 * Can't bind/unbind notifications if at least one is bound to a
3340 * different sender.
3341 */
3342 if (!vm_notifications_validate_bound_sender(
3343 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3344 id_to_validate, notifications)) {
3345 dlog_verbose("Notifications are bound to other sender.\n");
3346 ret = ffa_error(FFA_DENIED);
3347 goto out;
3348 }
3349
3350 /**
3351 * Check if there is a pending notification within those specified in
3352 * the bitmap.
3353 */
3354 if (vm_are_notifications_pending(receiver_locked,
3355 plat_ffa_is_vm_id(sender_vm_id),
3356 notifications)) {
3357 dlog_verbose("Notifications within '%x' pending.\n",
3358 notifications);
3359 ret = ffa_error(FFA_DENIED);
3360 goto out;
3361 }
3362
3363 vm_notifications_update_bindings(
3364 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
3365 notifications, is_per_vcpu && is_bind);
3366
3367out:
3368 vm_unlock(&receiver_locked);
3369 return ret;
3370}
J-Alvesaa79c012021-07-09 14:29:45 +01003371
3372struct ffa_value api_ffa_notification_set(
3373 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
3374 ffa_notifications_bitmap_t notifications, struct vcpu *current)
3375{
3376 struct ffa_value ret;
3377 struct vm_locked receiver_locked;
3378
3379 /*
3380 * Check if is per-vCPU or global, and extracting vCPU ID according
3381 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
3382 */
3383 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
3384 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
3385
J-Alvesaa79c012021-07-09 14:29:45 +01003386 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
3387 receiver_vm_id)) {
3388 dlog_verbose("Invalid use of notifications set interface.\n");
3389 return ffa_error(FFA_INVALID_PARAMETERS);
3390 }
3391
3392 if (notifications == 0U) {
3393 dlog_verbose("No notifications have been specified.\n");
3394 return ffa_error(FFA_INVALID_PARAMETERS);
3395 }
3396
J-Alvesde7bd2f2021-09-09 19:54:35 +01003397 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3398 flags, notifications, &ret)) {
3399 return ret;
3400 }
3401
J-Alvesaa79c012021-07-09 14:29:45 +01003402 /*
3403 * This check assumes receiver is the current VM, and has been enforced
3404 * by 'plat_ffa_is_notification_set_valid'.
3405 */
3406 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3407
3408 if (receiver_locked.vm == NULL) {
3409 dlog_verbose("Receiver ID is not valid.\n");
3410 return ffa_error(FFA_INVALID_PARAMETERS);
3411 }
3412
J-Alves09ff9d82021-11-02 11:55:20 +00003413 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003414 dlog_verbose("Receiver's notifications not enabled.\n");
3415 ret = ffa_error(FFA_DENIED);
3416 goto out;
3417 }
3418
3419 /*
3420 * If notifications are not bound to the sender, they wouldn't be
3421 * enabled either for the receiver.
3422 */
3423 if (!vm_notifications_validate_binding(
3424 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3425 sender_vm_id, notifications, is_per_vcpu)) {
3426 dlog_verbose("Notifications bindings not valid.\n");
3427 ret = ffa_error(FFA_DENIED);
3428 goto out;
3429 }
3430
3431 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3432 dlog_verbose("Invalid VCPU ID!\n");
3433 ret = ffa_error(FFA_INVALID_PARAMETERS);
3434 goto out;
3435 }
3436
J-Alves7461ef22021-10-18 17:21:33 +01003437 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003438 vm_notifications_partition_set_pending(
3439 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3440 vcpu_id, is_per_vcpu);
3441
J-Alvesaa79c012021-07-09 14:29:45 +01003442 dlog_verbose("Set the notifications: %x.\n", notifications);
3443
J-Alves13394022021-06-30 13:48:49 +01003444 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3445 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3446 vcpu_index(current));
3447 plat_ffa_sri_trigger_not_delayed(current->cpu);
3448 } else {
3449 plat_ffa_sri_state_set(DELAYED);
3450 }
J-Alvesaa79c012021-07-09 14:29:45 +01003451
J-Alves13394022021-06-30 13:48:49 +01003452 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003453out:
3454 vm_unlock(&receiver_locked);
3455
3456 return ret;
3457}
3458
3459static struct ffa_value api_ffa_notification_get_success_return(
3460 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3461 ffa_notifications_bitmap_t from_framework)
3462{
3463 return (struct ffa_value){
3464 .func = FFA_SUCCESS_32,
3465 .arg1 = 0U,
3466 .arg2 = (uint32_t)from_sp,
3467 .arg3 = (uint32_t)(from_sp >> 32),
3468 .arg4 = (uint32_t)from_vm,
3469 .arg5 = (uint32_t)(from_vm >> 32),
3470 .arg6 = (uint32_t)from_framework,
3471 .arg7 = (uint32_t)(from_framework >> 32),
3472 };
3473}
3474
3475struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3476 ffa_vcpu_index_t vcpu_id,
3477 uint32_t flags, struct vcpu *current)
3478{
J-Alves663682a2022-03-25 13:56:51 +00003479 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003480 ffa_notifications_bitmap_t sp_notifications = 0;
3481 ffa_notifications_bitmap_t vm_notifications = 0;
3482 struct vm_locked receiver_locked;
3483 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003484 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3485 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3486 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3487 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3488
3489 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3490 if ((flags & flags_mbz) != 0U) {
3491 dlog_verbose(
3492 "Invalid flags bit(s) set in notifications get. [31:4] "
3493 "MBZ(%x)\n",
3494 flags);
3495 return ffa_error(FFA_INVALID_PARAMETERS);
3496 }
J-Alvesaa79c012021-07-09 14:29:45 +01003497
3498 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003499 * Following check should capture wrong uses of the interface,
3500 * depending on whether Hafnium is SPMC or hypervisor. On the
3501 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003502 */
J-Alvesfc95a302022-04-22 14:18:23 +01003503 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3504 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003505 dlog_verbose("Invalid use of notifications get interface.\n");
3506 return ffa_error(FFA_INVALID_PARAMETERS);
3507 }
3508
3509 /*
3510 * This check assumes receiver is the current VM, and has been enforced
3511 * by `plat_ffa_is_notifications_get_valid`.
3512 */
3513 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3514
3515 /*
3516 * `plat_ffa_is_notifications_get_valid` ensures following is never
3517 * true.
3518 */
3519 CHECK(receiver_locked.vm != NULL);
3520
3521 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3522 (receiver_locked.vm->vcpu_count != 1 &&
3523 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003524 dlog_verbose(
3525 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3526 vcpu_id, receiver_locked.vm->vcpu_count,
3527 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003528 ret = ffa_error(FFA_INVALID_PARAMETERS);
3529 goto out;
3530 }
3531
3532 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003533 if (!plat_ffa_notifications_get_from_sp(
3534 receiver_locked, vcpu_id, &sp_notifications,
3535 &ret)) {
3536 dlog_verbose("Failed to get notifications from sps.");
3537 goto out;
3538 }
J-Alvesaa79c012021-07-09 14:29:45 +01003539 }
3540
3541 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003542 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003543 receiver_locked, true, vcpu_id);
3544 }
3545
J-Alvesd605a092022-03-28 14:20:48 +01003546 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_HYP) != 0U ||
3547 (flags & FFA_NOTIFICATION_FLAG_BITMAP_SPM) != 0U) {
3548 if (!plat_ffa_notifications_get_framework_notifications(
3549 receiver_locked, &framework_notifications, flags,
3550 vcpu_id, &ret)) {
3551 dlog_verbose(
3552 "Failed to get notifications from "
3553 "framework.\n");
3554 goto out;
3555 }
3556 }
3557
J-Alves663682a2022-03-25 13:56:51 +00003558 ret = api_ffa_notification_get_success_return(
3559 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003560
J-Alvesfe23ebe2021-10-13 16:07:07 +01003561 /*
3562 * If there are no more pending notifications, change `sri_state` to
3563 * handled.
3564 */
3565 if (vm_is_notifications_pending_count_zero()) {
3566 plat_ffa_sri_state_set(HANDLED);
3567 }
3568
J-Alves6e2abc62021-12-02 14:58:56 +00003569 if (!receiver_locked.vm->el0_partition &&
3570 !vm_are_global_notifications_pending(receiver_locked)) {
3571 vm_notifications_set_npi_injected(receiver_locked, false);
3572 }
3573
J-Alvesaa79c012021-07-09 14:29:45 +01003574out:
3575 vm_unlock(&receiver_locked);
3576
3577 return ret;
3578}
J-Alvesc8e8a222021-06-08 17:33:52 +01003579
3580/**
3581 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3582 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3583 */
3584static struct ffa_value api_ffa_notification_info_get_success_return(
3585 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003586 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003587{
3588 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3589
3590 /*
3591 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3592 * hold the list of ids.
3593 */
3594 memcpy_s(&ret.arg3,
3595 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3596 sizeof(ids[0]) * ids_count);
3597
3598 /*
3599 * According to the spec x2 should have:
3600 * - Bit flagging if there are more notifications pending;
3601 * - The total number of elements (i.e. total list size);
3602 * - The number of VCPU IDs within each VM specific list.
3603 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003604 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3605 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3606 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003607
3608 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3609 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3610
3611 for (unsigned int i = 0; i < lists_count; i++) {
3612 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3613 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3614 }
3615
3616 return ret;
3617}
3618
3619struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3620{
3621 /*
3622 * Following set of variables should be populated with the return info.
3623 * At a successfull handling of this interface, they should be used
3624 * to populate the 'ret' structure in accordance to the table 17.29
3625 * of the FF-A v1.1 Beta0 specification.
3626 */
3627 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3628 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3629 uint32_t lists_count = 0;
3630 uint32_t ids_count = 0;
3631 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003632 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003633
3634 /*
3635 * This interface can only be called at NS virtual/physical FF-A
3636 * instance by the endpoint implementing the primary scheduler and the
3637 * Hypervisor/OS kernel.
3638 * In the SPM, following check passes if call has been forwarded from
3639 * the hypervisor.
3640 */
3641 if (current->vm->id != HF_PRIMARY_VM_ID) {
3642 dlog_verbose(
3643 "Only the receiver's scheduler can use this "
3644 "interface\n");
3645 return ffa_error(FFA_NOT_SUPPORTED);
3646 }
3647
J-Alvesca058c22021-09-10 14:02:07 +01003648 /*
3649 * Forward call to the other world, and fill the arrays used to assemble
3650 * return.
3651 */
3652 plat_ffa_notification_info_get_forward(
3653 ids, &ids_count, lists_sizes, &lists_count,
3654 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3655
3656 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3657
J-Alvesc8e8a222021-06-08 17:33:52 +01003658 /* Get notifications' info from this world */
3659 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3660 ++index) {
3661 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3662
3663 list_is_full = vm_notifications_info_get(
3664 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3665 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3666
3667 vm_unlock(&vm_locked);
3668 }
3669
3670 if (!list_is_full) {
3671 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003672 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003673 ids, &ids_count, lists_sizes, &lists_count,
3674 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3675 }
3676
3677 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003678 dlog_verbose(
3679 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003680 result = ffa_error(FFA_NO_DATA);
3681 } else {
3682 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003683 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003684 }
3685
J-Alvesfe23ebe2021-10-13 16:07:07 +01003686 plat_ffa_sri_state_set(HANDLED);
3687
J-Alves13394022021-06-30 13:48:49 +01003688 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003689}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003690
3691struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3692{
3693 struct vm_locked vm_locked;
3694 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3695 bool mode_ret = false;
3696 uint32_t mode = 0;
3697
3698 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3699 return ffa_error(FFA_NOT_SUPPORTED);
3700 }
3701
3702 if (!(current->vm->el0_partition)) {
3703 return ffa_error(FFA_DENIED);
3704 }
3705
3706 vm_locked = vm_lock(current->vm);
3707
3708 /*
3709 * mm_get_mode is used to check if the given base_addr page is already
3710 * mapped. If the page is unmapped, return error. If the page is mapped
3711 * appropriate attributes are returned to the caller. Note that
3712 * mm_get_mode returns true if the address is in the valid VA range as
3713 * supported by the architecture and MMU configurations, as opposed to
3714 * whether a page is mapped or not. For a page to be known as mapped,
3715 * the API must return true AND the returned mode must not have
3716 * MM_MODE_INVALID set.
3717 */
3718 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3719 va_add(base_addr, PAGE_SIZE), &mode);
3720 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3721 ret = ffa_error(FFA_INVALID_PARAMETERS);
3722 goto out;
3723 }
3724
3725 /* No memory should be marked RWX */
3726 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3727 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3728
3729 /*
3730 * S-EL0 partitions are expected to have all their pages marked as
3731 * non-global.
3732 */
3733 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3734 (MM_MODE_NG | MM_MODE_USER));
3735
3736 if (mode & MM_MODE_W) {
3737 /* No memory should be writeable but not readable. */
3738 CHECK(mode & MM_MODE_R);
3739 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3740 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3741 } else if (mode & MM_MODE_R) {
3742 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3743 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3744 if (!(mode & MM_MODE_X)) {
3745 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3746 }
3747 }
3748out:
3749 vm_unlock(&vm_locked);
3750 return ret;
3751}
3752
3753struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3754 uint32_t mem_perm, struct vcpu *current)
3755{
3756 struct vm_locked vm_locked;
3757 struct ffa_value ret;
3758 bool mode_ret = false;
3759 uint32_t original_mode;
3760 uint32_t new_mode;
3761 struct mpool local_page_pool;
3762
3763 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3764 return ffa_error(FFA_NOT_SUPPORTED);
3765 }
3766
3767 if (!(current->vm->el0_partition)) {
3768 return ffa_error(FFA_DENIED);
3769 }
3770
3771 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3772 return ffa_error(FFA_INVALID_PARAMETERS);
3773 }
3774
3775 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3776 (mem_perm != FFA_MEM_PERM_RX)) {
3777 return ffa_error(FFA_INVALID_PARAMETERS);
3778 }
3779
3780 /*
3781 * Create a local pool so any freed memory can't be used by another
3782 * thread. This is to ensure the original mapping can be restored if any
3783 * stage of the process fails.
3784 */
3785 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3786
3787 vm_locked = vm_lock(current->vm);
3788
3789 /*
3790 * All regions accessible by the partition are mapped during boot. If we
3791 * cannot get a successful translation for the page range, the request
3792 * to change permissions is rejected.
3793 * mm_get_mode is used to check if the given address range is already
3794 * mapped. If the range is unmapped, return error. If the range is
3795 * mapped appropriate attributes are returned to the caller. Note that
3796 * mm_get_mode returns true if the address is in the valid VA range as
3797 * supported by the architecture and MMU configurations, as opposed to
3798 * whether a page is mapped or not. For a page to be known as mapped,
3799 * the API must return true AND the returned mode must not have
3800 * MM_MODE_INVALID set.
3801 */
3802
3803 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3804 va_add(base_addr, page_count * PAGE_SIZE),
3805 &original_mode);
3806 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3807 ret = ffa_error(FFA_INVALID_PARAMETERS);
3808 goto out;
3809 }
3810
3811 /* Device memory cannot be marked as executable */
3812 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3813 ret = ffa_error(FFA_INVALID_PARAMETERS);
3814 goto out;
3815 }
3816
3817 new_mode = MM_MODE_USER | MM_MODE_NG;
3818
3819 if (mem_perm == FFA_MEM_PERM_RW) {
3820 new_mode |= MM_MODE_R | MM_MODE_W;
3821 } else if (mem_perm == FFA_MEM_PERM_RX) {
3822 new_mode |= MM_MODE_R | MM_MODE_X;
3823 } else if (mem_perm == FFA_MEM_PERM_RO) {
3824 new_mode |= MM_MODE_R;
3825 }
3826
3827 /*
3828 * Safe to re-map memory, since we know the requested permissions are
3829 * valid, and the memory requested to be re-mapped is also valid.
3830 */
3831 if (!mm_identity_prepare(
3832 &vm_locked.vm->ptable, pa_from_va(base_addr),
3833 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3834 new_mode, &local_page_pool)) {
3835 /*
3836 * Defrag the table into the local page pool.
3837 * mm_identity_prepare could have allocated or freed pages to
3838 * split blocks or tables etc.
3839 */
3840 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3841
3842 /*
3843 * Guaranteed to succeed mapping with old mode since the mapping
3844 * with old mode already existed and we have a local page pool
3845 * that should have sufficient memory to go back to the original
3846 * state.
3847 */
3848 CHECK(mm_identity_prepare(
3849 &vm_locked.vm->ptable, pa_from_va(base_addr),
3850 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3851 original_mode, &local_page_pool));
3852 mm_identity_commit(
3853 &vm_locked.vm->ptable, pa_from_va(base_addr),
3854 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3855 original_mode, &local_page_pool);
3856
3857 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3858 ret = ffa_error(FFA_NO_MEMORY);
3859 goto out;
3860 }
3861
3862 mm_identity_commit(
3863 &vm_locked.vm->ptable, pa_from_va(base_addr),
3864 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3865 &local_page_pool);
3866
3867 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3868
3869out:
3870 mpool_fini(&local_page_pool);
3871 vm_unlock(&vm_locked);
3872
3873 return ret;
3874}
Maksims Svecovs71b76702022-05-20 15:32:58 +01003875
3876/**
3877 * Helper function for FFA_CONSOLE_LOG ABI.
3878 * Writes number of characters to a given VM buffer.
3879 */
3880static rsize_t arg_to_char_helper(struct vm_locked from_locked,
3881 const uint64_t src, rsize_t src_size,
3882 rsize_t to_write)
3883{
3884 bool flush = false;
3885 char c;
3886 rsize_t size = src_size < to_write ? src_size : to_write;
3887 rsize_t written = 0;
3888
3889 if (size == 0) {
3890 return 0;
3891 }
3892
3893 while (written < size) {
3894 c = ((char *)&src)[written++];
3895 if (c == '\n' || c == '\0') {
3896 flush = true;
3897 } else {
3898 from_locked.vm->log_buffer
3899 [from_locked.vm->log_buffer_length++] = c;
3900 flush = (from_locked.vm->log_buffer_length ==
3901 LOG_BUFFER_SIZE);
3902 }
3903
3904 if (flush) {
3905 dlog_flush_vm_buffer(from_locked.vm->id,
3906 from_locked.vm->log_buffer,
3907 from_locked.vm->log_buffer_length);
3908 from_locked.vm->log_buffer_length = 0;
3909 }
3910 }
3911
3912 return written;
3913}
3914
3915/**
3916 * Implements FFA_CONSOLE_LOG buffered logging.
3917 */
3918struct ffa_value api_ffa_console_log(const struct ffa_value args,
3919 struct vcpu *current)
3920{
3921 struct vm *vm = current->vm;
3922 struct vm_locked vm_locked;
3923 size_t chars_in_param = args.func == FFA_CONSOLE_LOG_32
3924 ? sizeof(uint32_t)
3925 : sizeof(uint64_t);
3926 size_t total_to_write = args.arg1;
3927
3928 if (total_to_write == 0 || total_to_write > chars_in_param * 6) {
3929 return ffa_error(FFA_INVALID_PARAMETERS);
3930 }
3931
3932 vm_locked = vm_lock(vm);
3933
3934 total_to_write -= arg_to_char_helper(vm_locked, args.arg2,
3935 chars_in_param, total_to_write);
3936 total_to_write -= arg_to_char_helper(vm_locked, args.arg3,
3937 chars_in_param, total_to_write);
3938 total_to_write -= arg_to_char_helper(vm_locked, args.arg4,
3939 chars_in_param, total_to_write);
3940 total_to_write -= arg_to_char_helper(vm_locked, args.arg5,
3941 chars_in_param, total_to_write);
3942 total_to_write -= arg_to_char_helper(vm_locked, args.arg6,
3943 chars_in_param, total_to_write);
3944 arg_to_char_helper(vm_locked, args.arg7, chars_in_param,
3945 total_to_write);
3946
3947 vm_unlock(&vm_locked);
3948
3949 return (struct ffa_value){.func = FFA_SUCCESS_32};
3950}