blob: f92957b1b1ac574709280fc3e448f8c1ba0e88d5 [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 */
482 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
483 &current_mode)) {
484 return ffa_error(FFA_DENIED);
485 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100486
Andrew Walbranca808b12020-05-15 17:22:28 +0100487 /*
488 * Ensure that all constituents are mapped with the same
489 * mode.
490 */
491 if (i == 0) {
492 *orig_mode = current_mode;
493 } else if (current_mode != *orig_mode) {
494 dlog_verbose(
495 "Expected mode %#x but was %#x for %d "
496 "pages at %#x.\n",
497 *orig_mode, current_mode,
498 fragments[i][j].page_count,
499 ipa_addr(begin));
500 return ffa_error(FFA_DENIED);
501 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100502 }
Jose Marinho75509b42019-04-09 09:34:59 +0100503 }
504
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000506}
507
508/**
509 * Verify that all pages have the same mode, that the starting mode
510 * constitutes a valid state and obtain the next mode to apply
511 * to the sending VM.
512 *
513 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100514 * 1) FFA_DENIED if a state transition was not found;
515 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100516 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100517 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100518 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
520 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100522static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100523 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100525 struct ffa_memory_region_constituent **fragments,
526 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
527 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528{
529 const uint32_t state_mask =
530 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100531 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100532 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000533
Andrew Walbranca808b12020-05-15 17:22:28 +0100534 ret = constituents_get_mode(from, orig_from_mode, fragments,
535 fragment_constituent_counts,
536 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100538 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100539 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100540 }
541
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000542 /* Ensure the address range is normal memory and not a device. */
543 if (*orig_from_mode & MM_MODE_D) {
544 dlog_verbose("Can't share device memory (mode is %#x).\n",
545 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547 }
548
549 /*
550 * Ensure the sender is the owner and has exclusive access to the
551 * memory.
552 */
553 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100555 }
556
J-Alves7cd5eb32020-10-16 19:06:10 +0100557 required_from_mode =
558 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
559
Andrew Walbrana65a1322020-04-06 19:32:32 +0100560 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
561 dlog_verbose(
562 "Sender tried to send memory with permissions which "
563 "required mode %#x but only had %#x itself.\n",
564 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100565 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566 }
567
568 /* Find the appropriate new mode. */
569 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000570 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100573 break;
574
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000576 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100577 break;
578
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000580 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100581 break;
582
Jose Marinho75509b42019-04-09 09:34:59 +0100583 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100585 }
586
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100587 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000588}
589
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000591 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100592 struct ffa_memory_region_constituent **fragments,
593 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
594 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000595{
596 const uint32_t state_mask =
597 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
598 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000600
Andrew Walbranca808b12020-05-15 17:22:28 +0100601 ret = constituents_get_mode(from, orig_from_mode, fragments,
602 fragment_constituent_counts,
603 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100605 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 }
607
608 /* Ensure the address range is normal memory and not a device. */
609 if (*orig_from_mode & MM_MODE_D) {
610 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
611 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 }
614
615 /*
616 * Ensure the relinquishing VM is not the owner but has access to the
617 * memory.
618 */
619 orig_from_state = *orig_from_mode & state_mask;
620 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
621 dlog_verbose(
622 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100623 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000624 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100625 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 }
627
628 /* Find the appropriate new mode. */
629 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
630
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632}
633
634/**
635 * Verify that all pages have the same mode, that the starting mode
636 * constitutes a valid state and obtain the next mode to apply
637 * to the retrieving VM.
638 *
639 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 * 1) FFA_DENIED if a state transition was not found;
641 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100642 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100644 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100645 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
646 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000647 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000649 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100650 struct ffa_memory_region_constituent **fragments,
651 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
652 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653{
654 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000656
Andrew Walbranca808b12020-05-15 17:22:28 +0100657 ret = constituents_get_mode(to, &orig_to_mode, fragments,
658 fragment_constituent_counts,
659 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100661 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100662 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000663 }
664
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100665 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000666 const uint32_t state_mask =
667 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
668 uint32_t orig_to_state = orig_to_mode & state_mask;
669
670 if (orig_to_state != MM_MODE_INVALID &&
671 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100672 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000673 }
674 } else {
675 /*
676 * Ensure the retriever has the expected state. We don't care
677 * about the MM_MODE_SHARED bit; either with or without it set
678 * are both valid representations of the !O-NA state.
679 */
680 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
681 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100682 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000683 }
684 }
685
686 /* Find the appropriate new mode. */
687 *to_mode = memory_to_attributes;
688 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100689 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000690 *to_mode |= 0;
691 break;
692
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000694 *to_mode |= MM_MODE_UNOWNED;
695 break;
696
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100697 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000698 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
699 break;
700
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000702 *to_mode |= 0;
703 break;
704
705 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100706 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000708 }
709
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100711}
Jose Marinho09b1db82019-08-08 09:16:59 +0100712
713/**
714 * Updates a VM's page table such that the given set of physical address ranges
715 * are mapped in the address space at the corresponding address ranges, in the
716 * mode provided.
717 *
718 * If commit is false, the page tables will be allocated from the mpool but no
719 * mappings will actually be updated. This function must always be called first
720 * with commit false to check that it will succeed before calling with commit
721 * true, to avoid leaving the page table in a half-updated state. To make a
722 * series of changes atomically you can call them all with commit false before
723 * calling them all with commit true.
724 *
725 * mm_vm_defrag should always be called after a series of page table updates,
726 * whether they succeed or fail.
727 *
728 * Returns true on success, or false if the update failed and no changes were
729 * made to memory mappings.
730 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000732 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100733 struct ffa_memory_region_constituent **fragments,
734 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
735 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100736{
Andrew Walbranca808b12020-05-15 17:22:28 +0100737 uint32_t i;
738 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100739
Andrew Walbranca808b12020-05-15 17:22:28 +0100740 /* Iterate over the memory region constituents within each fragment. */
741 for (i = 0; i < fragment_count; ++i) {
742 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
743 size_t size = fragments[i][j].page_count * PAGE_SIZE;
744 paddr_t pa_begin =
745 pa_from_ipa(ipa_init(fragments[i][j].address));
746 paddr_t pa_end = pa_add(pa_begin, size);
747
748 if (commit) {
749 vm_identity_commit(vm_locked, pa_begin, pa_end,
750 mode, ppool, NULL);
751 } else if (!vm_identity_prepare(vm_locked, pa_begin,
752 pa_end, mode, ppool)) {
753 return false;
754 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100755 }
756 }
757
758 return true;
759}
760
761/**
762 * Clears a region of physical memory by overwriting it with zeros. The data is
763 * flushed from the cache so the memory has been cleared across the system.
764 */
765static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
766{
767 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000768 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100769 * global mapping of the whole range. Such an approach will limit
770 * the changes to stage-1 tables and will allow only local
771 * invalidation.
772 */
773 bool ret;
774 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
775 void *ptr =
776 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
777 size_t size = pa_difference(begin, end);
778
779 if (!ptr) {
780 /* TODO: partial defrag of failed range. */
781 /* Recover any memory consumed in failed mapping. */
782 mm_defrag(stage1_locked, ppool);
783 goto fail;
784 }
785
786 memset_s(ptr, size, 0, size);
787 arch_mm_flush_dcache(ptr, size);
788 mm_unmap(stage1_locked, begin, end, ppool);
789
790 ret = true;
791 goto out;
792
793fail:
794 ret = false;
795
796out:
797 mm_unlock_stage1(&stage1_locked);
798
799 return ret;
800}
801
802/**
803 * Clears a region of physical memory by overwriting it with zeros. The data is
804 * flushed from the cache so the memory has been cleared across the system.
805 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100806static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100807 struct ffa_memory_region_constituent **fragments,
808 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
809 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100810{
811 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100812 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100813 struct mm_stage1_locked stage1_locked;
814 bool ret = false;
815
816 /*
817 * Create a local pool so any freed memory can't be used by another
818 * thread. This is to ensure each constituent that is mapped can be
819 * unmapped again afterwards.
820 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000821 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100822
Andrew Walbranca808b12020-05-15 17:22:28 +0100823 /* Iterate over the memory region constituents within each fragment. */
824 for (i = 0; i < fragment_count; ++i) {
825 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100826
Andrew Walbranca808b12020-05-15 17:22:28 +0100827 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
828 size_t size = fragments[i][j].page_count * PAGE_SIZE;
829 paddr_t begin =
830 pa_from_ipa(ipa_init(fragments[i][j].address));
831 paddr_t end = pa_add(begin, size);
832
833 if (!clear_memory(begin, end, &local_page_pool)) {
834 /*
835 * api_clear_memory will defrag on failure, so
836 * no need to do it here.
837 */
838 goto out;
839 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100840 }
841 }
842
843 /*
844 * Need to defrag after clearing, as it may have added extra mappings to
845 * the stage 1 page table.
846 */
847 stage1_locked = mm_lock_stage1();
848 mm_defrag(stage1_locked, &local_page_pool);
849 mm_unlock_stage1(&stage1_locked);
850
851 ret = true;
852
853out:
854 mpool_fini(&local_page_pool);
855 return ret;
856}
857
858/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000859 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100860 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000861 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100862 *
863 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000864 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100865 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100866 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100867 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
868 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100869 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100870 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100872 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100873static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100875 struct ffa_memory_region_constituent **fragments,
876 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
877 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100878 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100879{
Jose Marinho09b1db82019-08-08 09:16:59 +0100880 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100881 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100882 uint32_t orig_from_mode;
883 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100884 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100886
887 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100888 * Make sure constituents are properly aligned to a 64-bit boundary. If
889 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100890 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100891 for (i = 0; i < fragment_count; ++i) {
892 if (!is_aligned(fragments[i], 8)) {
893 dlog_verbose("Constituents not aligned.\n");
894 return ffa_error(FFA_INVALID_PARAMETERS);
895 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 }
897
898 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000899 * Check if the state transition is lawful for the sender, ensure that
900 * all constituents of a memory region being shared are at the same
901 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100904 &orig_from_mode, fragments,
905 fragment_constituent_counts,
906 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100907 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100908 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100909 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 }
911
Andrew Walbran37c574e2020-06-03 11:45:46 +0100912 if (orig_from_mode_ret != NULL) {
913 *orig_from_mode_ret = orig_from_mode;
914 }
915
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 /*
917 * Create a local pool so any freed memory can't be used by another
918 * thread. This is to ensure the original mapping can be restored if the
919 * clear fails.
920 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000921 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100922
923 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924 * First reserve all required memory for the new page table entries
925 * without committing, to make sure the entire operation will succeed
926 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100927 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100928 if (!ffa_region_group_identity_map(
929 from_locked, fragments, fragment_constituent_counts,
930 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100931 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100933 goto out;
934 }
935
936 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 * Update the mapping for the sender. This won't allocate because the
938 * transaction was already prepared above, but may free pages in the
939 * case that a whole block is being unmapped that was previously
940 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100941 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 CHECK(ffa_region_group_identity_map(
943 from_locked, fragments, fragment_constituent_counts,
944 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100945
946 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100947 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100948 fragments, fragment_constituent_counts,
949 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100950 /*
951 * On failure, roll back by returning memory to the sender. This
952 * may allocate pages which were previously freed into
953 * `local_page_pool` by the call above, but will never allocate
954 * more pages than that so can never fail.
955 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100956 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100957 from_locked, fragments, fragment_constituent_counts,
958 fragment_count, orig_from_mode, &local_page_pool,
959 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100960
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100962 goto out;
963 }
964
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966
967out:
968 mpool_fini(&local_page_pool);
969
970 /*
971 * Tidy up the page table by reclaiming failed mappings (if there was an
972 * error) or merging entries into blocks where possible (on success).
973 */
974 mm_vm_defrag(&from->ptable, page_pool);
975
976 return ret;
977}
978
979/**
980 * Validates and maps memory shared from one VM to another.
981 *
982 * This function requires the calling context to hold the <to> lock.
983 *
984 * Returns:
985 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000987 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000989 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100990 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100992static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100994 struct ffa_memory_region_constituent **fragments,
995 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
996 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
997 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000998{
999 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +01001000 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001001 uint32_t to_mode;
1002 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001003 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004
1005 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001006 * Make sure constituents are properly aligned to a 64-bit boundary. If
1007 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001008 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001009 for (i = 0; i < fragment_count; ++i) {
1010 if (!is_aligned(fragments[i], 8)) {
1011 return ffa_error(FFA_INVALID_PARAMETERS);
1012 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 }
1014
1015 /*
1016 * Check if the state transition is lawful for the recipient, and ensure
1017 * that all constituents of the memory region being retrieved are at the
1018 * same state.
1019 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001020 ret = ffa_retrieve_check_transition(
1021 to_locked, share_func, fragments, fragment_constituent_counts,
1022 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001024 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001025 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001026 }
1027
1028 /*
1029 * Create a local pool so any freed memory can't be used by another
1030 * thread. This is to ensure the original mapping can be restored if the
1031 * clear fails.
1032 */
1033 mpool_init_with_fallback(&local_page_pool, page_pool);
1034
1035 /*
1036 * First reserve all required memory for the new page table entries in
1037 * the recipient page tables without committing, to make sure the entire
1038 * operation will succeed without exhausting the page pool.
1039 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001040 if (!ffa_region_group_identity_map(
1041 to_locked, fragments, fragment_constituent_counts,
1042 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 /* TODO: partial defrag of failed range. */
1044 dlog_verbose(
1045 "Insufficient memory to update recipient page "
1046 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001048 goto out;
1049 }
1050
1051 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001053 fragments, fragment_constituent_counts,
1054 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001056 goto out;
1057 }
1058
Jose Marinho09b1db82019-08-08 09:16:59 +01001059 /*
1060 * Complete the transfer by mapping the memory into the recipient. This
1061 * won't allocate because the transaction was already prepared above, so
1062 * it doesn't need to use the `local_page_pool`.
1063 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001064 CHECK(ffa_region_group_identity_map(
1065 to_locked, fragments, fragment_constituent_counts,
1066 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001067
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001068 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001069
1070out:
1071 mpool_fini(&local_page_pool);
1072
1073 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001074 * Tidy up the page table by reclaiming failed mappings (if there was an
1075 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001076 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001077 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001078
1079 return ret;
1080}
1081
Andrew Walbran290b0c92020-02-03 16:37:14 +00001082/**
1083 * Reclaims the given memory from the TEE. To do this space is first reserved in
1084 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1085 * then (if that is successful) the memory is mapped back into the <to> VM's
1086 * page table.
1087 *
1088 * This function requires the calling context to hold the <to> lock.
1089 *
1090 * Returns:
1091 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001092 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001093 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001094 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001095 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001096 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001097 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001098static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001099 struct vm_locked to_locked, ffa_memory_handle_t handle,
1100 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001101 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1102 struct mpool *page_pool)
1103{
1104 struct vm *to = to_locked.vm;
1105 uint32_t to_mode;
1106 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001107 struct ffa_value ret;
1108 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001109
1110 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001111 * Make sure constituents are properly aligned to a 64-bit boundary. If
1112 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001113 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001114 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001115 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001117 }
1118
1119 /*
1120 * Check if the state transition is lawful for the recipient, and ensure
1121 * that all constituents of the memory region being retrieved are at the
1122 * same state.
1123 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001124 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001125 &constituents, &constituent_count,
1126 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001128 dlog_verbose("Invalid transition.\n");
1129 return ret;
1130 }
1131
1132 /*
1133 * Create a local pool so any freed memory can't be used by another
1134 * thread. This is to ensure the original mapping can be restored if the
1135 * clear fails.
1136 */
1137 mpool_init_with_fallback(&local_page_pool, page_pool);
1138
1139 /*
1140 * First reserve all required memory for the new page table entries in
1141 * the recipient page tables without committing, to make sure the entire
1142 * operation will succeed without exhausting the page pool.
1143 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001144 if (!ffa_region_group_identity_map(to_locked, &constituents,
1145 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001146 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001147 /* TODO: partial defrag of failed range. */
1148 dlog_verbose(
1149 "Insufficient memory to update recipient page "
1150 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001151 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001152 goto out;
1153 }
1154
1155 /*
1156 * Forward the request to the TEE and see what happens.
1157 */
1158 tee_flags = 0;
1159 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001161 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001162 ret = arch_other_world_call(
1163 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1164 .arg1 = (uint32_t)handle,
1165 .arg2 = (uint32_t)(handle >> 32),
1166 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001167
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001168 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001169 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001170 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1171 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001172 ret.func, ret.arg2);
1173 goto out;
1174 }
1175
1176 /*
1177 * The TEE was happy with it, so complete the reclaim by mapping the
1178 * memory into the recipient. This won't allocate because the
1179 * transaction was already prepared above, so it doesn't need to use the
1180 * `local_page_pool`.
1181 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001182 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1183 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001185
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001187
1188out:
1189 mpool_fini(&local_page_pool);
1190
1191 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001192 * Tidy up the page table by reclaiming failed mappings (if there was an
1193 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001194 */
1195 mm_vm_defrag(&to->ptable, page_pool);
1196
1197 return ret;
1198}
1199
Andrew Walbran996d1d12020-05-27 14:08:43 +01001200static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001201 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001202 struct ffa_memory_region_constituent **fragments,
1203 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1204 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001205{
1206 uint32_t orig_from_mode;
1207 uint32_t from_mode;
1208 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001210
Andrew Walbranca808b12020-05-15 17:22:28 +01001211 ret = ffa_relinquish_check_transition(
1212 from_locked, &orig_from_mode, fragments,
1213 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001215 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001216 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001217 }
1218
1219 /*
1220 * Create a local pool so any freed memory can't be used by another
1221 * thread. This is to ensure the original mapping can be restored if the
1222 * clear fails.
1223 */
1224 mpool_init_with_fallback(&local_page_pool, page_pool);
1225
1226 /*
1227 * First reserve all required memory for the new page table entries
1228 * without committing, to make sure the entire operation will succeed
1229 * without exhausting the page pool.
1230 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001231 if (!ffa_region_group_identity_map(
1232 from_locked, fragments, fragment_constituent_counts,
1233 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001234 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001235 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001236 goto out;
1237 }
1238
1239 /*
1240 * Update the mapping for the sender. This won't allocate because the
1241 * transaction was already prepared above, but may free pages in the
1242 * case that a whole block is being unmapped that was previously
1243 * partially mapped.
1244 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001245 CHECK(ffa_region_group_identity_map(
1246 from_locked, fragments, fragment_constituent_counts,
1247 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001248
1249 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001251 fragments, fragment_constituent_counts,
1252 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001253 /*
1254 * On failure, roll back by returning memory to the sender. This
1255 * may allocate pages which were previously freed into
1256 * `local_page_pool` by the call above, but will never allocate
1257 * more pages than that so can never fail.
1258 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001259 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001260 from_locked, fragments, fragment_constituent_counts,
1261 fragment_count, orig_from_mode, &local_page_pool,
1262 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001263
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001264 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001265 goto out;
1266 }
1267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001268 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001269
1270out:
1271 mpool_fini(&local_page_pool);
1272
1273 /*
1274 * Tidy up the page table by reclaiming failed mappings (if there was an
1275 * error) or merging entries into blocks where possible (on success).
1276 */
1277 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001278
1279 return ret;
1280}
1281
1282/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001283 * Complete a memory sending operation by checking that it is valid, updating
1284 * the sender page table, and then either marking the share state as having
1285 * completed sending (on success) or freeing it (on failure).
1286 *
1287 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1288 */
1289static struct ffa_value ffa_memory_send_complete(
1290 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001291 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1292 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001293{
1294 struct ffa_memory_region *memory_region = share_state->memory_region;
1295 struct ffa_value ret;
1296
1297 /* Lock must be held. */
1298 CHECK(share_states.share_states != NULL);
1299
1300 /* Check that state is valid in sender page table and update. */
1301 ret = ffa_send_check_update(
1302 from_locked, share_state->fragments,
1303 share_state->fragment_constituent_counts,
1304 share_state->fragment_count, share_state->share_func,
1305 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001306 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1307 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001308 if (ret.func != FFA_SUCCESS_32) {
1309 /*
1310 * Free share state, it failed to send so it can't be retrieved.
1311 */
1312 dlog_verbose("Complete failed, freeing share state.\n");
1313 share_state_free(share_states, share_state, page_pool);
1314 return ret;
1315 }
1316
1317 share_state->sending_complete = true;
1318 dlog_verbose("Marked sending complete.\n");
1319
J-Alvesee68c542020-10-29 17:48:20 +00001320 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001321}
1322
1323/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001324 * Check that the given `memory_region` represents a valid memory send request
1325 * of the given `share_func` type, return the clear flag and permissions via the
1326 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001327 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001328 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001329 * not.
1330 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001331static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001332 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1333 uint32_t memory_share_length, uint32_t fragment_length,
1334 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001335{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001336 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001337 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001338 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001339 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340 enum ffa_data_access data_access;
1341 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001342
Andrew Walbrana65a1322020-04-06 19:32:32 +01001343 CHECK(permissions != NULL);
1344
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001345 /*
1346 * This should already be checked by the caller, just making the
1347 * assumption clear here.
1348 */
1349 CHECK(memory_region->receiver_count == 1);
1350
Andrew Walbrana65a1322020-04-06 19:32:32 +01001351 /* The sender must match the message sender. */
1352 if (memory_region->sender != from_locked.vm->id) {
1353 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001355 }
1356
Andrew Walbrana65a1322020-04-06 19:32:32 +01001357 /*
1358 * Ensure that the composite header is within the memory bounds and
1359 * doesn't overlap the first part of the message.
1360 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001361 receivers_length = sizeof(struct ffa_memory_access) *
1362 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001363 constituents_offset =
1364 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001365 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001366 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001367 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001368 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001369 "Invalid composite memory region descriptor offset "
1370 "%d.\n",
1371 memory_region->receivers[0]
1372 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374 }
1375
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001376 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001377
1378 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001379 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001381 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1382 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001383 if (memory_share_length != constituents_offset + constituents_length) {
1384 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001385 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001386 memory_region->receivers[0]
1387 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001390 if (fragment_length < memory_share_length &&
1391 fragment_length < HF_MAILBOX_SIZE) {
1392 dlog_warning(
1393 "Initial fragment length %d smaller than mailbox "
1394 "size.\n",
1395 fragment_length);
1396 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397
Andrew Walbrana65a1322020-04-06 19:32:32 +01001398 /*
1399 * Clear is not allowed for memory sharing, as the sender still has
1400 * access to the memory.
1401 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001402 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1403 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001404 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001405 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001406 }
1407
1408 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 }
1413
1414 /* Check that the permissions are valid. */
1415 *permissions =
1416 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001417 data_access = ffa_get_data_access_attr(*permissions);
1418 instruction_access = ffa_get_instruction_access_attr(*permissions);
1419 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1420 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1422 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001423 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001424 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001426 dlog_verbose(
1427 "Invalid instruction access permissions %#x for "
1428 "sending memory.\n",
1429 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001430 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001431 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 if (share_func == FFA_MEM_SHARE_32) {
1433 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 dlog_verbose(
1435 "Invalid data access permissions %#x for "
1436 "sharing memory.\n",
1437 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 }
1440 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001441 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1442 * required for share operations (but must not be specified by
1443 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444 * returned to the retriever.
1445 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 ffa_set_instruction_access_attr(permissions,
1447 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448 memory_region->receivers[0].receiver_permissions.permissions =
1449 *permissions;
1450 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001451 if (share_func == FFA_MEM_LEND_32 &&
1452 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001453 dlog_verbose(
1454 "Invalid data access permissions %#x for lending "
1455 "memory.\n",
1456 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001457 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001458 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 if (share_func == FFA_MEM_DONATE_32 &&
1460 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001461 dlog_verbose(
1462 "Invalid data access permissions %#x for donating "
1463 "memory.\n",
1464 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001465 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001466 }
1467
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001468 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001469}
1470
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001471/** Forwards a memory send message on to the TEE. */
1472static struct ffa_value memory_send_tee_forward(
1473 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1474 uint32_t share_func, struct ffa_memory_region *memory_region,
1475 uint32_t memory_share_length, uint32_t fragment_length)
1476{
1477 struct ffa_value ret;
1478
1479 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1480 memory_region, fragment_length);
1481 tee_locked.vm->mailbox.recv_size = fragment_length;
1482 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1483 tee_locked.vm->mailbox.recv_func = share_func;
1484 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001485 ret = arch_other_world_call(
1486 (struct ffa_value){.func = share_func,
1487 .arg1 = memory_share_length,
1488 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001489 /*
1490 * After the call to the TEE completes it must have finished reading its
1491 * RX buffer, so it is ready for another message.
1492 */
1493 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1494
1495 return ret;
1496}
1497
Andrew Walbrana65a1322020-04-06 19:32:32 +01001498/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001499 * Gets the share state for continuing an operation to donate, lend or share
1500 * memory, and checks that it is a valid request.
1501 *
1502 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1503 * not.
1504 */
1505static struct ffa_value ffa_memory_send_continue_validate(
1506 struct share_states_locked share_states, ffa_memory_handle_t handle,
1507 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1508 struct mpool *page_pool)
1509{
1510 struct ffa_memory_share_state *share_state;
1511 struct ffa_memory_region *memory_region;
1512
1513 CHECK(share_state_ret != NULL);
1514
1515 /*
1516 * Look up the share state by handle and make sure that the VM ID
1517 * matches.
1518 */
1519 if (!get_share_state(share_states, handle, &share_state)) {
1520 dlog_verbose(
1521 "Invalid handle %#x for memory send continuation.\n",
1522 handle);
1523 return ffa_error(FFA_INVALID_PARAMETERS);
1524 }
1525 memory_region = share_state->memory_region;
1526
1527 if (memory_region->sender != from_vm_id) {
1528 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1529 return ffa_error(FFA_INVALID_PARAMETERS);
1530 }
1531
1532 if (share_state->sending_complete) {
1533 dlog_verbose(
1534 "Sending of memory handle %#x is already complete.\n",
1535 handle);
1536 return ffa_error(FFA_INVALID_PARAMETERS);
1537 }
1538
1539 if (share_state->fragment_count == MAX_FRAGMENTS) {
1540 /*
1541 * Log a warning as this is a sign that MAX_FRAGMENTS should
1542 * probably be increased.
1543 */
1544 dlog_warning(
1545 "Too many fragments for memory share with handle %#x; "
1546 "only %d supported.\n",
1547 handle, MAX_FRAGMENTS);
1548 /* Free share state, as it's not possible to complete it. */
1549 share_state_free(share_states, share_state, page_pool);
1550 return ffa_error(FFA_NO_MEMORY);
1551 }
1552
1553 *share_state_ret = share_state;
1554
1555 return (struct ffa_value){.func = FFA_SUCCESS_32};
1556}
1557
1558/**
1559 * Forwards a memory send continuation message on to the TEE.
1560 */
1561static struct ffa_value memory_send_continue_tee_forward(
1562 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1563 uint32_t fragment_length, ffa_memory_handle_t handle)
1564{
1565 struct ffa_value ret;
1566
1567 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1568 fragment_length);
1569 tee_locked.vm->mailbox.recv_size = fragment_length;
1570 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1571 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1572 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001573 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001574 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1575 .arg1 = (uint32_t)handle,
1576 .arg2 = (uint32_t)(handle >> 32),
1577 .arg3 = fragment_length,
1578 .arg4 = (uint64_t)sender_vm_id << 16});
1579 /*
1580 * After the call to the TEE completes it must have finished reading its
1581 * RX buffer, so it is ready for another message.
1582 */
1583 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1584
1585 return ret;
1586}
1587
1588/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001589 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1590 * updates the stage-2 page tables. Specifically, check if the message length
1591 * and number of memory region constituents match, and if the transition is
1592 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001593 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001594 * Assumes that the caller has already found and locked the sender VM and copied
1595 * the memory region descriptor from the sender's TX buffer to a freshly
1596 * allocated page from Hafnium's internal pool. The caller must have also
1597 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001598 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001599 * This function takes ownership of the `memory_region` passed in and will free
1600 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001601 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001602struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001603 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001604 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001605 uint32_t fragment_length, uint32_t share_func,
1606 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001607{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001608 ffa_memory_access_permissions_t permissions;
1609 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001610 struct share_states_locked share_states;
1611 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001612
1613 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001614 * If there is an error validating the `memory_region` then we need to
1615 * free it because we own it but we won't be storing it in a share state
1616 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001617 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001618 ret = ffa_memory_send_validate(from_locked, memory_region,
1619 memory_share_length, fragment_length,
1620 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001621 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001622 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001623 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001624 }
1625
Andrew Walbrana65a1322020-04-06 19:32:32 +01001626 /* Set flag for share function, ready to be retrieved later. */
1627 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001628 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001629 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001630 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001631 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001632 case FFA_MEM_LEND_32:
1633 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001634 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001635 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001636 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001638 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001639 }
1640
Andrew Walbranca808b12020-05-15 17:22:28 +01001641 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001642 /*
1643 * Allocate a share state before updating the page table. Otherwise if
1644 * updating the page table succeeded but allocating the share state
1645 * failed then it would leave the memory in a state where nobody could
1646 * get it back.
1647 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001648 if (!allocate_share_state(share_states, share_func, memory_region,
1649 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1650 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001651 dlog_verbose("Failed to allocate share state.\n");
1652 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001653 ret = ffa_error(FFA_NO_MEMORY);
1654 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 }
1656
Andrew Walbranca808b12020-05-15 17:22:28 +01001657 if (fragment_length == memory_share_length) {
1658 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001659 ret = ffa_memory_send_complete(
1660 from_locked, share_states, share_state, page_pool,
1661 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001662 } else {
1663 ret = (struct ffa_value){
1664 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001665 .arg1 = (uint32_t)memory_region->handle,
1666 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001667 .arg3 = fragment_length};
1668 }
1669
1670out:
1671 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001672 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001673 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001674}
1675
1676/**
1677 * Validates a call to donate, lend or share memory to the TEE and then updates
1678 * the stage-2 page tables. Specifically, check if the message length and number
1679 * of memory region constituents match, and if the transition is valid for the
1680 * type of memory sending operation.
1681 *
1682 * Assumes that the caller has already found and locked the sender VM and the
1683 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1684 * to a freshly allocated page from Hafnium's internal pool. The caller must
1685 * have also validated that the receiver VM ID is valid.
1686 *
1687 * This function takes ownership of the `memory_region` passed in and will free
1688 * it when necessary; it must not be freed by the caller.
1689 */
1690struct ffa_value ffa_memory_tee_send(
1691 struct vm_locked from_locked, struct vm_locked to_locked,
1692 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1693 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1694{
1695 ffa_memory_access_permissions_t permissions;
1696 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001697
1698 /*
1699 * If there is an error validating the `memory_region` then we need to
1700 * free it because we own it but we won't be storing it in a share state
1701 * after all.
1702 */
1703 ret = ffa_memory_send_validate(from_locked, memory_region,
1704 memory_share_length, fragment_length,
1705 share_func, &permissions);
1706 if (ret.func != FFA_SUCCESS_32) {
1707 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001708 }
1709
Andrew Walbranca808b12020-05-15 17:22:28 +01001710 if (fragment_length == memory_share_length) {
1711 /* No more fragments to come, everything fit in one message. */
1712 struct ffa_composite_memory_region *composite =
1713 ffa_memory_region_get_composite(memory_region, 0);
1714 struct ffa_memory_region_constituent *constituents =
1715 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001716 struct mpool local_page_pool;
1717 uint32_t orig_from_mode;
1718
1719 /*
1720 * Use a local page pool so that we can roll back if necessary.
1721 */
1722 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001723
1724 ret = ffa_send_check_update(
1725 from_locked, &constituents,
1726 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001727 permissions, &local_page_pool,
1728 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1729 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001730 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001731 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001732 goto out;
1733 }
1734
1735 /* Forward memory send message on to TEE. */
1736 ret = memory_send_tee_forward(
1737 to_locked, from_locked.vm->id, share_func,
1738 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001739
1740 if (ret.func != FFA_SUCCESS_32) {
1741 dlog_verbose(
1742 "TEE didn't successfully complete memory send "
1743 "operation; returned %#x (%d). Rolling back.\n",
1744 ret.func, ret.arg2);
1745
1746 /*
1747 * The TEE failed to complete the send operation, so
1748 * roll back the page table update for the VM. This
1749 * can't fail because it won't try to allocate more
1750 * memory than was freed into the `local_page_pool` by
1751 * `ffa_send_check_update` in the initial update.
1752 */
1753 CHECK(ffa_region_group_identity_map(
1754 from_locked, &constituents,
1755 &composite->constituent_count, 1,
1756 orig_from_mode, &local_page_pool, true));
1757 }
1758
1759 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001760 } else {
1761 struct share_states_locked share_states = share_states_lock();
1762 ffa_memory_handle_t handle;
1763
1764 /*
1765 * We need to wait for the rest of the fragments before we can
1766 * check whether the transaction is valid and unmap the memory.
1767 * Call the TEE so it can do its initial validation and assign a
1768 * handle, and allocate a share state to keep what we have so
1769 * far.
1770 */
1771 ret = memory_send_tee_forward(
1772 to_locked, from_locked.vm->id, share_func,
1773 memory_region, memory_share_length, fragment_length);
1774 if (ret.func == FFA_ERROR_32) {
1775 goto out_unlock;
1776 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1777 dlog_warning(
1778 "Got %#x from TEE in response to %#x for "
1779 "fragment with with %d/%d, expected "
1780 "FFA_MEM_FRAG_RX.\n",
1781 ret.func, share_func, fragment_length,
1782 memory_share_length);
1783 ret = ffa_error(FFA_INVALID_PARAMETERS);
1784 goto out_unlock;
1785 }
1786 handle = ffa_frag_handle(ret);
1787 if (ret.arg3 != fragment_length) {
1788 dlog_warning(
1789 "Got unexpected fragment offset %d for "
1790 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1791 ret.arg3, fragment_length);
1792 ret = ffa_error(FFA_INVALID_PARAMETERS);
1793 goto out_unlock;
1794 }
1795 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1796 dlog_warning(
1797 "Got unexpected sender ID %d for "
1798 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1799 ffa_frag_sender(ret), from_locked.vm->id);
1800 ret = ffa_error(FFA_INVALID_PARAMETERS);
1801 goto out_unlock;
1802 }
1803
1804 if (!allocate_share_state(share_states, share_func,
1805 memory_region, fragment_length,
1806 handle, NULL)) {
1807 dlog_verbose("Failed to allocate share state.\n");
1808 ret = ffa_error(FFA_NO_MEMORY);
1809 goto out_unlock;
1810 }
1811 /*
1812 * Don't free the memory region fragment, as it has been stored
1813 * in the share state.
1814 */
1815 memory_region = NULL;
1816 out_unlock:
1817 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001818 }
1819
Andrew Walbranca808b12020-05-15 17:22:28 +01001820out:
1821 if (memory_region != NULL) {
1822 mpool_free(page_pool, memory_region);
1823 }
1824 dump_share_states();
1825 return ret;
1826}
1827
1828/**
1829 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1830 * this is the last fragment then checks that the transition is valid for the
1831 * type of memory sending operation and updates the stage-2 page tables of the
1832 * sender.
1833 *
1834 * Assumes that the caller has already found and locked the sender VM and copied
1835 * the memory region descriptor from the sender's TX buffer to a freshly
1836 * allocated page from Hafnium's internal pool.
1837 *
1838 * This function takes ownership of the `fragment` passed in; it must not be
1839 * freed by the caller.
1840 */
1841struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1842 void *fragment,
1843 uint32_t fragment_length,
1844 ffa_memory_handle_t handle,
1845 struct mpool *page_pool)
1846{
1847 struct share_states_locked share_states = share_states_lock();
1848 struct ffa_memory_share_state *share_state;
1849 struct ffa_value ret;
1850 struct ffa_memory_region *memory_region;
1851
1852 ret = ffa_memory_send_continue_validate(share_states, handle,
1853 &share_state,
1854 from_locked.vm->id, page_pool);
1855 if (ret.func != FFA_SUCCESS_32) {
1856 goto out_free_fragment;
1857 }
1858 memory_region = share_state->memory_region;
1859
1860 if (memory_region->receivers[0].receiver_permissions.receiver ==
1861 HF_TEE_VM_ID) {
1862 dlog_error(
1863 "Got hypervisor-allocated handle for memory send to "
1864 "TEE. This should never happen, and indicates a bug in "
1865 "EL3 code.\n");
1866 ret = ffa_error(FFA_INVALID_PARAMETERS);
1867 goto out_free_fragment;
1868 }
1869
1870 /* Add this fragment. */
1871 share_state->fragments[share_state->fragment_count] = fragment;
1872 share_state->fragment_constituent_counts[share_state->fragment_count] =
1873 fragment_length / sizeof(struct ffa_memory_region_constituent);
1874 share_state->fragment_count++;
1875
1876 /* Check whether the memory send operation is now ready to complete. */
1877 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001878 ret = ffa_memory_send_complete(
1879 from_locked, share_states, share_state, page_pool,
1880 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001881 } else {
1882 ret = (struct ffa_value){
1883 .func = FFA_MEM_FRAG_RX_32,
1884 .arg1 = (uint32_t)handle,
1885 .arg2 = (uint32_t)(handle >> 32),
1886 .arg3 = share_state_next_fragment_offset(share_states,
1887 share_state)};
1888 }
1889 goto out;
1890
1891out_free_fragment:
1892 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001893
1894out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001895 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001896 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001897}
1898
Andrew Walbranca808b12020-05-15 17:22:28 +01001899/**
1900 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1901 * is the last fragment then checks that the transition is valid for the type of
1902 * memory sending operation and updates the stage-2 page tables of the sender.
1903 *
1904 * Assumes that the caller has already found and locked the sender VM and copied
1905 * the memory region descriptor from the sender's TX buffer to a freshly
1906 * allocated page from Hafnium's internal pool.
1907 *
1908 * This function takes ownership of the `memory_region` passed in and will free
1909 * it when necessary; it must not be freed by the caller.
1910 */
1911struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1912 struct vm_locked to_locked,
1913 void *fragment,
1914 uint32_t fragment_length,
1915 ffa_memory_handle_t handle,
1916 struct mpool *page_pool)
1917{
1918 struct share_states_locked share_states = share_states_lock();
1919 struct ffa_memory_share_state *share_state;
1920 struct ffa_value ret;
1921 struct ffa_memory_region *memory_region;
1922
1923 ret = ffa_memory_send_continue_validate(share_states, handle,
1924 &share_state,
1925 from_locked.vm->id, page_pool);
1926 if (ret.func != FFA_SUCCESS_32) {
1927 goto out_free_fragment;
1928 }
1929 memory_region = share_state->memory_region;
1930
1931 if (memory_region->receivers[0].receiver_permissions.receiver !=
1932 HF_TEE_VM_ID) {
1933 dlog_error(
1934 "Got SPM-allocated handle for memory send to non-TEE "
1935 "VM. This should never happen, and indicates a bug.\n");
1936 ret = ffa_error(FFA_INVALID_PARAMETERS);
1937 goto out_free_fragment;
1938 }
1939
1940 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1941 to_locked.vm->mailbox.recv == NULL) {
1942 /*
1943 * If the TEE RX buffer is not available, tell the sender to
1944 * retry by returning the current offset again.
1945 */
1946 ret = (struct ffa_value){
1947 .func = FFA_MEM_FRAG_RX_32,
1948 .arg1 = (uint32_t)handle,
1949 .arg2 = (uint32_t)(handle >> 32),
1950 .arg3 = share_state_next_fragment_offset(share_states,
1951 share_state),
1952 };
1953 goto out_free_fragment;
1954 }
1955
1956 /* Add this fragment. */
1957 share_state->fragments[share_state->fragment_count] = fragment;
1958 share_state->fragment_constituent_counts[share_state->fragment_count] =
1959 fragment_length / sizeof(struct ffa_memory_region_constituent);
1960 share_state->fragment_count++;
1961
1962 /* Check whether the memory send operation is now ready to complete. */
1963 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001964 struct mpool local_page_pool;
1965 uint32_t orig_from_mode;
1966
1967 /*
1968 * Use a local page pool so that we can roll back if necessary.
1969 */
1970 mpool_init_with_fallback(&local_page_pool, page_pool);
1971
Andrew Walbranca808b12020-05-15 17:22:28 +01001972 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001973 share_state, &local_page_pool,
1974 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001975
1976 if (ret.func == FFA_SUCCESS_32) {
1977 /*
1978 * Forward final fragment on to the TEE so that
1979 * it can complete the memory sending operation.
1980 */
1981 ret = memory_send_continue_tee_forward(
1982 to_locked, from_locked.vm->id, fragment,
1983 fragment_length, handle);
1984
1985 if (ret.func != FFA_SUCCESS_32) {
1986 /*
1987 * The error will be passed on to the caller,
1988 * but log it here too.
1989 */
1990 dlog_verbose(
1991 "TEE didn't successfully complete "
1992 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001993 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001994 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001995
1996 /*
1997 * The TEE failed to complete the send
1998 * operation, so roll back the page table update
1999 * for the VM. This can't fail because it won't
2000 * try to allocate more memory than was freed
2001 * into the `local_page_pool` by
2002 * `ffa_send_check_update` in the initial
2003 * update.
2004 */
2005 CHECK(ffa_region_group_identity_map(
2006 from_locked, share_state->fragments,
2007 share_state
2008 ->fragment_constituent_counts,
2009 share_state->fragment_count,
2010 orig_from_mode, &local_page_pool,
2011 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002012 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002013
Andrew Walbranca808b12020-05-15 17:22:28 +01002014 /* Free share state. */
2015 share_state_free(share_states, share_state, page_pool);
2016 } else {
2017 /* Abort sending to TEE. */
2018 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002019 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002020 .func = FFA_MEM_RECLAIM_32,
2021 .arg1 = (uint32_t)handle,
2022 .arg2 = (uint32_t)(handle >> 32)});
2023
2024 if (tee_ret.func != FFA_SUCCESS_32) {
2025 /*
2026 * Nothing we can do if TEE doesn't abort
2027 * properly, just log it.
2028 */
2029 dlog_verbose(
2030 "TEE didn't successfully abort failed "
2031 "memory send operation; returned %#x "
2032 "(%d).\n",
2033 tee_ret.func, tee_ret.arg2);
2034 }
2035 /*
2036 * We don't need to free the share state in this case
2037 * because ffa_memory_send_complete does that already.
2038 */
2039 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002040
2041 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002042 } else {
2043 uint32_t next_fragment_offset =
2044 share_state_next_fragment_offset(share_states,
2045 share_state);
2046
2047 ret = memory_send_continue_tee_forward(
2048 to_locked, from_locked.vm->id, fragment,
2049 fragment_length, handle);
2050
2051 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2052 ffa_frag_handle(ret) != handle ||
2053 ret.arg3 != next_fragment_offset ||
2054 ffa_frag_sender(ret) != from_locked.vm->id) {
2055 dlog_verbose(
2056 "Got unexpected result from forwarding "
2057 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2058 "offset %d, sender %d); expected "
2059 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2060 "sender %d).\n",
2061 ret.func, ffa_frag_handle(ret), ret.arg3,
2062 ffa_frag_sender(ret), handle,
2063 next_fragment_offset, from_locked.vm->id);
2064 /* Free share state. */
2065 share_state_free(share_states, share_state, page_pool);
2066 ret = ffa_error(FFA_INVALID_PARAMETERS);
2067 goto out;
2068 }
2069
2070 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2071 .arg1 = (uint32_t)handle,
2072 .arg2 = (uint32_t)(handle >> 32),
2073 .arg3 = next_fragment_offset};
2074 }
2075 goto out;
2076
2077out_free_fragment:
2078 mpool_free(page_pool, fragment);
2079
2080out:
2081 share_states_unlock(&share_states);
2082 return ret;
2083}
2084
2085/** Clean up after the receiver has finished retrieving a memory region. */
2086static void ffa_memory_retrieve_complete(
2087 struct share_states_locked share_states,
2088 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2089{
2090 if (share_state->share_func == FFA_MEM_DONATE_32) {
2091 /*
2092 * Memory that has been donated can't be relinquished,
2093 * so no need to keep the share state around.
2094 */
2095 share_state_free(share_states, share_state, page_pool);
2096 dlog_verbose("Freed share state for donate.\n");
2097 }
2098}
2099
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002100struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2101 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002102 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002103 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002104{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002105 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002106 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002107 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002108 sizeof(struct ffa_memory_access);
2109 ffa_memory_handle_t handle = retrieve_request->handle;
2110 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002111 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002112 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2113 struct ffa_memory_region *memory_region;
2114 ffa_memory_access_permissions_t sent_permissions;
2115 enum ffa_data_access sent_data_access;
2116 enum ffa_instruction_access sent_instruction_access;
2117 ffa_memory_access_permissions_t requested_permissions;
2118 enum ffa_data_access requested_data_access;
2119 enum ffa_instruction_access requested_instruction_access;
2120 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002122 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002123 struct ffa_memory_share_state *share_state;
2124 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002125 struct ffa_composite_memory_region *composite;
2126 uint32_t total_length;
2127 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002128
2129 dump_share_states();
2130
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002131 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002132 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002133 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002134 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002135 expected_retrieve_request_length,
2136 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002137 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002138 }
2139
Andrew Walbrana65a1322020-04-06 19:32:32 +01002140 if (retrieve_request->receiver_count != 1) {
2141 dlog_verbose(
2142 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002143 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002144 "expected 1).\n",
2145 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002146 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002147 }
2148
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002149 share_states = share_states_lock();
2150 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002151 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002152 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002153 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002154 goto out;
2155 }
2156
Andrew Walbrana65a1322020-04-06 19:32:32 +01002157 memory_region = share_state->memory_region;
2158 CHECK(memory_region != NULL);
2159
2160 /*
2161 * Check that the transaction type expected by the receiver is correct,
2162 * if it has been specified.
2163 */
2164 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002165 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002166 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002167 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002168 dlog_verbose(
2169 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002170 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002171 transaction_type,
2172 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002173 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002174 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002175 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002176 goto out;
2177 }
2178
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002179 if (retrieve_request->sender != memory_region->sender) {
2180 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002181 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002182 "expected %d for handle %#x.\n",
2183 retrieve_request->sender, memory_region->sender,
2184 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002185 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002186 goto out;
2187 }
2188
2189 if (retrieve_request->tag != memory_region->tag) {
2190 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002191 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002192 "%d for handle %#x.\n",
2193 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002194 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002195 goto out;
2196 }
2197
Andrew Walbrana65a1322020-04-06 19:32:32 +01002198 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2199 to_locked.vm->id) {
2200 dlog_verbose(
2201 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002202 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002203 retrieve_request->receivers[0]
2204 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002205 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002206 goto out;
2207 }
2208
2209 if (memory_region->receivers[0].receiver_permissions.receiver !=
2210 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002211 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002212 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2213 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002214 to_locked.vm->id,
2215 memory_region->receivers[0]
2216 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002217 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002218 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002219 goto out;
2220 }
2221
Andrew Walbranca808b12020-05-15 17:22:28 +01002222 if (!share_state->sending_complete) {
2223 dlog_verbose(
2224 "Memory with handle %#x not fully sent, can't "
2225 "retrieve.\n",
2226 handle);
2227 ret = ffa_error(FFA_INVALID_PARAMETERS);
2228 goto out;
2229 }
2230
2231 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002232 dlog_verbose("Memory with handle %#x already retrieved.\n",
2233 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002234 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002235 goto out;
2236 }
2237
Andrew Walbrana65a1322020-04-06 19:32:32 +01002238 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2239 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002240 dlog_verbose(
2241 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002242 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002243 retrieve_request->receivers[0]
2244 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002245 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002246 goto out;
2247 }
2248
Andrew Walbrana65a1322020-04-06 19:32:32 +01002249 /*
2250 * Check permissions from sender against permissions requested by
2251 * receiver.
2252 */
2253 /* TODO: Check attributes too. */
2254 sent_permissions =
2255 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002256 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002257 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002258 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002259 requested_permissions =
2260 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002261 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002262 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002263 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002264 permissions = 0;
2265 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002266 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2267 case FFA_DATA_ACCESS_RW:
2268 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2269 requested_data_access == FFA_DATA_ACCESS_RW) {
2270 ffa_set_data_access_attr(&permissions,
2271 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002272 break;
2273 }
2274 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002275 case FFA_DATA_ACCESS_RO:
2276 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2277 requested_data_access == FFA_DATA_ACCESS_RO) {
2278 ffa_set_data_access_attr(&permissions,
2279 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002280 break;
2281 }
2282 dlog_verbose(
2283 "Invalid data access requested; sender specified "
2284 "permissions %#x but receiver requested %#x.\n",
2285 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002286 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002287 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002288 case FFA_DATA_ACCESS_RESERVED:
2289 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002290 "checked before this point.");
2291 }
2292 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002293 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2294 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002295 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002296 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2297 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2298 ffa_set_instruction_access_attr(
2299 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002300 break;
2301 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002302 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002303 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002304 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2305 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2306 ffa_set_instruction_access_attr(
2307 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002308 break;
2309 }
2310 dlog_verbose(
2311 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002312 "specified permissions %#x but receiver requested "
2313 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002314 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002315 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002316 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002317 case FFA_INSTRUCTION_ACCESS_RESERVED:
2318 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002319 "be checked before this point.");
2320 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002321 memory_to_attributes = ffa_memory_permissions_to_mode(
2322 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002323 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002324 to_locked, share_state->fragments,
2325 share_state->fragment_constituent_counts,
2326 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002327 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002328 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 goto out;
2330 }
2331
2332 /*
2333 * Copy response to RX buffer of caller and deliver the message. This
2334 * must be done before the share_state is (possibly) freed.
2335 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002336 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002337 composite = ffa_memory_region_get_composite(memory_region, 0);
2338 /*
2339 * Constituents which we received in the first fragment should always
2340 * fit in the first fragment we are sending, because the header is the
2341 * same size in both cases and we have a fixed message buffer size. So
2342 * `ffa_retrieved_memory_region_init` should never fail.
2343 */
2344 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002345 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2346 memory_region->sender, memory_region->attributes,
2347 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002348 composite->page_count, composite->constituent_count,
2349 share_state->fragments[0],
2350 share_state->fragment_constituent_counts[0], &total_length,
2351 &fragment_length));
2352 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002353 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002354 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002355 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2356
Andrew Walbranca808b12020-05-15 17:22:28 +01002357 share_state->retrieved_fragment_count[0] = 1;
2358 if (share_state->retrieved_fragment_count[0] ==
2359 share_state->fragment_count) {
2360 ffa_memory_retrieve_complete(share_states, share_state,
2361 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002362 }
2363
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002364 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002365 .arg1 = total_length,
2366 .arg2 = fragment_length};
2367
2368out:
2369 share_states_unlock(&share_states);
2370 dump_share_states();
2371 return ret;
2372}
2373
2374struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2375 ffa_memory_handle_t handle,
2376 uint32_t fragment_offset,
2377 struct mpool *page_pool)
2378{
2379 struct ffa_memory_region *memory_region;
2380 struct share_states_locked share_states;
2381 struct ffa_memory_share_state *share_state;
2382 struct ffa_value ret;
2383 uint32_t fragment_index;
2384 uint32_t retrieved_constituents_count;
2385 uint32_t i;
2386 uint32_t expected_fragment_offset;
2387 uint32_t remaining_constituent_count;
2388 uint32_t fragment_length;
2389
2390 dump_share_states();
2391
2392 share_states = share_states_lock();
2393 if (!get_share_state(share_states, handle, &share_state)) {
2394 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2395 handle);
2396 ret = ffa_error(FFA_INVALID_PARAMETERS);
2397 goto out;
2398 }
2399
2400 memory_region = share_state->memory_region;
2401 CHECK(memory_region != NULL);
2402
2403 if (memory_region->receivers[0].receiver_permissions.receiver !=
2404 to_locked.vm->id) {
2405 dlog_verbose(
2406 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2407 "of handle %#x.\n",
2408 to_locked.vm->id,
2409 memory_region->receivers[0]
2410 .receiver_permissions.receiver,
2411 handle);
2412 ret = ffa_error(FFA_INVALID_PARAMETERS);
2413 goto out;
2414 }
2415
2416 if (!share_state->sending_complete) {
2417 dlog_verbose(
2418 "Memory with handle %#x not fully sent, can't "
2419 "retrieve.\n",
2420 handle);
2421 ret = ffa_error(FFA_INVALID_PARAMETERS);
2422 goto out;
2423 }
2424
2425 if (share_state->retrieved_fragment_count[0] == 0 ||
2426 share_state->retrieved_fragment_count[0] >=
2427 share_state->fragment_count) {
2428 dlog_verbose(
2429 "Retrieval of memory with handle %#x not yet started "
2430 "or already completed (%d/%d fragments retrieved).\n",
2431 handle, share_state->retrieved_fragment_count[0],
2432 share_state->fragment_count);
2433 ret = ffa_error(FFA_INVALID_PARAMETERS);
2434 goto out;
2435 }
2436
2437 fragment_index = share_state->retrieved_fragment_count[0];
2438
2439 /*
2440 * Check that the given fragment offset is correct by counting how many
2441 * constituents were in the fragments previously sent.
2442 */
2443 retrieved_constituents_count = 0;
2444 for (i = 0; i < fragment_index; ++i) {
2445 retrieved_constituents_count +=
2446 share_state->fragment_constituent_counts[i];
2447 }
2448 expected_fragment_offset =
2449 ffa_composite_constituent_offset(memory_region, 0) +
2450 retrieved_constituents_count *
2451 sizeof(struct ffa_memory_region_constituent);
2452 if (fragment_offset != expected_fragment_offset) {
2453 dlog_verbose("Fragment offset was %d but expected %d.\n",
2454 fragment_offset, expected_fragment_offset);
2455 ret = ffa_error(FFA_INVALID_PARAMETERS);
2456 goto out;
2457 }
2458
2459 remaining_constituent_count = ffa_memory_fragment_init(
2460 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2461 share_state->fragments[fragment_index],
2462 share_state->fragment_constituent_counts[fragment_index],
2463 &fragment_length);
2464 CHECK(remaining_constituent_count == 0);
2465 to_locked.vm->mailbox.recv_size = fragment_length;
2466 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2467 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2468 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2469 share_state->retrieved_fragment_count[0]++;
2470 if (share_state->retrieved_fragment_count[0] ==
2471 share_state->fragment_count) {
2472 ffa_memory_retrieve_complete(share_states, share_state,
2473 page_pool);
2474 }
2475
2476 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2477 .arg1 = (uint32_t)handle,
2478 .arg2 = (uint32_t)(handle >> 32),
2479 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480
2481out:
2482 share_states_unlock(&share_states);
2483 dump_share_states();
2484 return ret;
2485}
2486
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002487struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002488 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002489 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002490{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002491 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002492 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002493 struct ffa_memory_share_state *share_state;
2494 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002496 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002497
Andrew Walbrana65a1322020-04-06 19:32:32 +01002498 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002499 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002500 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002501 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002503 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002504 }
2505
Andrew Walbrana65a1322020-04-06 19:32:32 +01002506 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507 dlog_verbose(
2508 "VM ID %d in relinquish message doesn't match calling "
2509 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002510 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002511 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002512 }
2513
2514 dump_share_states();
2515
2516 share_states = share_states_lock();
2517 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002520 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521 goto out;
2522 }
2523
Andrew Walbranca808b12020-05-15 17:22:28 +01002524 if (!share_state->sending_complete) {
2525 dlog_verbose(
2526 "Memory with handle %#x not fully sent, can't "
2527 "relinquish.\n",
2528 handle);
2529 ret = ffa_error(FFA_INVALID_PARAMETERS);
2530 goto out;
2531 }
2532
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533 memory_region = share_state->memory_region;
2534 CHECK(memory_region != NULL);
2535
Andrew Walbrana65a1322020-04-06 19:32:32 +01002536 if (memory_region->receivers[0].receiver_permissions.receiver !=
2537 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002538 dlog_verbose(
2539 "VM ID %d tried to relinquish memory region with "
2540 "handle %#x but receiver was %d.\n",
2541 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002542 memory_region->receivers[0]
2543 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002544 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002545 goto out;
2546 }
2547
Andrew Walbranca808b12020-05-15 17:22:28 +01002548 if (share_state->retrieved_fragment_count[0] !=
2549 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002551 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 "relinquish.\n",
2553 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002554 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555 goto out;
2556 }
2557
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002558 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002559
2560 /*
2561 * Clear is not allowed for memory that was shared, as the original
2562 * sender still has access to the memory.
2563 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002564 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002566 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002567 goto out;
2568 }
2569
Andrew Walbranca808b12020-05-15 17:22:28 +01002570 ret = ffa_relinquish_check_update(
2571 from_locked, share_state->fragments,
2572 share_state->fragment_constituent_counts,
2573 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002574
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002575 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002576 /*
2577 * Mark memory handle as not retrieved, so it can be reclaimed
2578 * (or retrieved again).
2579 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002580 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581 }
2582
2583out:
2584 share_states_unlock(&share_states);
2585 dump_share_states();
2586 return ret;
2587}
2588
2589/**
2590 * Validates that the reclaim transition is allowed for the given handle,
2591 * updates the page table of the reclaiming VM, and frees the internal state
2592 * associated with the handle.
2593 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002594struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002595 ffa_memory_handle_t handle,
2596 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002597 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002598{
2599 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002600 struct ffa_memory_share_state *share_state;
2601 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603
2604 dump_share_states();
2605
2606 share_states = share_states_lock();
2607 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002608 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002609 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002610 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002611 goto out;
2612 }
2613
2614 memory_region = share_state->memory_region;
2615 CHECK(memory_region != NULL);
2616
2617 if (to_locked.vm->id != memory_region->sender) {
2618 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002619 "VM %#x attempted to reclaim memory handle %#x "
2620 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002621 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002622 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002623 goto out;
2624 }
2625
Andrew Walbranca808b12020-05-15 17:22:28 +01002626 if (!share_state->sending_complete) {
2627 dlog_verbose(
2628 "Memory with handle %#x not fully sent, can't "
2629 "reclaim.\n",
2630 handle);
2631 ret = ffa_error(FFA_INVALID_PARAMETERS);
2632 goto out;
2633 }
2634
2635 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636 dlog_verbose(
2637 "Tried to reclaim memory handle %#x that has not been "
2638 "relinquished.\n",
2639 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002640 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002641 goto out;
2642 }
2643
Andrew Walbranca808b12020-05-15 17:22:28 +01002644 ret = ffa_retrieve_check_update(
2645 to_locked, share_state->fragments,
2646 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002647 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002648 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002649
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651 share_state_free(share_states, share_state, page_pool);
2652 dlog_verbose("Freed share state after successful reclaim.\n");
2653 }
2654
2655out:
2656 share_states_unlock(&share_states);
2657 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002658}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002659
2660/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002661 * Validates that the reclaim transition is allowed for the memory region with
2662 * the given handle which was previously shared with the TEE, tells the TEE to
2663 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2664 *
2665 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002666 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002667struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002668 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002669 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002670 ffa_memory_region_flags_t flags,
2671 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002672{
Andrew Walbranca808b12020-05-15 17:22:28 +01002673 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2674 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2675 struct ffa_value tee_ret;
2676 uint32_t length;
2677 uint32_t fragment_length;
2678 uint32_t fragment_offset;
2679 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002680 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002681 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2682
2683 CHECK(request_length <= HF_MAILBOX_SIZE);
2684 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2685
2686 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002687 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002688 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2689 .arg1 = request_length,
2690 .arg2 = request_length});
2691 if (tee_ret.func == FFA_ERROR_32) {
2692 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2693 return tee_ret;
2694 }
2695 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2696 dlog_verbose(
2697 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2698 tee_ret.func);
2699 return ffa_error(FFA_INVALID_PARAMETERS);
2700 }
2701
2702 length = tee_ret.arg1;
2703 fragment_length = tee_ret.arg2;
2704
2705 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2706 length > sizeof(tee_retrieve_buffer)) {
2707 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2708 fragment_length, length, HF_MAILBOX_SIZE,
2709 sizeof(tee_retrieve_buffer));
2710 return ffa_error(FFA_INVALID_PARAMETERS);
2711 }
2712
2713 /*
2714 * Copy the first fragment of the memory region descriptor to an
2715 * internal buffer.
2716 */
2717 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2718 from_locked.vm->mailbox.send, fragment_length);
2719
2720 /* Fetch the remaining fragments into the same buffer. */
2721 fragment_offset = fragment_length;
2722 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002723 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002724 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2725 .arg1 = (uint32_t)handle,
2726 .arg2 = (uint32_t)(handle >> 32),
2727 .arg3 = fragment_offset});
2728 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2729 dlog_verbose(
2730 "Got %#x (%d) from TEE in response to "
2731 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2732 tee_ret.func, tee_ret.arg2);
2733 return tee_ret;
2734 }
2735 if (ffa_frag_handle(tee_ret) != handle) {
2736 dlog_verbose(
2737 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2738 "in response to FFA_MEM_FRAG_RX for handle "
2739 "%#x.\n",
2740 ffa_frag_handle(tee_ret), handle);
2741 return ffa_error(FFA_INVALID_PARAMETERS);
2742 }
2743 if (ffa_frag_sender(tee_ret) != 0) {
2744 dlog_verbose(
2745 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2746 "(expected 0).\n",
2747 ffa_frag_sender(tee_ret));
2748 return ffa_error(FFA_INVALID_PARAMETERS);
2749 }
2750 fragment_length = tee_ret.arg3;
2751 if (fragment_length > HF_MAILBOX_SIZE ||
2752 fragment_offset + fragment_length > length) {
2753 dlog_verbose(
2754 "Invalid fragment length %d at offset %d (max "
2755 "%d).\n",
2756 fragment_length, fragment_offset,
2757 HF_MAILBOX_SIZE);
2758 return ffa_error(FFA_INVALID_PARAMETERS);
2759 }
2760 memcpy_s(tee_retrieve_buffer + fragment_offset,
2761 sizeof(tee_retrieve_buffer) - fragment_offset,
2762 from_locked.vm->mailbox.send, fragment_length);
2763
2764 fragment_offset += fragment_length;
2765 }
2766
2767 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002768
2769 if (memory_region->receiver_count != 1) {
2770 /* Only one receiver supported by Hafnium for now. */
2771 dlog_verbose(
2772 "Multiple recipients not supported (got %d, expected "
2773 "1).\n",
2774 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002775 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002776 }
2777
2778 if (memory_region->handle != handle) {
2779 dlog_verbose(
2780 "Got memory region handle %#x from TEE but requested "
2781 "handle %#x.\n",
2782 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002783 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002784 }
2785
2786 /* The original sender must match the caller. */
2787 if (to_locked.vm->id != memory_region->sender) {
2788 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002789 "VM %#x attempted to reclaim memory handle %#x "
2790 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002791 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002792 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002793 }
2794
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002795 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002796
2797 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002798 * Validate that the reclaim transition is allowed for the given memory
2799 * region, forward the request to the TEE and then map the memory back
2800 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002801 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002802 return ffa_tee_reclaim_check_update(
2803 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002804 composite->constituent_count, memory_to_attributes,
2805 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002806}