blob: f0766a860b3f291b0e054c5ba808b39993e9e163 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000014#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020015#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000016
Andrew Scull877ae4b2019-07-02 12:52:33 +010017#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
20#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000021#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010022#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000023#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010024#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010025#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010026#include "hf/vm.h"
27
Andrew Scullf35a5c92018-08-07 18:09:46 +010028#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010029#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010030
Fuad Tabbae4efcc32020-07-16 15:37:27 +010031static_assert(sizeof(struct ffa_partition_info) == 8,
32 "Partition information descriptor size doesn't match the one in "
33 "the FF-A 1.0 EAC specification, Table 82.");
34
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000035/*
36 * To eliminate the risk of deadlocks, we define a partial order for the
37 * acquisition of locks held concurrently by the same physical CPU. Our current
38 * ordering requirements are as follows:
39 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010040 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000041 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010042 * Locks of the same kind require the lock of lowest address to be locked first,
43 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000044 */
45
Andrew Scullaa039b32018-10-04 15:02:26 +010046static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010047 "Currently, a page is mapped for the send and receive buffers so "
48 "the maximum request is the size of a page.");
49
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000050static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
51 "The page pool entry size must be at least as big as the mailbox "
52 "size, so that memory region descriptors can be copied from the "
53 "mailbox for memory sharing.");
54
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000055static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000056
57/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000058 * Initialises the API page pool by taking ownership of the contents of the
59 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000060 */
61void api_init(struct mpool *ppool)
62{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000063 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000064}
65
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010066/**
J-Alvesfe7f7372020-11-09 11:32:12 +000067 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
68 * is given as argument of the function.
69 *
70 * Called to change the context between SPs for direct messaging (when Hafnium
71 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
72 *
73 * This function works for partitions that are:
74 * - UP non-migratable.
75 * - MP with pinned Execution Contexts.
76 */
77static struct vcpu *api_switch_to_vm(struct vcpu *current,
78 struct ffa_value to_ret,
79 enum vcpu_state vcpu_state,
80 ffa_vm_id_t to_id)
81{
82 struct vm *to_vm = vm_find(to_id);
83 struct vcpu *next = vm_get_vcpu(to_vm, cpu_index(current->cpu));
84
85 CHECK(next != NULL);
86
87 /* Set the return value for the target VM. */
88 arch_regs_set_retval(&next->regs, to_ret);
89
90 /* Set the current vCPU state. */
91 sl_lock(&current->lock);
92 current->state = vcpu_state;
93 sl_unlock(&current->lock);
94
95 return next;
96}
97
98/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000099 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100100 *
101 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100102 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100103 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100104static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100105 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000106 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100107{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000108 /*
109 * If the secondary is blocked but has a timer running, sleep until the
110 * timer fires rather than indefinitely.
111 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100112 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100113 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
114 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100115 if (arch_timer_enabled_current()) {
116 uint64_t remaining_ns =
117 arch_timer_remaining_ns_current();
118
119 if (remaining_ns == 0) {
120 /*
121 * Timer is pending, so the current vCPU should
122 * be run again right away.
123 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100124 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100125 /*
126 * primary_ret.arg1 should already be set to the
127 * current VM ID and vCPU ID.
128 */
129 primary_ret.arg2 = 0;
130 } else {
131 primary_ret.arg2 = remaining_ns;
132 }
133 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100134 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100135 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000136 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100137 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000138
139 default:
140 /* Do nothing. */
141 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000142 }
143
J-Alvesfe7f7372020-11-09 11:32:12 +0000144 return api_switch_to_vm(current, primary_ret, secondary_state,
145 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100146}
147
148/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200149 * Choose next vCPU to run to be the counterpart vCPU in the other
150 * world (run the normal world if currently running in the secure
151 * world). Set current vCPU state to the given vcpu_state parameter.
152 * Set FF-A return values to the target vCPU in the other world.
153 *
154 * Called in context of a direct message response from a secure
155 * partition to a VM.
156 */
157static struct vcpu *api_switch_to_other_world(struct vcpu *current,
158 struct ffa_value other_world_ret,
159 enum vcpu_state vcpu_state)
160{
J-Alvesfe7f7372020-11-09 11:32:12 +0000161 return api_switch_to_vm(current, other_world_ret, vcpu_state,
162 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200163}
164
165/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100166 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
167 * registers the `from` VM to be notified when it becomes available.
168 */
169static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
170{
171 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
172 to.vm->mailbox.recv == NULL) {
173 /*
174 * Fail if the receiver isn't currently ready to receive data,
175 * setting up for notification if requested.
176 */
177 if (notify) {
178 struct wait_entry *entry =
179 vm_get_wait_entry(from, to.vm->id);
180
181 /* Append waiter only if it's not there yet. */
182 if (list_empty(&entry->wait_links)) {
183 list_append(&to.vm->mailbox.waiter_list,
184 &entry->wait_links);
185 }
186 }
187
188 return true;
189 }
190
191 return false;
192}
193
194/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000195 * Returns true if the given vCPU is executing in context of an
196 * FFA_MSG_SEND_DIRECT_REQ invocation.
197 */
198static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
199{
200 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
201}
202
203/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000204 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000205 */
206struct vcpu *api_preempt(struct vcpu *current)
207{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100208 struct ffa_value ret = {
209 .func = FFA_INTERRUPT_32,
210 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000211 };
212
Andrew Sculld6ee1102019-04-05 22:12:42 +0100213 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000214}
215
216/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000217 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000218 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100219 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100220struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100221{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100222 struct ffa_value ret = {
223 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
224 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100225 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000226
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000227 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100228 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100229}
230
231/**
Andrew Walbran33645652019-04-15 12:29:31 +0100232 * Puts the current vCPU in off mode, and returns to the primary VM.
233 */
234struct vcpu *api_vcpu_off(struct vcpu *current)
235{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100236 struct ffa_value ret = {
237 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
238 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100239 };
240
241 /*
242 * Disable the timer, so the scheduler doesn't get told to call back
243 * based on it.
244 */
245 arch_timer_disable_current();
246
247 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
248}
249
250/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000251 * Returns to the primary VM to allow this CPU to be used for other tasks as the
252 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100253 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000254 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000255struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000256{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000257 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
258 struct vcpu_locked current_locked;
259 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000260
261 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000262 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000263 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000264 }
265
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000266 current_locked = vcpu_lock(current);
267 is_direct_request_ongoing =
268 is_ffa_direct_msg_request_ongoing(current_locked);
269 vcpu_unlock(&current_locked);
270
271 if (is_direct_request_ongoing) {
272 return ffa_error(FFA_DENIED);
273 }
274
275 *next = api_switch_to_primary(
276 current,
277 (struct ffa_value){.func = FFA_YIELD_32,
278 .arg1 = ffa_vm_vcpu(current->vm->id,
279 vcpu_index(current))},
280 VCPU_STATE_READY);
281
282 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000283}
284
285/**
Andrew Walbran33645652019-04-15 12:29:31 +0100286 * Switches to the primary so that it can switch to the target, or kick it if it
287 * is already running on a different physical CPU.
288 */
289struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
290{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100291 struct ffa_value ret = {
292 .func = HF_FFA_RUN_WAKE_UP,
293 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
294 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100295 };
296 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
297}
298
299/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000300 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000301 */
302struct vcpu *api_abort(struct vcpu *current)
303{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100304 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000305
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100306 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000307 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000308
309 if (current->vm->id == HF_PRIMARY_VM_ID) {
310 /* TODO: what to do when the primary aborts? */
311 for (;;) {
312 /* Do nothing. */
313 }
314 }
315
316 atomic_store_explicit(&current->vm->aborting, true,
317 memory_order_relaxed);
318
319 /* TODO: free resources once all vCPUs abort. */
320
Andrew Sculld6ee1102019-04-05 22:12:42 +0100321 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000322}
323
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100324struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
325 const struct ffa_uuid *uuid)
326{
327 struct vm *current_vm = current->vm;
328 struct vm_locked current_vm_locked;
329 ffa_vm_count_t vm_count = 0;
330 bool uuid_is_null = ffa_uuid_is_null(uuid);
331 struct ffa_value ret;
332 uint32_t size;
333 struct ffa_partition_info partitions[MAX_VMS];
334
335 /*
336 * Iterate through the VMs to find the ones with a matching UUID.
337 * A Null UUID retrieves information for all VMs.
338 */
339 for (uint16_t index = 0; index < vm_get_count(); ++index) {
340 const struct vm *vm = vm_find_index(index);
341
342 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
343 partitions[vm_count].vm_id = vm->id;
344 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100345 partitions[vm_count].properties =
Olivier Deprez764fd2e2020-07-29 15:14:09 +0200346 arch_vm_partition_properties(vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100347
348 ++vm_count;
349 }
350 }
351
352 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
353 if (vm_count == 0) {
354 return ffa_error(FFA_INVALID_PARAMETERS);
355 }
356
357 size = vm_count * sizeof(partitions[0]);
358 if (size > FFA_MSG_PAYLOAD_MAX) {
359 dlog_error(
360 "Partition information does not fit in the VM's RX "
361 "buffer.\n");
362 return ffa_error(FFA_NO_MEMORY);
363 }
364
365 /*
366 * Partition information is returned in the VM's RX buffer, which is why
367 * the lock is needed.
368 */
369 current_vm_locked = vm_lock(current_vm);
370
371 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
372 /*
373 * Can't retrieve memory information if the mailbox is not
374 * available.
375 */
376 dlog_verbose("RX buffer not ready.\n");
377 ret = ffa_error(FFA_BUSY);
378 goto out_unlock;
379 }
380
381 /* Populate the VM's RX buffer with the partition information. */
382 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
383 size);
384 current_vm->mailbox.recv_size = size;
385 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
386 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
387 current_vm->mailbox.state = MAILBOX_STATE_READ;
388
389 /* Return the count of partition information descriptors in w2. */
390 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
391
392out_unlock:
393 vm_unlock(&current_vm_locked);
394
395 return ret;
396}
397
Andrew Scull9726c252019-01-23 13:44:19 +0000398/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000399 * Returns the ID of the VM.
400 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100401struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000402{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100403 return (struct ffa_value){.func = FFA_SUCCESS_32,
404 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000405}
406
407/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000408 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000409 * function to indicate that register state for the given vCPU has been saved
410 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000411 */
412void api_regs_state_saved(struct vcpu *vcpu)
413{
414 sl_lock(&vcpu->lock);
415 vcpu->regs_available = true;
416 sl_unlock(&vcpu->lock);
417}
418
419/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000420 * Retrieves the next waiter and removes it from the wait list if the VM's
421 * mailbox is in a writable state.
422 */
423static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
424{
425 struct wait_entry *entry;
426 struct vm *vm = locked_vm.vm;
427
Andrew Sculld6ee1102019-04-05 22:12:42 +0100428 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000429 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
430 /* The mailbox is not writable or there are no waiters. */
431 return NULL;
432 }
433
434 /* Remove waiter from the wait list. */
435 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
436 wait_links);
437 list_remove(&entry->wait_links);
438 return entry;
439}
440
441/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000442 * Assuming that the arguments have already been checked by the caller, injects
443 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
444 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
445 * is next run, which is up to the scheduler.
446 *
447 * Returns:
448 * - 0 on success if no further action is needed.
449 * - 1 if it was called by the primary VM and the primary VM now needs to wake
450 * up or kick the target vCPU.
451 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200452static int64_t internal_interrupt_inject_locked(
453 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
454 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000455{
456 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100457 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000458 int64_t ret = 0;
459
Andrew Walbran508e63c2018-12-20 17:02:37 +0000460 /*
461 * We only need to change state and (maybe) trigger a virtual IRQ if it
462 * is enabled and was not previously pending. Otherwise we can skip
463 * everything except setting the pending bit.
464 *
465 * If you change this logic make sure to update the need_vm_lock logic
466 * above to match.
467 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200468 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
469 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000470 intid_mask)) {
471 goto out;
472 }
473
474 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200475 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000476
477 /*
478 * Only need to update state if there was not already an
479 * interrupt enabled and pending.
480 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200481 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000482 goto out;
483 }
484
Andrew Walbran508e63c2018-12-20 17:02:37 +0000485 if (current->vm->id == HF_PRIMARY_VM_ID) {
486 /*
487 * If the call came from the primary VM, let it know that it
488 * should run or kick the target vCPU.
489 */
490 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200491 } else if (current != target_locked.vcpu && next != NULL) {
492 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000493 }
494
495out:
496 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200497 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
498 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000499
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200500 return ret;
501}
502
503/* Wrapper to internal_interrupt_inject with locking of target vCPU */
504static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
505 uint32_t intid, struct vcpu *current,
506 struct vcpu **next)
507{
508 int64_t ret;
509 struct vcpu_locked target_locked;
510
511 target_locked = vcpu_lock(target_vcpu);
512 ret = internal_interrupt_inject_locked(target_locked, intid, current,
513 next);
514 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000515
516 return ret;
517}
518
519/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
521 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100522 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100524{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000525 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526 case FFA_MSG_SEND_32:
527 return (struct ffa_value){
528 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000529 .arg1 = (receiver->mailbox.recv_sender << 16) |
530 receiver->id,
531 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000532 default:
533 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000534 dlog_error("Tried to return an invalid message function %#x\n",
535 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000537 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100538}
539
540/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000541 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000542 * value needs to be forced onto the vCPU.
543 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000544static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100545 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000546{
Andrew Scullb06d1752019-02-04 10:15:48 +0000547 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000548 bool ret;
549
Andrew Scullb06d1752019-02-04 10:15:48 +0000550 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000551 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000552 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100553 * The VM lock is not needed in the common case so it must only be taken
554 * when it is going to be needed. This ensures there are no inter-vCPU
555 * dependencies in the common run case meaning the sensitive context
556 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000557 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000558 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000559
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000560 /* The VM needs to be locked to deliver mailbox messages. */
561 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
562 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000563 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000564 sl_lock(&vcpu->vm->lock);
565 sl_lock(&vcpu->lock);
566 }
567
568 /*
569 * If the vCPU is already running somewhere then we can't run it here
570 * simultaneously. While it is actually running then the state should be
571 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
572 * stops running but while Hafnium is in the process of switching back
573 * to the primary there will be a brief period while the state has been
574 * updated but `regs_available` is still false (until
575 * `api_regs_state_saved` is called). We can't start running it again
576 * until this has finished, so count this state as still running for the
577 * purposes of this check.
578 */
579 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
580 /*
581 * vCPU is running on another pCPU.
582 *
583 * It's okay not to return the sleep duration here because the
584 * other physical CPU that is currently running this vCPU will
585 * return the sleep duration if needed.
586 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100587 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000588 ret = false;
589 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000590 }
Andrew Scull9726c252019-01-23 13:44:19 +0000591
592 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100593 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100594 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000595 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100596 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000597 }
598 ret = false;
599 goto out;
600 }
601
Andrew Walbran508e63c2018-12-20 17:02:37 +0000602 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100603 case VCPU_STATE_RUNNING:
604 case VCPU_STATE_OFF:
605 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000606 ret = false;
607 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000608
Andrew Sculld6ee1102019-04-05 22:12:42 +0100609 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000610 /*
611 * A pending message allows the vCPU to run so the message can
612 * be delivered directly.
613 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100614 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100615 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100617 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000618 break;
619 }
620 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100621 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000622 /* Allow virtual interrupts to be delivered. */
623 if (vcpu->interrupts.enabled_and_pending_count > 0) {
624 break;
625 }
626
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100627 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
628
Andrew Walbran508e63c2018-12-20 17:02:37 +0000629 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100630 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000631 arch_timer_remaining_ns(&vcpu->regs);
632
633 /*
634 * The timer expired so allow the interrupt to be
635 * delivered.
636 */
637 if (timer_remaining_ns == 0) {
638 break;
639 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100640 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000641
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100642 /*
643 * The vCPU is not ready to run, return the appropriate code to
644 * the primary which called vcpu_run.
645 */
646 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100647 ? FFA_MSG_WAIT_32
648 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100649 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
650 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000651
652 ret = false;
653 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000654
Andrew Sculld6ee1102019-04-05 22:12:42 +0100655 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000656 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000657 }
658
Andrew Scullb06d1752019-02-04 10:15:48 +0000659 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000660 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100661 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000662
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000663 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000664 * Mark the registers as unavailable now that we're about to reflect
665 * them onto the real registers. This will also prevent another physical
666 * CPU from trying to read these registers.
667 */
668 vcpu->regs_available = false;
669
670 ret = true;
671
672out:
673 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000674 if (need_vm_lock) {
675 sl_unlock(&vcpu->vm->lock);
676 }
677
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000678 return ret;
679}
680
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
682 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100683{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100684 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100685 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100687
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000688 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100689 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100690 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100691 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100692 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100693
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000694 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100695 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100696 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100697 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100698
Andrew Scull19503262018-09-20 14:48:39 +0100699 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100700 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100701 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100702 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100703 }
704
Fuad Tabbaed294af2019-12-20 10:43:01 +0000705 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100706 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100707 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100708 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100709
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000710 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100711 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000712 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000713 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100714 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000715
Andrew Walbran508e63c2018-12-20 17:02:37 +0000716 /*
717 * Inject timer interrupt if timer has expired. It's safe to access
718 * vcpu->regs here because api_vcpu_prepare_run already made sure that
719 * regs_available was true (and then set it to false) before returning
720 * true.
721 */
722 if (arch_timer_pending(&vcpu->regs)) {
723 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100724 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
725 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000726
727 /*
728 * Set the mask bit so the hardware interrupt doesn't fire
729 * again. Ideally we wouldn't do this because it affects what
730 * the secondary vCPU sees, but if we don't then we end up with
731 * a loop of the interrupt firing each time we try to return to
732 * the secondary vCPU.
733 */
734 arch_timer_mask(&vcpu->regs);
735 }
736
Fuad Tabbaed294af2019-12-20 10:43:01 +0000737 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000738 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000739
Andrew Scull33fecd32019-01-08 14:48:27 +0000740 /*
741 * Set a placeholder return code to the scheduler. This will be
742 * overwritten when the switch back to the primary occurs.
743 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100744 ret.func = FFA_INTERRUPT_32;
745 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100746 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000747
Andrew Scull6d2db332018-10-10 15:28:17 +0100748out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100749 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100750}
751
752/**
Andrew Scull81e85092018-12-12 12:56:20 +0000753 * Check that the mode indicates memory that is valid, owned and exclusive.
754 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100755static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000756{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100757 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
758 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000759}
760
761/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100762 * Determines the value to be returned by api_ffa_rxtx_map and
763 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
764 * there are waiters, it also switches back to the primary VM for it to wake
765 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000766 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100767static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
768 struct vcpu *current,
769 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000770{
771 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000772
773 if (list_empty(&vm->mailbox.waiter_list)) {
774 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100775 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000776 }
777
778 if (vm->id == HF_PRIMARY_VM_ID) {
779 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100780 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000781 }
782
783 /*
784 * Switch back to the primary VM, informing it that there are waiters
785 * that need to be notified.
786 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000787 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100788 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000789 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000790
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100791 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000792}
793
794/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100795 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100796 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100797static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
798 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100799 paddr_t pa_send_begin, paddr_t pa_send_end,
800 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200801 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100802 struct mpool *local_page_pool)
803{
804 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100805
806 /* Map the send page as read-only in the hypervisor address space. */
807 vm_locked.vm->mailbox.send =
808 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200809 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100810 if (!vm_locked.vm->mailbox.send) {
811 /* TODO: partial defrag of failed range. */
812 /* Recover any memory consumed in failed mapping. */
813 mm_defrag(mm_stage1_locked, local_page_pool);
814 goto fail;
815 }
816
817 /*
818 * Map the receive page as writable in the hypervisor address space. On
819 * failure, unmap the send page before returning.
820 */
821 vm_locked.vm->mailbox.recv =
822 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200823 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100824 if (!vm_locked.vm->mailbox.recv) {
825 /* TODO: partial defrag of failed range. */
826 /* Recover any memory consumed in failed mapping. */
827 mm_defrag(mm_stage1_locked, local_page_pool);
828 goto fail_undo_send;
829 }
830
831 ret = true;
832 goto out;
833
834 /*
835 * The following mappings will not require more memory than is available
836 * in the local pool.
837 */
838fail_undo_send:
839 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100840 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
841 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100842
843fail:
844 ret = false;
845
846out:
Andrew Sculle1322792019-07-01 17:46:10 +0100847 return ret;
848}
849
850/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100851 * Sanity checks and configures the send and receive pages in the VM stage-2
852 * and hypervisor stage-1 page tables.
853 *
854 * Returns:
855 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
856 * aligned or are the same.
857 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
858 * due to insuffient page table memory.
859 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
860 * the caller.
861 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100862 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100863
864struct ffa_value api_vm_configure_pages(
865 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
866 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
867 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100868{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100869 struct ffa_value ret;
870 paddr_t pa_send_begin;
871 paddr_t pa_send_end;
872 paddr_t pa_recv_begin;
873 paddr_t pa_recv_end;
874 uint32_t orig_send_mode;
875 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200876 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100877
878 /* We only allow these to be setup once. */
879 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
880 ret = ffa_error(FFA_DENIED);
881 goto out;
882 }
883
884 /* Hafnium only supports a fixed size of RX/TX buffers. */
885 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
886 ret = ffa_error(FFA_INVALID_PARAMETERS);
887 goto out;
888 }
889
890 /* Fail if addresses are not page-aligned. */
891 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
892 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
893 ret = ffa_error(FFA_INVALID_PARAMETERS);
894 goto out;
895 }
896
897 /* Convert to physical addresses. */
898 pa_send_begin = pa_from_ipa(send);
899 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
900 pa_recv_begin = pa_from_ipa(recv);
901 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
902
903 /* Fail if the same page is used for the send and receive pages. */
904 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
905 ret = ffa_error(FFA_INVALID_PARAMETERS);
906 goto out;
907 }
Andrew Sculle1322792019-07-01 17:46:10 +0100908
909 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100910 * Ensure the pages are valid, owned and exclusive to the VM and that
911 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100912 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100913 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
914 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
915 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
916 (orig_send_mode & MM_MODE_R) == 0 ||
917 (orig_send_mode & MM_MODE_W) == 0) {
918 ret = ffa_error(FFA_DENIED);
919 goto out;
920 }
921
922 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
923 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
924 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
925 (orig_recv_mode & MM_MODE_R) == 0) {
926 ret = ffa_error(FFA_DENIED);
927 goto out;
928 }
Andrew Sculle1322792019-07-01 17:46:10 +0100929
930 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000931 if (!vm_identity_map(
932 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100933 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100934 local_page_pool, NULL)) {
935 ret = ffa_error(FFA_NO_MEMORY);
936 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100937 }
938
Andrew Scull3c257452019-11-26 13:32:50 +0000939 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
940 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100941 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100942 /* TODO: partial defrag of failed range. */
943 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100944 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100945 goto fail_undo_send;
946 }
947
Olivier Deprez96a2a262020-06-11 17:21:38 +0200948 /* Get extra send/recv pages mapping attributes for the given VM ID. */
949 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
950
Manish Pandeyd34f8892020-06-19 17:41:07 +0100951 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
952 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200953 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100954 goto fail_undo_send_and_recv;
955 }
956
Manish Pandeyd34f8892020-06-19 17:41:07 +0100957 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100958 goto out;
959
Andrew Sculle1322792019-07-01 17:46:10 +0100960fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000961 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100962 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100963
964fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000965 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100966 orig_send_mode, local_page_pool, NULL));
967 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +0100968
969out:
Andrew Sculle1322792019-07-01 17:46:10 +0100970 return ret;
971}
972
973/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100974 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +0100975 * must not be shared. Locking of the page tables combined with a local memory
976 * pool ensures there will always be enough memory to recover from any errors
977 * that arise. The stage-1 page tables must be locked so memory cannot be taken
978 * by another core which could result in this transaction being unable to roll
979 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000980 *
981 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000983 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100984 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000985 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000987 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 * - FFA_SUCCESS on success if no further action is needed.
989 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000990 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100991 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
993 uint32_t page_count, struct vcpu *current,
994 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100995{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100996 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100997 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100998 struct vm_locked vm_locked;
999 struct mm_stage1_locked mm_stage1_locked;
1000 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001001
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001002 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001003 * Create a local pool so any freed memory can't be used by another
1004 * thread. This is to ensure the original mapping can be restored if any
1005 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001006 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001007 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1008
Andrew Sculle1322792019-07-01 17:46:10 +01001009 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001010 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001011
Manish Pandeyd34f8892020-06-19 17:41:07 +01001012 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1013 page_count, &local_page_pool);
1014 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001015 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001016 }
1017
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001018 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001019 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001020
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001021exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001022 mpool_fini(&local_page_pool);
1023
1024 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001025 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001026
1027 return ret;
1028}
1029
1030/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001031 * Notifies the `to` VM about the message currently in its mailbox, possibly
1032 * with the help of the primary VM.
1033 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1035 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001036{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001037 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1038 struct ffa_value primary_ret = {
1039 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001040 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001041 };
1042
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001043 /* Messages for the primary VM are delivered directly. */
1044 if (to.vm->id == HF_PRIMARY_VM_ID) {
1045 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001046 * Only tell the primary VM the size and other details if the
1047 * message is for it, to avoid leaking data about messages for
1048 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001049 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001051
1052 to.vm->mailbox.state = MAILBOX_STATE_READ;
1053 *next = api_switch_to_primary(current, primary_ret,
1054 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001055 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001056 }
1057
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001058 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1059
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001060 /* Messages for the TEE are sent on via the dispatcher. */
1061 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001062 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001063
Olivier Deprez112d2b52020-09-30 07:39:23 +02001064 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001065 /*
1066 * After the call to the TEE completes it must have finished
1067 * reading its RX buffer, so it is ready for another message.
1068 */
1069 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001070 /*
1071 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001073 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001074 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001075 }
1076
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001077 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001078 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001079 *next = api_switch_to_primary(current, primary_ret,
1080 VCPU_STATE_READY);
1081 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001082
1083 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001084}
1085
1086/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001087 * Copies data from the sender's send buffer to the recipient's receive buffer
1088 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001089 *
1090 * If the recipient's receive buffer is busy, it can optionally register the
1091 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001092 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001093struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1094 ffa_vm_id_t receiver_vm_id, uint32_t size,
1095 uint32_t attributes, struct vcpu *current,
1096 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001097{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001098 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001099 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001100 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001101 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001102 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001103 struct vcpu_locked current_locked;
1104 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001105 bool notify =
1106 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001107
Andrew Walbran70bc8622019-10-07 14:15:58 +01001108 /* Ensure sender VM ID corresponds to the current VM. */
1109 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001110 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001111 }
1112
1113 /* Disallow reflexive requests as this suggests an error in the VM. */
1114 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001116 }
1117
1118 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001119 if (size > FFA_MSG_PAYLOAD_MAX) {
1120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001121 }
1122
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001123 /*
1124 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1125 * invocation.
1126 */
1127 current_locked = vcpu_lock(current);
1128 is_direct_request_ongoing =
1129 is_ffa_direct_msg_request_ongoing(current_locked);
1130 vcpu_unlock(&current_locked);
1131
1132 if (is_direct_request_ongoing) {
1133 return ffa_error(FFA_DENIED);
1134 }
1135
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001136 /* Ensure the receiver VM exists. */
1137 to = vm_find(receiver_vm_id);
1138 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001140 }
1141
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001142 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001143 * Check that the sender has configured its send buffer. If the tx
1144 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1145 * be safely accessed after releasing the lock since the tx mailbox
1146 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001147 */
1148 sl_lock(&from->lock);
1149 from_msg = from->mailbox.send;
1150 sl_unlock(&from->lock);
1151
1152 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001153 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001154 }
1155
Andrew Walbran82d6d152019-12-24 15:02:06 +00001156 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001157
Andrew Walbran82d6d152019-12-24 15:02:06 +00001158 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001159 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001160 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001161 }
1162
Andrew Walbran82d6d152019-12-24 15:02:06 +00001163 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001165 to->mailbox.recv_size = size;
1166 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001167 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001168 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001169
1170out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001171 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001172
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001173 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001174}
1175
1176/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001177 * Checks whether the vCPU's attempt to block for a message has already been
1178 * interrupted or whether it is allowed to block.
1179 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001181{
1182 bool interrupted;
1183
1184 sl_lock(&current->lock);
1185
1186 /*
1187 * Don't block if there are enabled and pending interrupts, to match
1188 * behaviour of wait_for_interrupt.
1189 */
1190 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1191
1192 sl_unlock(&current->lock);
1193
1194 return interrupted;
1195}
1196
1197/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001198 * Receives a message from the mailbox. If one isn't available, this function
1199 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001200 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001201 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001202 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001203struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1204 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001205{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001206 bool is_direct_request_ongoing;
1207 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001208 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001210 bool is_from_secure_world =
1211 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001212
Andrew Scullaa039b32018-10-04 15:02:26 +01001213 /*
1214 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001215 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001216 */
J-Alvesb37fd082020-10-22 12:29:21 +01001217 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001218 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001219 }
1220
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001221 /*
1222 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1223 * invocation.
1224 */
1225 current_locked = vcpu_lock(current);
1226 is_direct_request_ongoing =
1227 is_ffa_direct_msg_request_ongoing(current_locked);
1228 vcpu_unlock(&current_locked);
1229
1230 if (is_direct_request_ongoing) {
1231 return ffa_error(FFA_DENIED);
1232 }
1233
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001234 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001235
Andrew Scullaa039b32018-10-04 15:02:26 +01001236 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001237 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1238 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001240 goto out;
1241 }
1242
1243 /* No pending message so fail if not allowed to block. */
1244 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001245 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001246 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001247 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001248
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001249 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001250 * From this point onward this call can only be interrupted or a message
1251 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001252 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001253 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254 return_code = ffa_error(FFA_INTERRUPTED);
1255 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001256 goto out;
1257 }
1258
J-Alvesb37fd082020-10-22 12:29:21 +01001259 if (is_from_secure_world) {
1260 /* Return to other world if caller is a SP. */
1261 *next = api_switch_to_other_world(
1262 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1263 VCPU_STATE_BLOCKED_MAILBOX);
1264 } else {
1265 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001266 struct ffa_value run_return = {
1267 .func = FFA_MSG_WAIT_32,
1268 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001269 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001270
Andrew Walbranb4816552018-12-05 17:35:42 +00001271 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001272 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001273 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001274out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001275 sl_unlock(&vm->lock);
1276
Jose Marinho3e2442f2019-03-12 13:30:37 +00001277 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001278}
1279
1280/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001281 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1282 * by this function, the caller must have called api_mailbox_send before with
1283 * the notify argument set to true, and this call must have failed because the
1284 * mailbox was not available.
1285 *
1286 * It should be called repeatedly to retrieve a list of VMs.
1287 *
1288 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1289 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001290 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001291int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001292{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001293 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001294 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001295 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001296
1297 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001298 if (list_empty(&vm->mailbox.ready_list)) {
1299 ret = -1;
1300 goto exit;
1301 }
1302
1303 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1304 ready_links);
1305 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001306 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001307
1308exit:
1309 sl_unlock(&vm->lock);
1310 return ret;
1311}
1312
1313/**
1314 * Retrieves the next VM waiting to be notified that the mailbox of the
1315 * specified VM became writable. Only primary VMs are allowed to call this.
1316 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001317 * Returns -1 on failure or if there are no waiters; the VM id of the next
1318 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001319 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001320int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001321{
1322 struct vm *vm;
1323 struct vm_locked locked;
1324 struct wait_entry *entry;
1325 struct vm *waiting_vm;
1326
1327 /* Only primary VMs are allowed to call this function. */
1328 if (current->vm->id != HF_PRIMARY_VM_ID) {
1329 return -1;
1330 }
1331
Andrew Walbran42347a92019-05-09 13:59:03 +01001332 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001333 if (vm == NULL) {
1334 return -1;
1335 }
1336
Fuad Tabbaed294af2019-12-20 10:43:01 +00001337 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001338 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001339 entry = api_fetch_waiter(locked);
1340 vm_unlock(&locked);
1341
1342 if (entry == NULL) {
1343 return -1;
1344 }
1345
1346 /* Enqueue notification to waiting VM. */
1347 waiting_vm = entry->waiting_vm;
1348
1349 sl_lock(&waiting_vm->lock);
1350 if (list_empty(&entry->ready_links)) {
1351 list_append(&waiting_vm->mailbox.ready_list,
1352 &entry->ready_links);
1353 }
1354 sl_unlock(&waiting_vm->lock);
1355
1356 return waiting_vm->id;
1357}
1358
1359/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001360 * Releases the caller's mailbox so that a new message can be received. The
1361 * caller must have copied out all data they wish to preserve as new messages
1362 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001363 *
1364 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001365 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1366 * - FFA_SUCCESS on success if no further action is needed.
1367 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001368 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001369 * hf_mailbox_waiter_get.
1370 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001371struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001372{
1373 struct vm *vm = current->vm;
1374 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001376
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001377 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001378 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001379 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001380 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001382 break;
1383
Andrew Sculld6ee1102019-04-05 22:12:42 +01001384 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001385 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001386 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001387 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001388 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001389 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001390
1391 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001392}
Andrew Walbran318f5732018-11-20 16:23:42 +00001393
1394/**
1395 * Enables or disables a given interrupt ID for the calling vCPU.
1396 *
1397 * Returns 0 on success, or -1 if the intid is invalid.
1398 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001399int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001400{
1401 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001402 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001403
Andrew Walbran318f5732018-11-20 16:23:42 +00001404 if (intid >= HF_NUM_INTIDS) {
1405 return -1;
1406 }
1407
1408 sl_lock(&current->lock);
1409 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001410 /*
1411 * If it is pending and was not enabled before, increment the
1412 * count.
1413 */
1414 if (current->interrupts.interrupt_pending[intid_index] &
1415 ~current->interrupts.interrupt_enabled[intid_index] &
1416 intid_mask) {
1417 current->interrupts.enabled_and_pending_count++;
1418 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001419 current->interrupts.interrupt_enabled[intid_index] |=
1420 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001421 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001422 /*
1423 * If it is pending and was enabled before, decrement the count.
1424 */
1425 if (current->interrupts.interrupt_pending[intid_index] &
1426 current->interrupts.interrupt_enabled[intid_index] &
1427 intid_mask) {
1428 current->interrupts.enabled_and_pending_count--;
1429 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001430 current->interrupts.interrupt_enabled[intid_index] &=
1431 ~intid_mask;
1432 }
1433
1434 sl_unlock(&current->lock);
1435 return 0;
1436}
1437
1438/**
1439 * Returns the ID of the next pending interrupt for the calling vCPU, and
1440 * acknowledges it (i.e. marks it as no longer pending). Returns
1441 * HF_INVALID_INTID if there are no pending interrupts.
1442 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001443uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001444{
1445 uint8_t i;
1446 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001447
1448 /*
1449 * Find the first enabled and pending interrupt ID, return it, and
1450 * deactivate it.
1451 */
1452 sl_lock(&current->lock);
1453 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1454 uint32_t enabled_and_pending =
1455 current->interrupts.interrupt_enabled[i] &
1456 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001457
Andrew Walbran318f5732018-11-20 16:23:42 +00001458 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001459 uint8_t bit_index = ctz(enabled_and_pending);
1460 /*
1461 * Mark it as no longer pending and decrement the count.
1462 */
1463 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001464 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001465 current->interrupts.enabled_and_pending_count--;
1466 first_interrupt =
1467 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001468 break;
1469 }
1470 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001471
1472 sl_unlock(&current->lock);
1473 return first_interrupt;
1474}
1475
1476/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001477 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001478 * given VM and vCPU.
1479 */
1480static inline bool is_injection_allowed(uint32_t target_vm_id,
1481 struct vcpu *current)
1482{
1483 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001484
Andrew Walbran318f5732018-11-20 16:23:42 +00001485 /*
1486 * The primary VM is allowed to inject interrupts into any VM. Secondary
1487 * VMs are only allowed to inject interrupts into their own vCPUs.
1488 */
1489 return current_vm_id == HF_PRIMARY_VM_ID ||
1490 current_vm_id == target_vm_id;
1491}
1492
1493/**
1494 * Injects a virtual interrupt of the given ID into the given target vCPU.
1495 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1496 * when the vCPU is next run, which is up to the scheduler.
1497 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001498 * Returns:
1499 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1500 * ID is invalid, or the current VM is not allowed to inject interrupts to
1501 * the target VM.
1502 * - 0 on success if no further action is needed.
1503 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1504 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001505 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001506int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1507 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001508 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001509{
Andrew Walbran318f5732018-11-20 16:23:42 +00001510 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001511 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001512
1513 if (intid >= HF_NUM_INTIDS) {
1514 return -1;
1515 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001516
Andrew Walbran318f5732018-11-20 16:23:42 +00001517 if (target_vm == NULL) {
1518 return -1;
1519 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001520
Andrew Walbran318f5732018-11-20 16:23:42 +00001521 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001522 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001523 return -1;
1524 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001525
Andrew Walbran318f5732018-11-20 16:23:42 +00001526 if (!is_injection_allowed(target_vm_id, current)) {
1527 return -1;
1528 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001529
Andrew Walbrane1310df2019-04-29 17:28:28 +01001530 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001531
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001532 dlog_info("Injecting IRQ %u for VM %#x vCPU %u from VM %#x vCPU %u\n",
Andrew Walbran17eebf92020-02-05 16:35:49 +00001533 intid, target_vm_id, target_vcpu_idx, current->vm->id,
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001534 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001535 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001536}
Andrew Scull6386f252018-12-06 13:29:10 +00001537
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538/** Returns the version of the implemented FF-A specification. */
1539struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001540{
1541 /*
1542 * Ensure that both major and minor revision representation occupies at
1543 * most 15 bits.
1544 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001545 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001546 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001547 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001548 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001549 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001550 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001552 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001553
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001554 return (struct ffa_value){
1555 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1556 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001557}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001558
1559int64_t api_debug_log(char c, struct vcpu *current)
1560{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001561 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001562 struct vm *vm = current->vm;
1563 struct vm_locked vm_locked = vm_lock(vm);
1564
Andrew Sculld54e1be2019-08-20 11:09:42 +01001565 if (c == '\n' || c == '\0') {
1566 flush = true;
1567 } else {
1568 vm->log_buffer[vm->log_buffer_length++] = c;
1569 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1570 }
1571
1572 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001573 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1574 vm->log_buffer_length);
1575 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001576 }
1577
1578 vm_unlock(&vm_locked);
1579
1580 return 0;
1581}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001582
1583/**
1584 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001586 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001587struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001588{
1589 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001590 case FFA_ERROR_32:
1591 case FFA_SUCCESS_32:
1592 case FFA_INTERRUPT_32:
1593 case FFA_VERSION_32:
1594 case FFA_FEATURES_32:
1595 case FFA_RX_RELEASE_32:
1596 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001597 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 case FFA_ID_GET_32:
1599 case FFA_MSG_POLL_32:
1600 case FFA_MSG_WAIT_32:
1601 case FFA_YIELD_32:
1602 case FFA_RUN_32:
1603 case FFA_MSG_SEND_32:
1604 case FFA_MEM_DONATE_32:
1605 case FFA_MEM_LEND_32:
1606 case FFA_MEM_SHARE_32:
1607 case FFA_MEM_RETRIEVE_REQ_32:
1608 case FFA_MEM_RETRIEVE_RESP_32:
1609 case FFA_MEM_RELINQUISH_32:
1610 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001611 case FFA_MSG_SEND_DIRECT_RESP_32:
1612 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001614 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001616 }
1617}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001618
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001619/**
1620 * Get target VM vCPU for direct messaging request.
1621 * If VM is UP then return first vCPU.
1622 * If VM is MP then return vCPU whose index matches current CPU index.
1623 */
1624static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1625 struct vm *vm, struct vcpu *current)
1626{
1627 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1628 struct vcpu *vcpu = NULL;
1629
1630 if (vm->vcpu_count == 1) {
1631 vcpu = vm_get_vcpu(vm, 0);
1632 } else if (current_cpu_index < vm->vcpu_count) {
1633 vcpu = vm_get_vcpu(vm, current_cpu_index);
1634 }
1635
1636 return vcpu;
1637}
1638
1639/**
1640 * Send an FF-A direct message request.
1641 */
1642struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1643 ffa_vm_id_t receiver_vm_id,
1644 struct ffa_value args,
1645 struct vcpu *current,
1646 struct vcpu **next)
1647{
1648 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001649 struct vm *receiver_vm;
1650 struct vcpu *receiver_vcpu;
1651 struct two_vcpu_locked vcpus_locked;
1652
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001653 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1654 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001655 return ffa_error(FFA_NOT_SUPPORTED);
1656 }
1657
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001658 receiver_vm = vm_find(receiver_vm_id);
1659 if (receiver_vm == NULL) {
1660 return ffa_error(FFA_INVALID_PARAMETERS);
1661 }
1662
1663 /*
1664 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1665 * UP (migratable) or MP partitions with a number of vCPUs matching the
1666 * number of PEs in the system. It further states that MP partitions
1667 * accepting direct request messages cannot migrate.
1668 */
1669 receiver_vcpu =
1670 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1671 if (receiver_vcpu == NULL) {
1672 return ffa_error(FFA_INVALID_PARAMETERS);
1673 }
1674
1675 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1676
1677 /*
1678 * If destination vCPU is executing or already received an
1679 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1680 * busy. There is a brief period of time where the vCPU state has
1681 * changed but regs_available is still false thus consider this case as
1682 * the vCPU not yet ready to receive a direct message request.
1683 */
1684 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1685 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1686 !receiver_vcpu->regs_available) {
1687 ret = ffa_error(FFA_BUSY);
1688 goto out;
1689 }
1690
1691 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1692 memory_order_relaxed)) {
1693 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001694 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001695 receiver_vcpu->vm->id,
1696 vcpu_index(receiver_vcpu));
1697 receiver_vcpu->state = VCPU_STATE_ABORTED;
1698 }
1699
1700 ret = ffa_error(FFA_ABORTED);
1701 goto out;
1702 }
1703
1704 switch (receiver_vcpu->state) {
1705 case VCPU_STATE_OFF:
1706 case VCPU_STATE_RUNNING:
1707 case VCPU_STATE_ABORTED:
1708 case VCPU_STATE_READY:
1709 case VCPU_STATE_BLOCKED_INTERRUPT:
1710 ret = ffa_error(FFA_BUSY);
1711 goto out;
1712 case VCPU_STATE_BLOCKED_MAILBOX:
1713 /*
1714 * Expect target vCPU to be blocked after having called
1715 * ffa_msg_wait or sent a direct message response.
1716 */
1717 break;
1718 }
1719
1720 /* Inject timer interrupt if any pending */
1721 if (arch_timer_pending(&receiver_vcpu->regs)) {
1722 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1723 HF_VIRTUAL_TIMER_INTID,
1724 current, NULL);
1725
1726 arch_timer_mask(&receiver_vcpu->regs);
1727 }
1728
1729 /* The receiver vCPU runs upon direct message invocation */
1730 receiver_vcpu->cpu = current->cpu;
1731 receiver_vcpu->state = VCPU_STATE_RUNNING;
1732 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001733 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001734
1735 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1736 .func = args.func,
1737 .arg1 = args.arg1,
1738 .arg2 = 0,
1739 .arg3 = args.arg3,
1740 .arg4 = args.arg4,
1741 .arg5 = args.arg5,
1742 .arg6 = args.arg6,
1743 .arg7 = args.arg7,
1744 });
1745
1746 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1747
1748 /* Switch to receiver vCPU targeted to by direct msg request */
1749 *next = receiver_vcpu;
1750
1751 /*
1752 * Since this flow will lead to a VM switch, the return value will not
1753 * be applied to current vCPU.
1754 */
1755
1756out:
1757 sl_unlock(&receiver_vcpu->lock);
1758 sl_unlock(&current->lock);
1759
1760 return ret;
1761}
1762
1763/**
1764 * Send an FF-A direct message response.
1765 */
1766struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1767 ffa_vm_id_t receiver_vm_id,
1768 struct ffa_value args,
1769 struct vcpu *current,
1770 struct vcpu **next)
1771{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001772 struct vcpu_locked current_locked;
J-Alvesfe7f7372020-11-09 11:32:12 +00001773 struct ffa_value to_ret = {
1774 .func = args.func,
1775 .arg1 = args.arg1,
1776 .arg2 = 0,
1777 .arg3 = args.arg3,
1778 .arg4 = args.arg4,
1779 .arg5 = args.arg5,
1780 .arg6 = args.arg6,
1781 .arg7 = args.arg7,
1782 };
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001783
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001784 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1785 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001786 return ffa_error(FFA_NOT_SUPPORTED);
1787 }
1788
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001789 current_locked = vcpu_lock(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001790
1791 /*
1792 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1793 * defined originator.
1794 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001795 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001796 /*
1797 * Sending direct response but direct request origin vCPU is
1798 * not set.
1799 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001800 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001801 return ffa_error(FFA_DENIED);
1802 }
1803
1804 if (current->direct_request_origin_vm_id != receiver_vm_id) {
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001805 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001806 return ffa_error(FFA_DENIED);
1807 }
1808
1809 /* Clear direct request origin for the caller. */
1810 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1811
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001812 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001813
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001814 if (!vm_id_is_current_world(receiver_vm_id)) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001815 *next = api_switch_to_other_world(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001816 VCPU_STATE_BLOCKED_MAILBOX);
1817 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001818 *next = api_switch_to_primary(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001819 VCPU_STATE_BLOCKED_MAILBOX);
J-Alvesfe7f7372020-11-09 11:32:12 +00001820 } else if (vm_id_is_current_world(receiver_vm_id)) {
1821 /*
1822 * It is expected the receiver_vm_id to be from an SP, otherwise
1823 * 'arch_other_world_is_direct_response_valid' should have
1824 * made function return error before getting to this point.
1825 */
1826 *next = api_switch_to_vm(current, to_ret,
1827 VCPU_STATE_BLOCKED_MAILBOX,
1828 receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001829 } else {
1830 panic("Invalid direct message response invocation");
1831 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001832
1833 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1834}
1835
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001836struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1837 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001838 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001839{
1840 struct vm *from = current->vm;
1841 struct vm *to;
1842 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001843 struct ffa_memory_region *memory_region;
1844 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001845
1846 if (ipa_addr(address) != 0 || page_count != 0) {
1847 /*
1848 * Hafnium only supports passing the descriptor in the TX
1849 * mailbox.
1850 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001851 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001852 }
1853
Andrew Walbranca808b12020-05-15 17:22:28 +01001854 if (fragment_length > length) {
1855 dlog_verbose(
1856 "Fragment length %d greater than total length %d.\n",
1857 fragment_length, length);
1858 return ffa_error(FFA_INVALID_PARAMETERS);
1859 }
1860 if (fragment_length < sizeof(struct ffa_memory_region) +
1861 sizeof(struct ffa_memory_access)) {
1862 dlog_verbose(
1863 "Initial fragment length %d smaller than header size "
1864 "%d.\n",
1865 fragment_length,
1866 sizeof(struct ffa_memory_region) +
1867 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001868 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001869 }
1870
1871 /*
1872 * Check that the sender has configured its send buffer. If the TX
1873 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1874 * be safely accessed after releasing the lock since the TX mailbox
1875 * address can only be configured once.
1876 */
1877 sl_lock(&from->lock);
1878 from_msg = from->mailbox.send;
1879 sl_unlock(&from->lock);
1880
1881 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001882 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001883 }
1884
1885 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001886 * Copy the memory region descriptor to a fresh page from the memory
1887 * pool. This prevents the sender from changing it underneath us, and
1888 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001889 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001890 if (fragment_length > HF_MAILBOX_SIZE ||
1891 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001892 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001893 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001894 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001895 if (memory_region == NULL) {
1896 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001897 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001898 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001899 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001900
1901 /* The sender must match the caller. */
1902 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001903 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001904 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001905 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001906 }
1907
Andrew Walbrana65a1322020-04-06 19:32:32 +01001908 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001909 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001910 dlog_verbose(
1911 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001912 "endpoint memory access descriptors, expected 1).\n",
1913 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001914 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001915 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001916 }
1917
1918 /*
1919 * Ensure that the receiver VM exists and isn't the same as the sender.
1920 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001921 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001922 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001923 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001924 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001925 goto out;
1926 }
1927
1928 if (to->id == HF_TEE_VM_ID) {
1929 /*
1930 * The 'to' VM lock is only needed in the case that it is the
1931 * TEE VM.
1932 */
1933 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1934
1935 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001936 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001937 goto out_unlock;
1938 }
1939
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001940 ret = ffa_memory_tee_send(
1941 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1942 length, fragment_length, share_func, &api_page_pool);
1943 /*
1944 * ffa_tee_memory_send takes ownership of the memory_region, so
1945 * make sure we don't free it.
1946 */
1947 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001948
1949 out_unlock:
1950 vm_unlock(&vm_to_from_lock.vm1);
1951 vm_unlock(&vm_to_from_lock.vm2);
1952 } else {
1953 struct vm_locked from_locked = vm_lock(from);
1954
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001955 ret = ffa_memory_send(from_locked, memory_region, length,
1956 fragment_length, share_func,
1957 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001958 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001960 * make sure we don't free it.
1961 */
1962 memory_region = NULL;
1963
1964 vm_unlock(&from_locked);
1965 }
1966
1967out:
1968 if (memory_region != NULL) {
1969 mpool_free(&api_page_pool, memory_region);
1970 }
1971
1972 return ret;
1973}
1974
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001975struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1976 uint32_t fragment_length,
1977 ipaddr_t address, uint32_t page_count,
1978 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001979{
1980 struct vm *to = current->vm;
1981 struct vm_locked to_locked;
1982 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001983 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001984 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001985 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001986
1987 if (ipa_addr(address) != 0 || page_count != 0) {
1988 /*
1989 * Hafnium only supports passing the descriptor in the TX
1990 * mailbox.
1991 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001992 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001993 }
1994
Andrew Walbrana65a1322020-04-06 19:32:32 +01001995 if (fragment_length != length) {
1996 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001997 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001998 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001999
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002000 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002001 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002002 message_buffer_size = cpu_get_buffer_size(current->cpu);
2003 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2004 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002005 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002006 }
2007
2008 to_locked = vm_lock(to);
2009 to_msg = to->mailbox.send;
2010
2011 if (to_msg == NULL) {
2012 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002013 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002014 goto out;
2015 }
2016
2017 /*
2018 * Copy the retrieve request descriptor to an internal buffer, so that
2019 * the caller can't change it underneath us.
2020 */
2021 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2022
2023 if (msg_receiver_busy(to_locked, NULL, false)) {
2024 /*
2025 * Can't retrieve memory information if the mailbox is not
2026 * available.
2027 */
2028 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002029 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002030 goto out;
2031 }
2032
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002033 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2034 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002035
2036out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002037 vm_unlock(&to_locked);
2038 return ret;
2039}
2040
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002041struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002042{
2043 struct vm *from = current->vm;
2044 struct vm_locked from_locked;
2045 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002046 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002047 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002048 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002049 uint32_t length;
2050
2051 from_locked = vm_lock(from);
2052 from_msg = from->mailbox.send;
2053
2054 if (from_msg == NULL) {
2055 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002056 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002057 goto out;
2058 }
2059
2060 /*
2061 * Calculate length from relinquish descriptor before copying. We will
2062 * check again later to make sure it hasn't changed.
2063 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002064 length = sizeof(struct ffa_mem_relinquish) +
2065 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2066 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002067 /*
2068 * Copy the relinquish descriptor to an internal buffer, so that the
2069 * caller can't change it underneath us.
2070 */
2071 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002072 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002073 message_buffer_size = cpu_get_buffer_size(current->cpu);
2074 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2075 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002076 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002077 goto out;
2078 }
2079 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2080
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002081 if (sizeof(struct ffa_mem_relinquish) +
2082 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002083 length) {
2084 dlog_verbose(
2085 "Endpoint count changed while copying to internal "
2086 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002087 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002088 goto out;
2089 }
2090
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002091 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2092 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002093
2094out:
2095 vm_unlock(&from_locked);
2096 return ret;
2097}
2098
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002099struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2100 ffa_memory_region_flags_t flags,
2101 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002102{
2103 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002104 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002105
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002106 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2107 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002108 struct vm_locked to_locked = vm_lock(to);
2109
Andrew Walbranca808b12020-05-15 17:22:28 +01002110 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002112
Andrew Walbran290b0c92020-02-03 16:37:14 +00002113 vm_unlock(&to_locked);
2114 } else {
2115 struct vm *from = vm_find(HF_TEE_VM_ID);
2116 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2117
Andrew Walbranca808b12020-05-15 17:22:28 +01002118 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2119 vm_to_from_lock.vm2, handle, flags,
2120 &api_page_pool);
2121
2122 vm_unlock(&vm_to_from_lock.vm1);
2123 vm_unlock(&vm_to_from_lock.vm2);
2124 }
2125
2126 return ret;
2127}
2128
2129struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2130 uint32_t fragment_offset,
2131 ffa_vm_id_t sender_vm_id,
2132 struct vcpu *current)
2133{
2134 struct vm *to = current->vm;
2135 struct vm_locked to_locked;
2136 struct ffa_value ret;
2137
2138 /* Sender ID MBZ at virtual instance. */
2139 if (sender_vm_id != 0) {
2140 return ffa_error(FFA_INVALID_PARAMETERS);
2141 }
2142
2143 to_locked = vm_lock(to);
2144
2145 if (msg_receiver_busy(to_locked, NULL, false)) {
2146 /*
2147 * Can't retrieve memory information if the mailbox is not
2148 * available.
2149 */
2150 dlog_verbose("RX buffer not ready.\n");
2151 ret = ffa_error(FFA_BUSY);
2152 goto out;
2153 }
2154
2155 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2156 &api_page_pool);
2157
2158out:
2159 vm_unlock(&to_locked);
2160 return ret;
2161}
2162
2163struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2164 uint32_t fragment_length,
2165 ffa_vm_id_t sender_vm_id,
2166 struct vcpu *current)
2167{
2168 struct vm *from = current->vm;
2169 const void *from_msg;
2170 void *fragment_copy;
2171 struct ffa_value ret;
2172
2173 /* Sender ID MBZ at virtual instance. */
2174 if (sender_vm_id != 0) {
2175 return ffa_error(FFA_INVALID_PARAMETERS);
2176 }
2177
2178 /*
2179 * Check that the sender has configured its send buffer. If the TX
2180 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2181 * be safely accessed after releasing the lock since the TX mailbox
2182 * address can only be configured once.
2183 */
2184 sl_lock(&from->lock);
2185 from_msg = from->mailbox.send;
2186 sl_unlock(&from->lock);
2187
2188 if (from_msg == NULL) {
2189 return ffa_error(FFA_INVALID_PARAMETERS);
2190 }
2191
2192 /*
2193 * Copy the fragment to a fresh page from the memory pool. This prevents
2194 * the sender from changing it underneath us, and also lets us keep it
2195 * around in the share state table if needed.
2196 */
2197 if (fragment_length > HF_MAILBOX_SIZE ||
2198 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2199 dlog_verbose(
2200 "Fragment length %d larger than mailbox size %d.\n",
2201 fragment_length, HF_MAILBOX_SIZE);
2202 return ffa_error(FFA_INVALID_PARAMETERS);
2203 }
2204 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2205 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2206 0) {
2207 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2208 return ffa_error(FFA_INVALID_PARAMETERS);
2209 }
2210 fragment_copy = mpool_alloc(&api_page_pool);
2211 if (fragment_copy == NULL) {
2212 dlog_verbose("Failed to allocate fragment copy.\n");
2213 return ffa_error(FFA_NO_MEMORY);
2214 }
2215 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2216
2217 /*
2218 * Hafnium doesn't support fragmentation of memory retrieve requests
2219 * (because it doesn't support caller-specified mappings, so a request
2220 * will never be larger than a single page), so this must be part of a
2221 * memory send (i.e. donate, lend or share) request.
2222 *
2223 * We can tell from the handle whether the memory transaction is for the
2224 * TEE or not.
2225 */
2226 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2227 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2228 struct vm_locked from_locked = vm_lock(from);
2229
2230 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2231 fragment_length, handle,
2232 &api_page_pool);
2233 /*
2234 * `ffa_memory_send_continue` takes ownership of the
2235 * fragment_copy, so we don't need to free it here.
2236 */
2237 vm_unlock(&from_locked);
2238 } else {
2239 struct vm *to = vm_find(HF_TEE_VM_ID);
2240 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2241
2242 /*
2243 * The TEE RX buffer state is checked in
2244 * `ffa_memory_tee_send_continue` rather than here, as we need
2245 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2246 * than FFA_ERROR FFA_BUSY in case it is busy.
2247 */
2248
2249 ret = ffa_memory_tee_send_continue(
2250 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2251 fragment_length, handle, &api_page_pool);
2252 /*
2253 * `ffa_memory_tee_send_continue` takes ownership of the
2254 * fragment_copy, so we don't need to free it here.
2255 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002256
2257 vm_unlock(&vm_to_from_lock.vm1);
2258 vm_unlock(&vm_to_from_lock.vm2);
2259 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002260
2261 return ret;
2262}