blob: b232a1a7032b120fcafa3170fc2b538dfab70a38 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +000012#include "hf/arch/ffa.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000016#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020017#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018
Andrew Scull877ae4b2019-07-02 12:52:33 +010019#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000020#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
22#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010024#include "hf/plat/console.h"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010025#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000026#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010027#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010028#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/vm.h"
30
Andrew Scullf35a5c92018-08-07 18:09:46 +010031#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010033
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000034static_assert(sizeof(struct ffa_partition_info_v1_0) == 8,
Fuad Tabbae4efcc32020-07-16 15:37:27 +010035 "Partition information descriptor size doesn't match the one in "
36 "the FF-A 1.0 EAC specification, Table 82.");
Daniel Boulby1ddb3d72021-12-16 18:16:50 +000037static_assert(sizeof(struct ffa_partition_info) == 24,
38 "Partition information descriptor size doesn't match the one in "
39 "the FF-A 1.1 BETA0 EAC specification, Table 13.34.");
Fuad Tabbae4efcc32020-07-16 15:37:27 +010040
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000041/*
42 * To eliminate the risk of deadlocks, we define a partial order for the
43 * acquisition of locks held concurrently by the same physical CPU. Our current
44 * ordering requirements are as follows:
45 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010046 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000047 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010048 * Locks of the same kind require the lock of lowest address to be locked first,
49 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000050 */
51
Andrew Scullaa039b32018-10-04 15:02:26 +010052static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010053 "Currently, a page is mapped for the send and receive buffers so "
54 "the maximum request is the size of a page.");
55
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
57 "The page pool entry size must be at least as big as the mailbox "
58 "size, so that memory region descriptors can be copied from the "
59 "mailbox for memory sharing.");
60
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000061static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062
63/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000064 * Initialises the API page pool by taking ownership of the contents of the
65 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000066 */
67void api_init(struct mpool *ppool)
68{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000069 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000070}
71
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010072/**
J-Alvesad6a0432021-04-09 16:06:21 +010073 * Get target VM vCPU:
74 * If VM is UP then return first vCPU.
75 * If VM is MP then return vCPU whose index matches current CPU index.
76 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050077struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
J-Alvesad6a0432021-04-09 16:06:21 +010078{
79 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
80 struct vcpu *vcpu = NULL;
81
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050082 CHECK((vm != NULL) && (current != NULL));
83
J-Alvesad6a0432021-04-09 16:06:21 +010084 if (vm->vcpu_count == 1) {
85 vcpu = vm_get_vcpu(vm, 0);
86 } else if (current_cpu_index < vm->vcpu_count) {
87 vcpu = vm_get_vcpu(vm, current_cpu_index);
88 }
89
90 return vcpu;
91}
92
93/**
J-Alvesfe7f7372020-11-09 11:32:12 +000094 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
95 * is given as argument of the function.
96 *
97 * Called to change the context between SPs for direct messaging (when Hafnium
98 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
99 *
100 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +0100101 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +0000102 * - MP with pinned Execution Contexts.
103 */
104static struct vcpu *api_switch_to_vm(struct vcpu *current,
105 struct ffa_value to_ret,
106 enum vcpu_state vcpu_state,
107 ffa_vm_id_t to_id)
108{
109 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100110 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000111
112 CHECK(next != NULL);
113
114 /* Set the return value for the target VM. */
115 arch_regs_set_retval(&next->regs, to_ret);
116
117 /* Set the current vCPU state. */
118 sl_lock(&current->lock);
119 current->state = vcpu_state;
120 sl_unlock(&current->lock);
121
122 return next;
123}
124
125/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000126 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100127 *
128 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100130 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500131struct vcpu *api_switch_to_primary(struct vcpu *current,
132 struct ffa_value primary_ret,
133 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100134{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000135 /*
136 * If the secondary is blocked but has a timer running, sleep until the
137 * timer fires rather than indefinitely.
138 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100139 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100140 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
141 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100142 if (arch_timer_enabled_current()) {
143 uint64_t remaining_ns =
144 arch_timer_remaining_ns_current();
145
146 if (remaining_ns == 0) {
147 /*
148 * Timer is pending, so the current vCPU should
149 * be run again right away.
150 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100151 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100152 /*
153 * primary_ret.arg1 should already be set to the
154 * current VM ID and vCPU ID.
155 */
156 primary_ret.arg2 = 0;
157 } else {
158 primary_ret.arg2 = remaining_ns;
159 }
160 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100161 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100162 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000163 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100164 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000165
166 default:
167 /* Do nothing. */
168 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000169 }
170
J-Alvesfe7f7372020-11-09 11:32:12 +0000171 return api_switch_to_vm(current, primary_ret, secondary_state,
172 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100173}
174
175/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200176 * Choose next vCPU to run to be the counterpart vCPU in the other
177 * world (run the normal world if currently running in the secure
178 * world). Set current vCPU state to the given vcpu_state parameter.
179 * Set FF-A return values to the target vCPU in the other world.
180 *
181 * Called in context of a direct message response from a secure
182 * partition to a VM.
183 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100184struct vcpu *api_switch_to_other_world(struct vcpu *current,
185 struct ffa_value other_world_ret,
186 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200187{
J-Alvesfe7f7372020-11-09 11:32:12 +0000188 return api_switch_to_vm(current, other_world_ret, vcpu_state,
189 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200190}
191
192/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100193 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
194 * registers the `from` VM to be notified when it becomes available.
195 */
196static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
197{
198 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
199 to.vm->mailbox.recv == NULL) {
200 /*
201 * Fail if the receiver isn't currently ready to receive data,
202 * setting up for notification if requested.
203 */
204 if (notify) {
205 struct wait_entry *entry =
206 vm_get_wait_entry(from, to.vm->id);
207
208 /* Append waiter only if it's not there yet. */
209 if (list_empty(&entry->wait_links)) {
210 list_append(&to.vm->mailbox.waiter_list,
211 &entry->wait_links);
212 }
213 }
214
215 return true;
216 }
217
218 return false;
219}
220
221/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000222 * Returns true if the given vCPU is executing in context of an
223 * FFA_MSG_SEND_DIRECT_REQ invocation.
224 */
225static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
226{
227 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
228}
229
230/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100231 * Returns true if the VM owning the given vCPU is supporting managed exit and
232 * the vCPU is currently processing a managed exit.
233 */
234static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
235{
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100236 return (plat_ffa_vm_managed_exit_supported(vcpu_locked.vcpu->vm) &&
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100237 vcpu_locked.vcpu->processing_managed_exit);
238}
239
240/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000241 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000242 */
243struct vcpu *api_preempt(struct vcpu *current)
244{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245 struct ffa_value ret = {
246 .func = FFA_INTERRUPT_32,
247 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000248 };
249
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500250 return api_switch_to_primary(current, ret, VCPU_STATE_PREEMPTED);
Andrew Scull33fecd32019-01-08 14:48:27 +0000251}
252
253/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000254 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000255 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100256 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100257struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100258{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100259 struct ffa_value ret = {
260 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
261 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100262 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000263
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000264 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100265 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100266}
267
268/**
Andrew Walbran33645652019-04-15 12:29:31 +0100269 * Puts the current vCPU in off mode, and returns to the primary VM.
270 */
271struct vcpu *api_vcpu_off(struct vcpu *current)
272{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100273 struct ffa_value ret = {
274 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
275 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100276 };
277
278 /*
279 * Disable the timer, so the scheduler doesn't get told to call back
280 * based on it.
281 */
282 arch_timer_disable_current();
283
284 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
285}
286
287/**
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500288 * The current vCPU is blocked on some resource and needs to relinquish
289 * control back to the execution context of the endpoint that originally
290 * allocated cycles to it.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000291 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000292struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000293{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
295 struct vcpu_locked current_locked;
296 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000297
298 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000299 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000300 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000301 }
302
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000303 current_locked = vcpu_lock(current);
304 is_direct_request_ongoing =
305 is_ffa_direct_msg_request_ongoing(current_locked);
306 vcpu_unlock(&current_locked);
307
308 if (is_direct_request_ongoing) {
309 return ffa_error(FFA_DENIED);
310 }
311
312 *next = api_switch_to_primary(
313 current,
314 (struct ffa_value){.func = FFA_YIELD_32,
315 .arg1 = ffa_vm_vcpu(current->vm->id,
316 vcpu_index(current))},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500317 VCPU_STATE_BLOCKED);
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000318
319 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000320}
321
322/**
Andrew Walbran33645652019-04-15 12:29:31 +0100323 * Switches to the primary so that it can switch to the target, or kick it if it
324 * is already running on a different physical CPU.
325 */
326struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
327{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 struct ffa_value ret = {
329 .func = HF_FFA_RUN_WAKE_UP,
330 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
331 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100332 };
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500333 return api_switch_to_primary(current, ret, VCPU_STATE_BLOCKED);
Andrew Walbran33645652019-04-15 12:29:31 +0100334}
335
336/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000337 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000338 */
339struct vcpu *api_abort(struct vcpu *current)
340{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000342
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100343 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000344 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000345
346 if (current->vm->id == HF_PRIMARY_VM_ID) {
347 /* TODO: what to do when the primary aborts? */
348 for (;;) {
349 /* Do nothing. */
350 }
351 }
352
353 atomic_store_explicit(&current->vm->aborting, true,
354 memory_order_relaxed);
355
356 /* TODO: free resources once all vCPUs abort. */
357
Andrew Sculld6ee1102019-04-05 22:12:42 +0100358 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000359}
360
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000361/*
362 * Format the partition info descriptors according to the version supported
363 * by the endpoint and return the size of the array created.
364 */
365static struct ffa_value send_versioned_partition_info_descriptors(
Daniel Boulby191b5f02022-02-17 17:26:14 +0000366 struct vm_locked vm_locked, struct ffa_partition_info *partitions,
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000367 uint32_t vm_count)
368{
Daniel Boulby191b5f02022-02-17 17:26:14 +0000369 struct vm *vm = vm_locked.vm;
370 uint32_t version = vm->ffa_version;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000371 uint32_t size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000372
Daniel Boulby191b5f02022-02-17 17:26:14 +0000373 if (msg_receiver_busy(vm_locked, NULL, false)) {
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000374 /*
375 * Can't retrieve memory information if the mailbox is not
376 * available.
377 */
378 dlog_verbose("RX buffer not ready.\n");
Daniel Boulby191b5f02022-02-17 17:26:14 +0000379 return ffa_error(FFA_BUSY);
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000380 }
381
Daniel Boulby191b5f02022-02-17 17:26:14 +0000382 if (version == MAKE_FFA_VERSION(1, 0)) {
383 struct ffa_partition_info_v1_0 *recv_mailbox = vm->mailbox.recv;
384
385 size = sizeof(struct ffa_partition_info_v1_0) * vm_count;
386 if (size > HF_MAILBOX_SIZE) {
387 dlog_error(
388 "Partition information does not fit in the "
389 "VM's RX "
390 "buffer.\n");
391 return ffa_error(FFA_NO_MEMORY);
392 }
393
394 for (uint32_t i = 0; i < vm_count; i++) {
395 /*
396 * Populate the VM's RX buffer with the partition
397 * information.
398 */
399 recv_mailbox[i].vm_id = partitions[i].vm_id;
400 recv_mailbox[i].vcpu_count = partitions[i].vcpu_count;
401 recv_mailbox[i].properties = partitions[i].properties;
402 }
403
404 } else {
405 size = sizeof(partitions[0]) * vm_count;
406 if (size > HF_MAILBOX_SIZE) {
407 dlog_error(
408 "Partition information does not fit in the "
409 "VM's RX "
410 "buffer.\n");
411 return ffa_error(FFA_NO_MEMORY);
412 }
413
414 /* Populate the VM's RX buffer with the partition information.
415 */
416 memcpy_s(vm->mailbox.recv, HF_MAILBOX_SIZE, partitions, size);
417 }
418
419 vm->mailbox.recv_size = size;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000420
421 /* Sender is Hypervisor in the normal world (TEE in secure world). */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000422 vm->mailbox.recv_sender = HF_VM_ID_BASE;
423 vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
424 vm->mailbox.state = MAILBOX_STATE_READ;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000425
426 /* Return the count of partition information descriptors in w2. */
Daniel Boulby191b5f02022-02-17 17:26:14 +0000427 return (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000428}
429
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100430struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000431 const struct ffa_uuid *uuid,
432 const uint32_t flags)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100433{
434 struct vm *current_vm = current->vm;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100435 ffa_vm_count_t vm_count = 0;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000436 bool count_flag = (flags && FFA_PARTITION_COUNT_FLAG_MASK) ==
437 FFA_PARTITION_COUNT_FLAG;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100438 bool uuid_is_null = ffa_uuid_is_null(uuid);
Olivier Depreze562e542020-06-11 17:31:54 +0200439 struct ffa_partition_info partitions[2 * MAX_VMS];
Daniel Boulby191b5f02022-02-17 17:26:14 +0000440 struct vm_locked vm_locked;
441 struct ffa_value ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100442
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000443 /* Bits 31:1 Must Be Zero */
444 if ((flags & ~FFA_PARTITION_COUNT_FLAG) != 0) {
445 return ffa_error(FFA_INVALID_PARAMETERS);
446 }
447
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100448 /*
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000449 * No need to count if we are returning the number of paritions as we
450 * already know this.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100451 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000452 if (uuid_is_null && count_flag) {
453 vm_count = vm_get_count();
454 } else {
455 /*
456 * Iterate through the VMs to find the ones with a matching
457 * UUID. A Null UUID retrieves information for all VMs.
458 */
459 for (uint16_t index = 0; index < vm_get_count(); ++index) {
460 struct vm *vm = vm_find_index(index);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100461
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000462 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
463 uint16_t array_index = vm_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100464
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000465 ++vm_count;
466 if (count_flag) {
467 continue;
468 }
469
470 partitions[array_index].vm_id = vm->id;
471 partitions[array_index].vcpu_count =
472 vm->vcpu_count;
473 partitions[array_index].properties =
474 plat_ffa_partition_properties(
475 current_vm->id, vm);
476 partitions[array_index].properties |=
477 vm_are_notifications_enabled(vm)
478 ? FFA_PARTITION_NOTIFICATION
479 : 0;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000480 partitions[array_index].uuid = vm->uuid;
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000481 }
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100482 }
483 }
484
Olivier Depreze562e542020-06-11 17:31:54 +0200485 /* If UUID is Null vm_count must not be zero at this stage. */
486 CHECK(!uuid_is_null || vm_count != 0);
487
488 /*
489 * When running the Hypervisor:
490 * - If UUID is Null the Hypervisor forwards the query to the SPMC for
491 * it to fill with secure partitions information.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000492 * - If UUID is non-Null vm_count may be zero because the UUID matches
Olivier Depreze562e542020-06-11 17:31:54 +0200493 * a secure partition and the query is forwarded to the SPMC.
494 * When running the SPMC:
495 * - If UUID is non-Null and vm_count is zero it means there is no such
496 * partition identified in the system.
497 */
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000498 plat_ffa_partition_info_get_forward(uuid, flags, partitions, &vm_count);
Olivier Depreze562e542020-06-11 17:31:54 +0200499
500 /*
501 * Unrecognized UUID: does not match any of the VMs (or SPs)
502 * and is not Null.
503 */
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100504 if (vm_count == 0) {
505 return ffa_error(FFA_INVALID_PARAMETERS);
506 }
507
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000508 /*
509 * If the count flag is set we don't need to return the partition info
510 * descriptors.
511 */
512 if (count_flag) {
513 return (struct ffa_value){.func = FFA_SUCCESS_32,
514 .arg2 = vm_count};
515 }
516
Daniel Boulby191b5f02022-02-17 17:26:14 +0000517 vm_locked = vm_lock(current_vm);
518 ret = send_versioned_partition_info_descriptors(vm_locked, partitions,
519 vm_count);
520 vm_unlock(&vm_locked);
521 return ret;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100522}
523
Andrew Scull9726c252019-01-23 13:44:19 +0000524/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000525 * Returns the ID of the VM.
526 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000528{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100529 return (struct ffa_value){.func = FFA_SUCCESS_32,
530 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000531}
532
533/**
Olivier Deprez421677d2021-06-18 12:18:53 +0200534 * Returns the SPMC FF-A ID at NS virtual/physical and secure virtual
535 * FF-A instances.
536 * DEN0077A FF-A v1.1 Beta0 section 13.9 FFA_SPM_ID_GET.
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000537 */
538struct ffa_value api_ffa_spm_id_get(void)
539{
J-Alves3829fc02021-03-18 12:49:18 +0000540#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
541 /*
542 * Return the SPMC ID that was fetched during FF-A
543 * initialization.
544 */
545 return (struct ffa_value){.func = FFA_SUCCESS_32,
546 .arg2 = arch_ffa_spmc_id_get()};
547#else
548 return ffa_error(FFA_NOT_SUPPORTED);
549#endif
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +0000550}
551
552/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000553 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000554 * function to indicate that register state for the given vCPU has been saved
555 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000556 */
557void api_regs_state_saved(struct vcpu *vcpu)
558{
559 sl_lock(&vcpu->lock);
560 vcpu->regs_available = true;
561 sl_unlock(&vcpu->lock);
562}
563
564/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000565 * Retrieves the next waiter and removes it from the wait list if the VM's
566 * mailbox is in a writable state.
567 */
568static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
569{
570 struct wait_entry *entry;
571 struct vm *vm = locked_vm.vm;
572
Andrew Sculld6ee1102019-04-05 22:12:42 +0100573 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000574 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
575 /* The mailbox is not writable or there are no waiters. */
576 return NULL;
577 }
578
579 /* Remove waiter from the wait list. */
580 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
581 wait_links);
582 list_remove(&entry->wait_links);
583 return entry;
584}
585
586/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000587 * Assuming that the arguments have already been checked by the caller, injects
588 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
589 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
590 * is next run, which is up to the scheduler.
591 *
592 * Returns:
593 * - 0 on success if no further action is needed.
594 * - 1 if it was called by the primary VM and the primary VM now needs to wake
595 * up or kick the target vCPU.
596 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100597int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
598 uint32_t intid, struct vcpu *current,
599 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000600{
Manish Pandey35e452f2021-02-18 21:36:34 +0000601 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000602 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000603 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
604 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000605 int64_t ret = 0;
606
Andrew Walbran508e63c2018-12-20 17:02:37 +0000607 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000608 * We only need to change state and (maybe) trigger a virtual interrupt
609 * if it is enabled and was not previously pending. Otherwise we can
610 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000611 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000612 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
613 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000614 intid_mask)) {
615 goto out;
616 }
617
618 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000619 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
620 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
621 vcpu_irq_count_increment(target_locked);
622 } else {
623 vcpu_fiq_count_increment(target_locked);
624 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000625
626 /*
627 * Only need to update state if there was not already an
628 * interrupt enabled and pending.
629 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000630 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000631 goto out;
632 }
633
Andrew Walbran508e63c2018-12-20 17:02:37 +0000634 if (current->vm->id == HF_PRIMARY_VM_ID) {
635 /*
636 * If the call came from the primary VM, let it know that it
637 * should run or kick the target vCPU.
638 */
639 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000640 } else if (current != target_vcpu && next != NULL) {
641 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000642 }
643
644out:
645 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000646 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000647
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200648 return ret;
649}
650
651/* Wrapper to internal_interrupt_inject with locking of target vCPU */
652static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
653 uint32_t intid, struct vcpu *current,
654 struct vcpu **next)
655{
656 int64_t ret;
657 struct vcpu_locked target_locked;
658
659 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100660 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200661 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000662
663 return ret;
664}
665
666/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100667 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
668 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100669 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100670static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100671{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000672 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100673 case FFA_MSG_SEND_32:
674 return (struct ffa_value){
675 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000676 .arg1 = (receiver->mailbox.recv_sender << 16) |
677 receiver->id,
678 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000679 default:
680 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000681 dlog_error("Tried to return an invalid message function %#x\n",
682 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100683 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000684 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100685}
686
Madhukar Pappireddy5522c672021-12-17 16:35:51 -0600687struct ffa_value api_ffa_msg_wait(struct vcpu *current, struct vcpu **next,
688 struct ffa_value *args)
689{
690 struct ffa_value ret;
691
692 if (args->arg1 != 0U || args->arg2 != 0U || args->arg3 != 0U ||
693 args->arg4 != 0U || args->arg5 != 0U || args->arg6 != 0U ||
694 args->arg7 != 0U) {
695 return ffa_error(FFA_INVALID_PARAMETERS);
696 }
697
698 if (plat_ffa_msg_wait_prepare(current, next, &ret)) {
699 return ret;
700 }
701
702 return api_ffa_msg_recv(true, current, next);
703}
704
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100705/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000706 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000707 * value needs to be forced onto the vCPU.
708 */
J-Alves6e2abc62021-12-02 14:58:56 +0000709static bool api_vcpu_prepare_run(struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000711{
Max Shvetsov40108e72020-08-27 12:39:50 +0100712 struct vcpu_locked vcpu_locked;
713 struct vm_locked vm_locked;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000714 bool ret;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500715 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
J-Alves6e2abc62021-12-02 14:58:56 +0000716 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000717
Andrew Scullb06d1752019-02-04 10:15:48 +0000718 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000719 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000720 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100721 * The VM lock is not needed in the common case so it must only be taken
722 * when it is going to be needed. This ensures there are no inter-vCPU
723 * dependencies in the common run case meaning the sensitive context
724 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000725 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100726 vcpu_locked = vcpu_lock(vcpu);
727
728#if SECURE_WORLD == 1
J-Alves7ac49052022-02-08 17:20:53 +0000729 bool is_vcpu_reset_and_start = vcpu_secondary_reset_and_start(
730 vcpu_locked, vcpu->vm->secondary_ep, 0);
731 if (is_vcpu_reset_and_start) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100732 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
733 __func__, vcpu->vm->id, current->cpu->id);
734 }
735
736#endif
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000737 /* The VM needs to be locked to deliver mailbox messages. */
J-Alves6e2abc62021-12-02 14:58:56 +0000738 need_vm_lock = vcpu->state == VCPU_STATE_WAITING ||
739 (!vcpu->vm->el0_partition &&
740 (vcpu->state == VCPU_STATE_BLOCKED_INTERRUPT ||
741 vcpu->state == VCPU_STATE_BLOCKED ||
742 vcpu->state == VCPU_STATE_PREEMPTED));
743
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000744 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100745 vcpu_unlock(&vcpu_locked);
746 vm_locked = vm_lock(vcpu->vm);
747 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000748 }
749
750 /*
751 * If the vCPU is already running somewhere then we can't run it here
752 * simultaneously. While it is actually running then the state should be
753 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
754 * stops running but while Hafnium is in the process of switching back
755 * to the primary there will be a brief period while the state has been
756 * updated but `regs_available` is still false (until
757 * `api_regs_state_saved` is called). We can't start running it again
758 * until this has finished, so count this state as still running for the
759 * purposes of this check.
760 */
761 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
762 /*
763 * vCPU is running on another pCPU.
764 *
765 * It's okay not to return the sleep duration here because the
766 * other physical CPU that is currently running this vCPU will
767 * return the sleep duration if needed.
768 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100769 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000770 ret = false;
771 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000772 }
Andrew Scull9726c252019-01-23 13:44:19 +0000773
774 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100775 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100776 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000777 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100778 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000779 }
780 ret = false;
781 goto out;
782 }
783
Andrew Walbran508e63c2018-12-20 17:02:37 +0000784 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100785 case VCPU_STATE_RUNNING:
786 case VCPU_STATE_OFF:
787 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000788 ret = false;
789 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000790
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500791 case VCPU_STATE_WAITING:
792 /*
793 * An initial FFA_RUN is necessary for secondary VM/SP to reach
794 * the message wait loop.
795 */
796 if (!vcpu->is_bootstrapped) {
797 vcpu->is_bootstrapped = true;
798 break;
799 }
800
J-Alves6e2abc62021-12-02 14:58:56 +0000801 assert(need_vm_lock == true);
802 if (!vm_locked.vm->el0_partition &&
803 plat_ffa_inject_notification_pending_interrupt(
804 vcpu_locked, current, vm_locked)) {
805 break;
806 }
807
Andrew Scullb06d1752019-02-04 10:15:48 +0000808 /*
809 * A pending message allows the vCPU to run so the message can
810 * be delivered directly.
811 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100812 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100813 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100815 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000816 break;
817 }
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500818
819 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
820 break;
821 }
822
823 if (arch_timer_enabled(&vcpu->regs)) {
824 timer_remaining_ns =
825 arch_timer_remaining_ns(&vcpu->regs);
826 if (timer_remaining_ns == 0) {
827 break;
828 }
829 } else {
830 dlog_verbose("Timer disabled\n");
831 }
832 run_ret->func = FFA_MSG_WAIT_32;
833 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
834 run_ret->arg2 = timer_remaining_ns;
835 ret = false;
836 goto out;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100837 case VCPU_STATE_BLOCKED_INTERRUPT:
J-Alves6e2abc62021-12-02 14:58:56 +0000838 if (need_vm_lock &&
839 plat_ffa_inject_notification_pending_interrupt(
840 vcpu_locked, current, vm_locked)) {
841 assert(vcpu_interrupt_count_get(vcpu_locked) > 0);
842 break;
843 }
844
Andrew Scullb06d1752019-02-04 10:15:48 +0000845 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000846 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000847 break;
848 }
849
Andrew Walbran508e63c2018-12-20 17:02:37 +0000850 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100851 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000852 arch_timer_remaining_ns(&vcpu->regs);
853
854 /*
855 * The timer expired so allow the interrupt to be
856 * delivered.
857 */
858 if (timer_remaining_ns == 0) {
859 break;
860 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100861 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000862
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100863 /*
864 * The vCPU is not ready to run, return the appropriate code to
865 * the primary which called vcpu_run.
866 */
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500867 run_ret->func = HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100868 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
869 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000870
871 ret = false;
872 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000873
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500874 case VCPU_STATE_BLOCKED:
875 /* A blocked vCPU is run unconditionally. Fall through. */
876 case VCPU_STATE_PREEMPTED:
J-Alves6e2abc62021-12-02 14:58:56 +0000877 /* Check NPI is to be injected here. */
878 if (need_vm_lock) {
879 plat_ffa_inject_notification_pending_interrupt(
880 vcpu_locked, current, vm_locked);
881 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000882 break;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500883 default:
884 /*
885 * Execution not expected to reach here. Deny the request
886 * gracefully.
887 */
888 *run_ret = ffa_error(FFA_DENIED);
889 ret = false;
890 goto out;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000891 }
892
Andrew Scullb06d1752019-02-04 10:15:48 +0000893 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000894 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100895 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000896
J-Alves7ac49052022-02-08 17:20:53 +0000897#if SECURE_WORLD == 1
898 /* Set the designated GP register with the vCPU ID. */
899 if (is_vcpu_reset_and_start) {
900 vcpu_set_phys_core_idx(vcpu_locked.vcpu);
901 }
902#endif
903
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000904 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000905 * Mark the registers as unavailable now that we're about to reflect
906 * them onto the real registers. This will also prevent another physical
907 * CPU from trying to read these registers.
908 */
909 vcpu->regs_available = false;
910
911 ret = true;
912
913out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100914 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000915 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100916 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000917 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000918 return ret;
919}
920
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500922 struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100923{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100924 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100925 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100927
Raghu Krishnamurthy048d63f2021-12-11 12:45:41 -0800928 if (!plat_ffa_run_checks(current, vm_id, vcpu_idx, &ret, next)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500929 return ret;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100930 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100931
Raghu Krishnamurthy62f97a72021-07-27 02:14:59 -0700932 if (plat_ffa_run_forward(vm_id, vcpu_idx, &ret)) {
933 return ret;
934 }
935
Andrew Scull19503262018-09-20 14:48:39 +0100936 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100937 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100938 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100939 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100940 }
941
Fuad Tabbaed294af2019-12-20 10:43:01 +0000942 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100943 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100944 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100945 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100946
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000947 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100948 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000949 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000950 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100951 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000952
Andrew Walbran508e63c2018-12-20 17:02:37 +0000953 /*
954 * Inject timer interrupt if timer has expired. It's safe to access
955 * vcpu->regs here because api_vcpu_prepare_run already made sure that
956 * regs_available was true (and then set it to false) before returning
957 * true.
958 */
959 if (arch_timer_pending(&vcpu->regs)) {
960 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100961 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
962 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000963
964 /*
965 * Set the mask bit so the hardware interrupt doesn't fire
966 * again. Ideally we wouldn't do this because it affects what
967 * the secondary vCPU sees, but if we don't then we end up with
968 * a loop of the interrupt firing each time we try to return to
969 * the secondary vCPU.
970 */
971 arch_timer_mask(&vcpu->regs);
972 }
973
Fuad Tabbaed294af2019-12-20 10:43:01 +0000974 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000975 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000976
Andrew Scull33fecd32019-01-08 14:48:27 +0000977 /*
978 * Set a placeholder return code to the scheduler. This will be
979 * overwritten when the switch back to the primary occurs.
980 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 ret.func = FFA_INTERRUPT_32;
982 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100983 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000984
Andrew Scull6d2db332018-10-10 15:28:17 +0100985out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100986 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100987}
988
989/**
Andrew Scull81e85092018-12-12 12:56:20 +0000990 * Check that the mode indicates memory that is valid, owned and exclusive.
991 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100992static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000993{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100994 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
995 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000996}
997
998/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100999 * Determines the value to be returned by api_ffa_rxtx_map and
1000 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
1001 * there are waiters, it also switches back to the primary VM for it to wake
1002 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001003 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
1005 struct vcpu *current,
1006 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001007{
1008 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001009
1010 if (list_empty(&vm->mailbox.waiter_list)) {
1011 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001012 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001013 }
1014
1015 if (vm->id == HF_PRIMARY_VM_ID) {
1016 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001018 }
1019
1020 /*
1021 * Switch back to the primary VM, informing it that there are waiters
1022 * that need to be notified.
1023 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001024 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001025 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001026 VCPU_STATE_WAITING);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001027
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001028 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001029}
1030
1031/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001032 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +01001033 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001034static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
1035 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +01001036 paddr_t pa_send_begin, paddr_t pa_send_end,
1037 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001038 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +01001039 struct mpool *local_page_pool)
1040{
1041 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +01001042
1043 /* Map the send page as read-only in the hypervisor address space. */
1044 vm_locked.vm->mailbox.send =
1045 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001046 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001047 if (!vm_locked.vm->mailbox.send) {
1048 /* TODO: partial defrag of failed range. */
1049 /* Recover any memory consumed in failed mapping. */
1050 mm_defrag(mm_stage1_locked, local_page_pool);
1051 goto fail;
1052 }
1053
1054 /*
1055 * Map the receive page as writable in the hypervisor address space. On
1056 * failure, unmap the send page before returning.
1057 */
1058 vm_locked.vm->mailbox.recv =
1059 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001060 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001061 if (!vm_locked.vm->mailbox.recv) {
1062 /* TODO: partial defrag of failed range. */
1063 /* Recover any memory consumed in failed mapping. */
1064 mm_defrag(mm_stage1_locked, local_page_pool);
1065 goto fail_undo_send;
1066 }
1067
1068 ret = true;
1069 goto out;
1070
1071 /*
1072 * The following mappings will not require more memory than is available
1073 * in the local pool.
1074 */
1075fail_undo_send:
1076 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +01001077 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
1078 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +01001079
1080fail:
1081 ret = false;
1082
1083out:
Andrew Sculle1322792019-07-01 17:46:10 +01001084 return ret;
1085}
1086
1087/**
Manish Pandeyd34f8892020-06-19 17:41:07 +01001088 * Sanity checks and configures the send and receive pages in the VM stage-2
1089 * and hypervisor stage-1 page tables.
1090 *
1091 * Returns:
1092 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001093 * aligned, are the same or have invalid attributes.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001094 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
1095 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001096 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Manish Pandeyd34f8892020-06-19 17:41:07 +01001097 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +01001098 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001099
1100struct ffa_value api_vm_configure_pages(
1101 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
1102 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
1103 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +01001104{
Manish Pandeyd34f8892020-06-19 17:41:07 +01001105 struct ffa_value ret;
1106 paddr_t pa_send_begin;
1107 paddr_t pa_send_end;
1108 paddr_t pa_recv_begin;
1109 paddr_t pa_recv_end;
1110 uint32_t orig_send_mode;
1111 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +02001112 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001113
1114 /* We only allow these to be setup once. */
1115 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
1116 ret = ffa_error(FFA_DENIED);
1117 goto out;
1118 }
1119
1120 /* Hafnium only supports a fixed size of RX/TX buffers. */
1121 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
1122 ret = ffa_error(FFA_INVALID_PARAMETERS);
1123 goto out;
1124 }
1125
1126 /* Fail if addresses are not page-aligned. */
1127 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
1128 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
1129 ret = ffa_error(FFA_INVALID_PARAMETERS);
1130 goto out;
1131 }
1132
1133 /* Convert to physical addresses. */
1134 pa_send_begin = pa_from_ipa(send);
1135 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
1136 pa_recv_begin = pa_from_ipa(recv);
1137 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
1138
1139 /* Fail if the same page is used for the send and receive pages. */
1140 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
1141 ret = ffa_error(FFA_INVALID_PARAMETERS);
1142 goto out;
1143 }
Andrew Sculle1322792019-07-01 17:46:10 +01001144
1145 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001146 * Ensure the pages are valid, owned and exclusive to the VM and that
1147 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +01001148 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001149 if (!vm_mem_get_mode(vm_locked, send, ipa_add(send, PAGE_SIZE),
1150 &orig_send_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001151 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
1152 (orig_send_mode & MM_MODE_R) == 0 ||
1153 (orig_send_mode & MM_MODE_W) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001154 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001155 goto out;
1156 }
1157
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -08001158 if (!vm_mem_get_mode(vm_locked, recv, ipa_add(recv, PAGE_SIZE),
1159 &orig_recv_mode) ||
Manish Pandeyd34f8892020-06-19 17:41:07 +01001160 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
1161 (orig_recv_mode & MM_MODE_R) == 0) {
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001162 ret = ffa_error(FFA_INVALID_PARAMETERS);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001163 goto out;
1164 }
Andrew Sculle1322792019-07-01 17:46:10 +01001165
1166 /* Take memory ownership away from the VM and mark as shared. */
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001167 uint32_t mode =
1168 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W;
1169 if (vm_locked.vm->el0_partition) {
1170 mode |= MM_MODE_USER | MM_MODE_NG;
1171 }
1172
1173 if (!vm_identity_map(vm_locked, pa_send_begin, pa_send_end, mode,
1174 local_page_pool, NULL)) {
Manish Pandeyd34f8892020-06-19 17:41:07 +01001175 ret = ffa_error(FFA_NO_MEMORY);
1176 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +01001177 }
1178
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001179 mode = MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R;
1180 if (vm_locked.vm->el0_partition) {
1181 mode |= MM_MODE_USER | MM_MODE_NG;
1182 }
1183
1184 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end, mode,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001185 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001186 /* TODO: partial defrag of failed range. */
1187 /* Recover any memory consumed in failed mapping. */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001188 vm_ptable_defrag(vm_locked, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +01001189 goto fail_undo_send;
1190 }
1191
Olivier Deprez96a2a262020-06-11 17:21:38 +02001192 /* Get extra send/recv pages mapping attributes for the given VM ID. */
1193 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
1194
Raghu Krishnamurthy95c3a272021-02-26 18:09:41 -08001195 /*
1196 * For EL0 partitions, since both the partition and the hypervisor code
1197 * use the EL2&0 translation regime, it is critical to mark the mappings
1198 * of the send and recv buffers as non-global in the TLB. For one, if we
1199 * dont mark it as non-global, it would cause TLB conflicts since there
1200 * would be an identity mapping with non-global attribute in the
1201 * partitions page tables, but another identity mapping in the
1202 * hypervisor page tables with the global attribute. The other issue is
1203 * one of security, we dont want other partitions to be able to access
1204 * other partitions buffers through cached translations.
1205 */
1206 if (vm_locked.vm->el0_partition) {
1207 extra_attributes |= MM_MODE_NG;
1208 }
1209
Manish Pandeyd34f8892020-06-19 17:41:07 +01001210 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
1211 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +02001212 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +01001213 goto fail_undo_send_and_recv;
1214 }
1215
Manish Pandeyd34f8892020-06-19 17:41:07 +01001216 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001217 goto out;
1218
Andrew Sculle1322792019-07-01 17:46:10 +01001219fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001220 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001221 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001222
1223fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001224 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001225 orig_send_mode, local_page_pool, NULL));
1226 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001227
1228out:
Andrew Sculle1322792019-07-01 17:46:10 +01001229 return ret;
1230}
1231
1232/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001233 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001234 * must not be shared. Locking of the page tables combined with a local memory
1235 * pool ensures there will always be enough memory to recover from any errors
1236 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1237 * by another core which could result in this transaction being unable to roll
1238 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001239 *
1240 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001242 * aligned, are the same or have invalid attributes.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001243 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001244 * due to insuffient page table memory.
Daniel Boulby6f8941e2021-06-14 18:27:18 +01001245 * - FFA_ERROR FFA_DENIED if the pages are already mapped.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 * - FFA_SUCCESS on success if no further action is needed.
1247 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001248 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001249 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1251 uint32_t page_count, struct vcpu *current,
1252 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001253{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001254 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001256 struct vm_locked vm_locked;
1257 struct mm_stage1_locked mm_stage1_locked;
1258 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001259
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001260 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001261 * Create a local pool so any freed memory can't be used by another
1262 * thread. This is to ensure the original mapping can be restored if any
1263 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001264 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001265 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1266
Andrew Sculle1322792019-07-01 17:46:10 +01001267 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001268 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001269
Manish Pandeyd34f8892020-06-19 17:41:07 +01001270 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1271 page_count, &local_page_pool);
1272 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001273 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001274 }
1275
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001276 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001277 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001278
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001279exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001280 mpool_fini(&local_page_pool);
1281
1282 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001283 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001284
1285 return ret;
1286}
1287
1288/**
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001289 * Unmaps the RX/TX buffer pair with a partition or partition manager from the
1290 * translation regime of the caller. Unmap the region for the hypervisor and
1291 * set the memory region to owned and exclusive for the component. Since the
1292 * memory region mapped in the page table, when the buffers were originally
1293 * created we can safely remap it.
1294 *
1295 * Returns:
1296 * - FFA_ERROR FFA_INVALID_PARAMETERS if there is no buffer pair registered on
1297 * behalf of the caller.
1298 * - FFA_SUCCESS on success if no further action is needed.
1299 */
1300struct ffa_value api_ffa_rxtx_unmap(ffa_vm_id_t allocator_id,
1301 struct vcpu *current)
1302{
1303 struct vm *vm = current->vm;
1304 struct vm_locked vm_locked;
1305 struct mm_stage1_locked mm_stage1_locked;
1306 paddr_t send_pa_begin;
1307 paddr_t send_pa_end;
1308 paddr_t recv_pa_begin;
1309 paddr_t recv_pa_end;
1310
1311 /*
1312 * Check there is a buffer pair registered on behalf of the caller.
1313 * Since forwarding is not yet supported the allocator ID MBZ.
1314 */
1315 if (allocator_id != 0) {
1316 dlog_error(
1317 "Forwarding MAP/UNMAP from the hypervisor is not yet "
1318 "supported so vm id must be zero.\n");
1319 return ffa_error(FFA_INVALID_PARAMETERS);
1320 }
1321
1322 /* Get send and receive buffers. */
1323 if (vm->mailbox.send == NULL || vm->mailbox.recv == NULL) {
Olivier Deprez86d87ae2021-08-19 14:27:46 +02001324 dlog_verbose(
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001325 "No buffer pair registered on behalf of the caller.\n");
1326 return ffa_error(FFA_INVALID_PARAMETERS);
1327 }
1328
1329 /* Currently a mailbox size of 1 page is assumed. */
1330 send_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.send));
1331 send_pa_end = pa_add(send_pa_begin, HF_MAILBOX_SIZE);
1332 recv_pa_begin = pa_from_va(va_from_ptr(vm->mailbox.recv));
1333 recv_pa_end = pa_add(recv_pa_begin, HF_MAILBOX_SIZE);
1334
1335 vm_locked = vm_lock(vm);
1336 mm_stage1_locked = mm_lock_stage1();
1337
1338 /*
1339 * Set the memory region of the buffers back to the default mode
1340 * for the VM. Since this memory region was already mapped for the
1341 * RXTX buffers we can safely remap them.
1342 */
1343 CHECK(vm_identity_map(vm_locked, send_pa_begin, send_pa_end,
1344 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1345 NULL));
1346
1347 CHECK(vm_identity_map(vm_locked, recv_pa_begin, recv_pa_end,
1348 MM_MODE_R | MM_MODE_W | MM_MODE_X, &api_page_pool,
1349 NULL));
1350
1351 /* Unmap the buffers in the partition manager. */
1352 CHECK(mm_unmap(mm_stage1_locked, send_pa_begin, send_pa_end,
1353 &api_page_pool));
1354 CHECK(mm_unmap(mm_stage1_locked, recv_pa_begin, recv_pa_end,
1355 &api_page_pool));
1356
1357 vm->mailbox.send = NULL;
1358 vm->mailbox.recv = NULL;
1359
1360 mm_unlock_stage1(&mm_stage1_locked);
1361 vm_unlock(&vm_locked);
1362
1363 return (struct ffa_value){.func = FFA_SUCCESS_32};
1364}
1365
1366/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001367 * Notifies the `to` VM about the message currently in its mailbox, possibly
1368 * with the help of the primary VM.
1369 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001370static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1371 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001372{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1374 struct ffa_value primary_ret = {
1375 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001376 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001377 };
1378
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001379 /* Messages for the primary VM are delivered directly. */
1380 if (to.vm->id == HF_PRIMARY_VM_ID) {
1381 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001382 * Only tell the primary VM the size and other details if the
1383 * message is for it, to avoid leaking data about messages for
1384 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001385 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001386 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001387
1388 to.vm->mailbox.state = MAILBOX_STATE_READ;
1389 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001390 VCPU_STATE_BLOCKED);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001391 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001392 }
1393
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001394 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1395
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001396 /* Messages for the TEE are sent on via the dispatcher. */
1397 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001398 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001399
Olivier Deprez112d2b52020-09-30 07:39:23 +02001400 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001401 /*
1402 * After the call to the TEE completes it must have finished
1403 * reading its RX buffer, so it is ready for another message.
1404 */
1405 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001406 /*
1407 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001409 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001410 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001411 }
1412
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001413 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001414 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001415 *next = api_switch_to_primary(current, primary_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001416 VCPU_STATE_BLOCKED);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001417 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001418
1419 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001420}
1421
1422/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001423 * Copies data from the sender's send buffer to the recipient's receive buffer
1424 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001425 *
1426 * If the recipient's receive buffer is busy, it can optionally register the
1427 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001428 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1430 ffa_vm_id_t receiver_vm_id, uint32_t size,
1431 uint32_t attributes, struct vcpu *current,
1432 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001433{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001434 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001435 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001436 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001437 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001439 struct vcpu_locked current_locked;
1440 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 bool notify =
1442 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001443
Andrew Walbran70bc8622019-10-07 14:15:58 +01001444 /* Ensure sender VM ID corresponds to the current VM. */
1445 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001447 }
1448
1449 /* Disallow reflexive requests as this suggests an error in the VM. */
1450 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001451 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001452 }
1453
1454 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 if (size > FFA_MSG_PAYLOAD_MAX) {
1456 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001457 }
1458
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001459 /*
1460 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1461 * invocation.
1462 */
1463 current_locked = vcpu_lock(current);
1464 is_direct_request_ongoing =
1465 is_ffa_direct_msg_request_ongoing(current_locked);
1466 vcpu_unlock(&current_locked);
1467
1468 if (is_direct_request_ongoing) {
1469 return ffa_error(FFA_DENIED);
1470 }
1471
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001472 /* Ensure the receiver VM exists. */
1473 to = vm_find(receiver_vm_id);
1474 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001476 }
1477
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001478 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001479 * Check that the sender has configured its send buffer. If the tx
1480 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1481 * be safely accessed after releasing the lock since the tx mailbox
1482 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001483 */
1484 sl_lock(&from->lock);
1485 from_msg = from->mailbox.send;
1486 sl_unlock(&from->lock);
1487
1488 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001490 }
1491
Andrew Walbran82d6d152019-12-24 15:02:06 +00001492 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001493
Andrew Walbran82d6d152019-12-24 15:02:06 +00001494 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001495 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001496 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001497 }
1498
Andrew Walbran82d6d152019-12-24 15:02:06 +00001499 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001500 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001501 to->mailbox.recv_size = size;
1502 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001504 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001505
1506out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001507 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001508
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001509 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001510}
1511
1512/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001513 * Checks whether the vCPU's attempt to block for a message has already been
1514 * interrupted or whether it is allowed to block.
1515 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001516bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001517{
Manish Pandey35e452f2021-02-18 21:36:34 +00001518 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001519 bool interrupted;
1520
Manish Pandey35e452f2021-02-18 21:36:34 +00001521 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001522
1523 /*
1524 * Don't block if there are enabled and pending interrupts, to match
1525 * behaviour of wait_for_interrupt.
1526 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001527 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001528
Manish Pandey35e452f2021-02-18 21:36:34 +00001529 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001530
1531 return interrupted;
1532}
1533
1534/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001535 * Receives a message from the mailbox. If one isn't available, this function
1536 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001537 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001538 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001539 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1541 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001542{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001543 bool is_direct_request_ongoing;
1544 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001545 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001547 bool is_from_secure_world =
1548 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001549
Andrew Scullaa039b32018-10-04 15:02:26 +01001550 /*
1551 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001552 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001553 */
J-Alvesb37fd082020-10-22 12:29:21 +01001554 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001555 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001556 }
1557
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001558 /*
1559 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1560 * invocation.
1561 */
1562 current_locked = vcpu_lock(current);
1563 is_direct_request_ongoing =
1564 is_ffa_direct_msg_request_ongoing(current_locked);
1565 vcpu_unlock(&current_locked);
1566
1567 if (is_direct_request_ongoing) {
1568 return ffa_error(FFA_DENIED);
1569 }
1570
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001571 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001572
Andrew Scullaa039b32018-10-04 15:02:26 +01001573 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001574 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1575 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001576 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001577 goto out;
1578 }
1579
1580 /* No pending message so fail if not allowed to block. */
1581 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001583 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001584 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001585
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001586 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001587 * From this point onward this call can only be interrupted or a message
1588 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001589 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001590 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 return_code = ffa_error(FFA_INTERRUPTED);
1592 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001593 goto out;
1594 }
1595
J-Alvesb37fd082020-10-22 12:29:21 +01001596 if (is_from_secure_world) {
1597 /* Return to other world if caller is a SP. */
1598 *next = api_switch_to_other_world(
1599 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001600 VCPU_STATE_WAITING);
J-Alvesb37fd082020-10-22 12:29:21 +01001601 } else {
1602 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001603 struct ffa_value run_return = {
1604 .func = FFA_MSG_WAIT_32,
1605 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001606 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001607
Andrew Walbranb4816552018-12-05 17:35:42 +00001608 *next = api_switch_to_primary(current, run_return,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05001609 VCPU_STATE_WAITING);
Andrew Walbranb4816552018-12-05 17:35:42 +00001610 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001611out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001612 sl_unlock(&vm->lock);
1613
Jose Marinho3e2442f2019-03-12 13:30:37 +00001614 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001615}
1616
1617/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001618 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1619 * by this function, the caller must have called api_mailbox_send before with
1620 * the notify argument set to true, and this call must have failed because the
1621 * mailbox was not available.
1622 *
1623 * It should be called repeatedly to retrieve a list of VMs.
1624 *
1625 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1626 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001627 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001628int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001629{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001630 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001631 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001632 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001633
1634 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001635 if (list_empty(&vm->mailbox.ready_list)) {
1636 ret = -1;
1637 goto exit;
1638 }
1639
1640 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1641 ready_links);
1642 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001643 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001644
1645exit:
1646 sl_unlock(&vm->lock);
1647 return ret;
1648}
1649
1650/**
1651 * Retrieves the next VM waiting to be notified that the mailbox of the
1652 * specified VM became writable. Only primary VMs are allowed to call this.
1653 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001654 * Returns -1 on failure or if there are no waiters; the VM id of the next
1655 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001656 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001657int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001658{
1659 struct vm *vm;
1660 struct vm_locked locked;
1661 struct wait_entry *entry;
1662 struct vm *waiting_vm;
1663
1664 /* Only primary VMs are allowed to call this function. */
1665 if (current->vm->id != HF_PRIMARY_VM_ID) {
1666 return -1;
1667 }
1668
Andrew Walbran42347a92019-05-09 13:59:03 +01001669 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001670 if (vm == NULL) {
1671 return -1;
1672 }
1673
Fuad Tabbaed294af2019-12-20 10:43:01 +00001674 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001675 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001676 entry = api_fetch_waiter(locked);
1677 vm_unlock(&locked);
1678
1679 if (entry == NULL) {
1680 return -1;
1681 }
1682
1683 /* Enqueue notification to waiting VM. */
1684 waiting_vm = entry->waiting_vm;
1685
1686 sl_lock(&waiting_vm->lock);
1687 if (list_empty(&entry->ready_links)) {
1688 list_append(&waiting_vm->mailbox.ready_list,
1689 &entry->ready_links);
1690 }
1691 sl_unlock(&waiting_vm->lock);
1692
1693 return waiting_vm->id;
1694}
1695
1696/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001697 * Releases the caller's mailbox so that a new message can be received. The
1698 * caller must have copied out all data they wish to preserve as new messages
1699 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001700 *
1701 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001702 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1703 * - FFA_SUCCESS on success if no further action is needed.
1704 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001705 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001706 * hf_mailbox_waiter_get.
1707 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001708struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001709{
1710 struct vm *vm = current->vm;
1711 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001712 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001713
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001714 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001715 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001716 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001717 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001718 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001719 break;
1720
Andrew Sculld6ee1102019-04-05 22:12:42 +01001721 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001722 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001723 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001724 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001725 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001726 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001727
1728 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001729}
Andrew Walbran318f5732018-11-20 16:23:42 +00001730
1731/**
1732 * Enables or disables a given interrupt ID for the calling vCPU.
1733 *
1734 * Returns 0 on success, or -1 if the intid is invalid.
1735 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001736int64_t api_interrupt_enable(uint32_t intid, bool enable,
1737 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001738{
Manish Pandey35e452f2021-02-18 21:36:34 +00001739 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001740 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001741 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1742 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001743
Andrew Walbran318f5732018-11-20 16:23:42 +00001744 if (intid >= HF_NUM_INTIDS) {
1745 return -1;
1746 }
1747
Manish Pandey35e452f2021-02-18 21:36:34 +00001748 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001749 if (enable) {
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;
Manish Pandey35e452f2021-02-18 21:36:34 +00001767
1768 if (type == INTERRUPT_TYPE_IRQ) {
1769 current->interrupts.interrupt_type[intid_index] &=
1770 ~intid_mask;
1771 } else if (type == INTERRUPT_TYPE_FIQ) {
1772 current->interrupts.interrupt_type[intid_index] |=
1773 intid_mask;
1774 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001775 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001776 /*
1777 * If it is pending and was enabled before, decrement the count.
1778 */
1779 if (current->interrupts.interrupt_pending[intid_index] &
1780 current->interrupts.interrupt_enabled[intid_index] &
1781 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001782 if ((current->interrupts.interrupt_type[intid_index] &
1783 intid_mask) ==
1784 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1785 vcpu_irq_count_decrement(current_locked);
1786 } else {
1787 vcpu_fiq_count_decrement(current_locked);
1788 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001789 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001790 current->interrupts.interrupt_enabled[intid_index] &=
1791 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001792 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001793 }
1794
Manish Pandey35e452f2021-02-18 21:36:34 +00001795 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001796 return 0;
1797}
1798
1799/**
1800 * Returns the ID of the next pending interrupt for the calling vCPU, and
1801 * acknowledges it (i.e. marks it as no longer pending). Returns
1802 * HF_INVALID_INTID if there are no pending interrupts.
1803 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001804uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001805{
1806 uint8_t i;
1807 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001808 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001809
1810 /*
1811 * Find the first enabled and pending interrupt ID, return it, and
1812 * deactivate it.
1813 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001814 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001815 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1816 uint32_t enabled_and_pending =
1817 current->interrupts.interrupt_enabled[i] &
1818 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001819
Andrew Walbran318f5732018-11-20 16:23:42 +00001820 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001821 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001822 uint32_t intid_mask = 1U << bit_index;
1823
Andrew Walbran3d84a262018-12-13 14:41:19 +00001824 /*
1825 * Mark it as no longer pending and decrement the count.
1826 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001827 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1828
1829 if ((current->interrupts.interrupt_type[i] &
1830 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1831 vcpu_irq_count_decrement(current_locked);
1832 } else {
1833 vcpu_fiq_count_decrement(current_locked);
1834 }
1835
Andrew Walbran3d84a262018-12-13 14:41:19 +00001836 first_interrupt =
1837 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001838 break;
1839 }
1840 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001841
Manish Pandey35e452f2021-02-18 21:36:34 +00001842 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001843 return first_interrupt;
1844}
1845
1846/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001847 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001848 * given VM and vCPU.
1849 */
1850static inline bool is_injection_allowed(uint32_t target_vm_id,
1851 struct vcpu *current)
1852{
1853 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001854
Andrew Walbran318f5732018-11-20 16:23:42 +00001855 /*
1856 * The primary VM is allowed to inject interrupts into any VM. Secondary
1857 * VMs are only allowed to inject interrupts into their own vCPUs.
1858 */
1859 return current_vm_id == HF_PRIMARY_VM_ID ||
1860 current_vm_id == target_vm_id;
1861}
1862
1863/**
1864 * Injects a virtual interrupt of the given ID into the given target vCPU.
1865 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1866 * when the vCPU is next run, which is up to the scheduler.
1867 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001868 * Returns:
1869 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1870 * ID is invalid, or the current VM is not allowed to inject interrupts to
1871 * the target VM.
1872 * - 0 on success if no further action is needed.
1873 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1874 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001875 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001876int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1877 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001878 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001879{
Andrew Walbran318f5732018-11-20 16:23:42 +00001880 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001881 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001882
1883 if (intid >= HF_NUM_INTIDS) {
1884 return -1;
1885 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001886
Andrew Walbran318f5732018-11-20 16:23:42 +00001887 if (target_vm == NULL) {
1888 return -1;
1889 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001890
Andrew Walbran318f5732018-11-20 16:23:42 +00001891 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001892 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001893 return -1;
1894 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001895
Andrew Walbran318f5732018-11-20 16:23:42 +00001896 if (!is_injection_allowed(target_vm_id, current)) {
1897 return -1;
1898 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001899
Andrew Walbrane1310df2019-04-29 17:28:28 +01001900 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001901
Manish Pandey35e452f2021-02-18 21:36:34 +00001902 dlog_verbose(
1903 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1904 "%u\n",
1905 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1906 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001907 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001908}
Andrew Scull6386f252018-12-06 13:29:10 +00001909
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001910/** Returns the version of the implemented FF-A specification. */
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001911struct ffa_value api_ffa_version(struct vcpu *current,
1912 uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001913{
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001914 struct vm_locked current_vm_locked;
1915
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001916 /*
1917 * Ensure that both major and minor revision representation occupies at
1918 * most 15 bits.
1919 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001920 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001921 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001922 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001923 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001924 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001925 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001926 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001927 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001928
Daniel Boulbybaeaf2e2021-12-09 11:42:36 +00001929 current_vm_locked = vm_lock(current->vm);
1930 current_vm_locked.vm->ffa_version = requested_version;
1931 vm_unlock(&current_vm_locked);
1932
Daniel Boulby6e32c612021-02-17 15:09:41 +00001933 return ((struct ffa_value){.func = FFA_VERSION_COMPILED});
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001934}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001935
1936int64_t api_debug_log(char c, struct vcpu *current)
1937{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001938 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001939 struct vm *vm = current->vm;
1940 struct vm_locked vm_locked = vm_lock(vm);
1941
Andrew Sculld54e1be2019-08-20 11:09:42 +01001942 if (c == '\n' || c == '\0') {
1943 flush = true;
1944 } else {
1945 vm->log_buffer[vm->log_buffer_length++] = c;
1946 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1947 }
1948
1949 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001950 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1951 vm->log_buffer_length);
1952 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001953 }
1954
1955 vm_unlock(&vm_locked);
1956
1957 return 0;
1958}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001959
1960/**
J-Alves6f72ca82021-11-01 12:34:58 +00001961 * Helper for success return of FFA_FEATURES, for when it is used to query
1962 * an interrupt ID.
1963 */
1964struct ffa_value api_ffa_feature_success(uint32_t arg2)
1965{
1966 return (struct ffa_value){
1967 .func = FFA_SUCCESS_32, .arg1 = 0U, .arg2 = arg2};
1968}
1969
1970/**
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001971 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001972 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001973 */
J-Alves6f72ca82021-11-01 12:34:58 +00001974struct ffa_value api_ffa_features(uint32_t feature_function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001975{
J-Alves6f72ca82021-11-01 12:34:58 +00001976 /*
1977 * According to table 13.8 of FF-A v1.1 Beta 0 spec, bits [30:8] MBZ
1978 * if using a feature ID.
1979 */
1980 if ((feature_function_id & FFA_FEATURES_FUNC_ID_MASK) == 0U &&
1981 (feature_function_id & ~FFA_FEATURES_FEATURE_ID_MASK) != 0) {
1982 return ffa_error(FFA_NOT_SUPPORTED);
1983 }
1984
1985 switch (feature_function_id) {
1986 /* Check support of the given Function ID. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 case FFA_ERROR_32:
1988 case FFA_SUCCESS_32:
1989 case FFA_INTERRUPT_32:
1990 case FFA_VERSION_32:
1991 case FFA_FEATURES_32:
1992 case FFA_RX_RELEASE_32:
1993 case FFA_RXTX_MAP_64:
Daniel Boulby9e420ca2021-07-07 15:03:49 +01001994 case FFA_RXTX_UNMAP_32:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001995 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001996 case FFA_ID_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001997 case FFA_MSG_WAIT_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001998 case FFA_RUN_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001999 case FFA_MEM_DONATE_32:
2000 case FFA_MEM_LEND_32:
2001 case FFA_MEM_SHARE_32:
2002 case FFA_MEM_RETRIEVE_REQ_32:
2003 case FFA_MEM_RETRIEVE_RESP_32:
2004 case FFA_MEM_RELINQUISH_32:
2005 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002006 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002007 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00002008 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002009 case FFA_MSG_SEND_DIRECT_REQ_32:
J-Alves3829fc02021-03-18 12:49:18 +00002010#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
Daniel Boulbyb2fb80e2021-02-03 15:09:23 +00002011 /* FF-A v1.1 features. */
2012 case FFA_SPM_ID_GET_32:
J-Alves6f72ca82021-11-01 12:34:58 +00002013 case FFA_NOTIFICATION_BITMAP_CREATE_32:
2014 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
2015 case FFA_NOTIFICATION_BIND_32:
2016 case FFA_NOTIFICATION_UNBIND_32:
2017 case FFA_NOTIFICATION_SET_32:
2018 case FFA_NOTIFICATION_GET_32:
2019 case FFA_NOTIFICATION_INFO_GET_64:
Raghu Krishnamurthy6764b072021-10-18 12:54:24 -07002020 case FFA_MEM_PERM_GET_32:
2021 case FFA_MEM_PERM_SET_32:
2022 case FFA_MEM_PERM_GET_64:
2023 case FFA_MEM_PERM_SET_64:
J-Alves3829fc02021-03-18 12:49:18 +00002024#endif
2025 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves6f72ca82021-11-01 12:34:58 +00002026
2027#if (MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED)
2028 /* Check support of a feature provided respective feature ID. */
2029 case FFA_FEATURE_NPI:
2030 return api_ffa_feature_success(HF_NOTIFICATION_PENDING_INTID);
2031 case FFA_FEATURE_SRI:
2032 return api_ffa_feature_success(HF_SCHEDULE_RECEIVER_INTID);
2033#endif
2034 /* Platform specific feature support. */
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002035 default:
J-Alves6f72ca82021-11-01 12:34:58 +00002036 return arch_ffa_features(feature_function_id);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01002037 }
2038}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002039
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002040/**
J-Alves645eabe2021-02-22 16:08:27 +00002041 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
2042 * interfaces.
2043 */
2044static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
2045{
2046 return args.arg2 == 0U;
2047}
2048
2049/**
J-Alves76d99af2021-03-10 17:42:11 +00002050 * Limits size of arguments in ffa_value structure to 32-bit.
2051 */
2052static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
2053{
2054 return (struct ffa_value){
2055 .func = (uint32_t)args.func,
2056 .arg1 = (uint32_t)args.arg1,
2057 .arg2 = (uint32_t)0,
2058 .arg3 = (uint32_t)args.arg3,
2059 .arg4 = (uint32_t)args.arg4,
2060 .arg5 = (uint32_t)args.arg5,
2061 .arg6 = (uint32_t)args.arg6,
2062 .arg7 = (uint32_t)args.arg7,
2063 };
2064}
2065
2066/**
2067 * Helper to copy direct message payload, depending on SMC used and expected
2068 * registers size.
2069 */
2070static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
2071{
2072 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
2073 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
2074 return api_ffa_value_copy32(args);
2075 }
2076
2077 return (struct ffa_value){
2078 .func = args.func,
2079 .arg1 = args.arg1,
2080 .arg2 = 0,
2081 .arg3 = args.arg3,
2082 .arg4 = args.arg4,
2083 .arg5 = args.arg5,
2084 .arg6 = args.arg6,
2085 .arg7 = args.arg7,
2086 };
2087}
2088
2089/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002090 * Send an FF-A direct message request.
2091 */
2092struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
2093 ffa_vm_id_t receiver_vm_id,
2094 struct ffa_value args,
2095 struct vcpu *current,
2096 struct vcpu **next)
2097{
J-Alves17228f72021-04-20 17:13:19 +01002098 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002099 struct vm *receiver_vm;
J-Alves6e2abc62021-12-02 14:58:56 +00002100 struct vm_locked receiver_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002101 struct vcpu *receiver_vcpu;
2102 struct two_vcpu_locked vcpus_locked;
2103
J-Alves645eabe2021-02-22 16:08:27 +00002104 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2105 return ffa_error(FFA_INVALID_PARAMETERS);
2106 }
2107
Olivier Deprez55a189e2021-06-09 15:45:27 +02002108 if (!plat_ffa_is_direct_request_valid(current, sender_vm_id,
2109 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002110 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002111 }
2112
Olivier Deprez55a189e2021-06-09 15:45:27 +02002113 if (plat_ffa_direct_request_forward(receiver_vm_id, args, &ret)) {
J-Alves17228f72021-04-20 17:13:19 +01002114 return ret;
2115 }
2116
2117 ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
2118
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002119 receiver_vm = vm_find(receiver_vm_id);
2120 if (receiver_vm == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002121 dlog_verbose("Invalid Receiver!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002122 return ffa_error(FFA_INVALID_PARAMETERS);
2123 }
2124
2125 /*
J-Alves439ac972021-11-18 17:32:03 +00002126 * Check if sender supports sending direct message req, and if
2127 * receiver supports receipt of direct message requests.
2128 */
2129 if (!plat_ffa_is_direct_request_supported(current->vm, receiver_vm)) {
2130 return ffa_error(FFA_DENIED);
2131 }
2132
2133 /*
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002134 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
2135 * UP (migratable) or MP partitions with a number of vCPUs matching the
2136 * number of PEs in the system. It further states that MP partitions
2137 * accepting direct request messages cannot migrate.
2138 */
J-Alvesad6a0432021-04-09 16:06:21 +01002139 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002140 if (receiver_vcpu == NULL) {
J-Alves88a13542021-12-14 15:39:52 +00002141 dlog_verbose("Invalid vCPU!\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002142 return ffa_error(FFA_INVALID_PARAMETERS);
2143 }
2144
J-Alves6e2abc62021-12-02 14:58:56 +00002145 receiver_locked = vm_lock(receiver_vm);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002146 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
2147
2148 /*
2149 * If destination vCPU is executing or already received an
2150 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
2151 * busy. There is a brief period of time where the vCPU state has
2152 * changed but regs_available is still false thus consider this case as
2153 * the vCPU not yet ready to receive a direct message request.
2154 */
2155 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
2156 receiver_vcpu->state == VCPU_STATE_RUNNING ||
2157 !receiver_vcpu->regs_available) {
J-Alves88a13542021-12-14 15:39:52 +00002158 dlog_verbose("Receiver is busy with another request.\n");
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002159 ret = ffa_error(FFA_BUSY);
2160 goto out;
2161 }
2162
2163 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
2164 memory_order_relaxed)) {
2165 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002166 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002167 receiver_vcpu->vm->id,
2168 vcpu_index(receiver_vcpu));
2169 receiver_vcpu->state = VCPU_STATE_ABORTED;
2170 }
2171
2172 ret = ffa_error(FFA_ABORTED);
2173 goto out;
2174 }
2175
2176 switch (receiver_vcpu->state) {
2177 case VCPU_STATE_OFF:
2178 case VCPU_STATE_RUNNING:
2179 case VCPU_STATE_ABORTED:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002180 case VCPU_STATE_BLOCKED_INTERRUPT:
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002181 case VCPU_STATE_BLOCKED:
2182 case VCPU_STATE_PREEMPTED:
J-Alves88a13542021-12-14 15:39:52 +00002183 dlog_verbose("Receiver's vCPU can't receive request (%u)!\n",
2184 vcpu_index(receiver_vcpu));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002185 ret = ffa_error(FFA_BUSY);
2186 goto out;
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002187 case VCPU_STATE_WAITING:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002188 /*
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002189 * We expect target vCPU to be in WAITING state after either
2190 * having called ffa_msg_wait or sent a direct message response.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002191 */
2192 break;
2193 }
2194
2195 /* Inject timer interrupt if any pending */
2196 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002197 api_interrupt_inject_locked(vcpus_locked.vcpu1,
2198 HF_VIRTUAL_TIMER_INTID, current,
2199 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002200
2201 arch_timer_mask(&receiver_vcpu->regs);
2202 }
2203
2204 /* The receiver vCPU runs upon direct message invocation */
2205 receiver_vcpu->cpu = current->cpu;
2206 receiver_vcpu->state = VCPU_STATE_RUNNING;
2207 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002208 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002209
J-Alves76d99af2021-03-10 17:42:11 +00002210 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002211
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002212 current->state = VCPU_STATE_BLOCKED;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002213
2214 /* Switch to receiver vCPU targeted to by direct msg request */
2215 *next = receiver_vcpu;
2216
J-Alves6e2abc62021-12-02 14:58:56 +00002217 if (!receiver_locked.vm->el0_partition) {
2218 /*
2219 * If the scheduler in the system is giving CPU cycles to the
2220 * receiver, due to pending notifications, inject the NPI
2221 * interrupt. Following call assumes that '*next' has been set
2222 * to receiver_vcpu.
2223 */
2224 plat_ffa_inject_notification_pending_interrupt(
2225 vcpus_locked.vcpu1.vcpu == receiver_vcpu
2226 ? vcpus_locked.vcpu1
2227 : vcpus_locked.vcpu2,
2228 current, receiver_locked);
2229 }
2230
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002231 /*
2232 * Since this flow will lead to a VM switch, the return value will not
2233 * be applied to current vCPU.
2234 */
2235
2236out:
2237 sl_unlock(&receiver_vcpu->lock);
2238 sl_unlock(&current->lock);
J-Alves6e2abc62021-12-02 14:58:56 +00002239 vm_unlock(&receiver_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002240
2241 return ret;
2242}
2243
2244/**
2245 * Send an FF-A direct message response.
2246 */
2247struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
2248 ffa_vm_id_t receiver_vm_id,
2249 struct ffa_value args,
2250 struct vcpu *current,
2251 struct vcpu **next)
2252{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002253 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00002254
2255 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
2256 return ffa_error(FFA_INVALID_PARAMETERS);
2257 }
2258
J-Alves76d99af2021-03-10 17:42:11 +00002259 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002260
Olivier Deprez55a189e2021-06-09 15:45:27 +02002261 if (!plat_ffa_is_direct_response_valid(current, sender_vm_id,
2262 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00002263 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002264 }
2265
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002266 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002267 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002268 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002269 * No need for REQ/RESP state management as managed exit does
2270 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002271 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002272 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
2273 vcpu_unlock(&current_locked);
2274 return ffa_error(FFA_DENIED);
2275 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002276
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002277 /*
2278 * Per FF-A v1.1 Beta section 8.4.1.2 bullet 6, SPMC can signal
2279 * a secure interrupt to a SP that is performing managed exit.
2280 * We have taken a implementation defined choice to not allow
2281 * Managed exit while a SP is processing a secure interrupt.
2282 */
2283 CHECK(!current->processing_secure_interrupt);
2284
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -06002285 plat_interrupts_set_priority_mask(current->priority_mask);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002286 current->processing_managed_exit = false;
2287 } else {
2288 /*
2289 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
2290 * defined originator.
2291 */
2292 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
2293 /*
2294 * Sending direct response but direct request origin
2295 * vCPU is not set.
2296 */
2297 vcpu_unlock(&current_locked);
2298 return ffa_error(FFA_DENIED);
2299 }
2300
Madhukar Pappireddyed4ab942021-08-03 14:22:53 -05002301 /* Refer to FF-A v1.1 Beta0 section 7.3 bulet 3. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01002302 if (current->direct_request_origin_vm_id != receiver_vm_id) {
2303 vcpu_unlock(&current_locked);
2304 return ffa_error(FFA_DENIED);
2305 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002306 }
2307
2308 /* Clear direct request origin for the caller. */
2309 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
2310
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002311 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002312
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002313 if (!vm_id_is_current_world(receiver_vm_id)) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002314 *next = api_switch_to_other_world(
2315 current, to_ret,
2316 /*
2317 * Current vcpu sent a direct response. It moves to
2318 * waiting state.
2319 */
2320 VCPU_STATE_WAITING);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002321 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002322 *next = api_switch_to_primary(
2323 current, to_ret,
2324 /*
2325 * Current vcpu sent a direct response. It moves to
2326 * waiting state.
2327 */
2328 VCPU_STATE_WAITING);
J-Alvesfe7f7372020-11-09 11:32:12 +00002329 } else if (vm_id_is_current_world(receiver_vm_id)) {
2330 /*
2331 * It is expected the receiver_vm_id to be from an SP, otherwise
J-Alvesaa79c012021-07-09 14:29:45 +01002332 * 'plat_ffa_is_direct_response_valid' should have
J-Alvesfe7f7372020-11-09 11:32:12 +00002333 * made function return error before getting to this point.
2334 */
2335 *next = api_switch_to_vm(current, to_ret,
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -05002336 /*
2337 * current vcpu sent a direct response.
2338 * It moves to waiting state.
2339 */
2340 VCPU_STATE_WAITING, receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02002341 } else {
2342 panic("Invalid direct message response invocation");
2343 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00002344
2345 return (struct ffa_value){.func = FFA_INTERRUPT_32};
2346}
2347
J-Alves84658fc2021-06-17 14:37:32 +01002348static bool api_memory_region_check_flags(
2349 struct ffa_memory_region *memory_region, uint32_t share_func)
2350{
2351 switch (share_func) {
2352 case FFA_MEM_SHARE_32:
2353 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2354 0U) {
2355 return false;
2356 }
2357 /* Intentional fall-through */
2358 case FFA_MEM_LEND_32:
2359 case FFA_MEM_DONATE_32: {
2360 /* Bits 31:2 Must Be Zero. */
2361 ffa_memory_receiver_flags_t to_mask =
2362 ~(FFA_MEMORY_REGION_FLAG_CLEAR |
2363 FFA_MEMORY_REGION_FLAG_TIME_SLICE);
2364
2365 if ((memory_region->flags & to_mask) != 0U) {
2366 return false;
2367 }
2368 break;
2369 }
2370 default:
2371 panic("Check for mem send calls only.\n");
2372 }
2373
2374 /* Last check reserved values are 0 */
2375 return true;
2376}
2377
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002378struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
2379 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002380 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002381{
2382 struct vm *from = current->vm;
2383 struct vm *to;
2384 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002385 struct ffa_memory_region *memory_region;
2386 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002387
2388 if (ipa_addr(address) != 0 || page_count != 0) {
2389 /*
2390 * Hafnium only supports passing the descriptor in the TX
2391 * mailbox.
2392 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002393 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002394 }
2395
Andrew Walbranca808b12020-05-15 17:22:28 +01002396 if (fragment_length > length) {
2397 dlog_verbose(
2398 "Fragment length %d greater than total length %d.\n",
2399 fragment_length, length);
2400 return ffa_error(FFA_INVALID_PARAMETERS);
2401 }
2402 if (fragment_length < sizeof(struct ffa_memory_region) +
2403 sizeof(struct ffa_memory_access)) {
2404 dlog_verbose(
2405 "Initial fragment length %d smaller than header size "
2406 "%d.\n",
2407 fragment_length,
2408 sizeof(struct ffa_memory_region) +
2409 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002410 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002411 }
2412
2413 /*
2414 * Check that the sender has configured its send buffer. If the TX
2415 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2416 * be safely accessed after releasing the lock since the TX mailbox
2417 * address can only be configured once.
2418 */
2419 sl_lock(&from->lock);
2420 from_msg = from->mailbox.send;
2421 sl_unlock(&from->lock);
2422
2423 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002424 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002425 }
2426
2427 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002428 * Copy the memory region descriptor to a fresh page from the memory
2429 * pool. This prevents the sender from changing it underneath us, and
2430 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002431 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002432 if (fragment_length > HF_MAILBOX_SIZE ||
2433 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002434 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002435 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002436 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002437 if (memory_region == NULL) {
2438 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002439 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002440 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002441 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002442
2443 /* The sender must match the caller. */
2444 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002445 dlog_verbose("Memory region sender doesn't match caller.\n");
J-Alves99948662021-07-28 18:07:04 +01002446 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002447 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002448 }
2449
J-Alves84658fc2021-06-17 14:37:32 +01002450 if (!api_memory_region_check_flags(memory_region, share_func)) {
2451 dlog_verbose(
2452 "Memory region reserved arguments must be zero.\n");
2453 ret = ffa_error(FFA_INVALID_PARAMETERS);
2454 goto out;
2455 }
2456
Andrew Walbrana65a1322020-04-06 19:32:32 +01002457 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002458 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002459 dlog_verbose(
2460 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002461 "endpoint memory access descriptors, expected 1).\n",
2462 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002464 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002465 }
2466
2467 /*
2468 * Ensure that the receiver VM exists and isn't the same as the sender.
2469 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002470 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002471 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002473 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474 goto out;
2475 }
2476
Maksims Svecovsa3d570c2021-12-08 11:16:32 +00002477 if (!plat_ffa_is_memory_send_valid(to->id, share_func)) {
2478 ret = ffa_error(FFA_DENIED);
2479 goto out;
2480 }
2481
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002482 if (to->id == HF_TEE_VM_ID) {
2483 /*
2484 * The 'to' VM lock is only needed in the case that it is the
2485 * TEE VM.
2486 */
2487 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2488
2489 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002490 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002491 goto out_unlock;
2492 }
2493
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002494 ret = ffa_memory_tee_send(
2495 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2496 length, fragment_length, share_func, &api_page_pool);
2497 /*
2498 * ffa_tee_memory_send takes ownership of the memory_region, so
2499 * make sure we don't free it.
2500 */
2501 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502
2503 out_unlock:
2504 vm_unlock(&vm_to_from_lock.vm1);
2505 vm_unlock(&vm_to_from_lock.vm2);
2506 } else {
2507 struct vm_locked from_locked = vm_lock(from);
2508
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002509 ret = ffa_memory_send(from_locked, memory_region, length,
2510 fragment_length, share_func,
2511 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002512 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002513 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002514 * make sure we don't free it.
2515 */
2516 memory_region = NULL;
2517
2518 vm_unlock(&from_locked);
2519 }
2520
2521out:
2522 if (memory_region != NULL) {
2523 mpool_free(&api_page_pool, memory_region);
2524 }
2525
2526 return ret;
2527}
2528
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002529struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2530 uint32_t fragment_length,
2531 ipaddr_t address, uint32_t page_count,
2532 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533{
2534 struct vm *to = current->vm;
2535 struct vm_locked to_locked;
2536 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002537 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002538 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002539 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002540
2541 if (ipa_addr(address) != 0 || page_count != 0) {
2542 /*
2543 * Hafnium only supports passing the descriptor in the TX
2544 * mailbox.
2545 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002546 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002547 }
2548
Andrew Walbrana65a1322020-04-06 19:32:32 +01002549 if (fragment_length != length) {
2550 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002553
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002555 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556 message_buffer_size = cpu_get_buffer_size(current->cpu);
2557 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2558 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 }
2561
2562 to_locked = vm_lock(to);
2563 to_msg = to->mailbox.send;
2564
2565 if (to_msg == NULL) {
2566 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002567 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002568 goto out;
2569 }
2570
2571 /*
2572 * Copy the retrieve request descriptor to an internal buffer, so that
2573 * the caller can't change it underneath us.
2574 */
2575 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2576
2577 if (msg_receiver_busy(to_locked, NULL, false)) {
2578 /*
2579 * Can't retrieve memory information if the mailbox is not
2580 * available.
2581 */
2582 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002583 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002584 goto out;
2585 }
2586
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002587 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2588 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002589
2590out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002591 vm_unlock(&to_locked);
2592 return ret;
2593}
2594
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002595struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002596{
2597 struct vm *from = current->vm;
2598 struct vm_locked from_locked;
2599 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002600 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002601 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603 uint32_t length;
2604
2605 from_locked = vm_lock(from);
2606 from_msg = from->mailbox.send;
2607
2608 if (from_msg == NULL) {
2609 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002610 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002611 goto out;
2612 }
2613
2614 /*
2615 * Calculate length from relinquish descriptor before copying. We will
2616 * check again later to make sure it hasn't changed.
2617 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002618 length = sizeof(struct ffa_mem_relinquish) +
2619 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2620 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002621 /*
2622 * Copy the relinquish descriptor to an internal buffer, so that the
2623 * caller can't change it underneath us.
2624 */
2625 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002626 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002627 message_buffer_size = cpu_get_buffer_size(current->cpu);
2628 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2629 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002630 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002631 goto out;
2632 }
2633 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 if (sizeof(struct ffa_mem_relinquish) +
2636 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002637 length) {
2638 dlog_verbose(
2639 "Endpoint count changed while copying to internal "
2640 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002641 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002642 goto out;
2643 }
2644
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2646 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002647
2648out:
2649 vm_unlock(&from_locked);
2650 return ret;
2651}
2652
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002653struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2654 ffa_memory_region_flags_t flags,
2655 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656{
2657 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002658 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002659
Olivier Deprez55a189e2021-06-09 15:45:27 +02002660 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002661 struct vm_locked to_locked = vm_lock(to);
2662
Andrew Walbranca808b12020-05-15 17:22:28 +01002663 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002664 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002665
Andrew Walbran290b0c92020-02-03 16:37:14 +00002666 vm_unlock(&to_locked);
2667 } else {
2668 struct vm *from = vm_find(HF_TEE_VM_ID);
2669 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2670
Andrew Walbranca808b12020-05-15 17:22:28 +01002671 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2672 vm_to_from_lock.vm2, handle, flags,
2673 &api_page_pool);
2674
2675 vm_unlock(&vm_to_from_lock.vm1);
2676 vm_unlock(&vm_to_from_lock.vm2);
2677 }
2678
2679 return ret;
2680}
2681
2682struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2683 uint32_t fragment_offset,
2684 ffa_vm_id_t sender_vm_id,
2685 struct vcpu *current)
2686{
2687 struct vm *to = current->vm;
2688 struct vm_locked to_locked;
2689 struct ffa_value ret;
2690
2691 /* Sender ID MBZ at virtual instance. */
2692 if (sender_vm_id != 0) {
2693 return ffa_error(FFA_INVALID_PARAMETERS);
2694 }
2695
2696 to_locked = vm_lock(to);
2697
2698 if (msg_receiver_busy(to_locked, NULL, false)) {
2699 /*
2700 * Can't retrieve memory information if the mailbox is not
2701 * available.
2702 */
2703 dlog_verbose("RX buffer not ready.\n");
2704 ret = ffa_error(FFA_BUSY);
2705 goto out;
2706 }
2707
2708 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2709 &api_page_pool);
2710
2711out:
2712 vm_unlock(&to_locked);
2713 return ret;
2714}
2715
2716struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2717 uint32_t fragment_length,
2718 ffa_vm_id_t sender_vm_id,
2719 struct vcpu *current)
2720{
2721 struct vm *from = current->vm;
2722 const void *from_msg;
2723 void *fragment_copy;
2724 struct ffa_value ret;
2725
2726 /* Sender ID MBZ at virtual instance. */
2727 if (sender_vm_id != 0) {
2728 return ffa_error(FFA_INVALID_PARAMETERS);
2729 }
2730
2731 /*
2732 * Check that the sender has configured its send buffer. If the TX
2733 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2734 * be safely accessed after releasing the lock since the TX mailbox
2735 * address can only be configured once.
2736 */
2737 sl_lock(&from->lock);
2738 from_msg = from->mailbox.send;
2739 sl_unlock(&from->lock);
2740
2741 if (from_msg == NULL) {
2742 return ffa_error(FFA_INVALID_PARAMETERS);
2743 }
2744
2745 /*
2746 * Copy the fragment to a fresh page from the memory pool. This prevents
2747 * the sender from changing it underneath us, and also lets us keep it
2748 * around in the share state table if needed.
2749 */
2750 if (fragment_length > HF_MAILBOX_SIZE ||
2751 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2752 dlog_verbose(
2753 "Fragment length %d larger than mailbox size %d.\n",
2754 fragment_length, HF_MAILBOX_SIZE);
2755 return ffa_error(FFA_INVALID_PARAMETERS);
2756 }
2757 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2758 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2759 0) {
2760 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2761 return ffa_error(FFA_INVALID_PARAMETERS);
2762 }
2763 fragment_copy = mpool_alloc(&api_page_pool);
2764 if (fragment_copy == NULL) {
2765 dlog_verbose("Failed to allocate fragment copy.\n");
2766 return ffa_error(FFA_NO_MEMORY);
2767 }
2768 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2769
2770 /*
2771 * Hafnium doesn't support fragmentation of memory retrieve requests
2772 * (because it doesn't support caller-specified mappings, so a request
2773 * will never be larger than a single page), so this must be part of a
2774 * memory send (i.e. donate, lend or share) request.
2775 *
2776 * We can tell from the handle whether the memory transaction is for the
2777 * TEE or not.
2778 */
2779 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2780 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2781 struct vm_locked from_locked = vm_lock(from);
2782
2783 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2784 fragment_length, handle,
2785 &api_page_pool);
2786 /*
2787 * `ffa_memory_send_continue` takes ownership of the
2788 * fragment_copy, so we don't need to free it here.
2789 */
2790 vm_unlock(&from_locked);
2791 } else {
2792 struct vm *to = vm_find(HF_TEE_VM_ID);
2793 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2794
2795 /*
2796 * The TEE RX buffer state is checked in
2797 * `ffa_memory_tee_send_continue` rather than here, as we need
2798 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2799 * than FFA_ERROR FFA_BUSY in case it is busy.
2800 */
2801
2802 ret = ffa_memory_tee_send_continue(
2803 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2804 fragment_length, handle, &api_page_pool);
2805 /*
2806 * `ffa_memory_tee_send_continue` takes ownership of the
2807 * fragment_copy, so we don't need to free it here.
2808 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002809
2810 vm_unlock(&vm_to_from_lock.vm1);
2811 vm_unlock(&vm_to_from_lock.vm2);
2812 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002813
2814 return ret;
2815}
Max Shvetsov40108e72020-08-27 12:39:50 +01002816
Olivier Deprezd614d322021-06-18 15:21:00 +02002817/**
2818 * Register an entry point for a vCPU in warm boot cases.
2819 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1 FFA_SECONDARY_EP_REGISTER.
2820 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002821struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2822 struct vcpu *current)
2823{
2824 struct vm_locked vm_locked;
Olivier Deprezd614d322021-06-18 15:21:00 +02002825 struct ffa_value ret = ffa_error(FFA_DENIED);
Max Shvetsov40108e72020-08-27 12:39:50 +01002826
Olivier Deprezd614d322021-06-18 15:21:00 +02002827 /*
2828 * Reject if interface is not supported at this FF-A instance
2829 * (DEN0077A FF-A v1.1 Beta0 Table 18.29) or the VM is UP.
2830 */
2831 if (!plat_ffa_is_secondary_ep_register_supported() ||
2832 current->vm->vcpu_count == 1) {
2833 return ffa_error(FFA_NOT_SUPPORTED);
2834 }
2835
2836 /*
2837 * No further check is made on the address validity
2838 * (FF-A v1.1 Beta0 Table 18.29) as the VM boundaries are not known
2839 * from the VM or vCPU structure.
2840 * DEN0077A FF-A v1.1 Beta0 section 18.3.2.1.1:
2841 * For each SP [...] the Framework assumes that the same entry point
2842 * address is used for initializing any execution context during a
2843 * secondary cold boot.
2844 * If this function is invoked multiple times, then the entry point
2845 * address specified in the last valid invocation must be used by the
2846 * callee.
2847 */
Max Shvetsov40108e72020-08-27 12:39:50 +01002848 vm_locked = vm_lock(current->vm);
Olivier Deprezd614d322021-06-18 15:21:00 +02002849 if (vm_locked.vm->initialized) {
2850 goto out;
2851 }
2852
Max Shvetsov40108e72020-08-27 12:39:50 +01002853 vm_locked.vm->secondary_ep = entry_point;
Olivier Deprezd614d322021-06-18 15:21:00 +02002854
2855 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
2856
2857out:
Max Shvetsov40108e72020-08-27 12:39:50 +01002858 vm_unlock(&vm_locked);
2859
Olivier Deprezd614d322021-06-18 15:21:00 +02002860 return ret;
Max Shvetsov40108e72020-08-27 12:39:50 +01002861}
J-Alvesa0f317d2021-06-09 13:31:59 +01002862
2863struct ffa_value api_ffa_notification_bitmap_create(ffa_vm_id_t vm_id,
2864 ffa_vcpu_count_t vcpu_count,
2865 struct vcpu *current)
2866{
2867 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2868 dlog_verbose("Bitmap create for NWd VM IDs only (%x).\n",
2869 vm_id);
2870 return ffa_error(FFA_NOT_SUPPORTED);
2871 }
2872
2873 return plat_ffa_notifications_bitmap_create(vm_id, vcpu_count);
2874}
2875
2876struct ffa_value api_ffa_notification_bitmap_destroy(ffa_vm_id_t vm_id,
2877 struct vcpu *current)
2878{
2879 /*
2880 * Validity of use of this interface is the same as for bitmap create.
2881 */
2882 if (!plat_ffa_is_notifications_create_valid(current, vm_id)) {
2883 dlog_verbose("Bitmap destroy for NWd VM IDs only (%x).\n",
2884 vm_id);
2885 return ffa_error(FFA_NOT_SUPPORTED);
2886 }
2887
2888 return plat_ffa_notifications_bitmap_destroy(vm_id);
2889}
J-Alvesc003a7a2021-03-18 13:06:53 +00002890
2891struct ffa_value api_ffa_notification_update_bindings(
2892 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2893 ffa_notifications_bitmap_t notifications, bool is_bind,
2894 struct vcpu *current)
2895{
2896 struct ffa_value ret = {.func = FFA_SUCCESS_32};
2897 struct vm_locked receiver_locked;
2898 const bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2899 const ffa_vm_id_t id_to_update =
2900 is_bind ? sender_vm_id : HF_INVALID_VM_ID;
2901 const ffa_vm_id_t id_to_validate =
2902 is_bind ? HF_INVALID_VM_ID : sender_vm_id;
2903
2904 if (!plat_ffa_is_notifications_bind_valid(current, sender_vm_id,
2905 receiver_vm_id)) {
2906 dlog_verbose("Invalid use of notifications bind interface.\n");
2907 return ffa_error(FFA_INVALID_PARAMETERS);
2908 }
2909
J-Alvesb15e9402021-09-08 11:44:42 +01002910 if (plat_ffa_notifications_update_bindings_forward(
2911 receiver_vm_id, sender_vm_id, flags, notifications, is_bind,
2912 &ret)) {
J-Alvesb15e9402021-09-08 11:44:42 +01002913 return ret;
2914 }
2915
J-Alvesc003a7a2021-03-18 13:06:53 +00002916 if (notifications == 0U) {
2917 dlog_verbose("No notifications have been specified.\n");
2918 return ffa_error(FFA_INVALID_PARAMETERS);
2919 }
2920
2921 /**
2922 * This check assumes receiver is the current VM, and has been enforced
2923 * by 'plat_ffa_is_notifications_bind_valid'.
2924 */
2925 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
2926
2927 if (receiver_locked.vm == NULL) {
2928 dlog_verbose("Receiver doesn't exist!\n");
2929 return ffa_error(FFA_DENIED);
2930 }
2931
J-Alves09ff9d82021-11-02 11:55:20 +00002932 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesc003a7a2021-03-18 13:06:53 +00002933 dlog_verbose("Notifications are not enabled.\n");
2934 ret = ffa_error(FFA_NOT_SUPPORTED);
2935 goto out;
2936 }
2937
2938 if (is_bind && vm_id_is_current_world(sender_vm_id) &&
2939 vm_find(sender_vm_id) == NULL) {
2940 dlog_verbose("Sender VM does not exist!\n");
2941 ret = ffa_error(FFA_INVALID_PARAMETERS);
2942 goto out;
2943 }
2944
2945 /*
2946 * Can't bind/unbind notifications if at least one is bound to a
2947 * different sender.
2948 */
2949 if (!vm_notifications_validate_bound_sender(
2950 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
2951 id_to_validate, notifications)) {
2952 dlog_verbose("Notifications are bound to other sender.\n");
2953 ret = ffa_error(FFA_DENIED);
2954 goto out;
2955 }
2956
2957 /**
2958 * Check if there is a pending notification within those specified in
2959 * the bitmap.
2960 */
2961 if (vm_are_notifications_pending(receiver_locked,
2962 plat_ffa_is_vm_id(sender_vm_id),
2963 notifications)) {
2964 dlog_verbose("Notifications within '%x' pending.\n",
2965 notifications);
2966 ret = ffa_error(FFA_DENIED);
2967 goto out;
2968 }
2969
2970 vm_notifications_update_bindings(
2971 receiver_locked, plat_ffa_is_vm_id(sender_vm_id), id_to_update,
2972 notifications, is_per_vcpu && is_bind);
2973
2974out:
2975 vm_unlock(&receiver_locked);
2976 return ret;
2977}
J-Alvesaa79c012021-07-09 14:29:45 +01002978
2979struct ffa_value api_ffa_notification_set(
2980 ffa_vm_id_t sender_vm_id, ffa_vm_id_t receiver_vm_id, uint32_t flags,
2981 ffa_notifications_bitmap_t notifications, struct vcpu *current)
2982{
2983 struct ffa_value ret;
2984 struct vm_locked receiver_locked;
2985
2986 /*
2987 * Check if is per-vCPU or global, and extracting vCPU ID according
2988 * to table 17.19 of the FF-A v1.1 Beta 0 spec.
2989 */
2990 bool is_per_vcpu = (flags & FFA_NOTIFICATION_FLAG_PER_VCPU) != 0U;
2991 ffa_vcpu_index_t vcpu_id = (uint16_t)(flags >> 16);
2992
J-Alvesaa79c012021-07-09 14:29:45 +01002993 if (!plat_ffa_is_notification_set_valid(current, sender_vm_id,
2994 receiver_vm_id)) {
2995 dlog_verbose("Invalid use of notifications set interface.\n");
2996 return ffa_error(FFA_INVALID_PARAMETERS);
2997 }
2998
2999 if (notifications == 0U) {
3000 dlog_verbose("No notifications have been specified.\n");
3001 return ffa_error(FFA_INVALID_PARAMETERS);
3002 }
3003
J-Alvesde7bd2f2021-09-09 19:54:35 +01003004 if (plat_ffa_notification_set_forward(sender_vm_id, receiver_vm_id,
3005 flags, notifications, &ret)) {
3006 return ret;
3007 }
3008
J-Alvesaa79c012021-07-09 14:29:45 +01003009 /*
3010 * This check assumes receiver is the current VM, and has been enforced
3011 * by 'plat_ffa_is_notification_set_valid'.
3012 */
3013 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3014
3015 if (receiver_locked.vm == NULL) {
3016 dlog_verbose("Receiver ID is not valid.\n");
3017 return ffa_error(FFA_INVALID_PARAMETERS);
3018 }
3019
J-Alves09ff9d82021-11-02 11:55:20 +00003020 if (!vm_locked_are_notifications_enabled(receiver_locked)) {
J-Alvesaa79c012021-07-09 14:29:45 +01003021 dlog_verbose("Receiver's notifications not enabled.\n");
3022 ret = ffa_error(FFA_DENIED);
3023 goto out;
3024 }
3025
3026 /*
3027 * If notifications are not bound to the sender, they wouldn't be
3028 * enabled either for the receiver.
3029 */
3030 if (!vm_notifications_validate_binding(
3031 receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3032 sender_vm_id, notifications, is_per_vcpu)) {
3033 dlog_verbose("Notifications bindings not valid.\n");
3034 ret = ffa_error(FFA_DENIED);
3035 goto out;
3036 }
3037
3038 if (is_per_vcpu && vcpu_id >= receiver_locked.vm->vcpu_count) {
3039 dlog_verbose("Invalid VCPU ID!\n");
3040 ret = ffa_error(FFA_INVALID_PARAMETERS);
3041 goto out;
3042 }
3043
J-Alves7461ef22021-10-18 17:21:33 +01003044 /* Set notifications pending. */
J-Alvesaa79c012021-07-09 14:29:45 +01003045 vm_notifications_set(receiver_locked, plat_ffa_is_vm_id(sender_vm_id),
3046 notifications, vcpu_id, is_per_vcpu);
3047 dlog_verbose("Set the notifications: %x.\n", notifications);
3048
J-Alves13394022021-06-30 13:48:49 +01003049 if ((FFA_NOTIFICATIONS_FLAG_DELAY_SRI & flags) == 0) {
3050 dlog_verbose("SRI was NOT delayed. vcpu: %u!\n",
3051 vcpu_index(current));
3052 plat_ffa_sri_trigger_not_delayed(current->cpu);
3053 } else {
3054 plat_ffa_sri_state_set(DELAYED);
3055 }
J-Alvesaa79c012021-07-09 14:29:45 +01003056
J-Alves13394022021-06-30 13:48:49 +01003057 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alvesaa79c012021-07-09 14:29:45 +01003058out:
3059 vm_unlock(&receiver_locked);
3060
3061 return ret;
3062}
3063
3064static struct ffa_value api_ffa_notification_get_success_return(
3065 ffa_notifications_bitmap_t from_sp, ffa_notifications_bitmap_t from_vm,
3066 ffa_notifications_bitmap_t from_framework)
3067{
3068 return (struct ffa_value){
3069 .func = FFA_SUCCESS_32,
3070 .arg1 = 0U,
3071 .arg2 = (uint32_t)from_sp,
3072 .arg3 = (uint32_t)(from_sp >> 32),
3073 .arg4 = (uint32_t)from_vm,
3074 .arg5 = (uint32_t)(from_vm >> 32),
3075 .arg6 = (uint32_t)from_framework,
3076 .arg7 = (uint32_t)(from_framework >> 32),
3077 };
3078}
3079
3080struct ffa_value api_ffa_notification_get(ffa_vm_id_t receiver_vm_id,
3081 ffa_vcpu_index_t vcpu_id,
3082 uint32_t flags, struct vcpu *current)
3083{
3084 /* TODO: get framework notifications, when these are supported. */
3085 ffa_notifications_bitmap_t sp_notifications = 0;
3086 ffa_notifications_bitmap_t vm_notifications = 0;
3087 struct vm_locked receiver_locked;
3088 struct ffa_value ret;
3089
3090 /*
3091 * Following check should capture wrong uses of the interface, depending
3092 * on whether Hafnium is SPMC or hypervisor.
3093 * On the rest of the function it is assumed this condition is met.
3094 */
3095 if (!plat_ffa_is_notification_get_valid(current, receiver_vm_id)) {
3096 dlog_verbose("Invalid use of notifications get interface.\n");
3097 return ffa_error(FFA_INVALID_PARAMETERS);
3098 }
3099
3100 /*
3101 * This check assumes receiver is the current VM, and has been enforced
3102 * by `plat_ffa_is_notifications_get_valid`.
3103 */
3104 receiver_locked = plat_ffa_vm_find_locked(receiver_vm_id);
3105
3106 /*
3107 * `plat_ffa_is_notifications_get_valid` ensures following is never
3108 * true.
3109 */
3110 CHECK(receiver_locked.vm != NULL);
3111
3112 if (receiver_locked.vm->vcpu_count <= vcpu_id ||
3113 (receiver_locked.vm->vcpu_count != 1 &&
3114 cpu_index(current->cpu) != vcpu_id)) {
J-Alves1abb3342022-01-05 11:59:10 +00003115 dlog_verbose(
3116 "Invalid VCPU ID %u. vcpu count %u current core: %u!\n",
3117 vcpu_id, receiver_locked.vm->vcpu_count,
3118 cpu_index(current->cpu));
J-Alvesaa79c012021-07-09 14:29:45 +01003119 ret = ffa_error(FFA_INVALID_PARAMETERS);
3120 goto out;
3121 }
3122
3123 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_SP) != 0U) {
J-Alves98ff9562021-09-09 14:39:41 +01003124 if (!plat_ffa_notifications_get_from_sp(
3125 receiver_locked, vcpu_id, &sp_notifications,
3126 &ret)) {
3127 dlog_verbose("Failed to get notifications from sps.");
3128 goto out;
3129 }
J-Alvesaa79c012021-07-09 14:29:45 +01003130 }
3131
3132 if ((flags & FFA_NOTIFICATION_FLAG_BITMAP_VM) != 0U) {
3133 vm_notifications = vm_notifications_get_pending_and_clear(
3134 receiver_locked, true, vcpu_id);
3135 }
3136
3137 ret = api_ffa_notification_get_success_return(sp_notifications,
3138 vm_notifications, 0);
3139
J-Alvesfe23ebe2021-10-13 16:07:07 +01003140 /*
3141 * If there are no more pending notifications, change `sri_state` to
3142 * handled.
3143 */
3144 if (vm_is_notifications_pending_count_zero()) {
3145 plat_ffa_sri_state_set(HANDLED);
3146 }
3147
J-Alves6e2abc62021-12-02 14:58:56 +00003148 if (!receiver_locked.vm->el0_partition &&
3149 !vm_are_global_notifications_pending(receiver_locked)) {
3150 vm_notifications_set_npi_injected(receiver_locked, false);
3151 }
3152
J-Alvesaa79c012021-07-09 14:29:45 +01003153out:
3154 vm_unlock(&receiver_locked);
3155
3156 return ret;
3157}
J-Alvesc8e8a222021-06-08 17:33:52 +01003158
3159/**
3160 * Prepares successful return for FFA_NOTIFICATION_INFO_GET, as described by
3161 * the section 17.7.1 of the FF-A v1.1 Beta0 specification.
3162 */
3163static struct ffa_value api_ffa_notification_info_get_success_return(
3164 const uint16_t *ids, uint32_t ids_count, const uint32_t *lists_sizes,
J-Alvesfe23ebe2021-10-13 16:07:07 +01003165 uint32_t lists_count)
J-Alvesc8e8a222021-06-08 17:33:52 +01003166{
3167 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_64};
3168
3169 /*
3170 * Copying content of ids into ret structure. Use 5 registers (x3-x7) to
3171 * hold the list of ids.
3172 */
3173 memcpy_s(&ret.arg3,
3174 sizeof(ret.arg3) * FFA_NOTIFICATIONS_INFO_GET_REGS_RET, ids,
3175 sizeof(ids[0]) * ids_count);
3176
3177 /*
3178 * According to the spec x2 should have:
3179 * - Bit flagging if there are more notifications pending;
3180 * - The total number of elements (i.e. total list size);
3181 * - The number of VCPU IDs within each VM specific list.
3182 */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003183 ret.arg2 = vm_notifications_pending_not_retrieved_by_scheduler()
3184 ? FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING
3185 : 0;
J-Alvesc8e8a222021-06-08 17:33:52 +01003186
3187 ret.arg2 |= (lists_count & FFA_NOTIFICATIONS_LISTS_COUNT_MASK)
3188 << FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT;
3189
3190 for (unsigned int i = 0; i < lists_count; i++) {
3191 ret.arg2 |= (lists_sizes[i] & FFA_NOTIFICATIONS_LIST_SIZE_MASK)
3192 << FFA_NOTIFICATIONS_LIST_SHIFT(i + 1);
3193 }
3194
3195 return ret;
3196}
3197
3198struct ffa_value api_ffa_notification_info_get(struct vcpu *current)
3199{
3200 /*
3201 * Following set of variables should be populated with the return info.
3202 * At a successfull handling of this interface, they should be used
3203 * to populate the 'ret' structure in accordance to the table 17.29
3204 * of the FF-A v1.1 Beta0 specification.
3205 */
3206 uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS];
3207 uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0};
3208 uint32_t lists_count = 0;
3209 uint32_t ids_count = 0;
3210 bool list_is_full = false;
J-Alves13394022021-06-30 13:48:49 +01003211 struct ffa_value result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003212
3213 /*
3214 * This interface can only be called at NS virtual/physical FF-A
3215 * instance by the endpoint implementing the primary scheduler and the
3216 * Hypervisor/OS kernel.
3217 * In the SPM, following check passes if call has been forwarded from
3218 * the hypervisor.
3219 */
3220 if (current->vm->id != HF_PRIMARY_VM_ID) {
3221 dlog_verbose(
3222 "Only the receiver's scheduler can use this "
3223 "interface\n");
3224 return ffa_error(FFA_NOT_SUPPORTED);
3225 }
3226
J-Alvesca058c22021-09-10 14:02:07 +01003227 /*
3228 * Forward call to the other world, and fill the arrays used to assemble
3229 * return.
3230 */
3231 plat_ffa_notification_info_get_forward(
3232 ids, &ids_count, lists_sizes, &lists_count,
3233 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3234
3235 list_is_full = ids_count == FFA_NOTIFICATIONS_INFO_GET_MAX_IDS;
3236
J-Alvesc8e8a222021-06-08 17:33:52 +01003237 /* Get notifications' info from this world */
3238 for (ffa_vm_count_t index = 0; index < vm_get_count() && !list_is_full;
3239 ++index) {
3240 struct vm_locked vm_locked = vm_lock(vm_find_index(index));
3241
3242 list_is_full = vm_notifications_info_get(
3243 vm_locked, ids, &ids_count, lists_sizes, &lists_count,
3244 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3245
3246 vm_unlock(&vm_locked);
3247 }
3248
3249 if (!list_is_full) {
3250 /* Grab notifications info from other world */
J-Alvesfe23ebe2021-10-13 16:07:07 +01003251 plat_ffa_vm_notifications_info_get(
J-Alvesc8e8a222021-06-08 17:33:52 +01003252 ids, &ids_count, lists_sizes, &lists_count,
3253 FFA_NOTIFICATIONS_INFO_GET_MAX_IDS);
3254 }
3255
3256 if (ids_count == 0) {
J-Alvesca058c22021-09-10 14:02:07 +01003257 dlog_verbose(
3258 "Notification info get has no data to retrieve.\n");
J-Alves13394022021-06-30 13:48:49 +01003259 result = ffa_error(FFA_NO_DATA);
3260 } else {
3261 result = api_ffa_notification_info_get_success_return(
J-Alvesfe23ebe2021-10-13 16:07:07 +01003262 ids, ids_count, lists_sizes, lists_count);
J-Alvesc8e8a222021-06-08 17:33:52 +01003263 }
3264
J-Alvesfe23ebe2021-10-13 16:07:07 +01003265 plat_ffa_sri_state_set(HANDLED);
3266
J-Alves13394022021-06-30 13:48:49 +01003267 return result;
J-Alvesc8e8a222021-06-08 17:33:52 +01003268}
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -07003269
3270struct ffa_value api_ffa_mem_perm_get(vaddr_t base_addr, struct vcpu *current)
3271{
3272 struct vm_locked vm_locked;
3273 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
3274 bool mode_ret = false;
3275 uint32_t mode = 0;
3276
3277 if (!plat_ffa_is_mem_perm_get_valid(current)) {
3278 return ffa_error(FFA_NOT_SUPPORTED);
3279 }
3280
3281 if (!(current->vm->el0_partition)) {
3282 return ffa_error(FFA_DENIED);
3283 }
3284
3285 vm_locked = vm_lock(current->vm);
3286
3287 /*
3288 * mm_get_mode is used to check if the given base_addr page is already
3289 * mapped. If the page is unmapped, return error. If the page is mapped
3290 * appropriate attributes are returned to the caller. Note that
3291 * mm_get_mode returns true if the address is in the valid VA range as
3292 * supported by the architecture and MMU configurations, as opposed to
3293 * whether a page is mapped or not. For a page to be known as mapped,
3294 * the API must return true AND the returned mode must not have
3295 * MM_MODE_INVALID set.
3296 */
3297 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3298 va_add(base_addr, PAGE_SIZE), &mode);
3299 if (!mode_ret || (mode & MM_MODE_INVALID)) {
3300 ret = ffa_error(FFA_INVALID_PARAMETERS);
3301 goto out;
3302 }
3303
3304 /* No memory should be marked RWX */
3305 CHECK((mode & (MM_MODE_R | MM_MODE_W | MM_MODE_X)) !=
3306 (MM_MODE_R | MM_MODE_W | MM_MODE_X));
3307
3308 /*
3309 * S-EL0 partitions are expected to have all their pages marked as
3310 * non-global.
3311 */
3312 CHECK((mode & (MM_MODE_NG | MM_MODE_USER)) ==
3313 (MM_MODE_NG | MM_MODE_USER));
3314
3315 if (mode & MM_MODE_W) {
3316 /* No memory should be writeable but not readable. */
3317 CHECK(mode & MM_MODE_R);
3318 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3319 .arg2 = (uint32_t)(FFA_MEM_PERM_RW)};
3320 } else if (mode & MM_MODE_R) {
3321 ret = (struct ffa_value){.func = FFA_SUCCESS_32,
3322 .arg2 = (uint32_t)(FFA_MEM_PERM_RX)};
3323 if (!(mode & MM_MODE_X)) {
3324 ret.arg2 = (uint32_t)(FFA_MEM_PERM_RO);
3325 }
3326 }
3327out:
3328 vm_unlock(&vm_locked);
3329 return ret;
3330}
3331
3332struct ffa_value api_ffa_mem_perm_set(vaddr_t base_addr, uint32_t page_count,
3333 uint32_t mem_perm, struct vcpu *current)
3334{
3335 struct vm_locked vm_locked;
3336 struct ffa_value ret;
3337 bool mode_ret = false;
3338 uint32_t original_mode;
3339 uint32_t new_mode;
3340 struct mpool local_page_pool;
3341
3342 if (!plat_ffa_is_mem_perm_set_valid(current)) {
3343 return ffa_error(FFA_NOT_SUPPORTED);
3344 }
3345
3346 if (!(current->vm->el0_partition)) {
3347 return ffa_error(FFA_DENIED);
3348 }
3349
3350 if (!is_aligned(va_addr(base_addr), PAGE_SIZE)) {
3351 return ffa_error(FFA_INVALID_PARAMETERS);
3352 }
3353
3354 if ((mem_perm != FFA_MEM_PERM_RW) && (mem_perm != FFA_MEM_PERM_RO) &&
3355 (mem_perm != FFA_MEM_PERM_RX)) {
3356 return ffa_error(FFA_INVALID_PARAMETERS);
3357 }
3358
3359 /*
3360 * Create a local pool so any freed memory can't be used by another
3361 * thread. This is to ensure the original mapping can be restored if any
3362 * stage of the process fails.
3363 */
3364 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
3365
3366 vm_locked = vm_lock(current->vm);
3367
3368 /*
3369 * All regions accessible by the partition are mapped during boot. If we
3370 * cannot get a successful translation for the page range, the request
3371 * to change permissions is rejected.
3372 * mm_get_mode is used to check if the given address range is already
3373 * mapped. If the range is unmapped, return error. If the range is
3374 * mapped appropriate attributes are returned to the caller. Note that
3375 * mm_get_mode returns true if the address is in the valid VA range as
3376 * supported by the architecture and MMU configurations, as opposed to
3377 * whether a page is mapped or not. For a page to be known as mapped,
3378 * the API must return true AND the returned mode must not have
3379 * MM_MODE_INVALID set.
3380 */
3381
3382 mode_ret = mm_get_mode(&vm_locked.vm->ptable, base_addr,
3383 va_add(base_addr, page_count * PAGE_SIZE),
3384 &original_mode);
3385 if (!mode_ret || (original_mode & MM_MODE_INVALID)) {
3386 ret = ffa_error(FFA_INVALID_PARAMETERS);
3387 goto out;
3388 }
3389
3390 /* Device memory cannot be marked as executable */
3391 if ((original_mode & MM_MODE_D) && (mem_perm == FFA_MEM_PERM_RX)) {
3392 ret = ffa_error(FFA_INVALID_PARAMETERS);
3393 goto out;
3394 }
3395
3396 new_mode = MM_MODE_USER | MM_MODE_NG;
3397
3398 if (mem_perm == FFA_MEM_PERM_RW) {
3399 new_mode |= MM_MODE_R | MM_MODE_W;
3400 } else if (mem_perm == FFA_MEM_PERM_RX) {
3401 new_mode |= MM_MODE_R | MM_MODE_X;
3402 } else if (mem_perm == FFA_MEM_PERM_RO) {
3403 new_mode |= MM_MODE_R;
3404 }
3405
3406 /*
3407 * Safe to re-map memory, since we know the requested permissions are
3408 * valid, and the memory requested to be re-mapped is also valid.
3409 */
3410 if (!mm_identity_prepare(
3411 &vm_locked.vm->ptable, pa_from_va(base_addr),
3412 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3413 new_mode, &local_page_pool)) {
3414 /*
3415 * Defrag the table into the local page pool.
3416 * mm_identity_prepare could have allocated or freed pages to
3417 * split blocks or tables etc.
3418 */
3419 mm_stage1_defrag(&vm_locked.vm->ptable, &local_page_pool);
3420
3421 /*
3422 * Guaranteed to succeed mapping with old mode since the mapping
3423 * with old mode already existed and we have a local page pool
3424 * that should have sufficient memory to go back to the original
3425 * state.
3426 */
3427 CHECK(mm_identity_prepare(
3428 &vm_locked.vm->ptable, pa_from_va(base_addr),
3429 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3430 original_mode, &local_page_pool));
3431 mm_identity_commit(
3432 &vm_locked.vm->ptable, pa_from_va(base_addr),
3433 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)),
3434 original_mode, &local_page_pool);
3435
3436 mm_stage1_defrag(&vm_locked.vm->ptable, &api_page_pool);
3437 ret = ffa_error(FFA_NO_MEMORY);
3438 goto out;
3439 }
3440
3441 mm_identity_commit(
3442 &vm_locked.vm->ptable, pa_from_va(base_addr),
3443 pa_from_va(va_add(base_addr, page_count * PAGE_SIZE)), new_mode,
3444 &local_page_pool);
3445
3446 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
3447
3448out:
3449 mpool_fini(&local_page_pool);
3450 vm_unlock(&vm_locked);
3451
3452 return ret;
3453}