blob: 5826cb2fdd4bf00207d3ed6c33ce33fe14551a42 [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
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000016#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010017#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000020#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000022#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000024/**
25 * The maximum number of memory sharing handles which may be active at once. A
26 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
27 * or LEND handle is active from when it is sent to when it is reclaimed.
28 */
29#define MAX_MEM_SHARES 100
30
Andrew Walbranca808b12020-05-15 17:22:28 +010031/**
32 * The maximum number of fragments into which a memory sharing message may be
33 * broken.
34 */
35#define MAX_FRAGMENTS 20
36
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
38 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000039 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
41 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000042 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010044 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
46 "struct ffa_memory_access must be a multiple of 16 bytes long.");
47static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
48 "struct ffa_memory_region must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
50 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010053struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 /**
55 * The memory region being shared, or NULL if this share state is
56 * unallocated.
57 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010058 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059
Andrew Walbranca808b12020-05-15 17:22:28 +010060 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
61
62 /** The number of constituents in each fragment. */
63 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
64
65 /**
66 * The number of valid elements in the `fragments` and
67 * `fragment_constituent_counts` arrays.
68 */
69 uint32_t fragment_count;
70
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000071 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 * The FF-A function used for sharing the memory. Must be one of
73 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 * share state is allocated, or 0.
75 */
76 uint32_t share_func;
77
78 /**
J-Alves2a0d2882020-10-29 14:49:50 +000079 * The sender's original mode before invoking the FF-A function for
80 * sharing the memory.
81 * This is used to reset the original configuration when sender invokes
82 * FFA_MEM_RECLAIM_32.
83 */
84 uint32_t sender_orig_mode;
85
86 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010087 * True if all the fragments of this sharing request have been sent and
88 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089 */
Andrew Walbranca808b12020-05-15 17:22:28 +010090 bool sending_complete;
91
92 /**
93 * How many fragments of the memory region each recipient has retrieved
94 * so far. The order of this array matches the order of the endpoint
95 * memory access descriptors in the memory region descriptor. Any
96 * entries beyond the receiver_count will always be 0.
97 */
98 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +000099};
100
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000101/**
102 * Encapsulates the set of share states while the `share_states_lock` is held.
103 */
104struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100105 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000106};
107
108/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100109 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 * by this lock.
111 */
112static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100113static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000114
115/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100116 * Buffer for retrieving memory region information from the TEE for when a
117 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
118 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000119 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100120alignas(PAGE_SIZE) static uint8_t
121 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
122
123/**
J-Alves917d2f22020-10-30 18:39:30 +0000124 * Extracts the index from a memory handle allocated by Hafnium's current world.
125 */
126uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
127{
128 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
129}
130
131/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100132 * Initialises the next available `struct ffa_memory_share_state` and sets
133 * `share_state_ret` to a pointer to it. If `handle` is
134 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
135 * uses the provided handle which is assumed to be globally unique.
136 *
137 * Returns true on success or false if none are available.
138 */
139static bool allocate_share_state(
140 struct share_states_locked share_states, uint32_t share_func,
141 struct ffa_memory_region *memory_region, uint32_t fragment_length,
142 ffa_memory_handle_t handle,
143 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100145 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000147 assert(share_states.share_states != NULL);
148 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100151 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100154 &share_states.share_states[i];
155 struct ffa_composite_memory_region *composite =
156 ffa_memory_region_get_composite(memory_region,
157 0);
158
159 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000160 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000163 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000165 allocated_state->share_func = share_func;
166 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 allocated_state->fragment_count = 1;
168 allocated_state->fragments[0] = composite->constituents;
169 allocated_state->fragment_constituent_counts[0] =
170 (fragment_length -
171 ffa_composite_constituent_offset(memory_region,
172 0)) /
173 sizeof(struct ffa_memory_region_constituent);
174 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000175 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100176 allocated_state->retrieved_fragment_count[j] =
177 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000178 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 if (share_state_ret != NULL) {
180 *share_state_ret = allocated_state;
181 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000182 return true;
183 }
184 }
185
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000186 return false;
187}
188
189/** Locks the share states lock. */
190struct share_states_locked share_states_lock(void)
191{
192 sl_lock(&share_states_lock_instance);
193
194 return (struct share_states_locked){.share_states = share_states};
195}
196
197/** Unlocks the share states lock. */
198static void share_states_unlock(struct share_states_locked *share_states)
199{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000200 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000201 share_states->share_states = NULL;
202 sl_unlock(&share_states_lock_instance);
203}
204
205/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100206 * If the given handle is a valid handle for an allocated share state then
207 * initialises `share_state_ret` to point to the share state and returns true.
208 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209 */
210static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100211 ffa_memory_handle_t handle,
212 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000213{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100214 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000215 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000217 assert(share_states.share_states != NULL);
218 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100219
220 /*
221 * First look for a share_state allocated by us, in which case the
222 * handle is based on the index.
223 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200224 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000225 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100226 if (index < MAX_MEM_SHARES) {
227 share_state = &share_states.share_states[index];
228 if (share_state->share_func != 0) {
229 *share_state_ret = share_state;
230 return true;
231 }
232 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000233 }
234
Andrew Walbranca808b12020-05-15 17:22:28 +0100235 /* Fall back to a linear scan. */
236 for (index = 0; index < MAX_MEM_SHARES; ++index) {
237 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000238 if (share_state->memory_region != NULL &&
239 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100240 share_state->share_func != 0) {
241 *share_state_ret = share_state;
242 return true;
243 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000244 }
245
Andrew Walbranca808b12020-05-15 17:22:28 +0100246 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247}
248
249/** Marks a share state as unallocated. */
250static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100251 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000252 struct mpool *page_pool)
253{
Andrew Walbranca808b12020-05-15 17:22:28 +0100254 uint32_t i;
255
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000256 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000257 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100258 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000259 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260 /*
261 * First fragment is part of the same page as the `memory_region`, so it
262 * doesn't need to be freed separately.
263 */
264 share_state->fragments[0] = NULL;
265 share_state->fragment_constituent_counts[0] = 0;
266 for (i = 1; i < share_state->fragment_count; ++i) {
267 mpool_free(page_pool, share_state->fragments[i]);
268 share_state->fragments[i] = NULL;
269 share_state->fragment_constituent_counts[i] = 0;
270 }
271 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000272 share_state->memory_region = NULL;
273}
274
Andrew Walbranca808b12020-05-15 17:22:28 +0100275/** Checks whether the given share state has been fully sent. */
276static bool share_state_sending_complete(
277 struct share_states_locked share_states,
278 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000279{
Andrew Walbranca808b12020-05-15 17:22:28 +0100280 struct ffa_composite_memory_region *composite;
281 uint32_t expected_constituent_count;
282 uint32_t fragment_constituent_count_total = 0;
283 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000284
Andrew Walbranca808b12020-05-15 17:22:28 +0100285 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000286 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100287
288 /*
289 * Share state must already be valid, or it's not possible to get hold
290 * of it.
291 */
292 CHECK(share_state->memory_region != NULL &&
293 share_state->share_func != 0);
294
295 composite =
296 ffa_memory_region_get_composite(share_state->memory_region, 0);
297 expected_constituent_count = composite->constituent_count;
298 for (i = 0; i < share_state->fragment_count; ++i) {
299 fragment_constituent_count_total +=
300 share_state->fragment_constituent_counts[i];
301 }
302 dlog_verbose(
303 "Checking completion: constituent count %d/%d from %d "
304 "fragments.\n",
305 fragment_constituent_count_total, expected_constituent_count,
306 share_state->fragment_count);
307
308 return fragment_constituent_count_total == expected_constituent_count;
309}
310
311/**
312 * Calculates the offset of the next fragment expected for the given share
313 * state.
314 */
315static uint32_t share_state_next_fragment_offset(
316 struct share_states_locked share_states,
317 struct ffa_memory_share_state *share_state)
318{
319 uint32_t next_fragment_offset;
320 uint32_t i;
321
322 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000323 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100324
325 next_fragment_offset =
326 ffa_composite_constituent_offset(share_state->memory_region, 0);
327 for (i = 0; i < share_state->fragment_count; ++i) {
328 next_fragment_offset +=
329 share_state->fragment_constituent_counts[i] *
330 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000331 }
332
Andrew Walbranca808b12020-05-15 17:22:28 +0100333 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334}
335
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337{
338 uint32_t i;
339
340 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
341 return;
342 }
343
Olivier Deprez935e1b12020-12-22 18:01:29 +0100344 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100345 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100346 "recipients [",
347 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100348 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 memory_region->receiver_count);
350 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 if (i != 0) {
352 dlog(", ");
353 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100354 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100355 memory_region->receivers[i].receiver_permissions.receiver,
356 memory_region->receivers[i]
357 .receiver_permissions.permissions,
358 memory_region->receivers[i]
359 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000360 }
361 dlog("]");
362}
363
364static void dump_share_states(void)
365{
366 uint32_t i;
367
368 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
369 return;
370 }
371
372 dlog("Current share states:\n");
373 sl_lock(&share_states_lock_instance);
374 for (i = 0; i < MAX_MEM_SHARES; ++i) {
375 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 dlog("SHARE");
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000381 dlog("LEND");
382 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 dlog("DONATE");
385 break;
386 default:
387 dlog("invalid share_func %#x",
388 share_states[i].share_func);
389 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100390 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000391 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100392 if (share_states[i].sending_complete) {
393 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000396 }
J-Alves2a0d2882020-10-29 14:49:50 +0000397 dlog(" with %d fragments, %d retrieved, "
398 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000400 share_states[i].retrieved_fragment_count[0],
401 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000402 }
403 }
404 sl_unlock(&share_states_lock_instance);
405}
406
Andrew Walbran475c1452020-02-07 13:22:22 +0000407/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100408static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100409 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000410{
411 uint32_t mode = 0;
412
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100413 switch (ffa_get_data_access_attr(permissions)) {
414 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000415 mode = MM_MODE_R;
416 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000418 mode = MM_MODE_R | MM_MODE_W;
419 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100420 case FFA_DATA_ACCESS_NOT_SPECIFIED:
421 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
422 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100423 case FFA_DATA_ACCESS_RESERVED:
424 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100425 }
426
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 switch (ffa_get_instruction_access_attr(permissions)) {
428 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000429 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100431 mode |= MM_MODE_X;
432 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100433 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
434 mode |= (default_mode & MM_MODE_X);
435 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436 case FFA_INSTRUCTION_ACCESS_RESERVED:
437 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000438 }
439
440 return mode;
441}
442
Jose Marinho75509b42019-04-09 09:34:59 +0100443/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000444 * Get the current mode in the stage-2 page table of the given vm of all the
445 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100446 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100447 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000449 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 struct ffa_memory_region_constituent **fragments,
451 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100452{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100453 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100454 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100455
Andrew Walbranca808b12020-05-15 17:22:28 +0100456 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100457 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000458 * Fail if there are no constituents. Otherwise we would get an
459 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100461 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100462 }
463
Andrew Walbranca808b12020-05-15 17:22:28 +0100464 for (i = 0; i < fragment_count; ++i) {
465 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
466 ipaddr_t begin = ipa_init(fragments[i][j].address);
467 size_t size = fragments[i][j].page_count * PAGE_SIZE;
468 ipaddr_t end = ipa_add(begin, size);
469 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100470
Andrew Walbranca808b12020-05-15 17:22:28 +0100471 /* Fail if addresses are not page-aligned. */
472 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
473 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
474 return ffa_error(FFA_INVALID_PARAMETERS);
475 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100476
Andrew Walbranca808b12020-05-15 17:22:28 +0100477 /*
478 * Ensure that this constituent memory range is all
479 * mapped with the same mode.
480 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800481 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100482 return ffa_error(FFA_DENIED);
483 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100484
Andrew Walbranca808b12020-05-15 17:22:28 +0100485 /*
486 * Ensure that all constituents are mapped with the same
487 * mode.
488 */
489 if (i == 0) {
490 *orig_mode = current_mode;
491 } else if (current_mode != *orig_mode) {
492 dlog_verbose(
493 "Expected mode %#x but was %#x for %d "
494 "pages at %#x.\n",
495 *orig_mode, current_mode,
496 fragments[i][j].page_count,
497 ipa_addr(begin));
498 return ffa_error(FFA_DENIED);
499 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100500 }
Jose Marinho75509b42019-04-09 09:34:59 +0100501 }
502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000504}
505
506/**
507 * Verify that all pages have the same mode, that the starting mode
508 * constitutes a valid state and obtain the next mode to apply
509 * to the sending VM.
510 *
511 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512 * 1) FFA_DENIED if a state transition was not found;
513 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100514 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100516 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100517 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
518 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000519 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100521 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100522 struct ffa_memory_access *receivers, uint32_t receivers_count,
523 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100524 struct ffa_memory_region_constituent **fragments,
525 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
526 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000527{
528 const uint32_t state_mask =
529 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100530 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000531
Andrew Walbranca808b12020-05-15 17:22:28 +0100532 ret = constituents_get_mode(from, orig_from_mode, fragments,
533 fragment_constituent_counts,
534 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100536 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100537 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100538 }
539
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000540 /* Ensure the address range is normal memory and not a device. */
541 if (*orig_from_mode & MM_MODE_D) {
542 dlog_verbose("Can't share device memory (mode is %#x).\n",
543 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 }
546
547 /*
548 * Ensure the sender is the owner and has exclusive access to the
549 * memory.
550 */
551 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100552 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100553 }
554
J-Alves363f5722022-04-25 17:37:37 +0100555 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100556
J-Alves363f5722022-04-25 17:37:37 +0100557 for (uint32_t i = 0U; i < receivers_count; i++) {
558 ffa_memory_access_permissions_t permissions =
559 receivers[i].receiver_permissions.permissions;
560 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
561 permissions, *orig_from_mode);
562
563 if ((*orig_from_mode & required_from_mode) !=
564 required_from_mode) {
565 dlog_verbose(
566 "Sender tried to send memory with permissions "
567 "which "
568 "required mode %#x but only had %#x itself.\n",
569 required_from_mode, *orig_from_mode);
570 return ffa_error(FFA_DENIED);
571 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572 }
573
574 /* Find the appropriate new mode. */
575 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000576 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100579 break;
580
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100583 break;
584
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000586 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100587 break;
588
Jose Marinho75509b42019-04-09 09:34:59 +0100589 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100591 }
592
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100593 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000594}
595
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100596static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000597 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100598 struct ffa_memory_region_constituent **fragments,
599 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
600 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601{
602 const uint32_t state_mask =
603 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
604 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606
Andrew Walbranca808b12020-05-15 17:22:28 +0100607 ret = constituents_get_mode(from, orig_from_mode, fragments,
608 fragment_constituent_counts,
609 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100611 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000612 }
613
614 /* Ensure the address range is normal memory and not a device. */
615 if (*orig_from_mode & MM_MODE_D) {
616 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
617 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000619 }
620
621 /*
622 * Ensure the relinquishing VM is not the owner but has access to the
623 * memory.
624 */
625 orig_from_state = *orig_from_mode & state_mask;
626 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
627 dlog_verbose(
628 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100629 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000630 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632 }
633
634 /* Find the appropriate new mode. */
635 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100637 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000638}
639
640/**
641 * Verify that all pages have the same mode, that the starting mode
642 * constitutes a valid state and obtain the next mode to apply
643 * to the retrieving VM.
644 *
645 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 * 1) FFA_DENIED if a state transition was not found;
647 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100648 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100649 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100650 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
652 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100654static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000655 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100656 struct ffa_memory_region_constituent **fragments,
657 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
658 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000659{
660 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662
Andrew Walbranca808b12020-05-15 17:22:28 +0100663 ret = constituents_get_mode(to, &orig_to_mode, fragments,
664 fragment_constituent_counts,
665 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100667 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100668 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669 }
670
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000672 /*
673 * If the original ffa memory send call has been processed
674 * successfully, it is expected the orig_to_mode would overlay
675 * with `state_mask`, as a result of the function
676 * `ffa_send_check_transition`.
677 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100678 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
679 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000680 } else {
681 /*
682 * Ensure the retriever has the expected state. We don't care
683 * about the MM_MODE_SHARED bit; either with or without it set
684 * are both valid representations of the !O-NA state.
685 */
686 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
687 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000689 }
690 }
691
692 /* Find the appropriate new mode. */
693 *to_mode = memory_to_attributes;
694 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100695 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000696 *to_mode |= 0;
697 break;
698
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100699 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000700 *to_mode |= MM_MODE_UNOWNED;
701 break;
702
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000704 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
705 break;
706
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000708 *to_mode |= 0;
709 break;
710
711 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100712 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000714 }
715
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100717}
Jose Marinho09b1db82019-08-08 09:16:59 +0100718
719/**
720 * Updates a VM's page table such that the given set of physical address ranges
721 * are mapped in the address space at the corresponding address ranges, in the
722 * mode provided.
723 *
724 * If commit is false, the page tables will be allocated from the mpool but no
725 * mappings will actually be updated. This function must always be called first
726 * with commit false to check that it will succeed before calling with commit
727 * true, to avoid leaving the page table in a half-updated state. To make a
728 * series of changes atomically you can call them all with commit false before
729 * calling them all with commit true.
730 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700731 * vm_ptable_defrag should always be called after a series of page table
732 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100733 *
734 * Returns true on success, or false if the update failed and no changes were
735 * made to memory mappings.
736 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100737static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000738 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100739 struct ffa_memory_region_constituent **fragments,
740 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100741 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100742{
Andrew Walbranca808b12020-05-15 17:22:28 +0100743 uint32_t i;
744 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100745
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700746 if (vm_locked.vm->el0_partition) {
747 mode |= MM_MODE_USER | MM_MODE_NG;
748 }
749
Andrew Walbranca808b12020-05-15 17:22:28 +0100750 /* Iterate over the memory region constituents within each fragment. */
751 for (i = 0; i < fragment_count; ++i) {
752 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
753 size_t size = fragments[i][j].page_count * PAGE_SIZE;
754 paddr_t pa_begin =
755 pa_from_ipa(ipa_init(fragments[i][j].address));
756 paddr_t pa_end = pa_add(pa_begin, size);
Federico Recanati4fd065d2021-12-13 20:06:23 +0100757 uint32_t pa_range = arch_mm_get_pa_range();
758
759 /*
760 * Ensure the requested region falls into system's PA
761 * range.
762 */
763 if (((pa_addr(pa_begin) >> pa_range) > 0) ||
764 ((pa_addr(pa_end) >> pa_range) > 0)) {
765 dlog_error("Region is outside of PA Range\n");
766 return false;
767 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100768
769 if (commit) {
770 vm_identity_commit(vm_locked, pa_begin, pa_end,
771 mode, ppool, NULL);
772 } else if (!vm_identity_prepare(vm_locked, pa_begin,
773 pa_end, mode, ppool)) {
774 return false;
775 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100776 }
777 }
778
779 return true;
780}
781
782/**
783 * Clears a region of physical memory by overwriting it with zeros. The data is
784 * flushed from the cache so the memory has been cleared across the system.
785 */
J-Alves7db32002021-12-14 14:44:50 +0000786static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
787 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100788{
789 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000790 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100791 * global mapping of the whole range. Such an approach will limit
792 * the changes to stage-1 tables and will allow only local
793 * invalidation.
794 */
795 bool ret;
796 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000797 void *ptr = mm_identity_map(stage1_locked, begin, end,
798 MM_MODE_W | (extra_mode_attributes &
799 plat_ffa_other_world_mode()),
800 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100801 size_t size = pa_difference(begin, end);
802
803 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100804 goto fail;
805 }
806
807 memset_s(ptr, size, 0, size);
808 arch_mm_flush_dcache(ptr, size);
809 mm_unmap(stage1_locked, begin, end, ppool);
810
811 ret = true;
812 goto out;
813
814fail:
815 ret = false;
816
817out:
818 mm_unlock_stage1(&stage1_locked);
819
820 return ret;
821}
822
823/**
824 * Clears a region of physical memory by overwriting it with zeros. The data is
825 * flushed from the cache so the memory has been cleared across the system.
826 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100827static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000828 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100829 struct ffa_memory_region_constituent **fragments,
830 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
831 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100832{
833 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100834 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100835 bool ret = false;
836
837 /*
838 * Create a local pool so any freed memory can't be used by another
839 * thread. This is to ensure each constituent that is mapped can be
840 * unmapped again afterwards.
841 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000842 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100843
Andrew Walbranca808b12020-05-15 17:22:28 +0100844 /* Iterate over the memory region constituents within each fragment. */
845 for (i = 0; i < fragment_count; ++i) {
846 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100847
Andrew Walbranca808b12020-05-15 17:22:28 +0100848 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
849 size_t size = fragments[i][j].page_count * PAGE_SIZE;
850 paddr_t begin =
851 pa_from_ipa(ipa_init(fragments[i][j].address));
852 paddr_t end = pa_add(begin, size);
853
J-Alves7db32002021-12-14 14:44:50 +0000854 if (!clear_memory(begin, end, &local_page_pool,
855 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100856 /*
857 * api_clear_memory will defrag on failure, so
858 * no need to do it here.
859 */
860 goto out;
861 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100862 }
863 }
864
Jose Marinho09b1db82019-08-08 09:16:59 +0100865 ret = true;
866
867out:
868 mpool_fini(&local_page_pool);
869 return ret;
870}
871
872/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000873 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100874 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000875 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100876 *
877 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000878 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100880 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100881 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
882 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100884 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100886 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100887static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100889 struct ffa_memory_region_constituent **fragments,
890 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100891 uint32_t share_func, struct ffa_memory_access *receivers,
892 uint32_t receivers_count, struct mpool *page_pool, bool clear,
893 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100894{
Andrew Walbranca808b12020-05-15 17:22:28 +0100895 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 uint32_t orig_from_mode;
897 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100898 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100899 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100900
901 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100902 * Make sure constituents are properly aligned to a 64-bit boundary. If
903 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100904 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100905 for (i = 0; i < fragment_count; ++i) {
906 if (!is_aligned(fragments[i], 8)) {
907 dlog_verbose("Constituents not aligned.\n");
908 return ffa_error(FFA_INVALID_PARAMETERS);
909 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 }
911
912 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000913 * Check if the state transition is lawful for the sender, ensure that
914 * all constituents of a memory region being shared are at the same
915 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 */
J-Alves363f5722022-04-25 17:37:37 +0100917 ret = ffa_send_check_transition(from_locked, share_func, receivers,
918 receivers_count, &orig_from_mode,
919 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100920 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100922 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100923 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100924 }
925
Andrew Walbran37c574e2020-06-03 11:45:46 +0100926 if (orig_from_mode_ret != NULL) {
927 *orig_from_mode_ret = orig_from_mode;
928 }
929
Jose Marinho09b1db82019-08-08 09:16:59 +0100930 /*
931 * Create a local pool so any freed memory can't be used by another
932 * thread. This is to ensure the original mapping can be restored if the
933 * clear fails.
934 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000935 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100936
937 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000938 * First reserve all required memory for the new page table entries
939 * without committing, to make sure the entire operation will succeed
940 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100941 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 if (!ffa_region_group_identity_map(
943 from_locked, fragments, fragment_constituent_counts,
944 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100945 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100947 goto out;
948 }
949
950 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 * Update the mapping for the sender. This won't allocate because the
952 * transaction was already prepared above, but may free pages in the
953 * case that a whole block is being unmapped that was previously
954 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100955 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100956 CHECK(ffa_region_group_identity_map(
957 from_locked, fragments, fragment_constituent_counts,
958 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100959
960 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000961 if (clear &&
962 !ffa_clear_memory_constituents(
963 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
964 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100965 /*
966 * On failure, roll back by returning memory to the sender. This
967 * may allocate pages which were previously freed into
968 * `local_page_pool` by the call above, but will never allocate
969 * more pages than that so can never fail.
970 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100971 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100972 from_locked, fragments, fragment_constituent_counts,
973 fragment_count, orig_from_mode, &local_page_pool,
974 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100975
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100976 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100977 goto out;
978 }
979
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100980 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000981
982out:
983 mpool_fini(&local_page_pool);
984
985 /*
986 * Tidy up the page table by reclaiming failed mappings (if there was an
987 * error) or merging entries into blocks where possible (on success).
988 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700989 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000990
991 return ret;
992}
993
994/**
995 * Validates and maps memory shared from one VM to another.
996 *
997 * This function requires the calling context to hold the <to> lock.
998 *
999 * Returns:
1000 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001002 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001003 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001005 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001006 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001007static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001008 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001009 struct ffa_memory_region_constituent **fragments,
1010 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1011 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1012 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013{
Andrew Walbranca808b12020-05-15 17:22:28 +01001014 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015 uint32_t to_mode;
1016 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018
1019 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001020 * Make sure constituents are properly aligned to a 64-bit boundary. If
1021 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001023 for (i = 0; i < fragment_count; ++i) {
1024 if (!is_aligned(fragments[i], 8)) {
1025 return ffa_error(FFA_INVALID_PARAMETERS);
1026 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027 }
1028
1029 /*
1030 * Check if the state transition is lawful for the recipient, and ensure
1031 * that all constituents of the memory region being retrieved are at the
1032 * same state.
1033 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001034 ret = ffa_retrieve_check_transition(
1035 to_locked, share_func, fragments, fragment_constituent_counts,
1036 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001037 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001038 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001039 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001040 }
1041
1042 /*
1043 * Create a local pool so any freed memory can't be used by another
1044 * thread. This is to ensure the original mapping can be restored if the
1045 * clear fails.
1046 */
1047 mpool_init_with_fallback(&local_page_pool, page_pool);
1048
1049 /*
1050 * First reserve all required memory for the new page table entries in
1051 * the recipient page tables without committing, to make sure the entire
1052 * operation will succeed without exhausting the page pool.
1053 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001054 if (!ffa_region_group_identity_map(
1055 to_locked, fragments, fragment_constituent_counts,
1056 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001057 /* TODO: partial defrag of failed range. */
1058 dlog_verbose(
1059 "Insufficient memory to update recipient page "
1060 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001061 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001062 goto out;
1063 }
1064
1065 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001066 if (clear &&
1067 !ffa_clear_memory_constituents(
1068 plat_ffa_owner_world_mode(from_id), fragments,
1069 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001071 goto out;
1072 }
1073
Jose Marinho09b1db82019-08-08 09:16:59 +01001074 /*
1075 * Complete the transfer by mapping the memory into the recipient. This
1076 * won't allocate because the transaction was already prepared above, so
1077 * it doesn't need to use the `local_page_pool`.
1078 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001079 CHECK(ffa_region_group_identity_map(
1080 to_locked, fragments, fragment_constituent_counts,
1081 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001082
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001084
1085out:
1086 mpool_fini(&local_page_pool);
1087
1088 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001089 * Tidy up the page table by reclaiming failed mappings (if there was an
1090 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001091 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001092 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001093
1094 return ret;
1095}
1096
Andrew Walbran290b0c92020-02-03 16:37:14 +00001097/**
1098 * Reclaims the given memory from the TEE. To do this space is first reserved in
1099 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1100 * then (if that is successful) the memory is mapped back into the <to> VM's
1101 * page table.
1102 *
1103 * This function requires the calling context to hold the <to> lock.
1104 *
1105 * Returns:
1106 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001107 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001108 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001109 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001110 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001112 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001113static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001114 struct vm_locked to_locked, ffa_memory_handle_t handle,
1115 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001116 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1117 struct mpool *page_pool)
1118{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001119 uint32_t to_mode;
1120 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001121 struct ffa_value ret;
1122 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001123
1124 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001125 * Make sure constituents are properly aligned to a 64-bit boundary. If
1126 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001127 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001128 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001129 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001130 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001131 }
1132
1133 /*
1134 * Check if the state transition is lawful for the recipient, and ensure
1135 * that all constituents of the memory region being retrieved are at the
1136 * same state.
1137 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001139 &constituents, &constituent_count,
1140 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001141 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001142 dlog_verbose("Invalid transition.\n");
1143 return ret;
1144 }
1145
1146 /*
1147 * Create a local pool so any freed memory can't be used by another
1148 * thread. This is to ensure the original mapping can be restored if the
1149 * clear fails.
1150 */
1151 mpool_init_with_fallback(&local_page_pool, page_pool);
1152
1153 /*
1154 * First reserve all required memory for the new page table entries in
1155 * the recipient page tables without committing, to make sure the entire
1156 * operation will succeed without exhausting the page pool.
1157 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001158 if (!ffa_region_group_identity_map(to_locked, &constituents,
1159 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001161 /* TODO: partial defrag of failed range. */
1162 dlog_verbose(
1163 "Insufficient memory to update recipient page "
1164 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001165 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001166 goto out;
1167 }
1168
1169 /*
1170 * Forward the request to the TEE and see what happens.
1171 */
1172 tee_flags = 0;
1173 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001174 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001175 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001176 ret = arch_other_world_call(
1177 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1178 .arg1 = (uint32_t)handle,
1179 .arg2 = (uint32_t)(handle >> 32),
1180 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001181
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001182 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001183 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001184 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1185 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001186 ret.func, ret.arg2);
1187 goto out;
1188 }
1189
1190 /*
1191 * The TEE was happy with it, so complete the reclaim by mapping the
1192 * memory into the recipient. This won't allocate because the
1193 * transaction was already prepared above, so it doesn't need to use the
1194 * `local_page_pool`.
1195 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001196 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1197 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001198 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001199
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001200 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001201
1202out:
1203 mpool_fini(&local_page_pool);
1204
1205 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001206 * Tidy up the page table by reclaiming failed mappings (if there was an
1207 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001208 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001209 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001210
1211 return ret;
1212}
1213
Andrew Walbran996d1d12020-05-27 14:08:43 +01001214static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001215 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001216 struct ffa_memory_region_constituent **fragments,
1217 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1218 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001219{
1220 uint32_t orig_from_mode;
1221 uint32_t from_mode;
1222 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001223 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001224
Andrew Walbranca808b12020-05-15 17:22:28 +01001225 ret = ffa_relinquish_check_transition(
1226 from_locked, &orig_from_mode, fragments,
1227 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001228 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001230 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001231 }
1232
1233 /*
1234 * Create a local pool so any freed memory can't be used by another
1235 * thread. This is to ensure the original mapping can be restored if the
1236 * clear fails.
1237 */
1238 mpool_init_with_fallback(&local_page_pool, page_pool);
1239
1240 /*
1241 * First reserve all required memory for the new page table entries
1242 * without committing, to make sure the entire operation will succeed
1243 * without exhausting the page pool.
1244 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001245 if (!ffa_region_group_identity_map(
1246 from_locked, fragments, fragment_constituent_counts,
1247 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001248 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001249 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001250 goto out;
1251 }
1252
1253 /*
1254 * Update the mapping for the sender. This won't allocate because the
1255 * transaction was already prepared above, but may free pages in the
1256 * case that a whole block is being unmapped that was previously
1257 * partially mapped.
1258 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001259 CHECK(ffa_region_group_identity_map(
1260 from_locked, fragments, fragment_constituent_counts,
1261 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001262
1263 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001264 if (clear &&
1265 !ffa_clear_memory_constituents(
1266 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1267 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001268 /*
1269 * On failure, roll back by returning memory to the sender. This
1270 * may allocate pages which were previously freed into
1271 * `local_page_pool` by the call above, but will never allocate
1272 * more pages than that so can never fail.
1273 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001274 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001275 from_locked, fragments, fragment_constituent_counts,
1276 fragment_count, orig_from_mode, &local_page_pool,
1277 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001278
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001279 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001280 goto out;
1281 }
1282
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001283 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001284
1285out:
1286 mpool_fini(&local_page_pool);
1287
1288 /*
1289 * Tidy up the page table by reclaiming failed mappings (if there was an
1290 * error) or merging entries into blocks where possible (on success).
1291 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001292 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001293
1294 return ret;
1295}
1296
1297/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001298 * Complete a memory sending operation by checking that it is valid, updating
1299 * the sender page table, and then either marking the share state as having
1300 * completed sending (on success) or freeing it (on failure).
1301 *
1302 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1303 */
1304static struct ffa_value ffa_memory_send_complete(
1305 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001306 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1307 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001308{
1309 struct ffa_memory_region *memory_region = share_state->memory_region;
1310 struct ffa_value ret;
1311
1312 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001313 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001314
1315 /* Check that state is valid in sender page table and update. */
1316 ret = ffa_send_check_update(
1317 from_locked, share_state->fragments,
1318 share_state->fragment_constituent_counts,
1319 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001320 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001321 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1322 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001323 if (ret.func != FFA_SUCCESS_32) {
1324 /*
1325 * Free share state, it failed to send so it can't be retrieved.
1326 */
1327 dlog_verbose("Complete failed, freeing share state.\n");
1328 share_state_free(share_states, share_state, page_pool);
1329 return ret;
1330 }
1331
1332 share_state->sending_complete = true;
1333 dlog_verbose("Marked sending complete.\n");
1334
J-Alvesee68c542020-10-29 17:48:20 +00001335 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001336}
1337
1338/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001339 * Check that the memory attributes match Hafnium expectations:
1340 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1341 * Write-Allocate Cacheable.
1342 */
1343static struct ffa_value ffa_memory_attributes_validate(
1344 ffa_memory_access_permissions_t attributes)
1345{
1346 enum ffa_memory_type memory_type;
1347 enum ffa_memory_cacheability cacheability;
1348 enum ffa_memory_shareability shareability;
1349
1350 memory_type = ffa_get_memory_type_attr(attributes);
1351 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1352 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1353 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001354 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001355 }
1356
1357 cacheability = ffa_get_memory_cacheability_attr(attributes);
1358 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1359 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1360 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001361 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001362 }
1363
1364 shareability = ffa_get_memory_shareability_attr(attributes);
1365 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1366 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1367 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001368 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001369 }
1370
1371 return (struct ffa_value){.func = FFA_SUCCESS_32};
1372}
1373
1374/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375 * Check that the given `memory_region` represents a valid memory send request
1376 * of the given `share_func` type, return the clear flag and permissions via the
1377 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001378 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001379 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 * not.
1381 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001383 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1384 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001385 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001386{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001388 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001389 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001390 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001391 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001392 enum ffa_data_access data_access;
1393 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001394 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001395
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396 /* The sender must match the message sender. */
1397 if (memory_region->sender != from_locked.vm->id) {
1398 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001399 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001400 }
1401
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 /*
1403 * Ensure that the composite header is within the memory bounds and
1404 * doesn't overlap the first part of the message.
1405 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001406 receivers_length = sizeof(struct ffa_memory_access) *
1407 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001408 constituents_offset =
1409 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001410 composite_memory_region_offset =
1411 memory_region->receivers[0].composite_memory_region_offset;
1412 if ((composite_memory_region_offset == 0) ||
1413 (composite_memory_region_offset <
1414 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001415 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001416 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001417 "Invalid composite memory region descriptor offset "
1418 "%d.\n",
1419 memory_region->receivers[0]
1420 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 }
1423
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425
1426 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001427 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001429 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1430 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001431 if (memory_share_length != constituents_offset + constituents_length) {
1432 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001433 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 memory_region->receivers[0]
1435 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001438 if (fragment_length < memory_share_length &&
1439 fragment_length < HF_MAILBOX_SIZE) {
1440 dlog_warning(
1441 "Initial fragment length %d smaller than mailbox "
1442 "size.\n",
1443 fragment_length);
1444 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001445
Andrew Walbrana65a1322020-04-06 19:32:32 +01001446 /*
1447 * Clear is not allowed for memory sharing, as the sender still has
1448 * access to the memory.
1449 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001450 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1451 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001453 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001454 }
1455
1456 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001457 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001458 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001460 }
1461
J-Alves363f5722022-04-25 17:37:37 +01001462 /* Check that the permissions are valid, for each specified receiver. */
1463 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1464 ffa_memory_access_permissions_t permissions =
1465 memory_region->receivers[i]
1466 .receiver_permissions.permissions;
1467 ffa_vm_id_t receiver_id =
1468 memory_region->receivers[i]
1469 .receiver_permissions.receiver;
1470
1471 if (memory_region->sender == receiver_id) {
1472 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474 }
Federico Recanati85090c42021-12-15 13:17:54 +01001475
J-Alves363f5722022-04-25 17:37:37 +01001476 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1477 j++) {
1478 if (receiver_id ==
1479 memory_region->receivers[j]
1480 .receiver_permissions.receiver) {
1481 dlog_verbose(
1482 "Repeated receiver(%x) in memory send "
1483 "operation.\n",
1484 memory_region->receivers[j]
1485 .receiver_permissions.receiver);
1486 return ffa_error(FFA_INVALID_PARAMETERS);
1487 }
1488 }
1489
1490 if (composite_memory_region_offset !=
1491 memory_region->receivers[i]
1492 .composite_memory_region_offset) {
1493 dlog_verbose(
1494 "All ffa_memory_access should point to the "
1495 "same composite memory region offset.\n");
1496 return ffa_error(FFA_INVALID_PARAMETERS);
1497 }
1498
1499 data_access = ffa_get_data_access_attr(permissions);
1500 instruction_access =
1501 ffa_get_instruction_access_attr(permissions);
1502 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1503 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1504 dlog_verbose(
1505 "Reserved value for receiver permissions "
1506 "%#x.\n",
1507 permissions);
1508 return ffa_error(FFA_INVALID_PARAMETERS);
1509 }
1510 if (instruction_access !=
1511 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1512 dlog_verbose(
1513 "Invalid instruction access permissions %#x "
1514 "for sending memory.\n",
1515 permissions);
1516 return ffa_error(FFA_INVALID_PARAMETERS);
1517 }
1518 if (share_func == FFA_MEM_SHARE_32) {
1519 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1520 dlog_verbose(
1521 "Invalid data access permissions %#x "
1522 "for sharing memory.\n",
1523 permissions);
1524 return ffa_error(FFA_INVALID_PARAMETERS);
1525 }
1526 /*
1527 * According to section 10.10.3 of the FF-A v1.1 EAC0
1528 * spec, NX is required for share operations (but must
1529 * not be specified by the sender) so set it in the
1530 * copy that we store, ready to be returned to the
1531 * retriever.
1532 */
1533 ffa_set_instruction_access_attr(
1534 &permissions, FFA_INSTRUCTION_ACCESS_NX);
1535 memory_region->receivers[i]
1536 .receiver_permissions.permissions = permissions;
1537 }
1538 if (share_func == FFA_MEM_LEND_32 &&
1539 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1540 dlog_verbose(
1541 "Invalid data access permissions %#x for "
1542 "lending memory.\n",
1543 permissions);
1544 return ffa_error(FFA_INVALID_PARAMETERS);
1545 }
1546
1547 if (share_func == FFA_MEM_DONATE_32 &&
1548 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1549 dlog_verbose(
1550 "Invalid data access permissions %#x for "
1551 "donating memory.\n",
1552 permissions);
1553 return ffa_error(FFA_INVALID_PARAMETERS);
1554 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001555 }
1556
Federico Recanatid937f5e2021-12-20 17:38:23 +01001557 /*
J-Alves807794e2022-06-16 13:42:47 +01001558 * If a memory donate or lend with single borrower, the memory type
1559 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001560 */
J-Alves807794e2022-06-16 13:42:47 +01001561 if (share_func == FFA_MEM_DONATE_32 ||
1562 (share_func == FFA_MEM_LEND_32 &&
1563 memory_region->receiver_count == 1)) {
1564 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1565 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1566 dlog_verbose(
1567 "Memory type shall not be specified by "
1568 "sender.\n");
1569 return ffa_error(FFA_INVALID_PARAMETERS);
1570 }
1571 } else {
1572 /*
1573 * Check that sender's memory attributes match Hafnium
1574 * expectations: Normal Memory, Inner shareable, Write-Back
1575 * Read-Allocate Write-Allocate Cacheable.
1576 */
1577 ret = ffa_memory_attributes_validate(memory_region->attributes);
1578 if (ret.func != FFA_SUCCESS_32) {
1579 return ret;
1580 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001581 }
1582
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001583 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001584}
1585
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001586/** Forwards a memory send message on to the TEE. */
1587static struct ffa_value memory_send_tee_forward(
1588 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1589 uint32_t share_func, struct ffa_memory_region *memory_region,
1590 uint32_t memory_share_length, uint32_t fragment_length)
1591{
1592 struct ffa_value ret;
1593
1594 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1595 memory_region, fragment_length);
1596 tee_locked.vm->mailbox.recv_size = fragment_length;
1597 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1598 tee_locked.vm->mailbox.recv_func = share_func;
1599 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001600 ret = arch_other_world_call(
1601 (struct ffa_value){.func = share_func,
1602 .arg1 = memory_share_length,
1603 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001604 /*
1605 * After the call to the TEE completes it must have finished reading its
1606 * RX buffer, so it is ready for another message.
1607 */
1608 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1609
1610 return ret;
1611}
1612
Andrew Walbrana65a1322020-04-06 19:32:32 +01001613/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001614 * Gets the share state for continuing an operation to donate, lend or share
1615 * memory, and checks that it is a valid request.
1616 *
1617 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1618 * not.
1619 */
1620static struct ffa_value ffa_memory_send_continue_validate(
1621 struct share_states_locked share_states, ffa_memory_handle_t handle,
1622 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1623 struct mpool *page_pool)
1624{
1625 struct ffa_memory_share_state *share_state;
1626 struct ffa_memory_region *memory_region;
1627
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001628 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001629
1630 /*
1631 * Look up the share state by handle and make sure that the VM ID
1632 * matches.
1633 */
1634 if (!get_share_state(share_states, handle, &share_state)) {
1635 dlog_verbose(
1636 "Invalid handle %#x for memory send continuation.\n",
1637 handle);
1638 return ffa_error(FFA_INVALID_PARAMETERS);
1639 }
1640 memory_region = share_state->memory_region;
1641
1642 if (memory_region->sender != from_vm_id) {
1643 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1644 return ffa_error(FFA_INVALID_PARAMETERS);
1645 }
1646
1647 if (share_state->sending_complete) {
1648 dlog_verbose(
1649 "Sending of memory handle %#x is already complete.\n",
1650 handle);
1651 return ffa_error(FFA_INVALID_PARAMETERS);
1652 }
1653
1654 if (share_state->fragment_count == MAX_FRAGMENTS) {
1655 /*
1656 * Log a warning as this is a sign that MAX_FRAGMENTS should
1657 * probably be increased.
1658 */
1659 dlog_warning(
1660 "Too many fragments for memory share with handle %#x; "
1661 "only %d supported.\n",
1662 handle, MAX_FRAGMENTS);
1663 /* Free share state, as it's not possible to complete it. */
1664 share_state_free(share_states, share_state, page_pool);
1665 return ffa_error(FFA_NO_MEMORY);
1666 }
1667
1668 *share_state_ret = share_state;
1669
1670 return (struct ffa_value){.func = FFA_SUCCESS_32};
1671}
1672
1673/**
1674 * Forwards a memory send continuation message on to the TEE.
1675 */
1676static struct ffa_value memory_send_continue_tee_forward(
1677 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1678 uint32_t fragment_length, ffa_memory_handle_t handle)
1679{
1680 struct ffa_value ret;
1681
1682 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1683 fragment_length);
1684 tee_locked.vm->mailbox.recv_size = fragment_length;
1685 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1686 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1687 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001688 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001689 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1690 .arg1 = (uint32_t)handle,
1691 .arg2 = (uint32_t)(handle >> 32),
1692 .arg3 = fragment_length,
1693 .arg4 = (uint64_t)sender_vm_id << 16});
1694 /*
1695 * After the call to the TEE completes it must have finished reading its
1696 * RX buffer, so it is ready for another message.
1697 */
1698 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1699
1700 return ret;
1701}
1702
1703/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001704 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1705 * updates the stage-2 page tables. Specifically, check if the message length
1706 * and number of memory region constituents match, and if the transition is
1707 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001708 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001709 * Assumes that the caller has already found and locked the sender VM and copied
1710 * the memory region descriptor from the sender's TX buffer to a freshly
1711 * allocated page from Hafnium's internal pool. The caller must have also
1712 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001713 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001714 * This function takes ownership of the `memory_region` passed in and will free
1715 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001716 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001717struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001718 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001719 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001720 uint32_t fragment_length, uint32_t share_func,
1721 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001722{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001723 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001724 struct share_states_locked share_states;
1725 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001726
1727 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001728 * If there is an error validating the `memory_region` then we need to
1729 * free it because we own it but we won't be storing it in a share state
1730 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001731 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001732 ret = ffa_memory_send_validate(from_locked, memory_region,
1733 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001734 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001735 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001736 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001737 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001738 }
1739
Andrew Walbrana65a1322020-04-06 19:32:32 +01001740 /* Set flag for share function, ready to be retrieved later. */
1741 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001742 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001743 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001744 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001745 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001746 case FFA_MEM_LEND_32:
1747 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001748 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001749 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001750 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001751 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001752 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001753 }
1754
Andrew Walbranca808b12020-05-15 17:22:28 +01001755 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001756 /*
1757 * Allocate a share state before updating the page table. Otherwise if
1758 * updating the page table succeeded but allocating the share state
1759 * failed then it would leave the memory in a state where nobody could
1760 * get it back.
1761 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001762 if (!allocate_share_state(share_states, share_func, memory_region,
1763 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1764 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001765 dlog_verbose("Failed to allocate share state.\n");
1766 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001767 ret = ffa_error(FFA_NO_MEMORY);
1768 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001769 }
1770
Andrew Walbranca808b12020-05-15 17:22:28 +01001771 if (fragment_length == memory_share_length) {
1772 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001773 ret = ffa_memory_send_complete(
1774 from_locked, share_states, share_state, page_pool,
1775 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001776 } else {
1777 ret = (struct ffa_value){
1778 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001779 .arg1 = (uint32_t)memory_region->handle,
1780 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001781 .arg3 = fragment_length};
1782 }
1783
1784out:
1785 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001786 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001787 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001788}
1789
1790/**
1791 * Validates a call to donate, lend or share memory to the TEE and then updates
1792 * the stage-2 page tables. Specifically, check if the message length and number
1793 * of memory region constituents match, and if the transition is valid for the
1794 * type of memory sending operation.
1795 *
1796 * Assumes that the caller has already found and locked the sender VM and the
1797 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1798 * to a freshly allocated page from Hafnium's internal pool. The caller must
1799 * have also validated that the receiver VM ID is valid.
1800 *
1801 * This function takes ownership of the `memory_region` passed in and will free
1802 * it when necessary; it must not be freed by the caller.
1803 */
1804struct ffa_value ffa_memory_tee_send(
1805 struct vm_locked from_locked, struct vm_locked to_locked,
1806 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1807 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1808{
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001809 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001810
1811 /*
1812 * If there is an error validating the `memory_region` then we need to
1813 * free it because we own it but we won't be storing it in a share state
1814 * after all.
1815 */
1816 ret = ffa_memory_send_validate(from_locked, memory_region,
1817 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001818 share_func);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001819 if (ret.func != FFA_SUCCESS_32) {
1820 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001821 }
1822
Andrew Walbranca808b12020-05-15 17:22:28 +01001823 if (fragment_length == memory_share_length) {
1824 /* No more fragments to come, everything fit in one message. */
1825 struct ffa_composite_memory_region *composite =
1826 ffa_memory_region_get_composite(memory_region, 0);
1827 struct ffa_memory_region_constituent *constituents =
1828 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001829 struct mpool local_page_pool;
1830 uint32_t orig_from_mode;
1831
1832 /*
1833 * Use a local page pool so that we can roll back if necessary.
1834 */
1835 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001836
1837 ret = ffa_send_check_update(
1838 from_locked, &constituents,
1839 &composite->constituent_count, 1, share_func,
J-Alves363f5722022-04-25 17:37:37 +01001840 memory_region->receivers, memory_region->receiver_count,
1841 &local_page_pool,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001842 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1843 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001844 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001845 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001846 goto out;
1847 }
1848
1849 /* Forward memory send message on to TEE. */
1850 ret = memory_send_tee_forward(
1851 to_locked, from_locked.vm->id, share_func,
1852 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001853
1854 if (ret.func != FFA_SUCCESS_32) {
1855 dlog_verbose(
1856 "TEE didn't successfully complete memory send "
1857 "operation; returned %#x (%d). Rolling back.\n",
1858 ret.func, ret.arg2);
1859
1860 /*
1861 * The TEE failed to complete the send operation, so
1862 * roll back the page table update for the VM. This
1863 * can't fail because it won't try to allocate more
1864 * memory than was freed into the `local_page_pool` by
1865 * `ffa_send_check_update` in the initial update.
1866 */
1867 CHECK(ffa_region_group_identity_map(
1868 from_locked, &constituents,
1869 &composite->constituent_count, 1,
1870 orig_from_mode, &local_page_pool, true));
1871 }
1872
1873 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001874 } else {
1875 struct share_states_locked share_states = share_states_lock();
1876 ffa_memory_handle_t handle;
1877
1878 /*
1879 * We need to wait for the rest of the fragments before we can
1880 * check whether the transaction is valid and unmap the memory.
1881 * Call the TEE so it can do its initial validation and assign a
1882 * handle, and allocate a share state to keep what we have so
1883 * far.
1884 */
1885 ret = memory_send_tee_forward(
1886 to_locked, from_locked.vm->id, share_func,
1887 memory_region, memory_share_length, fragment_length);
1888 if (ret.func == FFA_ERROR_32) {
1889 goto out_unlock;
1890 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1891 dlog_warning(
1892 "Got %#x from TEE in response to %#x for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001893 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001894 "FFA_MEM_FRAG_RX.\n",
1895 ret.func, share_func, fragment_length,
1896 memory_share_length);
1897 ret = ffa_error(FFA_INVALID_PARAMETERS);
1898 goto out_unlock;
1899 }
1900 handle = ffa_frag_handle(ret);
1901 if (ret.arg3 != fragment_length) {
1902 dlog_warning(
1903 "Got unexpected fragment offset %d for "
1904 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1905 ret.arg3, fragment_length);
1906 ret = ffa_error(FFA_INVALID_PARAMETERS);
1907 goto out_unlock;
1908 }
1909 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1910 dlog_warning(
1911 "Got unexpected sender ID %d for "
1912 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1913 ffa_frag_sender(ret), from_locked.vm->id);
1914 ret = ffa_error(FFA_INVALID_PARAMETERS);
1915 goto out_unlock;
1916 }
1917
1918 if (!allocate_share_state(share_states, share_func,
1919 memory_region, fragment_length,
1920 handle, NULL)) {
1921 dlog_verbose("Failed to allocate share state.\n");
1922 ret = ffa_error(FFA_NO_MEMORY);
1923 goto out_unlock;
1924 }
1925 /*
1926 * Don't free the memory region fragment, as it has been stored
1927 * in the share state.
1928 */
1929 memory_region = NULL;
1930 out_unlock:
1931 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001932 }
1933
Andrew Walbranca808b12020-05-15 17:22:28 +01001934out:
1935 if (memory_region != NULL) {
1936 mpool_free(page_pool, memory_region);
1937 }
1938 dump_share_states();
1939 return ret;
1940}
1941
1942/**
1943 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1944 * this is the last fragment then checks that the transition is valid for the
1945 * type of memory sending operation and updates the stage-2 page tables of the
1946 * sender.
1947 *
1948 * Assumes that the caller has already found and locked the sender VM and copied
1949 * the memory region descriptor from the sender's TX buffer to a freshly
1950 * allocated page from Hafnium's internal pool.
1951 *
1952 * This function takes ownership of the `fragment` passed in; it must not be
1953 * freed by the caller.
1954 */
1955struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1956 void *fragment,
1957 uint32_t fragment_length,
1958 ffa_memory_handle_t handle,
1959 struct mpool *page_pool)
1960{
1961 struct share_states_locked share_states = share_states_lock();
1962 struct ffa_memory_share_state *share_state;
1963 struct ffa_value ret;
1964 struct ffa_memory_region *memory_region;
1965
1966 ret = ffa_memory_send_continue_validate(share_states, handle,
1967 &share_state,
1968 from_locked.vm->id, page_pool);
1969 if (ret.func != FFA_SUCCESS_32) {
1970 goto out_free_fragment;
1971 }
1972 memory_region = share_state->memory_region;
1973
1974 if (memory_region->receivers[0].receiver_permissions.receiver ==
1975 HF_TEE_VM_ID) {
1976 dlog_error(
1977 "Got hypervisor-allocated handle for memory send to "
1978 "TEE. This should never happen, and indicates a bug in "
1979 "EL3 code.\n");
1980 ret = ffa_error(FFA_INVALID_PARAMETERS);
1981 goto out_free_fragment;
1982 }
1983
1984 /* Add this fragment. */
1985 share_state->fragments[share_state->fragment_count] = fragment;
1986 share_state->fragment_constituent_counts[share_state->fragment_count] =
1987 fragment_length / sizeof(struct ffa_memory_region_constituent);
1988 share_state->fragment_count++;
1989
1990 /* Check whether the memory send operation is now ready to complete. */
1991 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001992 ret = ffa_memory_send_complete(
1993 from_locked, share_states, share_state, page_pool,
1994 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001995 } else {
1996 ret = (struct ffa_value){
1997 .func = FFA_MEM_FRAG_RX_32,
1998 .arg1 = (uint32_t)handle,
1999 .arg2 = (uint32_t)(handle >> 32),
2000 .arg3 = share_state_next_fragment_offset(share_states,
2001 share_state)};
2002 }
2003 goto out;
2004
2005out_free_fragment:
2006 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002007
2008out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002009 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002010 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002011}
2012
Andrew Walbranca808b12020-05-15 17:22:28 +01002013/**
2014 * Continues an operation to donate, lend or share memory to the TEE VM. If this
2015 * is the last fragment then checks that the transition is valid for the type of
2016 * memory sending operation and updates the stage-2 page tables of the sender.
2017 *
2018 * Assumes that the caller has already found and locked the sender VM and copied
2019 * the memory region descriptor from the sender's TX buffer to a freshly
2020 * allocated page from Hafnium's internal pool.
2021 *
2022 * This function takes ownership of the `memory_region` passed in and will free
2023 * it when necessary; it must not be freed by the caller.
2024 */
2025struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
2026 struct vm_locked to_locked,
2027 void *fragment,
2028 uint32_t fragment_length,
2029 ffa_memory_handle_t handle,
2030 struct mpool *page_pool)
2031{
2032 struct share_states_locked share_states = share_states_lock();
2033 struct ffa_memory_share_state *share_state;
2034 struct ffa_value ret;
2035 struct ffa_memory_region *memory_region;
2036
2037 ret = ffa_memory_send_continue_validate(share_states, handle,
2038 &share_state,
2039 from_locked.vm->id, page_pool);
2040 if (ret.func != FFA_SUCCESS_32) {
2041 goto out_free_fragment;
2042 }
2043 memory_region = share_state->memory_region;
2044
2045 if (memory_region->receivers[0].receiver_permissions.receiver !=
2046 HF_TEE_VM_ID) {
2047 dlog_error(
2048 "Got SPM-allocated handle for memory send to non-TEE "
2049 "VM. This should never happen, and indicates a bug.\n");
2050 ret = ffa_error(FFA_INVALID_PARAMETERS);
2051 goto out_free_fragment;
2052 }
2053
2054 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2055 to_locked.vm->mailbox.recv == NULL) {
2056 /*
2057 * If the TEE RX buffer is not available, tell the sender to
2058 * retry by returning the current offset again.
2059 */
2060 ret = (struct ffa_value){
2061 .func = FFA_MEM_FRAG_RX_32,
2062 .arg1 = (uint32_t)handle,
2063 .arg2 = (uint32_t)(handle >> 32),
2064 .arg3 = share_state_next_fragment_offset(share_states,
2065 share_state),
2066 };
2067 goto out_free_fragment;
2068 }
2069
2070 /* Add this fragment. */
2071 share_state->fragments[share_state->fragment_count] = fragment;
2072 share_state->fragment_constituent_counts[share_state->fragment_count] =
2073 fragment_length / sizeof(struct ffa_memory_region_constituent);
2074 share_state->fragment_count++;
2075
2076 /* Check whether the memory send operation is now ready to complete. */
2077 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002078 struct mpool local_page_pool;
2079 uint32_t orig_from_mode;
2080
2081 /*
2082 * Use a local page pool so that we can roll back if necessary.
2083 */
2084 mpool_init_with_fallback(&local_page_pool, page_pool);
2085
Andrew Walbranca808b12020-05-15 17:22:28 +01002086 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002087 share_state, &local_page_pool,
2088 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002089
2090 if (ret.func == FFA_SUCCESS_32) {
2091 /*
2092 * Forward final fragment on to the TEE so that
2093 * it can complete the memory sending operation.
2094 */
2095 ret = memory_send_continue_tee_forward(
2096 to_locked, from_locked.vm->id, fragment,
2097 fragment_length, handle);
2098
2099 if (ret.func != FFA_SUCCESS_32) {
2100 /*
2101 * The error will be passed on to the caller,
2102 * but log it here too.
2103 */
2104 dlog_verbose(
2105 "TEE didn't successfully complete "
2106 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002107 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002108 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002109
2110 /*
2111 * The TEE failed to complete the send
2112 * operation, so roll back the page table update
2113 * for the VM. This can't fail because it won't
2114 * try to allocate more memory than was freed
2115 * into the `local_page_pool` by
2116 * `ffa_send_check_update` in the initial
2117 * update.
2118 */
2119 CHECK(ffa_region_group_identity_map(
2120 from_locked, share_state->fragments,
2121 share_state
2122 ->fragment_constituent_counts,
2123 share_state->fragment_count,
2124 orig_from_mode, &local_page_pool,
2125 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002126 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002127
Andrew Walbranca808b12020-05-15 17:22:28 +01002128 /* Free share state. */
2129 share_state_free(share_states, share_state, page_pool);
2130 } else {
2131 /* Abort sending to TEE. */
2132 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002133 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002134 .func = FFA_MEM_RECLAIM_32,
2135 .arg1 = (uint32_t)handle,
2136 .arg2 = (uint32_t)(handle >> 32)});
2137
2138 if (tee_ret.func != FFA_SUCCESS_32) {
2139 /*
2140 * Nothing we can do if TEE doesn't abort
2141 * properly, just log it.
2142 */
2143 dlog_verbose(
2144 "TEE didn't successfully abort failed "
2145 "memory send operation; returned %#x "
2146 "(%d).\n",
2147 tee_ret.func, tee_ret.arg2);
2148 }
2149 /*
2150 * We don't need to free the share state in this case
2151 * because ffa_memory_send_complete does that already.
2152 */
2153 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002154
2155 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002156 } else {
2157 uint32_t next_fragment_offset =
2158 share_state_next_fragment_offset(share_states,
2159 share_state);
2160
2161 ret = memory_send_continue_tee_forward(
2162 to_locked, from_locked.vm->id, fragment,
2163 fragment_length, handle);
2164
2165 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2166 ffa_frag_handle(ret) != handle ||
2167 ret.arg3 != next_fragment_offset ||
2168 ffa_frag_sender(ret) != from_locked.vm->id) {
2169 dlog_verbose(
2170 "Got unexpected result from forwarding "
2171 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2172 "offset %d, sender %d); expected "
2173 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2174 "sender %d).\n",
2175 ret.func, ffa_frag_handle(ret), ret.arg3,
2176 ffa_frag_sender(ret), handle,
2177 next_fragment_offset, from_locked.vm->id);
2178 /* Free share state. */
2179 share_state_free(share_states, share_state, page_pool);
2180 ret = ffa_error(FFA_INVALID_PARAMETERS);
2181 goto out;
2182 }
2183
2184 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2185 .arg1 = (uint32_t)handle,
2186 .arg2 = (uint32_t)(handle >> 32),
2187 .arg3 = next_fragment_offset};
2188 }
2189 goto out;
2190
2191out_free_fragment:
2192 mpool_free(page_pool, fragment);
2193
2194out:
2195 share_states_unlock(&share_states);
2196 return ret;
2197}
2198
2199/** Clean up after the receiver has finished retrieving a memory region. */
2200static void ffa_memory_retrieve_complete(
2201 struct share_states_locked share_states,
2202 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2203{
2204 if (share_state->share_func == FFA_MEM_DONATE_32) {
2205 /*
2206 * Memory that has been donated can't be relinquished,
2207 * so no need to keep the share state around.
2208 */
2209 share_state_free(share_states, share_state, page_pool);
2210 dlog_verbose("Freed share state for donate.\n");
2211 }
2212}
2213
J-Alves96de29f2022-04-26 16:05:24 +01002214/*
2215 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2216 * returns its index in the receiver's array. If receiver's ID doesn't exist
2217 * in the array, return the region's 'receiver_count'.
2218 */
2219static uint32_t ffa_memory_region_get_receiver(
2220 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
2221{
2222 struct ffa_memory_access *receivers;
2223 uint32_t i;
2224
2225 assert(memory_region != NULL);
2226
2227 receivers = memory_region->receivers;
2228
2229 for (i = 0U; i < memory_region->receiver_count; i++) {
2230 if (receivers[i].receiver_permissions.receiver == receiver) {
2231 break;
2232 }
2233 }
2234
2235 return i;
2236}
2237
2238/**
2239 * Validates the retrieved permissions against those specified by the lender
2240 * of memory share operation. Optionally can help set the permissions to be used
2241 * for the S2 mapping, through the `permissions` argument.
2242 * Returns true if permissions are valid, false otherwise.
2243 */
2244static bool ffa_memory_retrieve_is_memory_access_valid(
2245 enum ffa_data_access sent_data_access,
2246 enum ffa_data_access requested_data_access,
2247 enum ffa_instruction_access sent_instruction_access,
2248 enum ffa_instruction_access requested_instruction_access,
2249 ffa_memory_access_permissions_t *permissions)
2250{
2251 switch (sent_data_access) {
2252 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2253 case FFA_DATA_ACCESS_RW:
2254 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2255 requested_data_access == FFA_DATA_ACCESS_RW) {
2256 if (permissions != NULL) {
2257 ffa_set_data_access_attr(permissions,
2258 FFA_DATA_ACCESS_RW);
2259 }
2260 break;
2261 }
2262 /* Intentional fall-through. */
2263 case FFA_DATA_ACCESS_RO:
2264 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2265 requested_data_access == FFA_DATA_ACCESS_RO) {
2266 if (permissions != NULL) {
2267 ffa_set_data_access_attr(permissions,
2268 FFA_DATA_ACCESS_RO);
2269 }
2270 break;
2271 }
2272 dlog_verbose(
2273 "Invalid data access requested; sender specified "
2274 "permissions %#x but receiver requested %#x.\n",
2275 sent_data_access, requested_data_access);
2276 return false;
2277 case FFA_DATA_ACCESS_RESERVED:
2278 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2279 "checked before this point.");
2280 }
2281
2282 switch (sent_instruction_access) {
2283 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2284 case FFA_INSTRUCTION_ACCESS_X:
2285 if (requested_instruction_access ==
2286 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2287 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2288 if (permissions != NULL) {
2289 ffa_set_instruction_access_attr(
2290 permissions, FFA_INSTRUCTION_ACCESS_X);
2291 }
2292 break;
2293 }
2294 case FFA_INSTRUCTION_ACCESS_NX:
2295 if (requested_instruction_access ==
2296 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2297 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2298 if (permissions != NULL) {
2299 ffa_set_instruction_access_attr(
2300 permissions, FFA_INSTRUCTION_ACCESS_NX);
2301 }
2302 break;
2303 }
2304 dlog_verbose(
2305 "Invalid instruction access requested; sender "
2306 "specified permissions %#x but receiver requested "
2307 "%#x.\n",
2308 sent_instruction_access, requested_instruction_access);
2309 return false;
2310 case FFA_INSTRUCTION_ACCESS_RESERVED:
2311 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2312 "be checked before this point.");
2313 }
2314
2315 return true;
2316}
2317
2318/**
2319 * Validate the receivers' permissions in the retrieve request against those
2320 * specified by the lender.
2321 * In the `permissions` argument returns the permissions to set at S2 for the
2322 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2323 * Returns FFA_SUCCESS if all specified permissions are valid.
2324 */
2325static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2326 struct ffa_memory_region *memory_region,
2327 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2328 ffa_memory_access_permissions_t *permissions)
2329{
2330 uint32_t retrieve_receiver_index;
2331
2332 assert(permissions != NULL);
2333
2334 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2335 dlog_verbose(
2336 "Retrieve request should contain same list of "
2337 "borrowers, as specified by the lender.\n");
2338 return ffa_error(FFA_INVALID_PARAMETERS);
2339 }
2340
2341 retrieve_receiver_index = retrieve_request->receiver_count;
2342
2343 /* Should be populated with the permissions of the retriever. */
2344 *permissions = 0;
2345
2346 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2347 ffa_memory_access_permissions_t sent_permissions;
2348 struct ffa_memory_access *current_receiver =
2349 &retrieve_request->receivers[i];
2350 ffa_memory_access_permissions_t requested_permissions =
2351 current_receiver->receiver_permissions.permissions;
2352 ffa_vm_id_t current_receiver_id =
2353 current_receiver->receiver_permissions.receiver;
2354 bool found_to_id = current_receiver_id == to_vm_id;
2355
2356 /*
2357 * Find the current receiver in the transaction descriptor from
2358 * sender.
2359 */
2360 uint32_t mem_region_receiver_index =
2361 ffa_memory_region_get_receiver(memory_region,
2362 current_receiver_id);
2363
2364 if (mem_region_receiver_index ==
2365 memory_region->receiver_count) {
2366 dlog_verbose("%s: receiver %x not found\n", __func__,
2367 current_receiver_id);
2368 return ffa_error(FFA_DENIED);
2369 }
2370
2371 sent_permissions =
2372 memory_region->receivers[mem_region_receiver_index]
2373 .receiver_permissions.permissions;
2374
2375 if (found_to_id) {
2376 retrieve_receiver_index = i;
2377 }
2378
2379 /*
2380 * Since we are traversing the list of receivers, save the index
2381 * of the caller. As it needs to be there.
2382 */
2383
2384 if (current_receiver->composite_memory_region_offset != 0U) {
2385 dlog_verbose(
2386 "Retriever specified address ranges not "
2387 "supported (got offset %d).\n",
2388 current_receiver
2389 ->composite_memory_region_offset);
2390 return ffa_error(FFA_INVALID_PARAMETERS);
2391 }
2392
2393 /*
2394 * Check permissions from sender against permissions requested
2395 * by receiver.
2396 */
2397 if (!ffa_memory_retrieve_is_memory_access_valid(
2398 ffa_get_data_access_attr(sent_permissions),
2399 ffa_get_data_access_attr(requested_permissions),
2400 ffa_get_instruction_access_attr(sent_permissions),
2401 ffa_get_instruction_access_attr(
2402 requested_permissions),
2403 found_to_id ? permissions : NULL)) {
2404 return ffa_error(FFA_DENIED);
2405 }
2406
2407 /*
2408 * Can't request PM to clear memory if only provided with RO
2409 * permissions.
2410 */
2411 if (found_to_id &&
2412 (ffa_get_data_access_attr(*permissions) ==
2413 FFA_DATA_ACCESS_RO) &&
2414 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2415 0U) {
2416 dlog_verbose(
2417 "Receiver has RO permissions can not request "
2418 "clear.\n");
2419 return ffa_error(FFA_DENIED);
2420 }
2421 }
2422
2423 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2424 dlog_verbose(
2425 "Retrieve request does not contain caller's (%x) "
2426 "permissions\n",
2427 to_vm_id);
2428 return ffa_error(FFA_INVALID_PARAMETERS);
2429 }
2430
2431 return (struct ffa_value){.func = FFA_SUCCESS_32};
2432}
2433
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002434struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2435 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002436 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002437 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002438{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002439 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002440 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002441 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002442 sizeof(struct ffa_memory_access);
2443 ffa_memory_handle_t handle = retrieve_request->handle;
2444 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002445 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002446 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2447 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002448 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002449 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002450 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002451 struct ffa_memory_share_state *share_state;
2452 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002453 struct ffa_composite_memory_region *composite;
2454 uint32_t total_length;
2455 uint32_t fragment_length;
J-Alves96de29f2022-04-26 16:05:24 +01002456 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002457
2458 dump_share_states();
2459
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002460 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002461 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002462 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002463 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002464 expected_retrieve_request_length,
2465 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002467 }
2468
2469 share_states = share_states_lock();
2470 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002471 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002473 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474 goto out;
2475 }
2476
J-Alves96de29f2022-04-26 16:05:24 +01002477 if (!share_state->sending_complete) {
2478 dlog_verbose(
2479 "Memory with handle %#x not fully sent, can't "
2480 "retrieve.\n",
2481 handle);
2482 ret = ffa_error(FFA_INVALID_PARAMETERS);
2483 goto out;
2484 }
2485
Andrew Walbrana65a1322020-04-06 19:32:32 +01002486 memory_region = share_state->memory_region;
2487 CHECK(memory_region != NULL);
2488
2489 /*
J-Alves96de29f2022-04-26 16:05:24 +01002490 * Find receiver index in the receivers list specified by the sender.
2491 */
2492 receiver_index =
2493 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2494
2495 if (receiver_index == memory_region->receiver_count) {
2496 dlog_verbose(
2497 "Incorrect receiver VM ID %x for FFA_MEM_RETRIEVE_REQ, "
2498 "for handle %#x.\n",
2499 to_locked.vm->id, handle);
2500 ret = ffa_error(FFA_INVALID_PARAMETERS);
2501 goto out;
2502 }
2503
2504 if (share_state->retrieved_fragment_count[receiver_index] != 0U) {
2505 dlog_verbose("Memory with handle %#x already retrieved.\n",
2506 handle);
2507 ret = ffa_error(FFA_DENIED);
2508 goto out;
2509 }
2510
2511 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002512 * Check that the transaction type expected by the receiver is correct,
2513 * if it has been specified.
2514 */
2515 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002516 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002517 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 dlog_verbose(
2520 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002521 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002522 transaction_type,
2523 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002524 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002526 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 goto out;
2528 }
2529
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530 if (retrieve_request->sender != memory_region->sender) {
2531 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002532 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533 "expected %d for handle %#x.\n",
2534 retrieve_request->sender, memory_region->sender,
2535 handle);
J-Alves040c4ef2022-05-13 14:42:49 +01002536 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002537 goto out;
2538 }
2539
2540 if (retrieve_request->tag != memory_region->tag) {
2541 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002542 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002543 "%d for handle %#x.\n",
2544 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002545 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546 goto out;
2547 }
2548
Federico Recanati85090c42021-12-15 13:17:54 +01002549 if ((retrieve_request->flags &
J-Alves96de29f2022-04-26 16:05:24 +01002550 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
Federico Recanati85090c42021-12-15 13:17:54 +01002551 dlog_verbose(
2552 "Retriever specified 'address range alignment hint'"
2553 " not supported.\n");
2554 ret = ffa_error(FFA_INVALID_PARAMETERS);
2555 goto out;
2556 }
2557 if ((retrieve_request->flags &
2558 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2559 dlog_verbose(
2560 "Bits 8-5 must be zero in memory region's flags "
2561 "(address range alignment hint not supported).\n");
2562 ret = ffa_error(FFA_INVALID_PARAMETERS);
2563 goto out;
2564 }
2565
J-Alves84658fc2021-06-17 14:37:32 +01002566 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2567 dlog_verbose(
2568 "Bits 31-10 must be zero in memory region's flags.\n");
2569 ret = ffa_error(FFA_INVALID_PARAMETERS);
2570 goto out;
2571 }
2572
2573 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2574 (retrieve_request->flags &
2575 (FFA_MEMORY_REGION_FLAG_CLEAR |
2576 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2577 dlog_verbose(
2578 "Memory Share operation can't clean after relinquish "
2579 "memory region.\n");
2580 ret = ffa_error(FFA_INVALID_PARAMETERS);
2581 goto out;
2582 }
2583
Andrew Walbrana65a1322020-04-06 19:32:32 +01002584 /*
J-Alves17c069c2021-12-07 16:00:38 +00002585 * If the borrower needs the memory to be cleared before mapping to its
2586 * address space, the sender should have set the flag when calling
2587 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2588 */
2589 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
J-Alves96de29f2022-04-26 16:05:24 +01002590 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
J-Alves17c069c2021-12-07 16:00:38 +00002591 dlog_verbose(
2592 "Borrower needs memory cleared. Sender needs to set "
2593 "flag for clearing memory.\n");
2594 ret = ffa_error(FFA_DENIED);
2595 goto out;
2596 }
2597
J-Alves96de29f2022-04-26 16:05:24 +01002598 ret = ffa_memory_retrieve_validate_memory_access_list(
2599 memory_region, retrieve_request, to_locked.vm->id,
2600 &permissions);
2601 if (ret.func != FFA_SUCCESS_32) {
J-Alves84658fc2021-06-17 14:37:32 +01002602 goto out;
2603 }
2604
J-Alves614d9f42022-06-28 14:03:10 +01002605 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2606 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2607 /*
2608 * Ensure receiver's attributes are compatible with how Hafnium
2609 * maps memory: Normal Memory, Inner shareable, Write-Back
2610 * Read-Allocate Write-Allocate Cacheable.
2611 */
2612 ret = ffa_memory_attributes_validate(
2613 retrieve_request->attributes);
2614 if (ret.func != FFA_SUCCESS_32) {
2615 goto out;
2616 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002617 }
2618
J-Alves7cd5eb32020-10-16 19:06:10 +01002619 memory_to_attributes = ffa_memory_permissions_to_mode(
2620 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002621 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002622 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002623 share_state->fragment_constituent_counts,
2624 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002625 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002626 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002627 goto out;
2628 }
2629
2630 /*
2631 * Copy response to RX buffer of caller and deliver the message. This
2632 * must be done before the share_state is (possibly) freed.
2633 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002634 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002635 composite = ffa_memory_region_get_composite(memory_region, 0);
2636 /*
2637 * Constituents which we received in the first fragment should always
2638 * fit in the first fragment we are sending, because the header is the
2639 * same size in both cases and we have a fixed message buffer size. So
2640 * `ffa_retrieved_memory_region_init` should never fail.
2641 */
2642 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002643 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2644 memory_region->sender, memory_region->attributes,
2645 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002646 composite->page_count, composite->constituent_count,
2647 share_state->fragments[0],
2648 share_state->fragment_constituent_counts[0], &total_length,
2649 &fragment_length));
2650 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002652 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002653 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2654
J-Alves96de29f2022-04-26 16:05:24 +01002655 share_state->retrieved_fragment_count[receiver_index] = 1;
2656 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002657 share_state->fragment_count) {
2658 ffa_memory_retrieve_complete(share_states, share_state,
2659 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002660 }
2661
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002662 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002663 .arg1 = total_length,
2664 .arg2 = fragment_length};
2665
2666out:
2667 share_states_unlock(&share_states);
2668 dump_share_states();
2669 return ret;
2670}
2671
2672struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2673 ffa_memory_handle_t handle,
2674 uint32_t fragment_offset,
2675 struct mpool *page_pool)
2676{
2677 struct ffa_memory_region *memory_region;
2678 struct share_states_locked share_states;
2679 struct ffa_memory_share_state *share_state;
2680 struct ffa_value ret;
2681 uint32_t fragment_index;
2682 uint32_t retrieved_constituents_count;
2683 uint32_t i;
2684 uint32_t expected_fragment_offset;
2685 uint32_t remaining_constituent_count;
2686 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002687 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002688
2689 dump_share_states();
2690
2691 share_states = share_states_lock();
2692 if (!get_share_state(share_states, handle, &share_state)) {
2693 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2694 handle);
2695 ret = ffa_error(FFA_INVALID_PARAMETERS);
2696 goto out;
2697 }
2698
2699 memory_region = share_state->memory_region;
2700 CHECK(memory_region != NULL);
2701
J-Alvesc7484f12022-05-13 12:41:14 +01002702 receiver_index =
2703 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2704
2705 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002706 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002707 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2708 "memory sharing transaction (%x)\n",
2709 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002710 ret = ffa_error(FFA_INVALID_PARAMETERS);
2711 goto out;
2712 }
2713
2714 if (!share_state->sending_complete) {
2715 dlog_verbose(
2716 "Memory with handle %#x not fully sent, can't "
2717 "retrieve.\n",
2718 handle);
2719 ret = ffa_error(FFA_INVALID_PARAMETERS);
2720 goto out;
2721 }
2722
J-Alvesc7484f12022-05-13 12:41:14 +01002723 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2724 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002725 share_state->fragment_count) {
2726 dlog_verbose(
2727 "Retrieval of memory with handle %#x not yet started "
2728 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002729 handle,
2730 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002731 share_state->fragment_count);
2732 ret = ffa_error(FFA_INVALID_PARAMETERS);
2733 goto out;
2734 }
2735
J-Alvesc7484f12022-05-13 12:41:14 +01002736 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002737
2738 /*
2739 * Check that the given fragment offset is correct by counting how many
2740 * constituents were in the fragments previously sent.
2741 */
2742 retrieved_constituents_count = 0;
2743 for (i = 0; i < fragment_index; ++i) {
2744 retrieved_constituents_count +=
2745 share_state->fragment_constituent_counts[i];
2746 }
J-Alvesc7484f12022-05-13 12:41:14 +01002747
2748 CHECK(memory_region->receiver_count > 0);
2749
Andrew Walbranca808b12020-05-15 17:22:28 +01002750 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002751 ffa_composite_constituent_offset(memory_region,
2752 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002753 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002754 sizeof(struct ffa_memory_region_constituent) -
2755 sizeof(struct ffa_memory_access) *
2756 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002757 if (fragment_offset != expected_fragment_offset) {
2758 dlog_verbose("Fragment offset was %d but expected %d.\n",
2759 fragment_offset, expected_fragment_offset);
2760 ret = ffa_error(FFA_INVALID_PARAMETERS);
2761 goto out;
2762 }
2763
2764 remaining_constituent_count = ffa_memory_fragment_init(
2765 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2766 share_state->fragments[fragment_index],
2767 share_state->fragment_constituent_counts[fragment_index],
2768 &fragment_length);
2769 CHECK(remaining_constituent_count == 0);
2770 to_locked.vm->mailbox.recv_size = fragment_length;
2771 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2772 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2773 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002774 share_state->retrieved_fragment_count[receiver_index]++;
2775 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002776 share_state->fragment_count) {
2777 ffa_memory_retrieve_complete(share_states, share_state,
2778 page_pool);
2779 }
2780
2781 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2782 .arg1 = (uint32_t)handle,
2783 .arg2 = (uint32_t)(handle >> 32),
2784 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002785
2786out:
2787 share_states_unlock(&share_states);
2788 dump_share_states();
2789 return ret;
2790}
2791
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002792struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002793 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002794 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002795{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002796 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002797 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002798 struct ffa_memory_share_state *share_state;
2799 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002800 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002801 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002802 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002803
Andrew Walbrana65a1322020-04-06 19:32:32 +01002804 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002805 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002806 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002807 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002808 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002809 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002810 }
2811
Andrew Walbrana65a1322020-04-06 19:32:32 +01002812 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002813 dlog_verbose(
2814 "VM ID %d in relinquish message doesn't match calling "
2815 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002816 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002817 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002818 }
2819
2820 dump_share_states();
2821
2822 share_states = share_states_lock();
2823 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002824 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002825 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002826 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002827 goto out;
2828 }
2829
Andrew Walbranca808b12020-05-15 17:22:28 +01002830 if (!share_state->sending_complete) {
2831 dlog_verbose(
2832 "Memory with handle %#x not fully sent, can't "
2833 "relinquish.\n",
2834 handle);
2835 ret = ffa_error(FFA_INVALID_PARAMETERS);
2836 goto out;
2837 }
2838
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002839 memory_region = share_state->memory_region;
2840 CHECK(memory_region != NULL);
2841
J-Alves8eb19162022-04-28 10:56:48 +01002842 receiver_index = ffa_memory_region_get_receiver(memory_region,
2843 from_locked.vm->id);
2844
2845 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002846 dlog_verbose(
2847 "VM ID %d tried to relinquish memory region with "
J-Alves8eb19162022-04-28 10:56:48 +01002848 "handle %#x and it is not a valid borrower.\n",
2849 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002850 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002851 goto out;
2852 }
2853
J-Alves8eb19162022-04-28 10:56:48 +01002854 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002855 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002856 dlog_verbose(
J-Alves8eb19162022-04-28 10:56:48 +01002857 "Memory with handle %#x not yet fully retrieved, "
2858 "receiver %x can't relinquish.\n",
2859 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002860 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002861 goto out;
2862 }
2863
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002864 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002865
2866 /*
2867 * Clear is not allowed for memory that was shared, as the original
2868 * sender still has access to the memory.
2869 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002870 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002871 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002872 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002873 goto out;
2874 }
2875
Andrew Walbranca808b12020-05-15 17:22:28 +01002876 ret = ffa_relinquish_check_update(
2877 from_locked, share_state->fragments,
2878 share_state->fragment_constituent_counts,
2879 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002881 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002882 /*
2883 * Mark memory handle as not retrieved, so it can be reclaimed
2884 * (or retrieved again).
2885 */
J-Alves8eb19162022-04-28 10:56:48 +01002886 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002887 }
2888
2889out:
2890 share_states_unlock(&share_states);
2891 dump_share_states();
2892 return ret;
2893}
2894
2895/**
2896 * Validates that the reclaim transition is allowed for the given handle,
2897 * updates the page table of the reclaiming VM, and frees the internal state
2898 * associated with the handle.
2899 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002900struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002901 ffa_memory_handle_t handle,
2902 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002903 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002904{
2905 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002906 struct ffa_memory_share_state *share_state;
2907 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002908 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002909
2910 dump_share_states();
2911
2912 share_states = share_states_lock();
2913 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002914 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002916 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002917 goto out;
2918 }
2919
2920 memory_region = share_state->memory_region;
2921 CHECK(memory_region != NULL);
2922
2923 if (to_locked.vm->id != memory_region->sender) {
2924 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002925 "VM %#x attempted to reclaim memory handle %#x "
2926 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002927 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002928 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002929 goto out;
2930 }
2931
Andrew Walbranca808b12020-05-15 17:22:28 +01002932 if (!share_state->sending_complete) {
2933 dlog_verbose(
2934 "Memory with handle %#x not fully sent, can't "
2935 "reclaim.\n",
2936 handle);
2937 ret = ffa_error(FFA_INVALID_PARAMETERS);
2938 goto out;
2939 }
2940
J-Alves752236c2022-04-28 11:07:47 +01002941 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2942 if (share_state->retrieved_fragment_count[i] != 0) {
2943 dlog_verbose(
2944 "Tried to reclaim memory handle %#x that has "
2945 "not been relinquished by all borrowers(%x).\n",
2946 handle,
2947 memory_region->receivers[i]
2948 .receiver_permissions.receiver);
2949 ret = ffa_error(FFA_DENIED);
2950 goto out;
2951 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002952 }
2953
Andrew Walbranca808b12020-05-15 17:22:28 +01002954 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002955 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002956 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002957 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002958 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002959
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002960 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002961 share_state_free(share_states, share_state, page_pool);
2962 dlog_verbose("Freed share state after successful reclaim.\n");
2963 }
2964
2965out:
2966 share_states_unlock(&share_states);
2967 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002968}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002969
2970/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002971 * Validates that the reclaim transition is allowed for the memory region with
2972 * the given handle which was previously shared with the TEE, tells the TEE to
2973 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2974 *
2975 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002976 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002977struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002978 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002979 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002980 ffa_memory_region_flags_t flags,
2981 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002982{
Andrew Walbranca808b12020-05-15 17:22:28 +01002983 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2984 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2985 struct ffa_value tee_ret;
2986 uint32_t length;
2987 uint32_t fragment_length;
2988 uint32_t fragment_offset;
2989 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002990 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002991 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2992
2993 CHECK(request_length <= HF_MAILBOX_SIZE);
2994 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2995
2996 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002997 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002998 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2999 .arg1 = request_length,
3000 .arg2 = request_length});
3001 if (tee_ret.func == FFA_ERROR_32) {
3002 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
3003 return tee_ret;
3004 }
3005 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
3006 dlog_verbose(
3007 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
3008 tee_ret.func);
3009 return ffa_error(FFA_INVALID_PARAMETERS);
3010 }
3011
3012 length = tee_ret.arg1;
3013 fragment_length = tee_ret.arg2;
3014
3015 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
3016 length > sizeof(tee_retrieve_buffer)) {
3017 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
3018 fragment_length, length, HF_MAILBOX_SIZE,
3019 sizeof(tee_retrieve_buffer));
3020 return ffa_error(FFA_INVALID_PARAMETERS);
3021 }
3022
3023 /*
3024 * Copy the first fragment of the memory region descriptor to an
3025 * internal buffer.
3026 */
3027 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
3028 from_locked.vm->mailbox.send, fragment_length);
3029
3030 /* Fetch the remaining fragments into the same buffer. */
3031 fragment_offset = fragment_length;
3032 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02003033 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003034 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
3035 .arg1 = (uint32_t)handle,
3036 .arg2 = (uint32_t)(handle >> 32),
3037 .arg3 = fragment_offset});
3038 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
3039 dlog_verbose(
3040 "Got %#x (%d) from TEE in response to "
3041 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
3042 tee_ret.func, tee_ret.arg2);
3043 return tee_ret;
3044 }
3045 if (ffa_frag_handle(tee_ret) != handle) {
3046 dlog_verbose(
3047 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
3048 "in response to FFA_MEM_FRAG_RX for handle "
3049 "%#x.\n",
3050 ffa_frag_handle(tee_ret), handle);
3051 return ffa_error(FFA_INVALID_PARAMETERS);
3052 }
3053 if (ffa_frag_sender(tee_ret) != 0) {
3054 dlog_verbose(
3055 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
3056 "(expected 0).\n",
3057 ffa_frag_sender(tee_ret));
3058 return ffa_error(FFA_INVALID_PARAMETERS);
3059 }
3060 fragment_length = tee_ret.arg3;
3061 if (fragment_length > HF_MAILBOX_SIZE ||
3062 fragment_offset + fragment_length > length) {
3063 dlog_verbose(
3064 "Invalid fragment length %d at offset %d (max "
3065 "%d).\n",
3066 fragment_length, fragment_offset,
3067 HF_MAILBOX_SIZE);
3068 return ffa_error(FFA_INVALID_PARAMETERS);
3069 }
3070 memcpy_s(tee_retrieve_buffer + fragment_offset,
3071 sizeof(tee_retrieve_buffer) - fragment_offset,
3072 from_locked.vm->mailbox.send, fragment_length);
3073
3074 fragment_offset += fragment_length;
3075 }
3076
3077 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00003078
3079 if (memory_region->receiver_count != 1) {
3080 /* Only one receiver supported by Hafnium for now. */
3081 dlog_verbose(
3082 "Multiple recipients not supported (got %d, expected "
3083 "1).\n",
3084 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01003085 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003086 }
3087
3088 if (memory_region->handle != handle) {
3089 dlog_verbose(
3090 "Got memory region handle %#x from TEE but requested "
3091 "handle %#x.\n",
3092 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003093 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003094 }
3095
3096 /* The original sender must match the caller. */
3097 if (to_locked.vm->id != memory_region->sender) {
3098 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003099 "VM %#x attempted to reclaim memory handle %#x "
3100 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003101 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003102 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003103 }
3104
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003105 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003106
3107 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01003108 * Validate that the reclaim transition is allowed for the given memory
3109 * region, forward the request to the TEE and then map the memory back
3110 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003111 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01003112 return ffa_tee_reclaim_check_update(
3113 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01003114 composite->constituent_count, memory_to_attributes,
3115 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003116}