blob: 04c9de9fccdbbd441e7229e3784b6e74ba457fb2 [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 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200419static int64_t internal_interrupt_inject_locked(
420 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
421 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000422{
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
Andrew Walbran508e63c2018-12-20 17:02:37 +0000427 /*
428 * We only need to change state and (maybe) trigger a virtual IRQ if it
429 * is enabled and was not previously pending. Otherwise we can skip
430 * everything except setting the pending bit.
431 *
432 * If you change this logic make sure to update the need_vm_lock logic
433 * above to match.
434 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200435 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
436 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000437 intid_mask)) {
438 goto out;
439 }
440
441 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200442 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000443
444 /*
445 * Only need to update state if there was not already an
446 * interrupt enabled and pending.
447 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200448 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000449 goto out;
450 }
451
Andrew Walbran508e63c2018-12-20 17:02:37 +0000452 if (current->vm->id == HF_PRIMARY_VM_ID) {
453 /*
454 * If the call came from the primary VM, let it know that it
455 * should run or kick the target vCPU.
456 */
457 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200458 } else if (current != target_locked.vcpu && next != NULL) {
459 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000460 }
461
462out:
463 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200464 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
465 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000466
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200467 return ret;
468}
469
470/* Wrapper to internal_interrupt_inject with locking of target vCPU */
471static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
472 uint32_t intid, struct vcpu *current,
473 struct vcpu **next)
474{
475 int64_t ret;
476 struct vcpu_locked target_locked;
477
478 target_locked = vcpu_lock(target_vcpu);
479 ret = internal_interrupt_inject_locked(target_locked, intid, current,
480 next);
481 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000482
483 return ret;
484}
485
486/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100487 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
488 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100489 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100491{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000492 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100493 case FFA_MSG_SEND_32:
494 return (struct ffa_value){
495 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000496 .arg1 = (receiver->mailbox.recv_sender << 16) |
497 receiver->id,
498 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000499 default:
500 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000501 dlog_error("Tried to return an invalid message function %#x\n",
502 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000504 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100505}
506
507/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000508 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000509 * value needs to be forced onto the vCPU.
510 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000511static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000513{
Andrew Scullb06d1752019-02-04 10:15:48 +0000514 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000515 bool ret;
516
Andrew Scullb06d1752019-02-04 10:15:48 +0000517 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000518 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000519 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100520 * The VM lock is not needed in the common case so it must only be taken
521 * when it is going to be needed. This ensures there are no inter-vCPU
522 * dependencies in the common run case meaning the sensitive context
523 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000524 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000525 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000526
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000527 /* The VM needs to be locked to deliver mailbox messages. */
528 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
529 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000530 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000531 sl_lock(&vcpu->vm->lock);
532 sl_lock(&vcpu->lock);
533 }
534
535 /*
536 * If the vCPU is already running somewhere then we can't run it here
537 * simultaneously. While it is actually running then the state should be
538 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
539 * stops running but while Hafnium is in the process of switching back
540 * to the primary there will be a brief period while the state has been
541 * updated but `regs_available` is still false (until
542 * `api_regs_state_saved` is called). We can't start running it again
543 * until this has finished, so count this state as still running for the
544 * purposes of this check.
545 */
546 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
547 /*
548 * vCPU is running on another pCPU.
549 *
550 * It's okay not to return the sleep duration here because the
551 * other physical CPU that is currently running this vCPU will
552 * return the sleep duration if needed.
553 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000555 ret = false;
556 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000557 }
Andrew Scull9726c252019-01-23 13:44:19 +0000558
559 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100560 if (vcpu->state != VCPU_STATE_ABORTED) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000561 dlog_notice("Aborting VM %u vCPU %u\n", vcpu->vm->id,
562 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100563 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000564 }
565 ret = false;
566 goto out;
567 }
568
Andrew Walbran508e63c2018-12-20 17:02:37 +0000569 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100570 case VCPU_STATE_RUNNING:
571 case VCPU_STATE_OFF:
572 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000573 ret = false;
574 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000575
Andrew Sculld6ee1102019-04-05 22:12:42 +0100576 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000577 /*
578 * A pending message allows the vCPU to run so the message can
579 * be delivered directly.
580 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100581 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100582 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100584 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000585 break;
586 }
587 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100588 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000589 /* Allow virtual interrupts to be delivered. */
590 if (vcpu->interrupts.enabled_and_pending_count > 0) {
591 break;
592 }
593
Andrew Walbran508e63c2018-12-20 17:02:37 +0000594 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000595 uint64_t timer_remaining_ns =
596 arch_timer_remaining_ns(&vcpu->regs);
597
598 /*
599 * The timer expired so allow the interrupt to be
600 * delivered.
601 */
602 if (timer_remaining_ns == 0) {
603 break;
604 }
605
606 /*
607 * The vCPU is not ready to run, return the appropriate
608 * code to the primary which called vcpu_run.
609 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100610 run_ret->func =
Andrew Sculld6ee1102019-04-05 22:12:42 +0100611 vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 ? FFA_MSG_WAIT_32
613 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000614 run_ret->arg1 =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100615 ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000616 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000617 }
618
619 ret = false;
620 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000621
Andrew Sculld6ee1102019-04-05 22:12:42 +0100622 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000623 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000624 }
625
Andrew Scullb06d1752019-02-04 10:15:48 +0000626 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000627 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100628 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000629
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000630 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000631 * Mark the registers as unavailable now that we're about to reflect
632 * them onto the real registers. This will also prevent another physical
633 * CPU from trying to read these registers.
634 */
635 vcpu->regs_available = false;
636
637 ret = true;
638
639out:
640 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000641 if (need_vm_lock) {
642 sl_unlock(&vcpu->vm->lock);
643 }
644
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000645 return ret;
646}
647
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
649 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100650{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100651 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100652 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100653 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100654
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000655 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100656 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100657 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100658 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100659 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100660
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000661 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100662 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100663 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100664 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100665
Andrew Scull19503262018-09-20 14:48:39 +0100666 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100667 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100668 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100669 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100670 }
671
Fuad Tabbaed294af2019-12-20 10:43:01 +0000672 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100673 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100674 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100675 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100676
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000677 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100678 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000679 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000680 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100681 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000682
Andrew Walbran508e63c2018-12-20 17:02:37 +0000683 /*
684 * Inject timer interrupt if timer has expired. It's safe to access
685 * vcpu->regs here because api_vcpu_prepare_run already made sure that
686 * regs_available was true (and then set it to false) before returning
687 * true.
688 */
689 if (arch_timer_pending(&vcpu->regs)) {
690 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100691 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
692 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000693
694 /*
695 * Set the mask bit so the hardware interrupt doesn't fire
696 * again. Ideally we wouldn't do this because it affects what
697 * the secondary vCPU sees, but if we don't then we end up with
698 * a loop of the interrupt firing each time we try to return to
699 * the secondary vCPU.
700 */
701 arch_timer_mask(&vcpu->regs);
702 }
703
Fuad Tabbaed294af2019-12-20 10:43:01 +0000704 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000705 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000706
Andrew Scull33fecd32019-01-08 14:48:27 +0000707 /*
708 * Set a placeholder return code to the scheduler. This will be
709 * overwritten when the switch back to the primary occurs.
710 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 ret.func = FFA_INTERRUPT_32;
712 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100713 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000714
Andrew Scull6d2db332018-10-10 15:28:17 +0100715out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100716 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100717}
718
719/**
Andrew Scull81e85092018-12-12 12:56:20 +0000720 * Check that the mode indicates memory that is valid, owned and exclusive.
721 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100722static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000723{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100724 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
725 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000726}
727
728/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100729 * Determines the value to be returned by api_vm_configure and ffa_rx_release
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000730 * after they've succeeded. If a secondary VM is running and there are waiters,
731 * it also switches back to the primary VM for it to wake waiters up.
732 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100733static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
734 struct vcpu *current,
735 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000736{
737 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000738
739 if (list_empty(&vm->mailbox.waiter_list)) {
740 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000742 }
743
744 if (vm->id == HF_PRIMARY_VM_ID) {
745 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100746 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000747 }
748
749 /*
750 * Switch back to the primary VM, informing it that there are waiters
751 * that need to be notified.
752 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000753 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100754 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000755 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000756
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100757 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000758}
759
760/**
Andrew Sculle1322792019-07-01 17:46:10 +0100761 * Configures the hypervisor's stage-1 view of the send and receive pages. The
762 * stage-1 page tables must be locked so memory cannot be taken by another core
763 * which could result in this transaction being unable to roll back in the case
764 * of an error.
765 */
766static bool api_vm_configure_stage1(struct vm_locked vm_locked,
767 paddr_t pa_send_begin, paddr_t pa_send_end,
768 paddr_t pa_recv_begin, paddr_t pa_recv_end,
769 struct mpool *local_page_pool)
770{
771 bool ret;
772 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
773
774 /* Map the send page as read-only in the hypervisor address space. */
775 vm_locked.vm->mailbox.send =
776 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
777 MM_MODE_R, local_page_pool);
778 if (!vm_locked.vm->mailbox.send) {
779 /* TODO: partial defrag of failed range. */
780 /* Recover any memory consumed in failed mapping. */
781 mm_defrag(mm_stage1_locked, local_page_pool);
782 goto fail;
783 }
784
785 /*
786 * Map the receive page as writable in the hypervisor address space. On
787 * failure, unmap the send page before returning.
788 */
789 vm_locked.vm->mailbox.recv =
790 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
791 MM_MODE_W, local_page_pool);
792 if (!vm_locked.vm->mailbox.recv) {
793 /* TODO: partial defrag of failed range. */
794 /* Recover any memory consumed in failed mapping. */
795 mm_defrag(mm_stage1_locked, local_page_pool);
796 goto fail_undo_send;
797 }
798
799 ret = true;
800 goto out;
801
802 /*
803 * The following mappings will not require more memory than is available
804 * in the local pool.
805 */
806fail_undo_send:
807 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100808 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
809 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100810
811fail:
812 ret = false;
813
814out:
815 mm_unlock_stage1(&mm_stage1_locked);
816
817 return ret;
818}
819
820/**
821 * Configures the send and receive pages in the VM stage-2 and hypervisor
822 * stage-1 page tables. Locking of the page tables combined with a local memory
823 * pool ensures there will always be enough memory to recover from any errors
824 * that arise.
825 */
826static bool api_vm_configure_pages(struct vm_locked vm_locked,
827 paddr_t pa_send_begin, paddr_t pa_send_end,
Andrew Walbran1281ed42019-10-22 17:23:40 +0100828 uint32_t orig_send_mode,
829 paddr_t pa_recv_begin, paddr_t pa_recv_end,
830 uint32_t orig_recv_mode)
Andrew Sculle1322792019-07-01 17:46:10 +0100831{
832 bool ret;
833 struct mpool local_page_pool;
834
835 /*
836 * Create a local pool so any freed memory can't be used by another
837 * thread. This is to ensure the original mapping can be restored if any
838 * stage of the process fails.
839 */
840 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
841
842 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000843 if (!vm_identity_map(
844 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100845 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Andrew Walbran8ec2b9f2019-11-25 15:05:40 +0000846 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100847 goto fail;
848 }
849
Andrew Scull3c257452019-11-26 13:32:50 +0000850 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
851 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
852 &local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100853 /* TODO: partial defrag of failed range. */
854 /* Recover any memory consumed in failed mapping. */
855 mm_vm_defrag(&vm_locked.vm->ptable, &local_page_pool);
856 goto fail_undo_send;
857 }
858
859 if (!api_vm_configure_stage1(vm_locked, pa_send_begin, pa_send_end,
860 pa_recv_begin, pa_recv_end,
861 &local_page_pool)) {
862 goto fail_undo_send_and_recv;
863 }
864
865 ret = true;
866 goto out;
867
868 /*
869 * The following mappings will not require more memory than is available
870 * in the local pool.
871 */
872fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000873 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
874 orig_recv_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100875
876fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000877 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
878 orig_send_mode, &local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100879
880fail:
881 ret = false;
882
883out:
884 mpool_fini(&local_page_pool);
885
886 return ret;
887}
888
889/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100890 * Configures the VM to send/receive data through the specified pages. The pages
891 * must not be shared.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000892 *
893 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000895 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000897 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000899 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100900 * - FFA_SUCCESS on success if no further action is needed.
901 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000902 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100903 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100904struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
905 uint32_t page_count, struct vcpu *current,
906 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100907{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100908 struct vm *vm = current->vm;
Andrew Sculle1322792019-07-01 17:46:10 +0100909 struct vm_locked vm_locked;
Andrew Scull80871322018-08-06 12:04:09 +0100910 paddr_t pa_send_begin;
911 paddr_t pa_send_end;
912 paddr_t pa_recv_begin;
913 paddr_t pa_recv_end;
Andrew Walbran1281ed42019-10-22 17:23:40 +0100914 uint32_t orig_send_mode;
915 uint32_t orig_recv_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100916 struct ffa_value ret;
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000917
918 /* Hafnium only supports a fixed size of RX/TX buffers. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
920 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000921 }
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100922
923 /* Fail if addresses are not page-aligned. */
Alfredo Mazzinghieb1997c2019-02-07 18:00:01 +0000924 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
925 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100927 }
928
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000929 /* Convert to physical addresses. */
930 pa_send_begin = pa_from_ipa(send);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000931 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000932
933 pa_recv_begin = pa_from_ipa(recv);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000934 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
Andrew Scullc2eb6a32018-12-13 16:54:24 +0000935
Andrew Scullc9ccb3f2018-08-13 15:27:12 +0100936 /* Fail if the same page is used for the send and receive pages. */
937 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Scull220e6212018-12-21 18:09:00 +0000939 }
940
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100941 /*
942 * The hypervisor's memory map must be locked for the duration of this
943 * operation to ensure there will be sufficient memory to recover from
944 * any failures.
945 *
Fuad Tabba9dc276f2020-07-16 09:29:32 +0100946 * TODO: the scope can be reduced but will require restructuring to
947 * keep a single unlock point.
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100948 */
Andrew Sculle1322792019-07-01 17:46:10 +0100949 vm_locked = vm_lock(vm);
Andrew Scull220e6212018-12-21 18:09:00 +0000950
951 /* We only allow these to be setup once. */
952 if (vm->mailbox.send || vm->mailbox.recv) {
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 /*
958 * Ensure the pages are valid, owned and exclusive to the VM and that
959 * the VM has the required access to the memory.
960 */
961 if (!mm_vm_get_mode(&vm->ptable, send, ipa_add(send, PAGE_SIZE),
962 &orig_send_mode) ||
963 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
964 (orig_send_mode & MM_MODE_R) == 0 ||
965 (orig_send_mode & MM_MODE_W) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100966 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000967 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000968 }
969
970 if (!mm_vm_get_mode(&vm->ptable, recv, ipa_add(recv, PAGE_SIZE),
971 &orig_recv_mode) ||
972 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
973 (orig_recv_mode & MM_MODE_R) == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 ret = ffa_error(FFA_DENIED);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000975 goto exit;
Andrew Scull220e6212018-12-21 18:09:00 +0000976 }
977
Andrew Sculle1322792019-07-01 17:46:10 +0100978 if (!api_vm_configure_pages(vm_locked, pa_send_begin, pa_send_end,
979 orig_send_mode, pa_recv_begin, pa_recv_end,
980 orig_recv_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000982 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100983 }
984
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000985 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +0100986 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +0000987
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100988exit:
Andrew Sculle1322792019-07-01 17:46:10 +0100989 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100990
991 return ret;
992}
993
994/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +0100995 * Notifies the `to` VM about the message currently in its mailbox, possibly
996 * with the help of the primary VM.
997 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100998static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
999 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001000{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1002 struct ffa_value primary_ret = {
1003 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001004 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001005 };
1006
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001007 /* Messages for the primary VM are delivered directly. */
1008 if (to.vm->id == HF_PRIMARY_VM_ID) {
1009 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001010 * Only tell the primary VM the size and other details if the
1011 * message is for it, to avoid leaking data about messages for
1012 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001013 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001015
1016 to.vm->mailbox.state = MAILBOX_STATE_READ;
1017 *next = api_switch_to_primary(current, primary_ret,
1018 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001019 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001020 }
1021
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001022 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1023
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001024 /* Messages for the TEE are sent on via the dispatcher. */
1025 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001027
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001028 ret = arch_tee_call(call);
1029 /*
1030 * After the call to the TEE completes it must have finished
1031 * reading its RX buffer, so it is ready for another message.
1032 */
1033 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001034 /*
1035 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001037 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001038 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001039 }
1040
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001041 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001042 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001043 *next = api_switch_to_primary(current, primary_ret,
1044 VCPU_STATE_READY);
1045 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001046
1047 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001048}
1049
1050/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001051 * Copies data from the sender's send buffer to the recipient's receive buffer
1052 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001053 *
1054 * If the recipient's receive buffer is busy, it can optionally register the
1055 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001056 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001057struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1058 ffa_vm_id_t receiver_vm_id, uint32_t size,
1059 uint32_t attributes, struct vcpu *current,
1060 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001061{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001062 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001063 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001064 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001065 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001066 struct ffa_value ret;
1067 bool notify =
1068 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001069
Andrew Walbran70bc8622019-10-07 14:15:58 +01001070 /* Ensure sender VM ID corresponds to the current VM. */
1071 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001073 }
1074
1075 /* Disallow reflexive requests as this suggests an error in the VM. */
1076 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001077 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001078 }
1079
1080 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001081 if (size > FFA_MSG_PAYLOAD_MAX) {
1082 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001083 }
1084
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001085 /* Ensure the receiver VM exists. */
1086 to = vm_find(receiver_vm_id);
1087 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001088 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001089 }
1090
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001091 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001092 * Check that the sender has configured its send buffer. If the tx
1093 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1094 * be safely accessed after releasing the lock since the tx mailbox
1095 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001096 */
1097 sl_lock(&from->lock);
1098 from_msg = from->mailbox.send;
1099 sl_unlock(&from->lock);
1100
1101 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001102 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001103 }
1104
Andrew Walbran82d6d152019-12-24 15:02:06 +00001105 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001106
Andrew Walbran82d6d152019-12-24 15:02:06 +00001107 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001108 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001109 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001110 }
1111
Andrew Walbran82d6d152019-12-24 15:02:06 +00001112 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001113 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001114 to->mailbox.recv_size = size;
1115 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001117 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001118
1119out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001120 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001121
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001122 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001123}
1124
1125/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001126 * Checks whether the vCPU's attempt to block for a message has already been
1127 * interrupted or whether it is allowed to block.
1128 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001130{
1131 bool interrupted;
1132
1133 sl_lock(&current->lock);
1134
1135 /*
1136 * Don't block if there are enabled and pending interrupts, to match
1137 * behaviour of wait_for_interrupt.
1138 */
1139 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1140
1141 sl_unlock(&current->lock);
1142
1143 return interrupted;
1144}
1145
1146/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001147 * Receives a message from the mailbox. If one isn't available, this function
1148 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001149 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001150 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001151 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001152struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1153 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001154{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001155 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001156 struct ffa_value return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001157
Andrew Scullaa039b32018-10-04 15:02:26 +01001158 /*
1159 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001160 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001161 */
Andrew Scull19503262018-09-20 14:48:39 +01001162 if (vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001163 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001164 }
1165
1166 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001167
Andrew Scullaa039b32018-10-04 15:02:26 +01001168 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001169 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1170 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001171 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001172 goto out;
1173 }
1174
1175 /* No pending message so fail if not allowed to block. */
1176 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001177 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001178 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001179 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001180
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001181 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001182 * From this point onward this call can only be interrupted or a message
1183 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001185 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 return_code = ffa_error(FFA_INTERRUPTED);
1187 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001188 goto out;
1189 }
1190
Fuad Tabbaed294af2019-12-20 10:43:01 +00001191 /* Switch back to primary VM to block. */
Andrew Walbranb4816552018-12-05 17:35:42 +00001192 {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193 struct ffa_value run_return = {
1194 .func = FFA_MSG_WAIT_32,
1195 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001196 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001197
Andrew Walbranb4816552018-12-05 17:35:42 +00001198 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001199 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001200 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001201out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001202 sl_unlock(&vm->lock);
1203
Jose Marinho3e2442f2019-03-12 13:30:37 +00001204 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001205}
1206
1207/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001208 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1209 * by this function, the caller must have called api_mailbox_send before with
1210 * the notify argument set to true, and this call must have failed because the
1211 * mailbox was not available.
1212 *
1213 * It should be called repeatedly to retrieve a list of VMs.
1214 *
1215 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1216 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001217 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001218int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001219{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001220 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001221 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001222 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001223
1224 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001225 if (list_empty(&vm->mailbox.ready_list)) {
1226 ret = -1;
1227 goto exit;
1228 }
1229
1230 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1231 ready_links);
1232 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001233 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001234
1235exit:
1236 sl_unlock(&vm->lock);
1237 return ret;
1238}
1239
1240/**
1241 * Retrieves the next VM waiting to be notified that the mailbox of the
1242 * specified VM became writable. Only primary VMs are allowed to call this.
1243 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001244 * Returns -1 on failure or if there are no waiters; the VM id of the next
1245 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001246 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001247int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001248{
1249 struct vm *vm;
1250 struct vm_locked locked;
1251 struct wait_entry *entry;
1252 struct vm *waiting_vm;
1253
1254 /* Only primary VMs are allowed to call this function. */
1255 if (current->vm->id != HF_PRIMARY_VM_ID) {
1256 return -1;
1257 }
1258
Andrew Walbran42347a92019-05-09 13:59:03 +01001259 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001260 if (vm == NULL) {
1261 return -1;
1262 }
1263
Fuad Tabbaed294af2019-12-20 10:43:01 +00001264 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001265 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001266 entry = api_fetch_waiter(locked);
1267 vm_unlock(&locked);
1268
1269 if (entry == NULL) {
1270 return -1;
1271 }
1272
1273 /* Enqueue notification to waiting VM. */
1274 waiting_vm = entry->waiting_vm;
1275
1276 sl_lock(&waiting_vm->lock);
1277 if (list_empty(&entry->ready_links)) {
1278 list_append(&waiting_vm->mailbox.ready_list,
1279 &entry->ready_links);
1280 }
1281 sl_unlock(&waiting_vm->lock);
1282
1283 return waiting_vm->id;
1284}
1285
1286/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001287 * Releases the caller's mailbox so that a new message can be received. The
1288 * caller must have copied out all data they wish to preserve as new messages
1289 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001290 *
1291 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1293 * - FFA_SUCCESS on success if no further action is needed.
1294 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001295 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001296 * hf_mailbox_waiter_get.
1297 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001298struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001299{
1300 struct vm *vm = current->vm;
1301 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001302 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001303
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001304 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001305 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001306 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001307 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001308 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001309 break;
1310
Andrew Sculld6ee1102019-04-05 22:12:42 +01001311 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001312 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001313 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001314 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001315 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001316 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001317
1318 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001319}
Andrew Walbran318f5732018-11-20 16:23:42 +00001320
1321/**
1322 * Enables or disables a given interrupt ID for the calling vCPU.
1323 *
1324 * Returns 0 on success, or -1 if the intid is invalid.
1325 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001326int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001327{
1328 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001329 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001330
Andrew Walbran318f5732018-11-20 16:23:42 +00001331 if (intid >= HF_NUM_INTIDS) {
1332 return -1;
1333 }
1334
1335 sl_lock(&current->lock);
1336 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001337 /*
1338 * If it is pending and was not enabled before, increment the
1339 * count.
1340 */
1341 if (current->interrupts.interrupt_pending[intid_index] &
1342 ~current->interrupts.interrupt_enabled[intid_index] &
1343 intid_mask) {
1344 current->interrupts.enabled_and_pending_count++;
1345 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001346 current->interrupts.interrupt_enabled[intid_index] |=
1347 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001348 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001349 /*
1350 * If it is pending and was enabled before, decrement the count.
1351 */
1352 if (current->interrupts.interrupt_pending[intid_index] &
1353 current->interrupts.interrupt_enabled[intid_index] &
1354 intid_mask) {
1355 current->interrupts.enabled_and_pending_count--;
1356 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001357 current->interrupts.interrupt_enabled[intid_index] &=
1358 ~intid_mask;
1359 }
1360
1361 sl_unlock(&current->lock);
1362 return 0;
1363}
1364
1365/**
1366 * Returns the ID of the next pending interrupt for the calling vCPU, and
1367 * acknowledges it (i.e. marks it as no longer pending). Returns
1368 * HF_INVALID_INTID if there are no pending interrupts.
1369 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001370uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001371{
1372 uint8_t i;
1373 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001374
1375 /*
1376 * Find the first enabled and pending interrupt ID, return it, and
1377 * deactivate it.
1378 */
1379 sl_lock(&current->lock);
1380 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1381 uint32_t enabled_and_pending =
1382 current->interrupts.interrupt_enabled[i] &
1383 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001384
Andrew Walbran318f5732018-11-20 16:23:42 +00001385 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001386 uint8_t bit_index = ctz(enabled_and_pending);
1387 /*
1388 * Mark it as no longer pending and decrement the count.
1389 */
1390 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001391 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001392 current->interrupts.enabled_and_pending_count--;
1393 first_interrupt =
1394 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001395 break;
1396 }
1397 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001398
1399 sl_unlock(&current->lock);
1400 return first_interrupt;
1401}
1402
1403/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001404 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001405 * given VM and vCPU.
1406 */
1407static inline bool is_injection_allowed(uint32_t target_vm_id,
1408 struct vcpu *current)
1409{
1410 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001411
Andrew Walbran318f5732018-11-20 16:23:42 +00001412 /*
1413 * The primary VM is allowed to inject interrupts into any VM. Secondary
1414 * VMs are only allowed to inject interrupts into their own vCPUs.
1415 */
1416 return current_vm_id == HF_PRIMARY_VM_ID ||
1417 current_vm_id == target_vm_id;
1418}
1419
1420/**
1421 * Injects a virtual interrupt of the given ID into the given target vCPU.
1422 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1423 * when the vCPU is next run, which is up to the scheduler.
1424 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001425 * Returns:
1426 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1427 * ID is invalid, or the current VM is not allowed to inject interrupts to
1428 * the target VM.
1429 * - 0 on success if no further action is needed.
1430 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1431 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001432 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1434 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001435 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001436{
Andrew Walbran318f5732018-11-20 16:23:42 +00001437 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001438 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001439
1440 if (intid >= HF_NUM_INTIDS) {
1441 return -1;
1442 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001443
Andrew Walbran318f5732018-11-20 16:23:42 +00001444 if (target_vm == NULL) {
1445 return -1;
1446 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001447
Andrew Walbran318f5732018-11-20 16:23:42 +00001448 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001449 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001450 return -1;
1451 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001452
Andrew Walbran318f5732018-11-20 16:23:42 +00001453 if (!is_injection_allowed(target_vm_id, current)) {
1454 return -1;
1455 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001456
Andrew Walbrane1310df2019-04-29 17:28:28 +01001457 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001458
Andrew Walbran17eebf92020-02-05 16:35:49 +00001459 dlog_info("Injecting IRQ %d for VM %d vCPU %d from VM %d vCPU %d\n",
1460 intid, target_vm_id, target_vcpu_idx, current->vm->id,
1461 current->cpu->id);
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001462 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001463}
Andrew Scull6386f252018-12-06 13:29:10 +00001464
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001465/** Returns the version of the implemented FF-A specification. */
1466struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001467{
1468 /*
1469 * Ensure that both major and minor revision representation occupies at
1470 * most 15 bits.
1471 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001473 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001474 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001475 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001477 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001478 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001479 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001480
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001481 return (struct ffa_value){
1482 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1483 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001484}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001485
1486int64_t api_debug_log(char c, struct vcpu *current)
1487{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001488 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001489 struct vm *vm = current->vm;
1490 struct vm_locked vm_locked = vm_lock(vm);
1491
Andrew Sculld54e1be2019-08-20 11:09:42 +01001492 if (c == '\n' || c == '\0') {
1493 flush = true;
1494 } else {
1495 vm->log_buffer[vm->log_buffer_length++] = c;
1496 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1497 }
1498
1499 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001500 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1501 vm->log_buffer_length);
1502 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001503 }
1504
1505 vm_unlock(&vm_locked);
1506
1507 return 0;
1508}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001509
1510/**
1511 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001512 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001513 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001514struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001515{
1516 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001517 case FFA_ERROR_32:
1518 case FFA_SUCCESS_32:
1519 case FFA_INTERRUPT_32:
1520 case FFA_VERSION_32:
1521 case FFA_FEATURES_32:
1522 case FFA_RX_RELEASE_32:
1523 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001524 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001525 case FFA_ID_GET_32:
1526 case FFA_MSG_POLL_32:
1527 case FFA_MSG_WAIT_32:
1528 case FFA_YIELD_32:
1529 case FFA_RUN_32:
1530 case FFA_MSG_SEND_32:
1531 case FFA_MEM_DONATE_32:
1532 case FFA_MEM_LEND_32:
1533 case FFA_MEM_SHARE_32:
1534 case FFA_MEM_RETRIEVE_REQ_32:
1535 case FFA_MEM_RETRIEVE_RESP_32:
1536 case FFA_MEM_RELINQUISH_32:
1537 case FFA_MEM_RECLAIM_32:
1538 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001539 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001541 }
1542}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001543
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001544struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1545 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001546 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001547{
1548 struct vm *from = current->vm;
1549 struct vm *to;
1550 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 struct ffa_memory_region *memory_region;
1552 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001553
1554 if (ipa_addr(address) != 0 || page_count != 0) {
1555 /*
1556 * Hafnium only supports passing the descriptor in the TX
1557 * mailbox.
1558 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001559 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001560 }
1561
Andrew Walbranca808b12020-05-15 17:22:28 +01001562 if (fragment_length > length) {
1563 dlog_verbose(
1564 "Fragment length %d greater than total length %d.\n",
1565 fragment_length, length);
1566 return ffa_error(FFA_INVALID_PARAMETERS);
1567 }
1568 if (fragment_length < sizeof(struct ffa_memory_region) +
1569 sizeof(struct ffa_memory_access)) {
1570 dlog_verbose(
1571 "Initial fragment length %d smaller than header size "
1572 "%d.\n",
1573 fragment_length,
1574 sizeof(struct ffa_memory_region) +
1575 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001576 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001577 }
1578
1579 /*
1580 * Check that the sender has configured its send buffer. If the TX
1581 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1582 * be safely accessed after releasing the lock since the TX mailbox
1583 * address can only be configured once.
1584 */
1585 sl_lock(&from->lock);
1586 from_msg = from->mailbox.send;
1587 sl_unlock(&from->lock);
1588
1589 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001590 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001591 }
1592
1593 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001594 * Copy the memory region descriptor to a fresh page from the memory
1595 * pool. This prevents the sender from changing it underneath us, and
1596 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001597 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001598 if (fragment_length > HF_MAILBOX_SIZE ||
1599 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001600 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001601 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001603 if (memory_region == NULL) {
1604 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001606 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001607 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001608
1609 /* The sender must match the caller. */
1610 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001611 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001612 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001613 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001614 }
1615
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001617 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001618 dlog_verbose(
1619 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001620 "endpoint memory access descriptors, expected 1).\n",
1621 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001622 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001623 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001624 }
1625
1626 /*
1627 * Ensure that the receiver VM exists and isn't the same as the sender.
1628 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001629 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001630 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001631 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001632 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001633 goto out;
1634 }
1635
1636 if (to->id == HF_TEE_VM_ID) {
1637 /*
1638 * The 'to' VM lock is only needed in the case that it is the
1639 * TEE VM.
1640 */
1641 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1642
1643 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001644 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001645 goto out_unlock;
1646 }
1647
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001648 ret = ffa_memory_tee_send(
1649 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1650 length, fragment_length, share_func, &api_page_pool);
1651 /*
1652 * ffa_tee_memory_send takes ownership of the memory_region, so
1653 * make sure we don't free it.
1654 */
1655 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656
1657 out_unlock:
1658 vm_unlock(&vm_to_from_lock.vm1);
1659 vm_unlock(&vm_to_from_lock.vm2);
1660 } else {
1661 struct vm_locked from_locked = vm_lock(from);
1662
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001663 ret = ffa_memory_send(from_locked, memory_region, length,
1664 fragment_length, share_func,
1665 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001666 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001667 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001668 * make sure we don't free it.
1669 */
1670 memory_region = NULL;
1671
1672 vm_unlock(&from_locked);
1673 }
1674
1675out:
1676 if (memory_region != NULL) {
1677 mpool_free(&api_page_pool, memory_region);
1678 }
1679
1680 return ret;
1681}
1682
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001683struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1684 uint32_t fragment_length,
1685 ipaddr_t address, uint32_t page_count,
1686 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687{
1688 struct vm *to = current->vm;
1689 struct vm_locked to_locked;
1690 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001691 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001692 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001693 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001694
1695 if (ipa_addr(address) != 0 || page_count != 0) {
1696 /*
1697 * Hafnium only supports passing the descriptor in the TX
1698 * mailbox.
1699 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001700 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001701 }
1702
Andrew Walbrana65a1322020-04-06 19:32:32 +01001703 if (fragment_length != length) {
1704 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001705 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001706 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001707
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001708 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001709 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001710 message_buffer_size = cpu_get_buffer_size(current->cpu);
1711 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1712 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001713 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001714 }
1715
1716 to_locked = vm_lock(to);
1717 to_msg = to->mailbox.send;
1718
1719 if (to_msg == NULL) {
1720 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001721 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001722 goto out;
1723 }
1724
1725 /*
1726 * Copy the retrieve request descriptor to an internal buffer, so that
1727 * the caller can't change it underneath us.
1728 */
1729 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
1730
1731 if (msg_receiver_busy(to_locked, NULL, false)) {
1732 /*
1733 * Can't retrieve memory information if the mailbox is not
1734 * available.
1735 */
1736 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001737 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001738 goto out;
1739 }
1740
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001741 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
1742 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001743
1744out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001745 vm_unlock(&to_locked);
1746 return ret;
1747}
1748
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001749struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001750{
1751 struct vm *from = current->vm;
1752 struct vm_locked from_locked;
1753 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001754 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001755 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001756 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001757 uint32_t length;
1758
1759 from_locked = vm_lock(from);
1760 from_msg = from->mailbox.send;
1761
1762 if (from_msg == NULL) {
1763 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001764 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001765 goto out;
1766 }
1767
1768 /*
1769 * Calculate length from relinquish descriptor before copying. We will
1770 * check again later to make sure it hasn't changed.
1771 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001772 length = sizeof(struct ffa_mem_relinquish) +
1773 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
1774 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001775 /*
1776 * Copy the relinquish descriptor to an internal buffer, so that the
1777 * caller can't change it underneath us.
1778 */
1779 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001780 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001781 message_buffer_size = cpu_get_buffer_size(current->cpu);
1782 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
1783 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001784 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001785 goto out;
1786 }
1787 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
1788
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001789 if (sizeof(struct ffa_mem_relinquish) +
1790 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001791 length) {
1792 dlog_verbose(
1793 "Endpoint count changed while copying to internal "
1794 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001795 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001796 goto out;
1797 }
1798
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001799 ret = ffa_memory_relinquish(from_locked, relinquish_request,
1800 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001801
1802out:
1803 vm_unlock(&from_locked);
1804 return ret;
1805}
1806
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001807struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
1808 ffa_memory_region_flags_t flags,
1809 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001810{
1811 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001812 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001813
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001814 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1815 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001816 struct vm_locked to_locked = vm_lock(to);
1817
Andrew Walbranca808b12020-05-15 17:22:28 +01001818 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001819 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001820
Andrew Walbran290b0c92020-02-03 16:37:14 +00001821 vm_unlock(&to_locked);
1822 } else {
1823 struct vm *from = vm_find(HF_TEE_VM_ID);
1824 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1825
Andrew Walbranca808b12020-05-15 17:22:28 +01001826 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
1827 vm_to_from_lock.vm2, handle, flags,
1828 &api_page_pool);
1829
1830 vm_unlock(&vm_to_from_lock.vm1);
1831 vm_unlock(&vm_to_from_lock.vm2);
1832 }
1833
1834 return ret;
1835}
1836
1837struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
1838 uint32_t fragment_offset,
1839 ffa_vm_id_t sender_vm_id,
1840 struct vcpu *current)
1841{
1842 struct vm *to = current->vm;
1843 struct vm_locked to_locked;
1844 struct ffa_value ret;
1845
1846 /* Sender ID MBZ at virtual instance. */
1847 if (sender_vm_id != 0) {
1848 return ffa_error(FFA_INVALID_PARAMETERS);
1849 }
1850
1851 to_locked = vm_lock(to);
1852
1853 if (msg_receiver_busy(to_locked, NULL, false)) {
1854 /*
1855 * Can't retrieve memory information if the mailbox is not
1856 * available.
1857 */
1858 dlog_verbose("RX buffer not ready.\n");
1859 ret = ffa_error(FFA_BUSY);
1860 goto out;
1861 }
1862
1863 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
1864 &api_page_pool);
1865
1866out:
1867 vm_unlock(&to_locked);
1868 return ret;
1869}
1870
1871struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
1872 uint32_t fragment_length,
1873 ffa_vm_id_t sender_vm_id,
1874 struct vcpu *current)
1875{
1876 struct vm *from = current->vm;
1877 const void *from_msg;
1878 void *fragment_copy;
1879 struct ffa_value ret;
1880
1881 /* Sender ID MBZ at virtual instance. */
1882 if (sender_vm_id != 0) {
1883 return ffa_error(FFA_INVALID_PARAMETERS);
1884 }
1885
1886 /*
1887 * Check that the sender has configured its send buffer. If the TX
1888 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1889 * be safely accessed after releasing the lock since the TX mailbox
1890 * address can only be configured once.
1891 */
1892 sl_lock(&from->lock);
1893 from_msg = from->mailbox.send;
1894 sl_unlock(&from->lock);
1895
1896 if (from_msg == NULL) {
1897 return ffa_error(FFA_INVALID_PARAMETERS);
1898 }
1899
1900 /*
1901 * Copy the fragment to a fresh page from the memory pool. This prevents
1902 * the sender from changing it underneath us, and also lets us keep it
1903 * around in the share state table if needed.
1904 */
1905 if (fragment_length > HF_MAILBOX_SIZE ||
1906 fragment_length > MM_PPOOL_ENTRY_SIZE) {
1907 dlog_verbose(
1908 "Fragment length %d larger than mailbox size %d.\n",
1909 fragment_length, HF_MAILBOX_SIZE);
1910 return ffa_error(FFA_INVALID_PARAMETERS);
1911 }
1912 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
1913 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1914 0) {
1915 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
1916 return ffa_error(FFA_INVALID_PARAMETERS);
1917 }
1918 fragment_copy = mpool_alloc(&api_page_pool);
1919 if (fragment_copy == NULL) {
1920 dlog_verbose("Failed to allocate fragment copy.\n");
1921 return ffa_error(FFA_NO_MEMORY);
1922 }
1923 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
1924
1925 /*
1926 * Hafnium doesn't support fragmentation of memory retrieve requests
1927 * (because it doesn't support caller-specified mappings, so a request
1928 * will never be larger than a single page), so this must be part of a
1929 * memory send (i.e. donate, lend or share) request.
1930 *
1931 * We can tell from the handle whether the memory transaction is for the
1932 * TEE or not.
1933 */
1934 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
1935 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
1936 struct vm_locked from_locked = vm_lock(from);
1937
1938 ret = ffa_memory_send_continue(from_locked, fragment_copy,
1939 fragment_length, handle,
1940 &api_page_pool);
1941 /*
1942 * `ffa_memory_send_continue` takes ownership of the
1943 * fragment_copy, so we don't need to free it here.
1944 */
1945 vm_unlock(&from_locked);
1946 } else {
1947 struct vm *to = vm_find(HF_TEE_VM_ID);
1948 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1949
1950 /*
1951 * The TEE RX buffer state is checked in
1952 * `ffa_memory_tee_send_continue` rather than here, as we need
1953 * to return `FFA_MEM_FRAG_RX` with the current offset rather
1954 * than FFA_ERROR FFA_BUSY in case it is busy.
1955 */
1956
1957 ret = ffa_memory_tee_send_continue(
1958 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
1959 fragment_length, handle, &api_page_pool);
1960 /*
1961 * `ffa_memory_tee_send_continue` takes ownership of the
1962 * fragment_copy, so we don't need to free it here.
1963 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00001964
1965 vm_unlock(&vm_to_from_lock.vm1);
1966 vm_unlock(&vm_to_from_lock.vm2);
1967 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001968
1969 return ret;
1970}