blob: c7ae984ba981dc051d03a500338b531592777687 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000016#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010017#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000020#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000022#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000024/** The maximum number of recipients a memory region may be sent to. */
25#define MAX_MEM_SHARE_RECIPIENTS 1
26
27/**
28 * The maximum number of memory sharing handles which may be active at once. A
29 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
30 * or LEND handle is active from when it is sent to when it is reclaimed.
31 */
32#define MAX_MEM_SHARES 100
33
Andrew Walbranca808b12020-05-15 17:22:28 +010034/**
35 * The maximum number of fragments into which a memory sharing message may be
36 * broken.
37 */
38#define MAX_FRAGMENTS 20
39
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
41 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000042 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
44 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000045 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010046static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010047 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010048static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
49 "struct ffa_memory_access must be a multiple of 16 bytes long.");
50static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
51 "struct ffa_memory_region must be a multiple of 16 bytes long.");
52static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
53 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000055
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010056struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000057 /**
58 * The memory region being shared, or NULL if this share state is
59 * unallocated.
60 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010061 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000062
Andrew Walbranca808b12020-05-15 17:22:28 +010063 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
64
65 /** The number of constituents in each fragment. */
66 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
67
68 /**
69 * The number of valid elements in the `fragments` and
70 * `fragment_constituent_counts` arrays.
71 */
72 uint32_t fragment_count;
73
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010075 * The FF-A function used for sharing the memory. Must be one of
76 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000077 * share state is allocated, or 0.
78 */
79 uint32_t share_func;
80
81 /**
J-Alves2a0d2882020-10-29 14:49:50 +000082 * The sender's original mode before invoking the FF-A function for
83 * sharing the memory.
84 * This is used to reset the original configuration when sender invokes
85 * FFA_MEM_RECLAIM_32.
86 */
87 uint32_t sender_orig_mode;
88
89 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010090 * True if all the fragments of this sharing request have been sent and
91 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092 */
Andrew Walbranca808b12020-05-15 17:22:28 +010093 bool sending_complete;
94
95 /**
96 * How many fragments of the memory region each recipient has retrieved
97 * so far. The order of this array matches the order of the endpoint
98 * memory access descriptors in the memory region descriptor. Any
99 * entries beyond the receiver_count will always be 0.
100 */
101 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000102};
103
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000104/**
105 * Encapsulates the set of share states while the `share_states_lock` is held.
106 */
107struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100108 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000109};
110
111/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100112 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000113 * by this lock.
114 */
115static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100116static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000117
118/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100119 * Buffer for retrieving memory region information from the TEE for when a
120 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
121 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100123alignas(PAGE_SIZE) static uint8_t
124 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
125
126/**
J-Alves917d2f22020-10-30 18:39:30 +0000127 * Extracts the index from a memory handle allocated by Hafnium's current world.
128 */
129uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
130{
131 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
132}
133
134/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 * Initialises the next available `struct ffa_memory_share_state` and sets
136 * `share_state_ret` to a pointer to it. If `handle` is
137 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
138 * uses the provided handle which is assumed to be globally unique.
139 *
140 * Returns true on success or false if none are available.
141 */
142static bool allocate_share_state(
143 struct share_states_locked share_states, uint32_t share_func,
144 struct ffa_memory_region *memory_region, uint32_t fragment_length,
145 ffa_memory_handle_t handle,
146 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000147{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100148 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000150 assert(share_states.share_states != NULL);
151 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100154 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000155 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100156 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100157 &share_states.share_states[i];
158 struct ffa_composite_memory_region *composite =
159 ffa_memory_region_get_composite(memory_region,
160 0);
161
162 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000163 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200164 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000166 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 allocated_state->share_func = share_func;
169 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100170 allocated_state->fragment_count = 1;
171 allocated_state->fragments[0] = composite->constituents;
172 allocated_state->fragment_constituent_counts[0] =
173 (fragment_length -
174 ffa_composite_constituent_offset(memory_region,
175 0)) /
176 sizeof(struct ffa_memory_region_constituent);
177 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000178 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 allocated_state->retrieved_fragment_count[j] =
180 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100182 if (share_state_ret != NULL) {
183 *share_state_ret = allocated_state;
184 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185 return true;
186 }
187 }
188
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189 return false;
190}
191
192/** Locks the share states lock. */
193struct share_states_locked share_states_lock(void)
194{
195 sl_lock(&share_states_lock_instance);
196
197 return (struct share_states_locked){.share_states = share_states};
198}
199
200/** Unlocks the share states lock. */
201static void share_states_unlock(struct share_states_locked *share_states)
202{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000203 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000204 share_states->share_states = NULL;
205 sl_unlock(&share_states_lock_instance);
206}
207
208/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100209 * If the given handle is a valid handle for an allocated share state then
210 * initialises `share_state_ret` to point to the share state and returns true.
211 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000212 */
213static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100214 ffa_memory_handle_t handle,
215 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100217 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000218 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000219
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000220 assert(share_states.share_states != NULL);
221 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100222
223 /*
224 * First look for a share_state allocated by us, in which case the
225 * handle is based on the index.
226 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200227 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000228 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100229 if (index < MAX_MEM_SHARES) {
230 share_state = &share_states.share_states[index];
231 if (share_state->share_func != 0) {
232 *share_state_ret = share_state;
233 return true;
234 }
235 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000236 }
237
Andrew Walbranca808b12020-05-15 17:22:28 +0100238 /* Fall back to a linear scan. */
239 for (index = 0; index < MAX_MEM_SHARES; ++index) {
240 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000241 if (share_state->memory_region != NULL &&
242 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100243 share_state->share_func != 0) {
244 *share_state_ret = share_state;
245 return true;
246 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247 }
248
Andrew Walbranca808b12020-05-15 17:22:28 +0100249 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000250}
251
252/** Marks a share state as unallocated. */
253static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255 struct mpool *page_pool)
256{
Andrew Walbranca808b12020-05-15 17:22:28 +0100257 uint32_t i;
258
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100261 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000262 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100263 /*
264 * First fragment is part of the same page as the `memory_region`, so it
265 * doesn't need to be freed separately.
266 */
267 share_state->fragments[0] = NULL;
268 share_state->fragment_constituent_counts[0] = 0;
269 for (i = 1; i < share_state->fragment_count; ++i) {
270 mpool_free(page_pool, share_state->fragments[i]);
271 share_state->fragments[i] = NULL;
272 share_state->fragment_constituent_counts[i] = 0;
273 }
274 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275 share_state->memory_region = NULL;
276}
277
Andrew Walbranca808b12020-05-15 17:22:28 +0100278/** Checks whether the given share state has been fully sent. */
279static bool share_state_sending_complete(
280 struct share_states_locked share_states,
281 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000282{
Andrew Walbranca808b12020-05-15 17:22:28 +0100283 struct ffa_composite_memory_region *composite;
284 uint32_t expected_constituent_count;
285 uint32_t fragment_constituent_count_total = 0;
286 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000287
Andrew Walbranca808b12020-05-15 17:22:28 +0100288 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000289 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100290
291 /*
292 * Share state must already be valid, or it's not possible to get hold
293 * of it.
294 */
295 CHECK(share_state->memory_region != NULL &&
296 share_state->share_func != 0);
297
298 composite =
299 ffa_memory_region_get_composite(share_state->memory_region, 0);
300 expected_constituent_count = composite->constituent_count;
301 for (i = 0; i < share_state->fragment_count; ++i) {
302 fragment_constituent_count_total +=
303 share_state->fragment_constituent_counts[i];
304 }
305 dlog_verbose(
306 "Checking completion: constituent count %d/%d from %d "
307 "fragments.\n",
308 fragment_constituent_count_total, expected_constituent_count,
309 share_state->fragment_count);
310
311 return fragment_constituent_count_total == expected_constituent_count;
312}
313
314/**
315 * Calculates the offset of the next fragment expected for the given share
316 * state.
317 */
318static uint32_t share_state_next_fragment_offset(
319 struct share_states_locked share_states,
320 struct ffa_memory_share_state *share_state)
321{
322 uint32_t next_fragment_offset;
323 uint32_t i;
324
325 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000326 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100327
328 next_fragment_offset =
329 ffa_composite_constituent_offset(share_state->memory_region, 0);
330 for (i = 0; i < share_state->fragment_count; ++i) {
331 next_fragment_offset +=
332 share_state->fragment_constituent_counts[i] *
333 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334 }
335
Andrew Walbranca808b12020-05-15 17:22:28 +0100336 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337}
338
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340{
341 uint32_t i;
342
343 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
344 return;
345 }
346
Olivier Deprez935e1b12020-12-22 18:01:29 +0100347 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100348 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 "recipients [",
350 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100351 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100352 memory_region->receiver_count);
353 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 if (i != 0) {
355 dlog(", ");
356 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100357 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100358 memory_region->receivers[i].receiver_permissions.receiver,
359 memory_region->receivers[i]
360 .receiver_permissions.permissions,
361 memory_region->receivers[i]
362 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000363 }
364 dlog("]");
365}
366
367static void dump_share_states(void)
368{
369 uint32_t i;
370
371 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
372 return;
373 }
374
375 dlog("Current share states:\n");
376 sl_lock(&share_states_lock_instance);
377 for (i = 0; i < MAX_MEM_SHARES; ++i) {
378 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000381 dlog("SHARE");
382 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 dlog("LEND");
385 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000387 dlog("DONATE");
388 break;
389 default:
390 dlog("invalid share_func %#x",
391 share_states[i].share_func);
392 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100393 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 if (share_states[i].sending_complete) {
396 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000397 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100398 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000399 }
J-Alves2a0d2882020-10-29 14:49:50 +0000400 dlog(" with %d fragments, %d retrieved, "
401 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100402 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000403 share_states[i].retrieved_fragment_count[0],
404 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000405 }
406 }
407 sl_unlock(&share_states_lock_instance);
408}
409
Andrew Walbran475c1452020-02-07 13:22:22 +0000410/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100412 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000413{
414 uint32_t mode = 0;
415
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416 switch (ffa_get_data_access_attr(permissions)) {
417 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000418 mode = MM_MODE_R;
419 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100420 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000421 mode = MM_MODE_R | MM_MODE_W;
422 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100423 case FFA_DATA_ACCESS_NOT_SPECIFIED:
424 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
425 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 case FFA_DATA_ACCESS_RESERVED:
427 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100428 }
429
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 switch (ffa_get_instruction_access_attr(permissions)) {
431 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000432 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 mode |= MM_MODE_X;
435 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100436 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
437 mode |= (default_mode & MM_MODE_X);
438 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100439 case FFA_INSTRUCTION_ACCESS_RESERVED:
440 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000441 }
442
443 return mode;
444}
445
Jose Marinho75509b42019-04-09 09:34:59 +0100446/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000447 * Get the current mode in the stage-2 page table of the given vm of all the
448 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100449 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100450 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100451static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000452 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 struct ffa_memory_region_constituent **fragments,
454 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100455{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100456 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100457 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100458
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000461 * Fail if there are no constituents. Otherwise we would get an
462 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100463 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100464 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100465 }
466
Andrew Walbranca808b12020-05-15 17:22:28 +0100467 for (i = 0; i < fragment_count; ++i) {
468 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
469 ipaddr_t begin = ipa_init(fragments[i][j].address);
470 size_t size = fragments[i][j].page_count * PAGE_SIZE;
471 ipaddr_t end = ipa_add(begin, size);
472 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100473
Andrew Walbranca808b12020-05-15 17:22:28 +0100474 /* Fail if addresses are not page-aligned. */
475 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
476 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
477 return ffa_error(FFA_INVALID_PARAMETERS);
478 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100479
Andrew Walbranca808b12020-05-15 17:22:28 +0100480 /*
481 * Ensure that this constituent memory range is all
482 * mapped with the same mode.
483 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800484 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100485 return ffa_error(FFA_DENIED);
486 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100487
Andrew Walbranca808b12020-05-15 17:22:28 +0100488 /*
489 * Ensure that all constituents are mapped with the same
490 * mode.
491 */
492 if (i == 0) {
493 *orig_mode = current_mode;
494 } else if (current_mode != *orig_mode) {
495 dlog_verbose(
496 "Expected mode %#x but was %#x for %d "
497 "pages at %#x.\n",
498 *orig_mode, current_mode,
499 fragments[i][j].page_count,
500 ipa_addr(begin));
501 return ffa_error(FFA_DENIED);
502 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100503 }
Jose Marinho75509b42019-04-09 09:34:59 +0100504 }
505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000507}
508
509/**
510 * Verify that all pages have the same mode, that the starting mode
511 * constitutes a valid state and obtain the next mode to apply
512 * to the sending VM.
513 *
514 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 * 1) FFA_DENIED if a state transition was not found;
516 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100517 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100518 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100519 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
521 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000522 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100524 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100526 struct ffa_memory_region_constituent **fragments,
527 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
528 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000529{
530 const uint32_t state_mask =
531 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100532 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100533 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000534
Andrew Walbranca808b12020-05-15 17:22:28 +0100535 ret = constituents_get_mode(from, orig_from_mode, fragments,
536 fragment_constituent_counts,
537 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100538 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100539 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100540 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100541 }
542
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000543 /* Ensure the address range is normal memory and not a device. */
544 if (*orig_from_mode & MM_MODE_D) {
545 dlog_verbose("Can't share device memory (mode is %#x).\n",
546 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100547 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000548 }
549
550 /*
551 * Ensure the sender is the owner and has exclusive access to the
552 * memory.
553 */
554 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100555 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100556 }
557
J-Alves7cd5eb32020-10-16 19:06:10 +0100558 required_from_mode =
559 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
560
Andrew Walbrana65a1322020-04-06 19:32:32 +0100561 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
562 dlog_verbose(
563 "Sender tried to send memory with permissions which "
564 "required mode %#x but only had %#x itself.\n",
565 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567 }
568
569 /* Find the appropriate new mode. */
570 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000571 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100572 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100574 break;
575
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000577 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100578 break;
579
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100580 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000581 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100582 break;
583
Jose Marinho75509b42019-04-09 09:34:59 +0100584 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100586 }
587
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100588 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000589}
590
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000592 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100593 struct ffa_memory_region_constituent **fragments,
594 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
595 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000596{
597 const uint32_t state_mask =
598 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
599 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100600 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601
Andrew Walbranca808b12020-05-15 17:22:28 +0100602 ret = constituents_get_mode(from, orig_from_mode, fragments,
603 fragment_constituent_counts,
604 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100606 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000607 }
608
609 /* Ensure the address range is normal memory and not a device. */
610 if (*orig_from_mode & MM_MODE_D) {
611 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
612 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100613 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000614 }
615
616 /*
617 * Ensure the relinquishing VM is not the owner but has access to the
618 * memory.
619 */
620 orig_from_state = *orig_from_mode & state_mask;
621 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
622 dlog_verbose(
623 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100624 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100626 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000627 }
628
629 /* Find the appropriate new mode. */
630 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
631
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000633}
634
635/**
636 * Verify that all pages have the same mode, that the starting mode
637 * constitutes a valid state and obtain the next mode to apply
638 * to the retrieving VM.
639 *
640 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100641 * 1) FFA_DENIED if a state transition was not found;
642 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100643 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100645 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
647 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000648 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100649static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000650 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100651 struct ffa_memory_region_constituent **fragments,
652 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
653 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000654{
655 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100656 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000657
Andrew Walbranca808b12020-05-15 17:22:28 +0100658 ret = constituents_get_mode(to, &orig_to_mode, fragments,
659 fragment_constituent_counts,
660 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100662 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100663 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000664 }
665
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000667 const uint32_t state_mask =
668 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
669 uint32_t orig_to_state = orig_to_mode & state_mask;
670
J-Alves9256f162021-12-09 13:18:43 +0000671 /*
672 * If the original ffa memory send call has been processed
673 * successfully, it is expected the orig_to_mode would overlay
674 * with `state_mask`, as a result of the function
675 * `ffa_send_check_transition`.
676 */
677 assert(orig_to_state != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000678 } else {
679 /*
680 * Ensure the retriever has the expected state. We don't care
681 * about the MM_MODE_SHARED bit; either with or without it set
682 * are both valid representations of the !O-NA state.
683 */
684 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
685 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000687 }
688 }
689
690 /* Find the appropriate new mode. */
691 *to_mode = memory_to_attributes;
692 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000694 *to_mode |= 0;
695 break;
696
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100697 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000698 *to_mode |= MM_MODE_UNOWNED;
699 break;
700
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000702 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
703 break;
704
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100705 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000706 *to_mode |= 0;
707 break;
708
709 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100710 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000712 }
713
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100714 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100715}
Jose Marinho09b1db82019-08-08 09:16:59 +0100716
717/**
718 * Updates a VM's page table such that the given set of physical address ranges
719 * are mapped in the address space at the corresponding address ranges, in the
720 * mode provided.
721 *
722 * If commit is false, the page tables will be allocated from the mpool but no
723 * mappings will actually be updated. This function must always be called first
724 * with commit false to check that it will succeed before calling with commit
725 * true, to avoid leaving the page table in a half-updated state. To make a
726 * series of changes atomically you can call them all with commit false before
727 * calling them all with commit true.
728 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700729 * vm_ptable_defrag should always be called after a series of page table
730 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100731 *
732 * Returns true on success, or false if the update failed and no changes were
733 * made to memory mappings.
734 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100735static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000736 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100737 struct ffa_memory_region_constituent **fragments,
738 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100739 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100740{
Andrew Walbranca808b12020-05-15 17:22:28 +0100741 uint32_t i;
742 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100743
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700744 if (vm_locked.vm->el0_partition) {
745 mode |= MM_MODE_USER | MM_MODE_NG;
746 }
747
Andrew Walbranca808b12020-05-15 17:22:28 +0100748 /* Iterate over the memory region constituents within each fragment. */
749 for (i = 0; i < fragment_count; ++i) {
750 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
751 size_t size = fragments[i][j].page_count * PAGE_SIZE;
752 paddr_t pa_begin =
753 pa_from_ipa(ipa_init(fragments[i][j].address));
754 paddr_t pa_end = pa_add(pa_begin, size);
Federico Recanati4fd065d2021-12-13 20:06:23 +0100755 uint32_t pa_range = arch_mm_get_pa_range();
756
757 /*
758 * Ensure the requested region falls into system's PA
759 * range.
760 */
761 if (((pa_addr(pa_begin) >> pa_range) > 0) ||
762 ((pa_addr(pa_end) >> pa_range) > 0)) {
763 dlog_error("Region is outside of PA Range\n");
764 return false;
765 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100766
767 if (commit) {
768 vm_identity_commit(vm_locked, pa_begin, pa_end,
769 mode, ppool, NULL);
770 } else if (!vm_identity_prepare(vm_locked, pa_begin,
771 pa_end, mode, ppool)) {
772 return false;
773 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100774 }
775 }
776
777 return true;
778}
779
780/**
781 * Clears a region of physical memory by overwriting it with zeros. The data is
782 * flushed from the cache so the memory has been cleared across the system.
783 */
784static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
785{
786 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000787 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100788 * global mapping of the whole range. Such an approach will limit
789 * the changes to stage-1 tables and will allow only local
790 * invalidation.
791 */
792 bool ret;
793 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
794 void *ptr =
795 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
796 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(
Andrew Walbranca808b12020-05-15 17:22:28 +0100826 struct ffa_memory_region_constituent **fragments,
827 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
828 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100829{
830 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100831 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100832 struct mm_stage1_locked stage1_locked;
833 bool ret = false;
834
835 /*
836 * Create a local pool so any freed memory can't be used by another
837 * thread. This is to ensure each constituent that is mapped can be
838 * unmapped again afterwards.
839 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000840 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100841
Andrew Walbranca808b12020-05-15 17:22:28 +0100842 /* Iterate over the memory region constituents within each fragment. */
843 for (i = 0; i < fragment_count; ++i) {
844 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100845
Andrew Walbranca808b12020-05-15 17:22:28 +0100846 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
847 size_t size = fragments[i][j].page_count * PAGE_SIZE;
848 paddr_t begin =
849 pa_from_ipa(ipa_init(fragments[i][j].address));
850 paddr_t end = pa_add(begin, size);
851
852 if (!clear_memory(begin, end, &local_page_pool)) {
853 /*
854 * api_clear_memory will defrag on failure, so
855 * no need to do it here.
856 */
857 goto out;
858 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 }
860 }
861
862 /*
863 * Need to defrag after clearing, as it may have added extra mappings to
864 * the stage 1 page table.
865 */
866 stage1_locked = mm_lock_stage1();
867 mm_defrag(stage1_locked, &local_page_pool);
868 mm_unlock_stage1(&stage1_locked);
869
870 ret = true;
871
872out:
873 mpool_fini(&local_page_pool);
874 return ret;
875}
876
877/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000878 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000880 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 *
882 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000883 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100885 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100886 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
887 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100888 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100889 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100891 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100892static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000893 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100894 struct ffa_memory_region_constituent **fragments,
895 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
896 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100897 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100898{
Andrew Walbranca808b12020-05-15 17:22:28 +0100899 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100900 uint32_t orig_from_mode;
901 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100904
905 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100906 * Make sure constituents are properly aligned to a 64-bit boundary. If
907 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100908 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100909 for (i = 0; i < fragment_count; ++i) {
910 if (!is_aligned(fragments[i], 8)) {
911 dlog_verbose("Constituents not aligned.\n");
912 return ffa_error(FFA_INVALID_PARAMETERS);
913 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100914 }
915
916 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000917 * Check if the state transition is lawful for the sender, ensure that
918 * all constituents of a memory region being shared are at the same
919 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100920 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100922 &orig_from_mode, fragments,
923 fragment_constituent_counts,
924 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100926 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100927 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100928 }
929
Andrew Walbran37c574e2020-06-03 11:45:46 +0100930 if (orig_from_mode_ret != NULL) {
931 *orig_from_mode_ret = orig_from_mode;
932 }
933
Jose Marinho09b1db82019-08-08 09:16:59 +0100934 /*
935 * Create a local pool so any freed memory can't be used by another
936 * thread. This is to ensure the original mapping can be restored if the
937 * clear fails.
938 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000939 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100940
941 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000942 * First reserve all required memory for the new page table entries
943 * without committing, to make sure the entire operation will succeed
944 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100945 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100946 if (!ffa_region_group_identity_map(
947 from_locked, fragments, fragment_constituent_counts,
948 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100949 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100951 goto out;
952 }
953
954 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000955 * Update the mapping for the sender. This won't allocate because the
956 * transaction was already prepared above, but may free pages in the
957 * case that a whole block is being unmapped that was previously
958 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100959 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100960 CHECK(ffa_region_group_identity_map(
961 from_locked, fragments, fragment_constituent_counts,
962 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100963
964 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100966 fragments, fragment_constituent_counts,
967 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100968 /*
969 * On failure, roll back by returning memory to the sender. This
970 * may allocate pages which were previously freed into
971 * `local_page_pool` by the call above, but will never allocate
972 * more pages than that so can never fail.
973 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100975 from_locked, fragments, fragment_constituent_counts,
976 fragment_count, orig_from_mode, &local_page_pool,
977 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100978
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100979 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100980 goto out;
981 }
982
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100983 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000984
985out:
986 mpool_fini(&local_page_pool);
987
988 /*
989 * Tidy up the page table by reclaiming failed mappings (if there was an
990 * error) or merging entries into blocks where possible (on success).
991 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700992 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993
994 return ret;
995}
996
997/**
998 * Validates and maps memory shared from one VM to another.
999 *
1000 * This function requires the calling context to hold the <to> lock.
1001 *
1002 * Returns:
1003 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001006 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001010static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001012 struct ffa_memory_region_constituent **fragments,
1013 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1014 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1015 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001016{
Andrew Walbranca808b12020-05-15 17:22:28 +01001017 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 uint32_t to_mode;
1019 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021
1022 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001023 * Make sure constituents are properly aligned to a 64-bit boundary. If
1024 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001025 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001026 for (i = 0; i < fragment_count; ++i) {
1027 if (!is_aligned(fragments[i], 8)) {
1028 return ffa_error(FFA_INVALID_PARAMETERS);
1029 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 }
1031
1032 /*
1033 * Check if the state transition is lawful for the recipient, and ensure
1034 * that all constituents of the memory region being retrieved are at the
1035 * same state.
1036 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001037 ret = ffa_retrieve_check_transition(
1038 to_locked, share_func, fragments, fragment_constituent_counts,
1039 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001041 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001042 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 }
1044
1045 /*
1046 * Create a local pool so any freed memory can't be used by another
1047 * thread. This is to ensure the original mapping can be restored if the
1048 * clear fails.
1049 */
1050 mpool_init_with_fallback(&local_page_pool, page_pool);
1051
1052 /*
1053 * First reserve all required memory for the new page table entries in
1054 * the recipient page tables without committing, to make sure the entire
1055 * operation will succeed without exhausting the page pool.
1056 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001057 if (!ffa_region_group_identity_map(
1058 to_locked, fragments, fragment_constituent_counts,
1059 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001060 /* TODO: partial defrag of failed range. */
1061 dlog_verbose(
1062 "Insufficient memory to update recipient page "
1063 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001064 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001065 goto out;
1066 }
1067
1068 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001069 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001070 fragments, fragment_constituent_counts,
1071 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001073 goto out;
1074 }
1075
Jose Marinho09b1db82019-08-08 09:16:59 +01001076 /*
1077 * Complete the transfer by mapping the memory into the recipient. This
1078 * won't allocate because the transaction was already prepared above, so
1079 * it doesn't need to use the `local_page_pool`.
1080 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001081 CHECK(ffa_region_group_identity_map(
1082 to_locked, fragments, fragment_constituent_counts,
1083 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001084
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001086
1087out:
1088 mpool_fini(&local_page_pool);
1089
1090 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001091 * Tidy up the page table by reclaiming failed mappings (if there was an
1092 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001093 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001094 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001095
1096 return ret;
1097}
1098
Andrew Walbran290b0c92020-02-03 16:37:14 +00001099/**
1100 * Reclaims the given memory from the TEE. To do this space is first reserved in
1101 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1102 * then (if that is successful) the memory is mapped back into the <to> VM's
1103 * page table.
1104 *
1105 * This function requires the calling context to hold the <to> lock.
1106 *
1107 * Returns:
1108 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001109 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001110 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001112 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001113 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001114 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001115static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 struct vm_locked to_locked, ffa_memory_handle_t handle,
1117 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001118 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1119 struct mpool *page_pool)
1120{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001121 uint32_t to_mode;
1122 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 struct ffa_value ret;
1124 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001125
1126 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001127 * Make sure constituents are properly aligned to a 64-bit boundary. If
1128 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001129 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001130 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001131 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001132 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001133 }
1134
1135 /*
1136 * Check if the state transition is lawful for the recipient, and ensure
1137 * that all constituents of the memory region being retrieved are at the
1138 * same state.
1139 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001140 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001141 &constituents, &constituent_count,
1142 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001143 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001144 dlog_verbose("Invalid transition.\n");
1145 return ret;
1146 }
1147
1148 /*
1149 * Create a local pool so any freed memory can't be used by another
1150 * thread. This is to ensure the original mapping can be restored if the
1151 * clear fails.
1152 */
1153 mpool_init_with_fallback(&local_page_pool, page_pool);
1154
1155 /*
1156 * First reserve all required memory for the new page table entries in
1157 * the recipient page tables without committing, to make sure the entire
1158 * operation will succeed without exhausting the page pool.
1159 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001160 if (!ffa_region_group_identity_map(to_locked, &constituents,
1161 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001163 /* TODO: partial defrag of failed range. */
1164 dlog_verbose(
1165 "Insufficient memory to update recipient page "
1166 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001167 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001168 goto out;
1169 }
1170
1171 /*
1172 * Forward the request to the TEE and see what happens.
1173 */
1174 tee_flags = 0;
1175 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001176 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001177 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001178 ret = arch_other_world_call(
1179 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1180 .arg1 = (uint32_t)handle,
1181 .arg2 = (uint32_t)(handle >> 32),
1182 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001183
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001185 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001186 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1187 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001188 ret.func, ret.arg2);
1189 goto out;
1190 }
1191
1192 /*
1193 * The TEE was happy with it, so complete the reclaim by mapping the
1194 * memory into the recipient. This won't allocate because the
1195 * transaction was already prepared above, so it doesn't need to use the
1196 * `local_page_pool`.
1197 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001198 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1199 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001200 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001201
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001202 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001203
1204out:
1205 mpool_fini(&local_page_pool);
1206
1207 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001208 * Tidy up the page table by reclaiming failed mappings (if there was an
1209 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001210 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001211 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001212
1213 return ret;
1214}
1215
Andrew Walbran996d1d12020-05-27 14:08:43 +01001216static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001217 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001218 struct ffa_memory_region_constituent **fragments,
1219 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1220 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001221{
1222 uint32_t orig_from_mode;
1223 uint32_t from_mode;
1224 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001225 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001226
Andrew Walbranca808b12020-05-15 17:22:28 +01001227 ret = ffa_relinquish_check_transition(
1228 from_locked, &orig_from_mode, fragments,
1229 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001230 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001231 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001232 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001233 }
1234
1235 /*
1236 * Create a local pool so any freed memory can't be used by another
1237 * thread. This is to ensure the original mapping can be restored if the
1238 * clear fails.
1239 */
1240 mpool_init_with_fallback(&local_page_pool, page_pool);
1241
1242 /*
1243 * First reserve all required memory for the new page table entries
1244 * without committing, to make sure the entire operation will succeed
1245 * without exhausting the page pool.
1246 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001247 if (!ffa_region_group_identity_map(
1248 from_locked, fragments, fragment_constituent_counts,
1249 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001250 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001251 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252 goto out;
1253 }
1254
1255 /*
1256 * Update the mapping for the sender. This won't allocate because the
1257 * transaction was already prepared above, but may free pages in the
1258 * case that a whole block is being unmapped that was previously
1259 * partially mapped.
1260 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 CHECK(ffa_region_group_identity_map(
1262 from_locked, fragments, fragment_constituent_counts,
1263 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001264
1265 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001266 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001267 fragments, fragment_constituent_counts,
1268 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001269 /*
1270 * On failure, roll back by returning memory to the sender. This
1271 * may allocate pages which were previously freed into
1272 * `local_page_pool` by the call above, but will never allocate
1273 * more pages than that so can never fail.
1274 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001275 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001276 from_locked, fragments, fragment_constituent_counts,
1277 fragment_count, orig_from_mode, &local_page_pool,
1278 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001279
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001280 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001281 goto out;
1282 }
1283
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001284 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001285
1286out:
1287 mpool_fini(&local_page_pool);
1288
1289 /*
1290 * Tidy up the page table by reclaiming failed mappings (if there was an
1291 * error) or merging entries into blocks where possible (on success).
1292 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001293 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001294
1295 return ret;
1296}
1297
1298/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001299 * Complete a memory sending operation by checking that it is valid, updating
1300 * the sender page table, and then either marking the share state as having
1301 * completed sending (on success) or freeing it (on failure).
1302 *
1303 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1304 */
1305static struct ffa_value ffa_memory_send_complete(
1306 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001307 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1308 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001309{
1310 struct ffa_memory_region *memory_region = share_state->memory_region;
1311 struct ffa_value ret;
1312
1313 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001314 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001315
1316 /* Check that state is valid in sender page table and update. */
1317 ret = ffa_send_check_update(
1318 from_locked, share_state->fragments,
1319 share_state->fragment_constituent_counts,
1320 share_state->fragment_count, share_state->share_func,
1321 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001322 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1323 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001324 if (ret.func != FFA_SUCCESS_32) {
1325 /*
1326 * Free share state, it failed to send so it can't be retrieved.
1327 */
1328 dlog_verbose("Complete failed, freeing share state.\n");
1329 share_state_free(share_states, share_state, page_pool);
1330 return ret;
1331 }
1332
1333 share_state->sending_complete = true;
1334 dlog_verbose("Marked sending complete.\n");
1335
J-Alvesee68c542020-10-29 17:48:20 +00001336 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001337}
1338
1339/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001340 * Check that the given `memory_region` represents a valid memory send request
1341 * of the given `share_func` type, return the clear flag and permissions via the
1342 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001343 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001344 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001345 * not.
1346 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001347static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001348 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1349 uint32_t memory_share_length, uint32_t fragment_length,
1350 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001351{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001352 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001353 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001354 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001355 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356 enum ffa_data_access data_access;
1357 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001358
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001359 assert(permissions != NULL);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001360
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001361 /*
1362 * This should already be checked by the caller, just making the
1363 * assumption clear here.
1364 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001365 assert(memory_region->receiver_count == 1);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001366
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367 /* The sender must match the message sender. */
1368 if (memory_region->sender != from_locked.vm->id) {
1369 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001370 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001371 }
1372
Andrew Walbrana65a1322020-04-06 19:32:32 +01001373 /*
1374 * Ensure that the composite header is within the memory bounds and
1375 * doesn't overlap the first part of the message.
1376 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001377 receivers_length = sizeof(struct ffa_memory_access) *
1378 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001379 constituents_offset =
1380 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001382 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001383 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001385 "Invalid composite memory region descriptor offset "
1386 "%d.\n",
1387 memory_region->receivers[0]
1388 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 }
1391
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001392 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001393
1394 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001395 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001397 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1398 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001399 if (memory_share_length != constituents_offset + constituents_length) {
1400 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001401 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 memory_region->receivers[0]
1403 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001405 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001406 if (fragment_length < memory_share_length &&
1407 fragment_length < HF_MAILBOX_SIZE) {
1408 dlog_warning(
1409 "Initial fragment length %d smaller than mailbox "
1410 "size.\n",
1411 fragment_length);
1412 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001413
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 /*
1415 * Clear is not allowed for memory sharing, as the sender still has
1416 * access to the memory.
1417 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001418 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1419 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001420 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 }
1423
1424 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001426 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 }
1429
1430 /* Check that the permissions are valid. */
1431 *permissions =
1432 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 data_access = ffa_get_data_access_attr(*permissions);
1434 instruction_access = ffa_get_instruction_access_attr(*permissions);
1435 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1436 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1438 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001440 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001442 dlog_verbose(
1443 "Invalid instruction access permissions %#x for "
1444 "sending memory.\n",
1445 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001448 if (share_func == FFA_MEM_SHARE_32) {
1449 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001450 dlog_verbose(
1451 "Invalid data access permissions %#x for "
1452 "sharing memory.\n",
1453 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001455 }
1456 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001457 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1458 * required for share operations (but must not be specified by
1459 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001460 * returned to the retriever.
1461 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 ffa_set_instruction_access_attr(permissions,
1463 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001464 memory_region->receivers[0].receiver_permissions.permissions =
1465 *permissions;
1466 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 if (share_func == FFA_MEM_LEND_32 &&
1468 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001469 dlog_verbose(
1470 "Invalid data access permissions %#x for lending "
1471 "memory.\n",
1472 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474 }
Federico Recanati85090c42021-12-15 13:17:54 +01001475
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 if (share_func == FFA_MEM_DONATE_32 &&
1477 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001478 dlog_verbose(
1479 "Invalid data access permissions %#x for donating "
1480 "memory.\n",
1481 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001483 }
1484
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001485 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486}
1487
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001488/** Forwards a memory send message on to the TEE. */
1489static struct ffa_value memory_send_tee_forward(
1490 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1491 uint32_t share_func, struct ffa_memory_region *memory_region,
1492 uint32_t memory_share_length, uint32_t fragment_length)
1493{
1494 struct ffa_value ret;
1495
1496 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1497 memory_region, fragment_length);
1498 tee_locked.vm->mailbox.recv_size = fragment_length;
1499 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1500 tee_locked.vm->mailbox.recv_func = share_func;
1501 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001502 ret = arch_other_world_call(
1503 (struct ffa_value){.func = share_func,
1504 .arg1 = memory_share_length,
1505 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001506 /*
1507 * After the call to the TEE completes it must have finished reading its
1508 * RX buffer, so it is ready for another message.
1509 */
1510 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1511
1512 return ret;
1513}
1514
Andrew Walbrana65a1322020-04-06 19:32:32 +01001515/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001516 * Gets the share state for continuing an operation to donate, lend or share
1517 * memory, and checks that it is a valid request.
1518 *
1519 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1520 * not.
1521 */
1522static struct ffa_value ffa_memory_send_continue_validate(
1523 struct share_states_locked share_states, ffa_memory_handle_t handle,
1524 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1525 struct mpool *page_pool)
1526{
1527 struct ffa_memory_share_state *share_state;
1528 struct ffa_memory_region *memory_region;
1529
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001530 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001531
1532 /*
1533 * Look up the share state by handle and make sure that the VM ID
1534 * matches.
1535 */
1536 if (!get_share_state(share_states, handle, &share_state)) {
1537 dlog_verbose(
1538 "Invalid handle %#x for memory send continuation.\n",
1539 handle);
1540 return ffa_error(FFA_INVALID_PARAMETERS);
1541 }
1542 memory_region = share_state->memory_region;
1543
1544 if (memory_region->sender != from_vm_id) {
1545 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1546 return ffa_error(FFA_INVALID_PARAMETERS);
1547 }
1548
1549 if (share_state->sending_complete) {
1550 dlog_verbose(
1551 "Sending of memory handle %#x is already complete.\n",
1552 handle);
1553 return ffa_error(FFA_INVALID_PARAMETERS);
1554 }
1555
1556 if (share_state->fragment_count == MAX_FRAGMENTS) {
1557 /*
1558 * Log a warning as this is a sign that MAX_FRAGMENTS should
1559 * probably be increased.
1560 */
1561 dlog_warning(
1562 "Too many fragments for memory share with handle %#x; "
1563 "only %d supported.\n",
1564 handle, MAX_FRAGMENTS);
1565 /* Free share state, as it's not possible to complete it. */
1566 share_state_free(share_states, share_state, page_pool);
1567 return ffa_error(FFA_NO_MEMORY);
1568 }
1569
1570 *share_state_ret = share_state;
1571
1572 return (struct ffa_value){.func = FFA_SUCCESS_32};
1573}
1574
1575/**
1576 * Forwards a memory send continuation message on to the TEE.
1577 */
1578static struct ffa_value memory_send_continue_tee_forward(
1579 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1580 uint32_t fragment_length, ffa_memory_handle_t handle)
1581{
1582 struct ffa_value ret;
1583
1584 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1585 fragment_length);
1586 tee_locked.vm->mailbox.recv_size = fragment_length;
1587 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1588 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1589 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001590 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001591 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1592 .arg1 = (uint32_t)handle,
1593 .arg2 = (uint32_t)(handle >> 32),
1594 .arg3 = fragment_length,
1595 .arg4 = (uint64_t)sender_vm_id << 16});
1596 /*
1597 * After the call to the TEE completes it must have finished reading its
1598 * RX buffer, so it is ready for another message.
1599 */
1600 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1601
1602 return ret;
1603}
1604
1605/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001606 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1607 * updates the stage-2 page tables. Specifically, check if the message length
1608 * and number of memory region constituents match, and if the transition is
1609 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001610 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001611 * Assumes that the caller has already found and locked the sender VM and copied
1612 * the memory region descriptor from the sender's TX buffer to a freshly
1613 * allocated page from Hafnium's internal pool. The caller must have also
1614 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001615 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001616 * This function takes ownership of the `memory_region` passed in and will free
1617 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001618 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001619struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001621 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001622 uint32_t fragment_length, uint32_t share_func,
1623 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001624{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001625 ffa_memory_access_permissions_t permissions;
1626 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001627 struct share_states_locked share_states;
1628 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001629
1630 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001631 * If there is an error validating the `memory_region` then we need to
1632 * free it because we own it but we won't be storing it in a share state
1633 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001634 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001635 ret = ffa_memory_send_validate(from_locked, memory_region,
1636 memory_share_length, fragment_length,
1637 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001638 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001639 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001640 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001641 }
1642
Andrew Walbrana65a1322020-04-06 19:32:32 +01001643 /* Set flag for share function, ready to be retrieved later. */
1644 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001645 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001646 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001648 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001649 case FFA_MEM_LEND_32:
1650 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001651 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001653 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001654 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001655 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001656 }
1657
Andrew Walbranca808b12020-05-15 17:22:28 +01001658 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001659 /*
1660 * Allocate a share state before updating the page table. Otherwise if
1661 * updating the page table succeeded but allocating the share state
1662 * failed then it would leave the memory in a state where nobody could
1663 * get it back.
1664 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001665 if (!allocate_share_state(share_states, share_func, memory_region,
1666 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1667 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001668 dlog_verbose("Failed to allocate share state.\n");
1669 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001670 ret = ffa_error(FFA_NO_MEMORY);
1671 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001672 }
1673
Andrew Walbranca808b12020-05-15 17:22:28 +01001674 if (fragment_length == memory_share_length) {
1675 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001676 ret = ffa_memory_send_complete(
1677 from_locked, share_states, share_state, page_pool,
1678 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001679 } else {
1680 ret = (struct ffa_value){
1681 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001682 .arg1 = (uint32_t)memory_region->handle,
1683 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001684 .arg3 = fragment_length};
1685 }
1686
1687out:
1688 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001689 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001690 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001691}
1692
1693/**
1694 * Validates a call to donate, lend or share memory to the TEE and then updates
1695 * the stage-2 page tables. Specifically, check if the message length and number
1696 * of memory region constituents match, and if the transition is valid for the
1697 * type of memory sending operation.
1698 *
1699 * Assumes that the caller has already found and locked the sender VM and the
1700 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1701 * to a freshly allocated page from Hafnium's internal pool. The caller must
1702 * have also validated that the receiver VM ID is valid.
1703 *
1704 * This function takes ownership of the `memory_region` passed in and will free
1705 * it when necessary; it must not be freed by the caller.
1706 */
1707struct ffa_value ffa_memory_tee_send(
1708 struct vm_locked from_locked, struct vm_locked to_locked,
1709 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1710 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1711{
1712 ffa_memory_access_permissions_t permissions;
1713 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001714
1715 /*
1716 * If there is an error validating the `memory_region` then we need to
1717 * free it because we own it but we won't be storing it in a share state
1718 * after all.
1719 */
1720 ret = ffa_memory_send_validate(from_locked, memory_region,
1721 memory_share_length, fragment_length,
1722 share_func, &permissions);
1723 if (ret.func != FFA_SUCCESS_32) {
1724 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001725 }
1726
Andrew Walbranca808b12020-05-15 17:22:28 +01001727 if (fragment_length == memory_share_length) {
1728 /* No more fragments to come, everything fit in one message. */
1729 struct ffa_composite_memory_region *composite =
1730 ffa_memory_region_get_composite(memory_region, 0);
1731 struct ffa_memory_region_constituent *constituents =
1732 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001733 struct mpool local_page_pool;
1734 uint32_t orig_from_mode;
1735
1736 /*
1737 * Use a local page pool so that we can roll back if necessary.
1738 */
1739 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001740
1741 ret = ffa_send_check_update(
1742 from_locked, &constituents,
1743 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001744 permissions, &local_page_pool,
1745 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1746 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001747 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001748 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001749 goto out;
1750 }
1751
1752 /* Forward memory send message on to TEE. */
1753 ret = memory_send_tee_forward(
1754 to_locked, from_locked.vm->id, share_func,
1755 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001756
1757 if (ret.func != FFA_SUCCESS_32) {
1758 dlog_verbose(
1759 "TEE didn't successfully complete memory send "
1760 "operation; returned %#x (%d). Rolling back.\n",
1761 ret.func, ret.arg2);
1762
1763 /*
1764 * The TEE failed to complete the send operation, so
1765 * roll back the page table update for the VM. This
1766 * can't fail because it won't try to allocate more
1767 * memory than was freed into the `local_page_pool` by
1768 * `ffa_send_check_update` in the initial update.
1769 */
1770 CHECK(ffa_region_group_identity_map(
1771 from_locked, &constituents,
1772 &composite->constituent_count, 1,
1773 orig_from_mode, &local_page_pool, true));
1774 }
1775
1776 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001777 } else {
1778 struct share_states_locked share_states = share_states_lock();
1779 ffa_memory_handle_t handle;
1780
1781 /*
1782 * We need to wait for the rest of the fragments before we can
1783 * check whether the transaction is valid and unmap the memory.
1784 * Call the TEE so it can do its initial validation and assign a
1785 * handle, and allocate a share state to keep what we have so
1786 * far.
1787 */
1788 ret = memory_send_tee_forward(
1789 to_locked, from_locked.vm->id, share_func,
1790 memory_region, memory_share_length, fragment_length);
1791 if (ret.func == FFA_ERROR_32) {
1792 goto out_unlock;
1793 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1794 dlog_warning(
1795 "Got %#x from TEE in response to %#x for "
1796 "fragment with with %d/%d, expected "
1797 "FFA_MEM_FRAG_RX.\n",
1798 ret.func, share_func, fragment_length,
1799 memory_share_length);
1800 ret = ffa_error(FFA_INVALID_PARAMETERS);
1801 goto out_unlock;
1802 }
1803 handle = ffa_frag_handle(ret);
1804 if (ret.arg3 != fragment_length) {
1805 dlog_warning(
1806 "Got unexpected fragment offset %d for "
1807 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1808 ret.arg3, fragment_length);
1809 ret = ffa_error(FFA_INVALID_PARAMETERS);
1810 goto out_unlock;
1811 }
1812 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1813 dlog_warning(
1814 "Got unexpected sender ID %d for "
1815 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1816 ffa_frag_sender(ret), from_locked.vm->id);
1817 ret = ffa_error(FFA_INVALID_PARAMETERS);
1818 goto out_unlock;
1819 }
1820
1821 if (!allocate_share_state(share_states, share_func,
1822 memory_region, fragment_length,
1823 handle, NULL)) {
1824 dlog_verbose("Failed to allocate share state.\n");
1825 ret = ffa_error(FFA_NO_MEMORY);
1826 goto out_unlock;
1827 }
1828 /*
1829 * Don't free the memory region fragment, as it has been stored
1830 * in the share state.
1831 */
1832 memory_region = NULL;
1833 out_unlock:
1834 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001835 }
1836
Andrew Walbranca808b12020-05-15 17:22:28 +01001837out:
1838 if (memory_region != NULL) {
1839 mpool_free(page_pool, memory_region);
1840 }
1841 dump_share_states();
1842 return ret;
1843}
1844
1845/**
1846 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1847 * this is the last fragment then checks that the transition is valid for the
1848 * type of memory sending operation and updates the stage-2 page tables of the
1849 * sender.
1850 *
1851 * Assumes that the caller has already found and locked the sender VM and copied
1852 * the memory region descriptor from the sender's TX buffer to a freshly
1853 * allocated page from Hafnium's internal pool.
1854 *
1855 * This function takes ownership of the `fragment` passed in; it must not be
1856 * freed by the caller.
1857 */
1858struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1859 void *fragment,
1860 uint32_t fragment_length,
1861 ffa_memory_handle_t handle,
1862 struct mpool *page_pool)
1863{
1864 struct share_states_locked share_states = share_states_lock();
1865 struct ffa_memory_share_state *share_state;
1866 struct ffa_value ret;
1867 struct ffa_memory_region *memory_region;
1868
1869 ret = ffa_memory_send_continue_validate(share_states, handle,
1870 &share_state,
1871 from_locked.vm->id, page_pool);
1872 if (ret.func != FFA_SUCCESS_32) {
1873 goto out_free_fragment;
1874 }
1875 memory_region = share_state->memory_region;
1876
1877 if (memory_region->receivers[0].receiver_permissions.receiver ==
1878 HF_TEE_VM_ID) {
1879 dlog_error(
1880 "Got hypervisor-allocated handle for memory send to "
1881 "TEE. This should never happen, and indicates a bug in "
1882 "EL3 code.\n");
1883 ret = ffa_error(FFA_INVALID_PARAMETERS);
1884 goto out_free_fragment;
1885 }
1886
1887 /* Add this fragment. */
1888 share_state->fragments[share_state->fragment_count] = fragment;
1889 share_state->fragment_constituent_counts[share_state->fragment_count] =
1890 fragment_length / sizeof(struct ffa_memory_region_constituent);
1891 share_state->fragment_count++;
1892
1893 /* Check whether the memory send operation is now ready to complete. */
1894 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001895 ret = ffa_memory_send_complete(
1896 from_locked, share_states, share_state, page_pool,
1897 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001898 } else {
1899 ret = (struct ffa_value){
1900 .func = FFA_MEM_FRAG_RX_32,
1901 .arg1 = (uint32_t)handle,
1902 .arg2 = (uint32_t)(handle >> 32),
1903 .arg3 = share_state_next_fragment_offset(share_states,
1904 share_state)};
1905 }
1906 goto out;
1907
1908out_free_fragment:
1909 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001910
1911out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001912 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001913 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001914}
1915
Andrew Walbranca808b12020-05-15 17:22:28 +01001916/**
1917 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1918 * is the last fragment then checks that the transition is valid for the type of
1919 * memory sending operation and updates the stage-2 page tables of the sender.
1920 *
1921 * Assumes that the caller has already found and locked the sender VM and copied
1922 * the memory region descriptor from the sender's TX buffer to a freshly
1923 * allocated page from Hafnium's internal pool.
1924 *
1925 * This function takes ownership of the `memory_region` passed in and will free
1926 * it when necessary; it must not be freed by the caller.
1927 */
1928struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1929 struct vm_locked to_locked,
1930 void *fragment,
1931 uint32_t fragment_length,
1932 ffa_memory_handle_t handle,
1933 struct mpool *page_pool)
1934{
1935 struct share_states_locked share_states = share_states_lock();
1936 struct ffa_memory_share_state *share_state;
1937 struct ffa_value ret;
1938 struct ffa_memory_region *memory_region;
1939
1940 ret = ffa_memory_send_continue_validate(share_states, handle,
1941 &share_state,
1942 from_locked.vm->id, page_pool);
1943 if (ret.func != FFA_SUCCESS_32) {
1944 goto out_free_fragment;
1945 }
1946 memory_region = share_state->memory_region;
1947
1948 if (memory_region->receivers[0].receiver_permissions.receiver !=
1949 HF_TEE_VM_ID) {
1950 dlog_error(
1951 "Got SPM-allocated handle for memory send to non-TEE "
1952 "VM. This should never happen, and indicates a bug.\n");
1953 ret = ffa_error(FFA_INVALID_PARAMETERS);
1954 goto out_free_fragment;
1955 }
1956
1957 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1958 to_locked.vm->mailbox.recv == NULL) {
1959 /*
1960 * If the TEE RX buffer is not available, tell the sender to
1961 * retry by returning the current offset again.
1962 */
1963 ret = (struct ffa_value){
1964 .func = FFA_MEM_FRAG_RX_32,
1965 .arg1 = (uint32_t)handle,
1966 .arg2 = (uint32_t)(handle >> 32),
1967 .arg3 = share_state_next_fragment_offset(share_states,
1968 share_state),
1969 };
1970 goto out_free_fragment;
1971 }
1972
1973 /* Add this fragment. */
1974 share_state->fragments[share_state->fragment_count] = fragment;
1975 share_state->fragment_constituent_counts[share_state->fragment_count] =
1976 fragment_length / sizeof(struct ffa_memory_region_constituent);
1977 share_state->fragment_count++;
1978
1979 /* Check whether the memory send operation is now ready to complete. */
1980 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001981 struct mpool local_page_pool;
1982 uint32_t orig_from_mode;
1983
1984 /*
1985 * Use a local page pool so that we can roll back if necessary.
1986 */
1987 mpool_init_with_fallback(&local_page_pool, page_pool);
1988
Andrew Walbranca808b12020-05-15 17:22:28 +01001989 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001990 share_state, &local_page_pool,
1991 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001992
1993 if (ret.func == FFA_SUCCESS_32) {
1994 /*
1995 * Forward final fragment on to the TEE so that
1996 * it can complete the memory sending operation.
1997 */
1998 ret = memory_send_continue_tee_forward(
1999 to_locked, from_locked.vm->id, fragment,
2000 fragment_length, handle);
2001
2002 if (ret.func != FFA_SUCCESS_32) {
2003 /*
2004 * The error will be passed on to the caller,
2005 * but log it here too.
2006 */
2007 dlog_verbose(
2008 "TEE didn't successfully complete "
2009 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002010 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002011 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002012
2013 /*
2014 * The TEE failed to complete the send
2015 * operation, so roll back the page table update
2016 * for the VM. This can't fail because it won't
2017 * try to allocate more memory than was freed
2018 * into the `local_page_pool` by
2019 * `ffa_send_check_update` in the initial
2020 * update.
2021 */
2022 CHECK(ffa_region_group_identity_map(
2023 from_locked, share_state->fragments,
2024 share_state
2025 ->fragment_constituent_counts,
2026 share_state->fragment_count,
2027 orig_from_mode, &local_page_pool,
2028 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002029 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002030
Andrew Walbranca808b12020-05-15 17:22:28 +01002031 /* Free share state. */
2032 share_state_free(share_states, share_state, page_pool);
2033 } else {
2034 /* Abort sending to TEE. */
2035 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002036 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002037 .func = FFA_MEM_RECLAIM_32,
2038 .arg1 = (uint32_t)handle,
2039 .arg2 = (uint32_t)(handle >> 32)});
2040
2041 if (tee_ret.func != FFA_SUCCESS_32) {
2042 /*
2043 * Nothing we can do if TEE doesn't abort
2044 * properly, just log it.
2045 */
2046 dlog_verbose(
2047 "TEE didn't successfully abort failed "
2048 "memory send operation; returned %#x "
2049 "(%d).\n",
2050 tee_ret.func, tee_ret.arg2);
2051 }
2052 /*
2053 * We don't need to free the share state in this case
2054 * because ffa_memory_send_complete does that already.
2055 */
2056 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002057
2058 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002059 } else {
2060 uint32_t next_fragment_offset =
2061 share_state_next_fragment_offset(share_states,
2062 share_state);
2063
2064 ret = memory_send_continue_tee_forward(
2065 to_locked, from_locked.vm->id, fragment,
2066 fragment_length, handle);
2067
2068 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2069 ffa_frag_handle(ret) != handle ||
2070 ret.arg3 != next_fragment_offset ||
2071 ffa_frag_sender(ret) != from_locked.vm->id) {
2072 dlog_verbose(
2073 "Got unexpected result from forwarding "
2074 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2075 "offset %d, sender %d); expected "
2076 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2077 "sender %d).\n",
2078 ret.func, ffa_frag_handle(ret), ret.arg3,
2079 ffa_frag_sender(ret), handle,
2080 next_fragment_offset, from_locked.vm->id);
2081 /* Free share state. */
2082 share_state_free(share_states, share_state, page_pool);
2083 ret = ffa_error(FFA_INVALID_PARAMETERS);
2084 goto out;
2085 }
2086
2087 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2088 .arg1 = (uint32_t)handle,
2089 .arg2 = (uint32_t)(handle >> 32),
2090 .arg3 = next_fragment_offset};
2091 }
2092 goto out;
2093
2094out_free_fragment:
2095 mpool_free(page_pool, fragment);
2096
2097out:
2098 share_states_unlock(&share_states);
2099 return ret;
2100}
2101
2102/** Clean up after the receiver has finished retrieving a memory region. */
2103static void ffa_memory_retrieve_complete(
2104 struct share_states_locked share_states,
2105 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2106{
2107 if (share_state->share_func == FFA_MEM_DONATE_32) {
2108 /*
2109 * Memory that has been donated can't be relinquished,
2110 * so no need to keep the share state around.
2111 */
2112 share_state_free(share_states, share_state, page_pool);
2113 dlog_verbose("Freed share state for donate.\n");
2114 }
2115}
2116
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002117struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2118 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002119 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002122 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002123 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002124 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125 sizeof(struct ffa_memory_access);
2126 ffa_memory_handle_t handle = retrieve_request->handle;
2127 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002128 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002129 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2130 struct ffa_memory_region *memory_region;
2131 ffa_memory_access_permissions_t sent_permissions;
2132 enum ffa_data_access sent_data_access;
2133 enum ffa_instruction_access sent_instruction_access;
2134 ffa_memory_access_permissions_t requested_permissions;
2135 enum ffa_data_access requested_data_access;
2136 enum ffa_instruction_access requested_instruction_access;
2137 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002138 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002139 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002140 struct ffa_memory_share_state *share_state;
2141 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002142 struct ffa_composite_memory_region *composite;
2143 uint32_t total_length;
2144 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002145
2146 dump_share_states();
2147
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002148 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002149 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002151 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002152 expected_retrieve_request_length,
2153 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002154 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002155 }
2156
Andrew Walbrana65a1322020-04-06 19:32:32 +01002157 if (retrieve_request->receiver_count != 1) {
2158 dlog_verbose(
2159 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002160 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002161 "expected 1).\n",
2162 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002163 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002164 }
2165
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002166 share_states = share_states_lock();
2167 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002170 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002171 goto out;
2172 }
2173
Andrew Walbrana65a1322020-04-06 19:32:32 +01002174 memory_region = share_state->memory_region;
2175 CHECK(memory_region != NULL);
2176
2177 /*
2178 * Check that the transaction type expected by the receiver is correct,
2179 * if it has been specified.
2180 */
2181 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002182 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002183 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002184 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002185 dlog_verbose(
2186 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002187 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002188 transaction_type,
2189 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002190 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002191 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002192 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002193 goto out;
2194 }
2195
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002196 if (retrieve_request->sender != memory_region->sender) {
2197 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002198 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002199 "expected %d for handle %#x.\n",
2200 retrieve_request->sender, memory_region->sender,
2201 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002202 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002203 goto out;
2204 }
2205
2206 if (retrieve_request->tag != memory_region->tag) {
2207 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002208 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002209 "%d for handle %#x.\n",
2210 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002211 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002212 goto out;
2213 }
2214
Andrew Walbrana65a1322020-04-06 19:32:32 +01002215 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2216 to_locked.vm->id) {
2217 dlog_verbose(
2218 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002219 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002220 retrieve_request->receivers[0]
2221 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002222 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002223 goto out;
2224 }
2225
2226 if (memory_region->receivers[0].receiver_permissions.receiver !=
2227 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002228 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002229 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2230 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002231 to_locked.vm->id,
2232 memory_region->receivers[0]
2233 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002234 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002235 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002236 goto out;
2237 }
2238
Andrew Walbranca808b12020-05-15 17:22:28 +01002239 if (!share_state->sending_complete) {
2240 dlog_verbose(
2241 "Memory with handle %#x not fully sent, can't "
2242 "retrieve.\n",
2243 handle);
2244 ret = ffa_error(FFA_INVALID_PARAMETERS);
2245 goto out;
2246 }
2247
2248 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002249 dlog_verbose("Memory with handle %#x already retrieved.\n",
2250 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002251 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002252 goto out;
2253 }
2254
Andrew Walbrana65a1322020-04-06 19:32:32 +01002255 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2256 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002257 dlog_verbose(
2258 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002259 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002260 retrieve_request->receivers[0]
2261 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002263 goto out;
2264 }
2265
Federico Recanati85090c42021-12-15 13:17:54 +01002266 if ((retrieve_request->flags &
2267 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0) {
2268 dlog_verbose(
2269 "Retriever specified 'address range alignment hint'"
2270 " not supported.\n");
2271 ret = ffa_error(FFA_INVALID_PARAMETERS);
2272 goto out;
2273 }
2274 if ((retrieve_request->flags &
2275 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2276 dlog_verbose(
2277 "Bits 8-5 must be zero in memory region's flags "
2278 "(address range alignment hint not supported).\n");
2279 ret = ffa_error(FFA_INVALID_PARAMETERS);
2280 goto out;
2281 }
2282
J-Alves84658fc2021-06-17 14:37:32 +01002283 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2284 dlog_verbose(
2285 "Bits 31-10 must be zero in memory region's flags.\n");
2286 ret = ffa_error(FFA_INVALID_PARAMETERS);
2287 goto out;
2288 }
2289
2290 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2291 (retrieve_request->flags &
2292 (FFA_MEMORY_REGION_FLAG_CLEAR |
2293 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2294 dlog_verbose(
2295 "Memory Share operation can't clean after relinquish "
2296 "memory region.\n");
2297 ret = ffa_error(FFA_INVALID_PARAMETERS);
2298 goto out;
2299 }
2300
Andrew Walbrana65a1322020-04-06 19:32:32 +01002301 /*
J-Alves17c069c2021-12-07 16:00:38 +00002302 * If the borrower needs the memory to be cleared before mapping to its
2303 * address space, the sender should have set the flag when calling
2304 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2305 */
2306 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2307 (share_state->memory_region->flags &
2308 FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2309 dlog_verbose(
2310 "Borrower needs memory cleared. Sender needs to set "
2311 "flag for clearing memory.\n");
2312 ret = ffa_error(FFA_DENIED);
2313 goto out;
2314 }
2315
2316 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002317 * Check permissions from sender against permissions requested by
2318 * receiver.
2319 */
2320 /* TODO: Check attributes too. */
2321 sent_permissions =
2322 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002323 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002324 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002325 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002326 requested_permissions =
2327 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002328 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002329 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002330 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002331 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002332
2333 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2334 requested_permissions == FFA_DATA_ACCESS_RO) &&
2335 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2336 dlog_verbose(
2337 "Receiver has RO permissions can not request clear.\n");
2338 ret = ffa_error(FFA_DENIED);
2339 goto out;
2340 }
2341
Andrew Walbrana65a1322020-04-06 19:32:32 +01002342 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002343 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2344 case FFA_DATA_ACCESS_RW:
2345 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2346 requested_data_access == FFA_DATA_ACCESS_RW) {
2347 ffa_set_data_access_attr(&permissions,
2348 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002349 break;
2350 }
2351 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002352 case FFA_DATA_ACCESS_RO:
2353 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2354 requested_data_access == FFA_DATA_ACCESS_RO) {
2355 ffa_set_data_access_attr(&permissions,
2356 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002357 break;
2358 }
2359 dlog_verbose(
2360 "Invalid data access requested; sender specified "
2361 "permissions %#x but receiver requested %#x.\n",
2362 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002363 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002364 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002365 case FFA_DATA_ACCESS_RESERVED:
2366 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002367 "checked before this point.");
2368 }
2369 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002370 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2371 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002372 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002373 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2374 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2375 ffa_set_instruction_access_attr(
2376 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002377 break;
2378 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002379 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002380 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002381 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2382 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2383 ffa_set_instruction_access_attr(
2384 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002385 break;
2386 }
2387 dlog_verbose(
2388 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002389 "specified permissions %#x but receiver requested "
2390 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002391 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002392 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002393 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002394 case FFA_INSTRUCTION_ACCESS_RESERVED:
2395 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002396 "be checked before this point.");
2397 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002398 memory_to_attributes = ffa_memory_permissions_to_mode(
2399 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002400 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002401 to_locked, share_state->fragments,
2402 share_state->fragment_constituent_counts,
2403 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002404 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002405 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002406 goto out;
2407 }
2408
2409 /*
2410 * Copy response to RX buffer of caller and deliver the message. This
2411 * must be done before the share_state is (possibly) freed.
2412 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002413 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002414 composite = ffa_memory_region_get_composite(memory_region, 0);
2415 /*
2416 * Constituents which we received in the first fragment should always
2417 * fit in the first fragment we are sending, because the header is the
2418 * same size in both cases and we have a fixed message buffer size. So
2419 * `ffa_retrieved_memory_region_init` should never fail.
2420 */
2421 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002422 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2423 memory_region->sender, memory_region->attributes,
2424 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002425 composite->page_count, composite->constituent_count,
2426 share_state->fragments[0],
2427 share_state->fragment_constituent_counts[0], &total_length,
2428 &fragment_length));
2429 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002430 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002431 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002432 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2433
Andrew Walbranca808b12020-05-15 17:22:28 +01002434 share_state->retrieved_fragment_count[0] = 1;
2435 if (share_state->retrieved_fragment_count[0] ==
2436 share_state->fragment_count) {
2437 ffa_memory_retrieve_complete(share_states, share_state,
2438 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002439 }
2440
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002441 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002442 .arg1 = total_length,
2443 .arg2 = fragment_length};
2444
2445out:
2446 share_states_unlock(&share_states);
2447 dump_share_states();
2448 return ret;
2449}
2450
2451struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2452 ffa_memory_handle_t handle,
2453 uint32_t fragment_offset,
2454 struct mpool *page_pool)
2455{
2456 struct ffa_memory_region *memory_region;
2457 struct share_states_locked share_states;
2458 struct ffa_memory_share_state *share_state;
2459 struct ffa_value ret;
2460 uint32_t fragment_index;
2461 uint32_t retrieved_constituents_count;
2462 uint32_t i;
2463 uint32_t expected_fragment_offset;
2464 uint32_t remaining_constituent_count;
2465 uint32_t fragment_length;
2466
2467 dump_share_states();
2468
2469 share_states = share_states_lock();
2470 if (!get_share_state(share_states, handle, &share_state)) {
2471 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2472 handle);
2473 ret = ffa_error(FFA_INVALID_PARAMETERS);
2474 goto out;
2475 }
2476
2477 memory_region = share_state->memory_region;
2478 CHECK(memory_region != NULL);
2479
2480 if (memory_region->receivers[0].receiver_permissions.receiver !=
2481 to_locked.vm->id) {
2482 dlog_verbose(
2483 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2484 "of handle %#x.\n",
2485 to_locked.vm->id,
2486 memory_region->receivers[0]
2487 .receiver_permissions.receiver,
2488 handle);
2489 ret = ffa_error(FFA_INVALID_PARAMETERS);
2490 goto out;
2491 }
2492
2493 if (!share_state->sending_complete) {
2494 dlog_verbose(
2495 "Memory with handle %#x not fully sent, can't "
2496 "retrieve.\n",
2497 handle);
2498 ret = ffa_error(FFA_INVALID_PARAMETERS);
2499 goto out;
2500 }
2501
2502 if (share_state->retrieved_fragment_count[0] == 0 ||
2503 share_state->retrieved_fragment_count[0] >=
2504 share_state->fragment_count) {
2505 dlog_verbose(
2506 "Retrieval of memory with handle %#x not yet started "
2507 "or already completed (%d/%d fragments retrieved).\n",
2508 handle, share_state->retrieved_fragment_count[0],
2509 share_state->fragment_count);
2510 ret = ffa_error(FFA_INVALID_PARAMETERS);
2511 goto out;
2512 }
2513
2514 fragment_index = share_state->retrieved_fragment_count[0];
2515
2516 /*
2517 * Check that the given fragment offset is correct by counting how many
2518 * constituents were in the fragments previously sent.
2519 */
2520 retrieved_constituents_count = 0;
2521 for (i = 0; i < fragment_index; ++i) {
2522 retrieved_constituents_count +=
2523 share_state->fragment_constituent_counts[i];
2524 }
2525 expected_fragment_offset =
2526 ffa_composite_constituent_offset(memory_region, 0) +
2527 retrieved_constituents_count *
2528 sizeof(struct ffa_memory_region_constituent);
2529 if (fragment_offset != expected_fragment_offset) {
2530 dlog_verbose("Fragment offset was %d but expected %d.\n",
2531 fragment_offset, expected_fragment_offset);
2532 ret = ffa_error(FFA_INVALID_PARAMETERS);
2533 goto out;
2534 }
2535
2536 remaining_constituent_count = ffa_memory_fragment_init(
2537 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2538 share_state->fragments[fragment_index],
2539 share_state->fragment_constituent_counts[fragment_index],
2540 &fragment_length);
2541 CHECK(remaining_constituent_count == 0);
2542 to_locked.vm->mailbox.recv_size = fragment_length;
2543 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2544 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2545 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2546 share_state->retrieved_fragment_count[0]++;
2547 if (share_state->retrieved_fragment_count[0] ==
2548 share_state->fragment_count) {
2549 ffa_memory_retrieve_complete(share_states, share_state,
2550 page_pool);
2551 }
2552
2553 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2554 .arg1 = (uint32_t)handle,
2555 .arg2 = (uint32_t)(handle >> 32),
2556 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557
2558out:
2559 share_states_unlock(&share_states);
2560 dump_share_states();
2561 return ret;
2562}
2563
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002564struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002566 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002567{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002568 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002569 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002570 struct ffa_memory_share_state *share_state;
2571 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002573 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002574
Andrew Walbrana65a1322020-04-06 19:32:32 +01002575 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002576 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002577 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002578 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002579 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581 }
2582
Andrew Walbrana65a1322020-04-06 19:32:32 +01002583 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002584 dlog_verbose(
2585 "VM ID %d in relinquish message doesn't match calling "
2586 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002587 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002588 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002589 }
2590
2591 dump_share_states();
2592
2593 share_states = share_states_lock();
2594 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002595 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002596 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002597 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002598 goto out;
2599 }
2600
Andrew Walbranca808b12020-05-15 17:22:28 +01002601 if (!share_state->sending_complete) {
2602 dlog_verbose(
2603 "Memory with handle %#x not fully sent, can't "
2604 "relinquish.\n",
2605 handle);
2606 ret = ffa_error(FFA_INVALID_PARAMETERS);
2607 goto out;
2608 }
2609
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002610 memory_region = share_state->memory_region;
2611 CHECK(memory_region != NULL);
2612
Andrew Walbrana65a1322020-04-06 19:32:32 +01002613 if (memory_region->receivers[0].receiver_permissions.receiver !=
2614 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002615 dlog_verbose(
2616 "VM ID %d tried to relinquish memory region with "
2617 "handle %#x but receiver was %d.\n",
2618 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002619 memory_region->receivers[0]
2620 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002621 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002622 goto out;
2623 }
2624
Andrew Walbranca808b12020-05-15 17:22:28 +01002625 if (share_state->retrieved_fragment_count[0] !=
2626 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002627 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002628 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002629 "relinquish.\n",
2630 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002631 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002632 goto out;
2633 }
2634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636
2637 /*
2638 * Clear is not allowed for memory that was shared, as the original
2639 * sender still has access to the memory.
2640 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002641 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002642 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002643 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002644 goto out;
2645 }
2646
Andrew Walbranca808b12020-05-15 17:22:28 +01002647 ret = ffa_relinquish_check_update(
2648 from_locked, share_state->fragments,
2649 share_state->fragment_constituent_counts,
2650 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002652 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002653 /*
2654 * Mark memory handle as not retrieved, so it can be reclaimed
2655 * (or retrieved again).
2656 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002657 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002658 }
2659
2660out:
2661 share_states_unlock(&share_states);
2662 dump_share_states();
2663 return ret;
2664}
2665
2666/**
2667 * Validates that the reclaim transition is allowed for the given handle,
2668 * updates the page table of the reclaiming VM, and frees the internal state
2669 * associated with the handle.
2670 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002671struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002672 ffa_memory_handle_t handle,
2673 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002674 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002675{
2676 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002677 struct ffa_memory_share_state *share_state;
2678 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002679 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002680
2681 dump_share_states();
2682
2683 share_states = share_states_lock();
2684 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002685 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002687 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688 goto out;
2689 }
2690
2691 memory_region = share_state->memory_region;
2692 CHECK(memory_region != NULL);
2693
2694 if (to_locked.vm->id != memory_region->sender) {
2695 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002696 "VM %#x attempted to reclaim memory handle %#x "
2697 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002698 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002699 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002700 goto out;
2701 }
2702
Andrew Walbranca808b12020-05-15 17:22:28 +01002703 if (!share_state->sending_complete) {
2704 dlog_verbose(
2705 "Memory with handle %#x not fully sent, can't "
2706 "reclaim.\n",
2707 handle);
2708 ret = ffa_error(FFA_INVALID_PARAMETERS);
2709 goto out;
2710 }
2711
2712 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002713 dlog_verbose(
2714 "Tried to reclaim memory handle %#x that has not been "
2715 "relinquished.\n",
2716 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002717 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002718 goto out;
2719 }
2720
Andrew Walbranca808b12020-05-15 17:22:28 +01002721 ret = ffa_retrieve_check_update(
2722 to_locked, share_state->fragments,
2723 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002724 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002725 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002726
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002727 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728 share_state_free(share_states, share_state, page_pool);
2729 dlog_verbose("Freed share state after successful reclaim.\n");
2730 }
2731
2732out:
2733 share_states_unlock(&share_states);
2734 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002735}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002736
2737/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002738 * Validates that the reclaim transition is allowed for the memory region with
2739 * the given handle which was previously shared with the TEE, tells the TEE to
2740 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2741 *
2742 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002743 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002744struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002745 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002746 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002747 ffa_memory_region_flags_t flags,
2748 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002749{
Andrew Walbranca808b12020-05-15 17:22:28 +01002750 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2751 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2752 struct ffa_value tee_ret;
2753 uint32_t length;
2754 uint32_t fragment_length;
2755 uint32_t fragment_offset;
2756 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002757 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002758 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2759
2760 CHECK(request_length <= HF_MAILBOX_SIZE);
2761 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2762
2763 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002764 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002765 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2766 .arg1 = request_length,
2767 .arg2 = request_length});
2768 if (tee_ret.func == FFA_ERROR_32) {
2769 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2770 return tee_ret;
2771 }
2772 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2773 dlog_verbose(
2774 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2775 tee_ret.func);
2776 return ffa_error(FFA_INVALID_PARAMETERS);
2777 }
2778
2779 length = tee_ret.arg1;
2780 fragment_length = tee_ret.arg2;
2781
2782 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2783 length > sizeof(tee_retrieve_buffer)) {
2784 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2785 fragment_length, length, HF_MAILBOX_SIZE,
2786 sizeof(tee_retrieve_buffer));
2787 return ffa_error(FFA_INVALID_PARAMETERS);
2788 }
2789
2790 /*
2791 * Copy the first fragment of the memory region descriptor to an
2792 * internal buffer.
2793 */
2794 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2795 from_locked.vm->mailbox.send, fragment_length);
2796
2797 /* Fetch the remaining fragments into the same buffer. */
2798 fragment_offset = fragment_length;
2799 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002800 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002801 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2802 .arg1 = (uint32_t)handle,
2803 .arg2 = (uint32_t)(handle >> 32),
2804 .arg3 = fragment_offset});
2805 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2806 dlog_verbose(
2807 "Got %#x (%d) from TEE in response to "
2808 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2809 tee_ret.func, tee_ret.arg2);
2810 return tee_ret;
2811 }
2812 if (ffa_frag_handle(tee_ret) != handle) {
2813 dlog_verbose(
2814 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2815 "in response to FFA_MEM_FRAG_RX for handle "
2816 "%#x.\n",
2817 ffa_frag_handle(tee_ret), handle);
2818 return ffa_error(FFA_INVALID_PARAMETERS);
2819 }
2820 if (ffa_frag_sender(tee_ret) != 0) {
2821 dlog_verbose(
2822 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2823 "(expected 0).\n",
2824 ffa_frag_sender(tee_ret));
2825 return ffa_error(FFA_INVALID_PARAMETERS);
2826 }
2827 fragment_length = tee_ret.arg3;
2828 if (fragment_length > HF_MAILBOX_SIZE ||
2829 fragment_offset + fragment_length > length) {
2830 dlog_verbose(
2831 "Invalid fragment length %d at offset %d (max "
2832 "%d).\n",
2833 fragment_length, fragment_offset,
2834 HF_MAILBOX_SIZE);
2835 return ffa_error(FFA_INVALID_PARAMETERS);
2836 }
2837 memcpy_s(tee_retrieve_buffer + fragment_offset,
2838 sizeof(tee_retrieve_buffer) - fragment_offset,
2839 from_locked.vm->mailbox.send, fragment_length);
2840
2841 fragment_offset += fragment_length;
2842 }
2843
2844 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002845
2846 if (memory_region->receiver_count != 1) {
2847 /* Only one receiver supported by Hafnium for now. */
2848 dlog_verbose(
2849 "Multiple recipients not supported (got %d, expected "
2850 "1).\n",
2851 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002852 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002853 }
2854
2855 if (memory_region->handle != handle) {
2856 dlog_verbose(
2857 "Got memory region handle %#x from TEE but requested "
2858 "handle %#x.\n",
2859 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002860 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002861 }
2862
2863 /* The original sender must match the caller. */
2864 if (to_locked.vm->id != memory_region->sender) {
2865 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002866 "VM %#x attempted to reclaim memory handle %#x "
2867 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002868 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002869 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002870 }
2871
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002872 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002873
2874 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002875 * Validate that the reclaim transition is allowed for the given memory
2876 * region, forward the request to the TEE and then map the memory back
2877 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002878 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002879 return ffa_tee_reclaim_check_update(
2880 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002881 composite->constituent_count, memory_to_attributes,
2882 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002883}