blob: eb0511be7debd9f77c2ea425a9c26c64e7cd88b0 [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"
J-Alves917d2f22020-10-30 18:39:30 +000012#include "hf/arch/ffa_memory_handle.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"
Andrew Walbran508e63c2018-12-20 17:02:37 +000015#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020016#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017
Andrew Scull877ae4b2019-07-02 12:52:33 +010018#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000019#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa_internal.h"
21#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000022#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010023#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010025#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010031
Fuad Tabbae4efcc32020-07-16 15:37:27 +010032static_assert(sizeof(struct ffa_partition_info) == 8,
33 "Partition information descriptor size doesn't match the one in "
34 "the FF-A 1.0 EAC specification, Table 82.");
35
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000036/*
37 * To eliminate the risk of deadlocks, we define a partial order for the
38 * acquisition of locks held concurrently by the same physical CPU. Our current
39 * ordering requirements are as follows:
40 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010041 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000042 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010043 * Locks of the same kind require the lock of lowest address to be locked first,
44 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000045 */
46
Andrew Scullaa039b32018-10-04 15:02:26 +010047static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010048 "Currently, a page is mapped for the send and receive buffers so "
49 "the maximum request is the size of a page.");
50
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
52 "The page pool entry size must be at least as big as the mailbox "
53 "size, so that memory region descriptors can be copied from the "
54 "mailbox for memory sharing.");
55
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000056static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000057
58/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000059 * Initialises the API page pool by taking ownership of the contents of the
60 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000061 */
62void api_init(struct mpool *ppool)
63{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000064 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000065}
66
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010067/**
J-Alvesad6a0432021-04-09 16:06:21 +010068 * Get target VM vCPU:
69 * If VM is UP then return first vCPU.
70 * If VM is MP then return vCPU whose index matches current CPU index.
71 */
72static struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
73{
74 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
75 struct vcpu *vcpu = NULL;
76
77 if (vm->vcpu_count == 1) {
78 vcpu = vm_get_vcpu(vm, 0);
79 } else if (current_cpu_index < vm->vcpu_count) {
80 vcpu = vm_get_vcpu(vm, current_cpu_index);
81 }
82
83 return vcpu;
84}
85
86/**
J-Alvesfe7f7372020-11-09 11:32:12 +000087 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
88 * is given as argument of the function.
89 *
90 * Called to change the context between SPs for direct messaging (when Hafnium
91 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
92 *
93 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +010094 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +000095 * - MP with pinned Execution Contexts.
96 */
97static struct vcpu *api_switch_to_vm(struct vcpu *current,
98 struct ffa_value to_ret,
99 enum vcpu_state vcpu_state,
100 ffa_vm_id_t to_id)
101{
102 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100103 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000104
105 CHECK(next != NULL);
106
107 /* Set the return value for the target VM. */
108 arch_regs_set_retval(&next->regs, to_ret);
109
110 /* Set the current vCPU state. */
111 sl_lock(&current->lock);
112 current->state = vcpu_state;
113 sl_unlock(&current->lock);
114
115 return next;
116}
117
118/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000119 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100120 *
121 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100122 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100123 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100124static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100125 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000126 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100127{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000128 /*
129 * If the secondary is blocked but has a timer running, sleep until the
130 * timer fires rather than indefinitely.
131 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100132 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100133 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
134 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100135 if (arch_timer_enabled_current()) {
136 uint64_t remaining_ns =
137 arch_timer_remaining_ns_current();
138
139 if (remaining_ns == 0) {
140 /*
141 * Timer is pending, so the current vCPU should
142 * be run again right away.
143 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100144 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100145 /*
146 * primary_ret.arg1 should already be set to the
147 * current VM ID and vCPU ID.
148 */
149 primary_ret.arg2 = 0;
150 } else {
151 primary_ret.arg2 = remaining_ns;
152 }
153 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100154 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100155 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000156 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100157 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000158
159 default:
160 /* Do nothing. */
161 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000162 }
163
J-Alvesfe7f7372020-11-09 11:32:12 +0000164 return api_switch_to_vm(current, primary_ret, secondary_state,
165 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100166}
167
168/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200169 * Choose next vCPU to run to be the counterpart vCPU in the other
170 * world (run the normal world if currently running in the secure
171 * world). Set current vCPU state to the given vcpu_state parameter.
172 * Set FF-A return values to the target vCPU in the other world.
173 *
174 * Called in context of a direct message response from a secure
175 * partition to a VM.
176 */
177static struct vcpu *api_switch_to_other_world(struct vcpu *current,
178 struct ffa_value other_world_ret,
179 enum vcpu_state vcpu_state)
180{
J-Alvesfe7f7372020-11-09 11:32:12 +0000181 return api_switch_to_vm(current, other_world_ret, vcpu_state,
182 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200183}
184
185/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100186 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
187 * registers the `from` VM to be notified when it becomes available.
188 */
189static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
190{
191 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
192 to.vm->mailbox.recv == NULL) {
193 /*
194 * Fail if the receiver isn't currently ready to receive data,
195 * setting up for notification if requested.
196 */
197 if (notify) {
198 struct wait_entry *entry =
199 vm_get_wait_entry(from, to.vm->id);
200
201 /* Append waiter only if it's not there yet. */
202 if (list_empty(&entry->wait_links)) {
203 list_append(&to.vm->mailbox.waiter_list,
204 &entry->wait_links);
205 }
206 }
207
208 return true;
209 }
210
211 return false;
212}
213
214/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000215 * Returns true if the given vCPU is executing in context of an
216 * FFA_MSG_SEND_DIRECT_REQ invocation.
217 */
218static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
219{
220 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
221}
222
223/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000224 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000225 */
226struct vcpu *api_preempt(struct vcpu *current)
227{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100228 struct ffa_value ret = {
229 .func = FFA_INTERRUPT_32,
230 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000231 };
232
Andrew Sculld6ee1102019-04-05 22:12:42 +0100233 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000234}
235
236/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000237 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000238 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100239 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100240struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100241{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100242 struct ffa_value ret = {
243 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
244 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100245 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000246
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000247 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100248 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100249}
250
251/**
Andrew Walbran33645652019-04-15 12:29:31 +0100252 * Puts the current vCPU in off mode, and returns to the primary VM.
253 */
254struct vcpu *api_vcpu_off(struct vcpu *current)
255{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100256 struct ffa_value ret = {
257 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
258 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100259 };
260
261 /*
262 * Disable the timer, so the scheduler doesn't get told to call back
263 * based on it.
264 */
265 arch_timer_disable_current();
266
267 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
268}
269
270/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000271 * Returns to the primary VM to allow this CPU to be used for other tasks as the
272 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100273 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000274 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000275struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000276{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000277 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
278 struct vcpu_locked current_locked;
279 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000280
281 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000282 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000283 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000284 }
285
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000286 current_locked = vcpu_lock(current);
287 is_direct_request_ongoing =
288 is_ffa_direct_msg_request_ongoing(current_locked);
289 vcpu_unlock(&current_locked);
290
291 if (is_direct_request_ongoing) {
292 return ffa_error(FFA_DENIED);
293 }
294
295 *next = api_switch_to_primary(
296 current,
297 (struct ffa_value){.func = FFA_YIELD_32,
298 .arg1 = ffa_vm_vcpu(current->vm->id,
299 vcpu_index(current))},
300 VCPU_STATE_READY);
301
302 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000303}
304
305/**
Andrew Walbran33645652019-04-15 12:29:31 +0100306 * Switches to the primary so that it can switch to the target, or kick it if it
307 * is already running on a different physical CPU.
308 */
309struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
310{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100311 struct ffa_value ret = {
312 .func = HF_FFA_RUN_WAKE_UP,
313 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
314 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100315 };
316 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
317}
318
319/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000320 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000321 */
322struct vcpu *api_abort(struct vcpu *current)
323{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000325
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100326 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000327 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000328
329 if (current->vm->id == HF_PRIMARY_VM_ID) {
330 /* TODO: what to do when the primary aborts? */
331 for (;;) {
332 /* Do nothing. */
333 }
334 }
335
336 atomic_store_explicit(&current->vm->aborting, true,
337 memory_order_relaxed);
338
339 /* TODO: free resources once all vCPUs abort. */
340
Andrew Sculld6ee1102019-04-05 22:12:42 +0100341 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000342}
343
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100344struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
345 const struct ffa_uuid *uuid)
346{
347 struct vm *current_vm = current->vm;
348 struct vm_locked current_vm_locked;
349 ffa_vm_count_t vm_count = 0;
350 bool uuid_is_null = ffa_uuid_is_null(uuid);
351 struct ffa_value ret;
352 uint32_t size;
353 struct ffa_partition_info partitions[MAX_VMS];
354
355 /*
356 * Iterate through the VMs to find the ones with a matching UUID.
357 * A Null UUID retrieves information for all VMs.
358 */
359 for (uint16_t index = 0; index < vm_get_count(); ++index) {
360 const struct vm *vm = vm_find_index(index);
361
362 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
363 partitions[vm_count].vm_id = vm->id;
364 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100365 partitions[vm_count].properties =
Olivier Deprez764fd2e2020-07-29 15:14:09 +0200366 arch_vm_partition_properties(vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100367
368 ++vm_count;
369 }
370 }
371
372 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
373 if (vm_count == 0) {
374 return ffa_error(FFA_INVALID_PARAMETERS);
375 }
376
377 size = vm_count * sizeof(partitions[0]);
378 if (size > FFA_MSG_PAYLOAD_MAX) {
379 dlog_error(
380 "Partition information does not fit in the VM's RX "
381 "buffer.\n");
382 return ffa_error(FFA_NO_MEMORY);
383 }
384
385 /*
386 * Partition information is returned in the VM's RX buffer, which is why
387 * the lock is needed.
388 */
389 current_vm_locked = vm_lock(current_vm);
390
391 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
392 /*
393 * Can't retrieve memory information if the mailbox is not
394 * available.
395 */
396 dlog_verbose("RX buffer not ready.\n");
397 ret = ffa_error(FFA_BUSY);
398 goto out_unlock;
399 }
400
401 /* Populate the VM's RX buffer with the partition information. */
402 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
403 size);
404 current_vm->mailbox.recv_size = size;
405 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
406 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
407 current_vm->mailbox.state = MAILBOX_STATE_READ;
408
409 /* Return the count of partition information descriptors in w2. */
410 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
411
412out_unlock:
413 vm_unlock(&current_vm_locked);
414
415 return ret;
416}
417
Andrew Scull9726c252019-01-23 13:44:19 +0000418/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000419 * Returns the ID of the VM.
420 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100421struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000422{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100423 return (struct ffa_value){.func = FFA_SUCCESS_32,
424 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000425}
426
427/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000428 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000429 * function to indicate that register state for the given vCPU has been saved
430 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000431 */
432void api_regs_state_saved(struct vcpu *vcpu)
433{
434 sl_lock(&vcpu->lock);
435 vcpu->regs_available = true;
436 sl_unlock(&vcpu->lock);
437}
438
439/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000440 * Retrieves the next waiter and removes it from the wait list if the VM's
441 * mailbox is in a writable state.
442 */
443static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
444{
445 struct wait_entry *entry;
446 struct vm *vm = locked_vm.vm;
447
Andrew Sculld6ee1102019-04-05 22:12:42 +0100448 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000449 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
450 /* The mailbox is not writable or there are no waiters. */
451 return NULL;
452 }
453
454 /* Remove waiter from the wait list. */
455 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
456 wait_links);
457 list_remove(&entry->wait_links);
458 return entry;
459}
460
461/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000462 * Assuming that the arguments have already been checked by the caller, injects
463 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
464 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
465 * is next run, which is up to the scheduler.
466 *
467 * Returns:
468 * - 0 on success if no further action is needed.
469 * - 1 if it was called by the primary VM and the primary VM now needs to wake
470 * up or kick the target vCPU.
471 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200472static int64_t internal_interrupt_inject_locked(
473 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
474 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000475{
Manish Pandey35e452f2021-02-18 21:36:34 +0000476 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000477 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000478 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
479 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000480 int64_t ret = 0;
481
Andrew Walbran508e63c2018-12-20 17:02:37 +0000482 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000483 * We only need to change state and (maybe) trigger a virtual interrupt
484 * if it is enabled and was not previously pending. Otherwise we can
485 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000486 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000487 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
488 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000489 intid_mask)) {
490 goto out;
491 }
492
493 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000494 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
495 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
496 vcpu_irq_count_increment(target_locked);
497 } else {
498 vcpu_fiq_count_increment(target_locked);
499 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000500
501 /*
502 * Only need to update state if there was not already an
503 * interrupt enabled and pending.
504 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000505 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000506 goto out;
507 }
508
Andrew Walbran508e63c2018-12-20 17:02:37 +0000509 if (current->vm->id == HF_PRIMARY_VM_ID) {
510 /*
511 * If the call came from the primary VM, let it know that it
512 * should run or kick the target vCPU.
513 */
514 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000515 } else if (current != target_vcpu && next != NULL) {
516 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000517 }
518
519out:
520 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000521 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000522
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200523 return ret;
524}
525
526/* Wrapper to internal_interrupt_inject with locking of target vCPU */
527static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
528 uint32_t intid, struct vcpu *current,
529 struct vcpu **next)
530{
531 int64_t ret;
532 struct vcpu_locked target_locked;
533
534 target_locked = vcpu_lock(target_vcpu);
535 ret = internal_interrupt_inject_locked(target_locked, intid, current,
536 next);
537 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000538
539 return ret;
540}
541
542/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100543 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
544 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100545 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100547{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000548 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100549 case FFA_MSG_SEND_32:
550 return (struct ffa_value){
551 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000552 .arg1 = (receiver->mailbox.recv_sender << 16) |
553 receiver->id,
554 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000555 default:
556 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000557 dlog_error("Tried to return an invalid message function %#x\n",
558 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100559 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000560 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100561}
562
563/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000564 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000565 * value needs to be forced onto the vCPU.
566 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000567static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100568 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000569{
Max Shvetsov40108e72020-08-27 12:39:50 +0100570 struct vcpu_locked vcpu_locked;
571 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000572 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000573 bool ret;
574
Andrew Scullb06d1752019-02-04 10:15:48 +0000575 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000576 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000577 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100578 * The VM lock is not needed in the common case so it must only be taken
579 * when it is going to be needed. This ensures there are no inter-vCPU
580 * dependencies in the common run case meaning the sensitive context
581 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000582 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100583 vcpu_locked = vcpu_lock(vcpu);
584
585#if SECURE_WORLD == 1
586
587 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
588 0)) {
589 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
590 __func__, vcpu->vm->id, current->cpu->id);
591 }
592
593#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000594
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000595 /* The VM needs to be locked to deliver mailbox messages. */
596 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
597 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100598 vcpu_unlock(&vcpu_locked);
599 vm_locked = vm_lock(vcpu->vm);
600 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000601 }
602
603 /*
604 * If the vCPU is already running somewhere then we can't run it here
605 * simultaneously. While it is actually running then the state should be
606 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
607 * stops running but while Hafnium is in the process of switching back
608 * to the primary there will be a brief period while the state has been
609 * updated but `regs_available` is still false (until
610 * `api_regs_state_saved` is called). We can't start running it again
611 * until this has finished, so count this state as still running for the
612 * purposes of this check.
613 */
614 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
615 /*
616 * vCPU is running on another pCPU.
617 *
618 * It's okay not to return the sleep duration here because the
619 * other physical CPU that is currently running this vCPU will
620 * return the sleep duration if needed.
621 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100622 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000623 ret = false;
624 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000625 }
Andrew Scull9726c252019-01-23 13:44:19 +0000626
627 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100628 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100629 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000630 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100631 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000632 }
633 ret = false;
634 goto out;
635 }
636
Andrew Walbran508e63c2018-12-20 17:02:37 +0000637 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100638 case VCPU_STATE_RUNNING:
639 case VCPU_STATE_OFF:
640 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000641 ret = false;
642 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000643
Andrew Sculld6ee1102019-04-05 22:12:42 +0100644 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000645 /*
646 * A pending message allows the vCPU to run so the message can
647 * be delivered directly.
648 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100649 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100650 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100652 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000653 break;
654 }
655 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100656 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000657 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000658 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000659 break;
660 }
661
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100662 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
663
Andrew Walbran508e63c2018-12-20 17:02:37 +0000664 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100665 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000666 arch_timer_remaining_ns(&vcpu->regs);
667
668 /*
669 * The timer expired so allow the interrupt to be
670 * delivered.
671 */
672 if (timer_remaining_ns == 0) {
673 break;
674 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100675 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000676
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100677 /*
678 * The vCPU is not ready to run, return the appropriate code to
679 * the primary which called vcpu_run.
680 */
681 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100682 ? FFA_MSG_WAIT_32
683 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100684 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
685 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000686
687 ret = false;
688 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000689
Andrew Sculld6ee1102019-04-05 22:12:42 +0100690 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000691 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000692 }
693
Andrew Scullb06d1752019-02-04 10:15:48 +0000694 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000695 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100696 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000697
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000698 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000699 * Mark the registers as unavailable now that we're about to reflect
700 * them onto the real registers. This will also prevent another physical
701 * CPU from trying to read these registers.
702 */
703 vcpu->regs_available = false;
704
705 ret = true;
706
707out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100708 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000709 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100710 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000711 }
712
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000713 return ret;
714}
715
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
717 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100718{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100719 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100720 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100721 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100722
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000723 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100724 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100725 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100726 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100727 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100728
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000729 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100730 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100731 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100732 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100733
Andrew Scull19503262018-09-20 14:48:39 +0100734 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100735 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100736 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100737 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100738 }
739
Fuad Tabbaed294af2019-12-20 10:43:01 +0000740 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100741 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100742 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100743 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100744
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000745 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100746 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000747 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000748 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100749 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000750
Andrew Walbran508e63c2018-12-20 17:02:37 +0000751 /*
752 * Inject timer interrupt if timer has expired. It's safe to access
753 * vcpu->regs here because api_vcpu_prepare_run already made sure that
754 * regs_available was true (and then set it to false) before returning
755 * true.
756 */
757 if (arch_timer_pending(&vcpu->regs)) {
758 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100759 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
760 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000761
762 /*
763 * Set the mask bit so the hardware interrupt doesn't fire
764 * again. Ideally we wouldn't do this because it affects what
765 * the secondary vCPU sees, but if we don't then we end up with
766 * a loop of the interrupt firing each time we try to return to
767 * the secondary vCPU.
768 */
769 arch_timer_mask(&vcpu->regs);
770 }
771
Fuad Tabbaed294af2019-12-20 10:43:01 +0000772 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000773 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000774
Andrew Scull33fecd32019-01-08 14:48:27 +0000775 /*
776 * Set a placeholder return code to the scheduler. This will be
777 * overwritten when the switch back to the primary occurs.
778 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100779 ret.func = FFA_INTERRUPT_32;
780 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100781 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000782
Andrew Scull6d2db332018-10-10 15:28:17 +0100783out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100784 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100785}
786
787/**
Andrew Scull81e85092018-12-12 12:56:20 +0000788 * Check that the mode indicates memory that is valid, owned and exclusive.
789 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100790static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000791{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100792 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
793 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000794}
795
796/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100797 * Determines the value to be returned by api_ffa_rxtx_map and
798 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
799 * there are waiters, it also switches back to the primary VM for it to wake
800 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000801 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100802static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
803 struct vcpu *current,
804 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000805{
806 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000807
808 if (list_empty(&vm->mailbox.waiter_list)) {
809 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100810 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000811 }
812
813 if (vm->id == HF_PRIMARY_VM_ID) {
814 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100815 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000816 }
817
818 /*
819 * Switch back to the primary VM, informing it that there are waiters
820 * that need to be notified.
821 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000822 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100823 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000824 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000825
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100826 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000827}
828
829/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100830 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100831 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100832static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
833 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100834 paddr_t pa_send_begin, paddr_t pa_send_end,
835 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200836 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100837 struct mpool *local_page_pool)
838{
839 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100840
841 /* Map the send page as read-only in the hypervisor address space. */
842 vm_locked.vm->mailbox.send =
843 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200844 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100845 if (!vm_locked.vm->mailbox.send) {
846 /* TODO: partial defrag of failed range. */
847 /* Recover any memory consumed in failed mapping. */
848 mm_defrag(mm_stage1_locked, local_page_pool);
849 goto fail;
850 }
851
852 /*
853 * Map the receive page as writable in the hypervisor address space. On
854 * failure, unmap the send page before returning.
855 */
856 vm_locked.vm->mailbox.recv =
857 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200858 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100859 if (!vm_locked.vm->mailbox.recv) {
860 /* TODO: partial defrag of failed range. */
861 /* Recover any memory consumed in failed mapping. */
862 mm_defrag(mm_stage1_locked, local_page_pool);
863 goto fail_undo_send;
864 }
865
866 ret = true;
867 goto out;
868
869 /*
870 * The following mappings will not require more memory than is available
871 * in the local pool.
872 */
873fail_undo_send:
874 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100875 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
876 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100877
878fail:
879 ret = false;
880
881out:
Andrew Sculle1322792019-07-01 17:46:10 +0100882 return ret;
883}
884
885/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100886 * Sanity checks and configures the send and receive pages in the VM stage-2
887 * and hypervisor stage-1 page tables.
888 *
889 * Returns:
890 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
891 * aligned or are the same.
892 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
893 * due to insuffient page table memory.
894 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
895 * the caller.
896 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100897 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100898
899struct ffa_value api_vm_configure_pages(
900 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
901 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
902 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100903{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100904 struct ffa_value ret;
905 paddr_t pa_send_begin;
906 paddr_t pa_send_end;
907 paddr_t pa_recv_begin;
908 paddr_t pa_recv_end;
909 uint32_t orig_send_mode;
910 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200911 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100912
913 /* We only allow these to be setup once. */
914 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
915 ret = ffa_error(FFA_DENIED);
916 goto out;
917 }
918
919 /* Hafnium only supports a fixed size of RX/TX buffers. */
920 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
921 ret = ffa_error(FFA_INVALID_PARAMETERS);
922 goto out;
923 }
924
925 /* Fail if addresses are not page-aligned. */
926 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
927 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
928 ret = ffa_error(FFA_INVALID_PARAMETERS);
929 goto out;
930 }
931
932 /* Convert to physical addresses. */
933 pa_send_begin = pa_from_ipa(send);
934 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
935 pa_recv_begin = pa_from_ipa(recv);
936 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
937
938 /* Fail if the same page is used for the send and receive pages. */
939 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
940 ret = ffa_error(FFA_INVALID_PARAMETERS);
941 goto out;
942 }
Andrew Sculle1322792019-07-01 17:46:10 +0100943
944 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100945 * Ensure the pages are valid, owned and exclusive to the VM and that
946 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100947 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100948 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
949 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
950 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
951 (orig_send_mode & MM_MODE_R) == 0 ||
952 (orig_send_mode & MM_MODE_W) == 0) {
953 ret = ffa_error(FFA_DENIED);
954 goto out;
955 }
956
957 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
958 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
959 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
960 (orig_recv_mode & MM_MODE_R) == 0) {
961 ret = ffa_error(FFA_DENIED);
962 goto out;
963 }
Andrew Sculle1322792019-07-01 17:46:10 +0100964
965 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000966 if (!vm_identity_map(
967 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100968 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100969 local_page_pool, NULL)) {
970 ret = ffa_error(FFA_NO_MEMORY);
971 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100972 }
973
Andrew Scull3c257452019-11-26 13:32:50 +0000974 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
975 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100976 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100977 /* TODO: partial defrag of failed range. */
978 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100979 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100980 goto fail_undo_send;
981 }
982
Olivier Deprez96a2a262020-06-11 17:21:38 +0200983 /* Get extra send/recv pages mapping attributes for the given VM ID. */
984 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
985
Manish Pandeyd34f8892020-06-19 17:41:07 +0100986 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
987 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200988 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100989 goto fail_undo_send_and_recv;
990 }
991
Manish Pandeyd34f8892020-06-19 17:41:07 +0100992 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100993 goto out;
994
Andrew Sculle1322792019-07-01 17:46:10 +0100995fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000996 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100997 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100998
999fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001000 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001001 orig_send_mode, local_page_pool, NULL));
1002 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001003
1004out:
Andrew Sculle1322792019-07-01 17:46:10 +01001005 return ret;
1006}
1007
1008/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001009 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001010 * must not be shared. Locking of the page tables combined with a local memory
1011 * pool ensures there will always be enough memory to recover from any errors
1012 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1013 * by another core which could result in this transaction being unable to roll
1014 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001015 *
1016 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001018 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001019 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001020 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001022 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 * - FFA_SUCCESS on success if no further action is needed.
1024 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001025 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001026 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1028 uint32_t page_count, struct vcpu *current,
1029 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001030{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001031 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001032 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001033 struct vm_locked vm_locked;
1034 struct mm_stage1_locked mm_stage1_locked;
1035 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001036
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001037 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001038 * Create a local pool so any freed memory can't be used by another
1039 * thread. This is to ensure the original mapping can be restored if any
1040 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001041 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001042 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1043
Andrew Sculle1322792019-07-01 17:46:10 +01001044 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001045 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001046
Manish Pandeyd34f8892020-06-19 17:41:07 +01001047 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1048 page_count, &local_page_pool);
1049 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001050 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001051 }
1052
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001053 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001054 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001055
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001056exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001057 mpool_fini(&local_page_pool);
1058
1059 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001060 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061
1062 return ret;
1063}
1064
1065/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001066 * Notifies the `to` VM about the message currently in its mailbox, possibly
1067 * with the help of the primary VM.
1068 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001069static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1070 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001071{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1073 struct ffa_value primary_ret = {
1074 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001075 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001076 };
1077
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001078 /* Messages for the primary VM are delivered directly. */
1079 if (to.vm->id == HF_PRIMARY_VM_ID) {
1080 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001081 * Only tell the primary VM the size and other details if the
1082 * message is for it, to avoid leaking data about messages for
1083 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001084 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001086
1087 to.vm->mailbox.state = MAILBOX_STATE_READ;
1088 *next = api_switch_to_primary(current, primary_ret,
1089 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001090 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001091 }
1092
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001093 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1094
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001095 /* Messages for the TEE are sent on via the dispatcher. */
1096 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001097 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001098
Olivier Deprez112d2b52020-09-30 07:39:23 +02001099 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001100 /*
1101 * After the call to the TEE completes it must have finished
1102 * reading its RX buffer, so it is ready for another message.
1103 */
1104 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001105 /*
1106 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001107 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001108 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001109 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001110 }
1111
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001112 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001113 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001114 *next = api_switch_to_primary(current, primary_ret,
1115 VCPU_STATE_READY);
1116 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001117
1118 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001119}
1120
1121/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001122 * Copies data from the sender's send buffer to the recipient's receive buffer
1123 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001124 *
1125 * If the recipient's receive buffer is busy, it can optionally register the
1126 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001127 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1129 ffa_vm_id_t receiver_vm_id, uint32_t size,
1130 uint32_t attributes, struct vcpu *current,
1131 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001132{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001133 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001134 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001135 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001136 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001137 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001138 struct vcpu_locked current_locked;
1139 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001140 bool notify =
1141 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001142
Andrew Walbran70bc8622019-10-07 14:15:58 +01001143 /* Ensure sender VM ID corresponds to the current VM. */
1144 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001145 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001146 }
1147
1148 /* Disallow reflexive requests as this suggests an error in the VM. */
1149 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001151 }
1152
1153 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 if (size > FFA_MSG_PAYLOAD_MAX) {
1155 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001156 }
1157
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001158 /*
1159 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1160 * invocation.
1161 */
1162 current_locked = vcpu_lock(current);
1163 is_direct_request_ongoing =
1164 is_ffa_direct_msg_request_ongoing(current_locked);
1165 vcpu_unlock(&current_locked);
1166
1167 if (is_direct_request_ongoing) {
1168 return ffa_error(FFA_DENIED);
1169 }
1170
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001171 /* Ensure the receiver VM exists. */
1172 to = vm_find(receiver_vm_id);
1173 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001174 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001175 }
1176
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001177 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001178 * Check that the sender has configured its send buffer. If the tx
1179 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1180 * be safely accessed after releasing the lock since the tx mailbox
1181 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001182 */
1183 sl_lock(&from->lock);
1184 from_msg = from->mailbox.send;
1185 sl_unlock(&from->lock);
1186
1187 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001189 }
1190
Andrew Walbran82d6d152019-12-24 15:02:06 +00001191 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001192
Andrew Walbran82d6d152019-12-24 15:02:06 +00001193 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001194 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001195 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001196 }
1197
Andrew Walbran82d6d152019-12-24 15:02:06 +00001198 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001200 to->mailbox.recv_size = size;
1201 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001202 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001203 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001204
1205out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001206 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001207
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001208 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001209}
1210
1211/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001212 * Checks whether the vCPU's attempt to block for a message has already been
1213 * interrupted or whether it is allowed to block.
1214 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001215bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001216{
Manish Pandey35e452f2021-02-18 21:36:34 +00001217 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001218 bool interrupted;
1219
Manish Pandey35e452f2021-02-18 21:36:34 +00001220 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001221
1222 /*
1223 * Don't block if there are enabled and pending interrupts, to match
1224 * behaviour of wait_for_interrupt.
1225 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001226 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001227
Manish Pandey35e452f2021-02-18 21:36:34 +00001228 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001229
1230 return interrupted;
1231}
1232
1233/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001234 * Receives a message from the mailbox. If one isn't available, this function
1235 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001236 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001237 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001238 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1240 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001241{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001242 bool is_direct_request_ongoing;
1243 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001244 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001245 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001246 bool is_from_secure_world =
1247 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001248
Andrew Scullaa039b32018-10-04 15:02:26 +01001249 /*
1250 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001251 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001252 */
J-Alvesb37fd082020-10-22 12:29:21 +01001253 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001255 }
1256
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001257 /*
1258 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1259 * invocation.
1260 */
1261 current_locked = vcpu_lock(current);
1262 is_direct_request_ongoing =
1263 is_ffa_direct_msg_request_ongoing(current_locked);
1264 vcpu_unlock(&current_locked);
1265
1266 if (is_direct_request_ongoing) {
1267 return ffa_error(FFA_DENIED);
1268 }
1269
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001270 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001271
Andrew Scullaa039b32018-10-04 15:02:26 +01001272 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001273 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1274 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001275 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001276 goto out;
1277 }
1278
1279 /* No pending message so fail if not allowed to block. */
1280 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001281 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001282 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001283 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001284
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001285 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001286 * From this point onward this call can only be interrupted or a message
1287 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001288 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001289 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 return_code = ffa_error(FFA_INTERRUPTED);
1291 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001292 goto out;
1293 }
1294
J-Alvesb37fd082020-10-22 12:29:21 +01001295 if (is_from_secure_world) {
1296 /* Return to other world if caller is a SP. */
1297 *next = api_switch_to_other_world(
1298 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1299 VCPU_STATE_BLOCKED_MAILBOX);
1300 } else {
1301 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001302 struct ffa_value run_return = {
1303 .func = FFA_MSG_WAIT_32,
1304 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001305 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001306
Andrew Walbranb4816552018-12-05 17:35:42 +00001307 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001308 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001309 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001310out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001311 sl_unlock(&vm->lock);
1312
Jose Marinho3e2442f2019-03-12 13:30:37 +00001313 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001314}
1315
1316/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001317 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1318 * by this function, the caller must have called api_mailbox_send before with
1319 * the notify argument set to true, and this call must have failed because the
1320 * mailbox was not available.
1321 *
1322 * It should be called repeatedly to retrieve a list of VMs.
1323 *
1324 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1325 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001326 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001327int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001328{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001329 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001330 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001331 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001332
1333 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001334 if (list_empty(&vm->mailbox.ready_list)) {
1335 ret = -1;
1336 goto exit;
1337 }
1338
1339 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1340 ready_links);
1341 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001342 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001343
1344exit:
1345 sl_unlock(&vm->lock);
1346 return ret;
1347}
1348
1349/**
1350 * Retrieves the next VM waiting to be notified that the mailbox of the
1351 * specified VM became writable. Only primary VMs are allowed to call this.
1352 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001353 * Returns -1 on failure or if there are no waiters; the VM id of the next
1354 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001355 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001357{
1358 struct vm *vm;
1359 struct vm_locked locked;
1360 struct wait_entry *entry;
1361 struct vm *waiting_vm;
1362
1363 /* Only primary VMs are allowed to call this function. */
1364 if (current->vm->id != HF_PRIMARY_VM_ID) {
1365 return -1;
1366 }
1367
Andrew Walbran42347a92019-05-09 13:59:03 +01001368 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001369 if (vm == NULL) {
1370 return -1;
1371 }
1372
Fuad Tabbaed294af2019-12-20 10:43:01 +00001373 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001374 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001375 entry = api_fetch_waiter(locked);
1376 vm_unlock(&locked);
1377
1378 if (entry == NULL) {
1379 return -1;
1380 }
1381
1382 /* Enqueue notification to waiting VM. */
1383 waiting_vm = entry->waiting_vm;
1384
1385 sl_lock(&waiting_vm->lock);
1386 if (list_empty(&entry->ready_links)) {
1387 list_append(&waiting_vm->mailbox.ready_list,
1388 &entry->ready_links);
1389 }
1390 sl_unlock(&waiting_vm->lock);
1391
1392 return waiting_vm->id;
1393}
1394
1395/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001396 * Releases the caller's mailbox so that a new message can be received. The
1397 * caller must have copied out all data they wish to preserve as new messages
1398 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001399 *
1400 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1402 * - FFA_SUCCESS on success if no further action is needed.
1403 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001404 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001405 * hf_mailbox_waiter_get.
1406 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001407struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001408{
1409 struct vm *vm = current->vm;
1410 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001412
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001413 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001414 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001415 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001416 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001417 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001418 break;
1419
Andrew Sculld6ee1102019-04-05 22:12:42 +01001420 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001421 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001422 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001423 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001424 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001425 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001426
1427 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001428}
Andrew Walbran318f5732018-11-20 16:23:42 +00001429
1430/**
1431 * Enables or disables a given interrupt ID for the calling vCPU.
1432 *
1433 * Returns 0 on success, or -1 if the intid is invalid.
1434 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001435int64_t api_interrupt_enable(uint32_t intid, bool enable,
1436 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001437{
Manish Pandey35e452f2021-02-18 21:36:34 +00001438 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001439 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001440 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1441 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001442
Andrew Walbran318f5732018-11-20 16:23:42 +00001443 if (intid >= HF_NUM_INTIDS) {
1444 return -1;
1445 }
1446
Manish Pandey35e452f2021-02-18 21:36:34 +00001447 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001448 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001449 /*
1450 * If it is pending and was not enabled before, increment the
1451 * count.
1452 */
1453 if (current->interrupts.interrupt_pending[intid_index] &
1454 ~current->interrupts.interrupt_enabled[intid_index] &
1455 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001456 if ((current->interrupts.interrupt_type[intid_index] &
1457 intid_mask) ==
1458 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1459 vcpu_irq_count_increment(current_locked);
1460 } else {
1461 vcpu_fiq_count_increment(current_locked);
1462 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001463 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001464 current->interrupts.interrupt_enabled[intid_index] |=
1465 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001466
1467 if (type == INTERRUPT_TYPE_IRQ) {
1468 current->interrupts.interrupt_type[intid_index] &=
1469 ~intid_mask;
1470 } else if (type == INTERRUPT_TYPE_FIQ) {
1471 current->interrupts.interrupt_type[intid_index] |=
1472 intid_mask;
1473 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001474 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001475 /*
1476 * If it is pending and was enabled before, decrement the count.
1477 */
1478 if (current->interrupts.interrupt_pending[intid_index] &
1479 current->interrupts.interrupt_enabled[intid_index] &
1480 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001481 if ((current->interrupts.interrupt_type[intid_index] &
1482 intid_mask) ==
1483 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1484 vcpu_irq_count_decrement(current_locked);
1485 } else {
1486 vcpu_fiq_count_decrement(current_locked);
1487 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001488 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001489 current->interrupts.interrupt_enabled[intid_index] &=
1490 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001491 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001492 }
1493
Manish Pandey35e452f2021-02-18 21:36:34 +00001494 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001495 return 0;
1496}
1497
1498/**
1499 * Returns the ID of the next pending interrupt for the calling vCPU, and
1500 * acknowledges it (i.e. marks it as no longer pending). Returns
1501 * HF_INVALID_INTID if there are no pending interrupts.
1502 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001503uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001504{
1505 uint8_t i;
1506 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001507 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001508
1509 /*
1510 * Find the first enabled and pending interrupt ID, return it, and
1511 * deactivate it.
1512 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001513 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001514 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1515 uint32_t enabled_and_pending =
1516 current->interrupts.interrupt_enabled[i] &
1517 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001518
Andrew Walbran318f5732018-11-20 16:23:42 +00001519 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001520 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001521 uint32_t intid_mask = 1U << bit_index;
1522
Andrew Walbran3d84a262018-12-13 14:41:19 +00001523 /*
1524 * Mark it as no longer pending and decrement the count.
1525 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001526 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1527
1528 if ((current->interrupts.interrupt_type[i] &
1529 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1530 vcpu_irq_count_decrement(current_locked);
1531 } else {
1532 vcpu_fiq_count_decrement(current_locked);
1533 }
1534
Andrew Walbran3d84a262018-12-13 14:41:19 +00001535 first_interrupt =
1536 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001537 break;
1538 }
1539 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001540
Manish Pandey35e452f2021-02-18 21:36:34 +00001541 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001542 return first_interrupt;
1543}
1544
1545/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001546 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001547 * given VM and vCPU.
1548 */
1549static inline bool is_injection_allowed(uint32_t target_vm_id,
1550 struct vcpu *current)
1551{
1552 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001553
Andrew Walbran318f5732018-11-20 16:23:42 +00001554 /*
1555 * The primary VM is allowed to inject interrupts into any VM. Secondary
1556 * VMs are only allowed to inject interrupts into their own vCPUs.
1557 */
1558 return current_vm_id == HF_PRIMARY_VM_ID ||
1559 current_vm_id == target_vm_id;
1560}
1561
1562/**
1563 * Injects a virtual interrupt of the given ID into the given target vCPU.
1564 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1565 * when the vCPU is next run, which is up to the scheduler.
1566 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001567 * Returns:
1568 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1569 * ID is invalid, or the current VM is not allowed to inject interrupts to
1570 * the target VM.
1571 * - 0 on success if no further action is needed.
1572 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1573 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001574 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001575int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1576 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001577 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001578{
Andrew Walbran318f5732018-11-20 16:23:42 +00001579 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001580 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001581
1582 if (intid >= HF_NUM_INTIDS) {
1583 return -1;
1584 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001585
Andrew Walbran318f5732018-11-20 16:23:42 +00001586 if (target_vm == NULL) {
1587 return -1;
1588 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001589
Andrew Walbran318f5732018-11-20 16:23:42 +00001590 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001591 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001592 return -1;
1593 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001594
Andrew Walbran318f5732018-11-20 16:23:42 +00001595 if (!is_injection_allowed(target_vm_id, current)) {
1596 return -1;
1597 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001598
Andrew Walbrane1310df2019-04-29 17:28:28 +01001599 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001600
Manish Pandey35e452f2021-02-18 21:36:34 +00001601 dlog_verbose(
1602 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1603 "%u\n",
1604 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1605 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001606 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001607}
Andrew Scull6386f252018-12-06 13:29:10 +00001608
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001609/** Returns the version of the implemented FF-A specification. */
1610struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001611{
1612 /*
1613 * Ensure that both major and minor revision representation occupies at
1614 * most 15 bits.
1615 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001616 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001617 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001618 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001619 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001621 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001622 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001623 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001624
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001625 return (struct ffa_value){
1626 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1627 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001628}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001629
1630int64_t api_debug_log(char c, struct vcpu *current)
1631{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001632 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001633 struct vm *vm = current->vm;
1634 struct vm_locked vm_locked = vm_lock(vm);
1635
Andrew Sculld54e1be2019-08-20 11:09:42 +01001636 if (c == '\n' || c == '\0') {
1637 flush = true;
1638 } else {
1639 vm->log_buffer[vm->log_buffer_length++] = c;
1640 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1641 }
1642
1643 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001644 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1645 vm->log_buffer_length);
1646 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001647 }
1648
1649 vm_unlock(&vm_locked);
1650
1651 return 0;
1652}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001653
1654/**
1655 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001657 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001658struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001659{
1660 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001661 case FFA_ERROR_32:
1662 case FFA_SUCCESS_32:
1663 case FFA_INTERRUPT_32:
1664 case FFA_VERSION_32:
1665 case FFA_FEATURES_32:
1666 case FFA_RX_RELEASE_32:
1667 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001668 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001669 case FFA_ID_GET_32:
1670 case FFA_MSG_POLL_32:
1671 case FFA_MSG_WAIT_32:
1672 case FFA_YIELD_32:
1673 case FFA_RUN_32:
1674 case FFA_MSG_SEND_32:
1675 case FFA_MEM_DONATE_32:
1676 case FFA_MEM_LEND_32:
1677 case FFA_MEM_SHARE_32:
1678 case FFA_MEM_RETRIEVE_REQ_32:
1679 case FFA_MEM_RETRIEVE_RESP_32:
1680 case FFA_MEM_RELINQUISH_32:
1681 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001682 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001683 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001684 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001685 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001686 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001687 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001688 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001689 }
1690}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001691
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001692/**
J-Alves645eabe2021-02-22 16:08:27 +00001693 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1694 * interfaces.
1695 */
1696static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1697{
1698 return args.arg2 == 0U;
1699}
1700
1701/**
J-Alves76d99af2021-03-10 17:42:11 +00001702 * Limits size of arguments in ffa_value structure to 32-bit.
1703 */
1704static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1705{
1706 return (struct ffa_value){
1707 .func = (uint32_t)args.func,
1708 .arg1 = (uint32_t)args.arg1,
1709 .arg2 = (uint32_t)0,
1710 .arg3 = (uint32_t)args.arg3,
1711 .arg4 = (uint32_t)args.arg4,
1712 .arg5 = (uint32_t)args.arg5,
1713 .arg6 = (uint32_t)args.arg6,
1714 .arg7 = (uint32_t)args.arg7,
1715 };
1716}
1717
1718/**
1719 * Helper to copy direct message payload, depending on SMC used and expected
1720 * registers size.
1721 */
1722static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1723{
1724 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1725 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1726 return api_ffa_value_copy32(args);
1727 }
1728
1729 return (struct ffa_value){
1730 .func = args.func,
1731 .arg1 = args.arg1,
1732 .arg2 = 0,
1733 .arg3 = args.arg3,
1734 .arg4 = args.arg4,
1735 .arg5 = args.arg5,
1736 .arg6 = args.arg6,
1737 .arg7 = args.arg7,
1738 };
1739}
1740
1741/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001742 * Send an FF-A direct message request.
1743 */
1744struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1745 ffa_vm_id_t receiver_vm_id,
1746 struct ffa_value args,
1747 struct vcpu *current,
1748 struct vcpu **next)
1749{
1750 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001751 struct vm *receiver_vm;
1752 struct vcpu *receiver_vcpu;
1753 struct two_vcpu_locked vcpus_locked;
1754
J-Alves645eabe2021-02-22 16:08:27 +00001755 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1756 return ffa_error(FFA_INVALID_PARAMETERS);
1757 }
1758
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001759 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1760 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001761 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001762 }
1763
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001764 receiver_vm = vm_find(receiver_vm_id);
1765 if (receiver_vm == NULL) {
1766 return ffa_error(FFA_INVALID_PARAMETERS);
1767 }
1768
1769 /*
1770 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1771 * UP (migratable) or MP partitions with a number of vCPUs matching the
1772 * number of PEs in the system. It further states that MP partitions
1773 * accepting direct request messages cannot migrate.
1774 */
J-Alvesad6a0432021-04-09 16:06:21 +01001775 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001776 if (receiver_vcpu == NULL) {
1777 return ffa_error(FFA_INVALID_PARAMETERS);
1778 }
1779
1780 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1781
1782 /*
1783 * If destination vCPU is executing or already received an
1784 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1785 * busy. There is a brief period of time where the vCPU state has
1786 * changed but regs_available is still false thus consider this case as
1787 * the vCPU not yet ready to receive a direct message request.
1788 */
1789 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1790 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1791 !receiver_vcpu->regs_available) {
1792 ret = ffa_error(FFA_BUSY);
1793 goto out;
1794 }
1795
1796 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1797 memory_order_relaxed)) {
1798 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001799 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001800 receiver_vcpu->vm->id,
1801 vcpu_index(receiver_vcpu));
1802 receiver_vcpu->state = VCPU_STATE_ABORTED;
1803 }
1804
1805 ret = ffa_error(FFA_ABORTED);
1806 goto out;
1807 }
1808
1809 switch (receiver_vcpu->state) {
1810 case VCPU_STATE_OFF:
1811 case VCPU_STATE_RUNNING:
1812 case VCPU_STATE_ABORTED:
1813 case VCPU_STATE_READY:
1814 case VCPU_STATE_BLOCKED_INTERRUPT:
1815 ret = ffa_error(FFA_BUSY);
1816 goto out;
1817 case VCPU_STATE_BLOCKED_MAILBOX:
1818 /*
1819 * Expect target vCPU to be blocked after having called
1820 * ffa_msg_wait or sent a direct message response.
1821 */
1822 break;
1823 }
1824
1825 /* Inject timer interrupt if any pending */
1826 if (arch_timer_pending(&receiver_vcpu->regs)) {
1827 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1828 HF_VIRTUAL_TIMER_INTID,
1829 current, NULL);
1830
1831 arch_timer_mask(&receiver_vcpu->regs);
1832 }
1833
1834 /* The receiver vCPU runs upon direct message invocation */
1835 receiver_vcpu->cpu = current->cpu;
1836 receiver_vcpu->state = VCPU_STATE_RUNNING;
1837 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001838 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001839
J-Alves76d99af2021-03-10 17:42:11 +00001840 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001841
1842 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1843
1844 /* Switch to receiver vCPU targeted to by direct msg request */
1845 *next = receiver_vcpu;
1846
1847 /*
1848 * Since this flow will lead to a VM switch, the return value will not
1849 * be applied to current vCPU.
1850 */
1851
1852out:
1853 sl_unlock(&receiver_vcpu->lock);
1854 sl_unlock(&current->lock);
1855
1856 return ret;
1857}
1858
1859/**
1860 * Send an FF-A direct message response.
1861 */
1862struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1863 ffa_vm_id_t receiver_vm_id,
1864 struct ffa_value args,
1865 struct vcpu *current,
1866 struct vcpu **next)
1867{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001868 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00001869
1870 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1871 return ffa_error(FFA_INVALID_PARAMETERS);
1872 }
1873
J-Alves76d99af2021-03-10 17:42:11 +00001874 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001875
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001876 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1877 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001878 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001879 }
1880
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001881 current_locked = vcpu_lock(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001882
1883 /*
1884 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1885 * defined originator.
1886 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001887 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001888 /*
1889 * Sending direct response but direct request origin vCPU is
1890 * not set.
1891 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001892 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001893 return ffa_error(FFA_DENIED);
1894 }
1895
1896 if (current->direct_request_origin_vm_id != receiver_vm_id) {
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001897 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001898 return ffa_error(FFA_DENIED);
1899 }
1900
1901 /* Clear direct request origin for the caller. */
1902 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1903
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001904 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001905
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001906 if (!vm_id_is_current_world(receiver_vm_id)) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001907 *next = api_switch_to_other_world(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001908 VCPU_STATE_BLOCKED_MAILBOX);
1909 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001910 *next = api_switch_to_primary(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001911 VCPU_STATE_BLOCKED_MAILBOX);
J-Alvesfe7f7372020-11-09 11:32:12 +00001912 } else if (vm_id_is_current_world(receiver_vm_id)) {
1913 /*
1914 * It is expected the receiver_vm_id to be from an SP, otherwise
1915 * 'arch_other_world_is_direct_response_valid' should have
1916 * made function return error before getting to this point.
1917 */
1918 *next = api_switch_to_vm(current, to_ret,
1919 VCPU_STATE_BLOCKED_MAILBOX,
1920 receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001921 } else {
1922 panic("Invalid direct message response invocation");
1923 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001924
1925 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1926}
1927
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001928struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1929 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001930 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001931{
1932 struct vm *from = current->vm;
1933 struct vm *to;
1934 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001935 struct ffa_memory_region *memory_region;
1936 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001937
1938 if (ipa_addr(address) != 0 || page_count != 0) {
1939 /*
1940 * Hafnium only supports passing the descriptor in the TX
1941 * mailbox.
1942 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001943 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001944 }
1945
Andrew Walbranca808b12020-05-15 17:22:28 +01001946 if (fragment_length > length) {
1947 dlog_verbose(
1948 "Fragment length %d greater than total length %d.\n",
1949 fragment_length, length);
1950 return ffa_error(FFA_INVALID_PARAMETERS);
1951 }
1952 if (fragment_length < sizeof(struct ffa_memory_region) +
1953 sizeof(struct ffa_memory_access)) {
1954 dlog_verbose(
1955 "Initial fragment length %d smaller than header size "
1956 "%d.\n",
1957 fragment_length,
1958 sizeof(struct ffa_memory_region) +
1959 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001960 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001961 }
1962
1963 /*
1964 * Check that the sender has configured its send buffer. If the TX
1965 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1966 * be safely accessed after releasing the lock since the TX mailbox
1967 * address can only be configured once.
1968 */
1969 sl_lock(&from->lock);
1970 from_msg = from->mailbox.send;
1971 sl_unlock(&from->lock);
1972
1973 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001974 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001975 }
1976
1977 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001978 * Copy the memory region descriptor to a fresh page from the memory
1979 * pool. This prevents the sender from changing it underneath us, and
1980 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001981 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001982 if (fragment_length > HF_MAILBOX_SIZE ||
1983 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001984 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001985 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001986 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001987 if (memory_region == NULL) {
1988 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001990 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001991 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001992
1993 /* The sender must match the caller. */
1994 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001995 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001996 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001997 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001998 }
1999
Andrew Walbrana65a1322020-04-06 19:32:32 +01002000 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002001 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002002 dlog_verbose(
2003 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002004 "endpoint memory access descriptors, expected 1).\n",
2005 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002006 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002007 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002008 }
2009
2010 /*
2011 * Ensure that the receiver VM exists and isn't the same as the sender.
2012 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002013 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002014 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002015 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002016 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002017 goto out;
2018 }
2019
2020 if (to->id == HF_TEE_VM_ID) {
2021 /*
2022 * The 'to' VM lock is only needed in the case that it is the
2023 * TEE VM.
2024 */
2025 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2026
2027 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002028 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002029 goto out_unlock;
2030 }
2031
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002032 ret = ffa_memory_tee_send(
2033 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2034 length, fragment_length, share_func, &api_page_pool);
2035 /*
2036 * ffa_tee_memory_send takes ownership of the memory_region, so
2037 * make sure we don't free it.
2038 */
2039 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002040
2041 out_unlock:
2042 vm_unlock(&vm_to_from_lock.vm1);
2043 vm_unlock(&vm_to_from_lock.vm2);
2044 } else {
2045 struct vm_locked from_locked = vm_lock(from);
2046
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002047 ret = ffa_memory_send(from_locked, memory_region, length,
2048 fragment_length, share_func,
2049 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002050 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002051 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002052 * make sure we don't free it.
2053 */
2054 memory_region = NULL;
2055
2056 vm_unlock(&from_locked);
2057 }
2058
2059out:
2060 if (memory_region != NULL) {
2061 mpool_free(&api_page_pool, memory_region);
2062 }
2063
2064 return ret;
2065}
2066
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002067struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2068 uint32_t fragment_length,
2069 ipaddr_t address, uint32_t page_count,
2070 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002071{
2072 struct vm *to = current->vm;
2073 struct vm_locked to_locked;
2074 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002075 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002076 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002078
2079 if (ipa_addr(address) != 0 || page_count != 0) {
2080 /*
2081 * Hafnium only supports passing the descriptor in the TX
2082 * mailbox.
2083 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002084 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002085 }
2086
Andrew Walbrana65a1322020-04-06 19:32:32 +01002087 if (fragment_length != length) {
2088 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002090 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002091
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002092 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002093 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002094 message_buffer_size = cpu_get_buffer_size(current->cpu);
2095 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2096 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002097 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002098 }
2099
2100 to_locked = vm_lock(to);
2101 to_msg = to->mailbox.send;
2102
2103 if (to_msg == NULL) {
2104 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002105 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002106 goto out;
2107 }
2108
2109 /*
2110 * Copy the retrieve request descriptor to an internal buffer, so that
2111 * the caller can't change it underneath us.
2112 */
2113 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2114
2115 if (msg_receiver_busy(to_locked, NULL, false)) {
2116 /*
2117 * Can't retrieve memory information if the mailbox is not
2118 * available.
2119 */
2120 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002121 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002122 goto out;
2123 }
2124
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2126 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002127
2128out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002129 vm_unlock(&to_locked);
2130 return ret;
2131}
2132
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002133struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002134{
2135 struct vm *from = current->vm;
2136 struct vm_locked from_locked;
2137 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002138 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002139 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002140 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002141 uint32_t length;
2142
2143 from_locked = vm_lock(from);
2144 from_msg = from->mailbox.send;
2145
2146 if (from_msg == NULL) {
2147 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002149 goto out;
2150 }
2151
2152 /*
2153 * Calculate length from relinquish descriptor before copying. We will
2154 * check again later to make sure it hasn't changed.
2155 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002156 length = sizeof(struct ffa_mem_relinquish) +
2157 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2158 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002159 /*
2160 * Copy the relinquish descriptor to an internal buffer, so that the
2161 * caller can't change it underneath us.
2162 */
2163 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002164 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002165 message_buffer_size = cpu_get_buffer_size(current->cpu);
2166 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2167 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169 goto out;
2170 }
2171 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2172
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002173 if (sizeof(struct ffa_mem_relinquish) +
2174 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002175 length) {
2176 dlog_verbose(
2177 "Endpoint count changed while copying to internal "
2178 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002180 goto out;
2181 }
2182
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002183 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2184 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002185
2186out:
2187 vm_unlock(&from_locked);
2188 return ret;
2189}
2190
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002191struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2192 ffa_memory_region_flags_t flags,
2193 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194{
2195 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002196 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002197
J-Alves917d2f22020-10-30 18:39:30 +00002198 if (ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002199 struct vm_locked to_locked = vm_lock(to);
2200
Andrew Walbranca808b12020-05-15 17:22:28 +01002201 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002202 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002203
Andrew Walbran290b0c92020-02-03 16:37:14 +00002204 vm_unlock(&to_locked);
2205 } else {
2206 struct vm *from = vm_find(HF_TEE_VM_ID);
2207 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2208
Andrew Walbranca808b12020-05-15 17:22:28 +01002209 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2210 vm_to_from_lock.vm2, handle, flags,
2211 &api_page_pool);
2212
2213 vm_unlock(&vm_to_from_lock.vm1);
2214 vm_unlock(&vm_to_from_lock.vm2);
2215 }
2216
2217 return ret;
2218}
2219
2220struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2221 uint32_t fragment_offset,
2222 ffa_vm_id_t sender_vm_id,
2223 struct vcpu *current)
2224{
2225 struct vm *to = current->vm;
2226 struct vm_locked to_locked;
2227 struct ffa_value ret;
2228
2229 /* Sender ID MBZ at virtual instance. */
2230 if (sender_vm_id != 0) {
2231 return ffa_error(FFA_INVALID_PARAMETERS);
2232 }
2233
2234 to_locked = vm_lock(to);
2235
2236 if (msg_receiver_busy(to_locked, NULL, false)) {
2237 /*
2238 * Can't retrieve memory information if the mailbox is not
2239 * available.
2240 */
2241 dlog_verbose("RX buffer not ready.\n");
2242 ret = ffa_error(FFA_BUSY);
2243 goto out;
2244 }
2245
2246 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2247 &api_page_pool);
2248
2249out:
2250 vm_unlock(&to_locked);
2251 return ret;
2252}
2253
2254struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2255 uint32_t fragment_length,
2256 ffa_vm_id_t sender_vm_id,
2257 struct vcpu *current)
2258{
2259 struct vm *from = current->vm;
2260 const void *from_msg;
2261 void *fragment_copy;
2262 struct ffa_value ret;
2263
2264 /* Sender ID MBZ at virtual instance. */
2265 if (sender_vm_id != 0) {
2266 return ffa_error(FFA_INVALID_PARAMETERS);
2267 }
2268
2269 /*
2270 * Check that the sender has configured its send buffer. If the TX
2271 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2272 * be safely accessed after releasing the lock since the TX mailbox
2273 * address can only be configured once.
2274 */
2275 sl_lock(&from->lock);
2276 from_msg = from->mailbox.send;
2277 sl_unlock(&from->lock);
2278
2279 if (from_msg == NULL) {
2280 return ffa_error(FFA_INVALID_PARAMETERS);
2281 }
2282
2283 /*
2284 * Copy the fragment to a fresh page from the memory pool. This prevents
2285 * the sender from changing it underneath us, and also lets us keep it
2286 * around in the share state table if needed.
2287 */
2288 if (fragment_length > HF_MAILBOX_SIZE ||
2289 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2290 dlog_verbose(
2291 "Fragment length %d larger than mailbox size %d.\n",
2292 fragment_length, HF_MAILBOX_SIZE);
2293 return ffa_error(FFA_INVALID_PARAMETERS);
2294 }
2295 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2296 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2297 0) {
2298 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2299 return ffa_error(FFA_INVALID_PARAMETERS);
2300 }
2301 fragment_copy = mpool_alloc(&api_page_pool);
2302 if (fragment_copy == NULL) {
2303 dlog_verbose("Failed to allocate fragment copy.\n");
2304 return ffa_error(FFA_NO_MEMORY);
2305 }
2306 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2307
2308 /*
2309 * Hafnium doesn't support fragmentation of memory retrieve requests
2310 * (because it doesn't support caller-specified mappings, so a request
2311 * will never be larger than a single page), so this must be part of a
2312 * memory send (i.e. donate, lend or share) request.
2313 *
2314 * We can tell from the handle whether the memory transaction is for the
2315 * TEE or not.
2316 */
2317 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2318 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2319 struct vm_locked from_locked = vm_lock(from);
2320
2321 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2322 fragment_length, handle,
2323 &api_page_pool);
2324 /*
2325 * `ffa_memory_send_continue` takes ownership of the
2326 * fragment_copy, so we don't need to free it here.
2327 */
2328 vm_unlock(&from_locked);
2329 } else {
2330 struct vm *to = vm_find(HF_TEE_VM_ID);
2331 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2332
2333 /*
2334 * The TEE RX buffer state is checked in
2335 * `ffa_memory_tee_send_continue` rather than here, as we need
2336 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2337 * than FFA_ERROR FFA_BUSY in case it is busy.
2338 */
2339
2340 ret = ffa_memory_tee_send_continue(
2341 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2342 fragment_length, handle, &api_page_pool);
2343 /*
2344 * `ffa_memory_tee_send_continue` takes ownership of the
2345 * fragment_copy, so we don't need to free it here.
2346 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002347
2348 vm_unlock(&vm_to_from_lock.vm1);
2349 vm_unlock(&vm_to_from_lock.vm2);
2350 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002351
2352 return ret;
2353}
Max Shvetsov40108e72020-08-27 12:39:50 +01002354
2355struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2356 struct vcpu *current)
2357{
2358 struct vm_locked vm_locked;
2359
2360 vm_locked = vm_lock(current->vm);
2361 vm_locked.vm->secondary_ep = entry_point;
2362 vm_unlock(&vm_locked);
2363
2364 return (struct ffa_value){.func = FFA_SUCCESS_32};
2365}