blob: 240b230cd31de0975cd892163cbab1822065152c [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
J-Alves95df0ef2022-12-07 10:09:48 +00001396 /* The sender must match the caller. */
1397 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1398 vm_id_is_current_world(memory_region->sender)) ||
1399 (vm_id_is_current_world(from_locked.vm->id) &&
1400 memory_region->sender != from_locked.vm->id)) {
1401 dlog_verbose("Invalid memory sender ID.\n");
1402 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001403 }
1404
Andrew Walbrana65a1322020-04-06 19:32:32 +01001405 /*
1406 * Ensure that the composite header is within the memory bounds and
1407 * doesn't overlap the first part of the message.
1408 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001409 receivers_length = sizeof(struct ffa_memory_access) *
1410 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001411 constituents_offset =
1412 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001413 composite_memory_region_offset =
1414 memory_region->receivers[0].composite_memory_region_offset;
1415 if ((composite_memory_region_offset == 0) ||
1416 (composite_memory_region_offset <
1417 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001418 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001419 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001420 "Invalid composite memory region descriptor offset "
1421 "%d.\n",
1422 memory_region->receivers[0]
1423 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 }
1426
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428
1429 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001430 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001431 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001432 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1433 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001434 if (memory_share_length != constituents_offset + constituents_length) {
1435 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001436 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 memory_region->receivers[0]
1438 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001440 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001441 if (fragment_length < memory_share_length &&
1442 fragment_length < HF_MAILBOX_SIZE) {
1443 dlog_warning(
1444 "Initial fragment length %d smaller than mailbox "
1445 "size.\n",
1446 fragment_length);
1447 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448
Andrew Walbrana65a1322020-04-06 19:32:32 +01001449 /*
1450 * Clear is not allowed for memory sharing, as the sender still has
1451 * access to the memory.
1452 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001453 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1454 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001455 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001456 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 }
1458
1459 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001460 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001461 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001463 }
1464
J-Alves363f5722022-04-25 17:37:37 +01001465 /* Check that the permissions are valid, for each specified receiver. */
1466 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1467 ffa_memory_access_permissions_t permissions =
1468 memory_region->receivers[i]
1469 .receiver_permissions.permissions;
1470 ffa_vm_id_t receiver_id =
1471 memory_region->receivers[i]
1472 .receiver_permissions.receiver;
1473
1474 if (memory_region->sender == receiver_id) {
1475 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001477 }
Federico Recanati85090c42021-12-15 13:17:54 +01001478
J-Alves363f5722022-04-25 17:37:37 +01001479 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1480 j++) {
1481 if (receiver_id ==
1482 memory_region->receivers[j]
1483 .receiver_permissions.receiver) {
1484 dlog_verbose(
1485 "Repeated receiver(%x) in memory send "
1486 "operation.\n",
1487 memory_region->receivers[j]
1488 .receiver_permissions.receiver);
1489 return ffa_error(FFA_INVALID_PARAMETERS);
1490 }
1491 }
1492
1493 if (composite_memory_region_offset !=
1494 memory_region->receivers[i]
1495 .composite_memory_region_offset) {
1496 dlog_verbose(
1497 "All ffa_memory_access should point to the "
1498 "same composite memory region offset.\n");
1499 return ffa_error(FFA_INVALID_PARAMETERS);
1500 }
1501
1502 data_access = ffa_get_data_access_attr(permissions);
1503 instruction_access =
1504 ffa_get_instruction_access_attr(permissions);
1505 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1506 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1507 dlog_verbose(
1508 "Reserved value for receiver permissions "
1509 "%#x.\n",
1510 permissions);
1511 return ffa_error(FFA_INVALID_PARAMETERS);
1512 }
1513 if (instruction_access !=
1514 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1515 dlog_verbose(
1516 "Invalid instruction access permissions %#x "
1517 "for sending memory.\n",
1518 permissions);
1519 return ffa_error(FFA_INVALID_PARAMETERS);
1520 }
1521 if (share_func == FFA_MEM_SHARE_32) {
1522 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1523 dlog_verbose(
1524 "Invalid data access permissions %#x "
1525 "for sharing memory.\n",
1526 permissions);
1527 return ffa_error(FFA_INVALID_PARAMETERS);
1528 }
1529 /*
1530 * According to section 10.10.3 of the FF-A v1.1 EAC0
1531 * spec, NX is required for share operations (but must
1532 * not be specified by the sender) so set it in the
1533 * copy that we store, ready to be returned to the
1534 * retriever.
1535 */
1536 ffa_set_instruction_access_attr(
1537 &permissions, FFA_INSTRUCTION_ACCESS_NX);
1538 memory_region->receivers[i]
1539 .receiver_permissions.permissions = permissions;
1540 }
1541 if (share_func == FFA_MEM_LEND_32 &&
1542 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1543 dlog_verbose(
1544 "Invalid data access permissions %#x for "
1545 "lending memory.\n",
1546 permissions);
1547 return ffa_error(FFA_INVALID_PARAMETERS);
1548 }
1549
1550 if (share_func == FFA_MEM_DONATE_32 &&
1551 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1552 dlog_verbose(
1553 "Invalid data access permissions %#x for "
1554 "donating memory.\n",
1555 permissions);
1556 return ffa_error(FFA_INVALID_PARAMETERS);
1557 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001558 }
1559
Federico Recanatid937f5e2021-12-20 17:38:23 +01001560 /*
J-Alves807794e2022-06-16 13:42:47 +01001561 * If a memory donate or lend with single borrower, the memory type
1562 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001563 */
J-Alves807794e2022-06-16 13:42:47 +01001564 if (share_func == FFA_MEM_DONATE_32 ||
1565 (share_func == FFA_MEM_LEND_32 &&
1566 memory_region->receiver_count == 1)) {
1567 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1568 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1569 dlog_verbose(
1570 "Memory type shall not be specified by "
1571 "sender.\n");
1572 return ffa_error(FFA_INVALID_PARAMETERS);
1573 }
1574 } else {
1575 /*
1576 * Check that sender's memory attributes match Hafnium
1577 * expectations: Normal Memory, Inner shareable, Write-Back
1578 * Read-Allocate Write-Allocate Cacheable.
1579 */
1580 ret = ffa_memory_attributes_validate(memory_region->attributes);
1581 if (ret.func != FFA_SUCCESS_32) {
1582 return ret;
1583 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001584 }
1585
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001586 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001587}
1588
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001589/** Forwards a memory send message on to the TEE. */
1590static struct ffa_value memory_send_tee_forward(
1591 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1592 uint32_t share_func, struct ffa_memory_region *memory_region,
1593 uint32_t memory_share_length, uint32_t fragment_length)
1594{
1595 struct ffa_value ret;
1596
1597 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1598 memory_region, fragment_length);
1599 tee_locked.vm->mailbox.recv_size = fragment_length;
1600 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1601 tee_locked.vm->mailbox.recv_func = share_func;
1602 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001603 ret = arch_other_world_call(
1604 (struct ffa_value){.func = share_func,
1605 .arg1 = memory_share_length,
1606 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001607 /*
1608 * After the call to the TEE completes it must have finished reading its
1609 * RX buffer, so it is ready for another message.
1610 */
1611 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1612
1613 return ret;
1614}
1615
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001617 * Gets the share state for continuing an operation to donate, lend or share
1618 * memory, and checks that it is a valid request.
1619 *
1620 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1621 * not.
1622 */
1623static struct ffa_value ffa_memory_send_continue_validate(
1624 struct share_states_locked share_states, ffa_memory_handle_t handle,
1625 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1626 struct mpool *page_pool)
1627{
1628 struct ffa_memory_share_state *share_state;
1629 struct ffa_memory_region *memory_region;
1630
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001631 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001632
1633 /*
1634 * Look up the share state by handle and make sure that the VM ID
1635 * matches.
1636 */
1637 if (!get_share_state(share_states, handle, &share_state)) {
1638 dlog_verbose(
1639 "Invalid handle %#x for memory send continuation.\n",
1640 handle);
1641 return ffa_error(FFA_INVALID_PARAMETERS);
1642 }
1643 memory_region = share_state->memory_region;
1644
1645 if (memory_region->sender != from_vm_id) {
1646 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1647 return ffa_error(FFA_INVALID_PARAMETERS);
1648 }
1649
1650 if (share_state->sending_complete) {
1651 dlog_verbose(
1652 "Sending of memory handle %#x is already complete.\n",
1653 handle);
1654 return ffa_error(FFA_INVALID_PARAMETERS);
1655 }
1656
1657 if (share_state->fragment_count == MAX_FRAGMENTS) {
1658 /*
1659 * Log a warning as this is a sign that MAX_FRAGMENTS should
1660 * probably be increased.
1661 */
1662 dlog_warning(
1663 "Too many fragments for memory share with handle %#x; "
1664 "only %d supported.\n",
1665 handle, MAX_FRAGMENTS);
1666 /* Free share state, as it's not possible to complete it. */
1667 share_state_free(share_states, share_state, page_pool);
1668 return ffa_error(FFA_NO_MEMORY);
1669 }
1670
1671 *share_state_ret = share_state;
1672
1673 return (struct ffa_value){.func = FFA_SUCCESS_32};
1674}
1675
1676/**
1677 * Forwards a memory send continuation message on to the TEE.
1678 */
1679static struct ffa_value memory_send_continue_tee_forward(
1680 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1681 uint32_t fragment_length, ffa_memory_handle_t handle)
1682{
1683 struct ffa_value ret;
1684
1685 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1686 fragment_length);
1687 tee_locked.vm->mailbox.recv_size = fragment_length;
1688 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1689 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1690 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001691 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001692 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1693 .arg1 = (uint32_t)handle,
1694 .arg2 = (uint32_t)(handle >> 32),
1695 .arg3 = fragment_length,
1696 .arg4 = (uint64_t)sender_vm_id << 16});
1697 /*
1698 * After the call to the TEE completes it must have finished reading its
1699 * RX buffer, so it is ready for another message.
1700 */
1701 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1702
1703 return ret;
1704}
1705
1706/**
J-Alves95df0ef2022-12-07 10:09:48 +00001707 * Checks if there is at least one receiver from the other world.
1708 */
1709static bool memory_region_receivers_from_other_world(
1710 struct ffa_memory_region *memory_region)
1711{
1712 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1713 ffa_vm_id_t receiver = memory_region->receivers[i]
1714 .receiver_permissions.receiver;
1715 if (!vm_id_is_current_world(receiver)) {
1716 return true;
1717 }
1718 }
1719 return false;
1720}
1721
1722/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001723 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1724 * updates the stage-2 page tables. Specifically, check if the message length
1725 * and number of memory region constituents match, and if the transition is
1726 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001727 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001728 * Assumes that the caller has already found and locked the sender VM and copied
1729 * the memory region descriptor from the sender's TX buffer to a freshly
1730 * allocated page from Hafnium's internal pool. The caller must have also
1731 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001732 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001733 * This function takes ownership of the `memory_region` passed in and will free
1734 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001735 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001736struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001737 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001738 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001739 uint32_t fragment_length, uint32_t share_func,
1740 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001741{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001742 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001743 struct share_states_locked share_states;
1744 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001745
1746 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001747 * If there is an error validating the `memory_region` then we need to
1748 * free it because we own it but we won't be storing it in a share state
1749 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001750 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001751 ret = ffa_memory_send_validate(from_locked, memory_region,
1752 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001753 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001754 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001755 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001756 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001757 }
1758
Andrew Walbrana65a1322020-04-06 19:32:32 +01001759 /* Set flag for share function, ready to be retrieved later. */
1760 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001761 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001762 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001763 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001764 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001765 case FFA_MEM_LEND_32:
1766 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001767 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001768 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001769 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001770 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001771 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001772 }
1773
Andrew Walbranca808b12020-05-15 17:22:28 +01001774 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001775 /*
1776 * Allocate a share state before updating the page table. Otherwise if
1777 * updating the page table succeeded but allocating the share state
1778 * failed then it would leave the memory in a state where nobody could
1779 * get it back.
1780 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001781 if (!allocate_share_state(share_states, share_func, memory_region,
1782 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1783 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001784 dlog_verbose("Failed to allocate share state.\n");
1785 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001786 ret = ffa_error(FFA_NO_MEMORY);
1787 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001788 }
1789
Andrew Walbranca808b12020-05-15 17:22:28 +01001790 if (fragment_length == memory_share_length) {
1791 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001792 ret = ffa_memory_send_complete(
1793 from_locked, share_states, share_state, page_pool,
1794 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001795 } else {
1796 ret = (struct ffa_value){
1797 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001798 .arg1 = (uint32_t)memory_region->handle,
1799 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001800 .arg3 = fragment_length};
1801 }
1802
1803out:
1804 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001805 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001806 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001807}
1808
1809/**
1810 * Validates a call to donate, lend or share memory to the TEE and then updates
1811 * the stage-2 page tables. Specifically, check if the message length and number
1812 * of memory region constituents match, and if the transition is valid for the
1813 * type of memory sending operation.
1814 *
1815 * Assumes that the caller has already found and locked the sender VM and the
1816 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1817 * to a freshly allocated page from Hafnium's internal pool. The caller must
1818 * have also validated that the receiver VM ID is valid.
1819 *
1820 * This function takes ownership of the `memory_region` passed in and will free
1821 * it when necessary; it must not be freed by the caller.
1822 */
1823struct ffa_value ffa_memory_tee_send(
1824 struct vm_locked from_locked, struct vm_locked to_locked,
1825 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1826 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1827{
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001828 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001829
1830 /*
1831 * If there is an error validating the `memory_region` then we need to
1832 * free it because we own it but we won't be storing it in a share state
1833 * after all.
1834 */
1835 ret = ffa_memory_send_validate(from_locked, memory_region,
1836 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001837 share_func);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001838 if (ret.func != FFA_SUCCESS_32) {
1839 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001840 }
1841
Andrew Walbranca808b12020-05-15 17:22:28 +01001842 if (fragment_length == memory_share_length) {
1843 /* No more fragments to come, everything fit in one message. */
1844 struct ffa_composite_memory_region *composite =
1845 ffa_memory_region_get_composite(memory_region, 0);
1846 struct ffa_memory_region_constituent *constituents =
1847 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001848 struct mpool local_page_pool;
1849 uint32_t orig_from_mode;
1850
1851 /*
1852 * Use a local page pool so that we can roll back if necessary.
1853 */
1854 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001855
1856 ret = ffa_send_check_update(
1857 from_locked, &constituents,
1858 &composite->constituent_count, 1, share_func,
J-Alves363f5722022-04-25 17:37:37 +01001859 memory_region->receivers, memory_region->receiver_count,
1860 &local_page_pool,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001861 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1862 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001863 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001864 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001865 goto out;
1866 }
1867
1868 /* Forward memory send message on to TEE. */
1869 ret = memory_send_tee_forward(
1870 to_locked, from_locked.vm->id, share_func,
1871 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001872
1873 if (ret.func != FFA_SUCCESS_32) {
1874 dlog_verbose(
1875 "TEE didn't successfully complete memory send "
1876 "operation; returned %#x (%d). Rolling back.\n",
1877 ret.func, ret.arg2);
1878
1879 /*
1880 * The TEE failed to complete the send operation, so
1881 * roll back the page table update for the VM. This
1882 * can't fail because it won't try to allocate more
1883 * memory than was freed into the `local_page_pool` by
1884 * `ffa_send_check_update` in the initial update.
1885 */
1886 CHECK(ffa_region_group_identity_map(
1887 from_locked, &constituents,
1888 &composite->constituent_count, 1,
1889 orig_from_mode, &local_page_pool, true));
1890 }
1891
1892 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001893 } else {
1894 struct share_states_locked share_states = share_states_lock();
1895 ffa_memory_handle_t handle;
1896
1897 /*
1898 * We need to wait for the rest of the fragments before we can
1899 * check whether the transaction is valid and unmap the memory.
1900 * Call the TEE so it can do its initial validation and assign a
1901 * handle, and allocate a share state to keep what we have so
1902 * far.
1903 */
1904 ret = memory_send_tee_forward(
1905 to_locked, from_locked.vm->id, share_func,
1906 memory_region, memory_share_length, fragment_length);
1907 if (ret.func == FFA_ERROR_32) {
1908 goto out_unlock;
1909 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1910 dlog_warning(
1911 "Got %#x from TEE in response to %#x for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001912 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001913 "FFA_MEM_FRAG_RX.\n",
1914 ret.func, share_func, fragment_length,
1915 memory_share_length);
1916 ret = ffa_error(FFA_INVALID_PARAMETERS);
1917 goto out_unlock;
1918 }
1919 handle = ffa_frag_handle(ret);
1920 if (ret.arg3 != fragment_length) {
1921 dlog_warning(
1922 "Got unexpected fragment offset %d for "
1923 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1924 ret.arg3, fragment_length);
1925 ret = ffa_error(FFA_INVALID_PARAMETERS);
1926 goto out_unlock;
1927 }
1928 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1929 dlog_warning(
1930 "Got unexpected sender ID %d for "
1931 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1932 ffa_frag_sender(ret), from_locked.vm->id);
1933 ret = ffa_error(FFA_INVALID_PARAMETERS);
1934 goto out_unlock;
1935 }
1936
1937 if (!allocate_share_state(share_states, share_func,
1938 memory_region, fragment_length,
1939 handle, NULL)) {
1940 dlog_verbose("Failed to allocate share state.\n");
1941 ret = ffa_error(FFA_NO_MEMORY);
1942 goto out_unlock;
1943 }
1944 /*
1945 * Don't free the memory region fragment, as it has been stored
1946 * in the share state.
1947 */
1948 memory_region = NULL;
1949 out_unlock:
1950 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001951 }
1952
Andrew Walbranca808b12020-05-15 17:22:28 +01001953out:
1954 if (memory_region != NULL) {
1955 mpool_free(page_pool, memory_region);
1956 }
1957 dump_share_states();
1958 return ret;
1959}
1960
1961/**
1962 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1963 * this is the last fragment then checks that the transition is valid for the
1964 * type of memory sending operation and updates the stage-2 page tables of the
1965 * sender.
1966 *
1967 * Assumes that the caller has already found and locked the sender VM and copied
1968 * the memory region descriptor from the sender's TX buffer to a freshly
1969 * allocated page from Hafnium's internal pool.
1970 *
1971 * This function takes ownership of the `fragment` passed in; it must not be
1972 * freed by the caller.
1973 */
1974struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1975 void *fragment,
1976 uint32_t fragment_length,
1977 ffa_memory_handle_t handle,
1978 struct mpool *page_pool)
1979{
1980 struct share_states_locked share_states = share_states_lock();
1981 struct ffa_memory_share_state *share_state;
1982 struct ffa_value ret;
1983 struct ffa_memory_region *memory_region;
1984
1985 ret = ffa_memory_send_continue_validate(share_states, handle,
1986 &share_state,
1987 from_locked.vm->id, page_pool);
1988 if (ret.func != FFA_SUCCESS_32) {
1989 goto out_free_fragment;
1990 }
1991 memory_region = share_state->memory_region;
1992
J-Alves95df0ef2022-12-07 10:09:48 +00001993 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001994 dlog_error(
1995 "Got hypervisor-allocated handle for memory send to "
1996 "TEE. This should never happen, and indicates a bug in "
1997 "EL3 code.\n");
1998 ret = ffa_error(FFA_INVALID_PARAMETERS);
1999 goto out_free_fragment;
2000 }
2001
2002 /* Add this fragment. */
2003 share_state->fragments[share_state->fragment_count] = fragment;
2004 share_state->fragment_constituent_counts[share_state->fragment_count] =
2005 fragment_length / sizeof(struct ffa_memory_region_constituent);
2006 share_state->fragment_count++;
2007
2008 /* Check whether the memory send operation is now ready to complete. */
2009 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002010 ret = ffa_memory_send_complete(
2011 from_locked, share_states, share_state, page_pool,
2012 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002013 } else {
2014 ret = (struct ffa_value){
2015 .func = FFA_MEM_FRAG_RX_32,
2016 .arg1 = (uint32_t)handle,
2017 .arg2 = (uint32_t)(handle >> 32),
2018 .arg3 = share_state_next_fragment_offset(share_states,
2019 share_state)};
2020 }
2021 goto out;
2022
2023out_free_fragment:
2024 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002025
2026out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002027 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002028 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002029}
2030
Andrew Walbranca808b12020-05-15 17:22:28 +01002031/**
2032 * Continues an operation to donate, lend or share memory to the TEE VM. If this
2033 * is the last fragment then checks that the transition is valid for the type of
2034 * memory sending operation and updates the stage-2 page tables of the sender.
2035 *
2036 * Assumes that the caller has already found and locked the sender VM and copied
2037 * the memory region descriptor from the sender's TX buffer to a freshly
2038 * allocated page from Hafnium's internal pool.
2039 *
2040 * This function takes ownership of the `memory_region` passed in and will free
2041 * it when necessary; it must not be freed by the caller.
2042 */
2043struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
2044 struct vm_locked to_locked,
2045 void *fragment,
2046 uint32_t fragment_length,
2047 ffa_memory_handle_t handle,
2048 struct mpool *page_pool)
2049{
2050 struct share_states_locked share_states = share_states_lock();
2051 struct ffa_memory_share_state *share_state;
2052 struct ffa_value ret;
2053 struct ffa_memory_region *memory_region;
2054
2055 ret = ffa_memory_send_continue_validate(share_states, handle,
2056 &share_state,
2057 from_locked.vm->id, page_pool);
2058 if (ret.func != FFA_SUCCESS_32) {
2059 goto out_free_fragment;
2060 }
2061 memory_region = share_state->memory_region;
2062
J-Alves95df0ef2022-12-07 10:09:48 +00002063 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002064 dlog_error(
2065 "Got SPM-allocated handle for memory send to non-TEE "
2066 "VM. This should never happen, and indicates a bug.\n");
2067 ret = ffa_error(FFA_INVALID_PARAMETERS);
2068 goto out_free_fragment;
2069 }
2070
2071 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2072 to_locked.vm->mailbox.recv == NULL) {
2073 /*
2074 * If the TEE RX buffer is not available, tell the sender to
2075 * retry by returning the current offset again.
2076 */
2077 ret = (struct ffa_value){
2078 .func = FFA_MEM_FRAG_RX_32,
2079 .arg1 = (uint32_t)handle,
2080 .arg2 = (uint32_t)(handle >> 32),
2081 .arg3 = share_state_next_fragment_offset(share_states,
2082 share_state),
2083 };
2084 goto out_free_fragment;
2085 }
2086
2087 /* Add this fragment. */
2088 share_state->fragments[share_state->fragment_count] = fragment;
2089 share_state->fragment_constituent_counts[share_state->fragment_count] =
2090 fragment_length / sizeof(struct ffa_memory_region_constituent);
2091 share_state->fragment_count++;
2092
2093 /* Check whether the memory send operation is now ready to complete. */
2094 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002095 struct mpool local_page_pool;
2096 uint32_t orig_from_mode;
2097
2098 /*
2099 * Use a local page pool so that we can roll back if necessary.
2100 */
2101 mpool_init_with_fallback(&local_page_pool, page_pool);
2102
Andrew Walbranca808b12020-05-15 17:22:28 +01002103 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002104 share_state, &local_page_pool,
2105 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002106
2107 if (ret.func == FFA_SUCCESS_32) {
2108 /*
2109 * Forward final fragment on to the TEE so that
2110 * it can complete the memory sending operation.
2111 */
2112 ret = memory_send_continue_tee_forward(
2113 to_locked, from_locked.vm->id, fragment,
2114 fragment_length, handle);
2115
2116 if (ret.func != FFA_SUCCESS_32) {
2117 /*
2118 * The error will be passed on to the caller,
2119 * but log it here too.
2120 */
2121 dlog_verbose(
2122 "TEE didn't successfully complete "
2123 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002124 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002125 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002126
2127 /*
2128 * The TEE failed to complete the send
2129 * operation, so roll back the page table update
2130 * for the VM. This can't fail because it won't
2131 * try to allocate more memory than was freed
2132 * into the `local_page_pool` by
2133 * `ffa_send_check_update` in the initial
2134 * update.
2135 */
2136 CHECK(ffa_region_group_identity_map(
2137 from_locked, share_state->fragments,
2138 share_state
2139 ->fragment_constituent_counts,
2140 share_state->fragment_count,
2141 orig_from_mode, &local_page_pool,
2142 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002143 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002144
Andrew Walbranca808b12020-05-15 17:22:28 +01002145 /* Free share state. */
2146 share_state_free(share_states, share_state, page_pool);
2147 } else {
2148 /* Abort sending to TEE. */
2149 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002150 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002151 .func = FFA_MEM_RECLAIM_32,
2152 .arg1 = (uint32_t)handle,
2153 .arg2 = (uint32_t)(handle >> 32)});
2154
2155 if (tee_ret.func != FFA_SUCCESS_32) {
2156 /*
2157 * Nothing we can do if TEE doesn't abort
2158 * properly, just log it.
2159 */
2160 dlog_verbose(
2161 "TEE didn't successfully abort failed "
2162 "memory send operation; returned %#x "
2163 "(%d).\n",
2164 tee_ret.func, tee_ret.arg2);
2165 }
2166 /*
2167 * We don't need to free the share state in this case
2168 * because ffa_memory_send_complete does that already.
2169 */
2170 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002171
2172 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002173 } else {
2174 uint32_t next_fragment_offset =
2175 share_state_next_fragment_offset(share_states,
2176 share_state);
2177
2178 ret = memory_send_continue_tee_forward(
2179 to_locked, from_locked.vm->id, fragment,
2180 fragment_length, handle);
2181
2182 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2183 ffa_frag_handle(ret) != handle ||
2184 ret.arg3 != next_fragment_offset ||
2185 ffa_frag_sender(ret) != from_locked.vm->id) {
2186 dlog_verbose(
2187 "Got unexpected result from forwarding "
2188 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2189 "offset %d, sender %d); expected "
2190 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2191 "sender %d).\n",
2192 ret.func, ffa_frag_handle(ret), ret.arg3,
2193 ffa_frag_sender(ret), handle,
2194 next_fragment_offset, from_locked.vm->id);
2195 /* Free share state. */
2196 share_state_free(share_states, share_state, page_pool);
2197 ret = ffa_error(FFA_INVALID_PARAMETERS);
2198 goto out;
2199 }
2200
2201 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2202 .arg1 = (uint32_t)handle,
2203 .arg2 = (uint32_t)(handle >> 32),
2204 .arg3 = next_fragment_offset};
2205 }
2206 goto out;
2207
2208out_free_fragment:
2209 mpool_free(page_pool, fragment);
2210
2211out:
2212 share_states_unlock(&share_states);
2213 return ret;
2214}
2215
2216/** Clean up after the receiver has finished retrieving a memory region. */
2217static void ffa_memory_retrieve_complete(
2218 struct share_states_locked share_states,
2219 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2220{
2221 if (share_state->share_func == FFA_MEM_DONATE_32) {
2222 /*
2223 * Memory that has been donated can't be relinquished,
2224 * so no need to keep the share state around.
2225 */
2226 share_state_free(share_states, share_state, page_pool);
2227 dlog_verbose("Freed share state for donate.\n");
2228 }
2229}
2230
J-Alves96de29f2022-04-26 16:05:24 +01002231/*
2232 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2233 * returns its index in the receiver's array. If receiver's ID doesn't exist
2234 * in the array, return the region's 'receiver_count'.
2235 */
2236static uint32_t ffa_memory_region_get_receiver(
2237 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
2238{
2239 struct ffa_memory_access *receivers;
2240 uint32_t i;
2241
2242 assert(memory_region != NULL);
2243
2244 receivers = memory_region->receivers;
2245
2246 for (i = 0U; i < memory_region->receiver_count; i++) {
2247 if (receivers[i].receiver_permissions.receiver == receiver) {
2248 break;
2249 }
2250 }
2251
2252 return i;
2253}
2254
2255/**
2256 * Validates the retrieved permissions against those specified by the lender
2257 * of memory share operation. Optionally can help set the permissions to be used
2258 * for the S2 mapping, through the `permissions` argument.
2259 * Returns true if permissions are valid, false otherwise.
2260 */
2261static bool ffa_memory_retrieve_is_memory_access_valid(
2262 enum ffa_data_access sent_data_access,
2263 enum ffa_data_access requested_data_access,
2264 enum ffa_instruction_access sent_instruction_access,
2265 enum ffa_instruction_access requested_instruction_access,
2266 ffa_memory_access_permissions_t *permissions)
2267{
2268 switch (sent_data_access) {
2269 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2270 case FFA_DATA_ACCESS_RW:
2271 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2272 requested_data_access == FFA_DATA_ACCESS_RW) {
2273 if (permissions != NULL) {
2274 ffa_set_data_access_attr(permissions,
2275 FFA_DATA_ACCESS_RW);
2276 }
2277 break;
2278 }
2279 /* Intentional fall-through. */
2280 case FFA_DATA_ACCESS_RO:
2281 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2282 requested_data_access == FFA_DATA_ACCESS_RO) {
2283 if (permissions != NULL) {
2284 ffa_set_data_access_attr(permissions,
2285 FFA_DATA_ACCESS_RO);
2286 }
2287 break;
2288 }
2289 dlog_verbose(
2290 "Invalid data access requested; sender specified "
2291 "permissions %#x but receiver requested %#x.\n",
2292 sent_data_access, requested_data_access);
2293 return false;
2294 case FFA_DATA_ACCESS_RESERVED:
2295 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2296 "checked before this point.");
2297 }
2298
2299 switch (sent_instruction_access) {
2300 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2301 case FFA_INSTRUCTION_ACCESS_X:
2302 if (requested_instruction_access ==
2303 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2304 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2305 if (permissions != NULL) {
2306 ffa_set_instruction_access_attr(
2307 permissions, FFA_INSTRUCTION_ACCESS_X);
2308 }
2309 break;
2310 }
2311 case FFA_INSTRUCTION_ACCESS_NX:
2312 if (requested_instruction_access ==
2313 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2314 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2315 if (permissions != NULL) {
2316 ffa_set_instruction_access_attr(
2317 permissions, FFA_INSTRUCTION_ACCESS_NX);
2318 }
2319 break;
2320 }
2321 dlog_verbose(
2322 "Invalid instruction access requested; sender "
2323 "specified permissions %#x but receiver requested "
2324 "%#x.\n",
2325 sent_instruction_access, requested_instruction_access);
2326 return false;
2327 case FFA_INSTRUCTION_ACCESS_RESERVED:
2328 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2329 "be checked before this point.");
2330 }
2331
2332 return true;
2333}
2334
2335/**
2336 * Validate the receivers' permissions in the retrieve request against those
2337 * specified by the lender.
2338 * In the `permissions` argument returns the permissions to set at S2 for the
2339 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2340 * Returns FFA_SUCCESS if all specified permissions are valid.
2341 */
2342static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2343 struct ffa_memory_region *memory_region,
2344 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2345 ffa_memory_access_permissions_t *permissions)
2346{
2347 uint32_t retrieve_receiver_index;
2348
2349 assert(permissions != NULL);
2350
2351 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2352 dlog_verbose(
2353 "Retrieve request should contain same list of "
2354 "borrowers, as specified by the lender.\n");
2355 return ffa_error(FFA_INVALID_PARAMETERS);
2356 }
2357
2358 retrieve_receiver_index = retrieve_request->receiver_count;
2359
2360 /* Should be populated with the permissions of the retriever. */
2361 *permissions = 0;
2362
2363 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2364 ffa_memory_access_permissions_t sent_permissions;
2365 struct ffa_memory_access *current_receiver =
2366 &retrieve_request->receivers[i];
2367 ffa_memory_access_permissions_t requested_permissions =
2368 current_receiver->receiver_permissions.permissions;
2369 ffa_vm_id_t current_receiver_id =
2370 current_receiver->receiver_permissions.receiver;
2371 bool found_to_id = current_receiver_id == to_vm_id;
2372
2373 /*
2374 * Find the current receiver in the transaction descriptor from
2375 * sender.
2376 */
2377 uint32_t mem_region_receiver_index =
2378 ffa_memory_region_get_receiver(memory_region,
2379 current_receiver_id);
2380
2381 if (mem_region_receiver_index ==
2382 memory_region->receiver_count) {
2383 dlog_verbose("%s: receiver %x not found\n", __func__,
2384 current_receiver_id);
2385 return ffa_error(FFA_DENIED);
2386 }
2387
2388 sent_permissions =
2389 memory_region->receivers[mem_region_receiver_index]
2390 .receiver_permissions.permissions;
2391
2392 if (found_to_id) {
2393 retrieve_receiver_index = i;
2394 }
2395
2396 /*
2397 * Since we are traversing the list of receivers, save the index
2398 * of the caller. As it needs to be there.
2399 */
2400
2401 if (current_receiver->composite_memory_region_offset != 0U) {
2402 dlog_verbose(
2403 "Retriever specified address ranges not "
2404 "supported (got offset %d).\n",
2405 current_receiver
2406 ->composite_memory_region_offset);
2407 return ffa_error(FFA_INVALID_PARAMETERS);
2408 }
2409
2410 /*
2411 * Check permissions from sender against permissions requested
2412 * by receiver.
2413 */
2414 if (!ffa_memory_retrieve_is_memory_access_valid(
2415 ffa_get_data_access_attr(sent_permissions),
2416 ffa_get_data_access_attr(requested_permissions),
2417 ffa_get_instruction_access_attr(sent_permissions),
2418 ffa_get_instruction_access_attr(
2419 requested_permissions),
2420 found_to_id ? permissions : NULL)) {
2421 return ffa_error(FFA_DENIED);
2422 }
2423
2424 /*
2425 * Can't request PM to clear memory if only provided with RO
2426 * permissions.
2427 */
2428 if (found_to_id &&
2429 (ffa_get_data_access_attr(*permissions) ==
2430 FFA_DATA_ACCESS_RO) &&
2431 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2432 0U) {
2433 dlog_verbose(
2434 "Receiver has RO permissions can not request "
2435 "clear.\n");
2436 return ffa_error(FFA_DENIED);
2437 }
2438 }
2439
2440 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2441 dlog_verbose(
2442 "Retrieve request does not contain caller's (%x) "
2443 "permissions\n",
2444 to_vm_id);
2445 return ffa_error(FFA_INVALID_PARAMETERS);
2446 }
2447
2448 return (struct ffa_value){.func = FFA_SUCCESS_32};
2449}
2450
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002451struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2452 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002453 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002454 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002455{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002456 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002457 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002458 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002459 sizeof(struct ffa_memory_access);
2460 ffa_memory_handle_t handle = retrieve_request->handle;
2461 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002462 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2464 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002467 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002468 struct ffa_memory_share_state *share_state;
2469 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002470 struct ffa_composite_memory_region *composite;
2471 uint32_t total_length;
2472 uint32_t fragment_length;
J-Alves96de29f2022-04-26 16:05:24 +01002473 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474
2475 dump_share_states();
2476
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002477 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002478 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002479 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002481 expected_retrieve_request_length,
2482 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002483 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002484 }
2485
2486 share_states = share_states_lock();
2487 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002488 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002489 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002490 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002491 goto out;
2492 }
2493
J-Alves96de29f2022-04-26 16:05:24 +01002494 if (!share_state->sending_complete) {
2495 dlog_verbose(
2496 "Memory with handle %#x not fully sent, can't "
2497 "retrieve.\n",
2498 handle);
2499 ret = ffa_error(FFA_INVALID_PARAMETERS);
2500 goto out;
2501 }
2502
Andrew Walbrana65a1322020-04-06 19:32:32 +01002503 memory_region = share_state->memory_region;
2504 CHECK(memory_region != NULL);
2505
2506 /*
J-Alves96de29f2022-04-26 16:05:24 +01002507 * Find receiver index in the receivers list specified by the sender.
2508 */
2509 receiver_index =
2510 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2511
2512 if (receiver_index == memory_region->receiver_count) {
2513 dlog_verbose(
2514 "Incorrect receiver VM ID %x for FFA_MEM_RETRIEVE_REQ, "
2515 "for handle %#x.\n",
2516 to_locked.vm->id, handle);
2517 ret = ffa_error(FFA_INVALID_PARAMETERS);
2518 goto out;
2519 }
2520
2521 if (share_state->retrieved_fragment_count[receiver_index] != 0U) {
2522 dlog_verbose("Memory with handle %#x already retrieved.\n",
2523 handle);
2524 ret = ffa_error(FFA_DENIED);
2525 goto out;
2526 }
2527
2528 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002529 * Check that the transaction type expected by the receiver is correct,
2530 * if it has been specified.
2531 */
2532 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002533 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002534 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002535 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002536 dlog_verbose(
2537 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002539 transaction_type,
2540 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002541 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544 goto out;
2545 }
2546
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002547 if (retrieve_request->sender != memory_region->sender) {
2548 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002549 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 "expected %d for handle %#x.\n",
2551 retrieve_request->sender, memory_region->sender,
2552 handle);
J-Alves040c4ef2022-05-13 14:42:49 +01002553 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554 goto out;
2555 }
2556
2557 if (retrieve_request->tag != memory_region->tag) {
2558 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 "%d for handle %#x.\n",
2561 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002562 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002563 goto out;
2564 }
2565
Federico Recanati85090c42021-12-15 13:17:54 +01002566 if ((retrieve_request->flags &
J-Alves96de29f2022-04-26 16:05:24 +01002567 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
Federico Recanati85090c42021-12-15 13:17:54 +01002568 dlog_verbose(
2569 "Retriever specified 'address range alignment hint'"
2570 " not supported.\n");
2571 ret = ffa_error(FFA_INVALID_PARAMETERS);
2572 goto out;
2573 }
2574 if ((retrieve_request->flags &
2575 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2576 dlog_verbose(
2577 "Bits 8-5 must be zero in memory region's flags "
2578 "(address range alignment hint not supported).\n");
2579 ret = ffa_error(FFA_INVALID_PARAMETERS);
2580 goto out;
2581 }
2582
J-Alves84658fc2021-06-17 14:37:32 +01002583 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2584 dlog_verbose(
2585 "Bits 31-10 must be zero in memory region's flags.\n");
2586 ret = ffa_error(FFA_INVALID_PARAMETERS);
2587 goto out;
2588 }
2589
2590 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2591 (retrieve_request->flags &
2592 (FFA_MEMORY_REGION_FLAG_CLEAR |
2593 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2594 dlog_verbose(
2595 "Memory Share operation can't clean after relinquish "
2596 "memory region.\n");
2597 ret = ffa_error(FFA_INVALID_PARAMETERS);
2598 goto out;
2599 }
2600
Andrew Walbrana65a1322020-04-06 19:32:32 +01002601 /*
J-Alves17c069c2021-12-07 16:00:38 +00002602 * If the borrower needs the memory to be cleared before mapping to its
2603 * address space, the sender should have set the flag when calling
2604 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2605 */
2606 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
J-Alves96de29f2022-04-26 16:05:24 +01002607 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
J-Alves17c069c2021-12-07 16:00:38 +00002608 dlog_verbose(
2609 "Borrower needs memory cleared. Sender needs to set "
2610 "flag for clearing memory.\n");
2611 ret = ffa_error(FFA_DENIED);
2612 goto out;
2613 }
2614
J-Alves96de29f2022-04-26 16:05:24 +01002615 ret = ffa_memory_retrieve_validate_memory_access_list(
2616 memory_region, retrieve_request, to_locked.vm->id,
2617 &permissions);
2618 if (ret.func != FFA_SUCCESS_32) {
J-Alves84658fc2021-06-17 14:37:32 +01002619 goto out;
2620 }
2621
J-Alves614d9f42022-06-28 14:03:10 +01002622 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2623 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2624 /*
2625 * Ensure receiver's attributes are compatible with how Hafnium
2626 * maps memory: Normal Memory, Inner shareable, Write-Back
2627 * Read-Allocate Write-Allocate Cacheable.
2628 */
2629 ret = ffa_memory_attributes_validate(
2630 retrieve_request->attributes);
2631 if (ret.func != FFA_SUCCESS_32) {
2632 goto out;
2633 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002634 }
2635
J-Alves7cd5eb32020-10-16 19:06:10 +01002636 memory_to_attributes = ffa_memory_permissions_to_mode(
2637 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002638 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002639 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002640 share_state->fragment_constituent_counts,
2641 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002642 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002643 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002644 goto out;
2645 }
2646
2647 /*
2648 * Copy response to RX buffer of caller and deliver the message. This
2649 * must be done before the share_state is (possibly) freed.
2650 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002651 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002652 composite = ffa_memory_region_get_composite(memory_region, 0);
2653 /*
2654 * Constituents which we received in the first fragment should always
2655 * fit in the first fragment we are sending, because the header is the
2656 * same size in both cases and we have a fixed message buffer size. So
2657 * `ffa_retrieved_memory_region_init` should never fail.
2658 */
2659 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002660 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2661 memory_region->sender, memory_region->attributes,
2662 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002663 composite->page_count, composite->constituent_count,
2664 share_state->fragments[0],
2665 share_state->fragment_constituent_counts[0], &total_length,
2666 &fragment_length));
2667 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002668 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002669 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002670 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2671
J-Alves96de29f2022-04-26 16:05:24 +01002672 share_state->retrieved_fragment_count[receiver_index] = 1;
2673 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002674 share_state->fragment_count) {
2675 ffa_memory_retrieve_complete(share_states, share_state,
2676 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002677 }
2678
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002679 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002680 .arg1 = total_length,
2681 .arg2 = fragment_length};
2682
2683out:
2684 share_states_unlock(&share_states);
2685 dump_share_states();
2686 return ret;
2687}
2688
2689struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2690 ffa_memory_handle_t handle,
2691 uint32_t fragment_offset,
2692 struct mpool *page_pool)
2693{
2694 struct ffa_memory_region *memory_region;
2695 struct share_states_locked share_states;
2696 struct ffa_memory_share_state *share_state;
2697 struct ffa_value ret;
2698 uint32_t fragment_index;
2699 uint32_t retrieved_constituents_count;
2700 uint32_t i;
2701 uint32_t expected_fragment_offset;
2702 uint32_t remaining_constituent_count;
2703 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002704 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002705
2706 dump_share_states();
2707
2708 share_states = share_states_lock();
2709 if (!get_share_state(share_states, handle, &share_state)) {
2710 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2711 handle);
2712 ret = ffa_error(FFA_INVALID_PARAMETERS);
2713 goto out;
2714 }
2715
2716 memory_region = share_state->memory_region;
2717 CHECK(memory_region != NULL);
2718
J-Alvesc7484f12022-05-13 12:41:14 +01002719 receiver_index =
2720 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2721
2722 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002723 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002724 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2725 "memory sharing transaction (%x)\n",
2726 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002727 ret = ffa_error(FFA_INVALID_PARAMETERS);
2728 goto out;
2729 }
2730
2731 if (!share_state->sending_complete) {
2732 dlog_verbose(
2733 "Memory with handle %#x not fully sent, can't "
2734 "retrieve.\n",
2735 handle);
2736 ret = ffa_error(FFA_INVALID_PARAMETERS);
2737 goto out;
2738 }
2739
J-Alvesc7484f12022-05-13 12:41:14 +01002740 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2741 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002742 share_state->fragment_count) {
2743 dlog_verbose(
2744 "Retrieval of memory with handle %#x not yet started "
2745 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002746 handle,
2747 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002748 share_state->fragment_count);
2749 ret = ffa_error(FFA_INVALID_PARAMETERS);
2750 goto out;
2751 }
2752
J-Alvesc7484f12022-05-13 12:41:14 +01002753 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002754
2755 /*
2756 * Check that the given fragment offset is correct by counting how many
2757 * constituents were in the fragments previously sent.
2758 */
2759 retrieved_constituents_count = 0;
2760 for (i = 0; i < fragment_index; ++i) {
2761 retrieved_constituents_count +=
2762 share_state->fragment_constituent_counts[i];
2763 }
J-Alvesc7484f12022-05-13 12:41:14 +01002764
2765 CHECK(memory_region->receiver_count > 0);
2766
Andrew Walbranca808b12020-05-15 17:22:28 +01002767 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002768 ffa_composite_constituent_offset(memory_region,
2769 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002770 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002771 sizeof(struct ffa_memory_region_constituent) -
2772 sizeof(struct ffa_memory_access) *
2773 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002774 if (fragment_offset != expected_fragment_offset) {
2775 dlog_verbose("Fragment offset was %d but expected %d.\n",
2776 fragment_offset, expected_fragment_offset);
2777 ret = ffa_error(FFA_INVALID_PARAMETERS);
2778 goto out;
2779 }
2780
2781 remaining_constituent_count = ffa_memory_fragment_init(
2782 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2783 share_state->fragments[fragment_index],
2784 share_state->fragment_constituent_counts[fragment_index],
2785 &fragment_length);
2786 CHECK(remaining_constituent_count == 0);
2787 to_locked.vm->mailbox.recv_size = fragment_length;
2788 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2789 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2790 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002791 share_state->retrieved_fragment_count[receiver_index]++;
2792 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002793 share_state->fragment_count) {
2794 ffa_memory_retrieve_complete(share_states, share_state,
2795 page_pool);
2796 }
2797
2798 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2799 .arg1 = (uint32_t)handle,
2800 .arg2 = (uint32_t)(handle >> 32),
2801 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002802
2803out:
2804 share_states_unlock(&share_states);
2805 dump_share_states();
2806 return ret;
2807}
2808
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002809struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002810 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002811 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002812{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002813 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002814 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002815 struct ffa_memory_share_state *share_state;
2816 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002817 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002818 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002819 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002820
Andrew Walbrana65a1322020-04-06 19:32:32 +01002821 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002822 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002823 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002824 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002825 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002826 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002827 }
2828
Andrew Walbrana65a1322020-04-06 19:32:32 +01002829 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002830 dlog_verbose(
2831 "VM ID %d in relinquish message doesn't match calling "
2832 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002833 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002834 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002835 }
2836
2837 dump_share_states();
2838
2839 share_states = share_states_lock();
2840 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002841 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002842 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002843 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002844 goto out;
2845 }
2846
Andrew Walbranca808b12020-05-15 17:22:28 +01002847 if (!share_state->sending_complete) {
2848 dlog_verbose(
2849 "Memory with handle %#x not fully sent, can't "
2850 "relinquish.\n",
2851 handle);
2852 ret = ffa_error(FFA_INVALID_PARAMETERS);
2853 goto out;
2854 }
2855
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002856 memory_region = share_state->memory_region;
2857 CHECK(memory_region != NULL);
2858
J-Alves8eb19162022-04-28 10:56:48 +01002859 receiver_index = ffa_memory_region_get_receiver(memory_region,
2860 from_locked.vm->id);
2861
2862 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002863 dlog_verbose(
2864 "VM ID %d tried to relinquish memory region with "
J-Alves8eb19162022-04-28 10:56:48 +01002865 "handle %#x and it is not a valid borrower.\n",
2866 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002867 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002868 goto out;
2869 }
2870
J-Alves8eb19162022-04-28 10:56:48 +01002871 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002872 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002873 dlog_verbose(
J-Alves8eb19162022-04-28 10:56:48 +01002874 "Memory with handle %#x not yet fully retrieved, "
2875 "receiver %x can't relinquish.\n",
2876 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002877 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002878 goto out;
2879 }
2880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002881 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002882
2883 /*
2884 * Clear is not allowed for memory that was shared, as the original
2885 * sender still has access to the memory.
2886 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002887 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002888 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002889 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002890 goto out;
2891 }
2892
Andrew Walbranca808b12020-05-15 17:22:28 +01002893 ret = ffa_relinquish_check_update(
2894 from_locked, share_state->fragments,
2895 share_state->fragment_constituent_counts,
2896 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002897
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002898 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002899 /*
2900 * Mark memory handle as not retrieved, so it can be reclaimed
2901 * (or retrieved again).
2902 */
J-Alves8eb19162022-04-28 10:56:48 +01002903 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002904 }
2905
2906out:
2907 share_states_unlock(&share_states);
2908 dump_share_states();
2909 return ret;
2910}
2911
2912/**
2913 * Validates that the reclaim transition is allowed for the given handle,
2914 * updates the page table of the reclaiming VM, and frees the internal state
2915 * associated with the handle.
2916 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002917struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002918 ffa_memory_handle_t handle,
2919 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002920 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002921{
2922 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002923 struct ffa_memory_share_state *share_state;
2924 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002925 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002926
2927 dump_share_states();
2928
2929 share_states = share_states_lock();
2930 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002931 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002932 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002933 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002934 goto out;
2935 }
2936
2937 memory_region = share_state->memory_region;
2938 CHECK(memory_region != NULL);
2939
2940 if (to_locked.vm->id != memory_region->sender) {
2941 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002942 "VM %#x attempted to reclaim memory handle %#x "
2943 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002944 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002945 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002946 goto out;
2947 }
2948
Andrew Walbranca808b12020-05-15 17:22:28 +01002949 if (!share_state->sending_complete) {
2950 dlog_verbose(
2951 "Memory with handle %#x not fully sent, can't "
2952 "reclaim.\n",
2953 handle);
2954 ret = ffa_error(FFA_INVALID_PARAMETERS);
2955 goto out;
2956 }
2957
J-Alves752236c2022-04-28 11:07:47 +01002958 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2959 if (share_state->retrieved_fragment_count[i] != 0) {
2960 dlog_verbose(
2961 "Tried to reclaim memory handle %#x that has "
2962 "not been relinquished by all borrowers(%x).\n",
2963 handle,
2964 memory_region->receivers[i]
2965 .receiver_permissions.receiver);
2966 ret = ffa_error(FFA_DENIED);
2967 goto out;
2968 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002969 }
2970
Andrew Walbranca808b12020-05-15 17:22:28 +01002971 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002972 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002973 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002974 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002975 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002976
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002977 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002978 share_state_free(share_states, share_state, page_pool);
2979 dlog_verbose("Freed share state after successful reclaim.\n");
2980 }
2981
2982out:
2983 share_states_unlock(&share_states);
2984 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002985}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002986
2987/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002988 * Validates that the reclaim transition is allowed for the memory region with
2989 * the given handle which was previously shared with the TEE, tells the TEE to
2990 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2991 *
2992 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002993 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002994struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002995 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002996 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002997 ffa_memory_region_flags_t flags,
2998 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002999{
Andrew Walbranca808b12020-05-15 17:22:28 +01003000 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
3001 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
3002 struct ffa_value tee_ret;
3003 uint32_t length;
3004 uint32_t fragment_length;
3005 uint32_t fragment_offset;
3006 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003007 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01003008 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
3009
3010 CHECK(request_length <= HF_MAILBOX_SIZE);
3011 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
3012
3013 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02003014 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003015 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
3016 .arg1 = request_length,
3017 .arg2 = request_length});
3018 if (tee_ret.func == FFA_ERROR_32) {
3019 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
3020 return tee_ret;
3021 }
3022 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
3023 dlog_verbose(
3024 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
3025 tee_ret.func);
3026 return ffa_error(FFA_INVALID_PARAMETERS);
3027 }
3028
3029 length = tee_ret.arg1;
3030 fragment_length = tee_ret.arg2;
3031
3032 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
3033 length > sizeof(tee_retrieve_buffer)) {
3034 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
3035 fragment_length, length, HF_MAILBOX_SIZE,
3036 sizeof(tee_retrieve_buffer));
3037 return ffa_error(FFA_INVALID_PARAMETERS);
3038 }
3039
3040 /*
3041 * Copy the first fragment of the memory region descriptor to an
3042 * internal buffer.
3043 */
3044 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
3045 from_locked.vm->mailbox.send, fragment_length);
3046
3047 /* Fetch the remaining fragments into the same buffer. */
3048 fragment_offset = fragment_length;
3049 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02003050 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003051 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
3052 .arg1 = (uint32_t)handle,
3053 .arg2 = (uint32_t)(handle >> 32),
3054 .arg3 = fragment_offset});
3055 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
3056 dlog_verbose(
3057 "Got %#x (%d) from TEE in response to "
3058 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
3059 tee_ret.func, tee_ret.arg2);
3060 return tee_ret;
3061 }
3062 if (ffa_frag_handle(tee_ret) != handle) {
3063 dlog_verbose(
3064 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
3065 "in response to FFA_MEM_FRAG_RX for handle "
3066 "%#x.\n",
3067 ffa_frag_handle(tee_ret), handle);
3068 return ffa_error(FFA_INVALID_PARAMETERS);
3069 }
3070 if (ffa_frag_sender(tee_ret) != 0) {
3071 dlog_verbose(
3072 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
3073 "(expected 0).\n",
3074 ffa_frag_sender(tee_ret));
3075 return ffa_error(FFA_INVALID_PARAMETERS);
3076 }
3077 fragment_length = tee_ret.arg3;
3078 if (fragment_length > HF_MAILBOX_SIZE ||
3079 fragment_offset + fragment_length > length) {
3080 dlog_verbose(
3081 "Invalid fragment length %d at offset %d (max "
3082 "%d).\n",
3083 fragment_length, fragment_offset,
3084 HF_MAILBOX_SIZE);
3085 return ffa_error(FFA_INVALID_PARAMETERS);
3086 }
3087 memcpy_s(tee_retrieve_buffer + fragment_offset,
3088 sizeof(tee_retrieve_buffer) - fragment_offset,
3089 from_locked.vm->mailbox.send, fragment_length);
3090
3091 fragment_offset += fragment_length;
3092 }
3093
3094 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00003095
3096 if (memory_region->receiver_count != 1) {
3097 /* Only one receiver supported by Hafnium for now. */
3098 dlog_verbose(
3099 "Multiple recipients not supported (got %d, expected "
3100 "1).\n",
3101 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01003102 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003103 }
3104
3105 if (memory_region->handle != handle) {
3106 dlog_verbose(
3107 "Got memory region handle %#x from TEE but requested "
3108 "handle %#x.\n",
3109 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003110 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003111 }
3112
3113 /* The original sender must match the caller. */
3114 if (to_locked.vm->id != memory_region->sender) {
3115 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003116 "VM %#x attempted to reclaim memory handle %#x "
3117 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003118 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003119 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003120 }
3121
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003122 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003123
3124 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01003125 * Validate that the reclaim transition is allowed for the given memory
3126 * region, forward the request to the TEE and then map the memory back
3127 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003128 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01003129 return ffa_tee_reclaim_check_update(
3130 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01003131 composite->constituent_count, memory_to_attributes,
3132 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003133}