blob: 616b26c39c4121ea1b18be0d11a29a08a3268ab2 [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"
Andrew Walbran2619e0a2020-01-10 16:37:50 +000012#include "hf/arch/tee.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000013#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000014
Andrew Scull877ae4b2019-07-02 12:52:33 +010015#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000016#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "hf/ffa_internal.h"
18#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000019#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010020#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000021#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010022#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/vm.h"
25
Andrew Scullf35a5c92018-08-07 18:09:46 +010026#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010027#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Fuad Tabbae4efcc32020-07-16 15:37:27 +010029static_assert(sizeof(struct ffa_partition_info) == 8,
30 "Partition information descriptor size doesn't match the one in "
31 "the FF-A 1.0 EAC specification, Table 82.");
32
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000033/*
34 * To eliminate the risk of deadlocks, we define a partial order for the
35 * acquisition of locks held concurrently by the same physical CPU. Our current
36 * ordering requirements are as follows:
37 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010038 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000039 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010040 * Locks of the same kind require the lock of lowest address to be locked first,
41 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000042 */
43
Andrew Scullaa039b32018-10-04 15:02:26 +010044static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010045 "Currently, a page is mapped for the send and receive buffers so "
46 "the maximum request is the size of a page.");
47
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000048static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
49 "The page pool entry size must be at least as big as the mailbox "
50 "size, so that memory region descriptors can be copied from the "
51 "mailbox for memory sharing.");
52
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000053static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000054
55/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000056 * Initialises the API page pool by taking ownership of the contents of the
57 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000058 */
59void api_init(struct mpool *ppool)
60{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000061 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062}
63
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010064/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000065 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010066 *
67 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010068 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010070static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010071 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000072 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010073{
Andrew Walbran42347a92019-05-09 13:59:03 +010074 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010075 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010076
Andrew Walbran508e63c2018-12-20 17:02:37 +000077 /*
78 * If the secondary is blocked but has a timer running, sleep until the
79 * timer fires rather than indefinitely.
80 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010081 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
83 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010084 if (arch_timer_enabled_current()) {
85 uint64_t remaining_ns =
86 arch_timer_remaining_ns_current();
87
88 if (remaining_ns == 0) {
89 /*
90 * Timer is pending, so the current vCPU should
91 * be run again right away.
92 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010094 /*
95 * primary_ret.arg1 should already be set to the
96 * current VM ID and vCPU ID.
97 */
98 primary_ret.arg2 = 0;
99 } else {
100 primary_ret.arg2 = remaining_ns;
101 }
102 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100103 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100104 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000105 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100106 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000107
108 default:
109 /* Do nothing. */
110 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000111 }
112
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100113 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100114 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100115
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000116 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000117 sl_lock(&current->lock);
118 current->state = secondary_state;
119 sl_unlock(&current->lock);
120
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100121 return next;
122}
123
124/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100125 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
126 * registers the `from` VM to be notified when it becomes available.
127 */
128static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
129{
130 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
131 to.vm->mailbox.recv == NULL) {
132 /*
133 * Fail if the receiver isn't currently ready to receive data,
134 * setting up for notification if requested.
135 */
136 if (notify) {
137 struct wait_entry *entry =
138 vm_get_wait_entry(from, to.vm->id);
139
140 /* Append waiter only if it's not there yet. */
141 if (list_empty(&entry->wait_links)) {
142 list_append(&to.vm->mailbox.waiter_list,
143 &entry->wait_links);
144 }
145 }
146
147 return true;
148 }
149
150 return false;
151}
152
153/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000154 * Returns true if the given vCPU is executing in context of an
155 * FFA_MSG_SEND_DIRECT_REQ invocation.
156 */
157static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
158{
159 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
160}
161
162/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000163 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000164 */
165struct vcpu *api_preempt(struct vcpu *current)
166{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100167 struct ffa_value ret = {
168 .func = FFA_INTERRUPT_32,
169 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000170 };
171
Andrew Sculld6ee1102019-04-05 22:12:42 +0100172 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000173}
174
175/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000176 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000177 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100178 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100179struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100180{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100181 struct ffa_value ret = {
182 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
183 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100184 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000185
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000186 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100187 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100188}
189
190/**
Andrew Walbran33645652019-04-15 12:29:31 +0100191 * Puts the current vCPU in off mode, and returns to the primary VM.
192 */
193struct vcpu *api_vcpu_off(struct vcpu *current)
194{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100195 struct ffa_value ret = {
196 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
197 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100198 };
199
200 /*
201 * Disable the timer, so the scheduler doesn't get told to call back
202 * based on it.
203 */
204 arch_timer_disable_current();
205
206 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
207}
208
209/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000210 * Returns to the primary VM to allow this CPU to be used for other tasks as the
211 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100212 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000213 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000214struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000215{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000216 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
217 struct vcpu_locked current_locked;
218 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000219
220 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000221 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000222 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000223 }
224
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000225 current_locked = vcpu_lock(current);
226 is_direct_request_ongoing =
227 is_ffa_direct_msg_request_ongoing(current_locked);
228 vcpu_unlock(&current_locked);
229
230 if (is_direct_request_ongoing) {
231 return ffa_error(FFA_DENIED);
232 }
233
234 *next = api_switch_to_primary(
235 current,
236 (struct ffa_value){.func = FFA_YIELD_32,
237 .arg1 = ffa_vm_vcpu(current->vm->id,
238 vcpu_index(current))},
239 VCPU_STATE_READY);
240
241 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000242}
243
244/**
Andrew Walbran33645652019-04-15 12:29:31 +0100245 * Switches to the primary so that it can switch to the target, or kick it if it
246 * is already running on a different physical CPU.
247 */
248struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
249{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100250 struct ffa_value ret = {
251 .func = HF_FFA_RUN_WAKE_UP,
252 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
253 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100254 };
255 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
256}
257
258/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000259 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000260 */
261struct vcpu *api_abort(struct vcpu *current)
262{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100263 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000264
Andrew Walbran17eebf92020-02-05 16:35:49 +0000265 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
266 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000267
268 if (current->vm->id == HF_PRIMARY_VM_ID) {
269 /* TODO: what to do when the primary aborts? */
270 for (;;) {
271 /* Do nothing. */
272 }
273 }
274
275 atomic_store_explicit(&current->vm->aborting, true,
276 memory_order_relaxed);
277
278 /* TODO: free resources once all vCPUs abort. */
279
Andrew Sculld6ee1102019-04-05 22:12:42 +0100280 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000281}
282
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100283struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
284 const struct ffa_uuid *uuid)
285{
286 struct vm *current_vm = current->vm;
287 struct vm_locked current_vm_locked;
288 ffa_vm_count_t vm_count = 0;
289 bool uuid_is_null = ffa_uuid_is_null(uuid);
290 struct ffa_value ret;
291 uint32_t size;
292 struct ffa_partition_info partitions[MAX_VMS];
293
294 /*
295 * Iterate through the VMs to find the ones with a matching UUID.
296 * A Null UUID retrieves information for all VMs.
297 */
298 for (uint16_t index = 0; index < vm_get_count(); ++index) {
299 const struct vm *vm = vm_find_index(index);
300
301 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
302 partitions[vm_count].vm_id = vm->id;
303 partitions[vm_count].vcpu_count = vm->vcpu_count;
304
305 /* Hafnium only supports indirect messaging. */
306 partitions[vm_count].properties =
307 FFA_PARTITION_INDIRECT_MSG;
308
309 ++vm_count;
310 }
311 }
312
313 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
314 if (vm_count == 0) {
315 return ffa_error(FFA_INVALID_PARAMETERS);
316 }
317
318 size = vm_count * sizeof(partitions[0]);
319 if (size > FFA_MSG_PAYLOAD_MAX) {
320 dlog_error(
321 "Partition information does not fit in the VM's RX "
322 "buffer.\n");
323 return ffa_error(FFA_NO_MEMORY);
324 }
325
326 /*
327 * Partition information is returned in the VM's RX buffer, which is why
328 * the lock is needed.
329 */
330 current_vm_locked = vm_lock(current_vm);
331
332 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
333 /*
334 * Can't retrieve memory information if the mailbox is not
335 * available.
336 */
337 dlog_verbose("RX buffer not ready.\n");
338 ret = ffa_error(FFA_BUSY);
339 goto out_unlock;
340 }
341
342 /* Populate the VM's RX buffer with the partition information. */
343 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
344 size);
345 current_vm->mailbox.recv_size = size;
346 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
347 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
348 current_vm->mailbox.state = MAILBOX_STATE_READ;
349
350 /* Return the count of partition information descriptors in w2. */
351 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
352
353out_unlock:
354 vm_unlock(&current_vm_locked);
355
356 return ret;
357}
358
Andrew Scull9726c252019-01-23 13:44:19 +0000359/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000360 * Returns the ID of the VM.
361 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000363{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 return (struct ffa_value){.func = FFA_SUCCESS_32,
365 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000366}
367
368/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000369 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000370 * function to indicate that register state for the given vCPU has been saved
371 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000372 */
373void api_regs_state_saved(struct vcpu *vcpu)
374{
375 sl_lock(&vcpu->lock);
376 vcpu->regs_available = true;
377 sl_unlock(&vcpu->lock);
378}
379
380/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000381 * Retrieves the next waiter and removes it from the wait list if the VM's
382 * mailbox is in a writable state.
383 */
384static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
385{
386 struct wait_entry *entry;
387 struct vm *vm = locked_vm.vm;
388
Andrew Sculld6ee1102019-04-05 22:12:42 +0100389 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000390 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
391 /* The mailbox is not writable or there are no waiters. */
392 return NULL;
393 }
394
395 /* Remove waiter from the wait list. */
396 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
397 wait_links);
398 list_remove(&entry->wait_links);
399 return entry;
400}
401
402/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000403 * Assuming that the arguments have already been checked by the caller, injects
404 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
405 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
406 * is next run, which is up to the scheduler.
407 *
408 * Returns:
409 * - 0 on success if no further action is needed.
410 * - 1 if it was called by the primary VM and the primary VM now needs to wake
411 * up or kick the target vCPU.
412 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200413static int64_t internal_interrupt_inject_locked(
414 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
415 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000416{
417 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100418 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000419 int64_t ret = 0;
420
Andrew Walbran508e63c2018-12-20 17:02:37 +0000421 /*
422 * We only need to change state and (maybe) trigger a virtual IRQ if it
423 * is enabled and was not previously pending. Otherwise we can skip
424 * everything except setting the pending bit.
425 *
426 * If you change this logic make sure to update the need_vm_lock logic
427 * above to match.
428 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200429 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
430 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000431 intid_mask)) {
432 goto out;
433 }
434
435 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200436 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000437
438 /*
439 * Only need to update state if there was not already an
440 * interrupt enabled and pending.
441 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200442 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000443 goto out;
444 }
445
Andrew Walbran508e63c2018-12-20 17:02:37 +0000446 if (current->vm->id == HF_PRIMARY_VM_ID) {
447 /*
448 * If the call came from the primary VM, let it know that it
449 * should run or kick the target vCPU.
450 */
451 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200452 } else if (current != target_locked.vcpu && next != NULL) {
453 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000454 }
455
456out:
457 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200458 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
459 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000460
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200461 return ret;
462}
463
464/* Wrapper to internal_interrupt_inject with locking of target vCPU */
465static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
466 uint32_t intid, struct vcpu *current,
467 struct vcpu **next)
468{
469 int64_t ret;
470 struct vcpu_locked target_locked;
471
472 target_locked = vcpu_lock(target_vcpu);
473 ret = internal_interrupt_inject_locked(target_locked, intid, current,
474 next);
475 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000476
477 return ret;
478}
479
480/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100481 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
482 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100483 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100484static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100485{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000486 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100487 case FFA_MSG_SEND_32:
488 return (struct ffa_value){
489 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000490 .arg1 = (receiver->mailbox.recv_sender << 16) |
491 receiver->id,
492 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000493 default:
494 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000495 dlog_error("Tried to return an invalid message function %#x\n",
496 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100497 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000498 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100499}
500
501/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000502 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000503 * value needs to be forced onto the vCPU.
504 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000505static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000507{
Andrew Scullb06d1752019-02-04 10:15:48 +0000508 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000509 bool ret;
510
Andrew Scullb06d1752019-02-04 10:15:48 +0000511 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000512 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000513 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100514 * The VM lock is not needed in the common case so it must only be taken
515 * when it is going to be needed. This ensures there are no inter-vCPU
516 * dependencies in the common run case meaning the sensitive context
517 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000518 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000519 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000520
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000521 /* The VM needs to be locked to deliver mailbox messages. */
522 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
523 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000524 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000525 sl_lock(&vcpu->vm->lock);
526 sl_lock(&vcpu->lock);
527 }
528
529 /*
530 * If the vCPU is already running somewhere then we can't run it here
531 * simultaneously. While it is actually running then the state should be
532 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
533 * stops running but while Hafnium is in the process of switching back
534 * to the primary there will be a brief period while the state has been
535 * updated but `regs_available` is still false (until
536 * `api_regs_state_saved` is called). We can't start running it again
537 * until this has finished, so count this state as still running for the
538 * purposes of this check.
539 */
540 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
541 /*
542 * vCPU is running on another pCPU.
543 *
544 * It's okay not to return the sleep duration here because the
545 * other physical CPU that is currently running this vCPU will
546 * return the sleep duration if needed.
547 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100548 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000549 ret = false;
550 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000551 }
Andrew Scull9726c252019-01-23 13:44:19 +0000552
553 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100554 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000555 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
556 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100557 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000558 }
559 ret = false;
560 goto out;
561 }
562
Andrew Walbran508e63c2018-12-20 17:02:37 +0000563 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100564 case VCPU_STATE_RUNNING:
565 case VCPU_STATE_OFF:
566 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000567 ret = false;
568 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000569
Andrew Sculld6ee1102019-04-05 22:12:42 +0100570 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000571 /*
572 * A pending message allows the vCPU to run so the message can
573 * be delivered directly.
574 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100575 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100576 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100578 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000579 break;
580 }
581 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100582 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000583 /* Allow virtual interrupts to be delivered. */
584 if (vcpu->interrupts.enabled_and_pending_count > 0) {
585 break;
586 }
587
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100588 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
589
Andrew Walbran508e63c2018-12-20 17:02:37 +0000590 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100591 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000592 arch_timer_remaining_ns(&vcpu->regs);
593
594 /*
595 * The timer expired so allow the interrupt to be
596 * delivered.
597 */
598 if (timer_remaining_ns == 0) {
599 break;
600 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100601 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000602
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100603 /*
604 * The vCPU is not ready to run, return the appropriate code to
605 * the primary which called vcpu_run.
606 */
607 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100608 ? FFA_MSG_WAIT_32
609 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100610 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
611 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000612
613 ret = false;
614 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000615
Andrew Sculld6ee1102019-04-05 22:12:42 +0100616 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000617 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000618 }
619
Andrew Scullb06d1752019-02-04 10:15:48 +0000620 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000621 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100622 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000623
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000624 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000625 * Mark the registers as unavailable now that we're about to reflect
626 * them onto the real registers. This will also prevent another physical
627 * CPU from trying to read these registers.
628 */
629 vcpu->regs_available = false;
630
631 ret = true;
632
633out:
634 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000635 if (need_vm_lock) {
636 sl_unlock(&vcpu->vm->lock);
637 }
638
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000639 return ret;
640}
641
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100642struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
643 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100644{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100645 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100646 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100647 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100648
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000649 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100650 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100652 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100653 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100654
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000655 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100656 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100657 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100658 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100659
Andrew Scull19503262018-09-20 14:48:39 +0100660 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100661 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100662 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100663 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100664 }
665
Fuad Tabbaed294af2019-12-20 10:43:01 +0000666 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100667 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100668 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100669 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100670
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000671 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100672 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000673 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000674 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100675 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000676
Andrew Walbran508e63c2018-12-20 17:02:37 +0000677 /*
678 * Inject timer interrupt if timer has expired. It's safe to access
679 * vcpu->regs here because api_vcpu_prepare_run already made sure that
680 * regs_available was true (and then set it to false) before returning
681 * true.
682 */
683 if (arch_timer_pending(&vcpu->regs)) {
684 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100685 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
686 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000687
688 /*
689 * Set the mask bit so the hardware interrupt doesn't fire
690 * again. Ideally we wouldn't do this because it affects what
691 * the secondary vCPU sees, but if we don't then we end up with
692 * a loop of the interrupt firing each time we try to return to
693 * the secondary vCPU.
694 */
695 arch_timer_mask(&vcpu->regs);
696 }
697
Fuad Tabbaed294af2019-12-20 10:43:01 +0000698 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000699 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000700
Andrew Scull33fecd32019-01-08 14:48:27 +0000701 /*
702 * Set a placeholder return code to the scheduler. This will be
703 * overwritten when the switch back to the primary occurs.
704 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100705 ret.func = FFA_INTERRUPT_32;
706 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100707 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000708
Andrew Scull6d2db332018-10-10 15:28:17 +0100709out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100710 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100711}
712
713/**
Andrew Scull81e85092018-12-12 12:56:20 +0000714 * Check that the mode indicates memory that is valid, owned and exclusive.
715 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100716static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000717{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100718 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
719 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000720}
721
722/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100723 * Determines the value to be returned by api_ffa_rxtx_map and
724 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
725 * there are waiters, it also switches back to the primary VM for it to wake
726 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000727 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100728static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
729 struct vcpu *current,
730 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000731{
732 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000733
734 if (list_empty(&vm->mailbox.waiter_list)) {
735 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100736 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000737 }
738
739 if (vm->id == HF_PRIMARY_VM_ID) {
740 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000742 }
743
744 /*
745 * Switch back to the primary VM, informing it that there are waiters
746 * that need to be notified.
747 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000748 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000750 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000751
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100752 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000753}
754
755/**
Andrew Sculle1322792019-07-01 17:46:10 +0100756 * Configures the hypervisor's stage-1 view of the send and receive pages. The
757 * stage-1 page tables must be locked so memory cannot be taken by another core
758 * which could result in this transaction being unable to roll back in the case
759 * of an error.
760 */
761static bool api_vm_configure_stage1(struct vm_locked vm_locked,
762 paddr_t pa_send_begin, paddr_t pa_send_end,
763 paddr_t pa_recv_begin, paddr_t pa_recv_end,
764 struct mpool *local_page_pool)
765{
766 bool ret;
767 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
768
769 /* Map the send page as read-only in the hypervisor address space. */
770 vm_locked.vm->mailbox.send =
771 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
772 MM_MODE_R, local_page_pool);
773 if (!vm_locked.vm->mailbox.send) {
774 /* TODO: partial defrag of failed range. */
775 /* Recover any memory consumed in failed mapping. */
776 mm_defrag(mm_stage1_locked, local_page_pool);
777 goto fail;
778 }
779
780 /*
781 * Map the receive page as writable in the hypervisor address space. On
782 * failure, unmap the send page before returning.
783 */
784 vm_locked.vm->mailbox.recv =
785 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
786 MM_MODE_W, local_page_pool);
787 if (!vm_locked.vm->mailbox.recv) {
788 /* TODO: partial defrag of failed range. */
789 /* Recover any memory consumed in failed mapping. */
790 mm_defrag(mm_stage1_locked, local_page_pool);
791 goto fail_undo_send;
792 }
793
794 ret = true;
795 goto out;
796
797 /*
798 * The following mappings will not require more memory than is available
799 * in the local pool.
800 */
801fail_undo_send:
802 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100803 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
804 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100805
806fail:
807 ret = false;
808
809out:
810 mm_unlock_stage1(&mm_stage1_locked);
811
812 return ret;
813}
814
815/**
816 * Configures the send and receive pages in the VM stage-2 and hypervisor
817 * stage-1 page tables. Locking of the page tables combined with a local memory
818 * pool ensures there will always be enough memory to recover from any errors
819 * that arise.
820 */
821static bool api_vm_configure_pages(struct vm_locked vm_locked,
822 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100823 uint32_t orig_send_mode,
824 paddr_t pa_recv_begin, paddr_t pa_recv_end,
825 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100826{
827 bool ret;
828 struct mpool local_page_pool;
829
830 /*
831 * Create a local pool so any freed memory can't be used by another
832 * thread. This is to ensure the original mapping can be restored if any
833 * stage of the process fails.
834 */
835 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
836
837 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000838 if (!vm_identity_map(
839 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100840 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000841 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100842 goto fail;
843 }
844
Andrew Scull3c257452019-11-26 13:32:50 +0000845 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
846 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
847 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100848 /* TODO: partial defrag of failed range. */
849 /* Recover any memory consumed in failed mapping. */
850 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
851 goto fail_undo_send;
852 }
853
854 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
855 pa_recv_begin, pa_recv_end,
856 &local_page_pool)) {
857 goto fail_undo_send_and_recv;
858 }
859
860 ret = true;
861 goto out;
862
863 /*
864 * The following mappings will not require more memory than is available
865 * in the local pool.
866 */
867fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000868 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
869 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100870
871fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000872 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
873 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100874
875fail:
876 ret = false;
877
878out:
879 mpool_fini(&local_page_pool);
880
881 return ret;
882}
883
884/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100885 * Configures the VM to send/receive data through the specified pages. The pages
886 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000887 *
888 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100889 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000890 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000892 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100893 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000894 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895 * - FFA_SUCCESS on success if no further action is needed.
896 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000897 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100898 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100899struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
900 uint32_t page_count, struct vcpu *current,
901 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100902{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100903 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100904 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100905 paddr_t pa_send_begin;
906 paddr_t pa_send_end;
907 paddr_t pa_recv_begin;
908 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100909 uint32_t orig_send_mode;
910 uint32_t orig_recv_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100911 struct ffa_value ret;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000912
913 /* Hafnium only supports a fixed size of RX/TX buffers. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
915 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000916 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100917
918 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000919 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
920 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100922 }
923
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000924 /* Convert to physical addresses. */
925 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000926 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000927
928 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000929 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000930
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100931 /* Fail if the same page is used for the send and receive pages. */
932 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100933 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000934 }
935
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100936 /*
937 * The hypervisor's memory map must be locked for the duration of this
938 * operation to ensure there will be sufficient memory to recover from
939 * any failures.
940 *
Fuad Tabba9dc276f2020-07-16 09:29:32 +0100941 * TODO: the scope can be reduced but will require restructuring to
942 * keep a single unlock point.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100943 */
Andrew Sculle1322792019-07-01 17:46:10 +0100944 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000945
946 /* We only allow these to be setup once. */
947 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000949 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000950 }
951
952 /*
953 * Ensure the pages are valid, owned and exclusive to the VM and that
954 * the VM has the required access to the memory.
955 */
956 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
957 &orig_send_mode) ||
958 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
959 (orig_send_mode & MM_MODE_R) == 0 ||
960 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000962 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000963 }
964
965 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
966 &orig_recv_mode) ||
967 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
968 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100969 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000970 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000971 }
972
Andrew Sculle1322792019-07-01 17:46:10 +0100973 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
974 orig_send_mode, pa_recv_begin, pa_recv_end,
975 orig_recv_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100976 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000977 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100978 }
979
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000980 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100981 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000982
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100983exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100984 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100985
986 return ret;
987}
988
989/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100990 * Notifies the `to` VM about the message currently in its mailbox, possibly
991 * with the help of the primary VM.
992 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100993static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
994 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100995{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
997 struct ffa_value primary_ret = {
998 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000999 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001000 };
1001
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001002 /* Messages for the primary VM are delivered directly. */
1003 if (to.vm->id == HF_PRIMARY_VM_ID) {
1004 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001005 * Only tell the primary VM the size and other details if the
1006 * message is for it, to avoid leaking data about messages for
1007 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001008 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001010
1011 to.vm->mailbox.state = MAILBOX_STATE_READ;
1012 *next = api_switch_to_primary(current, primary_ret,
1013 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001014 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001015 }
1016
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001017 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1018
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001019 /* Messages for the TEE are sent on via the dispatcher. */
1020 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001022
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001023 ret = arch_tee_call(call);
1024 /*
1025 * After the call to the TEE completes it must have finished
1026 * reading its RX buffer, so it is ready for another message.
1027 */
1028 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001029 /*
1030 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001032 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001033 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001034 }
1035
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001036 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001037 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001038 *next = api_switch_to_primary(current, primary_ret,
1039 VCPU_STATE_READY);
1040 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001041
1042 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001043}
1044
1045/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001046 * Copies data from the sender's send buffer to the recipient's receive buffer
1047 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001048 *
1049 * If the recipient's receive buffer is busy, it can optionally register the
1050 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001051 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1053 ffa_vm_id_t receiver_vm_id, uint32_t size,
1054 uint32_t attributes, struct vcpu *current,
1055 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001056{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001057 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001058 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001059 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001060 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001061 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001062 struct vcpu_locked current_locked;
1063 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001064 bool notify =
1065 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001066
Andrew Walbran70bc8622019-10-07 14:15:58 +01001067 /* Ensure sender VM ID corresponds to the current VM. */
1068 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001069 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001070 }
1071
1072 /* Disallow reflexive requests as this suggests an error in the VM. */
1073 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001074 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001075 }
1076
1077 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 if (size > FFA_MSG_PAYLOAD_MAX) {
1079 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001080 }
1081
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001082 /*
1083 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1084 * invocation.
1085 */
1086 current_locked = vcpu_lock(current);
1087 is_direct_request_ongoing =
1088 is_ffa_direct_msg_request_ongoing(current_locked);
1089 vcpu_unlock(&current_locked);
1090
1091 if (is_direct_request_ongoing) {
1092 return ffa_error(FFA_DENIED);
1093 }
1094
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001095 /* Ensure the receiver VM exists. */
1096 to = vm_find(receiver_vm_id);
1097 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001098 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001099 }
1100
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001101 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001102 * Check that the sender has configured its send buffer. If the tx
1103 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1104 * be safely accessed after releasing the lock since the tx mailbox
1105 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001106 */
1107 sl_lock(&from->lock);
1108 from_msg = from->mailbox.send;
1109 sl_unlock(&from->lock);
1110
1111 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001112 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001113 }
1114
Andrew Walbran82d6d152019-12-24 15:02:06 +00001115 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001116
Andrew Walbran82d6d152019-12-24 15:02:06 +00001117 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001118 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001119 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001120 }
1121
Andrew Walbran82d6d152019-12-24 15:02:06 +00001122 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001124 to->mailbox.recv_size = size;
1125 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001126 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001127 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001128
1129out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001130 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001131
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001132 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001133}
1134
1135/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001136 * Checks whether the vCPU's attempt to block for a message has already been
1137 * interrupted or whether it is allowed to block.
1138 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001140{
1141 bool interrupted;
1142
1143 sl_lock(&current->lock);
1144
1145 /*
1146 * Don't block if there are enabled and pending interrupts, to match
1147 * behaviour of wait_for_interrupt.
1148 */
1149 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1150
1151 sl_unlock(&current->lock);
1152
1153 return interrupted;
1154}
1155
1156/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001157 * Receives a message from the mailbox. If one isn't available, this function
1158 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001159 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001160 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001161 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1163 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001164{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001165 bool is_direct_request_ongoing;
1166 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001167 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001168 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001169
Andrew Scullaa039b32018-10-04 15:02:26 +01001170 /*
1171 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001172 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001173 */
Andrew Scull19503262018-09-20 14:48:39 +01001174 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001175 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001176 }
1177
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001178 /*
1179 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1180 * invocation.
1181 */
1182 current_locked = vcpu_lock(current);
1183 is_direct_request_ongoing =
1184 is_ffa_direct_msg_request_ongoing(current_locked);
1185 vcpu_unlock(&current_locked);
1186
1187 if (is_direct_request_ongoing) {
1188 return ffa_error(FFA_DENIED);
1189 }
1190
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001191 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001192
Andrew Scullaa039b32018-10-04 15:02:26 +01001193 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001194 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1195 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001196 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001197 goto out;
1198 }
1199
1200 /* No pending message so fail if not allowed to block. */
1201 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001202 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001203 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001204 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001205
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001206 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001207 * From this point onward this call can only be interrupted or a message
1208 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001210 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001211 return_code = ffa_error(FFA_INTERRUPTED);
1212 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001213 goto out;
1214 }
1215
Fuad Tabbaed294af2019-12-20 10:43:01 +00001216 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001217 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001218 struct ffa_value run_return = {
1219 .func = FFA_MSG_WAIT_32,
1220 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001221 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001222
Andrew Walbranb4816552018-12-05 17:35:42 +00001223 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001224 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001225 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001226out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001227 sl_unlock(&vm->lock);
1228
Jose Marinho3e2442f2019-03-12 13:30:37 +00001229 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001230}
1231
1232/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001233 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1234 * by this function, the caller must have called api_mailbox_send before with
1235 * the notify argument set to true, and this call must have failed because the
1236 * mailbox was not available.
1237 *
1238 * It should be called repeatedly to retrieve a list of VMs.
1239 *
1240 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1241 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001242 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001243int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001244{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001245 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001246 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001247 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001248
1249 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001250 if (list_empty(&vm->mailbox.ready_list)) {
1251 ret = -1;
1252 goto exit;
1253 }
1254
1255 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1256 ready_links);
1257 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001258 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001259
1260exit:
1261 sl_unlock(&vm->lock);
1262 return ret;
1263}
1264
1265/**
1266 * Retrieves the next VM waiting to be notified that the mailbox of the
1267 * specified VM became writable. Only primary VMs are allowed to call this.
1268 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001269 * Returns -1 on failure or if there are no waiters; the VM id of the next
1270 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001271 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001273{
1274 struct vm *vm;
1275 struct vm_locked locked;
1276 struct wait_entry *entry;
1277 struct vm *waiting_vm;
1278
1279 /* Only primary VMs are allowed to call this function. */
1280 if (current->vm->id != HF_PRIMARY_VM_ID) {
1281 return -1;
1282 }
1283
Andrew Walbran42347a92019-05-09 13:59:03 +01001284 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001285 if (vm == NULL) {
1286 return -1;
1287 }
1288
Fuad Tabbaed294af2019-12-20 10:43:01 +00001289 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001290 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001291 entry = api_fetch_waiter(locked);
1292 vm_unlock(&locked);
1293
1294 if (entry == NULL) {
1295 return -1;
1296 }
1297
1298 /* Enqueue notification to waiting VM. */
1299 waiting_vm = entry->waiting_vm;
1300
1301 sl_lock(&waiting_vm->lock);
1302 if (list_empty(&entry->ready_links)) {
1303 list_append(&waiting_vm->mailbox.ready_list,
1304 &entry->ready_links);
1305 }
1306 sl_unlock(&waiting_vm->lock);
1307
1308 return waiting_vm->id;
1309}
1310
1311/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001312 * Releases the caller's mailbox so that a new message can be received. The
1313 * caller must have copied out all data they wish to preserve as new messages
1314 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001315 *
1316 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001317 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1318 * - FFA_SUCCESS on success if no further action is needed.
1319 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001320 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001321 * hf_mailbox_waiter_get.
1322 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001323struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001324{
1325 struct vm *vm = current->vm;
1326 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001328
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001329 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001330 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001331 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001332 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001333 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001334 break;
1335
Andrew Sculld6ee1102019-04-05 22:12:42 +01001336 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001337 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001338 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001339 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001340 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001341 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001342
1343 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001344}
Andrew Walbran318f5732018-11-20 16:23:42 +00001345
1346/**
1347 * Enables or disables a given interrupt ID for the calling vCPU.
1348 *
1349 * Returns 0 on success, or -1 if the intid is invalid.
1350 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001351int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001352{
1353 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001354 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001355
Andrew Walbran318f5732018-11-20 16:23:42 +00001356 if (intid >= HF_NUM_INTIDS) {
1357 return -1;
1358 }
1359
1360 sl_lock(&current->lock);
1361 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001362 /*
1363 * If it is pending and was not enabled before, increment the
1364 * count.
1365 */
1366 if (current->interrupts.interrupt_pending[intid_index] &
1367 ~current->interrupts.interrupt_enabled[intid_index] &
1368 intid_mask) {
1369 current->interrupts.enabled_and_pending_count++;
1370 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001371 current->interrupts.interrupt_enabled[intid_index] |=
1372 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001373 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001374 /*
1375 * If it is pending and was enabled before, decrement the count.
1376 */
1377 if (current->interrupts.interrupt_pending[intid_index] &
1378 current->interrupts.interrupt_enabled[intid_index] &
1379 intid_mask) {
1380 current->interrupts.enabled_and_pending_count--;
1381 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001382 current->interrupts.interrupt_enabled[intid_index] &=
1383 ~intid_mask;
1384 }
1385
1386 sl_unlock(&current->lock);
1387 return 0;
1388}
1389
1390/**
1391 * Returns the ID of the next pending interrupt for the calling vCPU, and
1392 * acknowledges it (i.e. marks it as no longer pending). Returns
1393 * HF_INVALID_INTID if there are no pending interrupts.
1394 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001395uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001396{
1397 uint8_t i;
1398 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001399
1400 /*
1401 * Find the first enabled and pending interrupt ID, return it, and
1402 * deactivate it.
1403 */
1404 sl_lock(&current->lock);
1405 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1406 uint32_t enabled_and_pending =
1407 current->interrupts.interrupt_enabled[i] &
1408 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001409
Andrew Walbran318f5732018-11-20 16:23:42 +00001410 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001411 uint8_t bit_index = ctz(enabled_and_pending);
1412 /*
1413 * Mark it as no longer pending and decrement the count.
1414 */
1415 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001416 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001417 current->interrupts.enabled_and_pending_count--;
1418 first_interrupt =
1419 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001420 break;
1421 }
1422 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001423
1424 sl_unlock(&current->lock);
1425 return first_interrupt;
1426}
1427
1428/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001429 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001430 * given VM and vCPU.
1431 */
1432static inline bool is_injection_allowed(uint32_t target_vm_id,
1433 struct vcpu *current)
1434{
1435 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001436
Andrew Walbran318f5732018-11-20 16:23:42 +00001437 /*
1438 * The primary VM is allowed to inject interrupts into any VM. Secondary
1439 * VMs are only allowed to inject interrupts into their own vCPUs.
1440 */
1441 return current_vm_id == HF_PRIMARY_VM_ID ||
1442 current_vm_id == target_vm_id;
1443}
1444
1445/**
1446 * Injects a virtual interrupt of the given ID into the given target vCPU.
1447 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1448 * when the vCPU is next run, which is up to the scheduler.
1449 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001450 * Returns:
1451 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1452 * ID is invalid, or the current VM is not allowed to inject interrupts to
1453 * the target VM.
1454 * - 0 on success if no further action is needed.
1455 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1456 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001457 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1459 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001460 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001461{
Andrew Walbran318f5732018-11-20 16:23:42 +00001462 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001463 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001464
1465 if (intid >= HF_NUM_INTIDS) {
1466 return -1;
1467 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001468
Andrew Walbran318f5732018-11-20 16:23:42 +00001469 if (target_vm == NULL) {
1470 return -1;
1471 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001472
Andrew Walbran318f5732018-11-20 16:23:42 +00001473 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001474 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001475 return -1;
1476 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001477
Andrew Walbran318f5732018-11-20 16:23:42 +00001478 if (!is_injection_allowed(target_vm_id, current)) {
1479 return -1;
1480 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001481
Andrew Walbrane1310df2019-04-29 17:28:28 +01001482 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001483
Andrew Walbran17eebf92020-02-05 16:35:49 +00001484 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1485 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1486 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001487 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001488}
Andrew Scull6386f252018-12-06 13:29:10 +00001489
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001490/** Returns the version of the implemented FF-A specification. */
1491struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001492{
1493 /*
1494 * Ensure that both major and minor revision representation occupies at
1495 * most 15 bits.
1496 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001497 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001498 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001499 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001500 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001501 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001502 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001504 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001506 return (struct ffa_value){
1507 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1508 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001509}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001510
1511int64_t api_debug_log(char c, struct vcpu *current)
1512{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001513 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001514 struct vm *vm = current->vm;
1515 struct vm_locked vm_locked = vm_lock(vm);
1516
Andrew Sculld54e1be2019-08-20 11:09:42 +01001517 if (c == '\n' || c == '\0') {
1518 flush = true;
1519 } else {
1520 vm->log_buffer[vm->log_buffer_length++] = c;
1521 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1522 }
1523
1524 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001525 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1526 vm->log_buffer_length);
1527 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001528 }
1529
1530 vm_unlock(&vm_locked);
1531
1532 return 0;
1533}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001534
1535/**
1536 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001538 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001540{
1541 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542 case FFA_ERROR_32:
1543 case FFA_SUCCESS_32:
1544 case FFA_INTERRUPT_32:
1545 case FFA_VERSION_32:
1546 case FFA_FEATURES_32:
1547 case FFA_RX_RELEASE_32:
1548 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001549 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 case FFA_ID_GET_32:
1551 case FFA_MSG_POLL_32:
1552 case FFA_MSG_WAIT_32:
1553 case FFA_YIELD_32:
1554 case FFA_RUN_32:
1555 case FFA_MSG_SEND_32:
1556 case FFA_MEM_DONATE_32:
1557 case FFA_MEM_LEND_32:
1558 case FFA_MEM_SHARE_32:
1559 case FFA_MEM_RETRIEVE_REQ_32:
1560 case FFA_MEM_RETRIEVE_RESP_32:
1561 case FFA_MEM_RELINQUISH_32:
1562 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001563 case FFA_MSG_SEND_DIRECT_RESP_32:
1564 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001565 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001566 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001567 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001568 }
1569}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001570
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001571/**
1572 * Get target VM vCPU for direct messaging request.
1573 * If VM is UP then return first vCPU.
1574 * If VM is MP then return vCPU whose index matches current CPU index.
1575 */
1576static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1577 struct vm *vm, struct vcpu *current)
1578{
1579 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1580 struct vcpu *vcpu = NULL;
1581
1582 if (vm->vcpu_count == 1) {
1583 vcpu = vm_get_vcpu(vm, 0);
1584 } else if (current_cpu_index < vm->vcpu_count) {
1585 vcpu = vm_get_vcpu(vm, current_cpu_index);
1586 }
1587
1588 return vcpu;
1589}
1590
1591/**
1592 * Send an FF-A direct message request.
1593 */
1594struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1595 ffa_vm_id_t receiver_vm_id,
1596 struct ffa_value args,
1597 struct vcpu *current,
1598 struct vcpu **next)
1599{
1600 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
1601 ffa_vm_id_t current_vm_id = current->vm->id;
1602 struct vm *receiver_vm;
1603 struct vcpu *receiver_vcpu;
1604 struct two_vcpu_locked vcpus_locked;
1605
1606 /* Only allow primary VM to send direct message requests. */
1607 if (current_vm_id != HF_PRIMARY_VM_ID) {
1608 return ffa_error(FFA_NOT_SUPPORTED);
1609 }
1610
1611 /* Prevent sender_vm_id spoofing. */
1612 if (current_vm_id != sender_vm_id) {
1613 return ffa_error(FFA_INVALID_PARAMETERS);
1614 }
1615
1616 /* Prevent a VM from sending messages to itself. */
1617 if (current_vm_id == receiver_vm_id) {
1618 return ffa_error(FFA_INVALID_PARAMETERS);
1619 }
1620
1621 receiver_vm = vm_find(receiver_vm_id);
1622 if (receiver_vm == NULL) {
1623 return ffa_error(FFA_INVALID_PARAMETERS);
1624 }
1625
1626 /*
1627 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1628 * UP (migratable) or MP partitions with a number of vCPUs matching the
1629 * number of PEs in the system. It further states that MP partitions
1630 * accepting direct request messages cannot migrate.
1631 */
1632 receiver_vcpu =
1633 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1634 if (receiver_vcpu == NULL) {
1635 return ffa_error(FFA_INVALID_PARAMETERS);
1636 }
1637
1638 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1639
1640 /*
1641 * If destination vCPU is executing or already received an
1642 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1643 * busy. There is a brief period of time where the vCPU state has
1644 * changed but regs_available is still false thus consider this case as
1645 * the vCPU not yet ready to receive a direct message request.
1646 */
1647 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1648 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1649 !receiver_vcpu->regs_available) {
1650 ret = ffa_error(FFA_BUSY);
1651 goto out;
1652 }
1653
1654 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1655 memory_order_relaxed)) {
1656 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
1657 dlog_notice("Aborting VM %u vCPU %u\n",
1658 receiver_vcpu->vm->id,
1659 vcpu_index(receiver_vcpu));
1660 receiver_vcpu->state = VCPU_STATE_ABORTED;
1661 }
1662
1663 ret = ffa_error(FFA_ABORTED);
1664 goto out;
1665 }
1666
1667 switch (receiver_vcpu->state) {
1668 case VCPU_STATE_OFF:
1669 case VCPU_STATE_RUNNING:
1670 case VCPU_STATE_ABORTED:
1671 case VCPU_STATE_READY:
1672 case VCPU_STATE_BLOCKED_INTERRUPT:
1673 ret = ffa_error(FFA_BUSY);
1674 goto out;
1675 case VCPU_STATE_BLOCKED_MAILBOX:
1676 /*
1677 * Expect target vCPU to be blocked after having called
1678 * ffa_msg_wait or sent a direct message response.
1679 */
1680 break;
1681 }
1682
1683 /* Inject timer interrupt if any pending */
1684 if (arch_timer_pending(&receiver_vcpu->regs)) {
1685 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1686 HF_VIRTUAL_TIMER_INTID,
1687 current, NULL);
1688
1689 arch_timer_mask(&receiver_vcpu->regs);
1690 }
1691
1692 /* The receiver vCPU runs upon direct message invocation */
1693 receiver_vcpu->cpu = current->cpu;
1694 receiver_vcpu->state = VCPU_STATE_RUNNING;
1695 receiver_vcpu->regs_available = false;
1696 receiver_vcpu->direct_request_origin_vm_id = current_vm_id;
1697
1698 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1699 .func = args.func,
1700 .arg1 = args.arg1,
1701 .arg2 = 0,
1702 .arg3 = args.arg3,
1703 .arg4 = args.arg4,
1704 .arg5 = args.arg5,
1705 .arg6 = args.arg6,
1706 .arg7 = args.arg7,
1707 });
1708
1709 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1710
1711 /* Switch to receiver vCPU targeted to by direct msg request */
1712 *next = receiver_vcpu;
1713
1714 /*
1715 * Since this flow will lead to a VM switch, the return value will not
1716 * be applied to current vCPU.
1717 */
1718
1719out:
1720 sl_unlock(&receiver_vcpu->lock);
1721 sl_unlock(&current->lock);
1722
1723 return ret;
1724}
1725
1726/**
1727 * Send an FF-A direct message response.
1728 */
1729struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1730 ffa_vm_id_t receiver_vm_id,
1731 struct ffa_value args,
1732 struct vcpu *current,
1733 struct vcpu **next)
1734{
1735 ffa_vm_id_t current_vm_id = current->vm->id;
1736 struct vcpu_locked vcpu_locked;
1737
1738 /* Only allow secondary VMs to send direct message responses. */
1739 if (current_vm_id == HF_PRIMARY_VM_ID) {
1740 return ffa_error(FFA_NOT_SUPPORTED);
1741 }
1742
1743 /* Prevent sender_vm_id spoofing. */
1744 if (current_vm_id != sender_vm_id) {
1745 return ffa_error(FFA_INVALID_PARAMETERS);
1746 }
1747
1748 /* Prevent a VM from sending messages to itself. */
1749 if (current_vm_id == receiver_vm_id) {
1750 return ffa_error(FFA_INVALID_PARAMETERS);
1751 }
1752
1753 vcpu_locked = vcpu_lock(current);
1754
1755 /*
1756 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1757 * defined originator.
1758 */
1759 if (!is_ffa_direct_msg_request_ongoing(vcpu_locked)) {
1760 /*
1761 * Sending direct response but direct request origin vCPU is
1762 * not set.
1763 */
1764 vcpu_unlock(&vcpu_locked);
1765 return ffa_error(FFA_DENIED);
1766 }
1767
1768 if (current->direct_request_origin_vm_id != receiver_vm_id) {
1769 vcpu_unlock(&vcpu_locked);
1770 return ffa_error(FFA_DENIED);
1771 }
1772
1773 /* Clear direct request origin for the caller. */
1774 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1775
1776 vcpu_unlock(&vcpu_locked);
1777
1778 *next = api_switch_to_primary(current,
1779 (struct ffa_value){
1780 .func = args.func,
1781 .arg1 = args.arg1,
1782 .arg2 = 0,
1783 .arg3 = args.arg3,
1784 .arg4 = args.arg4,
1785 .arg5 = args.arg5,
1786 .arg6 = args.arg6,
1787 .arg7 = args.arg7,
1788 },
1789 VCPU_STATE_BLOCKED_MAILBOX);
1790
1791 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1792}
1793
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001794struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1795 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001796 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001797{
1798 struct vm *from = current->vm;
1799 struct vm *to;
1800 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801 struct ffa_memory_region *memory_region;
1802 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001803
1804 if (ipa_addr(address) != 0 || page_count != 0) {
1805 /*
1806 * Hafnium only supports passing the descriptor in the TX
1807 * mailbox.
1808 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001809 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001810 }
1811
Andrew Walbranca808b12020-05-15 17:22:28 +01001812 if (fragment_length > length) {
1813 dlog_verbose(
1814 "Fragment length %d greater than total length %d.\n",
1815 fragment_length, length);
1816 return ffa_error(FFA_INVALID_PARAMETERS);
1817 }
1818 if (fragment_length < sizeof(struct ffa_memory_region) +
1819 sizeof(struct ffa_memory_access)) {
1820 dlog_verbose(
1821 "Initial fragment length %d smaller than header size "
1822 "%d.\n",
1823 fragment_length,
1824 sizeof(struct ffa_memory_region) +
1825 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001826 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001827 }
1828
1829 /*
1830 * Check that the sender has configured its send buffer. If the TX
1831 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1832 * be safely accessed after releasing the lock since the TX mailbox
1833 * address can only be configured once.
1834 */
1835 sl_lock(&from->lock);
1836 from_msg = from->mailbox.send;
1837 sl_unlock(&from->lock);
1838
1839 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001840 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001841 }
1842
1843 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001844 * Copy the memory region descriptor to a fresh page from the memory
1845 * pool. This prevents the sender from changing it underneath us, and
1846 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001847 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001848 if (fragment_length > HF_MAILBOX_SIZE ||
1849 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001850 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001851 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001853 if (memory_region == NULL) {
1854 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001855 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001856 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001857 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001858
1859 /* The sender must match the caller. */
1860 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001861 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001862 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001863 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001864 }
1865
Andrew Walbrana65a1322020-04-06 19:32:32 +01001866 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001867 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001868 dlog_verbose(
1869 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001870 "endpoint memory access descriptors, expected 1).\n",
1871 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001872 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001873 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001874 }
1875
1876 /*
1877 * Ensure that the receiver VM exists and isn't the same as the sender.
1878 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001879 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001880 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001881 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001882 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001883 goto out;
1884 }
1885
1886 if (to->id == HF_TEE_VM_ID) {
1887 /*
1888 * The 'to' VM lock is only needed in the case that it is the
1889 * TEE VM.
1890 */
1891 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1892
1893 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001894 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001895 goto out_unlock;
1896 }
1897
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001898 ret = ffa_memory_tee_send(
1899 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1900 length, fragment_length, share_func, &api_page_pool);
1901 /*
1902 * ffa_tee_memory_send takes ownership of the memory_region, so
1903 * make sure we don't free it.
1904 */
1905 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001906
1907 out_unlock:
1908 vm_unlock(&vm_to_from_lock.vm1);
1909 vm_unlock(&vm_to_from_lock.vm2);
1910 } else {
1911 struct vm_locked from_locked = vm_lock(from);
1912
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001913 ret = ffa_memory_send(from_locked, memory_region, length,
1914 fragment_length, share_func,
1915 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001916 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001917 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001918 * make sure we don't free it.
1919 */
1920 memory_region = NULL;
1921
1922 vm_unlock(&from_locked);
1923 }
1924
1925out:
1926 if (memory_region != NULL) {
1927 mpool_free(&api_page_pool, memory_region);
1928 }
1929
1930 return ret;
1931}
1932
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001933struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1934 uint32_t fragment_length,
1935 ipaddr_t address, uint32_t page_count,
1936 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001937{
1938 struct vm *to = current->vm;
1939 struct vm_locked to_locked;
1940 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001941 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001942 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001943 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001944
1945 if (ipa_addr(address) != 0 || page_count != 0) {
1946 /*
1947 * Hafnium only supports passing the descriptor in the TX
1948 * mailbox.
1949 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001950 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001951 }
1952
Andrew Walbrana65a1322020-04-06 19:32:32 +01001953 if (fragment_length != length) {
1954 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001955 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001956 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001957
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001958 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001960 message_buffer_size = cpu_get_buffer_size(current->cpu);
1961 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1962 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001963 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001964 }
1965
1966 to_locked = vm_lock(to);
1967 to_msg = to->mailbox.send;
1968
1969 if (to_msg == NULL) {
1970 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001971 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001972 goto out;
1973 }
1974
1975 /*
1976 * Copy the retrieve request descriptor to an internal buffer, so that
1977 * the caller can't change it underneath us.
1978 */
1979 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1980
1981 if (msg_receiver_busy(to_locked, NULL, false)) {
1982 /*
1983 * Can't retrieve memory information if the mailbox is not
1984 * available.
1985 */
1986 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001988 goto out;
1989 }
1990
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001991 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1992 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001993
1994out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001995 vm_unlock(&to_locked);
1996 return ret;
1997}
1998
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001999struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002000{
2001 struct vm *from = current->vm;
2002 struct vm_locked from_locked;
2003 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002004 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002005 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002006 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002007 uint32_t length;
2008
2009 from_locked = vm_lock(from);
2010 from_msg = from->mailbox.send;
2011
2012 if (from_msg == NULL) {
2013 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002014 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002015 goto out;
2016 }
2017
2018 /*
2019 * Calculate length from relinquish descriptor before copying. We will
2020 * check again later to make sure it hasn't changed.
2021 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002022 length = sizeof(struct ffa_mem_relinquish) +
2023 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2024 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002025 /*
2026 * Copy the relinquish descriptor to an internal buffer, so that the
2027 * caller can't change it underneath us.
2028 */
2029 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002030 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002031 message_buffer_size = cpu_get_buffer_size(current->cpu);
2032 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2033 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002034 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002035 goto out;
2036 }
2037 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2038
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002039 if (sizeof(struct ffa_mem_relinquish) +
2040 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002041 length) {
2042 dlog_verbose(
2043 "Endpoint count changed while copying to internal "
2044 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002045 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002046 goto out;
2047 }
2048
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002049 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2050 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002051
2052out:
2053 vm_unlock(&from_locked);
2054 return ret;
2055}
2056
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002057struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2058 ffa_memory_region_flags_t flags,
2059 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002060{
2061 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002062 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002063
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002064 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2065 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002066 struct vm_locked to_locked = vm_lock(to);
2067
Andrew Walbranca808b12020-05-15 17:22:28 +01002068 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002069 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002070
Andrew Walbran290b0c92020-02-03 16:37:14 +00002071 vm_unlock(&to_locked);
2072 } else {
2073 struct vm *from = vm_find(HF_TEE_VM_ID);
2074 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2075
Andrew Walbranca808b12020-05-15 17:22:28 +01002076 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2077 vm_to_from_lock.vm2, handle, flags,
2078 &api_page_pool);
2079
2080 vm_unlock(&vm_to_from_lock.vm1);
2081 vm_unlock(&vm_to_from_lock.vm2);
2082 }
2083
2084 return ret;
2085}
2086
2087struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2088 uint32_t fragment_offset,
2089 ffa_vm_id_t sender_vm_id,
2090 struct vcpu *current)
2091{
2092 struct vm *to = current->vm;
2093 struct vm_locked to_locked;
2094 struct ffa_value ret;
2095
2096 /* Sender ID MBZ at virtual instance. */
2097 if (sender_vm_id != 0) {
2098 return ffa_error(FFA_INVALID_PARAMETERS);
2099 }
2100
2101 to_locked = vm_lock(to);
2102
2103 if (msg_receiver_busy(to_locked, NULL, false)) {
2104 /*
2105 * Can't retrieve memory information if the mailbox is not
2106 * available.
2107 */
2108 dlog_verbose("RX buffer not ready.\n");
2109 ret = ffa_error(FFA_BUSY);
2110 goto out;
2111 }
2112
2113 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2114 &api_page_pool);
2115
2116out:
2117 vm_unlock(&to_locked);
2118 return ret;
2119}
2120
2121struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2122 uint32_t fragment_length,
2123 ffa_vm_id_t sender_vm_id,
2124 struct vcpu *current)
2125{
2126 struct vm *from = current->vm;
2127 const void *from_msg;
2128 void *fragment_copy;
2129 struct ffa_value ret;
2130
2131 /* Sender ID MBZ at virtual instance. */
2132 if (sender_vm_id != 0) {
2133 return ffa_error(FFA_INVALID_PARAMETERS);
2134 }
2135
2136 /*
2137 * Check that the sender has configured its send buffer. If the TX
2138 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2139 * be safely accessed after releasing the lock since the TX mailbox
2140 * address can only be configured once.
2141 */
2142 sl_lock(&from->lock);
2143 from_msg = from->mailbox.send;
2144 sl_unlock(&from->lock);
2145
2146 if (from_msg == NULL) {
2147 return ffa_error(FFA_INVALID_PARAMETERS);
2148 }
2149
2150 /*
2151 * Copy the fragment to a fresh page from the memory pool. This prevents
2152 * the sender from changing it underneath us, and also lets us keep it
2153 * around in the share state table if needed.
2154 */
2155 if (fragment_length > HF_MAILBOX_SIZE ||
2156 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2157 dlog_verbose(
2158 "Fragment length %d larger than mailbox size %d.\n",
2159 fragment_length, HF_MAILBOX_SIZE);
2160 return ffa_error(FFA_INVALID_PARAMETERS);
2161 }
2162 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2163 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2164 0) {
2165 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2166 return ffa_error(FFA_INVALID_PARAMETERS);
2167 }
2168 fragment_copy = mpool_alloc(&api_page_pool);
2169 if (fragment_copy == NULL) {
2170 dlog_verbose("Failed to allocate fragment copy.\n");
2171 return ffa_error(FFA_NO_MEMORY);
2172 }
2173 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2174
2175 /*
2176 * Hafnium doesn't support fragmentation of memory retrieve requests
2177 * (because it doesn't support caller-specified mappings, so a request
2178 * will never be larger than a single page), so this must be part of a
2179 * memory send (i.e. donate, lend or share) request.
2180 *
2181 * We can tell from the handle whether the memory transaction is for the
2182 * TEE or not.
2183 */
2184 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2185 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2186 struct vm_locked from_locked = vm_lock(from);
2187
2188 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2189 fragment_length, handle,
2190 &api_page_pool);
2191 /*
2192 * `ffa_memory_send_continue` takes ownership of the
2193 * fragment_copy, so we don't need to free it here.
2194 */
2195 vm_unlock(&from_locked);
2196 } else {
2197 struct vm *to = vm_find(HF_TEE_VM_ID);
2198 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2199
2200 /*
2201 * The TEE RX buffer state is checked in
2202 * `ffa_memory_tee_send_continue` rather than here, as we need
2203 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2204 * than FFA_ERROR FFA_BUSY in case it is busy.
2205 */
2206
2207 ret = ffa_memory_tee_send_continue(
2208 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2209 fragment_length, handle, &api_page_pool);
2210 /*
2211 * `ffa_memory_tee_send_continue` takes ownership of the
2212 * fragment_copy, so we don't need to free it here.
2213 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002214
2215 vm_unlock(&vm_to_from_lock.vm1);
2216 vm_unlock(&vm_to_from_lock.vm2);
2217 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002218
2219 return ret;
2220}