blob: 1457d584c0f9cb14008917abb929c23b3c920537 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 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"
Olivier Deprez96a2a262020-06-11 17:21:38 +020012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000014#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000015
Andrew Scull877ae4b2019-07-02 12:52:33 +010016#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "hf/ffa_internal.h"
19#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000020#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010021#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000022#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010023#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010024#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010025#include "hf/vm.h"
26
Andrew Scullf35a5c92018-08-07 18:09:46 +010027#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010028#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010029
Fuad Tabbae4efcc32020-07-16 15:37:27 +010030static_assert(sizeof(struct ffa_partition_info) == 8,
31 "Partition information descriptor size doesn't match the one in "
32 "the FF-A 1.0 EAC specification, Table 82.");
33
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000034/*
35 * To eliminate the risk of deadlocks, we define a partial order for the
36 * acquisition of locks held concurrently by the same physical CPU. Our current
37 * ordering requirements are as follows:
38 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010039 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000040 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010041 * Locks of the same kind require the lock of lowest address to be locked first,
42 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000043 */
44
Andrew Scullaa039b32018-10-04 15:02:26 +010045static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010046 "Currently, a page is mapped for the send and receive buffers so "
47 "the maximum request is the size of a page.");
48
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000049static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
50 "The page pool entry size must be at least as big as the mailbox "
51 "size, so that memory region descriptors can be copied from the "
52 "mailbox for memory sharing.");
53
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000054static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000055
56/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000057 * Initialises the API page pool by taking ownership of the contents of the
58 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000059 */
60void api_init(struct mpool *ppool)
61{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000062 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000063}
64
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010065/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000066 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010067 *
68 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010069 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010070 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010071static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000073 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010074{
Andrew Walbran42347a92019-05-09 13:59:03 +010075 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010076 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010077
Andrew Walbran508e63c2018-12-20 17:02:37 +000078 /*
79 * If the secondary is blocked but has a timer running, sleep until the
80 * timer fires rather than indefinitely.
81 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010082 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010083 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
84 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010085 if (arch_timer_enabled_current()) {
86 uint64_t remaining_ns =
87 arch_timer_remaining_ns_current();
88
89 if (remaining_ns == 0) {
90 /*
91 * Timer is pending, so the current vCPU should
92 * be run again right away.
93 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010094 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010095 /*
96 * primary_ret.arg1 should already be set to the
97 * current VM ID and vCPU ID.
98 */
99 primary_ret.arg2 = 0;
100 } else {
101 primary_ret.arg2 = remaining_ns;
102 }
103 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100104 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100105 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000106 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100107 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000108
109 default:
110 /* Do nothing. */
111 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000112 }
113
Andrew Walbrana0e30b02020-10-22 15:55:45 +0100114 /* Set the return value for the primary VM's call to FFA_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100115 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100116
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000117 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000118 sl_lock(&current->lock);
119 current->state = secondary_state;
120 sl_unlock(&current->lock);
121
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100122 return next;
123}
124
125/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200126 * Choose next vCPU to run to be the counterpart vCPU in the other
127 * world (run the normal world if currently running in the secure
128 * world). Set current vCPU state to the given vcpu_state parameter.
129 * Set FF-A return values to the target vCPU in the other world.
130 *
131 * Called in context of a direct message response from a secure
132 * partition to a VM.
133 */
134static struct vcpu *api_switch_to_other_world(struct vcpu *current,
135 struct ffa_value other_world_ret,
136 enum vcpu_state vcpu_state)
137{
138 struct vcpu *next = vcpu_get_other_world_counterpart(current);
139
140 CHECK(next != NULL);
141
142 /* Set the return value for the other world's VM. */
143 arch_regs_set_retval(&next->regs, other_world_ret);
144
145 /* Set the current vCPU state. */
146 sl_lock(&current->lock);
147 current->state = vcpu_state;
148 sl_unlock(&current->lock);
149
150 return next;
151}
152
153/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100154 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
155 * registers the `from` VM to be notified when it becomes available.
156 */
157static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
158{
159 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
160 to.vm->mailbox.recv == NULL) {
161 /*
162 * Fail if the receiver isn't currently ready to receive data,
163 * setting up for notification if requested.
164 */
165 if (notify) {
166 struct wait_entry *entry =
167 vm_get_wait_entry(from, to.vm->id);
168
169 /* Append waiter only if it's not there yet. */
170 if (list_empty(&entry->wait_links)) {
171 list_append(&to.vm->mailbox.waiter_list,
172 &entry->wait_links);
173 }
174 }
175
176 return true;
177 }
178
179 return false;
180}
181
182/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000183 * Returns true if the given vCPU is executing in context of an
184 * FFA_MSG_SEND_DIRECT_REQ invocation.
185 */
186static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
187{
188 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
189}
190
191/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000192 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000193 */
194struct vcpu *api_preempt(struct vcpu *current)
195{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100196 struct ffa_value ret = {
197 .func = FFA_INTERRUPT_32,
198 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000199 };
200
Andrew Sculld6ee1102019-04-05 22:12:42 +0100201 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000202}
203
204/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000205 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000206 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100207 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100208struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100209{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100210 struct ffa_value ret = {
211 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
212 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100213 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000214
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000215 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100216 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100217}
218
219/**
Andrew Walbran33645652019-04-15 12:29:31 +0100220 * Puts the current vCPU in off mode, and returns to the primary VM.
221 */
222struct vcpu *api_vcpu_off(struct vcpu *current)
223{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100224 struct ffa_value ret = {
225 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
226 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100227 };
228
229 /*
230 * Disable the timer, so the scheduler doesn't get told to call back
231 * based on it.
232 */
233 arch_timer_disable_current();
234
235 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
236}
237
238/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000239 * Returns to the primary VM to allow this CPU to be used for other tasks as the
240 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100241 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000242 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000243struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000244{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000245 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
246 struct vcpu_locked current_locked;
247 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000248
249 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000250 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000251 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000252 }
253
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000254 current_locked = vcpu_lock(current);
255 is_direct_request_ongoing =
256 is_ffa_direct_msg_request_ongoing(current_locked);
257 vcpu_unlock(&current_locked);
258
259 if (is_direct_request_ongoing) {
260 return ffa_error(FFA_DENIED);
261 }
262
263 *next = api_switch_to_primary(
264 current,
265 (struct ffa_value){.func = FFA_YIELD_32,
266 .arg1 = ffa_vm_vcpu(current->vm->id,
267 vcpu_index(current))},
268 VCPU_STATE_READY);
269
270 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000271}
272
273/**
Andrew Walbran33645652019-04-15 12:29:31 +0100274 * Switches to the primary so that it can switch to the target, or kick it if it
275 * is already running on a different physical CPU.
276 */
277struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
278{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100279 struct ffa_value ret = {
280 .func = HF_FFA_RUN_WAKE_UP,
281 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
282 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100283 };
284 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
285}
286
287/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000288 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000289 */
290struct vcpu *api_abort(struct vcpu *current)
291{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000293
Andrew Walbran17eebf92020-02-05 16:35:49 +0000294 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
295 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000296
297 if (current->vm->id == HF_PRIMARY_VM_ID) {
298 /* TODO: what to do when the primary aborts? */
299 for (;;) {
300 /* Do nothing. */
301 }
302 }
303
304 atomic_store_explicit(&current->vm->aborting, true,
305 memory_order_relaxed);
306
307 /* TODO: free resources once all vCPUs abort. */
308
Andrew Sculld6ee1102019-04-05 22:12:42 +0100309 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000310}
311
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100312struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
313 const struct ffa_uuid *uuid)
314{
315 struct vm *current_vm = current->vm;
316 struct vm_locked current_vm_locked;
317 ffa_vm_count_t vm_count = 0;
318 bool uuid_is_null = ffa_uuid_is_null(uuid);
319 struct ffa_value ret;
320 uint32_t size;
321 struct ffa_partition_info partitions[MAX_VMS];
322
323 /*
324 * Iterate through the VMs to find the ones with a matching UUID.
325 * A Null UUID retrieves information for all VMs.
326 */
327 for (uint16_t index = 0; index < vm_get_count(); ++index) {
328 const struct vm *vm = vm_find_index(index);
329
330 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
331 partitions[vm_count].vm_id = vm->id;
332 partitions[vm_count].vcpu_count = vm->vcpu_count;
333
334 /* Hafnium only supports indirect messaging. */
335 partitions[vm_count].properties =
336 FFA_PARTITION_INDIRECT_MSG;
337
338 ++vm_count;
339 }
340 }
341
342 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
343 if (vm_count == 0) {
344 return ffa_error(FFA_INVALID_PARAMETERS);
345 }
346
347 size = vm_count * sizeof(partitions[0]);
348 if (size > FFA_MSG_PAYLOAD_MAX) {
349 dlog_error(
350 "Partition information does not fit in the VM's RX "
351 "buffer.\n");
352 return ffa_error(FFA_NO_MEMORY);
353 }
354
355 /*
356 * Partition information is returned in the VM's RX buffer, which is why
357 * the lock is needed.
358 */
359 current_vm_locked = vm_lock(current_vm);
360
361 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
362 /*
363 * Can't retrieve memory information if the mailbox is not
364 * available.
365 */
366 dlog_verbose("RX buffer not ready.\n");
367 ret = ffa_error(FFA_BUSY);
368 goto out_unlock;
369 }
370
371 /* Populate the VM's RX buffer with the partition information. */
372 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
373 size);
374 current_vm->mailbox.recv_size = size;
375 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
376 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
377 current_vm->mailbox.state = MAILBOX_STATE_READ;
378
379 /* Return the count of partition information descriptors in w2. */
380 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
381
382out_unlock:
383 vm_unlock(&current_vm_locked);
384
385 return ret;
386}
387
Andrew Scull9726c252019-01-23 13:44:19 +0000388/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000389 * Returns the ID of the VM.
390 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100391struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000392{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100393 return (struct ffa_value){.func = FFA_SUCCESS_32,
394 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000395}
396
397/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000398 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000399 * function to indicate that register state for the given vCPU has been saved
400 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000401 */
402void api_regs_state_saved(struct vcpu *vcpu)
403{
404 sl_lock(&vcpu->lock);
405 vcpu->regs_available = true;
406 sl_unlock(&vcpu->lock);
407}
408
409/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000410 * Retrieves the next waiter and removes it from the wait list if the VM's
411 * mailbox is in a writable state.
412 */
413static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
414{
415 struct wait_entry *entry;
416 struct vm *vm = locked_vm.vm;
417
Andrew Sculld6ee1102019-04-05 22:12:42 +0100418 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000419 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
420 /* The mailbox is not writable or there are no waiters. */
421 return NULL;
422 }
423
424 /* Remove waiter from the wait list. */
425 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
426 wait_links);
427 list_remove(&entry->wait_links);
428 return entry;
429}
430
431/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000432 * Assuming that the arguments have already been checked by the caller, injects
433 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
434 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
435 * is next run, which is up to the scheduler.
436 *
437 * Returns:
438 * - 0 on success if no further action is needed.
439 * - 1 if it was called by the primary VM and the primary VM now needs to wake
440 * up or kick the target vCPU.
441 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200442static int64_t internal_interrupt_inject_locked(
443 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
444 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000445{
446 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100447 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000448 int64_t ret = 0;
449
Andrew Walbran508e63c2018-12-20 17:02:37 +0000450 /*
451 * We only need to change state and (maybe) trigger a virtual IRQ if it
452 * is enabled and was not previously pending. Otherwise we can skip
453 * everything except setting the pending bit.
454 *
455 * If you change this logic make sure to update the need_vm_lock logic
456 * above to match.
457 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200458 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
459 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000460 intid_mask)) {
461 goto out;
462 }
463
464 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200465 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000466
467 /*
468 * Only need to update state if there was not already an
469 * interrupt enabled and pending.
470 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200471 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000472 goto out;
473 }
474
Andrew Walbran508e63c2018-12-20 17:02:37 +0000475 if (current->vm->id == HF_PRIMARY_VM_ID) {
476 /*
477 * If the call came from the primary VM, let it know that it
478 * should run or kick the target vCPU.
479 */
480 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200481 } else if (current != target_locked.vcpu && next != NULL) {
482 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000483 }
484
485out:
486 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200487 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
488 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000489
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200490 return ret;
491}
492
493/* Wrapper to internal_interrupt_inject with locking of target vCPU */
494static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
495 uint32_t intid, struct vcpu *current,
496 struct vcpu **next)
497{
498 int64_t ret;
499 struct vcpu_locked target_locked;
500
501 target_locked = vcpu_lock(target_vcpu);
502 ret = internal_interrupt_inject_locked(target_locked, intid, current,
503 next);
504 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000505
506 return ret;
507}
508
509/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100510 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
511 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100512 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100514{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000515 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100516 case FFA_MSG_SEND_32:
517 return (struct ffa_value){
518 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000519 .arg1 = (receiver->mailbox.recv_sender << 16) |
520 receiver->id,
521 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000522 default:
523 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000524 dlog_error("Tried to return an invalid message function %#x\n",
525 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000527 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100528}
529
530/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000531 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000532 * value needs to be forced onto the vCPU.
533 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000534static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000536{
Andrew Scullb06d1752019-02-04 10:15:48 +0000537 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000538 bool ret;
539
Andrew Scullb06d1752019-02-04 10:15:48 +0000540 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000541 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000542 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100543 * The VM lock is not needed in the common case so it must only be taken
544 * when it is going to be needed. This ensures there are no inter-vCPU
545 * dependencies in the common run case meaning the sensitive context
546 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000547 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000548 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000549
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000550 /* The VM needs to be locked to deliver mailbox messages. */
551 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
552 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000553 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000554 sl_lock(&vcpu->vm->lock);
555 sl_lock(&vcpu->lock);
556 }
557
558 /*
559 * If the vCPU is already running somewhere then we can't run it here
560 * simultaneously. While it is actually running then the state should be
561 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
562 * stops running but while Hafnium is in the process of switching back
563 * to the primary there will be a brief period while the state has been
564 * updated but `regs_available` is still false (until
565 * `api_regs_state_saved` is called). We can't start running it again
566 * until this has finished, so count this state as still running for the
567 * purposes of this check.
568 */
569 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
570 /*
571 * vCPU is running on another pCPU.
572 *
573 * It's okay not to return the sleep duration here because the
574 * other physical CPU that is currently running this vCPU will
575 * return the sleep duration if needed.
576 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000578 ret = false;
579 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000580 }
Andrew Scull9726c252019-01-23 13:44:19 +0000581
582 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100583 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000584 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
585 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100586 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000587 }
588 ret = false;
589 goto out;
590 }
591
Andrew Walbran508e63c2018-12-20 17:02:37 +0000592 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100593 case VCPU_STATE_RUNNING:
594 case VCPU_STATE_OFF:
595 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000596 ret = false;
597 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000598
Andrew Sculld6ee1102019-04-05 22:12:42 +0100599 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000600 /*
601 * A pending message allows the vCPU to run so the message can
602 * be delivered directly.
603 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100604 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100605 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100606 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100607 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000608 break;
609 }
610 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100611 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000612 /* Allow virtual interrupts to be delivered. */
613 if (vcpu->interrupts.enabled_and_pending_count > 0) {
614 break;
615 }
616
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100617 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
618
Andrew Walbran508e63c2018-12-20 17:02:37 +0000619 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100620 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000621 arch_timer_remaining_ns(&vcpu->regs);
622
623 /*
624 * The timer expired so allow the interrupt to be
625 * delivered.
626 */
627 if (timer_remaining_ns == 0) {
628 break;
629 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100630 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000631
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100632 /*
633 * The vCPU is not ready to run, return the appropriate code to
634 * the primary which called vcpu_run.
635 */
636 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100637 ? FFA_MSG_WAIT_32
638 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100639 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
640 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000641
642 ret = false;
643 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000644
Andrew Sculld6ee1102019-04-05 22:12:42 +0100645 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000646 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000647 }
648
Andrew Scullb06d1752019-02-04 10:15:48 +0000649 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000650 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100651 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000652
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000653 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000654 * Mark the registers as unavailable now that we're about to reflect
655 * them onto the real registers. This will also prevent another physical
656 * CPU from trying to read these registers.
657 */
658 vcpu->regs_available = false;
659
660 ret = true;
661
662out:
663 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000664 if (need_vm_lock) {
665 sl_unlock(&vcpu->vm->lock);
666 }
667
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000668 return ret;
669}
670
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
672 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100673{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100674 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100675 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100677
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000678 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100679 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100681 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100682 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100683
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000684 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100685 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100686 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100687 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100688
Andrew Scull19503262018-09-20 14:48:39 +0100689 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100690 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100691 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100692 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100693 }
694
Fuad Tabbaed294af2019-12-20 10:43:01 +0000695 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100696 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100697 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100698 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100699
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000700 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100701 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000702 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000703 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100704 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000705
Andrew Walbran508e63c2018-12-20 17:02:37 +0000706 /*
707 * Inject timer interrupt if timer has expired. It's safe to access
708 * vcpu->regs here because api_vcpu_prepare_run already made sure that
709 * regs_available was true (and then set it to false) before returning
710 * true.
711 */
712 if (arch_timer_pending(&vcpu->regs)) {
713 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100714 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
715 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000716
717 /*
718 * Set the mask bit so the hardware interrupt doesn't fire
719 * again. Ideally we wouldn't do this because it affects what
720 * the secondary vCPU sees, but if we don't then we end up with
721 * a loop of the interrupt firing each time we try to return to
722 * the secondary vCPU.
723 */
724 arch_timer_mask(&vcpu->regs);
725 }
726
Fuad Tabbaed294af2019-12-20 10:43:01 +0000727 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000728 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000729
Andrew Scull33fecd32019-01-08 14:48:27 +0000730 /*
731 * Set a placeholder return code to the scheduler. This will be
732 * overwritten when the switch back to the primary occurs.
733 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734 ret.func = FFA_INTERRUPT_32;
735 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100736 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000737
Andrew Scull6d2db332018-10-10 15:28:17 +0100738out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100739 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100740}
741
742/**
Andrew Scull81e85092018-12-12 12:56:20 +0000743 * Check that the mode indicates memory that is valid, owned and exclusive.
744 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100745static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000746{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100747 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
748 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000749}
750
751/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100752 * Determines the value to be returned by api_ffa_rxtx_map and
753 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
754 * there are waiters, it also switches back to the primary VM for it to wake
755 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000756 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100757static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
758 struct vcpu *current,
759 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000760{
761 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000762
763 if (list_empty(&vm->mailbox.waiter_list)) {
764 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100765 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000766 }
767
768 if (vm->id == HF_PRIMARY_VM_ID) {
769 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100770 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000771 }
772
773 /*
774 * Switch back to the primary VM, informing it that there are waiters
775 * that need to be notified.
776 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000777 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100778 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000779 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000780
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100781 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000782}
783
784/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100785 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100786 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100787static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
788 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100789 paddr_t pa_send_begin, paddr_t pa_send_end,
790 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200791 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100792 struct mpool *local_page_pool)
793{
794 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100795
796 /* Map the send page as read-only in the hypervisor address space. */
797 vm_locked.vm->mailbox.send =
798 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200799 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100800 if (!vm_locked.vm->mailbox.send) {
801 /* TODO: partial defrag of failed range. */
802 /* Recover any memory consumed in failed mapping. */
803 mm_defrag(mm_stage1_locked, local_page_pool);
804 goto fail;
805 }
806
807 /*
808 * Map the receive page as writable in the hypervisor address space. On
809 * failure, unmap the send page before returning.
810 */
811 vm_locked.vm->mailbox.recv =
812 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200813 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100814 if (!vm_locked.vm->mailbox.recv) {
815 /* TODO: partial defrag of failed range. */
816 /* Recover any memory consumed in failed mapping. */
817 mm_defrag(mm_stage1_locked, local_page_pool);
818 goto fail_undo_send;
819 }
820
821 ret = true;
822 goto out;
823
824 /*
825 * The following mappings will not require more memory than is available
826 * in the local pool.
827 */
828fail_undo_send:
829 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100830 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
831 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100832
833fail:
834 ret = false;
835
836out:
Andrew Sculle1322792019-07-01 17:46:10 +0100837 return ret;
838}
839
840/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100841 * Sanity checks and configures the send and receive pages in the VM stage-2
842 * and hypervisor stage-1 page tables.
843 *
844 * Returns:
845 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
846 * aligned or are the same.
847 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
848 * due to insuffient page table memory.
849 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
850 * the caller.
851 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100852 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100853
854struct ffa_value api_vm_configure_pages(
855 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
856 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
857 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100858{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100859 struct ffa_value ret;
860 paddr_t pa_send_begin;
861 paddr_t pa_send_end;
862 paddr_t pa_recv_begin;
863 paddr_t pa_recv_end;
864 uint32_t orig_send_mode;
865 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200866 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100867
868 /* We only allow these to be setup once. */
869 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
870 ret = ffa_error(FFA_DENIED);
871 goto out;
872 }
873
874 /* Hafnium only supports a fixed size of RX/TX buffers. */
875 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
876 ret = ffa_error(FFA_INVALID_PARAMETERS);
877 goto out;
878 }
879
880 /* Fail if addresses are not page-aligned. */
881 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
882 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
883 ret = ffa_error(FFA_INVALID_PARAMETERS);
884 goto out;
885 }
886
887 /* Convert to physical addresses. */
888 pa_send_begin = pa_from_ipa(send);
889 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
890 pa_recv_begin = pa_from_ipa(recv);
891 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
892
893 /* Fail if the same page is used for the send and receive pages. */
894 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
895 ret = ffa_error(FFA_INVALID_PARAMETERS);
896 goto out;
897 }
Andrew Sculle1322792019-07-01 17:46:10 +0100898
899 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100900 * Ensure the pages are valid, owned and exclusive to the VM and that
901 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100902 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100903 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
904 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
905 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
906 (orig_send_mode & MM_MODE_R) == 0 ||
907 (orig_send_mode & MM_MODE_W) == 0) {
908 ret = ffa_error(FFA_DENIED);
909 goto out;
910 }
911
912 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
913 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
914 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
915 (orig_recv_mode & MM_MODE_R) == 0) {
916 ret = ffa_error(FFA_DENIED);
917 goto out;
918 }
Andrew Sculle1322792019-07-01 17:46:10 +0100919
920 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000921 if (!vm_identity_map(
922 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100923 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100924 local_page_pool, NULL)) {
925 ret = ffa_error(FFA_NO_MEMORY);
926 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100927 }
928
Andrew Scull3c257452019-11-26 13:32:50 +0000929 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
930 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100931 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100932 /* TODO: partial defrag of failed range. */
933 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100934 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100935 goto fail_undo_send;
936 }
937
Olivier Deprez96a2a262020-06-11 17:21:38 +0200938 /* Get extra send/recv pages mapping attributes for the given VM ID. */
939 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
940
Manish Pandeyd34f8892020-06-19 17:41:07 +0100941 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
942 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200943 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100944 goto fail_undo_send_and_recv;
945 }
946
Manish Pandeyd34f8892020-06-19 17:41:07 +0100947 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100948 goto out;
949
Andrew Sculle1322792019-07-01 17:46:10 +0100950fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000951 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100952 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100953
954fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000955 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100956 orig_send_mode, local_page_pool, NULL));
957 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +0100958
959out:
Andrew Sculle1322792019-07-01 17:46:10 +0100960 return ret;
961}
962
963/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100964 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +0100965 * must not be shared. Locking of the page tables combined with a local memory
966 * pool ensures there will always be enough memory to recover from any errors
967 * that arise. The stage-1 page tables must be locked so memory cannot be taken
968 * by another core which could result in this transaction being unable to roll
969 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000970 *
971 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100972 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000973 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000975 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100976 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000977 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100978 * - FFA_SUCCESS on success if no further action is needed.
979 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000980 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100981 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
983 uint32_t page_count, struct vcpu *current,
984 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100985{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100986 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100987 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100988 struct vm_locked vm_locked;
989 struct mm_stage1_locked mm_stage1_locked;
990 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +0000991
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100992 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100993 * Create a local pool so any freed memory can't be used by another
994 * thread. This is to ensure the original mapping can be restored if any
995 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100996 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100997 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
998
Andrew Sculle1322792019-07-01 17:46:10 +0100999 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001000 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001001
Manish Pandeyd34f8892020-06-19 17:41:07 +01001002 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1003 page_count, &local_page_pool);
1004 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001005 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001006 }
1007
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001008 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001009 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001010
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001011exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001012 mpool_fini(&local_page_pool);
1013
1014 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001015 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001016
1017 return ret;
1018}
1019
1020/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001021 * Notifies the `to` VM about the message currently in its mailbox, possibly
1022 * with the help of the primary VM.
1023 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1025 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001026{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1028 struct ffa_value primary_ret = {
1029 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001030 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001031 };
1032
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001033 /* Messages for the primary VM are delivered directly. */
1034 if (to.vm->id == HF_PRIMARY_VM_ID) {
1035 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001036 * Only tell the primary VM the size and other details if the
1037 * message is for it, to avoid leaking data about messages for
1038 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001039 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001041
1042 to.vm->mailbox.state = MAILBOX_STATE_READ;
1043 *next = api_switch_to_primary(current, primary_ret,
1044 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001045 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001046 }
1047
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001048 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1049
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001050 /* Messages for the TEE are sent on via the dispatcher. */
1051 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001053
Olivier Deprez112d2b52020-09-30 07:39:23 +02001054 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001055 /*
1056 * After the call to the TEE completes it must have finished
1057 * reading its RX buffer, so it is ready for another message.
1058 */
1059 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001060 /*
1061 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001062 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001063 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001064 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001065 }
1066
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001067 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001068 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001069 *next = api_switch_to_primary(current, primary_ret,
1070 VCPU_STATE_READY);
1071 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001072
1073 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001074}
1075
1076/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001077 * Copies data from the sender's send buffer to the recipient's receive buffer
1078 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001079 *
1080 * If the recipient's receive buffer is busy, it can optionally register the
1081 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001082 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1084 ffa_vm_id_t receiver_vm_id, uint32_t size,
1085 uint32_t attributes, struct vcpu *current,
1086 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001087{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001088 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001089 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001090 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001091 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001092 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001093 struct vcpu_locked current_locked;
1094 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 bool notify =
1096 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001097
Andrew Walbran70bc8622019-10-07 14:15:58 +01001098 /* Ensure sender VM ID corresponds to the current VM. */
1099 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001100 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001101 }
1102
1103 /* Disallow reflexive requests as this suggests an error in the VM. */
1104 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001105 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001106 }
1107
1108 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001109 if (size > FFA_MSG_PAYLOAD_MAX) {
1110 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001111 }
1112
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001113 /*
1114 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1115 * invocation.
1116 */
1117 current_locked = vcpu_lock(current);
1118 is_direct_request_ongoing =
1119 is_ffa_direct_msg_request_ongoing(current_locked);
1120 vcpu_unlock(&current_locked);
1121
1122 if (is_direct_request_ongoing) {
1123 return ffa_error(FFA_DENIED);
1124 }
1125
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001126 /* Ensure the receiver VM exists. */
1127 to = vm_find(receiver_vm_id);
1128 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001130 }
1131
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001132 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001133 * Check that the sender has configured its send buffer. If the tx
1134 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1135 * be safely accessed after releasing the lock since the tx mailbox
1136 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001137 */
1138 sl_lock(&from->lock);
1139 from_msg = from->mailbox.send;
1140 sl_unlock(&from->lock);
1141
1142 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001143 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001144 }
1145
Andrew Walbran82d6d152019-12-24 15:02:06 +00001146 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001147
Andrew Walbran82d6d152019-12-24 15:02:06 +00001148 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001149 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001150 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001151 }
1152
Andrew Walbran82d6d152019-12-24 15:02:06 +00001153 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001155 to->mailbox.recv_size = size;
1156 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001157 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001158 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001159
1160out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001161 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001162
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001163 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001164}
1165
1166/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001167 * Checks whether the vCPU's attempt to block for a message has already been
1168 * interrupted or whether it is allowed to block.
1169 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001170bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001171{
1172 bool interrupted;
1173
1174 sl_lock(&current->lock);
1175
1176 /*
1177 * Don't block if there are enabled and pending interrupts, to match
1178 * behaviour of wait_for_interrupt.
1179 */
1180 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1181
1182 sl_unlock(&current->lock);
1183
1184 return interrupted;
1185}
1186
1187/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001188 * Receives a message from the mailbox. If one isn't available, this function
1189 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001190 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001191 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001192 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1194 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001195{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001196 bool is_direct_request_ongoing;
1197 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001198 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001200
Andrew Scullaa039b32018-10-04 15:02:26 +01001201 /*
1202 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001203 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001204 */
Andrew Scull19503262018-09-20 14:48:39 +01001205 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001206 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001207 }
1208
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001209 /*
1210 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1211 * invocation.
1212 */
1213 current_locked = vcpu_lock(current);
1214 is_direct_request_ongoing =
1215 is_ffa_direct_msg_request_ongoing(current_locked);
1216 vcpu_unlock(&current_locked);
1217
1218 if (is_direct_request_ongoing) {
1219 return ffa_error(FFA_DENIED);
1220 }
1221
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001222 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001223
Andrew Scullaa039b32018-10-04 15:02:26 +01001224 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001225 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1226 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001227 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001228 goto out;
1229 }
1230
1231 /* No pending message so fail if not allowed to block. */
1232 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001233 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001234 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001235 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001236
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001237 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001238 * From this point onward this call can only be interrupted or a message
1239 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001240 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001241 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001242 return_code = ffa_error(FFA_INTERRUPTED);
1243 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001244 goto out;
1245 }
1246
Fuad Tabbaed294af2019-12-20 10:43:01 +00001247 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001248 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001249 struct ffa_value run_return = {
1250 .func = FFA_MSG_WAIT_32,
1251 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001252 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001253
Andrew Walbranb4816552018-12-05 17:35:42 +00001254 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001255 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001256 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001257out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001258 sl_unlock(&vm->lock);
1259
Jose Marinho3e2442f2019-03-12 13:30:37 +00001260 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001261}
1262
1263/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001264 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1265 * by this function, the caller must have called api_mailbox_send before with
1266 * the notify argument set to true, and this call must have failed because the
1267 * mailbox was not available.
1268 *
1269 * It should be called repeatedly to retrieve a list of VMs.
1270 *
1271 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1272 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001273 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001274int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001275{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001276 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001277 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001278 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001279
1280 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001281 if (list_empty(&vm->mailbox.ready_list)) {
1282 ret = -1;
1283 goto exit;
1284 }
1285
1286 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1287 ready_links);
1288 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001289 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001290
1291exit:
1292 sl_unlock(&vm->lock);
1293 return ret;
1294}
1295
1296/**
1297 * Retrieves the next VM waiting to be notified that the mailbox of the
1298 * specified VM became writable. Only primary VMs are allowed to call this.
1299 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001300 * Returns -1 on failure or if there are no waiters; the VM id of the next
1301 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001302 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001303int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001304{
1305 struct vm *vm;
1306 struct vm_locked locked;
1307 struct wait_entry *entry;
1308 struct vm *waiting_vm;
1309
1310 /* Only primary VMs are allowed to call this function. */
1311 if (current->vm->id != HF_PRIMARY_VM_ID) {
1312 return -1;
1313 }
1314
Andrew Walbran42347a92019-05-09 13:59:03 +01001315 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001316 if (vm == NULL) {
1317 return -1;
1318 }
1319
Fuad Tabbaed294af2019-12-20 10:43:01 +00001320 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001321 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001322 entry = api_fetch_waiter(locked);
1323 vm_unlock(&locked);
1324
1325 if (entry == NULL) {
1326 return -1;
1327 }
1328
1329 /* Enqueue notification to waiting VM. */
1330 waiting_vm = entry->waiting_vm;
1331
1332 sl_lock(&waiting_vm->lock);
1333 if (list_empty(&entry->ready_links)) {
1334 list_append(&waiting_vm->mailbox.ready_list,
1335 &entry->ready_links);
1336 }
1337 sl_unlock(&waiting_vm->lock);
1338
1339 return waiting_vm->id;
1340}
1341
1342/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001343 * Releases the caller's mailbox so that a new message can be received. The
1344 * caller must have copied out all data they wish to preserve as new messages
1345 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001346 *
1347 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001348 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1349 * - FFA_SUCCESS on success if no further action is needed.
1350 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001351 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001352 * hf_mailbox_waiter_get.
1353 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001355{
1356 struct vm *vm = current->vm;
1357 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001358 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001359
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001360 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001361 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001362 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001363 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001364 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001365 break;
1366
Andrew Sculld6ee1102019-04-05 22:12:42 +01001367 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001368 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001369 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001370 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001371 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001372 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001373
1374 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001375}
Andrew Walbran318f5732018-11-20 16:23:42 +00001376
1377/**
1378 * Enables or disables a given interrupt ID for the calling vCPU.
1379 *
1380 * Returns 0 on success, or -1 if the intid is invalid.
1381 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001382int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001383{
1384 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001385 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001386
Andrew Walbran318f5732018-11-20 16:23:42 +00001387 if (intid >= HF_NUM_INTIDS) {
1388 return -1;
1389 }
1390
1391 sl_lock(&current->lock);
1392 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001393 /*
1394 * If it is pending and was not enabled before, increment the
1395 * count.
1396 */
1397 if (current->interrupts.interrupt_pending[intid_index] &
1398 ~current->interrupts.interrupt_enabled[intid_index] &
1399 intid_mask) {
1400 current->interrupts.enabled_and_pending_count++;
1401 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001402 current->interrupts.interrupt_enabled[intid_index] |=
1403 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001404 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001405 /*
1406 * If it is pending and was enabled before, decrement the count.
1407 */
1408 if (current->interrupts.interrupt_pending[intid_index] &
1409 current->interrupts.interrupt_enabled[intid_index] &
1410 intid_mask) {
1411 current->interrupts.enabled_and_pending_count--;
1412 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001413 current->interrupts.interrupt_enabled[intid_index] &=
1414 ~intid_mask;
1415 }
1416
1417 sl_unlock(&current->lock);
1418 return 0;
1419}
1420
1421/**
1422 * Returns the ID of the next pending interrupt for the calling vCPU, and
1423 * acknowledges it (i.e. marks it as no longer pending). Returns
1424 * HF_INVALID_INTID if there are no pending interrupts.
1425 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001426uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001427{
1428 uint8_t i;
1429 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001430
1431 /*
1432 * Find the first enabled and pending interrupt ID, return it, and
1433 * deactivate it.
1434 */
1435 sl_lock(&current->lock);
1436 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1437 uint32_t enabled_and_pending =
1438 current->interrupts.interrupt_enabled[i] &
1439 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001440
Andrew Walbran318f5732018-11-20 16:23:42 +00001441 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001442 uint8_t bit_index = ctz(enabled_and_pending);
1443 /*
1444 * Mark it as no longer pending and decrement the count.
1445 */
1446 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001447 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001448 current->interrupts.enabled_and_pending_count--;
1449 first_interrupt =
1450 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001451 break;
1452 }
1453 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001454
1455 sl_unlock(&current->lock);
1456 return first_interrupt;
1457}
1458
1459/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001460 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001461 * given VM and vCPU.
1462 */
1463static inline bool is_injection_allowed(uint32_t target_vm_id,
1464 struct vcpu *current)
1465{
1466 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001467
Andrew Walbran318f5732018-11-20 16:23:42 +00001468 /*
1469 * The primary VM is allowed to inject interrupts into any VM. Secondary
1470 * VMs are only allowed to inject interrupts into their own vCPUs.
1471 */
1472 return current_vm_id == HF_PRIMARY_VM_ID ||
1473 current_vm_id == target_vm_id;
1474}
1475
1476/**
1477 * Injects a virtual interrupt of the given ID into the given target vCPU.
1478 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1479 * when the vCPU is next run, which is up to the scheduler.
1480 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001481 * Returns:
1482 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1483 * ID is invalid, or the current VM is not allowed to inject interrupts to
1484 * the target VM.
1485 * - 0 on success if no further action is needed.
1486 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1487 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001488 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1490 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001491 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001492{
Andrew Walbran318f5732018-11-20 16:23:42 +00001493 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001494 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001495
1496 if (intid >= HF_NUM_INTIDS) {
1497 return -1;
1498 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001499
Andrew Walbran318f5732018-11-20 16:23:42 +00001500 if (target_vm == NULL) {
1501 return -1;
1502 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001503
Andrew Walbran318f5732018-11-20 16:23:42 +00001504 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001505 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001506 return -1;
1507 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001508
Andrew Walbran318f5732018-11-20 16:23:42 +00001509 if (!is_injection_allowed(target_vm_id, current)) {
1510 return -1;
1511 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001512
Andrew Walbrane1310df2019-04-29 17:28:28 +01001513 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001514
Andrew Walbran17eebf92020-02-05 16:35:49 +00001515 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1516 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1517 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001518 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001519}
Andrew Scull6386f252018-12-06 13:29:10 +00001520
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001521/** Returns the version of the implemented FF-A specification. */
1522struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001523{
1524 /*
1525 * Ensure that both major and minor revision representation occupies at
1526 * most 15 bits.
1527 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001528 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001529 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001530 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001531 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001532 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001533 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001535 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001536
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 return (struct ffa_value){
1538 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1539 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001540}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001541
1542int64_t api_debug_log(char c, struct vcpu *current)
1543{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001544 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001545 struct vm *vm = current->vm;
1546 struct vm_locked vm_locked = vm_lock(vm);
1547
Andrew Sculld54e1be2019-08-20 11:09:42 +01001548 if (c == '\n' || c == '\0') {
1549 flush = true;
1550 } else {
1551 vm->log_buffer[vm->log_buffer_length++] = c;
1552 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1553 }
1554
1555 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001556 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1557 vm->log_buffer_length);
1558 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001559 }
1560
1561 vm_unlock(&vm_locked);
1562
1563 return 0;
1564}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001565
1566/**
1567 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001568 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001569 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001570struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001571{
1572 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001573 case FFA_ERROR_32:
1574 case FFA_SUCCESS_32:
1575 case FFA_INTERRUPT_32:
1576 case FFA_VERSION_32:
1577 case FFA_FEATURES_32:
1578 case FFA_RX_RELEASE_32:
1579 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001580 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581 case FFA_ID_GET_32:
1582 case FFA_MSG_POLL_32:
1583 case FFA_MSG_WAIT_32:
1584 case FFA_YIELD_32:
1585 case FFA_RUN_32:
1586 case FFA_MSG_SEND_32:
1587 case FFA_MEM_DONATE_32:
1588 case FFA_MEM_LEND_32:
1589 case FFA_MEM_SHARE_32:
1590 case FFA_MEM_RETRIEVE_REQ_32:
1591 case FFA_MEM_RETRIEVE_RESP_32:
1592 case FFA_MEM_RELINQUISH_32:
1593 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001594 case FFA_MSG_SEND_DIRECT_RESP_32:
1595 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001597 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001599 }
1600}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001601
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001602/**
1603 * Get target VM vCPU for direct messaging request.
1604 * If VM is UP then return first vCPU.
1605 * If VM is MP then return vCPU whose index matches current CPU index.
1606 */
1607static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1608 struct vm *vm, struct vcpu *current)
1609{
1610 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1611 struct vcpu *vcpu = NULL;
1612
1613 if (vm->vcpu_count == 1) {
1614 vcpu = vm_get_vcpu(vm, 0);
1615 } else if (current_cpu_index < vm->vcpu_count) {
1616 vcpu = vm_get_vcpu(vm, current_cpu_index);
1617 }
1618
1619 return vcpu;
1620}
1621
1622/**
1623 * Send an FF-A direct message request.
1624 */
1625struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1626 ffa_vm_id_t receiver_vm_id,
1627 struct ffa_value args,
1628 struct vcpu *current,
1629 struct vcpu **next)
1630{
1631 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001632 struct vm *receiver_vm;
1633 struct vcpu *receiver_vcpu;
1634 struct two_vcpu_locked vcpus_locked;
1635
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001636 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1637 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001638 return ffa_error(FFA_NOT_SUPPORTED);
1639 }
1640
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001641 receiver_vm = vm_find(receiver_vm_id);
1642 if (receiver_vm == NULL) {
1643 return ffa_error(FFA_INVALID_PARAMETERS);
1644 }
1645
1646 /*
1647 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1648 * UP (migratable) or MP partitions with a number of vCPUs matching the
1649 * number of PEs in the system. It further states that MP partitions
1650 * accepting direct request messages cannot migrate.
1651 */
1652 receiver_vcpu =
1653 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1654 if (receiver_vcpu == NULL) {
1655 return ffa_error(FFA_INVALID_PARAMETERS);
1656 }
1657
1658 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1659
1660 /*
1661 * If destination vCPU is executing or already received an
1662 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1663 * busy. There is a brief period of time where the vCPU state has
1664 * changed but regs_available is still false thus consider this case as
1665 * the vCPU not yet ready to receive a direct message request.
1666 */
1667 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1668 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1669 !receiver_vcpu->regs_available) {
1670 ret = ffa_error(FFA_BUSY);
1671 goto out;
1672 }
1673
1674 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1675 memory_order_relaxed)) {
1676 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
1677 dlog_notice("Aborting VM %u vCPU %u\n",
1678 receiver_vcpu->vm->id,
1679 vcpu_index(receiver_vcpu));
1680 receiver_vcpu->state = VCPU_STATE_ABORTED;
1681 }
1682
1683 ret = ffa_error(FFA_ABORTED);
1684 goto out;
1685 }
1686
1687 switch (receiver_vcpu->state) {
1688 case VCPU_STATE_OFF:
1689 case VCPU_STATE_RUNNING:
1690 case VCPU_STATE_ABORTED:
1691 case VCPU_STATE_READY:
1692 case VCPU_STATE_BLOCKED_INTERRUPT:
1693 ret = ffa_error(FFA_BUSY);
1694 goto out;
1695 case VCPU_STATE_BLOCKED_MAILBOX:
1696 /*
1697 * Expect target vCPU to be blocked after having called
1698 * ffa_msg_wait or sent a direct message response.
1699 */
1700 break;
1701 }
1702
1703 /* Inject timer interrupt if any pending */
1704 if (arch_timer_pending(&receiver_vcpu->regs)) {
1705 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1706 HF_VIRTUAL_TIMER_INTID,
1707 current, NULL);
1708
1709 arch_timer_mask(&receiver_vcpu->regs);
1710 }
1711
1712 /* The receiver vCPU runs upon direct message invocation */
1713 receiver_vcpu->cpu = current->cpu;
1714 receiver_vcpu->state = VCPU_STATE_RUNNING;
1715 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001716 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001717
1718 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1719 .func = args.func,
1720 .arg1 = args.arg1,
1721 .arg2 = 0,
1722 .arg3 = args.arg3,
1723 .arg4 = args.arg4,
1724 .arg5 = args.arg5,
1725 .arg6 = args.arg6,
1726 .arg7 = args.arg7,
1727 });
1728
1729 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1730
1731 /* Switch to receiver vCPU targeted to by direct msg request */
1732 *next = receiver_vcpu;
1733
1734 /*
1735 * Since this flow will lead to a VM switch, the return value will not
1736 * be applied to current vCPU.
1737 */
1738
1739out:
1740 sl_unlock(&receiver_vcpu->lock);
1741 sl_unlock(&current->lock);
1742
1743 return ret;
1744}
1745
1746/**
1747 * Send an FF-A direct message response.
1748 */
1749struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1750 ffa_vm_id_t receiver_vm_id,
1751 struct ffa_value args,
1752 struct vcpu *current,
1753 struct vcpu **next)
1754{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001755 struct vcpu_locked current_locked;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001756
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001757 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1758 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001759 return ffa_error(FFA_NOT_SUPPORTED);
1760 }
1761
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001762 current_locked = vcpu_lock(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001763
1764 /*
1765 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1766 * defined originator.
1767 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001768 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001769 /*
1770 * Sending direct response but direct request origin vCPU is
1771 * not set.
1772 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001773 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001774 return ffa_error(FFA_DENIED);
1775 }
1776
1777 if (current->direct_request_origin_vm_id != receiver_vm_id) {
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001778 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001779 return ffa_error(FFA_DENIED);
1780 }
1781
1782 /* Clear direct request origin for the caller. */
1783 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1784
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001785 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001786
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001787 if (!vm_id_is_current_world(receiver_vm_id)) {
1788 *next = api_switch_to_other_world(current,
1789 (struct ffa_value){
1790 .func = args.func,
1791 .arg1 = args.arg1,
1792 .arg2 = 0,
1793 .arg3 = args.arg3,
1794 .arg4 = args.arg4,
1795 .arg5 = args.arg5,
1796 .arg6 = args.arg6,
1797 .arg7 = args.arg7,
1798 },
1799 VCPU_STATE_BLOCKED_MAILBOX);
1800 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
1801 *next = api_switch_to_primary(current,
1802 (struct ffa_value){
1803 .func = args.func,
1804 .arg1 = args.arg1,
1805 .arg2 = 0,
1806 .arg3 = args.arg3,
1807 .arg4 = args.arg4,
1808 .arg5 = args.arg5,
1809 .arg6 = args.arg6,
1810 .arg7 = args.arg7,
1811 },
1812 VCPU_STATE_BLOCKED_MAILBOX);
1813 } else {
1814 panic("Invalid direct message response invocation");
1815 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001816
1817 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1818}
1819
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001820struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1821 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001822 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001823{
1824 struct vm *from = current->vm;
1825 struct vm *to;
1826 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001827 struct ffa_memory_region *memory_region;
1828 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001829
1830 if (ipa_addr(address) != 0 || page_count != 0) {
1831 /*
1832 * Hafnium only supports passing the descriptor in the TX
1833 * mailbox.
1834 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001835 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001836 }
1837
Andrew Walbranca808b12020-05-15 17:22:28 +01001838 if (fragment_length > length) {
1839 dlog_verbose(
1840 "Fragment length %d greater than total length %d.\n",
1841 fragment_length, length);
1842 return ffa_error(FFA_INVALID_PARAMETERS);
1843 }
1844 if (fragment_length < sizeof(struct ffa_memory_region) +
1845 sizeof(struct ffa_memory_access)) {
1846 dlog_verbose(
1847 "Initial fragment length %d smaller than header size "
1848 "%d.\n",
1849 fragment_length,
1850 sizeof(struct ffa_memory_region) +
1851 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001853 }
1854
1855 /*
1856 * Check that the sender has configured its send buffer. If the TX
1857 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1858 * be safely accessed after releasing the lock since the TX mailbox
1859 * address can only be configured once.
1860 */
1861 sl_lock(&from->lock);
1862 from_msg = from->mailbox.send;
1863 sl_unlock(&from->lock);
1864
1865 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001866 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001867 }
1868
1869 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001870 * Copy the memory region descriptor to a fresh page from the memory
1871 * pool. This prevents the sender from changing it underneath us, and
1872 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001873 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001874 if (fragment_length > HF_MAILBOX_SIZE ||
1875 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001876 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001877 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001878 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001879 if (memory_region == NULL) {
1880 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001881 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001882 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001883 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001884
1885 /* The sender must match the caller. */
1886 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001887 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001888 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001889 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001890 }
1891
Andrew Walbrana65a1322020-04-06 19:32:32 +01001892 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001893 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001894 dlog_verbose(
1895 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001896 "endpoint memory access descriptors, expected 1).\n",
1897 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001898 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001899 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001900 }
1901
1902 /*
1903 * Ensure that the receiver VM exists and isn't the same as the sender.
1904 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001905 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001906 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001907 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001908 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001909 goto out;
1910 }
1911
1912 if (to->id == HF_TEE_VM_ID) {
1913 /*
1914 * The 'to' VM lock is only needed in the case that it is the
1915 * TEE VM.
1916 */
1917 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1918
1919 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001920 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001921 goto out_unlock;
1922 }
1923
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001924 ret = ffa_memory_tee_send(
1925 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1926 length, fragment_length, share_func, &api_page_pool);
1927 /*
1928 * ffa_tee_memory_send takes ownership of the memory_region, so
1929 * make sure we don't free it.
1930 */
1931 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001932
1933 out_unlock:
1934 vm_unlock(&vm_to_from_lock.vm1);
1935 vm_unlock(&vm_to_from_lock.vm2);
1936 } else {
1937 struct vm_locked from_locked = vm_lock(from);
1938
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001939 ret = ffa_memory_send(from_locked, memory_region, length,
1940 fragment_length, share_func,
1941 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001942 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001943 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001944 * make sure we don't free it.
1945 */
1946 memory_region = NULL;
1947
1948 vm_unlock(&from_locked);
1949 }
1950
1951out:
1952 if (memory_region != NULL) {
1953 mpool_free(&api_page_pool, memory_region);
1954 }
1955
1956 return ret;
1957}
1958
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1960 uint32_t fragment_length,
1961 ipaddr_t address, uint32_t page_count,
1962 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001963{
1964 struct vm *to = current->vm;
1965 struct vm_locked to_locked;
1966 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001967 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001968 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001969 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001970
1971 if (ipa_addr(address) != 0 || page_count != 0) {
1972 /*
1973 * Hafnium only supports passing the descriptor in the TX
1974 * mailbox.
1975 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001976 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001977 }
1978
Andrew Walbrana65a1322020-04-06 19:32:32 +01001979 if (fragment_length != length) {
1980 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001981 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001982 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001983
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001984 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001985 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001986 message_buffer_size = cpu_get_buffer_size(current->cpu);
1987 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1988 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001990 }
1991
1992 to_locked = vm_lock(to);
1993 to_msg = to->mailbox.send;
1994
1995 if (to_msg == NULL) {
1996 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001997 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001998 goto out;
1999 }
2000
2001 /*
2002 * Copy the retrieve request descriptor to an internal buffer, so that
2003 * the caller can't change it underneath us.
2004 */
2005 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2006
2007 if (msg_receiver_busy(to_locked, NULL, false)) {
2008 /*
2009 * Can't retrieve memory information if the mailbox is not
2010 * available.
2011 */
2012 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002013 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002014 goto out;
2015 }
2016
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002017 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2018 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002019
2020out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002021 vm_unlock(&to_locked);
2022 return ret;
2023}
2024
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002025struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002026{
2027 struct vm *from = current->vm;
2028 struct vm_locked from_locked;
2029 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002030 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002031 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002032 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002033 uint32_t length;
2034
2035 from_locked = vm_lock(from);
2036 from_msg = from->mailbox.send;
2037
2038 if (from_msg == NULL) {
2039 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002040 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002041 goto out;
2042 }
2043
2044 /*
2045 * Calculate length from relinquish descriptor before copying. We will
2046 * check again later to make sure it hasn't changed.
2047 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002048 length = sizeof(struct ffa_mem_relinquish) +
2049 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2050 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002051 /*
2052 * Copy the relinquish descriptor to an internal buffer, so that the
2053 * caller can't change it underneath us.
2054 */
2055 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002056 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002057 message_buffer_size = cpu_get_buffer_size(current->cpu);
2058 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2059 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002060 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002061 goto out;
2062 }
2063 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2064
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002065 if (sizeof(struct ffa_mem_relinquish) +
2066 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002067 length) {
2068 dlog_verbose(
2069 "Endpoint count changed while copying to internal "
2070 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002071 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002072 goto out;
2073 }
2074
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002075 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2076 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002077
2078out:
2079 vm_unlock(&from_locked);
2080 return ret;
2081}
2082
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2084 ffa_memory_region_flags_t flags,
2085 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002086{
2087 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002088 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002090 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2091 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002092 struct vm_locked to_locked = vm_lock(to);
2093
Andrew Walbranca808b12020-05-15 17:22:28 +01002094 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002095 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002096
Andrew Walbran290b0c92020-02-03 16:37:14 +00002097 vm_unlock(&to_locked);
2098 } else {
2099 struct vm *from = vm_find(HF_TEE_VM_ID);
2100 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2101
Andrew Walbranca808b12020-05-15 17:22:28 +01002102 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2103 vm_to_from_lock.vm2, handle, flags,
2104 &api_page_pool);
2105
2106 vm_unlock(&vm_to_from_lock.vm1);
2107 vm_unlock(&vm_to_from_lock.vm2);
2108 }
2109
2110 return ret;
2111}
2112
2113struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2114 uint32_t fragment_offset,
2115 ffa_vm_id_t sender_vm_id,
2116 struct vcpu *current)
2117{
2118 struct vm *to = current->vm;
2119 struct vm_locked to_locked;
2120 struct ffa_value ret;
2121
2122 /* Sender ID MBZ at virtual instance. */
2123 if (sender_vm_id != 0) {
2124 return ffa_error(FFA_INVALID_PARAMETERS);
2125 }
2126
2127 to_locked = vm_lock(to);
2128
2129 if (msg_receiver_busy(to_locked, NULL, false)) {
2130 /*
2131 * Can't retrieve memory information if the mailbox is not
2132 * available.
2133 */
2134 dlog_verbose("RX buffer not ready.\n");
2135 ret = ffa_error(FFA_BUSY);
2136 goto out;
2137 }
2138
2139 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2140 &api_page_pool);
2141
2142out:
2143 vm_unlock(&to_locked);
2144 return ret;
2145}
2146
2147struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2148 uint32_t fragment_length,
2149 ffa_vm_id_t sender_vm_id,
2150 struct vcpu *current)
2151{
2152 struct vm *from = current->vm;
2153 const void *from_msg;
2154 void *fragment_copy;
2155 struct ffa_value ret;
2156
2157 /* Sender ID MBZ at virtual instance. */
2158 if (sender_vm_id != 0) {
2159 return ffa_error(FFA_INVALID_PARAMETERS);
2160 }
2161
2162 /*
2163 * Check that the sender has configured its send buffer. If the TX
2164 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2165 * be safely accessed after releasing the lock since the TX mailbox
2166 * address can only be configured once.
2167 */
2168 sl_lock(&from->lock);
2169 from_msg = from->mailbox.send;
2170 sl_unlock(&from->lock);
2171
2172 if (from_msg == NULL) {
2173 return ffa_error(FFA_INVALID_PARAMETERS);
2174 }
2175
2176 /*
2177 * Copy the fragment to a fresh page from the memory pool. This prevents
2178 * the sender from changing it underneath us, and also lets us keep it
2179 * around in the share state table if needed.
2180 */
2181 if (fragment_length > HF_MAILBOX_SIZE ||
2182 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2183 dlog_verbose(
2184 "Fragment length %d larger than mailbox size %d.\n",
2185 fragment_length, HF_MAILBOX_SIZE);
2186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
2188 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2189 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2190 0) {
2191 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2192 return ffa_error(FFA_INVALID_PARAMETERS);
2193 }
2194 fragment_copy = mpool_alloc(&api_page_pool);
2195 if (fragment_copy == NULL) {
2196 dlog_verbose("Failed to allocate fragment copy.\n");
2197 return ffa_error(FFA_NO_MEMORY);
2198 }
2199 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2200
2201 /*
2202 * Hafnium doesn't support fragmentation of memory retrieve requests
2203 * (because it doesn't support caller-specified mappings, so a request
2204 * will never be larger than a single page), so this must be part of a
2205 * memory send (i.e. donate, lend or share) request.
2206 *
2207 * We can tell from the handle whether the memory transaction is for the
2208 * TEE or not.
2209 */
2210 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2211 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2212 struct vm_locked from_locked = vm_lock(from);
2213
2214 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2215 fragment_length, handle,
2216 &api_page_pool);
2217 /*
2218 * `ffa_memory_send_continue` takes ownership of the
2219 * fragment_copy, so we don't need to free it here.
2220 */
2221 vm_unlock(&from_locked);
2222 } else {
2223 struct vm *to = vm_find(HF_TEE_VM_ID);
2224 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2225
2226 /*
2227 * The TEE RX buffer state is checked in
2228 * `ffa_memory_tee_send_continue` rather than here, as we need
2229 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2230 * than FFA_ERROR FFA_BUSY in case it is busy.
2231 */
2232
2233 ret = ffa_memory_tee_send_continue(
2234 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2235 fragment_length, handle, &api_page_pool);
2236 /*
2237 * `ffa_memory_tee_send_continue` takes ownership of the
2238 * fragment_copy, so we don't need to free it here.
2239 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002240
2241 vm_unlock(&vm_to_from_lock.vm1);
2242 vm_unlock(&vm_to_from_lock.vm2);
2243 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002244
2245 return ret;
2246}