blob: 180bb1ba2b37d52684ef6af399fa9f7932e45039 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scull18c78fc2018-08-20 12:57:41 +01009#include "hf/api.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010010
Andrew Walbran318f5732018-11-20 16:23:42 +000011#include "hf/arch/cpu.h"
Andrew Walbran2619e0a2020-01-10 16:37:50 +000012#include "hf/arch/tee.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000013#include "hf/arch/timer.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000014
Andrew Scull877ae4b2019-07-02 12:52:33 +010015#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000016#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "hf/ffa_internal.h"
18#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000019#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010020#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000021#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010022#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010023#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/vm.h"
25
Andrew Scullf35a5c92018-08-07 18:09:46 +010026#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010027#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010028
Fuad Tabbae4efcc32020-07-16 15:37:27 +010029static_assert(sizeof(struct ffa_partition_info) == 8,
30 "Partition information descriptor size doesn't match the one in "
31 "the FF-A 1.0 EAC specification, Table 82.");
32
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000033/*
34 * To eliminate the risk of deadlocks, we define a partial order for the
35 * acquisition of locks held concurrently by the same physical CPU. Our current
36 * ordering requirements are as follows:
37 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010038 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000039 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010040 * Locks of the same kind require the lock of lowest address to be locked first,
41 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000042 */
43
Andrew Scullaa039b32018-10-04 15:02:26 +010044static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010045 "Currently, a page is mapped for the send and receive buffers so "
46 "the maximum request is the size of a page.");
47
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000048static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
49 "The page pool entry size must be at least as big as the mailbox "
50 "size, so that memory region descriptors can be copied from the "
51 "mailbox for memory sharing.");
52
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000053static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000054
55/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000056 * Initialises the API page pool by taking ownership of the contents of the
57 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000058 */
59void api_init(struct mpool *ppool)
60{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000061 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000062}
63
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010064/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000065 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +010066 *
67 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010068 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010069 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +010070static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010071 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000072 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010073{
Andrew Walbran42347a92019-05-09 13:59:03 +010074 struct vm *primary = vm_find(HF_PRIMARY_VM_ID);
Andrew Walbrane1310df2019-04-29 17:28:28 +010075 struct vcpu *next = vm_get_vcpu(primary, cpu_index(current->cpu));
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +010076
Andrew Walbran508e63c2018-12-20 17:02:37 +000077 /*
78 * If the secondary is blocked but has a timer running, sleep until the
79 * timer fires rather than indefinitely.
80 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010081 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
83 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010084 if (arch_timer_enabled_current()) {
85 uint64_t remaining_ns =
86 arch_timer_remaining_ns_current();
87
88 if (remaining_ns == 0) {
89 /*
90 * Timer is pending, so the current vCPU should
91 * be run again right away.
92 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +010094 /*
95 * primary_ret.arg1 should already be set to the
96 * current VM ID and vCPU ID.
97 */
98 primary_ret.arg2 = 0;
99 } else {
100 primary_ret.arg2 = remaining_ns;
101 }
102 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100103 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100104 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000105 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100106 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000107
108 default:
109 /* Do nothing. */
110 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000111 }
112
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100113 /* Set the return value for the primary VM's call to HF_VCPU_RUN. */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100114 arch_regs_set_retval(&next->regs, primary_ret);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100115
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000116 /* Mark the current vCPU as waiting. */
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000117 sl_lock(&current->lock);
118 current->state = secondary_state;
119 sl_unlock(&current->lock);
120
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100121 return next;
122}
123
124/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100125 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
126 * registers the `from` VM to be notified when it becomes available.
127 */
128static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
129{
130 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
131 to.vm->mailbox.recv == NULL) {
132 /*
133 * Fail if the receiver isn't currently ready to receive data,
134 * setting up for notification if requested.
135 */
136 if (notify) {
137 struct wait_entry *entry =
138 vm_get_wait_entry(from, to.vm->id);
139
140 /* Append waiter only if it's not there yet. */
141 if (list_empty(&entry->wait_links)) {
142 list_append(&to.vm->mailbox.waiter_list,
143 &entry->wait_links);
144 }
145 }
146
147 return true;
148 }
149
150 return false;
151}
152
153/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000154 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000155 */
156struct vcpu *api_preempt(struct vcpu *current)
157{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158 struct ffa_value ret = {
159 .func = FFA_INTERRUPT_32,
160 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000161 };
162
Andrew Sculld6ee1102019-04-05 22:12:42 +0100163 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000164}
165
166/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000167 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000168 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100169 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100170struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100171{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100172 struct ffa_value ret = {
173 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
174 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100175 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000176
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000177 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100178 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100179}
180
181/**
Andrew Walbran33645652019-04-15 12:29:31 +0100182 * Puts the current vCPU in off mode, and returns to the primary VM.
183 */
184struct vcpu *api_vcpu_off(struct vcpu *current)
185{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100186 struct ffa_value ret = {
187 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
188 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100189 };
190
191 /*
192 * Disable the timer, so the scheduler doesn't get told to call back
193 * based on it.
194 */
195 arch_timer_disable_current();
196
197 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
198}
199
200/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000201 * Returns to the primary VM to allow this CPU to be used for other tasks as the
202 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100203 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000204 */
Andrew Walbran16075b62019-09-03 17:11:07 +0100205void api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000206{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100207 struct ffa_value primary_ret = {
208 .func = FFA_YIELD_32,
209 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull66d62bf2019-02-01 13:54:10 +0000210 };
211
212 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000213 /* NOOP on the primary as it makes the scheduling decisions. */
Andrew Walbran16075b62019-09-03 17:11:07 +0100214 return;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000215 }
216
Andrew Walbran16075b62019-09-03 17:11:07 +0100217 *next = api_switch_to_primary(current, primary_ret, VCPU_STATE_READY);
Andrew Scull66d62bf2019-02-01 13:54:10 +0000218}
219
220/**
Andrew Walbran33645652019-04-15 12:29:31 +0100221 * Switches to the primary so that it can switch to the target, or kick it if it
222 * is already running on a different physical CPU.
223 */
224struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
225{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100226 struct ffa_value ret = {
227 .func = HF_FFA_RUN_WAKE_UP,
228 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
229 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100230 };
231 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
232}
233
234/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000235 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000236 */
237struct vcpu *api_abort(struct vcpu *current)
238{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100239 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000240
Andrew Walbran17eebf92020-02-05 16:35:49 +0000241 dlog_notice("Aborting VM %u vCPU %u\n", current->vm->id,
242 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000243
244 if (current->vm->id == HF_PRIMARY_VM_ID) {
245 /* TODO: what to do when the primary aborts? */
246 for (;;) {
247 /* Do nothing. */
248 }
249 }
250
251 atomic_store_explicit(&current->vm->aborting, true,
252 memory_order_relaxed);
253
254 /* TODO: free resources once all vCPUs abort. */
255
Andrew Sculld6ee1102019-04-05 22:12:42 +0100256 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000257}
258
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100259struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
260 const struct ffa_uuid *uuid)
261{
262 struct vm *current_vm = current->vm;
263 struct vm_locked current_vm_locked;
264 ffa_vm_count_t vm_count = 0;
265 bool uuid_is_null = ffa_uuid_is_null(uuid);
266 struct ffa_value ret;
267 uint32_t size;
268 struct ffa_partition_info partitions[MAX_VMS];
269
270 /*
271 * Iterate through the VMs to find the ones with a matching UUID.
272 * A Null UUID retrieves information for all VMs.
273 */
274 for (uint16_t index = 0; index < vm_get_count(); ++index) {
275 const struct vm *vm = vm_find_index(index);
276
277 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
278 partitions[vm_count].vm_id = vm->id;
279 partitions[vm_count].vcpu_count = vm->vcpu_count;
280
281 /* Hafnium only supports indirect messaging. */
282 partitions[vm_count].properties =
283 FFA_PARTITION_INDIRECT_MSG;
284
285 ++vm_count;
286 }
287 }
288
289 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
290 if (vm_count == 0) {
291 return ffa_error(FFA_INVALID_PARAMETERS);
292 }
293
294 size = vm_count * sizeof(partitions[0]);
295 if (size > FFA_MSG_PAYLOAD_MAX) {
296 dlog_error(
297 "Partition information does not fit in the VM's RX "
298 "buffer.\n");
299 return ffa_error(FFA_NO_MEMORY);
300 }
301
302 /*
303 * Partition information is returned in the VM's RX buffer, which is why
304 * the lock is needed.
305 */
306 current_vm_locked = vm_lock(current_vm);
307
308 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
309 /*
310 * Can't retrieve memory information if the mailbox is not
311 * available.
312 */
313 dlog_verbose("RX buffer not ready.\n");
314 ret = ffa_error(FFA_BUSY);
315 goto out_unlock;
316 }
317
318 /* Populate the VM's RX buffer with the partition information. */
319 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
320 size);
321 current_vm->mailbox.recv_size = size;
322 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
323 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
324 current_vm->mailbox.state = MAILBOX_STATE_READ;
325
326 /* Return the count of partition information descriptors in w2. */
327 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
328
329out_unlock:
330 vm_unlock(&current_vm_locked);
331
332 return ret;
333}
334
Andrew Scull9726c252019-01-23 13:44:19 +0000335/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000336 * Returns the ID of the VM.
337 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000339{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100340 return (struct ffa_value){.func = FFA_SUCCESS_32,
341 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000342}
343
344/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000345 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000346 * function to indicate that register state for the given vCPU has been saved
347 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000348 */
349void api_regs_state_saved(struct vcpu *vcpu)
350{
351 sl_lock(&vcpu->lock);
352 vcpu->regs_available = true;
353 sl_unlock(&vcpu->lock);
354}
355
356/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000357 * Retrieves the next waiter and removes it from the wait list if the VM's
358 * mailbox is in a writable state.
359 */
360static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
361{
362 struct wait_entry *entry;
363 struct vm *vm = locked_vm.vm;
364
Andrew Sculld6ee1102019-04-05 22:12:42 +0100365 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000366 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
367 /* The mailbox is not writable or there are no waiters. */
368 return NULL;
369 }
370
371 /* Remove waiter from the wait list. */
372 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
373 wait_links);
374 list_remove(&entry->wait_links);
375 return entry;
376}
377
378/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000379 * Assuming that the arguments have already been checked by the caller, injects
380 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
381 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
382 * is next run, which is up to the scheduler.
383 *
384 * Returns:
385 * - 0 on success if no further action is needed.
386 * - 1 if it was called by the primary VM and the primary VM now needs to wake
387 * up or kick the target vCPU.
388 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200389static int64_t internal_interrupt_inject_locked(
390 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
391 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000392{
393 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100394 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000395 int64_t ret = 0;
396
Andrew Walbran508e63c2018-12-20 17:02:37 +0000397 /*
398 * We only need to change state and (maybe) trigger a virtual IRQ if it
399 * is enabled and was not previously pending. Otherwise we can skip
400 * everything except setting the pending bit.
401 *
402 * If you change this logic make sure to update the need_vm_lock logic
403 * above to match.
404 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200405 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
406 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000407 intid_mask)) {
408 goto out;
409 }
410
411 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200412 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000413
414 /*
415 * Only need to update state if there was not already an
416 * interrupt enabled and pending.
417 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200418 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000419 goto out;
420 }
421
Andrew Walbran508e63c2018-12-20 17:02:37 +0000422 if (current->vm->id == HF_PRIMARY_VM_ID) {
423 /*
424 * If the call came from the primary VM, let it know that it
425 * should run or kick the target vCPU.
426 */
427 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200428 } else if (current != target_locked.vcpu && next != NULL) {
429 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000430 }
431
432out:
433 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200434 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
435 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000436
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200437 return ret;
438}
439
440/* Wrapper to internal_interrupt_inject with locking of target vCPU */
441static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
442 uint32_t intid, struct vcpu *current,
443 struct vcpu **next)
444{
445 int64_t ret;
446 struct vcpu_locked target_locked;
447
448 target_locked = vcpu_lock(target_vcpu);
449 ret = internal_interrupt_inject_locked(target_locked, intid, current,
450 next);
451 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000452
453 return ret;
454}
455
456/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100457 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
458 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100459 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100460static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100461{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000462 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100463 case FFA_MSG_SEND_32:
464 return (struct ffa_value){
465 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000466 .arg1 = (receiver->mailbox.recv_sender << 16) |
467 receiver->id,
468 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000469 default:
470 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000471 dlog_error("Tried to return an invalid message function %#x\n",
472 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100473 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000474 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100475}
476
477/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000478 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000479 * value needs to be forced onto the vCPU.
480 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000481static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100482 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000483{
Andrew Scullb06d1752019-02-04 10:15:48 +0000484 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000485 bool ret;
486
Andrew Scullb06d1752019-02-04 10:15:48 +0000487 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000488 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000489 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100490 * The VM lock is not needed in the common case so it must only be taken
491 * when it is going to be needed. This ensures there are no inter-vCPU
492 * dependencies in the common run case meaning the sensitive context
493 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000494 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000495 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000496
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000497 /* The VM needs to be locked to deliver mailbox messages. */
498 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
499 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000500 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000501 sl_lock(&vcpu->vm->lock);
502 sl_lock(&vcpu->lock);
503 }
504
505 /*
506 * If the vCPU is already running somewhere then we can't run it here
507 * simultaneously. While it is actually running then the state should be
508 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
509 * stops running but while Hafnium is in the process of switching back
510 * to the primary there will be a brief period while the state has been
511 * updated but `regs_available` is still false (until
512 * `api_regs_state_saved` is called). We can't start running it again
513 * until this has finished, so count this state as still running for the
514 * purposes of this check.
515 */
516 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
517 /*
518 * vCPU is running on another pCPU.
519 *
520 * It's okay not to return the sleep duration here because the
521 * other physical CPU that is currently running this vCPU will
522 * return the sleep duration if needed.
523 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000525 ret = false;
526 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000527 }
Andrew Scull9726c252019-01-23 13:44:19 +0000528
529 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100530 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000531 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
532 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100533 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000534 }
535 ret = false;
536 goto out;
537 }
538
Andrew Walbran508e63c2018-12-20 17:02:37 +0000539 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100540 case VCPU_STATE_RUNNING:
541 case VCPU_STATE_OFF:
542 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000543 ret = false;
544 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000545
Andrew Sculld6ee1102019-04-05 22:12:42 +0100546 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000547 /*
548 * A pending message allows the vCPU to run so the message can
549 * be delivered directly.
550 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100551 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100552 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100553 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100554 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000555 break;
556 }
557 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100558 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000559 /* Allow virtual interrupts to be delivered. */
560 if (vcpu->interrupts.enabled_and_pending_count > 0) {
561 break;
562 }
563
Andrew Walbran508e63c2018-12-20 17:02:37 +0000564 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000565 uint64_t timer_remaining_ns =
566 arch_timer_remaining_ns(&vcpu->regs);
567
568 /*
569 * The timer expired so allow the interrupt to be
570 * delivered.
571 */
572 if (timer_remaining_ns == 0) {
573 break;
574 }
575
576 /*
577 * The vCPU is not ready to run, return the appropriate
578 * code to the primary which called vcpu_run.
579 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100580 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100581 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100582 ? FFA_MSG_WAIT_32
583 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000584 run_ret->arg1 =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000586 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000587 }
588
589 ret = false;
590 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000591
Andrew Sculld6ee1102019-04-05 22:12:42 +0100592 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000593 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000594 }
595
Andrew Scullb06d1752019-02-04 10:15:48 +0000596 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000597 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100598 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000599
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000600 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000601 * Mark the registers as unavailable now that we're about to reflect
602 * them onto the real registers. This will also prevent another physical
603 * CPU from trying to read these registers.
604 */
605 vcpu->regs_available = false;
606
607 ret = true;
608
609out:
610 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000611 if (need_vm_lock) {
612 sl_unlock(&vcpu->vm->lock);
613 }
614
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000615 return ret;
616}
617
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
619 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100620{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100621 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100622 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100623 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100624
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000625 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100626 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100628 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100629 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100630
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000631 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100632 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100633 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100634 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100635
Andrew Scull19503262018-09-20 14:48:39 +0100636 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100637 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100638 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100639 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100640 }
641
Fuad Tabbaed294af2019-12-20 10:43:01 +0000642 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100643 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100644 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100645 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100646
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000647 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100648 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000649 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000650 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100651 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000652
Andrew Walbran508e63c2018-12-20 17:02:37 +0000653 /*
654 * Inject timer interrupt if timer has expired. It's safe to access
655 * vcpu->regs here because api_vcpu_prepare_run already made sure that
656 * regs_available was true (and then set it to false) before returning
657 * true.
658 */
659 if (arch_timer_pending(&vcpu->regs)) {
660 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100661 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
662 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000663
664 /*
665 * Set the mask bit so the hardware interrupt doesn't fire
666 * again. Ideally we wouldn't do this because it affects what
667 * the secondary vCPU sees, but if we don't then we end up with
668 * a loop of the interrupt firing each time we try to return to
669 * the secondary vCPU.
670 */
671 arch_timer_mask(&vcpu->regs);
672 }
673
Fuad Tabbaed294af2019-12-20 10:43:01 +0000674 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000675 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000676
Andrew Scull33fecd32019-01-08 14:48:27 +0000677 /*
678 * Set a placeholder return code to the scheduler. This will be
679 * overwritten when the switch back to the primary occurs.
680 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681 ret.func = FFA_INTERRUPT_32;
682 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100683 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000684
Andrew Scull6d2db332018-10-10 15:28:17 +0100685out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100686 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100687}
688
689/**
Andrew Scull81e85092018-12-12 12:56:20 +0000690 * Check that the mode indicates memory that is valid, owned and exclusive.
691 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100692static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000693{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100694 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
695 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000696}
697
698/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100699 * Determines the value to be returned by api_vm_configure and ffa_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000700 * after they've succeeded. If a secondary VM is running and there are waiters,
701 * it also switches back to the primary VM for it to wake waiters up.
702 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
704 struct vcpu *current,
705 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000706{
707 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000708
709 if (list_empty(&vm->mailbox.waiter_list)) {
710 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000712 }
713
714 if (vm->id == HF_PRIMARY_VM_ID) {
715 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000717 }
718
719 /*
720 * Switch back to the primary VM, informing it that there are waiters
721 * that need to be notified.
722 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000723 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100724 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000725 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000726
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100727 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000728}
729
730/**
Andrew Sculle1322792019-07-01 17:46:10 +0100731 * Configures the hypervisor's stage-1 view of the send and receive pages. The
732 * stage-1 page tables must be locked so memory cannot be taken by another core
733 * which could result in this transaction being unable to roll back in the case
734 * of an error.
735 */
736static bool api_vm_configure_stage1(struct vm_locked vm_locked,
737 paddr_t pa_send_begin, paddr_t pa_send_end,
738 paddr_t pa_recv_begin, paddr_t pa_recv_end,
739 struct mpool *local_page_pool)
740{
741 bool ret;
742 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
743
744 /* Map the send page as read-only in the hypervisor address space. */
745 vm_locked.vm->mailbox.send =
746 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
747 MM_MODE_R, local_page_pool);
748 if (!vm_locked.vm->mailbox.send) {
749 /* TODO: partial defrag of failed range. */
750 /* Recover any memory consumed in failed mapping. */
751 mm_defrag(mm_stage1_locked, local_page_pool);
752 goto fail;
753 }
754
755 /*
756 * Map the receive page as writable in the hypervisor address space. On
757 * failure, unmap the send page before returning.
758 */
759 vm_locked.vm->mailbox.recv =
760 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
761 MM_MODE_W, local_page_pool);
762 if (!vm_locked.vm->mailbox.recv) {
763 /* TODO: partial defrag of failed range. */
764 /* Recover any memory consumed in failed mapping. */
765 mm_defrag(mm_stage1_locked, local_page_pool);
766 goto fail_undo_send;
767 }
768
769 ret = true;
770 goto out;
771
772 /*
773 * The following mappings will not require more memory than is available
774 * in the local pool.
775 */
776fail_undo_send:
777 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100778 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
779 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100780
781fail:
782 ret = false;
783
784out:
785 mm_unlock_stage1(&mm_stage1_locked);
786
787 return ret;
788}
789
790/**
791 * Configures the send and receive pages in the VM stage-2 and hypervisor
792 * stage-1 page tables. Locking of the page tables combined with a local memory
793 * pool ensures there will always be enough memory to recover from any errors
794 * that arise.
795 */
796static bool api_vm_configure_pages(struct vm_locked vm_locked,
797 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100798 uint32_t orig_send_mode,
799 paddr_t pa_recv_begin, paddr_t pa_recv_end,
800 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100801{
802 bool ret;
803 struct mpool local_page_pool;
804
805 /*
806 * Create a local pool so any freed memory can't be used by another
807 * thread. This is to ensure the original mapping can be restored if any
808 * stage of the process fails.
809 */
810 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
811
812 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000813 if (!vm_identity_map(
814 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100815 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000816 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100817 goto fail;
818 }
819
Andrew Scull3c257452019-11-26 13:32:50 +0000820 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
821 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
822 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100823 /* TODO: partial defrag of failed range. */
824 /* Recover any memory consumed in failed mapping. */
825 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
826 goto fail_undo_send;
827 }
828
829 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
830 pa_recv_begin, pa_recv_end,
831 &local_page_pool)) {
832 goto fail_undo_send_and_recv;
833 }
834
835 ret = true;
836 goto out;
837
838 /*
839 * The following mappings will not require more memory than is available
840 * in the local pool.
841 */
842fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000843 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
844 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100845
846fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000847 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
848 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100849
850fail:
851 ret = false;
852
853out:
854 mpool_fini(&local_page_pool);
855
856 return ret;
857}
858
859/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100860 * Configures the VM to send/receive data through the specified pages. The pages
861 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000862 *
863 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000865 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000867 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000869 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100870 * - FFA_SUCCESS on success if no further action is needed.
871 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000872 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100873 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100874struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
875 uint32_t page_count, struct vcpu *current,
876 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100877{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100878 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100879 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100880 paddr_t pa_send_begin;
881 paddr_t pa_send_end;
882 paddr_t pa_recv_begin;
883 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100884 uint32_t orig_send_mode;
885 uint32_t orig_recv_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 struct ffa_value ret;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000887
888 /* Hafnium only supports a fixed size of RX/TX buffers. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100889 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
890 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000891 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100892
893 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000894 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
895 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100897 }
898
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000899 /* Convert to physical addresses. */
900 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000901 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000902
903 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000904 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000905
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100906 /* Fail if the same page is used for the send and receive pages. */
907 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000909 }
910
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100911 /*
912 * The hypervisor's memory map must be locked for the duration of this
913 * operation to ensure there will be sufficient memory to recover from
914 * any failures.
915 *
Fuad Tabba9dc276f2020-07-16 09:29:32 +0100916 * TODO: the scope can be reduced but will require restructuring to
917 * keep a single unlock point.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100918 */
Andrew Sculle1322792019-07-01 17:46:10 +0100919 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000920
921 /* We only allow these to be setup once. */
922 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000924 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000925 }
926
927 /*
928 * Ensure the pages are valid, owned and exclusive to the VM and that
929 * the VM has the required access to the memory.
930 */
931 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
932 &orig_send_mode) ||
933 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
934 (orig_send_mode & MM_MODE_R) == 0 ||
935 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000937 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000938 }
939
940 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
941 &orig_recv_mode) ||
942 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
943 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000945 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000946 }
947
Andrew Sculle1322792019-07-01 17:46:10 +0100948 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
949 orig_send_mode, pa_recv_begin, pa_recv_end,
950 orig_recv_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100951 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000952 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100953 }
954
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000955 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100956 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000957
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100958exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100959 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100960
961 return ret;
962}
963
964/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100965 * Notifies the `to` VM about the message currently in its mailbox, possibly
966 * with the help of the primary VM.
967 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100968static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
969 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100970{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100971 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
972 struct ffa_value primary_ret = {
973 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000974 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100975 };
976
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100977 /* Messages for the primary VM are delivered directly. */
978 if (to.vm->id == HF_PRIMARY_VM_ID) {
979 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000980 * Only tell the primary VM the size and other details if the
981 * message is for it, to avoid leaking data about messages for
982 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100983 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100984 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100985
986 to.vm->mailbox.state = MAILBOX_STATE_READ;
987 *next = api_switch_to_primary(current, primary_ret,
988 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000989 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100990 }
991
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000992 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
993
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000994 /* Messages for the TEE are sent on via the dispatcher. */
995 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +0000997
Andrew Walbran11cff3a2020-02-28 11:33:17 +0000998 ret = arch_tee_call(call);
999 /*
1000 * After the call to the TEE completes it must have finished
1001 * reading its RX buffer, so it is ready for another message.
1002 */
1003 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001004 /*
1005 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001006 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001007 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001008 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001009 }
1010
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001011 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001012 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001013 *next = api_switch_to_primary(current, primary_ret,
1014 VCPU_STATE_READY);
1015 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001016
1017 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001018}
1019
1020/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001021 * Copies data from the sender's send buffer to the recipient's receive buffer
1022 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001023 *
1024 * If the recipient's receive buffer is busy, it can optionally register the
1025 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001026 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1028 ffa_vm_id_t receiver_vm_id, uint32_t size,
1029 uint32_t attributes, struct vcpu *current,
1030 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001031{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001032 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001033 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001034 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001035 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 struct ffa_value ret;
1037 bool notify =
1038 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001039
Andrew Walbran70bc8622019-10-07 14:15:58 +01001040 /* Ensure sender VM ID corresponds to the current VM. */
1041 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001043 }
1044
1045 /* Disallow reflexive requests as this suggests an error in the VM. */
1046 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001048 }
1049
1050 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 if (size > FFA_MSG_PAYLOAD_MAX) {
1052 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001053 }
1054
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001055 /* Ensure the receiver VM exists. */
1056 to = vm_find(receiver_vm_id);
1057 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001058 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001059 }
1060
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001061 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001062 * Check that the sender has configured its send buffer. If the tx
1063 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1064 * be safely accessed after releasing the lock since the tx mailbox
1065 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001066 */
1067 sl_lock(&from->lock);
1068 from_msg = from->mailbox.send;
1069 sl_unlock(&from->lock);
1070
1071 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001073 }
1074
Andrew Walbran82d6d152019-12-24 15:02:06 +00001075 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001076
Andrew Walbran82d6d152019-12-24 15:02:06 +00001077 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001079 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001080 }
1081
Andrew Walbran82d6d152019-12-24 15:02:06 +00001082 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001084 to->mailbox.recv_size = size;
1085 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001087 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001088
1089out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001090 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001091
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001092 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001093}
1094
1095/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001096 * Checks whether the vCPU's attempt to block for a message has already been
1097 * interrupted or whether it is allowed to block.
1098 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001099bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001100{
1101 bool interrupted;
1102
1103 sl_lock(&current->lock);
1104
1105 /*
1106 * Don't block if there are enabled and pending interrupts, to match
1107 * behaviour of wait_for_interrupt.
1108 */
1109 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1110
1111 sl_unlock(&current->lock);
1112
1113 return interrupted;
1114}
1115
1116/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001117 * Receives a message from the mailbox. If one isn't available, this function
1118 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001119 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001120 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001121 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001122struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1123 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001124{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001125 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001126 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001127
Andrew Scullaa039b32018-10-04 15:02:26 +01001128 /*
1129 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001130 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001131 */
Andrew Scull19503262018-09-20 14:48:39 +01001132 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001133 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001134 }
1135
1136 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001137
Andrew Scullaa039b32018-10-04 15:02:26 +01001138 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001139 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1140 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001141 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001142 goto out;
1143 }
1144
1145 /* No pending message so fail if not allowed to block. */
1146 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001148 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001149 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001150
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001151 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001152 * From this point onward this call can only be interrupted or a message
1153 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001155 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001156 return_code = ffa_error(FFA_INTERRUPTED);
1157 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001158 goto out;
1159 }
1160
Fuad Tabbaed294af2019-12-20 10:43:01 +00001161 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001162 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001163 struct ffa_value run_return = {
1164 .func = FFA_MSG_WAIT_32,
1165 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001166 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001167
Andrew Walbranb4816552018-12-05 17:35:42 +00001168 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001169 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001170 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001171out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001172 sl_unlock(&vm->lock);
1173
Jose Marinho3e2442f2019-03-12 13:30:37 +00001174 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001175}
1176
1177/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001178 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1179 * by this function, the caller must have called api_mailbox_send before with
1180 * the notify argument set to true, and this call must have failed because the
1181 * mailbox was not available.
1182 *
1183 * It should be called repeatedly to retrieve a list of VMs.
1184 *
1185 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1186 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001187 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001188int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001189{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001190 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001191 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001192 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001193
1194 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001195 if (list_empty(&vm->mailbox.ready_list)) {
1196 ret = -1;
1197 goto exit;
1198 }
1199
1200 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1201 ready_links);
1202 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001203 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001204
1205exit:
1206 sl_unlock(&vm->lock);
1207 return ret;
1208}
1209
1210/**
1211 * Retrieves the next VM waiting to be notified that the mailbox of the
1212 * specified VM became writable. Only primary VMs are allowed to call this.
1213 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001214 * Returns -1 on failure or if there are no waiters; the VM id of the next
1215 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001216 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001217int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001218{
1219 struct vm *vm;
1220 struct vm_locked locked;
1221 struct wait_entry *entry;
1222 struct vm *waiting_vm;
1223
1224 /* Only primary VMs are allowed to call this function. */
1225 if (current->vm->id != HF_PRIMARY_VM_ID) {
1226 return -1;
1227 }
1228
Andrew Walbran42347a92019-05-09 13:59:03 +01001229 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001230 if (vm == NULL) {
1231 return -1;
1232 }
1233
Fuad Tabbaed294af2019-12-20 10:43:01 +00001234 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001235 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001236 entry = api_fetch_waiter(locked);
1237 vm_unlock(&locked);
1238
1239 if (entry == NULL) {
1240 return -1;
1241 }
1242
1243 /* Enqueue notification to waiting VM. */
1244 waiting_vm = entry->waiting_vm;
1245
1246 sl_lock(&waiting_vm->lock);
1247 if (list_empty(&entry->ready_links)) {
1248 list_append(&waiting_vm->mailbox.ready_list,
1249 &entry->ready_links);
1250 }
1251 sl_unlock(&waiting_vm->lock);
1252
1253 return waiting_vm->id;
1254}
1255
1256/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001257 * Releases the caller's mailbox so that a new message can be received. The
1258 * caller must have copied out all data they wish to preserve as new messages
1259 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001260 *
1261 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001262 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1263 * - FFA_SUCCESS on success if no further action is needed.
1264 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001265 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001266 * hf_mailbox_waiter_get.
1267 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001268struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001269{
1270 struct vm *vm = current->vm;
1271 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001273
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001274 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001275 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001276 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001277 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001278 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001279 break;
1280
Andrew Sculld6ee1102019-04-05 22:12:42 +01001281 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001282 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001283 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001284 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001285 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001286 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001287
1288 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001289}
Andrew Walbran318f5732018-11-20 16:23:42 +00001290
1291/**
1292 * Enables or disables a given interrupt ID for the calling vCPU.
1293 *
1294 * Returns 0 on success, or -1 if the intid is invalid.
1295 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001296int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001297{
1298 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001299 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001300
Andrew Walbran318f5732018-11-20 16:23:42 +00001301 if (intid >= HF_NUM_INTIDS) {
1302 return -1;
1303 }
1304
1305 sl_lock(&current->lock);
1306 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001307 /*
1308 * If it is pending and was not enabled before, increment the
1309 * count.
1310 */
1311 if (current->interrupts.interrupt_pending[intid_index] &
1312 ~current->interrupts.interrupt_enabled[intid_index] &
1313 intid_mask) {
1314 current->interrupts.enabled_and_pending_count++;
1315 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001316 current->interrupts.interrupt_enabled[intid_index] |=
1317 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001318 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001319 /*
1320 * If it is pending and was enabled before, decrement the count.
1321 */
1322 if (current->interrupts.interrupt_pending[intid_index] &
1323 current->interrupts.interrupt_enabled[intid_index] &
1324 intid_mask) {
1325 current->interrupts.enabled_and_pending_count--;
1326 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001327 current->interrupts.interrupt_enabled[intid_index] &=
1328 ~intid_mask;
1329 }
1330
1331 sl_unlock(&current->lock);
1332 return 0;
1333}
1334
1335/**
1336 * Returns the ID of the next pending interrupt for the calling vCPU, and
1337 * acknowledges it (i.e. marks it as no longer pending). Returns
1338 * HF_INVALID_INTID if there are no pending interrupts.
1339 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001340uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001341{
1342 uint8_t i;
1343 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001344
1345 /*
1346 * Find the first enabled and pending interrupt ID, return it, and
1347 * deactivate it.
1348 */
1349 sl_lock(&current->lock);
1350 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1351 uint32_t enabled_and_pending =
1352 current->interrupts.interrupt_enabled[i] &
1353 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001354
Andrew Walbran318f5732018-11-20 16:23:42 +00001355 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001356 uint8_t bit_index = ctz(enabled_and_pending);
1357 /*
1358 * Mark it as no longer pending and decrement the count.
1359 */
1360 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001361 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001362 current->interrupts.enabled_and_pending_count--;
1363 first_interrupt =
1364 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001365 break;
1366 }
1367 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001368
1369 sl_unlock(&current->lock);
1370 return first_interrupt;
1371}
1372
1373/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001374 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001375 * given VM and vCPU.
1376 */
1377static inline bool is_injection_allowed(uint32_t target_vm_id,
1378 struct vcpu *current)
1379{
1380 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001381
Andrew Walbran318f5732018-11-20 16:23:42 +00001382 /*
1383 * The primary VM is allowed to inject interrupts into any VM. Secondary
1384 * VMs are only allowed to inject interrupts into their own vCPUs.
1385 */
1386 return current_vm_id == HF_PRIMARY_VM_ID ||
1387 current_vm_id == target_vm_id;
1388}
1389
1390/**
1391 * Injects a virtual interrupt of the given ID into the given target vCPU.
1392 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1393 * when the vCPU is next run, which is up to the scheduler.
1394 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001395 * Returns:
1396 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1397 * ID is invalid, or the current VM is not allowed to inject interrupts to
1398 * the target VM.
1399 * - 0 on success if no further action is needed.
1400 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1401 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001402 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1404 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001405 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001406{
Andrew Walbran318f5732018-11-20 16:23:42 +00001407 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001408 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001409
1410 if (intid >= HF_NUM_INTIDS) {
1411 return -1;
1412 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001413
Andrew Walbran318f5732018-11-20 16:23:42 +00001414 if (target_vm == NULL) {
1415 return -1;
1416 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001417
Andrew Walbran318f5732018-11-20 16:23:42 +00001418 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001419 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001420 return -1;
1421 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001422
Andrew Walbran318f5732018-11-20 16:23:42 +00001423 if (!is_injection_allowed(target_vm_id, current)) {
1424 return -1;
1425 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001426
Andrew Walbrane1310df2019-04-29 17:28:28 +01001427 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001428
Andrew Walbran17eebf92020-02-05 16:35:49 +00001429 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1430 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1431 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001432 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001433}
Andrew Scull6386f252018-12-06 13:29:10 +00001434
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435/** Returns the version of the implemented FF-A specification. */
1436struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001437{
1438 /*
1439 * Ensure that both major and minor revision representation occupies at
1440 * most 15 bits.
1441 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001442 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001443 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001444 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001445 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001447 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001448 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001449 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001450
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001451 return (struct ffa_value){
1452 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1453 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001454}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001455
1456int64_t api_debug_log(char c, struct vcpu *current)
1457{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001458 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001459 struct vm *vm = current->vm;
1460 struct vm_locked vm_locked = vm_lock(vm);
1461
Andrew Sculld54e1be2019-08-20 11:09:42 +01001462 if (c == '\n' || c == '\0') {
1463 flush = true;
1464 } else {
1465 vm->log_buffer[vm->log_buffer_length++] = c;
1466 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1467 }
1468
1469 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001470 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1471 vm->log_buffer_length);
1472 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001473 }
1474
1475 vm_unlock(&vm_locked);
1476
1477 return 0;
1478}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001479
1480/**
1481 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001483 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001484struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001485{
1486 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001487 case FFA_ERROR_32:
1488 case FFA_SUCCESS_32:
1489 case FFA_INTERRUPT_32:
1490 case FFA_VERSION_32:
1491 case FFA_FEATURES_32:
1492 case FFA_RX_RELEASE_32:
1493 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001494 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001495 case FFA_ID_GET_32:
1496 case FFA_MSG_POLL_32:
1497 case FFA_MSG_WAIT_32:
1498 case FFA_YIELD_32:
1499 case FFA_RUN_32:
1500 case FFA_MSG_SEND_32:
1501 case FFA_MEM_DONATE_32:
1502 case FFA_MEM_LEND_32:
1503 case FFA_MEM_SHARE_32:
1504 case FFA_MEM_RETRIEVE_REQ_32:
1505 case FFA_MEM_RETRIEVE_RESP_32:
1506 case FFA_MEM_RELINQUISH_32:
1507 case FFA_MEM_RECLAIM_32:
1508 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001509 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001510 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001511 }
1512}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001513
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001514struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1515 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001516 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001517{
1518 struct vm *from = current->vm;
1519 struct vm *to;
1520 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001521 struct ffa_memory_region *memory_region;
1522 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001523
1524 if (ipa_addr(address) != 0 || page_count != 0) {
1525 /*
1526 * Hafnium only supports passing the descriptor in the TX
1527 * mailbox.
1528 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001530 }
1531
Andrew Walbranca808b12020-05-15 17:22:28 +01001532 if (fragment_length > length) {
1533 dlog_verbose(
1534 "Fragment length %d greater than total length %d.\n",
1535 fragment_length, length);
1536 return ffa_error(FFA_INVALID_PARAMETERS);
1537 }
1538 if (fragment_length < sizeof(struct ffa_memory_region) +
1539 sizeof(struct ffa_memory_access)) {
1540 dlog_verbose(
1541 "Initial fragment length %d smaller than header size "
1542 "%d.\n",
1543 fragment_length,
1544 sizeof(struct ffa_memory_region) +
1545 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001547 }
1548
1549 /*
1550 * Check that the sender has configured its send buffer. If the TX
1551 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1552 * be safely accessed after releasing the lock since the TX mailbox
1553 * address can only be configured once.
1554 */
1555 sl_lock(&from->lock);
1556 from_msg = from->mailbox.send;
1557 sl_unlock(&from->lock);
1558
1559 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001560 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001561 }
1562
1563 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001564 * Copy the memory region descriptor to a fresh page from the memory
1565 * pool. This prevents the sender from changing it underneath us, and
1566 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001567 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001568 if (fragment_length > HF_MAILBOX_SIZE ||
1569 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001570 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001571 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001573 if (memory_region == NULL) {
1574 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001575 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001576 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001577 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001578
1579 /* The sender must match the caller. */
1580 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001581 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001583 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001584 }
1585
Andrew Walbrana65a1322020-04-06 19:32:32 +01001586 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001587 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001588 dlog_verbose(
1589 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001590 "endpoint memory access descriptors, expected 1).\n",
1591 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001592 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001593 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001594 }
1595
1596 /*
1597 * Ensure that the receiver VM exists and isn't the same as the sender.
1598 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001599 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001600 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001603 goto out;
1604 }
1605
1606 if (to->id == HF_TEE_VM_ID) {
1607 /*
1608 * The 'to' VM lock is only needed in the case that it is the
1609 * TEE VM.
1610 */
1611 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1612
1613 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001615 goto out_unlock;
1616 }
1617
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001618 ret = ffa_memory_tee_send(
1619 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1620 length, fragment_length, share_func, &api_page_pool);
1621 /*
1622 * ffa_tee_memory_send takes ownership of the memory_region, so
1623 * make sure we don't free it.
1624 */
1625 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001626
1627 out_unlock:
1628 vm_unlock(&vm_to_from_lock.vm1);
1629 vm_unlock(&vm_to_from_lock.vm2);
1630 } else {
1631 struct vm_locked from_locked = vm_lock(from);
1632
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001633 ret = ffa_memory_send(from_locked, memory_region, length,
1634 fragment_length, share_func,
1635 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001636 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001638 * make sure we don't free it.
1639 */
1640 memory_region = NULL;
1641
1642 vm_unlock(&from_locked);
1643 }
1644
1645out:
1646 if (memory_region != NULL) {
1647 mpool_free(&api_page_pool, memory_region);
1648 }
1649
1650 return ret;
1651}
1652
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001653struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1654 uint32_t fragment_length,
1655 ipaddr_t address, uint32_t page_count,
1656 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001657{
1658 struct vm *to = current->vm;
1659 struct vm_locked to_locked;
1660 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001661 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001662 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001663 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001664
1665 if (ipa_addr(address) != 0 || page_count != 0) {
1666 /*
1667 * Hafnium only supports passing the descriptor in the TX
1668 * mailbox.
1669 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001671 }
1672
Andrew Walbrana65a1322020-04-06 19:32:32 +01001673 if (fragment_length != length) {
1674 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001675 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001676 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001677
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001678 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001679 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001680 message_buffer_size = cpu_get_buffer_size(current->cpu);
1681 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1682 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001683 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001684 }
1685
1686 to_locked = vm_lock(to);
1687 to_msg = to->mailbox.send;
1688
1689 if (to_msg == NULL) {
1690 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001691 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001692 goto out;
1693 }
1694
1695 /*
1696 * Copy the retrieve request descriptor to an internal buffer, so that
1697 * the caller can't change it underneath us.
1698 */
1699 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1700
1701 if (msg_receiver_busy(to_locked, NULL, false)) {
1702 /*
1703 * Can't retrieve memory information if the mailbox is not
1704 * available.
1705 */
1706 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001707 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001708 goto out;
1709 }
1710
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001711 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1712 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001713
1714out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001715 vm_unlock(&to_locked);
1716 return ret;
1717}
1718
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001719struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001720{
1721 struct vm *from = current->vm;
1722 struct vm_locked from_locked;
1723 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001724 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001725 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001726 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727 uint32_t length;
1728
1729 from_locked = vm_lock(from);
1730 from_msg = from->mailbox.send;
1731
1732 if (from_msg == NULL) {
1733 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001734 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001735 goto out;
1736 }
1737
1738 /*
1739 * Calculate length from relinquish descriptor before copying. We will
1740 * check again later to make sure it hasn't changed.
1741 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001742 length = sizeof(struct ffa_mem_relinquish) +
1743 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
1744 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001745 /*
1746 * Copy the relinquish descriptor to an internal buffer, so that the
1747 * caller can't change it underneath us.
1748 */
1749 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001750 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001751 message_buffer_size = cpu_get_buffer_size(current->cpu);
1752 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1753 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001754 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001755 goto out;
1756 }
1757 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
1758
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001759 if (sizeof(struct ffa_mem_relinquish) +
1760 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001761 length) {
1762 dlog_verbose(
1763 "Endpoint count changed while copying to internal "
1764 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001765 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001766 goto out;
1767 }
1768
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001769 ret = ffa_memory_relinquish(from_locked, relinquish_request,
1770 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001771
1772out:
1773 vm_unlock(&from_locked);
1774 return ret;
1775}
1776
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001777struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
1778 ffa_memory_region_flags_t flags,
1779 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001780{
1781 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001783
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001784 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1785 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001786 struct vm_locked to_locked = vm_lock(to);
1787
Andrew Walbranca808b12020-05-15 17:22:28 +01001788 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001789 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001790
Andrew Walbran290b0c92020-02-03 16:37:14 +00001791 vm_unlock(&to_locked);
1792 } else {
1793 struct vm *from = vm_find(HF_TEE_VM_ID);
1794 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1795
Andrew Walbranca808b12020-05-15 17:22:28 +01001796 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
1797 vm_to_from_lock.vm2, handle, flags,
1798 &api_page_pool);
1799
1800 vm_unlock(&vm_to_from_lock.vm1);
1801 vm_unlock(&vm_to_from_lock.vm2);
1802 }
1803
1804 return ret;
1805}
1806
1807struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
1808 uint32_t fragment_offset,
1809 ffa_vm_id_t sender_vm_id,
1810 struct vcpu *current)
1811{
1812 struct vm *to = current->vm;
1813 struct vm_locked to_locked;
1814 struct ffa_value ret;
1815
1816 /* Sender ID MBZ at virtual instance. */
1817 if (sender_vm_id != 0) {
1818 return ffa_error(FFA_INVALID_PARAMETERS);
1819 }
1820
1821 to_locked = vm_lock(to);
1822
1823 if (msg_receiver_busy(to_locked, NULL, false)) {
1824 /*
1825 * Can't retrieve memory information if the mailbox is not
1826 * available.
1827 */
1828 dlog_verbose("RX buffer not ready.\n");
1829 ret = ffa_error(FFA_BUSY);
1830 goto out;
1831 }
1832
1833 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
1834 &api_page_pool);
1835
1836out:
1837 vm_unlock(&to_locked);
1838 return ret;
1839}
1840
1841struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
1842 uint32_t fragment_length,
1843 ffa_vm_id_t sender_vm_id,
1844 struct vcpu *current)
1845{
1846 struct vm *from = current->vm;
1847 const void *from_msg;
1848 void *fragment_copy;
1849 struct ffa_value ret;
1850
1851 /* Sender ID MBZ at virtual instance. */
1852 if (sender_vm_id != 0) {
1853 return ffa_error(FFA_INVALID_PARAMETERS);
1854 }
1855
1856 /*
1857 * Check that the sender has configured its send buffer. If the TX
1858 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1859 * be safely accessed after releasing the lock since the TX mailbox
1860 * address can only be configured once.
1861 */
1862 sl_lock(&from->lock);
1863 from_msg = from->mailbox.send;
1864 sl_unlock(&from->lock);
1865
1866 if (from_msg == NULL) {
1867 return ffa_error(FFA_INVALID_PARAMETERS);
1868 }
1869
1870 /*
1871 * Copy the fragment to a fresh page from the memory pool. This prevents
1872 * the sender from changing it underneath us, and also lets us keep it
1873 * around in the share state table if needed.
1874 */
1875 if (fragment_length > HF_MAILBOX_SIZE ||
1876 fragment_length > MM_PPOOL_ENTRY_SIZE) {
1877 dlog_verbose(
1878 "Fragment length %d larger than mailbox size %d.\n",
1879 fragment_length, HF_MAILBOX_SIZE);
1880 return ffa_error(FFA_INVALID_PARAMETERS);
1881 }
1882 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
1883 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1884 0) {
1885 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
1886 return ffa_error(FFA_INVALID_PARAMETERS);
1887 }
1888 fragment_copy = mpool_alloc(&api_page_pool);
1889 if (fragment_copy == NULL) {
1890 dlog_verbose("Failed to allocate fragment copy.\n");
1891 return ffa_error(FFA_NO_MEMORY);
1892 }
1893 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
1894
1895 /*
1896 * Hafnium doesn't support fragmentation of memory retrieve requests
1897 * (because it doesn't support caller-specified mappings, so a request
1898 * will never be larger than a single page), so this must be part of a
1899 * memory send (i.e. donate, lend or share) request.
1900 *
1901 * We can tell from the handle whether the memory transaction is for the
1902 * TEE or not.
1903 */
1904 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1905 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
1906 struct vm_locked from_locked = vm_lock(from);
1907
1908 ret = ffa_memory_send_continue(from_locked, fragment_copy,
1909 fragment_length, handle,
1910 &api_page_pool);
1911 /*
1912 * `ffa_memory_send_continue` takes ownership of the
1913 * fragment_copy, so we don't need to free it here.
1914 */
1915 vm_unlock(&from_locked);
1916 } else {
1917 struct vm *to = vm_find(HF_TEE_VM_ID);
1918 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1919
1920 /*
1921 * The TEE RX buffer state is checked in
1922 * `ffa_memory_tee_send_continue` rather than here, as we need
1923 * to return `FFA_MEM_FRAG_RX` with the current offset rather
1924 * than FFA_ERROR FFA_BUSY in case it is busy.
1925 */
1926
1927 ret = ffa_memory_tee_send_continue(
1928 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
1929 fragment_length, handle, &api_page_pool);
1930 /*
1931 * `ffa_memory_tee_send_continue` takes ownership of the
1932 * fragment_copy, so we don't need to free it here.
1933 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00001934
1935 vm_unlock(&vm_to_from_lock.vm1);
1936 vm_unlock(&vm_to_from_lock.vm2);
1937 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001938
1939 return ret;
1940}