blob: 3370bccbfb868153126928d5a2be454b3fa19d1f [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 Filho3fcbcff2018-07-10 23:53:39 +0100345 * Returns the number of VMs configured to run.
346 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347ffa_vm_count_t api_vm_get_count(void)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100348{
Andrew Scull19503262018-09-20 14:48:39 +0100349 return vm_get_count();
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100350}
351
352/**
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100353 * Returns the number of vCPUs configured in the given VM, or 0 if there is no
354 * such VM or the caller is not the primary VM.
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100355 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100356ffa_vcpu_count_t api_vcpu_get_count(ffa_vm_id_t vm_id,
357 const struct vcpu *current)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100358{
Andrew Scull19503262018-09-20 14:48:39 +0100359 struct vm *vm;
360
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000361 /* Only the primary VM needs to know about vCPUs for scheduling. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100362 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100363 return 0;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100364 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100365
Andrew Walbran42347a92019-05-09 13:59:03 +0100366 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100367 if (vm == NULL) {
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100368 return 0;
Andrew Scull19503262018-09-20 14:48:39 +0100369 }
370
371 return vm->vcpu_count;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100372}
373
374/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000375 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000376 * function to indicate that register state for the given vCPU has been saved
377 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000378 */
379void api_regs_state_saved(struct vcpu *vcpu)
380{
381 sl_lock(&vcpu->lock);
382 vcpu->regs_available = true;
383 sl_unlock(&vcpu->lock);
384}
385
386/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000387 * Retrieves the next waiter and removes it from the wait list if the VM's
388 * mailbox is in a writable state.
389 */
390static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
391{
392 struct wait_entry *entry;
393 struct vm *vm = locked_vm.vm;
394
Andrew Sculld6ee1102019-04-05 22:12:42 +0100395 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000396 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
397 /* The mailbox is not writable or there are no waiters. */
398 return NULL;
399 }
400
401 /* Remove waiter from the wait list. */
402 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
403 wait_links);
404 list_remove(&entry->wait_links);
405 return entry;
406}
407
408/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000409 * Assuming that the arguments have already been checked by the caller, injects
410 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
411 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
412 * is next run, which is up to the scheduler.
413 *
414 * Returns:
415 * - 0 on success if no further action is needed.
416 * - 1 if it was called by the primary VM and the primary VM now needs to wake
417 * up or kick the target vCPU.
418 */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100419static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
Andrew Walbran508e63c2018-12-20 17:02:37 +0000420 uint32_t intid, struct vcpu *current,
421 struct vcpu **next)
422{
423 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100424 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000425 int64_t ret = 0;
426
427 sl_lock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000428
429 /*
430 * We only need to change state and (maybe) trigger a virtual IRQ if it
431 * is enabled and was not previously pending. Otherwise we can skip
432 * everything except setting the pending bit.
433 *
434 * If you change this logic make sure to update the need_vm_lock logic
435 * above to match.
436 */
437 if (!(target_vcpu->interrupts.interrupt_enabled[intid_index] &
438 ~target_vcpu->interrupts.interrupt_pending[intid_index] &
439 intid_mask)) {
440 goto out;
441 }
442
443 /* Increment the count. */
444 target_vcpu->interrupts.enabled_and_pending_count++;
445
446 /*
447 * Only need to update state if there was not already an
448 * interrupt enabled and pending.
449 */
450 if (target_vcpu->interrupts.enabled_and_pending_count != 1) {
451 goto out;
452 }
453
Andrew Walbran508e63c2018-12-20 17:02:37 +0000454 if (current->vm->id == HF_PRIMARY_VM_ID) {
455 /*
456 * If the call came from the primary VM, let it know that it
457 * should run or kick the target vCPU.
458 */
459 ret = 1;
460 } else if (current != target_vcpu && next != NULL) {
Andrew Walbran33645652019-04-15 12:29:31 +0100461 *next = api_wake_up(current, target_vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000462 }
463
464out:
465 /* Either way, make it pending. */
466 target_vcpu->interrupts.interrupt_pending[intid_index] |= intid_mask;
467
468 sl_unlock(&target_vcpu->lock);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000469
470 return ret;
471}
472
473/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100474 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
475 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100476 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100477static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100478{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000479 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100480 case FFA_MSG_SEND_32:
481 return (struct ffa_value){
482 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000483 .arg1 = (receiver->mailbox.recv_sender << 16) |
484 receiver->id,
485 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000486 default:
487 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000488 dlog_error("Tried to return an invalid message function %#x\n",
489 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000491 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100492}
493
494/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000495 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000496 * value needs to be forced onto the vCPU.
497 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000498static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100499 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000500{
Andrew Scullb06d1752019-02-04 10:15:48 +0000501 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000502 bool ret;
503
Andrew Scullb06d1752019-02-04 10:15:48 +0000504 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000505 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000506 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100507 * The VM lock is not needed in the common case so it must only be taken
508 * when it is going to be needed. This ensures there are no inter-vCPU
509 * dependencies in the common run case meaning the sensitive context
510 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000511 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000512 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000513
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000514 /* The VM needs to be locked to deliver mailbox messages. */
515 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
516 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000517 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000518 sl_lock(&vcpu->vm->lock);
519 sl_lock(&vcpu->lock);
520 }
521
522 /*
523 * If the vCPU is already running somewhere then we can't run it here
524 * simultaneously. While it is actually running then the state should be
525 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
526 * stops running but while Hafnium is in the process of switching back
527 * to the primary there will be a brief period while the state has been
528 * updated but `regs_available` is still false (until
529 * `api_regs_state_saved` is called). We can't start running it again
530 * until this has finished, so count this state as still running for the
531 * purposes of this check.
532 */
533 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
534 /*
535 * vCPU is running on another pCPU.
536 *
537 * It's okay not to return the sleep duration here because the
538 * other physical CPU that is currently running this vCPU will
539 * return the sleep duration if needed.
540 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100541 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000542 ret = false;
543 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000544 }
Andrew Scull9726c252019-01-23 13:44:19 +0000545
546 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100547 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000548 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
549 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100550 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000551 }
552 ret = false;
553 goto out;
554 }
555
Andrew Walbran508e63c2018-12-20 17:02:37 +0000556 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100557 case VCPU_STATE_RUNNING:
558 case VCPU_STATE_OFF:
559 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000560 ret = false;
561 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000562
Andrew Sculld6ee1102019-04-05 22:12:42 +0100563 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000564 /*
565 * A pending message allows the vCPU to run so the message can
566 * be delivered directly.
567 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100568 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100569 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100570 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100571 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000572 break;
573 }
574 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100575 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000576 /* Allow virtual interrupts to be delivered. */
577 if (vcpu->interrupts.enabled_and_pending_count > 0) {
578 break;
579 }
580
Andrew Walbran508e63c2018-12-20 17:02:37 +0000581 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000582 uint64_t timer_remaining_ns =
583 arch_timer_remaining_ns(&vcpu->regs);
584
585 /*
586 * The timer expired so allow the interrupt to be
587 * delivered.
588 */
589 if (timer_remaining_ns == 0) {
590 break;
591 }
592
593 /*
594 * The vCPU is not ready to run, return the appropriate
595 * code to the primary which called vcpu_run.
596 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100597 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100598 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 ? FFA_MSG_WAIT_32
600 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000601 run_ret->arg1 =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100602 ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000603 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000604 }
605
606 ret = false;
607 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000608
Andrew Sculld6ee1102019-04-05 22:12:42 +0100609 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000610 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000611 }
612
Andrew Scullb06d1752019-02-04 10:15:48 +0000613 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000614 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100615 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000616
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000617 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000618 * Mark the registers as unavailable now that we're about to reflect
619 * them onto the real registers. This will also prevent another physical
620 * CPU from trying to read these registers.
621 */
622 vcpu->regs_available = false;
623
624 ret = true;
625
626out:
627 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000628 if (need_vm_lock) {
629 sl_unlock(&vcpu->vm->lock);
630 }
631
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000632 return ret;
633}
634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
636 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100637{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100638 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100639 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100641
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000642 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100643 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100645 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100646 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100647
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000648 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100649 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100650 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100651 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100652
Andrew Scull19503262018-09-20 14:48:39 +0100653 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100654 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100655 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100656 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100657 }
658
Fuad Tabbaed294af2019-12-20 10:43:01 +0000659 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100660 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100661 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100662 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100663
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000664 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100665 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000666 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000667 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100668 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000669
Andrew Walbran508e63c2018-12-20 17:02:37 +0000670 /*
671 * Inject timer interrupt if timer has expired. It's safe to access
672 * vcpu->regs here because api_vcpu_prepare_run already made sure that
673 * regs_available was true (and then set it to false) before returning
674 * true.
675 */
676 if (arch_timer_pending(&vcpu->regs)) {
677 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100678 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
679 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000680
681 /*
682 * Set the mask bit so the hardware interrupt doesn't fire
683 * again. Ideally we wouldn't do this because it affects what
684 * the secondary vCPU sees, but if we don't then we end up with
685 * a loop of the interrupt firing each time we try to return to
686 * the secondary vCPU.
687 */
688 arch_timer_mask(&vcpu->regs);
689 }
690
Fuad Tabbaed294af2019-12-20 10:43:01 +0000691 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000692 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000693
Andrew Scull33fecd32019-01-08 14:48:27 +0000694 /*
695 * Set a placeholder return code to the scheduler. This will be
696 * overwritten when the switch back to the primary occurs.
697 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 ret.func = FFA_INTERRUPT_32;
699 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100700 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000701
Andrew Scull6d2db332018-10-10 15:28:17 +0100702out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100703 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100704}
705
706/**
Andrew Scull81e85092018-12-12 12:56:20 +0000707 * Check that the mode indicates memory that is valid, owned and exclusive.
708 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100709static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000710{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100711 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
712 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000713}
714
715/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 * Determines the value to be returned by api_vm_configure and ffa_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000717 * after they've succeeded. If a secondary VM is running and there are waiters,
718 * it also switches back to the primary VM for it to wake waiters up.
719 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100720static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
721 struct vcpu *current,
722 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000723{
724 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000725
726 if (list_empty(&vm->mailbox.waiter_list)) {
727 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100728 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000729 }
730
731 if (vm->id == HF_PRIMARY_VM_ID) {
732 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100733 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000734 }
735
736 /*
737 * Switch back to the primary VM, informing it that there are waiters
738 * that need to be notified.
739 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000740 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000742 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000743
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100744 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000745}
746
747/**
Andrew Sculle1322792019-07-01 17:46:10 +0100748 * Configures the hypervisor's stage-1 view of the send and receive pages. The
749 * stage-1 page tables must be locked so memory cannot be taken by another core
750 * which could result in this transaction being unable to roll back in the case
751 * of an error.
752 */
753static bool api_vm_configure_stage1(struct vm_locked vm_locked,
754 paddr_t pa_send_begin, paddr_t pa_send_end,
755 paddr_t pa_recv_begin, paddr_t pa_recv_end,
756 struct mpool *local_page_pool)
757{
758 bool ret;
759 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
760
761 /* Map the send page as read-only in the hypervisor address space. */
762 vm_locked.vm->mailbox.send =
763 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
764 MM_MODE_R, local_page_pool);
765 if (!vm_locked.vm->mailbox.send) {
766 /* TODO: partial defrag of failed range. */
767 /* Recover any memory consumed in failed mapping. */
768 mm_defrag(mm_stage1_locked, local_page_pool);
769 goto fail;
770 }
771
772 /*
773 * Map the receive page as writable in the hypervisor address space. On
774 * failure, unmap the send page before returning.
775 */
776 vm_locked.vm->mailbox.recv =
777 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
778 MM_MODE_W, local_page_pool);
779 if (!vm_locked.vm->mailbox.recv) {
780 /* TODO: partial defrag of failed range. */
781 /* Recover any memory consumed in failed mapping. */
782 mm_defrag(mm_stage1_locked, local_page_pool);
783 goto fail_undo_send;
784 }
785
786 ret = true;
787 goto out;
788
789 /*
790 * The following mappings will not require more memory than is available
791 * in the local pool.
792 */
793fail_undo_send:
794 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100795 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
796 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100797
798fail:
799 ret = false;
800
801out:
802 mm_unlock_stage1(&mm_stage1_locked);
803
804 return ret;
805}
806
807/**
808 * Configures the send and receive pages in the VM stage-2 and hypervisor
809 * stage-1 page tables. Locking of the page tables combined with a local memory
810 * pool ensures there will always be enough memory to recover from any errors
811 * that arise.
812 */
813static bool api_vm_configure_pages(struct vm_locked vm_locked,
814 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100815 uint32_t orig_send_mode,
816 paddr_t pa_recv_begin, paddr_t pa_recv_end,
817 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100818{
819 bool ret;
820 struct mpool local_page_pool;
821
822 /*
823 * Create a local pool so any freed memory can't be used by another
824 * thread. This is to ensure the original mapping can be restored if any
825 * stage of the process fails.
826 */
827 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
828
829 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000830 if (!vm_identity_map(
831 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100832 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000833 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100834 goto fail;
835 }
836
Andrew Scull3c257452019-11-26 13:32:50 +0000837 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
838 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
839 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100840 /* TODO: partial defrag of failed range. */
841 /* Recover any memory consumed in failed mapping. */
842 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
843 goto fail_undo_send;
844 }
845
846 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
847 pa_recv_begin, pa_recv_end,
848 &local_page_pool)) {
849 goto fail_undo_send_and_recv;
850 }
851
852 ret = true;
853 goto out;
854
855 /*
856 * The following mappings will not require more memory than is available
857 * in the local pool.
858 */
859fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000860 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
861 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100862
863fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000864 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
865 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100866
867fail:
868 ret = false;
869
870out:
871 mpool_fini(&local_page_pool);
872
873 return ret;
874}
875
876/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100877 * Configures the VM to send/receive data through the specified pages. The pages
878 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000879 *
880 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000882 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000884 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000886 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100887 * - FFA_SUCCESS on success if no further action is needed.
888 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000889 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100890 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
892 uint32_t page_count, struct vcpu *current,
893 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100894{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100895 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100896 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100897 paddr_t pa_send_begin;
898 paddr_t pa_send_end;
899 paddr_t pa_recv_begin;
900 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100901 uint32_t orig_send_mode;
902 uint32_t orig_recv_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 struct ffa_value ret;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000904
905 /* Hafnium only supports a fixed size of RX/TX buffers. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100906 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
907 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000908 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100909
910 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000911 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
912 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100914 }
915
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000916 /* Convert to physical addresses. */
917 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000918 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000919
920 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000921 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000922
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100923 /* Fail if the same page is used for the send and receive pages. */
924 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000926 }
927
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100928 /*
929 * The hypervisor's memory map must be locked for the duration of this
930 * operation to ensure there will be sufficient memory to recover from
931 * any failures.
932 *
Fuad Tabba9dc276f2020-07-16 09:29:32 +0100933 * TODO: the scope can be reduced but will require restructuring to
934 * keep a single unlock point.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100935 */
Andrew Sculle1322792019-07-01 17:46:10 +0100936 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000937
938 /* We only allow these to be setup once. */
939 if (vm->mailbox.send || vm->mailbox.recv) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000941 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000942 }
943
944 /*
945 * Ensure the pages are valid, owned and exclusive to the VM and that
946 * the VM has the required access to the memory.
947 */
948 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
949 &orig_send_mode) ||
950 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
951 (orig_send_mode & MM_MODE_R) == 0 ||
952 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000954 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000955 }
956
957 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
958 &orig_recv_mode) ||
959 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
960 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000962 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000963 }
964
Andrew Sculle1322792019-07-01 17:46:10 +0100965 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
966 orig_send_mode, pa_recv_begin, pa_recv_end,
967 orig_recv_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100968 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000969 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100970 }
971
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000972 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100973 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000974
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100975exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100976 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100977
978 return ret;
979}
980
981/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100982 * Notifies the `to` VM about the message currently in its mailbox, possibly
983 * with the help of the primary VM.
984 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100985static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
986 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100987{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
989 struct ffa_value primary_ret = {
990 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +0000991 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100992 };
993
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100994 /* Messages for the primary VM are delivered directly. */
995 if (to.vm->id == HF_PRIMARY_VM_ID) {
996 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000997 * Only tell the primary VM the size and other details if the
998 * message is for it, to avoid leaking data about messages for
999 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001000 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001002
1003 to.vm->mailbox.state = MAILBOX_STATE_READ;
1004 *next = api_switch_to_primary(current, primary_ret,
1005 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001006 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001007 }
1008
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001009 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1010
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001011 /* Messages for the TEE are sent on via the dispatcher. */
1012 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001013 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001014
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001015 ret = arch_tee_call(call);
1016 /*
1017 * After the call to the TEE completes it must have finished
1018 * reading its RX buffer, so it is ready for another message.
1019 */
1020 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001021 /*
1022 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001024 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001025 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001026 }
1027
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001028 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001029 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001030 *next = api_switch_to_primary(current, primary_ret,
1031 VCPU_STATE_READY);
1032 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001033
1034 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001035}
1036
1037/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001038 * Copies data from the sender's send buffer to the recipient's receive buffer
1039 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001040 *
1041 * If the recipient's receive buffer is busy, it can optionally register the
1042 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001043 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001044struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1045 ffa_vm_id_t receiver_vm_id, uint32_t size,
1046 uint32_t attributes, struct vcpu *current,
1047 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001048{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001049 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001050 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001051 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001052 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001053 struct ffa_value ret;
1054 bool notify =
1055 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001056
Andrew Walbran70bc8622019-10-07 14:15:58 +01001057 /* Ensure sender VM ID corresponds to the current VM. */
1058 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001059 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001060 }
1061
1062 /* Disallow reflexive requests as this suggests an error in the VM. */
1063 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001064 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001065 }
1066
1067 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001068 if (size > FFA_MSG_PAYLOAD_MAX) {
1069 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001070 }
1071
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001072 /* Ensure the receiver VM exists. */
1073 to = vm_find(receiver_vm_id);
1074 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001075 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001076 }
1077
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001078 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001079 * Check that the sender has configured its send buffer. If the tx
1080 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1081 * be safely accessed after releasing the lock since the tx mailbox
1082 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001083 */
1084 sl_lock(&from->lock);
1085 from_msg = from->mailbox.send;
1086 sl_unlock(&from->lock);
1087
1088 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001089 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001090 }
1091
Andrew Walbran82d6d152019-12-24 15:02:06 +00001092 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001093
Andrew Walbran82d6d152019-12-24 15:02:06 +00001094 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001096 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001097 }
1098
Andrew Walbran82d6d152019-12-24 15:02:06 +00001099 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001100 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001101 to->mailbox.recv_size = size;
1102 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001103 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001104 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001105
1106out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001107 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001108
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001109 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001110}
1111
1112/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001113 * Checks whether the vCPU's attempt to block for a message has already been
1114 * interrupted or whether it is allowed to block.
1115 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001117{
1118 bool interrupted;
1119
1120 sl_lock(&current->lock);
1121
1122 /*
1123 * Don't block if there are enabled and pending interrupts, to match
1124 * behaviour of wait_for_interrupt.
1125 */
1126 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1127
1128 sl_unlock(&current->lock);
1129
1130 return interrupted;
1131}
1132
1133/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001134 * Receives a message from the mailbox. If one isn't available, this function
1135 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001136 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001137 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001138 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1140 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001141{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001142 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001143 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001144
Andrew Scullaa039b32018-10-04 15:02:26 +01001145 /*
1146 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001147 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001148 */
Andrew Scull19503262018-09-20 14:48:39 +01001149 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001151 }
1152
1153 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001154
Andrew Scullaa039b32018-10-04 15:02:26 +01001155 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001156 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1157 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001158 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001159 goto out;
1160 }
1161
1162 /* No pending message so fail if not allowed to block. */
1163 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001165 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001166 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001167
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001168 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001169 * From this point onward this call can only be interrupted or a message
1170 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001171 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001172 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001173 return_code = ffa_error(FFA_INTERRUPTED);
1174 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001175 goto out;
1176 }
1177
Fuad Tabbaed294af2019-12-20 10:43:01 +00001178 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001179 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 struct ffa_value run_return = {
1181 .func = FFA_MSG_WAIT_32,
1182 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001183 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001184
Andrew Walbranb4816552018-12-05 17:35:42 +00001185 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001186 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001187 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001188out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001189 sl_unlock(&vm->lock);
1190
Jose Marinho3e2442f2019-03-12 13:30:37 +00001191 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001192}
1193
1194/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001195 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1196 * by this function, the caller must have called api_mailbox_send before with
1197 * the notify argument set to true, and this call must have failed because the
1198 * mailbox was not available.
1199 *
1200 * It should be called repeatedly to retrieve a list of VMs.
1201 *
1202 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1203 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001204 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001205int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001206{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001207 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001208 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001209 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001210
1211 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001212 if (list_empty(&vm->mailbox.ready_list)) {
1213 ret = -1;
1214 goto exit;
1215 }
1216
1217 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1218 ready_links);
1219 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001220 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001221
1222exit:
1223 sl_unlock(&vm->lock);
1224 return ret;
1225}
1226
1227/**
1228 * Retrieves the next VM waiting to be notified that the mailbox of the
1229 * specified VM became writable. Only primary VMs are allowed to call this.
1230 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001231 * Returns -1 on failure or if there are no waiters; the VM id of the next
1232 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001233 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001235{
1236 struct vm *vm;
1237 struct vm_locked locked;
1238 struct wait_entry *entry;
1239 struct vm *waiting_vm;
1240
1241 /* Only primary VMs are allowed to call this function. */
1242 if (current->vm->id != HF_PRIMARY_VM_ID) {
1243 return -1;
1244 }
1245
Andrew Walbran42347a92019-05-09 13:59:03 +01001246 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001247 if (vm == NULL) {
1248 return -1;
1249 }
1250
Fuad Tabbaed294af2019-12-20 10:43:01 +00001251 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001252 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001253 entry = api_fetch_waiter(locked);
1254 vm_unlock(&locked);
1255
1256 if (entry == NULL) {
1257 return -1;
1258 }
1259
1260 /* Enqueue notification to waiting VM. */
1261 waiting_vm = entry->waiting_vm;
1262
1263 sl_lock(&waiting_vm->lock);
1264 if (list_empty(&entry->ready_links)) {
1265 list_append(&waiting_vm->mailbox.ready_list,
1266 &entry->ready_links);
1267 }
1268 sl_unlock(&waiting_vm->lock);
1269
1270 return waiting_vm->id;
1271}
1272
1273/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001274 * Releases the caller's mailbox so that a new message can be received. The
1275 * caller must have copied out all data they wish to preserve as new messages
1276 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001277 *
1278 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001279 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1280 * - FFA_SUCCESS on success if no further action is needed.
1281 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001282 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001283 * hf_mailbox_waiter_get.
1284 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001286{
1287 struct vm *vm = current->vm;
1288 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001289 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001290
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001291 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001292 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001293 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001294 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001295 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001296 break;
1297
Andrew Sculld6ee1102019-04-05 22:12:42 +01001298 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001299 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001300 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001301 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001302 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001303 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001304
1305 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001306}
Andrew Walbran318f5732018-11-20 16:23:42 +00001307
1308/**
1309 * Enables or disables a given interrupt ID for the calling vCPU.
1310 *
1311 * Returns 0 on success, or -1 if the intid is invalid.
1312 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001313int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001314{
1315 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001316 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001317
Andrew Walbran318f5732018-11-20 16:23:42 +00001318 if (intid >= HF_NUM_INTIDS) {
1319 return -1;
1320 }
1321
1322 sl_lock(&current->lock);
1323 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001324 /*
1325 * If it is pending and was not enabled before, increment the
1326 * count.
1327 */
1328 if (current->interrupts.interrupt_pending[intid_index] &
1329 ~current->interrupts.interrupt_enabled[intid_index] &
1330 intid_mask) {
1331 current->interrupts.enabled_and_pending_count++;
1332 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001333 current->interrupts.interrupt_enabled[intid_index] |=
1334 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001335 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001336 /*
1337 * If it is pending and was enabled before, decrement the count.
1338 */
1339 if (current->interrupts.interrupt_pending[intid_index] &
1340 current->interrupts.interrupt_enabled[intid_index] &
1341 intid_mask) {
1342 current->interrupts.enabled_and_pending_count--;
1343 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001344 current->interrupts.interrupt_enabled[intid_index] &=
1345 ~intid_mask;
1346 }
1347
1348 sl_unlock(&current->lock);
1349 return 0;
1350}
1351
1352/**
1353 * Returns the ID of the next pending interrupt for the calling vCPU, and
1354 * acknowledges it (i.e. marks it as no longer pending). Returns
1355 * HF_INVALID_INTID if there are no pending interrupts.
1356 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001357uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001358{
1359 uint8_t i;
1360 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001361
1362 /*
1363 * Find the first enabled and pending interrupt ID, return it, and
1364 * deactivate it.
1365 */
1366 sl_lock(&current->lock);
1367 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1368 uint32_t enabled_and_pending =
1369 current->interrupts.interrupt_enabled[i] &
1370 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001371
Andrew Walbran318f5732018-11-20 16:23:42 +00001372 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001373 uint8_t bit_index = ctz(enabled_and_pending);
1374 /*
1375 * Mark it as no longer pending and decrement the count.
1376 */
1377 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001378 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001379 current->interrupts.enabled_and_pending_count--;
1380 first_interrupt =
1381 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001382 break;
1383 }
1384 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001385
1386 sl_unlock(&current->lock);
1387 return first_interrupt;
1388}
1389
1390/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001391 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001392 * given VM and vCPU.
1393 */
1394static inline bool is_injection_allowed(uint32_t target_vm_id,
1395 struct vcpu *current)
1396{
1397 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001398
Andrew Walbran318f5732018-11-20 16:23:42 +00001399 /*
1400 * The primary VM is allowed to inject interrupts into any VM. Secondary
1401 * VMs are only allowed to inject interrupts into their own vCPUs.
1402 */
1403 return current_vm_id == HF_PRIMARY_VM_ID ||
1404 current_vm_id == target_vm_id;
1405}
1406
1407/**
1408 * Injects a virtual interrupt of the given ID into the given target vCPU.
1409 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1410 * when the vCPU is next run, which is up to the scheduler.
1411 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001412 * Returns:
1413 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1414 * ID is invalid, or the current VM is not allowed to inject interrupts to
1415 * the target VM.
1416 * - 0 on success if no further action is needed.
1417 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1418 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001419 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001420int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1421 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001422 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001423{
Andrew Walbran318f5732018-11-20 16:23:42 +00001424 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001425 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001426
1427 if (intid >= HF_NUM_INTIDS) {
1428 return -1;
1429 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001430
Andrew Walbran318f5732018-11-20 16:23:42 +00001431 if (target_vm == NULL) {
1432 return -1;
1433 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001434
Andrew Walbran318f5732018-11-20 16:23:42 +00001435 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001436 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001437 return -1;
1438 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001439
Andrew Walbran318f5732018-11-20 16:23:42 +00001440 if (!is_injection_allowed(target_vm_id, current)) {
1441 return -1;
1442 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001443
Andrew Walbrane1310df2019-04-29 17:28:28 +01001444 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001445
Andrew Walbran17eebf92020-02-05 16:35:49 +00001446 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1447 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1448 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001449 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001450}
Andrew Scull6386f252018-12-06 13:29:10 +00001451
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001452/** Returns the version of the implemented FF-A specification. */
1453struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001454{
1455 /*
1456 * Ensure that both major and minor revision representation occupies at
1457 * most 15 bits.
1458 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001460 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001462 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001464 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001465 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001466 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001467
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001468 return (struct ffa_value){
1469 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1470 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001471}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001472
1473int64_t api_debug_log(char c, struct vcpu *current)
1474{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001475 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001476 struct vm *vm = current->vm;
1477 struct vm_locked vm_locked = vm_lock(vm);
1478
Andrew Sculld54e1be2019-08-20 11:09:42 +01001479 if (c == '\n' || c == '\0') {
1480 flush = true;
1481 } else {
1482 vm->log_buffer[vm->log_buffer_length++] = c;
1483 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1484 }
1485
1486 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001487 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1488 vm->log_buffer_length);
1489 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001490 }
1491
1492 vm_unlock(&vm_locked);
1493
1494 return 0;
1495}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001496
1497/**
1498 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001499 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001500 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001501struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001502{
1503 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001504 case FFA_ERROR_32:
1505 case FFA_SUCCESS_32:
1506 case FFA_INTERRUPT_32:
1507 case FFA_VERSION_32:
1508 case FFA_FEATURES_32:
1509 case FFA_RX_RELEASE_32:
1510 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001511 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001512 case FFA_ID_GET_32:
1513 case FFA_MSG_POLL_32:
1514 case FFA_MSG_WAIT_32:
1515 case FFA_YIELD_32:
1516 case FFA_RUN_32:
1517 case FFA_MSG_SEND_32:
1518 case FFA_MEM_DONATE_32:
1519 case FFA_MEM_LEND_32:
1520 case FFA_MEM_SHARE_32:
1521 case FFA_MEM_RETRIEVE_REQ_32:
1522 case FFA_MEM_RETRIEVE_RESP_32:
1523 case FFA_MEM_RELINQUISH_32:
1524 case FFA_MEM_RECLAIM_32:
1525 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001526 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001527 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001528 }
1529}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001530
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1532 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001533 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001534{
1535 struct vm *from = current->vm;
1536 struct vm *to;
1537 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 struct ffa_memory_region *memory_region;
1539 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001540
1541 if (ipa_addr(address) != 0 || page_count != 0) {
1542 /*
1543 * Hafnium only supports passing the descriptor in the TX
1544 * mailbox.
1545 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001547 }
1548
Andrew Walbranca808b12020-05-15 17:22:28 +01001549 if (fragment_length > length) {
1550 dlog_verbose(
1551 "Fragment length %d greater than total length %d.\n",
1552 fragment_length, length);
1553 return ffa_error(FFA_INVALID_PARAMETERS);
1554 }
1555 if (fragment_length < sizeof(struct ffa_memory_region) +
1556 sizeof(struct ffa_memory_access)) {
1557 dlog_verbose(
1558 "Initial fragment length %d smaller than header size "
1559 "%d.\n",
1560 fragment_length,
1561 sizeof(struct ffa_memory_region) +
1562 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001563 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001564 }
1565
1566 /*
1567 * Check that the sender has configured its send buffer. If the TX
1568 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1569 * be safely accessed after releasing the lock since the TX mailbox
1570 * address can only be configured once.
1571 */
1572 sl_lock(&from->lock);
1573 from_msg = from->mailbox.send;
1574 sl_unlock(&from->lock);
1575
1576 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001577 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001578 }
1579
1580 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001581 * Copy the memory region descriptor to a fresh page from the memory
1582 * pool. This prevents the sender from changing it underneath us, and
1583 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001584 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001585 if (fragment_length > HF_MAILBOX_SIZE ||
1586 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001587 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001588 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001589 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001590 if (memory_region == NULL) {
1591 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001592 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001593 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001594 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001595
1596 /* The sender must match the caller. */
1597 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001598 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001600 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001601 }
1602
Andrew Walbrana65a1322020-04-06 19:32:32 +01001603 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001604 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001605 dlog_verbose(
1606 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001607 "endpoint memory access descriptors, expected 1).\n",
1608 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001609 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001610 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001611 }
1612
1613 /*
1614 * Ensure that the receiver VM exists and isn't the same as the sender.
1615 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001617 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001618 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001619 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001620 goto out;
1621 }
1622
1623 if (to->id == HF_TEE_VM_ID) {
1624 /*
1625 * The 'to' VM lock is only needed in the case that it is the
1626 * TEE VM.
1627 */
1628 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1629
1630 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001631 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001632 goto out_unlock;
1633 }
1634
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001635 ret = ffa_memory_tee_send(
1636 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1637 length, fragment_length, share_func, &api_page_pool);
1638 /*
1639 * ffa_tee_memory_send takes ownership of the memory_region, so
1640 * make sure we don't free it.
1641 */
1642 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001643
1644 out_unlock:
1645 vm_unlock(&vm_to_from_lock.vm1);
1646 vm_unlock(&vm_to_from_lock.vm2);
1647 } else {
1648 struct vm_locked from_locked = vm_lock(from);
1649
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001650 ret = ffa_memory_send(from_locked, memory_region, length,
1651 fragment_length, share_func,
1652 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001654 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 * make sure we don't free it.
1656 */
1657 memory_region = NULL;
1658
1659 vm_unlock(&from_locked);
1660 }
1661
1662out:
1663 if (memory_region != NULL) {
1664 mpool_free(&api_page_pool, memory_region);
1665 }
1666
1667 return ret;
1668}
1669
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1671 uint32_t fragment_length,
1672 ipaddr_t address, uint32_t page_count,
1673 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001674{
1675 struct vm *to = current->vm;
1676 struct vm_locked to_locked;
1677 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001678 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001679 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001680 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001681
1682 if (ipa_addr(address) != 0 || page_count != 0) {
1683 /*
1684 * Hafnium only supports passing the descriptor in the TX
1685 * mailbox.
1686 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001687 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001688 }
1689
Andrew Walbrana65a1322020-04-06 19:32:32 +01001690 if (fragment_length != length) {
1691 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001692 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001693 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001694
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001695 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001696 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001697 message_buffer_size = cpu_get_buffer_size(current->cpu);
1698 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1699 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001700 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001701 }
1702
1703 to_locked = vm_lock(to);
1704 to_msg = to->mailbox.send;
1705
1706 if (to_msg == NULL) {
1707 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001708 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001709 goto out;
1710 }
1711
1712 /*
1713 * Copy the retrieve request descriptor to an internal buffer, so that
1714 * the caller can't change it underneath us.
1715 */
1716 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1717
1718 if (msg_receiver_busy(to_locked, NULL, false)) {
1719 /*
1720 * Can't retrieve memory information if the mailbox is not
1721 * available.
1722 */
1723 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001724 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001725 goto out;
1726 }
1727
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001728 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1729 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001730
1731out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001732 vm_unlock(&to_locked);
1733 return ret;
1734}
1735
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001736struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001737{
1738 struct vm *from = current->vm;
1739 struct vm_locked from_locked;
1740 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001741 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001742 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001743 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001744 uint32_t length;
1745
1746 from_locked = vm_lock(from);
1747 from_msg = from->mailbox.send;
1748
1749 if (from_msg == NULL) {
1750 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001751 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001752 goto out;
1753 }
1754
1755 /*
1756 * Calculate length from relinquish descriptor before copying. We will
1757 * check again later to make sure it hasn't changed.
1758 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001759 length = sizeof(struct ffa_mem_relinquish) +
1760 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
1761 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001762 /*
1763 * Copy the relinquish descriptor to an internal buffer, so that the
1764 * caller can't change it underneath us.
1765 */
1766 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001767 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001768 message_buffer_size = cpu_get_buffer_size(current->cpu);
1769 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1770 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001771 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001772 goto out;
1773 }
1774 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
1775
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001776 if (sizeof(struct ffa_mem_relinquish) +
1777 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001778 length) {
1779 dlog_verbose(
1780 "Endpoint count changed while copying to internal "
1781 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001783 goto out;
1784 }
1785
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001786 ret = ffa_memory_relinquish(from_locked, relinquish_request,
1787 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001788
1789out:
1790 vm_unlock(&from_locked);
1791 return ret;
1792}
1793
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001794struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
1795 ffa_memory_region_flags_t flags,
1796 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001797{
1798 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001799 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001800
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001801 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1802 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001803 struct vm_locked to_locked = vm_lock(to);
1804
Andrew Walbranca808b12020-05-15 17:22:28 +01001805 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001806 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001807
Andrew Walbran290b0c92020-02-03 16:37:14 +00001808 vm_unlock(&to_locked);
1809 } else {
1810 struct vm *from = vm_find(HF_TEE_VM_ID);
1811 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1812
Andrew Walbranca808b12020-05-15 17:22:28 +01001813 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
1814 vm_to_from_lock.vm2, handle, flags,
1815 &api_page_pool);
1816
1817 vm_unlock(&vm_to_from_lock.vm1);
1818 vm_unlock(&vm_to_from_lock.vm2);
1819 }
1820
1821 return ret;
1822}
1823
1824struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
1825 uint32_t fragment_offset,
1826 ffa_vm_id_t sender_vm_id,
1827 struct vcpu *current)
1828{
1829 struct vm *to = current->vm;
1830 struct vm_locked to_locked;
1831 struct ffa_value ret;
1832
1833 /* Sender ID MBZ at virtual instance. */
1834 if (sender_vm_id != 0) {
1835 return ffa_error(FFA_INVALID_PARAMETERS);
1836 }
1837
1838 to_locked = vm_lock(to);
1839
1840 if (msg_receiver_busy(to_locked, NULL, false)) {
1841 /*
1842 * Can't retrieve memory information if the mailbox is not
1843 * available.
1844 */
1845 dlog_verbose("RX buffer not ready.\n");
1846 ret = ffa_error(FFA_BUSY);
1847 goto out;
1848 }
1849
1850 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
1851 &api_page_pool);
1852
1853out:
1854 vm_unlock(&to_locked);
1855 return ret;
1856}
1857
1858struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
1859 uint32_t fragment_length,
1860 ffa_vm_id_t sender_vm_id,
1861 struct vcpu *current)
1862{
1863 struct vm *from = current->vm;
1864 const void *from_msg;
1865 void *fragment_copy;
1866 struct ffa_value ret;
1867
1868 /* Sender ID MBZ at virtual instance. */
1869 if (sender_vm_id != 0) {
1870 return ffa_error(FFA_INVALID_PARAMETERS);
1871 }
1872
1873 /*
1874 * Check that the sender has configured its send buffer. If the TX
1875 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1876 * be safely accessed after releasing the lock since the TX mailbox
1877 * address can only be configured once.
1878 */
1879 sl_lock(&from->lock);
1880 from_msg = from->mailbox.send;
1881 sl_unlock(&from->lock);
1882
1883 if (from_msg == NULL) {
1884 return ffa_error(FFA_INVALID_PARAMETERS);
1885 }
1886
1887 /*
1888 * Copy the fragment to a fresh page from the memory pool. This prevents
1889 * the sender from changing it underneath us, and also lets us keep it
1890 * around in the share state table if needed.
1891 */
1892 if (fragment_length > HF_MAILBOX_SIZE ||
1893 fragment_length > MM_PPOOL_ENTRY_SIZE) {
1894 dlog_verbose(
1895 "Fragment length %d larger than mailbox size %d.\n",
1896 fragment_length, HF_MAILBOX_SIZE);
1897 return ffa_error(FFA_INVALID_PARAMETERS);
1898 }
1899 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
1900 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1901 0) {
1902 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
1903 return ffa_error(FFA_INVALID_PARAMETERS);
1904 }
1905 fragment_copy = mpool_alloc(&api_page_pool);
1906 if (fragment_copy == NULL) {
1907 dlog_verbose("Failed to allocate fragment copy.\n");
1908 return ffa_error(FFA_NO_MEMORY);
1909 }
1910 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
1911
1912 /*
1913 * Hafnium doesn't support fragmentation of memory retrieve requests
1914 * (because it doesn't support caller-specified mappings, so a request
1915 * will never be larger than a single page), so this must be part of a
1916 * memory send (i.e. donate, lend or share) request.
1917 *
1918 * We can tell from the handle whether the memory transaction is for the
1919 * TEE or not.
1920 */
1921 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1922 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
1923 struct vm_locked from_locked = vm_lock(from);
1924
1925 ret = ffa_memory_send_continue(from_locked, fragment_copy,
1926 fragment_length, handle,
1927 &api_page_pool);
1928 /*
1929 * `ffa_memory_send_continue` takes ownership of the
1930 * fragment_copy, so we don't need to free it here.
1931 */
1932 vm_unlock(&from_locked);
1933 } else {
1934 struct vm *to = vm_find(HF_TEE_VM_ID);
1935 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1936
1937 /*
1938 * The TEE RX buffer state is checked in
1939 * `ffa_memory_tee_send_continue` rather than here, as we need
1940 * to return `FFA_MEM_FRAG_RX` with the current offset rather
1941 * than FFA_ERROR FFA_BUSY in case it is busy.
1942 */
1943
1944 ret = ffa_memory_tee_send_continue(
1945 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
1946 fragment_length, handle, &api_page_pool);
1947 /*
1948 * `ffa_memory_tee_send_continue` takes ownership of the
1949 * fragment_copy, so we don't need to free it here.
1950 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00001951
1952 vm_unlock(&vm_to_from_lock.vm1);
1953 vm_unlock(&vm_to_from_lock.vm2);
1954 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001955
1956 return ret;
1957}