blob: 70df9f72600b669e48c16a323dfe7b5d3e153b46 [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"
Manish Pandeya5f39fb2020-09-11 09:47:11 +010024#include "hf/plat/interrupts.h"
Andrew Scull6386f252018-12-06 13:29:10 +000025#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010026#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010027#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010028#include "hf/vm.h"
29
Andrew Scullf35a5c92018-08-07 18:09:46 +010030#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010031#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010032
Fuad Tabbae4efcc32020-07-16 15:37:27 +010033static_assert(sizeof(struct ffa_partition_info) == 8,
34 "Partition information descriptor size doesn't match the one in "
35 "the FF-A 1.0 EAC specification, Table 82.");
36
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000037/*
38 * To eliminate the risk of deadlocks, we define a partial order for the
39 * acquisition of locks held concurrently by the same physical CPU. Our current
40 * ordering requirements are as follows:
41 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010042 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000043 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010044 * Locks of the same kind require the lock of lowest address to be locked first,
45 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000046 */
47
Andrew Scullaa039b32018-10-04 15:02:26 +010048static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010049 "Currently, a page is mapped for the send and receive buffers so "
50 "the maximum request is the size of a page.");
51
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000052static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
53 "The page pool entry size must be at least as big as the mailbox "
54 "size, so that memory region descriptors can be copied from the "
55 "mailbox for memory sharing.");
56
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000057static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000058
59/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000060 * Initialises the API page pool by taking ownership of the contents of the
61 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062 */
63void api_init(struct mpool *ppool)
64{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000065 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000066}
67
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010068/**
J-Alvesad6a0432021-04-09 16:06:21 +010069 * Get target VM vCPU:
70 * If VM is UP then return first vCPU.
71 * If VM is MP then return vCPU whose index matches current CPU index.
72 */
73static struct vcpu *api_ffa_get_vm_vcpu(struct vm *vm, struct vcpu *current)
74{
75 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
76 struct vcpu *vcpu = NULL;
77
78 if (vm->vcpu_count == 1) {
79 vcpu = vm_get_vcpu(vm, 0);
80 } else if (current_cpu_index < vm->vcpu_count) {
81 vcpu = vm_get_vcpu(vm, current_cpu_index);
82 }
83
84 return vcpu;
85}
86
87/**
J-Alvesfe7f7372020-11-09 11:32:12 +000088 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
89 * is given as argument of the function.
90 *
91 * Called to change the context between SPs for direct messaging (when Hafnium
92 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
93 *
94 * This function works for partitions that are:
J-Alvesad6a0432021-04-09 16:06:21 +010095 * - UP migratable.
J-Alvesfe7f7372020-11-09 11:32:12 +000096 * - MP with pinned Execution Contexts.
97 */
98static struct vcpu *api_switch_to_vm(struct vcpu *current,
99 struct ffa_value to_ret,
100 enum vcpu_state vcpu_state,
101 ffa_vm_id_t to_id)
102{
103 struct vm *to_vm = vm_find(to_id);
J-Alvesad6a0432021-04-09 16:06:21 +0100104 struct vcpu *next = api_ffa_get_vm_vcpu(to_vm, current);
J-Alvesfe7f7372020-11-09 11:32:12 +0000105
106 CHECK(next != NULL);
107
108 /* Set the return value for the target VM. */
109 arch_regs_set_retval(&next->regs, to_ret);
110
111 /* Set the current vCPU state. */
112 sl_lock(&current->lock);
113 current->state = vcpu_state;
114 sl_unlock(&current->lock);
115
116 return next;
117}
118
119/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000120 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100121 *
122 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100123 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100124 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100125static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100126 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000127 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100128{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000129 /*
130 * If the secondary is blocked but has a timer running, sleep until the
131 * timer fires rather than indefinitely.
132 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100133 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100134 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
135 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100136 if (arch_timer_enabled_current()) {
137 uint64_t remaining_ns =
138 arch_timer_remaining_ns_current();
139
140 if (remaining_ns == 0) {
141 /*
142 * Timer is pending, so the current vCPU should
143 * be run again right away.
144 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100145 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100146 /*
147 * primary_ret.arg1 should already be set to the
148 * current VM ID and vCPU ID.
149 */
150 primary_ret.arg2 = 0;
151 } else {
152 primary_ret.arg2 = remaining_ns;
153 }
154 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100155 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100156 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000157 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100158 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000159
160 default:
161 /* Do nothing. */
162 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000163 }
164
J-Alvesfe7f7372020-11-09 11:32:12 +0000165 return api_switch_to_vm(current, primary_ret, secondary_state,
166 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100167}
168
169/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200170 * Choose next vCPU to run to be the counterpart vCPU in the other
171 * world (run the normal world if currently running in the secure
172 * world). Set current vCPU state to the given vcpu_state parameter.
173 * Set FF-A return values to the target vCPU in the other world.
174 *
175 * Called in context of a direct message response from a secure
176 * partition to a VM.
177 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100178struct vcpu *api_switch_to_other_world(struct vcpu *current,
179 struct ffa_value other_world_ret,
180 enum vcpu_state vcpu_state)
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200181{
J-Alvesfe7f7372020-11-09 11:32:12 +0000182 return api_switch_to_vm(current, other_world_ret, vcpu_state,
183 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200184}
185
186/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100187 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
188 * registers the `from` VM to be notified when it becomes available.
189 */
190static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
191{
192 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
193 to.vm->mailbox.recv == NULL) {
194 /*
195 * Fail if the receiver isn't currently ready to receive data,
196 * setting up for notification if requested.
197 */
198 if (notify) {
199 struct wait_entry *entry =
200 vm_get_wait_entry(from, to.vm->id);
201
202 /* Append waiter only if it's not there yet. */
203 if (list_empty(&entry->wait_links)) {
204 list_append(&to.vm->mailbox.waiter_list,
205 &entry->wait_links);
206 }
207 }
208
209 return true;
210 }
211
212 return false;
213}
214
215/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000216 * Returns true if the given vCPU is executing in context of an
217 * FFA_MSG_SEND_DIRECT_REQ invocation.
218 */
219static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
220{
221 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
222}
223
224/**
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100225 * Returns true if the VM owning the given vCPU is supporting managed exit and
226 * the vCPU is currently processing a managed exit.
227 */
228static bool api_ffa_is_managed_exit_ongoing(struct vcpu_locked vcpu_locked)
229{
230 return (vcpu_locked.vcpu->vm->supports_managed_exit &&
231 vcpu_locked.vcpu->processing_managed_exit);
232}
233
234/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000235 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000236 */
237struct vcpu *api_preempt(struct vcpu *current)
238{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100239 struct ffa_value ret = {
240 .func = FFA_INTERRUPT_32,
241 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000242 };
243
Andrew Sculld6ee1102019-04-05 22:12:42 +0100244 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000245}
246
247/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000248 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000249 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100250 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100251struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100252{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100253 struct ffa_value ret = {
254 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
255 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100256 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000257
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000258 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100259 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100260}
261
262/**
Andrew Walbran33645652019-04-15 12:29:31 +0100263 * Puts the current vCPU in off mode, and returns to the primary VM.
264 */
265struct vcpu *api_vcpu_off(struct vcpu *current)
266{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100267 struct ffa_value ret = {
268 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
269 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100270 };
271
272 /*
273 * Disable the timer, so the scheduler doesn't get told to call back
274 * based on it.
275 */
276 arch_timer_disable_current();
277
278 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
279}
280
281/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000282 * Returns to the primary VM to allow this CPU to be used for other tasks as the
283 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100284 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000285 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000286struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000287{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000288 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
289 struct vcpu_locked current_locked;
290 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000291
292 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000293 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000294 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000295 }
296
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000297 current_locked = vcpu_lock(current);
298 is_direct_request_ongoing =
299 is_ffa_direct_msg_request_ongoing(current_locked);
300 vcpu_unlock(&current_locked);
301
302 if (is_direct_request_ongoing) {
303 return ffa_error(FFA_DENIED);
304 }
305
306 *next = api_switch_to_primary(
307 current,
308 (struct ffa_value){.func = FFA_YIELD_32,
309 .arg1 = ffa_vm_vcpu(current->vm->id,
310 vcpu_index(current))},
311 VCPU_STATE_READY);
312
313 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000314}
315
316/**
Andrew Walbran33645652019-04-15 12:29:31 +0100317 * Switches to the primary so that it can switch to the target, or kick it if it
318 * is already running on a different physical CPU.
319 */
320struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
321{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100322 struct ffa_value ret = {
323 .func = HF_FFA_RUN_WAKE_UP,
324 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
325 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100326 };
327 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
328}
329
330/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000331 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000332 */
333struct vcpu *api_abort(struct vcpu *current)
334{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000336
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100337 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000338 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000339
340 if (current->vm->id == HF_PRIMARY_VM_ID) {
341 /* TODO: what to do when the primary aborts? */
342 for (;;) {
343 /* Do nothing. */
344 }
345 }
346
347 atomic_store_explicit(&current->vm->aborting, true,
348 memory_order_relaxed);
349
350 /* TODO: free resources once all vCPUs abort. */
351
Andrew Sculld6ee1102019-04-05 22:12:42 +0100352 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000353}
354
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100355struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
356 const struct ffa_uuid *uuid)
357{
358 struct vm *current_vm = current->vm;
359 struct vm_locked current_vm_locked;
360 ffa_vm_count_t vm_count = 0;
361 bool uuid_is_null = ffa_uuid_is_null(uuid);
362 struct ffa_value ret;
363 uint32_t size;
364 struct ffa_partition_info partitions[MAX_VMS];
365
366 /*
367 * Iterate through the VMs to find the ones with a matching UUID.
368 * A Null UUID retrieves information for all VMs.
369 */
370 for (uint16_t index = 0; index < vm_get_count(); ++index) {
371 const struct vm *vm = vm_find_index(index);
372
373 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
374 partitions[vm_count].vm_id = vm->id;
375 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100376 partitions[vm_count].properties =
Olivier Deprez764fd2e2020-07-29 15:14:09 +0200377 arch_vm_partition_properties(vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100378
379 ++vm_count;
380 }
381 }
382
383 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
384 if (vm_count == 0) {
385 return ffa_error(FFA_INVALID_PARAMETERS);
386 }
387
388 size = vm_count * sizeof(partitions[0]);
389 if (size > FFA_MSG_PAYLOAD_MAX) {
390 dlog_error(
391 "Partition information does not fit in the VM's RX "
392 "buffer.\n");
393 return ffa_error(FFA_NO_MEMORY);
394 }
395
396 /*
397 * Partition information is returned in the VM's RX buffer, which is why
398 * the lock is needed.
399 */
400 current_vm_locked = vm_lock(current_vm);
401
402 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
403 /*
404 * Can't retrieve memory information if the mailbox is not
405 * available.
406 */
407 dlog_verbose("RX buffer not ready.\n");
408 ret = ffa_error(FFA_BUSY);
409 goto out_unlock;
410 }
411
412 /* Populate the VM's RX buffer with the partition information. */
413 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
414 size);
415 current_vm->mailbox.recv_size = size;
416 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
417 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
418 current_vm->mailbox.state = MAILBOX_STATE_READ;
419
420 /* Return the count of partition information descriptors in w2. */
421 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
422
423out_unlock:
424 vm_unlock(&current_vm_locked);
425
426 return ret;
427}
428
Andrew Scull9726c252019-01-23 13:44:19 +0000429/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000430 * Returns the ID of the VM.
431 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000433{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100434 return (struct ffa_value){.func = FFA_SUCCESS_32,
435 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000436}
437
438/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000439 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000440 * function to indicate that register state for the given vCPU has been saved
441 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000442 */
443void api_regs_state_saved(struct vcpu *vcpu)
444{
445 sl_lock(&vcpu->lock);
446 vcpu->regs_available = true;
447 sl_unlock(&vcpu->lock);
448}
449
450/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000451 * Retrieves the next waiter and removes it from the wait list if the VM's
452 * mailbox is in a writable state.
453 */
454static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
455{
456 struct wait_entry *entry;
457 struct vm *vm = locked_vm.vm;
458
Andrew Sculld6ee1102019-04-05 22:12:42 +0100459 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000460 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
461 /* The mailbox is not writable or there are no waiters. */
462 return NULL;
463 }
464
465 /* Remove waiter from the wait list. */
466 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
467 wait_links);
468 list_remove(&entry->wait_links);
469 return entry;
470}
471
472/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000473 * Assuming that the arguments have already been checked by the caller, injects
474 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
475 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
476 * is next run, which is up to the scheduler.
477 *
478 * Returns:
479 * - 0 on success if no further action is needed.
480 * - 1 if it was called by the primary VM and the primary VM now needs to wake
481 * up or kick the target vCPU.
482 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100483int64_t api_interrupt_inject_locked(struct vcpu_locked target_locked,
484 uint32_t intid, struct vcpu *current,
485 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000486{
Manish Pandey35e452f2021-02-18 21:36:34 +0000487 struct vcpu *target_vcpu = target_locked.vcpu;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000488 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +0000489 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
490 uint32_t intid_mask = 1U << intid_shift;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000491 int64_t ret = 0;
492
Andrew Walbran508e63c2018-12-20 17:02:37 +0000493 /*
Manish Pandey35e452f2021-02-18 21:36:34 +0000494 * We only need to change state and (maybe) trigger a virtual interrupt
495 * if it is enabled and was not previously pending. Otherwise we can
496 * skip everything except setting the pending bit.
Andrew Walbran508e63c2018-12-20 17:02:37 +0000497 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000498 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
499 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000500 intid_mask)) {
501 goto out;
502 }
503
504 /* Increment the count. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000505 if ((target_vcpu->interrupts.interrupt_type[intid_index] &
506 intid_mask) == (INTERRUPT_TYPE_IRQ << intid_shift)) {
507 vcpu_irq_count_increment(target_locked);
508 } else {
509 vcpu_fiq_count_increment(target_locked);
510 }
Andrew Walbran508e63c2018-12-20 17:02:37 +0000511
512 /*
513 * Only need to update state if there was not already an
514 * interrupt enabled and pending.
515 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000516 if (vcpu_interrupt_count_get(target_locked) != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000517 goto out;
518 }
519
Andrew Walbran508e63c2018-12-20 17:02:37 +0000520 if (current->vm->id == HF_PRIMARY_VM_ID) {
521 /*
522 * If the call came from the primary VM, let it know that it
523 * should run or kick the target vCPU.
524 */
525 ret = 1;
Manish Pandey35e452f2021-02-18 21:36:34 +0000526 } else if (current != target_vcpu && next != NULL) {
527 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000528 }
529
530out:
531 /* Either way, make it pending. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000532 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000533
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200534 return ret;
535}
536
537/* Wrapper to internal_interrupt_inject with locking of target vCPU */
538static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
539 uint32_t intid, struct vcpu *current,
540 struct vcpu **next)
541{
542 int64_t ret;
543 struct vcpu_locked target_locked;
544
545 target_locked = vcpu_lock(target_vcpu);
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100546 ret = api_interrupt_inject_locked(target_locked, intid, current, next);
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200547 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000548
549 return ret;
550}
551
552/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100553 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
554 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100555 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100556static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100557{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000558 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100559 case FFA_MSG_SEND_32:
560 return (struct ffa_value){
561 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000562 .arg1 = (receiver->mailbox.recv_sender << 16) |
563 receiver->id,
564 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000565 default:
566 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000567 dlog_error("Tried to return an invalid message function %#x\n",
568 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000570 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100571}
572
573/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000574 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000575 * value needs to be forced onto the vCPU.
576 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000577static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000579{
Max Shvetsov40108e72020-08-27 12:39:50 +0100580 struct vcpu_locked vcpu_locked;
581 struct vm_locked vm_locked;
Andrew Scullb06d1752019-02-04 10:15:48 +0000582 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000583 bool ret;
584
Andrew Scullb06d1752019-02-04 10:15:48 +0000585 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000586 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000587 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100588 * The VM lock is not needed in the common case so it must only be taken
589 * when it is going to be needed. This ensures there are no inter-vCPU
590 * dependencies in the common run case meaning the sensitive context
591 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000592 */
Max Shvetsov40108e72020-08-27 12:39:50 +0100593 vcpu_locked = vcpu_lock(vcpu);
594
595#if SECURE_WORLD == 1
596
597 if (vcpu_secondary_reset_and_start(vcpu_locked, vcpu->vm->secondary_ep,
598 0)) {
599 dlog_verbose("%s secondary cold boot vmid %#x vcpu id %#x\n",
600 __func__, vcpu->vm->id, current->cpu->id);
601 }
602
603#endif
Andrew Scullb06d1752019-02-04 10:15:48 +0000604
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000605 /* The VM needs to be locked to deliver mailbox messages. */
606 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
607 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100608 vcpu_unlock(&vcpu_locked);
609 vm_locked = vm_lock(vcpu->vm);
610 vcpu_locked = vcpu_lock(vcpu);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000611 }
612
613 /*
614 * If the vCPU is already running somewhere then we can't run it here
615 * simultaneously. While it is actually running then the state should be
616 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
617 * stops running but while Hafnium is in the process of switching back
618 * to the primary there will be a brief period while the state has been
619 * updated but `regs_available` is still false (until
620 * `api_regs_state_saved` is called). We can't start running it again
621 * until this has finished, so count this state as still running for the
622 * purposes of this check.
623 */
624 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
625 /*
626 * vCPU is running on another pCPU.
627 *
628 * It's okay not to return the sleep duration here because the
629 * other physical CPU that is currently running this vCPU will
630 * return the sleep duration if needed.
631 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000633 ret = false;
634 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000635 }
Andrew Scull9726c252019-01-23 13:44:19 +0000636
637 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100638 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100639 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000640 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100641 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000642 }
643 ret = false;
644 goto out;
645 }
646
Andrew Walbran508e63c2018-12-20 17:02:37 +0000647 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100648 case VCPU_STATE_RUNNING:
649 case VCPU_STATE_OFF:
650 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000651 ret = false;
652 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000653
Andrew Sculld6ee1102019-04-05 22:12:42 +0100654 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000655 /*
656 * A pending message allows the vCPU to run so the message can
657 * be delivered directly.
658 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100659 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100660 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100662 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000663 break;
664 }
665 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100666 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000667 /* Allow virtual interrupts to be delivered. */
Manish Pandey35e452f2021-02-18 21:36:34 +0000668 if (vcpu_interrupt_count_get(vcpu_locked) > 0) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000669 break;
670 }
671
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100672 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
673
Andrew Walbran508e63c2018-12-20 17:02:37 +0000674 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100675 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000676 arch_timer_remaining_ns(&vcpu->regs);
677
678 /*
679 * The timer expired so allow the interrupt to be
680 * delivered.
681 */
682 if (timer_remaining_ns == 0) {
683 break;
684 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100685 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000686
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100687 /*
688 * The vCPU is not ready to run, return the appropriate code to
689 * the primary which called vcpu_run.
690 */
691 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100692 ? FFA_MSG_WAIT_32
693 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100694 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
695 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000696
697 ret = false;
698 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000699
Andrew Sculld6ee1102019-04-05 22:12:42 +0100700 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000701 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000702 }
703
Andrew Scullb06d1752019-02-04 10:15:48 +0000704 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000705 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100706 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000707
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000708 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000709 * Mark the registers as unavailable now that we're about to reflect
710 * them onto the real registers. This will also prevent another physical
711 * CPU from trying to read these registers.
712 */
713 vcpu->regs_available = false;
714
715 ret = true;
716
717out:
Max Shvetsov40108e72020-08-27 12:39:50 +0100718 vcpu_unlock(&vcpu_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000719 if (need_vm_lock) {
Max Shvetsov40108e72020-08-27 12:39:50 +0100720 vm_unlock(&vm_locked);
Andrew Scullb06d1752019-02-04 10:15:48 +0000721 }
722
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000723 return ret;
724}
725
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100726struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
727 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100728{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100729 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100730 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100732
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000733 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100734 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100735 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100736 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100737 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100738
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000739 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100740 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100741 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100742 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100743
Andrew Scull19503262018-09-20 14:48:39 +0100744 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100745 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100746 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100747 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100748 }
749
Fuad Tabbaed294af2019-12-20 10:43:01 +0000750 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100751 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100752 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100753 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100754
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000755 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100756 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000757 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000758 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100759 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000760
Andrew Walbran508e63c2018-12-20 17:02:37 +0000761 /*
762 * Inject timer interrupt if timer has expired. It's safe to access
763 * vcpu->regs here because api_vcpu_prepare_run already made sure that
764 * regs_available was true (and then set it to false) before returning
765 * true.
766 */
767 if (arch_timer_pending(&vcpu->regs)) {
768 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100769 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
770 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000771
772 /*
773 * Set the mask bit so the hardware interrupt doesn't fire
774 * again. Ideally we wouldn't do this because it affects what
775 * the secondary vCPU sees, but if we don't then we end up with
776 * a loop of the interrupt firing each time we try to return to
777 * the secondary vCPU.
778 */
779 arch_timer_mask(&vcpu->regs);
780 }
781
Fuad Tabbaed294af2019-12-20 10:43:01 +0000782 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000783 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000784
Andrew Scull33fecd32019-01-08 14:48:27 +0000785 /*
786 * Set a placeholder return code to the scheduler. This will be
787 * overwritten when the switch back to the primary occurs.
788 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100789 ret.func = FFA_INTERRUPT_32;
790 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100791 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000792
Andrew Scull6d2db332018-10-10 15:28:17 +0100793out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100794 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100795}
796
797/**
Andrew Scull81e85092018-12-12 12:56:20 +0000798 * Check that the mode indicates memory that is valid, owned and exclusive.
799 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100800static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000801{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100802 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
803 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000804}
805
806/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100807 * Determines the value to be returned by api_ffa_rxtx_map and
808 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
809 * there are waiters, it also switches back to the primary VM for it to wake
810 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000811 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100812static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
813 struct vcpu *current,
814 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000815{
816 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000817
818 if (list_empty(&vm->mailbox.waiter_list)) {
819 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100820 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000821 }
822
823 if (vm->id == HF_PRIMARY_VM_ID) {
824 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100825 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000826 }
827
828 /*
829 * Switch back to the primary VM, informing it that there are waiters
830 * that need to be notified.
831 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000832 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100833 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000834 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000835
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100836 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000837}
838
839/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100840 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100841 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100842static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
843 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100844 paddr_t pa_send_begin, paddr_t pa_send_end,
845 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200846 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100847 struct mpool *local_page_pool)
848{
849 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100850
851 /* Map the send page as read-only in the hypervisor address space. */
852 vm_locked.vm->mailbox.send =
853 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200854 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100855 if (!vm_locked.vm->mailbox.send) {
856 /* TODO: partial defrag of failed range. */
857 /* Recover any memory consumed in failed mapping. */
858 mm_defrag(mm_stage1_locked, local_page_pool);
859 goto fail;
860 }
861
862 /*
863 * Map the receive page as writable in the hypervisor address space. On
864 * failure, unmap the send page before returning.
865 */
866 vm_locked.vm->mailbox.recv =
867 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200868 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100869 if (!vm_locked.vm->mailbox.recv) {
870 /* TODO: partial defrag of failed range. */
871 /* Recover any memory consumed in failed mapping. */
872 mm_defrag(mm_stage1_locked, local_page_pool);
873 goto fail_undo_send;
874 }
875
876 ret = true;
877 goto out;
878
879 /*
880 * The following mappings will not require more memory than is available
881 * in the local pool.
882 */
883fail_undo_send:
884 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100885 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
886 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100887
888fail:
889 ret = false;
890
891out:
Andrew Sculle1322792019-07-01 17:46:10 +0100892 return ret;
893}
894
895/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100896 * Sanity checks and configures the send and receive pages in the VM stage-2
897 * and hypervisor stage-1 page tables.
898 *
899 * Returns:
900 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
901 * aligned or are the same.
902 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
903 * due to insuffient page table memory.
904 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
905 * the caller.
906 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100907 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100908
909struct ffa_value api_vm_configure_pages(
910 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
911 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
912 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100913{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100914 struct ffa_value ret;
915 paddr_t pa_send_begin;
916 paddr_t pa_send_end;
917 paddr_t pa_recv_begin;
918 paddr_t pa_recv_end;
919 uint32_t orig_send_mode;
920 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200921 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100922
923 /* We only allow these to be setup once. */
924 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
925 ret = ffa_error(FFA_DENIED);
926 goto out;
927 }
928
929 /* Hafnium only supports a fixed size of RX/TX buffers. */
930 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
931 ret = ffa_error(FFA_INVALID_PARAMETERS);
932 goto out;
933 }
934
935 /* Fail if addresses are not page-aligned. */
936 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
937 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
938 ret = ffa_error(FFA_INVALID_PARAMETERS);
939 goto out;
940 }
941
942 /* Convert to physical addresses. */
943 pa_send_begin = pa_from_ipa(send);
944 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
945 pa_recv_begin = pa_from_ipa(recv);
946 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
947
948 /* Fail if the same page is used for the send and receive pages. */
949 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
950 ret = ffa_error(FFA_INVALID_PARAMETERS);
951 goto out;
952 }
Andrew Sculle1322792019-07-01 17:46:10 +0100953
954 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100955 * Ensure the pages are valid, owned and exclusive to the VM and that
956 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100957 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100958 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
959 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
960 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
961 (orig_send_mode & MM_MODE_R) == 0 ||
962 (orig_send_mode & MM_MODE_W) == 0) {
963 ret = ffa_error(FFA_DENIED);
964 goto out;
965 }
966
967 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
968 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
969 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
970 (orig_recv_mode & MM_MODE_R) == 0) {
971 ret = ffa_error(FFA_DENIED);
972 goto out;
973 }
Andrew Sculle1322792019-07-01 17:46:10 +0100974
975 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000976 if (!vm_identity_map(
977 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100978 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100979 local_page_pool, NULL)) {
980 ret = ffa_error(FFA_NO_MEMORY);
981 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100982 }
983
Andrew Scull3c257452019-11-26 13:32:50 +0000984 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
985 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100986 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100987 /* TODO: partial defrag of failed range. */
988 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100989 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100990 goto fail_undo_send;
991 }
992
Olivier Deprez96a2a262020-06-11 17:21:38 +0200993 /* Get extra send/recv pages mapping attributes for the given VM ID. */
994 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
995
Manish Pandeyd34f8892020-06-19 17:41:07 +0100996 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
997 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200998 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100999 goto fail_undo_send_and_recv;
1000 }
1001
Manish Pandeyd34f8892020-06-19 17:41:07 +01001002 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +01001003 goto out;
1004
Andrew Sculle1322792019-07-01 17:46:10 +01001005fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +00001006 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001007 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +01001008
1009fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +00001010 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +01001011 orig_send_mode, local_page_pool, NULL));
1012 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +01001013
1014out:
Andrew Sculle1322792019-07-01 17:46:10 +01001015 return ret;
1016}
1017
1018/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001019 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +01001020 * must not be shared. Locking of the page tables combined with a local memory
1021 * pool ensures there will always be enough memory to recover from any errors
1022 * that arise. The stage-1 page tables must be locked so memory cannot be taken
1023 * by another core which could result in this transaction being unable to roll
1024 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001025 *
1026 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001028 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001030 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001032 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 * - FFA_SUCCESS on success if no further action is needed.
1034 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001035 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001036 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001037struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
1038 uint32_t page_count, struct vcpu *current,
1039 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001040{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001041 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +01001043 struct vm_locked vm_locked;
1044 struct mm_stage1_locked mm_stage1_locked;
1045 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001046
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001047 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001048 * Create a local pool so any freed memory can't be used by another
1049 * thread. This is to ensure the original mapping can be restored if any
1050 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001051 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001052 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1053
Andrew Sculle1322792019-07-01 17:46:10 +01001054 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001055 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001056
Manish Pandeyd34f8892020-06-19 17:41:07 +01001057 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1058 page_count, &local_page_pool);
1059 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001060 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061 }
1062
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001063 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001064 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001065
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001066exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001067 mpool_fini(&local_page_pool);
1068
1069 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001070 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001071
1072 return ret;
1073}
1074
1075/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001076 * Notifies the `to` VM about the message currently in its mailbox, possibly
1077 * with the help of the primary VM.
1078 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001079static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1080 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001081{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1083 struct ffa_value primary_ret = {
1084 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001085 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001086 };
1087
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001088 /* Messages for the primary VM are delivered directly. */
1089 if (to.vm->id == HF_PRIMARY_VM_ID) {
1090 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001091 * Only tell the primary VM the size and other details if the
1092 * message is for it, to avoid leaking data about messages for
1093 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001094 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001096
1097 to.vm->mailbox.state = MAILBOX_STATE_READ;
1098 *next = api_switch_to_primary(current, primary_ret,
1099 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001100 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001101 }
1102
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001103 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1104
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001105 /* Messages for the TEE are sent on via the dispatcher. */
1106 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001107 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001108
Olivier Deprez112d2b52020-09-30 07:39:23 +02001109 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001110 /*
1111 * After the call to the TEE completes it must have finished
1112 * reading its RX buffer, so it is ready for another message.
1113 */
1114 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001115 /*
1116 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001117 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001118 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001119 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001120 }
1121
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001122 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001123 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001124 *next = api_switch_to_primary(current, primary_ret,
1125 VCPU_STATE_READY);
1126 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001127
1128 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001129}
1130
1131/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001132 * Copies data from the sender's send buffer to the recipient's receive buffer
1133 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001134 *
1135 * If the recipient's receive buffer is busy, it can optionally register the
1136 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001137 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1139 ffa_vm_id_t receiver_vm_id, uint32_t size,
1140 uint32_t attributes, struct vcpu *current,
1141 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001142{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001143 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001144 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001145 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001146 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001148 struct vcpu_locked current_locked;
1149 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 bool notify =
1151 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001152
Andrew Walbran70bc8622019-10-07 14:15:58 +01001153 /* Ensure sender VM ID corresponds to the current VM. */
1154 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001156 }
1157
1158 /* Disallow reflexive requests as this suggests an error in the VM. */
1159 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001161 }
1162
1163 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 if (size > FFA_MSG_PAYLOAD_MAX) {
1165 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001166 }
1167
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001168 /*
1169 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1170 * invocation.
1171 */
1172 current_locked = vcpu_lock(current);
1173 is_direct_request_ongoing =
1174 is_ffa_direct_msg_request_ongoing(current_locked);
1175 vcpu_unlock(&current_locked);
1176
1177 if (is_direct_request_ongoing) {
1178 return ffa_error(FFA_DENIED);
1179 }
1180
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001181 /* Ensure the receiver VM exists. */
1182 to = vm_find(receiver_vm_id);
1183 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001185 }
1186
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001187 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001188 * Check that the sender has configured its send buffer. If the tx
1189 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1190 * be safely accessed after releasing the lock since the tx mailbox
1191 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001192 */
1193 sl_lock(&from->lock);
1194 from_msg = from->mailbox.send;
1195 sl_unlock(&from->lock);
1196
1197 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001198 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001199 }
1200
Andrew Walbran82d6d152019-12-24 15:02:06 +00001201 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001202
Andrew Walbran82d6d152019-12-24 15:02:06 +00001203 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001204 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001205 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001206 }
1207
Andrew Walbran82d6d152019-12-24 15:02:06 +00001208 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001210 to->mailbox.recv_size = size;
1211 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001213 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001214
1215out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001216 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001217
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001218 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001219}
1220
1221/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001222 * Checks whether the vCPU's attempt to block for a message has already been
1223 * interrupted or whether it is allowed to block.
1224 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001225bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001226{
Manish Pandey35e452f2021-02-18 21:36:34 +00001227 struct vcpu_locked current_locked;
Andrew Scullec52ddf2019-08-20 10:41:01 +01001228 bool interrupted;
1229
Manish Pandey35e452f2021-02-18 21:36:34 +00001230 current_locked = vcpu_lock(current);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001231
1232 /*
1233 * Don't block if there are enabled and pending interrupts, to match
1234 * behaviour of wait_for_interrupt.
1235 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001236 interrupted = (vcpu_interrupt_count_get(current_locked) > 0);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001237
Manish Pandey35e452f2021-02-18 21:36:34 +00001238 vcpu_unlock(&current_locked);
Andrew Scullec52ddf2019-08-20 10:41:01 +01001239
1240 return interrupted;
1241}
1242
1243/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001244 * Receives a message from the mailbox. If one isn't available, this function
1245 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001246 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001247 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001248 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001249struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1250 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001251{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001252 bool is_direct_request_ongoing;
1253 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001254 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001256 bool is_from_secure_world =
1257 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001258
Andrew Scullaa039b32018-10-04 15:02:26 +01001259 /*
1260 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001261 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001262 */
J-Alvesb37fd082020-10-22 12:29:21 +01001263 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001264 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001265 }
1266
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001267 /*
1268 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1269 * invocation.
1270 */
1271 current_locked = vcpu_lock(current);
1272 is_direct_request_ongoing =
1273 is_ffa_direct_msg_request_ongoing(current_locked);
1274 vcpu_unlock(&current_locked);
1275
1276 if (is_direct_request_ongoing) {
1277 return ffa_error(FFA_DENIED);
1278 }
1279
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001280 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001281
Andrew Scullaa039b32018-10-04 15:02:26 +01001282 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001283 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1284 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001286 goto out;
1287 }
1288
1289 /* No pending message so fail if not allowed to block. */
1290 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001291 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001292 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001293 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001294
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001295 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001296 * From this point onward this call can only be interrupted or a message
1297 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001298 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001299 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001300 return_code = ffa_error(FFA_INTERRUPTED);
1301 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001302 goto out;
1303 }
1304
J-Alvesb37fd082020-10-22 12:29:21 +01001305 if (is_from_secure_world) {
1306 /* Return to other world if caller is a SP. */
1307 *next = api_switch_to_other_world(
1308 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1309 VCPU_STATE_BLOCKED_MAILBOX);
1310 } else {
1311 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312 struct ffa_value run_return = {
1313 .func = FFA_MSG_WAIT_32,
1314 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001315 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001316
Andrew Walbranb4816552018-12-05 17:35:42 +00001317 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001318 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001319 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001320out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001321 sl_unlock(&vm->lock);
1322
Jose Marinho3e2442f2019-03-12 13:30:37 +00001323 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001324}
1325
1326/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001327 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1328 * by this function, the caller must have called api_mailbox_send before with
1329 * the notify argument set to true, and this call must have failed because the
1330 * mailbox was not available.
1331 *
1332 * It should be called repeatedly to retrieve a list of VMs.
1333 *
1334 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1335 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001336 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001337int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001338{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001339 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001340 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001341 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001342
1343 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001344 if (list_empty(&vm->mailbox.ready_list)) {
1345 ret = -1;
1346 goto exit;
1347 }
1348
1349 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1350 ready_links);
1351 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001352 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001353
1354exit:
1355 sl_unlock(&vm->lock);
1356 return ret;
1357}
1358
1359/**
1360 * Retrieves the next VM waiting to be notified that the mailbox of the
1361 * specified VM became writable. Only primary VMs are allowed to call this.
1362 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001363 * Returns -1 on failure or if there are no waiters; the VM id of the next
1364 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001365 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001366int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001367{
1368 struct vm *vm;
1369 struct vm_locked locked;
1370 struct wait_entry *entry;
1371 struct vm *waiting_vm;
1372
1373 /* Only primary VMs are allowed to call this function. */
1374 if (current->vm->id != HF_PRIMARY_VM_ID) {
1375 return -1;
1376 }
1377
Andrew Walbran42347a92019-05-09 13:59:03 +01001378 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001379 if (vm == NULL) {
1380 return -1;
1381 }
1382
Fuad Tabbaed294af2019-12-20 10:43:01 +00001383 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001384 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001385 entry = api_fetch_waiter(locked);
1386 vm_unlock(&locked);
1387
1388 if (entry == NULL) {
1389 return -1;
1390 }
1391
1392 /* Enqueue notification to waiting VM. */
1393 waiting_vm = entry->waiting_vm;
1394
1395 sl_lock(&waiting_vm->lock);
1396 if (list_empty(&entry->ready_links)) {
1397 list_append(&waiting_vm->mailbox.ready_list,
1398 &entry->ready_links);
1399 }
1400 sl_unlock(&waiting_vm->lock);
1401
1402 return waiting_vm->id;
1403}
1404
1405/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001406 * Releases the caller's mailbox so that a new message can be received. The
1407 * caller must have copied out all data they wish to preserve as new messages
1408 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001409 *
1410 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1412 * - FFA_SUCCESS on success if no further action is needed.
1413 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001414 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001415 * hf_mailbox_waiter_get.
1416 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001417struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001418{
1419 struct vm *vm = current->vm;
1420 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001422
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001423 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001424 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001425 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001426 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001428 break;
1429
Andrew Sculld6ee1102019-04-05 22:12:42 +01001430 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001431 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001432 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001433 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001434 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001435 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001436
1437 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001438}
Andrew Walbran318f5732018-11-20 16:23:42 +00001439
1440/**
1441 * Enables or disables a given interrupt ID for the calling vCPU.
1442 *
1443 * Returns 0 on success, or -1 if the intid is invalid.
1444 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001445int64_t api_interrupt_enable(uint32_t intid, bool enable,
1446 enum interrupt_type type, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001447{
Manish Pandey35e452f2021-02-18 21:36:34 +00001448 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001449 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Manish Pandey35e452f2021-02-18 21:36:34 +00001450 uint32_t intid_shift = intid % INTERRUPT_REGISTER_BITS;
1451 uint32_t intid_mask = 1U << intid_shift;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001452
Andrew Walbran318f5732018-11-20 16:23:42 +00001453 if (intid >= HF_NUM_INTIDS) {
1454 return -1;
1455 }
1456
Manish Pandey35e452f2021-02-18 21:36:34 +00001457 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001458 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001459 /*
1460 * If it is pending and was not enabled before, increment the
1461 * count.
1462 */
1463 if (current->interrupts.interrupt_pending[intid_index] &
1464 ~current->interrupts.interrupt_enabled[intid_index] &
1465 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001466 if ((current->interrupts.interrupt_type[intid_index] &
1467 intid_mask) ==
1468 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1469 vcpu_irq_count_increment(current_locked);
1470 } else {
1471 vcpu_fiq_count_increment(current_locked);
1472 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001473 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001474 current->interrupts.interrupt_enabled[intid_index] |=
1475 intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001476
1477 if (type == INTERRUPT_TYPE_IRQ) {
1478 current->interrupts.interrupt_type[intid_index] &=
1479 ~intid_mask;
1480 } else if (type == INTERRUPT_TYPE_FIQ) {
1481 current->interrupts.interrupt_type[intid_index] |=
1482 intid_mask;
1483 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001484 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001485 /*
1486 * If it is pending and was enabled before, decrement the count.
1487 */
1488 if (current->interrupts.interrupt_pending[intid_index] &
1489 current->interrupts.interrupt_enabled[intid_index] &
1490 intid_mask) {
Manish Pandey35e452f2021-02-18 21:36:34 +00001491 if ((current->interrupts.interrupt_type[intid_index] &
1492 intid_mask) ==
1493 (INTERRUPT_TYPE_IRQ << intid_shift)) {
1494 vcpu_irq_count_decrement(current_locked);
1495 } else {
1496 vcpu_fiq_count_decrement(current_locked);
1497 }
Andrew Walbran3d84a262018-12-13 14:41:19 +00001498 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001499 current->interrupts.interrupt_enabled[intid_index] &=
1500 ~intid_mask;
Manish Pandey35e452f2021-02-18 21:36:34 +00001501 current->interrupts.interrupt_type[intid_index] &= ~intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001502 }
1503
Manish Pandey35e452f2021-02-18 21:36:34 +00001504 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001505 return 0;
1506}
1507
1508/**
1509 * Returns the ID of the next pending interrupt for the calling vCPU, and
1510 * acknowledges it (i.e. marks it as no longer pending). Returns
1511 * HF_INVALID_INTID if there are no pending interrupts.
1512 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001513uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001514{
1515 uint8_t i;
1516 uint32_t first_interrupt = HF_INVALID_INTID;
Manish Pandey35e452f2021-02-18 21:36:34 +00001517 struct vcpu_locked current_locked;
Andrew Walbran318f5732018-11-20 16:23:42 +00001518
1519 /*
1520 * Find the first enabled and pending interrupt ID, return it, and
1521 * deactivate it.
1522 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001523 current_locked = vcpu_lock(current);
Andrew Walbran318f5732018-11-20 16:23:42 +00001524 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1525 uint32_t enabled_and_pending =
1526 current->interrupts.interrupt_enabled[i] &
1527 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001528
Andrew Walbran318f5732018-11-20 16:23:42 +00001529 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001530 uint8_t bit_index = ctz(enabled_and_pending);
Manish Pandey35e452f2021-02-18 21:36:34 +00001531 uint32_t intid_mask = 1U << bit_index;
1532
Andrew Walbran3d84a262018-12-13 14:41:19 +00001533 /*
1534 * Mark it as no longer pending and decrement the count.
1535 */
Manish Pandey35e452f2021-02-18 21:36:34 +00001536 current->interrupts.interrupt_pending[i] &= ~intid_mask;
1537
1538 if ((current->interrupts.interrupt_type[i] &
1539 intid_mask) == (INTERRUPT_TYPE_IRQ << bit_index)) {
1540 vcpu_irq_count_decrement(current_locked);
1541 } else {
1542 vcpu_fiq_count_decrement(current_locked);
1543 }
1544
Andrew Walbran3d84a262018-12-13 14:41:19 +00001545 first_interrupt =
1546 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001547 break;
1548 }
1549 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001550
Manish Pandey35e452f2021-02-18 21:36:34 +00001551 vcpu_unlock(&current_locked);
Andrew Walbran318f5732018-11-20 16:23:42 +00001552 return first_interrupt;
1553}
1554
1555/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001556 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001557 * given VM and vCPU.
1558 */
1559static inline bool is_injection_allowed(uint32_t target_vm_id,
1560 struct vcpu *current)
1561{
1562 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001563
Andrew Walbran318f5732018-11-20 16:23:42 +00001564 /*
1565 * The primary VM is allowed to inject interrupts into any VM. Secondary
1566 * VMs are only allowed to inject interrupts into their own vCPUs.
1567 */
1568 return current_vm_id == HF_PRIMARY_VM_ID ||
1569 current_vm_id == target_vm_id;
1570}
1571
1572/**
1573 * Injects a virtual interrupt of the given ID into the given target vCPU.
1574 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1575 * when the vCPU is next run, which is up to the scheduler.
1576 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001577 * Returns:
1578 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1579 * ID is invalid, or the current VM is not allowed to inject interrupts to
1580 * the target VM.
1581 * - 0 on success if no further action is needed.
1582 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1583 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001584 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1586 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001587 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001588{
Andrew Walbran318f5732018-11-20 16:23:42 +00001589 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001590 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001591
1592 if (intid >= HF_NUM_INTIDS) {
1593 return -1;
1594 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001595
Andrew Walbran318f5732018-11-20 16:23:42 +00001596 if (target_vm == NULL) {
1597 return -1;
1598 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001599
Andrew Walbran318f5732018-11-20 16:23:42 +00001600 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001601 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001602 return -1;
1603 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001604
Andrew Walbran318f5732018-11-20 16:23:42 +00001605 if (!is_injection_allowed(target_vm_id, current)) {
1606 return -1;
1607 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001608
Andrew Walbrane1310df2019-04-29 17:28:28 +01001609 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001610
Manish Pandey35e452f2021-02-18 21:36:34 +00001611 dlog_verbose(
1612 "Injecting interrupt %u for VM %#x vCPU %u from VM %#x vCPU "
1613 "%u\n",
1614 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1615 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001616 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001617}
Andrew Scull6386f252018-12-06 13:29:10 +00001618
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001619/** Returns the version of the implemented FF-A specification. */
1620struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001621{
1622 /*
1623 * Ensure that both major and minor revision representation occupies at
1624 * most 15 bits.
1625 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001626 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001627 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001628 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001629 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001630 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001631 /* Invalid encoding, return an error. */
J-Alves13318e32021-02-22 17:21:00 +00001632 return (struct ffa_value){.func = (uint32_t)FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001633 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001635 return (struct ffa_value){
1636 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1637 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001638}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001639
1640int64_t api_debug_log(char c, struct vcpu *current)
1641{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001642 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001643 struct vm *vm = current->vm;
1644 struct vm_locked vm_locked = vm_lock(vm);
1645
Andrew Sculld54e1be2019-08-20 11:09:42 +01001646 if (c == '\n' || c == '\0') {
1647 flush = true;
1648 } else {
1649 vm->log_buffer[vm->log_buffer_length++] = c;
1650 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1651 }
1652
1653 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001654 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1655 vm->log_buffer_length);
1656 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001657 }
1658
1659 vm_unlock(&vm_locked);
1660
1661 return 0;
1662}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001663
1664/**
1665 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001666 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001667 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001668struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001669{
1670 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001671 case FFA_ERROR_32:
1672 case FFA_SUCCESS_32:
1673 case FFA_INTERRUPT_32:
1674 case FFA_VERSION_32:
1675 case FFA_FEATURES_32:
1676 case FFA_RX_RELEASE_32:
1677 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001678 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001679 case FFA_ID_GET_32:
1680 case FFA_MSG_POLL_32:
1681 case FFA_MSG_WAIT_32:
1682 case FFA_YIELD_32:
1683 case FFA_RUN_32:
1684 case FFA_MSG_SEND_32:
1685 case FFA_MEM_DONATE_32:
1686 case FFA_MEM_LEND_32:
1687 case FFA_MEM_SHARE_32:
1688 case FFA_MEM_RETRIEVE_REQ_32:
1689 case FFA_MEM_RETRIEVE_RESP_32:
1690 case FFA_MEM_RELINQUISH_32:
1691 case FFA_MEM_RECLAIM_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001692 case FFA_MSG_SEND_DIRECT_RESP_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001693 case FFA_MSG_SEND_DIRECT_RESP_32:
J-Alvesbc3de8b2020-12-07 14:32:04 +00001694 case FFA_MSG_SEND_DIRECT_REQ_64:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001695 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001696 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001697 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001698 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001699 }
1700}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001701
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001702/**
J-Alves645eabe2021-02-22 16:08:27 +00001703 * FF-A specification states that x2/w2 Must Be Zero for direct messaging
1704 * interfaces.
1705 */
1706static inline bool api_ffa_dir_msg_is_arg2_zero(struct ffa_value args)
1707{
1708 return args.arg2 == 0U;
1709}
1710
1711/**
J-Alves76d99af2021-03-10 17:42:11 +00001712 * Limits size of arguments in ffa_value structure to 32-bit.
1713 */
1714static struct ffa_value api_ffa_value_copy32(struct ffa_value args)
1715{
1716 return (struct ffa_value){
1717 .func = (uint32_t)args.func,
1718 .arg1 = (uint32_t)args.arg1,
1719 .arg2 = (uint32_t)0,
1720 .arg3 = (uint32_t)args.arg3,
1721 .arg4 = (uint32_t)args.arg4,
1722 .arg5 = (uint32_t)args.arg5,
1723 .arg6 = (uint32_t)args.arg6,
1724 .arg7 = (uint32_t)args.arg7,
1725 };
1726}
1727
1728/**
1729 * Helper to copy direct message payload, depending on SMC used and expected
1730 * registers size.
1731 */
1732static struct ffa_value api_ffa_dir_msg_value(struct ffa_value args)
1733{
1734 if (args.func == FFA_MSG_SEND_DIRECT_REQ_32 ||
1735 args.func == FFA_MSG_SEND_DIRECT_RESP_32) {
1736 return api_ffa_value_copy32(args);
1737 }
1738
1739 return (struct ffa_value){
1740 .func = args.func,
1741 .arg1 = args.arg1,
1742 .arg2 = 0,
1743 .arg3 = args.arg3,
1744 .arg4 = args.arg4,
1745 .arg5 = args.arg5,
1746 .arg6 = args.arg6,
1747 .arg7 = args.arg7,
1748 };
1749}
1750
1751/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001752 * Send an FF-A direct message request.
1753 */
1754struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1755 ffa_vm_id_t receiver_vm_id,
1756 struct ffa_value args,
1757 struct vcpu *current,
1758 struct vcpu **next)
1759{
1760 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001761 struct vm *receiver_vm;
1762 struct vcpu *receiver_vcpu;
1763 struct two_vcpu_locked vcpus_locked;
1764
J-Alves645eabe2021-02-22 16:08:27 +00001765 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1766 return ffa_error(FFA_INVALID_PARAMETERS);
1767 }
1768
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001769 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1770 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001771 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001772 }
1773
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001774 receiver_vm = vm_find(receiver_vm_id);
1775 if (receiver_vm == NULL) {
1776 return ffa_error(FFA_INVALID_PARAMETERS);
1777 }
1778
1779 /*
1780 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1781 * UP (migratable) or MP partitions with a number of vCPUs matching the
1782 * number of PEs in the system. It further states that MP partitions
1783 * accepting direct request messages cannot migrate.
1784 */
J-Alvesad6a0432021-04-09 16:06:21 +01001785 receiver_vcpu = api_ffa_get_vm_vcpu(receiver_vm, current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001786 if (receiver_vcpu == NULL) {
1787 return ffa_error(FFA_INVALID_PARAMETERS);
1788 }
1789
1790 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1791
1792 /*
1793 * If destination vCPU is executing or already received an
1794 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1795 * busy. There is a brief period of time where the vCPU state has
1796 * changed but regs_available is still false thus consider this case as
1797 * the vCPU not yet ready to receive a direct message request.
1798 */
1799 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1800 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1801 !receiver_vcpu->regs_available) {
1802 ret = ffa_error(FFA_BUSY);
1803 goto out;
1804 }
1805
1806 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1807 memory_order_relaxed)) {
1808 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001809 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001810 receiver_vcpu->vm->id,
1811 vcpu_index(receiver_vcpu));
1812 receiver_vcpu->state = VCPU_STATE_ABORTED;
1813 }
1814
1815 ret = ffa_error(FFA_ABORTED);
1816 goto out;
1817 }
1818
1819 switch (receiver_vcpu->state) {
1820 case VCPU_STATE_OFF:
1821 case VCPU_STATE_RUNNING:
1822 case VCPU_STATE_ABORTED:
1823 case VCPU_STATE_READY:
1824 case VCPU_STATE_BLOCKED_INTERRUPT:
1825 ret = ffa_error(FFA_BUSY);
1826 goto out;
1827 case VCPU_STATE_BLOCKED_MAILBOX:
1828 /*
1829 * Expect target vCPU to be blocked after having called
1830 * ffa_msg_wait or sent a direct message response.
1831 */
1832 break;
1833 }
1834
1835 /* Inject timer interrupt if any pending */
1836 if (arch_timer_pending(&receiver_vcpu->regs)) {
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001837 api_interrupt_inject_locked(vcpus_locked.vcpu1,
1838 HF_VIRTUAL_TIMER_INTID, current,
1839 NULL);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001840
1841 arch_timer_mask(&receiver_vcpu->regs);
1842 }
1843
1844 /* The receiver vCPU runs upon direct message invocation */
1845 receiver_vcpu->cpu = current->cpu;
1846 receiver_vcpu->state = VCPU_STATE_RUNNING;
1847 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001848 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001849
J-Alves76d99af2021-03-10 17:42:11 +00001850 arch_regs_set_retval(&receiver_vcpu->regs, api_ffa_dir_msg_value(args));
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001851
1852 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1853
1854 /* Switch to receiver vCPU targeted to by direct msg request */
1855 *next = receiver_vcpu;
1856
1857 /*
1858 * Since this flow will lead to a VM switch, the return value will not
1859 * be applied to current vCPU.
1860 */
1861
1862out:
1863 sl_unlock(&receiver_vcpu->lock);
1864 sl_unlock(&current->lock);
1865
1866 return ret;
1867}
1868
1869/**
1870 * Send an FF-A direct message response.
1871 */
1872struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1873 ffa_vm_id_t receiver_vm_id,
1874 struct ffa_value args,
1875 struct vcpu *current,
1876 struct vcpu **next)
1877{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001878 struct vcpu_locked current_locked;
J-Alves645eabe2021-02-22 16:08:27 +00001879
1880 if (!api_ffa_dir_msg_is_arg2_zero(args)) {
1881 return ffa_error(FFA_INVALID_PARAMETERS);
1882 }
1883
J-Alves76d99af2021-03-10 17:42:11 +00001884 struct ffa_value to_ret = api_ffa_dir_msg_value(args);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001885
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001886 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1887 receiver_vm_id)) {
J-Alvesaa336102021-03-01 13:02:45 +00001888 return ffa_error(FFA_INVALID_PARAMETERS);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001889 }
1890
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001891 current_locked = vcpu_lock(current);
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001892 if (api_ffa_is_managed_exit_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001893 /*
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001894 * No need for REQ/RESP state management as managed exit does
1895 * not have corresponding REQ pair.
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001896 */
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001897 if (receiver_vm_id != HF_PRIMARY_VM_ID) {
1898 vcpu_unlock(&current_locked);
1899 return ffa_error(FFA_DENIED);
1900 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001901
Manish Pandeya5f39fb2020-09-11 09:47:11 +01001902 plat_interrupts_set_priority_mask(0xff);
1903 current->processing_managed_exit = false;
1904 } else {
1905 /*
1906 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1907 * defined originator.
1908 */
1909 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
1910 /*
1911 * Sending direct response but direct request origin
1912 * vCPU is not set.
1913 */
1914 vcpu_unlock(&current_locked);
1915 return ffa_error(FFA_DENIED);
1916 }
1917
1918 if (current->direct_request_origin_vm_id != receiver_vm_id) {
1919 vcpu_unlock(&current_locked);
1920 return ffa_error(FFA_DENIED);
1921 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001922 }
1923
1924 /* Clear direct request origin for the caller. */
1925 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1926
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001927 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001928
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001929 if (!vm_id_is_current_world(receiver_vm_id)) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001930 *next = api_switch_to_other_world(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001931 VCPU_STATE_BLOCKED_MAILBOX);
1932 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001933 *next = api_switch_to_primary(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001934 VCPU_STATE_BLOCKED_MAILBOX);
J-Alvesfe7f7372020-11-09 11:32:12 +00001935 } else if (vm_id_is_current_world(receiver_vm_id)) {
1936 /*
1937 * It is expected the receiver_vm_id to be from an SP, otherwise
1938 * 'arch_other_world_is_direct_response_valid' should have
1939 * made function return error before getting to this point.
1940 */
1941 *next = api_switch_to_vm(current, to_ret,
1942 VCPU_STATE_BLOCKED_MAILBOX,
1943 receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001944 } else {
1945 panic("Invalid direct message response invocation");
1946 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001947
1948 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1949}
1950
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001951struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1952 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001953 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001954{
1955 struct vm *from = current->vm;
1956 struct vm *to;
1957 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001958 struct ffa_memory_region *memory_region;
1959 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001960
1961 if (ipa_addr(address) != 0 || page_count != 0) {
1962 /*
1963 * Hafnium only supports passing the descriptor in the TX
1964 * mailbox.
1965 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001966 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001967 }
1968
Andrew Walbranca808b12020-05-15 17:22:28 +01001969 if (fragment_length > length) {
1970 dlog_verbose(
1971 "Fragment length %d greater than total length %d.\n",
1972 fragment_length, length);
1973 return ffa_error(FFA_INVALID_PARAMETERS);
1974 }
1975 if (fragment_length < sizeof(struct ffa_memory_region) +
1976 sizeof(struct ffa_memory_access)) {
1977 dlog_verbose(
1978 "Initial fragment length %d smaller than header size "
1979 "%d.\n",
1980 fragment_length,
1981 sizeof(struct ffa_memory_region) +
1982 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001983 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001984 }
1985
1986 /*
1987 * Check that the sender has configured its send buffer. If the TX
1988 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1989 * be safely accessed after releasing the lock since the TX mailbox
1990 * address can only be configured once.
1991 */
1992 sl_lock(&from->lock);
1993 from_msg = from->mailbox.send;
1994 sl_unlock(&from->lock);
1995
1996 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001997 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001998 }
1999
2000 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002001 * Copy the memory region descriptor to a fresh page from the memory
2002 * pool. This prevents the sender from changing it underneath us, and
2003 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002004 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002005 if (fragment_length > HF_MAILBOX_SIZE ||
2006 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002007 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002008 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002009 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002010 if (memory_region == NULL) {
2011 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002012 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002013 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002014 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002015
2016 /* The sender must match the caller. */
2017 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002018 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002019 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002020 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002021 }
2022
Andrew Walbrana65a1322020-04-06 19:32:32 +01002023 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002024 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002025 dlog_verbose(
2026 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002027 "endpoint memory access descriptors, expected 1).\n",
2028 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002029 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002030 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002031 }
2032
2033 /*
2034 * Ensure that the receiver VM exists and isn't the same as the sender.
2035 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002036 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002037 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002038 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002039 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002040 goto out;
2041 }
2042
2043 if (to->id == HF_TEE_VM_ID) {
2044 /*
2045 * The 'to' VM lock is only needed in the case that it is the
2046 * TEE VM.
2047 */
2048 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2049
2050 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002051 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002052 goto out_unlock;
2053 }
2054
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002055 ret = ffa_memory_tee_send(
2056 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
2057 length, fragment_length, share_func, &api_page_pool);
2058 /*
2059 * ffa_tee_memory_send takes ownership of the memory_region, so
2060 * make sure we don't free it.
2061 */
2062 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002063
2064 out_unlock:
2065 vm_unlock(&vm_to_from_lock.vm1);
2066 vm_unlock(&vm_to_from_lock.vm2);
2067 } else {
2068 struct vm_locked from_locked = vm_lock(from);
2069
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002070 ret = ffa_memory_send(from_locked, memory_region, length,
2071 fragment_length, share_func,
2072 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002073 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002074 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002075 * make sure we don't free it.
2076 */
2077 memory_region = NULL;
2078
2079 vm_unlock(&from_locked);
2080 }
2081
2082out:
2083 if (memory_region != NULL) {
2084 mpool_free(&api_page_pool, memory_region);
2085 }
2086
2087 return ret;
2088}
2089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002090struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
2091 uint32_t fragment_length,
2092 ipaddr_t address, uint32_t page_count,
2093 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002094{
2095 struct vm *to = current->vm;
2096 struct vm_locked to_locked;
2097 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002098 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002099 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002100 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002101
2102 if (ipa_addr(address) != 0 || page_count != 0) {
2103 /*
2104 * Hafnium only supports passing the descriptor in the TX
2105 * mailbox.
2106 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002107 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002108 }
2109
Andrew Walbrana65a1322020-04-06 19:32:32 +01002110 if (fragment_length != length) {
2111 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002112 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002113 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002114
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002115 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002116 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002117 message_buffer_size = cpu_get_buffer_size(current->cpu);
2118 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2119 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121 }
2122
2123 to_locked = vm_lock(to);
2124 to_msg = to->mailbox.send;
2125
2126 if (to_msg == NULL) {
2127 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002128 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002129 goto out;
2130 }
2131
2132 /*
2133 * Copy the retrieve request descriptor to an internal buffer, so that
2134 * the caller can't change it underneath us.
2135 */
2136 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2137
2138 if (msg_receiver_busy(to_locked, NULL, false)) {
2139 /*
2140 * Can't retrieve memory information if the mailbox is not
2141 * available.
2142 */
2143 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002144 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002145 goto out;
2146 }
2147
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2149 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002150
2151out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002152 vm_unlock(&to_locked);
2153 return ret;
2154}
2155
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002156struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002157{
2158 struct vm *from = current->vm;
2159 struct vm_locked from_locked;
2160 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002161 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002162 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002163 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002164 uint32_t length;
2165
2166 from_locked = vm_lock(from);
2167 from_msg = from->mailbox.send;
2168
2169 if (from_msg == NULL) {
2170 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002171 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002172 goto out;
2173 }
2174
2175 /*
2176 * Calculate length from relinquish descriptor before copying. We will
2177 * check again later to make sure it hasn't changed.
2178 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 length = sizeof(struct ffa_mem_relinquish) +
2180 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2181 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002182 /*
2183 * Copy the relinquish descriptor to an internal buffer, so that the
2184 * caller can't change it underneath us.
2185 */
2186 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002187 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002188 message_buffer_size = cpu_get_buffer_size(current->cpu);
2189 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2190 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002191 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002192 goto out;
2193 }
2194 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2195
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002196 if (sizeof(struct ffa_mem_relinquish) +
2197 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002198 length) {
2199 dlog_verbose(
2200 "Endpoint count changed while copying to internal "
2201 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002202 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002203 goto out;
2204 }
2205
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2207 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002208
2209out:
2210 vm_unlock(&from_locked);
2211 return ret;
2212}
2213
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002214struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2215 ffa_memory_region_flags_t flags,
2216 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002217{
2218 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002219 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002220
J-Alves917d2f22020-10-30 18:39:30 +00002221 if (ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002222 struct vm_locked to_locked = vm_lock(to);
2223
Andrew Walbranca808b12020-05-15 17:22:28 +01002224 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002225 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002226
Andrew Walbran290b0c92020-02-03 16:37:14 +00002227 vm_unlock(&to_locked);
2228 } else {
2229 struct vm *from = vm_find(HF_TEE_VM_ID);
2230 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2231
Andrew Walbranca808b12020-05-15 17:22:28 +01002232 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2233 vm_to_from_lock.vm2, handle, flags,
2234 &api_page_pool);
2235
2236 vm_unlock(&vm_to_from_lock.vm1);
2237 vm_unlock(&vm_to_from_lock.vm2);
2238 }
2239
2240 return ret;
2241}
2242
2243struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2244 uint32_t fragment_offset,
2245 ffa_vm_id_t sender_vm_id,
2246 struct vcpu *current)
2247{
2248 struct vm *to = current->vm;
2249 struct vm_locked to_locked;
2250 struct ffa_value ret;
2251
2252 /* Sender ID MBZ at virtual instance. */
2253 if (sender_vm_id != 0) {
2254 return ffa_error(FFA_INVALID_PARAMETERS);
2255 }
2256
2257 to_locked = vm_lock(to);
2258
2259 if (msg_receiver_busy(to_locked, NULL, false)) {
2260 /*
2261 * Can't retrieve memory information if the mailbox is not
2262 * available.
2263 */
2264 dlog_verbose("RX buffer not ready.\n");
2265 ret = ffa_error(FFA_BUSY);
2266 goto out;
2267 }
2268
2269 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2270 &api_page_pool);
2271
2272out:
2273 vm_unlock(&to_locked);
2274 return ret;
2275}
2276
2277struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2278 uint32_t fragment_length,
2279 ffa_vm_id_t sender_vm_id,
2280 struct vcpu *current)
2281{
2282 struct vm *from = current->vm;
2283 const void *from_msg;
2284 void *fragment_copy;
2285 struct ffa_value ret;
2286
2287 /* Sender ID MBZ at virtual instance. */
2288 if (sender_vm_id != 0) {
2289 return ffa_error(FFA_INVALID_PARAMETERS);
2290 }
2291
2292 /*
2293 * Check that the sender has configured its send buffer. If the TX
2294 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2295 * be safely accessed after releasing the lock since the TX mailbox
2296 * address can only be configured once.
2297 */
2298 sl_lock(&from->lock);
2299 from_msg = from->mailbox.send;
2300 sl_unlock(&from->lock);
2301
2302 if (from_msg == NULL) {
2303 return ffa_error(FFA_INVALID_PARAMETERS);
2304 }
2305
2306 /*
2307 * Copy the fragment to a fresh page from the memory pool. This prevents
2308 * the sender from changing it underneath us, and also lets us keep it
2309 * around in the share state table if needed.
2310 */
2311 if (fragment_length > HF_MAILBOX_SIZE ||
2312 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2313 dlog_verbose(
2314 "Fragment length %d larger than mailbox size %d.\n",
2315 fragment_length, HF_MAILBOX_SIZE);
2316 return ffa_error(FFA_INVALID_PARAMETERS);
2317 }
2318 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2319 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2320 0) {
2321 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2322 return ffa_error(FFA_INVALID_PARAMETERS);
2323 }
2324 fragment_copy = mpool_alloc(&api_page_pool);
2325 if (fragment_copy == NULL) {
2326 dlog_verbose("Failed to allocate fragment copy.\n");
2327 return ffa_error(FFA_NO_MEMORY);
2328 }
2329 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2330
2331 /*
2332 * Hafnium doesn't support fragmentation of memory retrieve requests
2333 * (because it doesn't support caller-specified mappings, so a request
2334 * will never be larger than a single page), so this must be part of a
2335 * memory send (i.e. donate, lend or share) request.
2336 *
2337 * We can tell from the handle whether the memory transaction is for the
2338 * TEE or not.
2339 */
2340 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2341 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2342 struct vm_locked from_locked = vm_lock(from);
2343
2344 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2345 fragment_length, handle,
2346 &api_page_pool);
2347 /*
2348 * `ffa_memory_send_continue` takes ownership of the
2349 * fragment_copy, so we don't need to free it here.
2350 */
2351 vm_unlock(&from_locked);
2352 } else {
2353 struct vm *to = vm_find(HF_TEE_VM_ID);
2354 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2355
2356 /*
2357 * The TEE RX buffer state is checked in
2358 * `ffa_memory_tee_send_continue` rather than here, as we need
2359 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2360 * than FFA_ERROR FFA_BUSY in case it is busy.
2361 */
2362
2363 ret = ffa_memory_tee_send_continue(
2364 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2365 fragment_length, handle, &api_page_pool);
2366 /*
2367 * `ffa_memory_tee_send_continue` takes ownership of the
2368 * fragment_copy, so we don't need to free it here.
2369 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002370
2371 vm_unlock(&vm_to_from_lock.vm1);
2372 vm_unlock(&vm_to_from_lock.vm2);
2373 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002374
2375 return ret;
2376}
Max Shvetsov40108e72020-08-27 12:39:50 +01002377
2378struct ffa_value api_ffa_secondary_ep_register(ipaddr_t entry_point,
2379 struct vcpu *current)
2380{
2381 struct vm_locked vm_locked;
2382
2383 vm_locked = vm_lock(current->vm);
2384 vm_locked.vm->secondary_ep = entry_point;
2385 vm_unlock(&vm_locked);
2386
2387 return (struct ffa_value){.func = FFA_SUCCESS_32};
2388}