blob: 6da0459f589f9db5522d0dc2e1da1f75543880d0 [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) {
804 /* TODO: partial defrag of failed range. */
805 /* Recover any memory consumed in failed mapping. */
806 mm_defrag(stage1_locked, ppool);
807 goto fail;
808 }
809
810 memset_s(ptr, size, 0, size);
811 arch_mm_flush_dcache(ptr, size);
812 mm_unmap(stage1_locked, begin, end, ppool);
813
814 ret = true;
815 goto out;
816
817fail:
818 ret = false;
819
820out:
821 mm_unlock_stage1(&stage1_locked);
822
823 return ret;
824}
825
826/**
827 * Clears a region of physical memory by overwriting it with zeros. The data is
828 * flushed from the cache so the memory has been cleared across the system.
829 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100830static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000831 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100832 struct ffa_memory_region_constituent **fragments,
833 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
834 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100835{
836 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100837 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100838 struct mm_stage1_locked stage1_locked;
839 bool ret = false;
840
841 /*
842 * Create a local pool so any freed memory can't be used by another
843 * thread. This is to ensure each constituent that is mapped can be
844 * unmapped again afterwards.
845 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000846 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100847
Andrew Walbranca808b12020-05-15 17:22:28 +0100848 /* Iterate over the memory region constituents within each fragment. */
849 for (i = 0; i < fragment_count; ++i) {
850 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100851
Andrew Walbranca808b12020-05-15 17:22:28 +0100852 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
853 size_t size = fragments[i][j].page_count * PAGE_SIZE;
854 paddr_t begin =
855 pa_from_ipa(ipa_init(fragments[i][j].address));
856 paddr_t end = pa_add(begin, size);
857
J-Alves7db32002021-12-14 14:44:50 +0000858 if (!clear_memory(begin, end, &local_page_pool,
859 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 /*
861 * api_clear_memory will defrag on failure, so
862 * no need to do it here.
863 */
864 goto out;
865 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100866 }
867 }
868
869 /*
870 * Need to defrag after clearing, as it may have added extra mappings to
871 * the stage 1 page table.
872 */
873 stage1_locked = mm_lock_stage1();
874 mm_defrag(stage1_locked, &local_page_pool);
875 mm_unlock_stage1(&stage1_locked);
876
877 ret = true;
878
879out:
880 mpool_fini(&local_page_pool);
881 return ret;
882}
883
884/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100886 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000887 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100888 *
889 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000890 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100892 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100893 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
894 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100896 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100897 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100898 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100899static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000900 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100901 struct ffa_memory_region_constituent **fragments,
902 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100903 uint32_t share_func, struct ffa_memory_access *receivers,
904 uint32_t receivers_count, struct mpool *page_pool, bool clear,
905 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100906{
Andrew Walbranca808b12020-05-15 17:22:28 +0100907 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100908 uint32_t orig_from_mode;
909 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100911 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100912
913 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100914 * Make sure constituents are properly aligned to a 64-bit boundary. If
915 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100917 for (i = 0; i < fragment_count; ++i) {
918 if (!is_aligned(fragments[i], 8)) {
919 dlog_verbose("Constituents not aligned.\n");
920 return ffa_error(FFA_INVALID_PARAMETERS);
921 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100922 }
923
924 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000925 * Check if the state transition is lawful for the sender, ensure that
926 * all constituents of a memory region being shared are at the same
927 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100928 */
J-Alves363f5722022-04-25 17:37:37 +0100929 ret = ffa_send_check_transition(from_locked, share_func, receivers,
930 receivers_count, &orig_from_mode,
931 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100932 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100933 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100935 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100936 }
937
Andrew Walbran37c574e2020-06-03 11:45:46 +0100938 if (orig_from_mode_ret != NULL) {
939 *orig_from_mode_ret = orig_from_mode;
940 }
941
Jose Marinho09b1db82019-08-08 09:16:59 +0100942 /*
943 * Create a local pool so any freed memory can't be used by another
944 * thread. This is to ensure the original mapping can be restored if the
945 * clear fails.
946 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000947 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100948
949 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000950 * First reserve all required memory for the new page table entries
951 * without committing, to make sure the entire operation will succeed
952 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100953 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 if (!ffa_region_group_identity_map(
955 from_locked, fragments, fragment_constituent_counts,
956 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100957 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100958 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100959 goto out;
960 }
961
962 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000963 * Update the mapping for the sender. This won't allocate because the
964 * transaction was already prepared above, but may free pages in the
965 * case that a whole block is being unmapped that was previously
966 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100967 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100968 CHECK(ffa_region_group_identity_map(
969 from_locked, fragments, fragment_constituent_counts,
970 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100971
972 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000973 if (clear &&
974 !ffa_clear_memory_constituents(
975 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
976 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100977 /*
978 * On failure, roll back by returning memory to the sender. This
979 * may allocate pages which were previously freed into
980 * `local_page_pool` by the call above, but will never allocate
981 * more pages than that so can never fail.
982 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100983 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100984 from_locked, fragments, fragment_constituent_counts,
985 fragment_count, orig_from_mode, &local_page_pool,
986 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100987
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100989 goto out;
990 }
991
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993
994out:
995 mpool_fini(&local_page_pool);
996
997 /*
998 * Tidy up the page table by reclaiming failed mappings (if there was an
999 * error) or merging entries into blocks where possible (on success).
1000 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001001 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001002
1003 return ret;
1004}
1005
1006/**
1007 * Validates and maps memory shared from one VM to another.
1008 *
1009 * This function requires the calling context to hold the <to> lock.
1010 *
1011 * Returns:
1012 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001013 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001014 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001015 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001016 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001019static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001020 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001021 struct ffa_memory_region_constituent **fragments,
1022 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1023 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1024 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001025{
Andrew Walbranca808b12020-05-15 17:22:28 +01001026 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027 uint32_t to_mode;
1028 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030
1031 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001032 * Make sure constituents are properly aligned to a 64-bit boundary. If
1033 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001035 for (i = 0; i < fragment_count; ++i) {
1036 if (!is_aligned(fragments[i], 8)) {
1037 return ffa_error(FFA_INVALID_PARAMETERS);
1038 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001039 }
1040
1041 /*
1042 * Check if the state transition is lawful for the recipient, and ensure
1043 * that all constituents of the memory region being retrieved are at the
1044 * same state.
1045 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001046 ret = ffa_retrieve_check_transition(
1047 to_locked, share_func, fragments, fragment_constituent_counts,
1048 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001049 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001051 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001052 }
1053
1054 /*
1055 * Create a local pool so any freed memory can't be used by another
1056 * thread. This is to ensure the original mapping can be restored if the
1057 * clear fails.
1058 */
1059 mpool_init_with_fallback(&local_page_pool, page_pool);
1060
1061 /*
1062 * First reserve all required memory for the new page table entries in
1063 * the recipient page tables without committing, to make sure the entire
1064 * operation will succeed without exhausting the page pool.
1065 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001066 if (!ffa_region_group_identity_map(
1067 to_locked, fragments, fragment_constituent_counts,
1068 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001069 /* TODO: partial defrag of failed range. */
1070 dlog_verbose(
1071 "Insufficient memory to update recipient page "
1072 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001074 goto out;
1075 }
1076
1077 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001078 if (clear &&
1079 !ffa_clear_memory_constituents(
1080 plat_ffa_owner_world_mode(from_id), fragments,
1081 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083 goto out;
1084 }
1085
Jose Marinho09b1db82019-08-08 09:16:59 +01001086 /*
1087 * Complete the transfer by mapping the memory into the recipient. This
1088 * won't allocate because the transaction was already prepared above, so
1089 * it doesn't need to use the `local_page_pool`.
1090 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001091 CHECK(ffa_region_group_identity_map(
1092 to_locked, fragments, fragment_constituent_counts,
1093 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001094
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001096
1097out:
1098 mpool_fini(&local_page_pool);
1099
1100 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001101 * Tidy up the page table by reclaiming failed mappings (if there was an
1102 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001103 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001104 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001105
1106 return ret;
1107}
1108
Andrew Walbran290b0c92020-02-03 16:37:14 +00001109/**
1110 * Reclaims the given memory from the TEE. To do this space is first reserved in
1111 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1112 * then (if that is successful) the memory is mapped back into the <to> VM's
1113 * page table.
1114 *
1115 * This function requires the calling context to hold the <to> lock.
1116 *
1117 * Returns:
1118 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001119 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001120 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001121 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001122 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001124 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001125static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001126 struct vm_locked to_locked, ffa_memory_handle_t handle,
1127 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001128 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1129 struct mpool *page_pool)
1130{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001131 uint32_t to_mode;
1132 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001133 struct ffa_value ret;
1134 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001135
1136 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001137 * Make sure constituents are properly aligned to a 64-bit boundary. If
1138 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001139 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001140 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001141 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001142 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001143 }
1144
1145 /*
1146 * Check if the state transition is lawful for the recipient, and ensure
1147 * that all constituents of the memory region being retrieved are at the
1148 * same state.
1149 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001151 &constituents, &constituent_count,
1152 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001153 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001154 dlog_verbose("Invalid transition.\n");
1155 return ret;
1156 }
1157
1158 /*
1159 * Create a local pool so any freed memory can't be used by another
1160 * thread. This is to ensure the original mapping can be restored if the
1161 * clear fails.
1162 */
1163 mpool_init_with_fallback(&local_page_pool, page_pool);
1164
1165 /*
1166 * First reserve all required memory for the new page table entries in
1167 * the recipient page tables without committing, to make sure the entire
1168 * operation will succeed without exhausting the page pool.
1169 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001170 if (!ffa_region_group_identity_map(to_locked, &constituents,
1171 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001173 /* TODO: partial defrag of failed range. */
1174 dlog_verbose(
1175 "Insufficient memory to update recipient page "
1176 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001177 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001178 goto out;
1179 }
1180
1181 /*
1182 * Forward the request to the TEE and see what happens.
1183 */
1184 tee_flags = 0;
1185 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001187 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001188 ret = arch_other_world_call(
1189 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1190 .arg1 = (uint32_t)handle,
1191 .arg2 = (uint32_t)(handle >> 32),
1192 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001193
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001194 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001195 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001196 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1197 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001198 ret.func, ret.arg2);
1199 goto out;
1200 }
1201
1202 /*
1203 * The TEE was happy with it, so complete the reclaim by mapping the
1204 * memory into the recipient. This won't allocate because the
1205 * transaction was already prepared above, so it doesn't need to use the
1206 * `local_page_pool`.
1207 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001208 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1209 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001210 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001211
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001213
1214out:
1215 mpool_fini(&local_page_pool);
1216
1217 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001218 * Tidy up the page table by reclaiming failed mappings (if there was an
1219 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001220 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001221 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001222
1223 return ret;
1224}
1225
Andrew Walbran996d1d12020-05-27 14:08:43 +01001226static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001227 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001228 struct ffa_memory_region_constituent **fragments,
1229 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1230 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001231{
1232 uint32_t orig_from_mode;
1233 uint32_t from_mode;
1234 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001235 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001236
Andrew Walbranca808b12020-05-15 17:22:28 +01001237 ret = ffa_relinquish_check_transition(
1238 from_locked, &orig_from_mode, fragments,
1239 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001240 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001241 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001242 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001243 }
1244
1245 /*
1246 * Create a local pool so any freed memory can't be used by another
1247 * thread. This is to ensure the original mapping can be restored if the
1248 * clear fails.
1249 */
1250 mpool_init_with_fallback(&local_page_pool, page_pool);
1251
1252 /*
1253 * First reserve all required memory for the new page table entries
1254 * without committing, to make sure the entire operation will succeed
1255 * without exhausting the page pool.
1256 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001257 if (!ffa_region_group_identity_map(
1258 from_locked, fragments, fragment_constituent_counts,
1259 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001260 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001261 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001262 goto out;
1263 }
1264
1265 /*
1266 * Update the mapping for the sender. This won't allocate because the
1267 * transaction was already prepared above, but may free pages in the
1268 * case that a whole block is being unmapped that was previously
1269 * partially mapped.
1270 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001271 CHECK(ffa_region_group_identity_map(
1272 from_locked, fragments, fragment_constituent_counts,
1273 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001274
1275 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001276 if (clear &&
1277 !ffa_clear_memory_constituents(
1278 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1279 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001280 /*
1281 * On failure, roll back by returning memory to the sender. This
1282 * may allocate pages which were previously freed into
1283 * `local_page_pool` by the call above, but will never allocate
1284 * more pages than that so can never fail.
1285 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001286 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001287 from_locked, fragments, fragment_constituent_counts,
1288 fragment_count, orig_from_mode, &local_page_pool,
1289 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001290
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001291 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001292 goto out;
1293 }
1294
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001295 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001296
1297out:
1298 mpool_fini(&local_page_pool);
1299
1300 /*
1301 * Tidy up the page table by reclaiming failed mappings (if there was an
1302 * error) or merging entries into blocks where possible (on success).
1303 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001304 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001305
1306 return ret;
1307}
1308
1309/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001310 * Complete a memory sending operation by checking that it is valid, updating
1311 * the sender page table, and then either marking the share state as having
1312 * completed sending (on success) or freeing it (on failure).
1313 *
1314 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1315 */
1316static struct ffa_value ffa_memory_send_complete(
1317 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001318 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1319 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001320{
1321 struct ffa_memory_region *memory_region = share_state->memory_region;
1322 struct ffa_value ret;
1323
1324 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001325 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001326
1327 /* Check that state is valid in sender page table and update. */
1328 ret = ffa_send_check_update(
1329 from_locked, share_state->fragments,
1330 share_state->fragment_constituent_counts,
1331 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001332 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001333 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1334 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001335 if (ret.func != FFA_SUCCESS_32) {
1336 /*
1337 * Free share state, it failed to send so it can't be retrieved.
1338 */
1339 dlog_verbose("Complete failed, freeing share state.\n");
1340 share_state_free(share_states, share_state, page_pool);
1341 return ret;
1342 }
1343
1344 share_state->sending_complete = true;
1345 dlog_verbose("Marked sending complete.\n");
1346
J-Alvesee68c542020-10-29 17:48:20 +00001347 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001348}
1349
1350/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001351 * Check that the memory attributes match Hafnium expectations:
1352 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1353 * Write-Allocate Cacheable.
1354 */
1355static struct ffa_value ffa_memory_attributes_validate(
1356 ffa_memory_access_permissions_t attributes)
1357{
1358 enum ffa_memory_type memory_type;
1359 enum ffa_memory_cacheability cacheability;
1360 enum ffa_memory_shareability shareability;
1361
1362 memory_type = ffa_get_memory_type_attr(attributes);
1363 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1364 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1365 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001366 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001367 }
1368
1369 cacheability = ffa_get_memory_cacheability_attr(attributes);
1370 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1371 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1372 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001373 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001374 }
1375
1376 shareability = ffa_get_memory_shareability_attr(attributes);
1377 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1378 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1379 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001380 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001381 }
1382
1383 return (struct ffa_value){.func = FFA_SUCCESS_32};
1384}
1385
1386/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001387 * Check that the given `memory_region` represents a valid memory send request
1388 * of the given `share_func` type, return the clear flag and permissions via the
1389 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001390 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001392 * not.
1393 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001394static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001395 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1396 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001397 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001398{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001399 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001400 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001401 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001402 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001403 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404 enum ffa_data_access data_access;
1405 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001406 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001407
Andrew Walbrana65a1322020-04-06 19:32:32 +01001408 /* The sender must match the message sender. */
1409 if (memory_region->sender != from_locked.vm->id) {
1410 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 }
1413
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 /*
1415 * Ensure that the composite header is within the memory bounds and
1416 * doesn't overlap the first part of the message.
1417 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001418 receivers_length = sizeof(struct ffa_memory_access) *
1419 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001420 constituents_offset =
1421 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001422 composite_memory_region_offset =
1423 memory_region->receivers[0].composite_memory_region_offset;
1424 if ((composite_memory_region_offset == 0) ||
1425 (composite_memory_region_offset <
1426 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001427 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001429 "Invalid composite memory region descriptor offset "
1430 "%d.\n",
1431 memory_region->receivers[0]
1432 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 }
1435
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437
1438 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001439 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001440 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001441 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1442 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001443 if (memory_share_length != constituents_offset + constituents_length) {
1444 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001445 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001446 memory_region->receivers[0]
1447 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001448 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001449 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001450 if (fragment_length < memory_share_length &&
1451 fragment_length < HF_MAILBOX_SIZE) {
1452 dlog_warning(
1453 "Initial fragment length %d smaller than mailbox "
1454 "size.\n",
1455 fragment_length);
1456 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457
Andrew Walbrana65a1322020-04-06 19:32:32 +01001458 /*
1459 * Clear is not allowed for memory sharing, as the sender still has
1460 * access to the memory.
1461 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001462 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1463 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001464 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001465 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001466 }
1467
1468 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001470 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001471 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001472 }
1473
J-Alves363f5722022-04-25 17:37:37 +01001474 /* Check that the permissions are valid, for each specified receiver. */
1475 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1476 ffa_memory_access_permissions_t permissions =
1477 memory_region->receivers[i]
1478 .receiver_permissions.permissions;
1479 ffa_vm_id_t receiver_id =
1480 memory_region->receivers[i]
1481 .receiver_permissions.receiver;
1482
1483 if (memory_region->sender == receiver_id) {
1484 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001485 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 }
Federico Recanati85090c42021-12-15 13:17:54 +01001487
J-Alves363f5722022-04-25 17:37:37 +01001488 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1489 j++) {
1490 if (receiver_id ==
1491 memory_region->receivers[j]
1492 .receiver_permissions.receiver) {
1493 dlog_verbose(
1494 "Repeated receiver(%x) in memory send "
1495 "operation.\n",
1496 memory_region->receivers[j]
1497 .receiver_permissions.receiver);
1498 return ffa_error(FFA_INVALID_PARAMETERS);
1499 }
1500 }
1501
1502 if (composite_memory_region_offset !=
1503 memory_region->receivers[i]
1504 .composite_memory_region_offset) {
1505 dlog_verbose(
1506 "All ffa_memory_access should point to the "
1507 "same composite memory region offset.\n");
1508 return ffa_error(FFA_INVALID_PARAMETERS);
1509 }
1510
1511 data_access = ffa_get_data_access_attr(permissions);
1512 instruction_access =
1513 ffa_get_instruction_access_attr(permissions);
1514 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1515 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1516 dlog_verbose(
1517 "Reserved value for receiver permissions "
1518 "%#x.\n",
1519 permissions);
1520 return ffa_error(FFA_INVALID_PARAMETERS);
1521 }
1522 if (instruction_access !=
1523 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1524 dlog_verbose(
1525 "Invalid instruction access permissions %#x "
1526 "for sending memory.\n",
1527 permissions);
1528 return ffa_error(FFA_INVALID_PARAMETERS);
1529 }
1530 if (share_func == FFA_MEM_SHARE_32) {
1531 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1532 dlog_verbose(
1533 "Invalid data access permissions %#x "
1534 "for sharing memory.\n",
1535 permissions);
1536 return ffa_error(FFA_INVALID_PARAMETERS);
1537 }
1538 /*
1539 * According to section 10.10.3 of the FF-A v1.1 EAC0
1540 * spec, NX is required for share operations (but must
1541 * not be specified by the sender) so set it in the
1542 * copy that we store, ready to be returned to the
1543 * retriever.
1544 */
1545 ffa_set_instruction_access_attr(
1546 &permissions, FFA_INSTRUCTION_ACCESS_NX);
1547 memory_region->receivers[i]
1548 .receiver_permissions.permissions = permissions;
1549 }
1550 if (share_func == FFA_MEM_LEND_32 &&
1551 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1552 dlog_verbose(
1553 "Invalid data access permissions %#x for "
1554 "lending memory.\n",
1555 permissions);
1556 return ffa_error(FFA_INVALID_PARAMETERS);
1557 }
1558
1559 if (share_func == FFA_MEM_DONATE_32 &&
1560 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1561 dlog_verbose(
1562 "Invalid data access permissions %#x for "
1563 "donating memory.\n",
1564 permissions);
1565 return ffa_error(FFA_INVALID_PARAMETERS);
1566 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001567 }
1568
Federico Recanatid937f5e2021-12-20 17:38:23 +01001569 /*
J-Alves807794e2022-06-16 13:42:47 +01001570 * If a memory donate or lend with single borrower, the memory type
1571 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001572 */
J-Alves807794e2022-06-16 13:42:47 +01001573 if (share_func == FFA_MEM_DONATE_32 ||
1574 (share_func == FFA_MEM_LEND_32 &&
1575 memory_region->receiver_count == 1)) {
1576 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1577 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1578 dlog_verbose(
1579 "Memory type shall not be specified by "
1580 "sender.\n");
1581 return ffa_error(FFA_INVALID_PARAMETERS);
1582 }
1583 } else {
1584 /*
1585 * Check that sender's memory attributes match Hafnium
1586 * expectations: Normal Memory, Inner shareable, Write-Back
1587 * Read-Allocate Write-Allocate Cacheable.
1588 */
1589 ret = ffa_memory_attributes_validate(memory_region->attributes);
1590 if (ret.func != FFA_SUCCESS_32) {
1591 return ret;
1592 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001593 }
1594
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001595 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001596}
1597
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001598/** Forwards a memory send message on to the TEE. */
1599static struct ffa_value memory_send_tee_forward(
1600 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1601 uint32_t share_func, struct ffa_memory_region *memory_region,
1602 uint32_t memory_share_length, uint32_t fragment_length)
1603{
1604 struct ffa_value ret;
1605
1606 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1607 memory_region, fragment_length);
1608 tee_locked.vm->mailbox.recv_size = fragment_length;
1609 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1610 tee_locked.vm->mailbox.recv_func = share_func;
1611 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001612 ret = arch_other_world_call(
1613 (struct ffa_value){.func = share_func,
1614 .arg1 = memory_share_length,
1615 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001616 /*
1617 * After the call to the TEE completes it must have finished reading its
1618 * RX buffer, so it is ready for another message.
1619 */
1620 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1621
1622 return ret;
1623}
1624
Andrew Walbrana65a1322020-04-06 19:32:32 +01001625/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001626 * Gets the share state for continuing an operation to donate, lend or share
1627 * memory, and checks that it is a valid request.
1628 *
1629 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1630 * not.
1631 */
1632static struct ffa_value ffa_memory_send_continue_validate(
1633 struct share_states_locked share_states, ffa_memory_handle_t handle,
1634 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1635 struct mpool *page_pool)
1636{
1637 struct ffa_memory_share_state *share_state;
1638 struct ffa_memory_region *memory_region;
1639
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001640 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001641
1642 /*
1643 * Look up the share state by handle and make sure that the VM ID
1644 * matches.
1645 */
1646 if (!get_share_state(share_states, handle, &share_state)) {
1647 dlog_verbose(
1648 "Invalid handle %#x for memory send continuation.\n",
1649 handle);
1650 return ffa_error(FFA_INVALID_PARAMETERS);
1651 }
1652 memory_region = share_state->memory_region;
1653
1654 if (memory_region->sender != from_vm_id) {
1655 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1656 return ffa_error(FFA_INVALID_PARAMETERS);
1657 }
1658
1659 if (share_state->sending_complete) {
1660 dlog_verbose(
1661 "Sending of memory handle %#x is already complete.\n",
1662 handle);
1663 return ffa_error(FFA_INVALID_PARAMETERS);
1664 }
1665
1666 if (share_state->fragment_count == MAX_FRAGMENTS) {
1667 /*
1668 * Log a warning as this is a sign that MAX_FRAGMENTS should
1669 * probably be increased.
1670 */
1671 dlog_warning(
1672 "Too many fragments for memory share with handle %#x; "
1673 "only %d supported.\n",
1674 handle, MAX_FRAGMENTS);
1675 /* Free share state, as it's not possible to complete it. */
1676 share_state_free(share_states, share_state, page_pool);
1677 return ffa_error(FFA_NO_MEMORY);
1678 }
1679
1680 *share_state_ret = share_state;
1681
1682 return (struct ffa_value){.func = FFA_SUCCESS_32};
1683}
1684
1685/**
1686 * Forwards a memory send continuation message on to the TEE.
1687 */
1688static struct ffa_value memory_send_continue_tee_forward(
1689 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1690 uint32_t fragment_length, ffa_memory_handle_t handle)
1691{
1692 struct ffa_value ret;
1693
1694 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1695 fragment_length);
1696 tee_locked.vm->mailbox.recv_size = fragment_length;
1697 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1698 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1699 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001700 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001701 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1702 .arg1 = (uint32_t)handle,
1703 .arg2 = (uint32_t)(handle >> 32),
1704 .arg3 = fragment_length,
1705 .arg4 = (uint64_t)sender_vm_id << 16});
1706 /*
1707 * After the call to the TEE completes it must have finished reading its
1708 * RX buffer, so it is ready for another message.
1709 */
1710 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1711
1712 return ret;
1713}
1714
1715/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001716 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1717 * updates the stage-2 page tables. Specifically, check if the message length
1718 * and number of memory region constituents match, and if the transition is
1719 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001720 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001721 * Assumes that the caller has already found and locked the sender VM and copied
1722 * the memory region descriptor from the sender's TX buffer to a freshly
1723 * allocated page from Hafnium's internal pool. The caller must have also
1724 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001725 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001726 * This function takes ownership of the `memory_region` passed in and will free
1727 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001728 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001729struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001730 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001731 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001732 uint32_t fragment_length, uint32_t share_func,
1733 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001734{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001735 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001736 struct share_states_locked share_states;
1737 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001738
1739 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001740 * If there is an error validating the `memory_region` then we need to
1741 * free it because we own it but we won't be storing it in a share state
1742 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001743 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001744 ret = ffa_memory_send_validate(from_locked, memory_region,
1745 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001746 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001747 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001748 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001749 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001750 }
1751
Andrew Walbrana65a1322020-04-06 19:32:32 +01001752 /* Set flag for share function, ready to be retrieved later. */
1753 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001754 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001755 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001756 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001757 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001758 case FFA_MEM_LEND_32:
1759 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001760 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001761 case FFA_MEM_DONATE_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_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001764 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001765 }
1766
Andrew Walbranca808b12020-05-15 17:22:28 +01001767 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001768 /*
1769 * Allocate a share state before updating the page table. Otherwise if
1770 * updating the page table succeeded but allocating the share state
1771 * failed then it would leave the memory in a state where nobody could
1772 * get it back.
1773 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001774 if (!allocate_share_state(share_states, share_func, memory_region,
1775 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1776 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001777 dlog_verbose("Failed to allocate share state.\n");
1778 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001779 ret = ffa_error(FFA_NO_MEMORY);
1780 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001781 }
1782
Andrew Walbranca808b12020-05-15 17:22:28 +01001783 if (fragment_length == memory_share_length) {
1784 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001785 ret = ffa_memory_send_complete(
1786 from_locked, share_states, share_state, page_pool,
1787 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001788 } else {
1789 ret = (struct ffa_value){
1790 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001791 .arg1 = (uint32_t)memory_region->handle,
1792 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001793 .arg3 = fragment_length};
1794 }
1795
1796out:
1797 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001798 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001799 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001800}
1801
1802/**
1803 * Validates a call to donate, lend or share memory to the TEE and then updates
1804 * the stage-2 page tables. Specifically, check if the message length and number
1805 * of memory region constituents match, and if the transition is valid for the
1806 * type of memory sending operation.
1807 *
1808 * Assumes that the caller has already found and locked the sender VM and the
1809 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1810 * to a freshly allocated page from Hafnium's internal pool. The caller must
1811 * have also validated that the receiver VM ID is valid.
1812 *
1813 * This function takes ownership of the `memory_region` passed in and will free
1814 * it when necessary; it must not be freed by the caller.
1815 */
1816struct ffa_value ffa_memory_tee_send(
1817 struct vm_locked from_locked, struct vm_locked to_locked,
1818 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1819 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1820{
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001821 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001822
1823 /*
1824 * If there is an error validating the `memory_region` then we need to
1825 * free it because we own it but we won't be storing it in a share state
1826 * after all.
1827 */
1828 ret = ffa_memory_send_validate(from_locked, memory_region,
1829 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001830 share_func);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001831 if (ret.func != FFA_SUCCESS_32) {
1832 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001833 }
1834
Andrew Walbranca808b12020-05-15 17:22:28 +01001835 if (fragment_length == memory_share_length) {
1836 /* No more fragments to come, everything fit in one message. */
1837 struct ffa_composite_memory_region *composite =
1838 ffa_memory_region_get_composite(memory_region, 0);
1839 struct ffa_memory_region_constituent *constituents =
1840 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001841 struct mpool local_page_pool;
1842 uint32_t orig_from_mode;
1843
1844 /*
1845 * Use a local page pool so that we can roll back if necessary.
1846 */
1847 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001848
1849 ret = ffa_send_check_update(
1850 from_locked, &constituents,
1851 &composite->constituent_count, 1, share_func,
J-Alves363f5722022-04-25 17:37:37 +01001852 memory_region->receivers, memory_region->receiver_count,
1853 &local_page_pool,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001854 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1855 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001856 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001857 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001858 goto out;
1859 }
1860
1861 /* Forward memory send message on to TEE. */
1862 ret = memory_send_tee_forward(
1863 to_locked, from_locked.vm->id, share_func,
1864 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001865
1866 if (ret.func != FFA_SUCCESS_32) {
1867 dlog_verbose(
1868 "TEE didn't successfully complete memory send "
1869 "operation; returned %#x (%d). Rolling back.\n",
1870 ret.func, ret.arg2);
1871
1872 /*
1873 * The TEE failed to complete the send operation, so
1874 * roll back the page table update for the VM. This
1875 * can't fail because it won't try to allocate more
1876 * memory than was freed into the `local_page_pool` by
1877 * `ffa_send_check_update` in the initial update.
1878 */
1879 CHECK(ffa_region_group_identity_map(
1880 from_locked, &constituents,
1881 &composite->constituent_count, 1,
1882 orig_from_mode, &local_page_pool, true));
1883 }
1884
1885 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001886 } else {
1887 struct share_states_locked share_states = share_states_lock();
1888 ffa_memory_handle_t handle;
1889
1890 /*
1891 * We need to wait for the rest of the fragments before we can
1892 * check whether the transaction is valid and unmap the memory.
1893 * Call the TEE so it can do its initial validation and assign a
1894 * handle, and allocate a share state to keep what we have so
1895 * far.
1896 */
1897 ret = memory_send_tee_forward(
1898 to_locked, from_locked.vm->id, share_func,
1899 memory_region, memory_share_length, fragment_length);
1900 if (ret.func == FFA_ERROR_32) {
1901 goto out_unlock;
1902 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1903 dlog_warning(
1904 "Got %#x from TEE in response to %#x for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001905 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001906 "FFA_MEM_FRAG_RX.\n",
1907 ret.func, share_func, fragment_length,
1908 memory_share_length);
1909 ret = ffa_error(FFA_INVALID_PARAMETERS);
1910 goto out_unlock;
1911 }
1912 handle = ffa_frag_handle(ret);
1913 if (ret.arg3 != fragment_length) {
1914 dlog_warning(
1915 "Got unexpected fragment offset %d for "
1916 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1917 ret.arg3, fragment_length);
1918 ret = ffa_error(FFA_INVALID_PARAMETERS);
1919 goto out_unlock;
1920 }
1921 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1922 dlog_warning(
1923 "Got unexpected sender ID %d for "
1924 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1925 ffa_frag_sender(ret), from_locked.vm->id);
1926 ret = ffa_error(FFA_INVALID_PARAMETERS);
1927 goto out_unlock;
1928 }
1929
1930 if (!allocate_share_state(share_states, share_func,
1931 memory_region, fragment_length,
1932 handle, NULL)) {
1933 dlog_verbose("Failed to allocate share state.\n");
1934 ret = ffa_error(FFA_NO_MEMORY);
1935 goto out_unlock;
1936 }
1937 /*
1938 * Don't free the memory region fragment, as it has been stored
1939 * in the share state.
1940 */
1941 memory_region = NULL;
1942 out_unlock:
1943 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001944 }
1945
Andrew Walbranca808b12020-05-15 17:22:28 +01001946out:
1947 if (memory_region != NULL) {
1948 mpool_free(page_pool, memory_region);
1949 }
1950 dump_share_states();
1951 return ret;
1952}
1953
1954/**
1955 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1956 * this is the last fragment then checks that the transition is valid for the
1957 * type of memory sending operation and updates the stage-2 page tables of the
1958 * sender.
1959 *
1960 * Assumes that the caller has already found and locked the sender VM and copied
1961 * the memory region descriptor from the sender's TX buffer to a freshly
1962 * allocated page from Hafnium's internal pool.
1963 *
1964 * This function takes ownership of the `fragment` passed in; it must not be
1965 * freed by the caller.
1966 */
1967struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1968 void *fragment,
1969 uint32_t fragment_length,
1970 ffa_memory_handle_t handle,
1971 struct mpool *page_pool)
1972{
1973 struct share_states_locked share_states = share_states_lock();
1974 struct ffa_memory_share_state *share_state;
1975 struct ffa_value ret;
1976 struct ffa_memory_region *memory_region;
1977
1978 ret = ffa_memory_send_continue_validate(share_states, handle,
1979 &share_state,
1980 from_locked.vm->id, page_pool);
1981 if (ret.func != FFA_SUCCESS_32) {
1982 goto out_free_fragment;
1983 }
1984 memory_region = share_state->memory_region;
1985
1986 if (memory_region->receivers[0].receiver_permissions.receiver ==
1987 HF_TEE_VM_ID) {
1988 dlog_error(
1989 "Got hypervisor-allocated handle for memory send to "
1990 "TEE. This should never happen, and indicates a bug in "
1991 "EL3 code.\n");
1992 ret = ffa_error(FFA_INVALID_PARAMETERS);
1993 goto out_free_fragment;
1994 }
1995
1996 /* Add this fragment. */
1997 share_state->fragments[share_state->fragment_count] = fragment;
1998 share_state->fragment_constituent_counts[share_state->fragment_count] =
1999 fragment_length / sizeof(struct ffa_memory_region_constituent);
2000 share_state->fragment_count++;
2001
2002 /* Check whether the memory send operation is now ready to complete. */
2003 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002004 ret = ffa_memory_send_complete(
2005 from_locked, share_states, share_state, page_pool,
2006 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002007 } else {
2008 ret = (struct ffa_value){
2009 .func = FFA_MEM_FRAG_RX_32,
2010 .arg1 = (uint32_t)handle,
2011 .arg2 = (uint32_t)(handle >> 32),
2012 .arg3 = share_state_next_fragment_offset(share_states,
2013 share_state)};
2014 }
2015 goto out;
2016
2017out_free_fragment:
2018 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002019
2020out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002021 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002022 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002023}
2024
Andrew Walbranca808b12020-05-15 17:22:28 +01002025/**
2026 * Continues an operation to donate, lend or share memory to the TEE VM. If this
2027 * is the last fragment then checks that the transition is valid for the type of
2028 * memory sending operation and updates the stage-2 page tables of the sender.
2029 *
2030 * Assumes that the caller has already found and locked the sender VM and copied
2031 * the memory region descriptor from the sender's TX buffer to a freshly
2032 * allocated page from Hafnium's internal pool.
2033 *
2034 * This function takes ownership of the `memory_region` passed in and will free
2035 * it when necessary; it must not be freed by the caller.
2036 */
2037struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
2038 struct vm_locked to_locked,
2039 void *fragment,
2040 uint32_t fragment_length,
2041 ffa_memory_handle_t handle,
2042 struct mpool *page_pool)
2043{
2044 struct share_states_locked share_states = share_states_lock();
2045 struct ffa_memory_share_state *share_state;
2046 struct ffa_value ret;
2047 struct ffa_memory_region *memory_region;
2048
2049 ret = ffa_memory_send_continue_validate(share_states, handle,
2050 &share_state,
2051 from_locked.vm->id, page_pool);
2052 if (ret.func != FFA_SUCCESS_32) {
2053 goto out_free_fragment;
2054 }
2055 memory_region = share_state->memory_region;
2056
2057 if (memory_region->receivers[0].receiver_permissions.receiver !=
2058 HF_TEE_VM_ID) {
2059 dlog_error(
2060 "Got SPM-allocated handle for memory send to non-TEE "
2061 "VM. This should never happen, and indicates a bug.\n");
2062 ret = ffa_error(FFA_INVALID_PARAMETERS);
2063 goto out_free_fragment;
2064 }
2065
2066 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2067 to_locked.vm->mailbox.recv == NULL) {
2068 /*
2069 * If the TEE RX buffer is not available, tell the sender to
2070 * retry by returning the current offset again.
2071 */
2072 ret = (struct ffa_value){
2073 .func = FFA_MEM_FRAG_RX_32,
2074 .arg1 = (uint32_t)handle,
2075 .arg2 = (uint32_t)(handle >> 32),
2076 .arg3 = share_state_next_fragment_offset(share_states,
2077 share_state),
2078 };
2079 goto out_free_fragment;
2080 }
2081
2082 /* Add this fragment. */
2083 share_state->fragments[share_state->fragment_count] = fragment;
2084 share_state->fragment_constituent_counts[share_state->fragment_count] =
2085 fragment_length / sizeof(struct ffa_memory_region_constituent);
2086 share_state->fragment_count++;
2087
2088 /* Check whether the memory send operation is now ready to complete. */
2089 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002090 struct mpool local_page_pool;
2091 uint32_t orig_from_mode;
2092
2093 /*
2094 * Use a local page pool so that we can roll back if necessary.
2095 */
2096 mpool_init_with_fallback(&local_page_pool, page_pool);
2097
Andrew Walbranca808b12020-05-15 17:22:28 +01002098 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002099 share_state, &local_page_pool,
2100 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002101
2102 if (ret.func == FFA_SUCCESS_32) {
2103 /*
2104 * Forward final fragment on to the TEE so that
2105 * it can complete the memory sending operation.
2106 */
2107 ret = memory_send_continue_tee_forward(
2108 to_locked, from_locked.vm->id, fragment,
2109 fragment_length, handle);
2110
2111 if (ret.func != FFA_SUCCESS_32) {
2112 /*
2113 * The error will be passed on to the caller,
2114 * but log it here too.
2115 */
2116 dlog_verbose(
2117 "TEE didn't successfully complete "
2118 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002119 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002120 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002121
2122 /*
2123 * The TEE failed to complete the send
2124 * operation, so roll back the page table update
2125 * for the VM. This can't fail because it won't
2126 * try to allocate more memory than was freed
2127 * into the `local_page_pool` by
2128 * `ffa_send_check_update` in the initial
2129 * update.
2130 */
2131 CHECK(ffa_region_group_identity_map(
2132 from_locked, share_state->fragments,
2133 share_state
2134 ->fragment_constituent_counts,
2135 share_state->fragment_count,
2136 orig_from_mode, &local_page_pool,
2137 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002138 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002139
Andrew Walbranca808b12020-05-15 17:22:28 +01002140 /* Free share state. */
2141 share_state_free(share_states, share_state, page_pool);
2142 } else {
2143 /* Abort sending to TEE. */
2144 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002145 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002146 .func = FFA_MEM_RECLAIM_32,
2147 .arg1 = (uint32_t)handle,
2148 .arg2 = (uint32_t)(handle >> 32)});
2149
2150 if (tee_ret.func != FFA_SUCCESS_32) {
2151 /*
2152 * Nothing we can do if TEE doesn't abort
2153 * properly, just log it.
2154 */
2155 dlog_verbose(
2156 "TEE didn't successfully abort failed "
2157 "memory send operation; returned %#x "
2158 "(%d).\n",
2159 tee_ret.func, tee_ret.arg2);
2160 }
2161 /*
2162 * We don't need to free the share state in this case
2163 * because ffa_memory_send_complete does that already.
2164 */
2165 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002166
2167 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002168 } else {
2169 uint32_t next_fragment_offset =
2170 share_state_next_fragment_offset(share_states,
2171 share_state);
2172
2173 ret = memory_send_continue_tee_forward(
2174 to_locked, from_locked.vm->id, fragment,
2175 fragment_length, handle);
2176
2177 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2178 ffa_frag_handle(ret) != handle ||
2179 ret.arg3 != next_fragment_offset ||
2180 ffa_frag_sender(ret) != from_locked.vm->id) {
2181 dlog_verbose(
2182 "Got unexpected result from forwarding "
2183 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2184 "offset %d, sender %d); expected "
2185 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2186 "sender %d).\n",
2187 ret.func, ffa_frag_handle(ret), ret.arg3,
2188 ffa_frag_sender(ret), handle,
2189 next_fragment_offset, from_locked.vm->id);
2190 /* Free share state. */
2191 share_state_free(share_states, share_state, page_pool);
2192 ret = ffa_error(FFA_INVALID_PARAMETERS);
2193 goto out;
2194 }
2195
2196 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2197 .arg1 = (uint32_t)handle,
2198 .arg2 = (uint32_t)(handle >> 32),
2199 .arg3 = next_fragment_offset};
2200 }
2201 goto out;
2202
2203out_free_fragment:
2204 mpool_free(page_pool, fragment);
2205
2206out:
2207 share_states_unlock(&share_states);
2208 return ret;
2209}
2210
2211/** Clean up after the receiver has finished retrieving a memory region. */
2212static void ffa_memory_retrieve_complete(
2213 struct share_states_locked share_states,
2214 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2215{
2216 if (share_state->share_func == FFA_MEM_DONATE_32) {
2217 /*
2218 * Memory that has been donated can't be relinquished,
2219 * so no need to keep the share state around.
2220 */
2221 share_state_free(share_states, share_state, page_pool);
2222 dlog_verbose("Freed share state for donate.\n");
2223 }
2224}
2225
J-Alves96de29f2022-04-26 16:05:24 +01002226/*
2227 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2228 * returns its index in the receiver's array. If receiver's ID doesn't exist
2229 * in the array, return the region's 'receiver_count'.
2230 */
2231static uint32_t ffa_memory_region_get_receiver(
2232 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
2233{
2234 struct ffa_memory_access *receivers;
2235 uint32_t i;
2236
2237 assert(memory_region != NULL);
2238
2239 receivers = memory_region->receivers;
2240
2241 for (i = 0U; i < memory_region->receiver_count; i++) {
2242 if (receivers[i].receiver_permissions.receiver == receiver) {
2243 break;
2244 }
2245 }
2246
2247 return i;
2248}
2249
2250/**
2251 * Validates the retrieved permissions against those specified by the lender
2252 * of memory share operation. Optionally can help set the permissions to be used
2253 * for the S2 mapping, through the `permissions` argument.
2254 * Returns true if permissions are valid, false otherwise.
2255 */
2256static bool ffa_memory_retrieve_is_memory_access_valid(
2257 enum ffa_data_access sent_data_access,
2258 enum ffa_data_access requested_data_access,
2259 enum ffa_instruction_access sent_instruction_access,
2260 enum ffa_instruction_access requested_instruction_access,
2261 ffa_memory_access_permissions_t *permissions)
2262{
2263 switch (sent_data_access) {
2264 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2265 case FFA_DATA_ACCESS_RW:
2266 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2267 requested_data_access == FFA_DATA_ACCESS_RW) {
2268 if (permissions != NULL) {
2269 ffa_set_data_access_attr(permissions,
2270 FFA_DATA_ACCESS_RW);
2271 }
2272 break;
2273 }
2274 /* Intentional fall-through. */
2275 case FFA_DATA_ACCESS_RO:
2276 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2277 requested_data_access == FFA_DATA_ACCESS_RO) {
2278 if (permissions != NULL) {
2279 ffa_set_data_access_attr(permissions,
2280 FFA_DATA_ACCESS_RO);
2281 }
2282 break;
2283 }
2284 dlog_verbose(
2285 "Invalid data access requested; sender specified "
2286 "permissions %#x but receiver requested %#x.\n",
2287 sent_data_access, requested_data_access);
2288 return false;
2289 case FFA_DATA_ACCESS_RESERVED:
2290 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2291 "checked before this point.");
2292 }
2293
2294 switch (sent_instruction_access) {
2295 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2296 case FFA_INSTRUCTION_ACCESS_X:
2297 if (requested_instruction_access ==
2298 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2299 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2300 if (permissions != NULL) {
2301 ffa_set_instruction_access_attr(
2302 permissions, FFA_INSTRUCTION_ACCESS_X);
2303 }
2304 break;
2305 }
2306 case FFA_INSTRUCTION_ACCESS_NX:
2307 if (requested_instruction_access ==
2308 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2309 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2310 if (permissions != NULL) {
2311 ffa_set_instruction_access_attr(
2312 permissions, FFA_INSTRUCTION_ACCESS_NX);
2313 }
2314 break;
2315 }
2316 dlog_verbose(
2317 "Invalid instruction access requested; sender "
2318 "specified permissions %#x but receiver requested "
2319 "%#x.\n",
2320 sent_instruction_access, requested_instruction_access);
2321 return false;
2322 case FFA_INSTRUCTION_ACCESS_RESERVED:
2323 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2324 "be checked before this point.");
2325 }
2326
2327 return true;
2328}
2329
2330/**
2331 * Validate the receivers' permissions in the retrieve request against those
2332 * specified by the lender.
2333 * In the `permissions` argument returns the permissions to set at S2 for the
2334 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2335 * Returns FFA_SUCCESS if all specified permissions are valid.
2336 */
2337static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2338 struct ffa_memory_region *memory_region,
2339 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2340 ffa_memory_access_permissions_t *permissions)
2341{
2342 uint32_t retrieve_receiver_index;
2343
2344 assert(permissions != NULL);
2345
2346 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2347 dlog_verbose(
2348 "Retrieve request should contain same list of "
2349 "borrowers, as specified by the lender.\n");
2350 return ffa_error(FFA_INVALID_PARAMETERS);
2351 }
2352
2353 retrieve_receiver_index = retrieve_request->receiver_count;
2354
2355 /* Should be populated with the permissions of the retriever. */
2356 *permissions = 0;
2357
2358 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2359 ffa_memory_access_permissions_t sent_permissions;
2360 struct ffa_memory_access *current_receiver =
2361 &retrieve_request->receivers[i];
2362 ffa_memory_access_permissions_t requested_permissions =
2363 current_receiver->receiver_permissions.permissions;
2364 ffa_vm_id_t current_receiver_id =
2365 current_receiver->receiver_permissions.receiver;
2366 bool found_to_id = current_receiver_id == to_vm_id;
2367
2368 /*
2369 * Find the current receiver in the transaction descriptor from
2370 * sender.
2371 */
2372 uint32_t mem_region_receiver_index =
2373 ffa_memory_region_get_receiver(memory_region,
2374 current_receiver_id);
2375
2376 if (mem_region_receiver_index ==
2377 memory_region->receiver_count) {
2378 dlog_verbose("%s: receiver %x not found\n", __func__,
2379 current_receiver_id);
2380 return ffa_error(FFA_DENIED);
2381 }
2382
2383 sent_permissions =
2384 memory_region->receivers[mem_region_receiver_index]
2385 .receiver_permissions.permissions;
2386
2387 if (found_to_id) {
2388 retrieve_receiver_index = i;
2389 }
2390
2391 /*
2392 * Since we are traversing the list of receivers, save the index
2393 * of the caller. As it needs to be there.
2394 */
2395
2396 if (current_receiver->composite_memory_region_offset != 0U) {
2397 dlog_verbose(
2398 "Retriever specified address ranges not "
2399 "supported (got offset %d).\n",
2400 current_receiver
2401 ->composite_memory_region_offset);
2402 return ffa_error(FFA_INVALID_PARAMETERS);
2403 }
2404
2405 /*
2406 * Check permissions from sender against permissions requested
2407 * by receiver.
2408 */
2409 if (!ffa_memory_retrieve_is_memory_access_valid(
2410 ffa_get_data_access_attr(sent_permissions),
2411 ffa_get_data_access_attr(requested_permissions),
2412 ffa_get_instruction_access_attr(sent_permissions),
2413 ffa_get_instruction_access_attr(
2414 requested_permissions),
2415 found_to_id ? permissions : NULL)) {
2416 return ffa_error(FFA_DENIED);
2417 }
2418
2419 /*
2420 * Can't request PM to clear memory if only provided with RO
2421 * permissions.
2422 */
2423 if (found_to_id &&
2424 (ffa_get_data_access_attr(*permissions) ==
2425 FFA_DATA_ACCESS_RO) &&
2426 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2427 0U) {
2428 dlog_verbose(
2429 "Receiver has RO permissions can not request "
2430 "clear.\n");
2431 return ffa_error(FFA_DENIED);
2432 }
2433 }
2434
2435 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2436 dlog_verbose(
2437 "Retrieve request does not contain caller's (%x) "
2438 "permissions\n",
2439 to_vm_id);
2440 return ffa_error(FFA_INVALID_PARAMETERS);
2441 }
2442
2443 return (struct ffa_value){.func = FFA_SUCCESS_32};
2444}
2445
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002446struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2447 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002448 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002449 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002450{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002451 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002452 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002453 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002454 sizeof(struct ffa_memory_access);
2455 ffa_memory_handle_t handle = retrieve_request->handle;
2456 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002457 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002458 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2459 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002460 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002461 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002462 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 struct ffa_memory_share_state *share_state;
2464 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002465 struct ffa_composite_memory_region *composite;
2466 uint32_t total_length;
2467 uint32_t fragment_length;
J-Alves96de29f2022-04-26 16:05:24 +01002468 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002469
2470 dump_share_states();
2471
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002472 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002473 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002474 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002475 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002476 expected_retrieve_request_length,
2477 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002478 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002479 }
2480
2481 share_states = share_states_lock();
2482 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002483 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002484 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002485 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002486 goto out;
2487 }
2488
J-Alves96de29f2022-04-26 16:05:24 +01002489 if (!share_state->sending_complete) {
2490 dlog_verbose(
2491 "Memory with handle %#x not fully sent, can't "
2492 "retrieve.\n",
2493 handle);
2494 ret = ffa_error(FFA_INVALID_PARAMETERS);
2495 goto out;
2496 }
2497
Andrew Walbrana65a1322020-04-06 19:32:32 +01002498 memory_region = share_state->memory_region;
2499 CHECK(memory_region != NULL);
2500
2501 /*
J-Alves96de29f2022-04-26 16:05:24 +01002502 * Find receiver index in the receivers list specified by the sender.
2503 */
2504 receiver_index =
2505 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2506
2507 if (receiver_index == memory_region->receiver_count) {
2508 dlog_verbose(
2509 "Incorrect receiver VM ID %x for FFA_MEM_RETRIEVE_REQ, "
2510 "for handle %#x.\n",
2511 to_locked.vm->id, handle);
2512 ret = ffa_error(FFA_INVALID_PARAMETERS);
2513 goto out;
2514 }
2515
2516 if (share_state->retrieved_fragment_count[receiver_index] != 0U) {
2517 dlog_verbose("Memory with handle %#x already retrieved.\n",
2518 handle);
2519 ret = ffa_error(FFA_DENIED);
2520 goto out;
2521 }
2522
2523 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002524 * Check that the transaction type expected by the receiver is correct,
2525 * if it has been specified.
2526 */
2527 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002528 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002529 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531 dlog_verbose(
2532 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002533 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002534 transaction_type,
2535 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002536 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002537 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 goto out;
2540 }
2541
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 if (retrieve_request->sender != memory_region->sender) {
2543 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002544 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002545 "expected %d for handle %#x.\n",
2546 retrieve_request->sender, memory_region->sender,
2547 handle);
J-Alves040c4ef2022-05-13 14:42:49 +01002548 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002549 goto out;
2550 }
2551
2552 if (retrieve_request->tag != memory_region->tag) {
2553 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002554 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555 "%d for handle %#x.\n",
2556 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002557 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 goto out;
2559 }
2560
Federico Recanati85090c42021-12-15 13:17:54 +01002561 if ((retrieve_request->flags &
J-Alves96de29f2022-04-26 16:05:24 +01002562 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
Federico Recanati85090c42021-12-15 13:17:54 +01002563 dlog_verbose(
2564 "Retriever specified 'address range alignment hint'"
2565 " not supported.\n");
2566 ret = ffa_error(FFA_INVALID_PARAMETERS);
2567 goto out;
2568 }
2569 if ((retrieve_request->flags &
2570 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2571 dlog_verbose(
2572 "Bits 8-5 must be zero in memory region's flags "
2573 "(address range alignment hint not supported).\n");
2574 ret = ffa_error(FFA_INVALID_PARAMETERS);
2575 goto out;
2576 }
2577
J-Alves84658fc2021-06-17 14:37:32 +01002578 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2579 dlog_verbose(
2580 "Bits 31-10 must be zero in memory region's flags.\n");
2581 ret = ffa_error(FFA_INVALID_PARAMETERS);
2582 goto out;
2583 }
2584
2585 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2586 (retrieve_request->flags &
2587 (FFA_MEMORY_REGION_FLAG_CLEAR |
2588 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2589 dlog_verbose(
2590 "Memory Share operation can't clean after relinquish "
2591 "memory region.\n");
2592 ret = ffa_error(FFA_INVALID_PARAMETERS);
2593 goto out;
2594 }
2595
Andrew Walbrana65a1322020-04-06 19:32:32 +01002596 /*
J-Alves17c069c2021-12-07 16:00:38 +00002597 * If the borrower needs the memory to be cleared before mapping to its
2598 * address space, the sender should have set the flag when calling
2599 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2600 */
2601 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
J-Alves96de29f2022-04-26 16:05:24 +01002602 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
J-Alves17c069c2021-12-07 16:00:38 +00002603 dlog_verbose(
2604 "Borrower needs memory cleared. Sender needs to set "
2605 "flag for clearing memory.\n");
2606 ret = ffa_error(FFA_DENIED);
2607 goto out;
2608 }
2609
J-Alves96de29f2022-04-26 16:05:24 +01002610 ret = ffa_memory_retrieve_validate_memory_access_list(
2611 memory_region, retrieve_request, to_locked.vm->id,
2612 &permissions);
2613 if (ret.func != FFA_SUCCESS_32) {
J-Alves84658fc2021-06-17 14:37:32 +01002614 goto out;
2615 }
2616
J-Alves614d9f42022-06-28 14:03:10 +01002617 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2618 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2619 /*
2620 * Ensure receiver's attributes are compatible with how Hafnium
2621 * maps memory: Normal Memory, Inner shareable, Write-Back
2622 * Read-Allocate Write-Allocate Cacheable.
2623 */
2624 ret = ffa_memory_attributes_validate(
2625 retrieve_request->attributes);
2626 if (ret.func != FFA_SUCCESS_32) {
2627 goto out;
2628 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002629 }
2630
J-Alves7cd5eb32020-10-16 19:06:10 +01002631 memory_to_attributes = ffa_memory_permissions_to_mode(
2632 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002633 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002634 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002635 share_state->fragment_constituent_counts,
2636 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002637 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002638 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002639 goto out;
2640 }
2641
2642 /*
2643 * Copy response to RX buffer of caller and deliver the message. This
2644 * must be done before the share_state is (possibly) freed.
2645 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002646 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002647 composite = ffa_memory_region_get_composite(memory_region, 0);
2648 /*
2649 * Constituents which we received in the first fragment should always
2650 * fit in the first fragment we are sending, because the header is the
2651 * same size in both cases and we have a fixed message buffer size. So
2652 * `ffa_retrieved_memory_region_init` should never fail.
2653 */
2654 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002655 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2656 memory_region->sender, memory_region->attributes,
2657 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002658 composite->page_count, composite->constituent_count,
2659 share_state->fragments[0],
2660 share_state->fragment_constituent_counts[0], &total_length,
2661 &fragment_length));
2662 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002663 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002664 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002665 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2666
J-Alves96de29f2022-04-26 16:05:24 +01002667 share_state->retrieved_fragment_count[receiver_index] = 1;
2668 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002669 share_state->fragment_count) {
2670 ffa_memory_retrieve_complete(share_states, share_state,
2671 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002672 }
2673
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002674 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002675 .arg1 = total_length,
2676 .arg2 = fragment_length};
2677
2678out:
2679 share_states_unlock(&share_states);
2680 dump_share_states();
2681 return ret;
2682}
2683
2684struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2685 ffa_memory_handle_t handle,
2686 uint32_t fragment_offset,
2687 struct mpool *page_pool)
2688{
2689 struct ffa_memory_region *memory_region;
2690 struct share_states_locked share_states;
2691 struct ffa_memory_share_state *share_state;
2692 struct ffa_value ret;
2693 uint32_t fragment_index;
2694 uint32_t retrieved_constituents_count;
2695 uint32_t i;
2696 uint32_t expected_fragment_offset;
2697 uint32_t remaining_constituent_count;
2698 uint32_t fragment_length;
2699
2700 dump_share_states();
2701
2702 share_states = share_states_lock();
2703 if (!get_share_state(share_states, handle, &share_state)) {
2704 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2705 handle);
2706 ret = ffa_error(FFA_INVALID_PARAMETERS);
2707 goto out;
2708 }
2709
2710 memory_region = share_state->memory_region;
2711 CHECK(memory_region != NULL);
2712
2713 if (memory_region->receivers[0].receiver_permissions.receiver !=
2714 to_locked.vm->id) {
2715 dlog_verbose(
2716 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2717 "of handle %#x.\n",
2718 to_locked.vm->id,
2719 memory_region->receivers[0]
2720 .receiver_permissions.receiver,
2721 handle);
2722 ret = ffa_error(FFA_INVALID_PARAMETERS);
2723 goto out;
2724 }
2725
2726 if (!share_state->sending_complete) {
2727 dlog_verbose(
2728 "Memory with handle %#x not fully sent, can't "
2729 "retrieve.\n",
2730 handle);
2731 ret = ffa_error(FFA_INVALID_PARAMETERS);
2732 goto out;
2733 }
2734
2735 if (share_state->retrieved_fragment_count[0] == 0 ||
2736 share_state->retrieved_fragment_count[0] >=
2737 share_state->fragment_count) {
2738 dlog_verbose(
2739 "Retrieval of memory with handle %#x not yet started "
2740 "or already completed (%d/%d fragments retrieved).\n",
2741 handle, share_state->retrieved_fragment_count[0],
2742 share_state->fragment_count);
2743 ret = ffa_error(FFA_INVALID_PARAMETERS);
2744 goto out;
2745 }
2746
2747 fragment_index = share_state->retrieved_fragment_count[0];
2748
2749 /*
2750 * Check that the given fragment offset is correct by counting how many
2751 * constituents were in the fragments previously sent.
2752 */
2753 retrieved_constituents_count = 0;
2754 for (i = 0; i < fragment_index; ++i) {
2755 retrieved_constituents_count +=
2756 share_state->fragment_constituent_counts[i];
2757 }
2758 expected_fragment_offset =
2759 ffa_composite_constituent_offset(memory_region, 0) +
2760 retrieved_constituents_count *
2761 sizeof(struct ffa_memory_region_constituent);
2762 if (fragment_offset != expected_fragment_offset) {
2763 dlog_verbose("Fragment offset was %d but expected %d.\n",
2764 fragment_offset, expected_fragment_offset);
2765 ret = ffa_error(FFA_INVALID_PARAMETERS);
2766 goto out;
2767 }
2768
2769 remaining_constituent_count = ffa_memory_fragment_init(
2770 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2771 share_state->fragments[fragment_index],
2772 share_state->fragment_constituent_counts[fragment_index],
2773 &fragment_length);
2774 CHECK(remaining_constituent_count == 0);
2775 to_locked.vm->mailbox.recv_size = fragment_length;
2776 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2777 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2778 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2779 share_state->retrieved_fragment_count[0]++;
2780 if (share_state->retrieved_fragment_count[0] ==
2781 share_state->fragment_count) {
2782 ffa_memory_retrieve_complete(share_states, share_state,
2783 page_pool);
2784 }
2785
2786 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2787 .arg1 = (uint32_t)handle,
2788 .arg2 = (uint32_t)(handle >> 32),
2789 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002790
2791out:
2792 share_states_unlock(&share_states);
2793 dump_share_states();
2794 return ret;
2795}
2796
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002797struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002798 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002799 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002800{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002801 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002802 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002803 struct ffa_memory_share_state *share_state;
2804 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002805 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002806 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002807
Andrew Walbrana65a1322020-04-06 19:32:32 +01002808 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002809 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002810 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002811 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002812 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002813 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002814 }
2815
Andrew Walbrana65a1322020-04-06 19:32:32 +01002816 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002817 dlog_verbose(
2818 "VM ID %d in relinquish message doesn't match calling "
2819 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002820 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002821 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002822 }
2823
2824 dump_share_states();
2825
2826 share_states = share_states_lock();
2827 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002828 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002829 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002830 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002831 goto out;
2832 }
2833
Andrew Walbranca808b12020-05-15 17:22:28 +01002834 if (!share_state->sending_complete) {
2835 dlog_verbose(
2836 "Memory with handle %#x not fully sent, can't "
2837 "relinquish.\n",
2838 handle);
2839 ret = ffa_error(FFA_INVALID_PARAMETERS);
2840 goto out;
2841 }
2842
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002843 memory_region = share_state->memory_region;
2844 CHECK(memory_region != NULL);
2845
Andrew Walbrana65a1322020-04-06 19:32:32 +01002846 if (memory_region->receivers[0].receiver_permissions.receiver !=
2847 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002848 dlog_verbose(
2849 "VM ID %d tried to relinquish memory region with "
2850 "handle %#x but receiver was %d.\n",
2851 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002852 memory_region->receivers[0]
2853 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002854 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002855 goto out;
2856 }
2857
Andrew Walbranca808b12020-05-15 17:22:28 +01002858 if (share_state->retrieved_fragment_count[0] !=
2859 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002860 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002861 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002862 "relinquish.\n",
2863 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002864 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002865 goto out;
2866 }
2867
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002868 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002869
2870 /*
2871 * Clear is not allowed for memory that was shared, as the original
2872 * sender still has access to the memory.
2873 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002874 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002875 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002876 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002877 goto out;
2878 }
2879
Andrew Walbranca808b12020-05-15 17:22:28 +01002880 ret = ffa_relinquish_check_update(
2881 from_locked, share_state->fragments,
2882 share_state->fragment_constituent_counts,
2883 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002884
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002885 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002886 /*
2887 * Mark memory handle as not retrieved, so it can be reclaimed
2888 * (or retrieved again).
2889 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002890 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002891 }
2892
2893out:
2894 share_states_unlock(&share_states);
2895 dump_share_states();
2896 return ret;
2897}
2898
2899/**
2900 * Validates that the reclaim transition is allowed for the given handle,
2901 * updates the page table of the reclaiming VM, and frees the internal state
2902 * associated with the handle.
2903 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002904struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002905 ffa_memory_handle_t handle,
2906 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002907 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002908{
2909 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002910 struct ffa_memory_share_state *share_state;
2911 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002912 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002913
2914 dump_share_states();
2915
2916 share_states = share_states_lock();
2917 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002918 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002919 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002920 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002921 goto out;
2922 }
2923
2924 memory_region = share_state->memory_region;
2925 CHECK(memory_region != NULL);
2926
2927 if (to_locked.vm->id != memory_region->sender) {
2928 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002929 "VM %#x attempted to reclaim memory handle %#x "
2930 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002931 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002932 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002933 goto out;
2934 }
2935
Andrew Walbranca808b12020-05-15 17:22:28 +01002936 if (!share_state->sending_complete) {
2937 dlog_verbose(
2938 "Memory with handle %#x not fully sent, can't "
2939 "reclaim.\n",
2940 handle);
2941 ret = ffa_error(FFA_INVALID_PARAMETERS);
2942 goto out;
2943 }
2944
2945 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002946 dlog_verbose(
2947 "Tried to reclaim memory handle %#x that has not been "
2948 "relinquished.\n",
2949 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002950 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002951 goto out;
2952 }
2953
Andrew Walbranca808b12020-05-15 17:22:28 +01002954 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002955 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002956 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002957 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002958 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002959
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002960 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002961 share_state_free(share_states, share_state, page_pool);
2962 dlog_verbose("Freed share state after successful reclaim.\n");
2963 }
2964
2965out:
2966 share_states_unlock(&share_states);
2967 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002968}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002969
2970/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002971 * Validates that the reclaim transition is allowed for the memory region with
2972 * the given handle which was previously shared with the TEE, tells the TEE to
2973 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2974 *
2975 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002976 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002977struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002978 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002979 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002980 ffa_memory_region_flags_t flags,
2981 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002982{
Andrew Walbranca808b12020-05-15 17:22:28 +01002983 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2984 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2985 struct ffa_value tee_ret;
2986 uint32_t length;
2987 uint32_t fragment_length;
2988 uint32_t fragment_offset;
2989 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002990 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002991 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2992
2993 CHECK(request_length <= HF_MAILBOX_SIZE);
2994 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2995
2996 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002997 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002998 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2999 .arg1 = request_length,
3000 .arg2 = request_length});
3001 if (tee_ret.func == FFA_ERROR_32) {
3002 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
3003 return tee_ret;
3004 }
3005 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
3006 dlog_verbose(
3007 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
3008 tee_ret.func);
3009 return ffa_error(FFA_INVALID_PARAMETERS);
3010 }
3011
3012 length = tee_ret.arg1;
3013 fragment_length = tee_ret.arg2;
3014
3015 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
3016 length > sizeof(tee_retrieve_buffer)) {
3017 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
3018 fragment_length, length, HF_MAILBOX_SIZE,
3019 sizeof(tee_retrieve_buffer));
3020 return ffa_error(FFA_INVALID_PARAMETERS);
3021 }
3022
3023 /*
3024 * Copy the first fragment of the memory region descriptor to an
3025 * internal buffer.
3026 */
3027 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
3028 from_locked.vm->mailbox.send, fragment_length);
3029
3030 /* Fetch the remaining fragments into the same buffer. */
3031 fragment_offset = fragment_length;
3032 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02003033 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003034 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
3035 .arg1 = (uint32_t)handle,
3036 .arg2 = (uint32_t)(handle >> 32),
3037 .arg3 = fragment_offset});
3038 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
3039 dlog_verbose(
3040 "Got %#x (%d) from TEE in response to "
3041 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
3042 tee_ret.func, tee_ret.arg2);
3043 return tee_ret;
3044 }
3045 if (ffa_frag_handle(tee_ret) != handle) {
3046 dlog_verbose(
3047 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
3048 "in response to FFA_MEM_FRAG_RX for handle "
3049 "%#x.\n",
3050 ffa_frag_handle(tee_ret), handle);
3051 return ffa_error(FFA_INVALID_PARAMETERS);
3052 }
3053 if (ffa_frag_sender(tee_ret) != 0) {
3054 dlog_verbose(
3055 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
3056 "(expected 0).\n",
3057 ffa_frag_sender(tee_ret));
3058 return ffa_error(FFA_INVALID_PARAMETERS);
3059 }
3060 fragment_length = tee_ret.arg3;
3061 if (fragment_length > HF_MAILBOX_SIZE ||
3062 fragment_offset + fragment_length > length) {
3063 dlog_verbose(
3064 "Invalid fragment length %d at offset %d (max "
3065 "%d).\n",
3066 fragment_length, fragment_offset,
3067 HF_MAILBOX_SIZE);
3068 return ffa_error(FFA_INVALID_PARAMETERS);
3069 }
3070 memcpy_s(tee_retrieve_buffer + fragment_offset,
3071 sizeof(tee_retrieve_buffer) - fragment_offset,
3072 from_locked.vm->mailbox.send, fragment_length);
3073
3074 fragment_offset += fragment_length;
3075 }
3076
3077 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00003078
3079 if (memory_region->receiver_count != 1) {
3080 /* Only one receiver supported by Hafnium for now. */
3081 dlog_verbose(
3082 "Multiple recipients not supported (got %d, expected "
3083 "1).\n",
3084 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01003085 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003086 }
3087
3088 if (memory_region->handle != handle) {
3089 dlog_verbose(
3090 "Got memory region handle %#x from TEE but requested "
3091 "handle %#x.\n",
3092 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003093 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003094 }
3095
3096 /* The original sender must match the caller. */
3097 if (to_locked.vm->id != memory_region->sender) {
3098 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003099 "VM %#x attempted to reclaim memory handle %#x "
3100 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003101 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003102 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003103 }
3104
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003105 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003106
3107 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01003108 * Validate that the reclaim transition is allowed for the given memory
3109 * region, forward the request to the TEE and then map the memory back
3110 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003111 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01003112 return ffa_tee_reclaim_check_update(
3113 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01003114 composite->constituent_count, memory_to_attributes,
3115 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003116}