blob: 67900b41787284728d88876331fb91d4c110014a [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 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 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500131struct 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 Walbranb5ab43c2020-04-30 11:32:54 +0100151 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100152 /*
153 * primary_ret.arg1 should already be set to the
154 * current VM ID and vCPU ID.
155 */
156 primary_ret.arg2 = 0;
157 } else {
158 primary_ret.arg2 = remaining_ns;
159 }
160 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100161 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100162 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000163 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100164 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000165
166 default:
167 /* Do nothing. */
168 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000169 }
170
J-Alvesfe7f7372020-11-09 11:32:12 +0000171 return api_switch_to_vm(current, primary_ret, secondary_state,
172 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100173}
174
175/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200176 * Choose next vCPU to run to be the counterpart vCPU in the other
177 * world (run the normal world if currently running in the secure
178 * world). Set current vCPU state to the given vcpu_state parameter.
179 * Set FF-A return values to the target vCPU in the other world.
180 *
181 * Called in context of a direct message response from a secure
182 * partition to a VM.
183 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100184struct vcpu *api_switch_to_other_world(struct vcpu *current,
185 struct ffa_value other_world_ret,
186 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187{
J-Alvesfe7f7372020-11-09 11:32:12 +0000188 return api_switch_to_vm(current, other_world_ret, vcpu_state,
189 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200190}
191
192/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100193 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
194 * registers the `from` VM to be notified when it becomes available.
195 */
196static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
197{
198 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
199 to.vm->mailbox.recv == NULL) {
200 /*
201 * Fail if the receiver isn't currently ready to receive data,
202 * setting up for notification if requested.
203 */
204 if (notify) {
205 struct wait_entry *entry =
206 vm_get_wait_entry(from, to.vm->id);
207
208 /* Append waiter only if it's not there yet. */
209 if (list_empty(&entry->wait_links)) {
210 list_append(&to.vm->mailbox.waiter_list,
211 &entry->wait_links);
212 }
213 }
214
215 return true;
216 }
217
218 return false;
219}
220
221/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000222 * Returns true if the given vCPU is executing in context of an
223 * FFA_MSG_SEND_DIRECT_REQ invocation.
224 */
225static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
226{
227 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
228}
229
230/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100231 * Returns true if the VM owning the given vCPU is supporting managed exit and
232 * the vCPU is currently processing a managed exit.
233 */
234static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
235{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100236 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100237 vcpu_locked.vcpu->processing_managed_exit);
238}
239
240/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000241 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000242 */
243struct vcpu *api_preempt(struct vcpu *current)
244{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245 struct ffa_value ret = {
246 .func = FFA_INTERRUPT_32,
247 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000248 };
249
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500250 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000251}
252
253/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000254 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000255 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100256 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100257struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100258{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100259 struct ffa_value ret = {
260 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
261 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100262 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000263
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000264 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100265 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100266}
267
268/**
Andrew Walbran33645652019-04-15 12:29:31 +0100269 * Puts the current vCPU in off mode, and returns to the primary VM.
270 */
271struct vcpu *api_vcpu_off(struct vcpu *current)
272{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100273 struct ffa_value ret = {
274 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
275 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100276 };
277
278 /*
279 * Disable the timer, so the scheduler doesn't get told to call back
280 * based on it.
281 */
282 arch_timer_disable_current();
283
284 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
285}
286
287/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500288 * The current vCPU is blocked on some resource and needs to relinquish
289 * control back to the execution context of the endpoint that originally
290 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000291 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000292struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000293{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
295 struct vcpu_locked current_locked;
296 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000297
298 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000299 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000301 }
302
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000303 current_locked = vcpu_lock(current);
304 is_direct_request_ongoing =
305 is_ffa_direct_msg_request_ongoing(current_locked);
306 vcpu_unlock(&current_locked);
307
308 if (is_direct_request_ongoing) {
309 return ffa_error(FFA_DENIED);
310 }
311
312 *next = api_switch_to_primary(
313 current,
314 (struct ffa_value){.func = FFA_YIELD_32,
315 .arg1 = ffa_vm_vcpu(current->vm->id,
316 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500317 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000318
319 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000320}
321
322/**
Andrew Walbran33645652019-04-15 12:29:31 +0100323 * Switches to the primary so that it can switch to the target, or kick it if it
324 * is already running on a different physical CPU.
325 */
326struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
327{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 struct ffa_value ret = {
329 .func = HF_FFA_RUN_WAKE_UP,
330 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
331 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100332 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500333 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100334}
335
336/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000337 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000338 */
339struct vcpu *api_abort(struct vcpu *current)
340{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000342
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100343 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000344 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000345
346 if (current->vm->id == HF_PRIMARY_VM_ID) {
347 /* TODO: what to do when the primary aborts? */
348 for (;;) {
349 /* Do nothing. */
350 }
351 }
352
353 atomic_store_explicit(&current->vm->aborting, true,
354 memory_order_relaxed);
355
356 /* TODO: free resources once all vCPUs abort. */
357
Andrew Sculld6ee1102019-04-05 22:12:42 +0100358 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000359}
360
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000361/*
362 * Format the partition info descriptors according to the version supported
363 * by the endpoint and return the size of the array created.
364 */
365static struct ffa_value send_versioned_partition_info_descriptors(
366 struct vm *current_vm, struct ffa_partition_info *partitions,
367 uint32_t vm_count)
368{
369 struct vm_locked current_vm_locked;
370 uint32_t version = current_vm->ffa_version;
371 void *versioned_partition_info;
372 uint32_t size;
373 struct ffa_value ret;
374
375 if (version == MAKE_FFA_VERSION(1, 0)) {
376 struct ffa_partition_info_v1_0 v1_0_partitions[2 * MAX_VMS];
377
378 for (uint32_t i = 0; i < vm_count; i++) {
379 v1_0_partitions[i].vm_id = partitions[i].vm_id;
380 v1_0_partitions[i].vcpu_count =
381 partitions[i].vcpu_count;
382 v1_0_partitions[i].properties =
383 partitions[i].properties;
384 }
385 versioned_partition_info = v1_0_partitions;
386 size = sizeof(v1_0_partitions[0]) * vm_count;
387 } else {
388 versioned_partition_info = partitions;
389 size = sizeof(partitions[0]) * vm_count;
390 }
391
392 if (size > FFA_MSG_PAYLOAD_MAX) {
393 dlog_error(
394 "Partition information does not fit in the VM's RX "
395 "buffer.\n");
396 return ffa_error(FFA_NO_MEMORY);
397 }
398
399 /*
400 * Partition information is returned in the VM's RX buffer, which is why
401 * the lock is needed.
402 */
403 current_vm_locked = vm_lock(current_vm);
404
405 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
406 /*
407 * Can't retrieve memory information if the mailbox is not
408 * available.
409 */
410 dlog_verbose("RX buffer not ready.\n");
411 ret = ffa_error(FFA_BUSY);
412 goto out_unlock;
413 }
414
415 /* Populate the VM's RX buffer with the partition information. */
416 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
417 versioned_partition_info, size);
418 current_vm->mailbox.recv_size = size;
419
420 /* Sender is Hypervisor in the normal world (TEE in secure world). */
421 current_vm->mailbox.recv_sender = HF_VM_ID_BASE;
422 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
423 current_vm->mailbox.state = MAILBOX_STATE_READ;
424
425 /* Return the count of partition information descriptors in w2. */
426 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
427
428out_unlock:
429 vm_unlock(&current_vm_locked);
430
431 return ret;
432}
433
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100434struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000435 const struct ffa_uuid *uuid,
436 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100437{
438 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100439 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000440 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
441 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100442 bool uuid_is_null = ffa_uuid_is_null(uuid);
Olivier Depreze562e542020-06-11 17:31:54 +0200443 struct ffa_partition_info partitions[2 * MAX_VMS];
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100444
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000445 /* Bits 31:1 Must Be Zero */
446 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
447 return ffa_error(FFA_INVALID_PARAMETERS);
448 }
449
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100450 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000451 * No need to count if we are returning the number of paritions as we
452 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100453 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000454 if (uuid_is_null && count_flag) {
455 vm_count = vm_get_count();
456 } else {
457 /*
458 * Iterate through the VMs to find the ones with a matching
459 * UUID. A Null UUID retrieves information for all VMs.
460 */
461 for (uint16_t index = 0; index < vm_get_count(); ++index) {
462 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100463
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000464 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
465 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100466
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000467 ++vm_count;
468 if (count_flag) {
469 continue;
470 }
471
472 partitions[array_index].vm_id = vm->id;
473 partitions[array_index].vcpu_count =
474 vm->vcpu_count;
475 partitions[array_index].properties =
476 plat_ffa_partition_properties(
477 current_vm->id, vm);
478 partitions[array_index].properties |=
479 vm_are_notifications_enabled(vm)
480 ? FFA_PARTITION_NOTIFICATION
481 : 0;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000482 partitions[array_index].uuid = vm->uuid;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000483 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100484 }
485 }
486
Olivier Depreze562e542020-06-11 17:31:54 +0200487 /* If UUID is Null vm_count must not be zero at this stage. */
488 CHECK(!uuid_is_null || vm_count != 0);
489
490 /*
491 * When running the Hypervisor:
492 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
493 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000494 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200495 * a secure partition and the query is forwarded to the SPMC.
496 * When running the SPMC:
497 * - If UUID is non-Null and vm_count is zero it means there is no such
498 * partition identified in the system.
499 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000500 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200501
502 /*
503 * Unrecognized UUID: does not match any of the VMs (or SPs)
504 * and is not Null.
505 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100506 if (vm_count == 0) {
507 return ffa_error(FFA_INVALID_PARAMETERS);
508 }
509
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000510 /*
511 * If the count flag is set we don't need to return the partition info
512 * descriptors.
513 */
514 if (count_flag) {
515 return (struct ffa_value){.func = FFA_SUCCESS_32,
516 .arg2 = vm_count};
517 }
518
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000519 return send_versioned_partition_info_descriptors(current_vm, partitions,
520 vm_count);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100521}
522
Andrew Scull9726c252019-01-23 13:44:19 +0000523/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000524 * Returns the ID of the VM.
525 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000527{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528 return (struct ffa_value){.func = FFA_SUCCESS_32,
529 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000530}
531
532/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200533 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
534 * FF-A instances.
535 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000536 */
537struct ffa_value api_ffa_spm_id_get(void)
538{
J-Alves3829fc02021-03-18 12:49:18 +0000539#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
540 /*
541 * Return the SPMC ID that was fetched during FF-A
542 * initialization.
543 */
544 return (struct ffa_value){.func = FFA_SUCCESS_32,
545 .arg2 = arch_ffa_spmc_id_get()};
546#else
547 return ffa_error(FFA_NOT_SUPPORTED);
548#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000549}
550
551/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000552 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000553 * function to indicate that register state for the given vCPU has been saved
554 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000555 */
556void api_regs_state_saved(struct vcpu *vcpu)
557{
558 sl_lock(&vcpu->lock);
559 vcpu->regs_available = true;
560 sl_unlock(&vcpu->lock);
561}
562
563/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000564 * Retrieves the next waiter and removes it from the wait list if the VM's
565 * mailbox is in a writable state.
566 */
567static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
568{
569 struct wait_entry *entry;
570 struct vm *vm = locked_vm.vm;
571
Andrew Sculld6ee1102019-04-05 22:12:42 +0100572 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000573 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
574 /* The mailbox is not writable or there are no waiters. */
575 return NULL;
576 }
577
578 /* Remove waiter from the wait list. */
579 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
580 wait_links);
581 list_remove(&entry->wait_links);
582 return entry;
583}
584
585/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000586 * Assuming that the arguments have already been checked by the caller, injects
587 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
588 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
589 * is next run, which is up to the scheduler.
590 *
591 * Returns:
592 * - 0 on success if no further action is needed.
593 * - 1 if it was called by the primary VM and the primary VM now needs to wake
594 * up or kick the target vCPU.
595 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100596int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
597 uint32_t intid, struct vcpu *current,
598 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000599{
Manish Pandey35e452f2021-02-18 21:36:34 +0000600 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000601 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000602 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
603 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000604 int64_t ret = 0;
605
Andrew Walbran508e63c2018-12-20 17:02:37 +0000606 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000607 * We only need to change state and (maybe) trigger a virtual interrupt
608 * if it is enabled and was not previously pending. Otherwise we can
609 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000610 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000611 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
612 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000613 intid_mask)) {
614 goto out;
615 }
616
617 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000618 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
619 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
620 vcpu_irq_count_increment(target_locked);
621 } else {
622 vcpu_fiq_count_increment(target_locked);
623 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000624
625 /*
626 * Only need to update state if there was not already an
627 * interrupt enabled and pending.
628 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000629 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000630 goto out;
631 }
632
Andrew Walbran508e63c2018-12-20 17:02:37 +0000633 if (current->vm->id == HF_PRIMARY_VM_ID) {
634 /*
635 * If the call came from the primary VM, let it know that it
636 * should run or kick the target vCPU.
637 */
638 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000639 } else if (current != target_vcpu && next != NULL) {
640 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000641 }
642
643out:
644 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000645 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000646
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200647 return ret;
648}
649
650/* Wrapper to internal_interrupt_inject with locking of target vCPU */
651static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
652 uint32_t intid, struct vcpu *current,
653 struct vcpu **next)
654{
655 int64_t ret;
656 struct vcpu_locked target_locked;
657
658 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100659 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200660 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000661
662 return ret;
663}
664
665/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
667 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100668 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100669static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100670{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000671 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100672 case FFA_MSG_SEND_32:
673 return (struct ffa_value){
674 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000675 .arg1 = (receiver->mailbox.recv_sender << 16) |
676 receiver->id,
677 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000678 default:
679 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000680 dlog_error("Tried to return an invalid message function %#x\n",
681 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100682 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000683 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100684}
685
686/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000687 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000688 * value needs to be forced onto the vCPU.
689 */
J-Alves6e2abc62021-12-02 14:58:56 +0000690static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100691 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000692{
Max Shvetsov40108e72020-08-27 12:39:50 +0100693 struct vcpu_locked vcpu_locked;
694 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000695 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500696 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000697 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000698
Andrew Scullb06d1752019-02-04 10:15:48 +0000699 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000700 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000701 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100702 * The VM lock is not needed in the common case so it must only be taken
703 * when it is going to be needed. This ensures there are no inter-vCPU
704 * dependencies in the common run case meaning the sensitive context
705 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000706 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100707 vcpu_locked = vcpu_lock(vcpu);
708
709#if SECURE_WORLD == 1
710
711 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
712 0)) {
713 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
714 __func__, vcpu->vm->id, current->cpu->id);
715 }
716
717#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000718 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000719 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
720 (!vcpu->vm->el0_partition &&
721 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
722 vcpu->state == VCPU_STATE_BLOCKED ||
723 vcpu->state == VCPU_STATE_PREEMPTED));
724
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000725 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100726 vcpu_unlock(&vcpu_locked);
727 vm_locked = vm_lock(vcpu->vm);
728 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000729 }
730
731 /*
732 * If the vCPU is already running somewhere then we can't run it here
733 * simultaneously. While it is actually running then the state should be
734 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
735 * stops running but while Hafnium is in the process of switching back
736 * to the primary there will be a brief period while the state has been
737 * updated but `regs_available` is still false (until
738 * `api_regs_state_saved` is called). We can't start running it again
739 * until this has finished, so count this state as still running for the
740 * purposes of this check.
741 */
742 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
743 /*
744 * vCPU is running on another pCPU.
745 *
746 * It's okay not to return the sleep duration here because the
747 * other physical CPU that is currently running this vCPU will
748 * return the sleep duration if needed.
749 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100750 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000751 ret = false;
752 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000753 }
Andrew Scull9726c252019-01-23 13:44:19 +0000754
755 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100756 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100757 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000758 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100759 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000760 }
761 ret = false;
762 goto out;
763 }
764
Andrew Walbran508e63c2018-12-20 17:02:37 +0000765 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100766 case VCPU_STATE_RUNNING:
767 case VCPU_STATE_OFF:
768 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000769 ret = false;
770 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000771
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500772 case VCPU_STATE_WAITING:
773 /*
774 * An initial FFA_RUN is necessary for secondary VM/SP to reach
775 * the message wait loop.
776 */
777 if (!vcpu->is_bootstrapped) {
778 vcpu->is_bootstrapped = true;
779 break;
780 }
781
J-Alves6e2abc62021-12-02 14:58:56 +0000782 assert(need_vm_lock == true);
783 if (!vm_locked.vm->el0_partition &&
784 plat_ffa_inject_notification_pending_interrupt(
785 vcpu_locked, current, vm_locked)) {
786 break;
787 }
788
Andrew Scullb06d1752019-02-04 10:15:48 +0000789 /*
790 * A pending message allows the vCPU to run so the message can
791 * be delivered directly.
792 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100793 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100794 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100795 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100796 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000797 break;
798 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500799
800 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
801 break;
802 }
803
804 if (arch_timer_enabled(&vcpu->regs)) {
805 timer_remaining_ns =
806 arch_timer_remaining_ns(&vcpu->regs);
807 if (timer_remaining_ns == 0) {
808 break;
809 }
810 } else {
811 dlog_verbose("Timer disabled\n");
812 }
813 run_ret->func = FFA_MSG_WAIT_32;
814 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
815 run_ret->arg2 = timer_remaining_ns;
816 ret = false;
817 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100818 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000819 if (need_vm_lock &&
820 plat_ffa_inject_notification_pending_interrupt(
821 vcpu_locked, current, vm_locked)) {
822 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
823 break;
824 }
825
Andrew Scullb06d1752019-02-04 10:15:48 +0000826 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000827 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000828 break;
829 }
830
Andrew Walbran508e63c2018-12-20 17:02:37 +0000831 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100832 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000833 arch_timer_remaining_ns(&vcpu->regs);
834
835 /*
836 * The timer expired so allow the interrupt to be
837 * delivered.
838 */
839 if (timer_remaining_ns == 0) {
840 break;
841 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100842 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000843
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100844 /*
845 * The vCPU is not ready to run, return the appropriate code to
846 * the primary which called vcpu_run.
847 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500848 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100849 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
850 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000851
852 ret = false;
853 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000854
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500855 case VCPU_STATE_BLOCKED:
856 /* A blocked vCPU is run unconditionally. Fall through. */
857 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000858 /* Check NPI is to be injected here. */
859 if (need_vm_lock) {
860 plat_ffa_inject_notification_pending_interrupt(
861 vcpu_locked, current, vm_locked);
862 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000863 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500864 default:
865 /*
866 * Execution not expected to reach here. Deny the request
867 * gracefully.
868 */
869 *run_ret = ffa_error(FFA_DENIED);
870 ret = false;
871 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000872 }
873
Andrew Scullb06d1752019-02-04 10:15:48 +0000874 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000875 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100876 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000877
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000878 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000879 * Mark the registers as unavailable now that we're about to reflect
880 * them onto the real registers. This will also prevent another physical
881 * CPU from trying to read these registers.
882 */
883 vcpu->regs_available = false;
884
885 ret = true;
886
887out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100888 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000889 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100890 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000891 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000892 return ret;
893}
894
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500896 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100897{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100898 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100899 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100900 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100901
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800902 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500903 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100904 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100905
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700906 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
907 return ret;
908 }
909
Andrew Scull19503262018-09-20 14:48:39 +0100910 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100911 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100912 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100913 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100914 }
915
Fuad Tabbaed294af2019-12-20 10:43:01 +0000916 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100917 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100918 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100919 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100920
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000921 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100922 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000923 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000924 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100925 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000926
Andrew Walbran508e63c2018-12-20 17:02:37 +0000927 /*
928 * Inject timer interrupt if timer has expired. It's safe to access
929 * vcpu->regs here because api_vcpu_prepare_run already made sure that
930 * regs_available was true (and then set it to false) before returning
931 * true.
932 */
933 if (arch_timer_pending(&vcpu->regs)) {
934 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100935 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
936 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000937
938 /*
939 * Set the mask bit so the hardware interrupt doesn't fire
940 * again. Ideally we wouldn't do this because it affects what
941 * the secondary vCPU sees, but if we don't then we end up with
942 * a loop of the interrupt firing each time we try to return to
943 * the secondary vCPU.
944 */
945 arch_timer_mask(&vcpu->regs);
946 }
947
Fuad Tabbaed294af2019-12-20 10:43:01 +0000948 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000949 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000950
Andrew Scull33fecd32019-01-08 14:48:27 +0000951 /*
952 * Set a placeholder return code to the scheduler. This will be
953 * overwritten when the switch back to the primary occurs.
954 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 ret.func = FFA_INTERRUPT_32;
956 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100957 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000958
Andrew Scull6d2db332018-10-10 15:28:17 +0100959out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100960 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100961}
962
963/**
Andrew Scull81e85092018-12-12 12:56:20 +0000964 * Check that the mode indicates memory that is valid, owned and exclusive.
965 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100966static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000967{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100968 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
969 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000970}
971
972/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100973 * Determines the value to be returned by api_ffa_rxtx_map and
974 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
975 * there are waiters, it also switches back to the primary VM for it to wake
976 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000977 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100978static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
979 struct vcpu *current,
980 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000981{
982 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000983
984 if (list_empty(&vm->mailbox.waiter_list)) {
985 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000987 }
988
989 if (vm->id == HF_PRIMARY_VM_ID) {
990 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100991 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000992 }
993
994 /*
995 * Switch back to the primary VM, informing it that there are waiters
996 * that need to be notified.
997 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000998 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100999 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001000 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001001
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001003}
1004
1005/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001006 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001007 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001008static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1009 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001010 paddr_t pa_send_begin, paddr_t pa_send_end,
1011 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001012 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001013 struct mpool *local_page_pool)
1014{
1015 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001016
1017 /* Map the send page as read-only in the hypervisor address space. */
1018 vm_locked.vm->mailbox.send =
1019 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001020 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001021 if (!vm_locked.vm->mailbox.send) {
1022 /* TODO: partial defrag of failed range. */
1023 /* Recover any memory consumed in failed mapping. */
1024 mm_defrag(mm_stage1_locked, local_page_pool);
1025 goto fail;
1026 }
1027
1028 /*
1029 * Map the receive page as writable in the hypervisor address space. On
1030 * failure, unmap the send page before returning.
1031 */
1032 vm_locked.vm->mailbox.recv =
1033 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001034 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001035 if (!vm_locked.vm->mailbox.recv) {
1036 /* TODO: partial defrag of failed range. */
1037 /* Recover any memory consumed in failed mapping. */
1038 mm_defrag(mm_stage1_locked, local_page_pool);
1039 goto fail_undo_send;
1040 }
1041
1042 ret = true;
1043 goto out;
1044
1045 /*
1046 * The following mappings will not require more memory than is available
1047 * in the local pool.
1048 */
1049fail_undo_send:
1050 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001051 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1052 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001053
1054fail:
1055 ret = false;
1056
1057out:
Andrew Sculle1322792019-07-01 17:46:10 +01001058 return ret;
1059}
1060
1061/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001062 * Sanity checks and configures the send and receive pages in the VM stage-2
1063 * and hypervisor stage-1 page tables.
1064 *
1065 * Returns:
1066 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001067 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001068 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1069 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001070 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001071 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001072 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001073
1074struct ffa_value api_vm_configure_pages(
1075 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1076 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1077 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001078{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001079 struct ffa_value ret;
1080 paddr_t pa_send_begin;
1081 paddr_t pa_send_end;
1082 paddr_t pa_recv_begin;
1083 paddr_t pa_recv_end;
1084 uint32_t orig_send_mode;
1085 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001086 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001087
1088 /* We only allow these to be setup once. */
1089 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1090 ret = ffa_error(FFA_DENIED);
1091 goto out;
1092 }
1093
1094 /* Hafnium only supports a fixed size of RX/TX buffers. */
1095 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1096 ret = ffa_error(FFA_INVALID_PARAMETERS);
1097 goto out;
1098 }
1099
1100 /* Fail if addresses are not page-aligned. */
1101 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1102 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1103 ret = ffa_error(FFA_INVALID_PARAMETERS);
1104 goto out;
1105 }
1106
1107 /* Convert to physical addresses. */
1108 pa_send_begin = pa_from_ipa(send);
1109 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1110 pa_recv_begin = pa_from_ipa(recv);
1111 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1112
1113 /* Fail if the same page is used for the send and receive pages. */
1114 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1115 ret = ffa_error(FFA_INVALID_PARAMETERS);
1116 goto out;
1117 }
Andrew Sculle1322792019-07-01 17:46:10 +01001118
1119 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001120 * Ensure the pages are valid, owned and exclusive to the VM and that
1121 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001122 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001123 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1124 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001125 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1126 (orig_send_mode & MM_MODE_R) == 0 ||
1127 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001128 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001129 goto out;
1130 }
1131
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001132 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1133 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001134 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1135 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001136 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001137 goto out;
1138 }
Andrew Sculle1322792019-07-01 17:46:10 +01001139
1140 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001141 uint32_t mode =
1142 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1143 if (vm_locked.vm->el0_partition) {
1144 mode |= MM_MODE_USER | MM_MODE_NG;
1145 }
1146
1147 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1148 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001149 ret = ffa_error(FFA_NO_MEMORY);
1150 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001151 }
1152
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001153 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1154 if (vm_locked.vm->el0_partition) {
1155 mode |= MM_MODE_USER | MM_MODE_NG;
1156 }
1157
1158 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001159 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001160 /* TODO: partial defrag of failed range. */
1161 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001162 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001163 goto fail_undo_send;
1164 }
1165
Olivier Deprez96a2a262020-06-11 17:21:38 +02001166 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1167 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1168
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001169 /*
1170 * For EL0 partitions, since both the partition and the hypervisor code
1171 * use the EL2&0 translation regime, it is critical to mark the mappings
1172 * of the send and recv buffers as non-global in the TLB. For one, if we
1173 * dont mark it as non-global, it would cause TLB conflicts since there
1174 * would be an identity mapping with non-global attribute in the
1175 * partitions page tables, but another identity mapping in the
1176 * hypervisor page tables with the global attribute. The other issue is
1177 * one of security, we dont want other partitions to be able to access
1178 * other partitions buffers through cached translations.
1179 */
1180 if (vm_locked.vm->el0_partition) {
1181 extra_attributes |= MM_MODE_NG;
1182 }
1183
Manish Pandeyd34f8892020-06-19 17:41:07 +01001184 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1185 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001186 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001187 goto fail_undo_send_and_recv;
1188 }
1189
Manish Pandeyd34f8892020-06-19 17:41:07 +01001190 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001191 goto out;
1192
Andrew Sculle1322792019-07-01 17:46:10 +01001193fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001194 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001195 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001196
1197fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001198 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001199 orig_send_mode, local_page_pool, NULL));
1200 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001201
1202out:
Andrew Sculle1322792019-07-01 17:46:10 +01001203 return ret;
1204}
1205
1206/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001207 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001208 * must not be shared. Locking of the page tables combined with a local memory
1209 * pool ensures there will always be enough memory to recover from any errors
1210 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1211 * by another core which could result in this transaction being unable to roll
1212 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001213 *
1214 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001215 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001216 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001217 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001218 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001219 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001220 * - FFA_SUCCESS on success if no further action is needed.
1221 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001222 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001223 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001224struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1225 uint32_t page_count, struct vcpu *current,
1226 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001227{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001228 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001229 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001230 struct vm_locked vm_locked;
1231 struct mm_stage1_locked mm_stage1_locked;
1232 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001233
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001234 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001235 * Create a local pool so any freed memory can't be used by another
1236 * thread. This is to ensure the original mapping can be restored if any
1237 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001238 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001239 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1240
Andrew Sculle1322792019-07-01 17:46:10 +01001241 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001242 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001243
Manish Pandeyd34f8892020-06-19 17:41:07 +01001244 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1245 page_count, &local_page_pool);
1246 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001247 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001248 }
1249
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001250 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001251 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001252
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001253exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001254 mpool_fini(&local_page_pool);
1255
1256 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001257 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001258
1259 return ret;
1260}
1261
1262/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001263 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1264 * translation regime of the caller. Unmap the region for the hypervisor and
1265 * set the memory region to owned and exclusive for the component. Since the
1266 * memory region mapped in the page table, when the buffers were originally
1267 * created we can safely remap it.
1268 *
1269 * Returns:
1270 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1271 * behalf of the caller.
1272 * - FFA_SUCCESS on success if no further action is needed.
1273 */
1274struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1275 struct vcpu *current)
1276{
1277 struct vm *vm = current->vm;
1278 struct vm_locked vm_locked;
1279 struct mm_stage1_locked mm_stage1_locked;
1280 paddr_t send_pa_begin;
1281 paddr_t send_pa_end;
1282 paddr_t recv_pa_begin;
1283 paddr_t recv_pa_end;
1284
1285 /*
1286 * Check there is a buffer pair registered on behalf of the caller.
1287 * Since forwarding is not yet supported the allocator ID MBZ.
1288 */
1289 if (allocator_id != 0) {
1290 dlog_error(
1291 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1292 "supported so vm id must be zero.\n");
1293 return ffa_error(FFA_INVALID_PARAMETERS);
1294 }
1295
1296 /* Get send and receive buffers. */
1297 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001298 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001299 "No buffer pair registered on behalf of the caller.\n");
1300 return ffa_error(FFA_INVALID_PARAMETERS);
1301 }
1302
1303 /* Currently a mailbox size of 1 page is assumed. */
1304 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1305 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1306 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1307 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1308
1309 vm_locked = vm_lock(vm);
1310 mm_stage1_locked = mm_lock_stage1();
1311
1312 /*
1313 * Set the memory region of the buffers back to the default mode
1314 * for the VM. Since this memory region was already mapped for the
1315 * RXTX buffers we can safely remap them.
1316 */
1317 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1318 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1319 NULL));
1320
1321 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1322 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1323 NULL));
1324
1325 /* Unmap the buffers in the partition manager. */
1326 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1327 &api_page_pool));
1328 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1329 &api_page_pool));
1330
1331 vm->mailbox.send = NULL;
1332 vm->mailbox.recv = NULL;
1333
1334 mm_unlock_stage1(&mm_stage1_locked);
1335 vm_unlock(&vm_locked);
1336
1337 return (struct ffa_value){.func = FFA_SUCCESS_32};
1338}
1339
1340/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001341 * Notifies the `to` VM about the message currently in its mailbox, possibly
1342 * with the help of the primary VM.
1343 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001344static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1345 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001346{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001347 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1348 struct ffa_value primary_ret = {
1349 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001350 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001351 };
1352
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001353 /* Messages for the primary VM are delivered directly. */
1354 if (to.vm->id == HF_PRIMARY_VM_ID) {
1355 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001356 * Only tell the primary VM the size and other details if the
1357 * message is for it, to avoid leaking data about messages for
1358 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001359 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001360 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001361
1362 to.vm->mailbox.state = MAILBOX_STATE_READ;
1363 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001364 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001365 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001366 }
1367
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001368 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1369
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001370 /* Messages for the TEE are sent on via the dispatcher. */
1371 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001372 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001373
Olivier Deprez112d2b52020-09-30 07:39:23 +02001374 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001375 /*
1376 * After the call to the TEE completes it must have finished
1377 * reading its RX buffer, so it is ready for another message.
1378 */
1379 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001380 /*
1381 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001383 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001384 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001385 }
1386
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001387 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001388 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001389 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001390 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001391 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001392
1393 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001394}
1395
1396/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001397 * Copies data from the sender's send buffer to the recipient's receive buffer
1398 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001399 *
1400 * If the recipient's receive buffer is busy, it can optionally register the
1401 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001402 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1404 ffa_vm_id_t receiver_vm_id, uint32_t size,
1405 uint32_t attributes, struct vcpu *current,
1406 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001407{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001408 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001409 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001410 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001411 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001412 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001413 struct vcpu_locked current_locked;
1414 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415 bool notify =
1416 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001417
Andrew Walbran70bc8622019-10-07 14:15:58 +01001418 /* Ensure sender VM ID corresponds to the current VM. */
1419 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001420 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001421 }
1422
1423 /* Disallow reflexive requests as this suggests an error in the VM. */
1424 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001426 }
1427
1428 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 if (size > FFA_MSG_PAYLOAD_MAX) {
1430 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001431 }
1432
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001433 /*
1434 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1435 * invocation.
1436 */
1437 current_locked = vcpu_lock(current);
1438 is_direct_request_ongoing =
1439 is_ffa_direct_msg_request_ongoing(current_locked);
1440 vcpu_unlock(&current_locked);
1441
1442 if (is_direct_request_ongoing) {
1443 return ffa_error(FFA_DENIED);
1444 }
1445
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001446 /* Ensure the receiver VM exists. */
1447 to = vm_find(receiver_vm_id);
1448 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001450 }
1451
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001452 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001453 * Check that the sender has configured its send buffer. If the tx
1454 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1455 * be safely accessed after releasing the lock since the tx mailbox
1456 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001457 */
1458 sl_lock(&from->lock);
1459 from_msg = from->mailbox.send;
1460 sl_unlock(&from->lock);
1461
1462 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001464 }
1465
Andrew Walbran82d6d152019-12-24 15:02:06 +00001466 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001467
Andrew Walbran82d6d152019-12-24 15:02:06 +00001468 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001470 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001471 }
1472
Andrew Walbran82d6d152019-12-24 15:02:06 +00001473 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001474 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001475 to->mailbox.recv_size = size;
1476 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001477 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001478 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001479
1480out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001481 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001482
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001483 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001484}
1485
1486/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001487 * Checks whether the vCPU's attempt to block for a message has already been
1488 * interrupted or whether it is allowed to block.
1489 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001490bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001491{
Manish Pandey35e452f2021-02-18 21:36:34 +00001492 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001493 bool interrupted;
1494
Manish Pandey35e452f2021-02-18 21:36:34 +00001495 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001496
1497 /*
1498 * Don't block if there are enabled and pending interrupts, to match
1499 * behaviour of wait_for_interrupt.
1500 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001501 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001502
Manish Pandey35e452f2021-02-18 21:36:34 +00001503 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001504
1505 return interrupted;
1506}
1507
1508/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001509 * Receives a message from the mailbox. If one isn't available, this function
1510 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001511 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001512 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001513 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001514struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1515 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001516{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001517 bool is_direct_request_ongoing;
1518 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001519 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001520 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001521 bool is_from_secure_world =
1522 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001523
Andrew Scullaa039b32018-10-04 15:02:26 +01001524 /*
1525 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001526 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001527 */
J-Alvesb37fd082020-10-22 12:29:21 +01001528 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001530 }
1531
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001532 /*
1533 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1534 * invocation.
1535 */
1536 current_locked = vcpu_lock(current);
1537 is_direct_request_ongoing =
1538 is_ffa_direct_msg_request_ongoing(current_locked);
1539 vcpu_unlock(&current_locked);
1540
1541 if (is_direct_request_ongoing) {
1542 return ffa_error(FFA_DENIED);
1543 }
1544
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001545 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001546
Andrew Scullaa039b32018-10-04 15:02:26 +01001547 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001548 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1549 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001551 goto out;
1552 }
1553
1554 /* No pending message so fail if not allowed to block. */
1555 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001556 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001557 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001558 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001559
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001560 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001561 * From this point onward this call can only be interrupted or a message
1562 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001563 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001564 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001565 return_code = ffa_error(FFA_INTERRUPTED);
1566 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001567 goto out;
1568 }
1569
J-Alvesb37fd082020-10-22 12:29:21 +01001570 if (is_from_secure_world) {
1571 /* Return to other world if caller is a SP. */
1572 *next = api_switch_to_other_world(
1573 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001574 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001575 } else {
1576 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001577 struct ffa_value run_return = {
1578 .func = FFA_MSG_WAIT_32,
1579 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001580 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001581
Andrew Walbranb4816552018-12-05 17:35:42 +00001582 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001583 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001584 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001585out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001586 sl_unlock(&vm->lock);
1587
Jose Marinho3e2442f2019-03-12 13:30:37 +00001588 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001589}
1590
1591/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001592 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1593 * by this function, the caller must have called api_mailbox_send before with
1594 * the notify argument set to true, and this call must have failed because the
1595 * mailbox was not available.
1596 *
1597 * It should be called repeatedly to retrieve a list of VMs.
1598 *
1599 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1600 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001601 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001602int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001603{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001604 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001605 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001606 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001607
1608 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001609 if (list_empty(&vm->mailbox.ready_list)) {
1610 ret = -1;
1611 goto exit;
1612 }
1613
1614 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1615 ready_links);
1616 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001617 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001618
1619exit:
1620 sl_unlock(&vm->lock);
1621 return ret;
1622}
1623
1624/**
1625 * Retrieves the next VM waiting to be notified that the mailbox of the
1626 * specified VM became writable. Only primary VMs are allowed to call this.
1627 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001628 * Returns -1 on failure or if there are no waiters; the VM id of the next
1629 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001630 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001631int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001632{
1633 struct vm *vm;
1634 struct vm_locked locked;
1635 struct wait_entry *entry;
1636 struct vm *waiting_vm;
1637
1638 /* Only primary VMs are allowed to call this function. */
1639 if (current->vm->id != HF_PRIMARY_VM_ID) {
1640 return -1;
1641 }
1642
Andrew Walbran42347a92019-05-09 13:59:03 +01001643 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001644 if (vm == NULL) {
1645 return -1;
1646 }
1647
Fuad Tabbaed294af2019-12-20 10:43:01 +00001648 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001649 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001650 entry = api_fetch_waiter(locked);
1651 vm_unlock(&locked);
1652
1653 if (entry == NULL) {
1654 return -1;
1655 }
1656
1657 /* Enqueue notification to waiting VM. */
1658 waiting_vm = entry->waiting_vm;
1659
1660 sl_lock(&waiting_vm->lock);
1661 if (list_empty(&entry->ready_links)) {
1662 list_append(&waiting_vm->mailbox.ready_list,
1663 &entry->ready_links);
1664 }
1665 sl_unlock(&waiting_vm->lock);
1666
1667 return waiting_vm->id;
1668}
1669
1670/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001671 * Releases the caller's mailbox so that a new message can be received. The
1672 * caller must have copied out all data they wish to preserve as new messages
1673 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001674 *
1675 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001676 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1677 * - FFA_SUCCESS on success if no further action is needed.
1678 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001679 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001680 * hf_mailbox_waiter_get.
1681 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001682struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001683{
1684 struct vm *vm = current->vm;
1685 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001686 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001687
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001688 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001689 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001690 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001691 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001692 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001693 break;
1694
Andrew Sculld6ee1102019-04-05 22:12:42 +01001695 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001696 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001697 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001698 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001699 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001700 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001701
1702 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001703}
Andrew Walbran318f5732018-11-20 16:23:42 +00001704
1705/**
1706 * Enables or disables a given interrupt ID for the calling vCPU.
1707 *
1708 * Returns 0 on success, or -1 if the intid is invalid.
1709 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001710int64_t api_interrupt_enable(uint32_t intid, bool enable,
1711 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001712{
Manish Pandey35e452f2021-02-18 21:36:34 +00001713 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001714 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001715 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1716 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001717
Andrew Walbran318f5732018-11-20 16:23:42 +00001718 if (intid >= HF_NUM_INTIDS) {
1719 return -1;
1720 }
1721
Manish Pandey35e452f2021-02-18 21:36:34 +00001722 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001723 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001724 /*
1725 * If it is pending and was not enabled before, increment the
1726 * count.
1727 */
1728 if (current->interrupts.interrupt_pending[intid_index] &
1729 ~current->interrupts.interrupt_enabled[intid_index] &
1730 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001731 if ((current->interrupts.interrupt_type[intid_index] &
1732 intid_mask) ==
1733 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1734 vcpu_irq_count_increment(current_locked);
1735 } else {
1736 vcpu_fiq_count_increment(current_locked);
1737 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001738 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001739 current->interrupts.interrupt_enabled[intid_index] |=
1740 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001741
1742 if (type == INTERRUPT_TYPE_IRQ) {
1743 current->interrupts.interrupt_type[intid_index] &=
1744 ~intid_mask;
1745 } else if (type == INTERRUPT_TYPE_FIQ) {
1746 current->interrupts.interrupt_type[intid_index] |=
1747 intid_mask;
1748 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001749 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001750 /*
1751 * If it is pending and was enabled before, decrement the count.
1752 */
1753 if (current->interrupts.interrupt_pending[intid_index] &
1754 current->interrupts.interrupt_enabled[intid_index] &
1755 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001756 if ((current->interrupts.interrupt_type[intid_index] &
1757 intid_mask) ==
1758 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1759 vcpu_irq_count_decrement(current_locked);
1760 } else {
1761 vcpu_fiq_count_decrement(current_locked);
1762 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001763 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001764 current->interrupts.interrupt_enabled[intid_index] &=
1765 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001766 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001767 }
1768
Manish Pandey35e452f2021-02-18 21:36:34 +00001769 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001770 return 0;
1771}
1772
1773/**
1774 * Returns the ID of the next pending interrupt for the calling vCPU, and
1775 * acknowledges it (i.e. marks it as no longer pending). Returns
1776 * HF_INVALID_INTID if there are no pending interrupts.
1777 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001778uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001779{
1780 uint8_t i;
1781 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001782 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001783
1784 /*
1785 * Find the first enabled and pending interrupt ID, return it, and
1786 * deactivate it.
1787 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001788 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001789 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1790 uint32_t enabled_and_pending =
1791 current->interrupts.interrupt_enabled[i] &
1792 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001793
Andrew Walbran318f5732018-11-20 16:23:42 +00001794 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001795 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001796 uint32_t intid_mask = 1U << bit_index;
1797
Andrew Walbran3d84a262018-12-13 14:41:19 +00001798 /*
1799 * Mark it as no longer pending and decrement the count.
1800 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001801 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1802
1803 if ((current->interrupts.interrupt_type[i] &
1804 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1805 vcpu_irq_count_decrement(current_locked);
1806 } else {
1807 vcpu_fiq_count_decrement(current_locked);
1808 }
1809
Andrew Walbran3d84a262018-12-13 14:41:19 +00001810 first_interrupt =
1811 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001812 break;
1813 }
1814 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001815
Manish Pandey35e452f2021-02-18 21:36:34 +00001816 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001817 return first_interrupt;
1818}
1819
1820/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001821 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001822 * given VM and vCPU.
1823 */
1824static inline bool is_injection_allowed(uint32_t target_vm_id,
1825 struct vcpu *current)
1826{
1827 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001828
Andrew Walbran318f5732018-11-20 16:23:42 +00001829 /*
1830 * The primary VM is allowed to inject interrupts into any VM. Secondary
1831 * VMs are only allowed to inject interrupts into their own vCPUs.
1832 */
1833 return current_vm_id == HF_PRIMARY_VM_ID ||
1834 current_vm_id == target_vm_id;
1835}
1836
1837/**
1838 * Injects a virtual interrupt of the given ID into the given target vCPU.
1839 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1840 * when the vCPU is next run, which is up to the scheduler.
1841 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001842 * Returns:
1843 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1844 * ID is invalid, or the current VM is not allowed to inject interrupts to
1845 * the target VM.
1846 * - 0 on success if no further action is needed.
1847 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1848 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001849 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001850int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1851 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001852 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001853{
Andrew Walbran318f5732018-11-20 16:23:42 +00001854 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001855 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001856
1857 if (intid >= HF_NUM_INTIDS) {
1858 return -1;
1859 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001860
Andrew Walbran318f5732018-11-20 16:23:42 +00001861 if (target_vm == NULL) {
1862 return -1;
1863 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001864
Andrew Walbran318f5732018-11-20 16:23:42 +00001865 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001866 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001867 return -1;
1868 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001869
Andrew Walbran318f5732018-11-20 16:23:42 +00001870 if (!is_injection_allowed(target_vm_id, current)) {
1871 return -1;
1872 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001873
Andrew Walbrane1310df2019-04-29 17:28:28 +01001874 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001875
Manish Pandey35e452f2021-02-18 21:36:34 +00001876 dlog_verbose(
1877 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1878 "%u\n",
1879 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1880 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001881 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001882}
Andrew Scull6386f252018-12-06 13:29:10 +00001883
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001884/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001885struct ffa_value api_ffa_version(struct vcpu *current,
1886 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001887{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001888 struct vm_locked current_vm_locked;
1889
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001890 /*
1891 * Ensure that both major and minor revision representation occupies at
1892 * most 15 bits.
1893 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001894 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001895 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001896 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001897 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001898 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001899 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001900 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001901 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001902
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001903 current_vm_locked = vm_lock(current->vm);
1904 current_vm_locked.vm->ffa_version = requested_version;
1905 vm_unlock(&current_vm_locked);
1906
Daniel Boulby6e32c612021-02-17 15:09:41 +00001907 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001908}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001909
1910int64_t api_debug_log(char c, struct vcpu *current)
1911{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001912 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001913 struct vm *vm = current->vm;
1914 struct vm_locked vm_locked = vm_lock(vm);
1915
Andrew Sculld54e1be2019-08-20 11:09:42 +01001916 if (c == '\n' || c == '\0') {
1917 flush = true;
1918 } else {
1919 vm->log_buffer[vm->log_buffer_length++] = c;
1920 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1921 }
1922
1923 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001924 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1925 vm->log_buffer_length);
1926 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001927 }
1928
1929 vm_unlock(&vm_locked);
1930
1931 return 0;
1932}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001933
1934/**
J-Alves6f72ca82021-11-01 12:34:58 +00001935 * Helper for success return of FFA_FEATURES, for when it is used to query
1936 * an interrupt ID.
1937 */
1938struct ffa_value api_ffa_feature_success(uint32_t arg2)
1939{
1940 return (struct ffa_value){
1941 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1942}
1943
1944/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001945 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001946 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001947 */
J-Alves6f72ca82021-11-01 12:34:58 +00001948struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001949{
J-Alves6f72ca82021-11-01 12:34:58 +00001950 /*
1951 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1952 * if using a feature ID.
1953 */
1954 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1955 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1956 return ffa_error(FFA_NOT_SUPPORTED);
1957 }
1958
1959 switch (feature_function_id) {
1960 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001961 case FFA_ERROR_32:
1962 case FFA_SUCCESS_32:
1963 case FFA_INTERRUPT_32:
1964 case FFA_VERSION_32:
1965 case FFA_FEATURES_32:
1966 case FFA_RX_RELEASE_32:
1967 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001968 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001969 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001970 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001971 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001972 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001973 case FFA_MEM_DONATE_32:
1974 case FFA_MEM_LEND_32:
1975 case FFA_MEM_SHARE_32:
1976 case FFA_MEM_RETRIEVE_REQ_32:
1977 case FFA_MEM_RETRIEVE_RESP_32:
1978 case FFA_MEM_RELINQUISH_32:
1979 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001980 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001981 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001982 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001983 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00001984#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00001985 /* FF-A v1.1 features. */
1986 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00001987 case FFA_NOTIFICATION_BITMAP_CREATE_32:
1988 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
1989 case FFA_NOTIFICATION_BIND_32:
1990 case FFA_NOTIFICATION_UNBIND_32:
1991 case FFA_NOTIFICATION_SET_32:
1992 case FFA_NOTIFICATION_GET_32:
1993 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07001994 case FFA_MEM_PERM_GET_32:
1995 case FFA_MEM_PERM_SET_32:
1996 case FFA_MEM_PERM_GET_64:
1997 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00001998#endif
1999 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002000
2001#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2002 /* Check support of a feature provided respective feature ID. */
2003 case FFA_FEATURE_NPI:
2004 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2005 case FFA_FEATURE_SRI:
2006 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2007#endif
2008 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002009 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002010 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002011 }
2012}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002013
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002014/**
J-Alves645eabe2021-02-22 16:08:27 +00002015 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2016 * interfaces.
2017 */
2018static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2019{
2020 return args.arg2 == 0U;
2021}
2022
2023/**
J-Alves76d99af2021-03-10 17:42:11 +00002024 * Limits size of arguments in ffa_value structure to 32-bit.
2025 */
2026static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2027{
2028 return (struct ffa_value){
2029 .func = (uint32_t)args.func,
2030 .arg1 = (uint32_t)args.arg1,
2031 .arg2 = (uint32_t)0,
2032 .arg3 = (uint32_t)args.arg3,
2033 .arg4 = (uint32_t)args.arg4,
2034 .arg5 = (uint32_t)args.arg5,
2035 .arg6 = (uint32_t)args.arg6,
2036 .arg7 = (uint32_t)args.arg7,
2037 };
2038}
2039
2040/**
2041 * Helper to copy direct message payload, depending on SMC used and expected
2042 * registers size.
2043 */
2044static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2045{
2046 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2047 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2048 return api_ffa_value_copy32(args);
2049 }
2050
2051 return (struct ffa_value){
2052 .func = args.func,
2053 .arg1 = args.arg1,
2054 .arg2 = 0,
2055 .arg3 = args.arg3,
2056 .arg4 = args.arg4,
2057 .arg5 = args.arg5,
2058 .arg6 = args.arg6,
2059 .arg7 = args.arg7,
2060 };
2061}
2062
2063/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002064 * Send an FF-A direct message request.
2065 */
2066struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2067 ffa_vm_id_t receiver_vm_id,
2068 struct ffa_value args,
2069 struct vcpu *current,
2070 struct vcpu **next)
2071{
J-Alves17228f72021-04-20 17:13:19 +01002072 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002073 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002074 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002075 struct vcpu *receiver_vcpu;
2076 struct two_vcpu_locked vcpus_locked;
2077
J-Alves645eabe2021-02-22 16:08:27 +00002078 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2079 return ffa_error(FFA_INVALID_PARAMETERS);
2080 }
2081
Olivier Deprez55a189e2021-06-09 15:45:27 +02002082 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2083 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002084 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002085 }
2086
Olivier Deprez55a189e2021-06-09 15:45:27 +02002087 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002088 return ret;
2089 }
2090
2091 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2092
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002093 receiver_vm = vm_find(receiver_vm_id);
2094 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002095 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002096 return ffa_error(FFA_INVALID_PARAMETERS);
2097 }
2098
2099 /*
J-Alves439ac972021-11-18 17:32:03 +00002100 * Check if sender supports sending direct message req, and if
2101 * receiver supports receipt of direct message requests.
2102 */
2103 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2104 return ffa_error(FFA_DENIED);
2105 }
2106
2107 /*
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002108 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
2109 * UP (migratable) or MP partitions with a number of vCPUs matching the
2110 * number of PEs in the system. It further states that MP partitions
2111 * accepting direct request messages cannot migrate.
2112 */
J-Alvesad6a0432021-04-09 16:06:21 +01002113 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002114 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002115 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002116 return ffa_error(FFA_INVALID_PARAMETERS);
2117 }
2118
J-Alves6e2abc62021-12-02 14:58:56 +00002119 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002120 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2121
2122 /*
2123 * If destination vCPU is executing or already received an
2124 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2125 * busy. There is a brief period of time where the vCPU state has
2126 * changed but regs_available is still false thus consider this case as
2127 * the vCPU not yet ready to receive a direct message request.
2128 */
2129 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2130 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2131 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002132 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002133 ret = ffa_error(FFA_BUSY);
2134 goto out;
2135 }
2136
2137 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2138 memory_order_relaxed)) {
2139 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002140 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002141 receiver_vcpu->vm->id,
2142 vcpu_index(receiver_vcpu));
2143 receiver_vcpu->state = VCPU_STATE_ABORTED;
2144 }
2145
2146 ret = ffa_error(FFA_ABORTED);
2147 goto out;
2148 }
2149
2150 switch (receiver_vcpu->state) {
2151 case VCPU_STATE_OFF:
2152 case VCPU_STATE_RUNNING:
2153 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002154 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002155 case VCPU_STATE_BLOCKED:
2156 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002157 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2158 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002159 ret = ffa_error(FFA_BUSY);
2160 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002161 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002162 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002163 * We expect target vCPU to be in WAITING state after either
2164 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002165 */
2166 break;
2167 }
2168
2169 /* Inject timer interrupt if any pending */
2170 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002171 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2172 HF_VIRTUAL_TIMER_INTID, current,
2173 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002174
2175 arch_timer_mask(&receiver_vcpu->regs);
2176 }
2177
2178 /* The receiver vCPU runs upon direct message invocation */
2179 receiver_vcpu->cpu = current->cpu;
2180 receiver_vcpu->state = VCPU_STATE_RUNNING;
2181 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002182 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002183
J-Alves76d99af2021-03-10 17:42:11 +00002184 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002185
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002186 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002187
2188 /* Switch to receiver vCPU targeted to by direct msg request */
2189 *next = receiver_vcpu;
2190
J-Alves6e2abc62021-12-02 14:58:56 +00002191 if (!receiver_locked.vm->el0_partition) {
2192 /*
2193 * If the scheduler in the system is giving CPU cycles to the
2194 * receiver, due to pending notifications, inject the NPI
2195 * interrupt. Following call assumes that '*next' has been set
2196 * to receiver_vcpu.
2197 */
2198 plat_ffa_inject_notification_pending_interrupt(
2199 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2200 ? vcpus_locked.vcpu1
2201 : vcpus_locked.vcpu2,
2202 current, receiver_locked);
2203 }
2204
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002205 /*
2206 * Since this flow will lead to a VM switch, the return value will not
2207 * be applied to current vCPU.
2208 */
2209
2210out:
2211 sl_unlock(&receiver_vcpu->lock);
2212 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002213 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002214
2215 return ret;
2216}
2217
2218/**
2219 * Send an FF-A direct message response.
2220 */
2221struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2222 ffa_vm_id_t receiver_vm_id,
2223 struct ffa_value args,
2224 struct vcpu *current,
2225 struct vcpu **next)
2226{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002227 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002228
2229 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2230 return ffa_error(FFA_INVALID_PARAMETERS);
2231 }
2232
J-Alves76d99af2021-03-10 17:42:11 +00002233 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002234
Olivier Deprez55a189e2021-06-09 15:45:27 +02002235 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2236 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002237 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002238 }
2239
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002240 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002241 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002242 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002243 * No need for REQ/RESP state management as managed exit does
2244 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002245 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002246 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2247 vcpu_unlock(&current_locked);
2248 return ffa_error(FFA_DENIED);
2249 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002250
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002251 /*
2252 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2253 * a secure interrupt to a SP that is performing managed exit.
2254 * We have taken a implementation defined choice to not allow
2255 * Managed exit while a SP is processing a secure interrupt.
2256 */
2257 CHECK(!current->processing_secure_interrupt);
2258
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002259 plat_interrupts_set_priority_mask(0xff);
2260 current->processing_managed_exit = false;
2261 } else {
2262 /*
2263 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2264 * defined originator.
2265 */
2266 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2267 /*
2268 * Sending direct response but direct request origin
2269 * vCPU is not set.
2270 */
2271 vcpu_unlock(&current_locked);
2272 return ffa_error(FFA_DENIED);
2273 }
2274
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002275 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002276 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2277 vcpu_unlock(&current_locked);
2278 return ffa_error(FFA_DENIED);
2279 }
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002280
2281 /*
2282 * Per FF-A v1.1 Beta0 section 7.4, if a secure interrupt is
2283 * handled by an SP in RUNNING state the existing runtime model
2284 * is preserved. Hence, per section 7.3 bullet 3, SP can use
2285 * FFA_MSG_SEND_DIRECT_RESP to return a response after
2286 * interrupt completion.
2287 */
2288 if (current->processing_secure_interrupt) {
2289 /* There is no preempted vCPU to resume. */
2290 CHECK(current->preempted_vcpu == NULL);
2291
2292 /* Unmask interrupts. */
2293 plat_interrupts_set_priority_mask(0xff);
2294
2295 /*
2296 * Clear fields corresponding to secure interrupt
2297 * handling.
2298 */
2299 current->processing_secure_interrupt = false;
2300 current->secure_interrupt_deactivated = false;
2301 current->current_sec_interrupt_id = 0;
2302 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002303 }
2304
2305 /* Clear direct request origin for the caller. */
2306 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2307
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002308 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002309
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002310 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002311 *next = api_switch_to_other_world(
2312 current, to_ret,
2313 /*
2314 * Current vcpu sent a direct response. It moves to
2315 * waiting state.
2316 */
2317 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002318 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002319 *next = api_switch_to_primary(
2320 current, to_ret,
2321 /*
2322 * Current vcpu sent a direct response. It moves to
2323 * waiting state.
2324 */
2325 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002326 } else if (vm_id_is_current_world(receiver_vm_id)) {
2327 /*
2328 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002329 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002330 * made function return error before getting to this point.
2331 */
2332 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002333 /*
2334 * current vcpu sent a direct response.
2335 * It moves to waiting state.
2336 */
2337 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002338 } else {
2339 panic("Invalid direct message response invocation");
2340 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002341
2342 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2343}
2344
J-Alves84658fc2021-06-17 14:37:32 +01002345static bool api_memory_region_check_flags(
2346 struct ffa_memory_region *memory_region, uint32_t share_func)
2347{
2348 switch (share_func) {
2349 case FFA_MEM_SHARE_32:
2350 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2351 0U) {
2352 return false;
2353 }
2354 /* Intentional fall-through */
2355 case FFA_MEM_LEND_32:
2356 case FFA_MEM_DONATE_32: {
2357 /* Bits 31:2 Must Be Zero. */
2358 ffa_memory_receiver_flags_t to_mask =
2359 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2360 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2361
2362 if ((memory_region->flags & to_mask) != 0U) {
2363 return false;
2364 }
2365 break;
2366 }
2367 default:
2368 panic("Check for mem send calls only.\n");
2369 }
2370
2371 /* Last check reserved values are 0 */
2372 return true;
2373}
2374
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002375struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2376 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002377 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002378{
2379 struct vm *from = current->vm;
2380 struct vm *to;
2381 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002382 struct ffa_memory_region *memory_region;
2383 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002384
2385 if (ipa_addr(address) != 0 || page_count != 0) {
2386 /*
2387 * Hafnium only supports passing the descriptor in the TX
2388 * mailbox.
2389 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002390 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002391 }
2392
Andrew Walbranca808b12020-05-15 17:22:28 +01002393 if (fragment_length > length) {
2394 dlog_verbose(
2395 "Fragment length %d greater than total length %d.\n",
2396 fragment_length, length);
2397 return ffa_error(FFA_INVALID_PARAMETERS);
2398 }
2399 if (fragment_length < sizeof(struct ffa_memory_region) +
2400 sizeof(struct ffa_memory_access)) {
2401 dlog_verbose(
2402 "Initial fragment length %d smaller than header size "
2403 "%d.\n",
2404 fragment_length,
2405 sizeof(struct ffa_memory_region) +
2406 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002407 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002408 }
2409
2410 /*
2411 * Check that the sender has configured its send buffer. If the TX
2412 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2413 * be safely accessed after releasing the lock since the TX mailbox
2414 * address can only be configured once.
2415 */
2416 sl_lock(&from->lock);
2417 from_msg = from->mailbox.send;
2418 sl_unlock(&from->lock);
2419
2420 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002421 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002422 }
2423
2424 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002425 * Copy the memory region descriptor to a fresh page from the memory
2426 * pool. This prevents the sender from changing it underneath us, and
2427 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002428 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002429 if (fragment_length > HF_MAILBOX_SIZE ||
2430 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002431 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002432 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002433 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002434 if (memory_region == NULL) {
2435 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002436 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002437 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002438 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002439
2440 /* The sender must match the caller. */
2441 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002442 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002443 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002444 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002445 }
2446
J-Alves84658fc2021-06-17 14:37:32 +01002447 if (!api_memory_region_check_flags(memory_region, share_func)) {
2448 dlog_verbose(
2449 "Memory region reserved arguments must be zero.\n");
2450 ret = ffa_error(FFA_INVALID_PARAMETERS);
2451 goto out;
2452 }
2453
Andrew Walbrana65a1322020-04-06 19:32:32 +01002454 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002455 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002456 dlog_verbose(
2457 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002458 "endpoint memory access descriptors, expected 1).\n",
2459 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002460 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002461 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002462 }
2463
2464 /*
2465 * Ensure that the receiver VM exists and isn't the same as the sender.
2466 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002467 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002468 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002469 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002470 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471 goto out;
2472 }
2473
Maksims Svecovsa3d570c2021-12-08 11:16:32 +00002474 if (!plat_ffa_is_memory_send_valid(to->id, share_func)) {
2475 ret = ffa_error(FFA_DENIED);
2476 goto out;
2477 }
2478
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002479 if (to->id == HF_TEE_VM_ID) {
2480 /*
2481 * The 'to' VM lock is only needed in the case that it is the
2482 * TEE VM.
2483 */
2484 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2485
2486 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002487 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002488 goto out_unlock;
2489 }
2490
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002491 ret = ffa_memory_tee_send(
2492 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2493 length, fragment_length, share_func, &api_page_pool);
2494 /*
2495 * ffa_tee_memory_send takes ownership of the memory_region, so
2496 * make sure we don't free it.
2497 */
2498 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002499
2500 out_unlock:
2501 vm_unlock(&vm_to_from_lock.vm1);
2502 vm_unlock(&vm_to_from_lock.vm2);
2503 } else {
2504 struct vm_locked from_locked = vm_lock(from);
2505
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002506 ret = ffa_memory_send(from_locked, memory_region, length,
2507 fragment_length, share_func,
2508 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002509 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002510 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002511 * make sure we don't free it.
2512 */
2513 memory_region = NULL;
2514
2515 vm_unlock(&from_locked);
2516 }
2517
2518out:
2519 if (memory_region != NULL) {
2520 mpool_free(&api_page_pool, memory_region);
2521 }
2522
2523 return ret;
2524}
2525
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002526struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2527 uint32_t fragment_length,
2528 ipaddr_t address, uint32_t page_count,
2529 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530{
2531 struct vm *to = current->vm;
2532 struct vm_locked to_locked;
2533 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002534 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002536 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002537
2538 if (ipa_addr(address) != 0 || page_count != 0) {
2539 /*
2540 * Hafnium only supports passing the descriptor in the TX
2541 * mailbox.
2542 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002544 }
2545
Andrew Walbrana65a1322020-04-06 19:32:32 +01002546 if (fragment_length != length) {
2547 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002548 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002549 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002550
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002552 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553 message_buffer_size = cpu_get_buffer_size(current->cpu);
2554 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2555 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002556 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557 }
2558
2559 to_locked = vm_lock(to);
2560 to_msg = to->mailbox.send;
2561
2562 if (to_msg == NULL) {
2563 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002564 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 goto out;
2566 }
2567
2568 /*
2569 * Copy the retrieve request descriptor to an internal buffer, so that
2570 * the caller can't change it underneath us.
2571 */
2572 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2573
2574 if (msg_receiver_busy(to_locked, NULL, false)) {
2575 /*
2576 * Can't retrieve memory information if the mailbox is not
2577 * available.
2578 */
2579 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002581 goto out;
2582 }
2583
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002584 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2585 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002586
2587out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002588 vm_unlock(&to_locked);
2589 return ret;
2590}
2591
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002592struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002593{
2594 struct vm *from = current->vm;
2595 struct vm_locked from_locked;
2596 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002597 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002598 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002599 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002600 uint32_t length;
2601
2602 from_locked = vm_lock(from);
2603 from_msg = from->mailbox.send;
2604
2605 if (from_msg == NULL) {
2606 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002607 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002608 goto out;
2609 }
2610
2611 /*
2612 * Calculate length from relinquish descriptor before copying. We will
2613 * check again later to make sure it hasn't changed.
2614 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002615 length = sizeof(struct ffa_mem_relinquish) +
2616 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2617 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002618 /*
2619 * Copy the relinquish descriptor to an internal buffer, so that the
2620 * caller can't change it underneath us.
2621 */
2622 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002623 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002624 message_buffer_size = cpu_get_buffer_size(current->cpu);
2625 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2626 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002627 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002628 goto out;
2629 }
2630 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2631
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002632 if (sizeof(struct ffa_mem_relinquish) +
2633 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002634 length) {
2635 dlog_verbose(
2636 "Endpoint count changed while copying to internal "
2637 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002638 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002639 goto out;
2640 }
2641
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002642 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2643 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002644
2645out:
2646 vm_unlock(&from_locked);
2647 return ret;
2648}
2649
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2651 ffa_memory_region_flags_t flags,
2652 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002653{
2654 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002655 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656
Olivier Deprez55a189e2021-06-09 15:45:27 +02002657 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002658 struct vm_locked to_locked = vm_lock(to);
2659
Andrew Walbranca808b12020-05-15 17:22:28 +01002660 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002661 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002662
Andrew Walbran290b0c92020-02-03 16:37:14 +00002663 vm_unlock(&to_locked);
2664 } else {
2665 struct vm *from = vm_find(HF_TEE_VM_ID);
2666 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2667
Andrew Walbranca808b12020-05-15 17:22:28 +01002668 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2669 vm_to_from_lock.vm2, handle, flags,
2670 &api_page_pool);
2671
2672 vm_unlock(&vm_to_from_lock.vm1);
2673 vm_unlock(&vm_to_from_lock.vm2);
2674 }
2675
2676 return ret;
2677}
2678
2679struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2680 uint32_t fragment_offset,
2681 ffa_vm_id_t sender_vm_id,
2682 struct vcpu *current)
2683{
2684 struct vm *to = current->vm;
2685 struct vm_locked to_locked;
2686 struct ffa_value ret;
2687
2688 /* Sender ID MBZ at virtual instance. */
2689 if (sender_vm_id != 0) {
2690 return ffa_error(FFA_INVALID_PARAMETERS);
2691 }
2692
2693 to_locked = vm_lock(to);
2694
2695 if (msg_receiver_busy(to_locked, NULL, false)) {
2696 /*
2697 * Can't retrieve memory information if the mailbox is not
2698 * available.
2699 */
2700 dlog_verbose("RX buffer not ready.\n");
2701 ret = ffa_error(FFA_BUSY);
2702 goto out;
2703 }
2704
2705 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2706 &api_page_pool);
2707
2708out:
2709 vm_unlock(&to_locked);
2710 return ret;
2711}
2712
2713struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2714 uint32_t fragment_length,
2715 ffa_vm_id_t sender_vm_id,
2716 struct vcpu *current)
2717{
2718 struct vm *from = current->vm;
2719 const void *from_msg;
2720 void *fragment_copy;
2721 struct ffa_value ret;
2722
2723 /* Sender ID MBZ at virtual instance. */
2724 if (sender_vm_id != 0) {
2725 return ffa_error(FFA_INVALID_PARAMETERS);
2726 }
2727
2728 /*
2729 * Check that the sender has configured its send buffer. If the TX
2730 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2731 * be safely accessed after releasing the lock since the TX mailbox
2732 * address can only be configured once.
2733 */
2734 sl_lock(&from->lock);
2735 from_msg = from->mailbox.send;
2736 sl_unlock(&from->lock);
2737
2738 if (from_msg == NULL) {
2739 return ffa_error(FFA_INVALID_PARAMETERS);
2740 }
2741
2742 /*
2743 * Copy the fragment to a fresh page from the memory pool. This prevents
2744 * the sender from changing it underneath us, and also lets us keep it
2745 * around in the share state table if needed.
2746 */
2747 if (fragment_length > HF_MAILBOX_SIZE ||
2748 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2749 dlog_verbose(
2750 "Fragment length %d larger than mailbox size %d.\n",
2751 fragment_length, HF_MAILBOX_SIZE);
2752 return ffa_error(FFA_INVALID_PARAMETERS);
2753 }
2754 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2755 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2756 0) {
2757 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2758 return ffa_error(FFA_INVALID_PARAMETERS);
2759 }
2760 fragment_copy = mpool_alloc(&api_page_pool);
2761 if (fragment_copy == NULL) {
2762 dlog_verbose("Failed to allocate fragment copy.\n");
2763 return ffa_error(FFA_NO_MEMORY);
2764 }
2765 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2766
2767 /*
2768 * Hafnium doesn't support fragmentation of memory retrieve requests
2769 * (because it doesn't support caller-specified mappings, so a request
2770 * will never be larger than a single page), so this must be part of a
2771 * memory send (i.e. donate, lend or share) request.
2772 *
2773 * We can tell from the handle whether the memory transaction is for the
2774 * TEE or not.
2775 */
2776 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2777 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2778 struct vm_locked from_locked = vm_lock(from);
2779
2780 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2781 fragment_length, handle,
2782 &api_page_pool);
2783 /*
2784 * `ffa_memory_send_continue` takes ownership of the
2785 * fragment_copy, so we don't need to free it here.
2786 */
2787 vm_unlock(&from_locked);
2788 } else {
2789 struct vm *to = vm_find(HF_TEE_VM_ID);
2790 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2791
2792 /*
2793 * The TEE RX buffer state is checked in
2794 * `ffa_memory_tee_send_continue` rather than here, as we need
2795 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2796 * than FFA_ERROR FFA_BUSY in case it is busy.
2797 */
2798
2799 ret = ffa_memory_tee_send_continue(
2800 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2801 fragment_length, handle, &api_page_pool);
2802 /*
2803 * `ffa_memory_tee_send_continue` takes ownership of the
2804 * fragment_copy, so we don't need to free it here.
2805 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002806
2807 vm_unlock(&vm_to_from_lock.vm1);
2808 vm_unlock(&vm_to_from_lock.vm2);
2809 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002810
2811 return ret;
2812}
Max Shvetsov40108e72020-08-27 12:39:50 +01002813
2814struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2815 struct vcpu *current)
2816{
2817 struct vm_locked vm_locked;
2818
2819 vm_locked = vm_lock(current->vm);
2820 vm_locked.vm->secondary_ep = entry_point;
2821 vm_unlock(&vm_locked);
2822
2823 return (struct ffa_value){.func = FFA_SUCCESS_32};
2824}
J-Alvesa0f317d2021-06-09 13:31:59 +01002825
2826struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2827 ffa_vcpu_count_t vcpu_count,
2828 struct vcpu *current)
2829{
2830 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2831 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2832 vm_id);
2833 return ffa_error(FFA_NOT_SUPPORTED);
2834 }
2835
2836 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2837}
2838
2839struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2840 struct vcpu *current)
2841{
2842 /*
2843 * Validity of use of this interface is the same as for bitmap create.
2844 */
2845 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2846 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2847 vm_id);
2848 return ffa_error(FFA_NOT_SUPPORTED);
2849 }
2850
2851 return plat_ffa_notifications_bitmap_destroy(vm_id);
2852}
J-Alvesc003a7a2021-03-18 13:06:53 +00002853
2854struct ffa_value api_ffa_notification_update_bindings(
2855 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2856 ffa_notifications_bitmap_t notifications, bool is_bind,
2857 struct vcpu *current)
2858{
2859 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2860 struct vm_locked receiver_locked;
2861 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2862 const ffa_vm_id_t id_to_update =
2863 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2864 const ffa_vm_id_t id_to_validate =
2865 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2866
2867 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2868 receiver_vm_id)) {
2869 dlog_verbose("Invalid use of notifications bind interface.\n");
2870 return ffa_error(FFA_INVALID_PARAMETERS);
2871 }
2872
J-Alvesb15e9402021-09-08 11:44:42 +01002873 if (plat_ffa_notifications_update_bindings_forward(
2874 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2875 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01002876 return ret;
2877 }
2878
J-Alvesc003a7a2021-03-18 13:06:53 +00002879 if (notifications == 0U) {
2880 dlog_verbose("No notifications have been specified.\n");
2881 return ffa_error(FFA_INVALID_PARAMETERS);
2882 }
2883
2884 /**
2885 * This check assumes receiver is the current VM, and has been enforced
2886 * by 'plat_ffa_is_notifications_bind_valid'.
2887 */
2888 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2889
2890 if (receiver_locked.vm == NULL) {
2891 dlog_verbose("Receiver doesn't exist!\n");
2892 return ffa_error(FFA_DENIED);
2893 }
2894
J-Alves09ff9d82021-11-02 11:55:20 +00002895 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002896 dlog_verbose("Notifications are not enabled.\n");
2897 ret = ffa_error(FFA_NOT_SUPPORTED);
2898 goto out;
2899 }
2900
2901 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2902 vm_find(sender_vm_id) == NULL) {
2903 dlog_verbose("Sender VM does not exist!\n");
2904 ret = ffa_error(FFA_INVALID_PARAMETERS);
2905 goto out;
2906 }
2907
2908 /*
2909 * Can't bind/unbind notifications if at least one is bound to a
2910 * different sender.
2911 */
2912 if (!vm_notifications_validate_bound_sender(
2913 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2914 id_to_validate, notifications)) {
2915 dlog_verbose("Notifications are bound to other sender.\n");
2916 ret = ffa_error(FFA_DENIED);
2917 goto out;
2918 }
2919
2920 /**
2921 * Check if there is a pending notification within those specified in
2922 * the bitmap.
2923 */
2924 if (vm_are_notifications_pending(receiver_locked,
2925 plat_ffa_is_vm_id(sender_vm_id),
2926 notifications)) {
2927 dlog_verbose("Notifications within '%x' pending.\n",
2928 notifications);
2929 ret = ffa_error(FFA_DENIED);
2930 goto out;
2931 }
2932
2933 vm_notifications_update_bindings(
2934 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2935 notifications, is_per_vcpu && is_bind);
2936
2937out:
2938 vm_unlock(&receiver_locked);
2939 return ret;
2940}
J-Alvesaa79c012021-07-09 14:29:45 +01002941
2942struct ffa_value api_ffa_notification_set(
2943 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2944 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2945{
2946 struct ffa_value ret;
2947 struct vm_locked receiver_locked;
2948
2949 /*
2950 * Check if is per-vCPU or global, and extracting vCPU ID according
2951 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2952 */
2953 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2954 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2955
J-Alvesaa79c012021-07-09 14:29:45 +01002956 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2957 receiver_vm_id)) {
2958 dlog_verbose("Invalid use of notifications set interface.\n");
2959 return ffa_error(FFA_INVALID_PARAMETERS);
2960 }
2961
2962 if (notifications == 0U) {
2963 dlog_verbose("No notifications have been specified.\n");
2964 return ffa_error(FFA_INVALID_PARAMETERS);
2965 }
2966
J-Alvesde7bd2f2021-09-09 19:54:35 +01002967 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
2968 flags, notifications, &ret)) {
2969 return ret;
2970 }
2971
J-Alvesaa79c012021-07-09 14:29:45 +01002972 /*
2973 * This check assumes receiver is the current VM, and has been enforced
2974 * by 'plat_ffa_is_notification_set_valid'.
2975 */
2976 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2977
2978 if (receiver_locked.vm == NULL) {
2979 dlog_verbose("Receiver ID is not valid.\n");
2980 return ffa_error(FFA_INVALID_PARAMETERS);
2981 }
2982
J-Alves09ff9d82021-11-02 11:55:20 +00002983 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01002984 dlog_verbose("Receiver's notifications not enabled.\n");
2985 ret = ffa_error(FFA_DENIED);
2986 goto out;
2987 }
2988
2989 /*
2990 * If notifications are not bound to the sender, they wouldn't be
2991 * enabled either for the receiver.
2992 */
2993 if (!vm_notifications_validate_binding(
2994 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2995 sender_vm_id, notifications, is_per_vcpu)) {
2996 dlog_verbose("Notifications bindings not valid.\n");
2997 ret = ffa_error(FFA_DENIED);
2998 goto out;
2999 }
3000
3001 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3002 dlog_verbose("Invalid VCPU ID!\n");
3003 ret = ffa_error(FFA_INVALID_PARAMETERS);
3004 goto out;
3005 }
3006
J-Alves7461ef22021-10-18 17:21:33 +01003007 /* Set notifications pending. */
J-Alvesaa79c012021-07-09 14:29:45 +01003008 vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3009 notifications, vcpu_id, is_per_vcpu);
3010 dlog_verbose("Set the notifications: %x.\n", notifications);
3011
J-Alves13394022021-06-30 13:48:49 +01003012 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3013 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3014 vcpu_index(current));
3015 plat_ffa_sri_trigger_not_delayed(current->cpu);
3016 } else {
3017 plat_ffa_sri_state_set(DELAYED);
3018 }
J-Alvesaa79c012021-07-09 14:29:45 +01003019
J-Alves13394022021-06-30 13:48:49 +01003020 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003021out:
3022 vm_unlock(&receiver_locked);
3023
3024 return ret;
3025}
3026
3027static struct ffa_value api_ffa_notification_get_success_return(
3028 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3029 ffa_notifications_bitmap_t from_framework)
3030{
3031 return (struct ffa_value){
3032 .func = FFA_SUCCESS_32,
3033 .arg1 = 0U,
3034 .arg2 = (uint32_t)from_sp,
3035 .arg3 = (uint32_t)(from_sp >> 32),
3036 .arg4 = (uint32_t)from_vm,
3037 .arg5 = (uint32_t)(from_vm >> 32),
3038 .arg6 = (uint32_t)from_framework,
3039 .arg7 = (uint32_t)(from_framework >> 32),
3040 };
3041}
3042
3043struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3044 ffa_vcpu_index_t vcpu_id,
3045 uint32_t flags, struct vcpu *current)
3046{
3047 /* TODO: get framework notifications, when these are supported. */
3048 ffa_notifications_bitmap_t sp_notifications = 0;
3049 ffa_notifications_bitmap_t vm_notifications = 0;
3050 struct vm_locked receiver_locked;
3051 struct ffa_value ret;
3052
3053 /*
3054 * Following check should capture wrong uses of the interface, depending
3055 * on whether Hafnium is SPMC or hypervisor.
3056 * On the rest of the function it is assumed this condition is met.
3057 */
3058 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) {
3059 dlog_verbose("Invalid use of notifications get interface.\n");
3060 return ffa_error(FFA_INVALID_PARAMETERS);
3061 }
3062
3063 /*
3064 * This check assumes receiver is the current VM, and has been enforced
3065 * by `plat_ffa_is_notifications_get_valid`.
3066 */
3067 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3068
3069 /*
3070 * `plat_ffa_is_notifications_get_valid` ensures following is never
3071 * true.
3072 */
3073 CHECK(receiver_locked.vm != NULL);
3074
3075 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3076 (receiver_locked.vm->vcpu_count != 1 &&
3077 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003078 dlog_verbose(
3079 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3080 vcpu_id, receiver_locked.vm->vcpu_count,
3081 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003082 ret = ffa_error(FFA_INVALID_PARAMETERS);
3083 goto out;
3084 }
3085
3086 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003087 if (!plat_ffa_notifications_get_from_sp(
3088 receiver_locked, vcpu_id, &sp_notifications,
3089 &ret)) {
3090 dlog_verbose("Failed to get notifications from sps.");
3091 goto out;
3092 }
J-Alvesaa79c012021-07-09 14:29:45 +01003093 }
3094
3095 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
3096 vm_notifications = vm_notifications_get_pending_and_clear(
3097 receiver_locked, true, vcpu_id);
3098 }
3099
3100 ret = api_ffa_notification_get_success_return(sp_notifications,
3101 vm_notifications, 0);
3102
J-Alvesfe23ebe2021-10-13 16:07:07 +01003103 /*
3104 * If there are no more pending notifications, change `sri_state` to
3105 * handled.
3106 */
3107 if (vm_is_notifications_pending_count_zero()) {
3108 plat_ffa_sri_state_set(HANDLED);
3109 }
3110
J-Alves6e2abc62021-12-02 14:58:56 +00003111 if (!receiver_locked.vm->el0_partition &&
3112 !vm_are_global_notifications_pending(receiver_locked)) {
3113 vm_notifications_set_npi_injected(receiver_locked, false);
3114 }
3115
J-Alvesaa79c012021-07-09 14:29:45 +01003116out:
3117 vm_unlock(&receiver_locked);
3118
3119 return ret;
3120}
J-Alvesc8e8a222021-06-08 17:33:52 +01003121
3122/**
3123 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3124 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3125 */
3126static struct ffa_value api_ffa_notification_info_get_success_return(
3127 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003128 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003129{
3130 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3131
3132 /*
3133 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3134 * hold the list of ids.
3135 */
3136 memcpy_s(&ret.arg3,
3137 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3138 sizeof(ids[0]) * ids_count);
3139
3140 /*
3141 * According to the spec x2 should have:
3142 * - Bit flagging if there are more notifications pending;
3143 * - The total number of elements (i.e. total list size);
3144 * - The number of VCPU IDs within each VM specific list.
3145 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003146 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3147 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3148 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003149
3150 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3151 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3152
3153 for (unsigned int i = 0; i < lists_count; i++) {
3154 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3155 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3156 }
3157
3158 return ret;
3159}
3160
3161struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3162{
3163 /*
3164 * Following set of variables should be populated with the return info.
3165 * At a successfull handling of this interface, they should be used
3166 * to populate the 'ret' structure in accordance to the table 17.29
3167 * of the FF-A v1.1 Beta0 specification.
3168 */
3169 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3170 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3171 uint32_t lists_count = 0;
3172 uint32_t ids_count = 0;
3173 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003174 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003175
3176 /*
3177 * This interface can only be called at NS virtual/physical FF-A
3178 * instance by the endpoint implementing the primary scheduler and the
3179 * Hypervisor/OS kernel.
3180 * In the SPM, following check passes if call has been forwarded from
3181 * the hypervisor.
3182 */
3183 if (current->vm->id != HF_PRIMARY_VM_ID) {
3184 dlog_verbose(
3185 "Only the receiver's scheduler can use this "
3186 "interface\n");
3187 return ffa_error(FFA_NOT_SUPPORTED);
3188 }
3189
J-Alvesca058c22021-09-10 14:02:07 +01003190 /*
3191 * Forward call to the other world, and fill the arrays used to assemble
3192 * return.
3193 */
3194 plat_ffa_notification_info_get_forward(
3195 ids, &ids_count, lists_sizes, &lists_count,
3196 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3197
3198 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3199
J-Alvesc8e8a222021-06-08 17:33:52 +01003200 /* Get notifications' info from this world */
3201 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3202 ++index) {
3203 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3204
3205 list_is_full = vm_notifications_info_get(
3206 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3207 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3208
3209 vm_unlock(&vm_locked);
3210 }
3211
3212 if (!list_is_full) {
3213 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003214 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003215 ids, &ids_count, lists_sizes, &lists_count,
3216 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3217 }
3218
3219 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003220 dlog_verbose(
3221 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003222 result = ffa_error(FFA_NO_DATA);
3223 } else {
3224 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003225 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003226 }
3227
J-Alvesfe23ebe2021-10-13 16:07:07 +01003228 plat_ffa_sri_state_set(HANDLED);
3229
J-Alves13394022021-06-30 13:48:49 +01003230 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003231}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003232
3233struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3234{
3235 struct vm_locked vm_locked;
3236 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3237 bool mode_ret = false;
3238 uint32_t mode = 0;
3239
3240 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3241 return ffa_error(FFA_NOT_SUPPORTED);
3242 }
3243
3244 if (!(current->vm->el0_partition)) {
3245 return ffa_error(FFA_DENIED);
3246 }
3247
3248 vm_locked = vm_lock(current->vm);
3249
3250 /*
3251 * mm_get_mode is used to check if the given base_addr page is already
3252 * mapped. If the page is unmapped, return error. If the page is mapped
3253 * appropriate attributes are returned to the caller. Note that
3254 * mm_get_mode returns true if the address is in the valid VA range as
3255 * supported by the architecture and MMU configurations, as opposed to
3256 * whether a page is mapped or not. For a page to be known as mapped,
3257 * the API must return true AND the returned mode must not have
3258 * MM_MODE_INVALID set.
3259 */
3260 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3261 va_add(base_addr, PAGE_SIZE), &mode);
3262 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3263 ret = ffa_error(FFA_INVALID_PARAMETERS);
3264 goto out;
3265 }
3266
3267 /* No memory should be marked RWX */
3268 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3269 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3270
3271 /*
3272 * S-EL0 partitions are expected to have all their pages marked as
3273 * non-global.
3274 */
3275 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3276 (MM_MODE_NG | MM_MODE_USER));
3277
3278 if (mode & MM_MODE_W) {
3279 /* No memory should be writeable but not readable. */
3280 CHECK(mode & MM_MODE_R);
3281 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3282 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3283 } else if (mode & MM_MODE_R) {
3284 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3285 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3286 if (!(mode & MM_MODE_X)) {
3287 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3288 }
3289 }
3290out:
3291 vm_unlock(&vm_locked);
3292 return ret;
3293}
3294
3295struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3296 uint32_t mem_perm, struct vcpu *current)
3297{
3298 struct vm_locked vm_locked;
3299 struct ffa_value ret;
3300 bool mode_ret = false;
3301 uint32_t original_mode;
3302 uint32_t new_mode;
3303 struct mpool local_page_pool;
3304
3305 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3306 return ffa_error(FFA_NOT_SUPPORTED);
3307 }
3308
3309 if (!(current->vm->el0_partition)) {
3310 return ffa_error(FFA_DENIED);
3311 }
3312
3313 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3314 return ffa_error(FFA_INVALID_PARAMETERS);
3315 }
3316
3317 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3318 (mem_perm != FFA_MEM_PERM_RX)) {
3319 return ffa_error(FFA_INVALID_PARAMETERS);
3320 }
3321
3322 /*
3323 * Create a local pool so any freed memory can't be used by another
3324 * thread. This is to ensure the original mapping can be restored if any
3325 * stage of the process fails.
3326 */
3327 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3328
3329 vm_locked = vm_lock(current->vm);
3330
3331 /*
3332 * All regions accessible by the partition are mapped during boot. If we
3333 * cannot get a successful translation for the page range, the request
3334 * to change permissions is rejected.
3335 * mm_get_mode is used to check if the given address range is already
3336 * mapped. If the range is unmapped, return error. If the range is
3337 * mapped appropriate attributes are returned to the caller. Note that
3338 * mm_get_mode returns true if the address is in the valid VA range as
3339 * supported by the architecture and MMU configurations, as opposed to
3340 * whether a page is mapped or not. For a page to be known as mapped,
3341 * the API must return true AND the returned mode must not have
3342 * MM_MODE_INVALID set.
3343 */
3344
3345 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3346 va_add(base_addr, page_count * PAGE_SIZE),
3347 &original_mode);
3348 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3349 ret = ffa_error(FFA_INVALID_PARAMETERS);
3350 goto out;
3351 }
3352
3353 /* Device memory cannot be marked as executable */
3354 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3355 ret = ffa_error(FFA_INVALID_PARAMETERS);
3356 goto out;
3357 }
3358
3359 new_mode = MM_MODE_USER | MM_MODE_NG;
3360
3361 if (mem_perm == FFA_MEM_PERM_RW) {
3362 new_mode |= MM_MODE_R | MM_MODE_W;
3363 } else if (mem_perm == FFA_MEM_PERM_RX) {
3364 new_mode |= MM_MODE_R | MM_MODE_X;
3365 } else if (mem_perm == FFA_MEM_PERM_RO) {
3366 new_mode |= MM_MODE_R;
3367 }
3368
3369 /*
3370 * Safe to re-map memory, since we know the requested permissions are
3371 * valid, and the memory requested to be re-mapped is also valid.
3372 */
3373 if (!mm_identity_prepare(
3374 &vm_locked.vm->ptable, pa_from_va(base_addr),
3375 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3376 new_mode, &local_page_pool)) {
3377 /*
3378 * Defrag the table into the local page pool.
3379 * mm_identity_prepare could have allocated or freed pages to
3380 * split blocks or tables etc.
3381 */
3382 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3383
3384 /*
3385 * Guaranteed to succeed mapping with old mode since the mapping
3386 * with old mode already existed and we have a local page pool
3387 * that should have sufficient memory to go back to the original
3388 * state.
3389 */
3390 CHECK(mm_identity_prepare(
3391 &vm_locked.vm->ptable, pa_from_va(base_addr),
3392 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3393 original_mode, &local_page_pool));
3394 mm_identity_commit(
3395 &vm_locked.vm->ptable, pa_from_va(base_addr),
3396 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3397 original_mode, &local_page_pool);
3398
3399 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3400 ret = ffa_error(FFA_NO_MEMORY);
3401 goto out;
3402 }
3403
3404 mm_identity_commit(
3405 &vm_locked.vm->ptable, pa_from_va(base_addr),
3406 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3407 &local_page_pool);
3408
3409 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3410
3411out:
3412 mpool_fini(&local_page_pool);
3413 vm_unlock(&vm_locked);
3414
3415 return ret;
3416}