blob: 048cca9c2e1c9df4214252411f41e6b53953723c [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) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100539 dlog_verbose("Inconsistent modes.\n");
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) {
J-Alves9256f162021-12-09 13:18:43 +0000667 /*
668 * If the original ffa memory send call has been processed
669 * successfully, it is expected the orig_to_mode would overlay
670 * with `state_mask`, as a result of the function
671 * `ffa_send_check_transition`.
672 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100673 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
674 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000675 } else {
676 /*
677 * Ensure the retriever has the expected state. We don't care
678 * about the MM_MODE_SHARED bit; either with or without it set
679 * are both valid representations of the !O-NA state.
680 */
681 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
682 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100683 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000684 }
685 }
686
687 /* Find the appropriate new mode. */
688 *to_mode = memory_to_attributes;
689 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100690 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 *to_mode |= 0;
692 break;
693
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100694 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000695 *to_mode |= MM_MODE_UNOWNED;
696 break;
697
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000699 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
700 break;
701
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100702 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000703 *to_mode |= 0;
704 break;
705
706 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100707 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100708 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000709 }
710
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100712}
Jose Marinho09b1db82019-08-08 09:16:59 +0100713
714/**
715 * Updates a VM's page table such that the given set of physical address ranges
716 * are mapped in the address space at the corresponding address ranges, in the
717 * mode provided.
718 *
719 * If commit is false, the page tables will be allocated from the mpool but no
720 * mappings will actually be updated. This function must always be called first
721 * with commit false to check that it will succeed before calling with commit
722 * true, to avoid leaving the page table in a half-updated state. To make a
723 * series of changes atomically you can call them all with commit false before
724 * calling them all with commit true.
725 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700726 * vm_ptable_defrag should always be called after a series of page table
727 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100728 *
729 * Returns true on success, or false if the update failed and no changes were
730 * made to memory mappings.
731 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100732static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000733 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100734 struct ffa_memory_region_constituent **fragments,
735 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100736 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100737{
Andrew Walbranca808b12020-05-15 17:22:28 +0100738 uint32_t i;
739 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100740
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700741 if (vm_locked.vm->el0_partition) {
742 mode |= MM_MODE_USER | MM_MODE_NG;
743 }
744
Andrew Walbranca808b12020-05-15 17:22:28 +0100745 /* Iterate over the memory region constituents within each fragment. */
746 for (i = 0; i < fragment_count; ++i) {
747 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
748 size_t size = fragments[i][j].page_count * PAGE_SIZE;
749 paddr_t pa_begin =
750 pa_from_ipa(ipa_init(fragments[i][j].address));
751 paddr_t pa_end = pa_add(pa_begin, size);
Federico Recanati4fd065d2021-12-13 20:06:23 +0100752 uint32_t pa_range = arch_mm_get_pa_range();
753
754 /*
755 * Ensure the requested region falls into system's PA
756 * range.
757 */
758 if (((pa_addr(pa_begin) >> pa_range) > 0) ||
759 ((pa_addr(pa_end) >> pa_range) > 0)) {
760 dlog_error("Region is outside of PA Range\n");
761 return false;
762 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100763
764 if (commit) {
765 vm_identity_commit(vm_locked, pa_begin, pa_end,
766 mode, ppool, NULL);
767 } else if (!vm_identity_prepare(vm_locked, pa_begin,
768 pa_end, mode, ppool)) {
769 return false;
770 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100771 }
772 }
773
774 return true;
775}
776
777/**
778 * Clears a region of physical memory by overwriting it with zeros. The data is
779 * flushed from the cache so the memory has been cleared across the system.
780 */
J-Alves7db32002021-12-14 14:44:50 +0000781static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
782 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100783{
784 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000785 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100786 * global mapping of the whole range. Such an approach will limit
787 * the changes to stage-1 tables and will allow only local
788 * invalidation.
789 */
790 bool ret;
791 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000792 void *ptr = mm_identity_map(stage1_locked, begin, end,
793 MM_MODE_W | (extra_mode_attributes &
794 plat_ffa_other_world_mode()),
795 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100796 size_t size = pa_difference(begin, end);
797
798 if (!ptr) {
799 /* TODO: partial defrag of failed range. */
800 /* Recover any memory consumed in failed mapping. */
801 mm_defrag(stage1_locked, ppool);
802 goto fail;
803 }
804
805 memset_s(ptr, size, 0, size);
806 arch_mm_flush_dcache(ptr, size);
807 mm_unmap(stage1_locked, begin, end, ppool);
808
809 ret = true;
810 goto out;
811
812fail:
813 ret = false;
814
815out:
816 mm_unlock_stage1(&stage1_locked);
817
818 return ret;
819}
820
821/**
822 * Clears a region of physical memory by overwriting it with zeros. The data is
823 * flushed from the cache so the memory has been cleared across the system.
824 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100825static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000826 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100827 struct ffa_memory_region_constituent **fragments,
828 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
829 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100830{
831 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100832 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100833 struct mm_stage1_locked stage1_locked;
834 bool ret = false;
835
836 /*
837 * Create a local pool so any freed memory can't be used by another
838 * thread. This is to ensure each constituent that is mapped can be
839 * unmapped again afterwards.
840 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000841 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100842
Andrew Walbranca808b12020-05-15 17:22:28 +0100843 /* Iterate over the memory region constituents within each fragment. */
844 for (i = 0; i < fragment_count; ++i) {
845 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100846
Andrew Walbranca808b12020-05-15 17:22:28 +0100847 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
848 size_t size = fragments[i][j].page_count * PAGE_SIZE;
849 paddr_t begin =
850 pa_from_ipa(ipa_init(fragments[i][j].address));
851 paddr_t end = pa_add(begin, size);
852
J-Alves7db32002021-12-14 14:44:50 +0000853 if (!clear_memory(begin, end, &local_page_pool,
854 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100855 /*
856 * api_clear_memory will defrag on failure, so
857 * no need to do it here.
858 */
859 goto out;
860 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 }
862 }
863
864 /*
865 * Need to defrag after clearing, as it may have added extra mappings to
866 * the stage 1 page table.
867 */
868 stage1_locked = mm_lock_stage1();
869 mm_defrag(stage1_locked, &local_page_pool);
870 mm_unlock_stage1(&stage1_locked);
871
872 ret = true;
873
874out:
875 mpool_fini(&local_page_pool);
876 return ret;
877}
878
879/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000880 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 *
884 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100887 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100888 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
889 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100891 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100893 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100894static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000895 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100896 struct ffa_memory_region_constituent **fragments,
897 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
898 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100899 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100900{
Andrew Walbranca808b12020-05-15 17:22:28 +0100901 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 uint32_t orig_from_mode;
903 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100904 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100905 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100906
907 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100908 * Make sure constituents are properly aligned to a 64-bit boundary. If
909 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100911 for (i = 0; i < fragment_count; ++i) {
912 if (!is_aligned(fragments[i], 8)) {
913 dlog_verbose("Constituents not aligned.\n");
914 return ffa_error(FFA_INVALID_PARAMETERS);
915 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 }
917
918 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000919 * Check if the state transition is lawful for the sender, ensure that
920 * all constituents of a memory region being shared are at the same
921 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100922 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100924 &orig_from_mode, fragments,
925 fragment_constituent_counts,
926 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100927 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100928 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100929 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100930 }
931
Andrew Walbran37c574e2020-06-03 11:45:46 +0100932 if (orig_from_mode_ret != NULL) {
933 *orig_from_mode_ret = orig_from_mode;
934 }
935
Jose Marinho09b1db82019-08-08 09:16:59 +0100936 /*
937 * Create a local pool so any freed memory can't be used by another
938 * thread. This is to ensure the original mapping can be restored if the
939 * clear fails.
940 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000941 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100942
943 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000944 * First reserve all required memory for the new page table entries
945 * without committing, to make sure the entire operation will succeed
946 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100947 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100948 if (!ffa_region_group_identity_map(
949 from_locked, fragments, fragment_constituent_counts,
950 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100951 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100952 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100953 goto out;
954 }
955
956 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000957 * Update the mapping for the sender. This won't allocate because the
958 * transaction was already prepared above, but may free pages in the
959 * case that a whole block is being unmapped that was previously
960 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100961 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100962 CHECK(ffa_region_group_identity_map(
963 from_locked, fragments, fragment_constituent_counts,
964 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100965
966 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000967 if (clear &&
968 !ffa_clear_memory_constituents(
969 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
970 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100971 /*
972 * On failure, roll back by returning memory to the sender. This
973 * may allocate pages which were previously freed into
974 * `local_page_pool` by the call above, but will never allocate
975 * more pages than that so can never fail.
976 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100978 from_locked, fragments, fragment_constituent_counts,
979 fragment_count, orig_from_mode, &local_page_pool,
980 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100981
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100983 goto out;
984 }
985
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000987
988out:
989 mpool_fini(&local_page_pool);
990
991 /*
992 * Tidy up the page table by reclaiming failed mappings (if there was an
993 * error) or merging entries into blocks where possible (on success).
994 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700995 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000996
997 return ret;
998}
999
1000/**
1001 * Validates and maps memory shared from one VM to another.
1002 *
1003 * This function requires the calling context to hold the <to> lock.
1004 *
1005 * Returns:
1006 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001007 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001008 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001010 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001011 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001012 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001013static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001014 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001015 struct ffa_memory_region_constituent **fragments,
1016 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1017 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1018 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019{
Andrew Walbranca808b12020-05-15 17:22:28 +01001020 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021 uint32_t to_mode;
1022 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001024
1025 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001026 * Make sure constituents are properly aligned to a 64-bit boundary. If
1027 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001029 for (i = 0; i < fragment_count; ++i) {
1030 if (!is_aligned(fragments[i], 8)) {
1031 return ffa_error(FFA_INVALID_PARAMETERS);
1032 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001033 }
1034
1035 /*
1036 * Check if the state transition is lawful for the recipient, and ensure
1037 * that all constituents of the memory region being retrieved are at the
1038 * same state.
1039 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001040 ret = ffa_retrieve_check_transition(
1041 to_locked, share_func, fragments, fragment_constituent_counts,
1042 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001043 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001044 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001045 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001046 }
1047
1048 /*
1049 * Create a local pool so any freed memory can't be used by another
1050 * thread. This is to ensure the original mapping can be restored if the
1051 * clear fails.
1052 */
1053 mpool_init_with_fallback(&local_page_pool, page_pool);
1054
1055 /*
1056 * First reserve all required memory for the new page table entries in
1057 * the recipient page tables without committing, to make sure the entire
1058 * operation will succeed without exhausting the page pool.
1059 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001060 if (!ffa_region_group_identity_map(
1061 to_locked, fragments, fragment_constituent_counts,
1062 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001063 /* TODO: partial defrag of failed range. */
1064 dlog_verbose(
1065 "Insufficient memory to update recipient page "
1066 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001067 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001068 goto out;
1069 }
1070
1071 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001072 if (clear &&
1073 !ffa_clear_memory_constituents(
1074 plat_ffa_owner_world_mode(from_id), fragments,
1075 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001076 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001077 goto out;
1078 }
1079
Jose Marinho09b1db82019-08-08 09:16:59 +01001080 /*
1081 * Complete the transfer by mapping the memory into the recipient. This
1082 * won't allocate because the transaction was already prepared above, so
1083 * it doesn't need to use the `local_page_pool`.
1084 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001085 CHECK(ffa_region_group_identity_map(
1086 to_locked, fragments, fragment_constituent_counts,
1087 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001088
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001089 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001090
1091out:
1092 mpool_fini(&local_page_pool);
1093
1094 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001095 * Tidy up the page table by reclaiming failed mappings (if there was an
1096 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001097 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001098 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001099
1100 return ret;
1101}
1102
Andrew Walbran290b0c92020-02-03 16:37:14 +00001103/**
1104 * Reclaims the given memory from the TEE. To do this space is first reserved in
1105 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1106 * then (if that is successful) the memory is mapped back into the <to> VM's
1107 * page table.
1108 *
1109 * This function requires the calling context to hold the <to> lock.
1110 *
1111 * Returns:
1112 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001113 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001114 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001116 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001117 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001118 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001119static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 struct vm_locked to_locked, ffa_memory_handle_t handle,
1121 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001122 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1123 struct mpool *page_pool)
1124{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001125 uint32_t to_mode;
1126 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127 struct ffa_value ret;
1128 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001129
1130 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001131 * Make sure constituents are properly aligned to a 64-bit boundary. If
1132 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001133 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001134 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001135 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001136 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001137 }
1138
1139 /*
1140 * Check if the state transition is lawful for the recipient, and ensure
1141 * that all constituents of the memory region being retrieved are at the
1142 * same state.
1143 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001144 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001145 &constituents, &constituent_count,
1146 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001148 dlog_verbose("Invalid transition.\n");
1149 return ret;
1150 }
1151
1152 /*
1153 * Create a local pool so any freed memory can't be used by another
1154 * thread. This is to ensure the original mapping can be restored if the
1155 * clear fails.
1156 */
1157 mpool_init_with_fallback(&local_page_pool, page_pool);
1158
1159 /*
1160 * First reserve all required memory for the new page table entries in
1161 * the recipient page tables without committing, to make sure the entire
1162 * operation will succeed without exhausting the page pool.
1163 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001164 if (!ffa_region_group_identity_map(to_locked, &constituents,
1165 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001166 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001167 /* TODO: partial defrag of failed range. */
1168 dlog_verbose(
1169 "Insufficient memory to update recipient page "
1170 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001171 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001172 goto out;
1173 }
1174
1175 /*
1176 * Forward the request to the TEE and see what happens.
1177 */
1178 tee_flags = 0;
1179 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001181 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001182 ret = arch_other_world_call(
1183 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1184 .arg1 = (uint32_t)handle,
1185 .arg2 = (uint32_t)(handle >> 32),
1186 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001187
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001189 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001190 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1191 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001192 ret.func, ret.arg2);
1193 goto out;
1194 }
1195
1196 /*
1197 * The TEE was happy with it, so complete the reclaim by mapping the
1198 * memory into the recipient. This won't allocate because the
1199 * transaction was already prepared above, so it doesn't need to use the
1200 * `local_page_pool`.
1201 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001202 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1203 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001204 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001205
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001206 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001207
1208out:
1209 mpool_fini(&local_page_pool);
1210
1211 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001212 * Tidy up the page table by reclaiming failed mappings (if there was an
1213 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001214 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001215 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001216
1217 return ret;
1218}
1219
Andrew Walbran996d1d12020-05-27 14:08:43 +01001220static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001221 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001222 struct ffa_memory_region_constituent **fragments,
1223 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1224 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001225{
1226 uint32_t orig_from_mode;
1227 uint32_t from_mode;
1228 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001229 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001230
Andrew Walbranca808b12020-05-15 17:22:28 +01001231 ret = ffa_relinquish_check_transition(
1232 from_locked, &orig_from_mode, fragments,
1233 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001235 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001236 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001237 }
1238
1239 /*
1240 * Create a local pool so any freed memory can't be used by another
1241 * thread. This is to ensure the original mapping can be restored if the
1242 * clear fails.
1243 */
1244 mpool_init_with_fallback(&local_page_pool, page_pool);
1245
1246 /*
1247 * First reserve all required memory for the new page table entries
1248 * without committing, to make sure the entire operation will succeed
1249 * without exhausting the page pool.
1250 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001251 if (!ffa_region_group_identity_map(
1252 from_locked, fragments, fragment_constituent_counts,
1253 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001254 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001256 goto out;
1257 }
1258
1259 /*
1260 * Update the mapping for the sender. This won't allocate because the
1261 * transaction was already prepared above, but may free pages in the
1262 * case that a whole block is being unmapped that was previously
1263 * partially mapped.
1264 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001265 CHECK(ffa_region_group_identity_map(
1266 from_locked, fragments, fragment_constituent_counts,
1267 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001268
1269 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001270 if (clear &&
1271 !ffa_clear_memory_constituents(
1272 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1273 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001274 /*
1275 * On failure, roll back by returning memory to the sender. This
1276 * may allocate pages which were previously freed into
1277 * `local_page_pool` by the call above, but will never allocate
1278 * more pages than that so can never fail.
1279 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001280 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001281 from_locked, fragments, fragment_constituent_counts,
1282 fragment_count, orig_from_mode, &local_page_pool,
1283 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001284
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001286 goto out;
1287 }
1288
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001289 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001290
1291out:
1292 mpool_fini(&local_page_pool);
1293
1294 /*
1295 * Tidy up the page table by reclaiming failed mappings (if there was an
1296 * error) or merging entries into blocks where possible (on success).
1297 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001298 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001299
1300 return ret;
1301}
1302
1303/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001304 * Complete a memory sending operation by checking that it is valid, updating
1305 * the sender page table, and then either marking the share state as having
1306 * completed sending (on success) or freeing it (on failure).
1307 *
1308 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1309 */
1310static struct ffa_value ffa_memory_send_complete(
1311 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001312 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1313 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001314{
1315 struct ffa_memory_region *memory_region = share_state->memory_region;
1316 struct ffa_value ret;
1317
1318 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001319 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001320
1321 /* Check that state is valid in sender page table and update. */
1322 ret = ffa_send_check_update(
1323 from_locked, share_state->fragments,
1324 share_state->fragment_constituent_counts,
1325 share_state->fragment_count, share_state->share_func,
1326 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001327 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1328 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001329 if (ret.func != FFA_SUCCESS_32) {
1330 /*
1331 * Free share state, it failed to send so it can't be retrieved.
1332 */
1333 dlog_verbose("Complete failed, freeing share state.\n");
1334 share_state_free(share_states, share_state, page_pool);
1335 return ret;
1336 }
1337
1338 share_state->sending_complete = true;
1339 dlog_verbose("Marked sending complete.\n");
1340
J-Alvesee68c542020-10-29 17:48:20 +00001341 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001342}
1343
1344/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001345 * Check that the memory attributes match Hafnium expectations:
1346 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1347 * Write-Allocate Cacheable.
1348 */
1349static struct ffa_value ffa_memory_attributes_validate(
1350 ffa_memory_access_permissions_t attributes)
1351{
1352 enum ffa_memory_type memory_type;
1353 enum ffa_memory_cacheability cacheability;
1354 enum ffa_memory_shareability shareability;
1355
1356 memory_type = ffa_get_memory_type_attr(attributes);
1357 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1358 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1359 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001360 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001361 }
1362
1363 cacheability = ffa_get_memory_cacheability_attr(attributes);
1364 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1365 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1366 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001367 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001368 }
1369
1370 shareability = ffa_get_memory_shareability_attr(attributes);
1371 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1372 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1373 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001374 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001375 }
1376
1377 return (struct ffa_value){.func = FFA_SUCCESS_32};
1378}
1379
1380/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381 * Check that the given `memory_region` represents a valid memory send request
1382 * of the given `share_func` type, return the clear flag and permissions via the
1383 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001384 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001385 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001386 * not.
1387 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001389 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1390 uint32_t memory_share_length, uint32_t fragment_length,
1391 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001392{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001393 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001394 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001395 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001396 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001397 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001398 enum ffa_data_access data_access;
1399 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001400 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001401
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001402 assert(permissions != NULL);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001403
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001404 /*
1405 * This should already be checked by the caller, just making the
1406 * assumption clear here.
1407 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001408 assert(memory_region->receiver_count == 1);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001409
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 /* The sender must match the message sender. */
1411 if (memory_region->sender != from_locked.vm->id) {
1412 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001413 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 }
1415
Andrew Walbrana65a1322020-04-06 19:32:32 +01001416 /*
1417 * Ensure that the composite header is within the memory bounds and
1418 * doesn't overlap the first part of the message.
1419 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001420 receivers_length = sizeof(struct ffa_memory_access) *
1421 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001422 constituents_offset =
1423 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001424 composite_memory_region_offset =
1425 memory_region->receivers[0].composite_memory_region_offset;
1426 if ((composite_memory_region_offset == 0) ||
1427 (composite_memory_region_offset <
1428 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001429 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001431 "Invalid composite memory region descriptor offset "
1432 "%d.\n",
1433 memory_region->receivers[0]
1434 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
1437
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439
1440 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001441 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001442 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001443 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1444 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001445 if (memory_share_length != constituents_offset + constituents_length) {
1446 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001447 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448 memory_region->receivers[0]
1449 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001451 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001452 if (fragment_length < memory_share_length &&
1453 fragment_length < HF_MAILBOX_SIZE) {
1454 dlog_warning(
1455 "Initial fragment length %d smaller than mailbox "
1456 "size.\n",
1457 fragment_length);
1458 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001459
Andrew Walbrana65a1322020-04-06 19:32:32 +01001460 /*
1461 * Clear is not allowed for memory sharing, as the sender still has
1462 * access to the memory.
1463 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001464 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1465 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001466 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001468 }
1469
1470 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001471 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001472 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474 }
1475
1476 /* Check that the permissions are valid. */
1477 *permissions =
1478 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001479 data_access = ffa_get_data_access_attr(*permissions);
1480 instruction_access = ffa_get_instruction_access_attr(*permissions);
1481 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1482 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001483 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1484 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001485 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001487 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001488 dlog_verbose(
1489 "Invalid instruction access permissions %#x for "
1490 "sending memory.\n",
1491 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001492 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001493 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001494 if (share_func == FFA_MEM_SHARE_32) {
1495 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001496 dlog_verbose(
1497 "Invalid data access permissions %#x for "
1498 "sharing memory.\n",
1499 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001500 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001501 }
1502 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001503 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1504 * required for share operations (but must not be specified by
1505 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001506 * returned to the retriever.
1507 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001508 ffa_set_instruction_access_attr(permissions,
1509 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001510 memory_region->receivers[0].receiver_permissions.permissions =
1511 *permissions;
1512 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 if (share_func == FFA_MEM_LEND_32 &&
1514 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001515 dlog_verbose(
1516 "Invalid data access permissions %#x for lending "
1517 "memory.\n",
1518 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001519 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001520 }
Federico Recanati85090c42021-12-15 13:17:54 +01001521
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001522 if (share_func == FFA_MEM_DONATE_32 &&
1523 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001524 dlog_verbose(
1525 "Invalid data access permissions %#x for donating "
1526 "memory.\n",
1527 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001528 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001529 }
1530
Federico Recanatid937f5e2021-12-20 17:38:23 +01001531 /*
J-Alves807794e2022-06-16 13:42:47 +01001532 * If a memory donate or lend with single borrower, the memory type
1533 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001534 */
J-Alves807794e2022-06-16 13:42:47 +01001535 if (share_func == FFA_MEM_DONATE_32 ||
1536 (share_func == FFA_MEM_LEND_32 &&
1537 memory_region->receiver_count == 1)) {
1538 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1539 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1540 dlog_verbose(
1541 "Memory type shall not be specified by "
1542 "sender.\n");
1543 return ffa_error(FFA_INVALID_PARAMETERS);
1544 }
1545 } else {
1546 /*
1547 * Check that sender's memory attributes match Hafnium
1548 * expectations: Normal Memory, Inner shareable, Write-Back
1549 * Read-Allocate Write-Allocate Cacheable.
1550 */
1551 ret = ffa_memory_attributes_validate(memory_region->attributes);
1552 if (ret.func != FFA_SUCCESS_32) {
1553 return ret;
1554 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001555 }
1556
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001557 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001558}
1559
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001560/** Forwards a memory send message on to the TEE. */
1561static struct ffa_value memory_send_tee_forward(
1562 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1563 uint32_t share_func, struct ffa_memory_region *memory_region,
1564 uint32_t memory_share_length, uint32_t fragment_length)
1565{
1566 struct ffa_value ret;
1567
1568 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1569 memory_region, fragment_length);
1570 tee_locked.vm->mailbox.recv_size = fragment_length;
1571 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1572 tee_locked.vm->mailbox.recv_func = share_func;
1573 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001574 ret = arch_other_world_call(
1575 (struct ffa_value){.func = share_func,
1576 .arg1 = memory_share_length,
1577 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001578 /*
1579 * After the call to the TEE completes it must have finished reading its
1580 * RX buffer, so it is ready for another message.
1581 */
1582 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1583
1584 return ret;
1585}
1586
Andrew Walbrana65a1322020-04-06 19:32:32 +01001587/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001588 * Gets the share state for continuing an operation to donate, lend or share
1589 * memory, and checks that it is a valid request.
1590 *
1591 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1592 * not.
1593 */
1594static struct ffa_value ffa_memory_send_continue_validate(
1595 struct share_states_locked share_states, ffa_memory_handle_t handle,
1596 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1597 struct mpool *page_pool)
1598{
1599 struct ffa_memory_share_state *share_state;
1600 struct ffa_memory_region *memory_region;
1601
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001602 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001603
1604 /*
1605 * Look up the share state by handle and make sure that the VM ID
1606 * matches.
1607 */
1608 if (!get_share_state(share_states, handle, &share_state)) {
1609 dlog_verbose(
1610 "Invalid handle %#x for memory send continuation.\n",
1611 handle);
1612 return ffa_error(FFA_INVALID_PARAMETERS);
1613 }
1614 memory_region = share_state->memory_region;
1615
1616 if (memory_region->sender != from_vm_id) {
1617 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1618 return ffa_error(FFA_INVALID_PARAMETERS);
1619 }
1620
1621 if (share_state->sending_complete) {
1622 dlog_verbose(
1623 "Sending of memory handle %#x is already complete.\n",
1624 handle);
1625 return ffa_error(FFA_INVALID_PARAMETERS);
1626 }
1627
1628 if (share_state->fragment_count == MAX_FRAGMENTS) {
1629 /*
1630 * Log a warning as this is a sign that MAX_FRAGMENTS should
1631 * probably be increased.
1632 */
1633 dlog_warning(
1634 "Too many fragments for memory share with handle %#x; "
1635 "only %d supported.\n",
1636 handle, MAX_FRAGMENTS);
1637 /* Free share state, as it's not possible to complete it. */
1638 share_state_free(share_states, share_state, page_pool);
1639 return ffa_error(FFA_NO_MEMORY);
1640 }
1641
1642 *share_state_ret = share_state;
1643
1644 return (struct ffa_value){.func = FFA_SUCCESS_32};
1645}
1646
1647/**
1648 * Forwards a memory send continuation message on to the TEE.
1649 */
1650static struct ffa_value memory_send_continue_tee_forward(
1651 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1652 uint32_t fragment_length, ffa_memory_handle_t handle)
1653{
1654 struct ffa_value ret;
1655
1656 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1657 fragment_length);
1658 tee_locked.vm->mailbox.recv_size = fragment_length;
1659 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1660 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1661 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001662 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001663 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1664 .arg1 = (uint32_t)handle,
1665 .arg2 = (uint32_t)(handle >> 32),
1666 .arg3 = fragment_length,
1667 .arg4 = (uint64_t)sender_vm_id << 16});
1668 /*
1669 * After the call to the TEE completes it must have finished reading its
1670 * RX buffer, so it is ready for another message.
1671 */
1672 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1673
1674 return ret;
1675}
1676
1677/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001678 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1679 * updates the stage-2 page tables. Specifically, check if the message length
1680 * and number of memory region constituents match, and if the transition is
1681 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001682 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001683 * Assumes that the caller has already found and locked the sender VM and copied
1684 * the memory region descriptor from the sender's TX buffer to a freshly
1685 * allocated page from Hafnium's internal pool. The caller must have also
1686 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001688 * This function takes ownership of the `memory_region` passed in and will free
1689 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001690 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001691struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001692 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001693 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001694 uint32_t fragment_length, uint32_t share_func,
1695 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001696{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001697 ffa_memory_access_permissions_t permissions;
1698 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001699 struct share_states_locked share_states;
1700 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001701
1702 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001703 * If there is an error validating the `memory_region` then we need to
1704 * free it because we own it but we won't be storing it in a share state
1705 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001706 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001707 ret = ffa_memory_send_validate(from_locked, memory_region,
1708 memory_share_length, fragment_length,
1709 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001711 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001712 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001713 }
1714
Andrew Walbrana65a1322020-04-06 19:32:32 +01001715 /* Set flag for share function, ready to be retrieved later. */
1716 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001717 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001718 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001719 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001720 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001721 case FFA_MEM_LEND_32:
1722 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001723 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001724 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001725 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001726 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001727 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001728 }
1729
Andrew Walbranca808b12020-05-15 17:22:28 +01001730 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001731 /*
1732 * Allocate a share state before updating the page table. Otherwise if
1733 * updating the page table succeeded but allocating the share state
1734 * failed then it would leave the memory in a state where nobody could
1735 * get it back.
1736 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001737 if (!allocate_share_state(share_states, share_func, memory_region,
1738 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1739 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001740 dlog_verbose("Failed to allocate share state.\n");
1741 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001742 ret = ffa_error(FFA_NO_MEMORY);
1743 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001744 }
1745
Andrew Walbranca808b12020-05-15 17:22:28 +01001746 if (fragment_length == memory_share_length) {
1747 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001748 ret = ffa_memory_send_complete(
1749 from_locked, share_states, share_state, page_pool,
1750 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001751 } else {
1752 ret = (struct ffa_value){
1753 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001754 .arg1 = (uint32_t)memory_region->handle,
1755 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001756 .arg3 = fragment_length};
1757 }
1758
1759out:
1760 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001761 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001762 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001763}
1764
1765/**
1766 * Validates a call to donate, lend or share memory to the TEE and then updates
1767 * the stage-2 page tables. Specifically, check if the message length and number
1768 * of memory region constituents match, and if the transition is valid for the
1769 * type of memory sending operation.
1770 *
1771 * Assumes that the caller has already found and locked the sender VM and the
1772 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1773 * to a freshly allocated page from Hafnium's internal pool. The caller must
1774 * have also validated that the receiver VM ID is valid.
1775 *
1776 * This function takes ownership of the `memory_region` passed in and will free
1777 * it when necessary; it must not be freed by the caller.
1778 */
1779struct ffa_value ffa_memory_tee_send(
1780 struct vm_locked from_locked, struct vm_locked to_locked,
1781 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1782 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1783{
1784 ffa_memory_access_permissions_t permissions;
1785 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001786
1787 /*
1788 * If there is an error validating the `memory_region` then we need to
1789 * free it because we own it but we won't be storing it in a share state
1790 * after all.
1791 */
1792 ret = ffa_memory_send_validate(from_locked, memory_region,
1793 memory_share_length, fragment_length,
1794 share_func, &permissions);
1795 if (ret.func != FFA_SUCCESS_32) {
1796 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001797 }
1798
Andrew Walbranca808b12020-05-15 17:22:28 +01001799 if (fragment_length == memory_share_length) {
1800 /* No more fragments to come, everything fit in one message. */
1801 struct ffa_composite_memory_region *composite =
1802 ffa_memory_region_get_composite(memory_region, 0);
1803 struct ffa_memory_region_constituent *constituents =
1804 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001805 struct mpool local_page_pool;
1806 uint32_t orig_from_mode;
1807
1808 /*
1809 * Use a local page pool so that we can roll back if necessary.
1810 */
1811 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001812
1813 ret = ffa_send_check_update(
1814 from_locked, &constituents,
1815 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001816 permissions, &local_page_pool,
1817 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1818 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001819 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001820 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001821 goto out;
1822 }
1823
1824 /* Forward memory send message on to TEE. */
1825 ret = memory_send_tee_forward(
1826 to_locked, from_locked.vm->id, share_func,
1827 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001828
1829 if (ret.func != FFA_SUCCESS_32) {
1830 dlog_verbose(
1831 "TEE didn't successfully complete memory send "
1832 "operation; returned %#x (%d). Rolling back.\n",
1833 ret.func, ret.arg2);
1834
1835 /*
1836 * The TEE failed to complete the send operation, so
1837 * roll back the page table update for the VM. This
1838 * can't fail because it won't try to allocate more
1839 * memory than was freed into the `local_page_pool` by
1840 * `ffa_send_check_update` in the initial update.
1841 */
1842 CHECK(ffa_region_group_identity_map(
1843 from_locked, &constituents,
1844 &composite->constituent_count, 1,
1845 orig_from_mode, &local_page_pool, true));
1846 }
1847
1848 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001849 } else {
1850 struct share_states_locked share_states = share_states_lock();
1851 ffa_memory_handle_t handle;
1852
1853 /*
1854 * We need to wait for the rest of the fragments before we can
1855 * check whether the transaction is valid and unmap the memory.
1856 * Call the TEE so it can do its initial validation and assign a
1857 * handle, and allocate a share state to keep what we have so
1858 * far.
1859 */
1860 ret = memory_send_tee_forward(
1861 to_locked, from_locked.vm->id, share_func,
1862 memory_region, memory_share_length, fragment_length);
1863 if (ret.func == FFA_ERROR_32) {
1864 goto out_unlock;
1865 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1866 dlog_warning(
1867 "Got %#x from TEE in response to %#x for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001868 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001869 "FFA_MEM_FRAG_RX.\n",
1870 ret.func, share_func, fragment_length,
1871 memory_share_length);
1872 ret = ffa_error(FFA_INVALID_PARAMETERS);
1873 goto out_unlock;
1874 }
1875 handle = ffa_frag_handle(ret);
1876 if (ret.arg3 != fragment_length) {
1877 dlog_warning(
1878 "Got unexpected fragment offset %d for "
1879 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1880 ret.arg3, fragment_length);
1881 ret = ffa_error(FFA_INVALID_PARAMETERS);
1882 goto out_unlock;
1883 }
1884 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1885 dlog_warning(
1886 "Got unexpected sender ID %d for "
1887 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1888 ffa_frag_sender(ret), from_locked.vm->id);
1889 ret = ffa_error(FFA_INVALID_PARAMETERS);
1890 goto out_unlock;
1891 }
1892
1893 if (!allocate_share_state(share_states, share_func,
1894 memory_region, fragment_length,
1895 handle, NULL)) {
1896 dlog_verbose("Failed to allocate share state.\n");
1897 ret = ffa_error(FFA_NO_MEMORY);
1898 goto out_unlock;
1899 }
1900 /*
1901 * Don't free the memory region fragment, as it has been stored
1902 * in the share state.
1903 */
1904 memory_region = NULL;
1905 out_unlock:
1906 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001907 }
1908
Andrew Walbranca808b12020-05-15 17:22:28 +01001909out:
1910 if (memory_region != NULL) {
1911 mpool_free(page_pool, memory_region);
1912 }
1913 dump_share_states();
1914 return ret;
1915}
1916
1917/**
1918 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1919 * this is the last fragment then checks that the transition is valid for the
1920 * type of memory sending operation and updates the stage-2 page tables of the
1921 * sender.
1922 *
1923 * Assumes that the caller has already found and locked the sender VM and copied
1924 * the memory region descriptor from the sender's TX buffer to a freshly
1925 * allocated page from Hafnium's internal pool.
1926 *
1927 * This function takes ownership of the `fragment` passed in; it must not be
1928 * freed by the caller.
1929 */
1930struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1931 void *fragment,
1932 uint32_t fragment_length,
1933 ffa_memory_handle_t handle,
1934 struct mpool *page_pool)
1935{
1936 struct share_states_locked share_states = share_states_lock();
1937 struct ffa_memory_share_state *share_state;
1938 struct ffa_value ret;
1939 struct ffa_memory_region *memory_region;
1940
1941 ret = ffa_memory_send_continue_validate(share_states, handle,
1942 &share_state,
1943 from_locked.vm->id, page_pool);
1944 if (ret.func != FFA_SUCCESS_32) {
1945 goto out_free_fragment;
1946 }
1947 memory_region = share_state->memory_region;
1948
1949 if (memory_region->receivers[0].receiver_permissions.receiver ==
1950 HF_TEE_VM_ID) {
1951 dlog_error(
1952 "Got hypervisor-allocated handle for memory send to "
1953 "TEE. This should never happen, and indicates a bug in "
1954 "EL3 code.\n");
1955 ret = ffa_error(FFA_INVALID_PARAMETERS);
1956 goto out_free_fragment;
1957 }
1958
1959 /* Add this fragment. */
1960 share_state->fragments[share_state->fragment_count] = fragment;
1961 share_state->fragment_constituent_counts[share_state->fragment_count] =
1962 fragment_length / sizeof(struct ffa_memory_region_constituent);
1963 share_state->fragment_count++;
1964
1965 /* Check whether the memory send operation is now ready to complete. */
1966 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001967 ret = ffa_memory_send_complete(
1968 from_locked, share_states, share_state, page_pool,
1969 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001970 } else {
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;
1979
1980out_free_fragment:
1981 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001982
1983out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001984 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001985 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001986}
1987
Andrew Walbranca808b12020-05-15 17:22:28 +01001988/**
1989 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1990 * is the last fragment then checks that the transition is valid for the type of
1991 * memory sending operation and updates the stage-2 page tables of the sender.
1992 *
1993 * Assumes that the caller has already found and locked the sender VM and copied
1994 * the memory region descriptor from the sender's TX buffer to a freshly
1995 * allocated page from Hafnium's internal pool.
1996 *
1997 * This function takes ownership of the `memory_region` passed in and will free
1998 * it when necessary; it must not be freed by the caller.
1999 */
2000struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
2001 struct vm_locked to_locked,
2002 void *fragment,
2003 uint32_t fragment_length,
2004 ffa_memory_handle_t handle,
2005 struct mpool *page_pool)
2006{
2007 struct share_states_locked share_states = share_states_lock();
2008 struct ffa_memory_share_state *share_state;
2009 struct ffa_value ret;
2010 struct ffa_memory_region *memory_region;
2011
2012 ret = ffa_memory_send_continue_validate(share_states, handle,
2013 &share_state,
2014 from_locked.vm->id, page_pool);
2015 if (ret.func != FFA_SUCCESS_32) {
2016 goto out_free_fragment;
2017 }
2018 memory_region = share_state->memory_region;
2019
2020 if (memory_region->receivers[0].receiver_permissions.receiver !=
2021 HF_TEE_VM_ID) {
2022 dlog_error(
2023 "Got SPM-allocated handle for memory send to non-TEE "
2024 "VM. This should never happen, and indicates a bug.\n");
2025 ret = ffa_error(FFA_INVALID_PARAMETERS);
2026 goto out_free_fragment;
2027 }
2028
2029 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2030 to_locked.vm->mailbox.recv == NULL) {
2031 /*
2032 * If the TEE RX buffer is not available, tell the sender to
2033 * retry by returning the current offset again.
2034 */
2035 ret = (struct ffa_value){
2036 .func = FFA_MEM_FRAG_RX_32,
2037 .arg1 = (uint32_t)handle,
2038 .arg2 = (uint32_t)(handle >> 32),
2039 .arg3 = share_state_next_fragment_offset(share_states,
2040 share_state),
2041 };
2042 goto out_free_fragment;
2043 }
2044
2045 /* Add this fragment. */
2046 share_state->fragments[share_state->fragment_count] = fragment;
2047 share_state->fragment_constituent_counts[share_state->fragment_count] =
2048 fragment_length / sizeof(struct ffa_memory_region_constituent);
2049 share_state->fragment_count++;
2050
2051 /* Check whether the memory send operation is now ready to complete. */
2052 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002053 struct mpool local_page_pool;
2054 uint32_t orig_from_mode;
2055
2056 /*
2057 * Use a local page pool so that we can roll back if necessary.
2058 */
2059 mpool_init_with_fallback(&local_page_pool, page_pool);
2060
Andrew Walbranca808b12020-05-15 17:22:28 +01002061 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002062 share_state, &local_page_pool,
2063 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002064
2065 if (ret.func == FFA_SUCCESS_32) {
2066 /*
2067 * Forward final fragment on to the TEE so that
2068 * it can complete the memory sending operation.
2069 */
2070 ret = memory_send_continue_tee_forward(
2071 to_locked, from_locked.vm->id, fragment,
2072 fragment_length, handle);
2073
2074 if (ret.func != FFA_SUCCESS_32) {
2075 /*
2076 * The error will be passed on to the caller,
2077 * but log it here too.
2078 */
2079 dlog_verbose(
2080 "TEE didn't successfully complete "
2081 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002082 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002083 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002084
2085 /*
2086 * The TEE failed to complete the send
2087 * operation, so roll back the page table update
2088 * for the VM. This can't fail because it won't
2089 * try to allocate more memory than was freed
2090 * into the `local_page_pool` by
2091 * `ffa_send_check_update` in the initial
2092 * update.
2093 */
2094 CHECK(ffa_region_group_identity_map(
2095 from_locked, share_state->fragments,
2096 share_state
2097 ->fragment_constituent_counts,
2098 share_state->fragment_count,
2099 orig_from_mode, &local_page_pool,
2100 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002101 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002102
Andrew Walbranca808b12020-05-15 17:22:28 +01002103 /* Free share state. */
2104 share_state_free(share_states, share_state, page_pool);
2105 } else {
2106 /* Abort sending to TEE. */
2107 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002108 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002109 .func = FFA_MEM_RECLAIM_32,
2110 .arg1 = (uint32_t)handle,
2111 .arg2 = (uint32_t)(handle >> 32)});
2112
2113 if (tee_ret.func != FFA_SUCCESS_32) {
2114 /*
2115 * Nothing we can do if TEE doesn't abort
2116 * properly, just log it.
2117 */
2118 dlog_verbose(
2119 "TEE didn't successfully abort failed "
2120 "memory send operation; returned %#x "
2121 "(%d).\n",
2122 tee_ret.func, tee_ret.arg2);
2123 }
2124 /*
2125 * We don't need to free the share state in this case
2126 * because ffa_memory_send_complete does that already.
2127 */
2128 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002129
2130 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002131 } else {
2132 uint32_t next_fragment_offset =
2133 share_state_next_fragment_offset(share_states,
2134 share_state);
2135
2136 ret = memory_send_continue_tee_forward(
2137 to_locked, from_locked.vm->id, fragment,
2138 fragment_length, handle);
2139
2140 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2141 ffa_frag_handle(ret) != handle ||
2142 ret.arg3 != next_fragment_offset ||
2143 ffa_frag_sender(ret) != from_locked.vm->id) {
2144 dlog_verbose(
2145 "Got unexpected result from forwarding "
2146 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2147 "offset %d, sender %d); expected "
2148 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2149 "sender %d).\n",
2150 ret.func, ffa_frag_handle(ret), ret.arg3,
2151 ffa_frag_sender(ret), handle,
2152 next_fragment_offset, from_locked.vm->id);
2153 /* Free share state. */
2154 share_state_free(share_states, share_state, page_pool);
2155 ret = ffa_error(FFA_INVALID_PARAMETERS);
2156 goto out;
2157 }
2158
2159 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2160 .arg1 = (uint32_t)handle,
2161 .arg2 = (uint32_t)(handle >> 32),
2162 .arg3 = next_fragment_offset};
2163 }
2164 goto out;
2165
2166out_free_fragment:
2167 mpool_free(page_pool, fragment);
2168
2169out:
2170 share_states_unlock(&share_states);
2171 return ret;
2172}
2173
2174/** Clean up after the receiver has finished retrieving a memory region. */
2175static void ffa_memory_retrieve_complete(
2176 struct share_states_locked share_states,
2177 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2178{
2179 if (share_state->share_func == FFA_MEM_DONATE_32) {
2180 /*
2181 * Memory that has been donated can't be relinquished,
2182 * so no need to keep the share state around.
2183 */
2184 share_state_free(share_states, share_state, page_pool);
2185 dlog_verbose("Freed share state for donate.\n");
2186 }
2187}
2188
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002189struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2190 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002191 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002192 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002193{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002194 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002195 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002196 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002197 sizeof(struct ffa_memory_access);
2198 ffa_memory_handle_t handle = retrieve_request->handle;
2199 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002200 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002201 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2202 struct ffa_memory_region *memory_region;
2203 ffa_memory_access_permissions_t sent_permissions;
2204 enum ffa_data_access sent_data_access;
2205 enum ffa_instruction_access sent_instruction_access;
2206 ffa_memory_access_permissions_t requested_permissions;
2207 enum ffa_data_access requested_data_access;
2208 enum ffa_instruction_access requested_instruction_access;
2209 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002210 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002211 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002212 struct ffa_memory_share_state *share_state;
2213 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002214 struct ffa_composite_memory_region *composite;
2215 uint32_t total_length;
2216 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002217
2218 dump_share_states();
2219
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002220 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002222 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002223 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002224 expected_retrieve_request_length,
2225 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002226 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002227 }
2228
Andrew Walbrana65a1322020-04-06 19:32:32 +01002229 if (retrieve_request->receiver_count != 1) {
2230 dlog_verbose(
2231 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002232 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 "expected 1).\n",
2234 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002235 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 }
2237
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002238 share_states = share_states_lock();
2239 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002240 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002241 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002242 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002243 goto out;
2244 }
2245
Andrew Walbrana65a1322020-04-06 19:32:32 +01002246 memory_region = share_state->memory_region;
2247 CHECK(memory_region != NULL);
2248
2249 /*
2250 * Check that the transaction type expected by the receiver is correct,
2251 * if it has been specified.
2252 */
2253 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002254 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002255 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002256 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002257 dlog_verbose(
2258 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002259 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002260 transaction_type,
2261 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002263 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002264 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002265 goto out;
2266 }
2267
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002268 if (retrieve_request->sender != memory_region->sender) {
2269 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002270 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002271 "expected %d for handle %#x.\n",
2272 retrieve_request->sender, memory_region->sender,
2273 handle);
J-Alves040c4ef2022-05-13 14:42:49 +01002274 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002275 goto out;
2276 }
2277
2278 if (retrieve_request->tag != memory_region->tag) {
2279 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002281 "%d for handle %#x.\n",
2282 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002283 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002284 goto out;
2285 }
2286
Andrew Walbrana65a1322020-04-06 19:32:32 +01002287 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2288 to_locked.vm->id) {
2289 dlog_verbose(
2290 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002291 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002292 retrieve_request->receivers[0]
2293 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002294 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002295 goto out;
2296 }
2297
2298 if (memory_region->receivers[0].receiver_permissions.receiver !=
2299 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002300 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002301 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2302 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002303 to_locked.vm->id,
2304 memory_region->receivers[0]
2305 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002306 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002307 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002308 goto out;
2309 }
2310
Andrew Walbranca808b12020-05-15 17:22:28 +01002311 if (!share_state->sending_complete) {
2312 dlog_verbose(
2313 "Memory with handle %#x not fully sent, can't "
2314 "retrieve.\n",
2315 handle);
2316 ret = ffa_error(FFA_INVALID_PARAMETERS);
2317 goto out;
2318 }
2319
2320 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002321 dlog_verbose("Memory with handle %#x already retrieved.\n",
2322 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002323 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002324 goto out;
2325 }
2326
Andrew Walbrana65a1322020-04-06 19:32:32 +01002327 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2328 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 dlog_verbose(
2330 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002331 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002332 retrieve_request->receivers[0]
2333 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002334 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002335 goto out;
2336 }
2337
Federico Recanati85090c42021-12-15 13:17:54 +01002338 if ((retrieve_request->flags &
2339 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0) {
2340 dlog_verbose(
2341 "Retriever specified 'address range alignment hint'"
2342 " not supported.\n");
2343 ret = ffa_error(FFA_INVALID_PARAMETERS);
2344 goto out;
2345 }
2346 if ((retrieve_request->flags &
2347 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2348 dlog_verbose(
2349 "Bits 8-5 must be zero in memory region's flags "
2350 "(address range alignment hint not supported).\n");
2351 ret = ffa_error(FFA_INVALID_PARAMETERS);
2352 goto out;
2353 }
2354
J-Alves84658fc2021-06-17 14:37:32 +01002355 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2356 dlog_verbose(
2357 "Bits 31-10 must be zero in memory region's flags.\n");
2358 ret = ffa_error(FFA_INVALID_PARAMETERS);
2359 goto out;
2360 }
2361
2362 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2363 (retrieve_request->flags &
2364 (FFA_MEMORY_REGION_FLAG_CLEAR |
2365 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2366 dlog_verbose(
2367 "Memory Share operation can't clean after relinquish "
2368 "memory region.\n");
2369 ret = ffa_error(FFA_INVALID_PARAMETERS);
2370 goto out;
2371 }
2372
Andrew Walbrana65a1322020-04-06 19:32:32 +01002373 /*
J-Alves17c069c2021-12-07 16:00:38 +00002374 * If the borrower needs the memory to be cleared before mapping to its
2375 * address space, the sender should have set the flag when calling
2376 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2377 */
2378 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2379 (share_state->memory_region->flags &
2380 FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2381 dlog_verbose(
2382 "Borrower needs memory cleared. Sender needs to set "
2383 "flag for clearing memory.\n");
2384 ret = ffa_error(FFA_DENIED);
2385 goto out;
2386 }
2387
2388 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002389 * Check permissions from sender against permissions requested by
2390 * receiver.
2391 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002392 sent_permissions =
2393 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002394 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002395 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002396 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002397 requested_permissions =
2398 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002399 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002400 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002401 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002402 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002403
2404 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2405 requested_permissions == FFA_DATA_ACCESS_RO) &&
2406 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2407 dlog_verbose(
2408 "Receiver has RO permissions can not request clear.\n");
2409 ret = ffa_error(FFA_DENIED);
2410 goto out;
2411 }
2412
Andrew Walbrana65a1322020-04-06 19:32:32 +01002413 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002414 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2415 case FFA_DATA_ACCESS_RW:
2416 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2417 requested_data_access == FFA_DATA_ACCESS_RW) {
2418 ffa_set_data_access_attr(&permissions,
2419 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002420 break;
2421 }
2422 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002423 case FFA_DATA_ACCESS_RO:
2424 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2425 requested_data_access == FFA_DATA_ACCESS_RO) {
2426 ffa_set_data_access_attr(&permissions,
2427 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002428 break;
2429 }
2430 dlog_verbose(
2431 "Invalid data access requested; sender specified "
2432 "permissions %#x but receiver requested %#x.\n",
2433 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002434 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002435 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002436 case FFA_DATA_ACCESS_RESERVED:
2437 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002438 "checked before this point.");
2439 }
2440 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002441 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2442 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002443 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002444 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2445 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2446 ffa_set_instruction_access_attr(
2447 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002448 break;
2449 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002450 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002451 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002452 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2453 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2454 ffa_set_instruction_access_attr(
2455 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002456 break;
2457 }
2458 dlog_verbose(
2459 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002460 "specified permissions %#x but receiver requested "
2461 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002462 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002464 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 case FFA_INSTRUCTION_ACCESS_RESERVED:
2466 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002467 "be checked before this point.");
2468 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002469
J-Alves614d9f42022-06-28 14:03:10 +01002470 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2471 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2472 /*
2473 * Ensure receiver's attributes are compatible with how Hafnium
2474 * maps memory: Normal Memory, Inner shareable, Write-Back
2475 * Read-Allocate Write-Allocate Cacheable.
2476 */
2477 ret = ffa_memory_attributes_validate(
2478 retrieve_request->attributes);
2479 if (ret.func != FFA_SUCCESS_32) {
2480 goto out;
2481 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002482 }
2483
J-Alves7cd5eb32020-10-16 19:06:10 +01002484 memory_to_attributes = ffa_memory_permissions_to_mode(
2485 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002486 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002487 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002488 share_state->fragment_constituent_counts,
2489 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002490 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002491 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002492 goto out;
2493 }
2494
2495 /*
2496 * Copy response to RX buffer of caller and deliver the message. This
2497 * must be done before the share_state is (possibly) freed.
2498 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002499 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002500 composite = ffa_memory_region_get_composite(memory_region, 0);
2501 /*
2502 * Constituents which we received in the first fragment should always
2503 * fit in the first fragment we are sending, because the header is the
2504 * same size in both cases and we have a fixed message buffer size. So
2505 * `ffa_retrieved_memory_region_init` should never fail.
2506 */
2507 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002508 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2509 memory_region->sender, memory_region->attributes,
2510 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002511 composite->page_count, composite->constituent_count,
2512 share_state->fragments[0],
2513 share_state->fragment_constituent_counts[0], &total_length,
2514 &fragment_length));
2515 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002516 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002517 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002518 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2519
Andrew Walbranca808b12020-05-15 17:22:28 +01002520 share_state->retrieved_fragment_count[0] = 1;
2521 if (share_state->retrieved_fragment_count[0] ==
2522 share_state->fragment_count) {
2523 ffa_memory_retrieve_complete(share_states, share_state,
2524 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525 }
2526
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002527 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002528 .arg1 = total_length,
2529 .arg2 = fragment_length};
2530
2531out:
2532 share_states_unlock(&share_states);
2533 dump_share_states();
2534 return ret;
2535}
2536
2537struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2538 ffa_memory_handle_t handle,
2539 uint32_t fragment_offset,
2540 struct mpool *page_pool)
2541{
2542 struct ffa_memory_region *memory_region;
2543 struct share_states_locked share_states;
2544 struct ffa_memory_share_state *share_state;
2545 struct ffa_value ret;
2546 uint32_t fragment_index;
2547 uint32_t retrieved_constituents_count;
2548 uint32_t i;
2549 uint32_t expected_fragment_offset;
2550 uint32_t remaining_constituent_count;
2551 uint32_t fragment_length;
2552
2553 dump_share_states();
2554
2555 share_states = share_states_lock();
2556 if (!get_share_state(share_states, handle, &share_state)) {
2557 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2558 handle);
2559 ret = ffa_error(FFA_INVALID_PARAMETERS);
2560 goto out;
2561 }
2562
2563 memory_region = share_state->memory_region;
2564 CHECK(memory_region != NULL);
2565
2566 if (memory_region->receivers[0].receiver_permissions.receiver !=
2567 to_locked.vm->id) {
2568 dlog_verbose(
2569 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2570 "of handle %#x.\n",
2571 to_locked.vm->id,
2572 memory_region->receivers[0]
2573 .receiver_permissions.receiver,
2574 handle);
2575 ret = ffa_error(FFA_INVALID_PARAMETERS);
2576 goto out;
2577 }
2578
2579 if (!share_state->sending_complete) {
2580 dlog_verbose(
2581 "Memory with handle %#x not fully sent, can't "
2582 "retrieve.\n",
2583 handle);
2584 ret = ffa_error(FFA_INVALID_PARAMETERS);
2585 goto out;
2586 }
2587
2588 if (share_state->retrieved_fragment_count[0] == 0 ||
2589 share_state->retrieved_fragment_count[0] >=
2590 share_state->fragment_count) {
2591 dlog_verbose(
2592 "Retrieval of memory with handle %#x not yet started "
2593 "or already completed (%d/%d fragments retrieved).\n",
2594 handle, share_state->retrieved_fragment_count[0],
2595 share_state->fragment_count);
2596 ret = ffa_error(FFA_INVALID_PARAMETERS);
2597 goto out;
2598 }
2599
2600 fragment_index = share_state->retrieved_fragment_count[0];
2601
2602 /*
2603 * Check that the given fragment offset is correct by counting how many
2604 * constituents were in the fragments previously sent.
2605 */
2606 retrieved_constituents_count = 0;
2607 for (i = 0; i < fragment_index; ++i) {
2608 retrieved_constituents_count +=
2609 share_state->fragment_constituent_counts[i];
2610 }
2611 expected_fragment_offset =
2612 ffa_composite_constituent_offset(memory_region, 0) +
2613 retrieved_constituents_count *
2614 sizeof(struct ffa_memory_region_constituent);
2615 if (fragment_offset != expected_fragment_offset) {
2616 dlog_verbose("Fragment offset was %d but expected %d.\n",
2617 fragment_offset, expected_fragment_offset);
2618 ret = ffa_error(FFA_INVALID_PARAMETERS);
2619 goto out;
2620 }
2621
2622 remaining_constituent_count = ffa_memory_fragment_init(
2623 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2624 share_state->fragments[fragment_index],
2625 share_state->fragment_constituent_counts[fragment_index],
2626 &fragment_length);
2627 CHECK(remaining_constituent_count == 0);
2628 to_locked.vm->mailbox.recv_size = fragment_length;
2629 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2630 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2631 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2632 share_state->retrieved_fragment_count[0]++;
2633 if (share_state->retrieved_fragment_count[0] ==
2634 share_state->fragment_count) {
2635 ffa_memory_retrieve_complete(share_states, share_state,
2636 page_pool);
2637 }
2638
2639 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2640 .arg1 = (uint32_t)handle,
2641 .arg2 = (uint32_t)(handle >> 32),
2642 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002643
2644out:
2645 share_states_unlock(&share_states);
2646 dump_share_states();
2647 return ret;
2648}
2649
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002652 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002653{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002654 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002655 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002656 struct ffa_memory_share_state *share_state;
2657 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002658 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002659 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002660
Andrew Walbrana65a1322020-04-06 19:32:32 +01002661 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002662 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002663 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002664 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002665 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002666 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002667 }
2668
Andrew Walbrana65a1322020-04-06 19:32:32 +01002669 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002670 dlog_verbose(
2671 "VM ID %d in relinquish message doesn't match calling "
2672 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002673 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002674 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002675 }
2676
2677 dump_share_states();
2678
2679 share_states = share_states_lock();
2680 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002681 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002682 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002683 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 goto out;
2685 }
2686
Andrew Walbranca808b12020-05-15 17:22:28 +01002687 if (!share_state->sending_complete) {
2688 dlog_verbose(
2689 "Memory with handle %#x not fully sent, can't "
2690 "relinquish.\n",
2691 handle);
2692 ret = ffa_error(FFA_INVALID_PARAMETERS);
2693 goto out;
2694 }
2695
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002696 memory_region = share_state->memory_region;
2697 CHECK(memory_region != NULL);
2698
Andrew Walbrana65a1322020-04-06 19:32:32 +01002699 if (memory_region->receivers[0].receiver_permissions.receiver !=
2700 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002701 dlog_verbose(
2702 "VM ID %d tried to relinquish memory region with "
2703 "handle %#x but receiver was %d.\n",
2704 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002705 memory_region->receivers[0]
2706 .receiver_permissions.receiver);
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->retrieved_fragment_count[0] !=
2712 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002713 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002714 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002715 "relinquish.\n",
2716 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002717 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002718 goto out;
2719 }
2720
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002721 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002722
2723 /*
2724 * Clear is not allowed for memory that was shared, as the original
2725 * sender still has access to the memory.
2726 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002727 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002729 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730 goto out;
2731 }
2732
Andrew Walbranca808b12020-05-15 17:22:28 +01002733 ret = ffa_relinquish_check_update(
2734 from_locked, share_state->fragments,
2735 share_state->fragment_constituent_counts,
2736 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002737
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002738 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002739 /*
2740 * Mark memory handle as not retrieved, so it can be reclaimed
2741 * (or retrieved again).
2742 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002743 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002744 }
2745
2746out:
2747 share_states_unlock(&share_states);
2748 dump_share_states();
2749 return ret;
2750}
2751
2752/**
2753 * Validates that the reclaim transition is allowed for the given handle,
2754 * updates the page table of the reclaiming VM, and frees the internal state
2755 * associated with the handle.
2756 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002757struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002758 ffa_memory_handle_t handle,
2759 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002760 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002761{
2762 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002763 struct ffa_memory_share_state *share_state;
2764 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002765 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002766
2767 dump_share_states();
2768
2769 share_states = share_states_lock();
2770 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002771 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002772 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002773 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002774 goto out;
2775 }
2776
2777 memory_region = share_state->memory_region;
2778 CHECK(memory_region != NULL);
2779
2780 if (to_locked.vm->id != memory_region->sender) {
2781 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002782 "VM %#x attempted to reclaim memory handle %#x "
2783 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002784 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002785 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002786 goto out;
2787 }
2788
Andrew Walbranca808b12020-05-15 17:22:28 +01002789 if (!share_state->sending_complete) {
2790 dlog_verbose(
2791 "Memory with handle %#x not fully sent, can't "
2792 "reclaim.\n",
2793 handle);
2794 ret = ffa_error(FFA_INVALID_PARAMETERS);
2795 goto out;
2796 }
2797
2798 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002799 dlog_verbose(
2800 "Tried to reclaim memory handle %#x that has not been "
2801 "relinquished.\n",
2802 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002803 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002804 goto out;
2805 }
2806
Andrew Walbranca808b12020-05-15 17:22:28 +01002807 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002808 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002809 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002810 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002811 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002812
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002813 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002814 share_state_free(share_states, share_state, page_pool);
2815 dlog_verbose("Freed share state after successful reclaim.\n");
2816 }
2817
2818out:
2819 share_states_unlock(&share_states);
2820 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002821}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002822
2823/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002824 * Validates that the reclaim transition is allowed for the memory region with
2825 * the given handle which was previously shared with the TEE, tells the TEE to
2826 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2827 *
2828 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002829 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002830struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002831 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002832 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002833 ffa_memory_region_flags_t flags,
2834 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002835{
Andrew Walbranca808b12020-05-15 17:22:28 +01002836 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2837 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2838 struct ffa_value tee_ret;
2839 uint32_t length;
2840 uint32_t fragment_length;
2841 uint32_t fragment_offset;
2842 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002843 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002844 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2845
2846 CHECK(request_length <= HF_MAILBOX_SIZE);
2847 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2848
2849 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002850 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002851 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2852 .arg1 = request_length,
2853 .arg2 = request_length});
2854 if (tee_ret.func == FFA_ERROR_32) {
2855 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2856 return tee_ret;
2857 }
2858 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2859 dlog_verbose(
2860 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2861 tee_ret.func);
2862 return ffa_error(FFA_INVALID_PARAMETERS);
2863 }
2864
2865 length = tee_ret.arg1;
2866 fragment_length = tee_ret.arg2;
2867
2868 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2869 length > sizeof(tee_retrieve_buffer)) {
2870 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2871 fragment_length, length, HF_MAILBOX_SIZE,
2872 sizeof(tee_retrieve_buffer));
2873 return ffa_error(FFA_INVALID_PARAMETERS);
2874 }
2875
2876 /*
2877 * Copy the first fragment of the memory region descriptor to an
2878 * internal buffer.
2879 */
2880 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2881 from_locked.vm->mailbox.send, fragment_length);
2882
2883 /* Fetch the remaining fragments into the same buffer. */
2884 fragment_offset = fragment_length;
2885 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002886 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002887 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2888 .arg1 = (uint32_t)handle,
2889 .arg2 = (uint32_t)(handle >> 32),
2890 .arg3 = fragment_offset});
2891 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2892 dlog_verbose(
2893 "Got %#x (%d) from TEE in response to "
2894 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2895 tee_ret.func, tee_ret.arg2);
2896 return tee_ret;
2897 }
2898 if (ffa_frag_handle(tee_ret) != handle) {
2899 dlog_verbose(
2900 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2901 "in response to FFA_MEM_FRAG_RX for handle "
2902 "%#x.\n",
2903 ffa_frag_handle(tee_ret), handle);
2904 return ffa_error(FFA_INVALID_PARAMETERS);
2905 }
2906 if (ffa_frag_sender(tee_ret) != 0) {
2907 dlog_verbose(
2908 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2909 "(expected 0).\n",
2910 ffa_frag_sender(tee_ret));
2911 return ffa_error(FFA_INVALID_PARAMETERS);
2912 }
2913 fragment_length = tee_ret.arg3;
2914 if (fragment_length > HF_MAILBOX_SIZE ||
2915 fragment_offset + fragment_length > length) {
2916 dlog_verbose(
2917 "Invalid fragment length %d at offset %d (max "
2918 "%d).\n",
2919 fragment_length, fragment_offset,
2920 HF_MAILBOX_SIZE);
2921 return ffa_error(FFA_INVALID_PARAMETERS);
2922 }
2923 memcpy_s(tee_retrieve_buffer + fragment_offset,
2924 sizeof(tee_retrieve_buffer) - fragment_offset,
2925 from_locked.vm->mailbox.send, fragment_length);
2926
2927 fragment_offset += fragment_length;
2928 }
2929
2930 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002931
2932 if (memory_region->receiver_count != 1) {
2933 /* Only one receiver supported by Hafnium for now. */
2934 dlog_verbose(
2935 "Multiple recipients not supported (got %d, expected "
2936 "1).\n",
2937 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002938 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002939 }
2940
2941 if (memory_region->handle != handle) {
2942 dlog_verbose(
2943 "Got memory region handle %#x from TEE but requested "
2944 "handle %#x.\n",
2945 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002946 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002947 }
2948
2949 /* The original sender must match the caller. */
2950 if (to_locked.vm->id != memory_region->sender) {
2951 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002952 "VM %#x attempted to reclaim memory handle %#x "
2953 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002954 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002955 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002956 }
2957
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002958 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002959
2960 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002961 * Validate that the reclaim transition is allowed for the given memory
2962 * region, forward the request to the TEE and then map the memory back
2963 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002964 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002965 return ffa_tee_reclaim_check_update(
2966 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002967 composite->constituent_count, memory_to_attributes,
2968 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002969}