blob: 78a9574e24f9b40df225c06ae574e468ce4f3a8e [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/** The maximum number of recipients a memory region may be sent to. */
25#define MAX_MEM_SHARE_RECIPIENTS 1
26
27/**
28 * The maximum number of memory sharing handles which may be active at once. A
29 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
30 * or LEND handle is active from when it is sent to when it is reclaimed.
31 */
32#define MAX_MEM_SHARES 100
33
Andrew Walbranca808b12020-05-15 17:22:28 +010034/**
35 * The maximum number of fragments into which a memory sharing message may be
36 * broken.
37 */
38#define MAX_FRAGMENTS 20
39
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
41 "struct ffa_memory_region_constituent 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_composite_memory_region) % 16 == 0,
44 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000045 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010046static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010047 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010048static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
49 "struct ffa_memory_access must be a multiple of 16 bytes long.");
50static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
51 "struct ffa_memory_region must be a multiple of 16 bytes long.");
52static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
53 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000055
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010056struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000057 /**
58 * The memory region being shared, or NULL if this share state is
59 * unallocated.
60 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010061 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000062
Andrew Walbranca808b12020-05-15 17:22:28 +010063 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
64
65 /** The number of constituents in each fragment. */
66 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
67
68 /**
69 * The number of valid elements in the `fragments` and
70 * `fragment_constituent_counts` arrays.
71 */
72 uint32_t fragment_count;
73
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010075 * The FF-A function used for sharing the memory. Must be one of
76 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000077 * share state is allocated, or 0.
78 */
79 uint32_t share_func;
80
81 /**
J-Alves2a0d2882020-10-29 14:49:50 +000082 * The sender's original mode before invoking the FF-A function for
83 * sharing the memory.
84 * This is used to reset the original configuration when sender invokes
85 * FFA_MEM_RECLAIM_32.
86 */
87 uint32_t sender_orig_mode;
88
89 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010090 * True if all the fragments of this sharing request have been sent and
91 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092 */
Andrew Walbranca808b12020-05-15 17:22:28 +010093 bool sending_complete;
94
95 /**
96 * How many fragments of the memory region each recipient has retrieved
97 * so far. The order of this array matches the order of the endpoint
98 * memory access descriptors in the memory region descriptor. Any
99 * entries beyond the receiver_count will always be 0.
100 */
101 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000102};
103
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000104/**
105 * Encapsulates the set of share states while the `share_states_lock` is held.
106 */
107struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100108 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000109};
110
111/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100112 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000113 * by this lock.
114 */
115static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100116static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000117
118/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100119 * Buffer for retrieving memory region information from the TEE for when a
120 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
121 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100123alignas(PAGE_SIZE) static uint8_t
124 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
125
126/**
J-Alves917d2f22020-10-30 18:39:30 +0000127 * Extracts the index from a memory handle allocated by Hafnium's current world.
128 */
129uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
130{
131 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
132}
133
134/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 * Initialises the next available `struct ffa_memory_share_state` and sets
136 * `share_state_ret` to a pointer to it. If `handle` is
137 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
138 * uses the provided handle which is assumed to be globally unique.
139 *
140 * Returns true on success or false if none are available.
141 */
142static bool allocate_share_state(
143 struct share_states_locked share_states, uint32_t share_func,
144 struct ffa_memory_region *memory_region, uint32_t fragment_length,
145 ffa_memory_handle_t handle,
146 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000147{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100148 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000150 assert(share_states.share_states != NULL);
151 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100154 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000155 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100156 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100157 &share_states.share_states[i];
158 struct ffa_composite_memory_region *composite =
159 ffa_memory_region_get_composite(memory_region,
160 0);
161
162 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000163 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200164 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000166 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 allocated_state->share_func = share_func;
169 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100170 allocated_state->fragment_count = 1;
171 allocated_state->fragments[0] = composite->constituents;
172 allocated_state->fragment_constituent_counts[0] =
173 (fragment_length -
174 ffa_composite_constituent_offset(memory_region,
175 0)) /
176 sizeof(struct ffa_memory_region_constituent);
177 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000178 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 allocated_state->retrieved_fragment_count[j] =
180 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100182 if (share_state_ret != NULL) {
183 *share_state_ret = allocated_state;
184 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185 return true;
186 }
187 }
188
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189 return false;
190}
191
192/** Locks the share states lock. */
193struct share_states_locked share_states_lock(void)
194{
195 sl_lock(&share_states_lock_instance);
196
197 return (struct share_states_locked){.share_states = share_states};
198}
199
200/** Unlocks the share states lock. */
201static void share_states_unlock(struct share_states_locked *share_states)
202{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000203 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000204 share_states->share_states = NULL;
205 sl_unlock(&share_states_lock_instance);
206}
207
208/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100209 * If the given handle is a valid handle for an allocated share state then
210 * initialises `share_state_ret` to point to the share state and returns true.
211 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000212 */
213static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100214 ffa_memory_handle_t handle,
215 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100217 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000218 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000219
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000220 assert(share_states.share_states != NULL);
221 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100222
223 /*
224 * First look for a share_state allocated by us, in which case the
225 * handle is based on the index.
226 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200227 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000228 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100229 if (index < MAX_MEM_SHARES) {
230 share_state = &share_states.share_states[index];
231 if (share_state->share_func != 0) {
232 *share_state_ret = share_state;
233 return true;
234 }
235 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000236 }
237
Andrew Walbranca808b12020-05-15 17:22:28 +0100238 /* Fall back to a linear scan. */
239 for (index = 0; index < MAX_MEM_SHARES; ++index) {
240 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000241 if (share_state->memory_region != NULL &&
242 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100243 share_state->share_func != 0) {
244 *share_state_ret = share_state;
245 return true;
246 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247 }
248
Andrew Walbranca808b12020-05-15 17:22:28 +0100249 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000250}
251
252/** Marks a share state as unallocated. */
253static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255 struct mpool *page_pool)
256{
Andrew Walbranca808b12020-05-15 17:22:28 +0100257 uint32_t i;
258
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100261 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000262 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100263 /*
264 * First fragment is part of the same page as the `memory_region`, so it
265 * doesn't need to be freed separately.
266 */
267 share_state->fragments[0] = NULL;
268 share_state->fragment_constituent_counts[0] = 0;
269 for (i = 1; i < share_state->fragment_count; ++i) {
270 mpool_free(page_pool, share_state->fragments[i]);
271 share_state->fragments[i] = NULL;
272 share_state->fragment_constituent_counts[i] = 0;
273 }
274 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275 share_state->memory_region = NULL;
276}
277
Andrew Walbranca808b12020-05-15 17:22:28 +0100278/** Checks whether the given share state has been fully sent. */
279static bool share_state_sending_complete(
280 struct share_states_locked share_states,
281 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000282{
Andrew Walbranca808b12020-05-15 17:22:28 +0100283 struct ffa_composite_memory_region *composite;
284 uint32_t expected_constituent_count;
285 uint32_t fragment_constituent_count_total = 0;
286 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000287
Andrew Walbranca808b12020-05-15 17:22:28 +0100288 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000289 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100290
291 /*
292 * Share state must already be valid, or it's not possible to get hold
293 * of it.
294 */
295 CHECK(share_state->memory_region != NULL &&
296 share_state->share_func != 0);
297
298 composite =
299 ffa_memory_region_get_composite(share_state->memory_region, 0);
300 expected_constituent_count = composite->constituent_count;
301 for (i = 0; i < share_state->fragment_count; ++i) {
302 fragment_constituent_count_total +=
303 share_state->fragment_constituent_counts[i];
304 }
305 dlog_verbose(
306 "Checking completion: constituent count %d/%d from %d "
307 "fragments.\n",
308 fragment_constituent_count_total, expected_constituent_count,
309 share_state->fragment_count);
310
311 return fragment_constituent_count_total == expected_constituent_count;
312}
313
314/**
315 * Calculates the offset of the next fragment expected for the given share
316 * state.
317 */
318static uint32_t share_state_next_fragment_offset(
319 struct share_states_locked share_states,
320 struct ffa_memory_share_state *share_state)
321{
322 uint32_t next_fragment_offset;
323 uint32_t i;
324
325 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000326 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100327
328 next_fragment_offset =
329 ffa_composite_constituent_offset(share_state->memory_region, 0);
330 for (i = 0; i < share_state->fragment_count; ++i) {
331 next_fragment_offset +=
332 share_state->fragment_constituent_counts[i] *
333 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334 }
335
Andrew Walbranca808b12020-05-15 17:22:28 +0100336 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337}
338
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340{
341 uint32_t i;
342
343 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
344 return;
345 }
346
Olivier Deprez935e1b12020-12-22 18:01:29 +0100347 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100348 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 "recipients [",
350 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100351 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100352 memory_region->receiver_count);
353 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 if (i != 0) {
355 dlog(", ");
356 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100357 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100358 memory_region->receivers[i].receiver_permissions.receiver,
359 memory_region->receivers[i]
360 .receiver_permissions.permissions,
361 memory_region->receivers[i]
362 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000363 }
364 dlog("]");
365}
366
367static void dump_share_states(void)
368{
369 uint32_t i;
370
371 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
372 return;
373 }
374
375 dlog("Current share states:\n");
376 sl_lock(&share_states_lock_instance);
377 for (i = 0; i < MAX_MEM_SHARES; ++i) {
378 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000381 dlog("SHARE");
382 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 dlog("LEND");
385 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000387 dlog("DONATE");
388 break;
389 default:
390 dlog("invalid share_func %#x",
391 share_states[i].share_func);
392 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100393 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 if (share_states[i].sending_complete) {
396 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000397 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100398 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000399 }
J-Alves2a0d2882020-10-29 14:49:50 +0000400 dlog(" with %d fragments, %d retrieved, "
401 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100402 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000403 share_states[i].retrieved_fragment_count[0],
404 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000405 }
406 }
407 sl_unlock(&share_states_lock_instance);
408}
409
Andrew Walbran475c1452020-02-07 13:22:22 +0000410/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100412 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000413{
414 uint32_t mode = 0;
415
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416 switch (ffa_get_data_access_attr(permissions)) {
417 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000418 mode = MM_MODE_R;
419 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100420 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000421 mode = MM_MODE_R | MM_MODE_W;
422 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100423 case FFA_DATA_ACCESS_NOT_SPECIFIED:
424 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
425 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 case FFA_DATA_ACCESS_RESERVED:
427 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100428 }
429
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 switch (ffa_get_instruction_access_attr(permissions)) {
431 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000432 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 mode |= MM_MODE_X;
435 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100436 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
437 mode |= (default_mode & MM_MODE_X);
438 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100439 case FFA_INSTRUCTION_ACCESS_RESERVED:
440 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000441 }
442
443 return mode;
444}
445
Jose Marinho75509b42019-04-09 09:34:59 +0100446/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000447 * Get the current mode in the stage-2 page table of the given vm of all the
448 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100449 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100450 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100451static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000452 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 struct ffa_memory_region_constituent **fragments,
454 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100455{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100456 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100457 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100458
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000461 * Fail if there are no constituents. Otherwise we would get an
462 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100463 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100464 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100465 }
466
Andrew Walbranca808b12020-05-15 17:22:28 +0100467 for (i = 0; i < fragment_count; ++i) {
468 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
469 ipaddr_t begin = ipa_init(fragments[i][j].address);
470 size_t size = fragments[i][j].page_count * PAGE_SIZE;
471 ipaddr_t end = ipa_add(begin, size);
472 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100473
Andrew Walbranca808b12020-05-15 17:22:28 +0100474 /* Fail if addresses are not page-aligned. */
475 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
476 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
477 return ffa_error(FFA_INVALID_PARAMETERS);
478 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100479
Andrew Walbranca808b12020-05-15 17:22:28 +0100480 /*
481 * Ensure that this constituent memory range is all
482 * mapped with the same mode.
483 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800484 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100485 return ffa_error(FFA_DENIED);
486 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100487
Andrew Walbranca808b12020-05-15 17:22:28 +0100488 /*
489 * Ensure that all constituents are mapped with the same
490 * mode.
491 */
492 if (i == 0) {
493 *orig_mode = current_mode;
494 } else if (current_mode != *orig_mode) {
495 dlog_verbose(
496 "Expected mode %#x but was %#x for %d "
497 "pages at %#x.\n",
498 *orig_mode, current_mode,
499 fragments[i][j].page_count,
500 ipa_addr(begin));
501 return ffa_error(FFA_DENIED);
502 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100503 }
Jose Marinho75509b42019-04-09 09:34:59 +0100504 }
505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000507}
508
509/**
510 * Verify that all pages have the same mode, that the starting mode
511 * constitutes a valid state and obtain the next mode to apply
512 * to the sending VM.
513 *
514 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 * 1) FFA_DENIED if a state transition was not found;
516 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100517 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100518 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100519 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
521 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000522 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100524 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100526 struct ffa_memory_region_constituent **fragments,
527 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
528 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000529{
530 const uint32_t state_mask =
531 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100532 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100533 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000534
Andrew Walbranca808b12020-05-15 17:22:28 +0100535 ret = constituents_get_mode(from, orig_from_mode, fragments,
536 fragment_constituent_counts,
537 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100538 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100539 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100540 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100541 }
542
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000543 /* Ensure the address range is normal memory and not a device. */
544 if (*orig_from_mode & MM_MODE_D) {
545 dlog_verbose("Can't share device memory (mode is %#x).\n",
546 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100547 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000548 }
549
550 /*
551 * Ensure the sender is the owner and has exclusive access to the
552 * memory.
553 */
554 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100555 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100556 }
557
J-Alves7cd5eb32020-10-16 19:06:10 +0100558 required_from_mode =
559 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
560
Andrew Walbrana65a1322020-04-06 19:32:32 +0100561 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
562 dlog_verbose(
563 "Sender tried to send memory with permissions which "
564 "required mode %#x but only had %#x itself.\n",
565 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567 }
568
569 /* Find the appropriate new mode. */
570 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000571 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100572 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100574 break;
575
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000577 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100578 break;
579
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100580 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000581 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100582 break;
583
Jose Marinho75509b42019-04-09 09:34:59 +0100584 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100586 }
587
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100588 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000589}
590
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000592 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100593 struct ffa_memory_region_constituent **fragments,
594 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
595 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000596{
597 const uint32_t state_mask =
598 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
599 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100600 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601
Andrew Walbranca808b12020-05-15 17:22:28 +0100602 ret = constituents_get_mode(from, orig_from_mode, fragments,
603 fragment_constituent_counts,
604 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100606 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000607 }
608
609 /* Ensure the address range is normal memory and not a device. */
610 if (*orig_from_mode & MM_MODE_D) {
611 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
612 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100613 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000614 }
615
616 /*
617 * Ensure the relinquishing VM is not the owner but has access to the
618 * memory.
619 */
620 orig_from_state = *orig_from_mode & state_mask;
621 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
622 dlog_verbose(
623 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100624 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100626 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000627 }
628
629 /* Find the appropriate new mode. */
630 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
631
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000633}
634
635/**
636 * Verify that all pages have the same mode, that the starting mode
637 * constitutes a valid state and obtain the next mode to apply
638 * to the retrieving VM.
639 *
640 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100641 * 1) FFA_DENIED if a state transition was not found;
642 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100643 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100645 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
647 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000648 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100649static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000650 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100651 struct ffa_memory_region_constituent **fragments,
652 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
653 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000654{
655 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100656 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000657
Andrew Walbranca808b12020-05-15 17:22:28 +0100658 ret = constituents_get_mode(to, &orig_to_mode, fragments,
659 fragment_constituent_counts,
660 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100662 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100663 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000664 }
665
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000667 const uint32_t state_mask =
668 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
669 uint32_t orig_to_state = orig_to_mode & state_mask;
670
J-Alves9256f162021-12-09 13:18:43 +0000671 /*
672 * If the original ffa memory send call has been processed
673 * successfully, it is expected the orig_to_mode would overlay
674 * with `state_mask`, as a result of the function
675 * `ffa_send_check_transition`.
676 */
677 assert(orig_to_state != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000678 } else {
679 /*
680 * Ensure the retriever has the expected state. We don't care
681 * about the MM_MODE_SHARED bit; either with or without it set
682 * are both valid representations of the !O-NA state.
683 */
684 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
685 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000687 }
688 }
689
690 /* Find the appropriate new mode. */
691 *to_mode = memory_to_attributes;
692 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000694 *to_mode |= 0;
695 break;
696
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100697 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000698 *to_mode |= MM_MODE_UNOWNED;
699 break;
700
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000702 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
703 break;
704
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100705 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000706 *to_mode |= 0;
707 break;
708
709 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100710 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000712 }
713
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100714 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100715}
Jose Marinho09b1db82019-08-08 09:16:59 +0100716
717/**
718 * Updates a VM's page table such that the given set of physical address ranges
719 * are mapped in the address space at the corresponding address ranges, in the
720 * mode provided.
721 *
722 * If commit is false, the page tables will be allocated from the mpool but no
723 * mappings will actually be updated. This function must always be called first
724 * with commit false to check that it will succeed before calling with commit
725 * true, to avoid leaving the page table in a half-updated state. To make a
726 * series of changes atomically you can call them all with commit false before
727 * calling them all with commit true.
728 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700729 * vm_ptable_defrag should always be called after a series of page table
730 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100731 *
732 * Returns true on success, or false if the update failed and no changes were
733 * made to memory mappings.
734 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100735static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000736 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100737 struct ffa_memory_region_constituent **fragments,
738 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100739 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100740{
Andrew Walbranca808b12020-05-15 17:22:28 +0100741 uint32_t i;
742 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100743
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700744 if (vm_locked.vm->el0_partition) {
745 mode |= MM_MODE_USER | MM_MODE_NG;
746 }
747
Andrew Walbranca808b12020-05-15 17:22:28 +0100748 /* Iterate over the memory region constituents within each fragment. */
749 for (i = 0; i < fragment_count; ++i) {
750 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
751 size_t size = fragments[i][j].page_count * PAGE_SIZE;
752 paddr_t pa_begin =
753 pa_from_ipa(ipa_init(fragments[i][j].address));
754 paddr_t pa_end = pa_add(pa_begin, size);
Federico Recanati4fd065d2021-12-13 20:06:23 +0100755 uint32_t pa_range = arch_mm_get_pa_range();
756
757 /*
758 * Ensure the requested region falls into system's PA
759 * range.
760 */
761 if (((pa_addr(pa_begin) >> pa_range) > 0) ||
762 ((pa_addr(pa_end) >> pa_range) > 0)) {
763 dlog_error("Region is outside of PA Range\n");
764 return false;
765 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100766
767 if (commit) {
768 vm_identity_commit(vm_locked, pa_begin, pa_end,
769 mode, ppool, NULL);
770 } else if (!vm_identity_prepare(vm_locked, pa_begin,
771 pa_end, mode, ppool)) {
772 return false;
773 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100774 }
775 }
776
777 return true;
778}
779
780/**
781 * Clears a region of physical memory by overwriting it with zeros. The data is
782 * flushed from the cache so the memory has been cleared across the system.
783 */
J-Alves7db32002021-12-14 14:44:50 +0000784static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
785 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100786{
787 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000788 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100789 * global mapping of the whole range. Such an approach will limit
790 * the changes to stage-1 tables and will allow only local
791 * invalidation.
792 */
793 bool ret;
794 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000795 void *ptr = mm_identity_map(stage1_locked, begin, end,
796 MM_MODE_W | (extra_mode_attributes &
797 plat_ffa_other_world_mode()),
798 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100799 size_t size = pa_difference(begin, end);
800
801 if (!ptr) {
802 /* TODO: partial defrag of failed range. */
803 /* Recover any memory consumed in failed mapping. */
804 mm_defrag(stage1_locked, ppool);
805 goto fail;
806 }
807
808 memset_s(ptr, size, 0, size);
809 arch_mm_flush_dcache(ptr, size);
810 mm_unmap(stage1_locked, begin, end, ppool);
811
812 ret = true;
813 goto out;
814
815fail:
816 ret = false;
817
818out:
819 mm_unlock_stage1(&stage1_locked);
820
821 return ret;
822}
823
824/**
825 * Clears a region of physical memory by overwriting it with zeros. The data is
826 * flushed from the cache so the memory has been cleared across the system.
827 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000829 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100830 struct ffa_memory_region_constituent **fragments,
831 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
832 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100833{
834 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100835 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100836 struct mm_stage1_locked stage1_locked;
837 bool ret = false;
838
839 /*
840 * Create a local pool so any freed memory can't be used by another
841 * thread. This is to ensure each constituent that is mapped can be
842 * unmapped again afterwards.
843 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000844 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100845
Andrew Walbranca808b12020-05-15 17:22:28 +0100846 /* Iterate over the memory region constituents within each fragment. */
847 for (i = 0; i < fragment_count; ++i) {
848 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100849
Andrew Walbranca808b12020-05-15 17:22:28 +0100850 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
851 size_t size = fragments[i][j].page_count * PAGE_SIZE;
852 paddr_t begin =
853 pa_from_ipa(ipa_init(fragments[i][j].address));
854 paddr_t end = pa_add(begin, size);
855
J-Alves7db32002021-12-14 14:44:50 +0000856 if (!clear_memory(begin, end, &local_page_pool,
857 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100858 /*
859 * api_clear_memory will defrag on failure, so
860 * no need to do it here.
861 */
862 goto out;
863 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100864 }
865 }
866
867 /*
868 * Need to defrag after clearing, as it may have added extra mappings to
869 * the stage 1 page table.
870 */
871 stage1_locked = mm_lock_stage1();
872 mm_defrag(stage1_locked, &local_page_pool);
873 mm_unlock_stage1(&stage1_locked);
874
875 ret = true;
876
877out:
878 mpool_fini(&local_page_pool);
879 return ret;
880}
881
882/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000883 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100884 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100886 *
887 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100889 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100890 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100891 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
892 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100893 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100894 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100897static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000898 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100899 struct ffa_memory_region_constituent **fragments,
900 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
901 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100902 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100903{
Andrew Walbranca808b12020-05-15 17:22:28 +0100904 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100905 uint32_t orig_from_mode;
906 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100907 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100909
910 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100911 * Make sure constituents are properly aligned to a 64-bit boundary. If
912 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100913 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100914 for (i = 0; i < fragment_count; ++i) {
915 if (!is_aligned(fragments[i], 8)) {
916 dlog_verbose("Constituents not aligned.\n");
917 return ffa_error(FFA_INVALID_PARAMETERS);
918 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100919 }
920
921 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 * Check if the state transition is lawful for the sender, ensure that
923 * all constituents of a memory region being shared are at the same
924 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100925 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 &orig_from_mode, fragments,
928 fragment_constituent_counts,
929 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100930 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100931 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100932 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100933 }
934
Andrew Walbran37c574e2020-06-03 11:45:46 +0100935 if (orig_from_mode_ret != NULL) {
936 *orig_from_mode_ret = orig_from_mode;
937 }
938
Jose Marinho09b1db82019-08-08 09:16:59 +0100939 /*
940 * Create a local pool so any freed memory can't be used by another
941 * thread. This is to ensure the original mapping can be restored if the
942 * clear fails.
943 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000944 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100945
946 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000947 * First reserve all required memory for the new page table entries
948 * without committing, to make sure the entire operation will succeed
949 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100950 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100951 if (!ffa_region_group_identity_map(
952 from_locked, fragments, fragment_constituent_counts,
953 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100954 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100956 goto out;
957 }
958
959 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000960 * Update the mapping for the sender. This won't allocate because the
961 * transaction was already prepared above, but may free pages in the
962 * case that a whole block is being unmapped that was previously
963 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100964 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100965 CHECK(ffa_region_group_identity_map(
966 from_locked, fragments, fragment_constituent_counts,
967 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100968
969 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000970 if (clear &&
971 !ffa_clear_memory_constituents(
972 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
973 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100974 /*
975 * On failure, roll back by returning memory to the sender. This
976 * may allocate pages which were previously freed into
977 * `local_page_pool` by the call above, but will never allocate
978 * more pages than that so can never fail.
979 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100980 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100981 from_locked, fragments, fragment_constituent_counts,
982 fragment_count, orig_from_mode, &local_page_pool,
983 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100984
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100985 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100986 goto out;
987 }
988
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100989 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000990
991out:
992 mpool_fini(&local_page_pool);
993
994 /*
995 * Tidy up the page table by reclaiming failed mappings (if there was an
996 * error) or merging entries into blocks where possible (on success).
997 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700998 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000999
1000 return ret;
1001}
1002
1003/**
1004 * Validates and maps memory shared from one VM to another.
1005 *
1006 * This function requires the calling context to hold the <to> lock.
1007 *
1008 * Returns:
1009 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001010 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001012 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001016static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001017 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001018 struct ffa_memory_region_constituent **fragments,
1019 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1020 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1021 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022{
Andrew Walbranca808b12020-05-15 17:22:28 +01001023 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001024 uint32_t to_mode;
1025 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027
1028 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001029 * Make sure constituents are properly aligned to a 64-bit boundary. If
1030 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001032 for (i = 0; i < fragment_count; ++i) {
1033 if (!is_aligned(fragments[i], 8)) {
1034 return ffa_error(FFA_INVALID_PARAMETERS);
1035 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001036 }
1037
1038 /*
1039 * Check if the state transition is lawful for the recipient, and ensure
1040 * that all constituents of the memory region being retrieved are at the
1041 * same state.
1042 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001043 ret = ffa_retrieve_check_transition(
1044 to_locked, share_func, fragments, fragment_constituent_counts,
1045 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001047 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001048 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 }
1050
1051 /*
1052 * Create a local pool so any freed memory can't be used by another
1053 * thread. This is to ensure the original mapping can be restored if the
1054 * clear fails.
1055 */
1056 mpool_init_with_fallback(&local_page_pool, page_pool);
1057
1058 /*
1059 * First reserve all required memory for the new page table entries in
1060 * the recipient page tables without committing, to make sure the entire
1061 * operation will succeed without exhausting the page pool.
1062 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001063 if (!ffa_region_group_identity_map(
1064 to_locked, fragments, fragment_constituent_counts,
1065 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001066 /* TODO: partial defrag of failed range. */
1067 dlog_verbose(
1068 "Insufficient memory to update recipient page "
1069 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001071 goto out;
1072 }
1073
1074 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001075 if (clear &&
1076 !ffa_clear_memory_constituents(
1077 plat_ffa_owner_world_mode(from_id), fragments,
1078 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001079 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001080 goto out;
1081 }
1082
Jose Marinho09b1db82019-08-08 09:16:59 +01001083 /*
1084 * Complete the transfer by mapping the memory into the recipient. This
1085 * won't allocate because the transaction was already prepared above, so
1086 * it doesn't need to use the `local_page_pool`.
1087 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001088 CHECK(ffa_region_group_identity_map(
1089 to_locked, fragments, fragment_constituent_counts,
1090 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001091
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001092 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001093
1094out:
1095 mpool_fini(&local_page_pool);
1096
1097 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001098 * Tidy up the page table by reclaiming failed mappings (if there was an
1099 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001100 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001101 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001102
1103 return ret;
1104}
1105
Andrew Walbran290b0c92020-02-03 16:37:14 +00001106/**
1107 * Reclaims the given memory from the TEE. To do this space is first reserved in
1108 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1109 * then (if that is successful) the memory is mapped back into the <to> VM's
1110 * page table.
1111 *
1112 * This function requires the calling context to hold the <to> lock.
1113 *
1114 * Returns:
1115 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001117 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001118 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001119 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001121 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001122static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 struct vm_locked to_locked, ffa_memory_handle_t handle,
1124 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001125 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1126 struct mpool *page_pool)
1127{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001128 uint32_t to_mode;
1129 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001130 struct ffa_value ret;
1131 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001132
1133 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001134 * Make sure constituents are properly aligned to a 64-bit boundary. If
1135 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001136 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001137 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001138 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001140 }
1141
1142 /*
1143 * Check if the state transition is lawful for the recipient, and ensure
1144 * that all constituents of the memory region being retrieved are at the
1145 * same state.
1146 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 &constituents, &constituent_count,
1149 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001151 dlog_verbose("Invalid transition.\n");
1152 return ret;
1153 }
1154
1155 /*
1156 * Create a local pool so any freed memory can't be used by another
1157 * thread. This is to ensure the original mapping can be restored if the
1158 * clear fails.
1159 */
1160 mpool_init_with_fallback(&local_page_pool, page_pool);
1161
1162 /*
1163 * First reserve all required memory for the new page table entries in
1164 * the recipient page tables without committing, to make sure the entire
1165 * operation will succeed without exhausting the page pool.
1166 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001167 if (!ffa_region_group_identity_map(to_locked, &constituents,
1168 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001169 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001170 /* TODO: partial defrag of failed range. */
1171 dlog_verbose(
1172 "Insufficient memory to update recipient page "
1173 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001174 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001175 goto out;
1176 }
1177
1178 /*
1179 * Forward the request to the TEE and see what happens.
1180 */
1181 tee_flags = 0;
1182 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001183 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001184 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001185 ret = arch_other_world_call(
1186 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1187 .arg1 = (uint32_t)handle,
1188 .arg2 = (uint32_t)(handle >> 32),
1189 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001190
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001191 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001192 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001193 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1194 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001195 ret.func, ret.arg2);
1196 goto out;
1197 }
1198
1199 /*
1200 * The TEE was happy with it, so complete the reclaim by mapping the
1201 * memory into the recipient. This won't allocate because the
1202 * transaction was already prepared above, so it doesn't need to use the
1203 * `local_page_pool`.
1204 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001205 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1206 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001207 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001208
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001210
1211out:
1212 mpool_fini(&local_page_pool);
1213
1214 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001215 * Tidy up the page table by reclaiming failed mappings (if there was an
1216 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001217 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001218 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001219
1220 return ret;
1221}
1222
Andrew Walbran996d1d12020-05-27 14:08:43 +01001223static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001224 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001225 struct ffa_memory_region_constituent **fragments,
1226 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1227 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001228{
1229 uint32_t orig_from_mode;
1230 uint32_t from_mode;
1231 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001232 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001233
Andrew Walbranca808b12020-05-15 17:22:28 +01001234 ret = ffa_relinquish_check_transition(
1235 from_locked, &orig_from_mode, fragments,
1236 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001238 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001239 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240 }
1241
1242 /*
1243 * Create a local pool so any freed memory can't be used by another
1244 * thread. This is to ensure the original mapping can be restored if the
1245 * clear fails.
1246 */
1247 mpool_init_with_fallback(&local_page_pool, page_pool);
1248
1249 /*
1250 * First reserve all required memory for the new page table entries
1251 * without committing, to make sure the entire operation will succeed
1252 * without exhausting the page pool.
1253 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001254 if (!ffa_region_group_identity_map(
1255 from_locked, fragments, fragment_constituent_counts,
1256 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001257 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001258 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001259 goto out;
1260 }
1261
1262 /*
1263 * Update the mapping for the sender. This won't allocate because the
1264 * transaction was already prepared above, but may free pages in the
1265 * case that a whole block is being unmapped that was previously
1266 * partially mapped.
1267 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001268 CHECK(ffa_region_group_identity_map(
1269 from_locked, fragments, fragment_constituent_counts,
1270 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001271
1272 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001273 if (clear &&
1274 !ffa_clear_memory_constituents(
1275 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1276 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001277 /*
1278 * On failure, roll back by returning memory to the sender. This
1279 * may allocate pages which were previously freed into
1280 * `local_page_pool` by the call above, but will never allocate
1281 * more pages than that so can never fail.
1282 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001283 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001284 from_locked, fragments, fragment_constituent_counts,
1285 fragment_count, orig_from_mode, &local_page_pool,
1286 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001287
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001288 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001289 goto out;
1290 }
1291
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001293
1294out:
1295 mpool_fini(&local_page_pool);
1296
1297 /*
1298 * Tidy up the page table by reclaiming failed mappings (if there was an
1299 * error) or merging entries into blocks where possible (on success).
1300 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001301 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001302
1303 return ret;
1304}
1305
1306/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001307 * Complete a memory sending operation by checking that it is valid, updating
1308 * the sender page table, and then either marking the share state as having
1309 * completed sending (on success) or freeing it (on failure).
1310 *
1311 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1312 */
1313static struct ffa_value ffa_memory_send_complete(
1314 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001315 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1316 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001317{
1318 struct ffa_memory_region *memory_region = share_state->memory_region;
1319 struct ffa_value ret;
1320
1321 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001322 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001323
1324 /* Check that state is valid in sender page table and update. */
1325 ret = ffa_send_check_update(
1326 from_locked, share_state->fragments,
1327 share_state->fragment_constituent_counts,
1328 share_state->fragment_count, share_state->share_func,
1329 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001330 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1331 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001332 if (ret.func != FFA_SUCCESS_32) {
1333 /*
1334 * Free share state, it failed to send so it can't be retrieved.
1335 */
1336 dlog_verbose("Complete failed, freeing share state.\n");
1337 share_state_free(share_states, share_state, page_pool);
1338 return ret;
1339 }
1340
1341 share_state->sending_complete = true;
1342 dlog_verbose("Marked sending complete.\n");
1343
J-Alvesee68c542020-10-29 17:48:20 +00001344 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001345}
1346
1347/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001348 * Check that the given `memory_region` represents a valid memory send request
1349 * of the given `share_func` type, return the clear flag and permissions via the
1350 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001351 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001352 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001353 * not.
1354 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001355static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001356 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1357 uint32_t memory_share_length, uint32_t fragment_length,
1358 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001359{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001360 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001361 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001362 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001363 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001364 enum ffa_data_access data_access;
1365 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001366
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001367 assert(permissions != NULL);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001368
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001369 /*
1370 * This should already be checked by the caller, just making the
1371 * assumption clear here.
1372 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001373 assert(memory_region->receiver_count == 1);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001374
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375 /* The sender must match the message sender. */
1376 if (memory_region->sender != from_locked.vm->id) {
1377 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001378 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001379 }
1380
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381 /*
1382 * Ensure that the composite header is within the memory bounds and
1383 * doesn't overlap the first part of the message.
1384 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001385 receivers_length = sizeof(struct ffa_memory_access) *
1386 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001387 constituents_offset =
1388 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001390 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001391 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001392 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001393 "Invalid composite memory region descriptor offset "
1394 "%d.\n",
1395 memory_region->receivers[0]
1396 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001397 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001398 }
1399
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001401
1402 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001403 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001404 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001405 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1406 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001407 if (memory_share_length != constituents_offset + constituents_length) {
1408 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001409 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 memory_region->receivers[0]
1411 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001412 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001413 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001414 if (fragment_length < memory_share_length &&
1415 fragment_length < HF_MAILBOX_SIZE) {
1416 dlog_warning(
1417 "Initial fragment length %d smaller than mailbox "
1418 "size.\n",
1419 fragment_length);
1420 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 /*
1423 * Clear is not allowed for memory sharing, as the sender still has
1424 * access to the memory.
1425 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001426 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1427 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 }
1431
1432 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
1437
1438 /* Check that the permissions are valid. */
1439 *permissions =
1440 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 data_access = ffa_get_data_access_attr(*permissions);
1442 instruction_access = ffa_get_instruction_access_attr(*permissions);
1443 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1444 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001445 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1446 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001447 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001450 dlog_verbose(
1451 "Invalid instruction access permissions %#x for "
1452 "sending memory.\n",
1453 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001455 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001456 if (share_func == FFA_MEM_SHARE_32) {
1457 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001458 dlog_verbose(
1459 "Invalid data access permissions %#x for "
1460 "sharing memory.\n",
1461 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001463 }
1464 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001465 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1466 * required for share operations (but must not be specified by
1467 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001468 * returned to the retriever.
1469 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001470 ffa_set_instruction_access_attr(permissions,
1471 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001472 memory_region->receivers[0].receiver_permissions.permissions =
1473 *permissions;
1474 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 if (share_func == FFA_MEM_LEND_32 &&
1476 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001477 dlog_verbose(
1478 "Invalid data access permissions %#x for lending "
1479 "memory.\n",
1480 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001481 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001482 }
Federico Recanati85090c42021-12-15 13:17:54 +01001483
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001484 if (share_func == FFA_MEM_DONATE_32 &&
1485 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 dlog_verbose(
1487 "Invalid data access permissions %#x for donating "
1488 "memory.\n",
1489 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001490 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001491 }
1492
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001493 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001494}
1495
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001496/** Forwards a memory send message on to the TEE. */
1497static struct ffa_value memory_send_tee_forward(
1498 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1499 uint32_t share_func, struct ffa_memory_region *memory_region,
1500 uint32_t memory_share_length, uint32_t fragment_length)
1501{
1502 struct ffa_value ret;
1503
1504 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1505 memory_region, fragment_length);
1506 tee_locked.vm->mailbox.recv_size = fragment_length;
1507 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1508 tee_locked.vm->mailbox.recv_func = share_func;
1509 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001510 ret = arch_other_world_call(
1511 (struct ffa_value){.func = share_func,
1512 .arg1 = memory_share_length,
1513 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001514 /*
1515 * After the call to the TEE completes it must have finished reading its
1516 * RX buffer, so it is ready for another message.
1517 */
1518 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1519
1520 return ret;
1521}
1522
Andrew Walbrana65a1322020-04-06 19:32:32 +01001523/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001524 * Gets the share state for continuing an operation to donate, lend or share
1525 * memory, and checks that it is a valid request.
1526 *
1527 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1528 * not.
1529 */
1530static struct ffa_value ffa_memory_send_continue_validate(
1531 struct share_states_locked share_states, ffa_memory_handle_t handle,
1532 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1533 struct mpool *page_pool)
1534{
1535 struct ffa_memory_share_state *share_state;
1536 struct ffa_memory_region *memory_region;
1537
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001538 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001539
1540 /*
1541 * Look up the share state by handle and make sure that the VM ID
1542 * matches.
1543 */
1544 if (!get_share_state(share_states, handle, &share_state)) {
1545 dlog_verbose(
1546 "Invalid handle %#x for memory send continuation.\n",
1547 handle);
1548 return ffa_error(FFA_INVALID_PARAMETERS);
1549 }
1550 memory_region = share_state->memory_region;
1551
1552 if (memory_region->sender != from_vm_id) {
1553 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1554 return ffa_error(FFA_INVALID_PARAMETERS);
1555 }
1556
1557 if (share_state->sending_complete) {
1558 dlog_verbose(
1559 "Sending of memory handle %#x is already complete.\n",
1560 handle);
1561 return ffa_error(FFA_INVALID_PARAMETERS);
1562 }
1563
1564 if (share_state->fragment_count == MAX_FRAGMENTS) {
1565 /*
1566 * Log a warning as this is a sign that MAX_FRAGMENTS should
1567 * probably be increased.
1568 */
1569 dlog_warning(
1570 "Too many fragments for memory share with handle %#x; "
1571 "only %d supported.\n",
1572 handle, MAX_FRAGMENTS);
1573 /* Free share state, as it's not possible to complete it. */
1574 share_state_free(share_states, share_state, page_pool);
1575 return ffa_error(FFA_NO_MEMORY);
1576 }
1577
1578 *share_state_ret = share_state;
1579
1580 return (struct ffa_value){.func = FFA_SUCCESS_32};
1581}
1582
1583/**
1584 * Forwards a memory send continuation message on to the TEE.
1585 */
1586static struct ffa_value memory_send_continue_tee_forward(
1587 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1588 uint32_t fragment_length, ffa_memory_handle_t handle)
1589{
1590 struct ffa_value ret;
1591
1592 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1593 fragment_length);
1594 tee_locked.vm->mailbox.recv_size = fragment_length;
1595 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1596 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1597 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001598 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001599 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1600 .arg1 = (uint32_t)handle,
1601 .arg2 = (uint32_t)(handle >> 32),
1602 .arg3 = fragment_length,
1603 .arg4 = (uint64_t)sender_vm_id << 16});
1604 /*
1605 * After the call to the TEE completes it must have finished reading its
1606 * RX buffer, so it is ready for another message.
1607 */
1608 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1609
1610 return ret;
1611}
1612
1613/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001614 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1615 * updates the stage-2 page tables. Specifically, check if the message length
1616 * and number of memory region constituents match, and if the transition is
1617 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001618 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001619 * Assumes that the caller has already found and locked the sender VM and copied
1620 * the memory region descriptor from the sender's TX buffer to a freshly
1621 * allocated page from Hafnium's internal pool. The caller must have also
1622 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001623 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001624 * This function takes ownership of the `memory_region` passed in and will free
1625 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001626 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001627struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001628 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001629 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001630 uint32_t fragment_length, uint32_t share_func,
1631 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001632{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001633 ffa_memory_access_permissions_t permissions;
1634 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001635 struct share_states_locked share_states;
1636 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001637
1638 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001639 * If there is an error validating the `memory_region` then we need to
1640 * free it because we own it but we won't be storing it in a share state
1641 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001642 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001643 ret = ffa_memory_send_validate(from_locked, memory_region,
1644 memory_share_length, fragment_length,
1645 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001646 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001647 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001648 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001649 }
1650
Andrew Walbrana65a1322020-04-06 19:32:32 +01001651 /* Set flag for share function, ready to be retrieved later. */
1652 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001653 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001654 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001655 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001656 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001657 case FFA_MEM_LEND_32:
1658 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001659 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001660 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001661 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001662 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001663 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001664 }
1665
Andrew Walbranca808b12020-05-15 17:22:28 +01001666 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001667 /*
1668 * Allocate a share state before updating the page table. Otherwise if
1669 * updating the page table succeeded but allocating the share state
1670 * failed then it would leave the memory in a state where nobody could
1671 * get it back.
1672 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001673 if (!allocate_share_state(share_states, share_func, memory_region,
1674 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1675 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001676 dlog_verbose("Failed to allocate share state.\n");
1677 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001678 ret = ffa_error(FFA_NO_MEMORY);
1679 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001680 }
1681
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 if (fragment_length == memory_share_length) {
1683 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001684 ret = ffa_memory_send_complete(
1685 from_locked, share_states, share_state, page_pool,
1686 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001687 } else {
1688 ret = (struct ffa_value){
1689 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001690 .arg1 = (uint32_t)memory_region->handle,
1691 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001692 .arg3 = fragment_length};
1693 }
1694
1695out:
1696 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001697 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001698 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001699}
1700
1701/**
1702 * Validates a call to donate, lend or share memory to the TEE and then updates
1703 * the stage-2 page tables. Specifically, check if the message length and number
1704 * of memory region constituents match, and if the transition is valid for the
1705 * type of memory sending operation.
1706 *
1707 * Assumes that the caller has already found and locked the sender VM and the
1708 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1709 * to a freshly allocated page from Hafnium's internal pool. The caller must
1710 * have also validated that the receiver VM ID is valid.
1711 *
1712 * This function takes ownership of the `memory_region` passed in and will free
1713 * it when necessary; it must not be freed by the caller.
1714 */
1715struct ffa_value ffa_memory_tee_send(
1716 struct vm_locked from_locked, struct vm_locked to_locked,
1717 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1718 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1719{
1720 ffa_memory_access_permissions_t permissions;
1721 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001722
1723 /*
1724 * If there is an error validating the `memory_region` then we need to
1725 * free it because we own it but we won't be storing it in a share state
1726 * after all.
1727 */
1728 ret = ffa_memory_send_validate(from_locked, memory_region,
1729 memory_share_length, fragment_length,
1730 share_func, &permissions);
1731 if (ret.func != FFA_SUCCESS_32) {
1732 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001733 }
1734
Andrew Walbranca808b12020-05-15 17:22:28 +01001735 if (fragment_length == memory_share_length) {
1736 /* No more fragments to come, everything fit in one message. */
1737 struct ffa_composite_memory_region *composite =
1738 ffa_memory_region_get_composite(memory_region, 0);
1739 struct ffa_memory_region_constituent *constituents =
1740 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001741 struct mpool local_page_pool;
1742 uint32_t orig_from_mode;
1743
1744 /*
1745 * Use a local page pool so that we can roll back if necessary.
1746 */
1747 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001748
1749 ret = ffa_send_check_update(
1750 from_locked, &constituents,
1751 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001752 permissions, &local_page_pool,
1753 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1754 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001755 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001756 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001757 goto out;
1758 }
1759
1760 /* Forward memory send message on to TEE. */
1761 ret = memory_send_tee_forward(
1762 to_locked, from_locked.vm->id, share_func,
1763 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001764
1765 if (ret.func != FFA_SUCCESS_32) {
1766 dlog_verbose(
1767 "TEE didn't successfully complete memory send "
1768 "operation; returned %#x (%d). Rolling back.\n",
1769 ret.func, ret.arg2);
1770
1771 /*
1772 * The TEE failed to complete the send operation, so
1773 * roll back the page table update for the VM. This
1774 * can't fail because it won't try to allocate more
1775 * memory than was freed into the `local_page_pool` by
1776 * `ffa_send_check_update` in the initial update.
1777 */
1778 CHECK(ffa_region_group_identity_map(
1779 from_locked, &constituents,
1780 &composite->constituent_count, 1,
1781 orig_from_mode, &local_page_pool, true));
1782 }
1783
1784 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001785 } else {
1786 struct share_states_locked share_states = share_states_lock();
1787 ffa_memory_handle_t handle;
1788
1789 /*
1790 * We need to wait for the rest of the fragments before we can
1791 * check whether the transaction is valid and unmap the memory.
1792 * Call the TEE so it can do its initial validation and assign a
1793 * handle, and allocate a share state to keep what we have so
1794 * far.
1795 */
1796 ret = memory_send_tee_forward(
1797 to_locked, from_locked.vm->id, share_func,
1798 memory_region, memory_share_length, fragment_length);
1799 if (ret.func == FFA_ERROR_32) {
1800 goto out_unlock;
1801 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1802 dlog_warning(
1803 "Got %#x from TEE in response to %#x for "
1804 "fragment with with %d/%d, expected "
1805 "FFA_MEM_FRAG_RX.\n",
1806 ret.func, share_func, fragment_length,
1807 memory_share_length);
1808 ret = ffa_error(FFA_INVALID_PARAMETERS);
1809 goto out_unlock;
1810 }
1811 handle = ffa_frag_handle(ret);
1812 if (ret.arg3 != fragment_length) {
1813 dlog_warning(
1814 "Got unexpected fragment offset %d for "
1815 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1816 ret.arg3, fragment_length);
1817 ret = ffa_error(FFA_INVALID_PARAMETERS);
1818 goto out_unlock;
1819 }
1820 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1821 dlog_warning(
1822 "Got unexpected sender ID %d for "
1823 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1824 ffa_frag_sender(ret), from_locked.vm->id);
1825 ret = ffa_error(FFA_INVALID_PARAMETERS);
1826 goto out_unlock;
1827 }
1828
1829 if (!allocate_share_state(share_states, share_func,
1830 memory_region, fragment_length,
1831 handle, NULL)) {
1832 dlog_verbose("Failed to allocate share state.\n");
1833 ret = ffa_error(FFA_NO_MEMORY);
1834 goto out_unlock;
1835 }
1836 /*
1837 * Don't free the memory region fragment, as it has been stored
1838 * in the share state.
1839 */
1840 memory_region = NULL;
1841 out_unlock:
1842 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001843 }
1844
Andrew Walbranca808b12020-05-15 17:22:28 +01001845out:
1846 if (memory_region != NULL) {
1847 mpool_free(page_pool, memory_region);
1848 }
1849 dump_share_states();
1850 return ret;
1851}
1852
1853/**
1854 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1855 * this is the last fragment then checks that the transition is valid for the
1856 * type of memory sending operation and updates the stage-2 page tables of the
1857 * sender.
1858 *
1859 * Assumes that the caller has already found and locked the sender VM and copied
1860 * the memory region descriptor from the sender's TX buffer to a freshly
1861 * allocated page from Hafnium's internal pool.
1862 *
1863 * This function takes ownership of the `fragment` passed in; it must not be
1864 * freed by the caller.
1865 */
1866struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1867 void *fragment,
1868 uint32_t fragment_length,
1869 ffa_memory_handle_t handle,
1870 struct mpool *page_pool)
1871{
1872 struct share_states_locked share_states = share_states_lock();
1873 struct ffa_memory_share_state *share_state;
1874 struct ffa_value ret;
1875 struct ffa_memory_region *memory_region;
1876
1877 ret = ffa_memory_send_continue_validate(share_states, handle,
1878 &share_state,
1879 from_locked.vm->id, page_pool);
1880 if (ret.func != FFA_SUCCESS_32) {
1881 goto out_free_fragment;
1882 }
1883 memory_region = share_state->memory_region;
1884
1885 if (memory_region->receivers[0].receiver_permissions.receiver ==
1886 HF_TEE_VM_ID) {
1887 dlog_error(
1888 "Got hypervisor-allocated handle for memory send to "
1889 "TEE. This should never happen, and indicates a bug in "
1890 "EL3 code.\n");
1891 ret = ffa_error(FFA_INVALID_PARAMETERS);
1892 goto out_free_fragment;
1893 }
1894
1895 /* Add this fragment. */
1896 share_state->fragments[share_state->fragment_count] = fragment;
1897 share_state->fragment_constituent_counts[share_state->fragment_count] =
1898 fragment_length / sizeof(struct ffa_memory_region_constituent);
1899 share_state->fragment_count++;
1900
1901 /* Check whether the memory send operation is now ready to complete. */
1902 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001903 ret = ffa_memory_send_complete(
1904 from_locked, share_states, share_state, page_pool,
1905 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001906 } else {
1907 ret = (struct ffa_value){
1908 .func = FFA_MEM_FRAG_RX_32,
1909 .arg1 = (uint32_t)handle,
1910 .arg2 = (uint32_t)(handle >> 32),
1911 .arg3 = share_state_next_fragment_offset(share_states,
1912 share_state)};
1913 }
1914 goto out;
1915
1916out_free_fragment:
1917 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001918
1919out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001920 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001921 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001922}
1923
Andrew Walbranca808b12020-05-15 17:22:28 +01001924/**
1925 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1926 * is the last fragment then checks that the transition is valid for the type of
1927 * memory sending operation and updates the stage-2 page tables of the sender.
1928 *
1929 * Assumes that the caller has already found and locked the sender VM and copied
1930 * the memory region descriptor from the sender's TX buffer to a freshly
1931 * allocated page from Hafnium's internal pool.
1932 *
1933 * This function takes ownership of the `memory_region` passed in and will free
1934 * it when necessary; it must not be freed by the caller.
1935 */
1936struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1937 struct vm_locked to_locked,
1938 void *fragment,
1939 uint32_t fragment_length,
1940 ffa_memory_handle_t handle,
1941 struct mpool *page_pool)
1942{
1943 struct share_states_locked share_states = share_states_lock();
1944 struct ffa_memory_share_state *share_state;
1945 struct ffa_value ret;
1946 struct ffa_memory_region *memory_region;
1947
1948 ret = ffa_memory_send_continue_validate(share_states, handle,
1949 &share_state,
1950 from_locked.vm->id, page_pool);
1951 if (ret.func != FFA_SUCCESS_32) {
1952 goto out_free_fragment;
1953 }
1954 memory_region = share_state->memory_region;
1955
1956 if (memory_region->receivers[0].receiver_permissions.receiver !=
1957 HF_TEE_VM_ID) {
1958 dlog_error(
1959 "Got SPM-allocated handle for memory send to non-TEE "
1960 "VM. This should never happen, and indicates a bug.\n");
1961 ret = ffa_error(FFA_INVALID_PARAMETERS);
1962 goto out_free_fragment;
1963 }
1964
1965 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1966 to_locked.vm->mailbox.recv == NULL) {
1967 /*
1968 * If the TEE RX buffer is not available, tell the sender to
1969 * retry by returning the current offset again.
1970 */
1971 ret = (struct ffa_value){
1972 .func = FFA_MEM_FRAG_RX_32,
1973 .arg1 = (uint32_t)handle,
1974 .arg2 = (uint32_t)(handle >> 32),
1975 .arg3 = share_state_next_fragment_offset(share_states,
1976 share_state),
1977 };
1978 goto out_free_fragment;
1979 }
1980
1981 /* Add this fragment. */
1982 share_state->fragments[share_state->fragment_count] = fragment;
1983 share_state->fragment_constituent_counts[share_state->fragment_count] =
1984 fragment_length / sizeof(struct ffa_memory_region_constituent);
1985 share_state->fragment_count++;
1986
1987 /* Check whether the memory send operation is now ready to complete. */
1988 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001989 struct mpool local_page_pool;
1990 uint32_t orig_from_mode;
1991
1992 /*
1993 * Use a local page pool so that we can roll back if necessary.
1994 */
1995 mpool_init_with_fallback(&local_page_pool, page_pool);
1996
Andrew Walbranca808b12020-05-15 17:22:28 +01001997 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001998 share_state, &local_page_pool,
1999 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002000
2001 if (ret.func == FFA_SUCCESS_32) {
2002 /*
2003 * Forward final fragment on to the TEE so that
2004 * it can complete the memory sending operation.
2005 */
2006 ret = memory_send_continue_tee_forward(
2007 to_locked, from_locked.vm->id, fragment,
2008 fragment_length, handle);
2009
2010 if (ret.func != FFA_SUCCESS_32) {
2011 /*
2012 * The error will be passed on to the caller,
2013 * but log it here too.
2014 */
2015 dlog_verbose(
2016 "TEE didn't successfully complete "
2017 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002018 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002019 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002020
2021 /*
2022 * The TEE failed to complete the send
2023 * operation, so roll back the page table update
2024 * for the VM. This can't fail because it won't
2025 * try to allocate more memory than was freed
2026 * into the `local_page_pool` by
2027 * `ffa_send_check_update` in the initial
2028 * update.
2029 */
2030 CHECK(ffa_region_group_identity_map(
2031 from_locked, share_state->fragments,
2032 share_state
2033 ->fragment_constituent_counts,
2034 share_state->fragment_count,
2035 orig_from_mode, &local_page_pool,
2036 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002037 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002038
Andrew Walbranca808b12020-05-15 17:22:28 +01002039 /* Free share state. */
2040 share_state_free(share_states, share_state, page_pool);
2041 } else {
2042 /* Abort sending to TEE. */
2043 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002044 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002045 .func = FFA_MEM_RECLAIM_32,
2046 .arg1 = (uint32_t)handle,
2047 .arg2 = (uint32_t)(handle >> 32)});
2048
2049 if (tee_ret.func != FFA_SUCCESS_32) {
2050 /*
2051 * Nothing we can do if TEE doesn't abort
2052 * properly, just log it.
2053 */
2054 dlog_verbose(
2055 "TEE didn't successfully abort failed "
2056 "memory send operation; returned %#x "
2057 "(%d).\n",
2058 tee_ret.func, tee_ret.arg2);
2059 }
2060 /*
2061 * We don't need to free the share state in this case
2062 * because ffa_memory_send_complete does that already.
2063 */
2064 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002065
2066 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002067 } else {
2068 uint32_t next_fragment_offset =
2069 share_state_next_fragment_offset(share_states,
2070 share_state);
2071
2072 ret = memory_send_continue_tee_forward(
2073 to_locked, from_locked.vm->id, fragment,
2074 fragment_length, handle);
2075
2076 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2077 ffa_frag_handle(ret) != handle ||
2078 ret.arg3 != next_fragment_offset ||
2079 ffa_frag_sender(ret) != from_locked.vm->id) {
2080 dlog_verbose(
2081 "Got unexpected result from forwarding "
2082 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2083 "offset %d, sender %d); expected "
2084 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2085 "sender %d).\n",
2086 ret.func, ffa_frag_handle(ret), ret.arg3,
2087 ffa_frag_sender(ret), handle,
2088 next_fragment_offset, from_locked.vm->id);
2089 /* Free share state. */
2090 share_state_free(share_states, share_state, page_pool);
2091 ret = ffa_error(FFA_INVALID_PARAMETERS);
2092 goto out;
2093 }
2094
2095 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2096 .arg1 = (uint32_t)handle,
2097 .arg2 = (uint32_t)(handle >> 32),
2098 .arg3 = next_fragment_offset};
2099 }
2100 goto out;
2101
2102out_free_fragment:
2103 mpool_free(page_pool, fragment);
2104
2105out:
2106 share_states_unlock(&share_states);
2107 return ret;
2108}
2109
2110/** Clean up after the receiver has finished retrieving a memory region. */
2111static void ffa_memory_retrieve_complete(
2112 struct share_states_locked share_states,
2113 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2114{
2115 if (share_state->share_func == FFA_MEM_DONATE_32) {
2116 /*
2117 * Memory that has been donated can't be relinquished,
2118 * so no need to keep the share state around.
2119 */
2120 share_state_free(share_states, share_state, page_pool);
2121 dlog_verbose("Freed share state for donate.\n");
2122 }
2123}
2124
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2126 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002127 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002128 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002129{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002130 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002131 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002132 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002133 sizeof(struct ffa_memory_access);
2134 ffa_memory_handle_t handle = retrieve_request->handle;
2135 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002136 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002137 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2138 struct ffa_memory_region *memory_region;
2139 ffa_memory_access_permissions_t sent_permissions;
2140 enum ffa_data_access sent_data_access;
2141 enum ffa_instruction_access sent_instruction_access;
2142 ffa_memory_access_permissions_t requested_permissions;
2143 enum ffa_data_access requested_data_access;
2144 enum ffa_instruction_access requested_instruction_access;
2145 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002146 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002147 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 struct ffa_memory_share_state *share_state;
2149 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002150 struct ffa_composite_memory_region *composite;
2151 uint32_t total_length;
2152 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002153
2154 dump_share_states();
2155
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002156 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002157 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002159 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002160 expected_retrieve_request_length,
2161 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002162 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002163 }
2164
Andrew Walbrana65a1322020-04-06 19:32:32 +01002165 if (retrieve_request->receiver_count != 1) {
2166 dlog_verbose(
2167 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002169 "expected 1).\n",
2170 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002171 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002172 }
2173
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002174 share_states = share_states_lock();
2175 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002176 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002177 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002178 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002179 goto out;
2180 }
2181
Andrew Walbrana65a1322020-04-06 19:32:32 +01002182 memory_region = share_state->memory_region;
2183 CHECK(memory_region != NULL);
2184
2185 /*
2186 * Check that the transaction type expected by the receiver is correct,
2187 * if it has been specified.
2188 */
2189 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002190 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002191 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002192 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002193 dlog_verbose(
2194 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002195 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002196 transaction_type,
2197 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002198 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002199 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002200 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002201 goto out;
2202 }
2203
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002204 if (retrieve_request->sender != memory_region->sender) {
2205 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002207 "expected %d for handle %#x.\n",
2208 retrieve_request->sender, memory_region->sender,
2209 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002210 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002211 goto out;
2212 }
2213
2214 if (retrieve_request->tag != memory_region->tag) {
2215 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002216 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002217 "%d for handle %#x.\n",
2218 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002219 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002220 goto out;
2221 }
2222
Andrew Walbrana65a1322020-04-06 19:32:32 +01002223 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2224 to_locked.vm->id) {
2225 dlog_verbose(
2226 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002227 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002228 retrieve_request->receivers[0]
2229 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002231 goto out;
2232 }
2233
2234 if (memory_region->receivers[0].receiver_permissions.receiver !=
2235 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002236 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002237 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2238 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002239 to_locked.vm->id,
2240 memory_region->receivers[0]
2241 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002242 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002243 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002244 goto out;
2245 }
2246
Andrew Walbranca808b12020-05-15 17:22:28 +01002247 if (!share_state->sending_complete) {
2248 dlog_verbose(
2249 "Memory with handle %#x not fully sent, can't "
2250 "retrieve.\n",
2251 handle);
2252 ret = ffa_error(FFA_INVALID_PARAMETERS);
2253 goto out;
2254 }
2255
2256 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002257 dlog_verbose("Memory with handle %#x already retrieved.\n",
2258 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002259 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002260 goto out;
2261 }
2262
Andrew Walbrana65a1322020-04-06 19:32:32 +01002263 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2264 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002265 dlog_verbose(
2266 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002267 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002268 retrieve_request->receivers[0]
2269 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002270 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002271 goto out;
2272 }
2273
Federico Recanati85090c42021-12-15 13:17:54 +01002274 if ((retrieve_request->flags &
2275 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0) {
2276 dlog_verbose(
2277 "Retriever specified 'address range alignment hint'"
2278 " not supported.\n");
2279 ret = ffa_error(FFA_INVALID_PARAMETERS);
2280 goto out;
2281 }
2282 if ((retrieve_request->flags &
2283 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2284 dlog_verbose(
2285 "Bits 8-5 must be zero in memory region's flags "
2286 "(address range alignment hint not supported).\n");
2287 ret = ffa_error(FFA_INVALID_PARAMETERS);
2288 goto out;
2289 }
2290
J-Alves84658fc2021-06-17 14:37:32 +01002291 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2292 dlog_verbose(
2293 "Bits 31-10 must be zero in memory region's flags.\n");
2294 ret = ffa_error(FFA_INVALID_PARAMETERS);
2295 goto out;
2296 }
2297
2298 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2299 (retrieve_request->flags &
2300 (FFA_MEMORY_REGION_FLAG_CLEAR |
2301 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2302 dlog_verbose(
2303 "Memory Share operation can't clean after relinquish "
2304 "memory region.\n");
2305 ret = ffa_error(FFA_INVALID_PARAMETERS);
2306 goto out;
2307 }
2308
Andrew Walbrana65a1322020-04-06 19:32:32 +01002309 /*
J-Alves17c069c2021-12-07 16:00:38 +00002310 * If the borrower needs the memory to be cleared before mapping to its
2311 * address space, the sender should have set the flag when calling
2312 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2313 */
2314 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2315 (share_state->memory_region->flags &
2316 FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2317 dlog_verbose(
2318 "Borrower needs memory cleared. Sender needs to set "
2319 "flag for clearing memory.\n");
2320 ret = ffa_error(FFA_DENIED);
2321 goto out;
2322 }
2323
2324 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002325 * Check permissions from sender against permissions requested by
2326 * receiver.
2327 */
2328 /* TODO: Check attributes too. */
2329 sent_permissions =
2330 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002332 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002333 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002334 requested_permissions =
2335 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002336 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002337 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002338 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002339 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002340
2341 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2342 requested_permissions == FFA_DATA_ACCESS_RO) &&
2343 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2344 dlog_verbose(
2345 "Receiver has RO permissions can not request clear.\n");
2346 ret = ffa_error(FFA_DENIED);
2347 goto out;
2348 }
2349
Andrew Walbrana65a1322020-04-06 19:32:32 +01002350 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002351 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2352 case FFA_DATA_ACCESS_RW:
2353 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2354 requested_data_access == FFA_DATA_ACCESS_RW) {
2355 ffa_set_data_access_attr(&permissions,
2356 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002357 break;
2358 }
2359 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002360 case FFA_DATA_ACCESS_RO:
2361 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2362 requested_data_access == FFA_DATA_ACCESS_RO) {
2363 ffa_set_data_access_attr(&permissions,
2364 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002365 break;
2366 }
2367 dlog_verbose(
2368 "Invalid data access requested; sender specified "
2369 "permissions %#x but receiver requested %#x.\n",
2370 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002371 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002372 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002373 case FFA_DATA_ACCESS_RESERVED:
2374 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002375 "checked before this point.");
2376 }
2377 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002378 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2379 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002380 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002381 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2382 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2383 ffa_set_instruction_access_attr(
2384 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002385 break;
2386 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002387 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002388 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002389 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2390 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2391 ffa_set_instruction_access_attr(
2392 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002393 break;
2394 }
2395 dlog_verbose(
2396 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002397 "specified permissions %#x but receiver requested "
2398 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002399 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002400 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002401 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002402 case FFA_INSTRUCTION_ACCESS_RESERVED:
2403 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002404 "be checked before this point.");
2405 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002406 memory_to_attributes = ffa_memory_permissions_to_mode(
2407 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002408 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002409 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002410 share_state->fragment_constituent_counts,
2411 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002412 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002413 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002414 goto out;
2415 }
2416
2417 /*
2418 * Copy response to RX buffer of caller and deliver the message. This
2419 * must be done before the share_state is (possibly) freed.
2420 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002421 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002422 composite = ffa_memory_region_get_composite(memory_region, 0);
2423 /*
2424 * Constituents which we received in the first fragment should always
2425 * fit in the first fragment we are sending, because the header is the
2426 * same size in both cases and we have a fixed message buffer size. So
2427 * `ffa_retrieved_memory_region_init` should never fail.
2428 */
2429 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002430 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2431 memory_region->sender, memory_region->attributes,
2432 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002433 composite->page_count, composite->constituent_count,
2434 share_state->fragments[0],
2435 share_state->fragment_constituent_counts[0], &total_length,
2436 &fragment_length));
2437 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002438 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002439 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002440 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2441
Andrew Walbranca808b12020-05-15 17:22:28 +01002442 share_state->retrieved_fragment_count[0] = 1;
2443 if (share_state->retrieved_fragment_count[0] ==
2444 share_state->fragment_count) {
2445 ffa_memory_retrieve_complete(share_states, share_state,
2446 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002447 }
2448
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002449 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002450 .arg1 = total_length,
2451 .arg2 = fragment_length};
2452
2453out:
2454 share_states_unlock(&share_states);
2455 dump_share_states();
2456 return ret;
2457}
2458
2459struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2460 ffa_memory_handle_t handle,
2461 uint32_t fragment_offset,
2462 struct mpool *page_pool)
2463{
2464 struct ffa_memory_region *memory_region;
2465 struct share_states_locked share_states;
2466 struct ffa_memory_share_state *share_state;
2467 struct ffa_value ret;
2468 uint32_t fragment_index;
2469 uint32_t retrieved_constituents_count;
2470 uint32_t i;
2471 uint32_t expected_fragment_offset;
2472 uint32_t remaining_constituent_count;
2473 uint32_t fragment_length;
2474
2475 dump_share_states();
2476
2477 share_states = share_states_lock();
2478 if (!get_share_state(share_states, handle, &share_state)) {
2479 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2480 handle);
2481 ret = ffa_error(FFA_INVALID_PARAMETERS);
2482 goto out;
2483 }
2484
2485 memory_region = share_state->memory_region;
2486 CHECK(memory_region != NULL);
2487
2488 if (memory_region->receivers[0].receiver_permissions.receiver !=
2489 to_locked.vm->id) {
2490 dlog_verbose(
2491 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2492 "of handle %#x.\n",
2493 to_locked.vm->id,
2494 memory_region->receivers[0]
2495 .receiver_permissions.receiver,
2496 handle);
2497 ret = ffa_error(FFA_INVALID_PARAMETERS);
2498 goto out;
2499 }
2500
2501 if (!share_state->sending_complete) {
2502 dlog_verbose(
2503 "Memory with handle %#x not fully sent, can't "
2504 "retrieve.\n",
2505 handle);
2506 ret = ffa_error(FFA_INVALID_PARAMETERS);
2507 goto out;
2508 }
2509
2510 if (share_state->retrieved_fragment_count[0] == 0 ||
2511 share_state->retrieved_fragment_count[0] >=
2512 share_state->fragment_count) {
2513 dlog_verbose(
2514 "Retrieval of memory with handle %#x not yet started "
2515 "or already completed (%d/%d fragments retrieved).\n",
2516 handle, share_state->retrieved_fragment_count[0],
2517 share_state->fragment_count);
2518 ret = ffa_error(FFA_INVALID_PARAMETERS);
2519 goto out;
2520 }
2521
2522 fragment_index = share_state->retrieved_fragment_count[0];
2523
2524 /*
2525 * Check that the given fragment offset is correct by counting how many
2526 * constituents were in the fragments previously sent.
2527 */
2528 retrieved_constituents_count = 0;
2529 for (i = 0; i < fragment_index; ++i) {
2530 retrieved_constituents_count +=
2531 share_state->fragment_constituent_counts[i];
2532 }
2533 expected_fragment_offset =
2534 ffa_composite_constituent_offset(memory_region, 0) +
2535 retrieved_constituents_count *
2536 sizeof(struct ffa_memory_region_constituent);
2537 if (fragment_offset != expected_fragment_offset) {
2538 dlog_verbose("Fragment offset was %d but expected %d.\n",
2539 fragment_offset, expected_fragment_offset);
2540 ret = ffa_error(FFA_INVALID_PARAMETERS);
2541 goto out;
2542 }
2543
2544 remaining_constituent_count = ffa_memory_fragment_init(
2545 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2546 share_state->fragments[fragment_index],
2547 share_state->fragment_constituent_counts[fragment_index],
2548 &fragment_length);
2549 CHECK(remaining_constituent_count == 0);
2550 to_locked.vm->mailbox.recv_size = fragment_length;
2551 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2552 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2553 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2554 share_state->retrieved_fragment_count[0]++;
2555 if (share_state->retrieved_fragment_count[0] ==
2556 share_state->fragment_count) {
2557 ffa_memory_retrieve_complete(share_states, share_state,
2558 page_pool);
2559 }
2560
2561 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2562 .arg1 = (uint32_t)handle,
2563 .arg2 = (uint32_t)(handle >> 32),
2564 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565
2566out:
2567 share_states_unlock(&share_states);
2568 dump_share_states();
2569 return ret;
2570}
2571
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002572struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002573 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002574 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002575{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002576 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002577 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002578 struct ffa_memory_share_state *share_state;
2579 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002580 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002581 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002582
Andrew Walbrana65a1322020-04-06 19:32:32 +01002583 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002584 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002585 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002586 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002587 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002588 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002589 }
2590
Andrew Walbrana65a1322020-04-06 19:32:32 +01002591 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002592 dlog_verbose(
2593 "VM ID %d in relinquish message doesn't match calling "
2594 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002595 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002596 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002597 }
2598
2599 dump_share_states();
2600
2601 share_states = share_states_lock();
2602 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002603 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002604 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002605 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002606 goto out;
2607 }
2608
Andrew Walbranca808b12020-05-15 17:22:28 +01002609 if (!share_state->sending_complete) {
2610 dlog_verbose(
2611 "Memory with handle %#x not fully sent, can't "
2612 "relinquish.\n",
2613 handle);
2614 ret = ffa_error(FFA_INVALID_PARAMETERS);
2615 goto out;
2616 }
2617
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002618 memory_region = share_state->memory_region;
2619 CHECK(memory_region != NULL);
2620
Andrew Walbrana65a1322020-04-06 19:32:32 +01002621 if (memory_region->receivers[0].receiver_permissions.receiver !=
2622 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002623 dlog_verbose(
2624 "VM ID %d tried to relinquish memory region with "
2625 "handle %#x but receiver was %d.\n",
2626 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002627 memory_region->receivers[0]
2628 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630 goto out;
2631 }
2632
Andrew Walbranca808b12020-05-15 17:22:28 +01002633 if (share_state->retrieved_fragment_count[0] !=
2634 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002635 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002636 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002637 "relinquish.\n",
2638 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002639 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002640 goto out;
2641 }
2642
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002643 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002644
2645 /*
2646 * Clear is not allowed for memory that was shared, as the original
2647 * sender still has access to the memory.
2648 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002649 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002651 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002652 goto out;
2653 }
2654
Andrew Walbranca808b12020-05-15 17:22:28 +01002655 ret = ffa_relinquish_check_update(
2656 from_locked, share_state->fragments,
2657 share_state->fragment_constituent_counts,
2658 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002659
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002660 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002661 /*
2662 * Mark memory handle as not retrieved, so it can be reclaimed
2663 * (or retrieved again).
2664 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002665 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002666 }
2667
2668out:
2669 share_states_unlock(&share_states);
2670 dump_share_states();
2671 return ret;
2672}
2673
2674/**
2675 * Validates that the reclaim transition is allowed for the given handle,
2676 * updates the page table of the reclaiming VM, and frees the internal state
2677 * associated with the handle.
2678 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002679struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002680 ffa_memory_handle_t handle,
2681 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002682 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002683{
2684 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002685 struct ffa_memory_share_state *share_state;
2686 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002687 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688
2689 dump_share_states();
2690
2691 share_states = share_states_lock();
2692 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002693 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002694 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002695 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002696 goto out;
2697 }
2698
2699 memory_region = share_state->memory_region;
2700 CHECK(memory_region != NULL);
2701
2702 if (to_locked.vm->id != memory_region->sender) {
2703 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002704 "VM %#x attempted to reclaim memory handle %#x "
2705 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002706 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002707 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002708 goto out;
2709 }
2710
Andrew Walbranca808b12020-05-15 17:22:28 +01002711 if (!share_state->sending_complete) {
2712 dlog_verbose(
2713 "Memory with handle %#x not fully sent, can't "
2714 "reclaim.\n",
2715 handle);
2716 ret = ffa_error(FFA_INVALID_PARAMETERS);
2717 goto out;
2718 }
2719
2720 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002721 dlog_verbose(
2722 "Tried to reclaim memory handle %#x that has not been "
2723 "relinquished.\n",
2724 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002725 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002726 goto out;
2727 }
2728
Andrew Walbranca808b12020-05-15 17:22:28 +01002729 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002730 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002731 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002732 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002733 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002734
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002735 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002736 share_state_free(share_states, share_state, page_pool);
2737 dlog_verbose("Freed share state after successful reclaim.\n");
2738 }
2739
2740out:
2741 share_states_unlock(&share_states);
2742 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002743}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002744
2745/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002746 * Validates that the reclaim transition is allowed for the memory region with
2747 * the given handle which was previously shared with the TEE, tells the TEE to
2748 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2749 *
2750 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002751 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002752struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002753 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002754 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002755 ffa_memory_region_flags_t flags,
2756 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002757{
Andrew Walbranca808b12020-05-15 17:22:28 +01002758 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2759 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2760 struct ffa_value tee_ret;
2761 uint32_t length;
2762 uint32_t fragment_length;
2763 uint32_t fragment_offset;
2764 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002765 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002766 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2767
2768 CHECK(request_length <= HF_MAILBOX_SIZE);
2769 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2770
2771 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002772 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002773 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2774 .arg1 = request_length,
2775 .arg2 = request_length});
2776 if (tee_ret.func == FFA_ERROR_32) {
2777 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2778 return tee_ret;
2779 }
2780 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2781 dlog_verbose(
2782 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2783 tee_ret.func);
2784 return ffa_error(FFA_INVALID_PARAMETERS);
2785 }
2786
2787 length = tee_ret.arg1;
2788 fragment_length = tee_ret.arg2;
2789
2790 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2791 length > sizeof(tee_retrieve_buffer)) {
2792 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2793 fragment_length, length, HF_MAILBOX_SIZE,
2794 sizeof(tee_retrieve_buffer));
2795 return ffa_error(FFA_INVALID_PARAMETERS);
2796 }
2797
2798 /*
2799 * Copy the first fragment of the memory region descriptor to an
2800 * internal buffer.
2801 */
2802 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2803 from_locked.vm->mailbox.send, fragment_length);
2804
2805 /* Fetch the remaining fragments into the same buffer. */
2806 fragment_offset = fragment_length;
2807 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002808 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002809 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2810 .arg1 = (uint32_t)handle,
2811 .arg2 = (uint32_t)(handle >> 32),
2812 .arg3 = fragment_offset});
2813 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2814 dlog_verbose(
2815 "Got %#x (%d) from TEE in response to "
2816 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2817 tee_ret.func, tee_ret.arg2);
2818 return tee_ret;
2819 }
2820 if (ffa_frag_handle(tee_ret) != handle) {
2821 dlog_verbose(
2822 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2823 "in response to FFA_MEM_FRAG_RX for handle "
2824 "%#x.\n",
2825 ffa_frag_handle(tee_ret), handle);
2826 return ffa_error(FFA_INVALID_PARAMETERS);
2827 }
2828 if (ffa_frag_sender(tee_ret) != 0) {
2829 dlog_verbose(
2830 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2831 "(expected 0).\n",
2832 ffa_frag_sender(tee_ret));
2833 return ffa_error(FFA_INVALID_PARAMETERS);
2834 }
2835 fragment_length = tee_ret.arg3;
2836 if (fragment_length > HF_MAILBOX_SIZE ||
2837 fragment_offset + fragment_length > length) {
2838 dlog_verbose(
2839 "Invalid fragment length %d at offset %d (max "
2840 "%d).\n",
2841 fragment_length, fragment_offset,
2842 HF_MAILBOX_SIZE);
2843 return ffa_error(FFA_INVALID_PARAMETERS);
2844 }
2845 memcpy_s(tee_retrieve_buffer + fragment_offset,
2846 sizeof(tee_retrieve_buffer) - fragment_offset,
2847 from_locked.vm->mailbox.send, fragment_length);
2848
2849 fragment_offset += fragment_length;
2850 }
2851
2852 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002853
2854 if (memory_region->receiver_count != 1) {
2855 /* Only one receiver supported by Hafnium for now. */
2856 dlog_verbose(
2857 "Multiple recipients not supported (got %d, expected "
2858 "1).\n",
2859 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002860 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002861 }
2862
2863 if (memory_region->handle != handle) {
2864 dlog_verbose(
2865 "Got memory region handle %#x from TEE but requested "
2866 "handle %#x.\n",
2867 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002868 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002869 }
2870
2871 /* The original sender must match the caller. */
2872 if (to_locked.vm->id != memory_region->sender) {
2873 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002874 "VM %#x attempted to reclaim memory handle %#x "
2875 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002876 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002877 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002878 }
2879
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002880 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002881
2882 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002883 * Validate that the reclaim transition is allowed for the given memory
2884 * region, forward the request to the TEE and then map the memory back
2885 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002886 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002887 return ffa_tee_reclaim_check_update(
2888 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002889 composite->constituent_count, memory_to_attributes,
2890 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002891}