blob: 8ce12c0cc591dd4ae5967bab5dc696b37161e534 [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 Walbran7e824602020-10-22 16:51:40 +0100151 primary_ret = (struct ffa_value){
152 .func = FFA_INTERRUPT_32};
153
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100154 } else {
155 primary_ret.arg2 = remaining_ns;
156 }
157 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100159 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000160 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100161 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000162
163 default:
164 /* Do nothing. */
165 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000166 }
167
J-Alvesfe7f7372020-11-09 11:32:12 +0000168 return api_switch_to_vm(current, primary_ret, secondary_state,
169 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100170}
171
172/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200173 * Choose next vCPU to run to be the counterpart vCPU in the other
174 * world (run the normal world if currently running in the secure
175 * world). Set current vCPU state to the given vcpu_state parameter.
176 * Set FF-A return values to the target vCPU in the other world.
177 *
178 * Called in context of a direct message response from a secure
179 * partition to a VM.
180 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100181struct vcpu *api_switch_to_other_world(struct vcpu *current,
182 struct ffa_value other_world_ret,
183 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200184{
J-Alvesfe7f7372020-11-09 11:32:12 +0000185 return api_switch_to_vm(current, other_world_ret, vcpu_state,
186 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187}
188
189/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100190 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
191 * registers the `from` VM to be notified when it becomes available.
192 */
193static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
194{
195 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
196 to.vm->mailbox.recv == NULL) {
197 /*
198 * Fail if the receiver isn't currently ready to receive data,
199 * setting up for notification if requested.
200 */
201 if (notify) {
202 struct wait_entry *entry =
203 vm_get_wait_entry(from, to.vm->id);
204
205 /* Append waiter only if it's not there yet. */
206 if (list_empty(&entry->wait_links)) {
207 list_append(&to.vm->mailbox.waiter_list,
208 &entry->wait_links);
209 }
210 }
211
212 return true;
213 }
214
215 return false;
216}
217
218/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000219 * Returns true if the given vCPU is executing in context of an
220 * FFA_MSG_SEND_DIRECT_REQ invocation.
221 */
222static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
223{
224 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
225}
226
227/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100228 * Returns true if the VM owning the given vCPU is supporting managed exit and
229 * the vCPU is currently processing a managed exit.
230 */
231static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
232{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100233 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100234 vcpu_locked.vcpu->processing_managed_exit);
235}
236
237/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000238 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000239 */
240struct vcpu *api_preempt(struct vcpu *current)
241{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = FFA_INTERRUPT_32,
Andrew Scull33fecd32019-01-08 14:48:27 +0000244 };
245
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500246 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000247}
248
249/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000250 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000251 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100252 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100253struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100254{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100255 struct ffa_value ret = {
256 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
257 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100258 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000259
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000260 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100261 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100262}
263
264/**
Andrew Walbran33645652019-04-15 12:29:31 +0100265 * Puts the current vCPU in off mode, and returns to the primary VM.
266 */
267struct vcpu *api_vcpu_off(struct vcpu *current)
268{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100269 struct ffa_value ret = {
270 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
271 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100272 };
273
274 /*
275 * Disable the timer, so the scheduler doesn't get told to call back
276 * based on it.
277 */
278 arch_timer_disable_current();
279
280 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
281}
282
283/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500284 * The current vCPU is blocked on some resource and needs to relinquish
285 * control back to the execution context of the endpoint that originally
286 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000287 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000288struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000289{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000290 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
291 struct vcpu_locked current_locked;
292 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000293
294 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000295 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000296 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000297 }
298
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000299 current_locked = vcpu_lock(current);
300 is_direct_request_ongoing =
301 is_ffa_direct_msg_request_ongoing(current_locked);
302 vcpu_unlock(&current_locked);
303
304 if (is_direct_request_ongoing) {
305 return ffa_error(FFA_DENIED);
306 }
307
308 *next = api_switch_to_primary(
309 current,
310 (struct ffa_value){.func = FFA_YIELD_32,
311 .arg1 = ffa_vm_vcpu(current->vm->id,
312 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500313 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000314
315 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000316}
317
318/**
Andrew Walbran33645652019-04-15 12:29:31 +0100319 * Switches to the primary so that it can switch to the target, or kick it if it
320 * is already running on a different physical CPU.
321 */
322struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
323{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 struct ffa_value ret = {
Andrew Walbran7e824602020-10-22 16:51:40 +0100325 .func = FFA_INTERRUPT_32,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
327 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100328 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500329 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100330}
331
332/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000333 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000334 */
335struct vcpu *api_abort(struct vcpu *current)
336{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000338
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100339 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000340 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000341
342 if (current->vm->id == HF_PRIMARY_VM_ID) {
343 /* TODO: what to do when the primary aborts? */
344 for (;;) {
345 /* Do nothing. */
346 }
347 }
348
349 atomic_store_explicit(&current->vm->aborting, true,
350 memory_order_relaxed);
351
352 /* TODO: free resources once all vCPUs abort. */
353
Andrew Sculld6ee1102019-04-05 22:12:42 +0100354 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000355}
356
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000357/*
358 * Format the partition info descriptors according to the version supported
359 * by the endpoint and return the size of the array created.
360 */
361static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000362 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000363 uint32_t vm_count)
364{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000365 struct vm *vm = vm_locked.vm;
366 uint32_t version = vm->ffa_version;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000367 uint32_t size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000368
Daniel Boulby191b5f02022-02-17 17:26:14 +0000369 if (msg_receiver_busy(vm_locked, NULL, false)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000370 /*
371 * Can't retrieve memory information if the mailbox is not
372 * available.
373 */
374 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000375 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000376 }
377
Daniel Boulby191b5f02022-02-17 17:26:14 +0000378 if (version == MAKE_FFA_VERSION(1, 0)) {
379 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
380
381 size = sizeof(struct ffa_partition_info_v1_0) * vm_count;
382 if (size > HF_MAILBOX_SIZE) {
383 dlog_error(
384 "Partition information does not fit in the "
385 "VM's RX "
386 "buffer.\n");
387 return ffa_error(FFA_NO_MEMORY);
388 }
389
390 for (uint32_t i = 0; i < vm_count; i++) {
391 /*
392 * Populate the VM's RX buffer with the partition
393 * information.
394 */
395 recv_mailbox[i].vm_id = partitions[i].vm_id;
396 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
397 recv_mailbox[i].properties = partitions[i].properties;
398 }
399
400 } else {
401 size = sizeof(partitions[0]) * vm_count;
402 if (size > HF_MAILBOX_SIZE) {
403 dlog_error(
404 "Partition information does not fit in the "
405 "VM's RX "
406 "buffer.\n");
407 return ffa_error(FFA_NO_MEMORY);
408 }
409
410 /* Populate the VM's RX buffer with the partition information.
411 */
412 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions, size);
413 }
414
415 vm->mailbox.recv_size = size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000416
417 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000418 vm->mailbox.recv_sender = HF_VM_ID_BASE;
419 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
420 vm->mailbox.state = MAILBOX_STATE_READ;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000421
422 /* Return the count of partition information descriptors in w2. */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000423 return (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000424}
425
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100426struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000427 const struct ffa_uuid *uuid,
428 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100429{
430 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100431 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000432 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
433 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100434 bool uuid_is_null = ffa_uuid_is_null(uuid);
Olivier Depreze562e542020-06-11 17:31:54 +0200435 struct ffa_partition_info partitions[2 * MAX_VMS];
Daniel Boulby191b5f02022-02-17 17:26:14 +0000436 struct vm_locked vm_locked;
437 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100438
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000439 /* Bits 31:1 Must Be Zero */
440 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
441 return ffa_error(FFA_INVALID_PARAMETERS);
442 }
443
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100444 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000445 * No need to count if we are returning the number of paritions as we
446 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100447 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000448 if (uuid_is_null && count_flag) {
449 vm_count = vm_get_count();
450 } else {
451 /*
452 * Iterate through the VMs to find the ones with a matching
453 * UUID. A Null UUID retrieves information for all VMs.
454 */
455 for (uint16_t index = 0; index < vm_get_count(); ++index) {
456 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100457
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000458 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
459 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100460
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000461 ++vm_count;
462 if (count_flag) {
463 continue;
464 }
465
466 partitions[array_index].vm_id = vm->id;
467 partitions[array_index].vcpu_count =
468 vm->vcpu_count;
469 partitions[array_index].properties =
470 plat_ffa_partition_properties(
471 current_vm->id, vm);
472 partitions[array_index].properties |=
473 vm_are_notifications_enabled(vm)
474 ? FFA_PARTITION_NOTIFICATION
475 : 0;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000476 partitions[array_index].uuid = vm->uuid;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000477 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100478 }
479 }
480
Olivier Depreze562e542020-06-11 17:31:54 +0200481 /* If UUID is Null vm_count must not be zero at this stage. */
482 CHECK(!uuid_is_null || vm_count != 0);
483
484 /*
485 * When running the Hypervisor:
486 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
487 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000488 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200489 * a secure partition and the query is forwarded to the SPMC.
490 * When running the SPMC:
491 * - If UUID is non-Null and vm_count is zero it means there is no such
492 * partition identified in the system.
493 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000494 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200495
496 /*
497 * Unrecognized UUID: does not match any of the VMs (or SPs)
498 * and is not Null.
499 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100500 if (vm_count == 0) {
501 return ffa_error(FFA_INVALID_PARAMETERS);
502 }
503
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000504 /*
505 * If the count flag is set we don't need to return the partition info
506 * descriptors.
507 */
508 if (count_flag) {
509 return (struct ffa_value){.func = FFA_SUCCESS_32,
510 .arg2 = vm_count};
511 }
512
Daniel Boulby191b5f02022-02-17 17:26:14 +0000513 vm_locked = vm_lock(current_vm);
514 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
515 vm_count);
516 vm_unlock(&vm_locked);
517 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100518}
519
Andrew Scull9726c252019-01-23 13:44:19 +0000520/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000521 * Returns the ID of the VM.
522 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000524{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 return (struct ffa_value){.func = FFA_SUCCESS_32,
526 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000527}
528
529/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200530 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
531 * FF-A instances.
532 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000533 */
534struct ffa_value api_ffa_spm_id_get(void)
535{
J-Alves3829fc02021-03-18 12:49:18 +0000536#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
537 /*
538 * Return the SPMC ID that was fetched during FF-A
539 * initialization.
540 */
541 return (struct ffa_value){.func = FFA_SUCCESS_32,
542 .arg2 = arch_ffa_spmc_id_get()};
543#else
544 return ffa_error(FFA_NOT_SUPPORTED);
545#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000546}
547
548/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000549 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000550 * function to indicate that register state for the given vCPU has been saved
551 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000552 */
553void api_regs_state_saved(struct vcpu *vcpu)
554{
555 sl_lock(&vcpu->lock);
556 vcpu->regs_available = true;
557 sl_unlock(&vcpu->lock);
558}
559
560/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000561 * Retrieves the next waiter and removes it from the wait list if the VM's
562 * mailbox is in a writable state.
563 */
564static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
565{
566 struct wait_entry *entry;
567 struct vm *vm = locked_vm.vm;
568
Andrew Sculld6ee1102019-04-05 22:12:42 +0100569 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000570 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
571 /* The mailbox is not writable or there are no waiters. */
572 return NULL;
573 }
574
575 /* Remove waiter from the wait list. */
576 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
577 wait_links);
578 list_remove(&entry->wait_links);
579 return entry;
580}
581
582/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000583 * Assuming that the arguments have already been checked by the caller, injects
584 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
585 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
586 * is next run, which is up to the scheduler.
587 *
588 * Returns:
589 * - 0 on success if no further action is needed.
590 * - 1 if it was called by the primary VM and the primary VM now needs to wake
591 * up or kick the target vCPU.
592 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100593int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
594 uint32_t intid, struct vcpu *current,
595 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000596{
Manish Pandey35e452f2021-02-18 21:36:34 +0000597 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000598 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000599 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
600 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000601 int64_t ret = 0;
602
Andrew Walbran508e63c2018-12-20 17:02:37 +0000603 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000604 * We only need to change state and (maybe) trigger a virtual interrupt
605 * if it is enabled and was not previously pending. Otherwise we can
606 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000607 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000608 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
609 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000610 intid_mask)) {
611 goto out;
612 }
613
614 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000615 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
616 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
617 vcpu_irq_count_increment(target_locked);
618 } else {
619 vcpu_fiq_count_increment(target_locked);
620 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000621
622 /*
623 * Only need to update state if there was not already an
624 * interrupt enabled and pending.
625 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000626 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000627 goto out;
628 }
629
Andrew Walbran508e63c2018-12-20 17:02:37 +0000630 if (current->vm->id == HF_PRIMARY_VM_ID) {
631 /*
632 * If the call came from the primary VM, let it know that it
633 * should run or kick the target vCPU.
634 */
635 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000636 } else if (current != target_vcpu && next != NULL) {
637 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000638 }
639
640out:
641 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000642 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000643
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200644 return ret;
645}
646
647/* Wrapper to internal_interrupt_inject with locking of target vCPU */
648static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
649 uint32_t intid, struct vcpu *current,
650 struct vcpu **next)
651{
652 int64_t ret;
653 struct vcpu_locked target_locked;
654
655 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100656 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200657 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000658
659 return ret;
660}
661
662/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100663 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
664 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100665 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100667{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000668 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100669 case FFA_MSG_SEND_32:
670 return (struct ffa_value){
671 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000672 .arg1 = (receiver->mailbox.recv_sender << 16) |
673 receiver->id,
674 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000675 default:
676 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000677 dlog_error("Tried to return an invalid message function %#x\n",
678 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100679 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000680 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100681}
682
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600683struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
684 struct ffa_value *args)
685{
686 struct ffa_value ret;
687
688 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
689 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
690 args->arg7 != 0U) {
691 return ffa_error(FFA_INVALID_PARAMETERS);
692 }
693
694 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
695 return ret;
696 }
697
698 return api_ffa_msg_recv(true, current, next);
699}
700
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100701/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000702 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000703 * value needs to be forced onto the vCPU.
704 */
J-Alves6e2abc62021-12-02 14:58:56 +0000705static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100706 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000707{
Max Shvetsov40108e72020-08-27 12:39:50 +0100708 struct vcpu_locked vcpu_locked;
709 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000710 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500711 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000712 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000713
Andrew Scullb06d1752019-02-04 10:15:48 +0000714 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000715 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000716 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100717 * The VM lock is not needed in the common case so it must only be taken
718 * when it is going to be needed. This ensures there are no inter-vCPU
719 * dependencies in the common run case meaning the sensitive context
720 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000721 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100722 vcpu_locked = vcpu_lock(vcpu);
723
724#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000725 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
726 vcpu_locked, vcpu->vm->secondary_ep, 0);
727 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100728 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
729 __func__, vcpu->vm->id, current->cpu->id);
730 }
731
732#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000733 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000734 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
735 (!vcpu->vm->el0_partition &&
736 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
737 vcpu->state == VCPU_STATE_BLOCKED ||
738 vcpu->state == VCPU_STATE_PREEMPTED));
739
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000740 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100741 vcpu_unlock(&vcpu_locked);
742 vm_locked = vm_lock(vcpu->vm);
743 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000744 }
745
746 /*
747 * If the vCPU is already running somewhere then we can't run it here
748 * simultaneously. While it is actually running then the state should be
749 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
750 * stops running but while Hafnium is in the process of switching back
751 * to the primary there will be a brief period while the state has been
752 * updated but `regs_available` is still false (until
753 * `api_regs_state_saved` is called). We can't start running it again
754 * until this has finished, so count this state as still running for the
755 * purposes of this check.
756 */
757 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
758 /*
759 * vCPU is running on another pCPU.
760 *
761 * It's okay not to return the sleep duration here because the
762 * other physical CPU that is currently running this vCPU will
763 * return the sleep duration if needed.
764 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100765 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000766 ret = false;
767 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000768 }
Andrew Scull9726c252019-01-23 13:44:19 +0000769
770 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100771 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100772 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000773 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100774 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000775 }
776 ret = false;
777 goto out;
778 }
779
Andrew Walbran508e63c2018-12-20 17:02:37 +0000780 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100781 case VCPU_STATE_RUNNING:
782 case VCPU_STATE_OFF:
783 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000784 ret = false;
785 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000786
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500787 case VCPU_STATE_WAITING:
788 /*
789 * An initial FFA_RUN is necessary for secondary VM/SP to reach
790 * the message wait loop.
791 */
792 if (!vcpu->is_bootstrapped) {
793 vcpu->is_bootstrapped = true;
794 break;
795 }
796
J-Alves6e2abc62021-12-02 14:58:56 +0000797 assert(need_vm_lock == true);
798 if (!vm_locked.vm->el0_partition &&
799 plat_ffa_inject_notification_pending_interrupt(
800 vcpu_locked, current, vm_locked)) {
801 break;
802 }
803
Andrew Scullb06d1752019-02-04 10:15:48 +0000804 /*
805 * A pending message allows the vCPU to run so the message can
806 * be delivered directly.
807 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100808 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100809 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100810 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100811 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000812 break;
813 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500814
815 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
816 break;
817 }
818
819 if (arch_timer_enabled(&vcpu->regs)) {
820 timer_remaining_ns =
821 arch_timer_remaining_ns(&vcpu->regs);
822 if (timer_remaining_ns == 0) {
823 break;
824 }
825 } else {
826 dlog_verbose("Timer disabled\n");
827 }
828 run_ret->func = FFA_MSG_WAIT_32;
829 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
830 run_ret->arg2 = timer_remaining_ns;
831 ret = false;
832 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100833 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000834 if (need_vm_lock &&
835 plat_ffa_inject_notification_pending_interrupt(
836 vcpu_locked, current, vm_locked)) {
837 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
838 break;
839 }
840
Andrew Scullb06d1752019-02-04 10:15:48 +0000841 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000842 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000843 break;
844 }
845
Andrew Walbran508e63c2018-12-20 17:02:37 +0000846 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100847 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000848 arch_timer_remaining_ns(&vcpu->regs);
849
850 /*
851 * The timer expired so allow the interrupt to be
852 * delivered.
853 */
854 if (timer_remaining_ns == 0) {
855 break;
856 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100857 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000858
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100859 /*
860 * The vCPU is not ready to run, return the appropriate code to
861 * the primary which called vcpu_run.
862 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500863 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100864 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
865 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000866
867 ret = false;
868 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000869
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500870 case VCPU_STATE_BLOCKED:
871 /* A blocked vCPU is run unconditionally. Fall through. */
872 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000873 /* Check NPI is to be injected here. */
874 if (need_vm_lock) {
875 plat_ffa_inject_notification_pending_interrupt(
876 vcpu_locked, current, vm_locked);
877 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000878 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500879 default:
880 /*
881 * Execution not expected to reach here. Deny the request
882 * gracefully.
883 */
884 *run_ret = ffa_error(FFA_DENIED);
885 ret = false;
886 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000887 }
888
Andrew Scullb06d1752019-02-04 10:15:48 +0000889 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000890 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100891 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000892
J-Alves7ac49052022-02-08 17:20:53 +0000893#if SECURE_WORLD == 1
894 /* Set the designated GP register with the vCPU ID. */
895 if (is_vcpu_reset_and_start) {
896 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
897 }
898#endif
899
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000900 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000901 * Mark the registers as unavailable now that we're about to reflect
902 * them onto the real registers. This will also prevent another physical
903 * CPU from trying to read these registers.
904 */
905 vcpu->regs_available = false;
906
907 ret = true;
908
909out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100910 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000911 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100912 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000913 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000914 return ret;
915}
916
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100917struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500918 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100919{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100920 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100921 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100922 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100923
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800924 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500925 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100926 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100927
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700928 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
929 return ret;
930 }
931
Andrew Scull19503262018-09-20 14:48:39 +0100932 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100933 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100934 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100935 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100936 }
937
Fuad Tabbaed294af2019-12-20 10:43:01 +0000938 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100939 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100940 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100941 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100942
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000943 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100944 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000945 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000946 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100947 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000948
Andrew Walbran508e63c2018-12-20 17:02:37 +0000949 /*
950 * Inject timer interrupt if timer has expired. It's safe to access
951 * vcpu->regs here because api_vcpu_prepare_run already made sure that
952 * regs_available was true (and then set it to false) before returning
953 * true.
954 */
955 if (arch_timer_pending(&vcpu->regs)) {
956 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100957 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
958 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000959
960 /*
961 * Set the mask bit so the hardware interrupt doesn't fire
962 * again. Ideally we wouldn't do this because it affects what
963 * the secondary vCPU sees, but if we don't then we end up with
964 * a loop of the interrupt firing each time we try to return to
965 * the secondary vCPU.
966 */
967 arch_timer_mask(&vcpu->regs);
968 }
969
Fuad Tabbaed294af2019-12-20 10:43:01 +0000970 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000971 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000972
Andrew Scull33fecd32019-01-08 14:48:27 +0000973 /*
974 * Set a placeholder return code to the scheduler. This will be
975 * overwritten when the switch back to the primary occurs.
976 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 ret.func = FFA_INTERRUPT_32;
Andrew Walbran7e824602020-10-22 16:51:40 +0100978 ret.arg1 = 0;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100979 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000980
Andrew Scull6d2db332018-10-10 15:28:17 +0100981out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100982 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100983}
984
985/**
Andrew Scull81e85092018-12-12 12:56:20 +0000986 * Check that the mode indicates memory that is valid, owned and exclusive.
987 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100988static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000989{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100990 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
991 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000992}
993
994/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100995 * Determines the value to be returned by api_ffa_rxtx_map and
996 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
997 * there are waiters, it also switches back to the primary VM for it to wake
998 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000999 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001000static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1001 struct vcpu *current,
1002 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001003{
1004 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001005
1006 if (list_empty(&vm->mailbox.waiter_list)) {
1007 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001009 }
1010
1011 if (vm->id == HF_PRIMARY_VM_ID) {
1012 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001013 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001014 }
1015
1016 /*
1017 * Switch back to the primary VM, informing it that there are waiters
1018 * that need to be notified.
1019 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001020 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001022 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001023
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001025}
1026
1027/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001028 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001029 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001030static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1031 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001032 paddr_t pa_send_begin, paddr_t pa_send_end,
1033 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001034 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001035 struct mpool *local_page_pool)
1036{
1037 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001038
1039 /* Map the send page as read-only in the hypervisor address space. */
1040 vm_locked.vm->mailbox.send =
1041 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001042 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001043 if (!vm_locked.vm->mailbox.send) {
1044 /* TODO: partial defrag of failed range. */
1045 /* Recover any memory consumed in failed mapping. */
1046 mm_defrag(mm_stage1_locked, local_page_pool);
1047 goto fail;
1048 }
1049
1050 /*
1051 * Map the receive page as writable in the hypervisor address space. On
1052 * failure, unmap the send page before returning.
1053 */
1054 vm_locked.vm->mailbox.recv =
1055 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001056 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001057 if (!vm_locked.vm->mailbox.recv) {
1058 /* TODO: partial defrag of failed range. */
1059 /* Recover any memory consumed in failed mapping. */
1060 mm_defrag(mm_stage1_locked, local_page_pool);
1061 goto fail_undo_send;
1062 }
1063
1064 ret = true;
1065 goto out;
1066
1067 /*
1068 * The following mappings will not require more memory than is available
1069 * in the local pool.
1070 */
1071fail_undo_send:
1072 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001073 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1074 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001075
1076fail:
1077 ret = false;
1078
1079out:
Andrew Sculle1322792019-07-01 17:46:10 +01001080 return ret;
1081}
1082
1083/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001084 * Sanity checks and configures the send and receive pages in the VM stage-2
1085 * and hypervisor stage-1 page tables.
1086 *
1087 * Returns:
1088 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001089 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001090 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1091 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001092 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001093 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001094 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001095
1096struct ffa_value api_vm_configure_pages(
1097 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1098 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1099 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001100{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001101 struct ffa_value ret;
1102 paddr_t pa_send_begin;
1103 paddr_t pa_send_end;
1104 paddr_t pa_recv_begin;
1105 paddr_t pa_recv_end;
1106 uint32_t orig_send_mode;
1107 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001108 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001109
1110 /* We only allow these to be setup once. */
1111 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1112 ret = ffa_error(FFA_DENIED);
1113 goto out;
1114 }
1115
1116 /* Hafnium only supports a fixed size of RX/TX buffers. */
1117 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1118 ret = ffa_error(FFA_INVALID_PARAMETERS);
1119 goto out;
1120 }
1121
1122 /* Fail if addresses are not page-aligned. */
1123 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1124 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1125 ret = ffa_error(FFA_INVALID_PARAMETERS);
1126 goto out;
1127 }
1128
1129 /* Convert to physical addresses. */
1130 pa_send_begin = pa_from_ipa(send);
1131 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1132 pa_recv_begin = pa_from_ipa(recv);
1133 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1134
1135 /* Fail if the same page is used for the send and receive pages. */
1136 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1137 ret = ffa_error(FFA_INVALID_PARAMETERS);
1138 goto out;
1139 }
Andrew Sculle1322792019-07-01 17:46:10 +01001140
1141 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001142 * Ensure the pages are valid, owned and exclusive to the VM and that
1143 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001144 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001145 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1146 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001147 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1148 (orig_send_mode & MM_MODE_R) == 0 ||
1149 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001150 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001151 goto out;
1152 }
1153
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001154 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1155 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001156 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1157 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001158 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001159 goto out;
1160 }
Andrew Sculle1322792019-07-01 17:46:10 +01001161
1162 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001163 uint32_t mode =
1164 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1165 if (vm_locked.vm->el0_partition) {
1166 mode |= MM_MODE_USER | MM_MODE_NG;
1167 }
1168
1169 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1170 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001171 ret = ffa_error(FFA_NO_MEMORY);
1172 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001173 }
1174
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001175 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1176 if (vm_locked.vm->el0_partition) {
1177 mode |= MM_MODE_USER | MM_MODE_NG;
1178 }
1179
1180 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001181 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001182 /* TODO: partial defrag of failed range. */
1183 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001184 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001185 goto fail_undo_send;
1186 }
1187
Olivier Deprez96a2a262020-06-11 17:21:38 +02001188 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1189 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1190
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001191 /*
1192 * For EL0 partitions, since both the partition and the hypervisor code
1193 * use the EL2&0 translation regime, it is critical to mark the mappings
1194 * of the send and recv buffers as non-global in the TLB. For one, if we
1195 * dont mark it as non-global, it would cause TLB conflicts since there
1196 * would be an identity mapping with non-global attribute in the
1197 * partitions page tables, but another identity mapping in the
1198 * hypervisor page tables with the global attribute. The other issue is
1199 * one of security, we dont want other partitions to be able to access
1200 * other partitions buffers through cached translations.
1201 */
1202 if (vm_locked.vm->el0_partition) {
1203 extra_attributes |= MM_MODE_NG;
1204 }
1205
Manish Pandeyd34f8892020-06-19 17:41:07 +01001206 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1207 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001208 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001209 goto fail_undo_send_and_recv;
1210 }
1211
Manish Pandeyd34f8892020-06-19 17:41:07 +01001212 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001213 goto out;
1214
Andrew Sculle1322792019-07-01 17:46:10 +01001215fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001216 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001217 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001218
1219fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001220 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001221 orig_send_mode, local_page_pool, NULL));
1222 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001223
1224out:
Andrew Sculle1322792019-07-01 17:46:10 +01001225 return ret;
1226}
1227
1228/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001229 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001230 * must not be shared. Locking of the page tables combined with a local memory
1231 * pool ensures there will always be enough memory to recover from any errors
1232 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1233 * by another core which could result in this transaction being unable to roll
1234 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001235 *
1236 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001238 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001240 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001241 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001242 * - FFA_SUCCESS on success if no further action is needed.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001243 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001244struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
Federico Recanati9f1b6532022-04-14 13:15:28 +02001245 uint32_t page_count, struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001246{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001247 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001248 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001249 struct vm_locked vm_locked;
1250 struct mm_stage1_locked mm_stage1_locked;
1251 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001252
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001253 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001254 * Create a local pool so any freed memory can't be used by another
1255 * thread. This is to ensure the original mapping can be restored if any
1256 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001257 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001258 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1259
Andrew Sculle1322792019-07-01 17:46:10 +01001260 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001261 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001262
Manish Pandeyd34f8892020-06-19 17:41:07 +01001263 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1264 page_count, &local_page_pool);
1265 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001266 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001267 }
1268
Federico Recanati9f1b6532022-04-14 13:15:28 +02001269 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Scull220e6212018-12-21 18:09:00 +00001270
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001271exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001272 mpool_fini(&local_page_pool);
1273
1274 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001275 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001276
1277 return ret;
1278}
1279
1280/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001281 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1282 * translation regime of the caller. Unmap the region for the hypervisor and
1283 * set the memory region to owned and exclusive for the component. Since the
1284 * memory region mapped in the page table, when the buffers were originally
1285 * created we can safely remap it.
1286 *
1287 * Returns:
1288 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1289 * behalf of the caller.
1290 * - FFA_SUCCESS on success if no further action is needed.
1291 */
1292struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1293 struct vcpu *current)
1294{
1295 struct vm *vm = current->vm;
1296 struct vm_locked vm_locked;
1297 struct mm_stage1_locked mm_stage1_locked;
1298 paddr_t send_pa_begin;
1299 paddr_t send_pa_end;
1300 paddr_t recv_pa_begin;
1301 paddr_t recv_pa_end;
1302
1303 /*
1304 * Check there is a buffer pair registered on behalf of the caller.
1305 * Since forwarding is not yet supported the allocator ID MBZ.
1306 */
1307 if (allocator_id != 0) {
1308 dlog_error(
1309 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1310 "supported so vm id must be zero.\n");
1311 return ffa_error(FFA_INVALID_PARAMETERS);
1312 }
1313
1314 /* Get send and receive buffers. */
1315 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001316 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001317 "No buffer pair registered on behalf of the caller.\n");
1318 return ffa_error(FFA_INVALID_PARAMETERS);
1319 }
1320
1321 /* Currently a mailbox size of 1 page is assumed. */
1322 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1323 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1324 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1325 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1326
1327 vm_locked = vm_lock(vm);
1328 mm_stage1_locked = mm_lock_stage1();
1329
1330 /*
1331 * Set the memory region of the buffers back to the default mode
1332 * for the VM. Since this memory region was already mapped for the
1333 * RXTX buffers we can safely remap them.
1334 */
1335 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1336 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1337 NULL));
1338
1339 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1340 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1341 NULL));
1342
1343 /* Unmap the buffers in the partition manager. */
1344 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1345 &api_page_pool));
1346 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1347 &api_page_pool));
1348
1349 vm->mailbox.send = NULL;
1350 vm->mailbox.recv = NULL;
1351
1352 mm_unlock_stage1(&mm_stage1_locked);
1353 vm_unlock(&vm_locked);
1354
1355 return (struct ffa_value){.func = FFA_SUCCESS_32};
1356}
1357
1358/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001359 * Notifies the `to` VM about the message currently in its mailbox, possibly
1360 * with the help of the primary VM.
1361 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001362static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1363 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001364{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001365 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1366 struct ffa_value primary_ret = {
1367 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001368 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001369 };
1370
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001371 /* Messages for the primary VM are delivered directly. */
1372 if (to.vm->id == HF_PRIMARY_VM_ID) {
1373 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001374 * Only tell the primary VM the size and other details if the
1375 * message is for it, to avoid leaking data about messages for
1376 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001377 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001378 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001379
1380 to.vm->mailbox.state = MAILBOX_STATE_READ;
1381 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001382 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001383 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001384 }
1385
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001386 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1387
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001388 /* Messages for the TEE are sent on via the dispatcher. */
1389 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001390 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001391
Olivier Deprez112d2b52020-09-30 07:39:23 +02001392 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001393 /*
1394 * After the call to the TEE completes it must have finished
1395 * reading its RX buffer, so it is ready for another message.
1396 */
1397 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001398 /*
1399 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001401 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001402 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001403 }
1404
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001405 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001406 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001407 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001408 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001409 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001410
1411 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001412}
1413
1414/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001415 * Copies data from the sender's send buffer to the recipient's receive buffer
1416 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001417 *
1418 * If the recipient's receive buffer is busy, it can optionally register the
1419 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001420 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1422 ffa_vm_id_t receiver_vm_id, uint32_t size,
1423 uint32_t attributes, struct vcpu *current,
1424 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001425{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001426 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001427 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001428 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001429 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001430 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001431 struct vcpu_locked current_locked;
1432 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 bool notify =
1434 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001435
Andrew Walbran70bc8622019-10-07 14:15:58 +01001436 /* Ensure sender VM ID corresponds to the current VM. */
1437 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001439 }
1440
1441 /* Disallow reflexive requests as this suggests an error in the VM. */
1442 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001444 }
1445
1446 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001447 if (size > FFA_MSG_PAYLOAD_MAX) {
1448 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001449 }
1450
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001451 /*
1452 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1453 * invocation.
1454 */
1455 current_locked = vcpu_lock(current);
1456 is_direct_request_ongoing =
1457 is_ffa_direct_msg_request_ongoing(current_locked);
1458 vcpu_unlock(&current_locked);
1459
1460 if (is_direct_request_ongoing) {
1461 return ffa_error(FFA_DENIED);
1462 }
1463
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001464 /* Ensure the receiver VM exists. */
1465 to = vm_find(receiver_vm_id);
1466 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001468 }
1469
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001470 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001471 * Check that the sender has configured its send buffer. If the tx
1472 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1473 * be safely accessed after releasing the lock since the tx mailbox
1474 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001475 */
1476 sl_lock(&from->lock);
1477 from_msg = from->mailbox.send;
1478 sl_unlock(&from->lock);
1479
1480 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001481 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001482 }
1483
Andrew Walbran82d6d152019-12-24 15:02:06 +00001484 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001485
Andrew Walbran82d6d152019-12-24 15:02:06 +00001486 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001487 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001488 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001489 }
1490
Andrew Walbran82d6d152019-12-24 15:02:06 +00001491 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001492 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001493 to->mailbox.recv_size = size;
1494 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001495 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001496 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001497
1498out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001499 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001500
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001501 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001502}
1503
1504/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001505 * Checks whether the vCPU's attempt to block for a message has already been
1506 * interrupted or whether it is allowed to block.
1507 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001508bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001509{
Manish Pandey35e452f2021-02-18 21:36:34 +00001510 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001511 bool interrupted;
1512
Manish Pandey35e452f2021-02-18 21:36:34 +00001513 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001514
1515 /*
1516 * Don't block if there are enabled and pending interrupts, to match
1517 * behaviour of wait_for_interrupt.
1518 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001519 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001520
Manish Pandey35e452f2021-02-18 21:36:34 +00001521 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001522
1523 return interrupted;
1524}
1525
1526/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001527 * Receives a message from the mailbox. If one isn't available, this function
1528 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001529 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001530 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001531 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001532struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1533 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001534{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001535 bool is_direct_request_ongoing;
1536 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001537 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001539 bool is_from_secure_world =
1540 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001541
Andrew Scullaa039b32018-10-04 15:02:26 +01001542 /*
1543 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001544 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001545 */
J-Alvesb37fd082020-10-22 12:29:21 +01001546 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001547 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001548 }
1549
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001550 /*
1551 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1552 * invocation.
1553 */
1554 current_locked = vcpu_lock(current);
1555 is_direct_request_ongoing =
1556 is_ffa_direct_msg_request_ongoing(current_locked);
1557 vcpu_unlock(&current_locked);
1558
1559 if (is_direct_request_ongoing) {
1560 return ffa_error(FFA_DENIED);
1561 }
1562
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001563 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001564
Andrew Scullaa039b32018-10-04 15:02:26 +01001565 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001566 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1567 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001568 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001569 goto out;
1570 }
1571
1572 /* No pending message so fail if not allowed to block. */
1573 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001574 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001575 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001576 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001577
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001578 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001579 * From this point onward this call can only be interrupted or a message
1580 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001582 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001583 return_code = ffa_error(FFA_INTERRUPTED);
1584 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001585 goto out;
1586 }
1587
J-Alvesb37fd082020-10-22 12:29:21 +01001588 if (is_from_secure_world) {
1589 /* Return to other world if caller is a SP. */
1590 *next = api_switch_to_other_world(
1591 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001592 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001593 } else {
1594 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001595 struct ffa_value run_return = {
1596 .func = FFA_MSG_WAIT_32,
1597 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001598 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001599
Andrew Walbranb4816552018-12-05 17:35:42 +00001600 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001601 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001602 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001603out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001604 sl_unlock(&vm->lock);
1605
Jose Marinho3e2442f2019-03-12 13:30:37 +00001606 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001607}
1608
1609/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001610 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1611 * by this function, the caller must have called api_mailbox_send before with
1612 * the notify argument set to true, and this call must have failed because the
1613 * mailbox was not available.
1614 *
1615 * It should be called repeatedly to retrieve a list of VMs.
1616 *
1617 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1618 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001619 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001620int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001621{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001622 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001623 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001624 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001625
1626 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001627 if (list_empty(&vm->mailbox.ready_list)) {
1628 ret = -1;
1629 goto exit;
1630 }
1631
1632 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1633 ready_links);
1634 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001635 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001636
1637exit:
1638 sl_unlock(&vm->lock);
1639 return ret;
1640}
1641
1642/**
1643 * Retrieves the next VM waiting to be notified that the mailbox of the
1644 * specified VM became writable. Only primary VMs are allowed to call this.
1645 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001646 * Returns -1 on failure or if there are no waiters; the VM id of the next
1647 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001648 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001649int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001650{
1651 struct vm *vm;
1652 struct vm_locked locked;
1653 struct wait_entry *entry;
1654 struct vm *waiting_vm;
1655
1656 /* Only primary VMs are allowed to call this function. */
1657 if (current->vm->id != HF_PRIMARY_VM_ID) {
1658 return -1;
1659 }
1660
Andrew Walbran42347a92019-05-09 13:59:03 +01001661 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001662 if (vm == NULL) {
1663 return -1;
1664 }
1665
Fuad Tabbaed294af2019-12-20 10:43:01 +00001666 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001667 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001668 entry = api_fetch_waiter(locked);
1669 vm_unlock(&locked);
1670
1671 if (entry == NULL) {
1672 return -1;
1673 }
1674
1675 /* Enqueue notification to waiting VM. */
1676 waiting_vm = entry->waiting_vm;
1677
1678 sl_lock(&waiting_vm->lock);
1679 if (list_empty(&entry->ready_links)) {
1680 list_append(&waiting_vm->mailbox.ready_list,
1681 &entry->ready_links);
1682 }
1683 sl_unlock(&waiting_vm->lock);
1684
1685 return waiting_vm->id;
1686}
1687
1688/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001689 * Releases the caller's mailbox so that a new message can be received. The
1690 * caller must have copied out all data they wish to preserve as new messages
1691 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001692 *
1693 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001694 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1695 * - FFA_SUCCESS on success if no further action is needed.
1696 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001697 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001698 * hf_mailbox_waiter_get.
1699 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001700struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001701{
1702 struct vm *vm = current->vm;
1703 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001704 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001705
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001706 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001707 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001708 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001709 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001711 break;
1712
Andrew Sculld6ee1102019-04-05 22:12:42 +01001713 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001714 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001715 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001716 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001717 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001718 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001719
1720 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001721}
Andrew Walbran318f5732018-11-20 16:23:42 +00001722
1723/**
1724 * Enables or disables a given interrupt ID for the calling vCPU.
1725 *
1726 * Returns 0 on success, or -1 if the intid is invalid.
1727 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001728int64_t api_interrupt_enable(uint32_t intid, bool enable,
1729 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001730{
Manish Pandey35e452f2021-02-18 21:36:34 +00001731 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001732 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001733 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1734 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001735
Andrew Walbran318f5732018-11-20 16:23:42 +00001736 if (intid >= HF_NUM_INTIDS) {
1737 return -1;
1738 }
1739
Manish Pandey35e452f2021-02-18 21:36:34 +00001740 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001741 if (enable) {
Maksims Svecovs953dee52022-04-01 12:15:36 +01001742 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 }
1749
Andrew Walbran3d84a262018-12-13 14:41:19 +00001750 /*
1751 * If it is pending and was not enabled before, increment the
1752 * count.
1753 */
1754 if (current->interrupts.interrupt_pending[intid_index] &
1755 ~current->interrupts.interrupt_enabled[intid_index] &
1756 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001757 if ((current->interrupts.interrupt_type[intid_index] &
1758 intid_mask) ==
1759 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1760 vcpu_irq_count_increment(current_locked);
1761 } else {
1762 vcpu_fiq_count_increment(current_locked);
1763 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001764 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001765 current->interrupts.interrupt_enabled[intid_index] |=
1766 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001767 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001768 /*
1769 * If it is pending and was enabled before, decrement the count.
1770 */
1771 if (current->interrupts.interrupt_pending[intid_index] &
1772 current->interrupts.interrupt_enabled[intid_index] &
1773 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001774 if ((current->interrupts.interrupt_type[intid_index] &
1775 intid_mask) ==
1776 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1777 vcpu_irq_count_decrement(current_locked);
1778 } else {
1779 vcpu_fiq_count_decrement(current_locked);
1780 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001781 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001782 current->interrupts.interrupt_enabled[intid_index] &=
1783 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001784 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001785 }
1786
Manish Pandey35e452f2021-02-18 21:36:34 +00001787 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001788 return 0;
1789}
1790
1791/**
1792 * Returns the ID of the next pending interrupt for the calling vCPU, and
1793 * acknowledges it (i.e. marks it as no longer pending). Returns
1794 * HF_INVALID_INTID if there are no pending interrupts.
1795 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001796uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001797{
1798 uint8_t i;
1799 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001800 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001801
1802 /*
1803 * Find the first enabled and pending interrupt ID, return it, and
1804 * deactivate it.
1805 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001806 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001807 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1808 uint32_t enabled_and_pending =
1809 current->interrupts.interrupt_enabled[i] &
1810 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001811
Andrew Walbran318f5732018-11-20 16:23:42 +00001812 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001813 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001814 uint32_t intid_mask = 1U << bit_index;
1815
Andrew Walbran3d84a262018-12-13 14:41:19 +00001816 /*
1817 * Mark it as no longer pending and decrement the count.
1818 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001819 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1820
1821 if ((current->interrupts.interrupt_type[i] &
1822 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1823 vcpu_irq_count_decrement(current_locked);
1824 } else {
1825 vcpu_fiq_count_decrement(current_locked);
1826 }
1827
Andrew Walbran3d84a262018-12-13 14:41:19 +00001828 first_interrupt =
1829 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001830 break;
1831 }
1832 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001833
Manish Pandey35e452f2021-02-18 21:36:34 +00001834 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001835 return first_interrupt;
1836}
1837
1838/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001839 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001840 * given VM and vCPU.
1841 */
1842static inline bool is_injection_allowed(uint32_t target_vm_id,
1843 struct vcpu *current)
1844{
1845 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001846
Andrew Walbran318f5732018-11-20 16:23:42 +00001847 /*
1848 * The primary VM is allowed to inject interrupts into any VM. Secondary
1849 * VMs are only allowed to inject interrupts into their own vCPUs.
1850 */
1851 return current_vm_id == HF_PRIMARY_VM_ID ||
1852 current_vm_id == target_vm_id;
1853}
1854
1855/**
1856 * Injects a virtual interrupt of the given ID into the given target vCPU.
1857 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1858 * when the vCPU is next run, which is up to the scheduler.
1859 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001860 * Returns:
1861 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1862 * ID is invalid, or the current VM is not allowed to inject interrupts to
1863 * the target VM.
1864 * - 0 on success if no further action is needed.
1865 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1866 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001867 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001868int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1869 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001870 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001871{
Andrew Walbran318f5732018-11-20 16:23:42 +00001872 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001873 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001874
1875 if (intid >= HF_NUM_INTIDS) {
1876 return -1;
1877 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001878
Andrew Walbran318f5732018-11-20 16:23:42 +00001879 if (target_vm == NULL) {
1880 return -1;
1881 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001882
Andrew Walbran318f5732018-11-20 16:23:42 +00001883 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001884 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001885 return -1;
1886 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001887
Andrew Walbran318f5732018-11-20 16:23:42 +00001888 if (!is_injection_allowed(target_vm_id, current)) {
1889 return -1;
1890 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001891
Andrew Walbrane1310df2019-04-29 17:28:28 +01001892 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001893
Manish Pandey35e452f2021-02-18 21:36:34 +00001894 dlog_verbose(
1895 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1896 "%u\n",
1897 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1898 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001899 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001900}
Andrew Scull6386f252018-12-06 13:29:10 +00001901
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001902/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001903struct ffa_value api_ffa_version(struct vcpu *current,
1904 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001905{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001906 struct vm_locked current_vm_locked;
1907
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001908 /*
1909 * Ensure that both major and minor revision representation occupies at
1910 * most 15 bits.
1911 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001912 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001913 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001914 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001915 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001916 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001917 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001918 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001919 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001920
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001921 current_vm_locked = vm_lock(current->vm);
1922 current_vm_locked.vm->ffa_version = requested_version;
1923 vm_unlock(&current_vm_locked);
1924
Daniel Boulby6e32c612021-02-17 15:09:41 +00001925 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001926}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001927
1928int64_t api_debug_log(char c, struct vcpu *current)
1929{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001930 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001931 struct vm *vm = current->vm;
1932 struct vm_locked vm_locked = vm_lock(vm);
1933
Andrew Sculld54e1be2019-08-20 11:09:42 +01001934 if (c == '\n' || c == '\0') {
1935 flush = true;
1936 } else {
1937 vm->log_buffer[vm->log_buffer_length++] = c;
1938 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1939 }
1940
1941 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001942 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1943 vm->log_buffer_length);
1944 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001945 }
1946
1947 vm_unlock(&vm_locked);
1948
1949 return 0;
1950}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001951
1952/**
J-Alves6f72ca82021-11-01 12:34:58 +00001953 * Helper for success return of FFA_FEATURES, for when it is used to query
1954 * an interrupt ID.
1955 */
1956struct ffa_value api_ffa_feature_success(uint32_t arg2)
1957{
1958 return (struct ffa_value){
1959 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1960}
1961
1962/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001963 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001964 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001965 */
J-Alves6f72ca82021-11-01 12:34:58 +00001966struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001967{
J-Alves6f72ca82021-11-01 12:34:58 +00001968 /*
1969 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1970 * if using a feature ID.
1971 */
1972 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1973 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1974 return ffa_error(FFA_NOT_SUPPORTED);
1975 }
1976
1977 switch (feature_function_id) {
1978 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001979 case FFA_ERROR_32:
1980 case FFA_SUCCESS_32:
1981 case FFA_INTERRUPT_32:
1982 case FFA_VERSION_32:
1983 case FFA_FEATURES_32:
1984 case FFA_RX_RELEASE_32:
1985 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001986 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001987 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001988 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001990 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001991 case FFA_MEM_DONATE_32:
1992 case FFA_MEM_LEND_32:
1993 case FFA_MEM_SHARE_32:
1994 case FFA_MEM_RETRIEVE_REQ_32:
1995 case FFA_MEM_RETRIEVE_RESP_32:
1996 case FFA_MEM_RELINQUISH_32:
1997 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001998 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001999 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002000 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002001 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002002#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002003 /* FF-A v1.1 features. */
2004 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002005 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2006 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2007 case FFA_NOTIFICATION_BIND_32:
2008 case FFA_NOTIFICATION_UNBIND_32:
2009 case FFA_NOTIFICATION_SET_32:
2010 case FFA_NOTIFICATION_GET_32:
2011 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002012 case FFA_MEM_PERM_GET_32:
2013 case FFA_MEM_PERM_SET_32:
2014 case FFA_MEM_PERM_GET_64:
2015 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00002016#endif
2017 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002018
2019#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2020 /* Check support of a feature provided respective feature ID. */
2021 case FFA_FEATURE_NPI:
2022 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2023 case FFA_FEATURE_SRI:
2024 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2025#endif
2026 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002027 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002028 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002029 }
2030}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002031
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002032/**
J-Alves645eabe2021-02-22 16:08:27 +00002033 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2034 * interfaces.
2035 */
2036static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2037{
2038 return args.arg2 == 0U;
2039}
2040
2041/**
J-Alves76d99af2021-03-10 17:42:11 +00002042 * Limits size of arguments in ffa_value structure to 32-bit.
2043 */
2044static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2045{
2046 return (struct ffa_value){
2047 .func = (uint32_t)args.func,
2048 .arg1 = (uint32_t)args.arg1,
2049 .arg2 = (uint32_t)0,
2050 .arg3 = (uint32_t)args.arg3,
2051 .arg4 = (uint32_t)args.arg4,
2052 .arg5 = (uint32_t)args.arg5,
2053 .arg6 = (uint32_t)args.arg6,
2054 .arg7 = (uint32_t)args.arg7,
2055 };
2056}
2057
2058/**
2059 * Helper to copy direct message payload, depending on SMC used and expected
2060 * registers size.
2061 */
2062static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2063{
2064 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2065 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2066 return api_ffa_value_copy32(args);
2067 }
2068
2069 return (struct ffa_value){
2070 .func = args.func,
2071 .arg1 = args.arg1,
2072 .arg2 = 0,
2073 .arg3 = args.arg3,
2074 .arg4 = args.arg4,
2075 .arg5 = args.arg5,
2076 .arg6 = args.arg6,
2077 .arg7 = args.arg7,
2078 };
2079}
2080
2081/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002082 * Send an FF-A direct message request.
2083 */
2084struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2085 ffa_vm_id_t receiver_vm_id,
2086 struct ffa_value args,
2087 struct vcpu *current,
2088 struct vcpu **next)
2089{
J-Alves17228f72021-04-20 17:13:19 +01002090 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002091 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002092 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002093 struct vcpu *receiver_vcpu;
2094 struct two_vcpu_locked vcpus_locked;
2095
J-Alves645eabe2021-02-22 16:08:27 +00002096 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2097 return ffa_error(FFA_INVALID_PARAMETERS);
2098 }
2099
Olivier Deprez55a189e2021-06-09 15:45:27 +02002100 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2101 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002102 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002103 }
2104
Olivier Deprez55a189e2021-06-09 15:45:27 +02002105 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002106 return ret;
2107 }
2108
2109 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2110
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002111 receiver_vm = vm_find(receiver_vm_id);
2112 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002113 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002114 return ffa_error(FFA_INVALID_PARAMETERS);
2115 }
2116
2117 /*
J-Alves439ac972021-11-18 17:32:03 +00002118 * Check if sender supports sending direct message req, and if
2119 * receiver supports receipt of direct message requests.
2120 */
2121 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2122 return ffa_error(FFA_DENIED);
2123 }
2124
2125 /*
Olivier Deprezc13a8692022-04-08 17:47:14 +02002126 * Per FF-A EAC spec section 4.4.1 the firmware framework supports
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002127 * UP (migratable) or MP partitions with a number of vCPUs matching the
2128 * number of PEs in the system. It further states that MP partitions
2129 * accepting direct request messages cannot migrate.
2130 */
J-Alvesad6a0432021-04-09 16:06:21 +01002131 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002132 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002133 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002134 return ffa_error(FFA_INVALID_PARAMETERS);
2135 }
2136
J-Alves6e2abc62021-12-02 14:58:56 +00002137 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002138 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2139
2140 /*
2141 * If destination vCPU is executing or already received an
2142 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2143 * busy. There is a brief period of time where the vCPU state has
2144 * changed but regs_available is still false thus consider this case as
2145 * the vCPU not yet ready to receive a direct message request.
2146 */
2147 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2148 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2149 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002150 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002151 ret = ffa_error(FFA_BUSY);
2152 goto out;
2153 }
2154
2155 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2156 memory_order_relaxed)) {
2157 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002158 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002159 receiver_vcpu->vm->id,
2160 vcpu_index(receiver_vcpu));
2161 receiver_vcpu->state = VCPU_STATE_ABORTED;
2162 }
2163
2164 ret = ffa_error(FFA_ABORTED);
2165 goto out;
2166 }
2167
2168 switch (receiver_vcpu->state) {
2169 case VCPU_STATE_OFF:
2170 case VCPU_STATE_RUNNING:
2171 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002172 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002173 case VCPU_STATE_BLOCKED:
2174 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002175 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2176 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002177 ret = ffa_error(FFA_BUSY);
2178 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002179 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002180 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002181 * We expect target vCPU to be in WAITING state after either
2182 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002183 */
2184 break;
2185 }
2186
2187 /* Inject timer interrupt if any pending */
2188 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002189 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2190 HF_VIRTUAL_TIMER_INTID, current,
2191 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002192
2193 arch_timer_mask(&receiver_vcpu->regs);
2194 }
2195
2196 /* The receiver vCPU runs upon direct message invocation */
2197 receiver_vcpu->cpu = current->cpu;
2198 receiver_vcpu->state = VCPU_STATE_RUNNING;
2199 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002200 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002201
J-Alves76d99af2021-03-10 17:42:11 +00002202 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002203
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002204 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002205
2206 /* Switch to receiver vCPU targeted to by direct msg request */
2207 *next = receiver_vcpu;
2208
J-Alves6e2abc62021-12-02 14:58:56 +00002209 if (!receiver_locked.vm->el0_partition) {
2210 /*
2211 * If the scheduler in the system is giving CPU cycles to the
2212 * receiver, due to pending notifications, inject the NPI
2213 * interrupt. Following call assumes that '*next' has been set
2214 * to receiver_vcpu.
2215 */
2216 plat_ffa_inject_notification_pending_interrupt(
2217 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2218 ? vcpus_locked.vcpu1
2219 : vcpus_locked.vcpu2,
2220 current, receiver_locked);
2221 }
2222
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002223 /*
2224 * Since this flow will lead to a VM switch, the return value will not
2225 * be applied to current vCPU.
2226 */
2227
2228out:
2229 sl_unlock(&receiver_vcpu->lock);
2230 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002231 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002232
2233 return ret;
2234}
2235
2236/**
2237 * Send an FF-A direct message response.
2238 */
2239struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2240 ffa_vm_id_t receiver_vm_id,
2241 struct ffa_value args,
2242 struct vcpu *current,
2243 struct vcpu **next)
2244{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002245 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002246
2247 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2248 return ffa_error(FFA_INVALID_PARAMETERS);
2249 }
2250
J-Alves76d99af2021-03-10 17:42:11 +00002251 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002252
Olivier Deprez55a189e2021-06-09 15:45:27 +02002253 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2254 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002255 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002256 }
2257
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002258 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002259 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002260 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002261 * No need for REQ/RESP state management as managed exit does
2262 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002263 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002264 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2265 vcpu_unlock(&current_locked);
2266 return ffa_error(FFA_DENIED);
2267 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002268
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002269 /*
2270 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2271 * a secure interrupt to a SP that is performing managed exit.
2272 * We have taken a implementation defined choice to not allow
2273 * Managed exit while a SP is processing a secure interrupt.
2274 */
2275 CHECK(!current->processing_secure_interrupt);
2276
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002277 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002278 current->processing_managed_exit = false;
2279 } else {
2280 /*
2281 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2282 * defined originator.
2283 */
2284 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2285 /*
2286 * Sending direct response but direct request origin
2287 * vCPU is not set.
2288 */
2289 vcpu_unlock(&current_locked);
2290 return ffa_error(FFA_DENIED);
2291 }
2292
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002293 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002294 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2295 vcpu_unlock(&current_locked);
2296 return ffa_error(FFA_DENIED);
2297 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002298 }
2299
2300 /* Clear direct request origin for the caller. */
2301 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2302
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002303 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002304
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002305 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002306 *next = api_switch_to_other_world(
2307 current, to_ret,
2308 /*
2309 * Current vcpu sent a direct response. It moves to
2310 * waiting state.
2311 */
2312 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002313 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002314 *next = api_switch_to_primary(
2315 current, to_ret,
2316 /*
2317 * Current vcpu sent a direct response. It moves to
2318 * waiting state.
2319 */
2320 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002321 } else if (vm_id_is_current_world(receiver_vm_id)) {
2322 /*
2323 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002324 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002325 * made function return error before getting to this point.
2326 */
2327 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002328 /*
2329 * current vcpu sent a direct response.
2330 * It moves to waiting state.
2331 */
2332 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002333 } else {
2334 panic("Invalid direct message response invocation");
2335 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002336
2337 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2338}
2339
J-Alves84658fc2021-06-17 14:37:32 +01002340static bool api_memory_region_check_flags(
2341 struct ffa_memory_region *memory_region, uint32_t share_func)
2342{
2343 switch (share_func) {
2344 case FFA_MEM_SHARE_32:
2345 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2346 0U) {
2347 return false;
2348 }
2349 /* Intentional fall-through */
2350 case FFA_MEM_LEND_32:
2351 case FFA_MEM_DONATE_32: {
2352 /* Bits 31:2 Must Be Zero. */
2353 ffa_memory_receiver_flags_t to_mask =
2354 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2355 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2356
2357 if ((memory_region->flags & to_mask) != 0U) {
2358 return false;
2359 }
2360 break;
2361 }
2362 default:
2363 panic("Check for mem send calls only.\n");
2364 }
2365
2366 /* Last check reserved values are 0 */
2367 return true;
2368}
2369
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002370struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2371 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002372 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002373{
2374 struct vm *from = current->vm;
2375 struct vm *to;
2376 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002377 struct ffa_memory_region *memory_region;
2378 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002379
2380 if (ipa_addr(address) != 0 || page_count != 0) {
2381 /*
2382 * Hafnium only supports passing the descriptor in the TX
2383 * mailbox.
2384 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002385 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002386 }
2387
Andrew Walbranca808b12020-05-15 17:22:28 +01002388 if (fragment_length > length) {
2389 dlog_verbose(
2390 "Fragment length %d greater than total length %d.\n",
2391 fragment_length, length);
2392 return ffa_error(FFA_INVALID_PARAMETERS);
2393 }
2394 if (fragment_length < sizeof(struct ffa_memory_region) +
2395 sizeof(struct ffa_memory_access)) {
2396 dlog_verbose(
2397 "Initial fragment length %d smaller than header size "
2398 "%d.\n",
2399 fragment_length,
2400 sizeof(struct ffa_memory_region) +
2401 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002402 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002403 }
2404
2405 /*
2406 * Check that the sender has configured its send buffer. If the TX
2407 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2408 * be safely accessed after releasing the lock since the TX mailbox
2409 * address can only be configured once.
2410 */
2411 sl_lock(&from->lock);
2412 from_msg = from->mailbox.send;
2413 sl_unlock(&from->lock);
2414
2415 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002417 }
2418
2419 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002420 * Copy the memory region descriptor to a fresh page from the memory
2421 * pool. This prevents the sender from changing it underneath us, and
2422 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002423 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002424 if (fragment_length > HF_MAILBOX_SIZE ||
2425 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002426 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002427 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002428 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002429 if (memory_region == NULL) {
2430 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002431 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002432 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002433 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002434
2435 /* The sender must match the caller. */
2436 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002437 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002438 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002439 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002440 }
2441
J-Alves84658fc2021-06-17 14:37:32 +01002442 if (!api_memory_region_check_flags(memory_region, share_func)) {
2443 dlog_verbose(
2444 "Memory region reserved arguments must be zero.\n");
2445 ret = ffa_error(FFA_INVALID_PARAMETERS);
2446 goto out;
2447 }
2448
Andrew Walbrana65a1322020-04-06 19:32:32 +01002449 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002450 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002451 dlog_verbose(
2452 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002453 "endpoint memory access descriptors, expected 1).\n",
2454 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002455 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002456 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002457 }
2458
2459 /*
2460 * Ensure that the receiver VM exists and isn't the same as the sender.
2461 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002462 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002463 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002464 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466 goto out;
2467 }
2468
Maksims Svecovsa3d570c2021-12-08 11:16:32 +00002469 if (!plat_ffa_is_memory_send_valid(to->id, share_func)) {
2470 ret = ffa_error(FFA_DENIED);
2471 goto out;
2472 }
2473
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474 if (to->id == HF_TEE_VM_ID) {
2475 /*
2476 * The 'to' VM lock is only needed in the case that it is the
2477 * TEE VM.
2478 */
2479 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2480
2481 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002482 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 goto out_unlock;
2484 }
2485
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002486 ret = ffa_memory_tee_send(
2487 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2488 length, fragment_length, share_func, &api_page_pool);
2489 /*
2490 * ffa_tee_memory_send takes ownership of the memory_region, so
2491 * make sure we don't free it.
2492 */
2493 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002494
2495 out_unlock:
2496 vm_unlock(&vm_to_from_lock.vm1);
2497 vm_unlock(&vm_to_from_lock.vm2);
2498 } else {
2499 struct vm_locked from_locked = vm_lock(from);
2500
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002501 ret = ffa_memory_send(from_locked, memory_region, length,
2502 fragment_length, share_func,
2503 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002504 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002505 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002506 * make sure we don't free it.
2507 */
2508 memory_region = NULL;
2509
2510 vm_unlock(&from_locked);
2511 }
2512
2513out:
2514 if (memory_region != NULL) {
2515 mpool_free(&api_page_pool, memory_region);
2516 }
2517
2518 return ret;
2519}
2520
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002521struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2522 uint32_t fragment_length,
2523 ipaddr_t address, uint32_t page_count,
2524 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525{
2526 struct vm *to = current->vm;
2527 struct vm_locked to_locked;
2528 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002529 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002531 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002532
2533 if (ipa_addr(address) != 0 || page_count != 0) {
2534 /*
2535 * Hafnium only supports passing the descriptor in the TX
2536 * mailbox.
2537 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002539 }
2540
Andrew Walbrana65a1322020-04-06 19:32:32 +01002541 if (fragment_length != length) {
2542 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002545
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002547 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002548 message_buffer_size = cpu_get_buffer_size(current->cpu);
2549 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2550 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 }
2553
2554 to_locked = vm_lock(to);
2555 to_msg = to->mailbox.send;
2556
2557 if (to_msg == NULL) {
2558 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 goto out;
2561 }
2562
2563 /*
2564 * Copy the retrieve request descriptor to an internal buffer, so that
2565 * the caller can't change it underneath us.
2566 */
2567 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2568
2569 if (msg_receiver_busy(to_locked, NULL, false)) {
2570 /*
2571 * Can't retrieve memory information if the mailbox is not
2572 * available.
2573 */
2574 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002575 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002576 goto out;
2577 }
2578
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002579 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2580 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002581
2582out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002583 vm_unlock(&to_locked);
2584 return ret;
2585}
2586
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002587struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002588{
2589 struct vm *from = current->vm;
2590 struct vm_locked from_locked;
2591 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002592 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002593 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002594 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002595 uint32_t length;
2596
2597 from_locked = vm_lock(from);
2598 from_msg = from->mailbox.send;
2599
2600 if (from_msg == NULL) {
2601 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603 goto out;
2604 }
2605
2606 /*
2607 * Calculate length from relinquish descriptor before copying. We will
2608 * check again later to make sure it hasn't changed.
2609 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002610 length = sizeof(struct ffa_mem_relinquish) +
2611 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2612 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002613 /*
2614 * Copy the relinquish descriptor to an internal buffer, so that the
2615 * caller can't change it underneath us.
2616 */
2617 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002618 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002619 message_buffer_size = cpu_get_buffer_size(current->cpu);
2620 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2621 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002622 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002623 goto out;
2624 }
2625 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2626
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002627 if (sizeof(struct ffa_mem_relinquish) +
2628 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002629 length) {
2630 dlog_verbose(
2631 "Endpoint count changed while copying to internal "
2632 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002633 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002634 goto out;
2635 }
2636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2638 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002639
2640out:
2641 vm_unlock(&from_locked);
2642 return ret;
2643}
2644
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2646 ffa_memory_region_flags_t flags,
2647 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002648{
2649 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651
Olivier Deprez55a189e2021-06-09 15:45:27 +02002652 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002653 struct vm_locked to_locked = vm_lock(to);
2654
Andrew Walbranca808b12020-05-15 17:22:28 +01002655 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002656 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002657
Andrew Walbran290b0c92020-02-03 16:37:14 +00002658 vm_unlock(&to_locked);
2659 } else {
2660 struct vm *from = vm_find(HF_TEE_VM_ID);
2661 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2662
Andrew Walbranca808b12020-05-15 17:22:28 +01002663 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2664 vm_to_from_lock.vm2, handle, flags,
2665 &api_page_pool);
2666
2667 vm_unlock(&vm_to_from_lock.vm1);
2668 vm_unlock(&vm_to_from_lock.vm2);
2669 }
2670
2671 return ret;
2672}
2673
2674struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2675 uint32_t fragment_offset,
2676 ffa_vm_id_t sender_vm_id,
2677 struct vcpu *current)
2678{
2679 struct vm *to = current->vm;
2680 struct vm_locked to_locked;
2681 struct ffa_value ret;
2682
2683 /* Sender ID MBZ at virtual instance. */
2684 if (sender_vm_id != 0) {
2685 return ffa_error(FFA_INVALID_PARAMETERS);
2686 }
2687
2688 to_locked = vm_lock(to);
2689
2690 if (msg_receiver_busy(to_locked, NULL, false)) {
2691 /*
2692 * Can't retrieve memory information if the mailbox is not
2693 * available.
2694 */
2695 dlog_verbose("RX buffer not ready.\n");
2696 ret = ffa_error(FFA_BUSY);
2697 goto out;
2698 }
2699
2700 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2701 &api_page_pool);
2702
2703out:
2704 vm_unlock(&to_locked);
2705 return ret;
2706}
2707
2708struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2709 uint32_t fragment_length,
2710 ffa_vm_id_t sender_vm_id,
2711 struct vcpu *current)
2712{
2713 struct vm *from = current->vm;
2714 const void *from_msg;
2715 void *fragment_copy;
2716 struct ffa_value ret;
2717
2718 /* Sender ID MBZ at virtual instance. */
2719 if (sender_vm_id != 0) {
2720 return ffa_error(FFA_INVALID_PARAMETERS);
2721 }
2722
2723 /*
2724 * Check that the sender has configured its send buffer. If the TX
2725 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2726 * be safely accessed after releasing the lock since the TX mailbox
2727 * address can only be configured once.
2728 */
2729 sl_lock(&from->lock);
2730 from_msg = from->mailbox.send;
2731 sl_unlock(&from->lock);
2732
2733 if (from_msg == NULL) {
2734 return ffa_error(FFA_INVALID_PARAMETERS);
2735 }
2736
2737 /*
2738 * Copy the fragment to a fresh page from the memory pool. This prevents
2739 * the sender from changing it underneath us, and also lets us keep it
2740 * around in the share state table if needed.
2741 */
2742 if (fragment_length > HF_MAILBOX_SIZE ||
2743 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2744 dlog_verbose(
2745 "Fragment length %d larger than mailbox size %d.\n",
2746 fragment_length, HF_MAILBOX_SIZE);
2747 return ffa_error(FFA_INVALID_PARAMETERS);
2748 }
2749 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2750 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2751 0) {
2752 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2753 return ffa_error(FFA_INVALID_PARAMETERS);
2754 }
2755 fragment_copy = mpool_alloc(&api_page_pool);
2756 if (fragment_copy == NULL) {
2757 dlog_verbose("Failed to allocate fragment copy.\n");
2758 return ffa_error(FFA_NO_MEMORY);
2759 }
2760 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2761
2762 /*
2763 * Hafnium doesn't support fragmentation of memory retrieve requests
2764 * (because it doesn't support caller-specified mappings, so a request
2765 * will never be larger than a single page), so this must be part of a
2766 * memory send (i.e. donate, lend or share) request.
2767 *
2768 * We can tell from the handle whether the memory transaction is for the
2769 * TEE or not.
2770 */
2771 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2772 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2773 struct vm_locked from_locked = vm_lock(from);
2774
2775 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2776 fragment_length, handle,
2777 &api_page_pool);
2778 /*
2779 * `ffa_memory_send_continue` takes ownership of the
2780 * fragment_copy, so we don't need to free it here.
2781 */
2782 vm_unlock(&from_locked);
2783 } else {
2784 struct vm *to = vm_find(HF_TEE_VM_ID);
2785 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2786
2787 /*
2788 * The TEE RX buffer state is checked in
2789 * `ffa_memory_tee_send_continue` rather than here, as we need
2790 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2791 * than FFA_ERROR FFA_BUSY in case it is busy.
2792 */
2793
2794 ret = ffa_memory_tee_send_continue(
2795 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2796 fragment_length, handle, &api_page_pool);
2797 /*
2798 * `ffa_memory_tee_send_continue` takes ownership of the
2799 * fragment_copy, so we don't need to free it here.
2800 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002801
2802 vm_unlock(&vm_to_from_lock.vm1);
2803 vm_unlock(&vm_to_from_lock.vm2);
2804 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002805
2806 return ret;
2807}
Max Shvetsov40108e72020-08-27 12:39:50 +01002808
Olivier Deprezd614d322021-06-18 15:21:00 +02002809/**
2810 * Register an entry point for a vCPU in warm boot cases.
2811 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
2812 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002813struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2814 struct vcpu *current)
2815{
2816 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02002817 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01002818
Olivier Deprezd614d322021-06-18 15:21:00 +02002819 /*
2820 * Reject if interface is not supported at this FF-A instance
2821 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
2822 */
2823 if (!plat_ffa_is_secondary_ep_register_supported() ||
2824 current->vm->vcpu_count == 1) {
2825 return ffa_error(FFA_NOT_SUPPORTED);
2826 }
2827
2828 /*
2829 * No further check is made on the address validity
2830 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
2831 * from the VM or vCPU structure.
2832 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
2833 * For each SP [...] the Framework assumes that the same entry point
2834 * address is used for initializing any execution context during a
2835 * secondary cold boot.
2836 * If this function is invoked multiple times, then the entry point
2837 * address specified in the last valid invocation must be used by the
2838 * callee.
2839 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002840 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02002841 if (vm_locked.vm->initialized) {
2842 goto out;
2843 }
2844
Max Shvetsov40108e72020-08-27 12:39:50 +01002845 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02002846
2847 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2848
2849out:
Max Shvetsov40108e72020-08-27 12:39:50 +01002850 vm_unlock(&vm_locked);
2851
Olivier Deprezd614d322021-06-18 15:21:00 +02002852 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01002853}
J-Alvesa0f317d2021-06-09 13:31:59 +01002854
2855struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2856 ffa_vcpu_count_t vcpu_count,
2857 struct vcpu *current)
2858{
2859 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2860 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2861 vm_id);
2862 return ffa_error(FFA_NOT_SUPPORTED);
2863 }
2864
2865 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2866}
2867
2868struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2869 struct vcpu *current)
2870{
2871 /*
2872 * Validity of use of this interface is the same as for bitmap create.
2873 */
2874 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2875 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2876 vm_id);
2877 return ffa_error(FFA_NOT_SUPPORTED);
2878 }
2879
2880 return plat_ffa_notifications_bitmap_destroy(vm_id);
2881}
J-Alvesc003a7a2021-03-18 13:06:53 +00002882
2883struct ffa_value api_ffa_notification_update_bindings(
2884 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2885 ffa_notifications_bitmap_t notifications, bool is_bind,
2886 struct vcpu *current)
2887{
2888 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2889 struct vm_locked receiver_locked;
2890 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2891 const ffa_vm_id_t id_to_update =
2892 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2893 const ffa_vm_id_t id_to_validate =
2894 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2895
2896 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2897 receiver_vm_id)) {
2898 dlog_verbose("Invalid use of notifications bind interface.\n");
2899 return ffa_error(FFA_INVALID_PARAMETERS);
2900 }
2901
J-Alvesb15e9402021-09-08 11:44:42 +01002902 if (plat_ffa_notifications_update_bindings_forward(
2903 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2904 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01002905 return ret;
2906 }
2907
J-Alvesc003a7a2021-03-18 13:06:53 +00002908 if (notifications == 0U) {
2909 dlog_verbose("No notifications have been specified.\n");
2910 return ffa_error(FFA_INVALID_PARAMETERS);
2911 }
2912
2913 /**
2914 * This check assumes receiver is the current VM, and has been enforced
2915 * by 'plat_ffa_is_notifications_bind_valid'.
2916 */
2917 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2918
2919 if (receiver_locked.vm == NULL) {
2920 dlog_verbose("Receiver doesn't exist!\n");
2921 return ffa_error(FFA_DENIED);
2922 }
2923
J-Alves09ff9d82021-11-02 11:55:20 +00002924 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002925 dlog_verbose("Notifications are not enabled.\n");
2926 ret = ffa_error(FFA_NOT_SUPPORTED);
2927 goto out;
2928 }
2929
2930 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2931 vm_find(sender_vm_id) == NULL) {
2932 dlog_verbose("Sender VM does not exist!\n");
2933 ret = ffa_error(FFA_INVALID_PARAMETERS);
2934 goto out;
2935 }
2936
2937 /*
2938 * Can't bind/unbind notifications if at least one is bound to a
2939 * different sender.
2940 */
2941 if (!vm_notifications_validate_bound_sender(
2942 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2943 id_to_validate, notifications)) {
2944 dlog_verbose("Notifications are bound to other sender.\n");
2945 ret = ffa_error(FFA_DENIED);
2946 goto out;
2947 }
2948
2949 /**
2950 * Check if there is a pending notification within those specified in
2951 * the bitmap.
2952 */
2953 if (vm_are_notifications_pending(receiver_locked,
2954 plat_ffa_is_vm_id(sender_vm_id),
2955 notifications)) {
2956 dlog_verbose("Notifications within '%x' pending.\n",
2957 notifications);
2958 ret = ffa_error(FFA_DENIED);
2959 goto out;
2960 }
2961
2962 vm_notifications_update_bindings(
2963 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2964 notifications, is_per_vcpu && is_bind);
2965
2966out:
2967 vm_unlock(&receiver_locked);
2968 return ret;
2969}
J-Alvesaa79c012021-07-09 14:29:45 +01002970
2971struct ffa_value api_ffa_notification_set(
2972 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2973 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2974{
2975 struct ffa_value ret;
2976 struct vm_locked receiver_locked;
2977
2978 /*
2979 * Check if is per-vCPU or global, and extracting vCPU ID according
2980 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2981 */
2982 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2983 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2984
J-Alvesaa79c012021-07-09 14:29:45 +01002985 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2986 receiver_vm_id)) {
2987 dlog_verbose("Invalid use of notifications set interface.\n");
2988 return ffa_error(FFA_INVALID_PARAMETERS);
2989 }
2990
2991 if (notifications == 0U) {
2992 dlog_verbose("No notifications have been specified.\n");
2993 return ffa_error(FFA_INVALID_PARAMETERS);
2994 }
2995
J-Alvesde7bd2f2021-09-09 19:54:35 +01002996 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
2997 flags, notifications, &ret)) {
2998 return ret;
2999 }
3000
J-Alvesaa79c012021-07-09 14:29:45 +01003001 /*
3002 * This check assumes receiver is the current VM, and has been enforced
3003 * by 'plat_ffa_is_notification_set_valid'.
3004 */
3005 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3006
3007 if (receiver_locked.vm == NULL) {
3008 dlog_verbose("Receiver ID is not valid.\n");
3009 return ffa_error(FFA_INVALID_PARAMETERS);
3010 }
3011
J-Alves09ff9d82021-11-02 11:55:20 +00003012 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003013 dlog_verbose("Receiver's notifications not enabled.\n");
3014 ret = ffa_error(FFA_DENIED);
3015 goto out;
3016 }
3017
3018 /*
3019 * If notifications are not bound to the sender, they wouldn't be
3020 * enabled either for the receiver.
3021 */
3022 if (!vm_notifications_validate_binding(
3023 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3024 sender_vm_id, notifications, is_per_vcpu)) {
3025 dlog_verbose("Notifications bindings not valid.\n");
3026 ret = ffa_error(FFA_DENIED);
3027 goto out;
3028 }
3029
3030 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3031 dlog_verbose("Invalid VCPU ID!\n");
3032 ret = ffa_error(FFA_INVALID_PARAMETERS);
3033 goto out;
3034 }
3035
J-Alves7461ef22021-10-18 17:21:33 +01003036 /* Set notifications pending. */
J-Alves5a16c962022-03-25 12:32:51 +00003037 vm_notifications_partition_set_pending(
3038 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), notifications,
3039 vcpu_id, is_per_vcpu);
3040
J-Alvesaa79c012021-07-09 14:29:45 +01003041 dlog_verbose("Set the notifications: %x.\n", notifications);
3042
J-Alves13394022021-06-30 13:48:49 +01003043 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3044 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3045 vcpu_index(current));
3046 plat_ffa_sri_trigger_not_delayed(current->cpu);
3047 } else {
3048 plat_ffa_sri_state_set(DELAYED);
3049 }
J-Alvesaa79c012021-07-09 14:29:45 +01003050
J-Alves13394022021-06-30 13:48:49 +01003051 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003052out:
3053 vm_unlock(&receiver_locked);
3054
3055 return ret;
3056}
3057
3058static struct ffa_value api_ffa_notification_get_success_return(
3059 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3060 ffa_notifications_bitmap_t from_framework)
3061{
3062 return (struct ffa_value){
3063 .func = FFA_SUCCESS_32,
3064 .arg1 = 0U,
3065 .arg2 = (uint32_t)from_sp,
3066 .arg3 = (uint32_t)(from_sp >> 32),
3067 .arg4 = (uint32_t)from_vm,
3068 .arg5 = (uint32_t)(from_vm >> 32),
3069 .arg6 = (uint32_t)from_framework,
3070 .arg7 = (uint32_t)(from_framework >> 32),
3071 };
3072}
3073
3074struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3075 ffa_vcpu_index_t vcpu_id,
3076 uint32_t flags, struct vcpu *current)
3077{
J-Alves663682a2022-03-25 13:56:51 +00003078 ffa_notifications_bitmap_t framework_notifications = 0;
J-Alvesaa79c012021-07-09 14:29:45 +01003079 ffa_notifications_bitmap_t sp_notifications = 0;
3080 ffa_notifications_bitmap_t vm_notifications = 0;
3081 struct vm_locked receiver_locked;
3082 struct ffa_value ret;
J-Alvesfc95a302022-04-22 14:18:23 +01003083 const uint32_t flags_mbz = ~(FFA_NOTIFICATION_FLAG_BITMAP_HYP |
3084 FFA_NOTIFICATION_FLAG_BITMAP_SPM |
3085 FFA_NOTIFICATION_FLAG_BITMAP_SP |
3086 FFA_NOTIFICATION_FLAG_BITMAP_VM);
3087
3088 /* The FF-A v1.1 EAC0 specification states bits [31:4] Must Be Zero. */
3089 if ((flags & flags_mbz) != 0U) {
3090 dlog_verbose(
3091 "Invalid flags bit(s) set in notifications get. [31:4] "
3092 "MBZ(%x)\n",
3093 flags);
3094 return ffa_error(FFA_INVALID_PARAMETERS);
3095 }
J-Alvesaa79c012021-07-09 14:29:45 +01003096
3097 /*
J-Alvesfc95a302022-04-22 14:18:23 +01003098 * Following check should capture wrong uses of the interface,
3099 * depending on whether Hafnium is SPMC or hypervisor. On the
3100 * rest of the function it is assumed this condition is met.
J-Alvesaa79c012021-07-09 14:29:45 +01003101 */
J-Alvesfc95a302022-04-22 14:18:23 +01003102 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id,
3103 flags)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003104 dlog_verbose("Invalid use of notifications get interface.\n");
3105 return ffa_error(FFA_INVALID_PARAMETERS);
3106 }
3107
3108 /*
3109 * This check assumes receiver is the current VM, and has been enforced
3110 * by `plat_ffa_is_notifications_get_valid`.
3111 */
3112 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3113
3114 /*
3115 * `plat_ffa_is_notifications_get_valid` ensures following is never
3116 * true.
3117 */
3118 CHECK(receiver_locked.vm != NULL);
3119
3120 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3121 (receiver_locked.vm->vcpu_count != 1 &&
3122 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003123 dlog_verbose(
3124 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3125 vcpu_id, receiver_locked.vm->vcpu_count,
3126 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003127 ret = ffa_error(FFA_INVALID_PARAMETERS);
3128 goto out;
3129 }
3130
3131 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003132 if (!plat_ffa_notifications_get_from_sp(
3133 receiver_locked, vcpu_id, &sp_notifications,
3134 &ret)) {
3135 dlog_verbose("Failed to get notifications from sps.");
3136 goto out;
3137 }
J-Alvesaa79c012021-07-09 14:29:45 +01003138 }
3139
3140 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
J-Alves5136dda2022-03-25 12:26:38 +00003141 vm_notifications = vm_notifications_partition_get_pending(
J-Alvesaa79c012021-07-09 14:29:45 +01003142 receiver_locked, true, vcpu_id);
3143 }
3144
J-Alves663682a2022-03-25 13:56:51 +00003145 ret = api_ffa_notification_get_success_return(
3146 sp_notifications, vm_notifications, framework_notifications);
J-Alvesaa79c012021-07-09 14:29:45 +01003147
J-Alvesfe23ebe2021-10-13 16:07:07 +01003148 /*
3149 * If there are no more pending notifications, change `sri_state` to
3150 * handled.
3151 */
3152 if (vm_is_notifications_pending_count_zero()) {
3153 plat_ffa_sri_state_set(HANDLED);
3154 }
3155
J-Alves6e2abc62021-12-02 14:58:56 +00003156 if (!receiver_locked.vm->el0_partition &&
3157 !vm_are_global_notifications_pending(receiver_locked)) {
3158 vm_notifications_set_npi_injected(receiver_locked, false);
3159 }
3160
J-Alvesaa79c012021-07-09 14:29:45 +01003161out:
3162 vm_unlock(&receiver_locked);
3163
3164 return ret;
3165}
J-Alvesc8e8a222021-06-08 17:33:52 +01003166
3167/**
3168 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3169 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3170 */
3171static struct ffa_value api_ffa_notification_info_get_success_return(
3172 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003173 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003174{
3175 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3176
3177 /*
3178 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3179 * hold the list of ids.
3180 */
3181 memcpy_s(&ret.arg3,
3182 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3183 sizeof(ids[0]) * ids_count);
3184
3185 /*
3186 * According to the spec x2 should have:
3187 * - Bit flagging if there are more notifications pending;
3188 * - The total number of elements (i.e. total list size);
3189 * - The number of VCPU IDs within each VM specific list.
3190 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003191 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3192 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3193 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003194
3195 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3196 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3197
3198 for (unsigned int i = 0; i < lists_count; i++) {
3199 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3200 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3201 }
3202
3203 return ret;
3204}
3205
3206struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3207{
3208 /*
3209 * Following set of variables should be populated with the return info.
3210 * At a successfull handling of this interface, they should be used
3211 * to populate the 'ret' structure in accordance to the table 17.29
3212 * of the FF-A v1.1 Beta0 specification.
3213 */
3214 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3215 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3216 uint32_t lists_count = 0;
3217 uint32_t ids_count = 0;
3218 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003219 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003220
3221 /*
3222 * This interface can only be called at NS virtual/physical FF-A
3223 * instance by the endpoint implementing the primary scheduler and the
3224 * Hypervisor/OS kernel.
3225 * In the SPM, following check passes if call has been forwarded from
3226 * the hypervisor.
3227 */
3228 if (current->vm->id != HF_PRIMARY_VM_ID) {
3229 dlog_verbose(
3230 "Only the receiver's scheduler can use this "
3231 "interface\n");
3232 return ffa_error(FFA_NOT_SUPPORTED);
3233 }
3234
J-Alvesca058c22021-09-10 14:02:07 +01003235 /*
3236 * Forward call to the other world, and fill the arrays used to assemble
3237 * return.
3238 */
3239 plat_ffa_notification_info_get_forward(
3240 ids, &ids_count, lists_sizes, &lists_count,
3241 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3242
3243 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3244
J-Alvesc8e8a222021-06-08 17:33:52 +01003245 /* Get notifications' info from this world */
3246 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3247 ++index) {
3248 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3249
3250 list_is_full = vm_notifications_info_get(
3251 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3252 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3253
3254 vm_unlock(&vm_locked);
3255 }
3256
3257 if (!list_is_full) {
3258 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003259 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003260 ids, &ids_count, lists_sizes, &lists_count,
3261 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3262 }
3263
3264 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003265 dlog_verbose(
3266 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003267 result = ffa_error(FFA_NO_DATA);
3268 } else {
3269 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003270 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003271 }
3272
J-Alvesfe23ebe2021-10-13 16:07:07 +01003273 plat_ffa_sri_state_set(HANDLED);
3274
J-Alves13394022021-06-30 13:48:49 +01003275 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003276}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003277
3278struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3279{
3280 struct vm_locked vm_locked;
3281 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3282 bool mode_ret = false;
3283 uint32_t mode = 0;
3284
3285 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3286 return ffa_error(FFA_NOT_SUPPORTED);
3287 }
3288
3289 if (!(current->vm->el0_partition)) {
3290 return ffa_error(FFA_DENIED);
3291 }
3292
3293 vm_locked = vm_lock(current->vm);
3294
3295 /*
3296 * mm_get_mode is used to check if the given base_addr page is already
3297 * mapped. If the page is unmapped, return error. If the page is mapped
3298 * appropriate attributes are returned to the caller. Note that
3299 * mm_get_mode returns true if the address is in the valid VA range as
3300 * supported by the architecture and MMU configurations, as opposed to
3301 * whether a page is mapped or not. For a page to be known as mapped,
3302 * the API must return true AND the returned mode must not have
3303 * MM_MODE_INVALID set.
3304 */
3305 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3306 va_add(base_addr, PAGE_SIZE), &mode);
3307 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3308 ret = ffa_error(FFA_INVALID_PARAMETERS);
3309 goto out;
3310 }
3311
3312 /* No memory should be marked RWX */
3313 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3314 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3315
3316 /*
3317 * S-EL0 partitions are expected to have all their pages marked as
3318 * non-global.
3319 */
3320 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3321 (MM_MODE_NG | MM_MODE_USER));
3322
3323 if (mode & MM_MODE_W) {
3324 /* No memory should be writeable but not readable. */
3325 CHECK(mode & MM_MODE_R);
3326 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3327 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3328 } else if (mode & MM_MODE_R) {
3329 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3330 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3331 if (!(mode & MM_MODE_X)) {
3332 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3333 }
3334 }
3335out:
3336 vm_unlock(&vm_locked);
3337 return ret;
3338}
3339
3340struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3341 uint32_t mem_perm, struct vcpu *current)
3342{
3343 struct vm_locked vm_locked;
3344 struct ffa_value ret;
3345 bool mode_ret = false;
3346 uint32_t original_mode;
3347 uint32_t new_mode;
3348 struct mpool local_page_pool;
3349
3350 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3351 return ffa_error(FFA_NOT_SUPPORTED);
3352 }
3353
3354 if (!(current->vm->el0_partition)) {
3355 return ffa_error(FFA_DENIED);
3356 }
3357
3358 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3359 return ffa_error(FFA_INVALID_PARAMETERS);
3360 }
3361
3362 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3363 (mem_perm != FFA_MEM_PERM_RX)) {
3364 return ffa_error(FFA_INVALID_PARAMETERS);
3365 }
3366
3367 /*
3368 * Create a local pool so any freed memory can't be used by another
3369 * thread. This is to ensure the original mapping can be restored if any
3370 * stage of the process fails.
3371 */
3372 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3373
3374 vm_locked = vm_lock(current->vm);
3375
3376 /*
3377 * All regions accessible by the partition are mapped during boot. If we
3378 * cannot get a successful translation for the page range, the request
3379 * to change permissions is rejected.
3380 * mm_get_mode is used to check if the given address range is already
3381 * mapped. If the range is unmapped, return error. If the range is
3382 * mapped appropriate attributes are returned to the caller. Note that
3383 * mm_get_mode returns true if the address is in the valid VA range as
3384 * supported by the architecture and MMU configurations, as opposed to
3385 * whether a page is mapped or not. For a page to be known as mapped,
3386 * the API must return true AND the returned mode must not have
3387 * MM_MODE_INVALID set.
3388 */
3389
3390 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3391 va_add(base_addr, page_count * PAGE_SIZE),
3392 &original_mode);
3393 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3394 ret = ffa_error(FFA_INVALID_PARAMETERS);
3395 goto out;
3396 }
3397
3398 /* Device memory cannot be marked as executable */
3399 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3400 ret = ffa_error(FFA_INVALID_PARAMETERS);
3401 goto out;
3402 }
3403
3404 new_mode = MM_MODE_USER | MM_MODE_NG;
3405
3406 if (mem_perm == FFA_MEM_PERM_RW) {
3407 new_mode |= MM_MODE_R | MM_MODE_W;
3408 } else if (mem_perm == FFA_MEM_PERM_RX) {
3409 new_mode |= MM_MODE_R | MM_MODE_X;
3410 } else if (mem_perm == FFA_MEM_PERM_RO) {
3411 new_mode |= MM_MODE_R;
3412 }
3413
3414 /*
3415 * Safe to re-map memory, since we know the requested permissions are
3416 * valid, and the memory requested to be re-mapped is also valid.
3417 */
3418 if (!mm_identity_prepare(
3419 &vm_locked.vm->ptable, pa_from_va(base_addr),
3420 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3421 new_mode, &local_page_pool)) {
3422 /*
3423 * Defrag the table into the local page pool.
3424 * mm_identity_prepare could have allocated or freed pages to
3425 * split blocks or tables etc.
3426 */
3427 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3428
3429 /*
3430 * Guaranteed to succeed mapping with old mode since the mapping
3431 * with old mode already existed and we have a local page pool
3432 * that should have sufficient memory to go back to the original
3433 * state.
3434 */
3435 CHECK(mm_identity_prepare(
3436 &vm_locked.vm->ptable, pa_from_va(base_addr),
3437 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3438 original_mode, &local_page_pool));
3439 mm_identity_commit(
3440 &vm_locked.vm->ptable, pa_from_va(base_addr),
3441 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3442 original_mode, &local_page_pool);
3443
3444 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3445 ret = ffa_error(FFA_NO_MEMORY);
3446 goto out;
3447 }
3448
3449 mm_identity_commit(
3450 &vm_locked.vm->ptable, pa_from_va(base_addr),
3451 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3452 &local_page_pool);
3453
3454 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3455
3456out:
3457 mpool_fini(&local_page_pool);
3458 vm_unlock(&vm_locked);
3459
3460 return ret;
3461}