blob: b62bc4e7f7b92fb2d99f2c04160511a09229ce48 [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
Olivier Deprez112d2b52020-09-30 07:39:23 +020011#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020012#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000013
Jose Marinho75509b42019-04-09 09:34:59 +010014#include "hf/api.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010015#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000018#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000020#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000022/** The maximum number of recipients a memory region may be sent to. */
23#define MAX_MEM_SHARE_RECIPIENTS 1
24
25/**
26 * The maximum number of memory sharing handles which may be active at once. A
27 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
28 * or LEND handle is active from when it is sent to when it is reclaimed.
29 */
30#define MAX_MEM_SHARES 100
31
Andrew Walbranca808b12020-05-15 17:22:28 +010032/**
33 * The maximum number of fragments into which a memory sharing message may be
34 * broken.
35 */
36#define MAX_FRAGMENTS 20
37
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
39 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000040 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010041static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
42 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000043 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010044static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010045 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010046static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
47 "struct ffa_memory_access must be a multiple of 16 bytes long.");
48static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
49 "struct ffa_memory_region must be a multiple of 16 bytes long.");
50static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
51 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000052 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000053
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010054struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000055 /**
56 * The memory region being shared, or NULL if this share state is
57 * unallocated.
58 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010059 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000060
Andrew Walbranca808b12020-05-15 17:22:28 +010061 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
62
63 /** The number of constituents in each fragment. */
64 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
65
66 /**
67 * The number of valid elements in the `fragments` and
68 * `fragment_constituent_counts` arrays.
69 */
70 uint32_t fragment_count;
71
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000072 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010073 * The FF-A function used for sharing the memory. Must be one of
74 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000075 * share state is allocated, or 0.
76 */
77 uint32_t share_func;
78
79 /**
J-Alves2a0d2882020-10-29 14:49:50 +000080 * The sender's original mode before invoking the FF-A function for
81 * sharing the memory.
82 * This is used to reset the original configuration when sender invokes
83 * FFA_MEM_RECLAIM_32.
84 */
85 uint32_t sender_orig_mode;
86
87 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010088 * True if all the fragments of this sharing request have been sent and
89 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090 */
Andrew Walbranca808b12020-05-15 17:22:28 +010091 bool sending_complete;
92
93 /**
94 * How many fragments of the memory region each recipient has retrieved
95 * so far. The order of this array matches the order of the endpoint
96 * memory access descriptors in the memory region descriptor. Any
97 * entries beyond the receiver_count will always be 0.
98 */
99 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000100};
101
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000102/**
103 * Encapsulates the set of share states while the `share_states_lock` is held.
104 */
105struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100106 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000107};
108
109/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100110 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000111 * by this lock.
112 */
113static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100114static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000115
116/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100117 * Buffer for retrieving memory region information from the TEE for when a
118 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
119 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000120 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100121alignas(PAGE_SIZE) static uint8_t
122 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
123
124/**
J-Alves917d2f22020-10-30 18:39:30 +0000125 * Extracts the index from a memory handle allocated by Hafnium's current world.
126 */
127uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
128{
129 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
130}
131
132/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100133 * Initialises the next available `struct ffa_memory_share_state` and sets
134 * `share_state_ret` to a pointer to it. If `handle` is
135 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
136 * uses the provided handle which is assumed to be globally unique.
137 *
138 * Returns true on success or false if none are available.
139 */
140static bool allocate_share_state(
141 struct share_states_locked share_states, uint32_t share_func,
142 struct ffa_memory_region *memory_region, uint32_t fragment_length,
143 ffa_memory_handle_t handle,
144 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000145{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100146 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000147
Andrew Walbranca808b12020-05-15 17:22:28 +0100148 CHECK(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149 CHECK(memory_region != NULL);
150
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100152 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100154 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100155 &share_states.share_states[i];
156 struct ffa_composite_memory_region *composite =
157 ffa_memory_region_get_composite(memory_region,
158 0);
159
160 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000161 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200162 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000164 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000166 allocated_state->share_func = share_func;
167 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 allocated_state->fragment_count = 1;
169 allocated_state->fragments[0] = composite->constituents;
170 allocated_state->fragment_constituent_counts[0] =
171 (fragment_length -
172 ffa_composite_constituent_offset(memory_region,
173 0)) /
174 sizeof(struct ffa_memory_region_constituent);
175 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000176 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 allocated_state->retrieved_fragment_count[j] =
178 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000179 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100180 if (share_state_ret != NULL) {
181 *share_state_ret = allocated_state;
182 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183 return true;
184 }
185 }
186
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000187 return false;
188}
189
190/** Locks the share states lock. */
191struct share_states_locked share_states_lock(void)
192{
193 sl_lock(&share_states_lock_instance);
194
195 return (struct share_states_locked){.share_states = share_states};
196}
197
198/** Unlocks the share states lock. */
199static void share_states_unlock(struct share_states_locked *share_states)
200{
201 CHECK(share_states->share_states != NULL);
202 share_states->share_states = NULL;
203 sl_unlock(&share_states_lock_instance);
204}
205
206/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100207 * If the given handle is a valid handle for an allocated share state then
208 * initialises `share_state_ret` to point to the share state and returns true.
209 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210 */
211static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100212 ffa_memory_handle_t handle,
213 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000214{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100215 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000216 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000217
Andrew Walbranca808b12020-05-15 17:22:28 +0100218 CHECK(share_states.share_states != NULL);
219 CHECK(share_state_ret != NULL);
220
221 /*
222 * First look for a share_state allocated by us, in which case the
223 * handle is based on the index.
224 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200225 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000226 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100227 if (index < MAX_MEM_SHARES) {
228 share_state = &share_states.share_states[index];
229 if (share_state->share_func != 0) {
230 *share_state_ret = share_state;
231 return true;
232 }
233 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000234 }
235
Andrew Walbranca808b12020-05-15 17:22:28 +0100236 /* Fall back to a linear scan. */
237 for (index = 0; index < MAX_MEM_SHARES; ++index) {
238 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000239 if (share_state->memory_region != NULL &&
240 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100241 share_state->share_func != 0) {
242 *share_state_ret = share_state;
243 return true;
244 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000245 }
246
Andrew Walbranca808b12020-05-15 17:22:28 +0100247 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000248}
249
250/** Marks a share state as unallocated. */
251static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100252 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000253 struct mpool *page_pool)
254{
Andrew Walbranca808b12020-05-15 17:22:28 +0100255 uint32_t i;
256
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000257 CHECK(share_states.share_states != NULL);
258 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100259 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100261 /*
262 * First fragment is part of the same page as the `memory_region`, so it
263 * doesn't need to be freed separately.
264 */
265 share_state->fragments[0] = NULL;
266 share_state->fragment_constituent_counts[0] = 0;
267 for (i = 1; i < share_state->fragment_count; ++i) {
268 mpool_free(page_pool, share_state->fragments[i]);
269 share_state->fragments[i] = NULL;
270 share_state->fragment_constituent_counts[i] = 0;
271 }
272 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273 share_state->memory_region = NULL;
274}
275
Andrew Walbranca808b12020-05-15 17:22:28 +0100276/** Checks whether the given share state has been fully sent. */
277static bool share_state_sending_complete(
278 struct share_states_locked share_states,
279 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000280{
Andrew Walbranca808b12020-05-15 17:22:28 +0100281 struct ffa_composite_memory_region *composite;
282 uint32_t expected_constituent_count;
283 uint32_t fragment_constituent_count_total = 0;
284 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000285
Andrew Walbranca808b12020-05-15 17:22:28 +0100286 /* Lock must be held. */
287 CHECK(share_states.share_states != NULL);
288
289 /*
290 * Share state must already be valid, or it's not possible to get hold
291 * of it.
292 */
293 CHECK(share_state->memory_region != NULL &&
294 share_state->share_func != 0);
295
296 composite =
297 ffa_memory_region_get_composite(share_state->memory_region, 0);
298 expected_constituent_count = composite->constituent_count;
299 for (i = 0; i < share_state->fragment_count; ++i) {
300 fragment_constituent_count_total +=
301 share_state->fragment_constituent_counts[i];
302 }
303 dlog_verbose(
304 "Checking completion: constituent count %d/%d from %d "
305 "fragments.\n",
306 fragment_constituent_count_total, expected_constituent_count,
307 share_state->fragment_count);
308
309 return fragment_constituent_count_total == expected_constituent_count;
310}
311
312/**
313 * Calculates the offset of the next fragment expected for the given share
314 * state.
315 */
316static uint32_t share_state_next_fragment_offset(
317 struct share_states_locked share_states,
318 struct ffa_memory_share_state *share_state)
319{
320 uint32_t next_fragment_offset;
321 uint32_t i;
322
323 /* Lock must be held. */
324 CHECK(share_states.share_states != NULL);
325
326 next_fragment_offset =
327 ffa_composite_constituent_offset(share_state->memory_region, 0);
328 for (i = 0; i < share_state->fragment_count; ++i) {
329 next_fragment_offset +=
330 share_state->fragment_constituent_counts[i] *
331 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000332 }
333
Andrew Walbranca808b12020-05-15 17:22:28 +0100334 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335}
336
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338{
339 uint32_t i;
340
341 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
342 return;
343 }
344
Olivier Deprez935e1b12020-12-22 18:01:29 +0100345 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100346 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100347 "recipients [",
348 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100349 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100350 memory_region->receiver_count);
351 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000352 if (i != 0) {
353 dlog(", ");
354 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100355 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100356 memory_region->receivers[i].receiver_permissions.receiver,
357 memory_region->receivers[i]
358 .receiver_permissions.permissions,
359 memory_region->receivers[i]
360 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000361 }
362 dlog("]");
363}
364
365static void dump_share_states(void)
366{
367 uint32_t i;
368
369 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
370 return;
371 }
372
373 dlog("Current share states:\n");
374 sl_lock(&share_states_lock_instance);
375 for (i = 0; i < MAX_MEM_SHARES; ++i) {
376 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000377 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100378 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 dlog("SHARE");
380 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000382 dlog("LEND");
383 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100384 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000385 dlog("DONATE");
386 break;
387 default:
388 dlog("invalid share_func %#x",
389 share_states[i].share_func);
390 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100391 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000392 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100393 if (share_states[i].sending_complete) {
394 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000395 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100396 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000397 }
J-Alves2a0d2882020-10-29 14:49:50 +0000398 dlog(" with %d fragments, %d retrieved, "
399 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100400 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000401 share_states[i].retrieved_fragment_count[0],
402 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000403 }
404 }
405 sl_unlock(&share_states_lock_instance);
406}
407
Andrew Walbran475c1452020-02-07 13:22:22 +0000408/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100409static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100410 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000411{
412 uint32_t mode = 0;
413
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 switch (ffa_get_data_access_attr(permissions)) {
415 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000416 mode = MM_MODE_R;
417 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100418 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000419 mode = MM_MODE_R | MM_MODE_W;
420 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100421 case FFA_DATA_ACCESS_NOT_SPECIFIED:
422 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
423 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100424 case FFA_DATA_ACCESS_RESERVED:
425 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100426 }
427
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428 switch (ffa_get_instruction_access_attr(permissions)) {
429 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000430 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100431 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100432 mode |= MM_MODE_X;
433 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100434 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
435 mode |= (default_mode & MM_MODE_X);
436 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100437 case FFA_INSTRUCTION_ACCESS_RESERVED:
438 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000439 }
440
441 return mode;
442}
443
Jose Marinho75509b42019-04-09 09:34:59 +0100444/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000445 * Get the current mode in the stage-2 page table of the given vm of all the
446 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100447 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100448 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100449static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000450 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100451 struct ffa_memory_region_constituent **fragments,
452 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100453{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100454 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100455 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100456
Andrew Walbranca808b12020-05-15 17:22:28 +0100457 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100458 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000459 * Fail if there are no constituents. Otherwise we would get an
460 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100461 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100463 }
464
Andrew Walbranca808b12020-05-15 17:22:28 +0100465 for (i = 0; i < fragment_count; ++i) {
466 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
467 ipaddr_t begin = ipa_init(fragments[i][j].address);
468 size_t size = fragments[i][j].page_count * PAGE_SIZE;
469 ipaddr_t end = ipa_add(begin, size);
470 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100471
Andrew Walbranca808b12020-05-15 17:22:28 +0100472 /* Fail if addresses are not page-aligned. */
473 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
474 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
475 return ffa_error(FFA_INVALID_PARAMETERS);
476 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100477
Andrew Walbranca808b12020-05-15 17:22:28 +0100478 /*
479 * Ensure that this constituent memory range is all
480 * mapped with the same mode.
481 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800482 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100483 return ffa_error(FFA_DENIED);
484 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100485
Andrew Walbranca808b12020-05-15 17:22:28 +0100486 /*
487 * Ensure that all constituents are mapped with the same
488 * mode.
489 */
490 if (i == 0) {
491 *orig_mode = current_mode;
492 } else if (current_mode != *orig_mode) {
493 dlog_verbose(
494 "Expected mode %#x but was %#x for %d "
495 "pages at %#x.\n",
496 *orig_mode, current_mode,
497 fragments[i][j].page_count,
498 ipa_addr(begin));
499 return ffa_error(FFA_DENIED);
500 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100501 }
Jose Marinho75509b42019-04-09 09:34:59 +0100502 }
503
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100504 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000505}
506
507/**
508 * Verify that all pages have the same mode, that the starting mode
509 * constitutes a valid state and obtain the next mode to apply
510 * to the sending VM.
511 *
512 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513 * 1) FFA_DENIED if a state transition was not found;
514 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100515 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100516 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100517 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100518 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
519 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000520 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100522 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100524 struct ffa_memory_region_constituent **fragments,
525 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
526 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000527{
528 const uint32_t state_mask =
529 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100530 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100531 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000532
Andrew Walbranca808b12020-05-15 17:22:28 +0100533 ret = constituents_get_mode(from, orig_from_mode, fragments,
534 fragment_constituent_counts,
535 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100537 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100538 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100539 }
540
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000541 /* Ensure the address range is normal memory and not a device. */
542 if (*orig_from_mode & MM_MODE_D) {
543 dlog_verbose("Can't share device memory (mode is %#x).\n",
544 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100545 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000546 }
547
548 /*
549 * Ensure the sender is the owner and has exclusive access to the
550 * memory.
551 */
552 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100553 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100554 }
555
J-Alves7cd5eb32020-10-16 19:06:10 +0100556 required_from_mode =
557 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
558
Andrew Walbrana65a1322020-04-06 19:32:32 +0100559 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
560 dlog_verbose(
561 "Sender tried to send memory with permissions which "
562 "required mode %#x but only had %#x itself.\n",
563 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100564 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000565 }
566
567 /* Find the appropriate new mode. */
568 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000569 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100570 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000571 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100572 break;
573
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100574 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000575 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100576 break;
577
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000579 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100580 break;
581
Jose Marinho75509b42019-04-09 09:34:59 +0100582 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100584 }
585
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100586 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000587}
588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000590 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100591 struct ffa_memory_region_constituent **fragments,
592 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
593 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000594{
595 const uint32_t state_mask =
596 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
597 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100598 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000599
Andrew Walbranca808b12020-05-15 17:22:28 +0100600 ret = constituents_get_mode(from, orig_from_mode, fragments,
601 fragment_constituent_counts,
602 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100604 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000605 }
606
607 /* Ensure the address range is normal memory and not a device. */
608 if (*orig_from_mode & MM_MODE_D) {
609 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
610 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100611 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000612 }
613
614 /*
615 * Ensure the relinquishing VM is not the owner but has access to the
616 * memory.
617 */
618 orig_from_state = *orig_from_mode & state_mask;
619 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
620 dlog_verbose(
621 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100622 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000623 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 }
626
627 /* Find the appropriate new mode. */
628 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
629
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100630 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000631}
632
633/**
634 * Verify that all pages have the same mode, that the starting mode
635 * constitutes a valid state and obtain the next mode to apply
636 * to the retrieving VM.
637 *
638 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100639 * 1) FFA_DENIED if a state transition was not found;
640 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100641 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100642 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100643 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
645 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000646 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100647static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000648 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100649 struct ffa_memory_region_constituent **fragments,
650 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
651 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000652{
653 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100654 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000655
Andrew Walbranca808b12020-05-15 17:22:28 +0100656 ret = constituents_get_mode(to, &orig_to_mode, fragments,
657 fragment_constituent_counts,
658 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100659 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100660 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100661 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662 }
663
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100664 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000665 const uint32_t state_mask =
666 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
667 uint32_t orig_to_state = orig_to_mode & state_mask;
668
669 if (orig_to_state != MM_MODE_INVALID &&
670 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000672 }
673 } else {
674 /*
675 * Ensure the retriever has the expected state. We don't care
676 * about the MM_MODE_SHARED bit; either with or without it set
677 * are both valid representations of the !O-NA state.
678 */
679 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
680 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000682 }
683 }
684
685 /* Find the appropriate new mode. */
686 *to_mode = memory_to_attributes;
687 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000689 *to_mode |= 0;
690 break;
691
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100692 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000693 *to_mode |= MM_MODE_UNOWNED;
694 break;
695
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000697 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
698 break;
699
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100700 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000701 *to_mode |= 0;
702 break;
703
704 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100705 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100706 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000707 }
708
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100709 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100710}
Jose Marinho09b1db82019-08-08 09:16:59 +0100711
712/**
713 * Updates a VM's page table such that the given set of physical address ranges
714 * are mapped in the address space at the corresponding address ranges, in the
715 * mode provided.
716 *
717 * If commit is false, the page tables will be allocated from the mpool but no
718 * mappings will actually be updated. This function must always be called first
719 * with commit false to check that it will succeed before calling with commit
720 * true, to avoid leaving the page table in a half-updated state. To make a
721 * series of changes atomically you can call them all with commit false before
722 * calling them all with commit true.
723 *
724 * mm_vm_defrag should always be called after a series of page table updates,
725 * whether they succeed or fail.
726 *
727 * Returns true on success, or false if the update failed and no changes were
728 * made to memory mappings.
729 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100730static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000731 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100732 struct ffa_memory_region_constituent **fragments,
733 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
734 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100735{
Andrew Walbranca808b12020-05-15 17:22:28 +0100736 uint32_t i;
737 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100738
Andrew Walbranca808b12020-05-15 17:22:28 +0100739 /* Iterate over the memory region constituents within each fragment. */
740 for (i = 0; i < fragment_count; ++i) {
741 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
742 size_t size = fragments[i][j].page_count * PAGE_SIZE;
743 paddr_t pa_begin =
744 pa_from_ipa(ipa_init(fragments[i][j].address));
745 paddr_t pa_end = pa_add(pa_begin, size);
746
747 if (commit) {
748 vm_identity_commit(vm_locked, pa_begin, pa_end,
749 mode, ppool, NULL);
750 } else if (!vm_identity_prepare(vm_locked, pa_begin,
751 pa_end, mode, ppool)) {
752 return false;
753 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100754 }
755 }
756
757 return true;
758}
759
760/**
761 * Clears a region of physical memory by overwriting it with zeros. The data is
762 * flushed from the cache so the memory has been cleared across the system.
763 */
764static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
765{
766 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000767 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100768 * global mapping of the whole range. Such an approach will limit
769 * the changes to stage-1 tables and will allow only local
770 * invalidation.
771 */
772 bool ret;
773 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
774 void *ptr =
775 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
776 size_t size = pa_difference(begin, end);
777
778 if (!ptr) {
779 /* TODO: partial defrag of failed range. */
780 /* Recover any memory consumed in failed mapping. */
781 mm_defrag(stage1_locked, ppool);
782 goto fail;
783 }
784
785 memset_s(ptr, size, 0, size);
786 arch_mm_flush_dcache(ptr, size);
787 mm_unmap(stage1_locked, begin, end, ppool);
788
789 ret = true;
790 goto out;
791
792fail:
793 ret = false;
794
795out:
796 mm_unlock_stage1(&stage1_locked);
797
798 return ret;
799}
800
801/**
802 * Clears a region of physical memory by overwriting it with zeros. The data is
803 * flushed from the cache so the memory has been cleared across the system.
804 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100806 struct ffa_memory_region_constituent **fragments,
807 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
808 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100809{
810 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100812 struct mm_stage1_locked stage1_locked;
813 bool ret = false;
814
815 /*
816 * Create a local pool so any freed memory can't be used by another
817 * thread. This is to ensure each constituent that is mapped can be
818 * unmapped again afterwards.
819 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000820 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100821
Andrew Walbranca808b12020-05-15 17:22:28 +0100822 /* Iterate over the memory region constituents within each fragment. */
823 for (i = 0; i < fragment_count; ++i) {
824 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100825
Andrew Walbranca808b12020-05-15 17:22:28 +0100826 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
827 size_t size = fragments[i][j].page_count * PAGE_SIZE;
828 paddr_t begin =
829 pa_from_ipa(ipa_init(fragments[i][j].address));
830 paddr_t end = pa_add(begin, size);
831
832 if (!clear_memory(begin, end, &local_page_pool)) {
833 /*
834 * api_clear_memory will defrag on failure, so
835 * no need to do it here.
836 */
837 goto out;
838 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100839 }
840 }
841
842 /*
843 * Need to defrag after clearing, as it may have added extra mappings to
844 * the stage 1 page table.
845 */
846 stage1_locked = mm_lock_stage1();
847 mm_defrag(stage1_locked, &local_page_pool);
848 mm_unlock_stage1(&stage1_locked);
849
850 ret = true;
851
852out:
853 mpool_fini(&local_page_pool);
854 return ret;
855}
856
857/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000858 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000860 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 *
862 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000863 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100865 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100866 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
867 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100869 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100870 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100871 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100872static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000873 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100874 struct ffa_memory_region_constituent **fragments,
875 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
876 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100877 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100878{
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100880 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 uint32_t orig_from_mode;
882 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100885
886 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100887 * Make sure constituents are properly aligned to a 64-bit boundary. If
888 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100890 for (i = 0; i < fragment_count; ++i) {
891 if (!is_aligned(fragments[i], 8)) {
892 dlog_verbose("Constituents not aligned.\n");
893 return ffa_error(FFA_INVALID_PARAMETERS);
894 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100895 }
896
897 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000898 * Check if the state transition is lawful for the sender, ensure that
899 * all constituents of a memory region being shared are at the same
900 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100901 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100902 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100903 &orig_from_mode, fragments,
904 fragment_constituent_counts,
905 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100906 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100907 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100908 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 }
910
Andrew Walbran37c574e2020-06-03 11:45:46 +0100911 if (orig_from_mode_ret != NULL) {
912 *orig_from_mode_ret = orig_from_mode;
913 }
914
Jose Marinho09b1db82019-08-08 09:16:59 +0100915 /*
916 * Create a local pool so any freed memory can't be used by another
917 * thread. This is to ensure the original mapping can be restored if the
918 * clear fails.
919 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000920 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100921
922 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000923 * First reserve all required memory for the new page table entries
924 * without committing, to make sure the entire operation will succeed
925 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100926 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 if (!ffa_region_group_identity_map(
928 from_locked, fragments, fragment_constituent_counts,
929 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100930 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100932 goto out;
933 }
934
935 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000936 * Update the mapping for the sender. This won't allocate because the
937 * transaction was already prepared above, but may free pages in the
938 * case that a whole block is being unmapped that was previously
939 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100940 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100941 CHECK(ffa_region_group_identity_map(
942 from_locked, fragments, fragment_constituent_counts,
943 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100944
945 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100947 fragments, fragment_constituent_counts,
948 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100949 /*
950 * On failure, roll back by returning memory to the sender. This
951 * may allocate pages which were previously freed into
952 * `local_page_pool` by the call above, but will never allocate
953 * more pages than that so can never fail.
954 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100956 from_locked, fragments, fragment_constituent_counts,
957 fragment_count, orig_from_mode, &local_page_pool,
958 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100959
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100960 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100961 goto out;
962 }
963
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100964 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000965
966out:
967 mpool_fini(&local_page_pool);
968
969 /*
970 * Tidy up the page table by reclaiming failed mappings (if there was an
971 * error) or merging entries into blocks where possible (on success).
972 */
973 mm_vm_defrag(&from->ptable, page_pool);
974
975 return ret;
976}
977
978/**
979 * Validates and maps memory shared from one VM to another.
980 *
981 * This function requires the calling context to hold the <to> lock.
982 *
983 * Returns:
984 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100985 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000986 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100987 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000988 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100989 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000990 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100991static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000992 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100993 struct ffa_memory_region_constituent **fragments,
994 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
995 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
996 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997{
998 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100999 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001000 uint32_t to_mode;
1001 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001003
1004 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001005 * Make sure constituents are properly aligned to a 64-bit boundary. If
1006 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001008 for (i = 0; i < fragment_count; ++i) {
1009 if (!is_aligned(fragments[i], 8)) {
1010 return ffa_error(FFA_INVALID_PARAMETERS);
1011 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001012 }
1013
1014 /*
1015 * Check if the state transition is lawful for the recipient, and ensure
1016 * that all constituents of the memory region being retrieved are at the
1017 * same state.
1018 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 ret = ffa_retrieve_check_transition(
1020 to_locked, share_func, fragments, fragment_constituent_counts,
1021 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001022 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001023 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001024 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001025 }
1026
1027 /*
1028 * Create a local pool so any freed memory can't be used by another
1029 * thread. This is to ensure the original mapping can be restored if the
1030 * clear fails.
1031 */
1032 mpool_init_with_fallback(&local_page_pool, page_pool);
1033
1034 /*
1035 * First reserve all required memory for the new page table entries in
1036 * the recipient page tables without committing, to make sure the entire
1037 * operation will succeed without exhausting the page pool.
1038 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001039 if (!ffa_region_group_identity_map(
1040 to_locked, fragments, fragment_constituent_counts,
1041 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001042 /* TODO: partial defrag of failed range. */
1043 dlog_verbose(
1044 "Insufficient memory to update recipient page "
1045 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001047 goto out;
1048 }
1049
1050 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001052 fragments, fragment_constituent_counts,
1053 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001054 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001055 goto out;
1056 }
1057
Jose Marinho09b1db82019-08-08 09:16:59 +01001058 /*
1059 * Complete the transfer by mapping the memory into the recipient. This
1060 * won't allocate because the transaction was already prepared above, so
1061 * it doesn't need to use the `local_page_pool`.
1062 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001063 CHECK(ffa_region_group_identity_map(
1064 to_locked, fragments, fragment_constituent_counts,
1065 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001066
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001067 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001068
1069out:
1070 mpool_fini(&local_page_pool);
1071
1072 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001073 * Tidy up the page table by reclaiming failed mappings (if there was an
1074 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001075 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001076 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001077
1078 return ret;
1079}
1080
Andrew Walbran290b0c92020-02-03 16:37:14 +00001081/**
1082 * Reclaims the given memory from the TEE. To do this space is first reserved in
1083 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1084 * then (if that is successful) the memory is mapped back into the <to> VM's
1085 * page table.
1086 *
1087 * This function requires the calling context to hold the <to> lock.
1088 *
1089 * Returns:
1090 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001091 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001092 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001093 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001094 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001096 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001097static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001098 struct vm_locked to_locked, ffa_memory_handle_t handle,
1099 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001100 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1101 struct mpool *page_pool)
1102{
1103 struct vm *to = to_locked.vm;
1104 uint32_t to_mode;
1105 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 struct ffa_value ret;
1107 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001108
1109 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001110 * Make sure constituents are properly aligned to a 64-bit boundary. If
1111 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001112 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001113 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001114 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001116 }
1117
1118 /*
1119 * Check if the state transition is lawful for the recipient, and ensure
1120 * that all constituents of the memory region being retrieved are at the
1121 * same state.
1122 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001124 &constituents, &constituent_count,
1125 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001126 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001127 dlog_verbose("Invalid transition.\n");
1128 return ret;
1129 }
1130
1131 /*
1132 * Create a local pool so any freed memory can't be used by another
1133 * thread. This is to ensure the original mapping can be restored if the
1134 * clear fails.
1135 */
1136 mpool_init_with_fallback(&local_page_pool, page_pool);
1137
1138 /*
1139 * First reserve all required memory for the new page table entries in
1140 * the recipient page tables without committing, to make sure the entire
1141 * operation will succeed without exhausting the page pool.
1142 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001143 if (!ffa_region_group_identity_map(to_locked, &constituents,
1144 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001145 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001146 /* TODO: partial defrag of failed range. */
1147 dlog_verbose(
1148 "Insufficient memory to update recipient page "
1149 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001151 goto out;
1152 }
1153
1154 /*
1155 * Forward the request to the TEE and see what happens.
1156 */
1157 tee_flags = 0;
1158 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001159 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001160 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001161 ret = arch_other_world_call(
1162 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1163 .arg1 = (uint32_t)handle,
1164 .arg2 = (uint32_t)(handle >> 32),
1165 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001166
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001167 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001168 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001169 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1170 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001171 ret.func, ret.arg2);
1172 goto out;
1173 }
1174
1175 /*
1176 * The TEE was happy with it, so complete the reclaim by mapping the
1177 * memory into the recipient. This won't allocate because the
1178 * transaction was already prepared above, so it doesn't need to use the
1179 * `local_page_pool`.
1180 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001181 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1182 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001183 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001184
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001185 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001186
1187out:
1188 mpool_fini(&local_page_pool);
1189
1190 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001191 * Tidy up the page table by reclaiming failed mappings (if there was an
1192 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001193 */
1194 mm_vm_defrag(&to->ptable, page_pool);
1195
1196 return ret;
1197}
1198
Andrew Walbran996d1d12020-05-27 14:08:43 +01001199static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001200 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001201 struct ffa_memory_region_constituent **fragments,
1202 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1203 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001204{
1205 uint32_t orig_from_mode;
1206 uint32_t from_mode;
1207 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001208 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001209
Andrew Walbranca808b12020-05-15 17:22:28 +01001210 ret = ffa_relinquish_check_transition(
1211 from_locked, &orig_from_mode, fragments,
1212 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001214 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001215 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001216 }
1217
1218 /*
1219 * Create a local pool so any freed memory can't be used by another
1220 * thread. This is to ensure the original mapping can be restored if the
1221 * clear fails.
1222 */
1223 mpool_init_with_fallback(&local_page_pool, page_pool);
1224
1225 /*
1226 * First reserve all required memory for the new page table entries
1227 * without committing, to make sure the entire operation will succeed
1228 * without exhausting the page pool.
1229 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 if (!ffa_region_group_identity_map(
1231 from_locked, fragments, fragment_constituent_counts,
1232 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001233 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001235 goto out;
1236 }
1237
1238 /*
1239 * Update the mapping for the sender. This won't allocate because the
1240 * transaction was already prepared above, but may free pages in the
1241 * case that a whole block is being unmapped that was previously
1242 * partially mapped.
1243 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001244 CHECK(ffa_region_group_identity_map(
1245 from_locked, fragments, fragment_constituent_counts,
1246 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001247
1248 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001249 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001250 fragments, fragment_constituent_counts,
1251 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252 /*
1253 * On failure, roll back by returning memory to the sender. This
1254 * may allocate pages which were previously freed into
1255 * `local_page_pool` by the call above, but will never allocate
1256 * more pages than that so can never fail.
1257 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001258 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001259 from_locked, fragments, fragment_constituent_counts,
1260 fragment_count, orig_from_mode, &local_page_pool,
1261 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001262
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001263 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001264 goto out;
1265 }
1266
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001267 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001268
1269out:
1270 mpool_fini(&local_page_pool);
1271
1272 /*
1273 * Tidy up the page table by reclaiming failed mappings (if there was an
1274 * error) or merging entries into blocks where possible (on success).
1275 */
1276 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001277
1278 return ret;
1279}
1280
1281/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001282 * Complete a memory sending operation by checking that it is valid, updating
1283 * the sender page table, and then either marking the share state as having
1284 * completed sending (on success) or freeing it (on failure).
1285 *
1286 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1287 */
1288static struct ffa_value ffa_memory_send_complete(
1289 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001290 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1291 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001292{
1293 struct ffa_memory_region *memory_region = share_state->memory_region;
1294 struct ffa_value ret;
1295
1296 /* Lock must be held. */
1297 CHECK(share_states.share_states != NULL);
1298
1299 /* Check that state is valid in sender page table and update. */
1300 ret = ffa_send_check_update(
1301 from_locked, share_state->fragments,
1302 share_state->fragment_constituent_counts,
1303 share_state->fragment_count, share_state->share_func,
1304 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001305 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1306 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001307 if (ret.func != FFA_SUCCESS_32) {
1308 /*
1309 * Free share state, it failed to send so it can't be retrieved.
1310 */
1311 dlog_verbose("Complete failed, freeing share state.\n");
1312 share_state_free(share_states, share_state, page_pool);
1313 return ret;
1314 }
1315
1316 share_state->sending_complete = true;
1317 dlog_verbose("Marked sending complete.\n");
1318
J-Alvesee68c542020-10-29 17:48:20 +00001319 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001320}
1321
1322/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001323 * Check that the given `memory_region` represents a valid memory send request
1324 * of the given `share_func` type, return the clear flag and permissions via the
1325 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001326 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 * not.
1329 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001331 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1332 uint32_t memory_share_length, uint32_t fragment_length,
1333 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001334{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001336 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001337 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001338 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001339 enum ffa_data_access data_access;
1340 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001341
Andrew Walbrana65a1322020-04-06 19:32:32 +01001342 CHECK(permissions != NULL);
1343
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001344 /*
1345 * This should already be checked by the caller, just making the
1346 * assumption clear here.
1347 */
1348 CHECK(memory_region->receiver_count == 1);
1349
Andrew Walbrana65a1322020-04-06 19:32:32 +01001350 /* The sender must match the message sender. */
1351 if (memory_region->sender != from_locked.vm->id) {
1352 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001353 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001354 }
1355
Andrew Walbrana65a1322020-04-06 19:32:32 +01001356 /*
1357 * Ensure that the composite header is within the memory bounds and
1358 * doesn't overlap the first part of the message.
1359 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001360 receivers_length = sizeof(struct ffa_memory_access) *
1361 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001362 constituents_offset =
1363 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001364 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001365 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001366 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001368 "Invalid composite memory region descriptor offset "
1369 "%d.\n",
1370 memory_region->receivers[0]
1371 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001372 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001373 }
1374
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001376
1377 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001378 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001379 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001380 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1381 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001382 if (memory_share_length != constituents_offset + constituents_length) {
1383 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001384 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001385 memory_region->receivers[0]
1386 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001389 if (fragment_length < memory_share_length &&
1390 fragment_length < HF_MAILBOX_SIZE) {
1391 dlog_warning(
1392 "Initial fragment length %d smaller than mailbox "
1393 "size.\n",
1394 fragment_length);
1395 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397 /*
1398 * Clear is not allowed for memory sharing, as the sender still has
1399 * access to the memory.
1400 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001401 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1402 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001403 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001405 }
1406
1407 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001409 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001410 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001411 }
1412
1413 /* Check that the permissions are valid. */
1414 *permissions =
1415 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001416 data_access = ffa_get_data_access_attr(*permissions);
1417 instruction_access = ffa_get_instruction_access_attr(*permissions);
1418 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1419 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001420 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1421 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001422 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001423 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 dlog_verbose(
1426 "Invalid instruction access permissions %#x for "
1427 "sending memory.\n",
1428 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001431 if (share_func == FFA_MEM_SHARE_32) {
1432 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001433 dlog_verbose(
1434 "Invalid data access permissions %#x for "
1435 "sharing memory.\n",
1436 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001438 }
1439 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001440 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1441 * required for share operations (but must not be specified by
1442 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001443 * returned to the retriever.
1444 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001445 ffa_set_instruction_access_attr(permissions,
1446 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447 memory_region->receivers[0].receiver_permissions.permissions =
1448 *permissions;
1449 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450 if (share_func == FFA_MEM_LEND_32 &&
1451 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 dlog_verbose(
1453 "Invalid data access permissions %#x for lending "
1454 "memory.\n",
1455 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001456 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458 if (share_func == FFA_MEM_DONATE_32 &&
1459 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001460 dlog_verbose(
1461 "Invalid data access permissions %#x for donating "
1462 "memory.\n",
1463 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001464 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001465 }
1466
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001468}
1469
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001470/** Forwards a memory send message on to the TEE. */
1471static struct ffa_value memory_send_tee_forward(
1472 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1473 uint32_t share_func, struct ffa_memory_region *memory_region,
1474 uint32_t memory_share_length, uint32_t fragment_length)
1475{
1476 struct ffa_value ret;
1477
1478 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1479 memory_region, fragment_length);
1480 tee_locked.vm->mailbox.recv_size = fragment_length;
1481 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1482 tee_locked.vm->mailbox.recv_func = share_func;
1483 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001484 ret = arch_other_world_call(
1485 (struct ffa_value){.func = share_func,
1486 .arg1 = memory_share_length,
1487 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001488 /*
1489 * After the call to the TEE completes it must have finished reading its
1490 * RX buffer, so it is ready for another message.
1491 */
1492 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1493
1494 return ret;
1495}
1496
Andrew Walbrana65a1322020-04-06 19:32:32 +01001497/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001498 * Gets the share state for continuing an operation to donate, lend or share
1499 * memory, and checks that it is a valid request.
1500 *
1501 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1502 * not.
1503 */
1504static struct ffa_value ffa_memory_send_continue_validate(
1505 struct share_states_locked share_states, ffa_memory_handle_t handle,
1506 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1507 struct mpool *page_pool)
1508{
1509 struct ffa_memory_share_state *share_state;
1510 struct ffa_memory_region *memory_region;
1511
1512 CHECK(share_state_ret != NULL);
1513
1514 /*
1515 * Look up the share state by handle and make sure that the VM ID
1516 * matches.
1517 */
1518 if (!get_share_state(share_states, handle, &share_state)) {
1519 dlog_verbose(
1520 "Invalid handle %#x for memory send continuation.\n",
1521 handle);
1522 return ffa_error(FFA_INVALID_PARAMETERS);
1523 }
1524 memory_region = share_state->memory_region;
1525
1526 if (memory_region->sender != from_vm_id) {
1527 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1528 return ffa_error(FFA_INVALID_PARAMETERS);
1529 }
1530
1531 if (share_state->sending_complete) {
1532 dlog_verbose(
1533 "Sending of memory handle %#x is already complete.\n",
1534 handle);
1535 return ffa_error(FFA_INVALID_PARAMETERS);
1536 }
1537
1538 if (share_state->fragment_count == MAX_FRAGMENTS) {
1539 /*
1540 * Log a warning as this is a sign that MAX_FRAGMENTS should
1541 * probably be increased.
1542 */
1543 dlog_warning(
1544 "Too many fragments for memory share with handle %#x; "
1545 "only %d supported.\n",
1546 handle, MAX_FRAGMENTS);
1547 /* Free share state, as it's not possible to complete it. */
1548 share_state_free(share_states, share_state, page_pool);
1549 return ffa_error(FFA_NO_MEMORY);
1550 }
1551
1552 *share_state_ret = share_state;
1553
1554 return (struct ffa_value){.func = FFA_SUCCESS_32};
1555}
1556
1557/**
1558 * Forwards a memory send continuation message on to the TEE.
1559 */
1560static struct ffa_value memory_send_continue_tee_forward(
1561 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1562 uint32_t fragment_length, ffa_memory_handle_t handle)
1563{
1564 struct ffa_value ret;
1565
1566 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1567 fragment_length);
1568 tee_locked.vm->mailbox.recv_size = fragment_length;
1569 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1570 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1571 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001572 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001573 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1574 .arg1 = (uint32_t)handle,
1575 .arg2 = (uint32_t)(handle >> 32),
1576 .arg3 = fragment_length,
1577 .arg4 = (uint64_t)sender_vm_id << 16});
1578 /*
1579 * After the call to the TEE completes it must have finished reading its
1580 * RX buffer, so it is ready for another message.
1581 */
1582 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1583
1584 return ret;
1585}
1586
1587/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001588 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1589 * updates the stage-2 page tables. Specifically, check if the message length
1590 * and number of memory region constituents match, and if the transition is
1591 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001592 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001593 * Assumes that the caller has already found and locked the sender VM and copied
1594 * the memory region descriptor from the sender's TX buffer to a freshly
1595 * allocated page from Hafnium's internal pool. The caller must have also
1596 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001597 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001598 * This function takes ownership of the `memory_region` passed in and will free
1599 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001600 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001601struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001603 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001604 uint32_t fragment_length, uint32_t share_func,
1605 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001606{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 ffa_memory_access_permissions_t permissions;
1608 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001609 struct share_states_locked share_states;
1610 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001611
1612 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001613 * If there is an error validating the `memory_region` then we need to
1614 * free it because we own it but we won't be storing it in a share state
1615 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001616 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001617 ret = ffa_memory_send_validate(from_locked, memory_region,
1618 memory_share_length, fragment_length,
1619 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001621 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001622 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001623 }
1624
Andrew Walbrana65a1322020-04-06 19:32:32 +01001625 /* Set flag for share function, ready to be retrieved later. */
1626 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001627 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001628 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001629 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001630 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001631 case FFA_MEM_LEND_32:
1632 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001633 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001634 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001635 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001637 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001638 }
1639
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001641 /*
1642 * Allocate a share state before updating the page table. Otherwise if
1643 * updating the page table succeeded but allocating the share state
1644 * failed then it would leave the memory in a state where nobody could
1645 * get it back.
1646 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001647 if (!allocate_share_state(share_states, share_func, memory_region,
1648 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1649 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001650 dlog_verbose("Failed to allocate share state.\n");
1651 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001652 ret = ffa_error(FFA_NO_MEMORY);
1653 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001654 }
1655
Andrew Walbranca808b12020-05-15 17:22:28 +01001656 if (fragment_length == memory_share_length) {
1657 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001658 ret = ffa_memory_send_complete(
1659 from_locked, share_states, share_state, page_pool,
1660 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001661 } else {
1662 ret = (struct ffa_value){
1663 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001664 .arg1 = (uint32_t)memory_region->handle,
1665 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001666 .arg3 = fragment_length};
1667 }
1668
1669out:
1670 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001671 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001672 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001673}
1674
1675/**
1676 * Validates a call to donate, lend or share memory to the TEE and then updates
1677 * the stage-2 page tables. Specifically, check if the message length and number
1678 * of memory region constituents match, and if the transition is valid for the
1679 * type of memory sending operation.
1680 *
1681 * Assumes that the caller has already found and locked the sender VM and the
1682 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1683 * to a freshly allocated page from Hafnium's internal pool. The caller must
1684 * have also validated that the receiver VM ID is valid.
1685 *
1686 * This function takes ownership of the `memory_region` passed in and will free
1687 * it when necessary; it must not be freed by the caller.
1688 */
1689struct ffa_value ffa_memory_tee_send(
1690 struct vm_locked from_locked, struct vm_locked to_locked,
1691 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1692 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1693{
1694 ffa_memory_access_permissions_t permissions;
1695 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001696
1697 /*
1698 * If there is an error validating the `memory_region` then we need to
1699 * free it because we own it but we won't be storing it in a share state
1700 * after all.
1701 */
1702 ret = ffa_memory_send_validate(from_locked, memory_region,
1703 memory_share_length, fragment_length,
1704 share_func, &permissions);
1705 if (ret.func != FFA_SUCCESS_32) {
1706 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 }
1708
Andrew Walbranca808b12020-05-15 17:22:28 +01001709 if (fragment_length == memory_share_length) {
1710 /* No more fragments to come, everything fit in one message. */
1711 struct ffa_composite_memory_region *composite =
1712 ffa_memory_region_get_composite(memory_region, 0);
1713 struct ffa_memory_region_constituent *constituents =
1714 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001715 struct mpool local_page_pool;
1716 uint32_t orig_from_mode;
1717
1718 /*
1719 * Use a local page pool so that we can roll back if necessary.
1720 */
1721 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001722
1723 ret = ffa_send_check_update(
1724 from_locked, &constituents,
1725 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001726 permissions, &local_page_pool,
1727 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1728 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001729 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001730 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001731 goto out;
1732 }
1733
1734 /* Forward memory send message on to TEE. */
1735 ret = memory_send_tee_forward(
1736 to_locked, from_locked.vm->id, share_func,
1737 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001738
1739 if (ret.func != FFA_SUCCESS_32) {
1740 dlog_verbose(
1741 "TEE didn't successfully complete memory send "
1742 "operation; returned %#x (%d). Rolling back.\n",
1743 ret.func, ret.arg2);
1744
1745 /*
1746 * The TEE failed to complete the send operation, so
1747 * roll back the page table update for the VM. This
1748 * can't fail because it won't try to allocate more
1749 * memory than was freed into the `local_page_pool` by
1750 * `ffa_send_check_update` in the initial update.
1751 */
1752 CHECK(ffa_region_group_identity_map(
1753 from_locked, &constituents,
1754 &composite->constituent_count, 1,
1755 orig_from_mode, &local_page_pool, true));
1756 }
1757
1758 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001759 } else {
1760 struct share_states_locked share_states = share_states_lock();
1761 ffa_memory_handle_t handle;
1762
1763 /*
1764 * We need to wait for the rest of the fragments before we can
1765 * check whether the transaction is valid and unmap the memory.
1766 * Call the TEE so it can do its initial validation and assign a
1767 * handle, and allocate a share state to keep what we have so
1768 * far.
1769 */
1770 ret = memory_send_tee_forward(
1771 to_locked, from_locked.vm->id, share_func,
1772 memory_region, memory_share_length, fragment_length);
1773 if (ret.func == FFA_ERROR_32) {
1774 goto out_unlock;
1775 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1776 dlog_warning(
1777 "Got %#x from TEE in response to %#x for "
1778 "fragment with with %d/%d, expected "
1779 "FFA_MEM_FRAG_RX.\n",
1780 ret.func, share_func, fragment_length,
1781 memory_share_length);
1782 ret = ffa_error(FFA_INVALID_PARAMETERS);
1783 goto out_unlock;
1784 }
1785 handle = ffa_frag_handle(ret);
1786 if (ret.arg3 != fragment_length) {
1787 dlog_warning(
1788 "Got unexpected fragment offset %d for "
1789 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1790 ret.arg3, fragment_length);
1791 ret = ffa_error(FFA_INVALID_PARAMETERS);
1792 goto out_unlock;
1793 }
1794 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1795 dlog_warning(
1796 "Got unexpected sender ID %d for "
1797 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1798 ffa_frag_sender(ret), from_locked.vm->id);
1799 ret = ffa_error(FFA_INVALID_PARAMETERS);
1800 goto out_unlock;
1801 }
1802
1803 if (!allocate_share_state(share_states, share_func,
1804 memory_region, fragment_length,
1805 handle, NULL)) {
1806 dlog_verbose("Failed to allocate share state.\n");
1807 ret = ffa_error(FFA_NO_MEMORY);
1808 goto out_unlock;
1809 }
1810 /*
1811 * Don't free the memory region fragment, as it has been stored
1812 * in the share state.
1813 */
1814 memory_region = NULL;
1815 out_unlock:
1816 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001817 }
1818
Andrew Walbranca808b12020-05-15 17:22:28 +01001819out:
1820 if (memory_region != NULL) {
1821 mpool_free(page_pool, memory_region);
1822 }
1823 dump_share_states();
1824 return ret;
1825}
1826
1827/**
1828 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1829 * this is the last fragment then checks that the transition is valid for the
1830 * type of memory sending operation and updates the stage-2 page tables of the
1831 * sender.
1832 *
1833 * Assumes that the caller has already found and locked the sender VM and copied
1834 * the memory region descriptor from the sender's TX buffer to a freshly
1835 * allocated page from Hafnium's internal pool.
1836 *
1837 * This function takes ownership of the `fragment` passed in; it must not be
1838 * freed by the caller.
1839 */
1840struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1841 void *fragment,
1842 uint32_t fragment_length,
1843 ffa_memory_handle_t handle,
1844 struct mpool *page_pool)
1845{
1846 struct share_states_locked share_states = share_states_lock();
1847 struct ffa_memory_share_state *share_state;
1848 struct ffa_value ret;
1849 struct ffa_memory_region *memory_region;
1850
1851 ret = ffa_memory_send_continue_validate(share_states, handle,
1852 &share_state,
1853 from_locked.vm->id, page_pool);
1854 if (ret.func != FFA_SUCCESS_32) {
1855 goto out_free_fragment;
1856 }
1857 memory_region = share_state->memory_region;
1858
1859 if (memory_region->receivers[0].receiver_permissions.receiver ==
1860 HF_TEE_VM_ID) {
1861 dlog_error(
1862 "Got hypervisor-allocated handle for memory send to "
1863 "TEE. This should never happen, and indicates a bug in "
1864 "EL3 code.\n");
1865 ret = ffa_error(FFA_INVALID_PARAMETERS);
1866 goto out_free_fragment;
1867 }
1868
1869 /* Add this fragment. */
1870 share_state->fragments[share_state->fragment_count] = fragment;
1871 share_state->fragment_constituent_counts[share_state->fragment_count] =
1872 fragment_length / sizeof(struct ffa_memory_region_constituent);
1873 share_state->fragment_count++;
1874
1875 /* Check whether the memory send operation is now ready to complete. */
1876 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001877 ret = ffa_memory_send_complete(
1878 from_locked, share_states, share_state, page_pool,
1879 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001880 } else {
1881 ret = (struct ffa_value){
1882 .func = FFA_MEM_FRAG_RX_32,
1883 .arg1 = (uint32_t)handle,
1884 .arg2 = (uint32_t)(handle >> 32),
1885 .arg3 = share_state_next_fragment_offset(share_states,
1886 share_state)};
1887 }
1888 goto out;
1889
1890out_free_fragment:
1891 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001892
1893out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001894 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001895 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001896}
1897
Andrew Walbranca808b12020-05-15 17:22:28 +01001898/**
1899 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1900 * is the last fragment then checks that the transition is valid for the type of
1901 * memory sending operation and updates the stage-2 page tables of the sender.
1902 *
1903 * Assumes that the caller has already found and locked the sender VM and copied
1904 * the memory region descriptor from the sender's TX buffer to a freshly
1905 * allocated page from Hafnium's internal pool.
1906 *
1907 * This function takes ownership of the `memory_region` passed in and will free
1908 * it when necessary; it must not be freed by the caller.
1909 */
1910struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1911 struct vm_locked to_locked,
1912 void *fragment,
1913 uint32_t fragment_length,
1914 ffa_memory_handle_t handle,
1915 struct mpool *page_pool)
1916{
1917 struct share_states_locked share_states = share_states_lock();
1918 struct ffa_memory_share_state *share_state;
1919 struct ffa_value ret;
1920 struct ffa_memory_region *memory_region;
1921
1922 ret = ffa_memory_send_continue_validate(share_states, handle,
1923 &share_state,
1924 from_locked.vm->id, page_pool);
1925 if (ret.func != FFA_SUCCESS_32) {
1926 goto out_free_fragment;
1927 }
1928 memory_region = share_state->memory_region;
1929
1930 if (memory_region->receivers[0].receiver_permissions.receiver !=
1931 HF_TEE_VM_ID) {
1932 dlog_error(
1933 "Got SPM-allocated handle for memory send to non-TEE "
1934 "VM. This should never happen, and indicates a bug.\n");
1935 ret = ffa_error(FFA_INVALID_PARAMETERS);
1936 goto out_free_fragment;
1937 }
1938
1939 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1940 to_locked.vm->mailbox.recv == NULL) {
1941 /*
1942 * If the TEE RX buffer is not available, tell the sender to
1943 * retry by returning the current offset again.
1944 */
1945 ret = (struct ffa_value){
1946 .func = FFA_MEM_FRAG_RX_32,
1947 .arg1 = (uint32_t)handle,
1948 .arg2 = (uint32_t)(handle >> 32),
1949 .arg3 = share_state_next_fragment_offset(share_states,
1950 share_state),
1951 };
1952 goto out_free_fragment;
1953 }
1954
1955 /* Add this fragment. */
1956 share_state->fragments[share_state->fragment_count] = fragment;
1957 share_state->fragment_constituent_counts[share_state->fragment_count] =
1958 fragment_length / sizeof(struct ffa_memory_region_constituent);
1959 share_state->fragment_count++;
1960
1961 /* Check whether the memory send operation is now ready to complete. */
1962 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001963 struct mpool local_page_pool;
1964 uint32_t orig_from_mode;
1965
1966 /*
1967 * Use a local page pool so that we can roll back if necessary.
1968 */
1969 mpool_init_with_fallback(&local_page_pool, page_pool);
1970
Andrew Walbranca808b12020-05-15 17:22:28 +01001971 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001972 share_state, &local_page_pool,
1973 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001974
1975 if (ret.func == FFA_SUCCESS_32) {
1976 /*
1977 * Forward final fragment on to the TEE so that
1978 * it can complete the memory sending operation.
1979 */
1980 ret = memory_send_continue_tee_forward(
1981 to_locked, from_locked.vm->id, fragment,
1982 fragment_length, handle);
1983
1984 if (ret.func != FFA_SUCCESS_32) {
1985 /*
1986 * The error will be passed on to the caller,
1987 * but log it here too.
1988 */
1989 dlog_verbose(
1990 "TEE didn't successfully complete "
1991 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001992 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001993 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001994
1995 /*
1996 * The TEE failed to complete the send
1997 * operation, so roll back the page table update
1998 * for the VM. This can't fail because it won't
1999 * try to allocate more memory than was freed
2000 * into the `local_page_pool` by
2001 * `ffa_send_check_update` in the initial
2002 * update.
2003 */
2004 CHECK(ffa_region_group_identity_map(
2005 from_locked, share_state->fragments,
2006 share_state
2007 ->fragment_constituent_counts,
2008 share_state->fragment_count,
2009 orig_from_mode, &local_page_pool,
2010 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002011 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002012
Andrew Walbranca808b12020-05-15 17:22:28 +01002013 /* Free share state. */
2014 share_state_free(share_states, share_state, page_pool);
2015 } else {
2016 /* Abort sending to TEE. */
2017 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002018 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002019 .func = FFA_MEM_RECLAIM_32,
2020 .arg1 = (uint32_t)handle,
2021 .arg2 = (uint32_t)(handle >> 32)});
2022
2023 if (tee_ret.func != FFA_SUCCESS_32) {
2024 /*
2025 * Nothing we can do if TEE doesn't abort
2026 * properly, just log it.
2027 */
2028 dlog_verbose(
2029 "TEE didn't successfully abort failed "
2030 "memory send operation; returned %#x "
2031 "(%d).\n",
2032 tee_ret.func, tee_ret.arg2);
2033 }
2034 /*
2035 * We don't need to free the share state in this case
2036 * because ffa_memory_send_complete does that already.
2037 */
2038 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002039
2040 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002041 } else {
2042 uint32_t next_fragment_offset =
2043 share_state_next_fragment_offset(share_states,
2044 share_state);
2045
2046 ret = memory_send_continue_tee_forward(
2047 to_locked, from_locked.vm->id, fragment,
2048 fragment_length, handle);
2049
2050 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2051 ffa_frag_handle(ret) != handle ||
2052 ret.arg3 != next_fragment_offset ||
2053 ffa_frag_sender(ret) != from_locked.vm->id) {
2054 dlog_verbose(
2055 "Got unexpected result from forwarding "
2056 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2057 "offset %d, sender %d); expected "
2058 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2059 "sender %d).\n",
2060 ret.func, ffa_frag_handle(ret), ret.arg3,
2061 ffa_frag_sender(ret), handle,
2062 next_fragment_offset, from_locked.vm->id);
2063 /* Free share state. */
2064 share_state_free(share_states, share_state, page_pool);
2065 ret = ffa_error(FFA_INVALID_PARAMETERS);
2066 goto out;
2067 }
2068
2069 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2070 .arg1 = (uint32_t)handle,
2071 .arg2 = (uint32_t)(handle >> 32),
2072 .arg3 = next_fragment_offset};
2073 }
2074 goto out;
2075
2076out_free_fragment:
2077 mpool_free(page_pool, fragment);
2078
2079out:
2080 share_states_unlock(&share_states);
2081 return ret;
2082}
2083
2084/** Clean up after the receiver has finished retrieving a memory region. */
2085static void ffa_memory_retrieve_complete(
2086 struct share_states_locked share_states,
2087 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2088{
2089 if (share_state->share_func == FFA_MEM_DONATE_32) {
2090 /*
2091 * Memory that has been donated can't be relinquished,
2092 * so no need to keep the share state around.
2093 */
2094 share_state_free(share_states, share_state, page_pool);
2095 dlog_verbose("Freed share state for donate.\n");
2096 }
2097}
2098
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002099struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2100 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002101 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002102 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002103{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002104 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002105 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002106 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002107 sizeof(struct ffa_memory_access);
2108 ffa_memory_handle_t handle = retrieve_request->handle;
2109 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002110 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2112 struct ffa_memory_region *memory_region;
2113 ffa_memory_access_permissions_t sent_permissions;
2114 enum ffa_data_access sent_data_access;
2115 enum ffa_instruction_access sent_instruction_access;
2116 ffa_memory_access_permissions_t requested_permissions;
2117 enum ffa_data_access requested_data_access;
2118 enum ffa_instruction_access requested_instruction_access;
2119 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002120 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002122 struct ffa_memory_share_state *share_state;
2123 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002124 struct ffa_composite_memory_region *composite;
2125 uint32_t total_length;
2126 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002127
2128 dump_share_states();
2129
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002130 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002131 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002132 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002133 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002134 expected_retrieve_request_length,
2135 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002136 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002137 }
2138
Andrew Walbrana65a1322020-04-06 19:32:32 +01002139 if (retrieve_request->receiver_count != 1) {
2140 dlog_verbose(
2141 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002142 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002143 "expected 1).\n",
2144 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002145 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002146 }
2147
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002148 share_states = share_states_lock();
2149 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002151 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002152 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002153 goto out;
2154 }
2155
Andrew Walbrana65a1322020-04-06 19:32:32 +01002156 memory_region = share_state->memory_region;
2157 CHECK(memory_region != NULL);
2158
2159 /*
2160 * Check that the transaction type expected by the receiver is correct,
2161 * if it has been specified.
2162 */
2163 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002164 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002165 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002166 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002167 dlog_verbose(
2168 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002169 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002170 transaction_type,
2171 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002172 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002173 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002174 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002175 goto out;
2176 }
2177
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002178 if (retrieve_request->sender != memory_region->sender) {
2179 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002180 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002181 "expected %d for handle %#x.\n",
2182 retrieve_request->sender, memory_region->sender,
2183 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002184 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002185 goto out;
2186 }
2187
2188 if (retrieve_request->tag != memory_region->tag) {
2189 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002190 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002191 "%d for handle %#x.\n",
2192 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002193 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 goto out;
2195 }
2196
Andrew Walbrana65a1322020-04-06 19:32:32 +01002197 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2198 to_locked.vm->id) {
2199 dlog_verbose(
2200 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002201 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002202 retrieve_request->receivers[0]
2203 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002204 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002205 goto out;
2206 }
2207
2208 if (memory_region->receivers[0].receiver_permissions.receiver !=
2209 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002210 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002211 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2212 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002213 to_locked.vm->id,
2214 memory_region->receivers[0]
2215 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002216 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002217 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002218 goto out;
2219 }
2220
Andrew Walbranca808b12020-05-15 17:22:28 +01002221 if (!share_state->sending_complete) {
2222 dlog_verbose(
2223 "Memory with handle %#x not fully sent, can't "
2224 "retrieve.\n",
2225 handle);
2226 ret = ffa_error(FFA_INVALID_PARAMETERS);
2227 goto out;
2228 }
2229
2230 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002231 dlog_verbose("Memory with handle %#x already retrieved.\n",
2232 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002233 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002234 goto out;
2235 }
2236
Andrew Walbrana65a1322020-04-06 19:32:32 +01002237 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2238 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002239 dlog_verbose(
2240 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002241 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002242 retrieve_request->receivers[0]
2243 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002245 goto out;
2246 }
2247
J-Alves84658fc2021-06-17 14:37:32 +01002248 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2249 dlog_verbose(
2250 "Bits 31-10 must be zero in memory region's flags.\n");
2251 ret = ffa_error(FFA_INVALID_PARAMETERS);
2252 goto out;
2253 }
2254
2255 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2256 (retrieve_request->flags &
2257 (FFA_MEMORY_REGION_FLAG_CLEAR |
2258 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2259 dlog_verbose(
2260 "Memory Share operation can't clean after relinquish "
2261 "memory region.\n");
2262 ret = ffa_error(FFA_INVALID_PARAMETERS);
2263 goto out;
2264 }
2265
Andrew Walbrana65a1322020-04-06 19:32:32 +01002266 /*
2267 * Check permissions from sender against permissions requested by
2268 * receiver.
2269 */
2270 /* TODO: Check attributes too. */
2271 sent_permissions =
2272 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002273 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002274 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002275 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002276 requested_permissions =
2277 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002279 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002281 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002282
2283 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2284 requested_permissions == FFA_DATA_ACCESS_RO) &&
2285 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2286 dlog_verbose(
2287 "Receiver has RO permissions can not request clear.\n");
2288 ret = ffa_error(FFA_DENIED);
2289 goto out;
2290 }
2291
Andrew Walbrana65a1322020-04-06 19:32:32 +01002292 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002293 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2294 case FFA_DATA_ACCESS_RW:
2295 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2296 requested_data_access == FFA_DATA_ACCESS_RW) {
2297 ffa_set_data_access_attr(&permissions,
2298 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002299 break;
2300 }
2301 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002302 case FFA_DATA_ACCESS_RO:
2303 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2304 requested_data_access == FFA_DATA_ACCESS_RO) {
2305 ffa_set_data_access_attr(&permissions,
2306 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002307 break;
2308 }
2309 dlog_verbose(
2310 "Invalid data access requested; sender specified "
2311 "permissions %#x but receiver requested %#x.\n",
2312 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002313 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002314 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002315 case FFA_DATA_ACCESS_RESERVED:
2316 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002317 "checked before this point.");
2318 }
2319 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002320 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2321 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002322 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002323 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2324 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2325 ffa_set_instruction_access_attr(
2326 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002327 break;
2328 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002329 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002330 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2332 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2333 ffa_set_instruction_access_attr(
2334 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002335 break;
2336 }
2337 dlog_verbose(
2338 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002339 "specified permissions %#x but receiver requested "
2340 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002341 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002342 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002343 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002344 case FFA_INSTRUCTION_ACCESS_RESERVED:
2345 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002346 "be checked before this point.");
2347 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002348 memory_to_attributes = ffa_memory_permissions_to_mode(
2349 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002350 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002351 to_locked, share_state->fragments,
2352 share_state->fragment_constituent_counts,
2353 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002354 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002355 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002356 goto out;
2357 }
2358
2359 /*
2360 * Copy response to RX buffer of caller and deliver the message. This
2361 * must be done before the share_state is (possibly) freed.
2362 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002363 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002364 composite = ffa_memory_region_get_composite(memory_region, 0);
2365 /*
2366 * Constituents which we received in the first fragment should always
2367 * fit in the first fragment we are sending, because the header is the
2368 * same size in both cases and we have a fixed message buffer size. So
2369 * `ffa_retrieved_memory_region_init` should never fail.
2370 */
2371 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002372 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2373 memory_region->sender, memory_region->attributes,
2374 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002375 composite->page_count, composite->constituent_count,
2376 share_state->fragments[0],
2377 share_state->fragment_constituent_counts[0], &total_length,
2378 &fragment_length));
2379 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002380 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002381 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002382 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2383
Andrew Walbranca808b12020-05-15 17:22:28 +01002384 share_state->retrieved_fragment_count[0] = 1;
2385 if (share_state->retrieved_fragment_count[0] ==
2386 share_state->fragment_count) {
2387 ffa_memory_retrieve_complete(share_states, share_state,
2388 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002389 }
2390
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002391 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002392 .arg1 = total_length,
2393 .arg2 = fragment_length};
2394
2395out:
2396 share_states_unlock(&share_states);
2397 dump_share_states();
2398 return ret;
2399}
2400
2401struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2402 ffa_memory_handle_t handle,
2403 uint32_t fragment_offset,
2404 struct mpool *page_pool)
2405{
2406 struct ffa_memory_region *memory_region;
2407 struct share_states_locked share_states;
2408 struct ffa_memory_share_state *share_state;
2409 struct ffa_value ret;
2410 uint32_t fragment_index;
2411 uint32_t retrieved_constituents_count;
2412 uint32_t i;
2413 uint32_t expected_fragment_offset;
2414 uint32_t remaining_constituent_count;
2415 uint32_t fragment_length;
2416
2417 dump_share_states();
2418
2419 share_states = share_states_lock();
2420 if (!get_share_state(share_states, handle, &share_state)) {
2421 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2422 handle);
2423 ret = ffa_error(FFA_INVALID_PARAMETERS);
2424 goto out;
2425 }
2426
2427 memory_region = share_state->memory_region;
2428 CHECK(memory_region != NULL);
2429
2430 if (memory_region->receivers[0].receiver_permissions.receiver !=
2431 to_locked.vm->id) {
2432 dlog_verbose(
2433 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2434 "of handle %#x.\n",
2435 to_locked.vm->id,
2436 memory_region->receivers[0]
2437 .receiver_permissions.receiver,
2438 handle);
2439 ret = ffa_error(FFA_INVALID_PARAMETERS);
2440 goto out;
2441 }
2442
2443 if (!share_state->sending_complete) {
2444 dlog_verbose(
2445 "Memory with handle %#x not fully sent, can't "
2446 "retrieve.\n",
2447 handle);
2448 ret = ffa_error(FFA_INVALID_PARAMETERS);
2449 goto out;
2450 }
2451
2452 if (share_state->retrieved_fragment_count[0] == 0 ||
2453 share_state->retrieved_fragment_count[0] >=
2454 share_state->fragment_count) {
2455 dlog_verbose(
2456 "Retrieval of memory with handle %#x not yet started "
2457 "or already completed (%d/%d fragments retrieved).\n",
2458 handle, share_state->retrieved_fragment_count[0],
2459 share_state->fragment_count);
2460 ret = ffa_error(FFA_INVALID_PARAMETERS);
2461 goto out;
2462 }
2463
2464 fragment_index = share_state->retrieved_fragment_count[0];
2465
2466 /*
2467 * Check that the given fragment offset is correct by counting how many
2468 * constituents were in the fragments previously sent.
2469 */
2470 retrieved_constituents_count = 0;
2471 for (i = 0; i < fragment_index; ++i) {
2472 retrieved_constituents_count +=
2473 share_state->fragment_constituent_counts[i];
2474 }
2475 expected_fragment_offset =
2476 ffa_composite_constituent_offset(memory_region, 0) +
2477 retrieved_constituents_count *
2478 sizeof(struct ffa_memory_region_constituent);
2479 if (fragment_offset != expected_fragment_offset) {
2480 dlog_verbose("Fragment offset was %d but expected %d.\n",
2481 fragment_offset, expected_fragment_offset);
2482 ret = ffa_error(FFA_INVALID_PARAMETERS);
2483 goto out;
2484 }
2485
2486 remaining_constituent_count = ffa_memory_fragment_init(
2487 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2488 share_state->fragments[fragment_index],
2489 share_state->fragment_constituent_counts[fragment_index],
2490 &fragment_length);
2491 CHECK(remaining_constituent_count == 0);
2492 to_locked.vm->mailbox.recv_size = fragment_length;
2493 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2494 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2495 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2496 share_state->retrieved_fragment_count[0]++;
2497 if (share_state->retrieved_fragment_count[0] ==
2498 share_state->fragment_count) {
2499 ffa_memory_retrieve_complete(share_states, share_state,
2500 page_pool);
2501 }
2502
2503 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2504 .arg1 = (uint32_t)handle,
2505 .arg2 = (uint32_t)(handle >> 32),
2506 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507
2508out:
2509 share_states_unlock(&share_states);
2510 dump_share_states();
2511 return ret;
2512}
2513
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002514struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002515 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002516 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002517{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002520 struct ffa_memory_share_state *share_state;
2521 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002522 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002523 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002524
Andrew Walbrana65a1322020-04-06 19:32:32 +01002525 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002527 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002528 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002529 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531 }
2532
Andrew Walbrana65a1322020-04-06 19:32:32 +01002533 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002534 dlog_verbose(
2535 "VM ID %d in relinquish message doesn't match calling "
2536 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002537 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 }
2540
2541 dump_share_states();
2542
2543 share_states = share_states_lock();
2544 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002545 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002547 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002548 goto out;
2549 }
2550
Andrew Walbranca808b12020-05-15 17:22:28 +01002551 if (!share_state->sending_complete) {
2552 dlog_verbose(
2553 "Memory with handle %#x not fully sent, can't "
2554 "relinquish.\n",
2555 handle);
2556 ret = ffa_error(FFA_INVALID_PARAMETERS);
2557 goto out;
2558 }
2559
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 memory_region = share_state->memory_region;
2561 CHECK(memory_region != NULL);
2562
Andrew Walbrana65a1322020-04-06 19:32:32 +01002563 if (memory_region->receivers[0].receiver_permissions.receiver !=
2564 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 dlog_verbose(
2566 "VM ID %d tried to relinquish memory region with "
2567 "handle %#x but receiver was %d.\n",
2568 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002569 memory_region->receivers[0]
2570 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002571 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572 goto out;
2573 }
2574
Andrew Walbranca808b12020-05-15 17:22:28 +01002575 if (share_state->retrieved_fragment_count[0] !=
2576 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002577 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002578 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002579 "relinquish.\n",
2580 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002581 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002582 goto out;
2583 }
2584
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002585 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002586
2587 /*
2588 * Clear is not allowed for memory that was shared, as the original
2589 * sender still has access to the memory.
2590 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002591 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002592 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002593 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002594 goto out;
2595 }
2596
Andrew Walbranca808b12020-05-15 17:22:28 +01002597 ret = ffa_relinquish_check_update(
2598 from_locked, share_state->fragments,
2599 share_state->fragment_constituent_counts,
2600 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002601
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603 /*
2604 * Mark memory handle as not retrieved, so it can be reclaimed
2605 * (or retrieved again).
2606 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002607 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002608 }
2609
2610out:
2611 share_states_unlock(&share_states);
2612 dump_share_states();
2613 return ret;
2614}
2615
2616/**
2617 * Validates that the reclaim transition is allowed for the given handle,
2618 * updates the page table of the reclaiming VM, and frees the internal state
2619 * associated with the handle.
2620 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002621struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002622 ffa_memory_handle_t handle,
2623 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002624 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002625{
2626 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002627 struct ffa_memory_share_state *share_state;
2628 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630
2631 dump_share_states();
2632
2633 share_states = share_states_lock();
2634 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002638 goto out;
2639 }
2640
2641 memory_region = share_state->memory_region;
2642 CHECK(memory_region != NULL);
2643
2644 if (to_locked.vm->id != memory_region->sender) {
2645 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002646 "VM %#x attempted to reclaim memory handle %#x "
2647 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002648 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002649 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 goto out;
2651 }
2652
Andrew Walbranca808b12020-05-15 17:22:28 +01002653 if (!share_state->sending_complete) {
2654 dlog_verbose(
2655 "Memory with handle %#x not fully sent, can't "
2656 "reclaim.\n",
2657 handle);
2658 ret = ffa_error(FFA_INVALID_PARAMETERS);
2659 goto out;
2660 }
2661
2662 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002663 dlog_verbose(
2664 "Tried to reclaim memory handle %#x that has not been "
2665 "relinquished.\n",
2666 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002667 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002668 goto out;
2669 }
2670
Andrew Walbranca808b12020-05-15 17:22:28 +01002671 ret = ffa_retrieve_check_update(
2672 to_locked, share_state->fragments,
2673 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002674 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002675 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002676
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002677 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002678 share_state_free(share_states, share_state, page_pool);
2679 dlog_verbose("Freed share state after successful reclaim.\n");
2680 }
2681
2682out:
2683 share_states_unlock(&share_states);
2684 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002685}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002686
2687/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002688 * Validates that the reclaim transition is allowed for the memory region with
2689 * the given handle which was previously shared with the TEE, tells the TEE to
2690 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2691 *
2692 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002693 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002694struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002695 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002696 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002697 ffa_memory_region_flags_t flags,
2698 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002699{
Andrew Walbranca808b12020-05-15 17:22:28 +01002700 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2701 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2702 struct ffa_value tee_ret;
2703 uint32_t length;
2704 uint32_t fragment_length;
2705 uint32_t fragment_offset;
2706 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002707 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002708 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2709
2710 CHECK(request_length <= HF_MAILBOX_SIZE);
2711 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2712
2713 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002714 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002715 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2716 .arg1 = request_length,
2717 .arg2 = request_length});
2718 if (tee_ret.func == FFA_ERROR_32) {
2719 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2720 return tee_ret;
2721 }
2722 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2723 dlog_verbose(
2724 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2725 tee_ret.func);
2726 return ffa_error(FFA_INVALID_PARAMETERS);
2727 }
2728
2729 length = tee_ret.arg1;
2730 fragment_length = tee_ret.arg2;
2731
2732 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2733 length > sizeof(tee_retrieve_buffer)) {
2734 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2735 fragment_length, length, HF_MAILBOX_SIZE,
2736 sizeof(tee_retrieve_buffer));
2737 return ffa_error(FFA_INVALID_PARAMETERS);
2738 }
2739
2740 /*
2741 * Copy the first fragment of the memory region descriptor to an
2742 * internal buffer.
2743 */
2744 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2745 from_locked.vm->mailbox.send, fragment_length);
2746
2747 /* Fetch the remaining fragments into the same buffer. */
2748 fragment_offset = fragment_length;
2749 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002750 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002751 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2752 .arg1 = (uint32_t)handle,
2753 .arg2 = (uint32_t)(handle >> 32),
2754 .arg3 = fragment_offset});
2755 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2756 dlog_verbose(
2757 "Got %#x (%d) from TEE in response to "
2758 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2759 tee_ret.func, tee_ret.arg2);
2760 return tee_ret;
2761 }
2762 if (ffa_frag_handle(tee_ret) != handle) {
2763 dlog_verbose(
2764 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2765 "in response to FFA_MEM_FRAG_RX for handle "
2766 "%#x.\n",
2767 ffa_frag_handle(tee_ret), handle);
2768 return ffa_error(FFA_INVALID_PARAMETERS);
2769 }
2770 if (ffa_frag_sender(tee_ret) != 0) {
2771 dlog_verbose(
2772 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2773 "(expected 0).\n",
2774 ffa_frag_sender(tee_ret));
2775 return ffa_error(FFA_INVALID_PARAMETERS);
2776 }
2777 fragment_length = tee_ret.arg3;
2778 if (fragment_length > HF_MAILBOX_SIZE ||
2779 fragment_offset + fragment_length > length) {
2780 dlog_verbose(
2781 "Invalid fragment length %d at offset %d (max "
2782 "%d).\n",
2783 fragment_length, fragment_offset,
2784 HF_MAILBOX_SIZE);
2785 return ffa_error(FFA_INVALID_PARAMETERS);
2786 }
2787 memcpy_s(tee_retrieve_buffer + fragment_offset,
2788 sizeof(tee_retrieve_buffer) - fragment_offset,
2789 from_locked.vm->mailbox.send, fragment_length);
2790
2791 fragment_offset += fragment_length;
2792 }
2793
2794 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002795
2796 if (memory_region->receiver_count != 1) {
2797 /* Only one receiver supported by Hafnium for now. */
2798 dlog_verbose(
2799 "Multiple recipients not supported (got %d, expected "
2800 "1).\n",
2801 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002802 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002803 }
2804
2805 if (memory_region->handle != handle) {
2806 dlog_verbose(
2807 "Got memory region handle %#x from TEE but requested "
2808 "handle %#x.\n",
2809 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002810 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002811 }
2812
2813 /* The original sender must match the caller. */
2814 if (to_locked.vm->id != memory_region->sender) {
2815 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002816 "VM %#x attempted to reclaim memory handle %#x "
2817 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002818 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002819 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002820 }
2821
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002822 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002823
2824 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002825 * Validate that the reclaim transition is allowed for the given memory
2826 * region, forward the request to the TEE and then map the memory back
2827 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002828 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002829 return ffa_tee_reclaim_check_update(
2830 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002831 composite->constituent_count, memory_to_attributes,
2832 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002833}