blob: d68636c4b7e7f4acbdf63ab412954046efb73759 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
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.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
Olivier Deprez112d2b52020-09-30 07:39:23 +020011#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020012#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000013
Jose Marinho75509b42019-04-09 09:34:59 +010014#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000015#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010016#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010017#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000019#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000021#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000023/** The maximum number of recipients a memory region may be sent to. */
24#define MAX_MEM_SHARE_RECIPIENTS 1
25
26/**
27 * The maximum number of memory sharing handles which may be active at once. A
28 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
29 * or LEND handle is active from when it is sent to when it is reclaimed.
30 */
31#define MAX_MEM_SHARES 100
32
Andrew Walbranca808b12020-05-15 17:22:28 +010033/**
34 * The maximum number of fragments into which a memory sharing message may be
35 * broken.
36 */
37#define MAX_FRAGMENTS 20
38
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
40 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000041 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010042static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
43 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000044 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010046 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010047static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
48 "struct ffa_memory_access must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
50 "struct ffa_memory_region must be a multiple of 16 bytes long.");
51static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
52 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000053 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010055struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056 /**
57 * The memory region being shared, or NULL if this share state is
58 * unallocated.
59 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010060 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061
Andrew Walbranca808b12020-05-15 17:22:28 +010062 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
63
64 /** The number of constituents in each fragment. */
65 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
66
67 /**
68 * The number of valid elements in the `fragments` and
69 * `fragment_constituent_counts` arrays.
70 */
71 uint32_t fragment_count;
72
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000073 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010074 * The FF-A function used for sharing the memory. Must be one of
75 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000076 * share state is allocated, or 0.
77 */
78 uint32_t share_func;
79
80 /**
J-Alves2a0d2882020-10-29 14:49:50 +000081 * The sender's original mode before invoking the FF-A function for
82 * sharing the memory.
83 * This is used to reset the original configuration when sender invokes
84 * FFA_MEM_RECLAIM_32.
85 */
86 uint32_t sender_orig_mode;
87
88 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010089 * True if all the fragments of this sharing request have been sent and
90 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091 */
Andrew Walbranca808b12020-05-15 17:22:28 +010092 bool sending_complete;
93
94 /**
95 * How many fragments of the memory region each recipient has retrieved
96 * so far. The order of this array matches the order of the endpoint
97 * memory access descriptors in the memory region descriptor. Any
98 * entries beyond the receiver_count will always be 0.
99 */
100 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000101};
102
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000103/**
104 * Encapsulates the set of share states while the `share_states_lock` is held.
105 */
106struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100107 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108};
109
110/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100111 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 * by this lock.
113 */
114static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100115static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116
117/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100118 * Buffer for retrieving memory region information from the TEE for when a
119 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
120 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000121 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100122alignas(PAGE_SIZE) static uint8_t
123 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
124
125/**
J-Alves917d2f22020-10-30 18:39:30 +0000126 * Extracts the index from a memory handle allocated by Hafnium's current world.
127 */
128uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
129{
130 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
131}
132
133/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100134 * Initialises the next available `struct ffa_memory_share_state` and sets
135 * `share_state_ret` to a pointer to it. If `handle` is
136 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
137 * uses the provided handle which is assumed to be globally unique.
138 *
139 * Returns true on success or false if none are available.
140 */
141static bool allocate_share_state(
142 struct share_states_locked share_states, uint32_t share_func,
143 struct ffa_memory_region *memory_region, uint32_t fragment_length,
144 ffa_memory_handle_t handle,
145 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100147 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000149 assert(share_states.share_states != NULL);
150 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100153 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100155 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100156 &share_states.share_states[i];
157 struct ffa_composite_memory_region *composite =
158 ffa_memory_region_get_composite(memory_region,
159 0);
160
161 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000162 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200163 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000165 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100166 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000167 allocated_state->share_func = share_func;
168 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 allocated_state->fragment_count = 1;
170 allocated_state->fragments[0] = composite->constituents;
171 allocated_state->fragment_constituent_counts[0] =
172 (fragment_length -
173 ffa_composite_constituent_offset(memory_region,
174 0)) /
175 sizeof(struct ffa_memory_region_constituent);
176 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000177 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 allocated_state->retrieved_fragment_count[j] =
179 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100181 if (share_state_ret != NULL) {
182 *share_state_ret = allocated_state;
183 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000184 return true;
185 }
186 }
187
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188 return false;
189}
190
191/** Locks the share states lock. */
192struct share_states_locked share_states_lock(void)
193{
194 sl_lock(&share_states_lock_instance);
195
196 return (struct share_states_locked){.share_states = share_states};
197}
198
199/** Unlocks the share states lock. */
200static void share_states_unlock(struct share_states_locked *share_states)
201{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000202 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203 share_states->share_states = NULL;
204 sl_unlock(&share_states_lock_instance);
205}
206
207/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100208 * If the given handle is a valid handle for an allocated share state then
209 * initialises `share_state_ret` to point to the share state and returns true.
210 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000211 */
212static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100213 ffa_memory_handle_t handle,
214 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100216 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000217 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000218
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000219 assert(share_states.share_states != NULL);
220 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100221
222 /*
223 * First look for a share_state allocated by us, in which case the
224 * handle is based on the index.
225 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200226 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000227 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100228 if (index < MAX_MEM_SHARES) {
229 share_state = &share_states.share_states[index];
230 if (share_state->share_func != 0) {
231 *share_state_ret = share_state;
232 return true;
233 }
234 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000235 }
236
Andrew Walbranca808b12020-05-15 17:22:28 +0100237 /* Fall back to a linear scan. */
238 for (index = 0; index < MAX_MEM_SHARES; ++index) {
239 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000240 if (share_state->memory_region != NULL &&
241 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100242 share_state->share_func != 0) {
243 *share_state_ret = share_state;
244 return true;
245 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246 }
247
Andrew Walbranca808b12020-05-15 17:22:28 +0100248 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000249}
250
251/** Marks a share state as unallocated. */
252static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100253 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000254 struct mpool *page_pool)
255{
Andrew Walbranca808b12020-05-15 17:22:28 +0100256 uint32_t i;
257
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000258 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000259 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100260 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000261 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100262 /*
263 * First fragment is part of the same page as the `memory_region`, so it
264 * doesn't need to be freed separately.
265 */
266 share_state->fragments[0] = NULL;
267 share_state->fragment_constituent_counts[0] = 0;
268 for (i = 1; i < share_state->fragment_count; ++i) {
269 mpool_free(page_pool, share_state->fragments[i]);
270 share_state->fragments[i] = NULL;
271 share_state->fragment_constituent_counts[i] = 0;
272 }
273 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274 share_state->memory_region = NULL;
275}
276
Andrew Walbranca808b12020-05-15 17:22:28 +0100277/** Checks whether the given share state has been fully sent. */
278static bool share_state_sending_complete(
279 struct share_states_locked share_states,
280 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000281{
Andrew Walbranca808b12020-05-15 17:22:28 +0100282 struct ffa_composite_memory_region *composite;
283 uint32_t expected_constituent_count;
284 uint32_t fragment_constituent_count_total = 0;
285 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000286
Andrew Walbranca808b12020-05-15 17:22:28 +0100287 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000288 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100289
290 /*
291 * Share state must already be valid, or it's not possible to get hold
292 * of it.
293 */
294 CHECK(share_state->memory_region != NULL &&
295 share_state->share_func != 0);
296
297 composite =
298 ffa_memory_region_get_composite(share_state->memory_region, 0);
299 expected_constituent_count = composite->constituent_count;
300 for (i = 0; i < share_state->fragment_count; ++i) {
301 fragment_constituent_count_total +=
302 share_state->fragment_constituent_counts[i];
303 }
304 dlog_verbose(
305 "Checking completion: constituent count %d/%d from %d "
306 "fragments.\n",
307 fragment_constituent_count_total, expected_constituent_count,
308 share_state->fragment_count);
309
310 return fragment_constituent_count_total == expected_constituent_count;
311}
312
313/**
314 * Calculates the offset of the next fragment expected for the given share
315 * state.
316 */
317static uint32_t share_state_next_fragment_offset(
318 struct share_states_locked share_states,
319 struct ffa_memory_share_state *share_state)
320{
321 uint32_t next_fragment_offset;
322 uint32_t i;
323
324 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000325 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100326
327 next_fragment_offset =
328 ffa_composite_constituent_offset(share_state->memory_region, 0);
329 for (i = 0; i < share_state->fragment_count; ++i) {
330 next_fragment_offset +=
331 share_state->fragment_constituent_counts[i] *
332 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 }
334
Andrew Walbranca808b12020-05-15 17:22:28 +0100335 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000336}
337
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000339{
340 uint32_t i;
341
342 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
343 return;
344 }
345
Olivier Deprez935e1b12020-12-22 18:01:29 +0100346 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100347 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100348 "recipients [",
349 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100350 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100351 memory_region->receiver_count);
352 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 if (i != 0) {
354 dlog(", ");
355 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100356 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100357 memory_region->receivers[i].receiver_permissions.receiver,
358 memory_region->receivers[i]
359 .receiver_permissions.permissions,
360 memory_region->receivers[i]
361 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000362 }
363 dlog("]");
364}
365
366static void dump_share_states(void)
367{
368 uint32_t i;
369
370 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
371 return;
372 }
373
374 dlog("Current share states:\n");
375 sl_lock(&share_states_lock_instance);
376 for (i = 0; i < MAX_MEM_SHARES; ++i) {
377 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000380 dlog("SHARE");
381 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100382 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000383 dlog("LEND");
384 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100385 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000386 dlog("DONATE");
387 break;
388 default:
389 dlog("invalid share_func %#x",
390 share_states[i].share_func);
391 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100392 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100394 if (share_states[i].sending_complete) {
395 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000396 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100397 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000398 }
J-Alves2a0d2882020-10-29 14:49:50 +0000399 dlog(" with %d fragments, %d retrieved, "
400 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100401 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000402 share_states[i].retrieved_fragment_count[0],
403 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000404 }
405 }
406 sl_unlock(&share_states_lock_instance);
407}
408
Andrew Walbran475c1452020-02-07 13:22:22 +0000409/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100410static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100411 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000412{
413 uint32_t mode = 0;
414
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415 switch (ffa_get_data_access_attr(permissions)) {
416 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000417 mode = MM_MODE_R;
418 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000420 mode = MM_MODE_R | MM_MODE_W;
421 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100422 case FFA_DATA_ACCESS_NOT_SPECIFIED:
423 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
424 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100425 case FFA_DATA_ACCESS_RESERVED:
426 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100427 }
428
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100429 switch (ffa_get_instruction_access_attr(permissions)) {
430 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000431 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100433 mode |= MM_MODE_X;
434 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100435 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
436 mode |= (default_mode & MM_MODE_X);
437 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100438 case FFA_INSTRUCTION_ACCESS_RESERVED:
439 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000440 }
441
442 return mode;
443}
444
Jose Marinho75509b42019-04-09 09:34:59 +0100445/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000446 * Get the current mode in the stage-2 page table of the given vm of all the
447 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100449 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000451 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 struct ffa_memory_region_constituent **fragments,
453 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100454{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100455 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100456 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100457
Andrew Walbranca808b12020-05-15 17:22:28 +0100458 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100459 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460 * Fail if there are no constituents. Otherwise we would get an
461 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100462 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100463 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100464 }
465
Andrew Walbranca808b12020-05-15 17:22:28 +0100466 for (i = 0; i < fragment_count; ++i) {
467 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
468 ipaddr_t begin = ipa_init(fragments[i][j].address);
469 size_t size = fragments[i][j].page_count * PAGE_SIZE;
470 ipaddr_t end = ipa_add(begin, size);
471 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100472
Andrew Walbranca808b12020-05-15 17:22:28 +0100473 /* Fail if addresses are not page-aligned. */
474 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
475 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
476 return ffa_error(FFA_INVALID_PARAMETERS);
477 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100478
Andrew Walbranca808b12020-05-15 17:22:28 +0100479 /*
480 * Ensure that this constituent memory range is all
481 * mapped with the same mode.
482 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800483 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100484 return ffa_error(FFA_DENIED);
485 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100486
Andrew Walbranca808b12020-05-15 17:22:28 +0100487 /*
488 * Ensure that all constituents are mapped with the same
489 * mode.
490 */
491 if (i == 0) {
492 *orig_mode = current_mode;
493 } else if (current_mode != *orig_mode) {
494 dlog_verbose(
495 "Expected mode %#x but was %#x for %d "
496 "pages at %#x.\n",
497 *orig_mode, current_mode,
498 fragments[i][j].page_count,
499 ipa_addr(begin));
500 return ffa_error(FFA_DENIED);
501 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100502 }
Jose Marinho75509b42019-04-09 09:34:59 +0100503 }
504
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000506}
507
508/**
509 * Verify that all pages have the same mode, that the starting mode
510 * constitutes a valid state and obtain the next mode to apply
511 * to the sending VM.
512 *
513 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100514 * 1) FFA_DENIED if a state transition was not found;
515 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100516 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100517 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100518 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
520 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100522static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100523 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100525 struct ffa_memory_region_constituent **fragments,
526 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
527 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528{
529 const uint32_t state_mask =
530 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100531 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100532 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000533
Andrew Walbranca808b12020-05-15 17:22:28 +0100534 ret = constituents_get_mode(from, orig_from_mode, fragments,
535 fragment_constituent_counts,
536 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100538 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100539 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100540 }
541
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000542 /* Ensure the address range is normal memory and not a device. */
543 if (*orig_from_mode & MM_MODE_D) {
544 dlog_verbose("Can't share device memory (mode is %#x).\n",
545 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547 }
548
549 /*
550 * Ensure the sender is the owner and has exclusive access to the
551 * memory.
552 */
553 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100555 }
556
J-Alves7cd5eb32020-10-16 19:06:10 +0100557 required_from_mode =
558 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
559
Andrew Walbrana65a1322020-04-06 19:32:32 +0100560 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
561 dlog_verbose(
562 "Sender tried to send memory with permissions which "
563 "required mode %#x but only had %#x itself.\n",
564 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100565 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566 }
567
568 /* Find the appropriate new mode. */
569 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000570 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100573 break;
574
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000576 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100577 break;
578
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000580 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100581 break;
582
Jose Marinho75509b42019-04-09 09:34:59 +0100583 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100585 }
586
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100587 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000588}
589
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000591 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100592 struct ffa_memory_region_constituent **fragments,
593 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
594 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000595{
596 const uint32_t state_mask =
597 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
598 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000600
Andrew Walbranca808b12020-05-15 17:22:28 +0100601 ret = constituents_get_mode(from, orig_from_mode, fragments,
602 fragment_constituent_counts,
603 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100605 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 }
607
608 /* Ensure the address range is normal memory and not a device. */
609 if (*orig_from_mode & MM_MODE_D) {
610 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
611 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 }
614
615 /*
616 * Ensure the relinquishing VM is not the owner but has access to the
617 * memory.
618 */
619 orig_from_state = *orig_from_mode & state_mask;
620 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
621 dlog_verbose(
622 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100623 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000624 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100625 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 }
627
628 /* Find the appropriate new mode. */
629 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
630
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632}
633
634/**
635 * Verify that all pages have the same mode, that the starting mode
636 * constitutes a valid state and obtain the next mode to apply
637 * to the retrieving VM.
638 *
639 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 * 1) FFA_DENIED if a state transition was not found;
641 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100642 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100644 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100645 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
646 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000647 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000649 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100650 struct ffa_memory_region_constituent **fragments,
651 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
652 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653{
654 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000656
Andrew Walbranca808b12020-05-15 17:22:28 +0100657 ret = constituents_get_mode(to, &orig_to_mode, fragments,
658 fragment_constituent_counts,
659 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100661 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100662 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000663 }
664
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100665 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000666 const uint32_t state_mask =
667 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
668 uint32_t orig_to_state = orig_to_mode & state_mask;
669
J-Alves9256f162021-12-09 13:18:43 +0000670 /*
671 * If the original ffa memory send call has been processed
672 * successfully, it is expected the orig_to_mode would overlay
673 * with `state_mask`, as a result of the function
674 * `ffa_send_check_transition`.
675 */
676 assert(orig_to_state != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000677 } else {
678 /*
679 * Ensure the retriever has the expected state. We don't care
680 * about the MM_MODE_SHARED bit; either with or without it set
681 * are both valid representations of the !O-NA state.
682 */
683 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
684 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100685 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 }
687 }
688
689 /* Find the appropriate new mode. */
690 *to_mode = memory_to_attributes;
691 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100692 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000693 *to_mode |= 0;
694 break;
695
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000697 *to_mode |= MM_MODE_UNOWNED;
698 break;
699
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100700 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000701 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
702 break;
703
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100704 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000705 *to_mode |= 0;
706 break;
707
708 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100709 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000711 }
712
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100714}
Jose Marinho09b1db82019-08-08 09:16:59 +0100715
716/**
717 * Updates a VM's page table such that the given set of physical address ranges
718 * are mapped in the address space at the corresponding address ranges, in the
719 * mode provided.
720 *
721 * If commit is false, the page tables will be allocated from the mpool but no
722 * mappings will actually be updated. This function must always be called first
723 * with commit false to check that it will succeed before calling with commit
724 * true, to avoid leaving the page table in a half-updated state. To make a
725 * series of changes atomically you can call them all with commit false before
726 * calling them all with commit true.
727 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700728 * vm_ptable_defrag should always be called after a series of page table
729 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100730 *
731 * Returns true on success, or false if the update failed and no changes were
732 * made to memory mappings.
733 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000735 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100736 struct ffa_memory_region_constituent **fragments,
737 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100738 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100739{
Andrew Walbranca808b12020-05-15 17:22:28 +0100740 uint32_t i;
741 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100742
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700743 if (vm_locked.vm->el0_partition) {
744 mode |= MM_MODE_USER | MM_MODE_NG;
745 }
746
Andrew Walbranca808b12020-05-15 17:22:28 +0100747 /* Iterate over the memory region constituents within each fragment. */
748 for (i = 0; i < fragment_count; ++i) {
749 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
750 size_t size = fragments[i][j].page_count * PAGE_SIZE;
751 paddr_t pa_begin =
752 pa_from_ipa(ipa_init(fragments[i][j].address));
753 paddr_t pa_end = pa_add(pa_begin, size);
754
755 if (commit) {
756 vm_identity_commit(vm_locked, pa_begin, pa_end,
757 mode, ppool, NULL);
758 } else if (!vm_identity_prepare(vm_locked, pa_begin,
759 pa_end, mode, ppool)) {
760 return false;
761 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100762 }
763 }
764
765 return true;
766}
767
768/**
769 * Clears a region of physical memory by overwriting it with zeros. The data is
770 * flushed from the cache so the memory has been cleared across the system.
771 */
772static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
773{
774 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000775 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100776 * global mapping of the whole range. Such an approach will limit
777 * the changes to stage-1 tables and will allow only local
778 * invalidation.
779 */
780 bool ret;
781 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
782 void *ptr =
783 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
784 size_t size = pa_difference(begin, end);
785
786 if (!ptr) {
787 /* TODO: partial defrag of failed range. */
788 /* Recover any memory consumed in failed mapping. */
789 mm_defrag(stage1_locked, ppool);
790 goto fail;
791 }
792
793 memset_s(ptr, size, 0, size);
794 arch_mm_flush_dcache(ptr, size);
795 mm_unmap(stage1_locked, begin, end, ppool);
796
797 ret = true;
798 goto out;
799
800fail:
801 ret = false;
802
803out:
804 mm_unlock_stage1(&stage1_locked);
805
806 return ret;
807}
808
809/**
810 * Clears a region of physical memory by overwriting it with zeros. The data is
811 * flushed from the cache so the memory has been cleared across the system.
812 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100813static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100814 struct ffa_memory_region_constituent **fragments,
815 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
816 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100817{
818 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100819 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100820 struct mm_stage1_locked stage1_locked;
821 bool ret = false;
822
823 /*
824 * Create a local pool so any freed memory can't be used by another
825 * thread. This is to ensure each constituent that is mapped can be
826 * unmapped again afterwards.
827 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000828 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100829
Andrew Walbranca808b12020-05-15 17:22:28 +0100830 /* Iterate over the memory region constituents within each fragment. */
831 for (i = 0; i < fragment_count; ++i) {
832 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100833
Andrew Walbranca808b12020-05-15 17:22:28 +0100834 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
835 size_t size = fragments[i][j].page_count * PAGE_SIZE;
836 paddr_t begin =
837 pa_from_ipa(ipa_init(fragments[i][j].address));
838 paddr_t end = pa_add(begin, size);
839
840 if (!clear_memory(begin, end, &local_page_pool)) {
841 /*
842 * api_clear_memory will defrag on failure, so
843 * no need to do it here.
844 */
845 goto out;
846 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100847 }
848 }
849
850 /*
851 * Need to defrag after clearing, as it may have added extra mappings to
852 * the stage 1 page table.
853 */
854 stage1_locked = mm_lock_stage1();
855 mm_defrag(stage1_locked, &local_page_pool);
856 mm_unlock_stage1(&stage1_locked);
857
858 ret = true;
859
860out:
861 mpool_fini(&local_page_pool);
862 return ret;
863}
864
865/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000866 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100867 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000868 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 *
870 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100872 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100873 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100874 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
875 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100876 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100877 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100878 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100880static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000881 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100882 struct ffa_memory_region_constituent **fragments,
883 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
884 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100885 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100886{
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100888 uint32_t orig_from_mode;
889 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100890 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100892
893 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100894 * Make sure constituents are properly aligned to a 64-bit boundary. If
895 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100897 for (i = 0; i < fragment_count; ++i) {
898 if (!is_aligned(fragments[i], 8)) {
899 dlog_verbose("Constituents not aligned.\n");
900 return ffa_error(FFA_INVALID_PARAMETERS);
901 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 }
903
904 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000905 * Check if the state transition is lawful for the sender, ensure that
906 * all constituents of a memory region being shared are at the same
907 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100908 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100909 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100910 &orig_from_mode, fragments,
911 fragment_constituent_counts,
912 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100914 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100915 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 }
917
Andrew Walbran37c574e2020-06-03 11:45:46 +0100918 if (orig_from_mode_ret != NULL) {
919 *orig_from_mode_ret = orig_from_mode;
920 }
921
Jose Marinho09b1db82019-08-08 09:16:59 +0100922 /*
923 * Create a local pool so any freed memory can't be used by another
924 * thread. This is to ensure the original mapping can be restored if the
925 * clear fails.
926 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000927 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100928
929 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000930 * First reserve all required memory for the new page table entries
931 * without committing, to make sure the entire operation will succeed
932 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100933 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 if (!ffa_region_group_identity_map(
935 from_locked, fragments, fragment_constituent_counts,
936 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100937 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100939 goto out;
940 }
941
942 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000943 * Update the mapping for the sender. This won't allocate because the
944 * transaction was already prepared above, but may free pages in the
945 * case that a whole block is being unmapped that was previously
946 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100947 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100948 CHECK(ffa_region_group_identity_map(
949 from_locked, fragments, fragment_constituent_counts,
950 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100951
952 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 fragments, fragment_constituent_counts,
955 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100956 /*
957 * On failure, roll back by returning memory to the sender. This
958 * may allocate pages which were previously freed into
959 * `local_page_pool` by the call above, but will never allocate
960 * more pages than that so can never fail.
961 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100963 from_locked, fragments, fragment_constituent_counts,
964 fragment_count, orig_from_mode, &local_page_pool,
965 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100966
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100968 goto out;
969 }
970
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100971 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972
973out:
974 mpool_fini(&local_page_pool);
975
976 /*
977 * Tidy up the page table by reclaiming failed mappings (if there was an
978 * error) or merging entries into blocks where possible (on success).
979 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700980 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000981
982 return ret;
983}
984
985/**
986 * Validates and maps memory shared from one VM to another.
987 *
988 * This function requires the calling context to hold the <to> lock.
989 *
990 * Returns:
991 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100994 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000995 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100998static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000999 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001000 struct ffa_memory_region_constituent **fragments,
1001 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1002 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1003 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004{
Andrew Walbranca808b12020-05-15 17:22:28 +01001005 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001006 uint32_t to_mode;
1007 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009
1010 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001011 * Make sure constituents are properly aligned to a 64-bit boundary. If
1012 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001014 for (i = 0; i < fragment_count; ++i) {
1015 if (!is_aligned(fragments[i], 8)) {
1016 return ffa_error(FFA_INVALID_PARAMETERS);
1017 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 }
1019
1020 /*
1021 * Check if the state transition is lawful for the recipient, and ensure
1022 * that all constituents of the memory region being retrieved are at the
1023 * same state.
1024 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001025 ret = ffa_retrieve_check_transition(
1026 to_locked, share_func, fragments, fragment_constituent_counts,
1027 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001028 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001029 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001030 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 }
1032
1033 /*
1034 * Create a local pool so any freed memory can't be used by another
1035 * thread. This is to ensure the original mapping can be restored if the
1036 * clear fails.
1037 */
1038 mpool_init_with_fallback(&local_page_pool, page_pool);
1039
1040 /*
1041 * First reserve all required memory for the new page table entries in
1042 * the recipient page tables without committing, to make sure the entire
1043 * operation will succeed without exhausting the page pool.
1044 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001045 if (!ffa_region_group_identity_map(
1046 to_locked, fragments, fragment_constituent_counts,
1047 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001048 /* TODO: partial defrag of failed range. */
1049 dlog_verbose(
1050 "Insufficient memory to update recipient page "
1051 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 goto out;
1054 }
1055
1056 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001057 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001058 fragments, fragment_constituent_counts,
1059 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001060 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001061 goto out;
1062 }
1063
Jose Marinho09b1db82019-08-08 09:16:59 +01001064 /*
1065 * Complete the transfer by mapping the memory into the recipient. This
1066 * won't allocate because the transaction was already prepared above, so
1067 * it doesn't need to use the `local_page_pool`.
1068 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001069 CHECK(ffa_region_group_identity_map(
1070 to_locked, fragments, fragment_constituent_counts,
1071 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001072
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001074
1075out:
1076 mpool_fini(&local_page_pool);
1077
1078 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001079 * Tidy up the page table by reclaiming failed mappings (if there was an
1080 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001081 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001082 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083
1084 return ret;
1085}
1086
Andrew Walbran290b0c92020-02-03 16:37:14 +00001087/**
1088 * Reclaims the given memory from the TEE. To do this space is first reserved in
1089 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1090 * then (if that is successful) the memory is mapped back into the <to> VM's
1091 * page table.
1092 *
1093 * This function requires the calling context to hold the <to> lock.
1094 *
1095 * Returns:
1096 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001097 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001098 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001099 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001100 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001101 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001102 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001103static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001104 struct vm_locked to_locked, ffa_memory_handle_t handle,
1105 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001106 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1107 struct mpool *page_pool)
1108{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001109 uint32_t to_mode;
1110 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 struct ffa_value ret;
1112 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001113
1114 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001115 * Make sure constituents are properly aligned to a 64-bit boundary. If
1116 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001117 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001118 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001119 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001121 }
1122
1123 /*
1124 * Check if the state transition is lawful for the recipient, and ensure
1125 * that all constituents of the memory region being retrieved are at the
1126 * same state.
1127 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001129 &constituents, &constituent_count,
1130 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001131 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001132 dlog_verbose("Invalid transition.\n");
1133 return ret;
1134 }
1135
1136 /*
1137 * Create a local pool so any freed memory can't be used by another
1138 * thread. This is to ensure the original mapping can be restored if the
1139 * clear fails.
1140 */
1141 mpool_init_with_fallback(&local_page_pool, page_pool);
1142
1143 /*
1144 * First reserve all required memory for the new page table entries in
1145 * the recipient page tables without committing, to make sure the entire
1146 * operation will succeed without exhausting the page pool.
1147 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 if (!ffa_region_group_identity_map(to_locked, &constituents,
1149 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001151 /* TODO: partial defrag of failed range. */
1152 dlog_verbose(
1153 "Insufficient memory to update recipient page "
1154 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001156 goto out;
1157 }
1158
1159 /*
1160 * Forward the request to the TEE and see what happens.
1161 */
1162 tee_flags = 0;
1163 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001165 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001166 ret = arch_other_world_call(
1167 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1168 .arg1 = (uint32_t)handle,
1169 .arg2 = (uint32_t)(handle >> 32),
1170 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001171
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001173 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001174 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1175 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001176 ret.func, ret.arg2);
1177 goto out;
1178 }
1179
1180 /*
1181 * The TEE was happy with it, so complete the reclaim by mapping the
1182 * memory into the recipient. This won't allocate because the
1183 * transaction was already prepared above, so it doesn't need to use the
1184 * `local_page_pool`.
1185 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001186 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1187 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001189
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001191
1192out:
1193 mpool_fini(&local_page_pool);
1194
1195 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001196 * Tidy up the page table by reclaiming failed mappings (if there was an
1197 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001198 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001199 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001200
1201 return ret;
1202}
1203
Andrew Walbran996d1d12020-05-27 14:08:43 +01001204static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001205 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001206 struct ffa_memory_region_constituent **fragments,
1207 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1208 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001209{
1210 uint32_t orig_from_mode;
1211 uint32_t from_mode;
1212 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001214
Andrew Walbranca808b12020-05-15 17:22:28 +01001215 ret = ffa_relinquish_check_transition(
1216 from_locked, &orig_from_mode, fragments,
1217 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001218 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001219 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001220 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001221 }
1222
1223 /*
1224 * Create a local pool so any freed memory can't be used by another
1225 * thread. This is to ensure the original mapping can be restored if the
1226 * clear fails.
1227 */
1228 mpool_init_with_fallback(&local_page_pool, page_pool);
1229
1230 /*
1231 * First reserve all required memory for the new page table entries
1232 * without committing, to make sure the entire operation will succeed
1233 * without exhausting the page pool.
1234 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001235 if (!ffa_region_group_identity_map(
1236 from_locked, fragments, fragment_constituent_counts,
1237 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001238 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240 goto out;
1241 }
1242
1243 /*
1244 * Update the mapping for the sender. This won't allocate because the
1245 * transaction was already prepared above, but may free pages in the
1246 * case that a whole block is being unmapped that was previously
1247 * partially mapped.
1248 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001249 CHECK(ffa_region_group_identity_map(
1250 from_locked, fragments, fragment_constituent_counts,
1251 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252
1253 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001255 fragments, fragment_constituent_counts,
1256 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001257 /*
1258 * On failure, roll back by returning memory to the sender. This
1259 * may allocate pages which were previously freed into
1260 * `local_page_pool` by the call above, but will never allocate
1261 * more pages than that so can never fail.
1262 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001263 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001264 from_locked, fragments, fragment_constituent_counts,
1265 fragment_count, orig_from_mode, &local_page_pool,
1266 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001268 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001269 goto out;
1270 }
1271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001273
1274out:
1275 mpool_fini(&local_page_pool);
1276
1277 /*
1278 * Tidy up the page table by reclaiming failed mappings (if there was an
1279 * error) or merging entries into blocks where possible (on success).
1280 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001281 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001282
1283 return ret;
1284}
1285
1286/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001287 * Complete a memory sending operation by checking that it is valid, updating
1288 * the sender page table, and then either marking the share state as having
1289 * completed sending (on success) or freeing it (on failure).
1290 *
1291 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1292 */
1293static struct ffa_value ffa_memory_send_complete(
1294 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001295 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1296 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001297{
1298 struct ffa_memory_region *memory_region = share_state->memory_region;
1299 struct ffa_value ret;
1300
1301 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001302 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001303
1304 /* Check that state is valid in sender page table and update. */
1305 ret = ffa_send_check_update(
1306 from_locked, share_state->fragments,
1307 share_state->fragment_constituent_counts,
1308 share_state->fragment_count, share_state->share_func,
1309 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001310 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1311 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001312 if (ret.func != FFA_SUCCESS_32) {
1313 /*
1314 * Free share state, it failed to send so it can't be retrieved.
1315 */
1316 dlog_verbose("Complete failed, freeing share state.\n");
1317 share_state_free(share_states, share_state, page_pool);
1318 return ret;
1319 }
1320
1321 share_state->sending_complete = true;
1322 dlog_verbose("Marked sending complete.\n");
1323
J-Alvesee68c542020-10-29 17:48:20 +00001324 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001325}
1326
1327/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 * Check that the given `memory_region` represents a valid memory send request
1329 * of the given `share_func` type, return the clear flag and permissions via the
1330 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001331 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001333 * not.
1334 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001336 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1337 uint32_t memory_share_length, uint32_t fragment_length,
1338 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001339{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001341 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001342 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001343 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001344 enum ffa_data_access data_access;
1345 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001346
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001347 assert(permissions != NULL);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001348
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001349 /*
1350 * This should already be checked by the caller, just making the
1351 * assumption clear here.
1352 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001353 assert(memory_region->receiver_count == 1);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001354
Andrew Walbrana65a1322020-04-06 19:32:32 +01001355 /* The sender must match the message sender. */
1356 if (memory_region->sender != from_locked.vm->id) {
1357 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001358 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001359 }
1360
Andrew Walbrana65a1322020-04-06 19:32:32 +01001361 /*
1362 * Ensure that the composite header is within the memory bounds and
1363 * doesn't overlap the first part of the message.
1364 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001365 receivers_length = sizeof(struct ffa_memory_access) *
1366 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001367 constituents_offset =
1368 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001369 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001370 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001371 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001372 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001373 "Invalid composite memory region descriptor offset "
1374 "%d.\n",
1375 memory_region->receivers[0]
1376 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001377 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001378 }
1379
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001380 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381
1382 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001383 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001385 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1386 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001387 if (memory_share_length != constituents_offset + constituents_length) {
1388 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001389 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 memory_region->receivers[0]
1391 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001392 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001393 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001394 if (fragment_length < memory_share_length &&
1395 fragment_length < HF_MAILBOX_SIZE) {
1396 dlog_warning(
1397 "Initial fragment length %d smaller than mailbox "
1398 "size.\n",
1399 fragment_length);
1400 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001401
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 /*
1403 * Clear is not allowed for memory sharing, as the sender still has
1404 * access to the memory.
1405 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001406 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1407 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001408 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 }
1411
1412 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001413 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001416 }
1417
1418 /* Check that the permissions are valid. */
1419 *permissions =
1420 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 data_access = ffa_get_data_access_attr(*permissions);
1422 instruction_access = ffa_get_instruction_access_attr(*permissions);
1423 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1424 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1426 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 dlog_verbose(
1431 "Invalid instruction access permissions %#x for "
1432 "sending memory.\n",
1433 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001434 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 if (share_func == FFA_MEM_SHARE_32) {
1437 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001438 dlog_verbose(
1439 "Invalid data access permissions %#x for "
1440 "sharing memory.\n",
1441 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001442 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001443 }
1444 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001445 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1446 * required for share operations (but must not be specified by
1447 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448 * returned to the retriever.
1449 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450 ffa_set_instruction_access_attr(permissions,
1451 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 memory_region->receivers[0].receiver_permissions.permissions =
1453 *permissions;
1454 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 if (share_func == FFA_MEM_LEND_32 &&
1456 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 dlog_verbose(
1458 "Invalid data access permissions %#x for lending "
1459 "memory.\n",
1460 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001462 }
Federico Recanati85090c42021-12-15 13:17:54 +01001463
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001464 if (share_func == FFA_MEM_DONATE_32 &&
1465 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001466 dlog_verbose(
1467 "Invalid data access permissions %#x for donating "
1468 "memory.\n",
1469 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001470 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001471 }
1472
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474}
1475
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001476/** Forwards a memory send message on to the TEE. */
1477static struct ffa_value memory_send_tee_forward(
1478 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1479 uint32_t share_func, struct ffa_memory_region *memory_region,
1480 uint32_t memory_share_length, uint32_t fragment_length)
1481{
1482 struct ffa_value ret;
1483
1484 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1485 memory_region, fragment_length);
1486 tee_locked.vm->mailbox.recv_size = fragment_length;
1487 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1488 tee_locked.vm->mailbox.recv_func = share_func;
1489 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001490 ret = arch_other_world_call(
1491 (struct ffa_value){.func = share_func,
1492 .arg1 = memory_share_length,
1493 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001494 /*
1495 * After the call to the TEE completes it must have finished reading its
1496 * RX buffer, so it is ready for another message.
1497 */
1498 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1499
1500 return ret;
1501}
1502
Andrew Walbrana65a1322020-04-06 19:32:32 +01001503/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001504 * Gets the share state for continuing an operation to donate, lend or share
1505 * memory, and checks that it is a valid request.
1506 *
1507 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1508 * not.
1509 */
1510static struct ffa_value ffa_memory_send_continue_validate(
1511 struct share_states_locked share_states, ffa_memory_handle_t handle,
1512 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1513 struct mpool *page_pool)
1514{
1515 struct ffa_memory_share_state *share_state;
1516 struct ffa_memory_region *memory_region;
1517
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001518 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001519
1520 /*
1521 * Look up the share state by handle and make sure that the VM ID
1522 * matches.
1523 */
1524 if (!get_share_state(share_states, handle, &share_state)) {
1525 dlog_verbose(
1526 "Invalid handle %#x for memory send continuation.\n",
1527 handle);
1528 return ffa_error(FFA_INVALID_PARAMETERS);
1529 }
1530 memory_region = share_state->memory_region;
1531
1532 if (memory_region->sender != from_vm_id) {
1533 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1534 return ffa_error(FFA_INVALID_PARAMETERS);
1535 }
1536
1537 if (share_state->sending_complete) {
1538 dlog_verbose(
1539 "Sending of memory handle %#x is already complete.\n",
1540 handle);
1541 return ffa_error(FFA_INVALID_PARAMETERS);
1542 }
1543
1544 if (share_state->fragment_count == MAX_FRAGMENTS) {
1545 /*
1546 * Log a warning as this is a sign that MAX_FRAGMENTS should
1547 * probably be increased.
1548 */
1549 dlog_warning(
1550 "Too many fragments for memory share with handle %#x; "
1551 "only %d supported.\n",
1552 handle, MAX_FRAGMENTS);
1553 /* Free share state, as it's not possible to complete it. */
1554 share_state_free(share_states, share_state, page_pool);
1555 return ffa_error(FFA_NO_MEMORY);
1556 }
1557
1558 *share_state_ret = share_state;
1559
1560 return (struct ffa_value){.func = FFA_SUCCESS_32};
1561}
1562
1563/**
1564 * Forwards a memory send continuation message on to the TEE.
1565 */
1566static struct ffa_value memory_send_continue_tee_forward(
1567 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1568 uint32_t fragment_length, ffa_memory_handle_t handle)
1569{
1570 struct ffa_value ret;
1571
1572 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1573 fragment_length);
1574 tee_locked.vm->mailbox.recv_size = fragment_length;
1575 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1576 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1577 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001578 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001579 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1580 .arg1 = (uint32_t)handle,
1581 .arg2 = (uint32_t)(handle >> 32),
1582 .arg3 = fragment_length,
1583 .arg4 = (uint64_t)sender_vm_id << 16});
1584 /*
1585 * After the call to the TEE completes it must have finished reading its
1586 * RX buffer, so it is ready for another message.
1587 */
1588 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1589
1590 return ret;
1591}
1592
1593/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001594 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1595 * updates the stage-2 page tables. Specifically, check if the message length
1596 * and number of memory region constituents match, and if the transition is
1597 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001598 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001599 * Assumes that the caller has already found and locked the sender VM and copied
1600 * the memory region descriptor from the sender's TX buffer to a freshly
1601 * allocated page from Hafnium's internal pool. The caller must have also
1602 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001603 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001604 * This function takes ownership of the `memory_region` passed in and will free
1605 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001606 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001607struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001608 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001609 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001610 uint32_t fragment_length, uint32_t share_func,
1611 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001612{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 ffa_memory_access_permissions_t permissions;
1614 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001615 struct share_states_locked share_states;
1616 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001617
1618 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001619 * If there is an error validating the `memory_region` then we need to
1620 * free it because we own it but we won't be storing it in a share state
1621 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001622 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001623 ret = ffa_memory_send_validate(from_locked, memory_region,
1624 memory_share_length, fragment_length,
1625 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001626 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001627 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001628 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001629 }
1630
Andrew Walbrana65a1322020-04-06 19:32:32 +01001631 /* Set flag for share function, ready to be retrieved later. */
1632 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001633 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001634 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001635 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001636 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 case FFA_MEM_LEND_32:
1638 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001639 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001640 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001641 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001642 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001643 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001644 }
1645
Andrew Walbranca808b12020-05-15 17:22:28 +01001646 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001647 /*
1648 * Allocate a share state before updating the page table. Otherwise if
1649 * updating the page table succeeded but allocating the share state
1650 * failed then it would leave the memory in a state where nobody could
1651 * get it back.
1652 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001653 if (!allocate_share_state(share_states, share_func, memory_region,
1654 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1655 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656 dlog_verbose("Failed to allocate share state.\n");
1657 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001658 ret = ffa_error(FFA_NO_MEMORY);
1659 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001660 }
1661
Andrew Walbranca808b12020-05-15 17:22:28 +01001662 if (fragment_length == memory_share_length) {
1663 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001664 ret = ffa_memory_send_complete(
1665 from_locked, share_states, share_state, page_pool,
1666 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001667 } else {
1668 ret = (struct ffa_value){
1669 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001670 .arg1 = (uint32_t)memory_region->handle,
1671 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001672 .arg3 = fragment_length};
1673 }
1674
1675out:
1676 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001677 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001678 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001679}
1680
1681/**
1682 * Validates a call to donate, lend or share memory to the TEE and then updates
1683 * the stage-2 page tables. Specifically, check if the message length and number
1684 * of memory region constituents match, and if the transition is valid for the
1685 * type of memory sending operation.
1686 *
1687 * Assumes that the caller has already found and locked the sender VM and the
1688 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1689 * to a freshly allocated page from Hafnium's internal pool. The caller must
1690 * have also validated that the receiver VM ID is valid.
1691 *
1692 * This function takes ownership of the `memory_region` passed in and will free
1693 * it when necessary; it must not be freed by the caller.
1694 */
1695struct ffa_value ffa_memory_tee_send(
1696 struct vm_locked from_locked, struct vm_locked to_locked,
1697 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1698 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1699{
1700 ffa_memory_access_permissions_t permissions;
1701 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001702
1703 /*
1704 * If there is an error validating the `memory_region` then we need to
1705 * free it because we own it but we won't be storing it in a share state
1706 * after all.
1707 */
1708 ret = ffa_memory_send_validate(from_locked, memory_region,
1709 memory_share_length, fragment_length,
1710 share_func, &permissions);
1711 if (ret.func != FFA_SUCCESS_32) {
1712 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001713 }
1714
Andrew Walbranca808b12020-05-15 17:22:28 +01001715 if (fragment_length == memory_share_length) {
1716 /* No more fragments to come, everything fit in one message. */
1717 struct ffa_composite_memory_region *composite =
1718 ffa_memory_region_get_composite(memory_region, 0);
1719 struct ffa_memory_region_constituent *constituents =
1720 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001721 struct mpool local_page_pool;
1722 uint32_t orig_from_mode;
1723
1724 /*
1725 * Use a local page pool so that we can roll back if necessary.
1726 */
1727 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001728
1729 ret = ffa_send_check_update(
1730 from_locked, &constituents,
1731 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001732 permissions, &local_page_pool,
1733 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1734 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001735 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001736 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001737 goto out;
1738 }
1739
1740 /* Forward memory send message on to TEE. */
1741 ret = memory_send_tee_forward(
1742 to_locked, from_locked.vm->id, share_func,
1743 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001744
1745 if (ret.func != FFA_SUCCESS_32) {
1746 dlog_verbose(
1747 "TEE didn't successfully complete memory send "
1748 "operation; returned %#x (%d). Rolling back.\n",
1749 ret.func, ret.arg2);
1750
1751 /*
1752 * The TEE failed to complete the send operation, so
1753 * roll back the page table update for the VM. This
1754 * can't fail because it won't try to allocate more
1755 * memory than was freed into the `local_page_pool` by
1756 * `ffa_send_check_update` in the initial update.
1757 */
1758 CHECK(ffa_region_group_identity_map(
1759 from_locked, &constituents,
1760 &composite->constituent_count, 1,
1761 orig_from_mode, &local_page_pool, true));
1762 }
1763
1764 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001765 } else {
1766 struct share_states_locked share_states = share_states_lock();
1767 ffa_memory_handle_t handle;
1768
1769 /*
1770 * We need to wait for the rest of the fragments before we can
1771 * check whether the transaction is valid and unmap the memory.
1772 * Call the TEE so it can do its initial validation and assign a
1773 * handle, and allocate a share state to keep what we have so
1774 * far.
1775 */
1776 ret = memory_send_tee_forward(
1777 to_locked, from_locked.vm->id, share_func,
1778 memory_region, memory_share_length, fragment_length);
1779 if (ret.func == FFA_ERROR_32) {
1780 goto out_unlock;
1781 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1782 dlog_warning(
1783 "Got %#x from TEE in response to %#x for "
1784 "fragment with with %d/%d, expected "
1785 "FFA_MEM_FRAG_RX.\n",
1786 ret.func, share_func, fragment_length,
1787 memory_share_length);
1788 ret = ffa_error(FFA_INVALID_PARAMETERS);
1789 goto out_unlock;
1790 }
1791 handle = ffa_frag_handle(ret);
1792 if (ret.arg3 != fragment_length) {
1793 dlog_warning(
1794 "Got unexpected fragment offset %d for "
1795 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1796 ret.arg3, fragment_length);
1797 ret = ffa_error(FFA_INVALID_PARAMETERS);
1798 goto out_unlock;
1799 }
1800 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1801 dlog_warning(
1802 "Got unexpected sender ID %d for "
1803 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1804 ffa_frag_sender(ret), from_locked.vm->id);
1805 ret = ffa_error(FFA_INVALID_PARAMETERS);
1806 goto out_unlock;
1807 }
1808
1809 if (!allocate_share_state(share_states, share_func,
1810 memory_region, fragment_length,
1811 handle, NULL)) {
1812 dlog_verbose("Failed to allocate share state.\n");
1813 ret = ffa_error(FFA_NO_MEMORY);
1814 goto out_unlock;
1815 }
1816 /*
1817 * Don't free the memory region fragment, as it has been stored
1818 * in the share state.
1819 */
1820 memory_region = NULL;
1821 out_unlock:
1822 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001823 }
1824
Andrew Walbranca808b12020-05-15 17:22:28 +01001825out:
1826 if (memory_region != NULL) {
1827 mpool_free(page_pool, memory_region);
1828 }
1829 dump_share_states();
1830 return ret;
1831}
1832
1833/**
1834 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1835 * this is the last fragment then checks that the transition is valid for the
1836 * type of memory sending operation and updates the stage-2 page tables of the
1837 * sender.
1838 *
1839 * Assumes that the caller has already found and locked the sender VM and copied
1840 * the memory region descriptor from the sender's TX buffer to a freshly
1841 * allocated page from Hafnium's internal pool.
1842 *
1843 * This function takes ownership of the `fragment` passed in; it must not be
1844 * freed by the caller.
1845 */
1846struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1847 void *fragment,
1848 uint32_t fragment_length,
1849 ffa_memory_handle_t handle,
1850 struct mpool *page_pool)
1851{
1852 struct share_states_locked share_states = share_states_lock();
1853 struct ffa_memory_share_state *share_state;
1854 struct ffa_value ret;
1855 struct ffa_memory_region *memory_region;
1856
1857 ret = ffa_memory_send_continue_validate(share_states, handle,
1858 &share_state,
1859 from_locked.vm->id, page_pool);
1860 if (ret.func != FFA_SUCCESS_32) {
1861 goto out_free_fragment;
1862 }
1863 memory_region = share_state->memory_region;
1864
1865 if (memory_region->receivers[0].receiver_permissions.receiver ==
1866 HF_TEE_VM_ID) {
1867 dlog_error(
1868 "Got hypervisor-allocated handle for memory send to "
1869 "TEE. This should never happen, and indicates a bug in "
1870 "EL3 code.\n");
1871 ret = ffa_error(FFA_INVALID_PARAMETERS);
1872 goto out_free_fragment;
1873 }
1874
1875 /* Add this fragment. */
1876 share_state->fragments[share_state->fragment_count] = fragment;
1877 share_state->fragment_constituent_counts[share_state->fragment_count] =
1878 fragment_length / sizeof(struct ffa_memory_region_constituent);
1879 share_state->fragment_count++;
1880
1881 /* Check whether the memory send operation is now ready to complete. */
1882 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001883 ret = ffa_memory_send_complete(
1884 from_locked, share_states, share_state, page_pool,
1885 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001886 } else {
1887 ret = (struct ffa_value){
1888 .func = FFA_MEM_FRAG_RX_32,
1889 .arg1 = (uint32_t)handle,
1890 .arg2 = (uint32_t)(handle >> 32),
1891 .arg3 = share_state_next_fragment_offset(share_states,
1892 share_state)};
1893 }
1894 goto out;
1895
1896out_free_fragment:
1897 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001898
1899out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001900 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001901 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001902}
1903
Andrew Walbranca808b12020-05-15 17:22:28 +01001904/**
1905 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1906 * is the last fragment then checks that the transition is valid for the type of
1907 * memory sending operation and updates the stage-2 page tables of the sender.
1908 *
1909 * Assumes that the caller has already found and locked the sender VM and copied
1910 * the memory region descriptor from the sender's TX buffer to a freshly
1911 * allocated page from Hafnium's internal pool.
1912 *
1913 * This function takes ownership of the `memory_region` passed in and will free
1914 * it when necessary; it must not be freed by the caller.
1915 */
1916struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1917 struct vm_locked to_locked,
1918 void *fragment,
1919 uint32_t fragment_length,
1920 ffa_memory_handle_t handle,
1921 struct mpool *page_pool)
1922{
1923 struct share_states_locked share_states = share_states_lock();
1924 struct ffa_memory_share_state *share_state;
1925 struct ffa_value ret;
1926 struct ffa_memory_region *memory_region;
1927
1928 ret = ffa_memory_send_continue_validate(share_states, handle,
1929 &share_state,
1930 from_locked.vm->id, page_pool);
1931 if (ret.func != FFA_SUCCESS_32) {
1932 goto out_free_fragment;
1933 }
1934 memory_region = share_state->memory_region;
1935
1936 if (memory_region->receivers[0].receiver_permissions.receiver !=
1937 HF_TEE_VM_ID) {
1938 dlog_error(
1939 "Got SPM-allocated handle for memory send to non-TEE "
1940 "VM. This should never happen, and indicates a bug.\n");
1941 ret = ffa_error(FFA_INVALID_PARAMETERS);
1942 goto out_free_fragment;
1943 }
1944
1945 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1946 to_locked.vm->mailbox.recv == NULL) {
1947 /*
1948 * If the TEE RX buffer is not available, tell the sender to
1949 * retry by returning the current offset again.
1950 */
1951 ret = (struct ffa_value){
1952 .func = FFA_MEM_FRAG_RX_32,
1953 .arg1 = (uint32_t)handle,
1954 .arg2 = (uint32_t)(handle >> 32),
1955 .arg3 = share_state_next_fragment_offset(share_states,
1956 share_state),
1957 };
1958 goto out_free_fragment;
1959 }
1960
1961 /* Add this fragment. */
1962 share_state->fragments[share_state->fragment_count] = fragment;
1963 share_state->fragment_constituent_counts[share_state->fragment_count] =
1964 fragment_length / sizeof(struct ffa_memory_region_constituent);
1965 share_state->fragment_count++;
1966
1967 /* Check whether the memory send operation is now ready to complete. */
1968 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001969 struct mpool local_page_pool;
1970 uint32_t orig_from_mode;
1971
1972 /*
1973 * Use a local page pool so that we can roll back if necessary.
1974 */
1975 mpool_init_with_fallback(&local_page_pool, page_pool);
1976
Andrew Walbranca808b12020-05-15 17:22:28 +01001977 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001978 share_state, &local_page_pool,
1979 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001980
1981 if (ret.func == FFA_SUCCESS_32) {
1982 /*
1983 * Forward final fragment on to the TEE so that
1984 * it can complete the memory sending operation.
1985 */
1986 ret = memory_send_continue_tee_forward(
1987 to_locked, from_locked.vm->id, fragment,
1988 fragment_length, handle);
1989
1990 if (ret.func != FFA_SUCCESS_32) {
1991 /*
1992 * The error will be passed on to the caller,
1993 * but log it here too.
1994 */
1995 dlog_verbose(
1996 "TEE didn't successfully complete "
1997 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001998 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001999 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002000
2001 /*
2002 * The TEE failed to complete the send
2003 * operation, so roll back the page table update
2004 * for the VM. This can't fail because it won't
2005 * try to allocate more memory than was freed
2006 * into the `local_page_pool` by
2007 * `ffa_send_check_update` in the initial
2008 * update.
2009 */
2010 CHECK(ffa_region_group_identity_map(
2011 from_locked, share_state->fragments,
2012 share_state
2013 ->fragment_constituent_counts,
2014 share_state->fragment_count,
2015 orig_from_mode, &local_page_pool,
2016 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002017 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002018
Andrew Walbranca808b12020-05-15 17:22:28 +01002019 /* Free share state. */
2020 share_state_free(share_states, share_state, page_pool);
2021 } else {
2022 /* Abort sending to TEE. */
2023 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002024 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002025 .func = FFA_MEM_RECLAIM_32,
2026 .arg1 = (uint32_t)handle,
2027 .arg2 = (uint32_t)(handle >> 32)});
2028
2029 if (tee_ret.func != FFA_SUCCESS_32) {
2030 /*
2031 * Nothing we can do if TEE doesn't abort
2032 * properly, just log it.
2033 */
2034 dlog_verbose(
2035 "TEE didn't successfully abort failed "
2036 "memory send operation; returned %#x "
2037 "(%d).\n",
2038 tee_ret.func, tee_ret.arg2);
2039 }
2040 /*
2041 * We don't need to free the share state in this case
2042 * because ffa_memory_send_complete does that already.
2043 */
2044 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002045
2046 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002047 } else {
2048 uint32_t next_fragment_offset =
2049 share_state_next_fragment_offset(share_states,
2050 share_state);
2051
2052 ret = memory_send_continue_tee_forward(
2053 to_locked, from_locked.vm->id, fragment,
2054 fragment_length, handle);
2055
2056 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2057 ffa_frag_handle(ret) != handle ||
2058 ret.arg3 != next_fragment_offset ||
2059 ffa_frag_sender(ret) != from_locked.vm->id) {
2060 dlog_verbose(
2061 "Got unexpected result from forwarding "
2062 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2063 "offset %d, sender %d); expected "
2064 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2065 "sender %d).\n",
2066 ret.func, ffa_frag_handle(ret), ret.arg3,
2067 ffa_frag_sender(ret), handle,
2068 next_fragment_offset, from_locked.vm->id);
2069 /* Free share state. */
2070 share_state_free(share_states, share_state, page_pool);
2071 ret = ffa_error(FFA_INVALID_PARAMETERS);
2072 goto out;
2073 }
2074
2075 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2076 .arg1 = (uint32_t)handle,
2077 .arg2 = (uint32_t)(handle >> 32),
2078 .arg3 = next_fragment_offset};
2079 }
2080 goto out;
2081
2082out_free_fragment:
2083 mpool_free(page_pool, fragment);
2084
2085out:
2086 share_states_unlock(&share_states);
2087 return ret;
2088}
2089
2090/** Clean up after the receiver has finished retrieving a memory region. */
2091static void ffa_memory_retrieve_complete(
2092 struct share_states_locked share_states,
2093 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2094{
2095 if (share_state->share_func == FFA_MEM_DONATE_32) {
2096 /*
2097 * Memory that has been donated can't be relinquished,
2098 * so no need to keep the share state around.
2099 */
2100 share_state_free(share_states, share_state, page_pool);
2101 dlog_verbose("Freed share state for donate.\n");
2102 }
2103}
2104
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002105struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2106 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002107 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002108 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002109{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002110 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002112 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002113 sizeof(struct ffa_memory_access);
2114 ffa_memory_handle_t handle = retrieve_request->handle;
2115 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002116 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002117 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2118 struct ffa_memory_region *memory_region;
2119 ffa_memory_access_permissions_t sent_permissions;
2120 enum ffa_data_access sent_data_access;
2121 enum ffa_instruction_access sent_instruction_access;
2122 ffa_memory_access_permissions_t requested_permissions;
2123 enum ffa_data_access requested_data_access;
2124 enum ffa_instruction_access requested_instruction_access;
2125 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002126 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002127 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002128 struct ffa_memory_share_state *share_state;
2129 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002130 struct ffa_composite_memory_region *composite;
2131 uint32_t total_length;
2132 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002133
2134 dump_share_states();
2135
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002136 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002137 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002138 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002139 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002140 expected_retrieve_request_length,
2141 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002142 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002143 }
2144
Andrew Walbrana65a1322020-04-06 19:32:32 +01002145 if (retrieve_request->receiver_count != 1) {
2146 dlog_verbose(
2147 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002149 "expected 1).\n",
2150 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002151 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002152 }
2153
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002154 share_states = share_states_lock();
2155 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002156 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002157 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002159 goto out;
2160 }
2161
Andrew Walbrana65a1322020-04-06 19:32:32 +01002162 memory_region = share_state->memory_region;
2163 CHECK(memory_region != NULL);
2164
2165 /*
2166 * Check that the transaction type expected by the receiver is correct,
2167 * if it has been specified.
2168 */
2169 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002170 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002171 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002172 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002173 dlog_verbose(
2174 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002175 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002176 transaction_type,
2177 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002178 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002179 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002180 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002181 goto out;
2182 }
2183
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002184 if (retrieve_request->sender != memory_region->sender) {
2185 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002186 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002187 "expected %d for handle %#x.\n",
2188 retrieve_request->sender, memory_region->sender,
2189 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002190 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002191 goto out;
2192 }
2193
2194 if (retrieve_request->tag != memory_region->tag) {
2195 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002196 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002197 "%d for handle %#x.\n",
2198 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002199 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002200 goto out;
2201 }
2202
Andrew Walbrana65a1322020-04-06 19:32:32 +01002203 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2204 to_locked.vm->id) {
2205 dlog_verbose(
2206 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002207 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002208 retrieve_request->receivers[0]
2209 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002210 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002211 goto out;
2212 }
2213
2214 if (memory_region->receivers[0].receiver_permissions.receiver !=
2215 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002216 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002217 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2218 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002219 to_locked.vm->id,
2220 memory_region->receivers[0]
2221 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002222 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002223 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002224 goto out;
2225 }
2226
Andrew Walbranca808b12020-05-15 17:22:28 +01002227 if (!share_state->sending_complete) {
2228 dlog_verbose(
2229 "Memory with handle %#x not fully sent, can't "
2230 "retrieve.\n",
2231 handle);
2232 ret = ffa_error(FFA_INVALID_PARAMETERS);
2233 goto out;
2234 }
2235
2236 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002237 dlog_verbose("Memory with handle %#x already retrieved.\n",
2238 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002240 goto out;
2241 }
2242
Andrew Walbrana65a1322020-04-06 19:32:32 +01002243 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2244 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002245 dlog_verbose(
2246 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002247 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002248 retrieve_request->receivers[0]
2249 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002250 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002251 goto out;
2252 }
2253
Federico Recanati85090c42021-12-15 13:17:54 +01002254 if ((retrieve_request->flags &
2255 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0) {
2256 dlog_verbose(
2257 "Retriever specified 'address range alignment hint'"
2258 " not supported.\n");
2259 ret = ffa_error(FFA_INVALID_PARAMETERS);
2260 goto out;
2261 }
2262 if ((retrieve_request->flags &
2263 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2264 dlog_verbose(
2265 "Bits 8-5 must be zero in memory region's flags "
2266 "(address range alignment hint not supported).\n");
2267 ret = ffa_error(FFA_INVALID_PARAMETERS);
2268 goto out;
2269 }
2270
J-Alves84658fc2021-06-17 14:37:32 +01002271 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2272 dlog_verbose(
2273 "Bits 31-10 must be zero in memory region's flags.\n");
2274 ret = ffa_error(FFA_INVALID_PARAMETERS);
2275 goto out;
2276 }
2277
2278 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2279 (retrieve_request->flags &
2280 (FFA_MEMORY_REGION_FLAG_CLEAR |
2281 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2282 dlog_verbose(
2283 "Memory Share operation can't clean after relinquish "
2284 "memory region.\n");
2285 ret = ffa_error(FFA_INVALID_PARAMETERS);
2286 goto out;
2287 }
2288
Andrew Walbrana65a1322020-04-06 19:32:32 +01002289 /*
J-Alves17c069c2021-12-07 16:00:38 +00002290 * If the borrower needs the memory to be cleared before mapping to its
2291 * address space, the sender should have set the flag when calling
2292 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2293 */
2294 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2295 (share_state->memory_region->flags &
2296 FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2297 dlog_verbose(
2298 "Borrower needs memory cleared. Sender needs to set "
2299 "flag for clearing memory.\n");
2300 ret = ffa_error(FFA_DENIED);
2301 goto out;
2302 }
2303
2304 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002305 * Check permissions from sender against permissions requested by
2306 * receiver.
2307 */
2308 /* TODO: Check attributes too. */
2309 sent_permissions =
2310 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002311 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002312 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002313 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002314 requested_permissions =
2315 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002316 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002317 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002318 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002319 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002320
2321 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2322 requested_permissions == FFA_DATA_ACCESS_RO) &&
2323 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2324 dlog_verbose(
2325 "Receiver has RO permissions can not request clear.\n");
2326 ret = ffa_error(FFA_DENIED);
2327 goto out;
2328 }
2329
Andrew Walbrana65a1322020-04-06 19:32:32 +01002330 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2332 case FFA_DATA_ACCESS_RW:
2333 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2334 requested_data_access == FFA_DATA_ACCESS_RW) {
2335 ffa_set_data_access_attr(&permissions,
2336 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002337 break;
2338 }
2339 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002340 case FFA_DATA_ACCESS_RO:
2341 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2342 requested_data_access == FFA_DATA_ACCESS_RO) {
2343 ffa_set_data_access_attr(&permissions,
2344 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002345 break;
2346 }
2347 dlog_verbose(
2348 "Invalid data access requested; sender specified "
2349 "permissions %#x but receiver requested %#x.\n",
2350 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002351 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002352 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002353 case FFA_DATA_ACCESS_RESERVED:
2354 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002355 "checked before this point.");
2356 }
2357 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002358 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2359 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002360 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002361 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2362 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2363 ffa_set_instruction_access_attr(
2364 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002365 break;
2366 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002367 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002368 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002369 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2370 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2371 ffa_set_instruction_access_attr(
2372 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002373 break;
2374 }
2375 dlog_verbose(
2376 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002377 "specified permissions %#x but receiver requested "
2378 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002379 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002380 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002381 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002382 case FFA_INSTRUCTION_ACCESS_RESERVED:
2383 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002384 "be checked before this point.");
2385 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002386 memory_to_attributes = ffa_memory_permissions_to_mode(
2387 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002388 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002389 to_locked, share_state->fragments,
2390 share_state->fragment_constituent_counts,
2391 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002392 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002393 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002394 goto out;
2395 }
2396
2397 /*
2398 * Copy response to RX buffer of caller and deliver the message. This
2399 * must be done before the share_state is (possibly) freed.
2400 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002401 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002402 composite = ffa_memory_region_get_composite(memory_region, 0);
2403 /*
2404 * Constituents which we received in the first fragment should always
2405 * fit in the first fragment we are sending, because the header is the
2406 * same size in both cases and we have a fixed message buffer size. So
2407 * `ffa_retrieved_memory_region_init` should never fail.
2408 */
2409 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002410 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2411 memory_region->sender, memory_region->attributes,
2412 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002413 composite->page_count, composite->constituent_count,
2414 share_state->fragments[0],
2415 share_state->fragment_constituent_counts[0], &total_length,
2416 &fragment_length));
2417 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002418 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002419 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002420 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2421
Andrew Walbranca808b12020-05-15 17:22:28 +01002422 share_state->retrieved_fragment_count[0] = 1;
2423 if (share_state->retrieved_fragment_count[0] ==
2424 share_state->fragment_count) {
2425 ffa_memory_retrieve_complete(share_states, share_state,
2426 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002427 }
2428
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002429 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002430 .arg1 = total_length,
2431 .arg2 = fragment_length};
2432
2433out:
2434 share_states_unlock(&share_states);
2435 dump_share_states();
2436 return ret;
2437}
2438
2439struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2440 ffa_memory_handle_t handle,
2441 uint32_t fragment_offset,
2442 struct mpool *page_pool)
2443{
2444 struct ffa_memory_region *memory_region;
2445 struct share_states_locked share_states;
2446 struct ffa_memory_share_state *share_state;
2447 struct ffa_value ret;
2448 uint32_t fragment_index;
2449 uint32_t retrieved_constituents_count;
2450 uint32_t i;
2451 uint32_t expected_fragment_offset;
2452 uint32_t remaining_constituent_count;
2453 uint32_t fragment_length;
2454
2455 dump_share_states();
2456
2457 share_states = share_states_lock();
2458 if (!get_share_state(share_states, handle, &share_state)) {
2459 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2460 handle);
2461 ret = ffa_error(FFA_INVALID_PARAMETERS);
2462 goto out;
2463 }
2464
2465 memory_region = share_state->memory_region;
2466 CHECK(memory_region != NULL);
2467
2468 if (memory_region->receivers[0].receiver_permissions.receiver !=
2469 to_locked.vm->id) {
2470 dlog_verbose(
2471 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2472 "of handle %#x.\n",
2473 to_locked.vm->id,
2474 memory_region->receivers[0]
2475 .receiver_permissions.receiver,
2476 handle);
2477 ret = ffa_error(FFA_INVALID_PARAMETERS);
2478 goto out;
2479 }
2480
2481 if (!share_state->sending_complete) {
2482 dlog_verbose(
2483 "Memory with handle %#x not fully sent, can't "
2484 "retrieve.\n",
2485 handle);
2486 ret = ffa_error(FFA_INVALID_PARAMETERS);
2487 goto out;
2488 }
2489
2490 if (share_state->retrieved_fragment_count[0] == 0 ||
2491 share_state->retrieved_fragment_count[0] >=
2492 share_state->fragment_count) {
2493 dlog_verbose(
2494 "Retrieval of memory with handle %#x not yet started "
2495 "or already completed (%d/%d fragments retrieved).\n",
2496 handle, share_state->retrieved_fragment_count[0],
2497 share_state->fragment_count);
2498 ret = ffa_error(FFA_INVALID_PARAMETERS);
2499 goto out;
2500 }
2501
2502 fragment_index = share_state->retrieved_fragment_count[0];
2503
2504 /*
2505 * Check that the given fragment offset is correct by counting how many
2506 * constituents were in the fragments previously sent.
2507 */
2508 retrieved_constituents_count = 0;
2509 for (i = 0; i < fragment_index; ++i) {
2510 retrieved_constituents_count +=
2511 share_state->fragment_constituent_counts[i];
2512 }
2513 expected_fragment_offset =
2514 ffa_composite_constituent_offset(memory_region, 0) +
2515 retrieved_constituents_count *
2516 sizeof(struct ffa_memory_region_constituent);
2517 if (fragment_offset != expected_fragment_offset) {
2518 dlog_verbose("Fragment offset was %d but expected %d.\n",
2519 fragment_offset, expected_fragment_offset);
2520 ret = ffa_error(FFA_INVALID_PARAMETERS);
2521 goto out;
2522 }
2523
2524 remaining_constituent_count = ffa_memory_fragment_init(
2525 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2526 share_state->fragments[fragment_index],
2527 share_state->fragment_constituent_counts[fragment_index],
2528 &fragment_length);
2529 CHECK(remaining_constituent_count == 0);
2530 to_locked.vm->mailbox.recv_size = fragment_length;
2531 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2532 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2533 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2534 share_state->retrieved_fragment_count[0]++;
2535 if (share_state->retrieved_fragment_count[0] ==
2536 share_state->fragment_count) {
2537 ffa_memory_retrieve_complete(share_states, share_state,
2538 page_pool);
2539 }
2540
2541 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2542 .arg1 = (uint32_t)handle,
2543 .arg2 = (uint32_t)(handle >> 32),
2544 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002545
2546out:
2547 share_states_unlock(&share_states);
2548 dump_share_states();
2549 return ret;
2550}
2551
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002552struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002554 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002556 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002558 struct ffa_memory_share_state *share_state;
2559 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002561 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002562
Andrew Walbrana65a1322020-04-06 19:32:32 +01002563 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002564 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002565 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002566 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002567 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002568 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002569 }
2570
Andrew Walbrana65a1322020-04-06 19:32:32 +01002571 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572 dlog_verbose(
2573 "VM ID %d in relinquish message doesn't match calling "
2574 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002575 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002576 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002577 }
2578
2579 dump_share_states();
2580
2581 share_states = share_states_lock();
2582 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002583 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002584 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002585 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002586 goto out;
2587 }
2588
Andrew Walbranca808b12020-05-15 17:22:28 +01002589 if (!share_state->sending_complete) {
2590 dlog_verbose(
2591 "Memory with handle %#x not fully sent, can't "
2592 "relinquish.\n",
2593 handle);
2594 ret = ffa_error(FFA_INVALID_PARAMETERS);
2595 goto out;
2596 }
2597
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002598 memory_region = share_state->memory_region;
2599 CHECK(memory_region != NULL);
2600
Andrew Walbrana65a1322020-04-06 19:32:32 +01002601 if (memory_region->receivers[0].receiver_permissions.receiver !=
2602 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603 dlog_verbose(
2604 "VM ID %d tried to relinquish memory region with "
2605 "handle %#x but receiver was %d.\n",
2606 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002607 memory_region->receivers[0]
2608 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002609 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002610 goto out;
2611 }
2612
Andrew Walbranca808b12020-05-15 17:22:28 +01002613 if (share_state->retrieved_fragment_count[0] !=
2614 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002615 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002616 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002617 "relinquish.\n",
2618 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002619 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002620 goto out;
2621 }
2622
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002623 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002624
2625 /*
2626 * Clear is not allowed for memory that was shared, as the original
2627 * sender still has access to the memory.
2628 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002631 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002632 goto out;
2633 }
2634
Andrew Walbranca808b12020-05-15 17:22:28 +01002635 ret = ffa_relinquish_check_update(
2636 from_locked, share_state->fragments,
2637 share_state->fragment_constituent_counts,
2638 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002639
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002640 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002641 /*
2642 * Mark memory handle as not retrieved, so it can be reclaimed
2643 * (or retrieved again).
2644 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002645 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002646 }
2647
2648out:
2649 share_states_unlock(&share_states);
2650 dump_share_states();
2651 return ret;
2652}
2653
2654/**
2655 * Validates that the reclaim transition is allowed for the given handle,
2656 * updates the page table of the reclaiming VM, and frees the internal state
2657 * associated with the handle.
2658 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002659struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002660 ffa_memory_handle_t handle,
2661 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002662 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002663{
2664 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002665 struct ffa_memory_share_state *share_state;
2666 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002667 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002668
2669 dump_share_states();
2670
2671 share_states = share_states_lock();
2672 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002673 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002675 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002676 goto out;
2677 }
2678
2679 memory_region = share_state->memory_region;
2680 CHECK(memory_region != NULL);
2681
2682 if (to_locked.vm->id != memory_region->sender) {
2683 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002684 "VM %#x attempted to reclaim memory handle %#x "
2685 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002687 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688 goto out;
2689 }
2690
Andrew Walbranca808b12020-05-15 17:22:28 +01002691 if (!share_state->sending_complete) {
2692 dlog_verbose(
2693 "Memory with handle %#x not fully sent, can't "
2694 "reclaim.\n",
2695 handle);
2696 ret = ffa_error(FFA_INVALID_PARAMETERS);
2697 goto out;
2698 }
2699
2700 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002701 dlog_verbose(
2702 "Tried to reclaim memory handle %#x that has not been "
2703 "relinquished.\n",
2704 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002705 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002706 goto out;
2707 }
2708
Andrew Walbranca808b12020-05-15 17:22:28 +01002709 ret = ffa_retrieve_check_update(
2710 to_locked, share_state->fragments,
2711 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002712 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002713 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002714
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002715 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002716 share_state_free(share_states, share_state, page_pool);
2717 dlog_verbose("Freed share state after successful reclaim.\n");
2718 }
2719
2720out:
2721 share_states_unlock(&share_states);
2722 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002723}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002724
2725/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002726 * Validates that the reclaim transition is allowed for the memory region with
2727 * the given handle which was previously shared with the TEE, tells the TEE to
2728 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2729 *
2730 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002731 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002732struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002733 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002734 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002735 ffa_memory_region_flags_t flags,
2736 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002737{
Andrew Walbranca808b12020-05-15 17:22:28 +01002738 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2739 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2740 struct ffa_value tee_ret;
2741 uint32_t length;
2742 uint32_t fragment_length;
2743 uint32_t fragment_offset;
2744 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002745 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002746 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2747
2748 CHECK(request_length <= HF_MAILBOX_SIZE);
2749 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2750
2751 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002752 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002753 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2754 .arg1 = request_length,
2755 .arg2 = request_length});
2756 if (tee_ret.func == FFA_ERROR_32) {
2757 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2758 return tee_ret;
2759 }
2760 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2761 dlog_verbose(
2762 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2763 tee_ret.func);
2764 return ffa_error(FFA_INVALID_PARAMETERS);
2765 }
2766
2767 length = tee_ret.arg1;
2768 fragment_length = tee_ret.arg2;
2769
2770 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2771 length > sizeof(tee_retrieve_buffer)) {
2772 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2773 fragment_length, length, HF_MAILBOX_SIZE,
2774 sizeof(tee_retrieve_buffer));
2775 return ffa_error(FFA_INVALID_PARAMETERS);
2776 }
2777
2778 /*
2779 * Copy the first fragment of the memory region descriptor to an
2780 * internal buffer.
2781 */
2782 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2783 from_locked.vm->mailbox.send, fragment_length);
2784
2785 /* Fetch the remaining fragments into the same buffer. */
2786 fragment_offset = fragment_length;
2787 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002788 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002789 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2790 .arg1 = (uint32_t)handle,
2791 .arg2 = (uint32_t)(handle >> 32),
2792 .arg3 = fragment_offset});
2793 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2794 dlog_verbose(
2795 "Got %#x (%d) from TEE in response to "
2796 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2797 tee_ret.func, tee_ret.arg2);
2798 return tee_ret;
2799 }
2800 if (ffa_frag_handle(tee_ret) != handle) {
2801 dlog_verbose(
2802 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2803 "in response to FFA_MEM_FRAG_RX for handle "
2804 "%#x.\n",
2805 ffa_frag_handle(tee_ret), handle);
2806 return ffa_error(FFA_INVALID_PARAMETERS);
2807 }
2808 if (ffa_frag_sender(tee_ret) != 0) {
2809 dlog_verbose(
2810 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2811 "(expected 0).\n",
2812 ffa_frag_sender(tee_ret));
2813 return ffa_error(FFA_INVALID_PARAMETERS);
2814 }
2815 fragment_length = tee_ret.arg3;
2816 if (fragment_length > HF_MAILBOX_SIZE ||
2817 fragment_offset + fragment_length > length) {
2818 dlog_verbose(
2819 "Invalid fragment length %d at offset %d (max "
2820 "%d).\n",
2821 fragment_length, fragment_offset,
2822 HF_MAILBOX_SIZE);
2823 return ffa_error(FFA_INVALID_PARAMETERS);
2824 }
2825 memcpy_s(tee_retrieve_buffer + fragment_offset,
2826 sizeof(tee_retrieve_buffer) - fragment_offset,
2827 from_locked.vm->mailbox.send, fragment_length);
2828
2829 fragment_offset += fragment_length;
2830 }
2831
2832 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002833
2834 if (memory_region->receiver_count != 1) {
2835 /* Only one receiver supported by Hafnium for now. */
2836 dlog_verbose(
2837 "Multiple recipients not supported (got %d, expected "
2838 "1).\n",
2839 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002840 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002841 }
2842
2843 if (memory_region->handle != handle) {
2844 dlog_verbose(
2845 "Got memory region handle %#x from TEE but requested "
2846 "handle %#x.\n",
2847 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002848 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002849 }
2850
2851 /* The original sender must match the caller. */
2852 if (to_locked.vm->id != memory_region->sender) {
2853 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002854 "VM %#x attempted to reclaim memory handle %#x "
2855 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002856 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002857 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002858 }
2859
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002860 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002861
2862 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002863 * Validate that the reclaim transition is allowed for the given memory
2864 * region, forward the request to the TEE and then map the memory back
2865 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002866 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002867 return ffa_tee_reclaim_check_update(
2868 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002869 composite->constituent_count, memory_to_attributes,
2870 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002871}