blob: 41bc1a34ba2305611726fecfd4a2e6f9a2e17727 [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"
J-Alves917d2f22020-10-30 18:39:30 +000012#include "hf/arch/ffa_memory_handle.h"
Olivier Deprez96a2a262020-06-11 17:21:38 +020013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Andrew Walbran508e63c2018-12-20 17:02:37 +000015#include "hf/arch/timer.h"
Olivier Deprez764fd2e2020-07-29 15:14:09 +020016#include "hf/arch/vm.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000017
Andrew Scull877ae4b2019-07-02 12:52:33 +010018#include "hf/check.h"
Andrew Walbran318f5732018-11-20 16:23:42 +000019#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa_internal.h"
21#include "hf/ffa_memory.h"
Andrew Scull6386f252018-12-06 13:29:10 +000022#include "hf/mm.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010023#include "hf/plat/console.h"
Andrew Scull6386f252018-12-06 13:29:10 +000024#include "hf/spinlock.h"
Andrew Scull877ae4b2019-07-02 12:52:33 +010025#include "hf/static_assert.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010026#include "hf/std.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010027#include "hf/vm.h"
28
Andrew Scullf35a5c92018-08-07 18:09:46 +010029#include "vmapi/hf/call.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030#include "vmapi/hf/ffa.h"
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010031
Fuad Tabbae4efcc32020-07-16 15:37:27 +010032static_assert(sizeof(struct ffa_partition_info) == 8,
33 "Partition information descriptor size doesn't match the one in "
34 "the FF-A 1.0 EAC specification, Table 82.");
35
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000036/*
37 * To eliminate the risk of deadlocks, we define a partial order for the
38 * acquisition of locks held concurrently by the same physical CPU. Our current
39 * ordering requirements are as follows:
40 *
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010041 * vm::lock -> vcpu::lock -> mm_stage1_lock -> dlog sl
Andrew Scull6386f252018-12-06 13:29:10 +000042 *
Andrew Scull4caadaf2019-07-03 13:13:47 +010043 * Locks of the same kind require the lock of lowest address to be locked first,
44 * see `sl_lock_both()`.
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +000045 */
46
Andrew Scullaa039b32018-10-04 15:02:26 +010047static_assert(HF_MAILBOX_SIZE == PAGE_SIZE,
Andrew Scull13652af2018-09-17 14:49:08 +010048 "Currently, a page is mapped for the send and receive buffers so "
49 "the maximum request is the size of a page.");
50
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051static_assert(MM_PPOOL_ENTRY_SIZE >= HF_MAILBOX_SIZE,
52 "The page pool entry size must be at least as big as the mailbox "
53 "size, so that memory region descriptors can be copied from the "
54 "mailbox for memory sharing.");
55
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000056static struct mpool api_page_pool;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000057
58/**
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000059 * Initialises the API page pool by taking ownership of the contents of the
60 * given page pool.
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000061 */
62void api_init(struct mpool *ppool)
63{
Wedson Almeida Filho9ed8da52018-12-17 16:09:11 +000064 mpool_init_from(&api_page_pool, ppool);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000065}
66
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010067/**
J-Alvesfe7f7372020-11-09 11:32:12 +000068 * Switches the physical CPU back to the corresponding vCPU of the VM whose ID
69 * is given as argument of the function.
70 *
71 * Called to change the context between SPs for direct messaging (when Hafnium
72 * is SPMC), and on the context of the remaining 'api_switch_to_*' functions.
73 *
74 * This function works for partitions that are:
75 * - UP non-migratable.
76 * - MP with pinned Execution Contexts.
77 */
78static struct vcpu *api_switch_to_vm(struct vcpu *current,
79 struct ffa_value to_ret,
80 enum vcpu_state vcpu_state,
81 ffa_vm_id_t to_id)
82{
83 struct vm *to_vm = vm_find(to_id);
84 struct vcpu *next = vm_get_vcpu(to_vm, cpu_index(current->cpu));
85
86 CHECK(next != NULL);
87
88 /* Set the return value for the target VM. */
89 arch_regs_set_retval(&next->regs, to_ret);
90
91 /* Set the current vCPU state. */
92 sl_lock(&current->lock);
93 current->state = vcpu_state;
94 sl_unlock(&current->lock);
95
96 return next;
97}
98
99/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000100 * Switches the physical CPU back to the corresponding vCPU of the primary VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100101 *
102 * This triggers the scheduling logic to run. Run in the context of secondary VM
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100103 * to cause FFA_RUN to return and the primary VM to regain control of the CPU.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100104 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100105static struct vcpu *api_switch_to_primary(struct vcpu *current,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100106 struct ffa_value primary_ret,
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000107 enum vcpu_state secondary_state)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100108{
Andrew Walbran508e63c2018-12-20 17:02:37 +0000109 /*
110 * If the secondary is blocked but has a timer running, sleep until the
111 * timer fires rather than indefinitely.
112 */
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100113 switch (primary_ret.func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100114 case HF_FFA_RUN_WAIT_FOR_INTERRUPT:
115 case FFA_MSG_WAIT_32: {
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100116 if (arch_timer_enabled_current()) {
117 uint64_t remaining_ns =
118 arch_timer_remaining_ns_current();
119
120 if (remaining_ns == 0) {
121 /*
122 * Timer is pending, so the current vCPU should
123 * be run again right away.
124 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100125 primary_ret.func = FFA_INTERRUPT_32;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100126 /*
127 * primary_ret.arg1 should already be set to the
128 * current VM ID and vCPU ID.
129 */
130 primary_ret.arg2 = 0;
131 } else {
132 primary_ret.arg2 = remaining_ns;
133 }
134 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100135 primary_ret.arg2 = FFA_SLEEP_INDEFINITE;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100136 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000137 break;
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100138 }
Andrew Scullb06d1752019-02-04 10:15:48 +0000139
140 default:
141 /* Do nothing. */
142 break;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000143 }
144
J-Alvesfe7f7372020-11-09 11:32:12 +0000145 return api_switch_to_vm(current, primary_ret, secondary_state,
146 HF_PRIMARY_VM_ID);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100147}
148
149/**
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200150 * Choose next vCPU to run to be the counterpart vCPU in the other
151 * world (run the normal world if currently running in the secure
152 * world). Set current vCPU state to the given vcpu_state parameter.
153 * Set FF-A return values to the target vCPU in the other world.
154 *
155 * Called in context of a direct message response from a secure
156 * partition to a VM.
157 */
158static struct vcpu *api_switch_to_other_world(struct vcpu *current,
159 struct ffa_value other_world_ret,
160 enum vcpu_state vcpu_state)
161{
J-Alvesfe7f7372020-11-09 11:32:12 +0000162 return api_switch_to_vm(current, other_world_ret, vcpu_state,
163 HF_OTHER_WORLD_ID);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200164}
165
166/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100167 * Checks whether the given `to` VM's mailbox is currently busy, and optionally
168 * registers the `from` VM to be notified when it becomes available.
169 */
170static bool msg_receiver_busy(struct vm_locked to, struct vm *from, bool notify)
171{
172 if (to.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
173 to.vm->mailbox.recv == NULL) {
174 /*
175 * Fail if the receiver isn't currently ready to receive data,
176 * setting up for notification if requested.
177 */
178 if (notify) {
179 struct wait_entry *entry =
180 vm_get_wait_entry(from, to.vm->id);
181
182 /* Append waiter only if it's not there yet. */
183 if (list_empty(&entry->wait_links)) {
184 list_append(&to.vm->mailbox.waiter_list,
185 &entry->wait_links);
186 }
187 }
188
189 return true;
190 }
191
192 return false;
193}
194
195/**
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000196 * Returns true if the given vCPU is executing in context of an
197 * FFA_MSG_SEND_DIRECT_REQ invocation.
198 */
199static bool is_ffa_direct_msg_request_ongoing(struct vcpu_locked locked)
200{
201 return locked.vcpu->direct_request_origin_vm_id != HF_INVALID_VM_ID;
202}
203
204/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000205 * Returns to the primary VM and signals that the vCPU still has work to do so.
Andrew Scull33fecd32019-01-08 14:48:27 +0000206 */
207struct vcpu *api_preempt(struct vcpu *current)
208{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100209 struct ffa_value ret = {
210 .func = FFA_INTERRUPT_32,
211 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull33fecd32019-01-08 14:48:27 +0000212 };
213
Andrew Sculld6ee1102019-04-05 22:12:42 +0100214 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
Andrew Scull33fecd32019-01-08 14:48:27 +0000215}
216
217/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000218 * Puts the current vCPU in wait for interrupt mode, and returns to the primary
Fuad Tabbaed294af2019-12-20 10:43:01 +0000219 * VM.
Andrew Scullaa039b32018-10-04 15:02:26 +0100220 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100221struct vcpu *api_wait_for_interrupt(struct vcpu *current)
Andrew Scullaa039b32018-10-04 15:02:26 +0100222{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100223 struct ffa_value ret = {
224 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
225 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Scull6d2db332018-10-10 15:28:17 +0100226 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000227
Wedson Almeida Filhoba641ef2018-12-03 04:19:44 +0000228 return api_switch_to_primary(current, ret,
Andrew Sculld6ee1102019-04-05 22:12:42 +0100229 VCPU_STATE_BLOCKED_INTERRUPT);
Andrew Scullaa039b32018-10-04 15:02:26 +0100230}
231
232/**
Andrew Walbran33645652019-04-15 12:29:31 +0100233 * Puts the current vCPU in off mode, and returns to the primary VM.
234 */
235struct vcpu *api_vcpu_off(struct vcpu *current)
236{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100237 struct ffa_value ret = {
238 .func = HF_FFA_RUN_WAIT_FOR_INTERRUPT,
239 .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)),
Andrew Walbran33645652019-04-15 12:29:31 +0100240 };
241
242 /*
243 * Disable the timer, so the scheduler doesn't get told to call back
244 * based on it.
245 */
246 arch_timer_disable_current();
247
248 return api_switch_to_primary(current, ret, VCPU_STATE_OFF);
249}
250
251/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000252 * Returns to the primary VM to allow this CPU to be used for other tasks as the
253 * vCPU does not have work to do at this moment. The current vCPU is marked as
Andrew Walbran16075b62019-09-03 17:11:07 +0100254 * ready to be scheduled again.
Andrew Scull66d62bf2019-02-01 13:54:10 +0000255 */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000256struct ffa_value api_yield(struct vcpu *current, struct vcpu **next)
Andrew Scull66d62bf2019-02-01 13:54:10 +0000257{
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000258 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
259 struct vcpu_locked current_locked;
260 bool is_direct_request_ongoing;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000261
262 if (current->vm->id == HF_PRIMARY_VM_ID) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000263 /* NOOP on the primary as it makes the scheduling decisions. */
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000264 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000265 }
266
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000267 current_locked = vcpu_lock(current);
268 is_direct_request_ongoing =
269 is_ffa_direct_msg_request_ongoing(current_locked);
270 vcpu_unlock(&current_locked);
271
272 if (is_direct_request_ongoing) {
273 return ffa_error(FFA_DENIED);
274 }
275
276 *next = api_switch_to_primary(
277 current,
278 (struct ffa_value){.func = FFA_YIELD_32,
279 .arg1 = ffa_vm_vcpu(current->vm->id,
280 vcpu_index(current))},
281 VCPU_STATE_READY);
282
283 return ret;
Andrew Scull66d62bf2019-02-01 13:54:10 +0000284}
285
286/**
Andrew Walbran33645652019-04-15 12:29:31 +0100287 * Switches to the primary so that it can switch to the target, or kick it if it
288 * is already running on a different physical CPU.
289 */
290struct vcpu *api_wake_up(struct vcpu *current, struct vcpu *target_vcpu)
291{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 struct ffa_value ret = {
293 .func = HF_FFA_RUN_WAKE_UP,
294 .arg1 = ffa_vm_vcpu(target_vcpu->vm->id,
295 vcpu_index(target_vcpu)),
Andrew Walbran33645652019-04-15 12:29:31 +0100296 };
297 return api_switch_to_primary(current, ret, VCPU_STATE_READY);
298}
299
300/**
Andrew Scull38772ab2019-01-24 15:16:50 +0000301 * Aborts the vCPU and triggers its VM to abort fully.
Andrew Scull9726c252019-01-23 13:44:19 +0000302 */
303struct vcpu *api_abort(struct vcpu *current)
304{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100305 struct ffa_value ret = ffa_error(FFA_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000306
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100307 dlog_notice("Aborting VM %#x vCPU %u\n", current->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000308 vcpu_index(current));
Andrew Scull9726c252019-01-23 13:44:19 +0000309
310 if (current->vm->id == HF_PRIMARY_VM_ID) {
311 /* TODO: what to do when the primary aborts? */
312 for (;;) {
313 /* Do nothing. */
314 }
315 }
316
317 atomic_store_explicit(&current->vm->aborting, true,
318 memory_order_relaxed);
319
320 /* TODO: free resources once all vCPUs abort. */
321
Andrew Sculld6ee1102019-04-05 22:12:42 +0100322 return api_switch_to_primary(current, ret, VCPU_STATE_ABORTED);
Andrew Scull9726c252019-01-23 13:44:19 +0000323}
324
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100325struct ffa_value api_ffa_partition_info_get(struct vcpu *current,
326 const struct ffa_uuid *uuid)
327{
328 struct vm *current_vm = current->vm;
329 struct vm_locked current_vm_locked;
330 ffa_vm_count_t vm_count = 0;
331 bool uuid_is_null = ffa_uuid_is_null(uuid);
332 struct ffa_value ret;
333 uint32_t size;
334 struct ffa_partition_info partitions[MAX_VMS];
335
336 /*
337 * Iterate through the VMs to find the ones with a matching UUID.
338 * A Null UUID retrieves information for all VMs.
339 */
340 for (uint16_t index = 0; index < vm_get_count(); ++index) {
341 const struct vm *vm = vm_find_index(index);
342
343 if (uuid_is_null || ffa_uuid_equal(uuid, &vm->uuid)) {
344 partitions[vm_count].vm_id = vm->id;
345 partitions[vm_count].vcpu_count = vm->vcpu_count;
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100346 partitions[vm_count].properties =
Olivier Deprez764fd2e2020-07-29 15:14:09 +0200347 arch_vm_partition_properties(vm->id);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100348
349 ++vm_count;
350 }
351 }
352
353 /* Unrecognized UUID: does not match any of the VMs and is not Null. */
354 if (vm_count == 0) {
355 return ffa_error(FFA_INVALID_PARAMETERS);
356 }
357
358 size = vm_count * sizeof(partitions[0]);
359 if (size > FFA_MSG_PAYLOAD_MAX) {
360 dlog_error(
361 "Partition information does not fit in the VM's RX "
362 "buffer.\n");
363 return ffa_error(FFA_NO_MEMORY);
364 }
365
366 /*
367 * Partition information is returned in the VM's RX buffer, which is why
368 * the lock is needed.
369 */
370 current_vm_locked = vm_lock(current_vm);
371
372 if (msg_receiver_busy(current_vm_locked, NULL, false)) {
373 /*
374 * Can't retrieve memory information if the mailbox is not
375 * available.
376 */
377 dlog_verbose("RX buffer not ready.\n");
378 ret = ffa_error(FFA_BUSY);
379 goto out_unlock;
380 }
381
382 /* Populate the VM's RX buffer with the partition information. */
383 memcpy_s(current_vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, partitions,
384 size);
385 current_vm->mailbox.recv_size = size;
386 current_vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
387 current_vm->mailbox.recv_func = FFA_PARTITION_INFO_GET_32;
388 current_vm->mailbox.state = MAILBOX_STATE_READ;
389
390 /* Return the count of partition information descriptors in w2. */
391 ret = (struct ffa_value){.func = FFA_SUCCESS_32, .arg2 = vm_count};
392
393out_unlock:
394 vm_unlock(&current_vm_locked);
395
396 return ret;
397}
398
Andrew Scull9726c252019-01-23 13:44:19 +0000399/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000400 * Returns the ID of the VM.
401 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100402struct ffa_value api_ffa_id_get(const struct vcpu *current)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000403{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100404 return (struct ffa_value){.func = FFA_SUCCESS_32,
405 .arg2 = current->vm->id};
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000406}
407
408/**
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000409 * This function is called by the architecture-specific context switching
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000410 * function to indicate that register state for the given vCPU has been saved
411 * and can therefore be used by other pCPUs.
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000412 */
413void api_regs_state_saved(struct vcpu *vcpu)
414{
415 sl_lock(&vcpu->lock);
416 vcpu->regs_available = true;
417 sl_unlock(&vcpu->lock);
418}
419
420/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000421 * Retrieves the next waiter and removes it from the wait list if the VM's
422 * mailbox is in a writable state.
423 */
424static struct wait_entry *api_fetch_waiter(struct vm_locked locked_vm)
425{
426 struct wait_entry *entry;
427 struct vm *vm = locked_vm.vm;
428
Andrew Sculld6ee1102019-04-05 22:12:42 +0100429 if (vm->mailbox.state != MAILBOX_STATE_EMPTY ||
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000430 vm->mailbox.recv == NULL || list_empty(&vm->mailbox.waiter_list)) {
431 /* The mailbox is not writable or there are no waiters. */
432 return NULL;
433 }
434
435 /* Remove waiter from the wait list. */
436 entry = CONTAINER_OF(vm->mailbox.waiter_list.next, struct wait_entry,
437 wait_links);
438 list_remove(&entry->wait_links);
439 return entry;
440}
441
442/**
Andrew Walbran508e63c2018-12-20 17:02:37 +0000443 * Assuming that the arguments have already been checked by the caller, injects
444 * a virtual interrupt of the given ID into the given target vCPU. This doesn't
445 * cause the vCPU to actually be run immediately; it will be taken when the vCPU
446 * is next run, which is up to the scheduler.
447 *
448 * Returns:
449 * - 0 on success if no further action is needed.
450 * - 1 if it was called by the primary VM and the primary VM now needs to wake
451 * up or kick the target vCPU.
452 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200453static int64_t internal_interrupt_inject_locked(
454 struct vcpu_locked target_locked, uint32_t intid, struct vcpu *current,
455 struct vcpu **next)
Andrew Walbran508e63c2018-12-20 17:02:37 +0000456{
457 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +0100458 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000459 int64_t ret = 0;
460
Andrew Walbran508e63c2018-12-20 17:02:37 +0000461 /*
462 * We only need to change state and (maybe) trigger a virtual IRQ if it
463 * is enabled and was not previously pending. Otherwise we can skip
464 * everything except setting the pending bit.
465 *
466 * If you change this logic make sure to update the need_vm_lock logic
467 * above to match.
468 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200469 if (!(target_locked.vcpu->interrupts.interrupt_enabled[intid_index] &
470 ~target_locked.vcpu->interrupts.interrupt_pending[intid_index] &
Andrew Walbran508e63c2018-12-20 17:02:37 +0000471 intid_mask)) {
472 goto out;
473 }
474
475 /* Increment the count. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200476 target_locked.vcpu->interrupts.enabled_and_pending_count++;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000477
478 /*
479 * Only need to update state if there was not already an
480 * interrupt enabled and pending.
481 */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200482 if (target_locked.vcpu->interrupts.enabled_and_pending_count != 1) {
Andrew Walbran508e63c2018-12-20 17:02:37 +0000483 goto out;
484 }
485
Andrew Walbran508e63c2018-12-20 17:02:37 +0000486 if (current->vm->id == HF_PRIMARY_VM_ID) {
487 /*
488 * If the call came from the primary VM, let it know that it
489 * should run or kick the target vCPU.
490 */
491 ret = 1;
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200492 } else if (current != target_locked.vcpu && next != NULL) {
493 *next = api_wake_up(current, target_locked.vcpu);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000494 }
495
496out:
497 /* Either way, make it pending. */
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200498 target_locked.vcpu->interrupts.interrupt_pending[intid_index] |=
499 intid_mask;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000500
Olivier Deprezc19a6ea2020-08-06 11:16:07 +0200501 return ret;
502}
503
504/* Wrapper to internal_interrupt_inject with locking of target vCPU */
505static int64_t internal_interrupt_inject(struct vcpu *target_vcpu,
506 uint32_t intid, struct vcpu *current,
507 struct vcpu **next)
508{
509 int64_t ret;
510 struct vcpu_locked target_locked;
511
512 target_locked = vcpu_lock(target_vcpu);
513 ret = internal_interrupt_inject_locked(target_locked, intid, current,
514 next);
515 vcpu_unlock(&target_locked);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000516
517 return ret;
518}
519
520/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521 * Constructs an FFA_MSG_SEND value to return from a successful FFA_MSG_POLL
522 * or FFA_MSG_WAIT call.
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100523 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524static struct ffa_value ffa_msg_recv_return(const struct vm *receiver)
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100525{
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000526 switch (receiver->mailbox.recv_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527 case FFA_MSG_SEND_32:
528 return (struct ffa_value){
529 .func = FFA_MSG_SEND_32,
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000530 .arg1 = (receiver->mailbox.recv_sender << 16) |
531 receiver->id,
532 .arg3 = receiver->mailbox.recv_size};
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000533 default:
534 /* This should never be reached, but return an error in case. */
Andrew Walbran17eebf92020-02-05 16:35:49 +0000535 dlog_error("Tried to return an invalid message function %#x\n",
536 receiver->mailbox.recv_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 return ffa_error(FFA_DENIED);
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000538 }
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100539}
540
541/**
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000542 * Prepares the vCPU to run by updating its state and fetching whether a return
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000543 * value needs to be forced onto the vCPU.
544 */
Andrew Scull38772ab2019-01-24 15:16:50 +0000545static bool api_vcpu_prepare_run(const struct vcpu *current, struct vcpu *vcpu,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 struct ffa_value *run_ret)
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000547{
Andrew Scullb06d1752019-02-04 10:15:48 +0000548 bool need_vm_lock;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000549 bool ret;
550
Andrew Scullb06d1752019-02-04 10:15:48 +0000551 /*
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000552 * Check that the registers are available so that the vCPU can be run.
Andrew Scullb06d1752019-02-04 10:15:48 +0000553 *
Andrew Scull4caadaf2019-07-03 13:13:47 +0100554 * The VM lock is not needed in the common case so it must only be taken
555 * when it is going to be needed. This ensures there are no inter-vCPU
556 * dependencies in the common run case meaning the sensitive context
557 * switch performance is consistent.
Andrew Scullb06d1752019-02-04 10:15:48 +0000558 */
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000559 sl_lock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000560
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000561 /* The VM needs to be locked to deliver mailbox messages. */
562 need_vm_lock = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX;
563 if (need_vm_lock) {
Andrew Scullb06d1752019-02-04 10:15:48 +0000564 sl_unlock(&vcpu->lock);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000565 sl_lock(&vcpu->vm->lock);
566 sl_lock(&vcpu->lock);
567 }
568
569 /*
570 * If the vCPU is already running somewhere then we can't run it here
571 * simultaneously. While it is actually running then the state should be
572 * `VCPU_STATE_RUNNING` and `regs_available` should be false. Once it
573 * stops running but while Hafnium is in the process of switching back
574 * to the primary there will be a brief period while the state has been
575 * updated but `regs_available` is still false (until
576 * `api_regs_state_saved` is called). We can't start running it again
577 * until this has finished, so count this state as still running for the
578 * purposes of this check.
579 */
580 if (vcpu->state == VCPU_STATE_RUNNING || !vcpu->regs_available) {
581 /*
582 * vCPU is running on another pCPU.
583 *
584 * It's okay not to return the sleep duration here because the
585 * other physical CPU that is currently running this vCPU will
586 * return the sleep duration if needed.
587 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100588 *run_ret = ffa_error(FFA_BUSY);
Andrew Walbrand81c7d82019-11-27 18:34:46 +0000589 ret = false;
590 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000591 }
Andrew Scull9726c252019-01-23 13:44:19 +0000592
593 if (atomic_load_explicit(&vcpu->vm->aborting, memory_order_relaxed)) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100594 if (vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100595 dlog_notice("Aborting VM %#x vCPU %u\n", vcpu->vm->id,
Andrew Walbran17eebf92020-02-05 16:35:49 +0000596 vcpu_index(vcpu));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100597 vcpu->state = VCPU_STATE_ABORTED;
Andrew Scull9726c252019-01-23 13:44:19 +0000598 }
599 ret = false;
600 goto out;
601 }
602
Andrew Walbran508e63c2018-12-20 17:02:37 +0000603 switch (vcpu->state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +0100604 case VCPU_STATE_RUNNING:
605 case VCPU_STATE_OFF:
606 case VCPU_STATE_ABORTED:
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000607 ret = false;
608 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000609
Andrew Sculld6ee1102019-04-05 22:12:42 +0100610 case VCPU_STATE_BLOCKED_MAILBOX:
Andrew Scullb06d1752019-02-04 10:15:48 +0000611 /*
612 * A pending message allows the vCPU to run so the message can
613 * be delivered directly.
614 */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100615 if (vcpu->vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100616 arch_regs_set_retval(&vcpu->regs,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100617 ffa_msg_recv_return(vcpu->vm));
Andrew Sculld6ee1102019-04-05 22:12:42 +0100618 vcpu->vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Scullb06d1752019-02-04 10:15:48 +0000619 break;
620 }
621 /* Fall through. */
Andrew Sculld6ee1102019-04-05 22:12:42 +0100622 case VCPU_STATE_BLOCKED_INTERRUPT:
Andrew Scullb06d1752019-02-04 10:15:48 +0000623 /* Allow virtual interrupts to be delivered. */
624 if (vcpu->interrupts.enabled_and_pending_count > 0) {
625 break;
626 }
627
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100628 uint64_t timer_remaining_ns = FFA_SLEEP_INDEFINITE;
629
Andrew Walbran508e63c2018-12-20 17:02:37 +0000630 if (arch_timer_enabled(&vcpu->regs)) {
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100631 timer_remaining_ns =
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000632 arch_timer_remaining_ns(&vcpu->regs);
633
634 /*
635 * The timer expired so allow the interrupt to be
636 * delivered.
637 */
638 if (timer_remaining_ns == 0) {
639 break;
640 }
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100641 }
Andrew Walbran2fc856a2019-11-04 15:17:24 +0000642
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100643 /*
644 * The vCPU is not ready to run, return the appropriate code to
645 * the primary which called vcpu_run.
646 */
647 run_ret->func = vcpu->state == VCPU_STATE_BLOCKED_MAILBOX
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648 ? FFA_MSG_WAIT_32
649 : HF_FFA_RUN_WAIT_FOR_INTERRUPT;
Andrew Walbran4692a3a2020-08-07 12:42:01 +0100650 run_ret->arg1 = ffa_vm_vcpu(vcpu->vm->id, vcpu_index(vcpu));
651 run_ret->arg2 = timer_remaining_ns;
Andrew Walbran508e63c2018-12-20 17:02:37 +0000652
653 ret = false;
654 goto out;
Andrew Scullb06d1752019-02-04 10:15:48 +0000655
Andrew Sculld6ee1102019-04-05 22:12:42 +0100656 case VCPU_STATE_READY:
Andrew Walbran508e63c2018-12-20 17:02:37 +0000657 break;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000658 }
659
Andrew Scullb06d1752019-02-04 10:15:48 +0000660 /* It has been decided that the vCPU should be run. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000661 vcpu->cpu = current->cpu;
Andrew Sculld6ee1102019-04-05 22:12:42 +0100662 vcpu->state = VCPU_STATE_RUNNING;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000663
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000664 /*
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000665 * Mark the registers as unavailable now that we're about to reflect
666 * them onto the real registers. This will also prevent another physical
667 * CPU from trying to read these registers.
668 */
669 vcpu->regs_available = false;
670
671 ret = true;
672
673out:
674 sl_unlock(&vcpu->lock);
Andrew Scullb06d1752019-02-04 10:15:48 +0000675 if (need_vm_lock) {
676 sl_unlock(&vcpu->vm->lock);
677 }
678
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000679 return ret;
680}
681
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100682struct ffa_value api_ffa_run(ffa_vm_id_t vm_id, ffa_vcpu_index_t vcpu_idx,
683 const struct vcpu *current, struct vcpu **next)
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100684{
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100685 struct vm *vm;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100686 struct vcpu *vcpu;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100687 struct ffa_value ret = ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100688
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000689 /* Only the primary VM can switch vCPUs. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100690 if (current->vm->id != HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100691 ret.arg2 = FFA_DENIED;
Andrew Scull6d2db332018-10-10 15:28:17 +0100692 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100693 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100694
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000695 /* Only secondary VM vCPUs can be run. */
Andrew Scull19503262018-09-20 14:48:39 +0100696 if (vm_id == HF_PRIMARY_VM_ID) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100697 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100698 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100699
Andrew Scull19503262018-09-20 14:48:39 +0100700 /* The requested VM must exist. */
Andrew Walbran42347a92019-05-09 13:59:03 +0100701 vm = vm_find(vm_id);
Andrew Scull19503262018-09-20 14:48:39 +0100702 if (vm == NULL) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100703 goto out;
Andrew Scull19503262018-09-20 14:48:39 +0100704 }
705
Fuad Tabbaed294af2019-12-20 10:43:01 +0000706 /* The requested vCPU must exist. */
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100707 if (vcpu_idx >= vm->vcpu_count) {
Andrew Scull6d2db332018-10-10 15:28:17 +0100708 goto out;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100709 }
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100710
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000711 /* Update state if allowed. */
Andrew Walbrane1310df2019-04-29 17:28:28 +0100712 vcpu = vm_get_vcpu(vm, vcpu_idx);
Andrew Scullb06d1752019-02-04 10:15:48 +0000713 if (!api_vcpu_prepare_run(current, vcpu, &ret)) {
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000714 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100715 }
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000716
Andrew Walbran508e63c2018-12-20 17:02:37 +0000717 /*
718 * Inject timer interrupt if timer has expired. It's safe to access
719 * vcpu->regs here because api_vcpu_prepare_run already made sure that
720 * regs_available was true (and then set it to false) before returning
721 * true.
722 */
723 if (arch_timer_pending(&vcpu->regs)) {
724 /* Make virtual timer interrupt pending. */
Andrew Walbranfc9d4382019-05-10 18:07:21 +0100725 internal_interrupt_inject(vcpu, HF_VIRTUAL_TIMER_INTID, vcpu,
726 NULL);
Andrew Walbran508e63c2018-12-20 17:02:37 +0000727
728 /*
729 * Set the mask bit so the hardware interrupt doesn't fire
730 * again. Ideally we wouldn't do this because it affects what
731 * the secondary vCPU sees, but if we don't then we end up with
732 * a loop of the interrupt firing each time we try to return to
733 * the secondary vCPU.
734 */
735 arch_timer_mask(&vcpu->regs);
736 }
737
Fuad Tabbaed294af2019-12-20 10:43:01 +0000738 /* Switch to the vCPU. */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000739 *next = vcpu;
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000740
Andrew Scull33fecd32019-01-08 14:48:27 +0000741 /*
742 * Set a placeholder return code to the scheduler. This will be
743 * overwritten when the switch back to the primary occurs.
744 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100745 ret.func = FFA_INTERRUPT_32;
746 ret.arg1 = ffa_vm_vcpu(vm_id, vcpu_idx);
Andrew Walbran7a1ea0b2019-10-02 18:18:44 +0100747 ret.arg2 = 0;
Andrew Scull33fecd32019-01-08 14:48:27 +0000748
Andrew Scull6d2db332018-10-10 15:28:17 +0100749out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100750 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100751}
752
753/**
Andrew Scull81e85092018-12-12 12:56:20 +0000754 * Check that the mode indicates memory that is valid, owned and exclusive.
755 */
Andrew Walbran1281ed42019-10-22 17:23:40 +0100756static bool api_mode_valid_owned_and_exclusive(uint32_t mode)
Andrew Scull81e85092018-12-12 12:56:20 +0000757{
Andrew Scullb5f49e02019-10-02 13:20:47 +0100758 return (mode & (MM_MODE_D | MM_MODE_INVALID | MM_MODE_UNOWNED |
759 MM_MODE_SHARED)) == 0;
Andrew Scull81e85092018-12-12 12:56:20 +0000760}
761
762/**
Andrew Walbranc8a01972020-09-22 11:23:30 +0100763 * Determines the value to be returned by api_ffa_rxtx_map and
764 * api_ffa_rx_release after they've succeeded. If a secondary VM is running and
765 * there are waiters, it also switches back to the primary VM for it to wake
766 * waiters up.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000767 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100768static struct ffa_value api_waiter_result(struct vm_locked locked_vm,
769 struct vcpu *current,
770 struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000771{
772 struct vm *vm = locked_vm.vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000773
774 if (list_empty(&vm->mailbox.waiter_list)) {
775 /* No waiters, nothing else to do. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100776 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000777 }
778
779 if (vm->id == HF_PRIMARY_VM_ID) {
780 /* The caller is the primary VM. Tell it to wake up waiters. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100781 return (struct ffa_value){.func = FFA_RX_RELEASE_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000782 }
783
784 /*
785 * Switch back to the primary VM, informing it that there are waiters
786 * that need to be notified.
787 */
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000788 *next = api_switch_to_primary(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100789 current, (struct ffa_value){.func = FFA_RX_RELEASE_32},
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000790 VCPU_STATE_READY);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000791
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100792 return (struct ffa_value){.func = FFA_SUCCESS_32};
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000793}
794
795/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100796 * Configures the hypervisor's stage-1 view of the send and receive pages.
Andrew Sculle1322792019-07-01 17:46:10 +0100797 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100798static bool api_vm_configure_stage1(struct mm_stage1_locked mm_stage1_locked,
799 struct vm_locked vm_locked,
Andrew Sculle1322792019-07-01 17:46:10 +0100800 paddr_t pa_send_begin, paddr_t pa_send_end,
801 paddr_t pa_recv_begin, paddr_t pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200802 uint32_t extra_attributes,
Andrew Sculle1322792019-07-01 17:46:10 +0100803 struct mpool *local_page_pool)
804{
805 bool ret;
Andrew Sculle1322792019-07-01 17:46:10 +0100806
807 /* Map the send page as read-only in the hypervisor address space. */
808 vm_locked.vm->mailbox.send =
809 mm_identity_map(mm_stage1_locked, pa_send_begin, pa_send_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200810 MM_MODE_R | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100811 if (!vm_locked.vm->mailbox.send) {
812 /* TODO: partial defrag of failed range. */
813 /* Recover any memory consumed in failed mapping. */
814 mm_defrag(mm_stage1_locked, local_page_pool);
815 goto fail;
816 }
817
818 /*
819 * Map the receive page as writable in the hypervisor address space. On
820 * failure, unmap the send page before returning.
821 */
822 vm_locked.vm->mailbox.recv =
823 mm_identity_map(mm_stage1_locked, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200824 MM_MODE_W | extra_attributes, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100825 if (!vm_locked.vm->mailbox.recv) {
826 /* TODO: partial defrag of failed range. */
827 /* Recover any memory consumed in failed mapping. */
828 mm_defrag(mm_stage1_locked, local_page_pool);
829 goto fail_undo_send;
830 }
831
832 ret = true;
833 goto out;
834
835 /*
836 * The following mappings will not require more memory than is available
837 * in the local pool.
838 */
839fail_undo_send:
840 vm_locked.vm->mailbox.send = NULL;
Andrew Scull7e8de322019-07-02 13:00:56 +0100841 CHECK(mm_unmap(mm_stage1_locked, pa_send_begin, pa_send_end,
842 local_page_pool));
Andrew Sculle1322792019-07-01 17:46:10 +0100843
844fail:
845 ret = false;
846
847out:
Andrew Sculle1322792019-07-01 17:46:10 +0100848 return ret;
849}
850
851/**
Manish Pandeyd34f8892020-06-19 17:41:07 +0100852 * Sanity checks and configures the send and receive pages in the VM stage-2
853 * and hypervisor stage-1 page tables.
854 *
855 * Returns:
856 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
857 * aligned or are the same.
858 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
859 * due to insuffient page table memory.
860 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
861 * the caller.
862 * - FFA_SUCCESS on success if no further action is needed.
Andrew Sculle1322792019-07-01 17:46:10 +0100863 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100864
865struct ffa_value api_vm_configure_pages(
866 struct mm_stage1_locked mm_stage1_locked, struct vm_locked vm_locked,
867 ipaddr_t send, ipaddr_t recv, uint32_t page_count,
868 struct mpool *local_page_pool)
Andrew Sculle1322792019-07-01 17:46:10 +0100869{
Manish Pandeyd34f8892020-06-19 17:41:07 +0100870 struct ffa_value ret;
871 paddr_t pa_send_begin;
872 paddr_t pa_send_end;
873 paddr_t pa_recv_begin;
874 paddr_t pa_recv_end;
875 uint32_t orig_send_mode;
876 uint32_t orig_recv_mode;
Olivier Deprez96a2a262020-06-11 17:21:38 +0200877 uint32_t extra_attributes;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100878
879 /* We only allow these to be setup once. */
880 if (vm_locked.vm->mailbox.send || vm_locked.vm->mailbox.recv) {
881 ret = ffa_error(FFA_DENIED);
882 goto out;
883 }
884
885 /* Hafnium only supports a fixed size of RX/TX buffers. */
886 if (page_count != HF_MAILBOX_SIZE / FFA_PAGE_SIZE) {
887 ret = ffa_error(FFA_INVALID_PARAMETERS);
888 goto out;
889 }
890
891 /* Fail if addresses are not page-aligned. */
892 if (!is_aligned(ipa_addr(send), PAGE_SIZE) ||
893 !is_aligned(ipa_addr(recv), PAGE_SIZE)) {
894 ret = ffa_error(FFA_INVALID_PARAMETERS);
895 goto out;
896 }
897
898 /* Convert to physical addresses. */
899 pa_send_begin = pa_from_ipa(send);
900 pa_send_end = pa_add(pa_send_begin, HF_MAILBOX_SIZE);
901 pa_recv_begin = pa_from_ipa(recv);
902 pa_recv_end = pa_add(pa_recv_begin, HF_MAILBOX_SIZE);
903
904 /* Fail if the same page is used for the send and receive pages. */
905 if (pa_addr(pa_send_begin) == pa_addr(pa_recv_begin)) {
906 ret = ffa_error(FFA_INVALID_PARAMETERS);
907 goto out;
908 }
Andrew Sculle1322792019-07-01 17:46:10 +0100909
910 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +0100911 * Ensure the pages are valid, owned and exclusive to the VM and that
912 * the VM has the required access to the memory.
Andrew Sculle1322792019-07-01 17:46:10 +0100913 */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100914 if (!mm_vm_get_mode(&vm_locked.vm->ptable, send,
915 ipa_add(send, PAGE_SIZE), &orig_send_mode) ||
916 !api_mode_valid_owned_and_exclusive(orig_send_mode) ||
917 (orig_send_mode & MM_MODE_R) == 0 ||
918 (orig_send_mode & MM_MODE_W) == 0) {
919 ret = ffa_error(FFA_DENIED);
920 goto out;
921 }
922
923 if (!mm_vm_get_mode(&vm_locked.vm->ptable, recv,
924 ipa_add(recv, PAGE_SIZE), &orig_recv_mode) ||
925 !api_mode_valid_owned_and_exclusive(orig_recv_mode) ||
926 (orig_recv_mode & MM_MODE_R) == 0) {
927 ret = ffa_error(FFA_DENIED);
928 goto out;
929 }
Andrew Sculle1322792019-07-01 17:46:10 +0100930
931 /* Take memory ownership away from the VM and mark as shared. */
Andrew Scull3c257452019-11-26 13:32:50 +0000932 if (!vm_identity_map(
933 vm_locked, pa_send_begin, pa_send_end,
Andrew Sculle1322792019-07-01 17:46:10 +0100934 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R | MM_MODE_W,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100935 local_page_pool, NULL)) {
936 ret = ffa_error(FFA_NO_MEMORY);
937 goto out;
Andrew Sculle1322792019-07-01 17:46:10 +0100938 }
939
Andrew Scull3c257452019-11-26 13:32:50 +0000940 if (!vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
941 MM_MODE_UNOWNED | MM_MODE_SHARED | MM_MODE_R,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100942 local_page_pool, NULL)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100943 /* TODO: partial defrag of failed range. */
944 /* Recover any memory consumed in failed mapping. */
Manish Pandeyd34f8892020-06-19 17:41:07 +0100945 mm_vm_defrag(&vm_locked.vm->ptable, local_page_pool);
Andrew Sculle1322792019-07-01 17:46:10 +0100946 goto fail_undo_send;
947 }
948
Olivier Deprez96a2a262020-06-11 17:21:38 +0200949 /* Get extra send/recv pages mapping attributes for the given VM ID. */
950 extra_attributes = arch_mm_extra_attributes_from_vm(vm_locked.vm->id);
951
Manish Pandeyd34f8892020-06-19 17:41:07 +0100952 if (!api_vm_configure_stage1(mm_stage1_locked, vm_locked, pa_send_begin,
953 pa_send_end, pa_recv_begin, pa_recv_end,
Olivier Deprez96a2a262020-06-11 17:21:38 +0200954 extra_attributes, local_page_pool)) {
Andrew Sculle1322792019-07-01 17:46:10 +0100955 goto fail_undo_send_and_recv;
956 }
957
Manish Pandeyd34f8892020-06-19 17:41:07 +0100958 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Sculle1322792019-07-01 17:46:10 +0100959 goto out;
960
Andrew Sculle1322792019-07-01 17:46:10 +0100961fail_undo_send_and_recv:
Andrew Scull3c257452019-11-26 13:32:50 +0000962 CHECK(vm_identity_map(vm_locked, pa_recv_begin, pa_recv_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100963 orig_send_mode, local_page_pool, NULL));
Andrew Sculle1322792019-07-01 17:46:10 +0100964
965fail_undo_send:
Andrew Scull3c257452019-11-26 13:32:50 +0000966 CHECK(vm_identity_map(vm_locked, pa_send_begin, pa_send_end,
Manish Pandeyd34f8892020-06-19 17:41:07 +0100967 orig_send_mode, local_page_pool, NULL));
968 ret = ffa_error(FFA_NO_MEMORY);
Andrew Sculle1322792019-07-01 17:46:10 +0100969
970out:
Andrew Sculle1322792019-07-01 17:46:10 +0100971 return ret;
972}
973
974/**
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100975 * Configures the VM to send/receive data through the specified pages. The pages
Manish Pandeyd34f8892020-06-19 17:41:07 +0100976 * must not be shared. Locking of the page tables combined with a local memory
977 * pool ensures there will always be enough memory to recover from any errors
978 * that arise. The stage-1 page tables must be locked so memory cannot be taken
979 * by another core which could result in this transaction being unable to roll
980 * back in the case of an error.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000981 *
982 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100983 * - FFA_ERROR FFA_INVALID_PARAMETERS if the given addresses are not properly
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000984 * aligned or are the same.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100985 * - FFA_ERROR FFA_NO_MEMORY if the hypervisor was unable to map the buffers
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000986 * due to insuffient page table memory.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100987 * - FFA_ERROR FFA_DENIED if the pages are already mapped or are not owned by
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000988 * the caller.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100989 * - FFA_SUCCESS on success if no further action is needed.
990 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbranbfffb0f2019-11-05 14:02:34 +0000991 * needs to wake up or kick waiters.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100992 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100993struct ffa_value api_ffa_rxtx_map(ipaddr_t send, ipaddr_t recv,
994 uint32_t page_count, struct vcpu *current,
995 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +0100996{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100997 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100998 struct ffa_value ret;
Manish Pandeyd34f8892020-06-19 17:41:07 +0100999 struct vm_locked vm_locked;
1000 struct mm_stage1_locked mm_stage1_locked;
1001 struct mpool local_page_pool;
Andrew Scull220e6212018-12-21 18:09:00 +00001002
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001003 /*
Manish Pandeyd34f8892020-06-19 17:41:07 +01001004 * Create a local pool so any freed memory can't be used by another
1005 * thread. This is to ensure the original mapping can be restored if any
1006 * stage of the process fails.
Andrew Scull3c0a90a2019-07-01 11:55:53 +01001007 */
Manish Pandeyd34f8892020-06-19 17:41:07 +01001008 mpool_init_with_fallback(&local_page_pool, &api_page_pool);
1009
Andrew Sculle1322792019-07-01 17:46:10 +01001010 vm_locked = vm_lock(vm);
Manish Pandeyd34f8892020-06-19 17:41:07 +01001011 mm_stage1_locked = mm_lock_stage1();
Andrew Scull220e6212018-12-21 18:09:00 +00001012
Manish Pandeyd34f8892020-06-19 17:41:07 +01001013 ret = api_vm_configure_pages(mm_stage1_locked, vm_locked, send, recv,
1014 page_count, &local_page_pool);
1015 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001016 goto exit;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001017 }
1018
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001019 /* Tell caller about waiters, if any. */
Andrew Sculle1322792019-07-01 17:46:10 +01001020 ret = api_waiter_result(vm_locked, current, next);
Andrew Scull220e6212018-12-21 18:09:00 +00001021
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001022exit:
Manish Pandeyd34f8892020-06-19 17:41:07 +01001023 mpool_fini(&local_page_pool);
1024
1025 mm_unlock_stage1(&mm_stage1_locked);
Andrew Sculle1322792019-07-01 17:46:10 +01001026 vm_unlock(&vm_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001027
1028 return ret;
1029}
1030
1031/**
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001032 * Notifies the `to` VM about the message currently in its mailbox, possibly
1033 * with the help of the primary VM.
1034 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001035static struct ffa_value deliver_msg(struct vm_locked to, ffa_vm_id_t from_id,
1036 struct vcpu *current, struct vcpu **next)
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001037{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001038 struct ffa_value ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1039 struct ffa_value primary_ret = {
1040 .func = FFA_MSG_SEND_32,
Andrew Walbranf76f5752019-12-03 18:33:08 +00001041 .arg1 = ((uint32_t)from_id << 16) | to.vm->id,
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001042 };
1043
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001044 /* Messages for the primary VM are delivered directly. */
1045 if (to.vm->id == HF_PRIMARY_VM_ID) {
1046 /*
Andrew Walbrane7ad3c02019-12-24 17:03:04 +00001047 * Only tell the primary VM the size and other details if the
1048 * message is for it, to avoid leaking data about messages for
1049 * other VMs.
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001050 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 primary_ret = ffa_msg_recv_return(to.vm);
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001052
1053 to.vm->mailbox.state = MAILBOX_STATE_READ;
1054 *next = api_switch_to_primary(current, primary_ret,
1055 VCPU_STATE_READY);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001056 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001057 }
1058
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001059 to.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1060
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001061 /* Messages for the TEE are sent on via the dispatcher. */
1062 if (to.vm->id == HF_TEE_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001063 struct ffa_value call = ffa_msg_recv_return(to.vm);
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001064
Olivier Deprez112d2b52020-09-30 07:39:23 +02001065 ret = arch_other_world_call(call);
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001066 /*
1067 * After the call to the TEE completes it must have finished
1068 * reading its RX buffer, so it is ready for another message.
1069 */
1070 to.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001071 /*
1072 * Don't return to the primary VM in this case, as the TEE is
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 * not (yet) scheduled via FF-A.
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001074 */
Andrew Walbran11cff3a2020-02-28 11:33:17 +00001075 return ret;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001076 }
1077
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001078 /* Return to the primary VM directly or with a switch. */
Andrew Walbranf76f5752019-12-03 18:33:08 +00001079 if (from_id != HF_PRIMARY_VM_ID) {
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001080 *next = api_switch_to_primary(current, primary_ret,
1081 VCPU_STATE_READY);
1082 }
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001083
1084 return ret;
Andrew Walbrane0f575f2019-10-16 16:00:12 +01001085}
1086
1087/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001088 * Copies data from the sender's send buffer to the recipient's receive buffer
1089 * and notifies the recipient.
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +00001090 *
1091 * If the recipient's receive buffer is busy, it can optionally register the
1092 * caller to be notified when the recipient's receive buffer becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001093 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001094struct ffa_value api_ffa_msg_send(ffa_vm_id_t sender_vm_id,
1095 ffa_vm_id_t receiver_vm_id, uint32_t size,
1096 uint32_t attributes, struct vcpu *current,
1097 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001098{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001099 struct vm *from = current->vm;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001100 struct vm *to;
Andrew Walbran82d6d152019-12-24 15:02:06 +00001101 struct vm_locked to_locked;
Andrew Walbran70bc8622019-10-07 14:15:58 +01001102 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001103 struct ffa_value ret;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001104 struct vcpu_locked current_locked;
1105 bool is_direct_request_ongoing;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 bool notify =
1107 (attributes & FFA_MSG_SEND_NOTIFY_MASK) == FFA_MSG_SEND_NOTIFY;
Andrew Scull19503262018-09-20 14:48:39 +01001108
Andrew Walbran70bc8622019-10-07 14:15:58 +01001109 /* Ensure sender VM ID corresponds to the current VM. */
1110 if (sender_vm_id != from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001112 }
1113
1114 /* Disallow reflexive requests as this suggests an error in the VM. */
1115 if (receiver_vm_id == from->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001117 }
1118
1119 /* Limit the size of transfer. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 if (size > FFA_MSG_PAYLOAD_MAX) {
1121 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran70bc8622019-10-07 14:15:58 +01001122 }
1123
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001124 /*
1125 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1126 * invocation.
1127 */
1128 current_locked = vcpu_lock(current);
1129 is_direct_request_ongoing =
1130 is_ffa_direct_msg_request_ongoing(current_locked);
1131 vcpu_unlock(&current_locked);
1132
1133 if (is_direct_request_ongoing) {
1134 return ffa_error(FFA_DENIED);
1135 }
1136
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001137 /* Ensure the receiver VM exists. */
1138 to = vm_find(receiver_vm_id);
1139 if (to == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001140 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran0b60c4f2019-12-10 17:05:29 +00001141 }
1142
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001143 /*
Andrew Walbran70bc8622019-10-07 14:15:58 +01001144 * Check that the sender has configured its send buffer. If the tx
1145 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1146 * be safely accessed after releasing the lock since the tx mailbox
1147 * address can only be configured once.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +00001148 */
1149 sl_lock(&from->lock);
1150 from_msg = from->mailbox.send;
1151 sl_unlock(&from->lock);
1152
1153 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 return ffa_error(FFA_INVALID_PARAMETERS);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001155 }
1156
Andrew Walbran82d6d152019-12-24 15:02:06 +00001157 to_locked = vm_lock(to);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001158
Andrew Walbran82d6d152019-12-24 15:02:06 +00001159 if (msg_receiver_busy(to_locked, from, notify)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 ret = ffa_error(FFA_BUSY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001161 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001162 }
1163
Andrew Walbran82d6d152019-12-24 15:02:06 +00001164 /* Copy data. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001165 memcpy_s(to->mailbox.recv, FFA_MSG_PAYLOAD_MAX, from_msg, size);
Andrew Walbran82d6d152019-12-24 15:02:06 +00001166 to->mailbox.recv_size = size;
1167 to->mailbox.recv_sender = sender_vm_id;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001168 to->mailbox.recv_func = FFA_MSG_SEND_32;
Andrew Walbran2619e0a2020-01-10 16:37:50 +00001169 ret = deliver_msg(to_locked, sender_vm_id, current, next);
Andrew Scullaa039b32018-10-04 15:02:26 +01001170
1171out:
Andrew Walbran82d6d152019-12-24 15:02:06 +00001172 vm_unlock(&to_locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001173
Wedson Almeida Filho80eb4a32018-11-30 17:11:15 +00001174 return ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001175}
1176
1177/**
Andrew Scullec52ddf2019-08-20 10:41:01 +01001178 * Checks whether the vCPU's attempt to block for a message has already been
1179 * interrupted or whether it is allowed to block.
1180 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001181bool api_ffa_msg_recv_block_interrupted(struct vcpu *current)
Andrew Scullec52ddf2019-08-20 10:41:01 +01001182{
1183 bool interrupted;
1184
1185 sl_lock(&current->lock);
1186
1187 /*
1188 * Don't block if there are enabled and pending interrupts, to match
1189 * behaviour of wait_for_interrupt.
1190 */
1191 interrupted = (current->interrupts.enabled_and_pending_count > 0);
1192
1193 sl_unlock(&current->lock);
1194
1195 return interrupted;
1196}
1197
1198/**
Andrew Scullaa039b32018-10-04 15:02:26 +01001199 * Receives a message from the mailbox. If one isn't available, this function
1200 * can optionally block the caller until one becomes available.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001201 *
Andrew Scullaa039b32018-10-04 15:02:26 +01001202 * No new messages can be received until the mailbox has been cleared.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001203 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001204struct ffa_value api_ffa_msg_recv(bool block, struct vcpu *current,
1205 struct vcpu **next)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001206{
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001207 bool is_direct_request_ongoing;
1208 struct vcpu_locked current_locked;
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001209 struct vm *vm = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001210 struct ffa_value return_code;
J-Alvesb37fd082020-10-22 12:29:21 +01001211 bool is_from_secure_world =
1212 (current->vm->id & HF_VM_ID_WORLD_MASK) != 0;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001213
Andrew Scullaa039b32018-10-04 15:02:26 +01001214 /*
1215 * The primary VM will receive messages as a status code from running
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001216 * vCPUs and must not call this function.
Andrew Scullaa039b32018-10-04 15:02:26 +01001217 */
J-Alvesb37fd082020-10-22 12:29:21 +01001218 if (!is_from_secure_world && vm->id == HF_PRIMARY_VM_ID) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001219 return ffa_error(FFA_NOT_SUPPORTED);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001220 }
1221
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001222 /*
1223 * Deny if vCPU is executing in context of an FFA_MSG_SEND_DIRECT_REQ
1224 * invocation.
1225 */
1226 current_locked = vcpu_lock(current);
1227 is_direct_request_ongoing =
1228 is_ffa_direct_msg_request_ongoing(current_locked);
1229 vcpu_unlock(&current_locked);
1230
1231 if (is_direct_request_ongoing) {
1232 return ffa_error(FFA_DENIED);
1233 }
1234
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001235 sl_lock(&vm->lock);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001236
Andrew Scullaa039b32018-10-04 15:02:26 +01001237 /* Return pending messages without blocking. */
Andrew Sculld6ee1102019-04-05 22:12:42 +01001238 if (vm->mailbox.state == MAILBOX_STATE_RECEIVED) {
1239 vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001240 return_code = ffa_msg_recv_return(vm);
Jose Marinho3e2442f2019-03-12 13:30:37 +00001241 goto out;
1242 }
1243
1244 /* No pending message so fail if not allowed to block. */
1245 if (!block) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 return_code = ffa_error(FFA_RETRY);
Andrew Scullaa039b32018-10-04 15:02:26 +01001247 goto out;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001248 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001249
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001250 /*
Jose Marinho3e2442f2019-03-12 13:30:37 +00001251 * From this point onward this call can only be interrupted or a message
1252 * received. If a message is received the return value will be set at
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001253 * that time to FFA_SUCCESS.
Andrew Walbran9311c9a2019-03-12 16:59:04 +00001254 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 return_code = ffa_error(FFA_INTERRUPTED);
1256 if (api_ffa_msg_recv_block_interrupted(current)) {
Andrew Scullaa039b32018-10-04 15:02:26 +01001257 goto out;
1258 }
1259
J-Alvesb37fd082020-10-22 12:29:21 +01001260 if (is_from_secure_world) {
1261 /* Return to other world if caller is a SP. */
1262 *next = api_switch_to_other_world(
1263 current, (struct ffa_value){.func = FFA_MSG_WAIT_32},
1264 VCPU_STATE_BLOCKED_MAILBOX);
1265 } else {
1266 /* Switch back to primary VM to block. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001267 struct ffa_value run_return = {
1268 .func = FFA_MSG_WAIT_32,
1269 .arg1 = ffa_vm_vcpu(vm->id, vcpu_index(current)),
Andrew Walbranb4816552018-12-05 17:35:42 +00001270 };
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001271
Andrew Walbranb4816552018-12-05 17:35:42 +00001272 *next = api_switch_to_primary(current, run_return,
Andrew Sculld6ee1102019-04-05 22:12:42 +01001273 VCPU_STATE_BLOCKED_MAILBOX);
Andrew Walbranb4816552018-12-05 17:35:42 +00001274 }
Andrew Scullaa039b32018-10-04 15:02:26 +01001275out:
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001276 sl_unlock(&vm->lock);
1277
Jose Marinho3e2442f2019-03-12 13:30:37 +00001278 return return_code;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001279}
1280
1281/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001282 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
1283 * by this function, the caller must have called api_mailbox_send before with
1284 * the notify argument set to true, and this call must have failed because the
1285 * mailbox was not available.
1286 *
1287 * It should be called repeatedly to retrieve a list of VMs.
1288 *
1289 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
1290 * became writable.
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001291 */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001292int64_t api_mailbox_writable_get(const struct vcpu *current)
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001293{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +01001294 struct vm *vm = current->vm;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001295 struct wait_entry *entry;
Andrew Scullc0e569a2018-10-02 18:05:21 +01001296 int64_t ret;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001297
1298 sl_lock(&vm->lock);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001299 if (list_empty(&vm->mailbox.ready_list)) {
1300 ret = -1;
1301 goto exit;
1302 }
1303
1304 entry = CONTAINER_OF(vm->mailbox.ready_list.next, struct wait_entry,
1305 ready_links);
1306 list_remove(&entry->ready_links);
Andrew Walbranaad8f982019-12-04 10:56:39 +00001307 ret = vm_id_for_wait_entry(vm, entry);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001308
1309exit:
1310 sl_unlock(&vm->lock);
1311 return ret;
1312}
1313
1314/**
1315 * Retrieves the next VM waiting to be notified that the mailbox of the
1316 * specified VM became writable. Only primary VMs are allowed to call this.
1317 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +00001318 * Returns -1 on failure or if there are no waiters; the VM id of the next
1319 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001320 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001321int64_t api_mailbox_waiter_get(ffa_vm_id_t vm_id, const struct vcpu *current)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001322{
1323 struct vm *vm;
1324 struct vm_locked locked;
1325 struct wait_entry *entry;
1326 struct vm *waiting_vm;
1327
1328 /* Only primary VMs are allowed to call this function. */
1329 if (current->vm->id != HF_PRIMARY_VM_ID) {
1330 return -1;
1331 }
1332
Andrew Walbran42347a92019-05-09 13:59:03 +01001333 vm = vm_find(vm_id);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001334 if (vm == NULL) {
1335 return -1;
1336 }
1337
Fuad Tabbaed294af2019-12-20 10:43:01 +00001338 /* Check if there are outstanding notifications from given VM. */
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001339 locked = vm_lock(vm);
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001340 entry = api_fetch_waiter(locked);
1341 vm_unlock(&locked);
1342
1343 if (entry == NULL) {
1344 return -1;
1345 }
1346
1347 /* Enqueue notification to waiting VM. */
1348 waiting_vm = entry->waiting_vm;
1349
1350 sl_lock(&waiting_vm->lock);
1351 if (list_empty(&entry->ready_links)) {
1352 list_append(&waiting_vm->mailbox.ready_list,
1353 &entry->ready_links);
1354 }
1355 sl_unlock(&waiting_vm->lock);
1356
1357 return waiting_vm->id;
1358}
1359
1360/**
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001361 * Releases the caller's mailbox so that a new message can be received. The
1362 * caller must have copied out all data they wish to preserve as new messages
1363 * will overwrite the old and will arrive asynchronously.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001364 *
1365 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001366 * - FFA_ERROR FFA_DENIED on failure, if the mailbox hasn't been read.
1367 * - FFA_SUCCESS on success if no further action is needed.
1368 * - FFA_RX_RELEASE if it was called by the primary VM and the primary VM now
Andrew Walbran8a0f5ca2019-11-05 13:12:23 +00001369 * needs to wake up or kick waiters. Waiters should be retrieved by calling
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001370 * hf_mailbox_waiter_get.
1371 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001372struct ffa_value api_ffa_rx_release(struct vcpu *current, struct vcpu **next)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001373{
1374 struct vm *vm = current->vm;
1375 struct vm_locked locked;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001376 struct ffa_value ret;
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001377
Andrew Walbran7e932bd2019-04-29 16:47:06 +01001378 locked = vm_lock(vm);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001379 switch (vm->mailbox.state) {
Andrew Sculld6ee1102019-04-05 22:12:42 +01001380 case MAILBOX_STATE_EMPTY:
Andrew Sculld6ee1102019-04-05 22:12:42 +01001381 case MAILBOX_STATE_RECEIVED:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382 ret = ffa_error(FFA_DENIED);
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001383 break;
1384
Andrew Sculld6ee1102019-04-05 22:12:42 +01001385 case MAILBOX_STATE_READ:
Andrew Walbranbfffb0f2019-11-05 14:02:34 +00001386 ret = api_waiter_result(locked, current, next);
Andrew Sculld6ee1102019-04-05 22:12:42 +01001387 vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Scullaa7db8e2019-02-01 14:12:19 +00001388 break;
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001389 }
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +00001390 vm_unlock(&locked);
Wedson Almeida Filho2f94ec12018-07-26 16:00:48 +01001391
1392 return ret;
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +01001393}
Andrew Walbran318f5732018-11-20 16:23:42 +00001394
1395/**
1396 * Enables or disables a given interrupt ID for the calling vCPU.
1397 *
1398 * Returns 0 on success, or -1 if the intid is invalid.
1399 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001400int64_t api_interrupt_enable(uint32_t intid, bool enable, struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001401{
1402 uint32_t intid_index = intid / INTERRUPT_REGISTER_BITS;
Andrew Walbrane52006c2019-10-22 18:01:28 +01001403 uint32_t intid_mask = 1U << (intid % INTERRUPT_REGISTER_BITS);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001404
Andrew Walbran318f5732018-11-20 16:23:42 +00001405 if (intid >= HF_NUM_INTIDS) {
1406 return -1;
1407 }
1408
1409 sl_lock(&current->lock);
1410 if (enable) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001411 /*
1412 * If it is pending and was not enabled before, increment the
1413 * count.
1414 */
1415 if (current->interrupts.interrupt_pending[intid_index] &
1416 ~current->interrupts.interrupt_enabled[intid_index] &
1417 intid_mask) {
1418 current->interrupts.enabled_and_pending_count++;
1419 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001420 current->interrupts.interrupt_enabled[intid_index] |=
1421 intid_mask;
Andrew Walbran318f5732018-11-20 16:23:42 +00001422 } else {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001423 /*
1424 * If it is pending and was enabled before, decrement the count.
1425 */
1426 if (current->interrupts.interrupt_pending[intid_index] &
1427 current->interrupts.interrupt_enabled[intid_index] &
1428 intid_mask) {
1429 current->interrupts.enabled_and_pending_count--;
1430 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001431 current->interrupts.interrupt_enabled[intid_index] &=
1432 ~intid_mask;
1433 }
1434
1435 sl_unlock(&current->lock);
1436 return 0;
1437}
1438
1439/**
1440 * Returns the ID of the next pending interrupt for the calling vCPU, and
1441 * acknowledges it (i.e. marks it as no longer pending). Returns
1442 * HF_INVALID_INTID if there are no pending interrupts.
1443 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +00001444uint32_t api_interrupt_get(struct vcpu *current)
Andrew Walbran318f5732018-11-20 16:23:42 +00001445{
1446 uint8_t i;
1447 uint32_t first_interrupt = HF_INVALID_INTID;
Andrew Walbran318f5732018-11-20 16:23:42 +00001448
1449 /*
1450 * Find the first enabled and pending interrupt ID, return it, and
1451 * deactivate it.
1452 */
1453 sl_lock(&current->lock);
1454 for (i = 0; i < HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS; ++i) {
1455 uint32_t enabled_and_pending =
1456 current->interrupts.interrupt_enabled[i] &
1457 current->interrupts.interrupt_pending[i];
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001458
Andrew Walbran318f5732018-11-20 16:23:42 +00001459 if (enabled_and_pending != 0) {
Andrew Walbran3d84a262018-12-13 14:41:19 +00001460 uint8_t bit_index = ctz(enabled_and_pending);
1461 /*
1462 * Mark it as no longer pending and decrement the count.
1463 */
1464 current->interrupts.interrupt_pending[i] &=
Andrew Walbrane52006c2019-10-22 18:01:28 +01001465 ~(1U << bit_index);
Andrew Walbran3d84a262018-12-13 14:41:19 +00001466 current->interrupts.enabled_and_pending_count--;
1467 first_interrupt =
1468 i * INTERRUPT_REGISTER_BITS + bit_index;
Andrew Walbran318f5732018-11-20 16:23:42 +00001469 break;
1470 }
1471 }
Andrew Walbran318f5732018-11-20 16:23:42 +00001472
1473 sl_unlock(&current->lock);
1474 return first_interrupt;
1475}
1476
1477/**
Andrew Walbran4cf217a2018-12-14 15:24:50 +00001478 * Returns whether the current vCPU is allowed to inject an interrupt into the
Andrew Walbran318f5732018-11-20 16:23:42 +00001479 * given VM and vCPU.
1480 */
1481static inline bool is_injection_allowed(uint32_t target_vm_id,
1482 struct vcpu *current)
1483{
1484 uint32_t current_vm_id = current->vm->id;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001485
Andrew Walbran318f5732018-11-20 16:23:42 +00001486 /*
1487 * The primary VM is allowed to inject interrupts into any VM. Secondary
1488 * VMs are only allowed to inject interrupts into their own vCPUs.
1489 */
1490 return current_vm_id == HF_PRIMARY_VM_ID ||
1491 current_vm_id == target_vm_id;
1492}
1493
1494/**
1495 * Injects a virtual interrupt of the given ID into the given target vCPU.
1496 * This doesn't cause the vCPU to actually be run immediately; it will be taken
1497 * when the vCPU is next run, which is up to the scheduler.
1498 *
Andrew Walbran3d84a262018-12-13 14:41:19 +00001499 * Returns:
1500 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
1501 * ID is invalid, or the current VM is not allowed to inject interrupts to
1502 * the target VM.
1503 * - 0 on success if no further action is needed.
1504 * - 1 if it was called by the primary VM and the primary VM now needs to wake
1505 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +00001506 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001507int64_t api_interrupt_inject(ffa_vm_id_t target_vm_id,
1508 ffa_vcpu_index_t target_vcpu_idx, uint32_t intid,
Andrew Walbran42347a92019-05-09 13:59:03 +01001509 struct vcpu *current, struct vcpu **next)
Andrew Walbran318f5732018-11-20 16:23:42 +00001510{
Andrew Walbran318f5732018-11-20 16:23:42 +00001511 struct vcpu *target_vcpu;
Andrew Walbran42347a92019-05-09 13:59:03 +01001512 struct vm *target_vm = vm_find(target_vm_id);
Andrew Walbran318f5732018-11-20 16:23:42 +00001513
1514 if (intid >= HF_NUM_INTIDS) {
1515 return -1;
1516 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001517
Andrew Walbran318f5732018-11-20 16:23:42 +00001518 if (target_vm == NULL) {
1519 return -1;
1520 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001521
Andrew Walbran318f5732018-11-20 16:23:42 +00001522 if (target_vcpu_idx >= target_vm->vcpu_count) {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +00001523 /* The requested vCPU must exist. */
Andrew Walbran318f5732018-11-20 16:23:42 +00001524 return -1;
1525 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001526
Andrew Walbran318f5732018-11-20 16:23:42 +00001527 if (!is_injection_allowed(target_vm_id, current)) {
1528 return -1;
1529 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +00001530
Andrew Walbrane1310df2019-04-29 17:28:28 +01001531 target_vcpu = vm_get_vcpu(target_vm, target_vcpu_idx);
Andrew Walbran318f5732018-11-20 16:23:42 +00001532
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001533 dlog_info("Injecting IRQ %u for VM %#x vCPU %u from VM %#x vCPU %u\n",
Andrew Walbran17eebf92020-02-05 16:35:49 +00001534 intid, target_vm_id, target_vcpu_idx, current->vm->id,
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001535 vcpu_index(current));
Andrew Walbranfc9d4382019-05-10 18:07:21 +01001536 return internal_interrupt_inject(target_vcpu, intid, current, next);
Andrew Walbran318f5732018-11-20 16:23:42 +00001537}
Andrew Scull6386f252018-12-06 13:29:10 +00001538
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539/** Returns the version of the implemented FF-A specification. */
1540struct ffa_value api_ffa_version(uint32_t requested_version)
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001541{
1542 /*
1543 * Ensure that both major and minor revision representation occupies at
1544 * most 15 bits.
1545 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 static_assert(0x8000 > FFA_VERSION_MAJOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001547 "Major revision representation takes more than 15 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001548 static_assert(0x10000 > FFA_VERSION_MINOR,
Andrew Walbran9fd29072020-04-22 12:12:14 +01001549 "Minor revision representation takes more than 16 bits.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 if (requested_version & FFA_VERSION_RESERVED_BIT) {
Andrew Walbran9fd29072020-04-22 12:12:14 +01001551 /* Invalid encoding, return an error. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001552 return (struct ffa_value){.func = FFA_NOT_SUPPORTED};
Andrew Walbran9fd29072020-04-22 12:12:14 +01001553 }
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001554
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001555 return (struct ffa_value){
1556 .func = (FFA_VERSION_MAJOR << FFA_VERSION_MAJOR_OFFSET) |
1557 FFA_VERSION_MINOR};
Jose Marinhofc0b2b62019-06-06 11:18:45 +01001558}
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001559
1560int64_t api_debug_log(char c, struct vcpu *current)
1561{
Andrew Sculld54e1be2019-08-20 11:09:42 +01001562 bool flush;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001563 struct vm *vm = current->vm;
1564 struct vm_locked vm_locked = vm_lock(vm);
1565
Andrew Sculld54e1be2019-08-20 11:09:42 +01001566 if (c == '\n' || c == '\0') {
1567 flush = true;
1568 } else {
1569 vm->log_buffer[vm->log_buffer_length++] = c;
1570 flush = (vm->log_buffer_length == sizeof(vm->log_buffer));
1571 }
1572
1573 if (flush) {
Andrew Walbran7f904bf2019-07-12 16:38:38 +01001574 dlog_flush_vm_buffer(vm->id, vm->log_buffer,
1575 vm->log_buffer_length);
1576 vm->log_buffer_length = 0;
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +01001577 }
1578
1579 vm_unlock(&vm_locked);
1580
1581 return 0;
1582}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001583
1584/**
1585 * Discovery function returning information about the implementation of optional
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001586 * FF-A interfaces.
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001587 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001588struct ffa_value api_ffa_features(uint32_t function_id)
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001589{
1590 switch (function_id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 case FFA_ERROR_32:
1592 case FFA_SUCCESS_32:
1593 case FFA_INTERRUPT_32:
1594 case FFA_VERSION_32:
1595 case FFA_FEATURES_32:
1596 case FFA_RX_RELEASE_32:
1597 case FFA_RXTX_MAP_64:
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001598 case FFA_PARTITION_INFO_GET_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 case FFA_ID_GET_32:
1600 case FFA_MSG_POLL_32:
1601 case FFA_MSG_WAIT_32:
1602 case FFA_YIELD_32:
1603 case FFA_RUN_32:
1604 case FFA_MSG_SEND_32:
1605 case FFA_MEM_DONATE_32:
1606 case FFA_MEM_LEND_32:
1607 case FFA_MEM_SHARE_32:
1608 case FFA_MEM_RETRIEVE_REQ_32:
1609 case FFA_MEM_RETRIEVE_RESP_32:
1610 case FFA_MEM_RELINQUISH_32:
1611 case FFA_MEM_RECLAIM_32:
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001612 case FFA_MSG_SEND_DIRECT_RESP_32:
1613 case FFA_MSG_SEND_DIRECT_REQ_32:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001615 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001616 return ffa_error(FFA_NOT_SUPPORTED);
Jose Marinhoc0f4ff22019-10-09 10:37:42 +01001617 }
1618}
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001619
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001620/**
1621 * Get target VM vCPU for direct messaging request.
1622 * If VM is UP then return first vCPU.
1623 * If VM is MP then return vCPU whose index matches current CPU index.
1624 */
1625static struct vcpu *api_ffa_msg_send_direct_get_receiver_vcpu(
1626 struct vm *vm, struct vcpu *current)
1627{
1628 ffa_vcpu_index_t current_cpu_index = cpu_index(current->cpu);
1629 struct vcpu *vcpu = NULL;
1630
1631 if (vm->vcpu_count == 1) {
1632 vcpu = vm_get_vcpu(vm, 0);
1633 } else if (current_cpu_index < vm->vcpu_count) {
1634 vcpu = vm_get_vcpu(vm, current_cpu_index);
1635 }
1636
1637 return vcpu;
1638}
1639
1640/**
1641 * Send an FF-A direct message request.
1642 */
1643struct ffa_value api_ffa_msg_send_direct_req(ffa_vm_id_t sender_vm_id,
1644 ffa_vm_id_t receiver_vm_id,
1645 struct ffa_value args,
1646 struct vcpu *current,
1647 struct vcpu **next)
1648{
1649 struct ffa_value ret = (struct ffa_value){.func = FFA_INTERRUPT_32};
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001650 struct vm *receiver_vm;
1651 struct vcpu *receiver_vcpu;
1652 struct two_vcpu_locked vcpus_locked;
1653
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001654 if (!arch_other_world_is_direct_request_valid(current, sender_vm_id,
1655 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001656 return ffa_error(FFA_NOT_SUPPORTED);
1657 }
1658
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001659 receiver_vm = vm_find(receiver_vm_id);
1660 if (receiver_vm == NULL) {
1661 return ffa_error(FFA_INVALID_PARAMETERS);
1662 }
1663
1664 /*
1665 * Per PSA FF-A EAC spec section 4.4.1 the firmware framework supports
1666 * UP (migratable) or MP partitions with a number of vCPUs matching the
1667 * number of PEs in the system. It further states that MP partitions
1668 * accepting direct request messages cannot migrate.
1669 */
1670 receiver_vcpu =
1671 api_ffa_msg_send_direct_get_receiver_vcpu(receiver_vm, current);
1672 if (receiver_vcpu == NULL) {
1673 return ffa_error(FFA_INVALID_PARAMETERS);
1674 }
1675
1676 vcpus_locked = vcpu_lock_both(receiver_vcpu, current);
1677
1678 /*
1679 * If destination vCPU is executing or already received an
1680 * FFA_MSG_SEND_DIRECT_REQ then return to caller hinting recipient is
1681 * busy. There is a brief period of time where the vCPU state has
1682 * changed but regs_available is still false thus consider this case as
1683 * the vCPU not yet ready to receive a direct message request.
1684 */
1685 if (is_ffa_direct_msg_request_ongoing(vcpus_locked.vcpu1) ||
1686 receiver_vcpu->state == VCPU_STATE_RUNNING ||
1687 !receiver_vcpu->regs_available) {
1688 ret = ffa_error(FFA_BUSY);
1689 goto out;
1690 }
1691
1692 if (atomic_load_explicit(&receiver_vcpu->vm->aborting,
1693 memory_order_relaxed)) {
1694 if (receiver_vcpu->state != VCPU_STATE_ABORTED) {
Olivier Deprezf92e5d42020-11-13 16:00:54 +01001695 dlog_notice("Aborting VM %#x vCPU %u\n",
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001696 receiver_vcpu->vm->id,
1697 vcpu_index(receiver_vcpu));
1698 receiver_vcpu->state = VCPU_STATE_ABORTED;
1699 }
1700
1701 ret = ffa_error(FFA_ABORTED);
1702 goto out;
1703 }
1704
1705 switch (receiver_vcpu->state) {
1706 case VCPU_STATE_OFF:
1707 case VCPU_STATE_RUNNING:
1708 case VCPU_STATE_ABORTED:
1709 case VCPU_STATE_READY:
1710 case VCPU_STATE_BLOCKED_INTERRUPT:
1711 ret = ffa_error(FFA_BUSY);
1712 goto out;
1713 case VCPU_STATE_BLOCKED_MAILBOX:
1714 /*
1715 * Expect target vCPU to be blocked after having called
1716 * ffa_msg_wait or sent a direct message response.
1717 */
1718 break;
1719 }
1720
1721 /* Inject timer interrupt if any pending */
1722 if (arch_timer_pending(&receiver_vcpu->regs)) {
1723 internal_interrupt_inject_locked(vcpus_locked.vcpu1,
1724 HF_VIRTUAL_TIMER_INTID,
1725 current, NULL);
1726
1727 arch_timer_mask(&receiver_vcpu->regs);
1728 }
1729
1730 /* The receiver vCPU runs upon direct message invocation */
1731 receiver_vcpu->cpu = current->cpu;
1732 receiver_vcpu->state = VCPU_STATE_RUNNING;
1733 receiver_vcpu->regs_available = false;
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001734 receiver_vcpu->direct_request_origin_vm_id = sender_vm_id;
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001735
1736 arch_regs_set_retval(&receiver_vcpu->regs, (struct ffa_value){
1737 .func = args.func,
1738 .arg1 = args.arg1,
1739 .arg2 = 0,
1740 .arg3 = args.arg3,
1741 .arg4 = args.arg4,
1742 .arg5 = args.arg5,
1743 .arg6 = args.arg6,
1744 .arg7 = args.arg7,
1745 });
1746
1747 current->state = VCPU_STATE_BLOCKED_MAILBOX;
1748
1749 /* Switch to receiver vCPU targeted to by direct msg request */
1750 *next = receiver_vcpu;
1751
1752 /*
1753 * Since this flow will lead to a VM switch, the return value will not
1754 * be applied to current vCPU.
1755 */
1756
1757out:
1758 sl_unlock(&receiver_vcpu->lock);
1759 sl_unlock(&current->lock);
1760
1761 return ret;
1762}
1763
1764/**
1765 * Send an FF-A direct message response.
1766 */
1767struct ffa_value api_ffa_msg_send_direct_resp(ffa_vm_id_t sender_vm_id,
1768 ffa_vm_id_t receiver_vm_id,
1769 struct ffa_value args,
1770 struct vcpu *current,
1771 struct vcpu **next)
1772{
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001773 struct vcpu_locked current_locked;
J-Alvesfe7f7372020-11-09 11:32:12 +00001774 struct ffa_value to_ret = {
1775 .func = args.func,
1776 .arg1 = args.arg1,
1777 .arg2 = 0,
1778 .arg3 = args.arg3,
1779 .arg4 = args.arg4,
1780 .arg5 = args.arg5,
1781 .arg6 = args.arg6,
1782 .arg7 = args.arg7,
1783 };
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001784
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001785 if (!arch_other_world_is_direct_response_valid(current, sender_vm_id,
1786 receiver_vm_id)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001787 return ffa_error(FFA_NOT_SUPPORTED);
1788 }
1789
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001790 current_locked = vcpu_lock(current);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001791
1792 /*
1793 * Ensure the terminating FFA_MSG_SEND_DIRECT_REQ had a
1794 * defined originator.
1795 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001796 if (!is_ffa_direct_msg_request_ongoing(current_locked)) {
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001797 /*
1798 * Sending direct response but direct request origin vCPU is
1799 * not set.
1800 */
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001801 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001802 return ffa_error(FFA_DENIED);
1803 }
1804
1805 if (current->direct_request_origin_vm_id != receiver_vm_id) {
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001806 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001807 return ffa_error(FFA_DENIED);
1808 }
1809
1810 /* Clear direct request origin for the caller. */
1811 current->direct_request_origin_vm_id = HF_INVALID_VM_ID;
1812
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001813 vcpu_unlock(&current_locked);
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001814
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001815 if (!vm_id_is_current_world(receiver_vm_id)) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001816 *next = api_switch_to_other_world(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001817 VCPU_STATE_BLOCKED_MAILBOX);
1818 } else if (receiver_vm_id == HF_PRIMARY_VM_ID) {
J-Alvesfe7f7372020-11-09 11:32:12 +00001819 *next = api_switch_to_primary(current, to_ret,
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001820 VCPU_STATE_BLOCKED_MAILBOX);
J-Alvesfe7f7372020-11-09 11:32:12 +00001821 } else if (vm_id_is_current_world(receiver_vm_id)) {
1822 /*
1823 * It is expected the receiver_vm_id to be from an SP, otherwise
1824 * 'arch_other_world_is_direct_response_valid' should have
1825 * made function return error before getting to this point.
1826 */
1827 *next = api_switch_to_vm(current, to_ret,
1828 VCPU_STATE_BLOCKED_MAILBOX,
1829 receiver_vm_id);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +02001830 } else {
1831 panic("Invalid direct message response invocation");
1832 }
Olivier Deprezee9d6a92019-11-26 09:14:11 +00001833
1834 return (struct ffa_value){.func = FFA_INTERRUPT_32};
1835}
1836
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001837struct ffa_value api_ffa_mem_send(uint32_t share_func, uint32_t length,
1838 uint32_t fragment_length, ipaddr_t address,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001839 uint32_t page_count, struct vcpu *current)
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001840{
1841 struct vm *from = current->vm;
1842 struct vm *to;
1843 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001844 struct ffa_memory_region *memory_region;
1845 struct ffa_value ret;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001846
1847 if (ipa_addr(address) != 0 || page_count != 0) {
1848 /*
1849 * Hafnium only supports passing the descriptor in the TX
1850 * mailbox.
1851 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001853 }
1854
Andrew Walbranca808b12020-05-15 17:22:28 +01001855 if (fragment_length > length) {
1856 dlog_verbose(
1857 "Fragment length %d greater than total length %d.\n",
1858 fragment_length, length);
1859 return ffa_error(FFA_INVALID_PARAMETERS);
1860 }
1861 if (fragment_length < sizeof(struct ffa_memory_region) +
1862 sizeof(struct ffa_memory_access)) {
1863 dlog_verbose(
1864 "Initial fragment length %d smaller than header size "
1865 "%d.\n",
1866 fragment_length,
1867 sizeof(struct ffa_memory_region) +
1868 sizeof(struct ffa_memory_access));
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001869 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001870 }
1871
1872 /*
1873 * Check that the sender has configured its send buffer. If the TX
1874 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
1875 * be safely accessed after releasing the lock since the TX mailbox
1876 * address can only be configured once.
1877 */
1878 sl_lock(&from->lock);
1879 from_msg = from->mailbox.send;
1880 sl_unlock(&from->lock);
1881
1882 if (from_msg == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001883 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001884 }
1885
1886 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001887 * Copy the memory region descriptor to a fresh page from the memory
1888 * pool. This prevents the sender from changing it underneath us, and
1889 * also lets us keep it around in the share state table if needed.
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001890 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001891 if (fragment_length > HF_MAILBOX_SIZE ||
1892 fragment_length > MM_PPOOL_ENTRY_SIZE) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001893 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001894 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001895 memory_region = (struct ffa_memory_region *)mpool_alloc(&api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001896 if (memory_region == NULL) {
1897 dlog_verbose("Failed to allocate memory region copy.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001898 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001899 }
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001900 memcpy_s(memory_region, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001901
1902 /* The sender must match the caller. */
1903 if (memory_region->sender != from->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001904 dlog_verbose("Memory region sender doesn't match caller.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001905 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001906 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001907 }
1908
Andrew Walbrana65a1322020-04-06 19:32:32 +01001909 if (memory_region->receiver_count != 1) {
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001910 /* Hafnium doesn't support multi-way memory sharing for now. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001911 dlog_verbose(
1912 "Multi-way memory sharing not supported (got %d "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001913 "endpoint memory access descriptors, expected 1).\n",
1914 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001915 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001916 goto out;
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001917 }
1918
1919 /*
1920 * Ensure that the receiver VM exists and isn't the same as the sender.
1921 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001922 to = vm_find(memory_region->receivers[0].receiver_permissions.receiver);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001923 if (to == NULL || to == from) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001924 dlog_verbose("Invalid receiver.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001925 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001926 goto out;
1927 }
1928
1929 if (to->id == HF_TEE_VM_ID) {
1930 /*
1931 * The 'to' VM lock is only needed in the case that it is the
1932 * TEE VM.
1933 */
1934 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
1935
1936 if (msg_receiver_busy(vm_to_from_lock.vm1, from, false)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001937 ret = ffa_error(FFA_BUSY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001938 goto out_unlock;
1939 }
1940
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001941 ret = ffa_memory_tee_send(
1942 vm_to_from_lock.vm2, vm_to_from_lock.vm1, memory_region,
1943 length, fragment_length, share_func, &api_page_pool);
1944 /*
1945 * ffa_tee_memory_send takes ownership of the memory_region, so
1946 * make sure we don't free it.
1947 */
1948 memory_region = NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001949
1950 out_unlock:
1951 vm_unlock(&vm_to_from_lock.vm1);
1952 vm_unlock(&vm_to_from_lock.vm2);
1953 } else {
1954 struct vm_locked from_locked = vm_lock(from);
1955
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001956 ret = ffa_memory_send(from_locked, memory_region, length,
1957 fragment_length, share_func,
1958 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001959 /*
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001960 * ffa_memory_send takes ownership of the memory_region, so
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001961 * make sure we don't free it.
1962 */
1963 memory_region = NULL;
1964
1965 vm_unlock(&from_locked);
1966 }
1967
1968out:
1969 if (memory_region != NULL) {
1970 mpool_free(&api_page_pool, memory_region);
1971 }
1972
1973 return ret;
1974}
1975
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001976struct ffa_value api_ffa_mem_retrieve_req(uint32_t length,
1977 uint32_t fragment_length,
1978 ipaddr_t address, uint32_t page_count,
1979 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001980{
1981 struct vm *to = current->vm;
1982 struct vm_locked to_locked;
1983 const void *to_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001984 struct ffa_memory_region *retrieve_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001985 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001986 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001987
1988 if (ipa_addr(address) != 0 || page_count != 0) {
1989 /*
1990 * Hafnium only supports passing the descriptor in the TX
1991 * mailbox.
1992 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001993 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00001994 }
1995
Andrew Walbrana65a1322020-04-06 19:32:32 +01001996 if (fragment_length != length) {
1997 dlog_verbose("Fragmentation not yet supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001998 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001999 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002000
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002001 retrieve_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002002 (struct ffa_memory_region *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002003 message_buffer_size = cpu_get_buffer_size(current->cpu);
2004 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2005 dlog_verbose("Retrieve request too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002006 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002007 }
2008
2009 to_locked = vm_lock(to);
2010 to_msg = to->mailbox.send;
2011
2012 if (to_msg == NULL) {
2013 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002014 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002015 goto out;
2016 }
2017
2018 /*
2019 * Copy the retrieve request descriptor to an internal buffer, so that
2020 * the caller can't change it underneath us.
2021 */
2022 memcpy_s(retrieve_request, message_buffer_size, to_msg, length);
2023
2024 if (msg_receiver_busy(to_locked, NULL, false)) {
2025 /*
2026 * Can't retrieve memory information if the mailbox is not
2027 * available.
2028 */
2029 dlog_verbose("RX buffer not ready.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002030 ret = ffa_error(FFA_BUSY);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002031 goto out;
2032 }
2033
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002034 ret = ffa_memory_retrieve(to_locked, retrieve_request, length,
2035 &api_page_pool);
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002036
2037out:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002038 vm_unlock(&to_locked);
2039 return ret;
2040}
2041
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002042struct ffa_value api_ffa_mem_relinquish(struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002043{
2044 struct vm *from = current->vm;
2045 struct vm_locked from_locked;
2046 const void *from_msg;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002047 struct ffa_mem_relinquish *relinquish_request;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002048 uint32_t message_buffer_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002049 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002050 uint32_t length;
2051
2052 from_locked = vm_lock(from);
2053 from_msg = from->mailbox.send;
2054
2055 if (from_msg == NULL) {
2056 dlog_verbose("TX buffer not setup.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002057 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002058 goto out;
2059 }
2060
2061 /*
2062 * Calculate length from relinquish descriptor before copying. We will
2063 * check again later to make sure it hasn't changed.
2064 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002065 length = sizeof(struct ffa_mem_relinquish) +
2066 ((struct ffa_mem_relinquish *)from_msg)->endpoint_count *
2067 sizeof(ffa_vm_id_t);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002068 /*
2069 * Copy the relinquish descriptor to an internal buffer, so that the
2070 * caller can't change it underneath us.
2071 */
2072 relinquish_request =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002073 (struct ffa_mem_relinquish *)cpu_get_buffer(current->cpu);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002074 message_buffer_size = cpu_get_buffer_size(current->cpu);
2075 if (length > HF_MAILBOX_SIZE || length > message_buffer_size) {
2076 dlog_verbose("Relinquish message too long.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002078 goto out;
2079 }
2080 memcpy_s(relinquish_request, message_buffer_size, from_msg, length);
2081
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002082 if (sizeof(struct ffa_mem_relinquish) +
2083 relinquish_request->endpoint_count * sizeof(ffa_vm_id_t) !=
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002084 length) {
2085 dlog_verbose(
2086 "Endpoint count changed while copying to internal "
2087 "buffer.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002088 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002089 goto out;
2090 }
2091
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002092 ret = ffa_memory_relinquish(from_locked, relinquish_request,
2093 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002094
2095out:
2096 vm_unlock(&from_locked);
2097 return ret;
2098}
2099
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002100struct ffa_value api_ffa_mem_reclaim(ffa_memory_handle_t handle,
2101 ffa_memory_region_flags_t flags,
2102 struct vcpu *current)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002103{
2104 struct vm *to = current->vm;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002105 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002106
J-Alves917d2f22020-10-30 18:39:30 +00002107 if (ffa_memory_handle_allocated_by_current_world(handle)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00002108 struct vm_locked to_locked = vm_lock(to);
2109
Andrew Walbranca808b12020-05-15 17:22:28 +01002110 ret = ffa_memory_reclaim(to_locked, handle, flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 &api_page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002112
Andrew Walbran290b0c92020-02-03 16:37:14 +00002113 vm_unlock(&to_locked);
2114 } else {
2115 struct vm *from = vm_find(HF_TEE_VM_ID);
2116 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2117
Andrew Walbranca808b12020-05-15 17:22:28 +01002118 ret = ffa_memory_tee_reclaim(vm_to_from_lock.vm1,
2119 vm_to_from_lock.vm2, handle, flags,
2120 &api_page_pool);
2121
2122 vm_unlock(&vm_to_from_lock.vm1);
2123 vm_unlock(&vm_to_from_lock.vm2);
2124 }
2125
2126 return ret;
2127}
2128
2129struct ffa_value api_ffa_mem_frag_rx(ffa_memory_handle_t handle,
2130 uint32_t fragment_offset,
2131 ffa_vm_id_t sender_vm_id,
2132 struct vcpu *current)
2133{
2134 struct vm *to = current->vm;
2135 struct vm_locked to_locked;
2136 struct ffa_value ret;
2137
2138 /* Sender ID MBZ at virtual instance. */
2139 if (sender_vm_id != 0) {
2140 return ffa_error(FFA_INVALID_PARAMETERS);
2141 }
2142
2143 to_locked = vm_lock(to);
2144
2145 if (msg_receiver_busy(to_locked, NULL, false)) {
2146 /*
2147 * Can't retrieve memory information if the mailbox is not
2148 * available.
2149 */
2150 dlog_verbose("RX buffer not ready.\n");
2151 ret = ffa_error(FFA_BUSY);
2152 goto out;
2153 }
2154
2155 ret = ffa_memory_retrieve_continue(to_locked, handle, fragment_offset,
2156 &api_page_pool);
2157
2158out:
2159 vm_unlock(&to_locked);
2160 return ret;
2161}
2162
2163struct ffa_value api_ffa_mem_frag_tx(ffa_memory_handle_t handle,
2164 uint32_t fragment_length,
2165 ffa_vm_id_t sender_vm_id,
2166 struct vcpu *current)
2167{
2168 struct vm *from = current->vm;
2169 const void *from_msg;
2170 void *fragment_copy;
2171 struct ffa_value ret;
2172
2173 /* Sender ID MBZ at virtual instance. */
2174 if (sender_vm_id != 0) {
2175 return ffa_error(FFA_INVALID_PARAMETERS);
2176 }
2177
2178 /*
2179 * Check that the sender has configured its send buffer. If the TX
2180 * mailbox at from_msg is configured (i.e. from_msg != NULL) then it can
2181 * be safely accessed after releasing the lock since the TX mailbox
2182 * address can only be configured once.
2183 */
2184 sl_lock(&from->lock);
2185 from_msg = from->mailbox.send;
2186 sl_unlock(&from->lock);
2187
2188 if (from_msg == NULL) {
2189 return ffa_error(FFA_INVALID_PARAMETERS);
2190 }
2191
2192 /*
2193 * Copy the fragment to a fresh page from the memory pool. This prevents
2194 * the sender from changing it underneath us, and also lets us keep it
2195 * around in the share state table if needed.
2196 */
2197 if (fragment_length > HF_MAILBOX_SIZE ||
2198 fragment_length > MM_PPOOL_ENTRY_SIZE) {
2199 dlog_verbose(
2200 "Fragment length %d larger than mailbox size %d.\n",
2201 fragment_length, HF_MAILBOX_SIZE);
2202 return ffa_error(FFA_INVALID_PARAMETERS);
2203 }
2204 if (fragment_length < sizeof(struct ffa_memory_region_constituent) ||
2205 fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2206 0) {
2207 dlog_verbose("Invalid fragment length %d.\n", fragment_length);
2208 return ffa_error(FFA_INVALID_PARAMETERS);
2209 }
2210 fragment_copy = mpool_alloc(&api_page_pool);
2211 if (fragment_copy == NULL) {
2212 dlog_verbose("Failed to allocate fragment copy.\n");
2213 return ffa_error(FFA_NO_MEMORY);
2214 }
2215 memcpy_s(fragment_copy, MM_PPOOL_ENTRY_SIZE, from_msg, fragment_length);
2216
2217 /*
2218 * Hafnium doesn't support fragmentation of memory retrieve requests
2219 * (because it doesn't support caller-specified mappings, so a request
2220 * will never be larger than a single page), so this must be part of a
2221 * memory send (i.e. donate, lend or share) request.
2222 *
2223 * We can tell from the handle whether the memory transaction is for the
2224 * TEE or not.
2225 */
2226 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
2227 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
2228 struct vm_locked from_locked = vm_lock(from);
2229
2230 ret = ffa_memory_send_continue(from_locked, fragment_copy,
2231 fragment_length, handle,
2232 &api_page_pool);
2233 /*
2234 * `ffa_memory_send_continue` takes ownership of the
2235 * fragment_copy, so we don't need to free it here.
2236 */
2237 vm_unlock(&from_locked);
2238 } else {
2239 struct vm *to = vm_find(HF_TEE_VM_ID);
2240 struct two_vm_locked vm_to_from_lock = vm_lock_both(to, from);
2241
2242 /*
2243 * The TEE RX buffer state is checked in
2244 * `ffa_memory_tee_send_continue` rather than here, as we need
2245 * to return `FFA_MEM_FRAG_RX` with the current offset rather
2246 * than FFA_ERROR FFA_BUSY in case it is busy.
2247 */
2248
2249 ret = ffa_memory_tee_send_continue(
2250 vm_to_from_lock.vm2, vm_to_from_lock.vm1, fragment_copy,
2251 fragment_length, handle, &api_page_pool);
2252 /*
2253 * `ffa_memory_tee_send_continue` takes ownership of the
2254 * fragment_copy, so we don't need to free it here.
2255 */
Andrew Walbran290b0c92020-02-03 16:37:14 +00002256
2257 vm_unlock(&vm_to_from_lock.vm1);
2258 vm_unlock(&vm_to_from_lock.vm2);
2259 }
Andrew Walbrane908c4a2019-12-02 17:13:47 +00002260
2261 return ret;
2262}