blob: 661647cd1aceb6b2e552df2dbe7d2a545c0c4f47 [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"
Andrew Walbran290b0c92020-02-03 16:37:14 +000012
Jose Marinho75509b42019-04-09 09:34:59 +010013#include "hf/api.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010014#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010016#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000017#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000019#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000021/** The maximum number of recipients a memory region may be sent to. */
22#define MAX_MEM_SHARE_RECIPIENTS 1
23
24/**
25 * The maximum number of memory sharing handles which may be active at once. A
26 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
27 * or LEND handle is active from when it is sent to when it is reclaimed.
28 */
29#define MAX_MEM_SHARES 100
30
Andrew Walbranca808b12020-05-15 17:22:28 +010031/**
32 * The maximum number of fragments into which a memory sharing message may be
33 * broken.
34 */
35#define MAX_FRAGMENTS 20
36
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
38 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000039 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
41 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000042 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010044 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
46 "struct ffa_memory_access must be a multiple of 16 bytes long.");
47static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
48 "struct ffa_memory_region must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
50 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010053struct ffa_memory_share_state {
Andrew Walbranca808b12020-05-15 17:22:28 +010054 ffa_memory_handle_t handle;
55
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056 /**
57 * The memory region being shared, or NULL if this share state is
58 * unallocated.
59 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010060 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061
Andrew Walbranca808b12020-05-15 17:22:28 +010062 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
63
64 /** The number of constituents in each fragment. */
65 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
66
67 /**
68 * The number of valid elements in the `fragments` and
69 * `fragment_constituent_counts` arrays.
70 */
71 uint32_t fragment_count;
72
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000073 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010074 * The FF-A function used for sharing the memory. Must be one of
75 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000076 * share state is allocated, or 0.
77 */
78 uint32_t share_func;
79
80 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010081 * True if all the fragments of this sharing request have been sent and
82 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000083 */
Andrew Walbranca808b12020-05-15 17:22:28 +010084 bool sending_complete;
85
86 /**
87 * How many fragments of the memory region each recipient has retrieved
88 * so far. The order of this array matches the order of the endpoint
89 * memory access descriptors in the memory region descriptor. Any
90 * entries beyond the receiver_count will always be 0.
91 */
92 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +000093};
94
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000095/**
96 * Encapsulates the set of share states while the `share_states_lock` is held.
97 */
98struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000100};
101
102/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100103 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000104 * by this lock.
105 */
106static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100107static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108
109/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100110 * Buffer for retrieving memory region information from the TEE for when a
111 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
112 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000113 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100114alignas(PAGE_SIZE) static uint8_t
115 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
116
117/**
118 * Initialises the next available `struct ffa_memory_share_state` and sets
119 * `share_state_ret` to a pointer to it. If `handle` is
120 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
121 * uses the provided handle which is assumed to be globally unique.
122 *
123 * Returns true on success or false if none are available.
124 */
125static bool allocate_share_state(
126 struct share_states_locked share_states, uint32_t share_func,
127 struct ffa_memory_region *memory_region, uint32_t fragment_length,
128 ffa_memory_handle_t handle,
129 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000130{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100131 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000132
Andrew Walbranca808b12020-05-15 17:22:28 +0100133 CHECK(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000134 CHECK(memory_region != NULL);
135
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000136 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100137 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000138 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100139 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100140 &share_states.share_states[i];
141 struct ffa_composite_memory_region *composite =
142 ffa_memory_region_get_composite(memory_region,
143 0);
144
145 if (handle == FFA_MEMORY_HANDLE_INVALID) {
146 allocated_state->handle =
147 i |
148 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
149 } else {
150 allocated_state->handle = handle;
151 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152 allocated_state->share_func = share_func;
153 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100154 allocated_state->fragment_count = 1;
155 allocated_state->fragments[0] = composite->constituents;
156 allocated_state->fragment_constituent_counts[0] =
157 (fragment_length -
158 ffa_composite_constituent_offset(memory_region,
159 0)) /
160 sizeof(struct ffa_memory_region_constituent);
161 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000162 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 allocated_state->retrieved_fragment_count[j] =
164 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000165 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100166 if (share_state_ret != NULL) {
167 *share_state_ret = allocated_state;
168 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000169 return true;
170 }
171 }
172
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000173 return false;
174}
175
176/** Locks the share states lock. */
177struct share_states_locked share_states_lock(void)
178{
179 sl_lock(&share_states_lock_instance);
180
181 return (struct share_states_locked){.share_states = share_states};
182}
183
184/** Unlocks the share states lock. */
185static void share_states_unlock(struct share_states_locked *share_states)
186{
187 CHECK(share_states->share_states != NULL);
188 share_states->share_states = NULL;
189 sl_unlock(&share_states_lock_instance);
190}
191
192/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100193 * If the given handle is a valid handle for an allocated share state then
194 * initialises `share_state_ret` to point to the share state and returns true.
195 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000196 */
197static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100198 ffa_memory_handle_t handle,
199 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000200{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100201 struct ffa_memory_share_state *share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100202 uint32_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203
Andrew Walbranca808b12020-05-15 17:22:28 +0100204 CHECK(share_states.share_states != NULL);
205 CHECK(share_state_ret != NULL);
206
207 /*
208 * First look for a share_state allocated by us, in which case the
209 * handle is based on the index.
210 */
211 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
212 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
213 index = handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
214 if (index < MAX_MEM_SHARES) {
215 share_state = &share_states.share_states[index];
216 if (share_state->share_func != 0) {
217 *share_state_ret = share_state;
218 return true;
219 }
220 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000221 }
222
Andrew Walbranca808b12020-05-15 17:22:28 +0100223 /* Fall back to a linear scan. */
224 for (index = 0; index < MAX_MEM_SHARES; ++index) {
225 share_state = &share_states.share_states[index];
226 if (share_state->handle == handle &&
227 share_state->share_func != 0) {
228 *share_state_ret = share_state;
229 return true;
230 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000231 }
232
Andrew Walbranca808b12020-05-15 17:22:28 +0100233 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000234}
235
236/** Marks a share state as unallocated. */
237static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100238 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000239 struct mpool *page_pool)
240{
Andrew Walbranca808b12020-05-15 17:22:28 +0100241 uint32_t i;
242
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000243 CHECK(share_states.share_states != NULL);
244 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100245 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100247 /*
248 * First fragment is part of the same page as the `memory_region`, so it
249 * doesn't need to be freed separately.
250 */
251 share_state->fragments[0] = NULL;
252 share_state->fragment_constituent_counts[0] = 0;
253 for (i = 1; i < share_state->fragment_count; ++i) {
254 mpool_free(page_pool, share_state->fragments[i]);
255 share_state->fragments[i] = NULL;
256 share_state->fragment_constituent_counts[i] = 0;
257 }
258 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000259 share_state->memory_region = NULL;
260}
261
Andrew Walbranca808b12020-05-15 17:22:28 +0100262/** Checks whether the given share state has been fully sent. */
263static bool share_state_sending_complete(
264 struct share_states_locked share_states,
265 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000266{
Andrew Walbranca808b12020-05-15 17:22:28 +0100267 struct ffa_composite_memory_region *composite;
268 uint32_t expected_constituent_count;
269 uint32_t fragment_constituent_count_total = 0;
270 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000271
Andrew Walbranca808b12020-05-15 17:22:28 +0100272 /* Lock must be held. */
273 CHECK(share_states.share_states != NULL);
274
275 /*
276 * Share state must already be valid, or it's not possible to get hold
277 * of it.
278 */
279 CHECK(share_state->memory_region != NULL &&
280 share_state->share_func != 0);
281
282 composite =
283 ffa_memory_region_get_composite(share_state->memory_region, 0);
284 expected_constituent_count = composite->constituent_count;
285 for (i = 0; i < share_state->fragment_count; ++i) {
286 fragment_constituent_count_total +=
287 share_state->fragment_constituent_counts[i];
288 }
289 dlog_verbose(
290 "Checking completion: constituent count %d/%d from %d "
291 "fragments.\n",
292 fragment_constituent_count_total, expected_constituent_count,
293 share_state->fragment_count);
294
295 return fragment_constituent_count_total == expected_constituent_count;
296}
297
298/**
299 * Calculates the offset of the next fragment expected for the given share
300 * state.
301 */
302static uint32_t share_state_next_fragment_offset(
303 struct share_states_locked share_states,
304 struct ffa_memory_share_state *share_state)
305{
306 uint32_t next_fragment_offset;
307 uint32_t i;
308
309 /* Lock must be held. */
310 CHECK(share_states.share_states != NULL);
311
312 next_fragment_offset =
313 ffa_composite_constituent_offset(share_state->memory_region, 0);
314 for (i = 0; i < share_state->fragment_count; ++i) {
315 next_fragment_offset +=
316 share_state->fragment_constituent_counts[i] *
317 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000318 }
319
Andrew Walbranca808b12020-05-15 17:22:28 +0100320 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000321}
322
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000324{
325 uint32_t i;
326
327 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
328 return;
329 }
330
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100331 dlog("from VM %#x, attributes %#x, flags %#x, handle %#x, tag %u, to "
332 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100333 "recipients [",
334 memory_region->sender, memory_region->attributes,
335 memory_region->flags, memory_region->handle, memory_region->tag,
336 memory_region->receiver_count);
337 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338 if (i != 0) {
339 dlog(", ");
340 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100341 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100342 memory_region->receivers[i].receiver_permissions.receiver,
343 memory_region->receivers[i]
344 .receiver_permissions.permissions,
345 memory_region->receivers[i]
346 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000347 }
348 dlog("]");
349}
350
351static void dump_share_states(void)
352{
353 uint32_t i;
354
355 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
356 return;
357 }
358
359 dlog("Current share states:\n");
360 sl_lock(&share_states_lock_instance);
361 for (i = 0; i < MAX_MEM_SHARES; ++i) {
362 if (share_states[i].share_func != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100363 dlog("%#x: ", share_states[i].handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000364 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100365 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000366 dlog("SHARE");
367 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100368 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000369 dlog("LEND");
370 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000372 dlog("DONATE");
373 break;
374 default:
375 dlog("invalid share_func %#x",
376 share_states[i].share_func);
377 }
378 dlog(" (");
379 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100380 if (share_states[i].sending_complete) {
381 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000382 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100383 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100385 dlog(" with %d fragments, %d retrieved\n",
386 share_states[i].fragment_count,
387 share_states[i].retrieved_fragment_count[0]);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000388 }
389 }
390 sl_unlock(&share_states_lock_instance);
391}
392
Andrew Walbran475c1452020-02-07 13:22:22 +0000393/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100394static inline uint32_t ffa_memory_permissions_to_mode(
395 ffa_memory_access_permissions_t permissions)
Andrew Walbran475c1452020-02-07 13:22:22 +0000396{
397 uint32_t mode = 0;
398
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100399 switch (ffa_get_data_access_attr(permissions)) {
400 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000401 mode = MM_MODE_R;
402 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100403 case FFA_DATA_ACCESS_RW:
404 case FFA_DATA_ACCESS_NOT_SPECIFIED:
Andrew Walbran475c1452020-02-07 13:22:22 +0000405 mode = MM_MODE_R | MM_MODE_W;
406 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100407 case FFA_DATA_ACCESS_RESERVED:
408 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100409 }
410
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 switch (ffa_get_instruction_access_attr(permissions)) {
412 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000413 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 case FFA_INSTRUCTION_ACCESS_X:
415 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100416 mode |= MM_MODE_X;
417 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100418 case FFA_INSTRUCTION_ACCESS_RESERVED:
419 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000420 }
421
422 return mode;
423}
424
Jose Marinho75509b42019-04-09 09:34:59 +0100425/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000426 * Get the current mode in the stage-2 page table of the given vm of all the
427 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100429 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000431 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100432 struct ffa_memory_region_constituent **fragments,
433 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100434{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100435 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100436 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100437
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100439 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000440 * Fail if there are no constituents. Otherwise we would get an
441 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100442 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100443 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100444 }
445
Andrew Walbranca808b12020-05-15 17:22:28 +0100446 for (i = 0; i < fragment_count; ++i) {
447 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
448 ipaddr_t begin = ipa_init(fragments[i][j].address);
449 size_t size = fragments[i][j].page_count * PAGE_SIZE;
450 ipaddr_t end = ipa_add(begin, size);
451 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100452
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 /* Fail if addresses are not page-aligned. */
454 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
455 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
456 return ffa_error(FFA_INVALID_PARAMETERS);
457 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100458
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 /*
460 * Ensure that this constituent memory range is all
461 * mapped with the same mode.
462 */
463 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
464 &current_mode)) {
465 return ffa_error(FFA_DENIED);
466 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100467
Andrew Walbranca808b12020-05-15 17:22:28 +0100468 /*
469 * Ensure that all constituents are mapped with the same
470 * mode.
471 */
472 if (i == 0) {
473 *orig_mode = current_mode;
474 } else if (current_mode != *orig_mode) {
475 dlog_verbose(
476 "Expected mode %#x but was %#x for %d "
477 "pages at %#x.\n",
478 *orig_mode, current_mode,
479 fragments[i][j].page_count,
480 ipa_addr(begin));
481 return ffa_error(FFA_DENIED);
482 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100483 }
Jose Marinho75509b42019-04-09 09:34:59 +0100484 }
485
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100486 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000487}
488
489/**
490 * Verify that all pages have the same mode, that the starting mode
491 * constitutes a valid state and obtain the next mode to apply
492 * to the sending VM.
493 *
494 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495 * 1) FFA_DENIED if a state transition was not found;
496 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100497 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100499 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100500 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
501 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000502 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100504 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100506 struct ffa_memory_region_constituent **fragments,
507 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
508 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000509{
510 const uint32_t state_mask =
511 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100512 const uint32_t required_from_mode =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513 ffa_memory_permissions_to_mode(permissions);
514 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000515
Andrew Walbranca808b12020-05-15 17:22:28 +0100516 ret = constituents_get_mode(from, orig_from_mode, fragments,
517 fragment_constituent_counts,
518 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100520 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100521 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100522 }
523
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000524 /* Ensure the address range is normal memory and not a device. */
525 if (*orig_from_mode & MM_MODE_D) {
526 dlog_verbose("Can't share device memory (mode is %#x).\n",
527 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000529 }
530
531 /*
532 * Ensure the sender is the owner and has exclusive access to the
533 * memory.
534 */
535 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100537 }
538
539 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
540 dlog_verbose(
541 "Sender tried to send memory with permissions which "
542 "required mode %#x but only had %#x itself.\n",
543 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 }
546
547 /* Find the appropriate new mode. */
548 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000549 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100550 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000551 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100552 break;
553
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000555 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100556 break;
557
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100558 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000559 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100560 break;
561
Jose Marinho75509b42019-04-09 09:34:59 +0100562 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100563 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100564 }
565
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567}
568
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000570 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100571 struct ffa_memory_region_constituent **fragments,
572 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
573 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000574{
575 const uint32_t state_mask =
576 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
577 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000579
Andrew Walbranca808b12020-05-15 17:22:28 +0100580 ret = constituents_get_mode(from, orig_from_mode, fragments,
581 fragment_constituent_counts,
582 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100584 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000585 }
586
587 /* Ensure the address range is normal memory and not a device. */
588 if (*orig_from_mode & MM_MODE_D) {
589 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
590 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000592 }
593
594 /*
595 * Ensure the relinquishing VM is not the owner but has access to the
596 * memory.
597 */
598 orig_from_state = *orig_from_mode & state_mask;
599 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
600 dlog_verbose(
601 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100602 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000603 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000605 }
606
607 /* Find the appropriate new mode. */
608 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
609
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000611}
612
613/**
614 * Verify that all pages have the same mode, that the starting mode
615 * constitutes a valid state and obtain the next mode to apply
616 * to the retrieving VM.
617 *
618 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100619 * 1) FFA_DENIED if a state transition was not found;
620 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100621 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100622 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100623 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
625 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000628 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100629 struct ffa_memory_region_constituent **fragments,
630 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
631 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632{
633 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100634 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000635
Andrew Walbranca808b12020-05-15 17:22:28 +0100636 ret = constituents_get_mode(to, &orig_to_mode, fragments,
637 fragment_constituent_counts,
638 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100639 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100640 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100641 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000642 }
643
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000645 const uint32_t state_mask =
646 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
647 uint32_t orig_to_state = orig_to_mode & state_mask;
648
649 if (orig_to_state != MM_MODE_INVALID &&
650 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000652 }
653 } else {
654 /*
655 * Ensure the retriever has the expected state. We don't care
656 * about the MM_MODE_SHARED bit; either with or without it set
657 * are both valid representations of the !O-NA state.
658 */
659 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
660 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662 }
663 }
664
665 /* Find the appropriate new mode. */
666 *to_mode = memory_to_attributes;
667 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100668 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669 *to_mode |= 0;
670 break;
671
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100672 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000673 *to_mode |= MM_MODE_UNOWNED;
674 break;
675
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000677 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
678 break;
679
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000681 *to_mode |= 0;
682 break;
683
684 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100685 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000687 }
688
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100689 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100690}
Jose Marinho09b1db82019-08-08 09:16:59 +0100691
692/**
693 * Updates a VM's page table such that the given set of physical address ranges
694 * are mapped in the address space at the corresponding address ranges, in the
695 * mode provided.
696 *
697 * If commit is false, the page tables will be allocated from the mpool but no
698 * mappings will actually be updated. This function must always be called first
699 * with commit false to check that it will succeed before calling with commit
700 * true, to avoid leaving the page table in a half-updated state. To make a
701 * series of changes atomically you can call them all with commit false before
702 * calling them all with commit true.
703 *
704 * mm_vm_defrag should always be called after a series of page table updates,
705 * whether they succeed or fail.
706 *
707 * Returns true on success, or false if the update failed and no changes were
708 * made to memory mappings.
709 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000711 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100712 struct ffa_memory_region_constituent **fragments,
713 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
714 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100715{
Andrew Walbranca808b12020-05-15 17:22:28 +0100716 uint32_t i;
717 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100718
Andrew Walbranca808b12020-05-15 17:22:28 +0100719 /* Iterate over the memory region constituents within each fragment. */
720 for (i = 0; i < fragment_count; ++i) {
721 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
722 size_t size = fragments[i][j].page_count * PAGE_SIZE;
723 paddr_t pa_begin =
724 pa_from_ipa(ipa_init(fragments[i][j].address));
725 paddr_t pa_end = pa_add(pa_begin, size);
726
727 if (commit) {
728 vm_identity_commit(vm_locked, pa_begin, pa_end,
729 mode, ppool, NULL);
730 } else if (!vm_identity_prepare(vm_locked, pa_begin,
731 pa_end, mode, ppool)) {
732 return false;
733 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100734 }
735 }
736
737 return true;
738}
739
740/**
741 * Clears a region of physical memory by overwriting it with zeros. The data is
742 * flushed from the cache so the memory has been cleared across the system.
743 */
744static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
745{
746 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000747 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100748 * global mapping of the whole range. Such an approach will limit
749 * the changes to stage-1 tables and will allow only local
750 * invalidation.
751 */
752 bool ret;
753 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
754 void *ptr =
755 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
756 size_t size = pa_difference(begin, end);
757
758 if (!ptr) {
759 /* TODO: partial defrag of failed range. */
760 /* Recover any memory consumed in failed mapping. */
761 mm_defrag(stage1_locked, ppool);
762 goto fail;
763 }
764
765 memset_s(ptr, size, 0, size);
766 arch_mm_flush_dcache(ptr, size);
767 mm_unmap(stage1_locked, begin, end, ppool);
768
769 ret = true;
770 goto out;
771
772fail:
773 ret = false;
774
775out:
776 mm_unlock_stage1(&stage1_locked);
777
778 return ret;
779}
780
781/**
782 * Clears a region of physical memory by overwriting it with zeros. The data is
783 * flushed from the cache so the memory has been cleared across the system.
784 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100786 struct ffa_memory_region_constituent **fragments,
787 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
788 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100789{
790 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100791 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 struct mm_stage1_locked stage1_locked;
793 bool ret = false;
794
795 /*
796 * Create a local pool so any freed memory can't be used by another
797 * thread. This is to ensure each constituent that is mapped can be
798 * unmapped again afterwards.
799 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000800 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100801
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 /* Iterate over the memory region constituents within each fragment. */
803 for (i = 0; i < fragment_count; ++i) {
804 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100805
Andrew Walbranca808b12020-05-15 17:22:28 +0100806 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
807 size_t size = fragments[i][j].page_count * PAGE_SIZE;
808 paddr_t begin =
809 pa_from_ipa(ipa_init(fragments[i][j].address));
810 paddr_t end = pa_add(begin, size);
811
812 if (!clear_memory(begin, end, &local_page_pool)) {
813 /*
814 * api_clear_memory will defrag on failure, so
815 * no need to do it here.
816 */
817 goto out;
818 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100819 }
820 }
821
822 /*
823 * Need to defrag after clearing, as it may have added extra mappings to
824 * the stage 1 page table.
825 */
826 stage1_locked = mm_lock_stage1();
827 mm_defrag(stage1_locked, &local_page_pool);
828 mm_unlock_stage1(&stage1_locked);
829
830 ret = true;
831
832out:
833 mpool_fini(&local_page_pool);
834 return ret;
835}
836
837/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000838 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100839 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000840 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100841 *
842 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000843 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100844 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100845 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100846 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
847 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100848 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100849 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100851 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100852static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000853 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100854 struct ffa_memory_region_constituent **fragments,
855 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
856 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100857 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100858{
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 uint32_t orig_from_mode;
862 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100865
866 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100867 * Make sure constituents are properly aligned to a 64-bit boundary. If
868 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100870 for (i = 0; i < fragment_count; ++i) {
871 if (!is_aligned(fragments[i], 8)) {
872 dlog_verbose("Constituents not aligned.\n");
873 return ffa_error(FFA_INVALID_PARAMETERS);
874 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100875 }
876
877 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000878 * Check if the state transition is lawful for the sender, ensure that
879 * all constituents of a memory region being shared are at the same
880 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100883 &orig_from_mode, fragments,
884 fragment_constituent_counts,
885 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100888 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 }
890
Andrew Walbran37c574e2020-06-03 11:45:46 +0100891 if (orig_from_mode_ret != NULL) {
892 *orig_from_mode_ret = orig_from_mode;
893 }
894
Jose Marinho09b1db82019-08-08 09:16:59 +0100895 /*
896 * Create a local pool so any freed memory can't be used by another
897 * thread. This is to ensure the original mapping can be restored if the
898 * clear fails.
899 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000900 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100901
902 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000903 * First reserve all required memory for the new page table entries
904 * without committing, to make sure the entire operation will succeed
905 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100906 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100907 if (!ffa_region_group_identity_map(
908 from_locked, fragments, fragment_constituent_counts,
909 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100911 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100912 goto out;
913 }
914
915 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 * Update the mapping for the sender. This won't allocate because the
917 * transaction was already prepared above, but may free pages in the
918 * case that a whole block is being unmapped that was previously
919 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100920 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100921 CHECK(ffa_region_group_identity_map(
922 from_locked, fragments, fragment_constituent_counts,
923 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100924
925 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 fragments, fragment_constituent_counts,
928 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100929 /*
930 * On failure, roll back by returning memory to the sender. This
931 * may allocate pages which were previously freed into
932 * `local_page_pool` by the call above, but will never allocate
933 * more pages than that so can never fail.
934 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 from_locked, fragments, fragment_constituent_counts,
937 fragment_count, orig_from_mode, &local_page_pool,
938 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100939
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100941 goto out;
942 }
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000945
946out:
947 mpool_fini(&local_page_pool);
948
949 /*
950 * Tidy up the page table by reclaiming failed mappings (if there was an
951 * error) or merging entries into blocks where possible (on success).
952 */
953 mm_vm_defrag(&from->ptable, page_pool);
954
955 return ret;
956}
957
958/**
959 * Validates and maps memory shared from one VM to another.
960 *
961 * This function requires the calling context to hold the <to> lock.
962 *
963 * Returns:
964 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000968 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100969 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000970 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100971static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100973 struct ffa_memory_region_constituent **fragments,
974 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
975 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
976 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000977{
978 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100979 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000980 uint32_t to_mode;
981 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000983
984 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100985 * Make sure constituents are properly aligned to a 64-bit boundary. If
986 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000987 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100988 for (i = 0; i < fragment_count; ++i) {
989 if (!is_aligned(fragments[i], 8)) {
990 return ffa_error(FFA_INVALID_PARAMETERS);
991 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000992 }
993
994 /*
995 * Check if the state transition is lawful for the recipient, and ensure
996 * that all constituents of the memory region being retrieved are at the
997 * same state.
998 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100999 ret = ffa_retrieve_check_transition(
1000 to_locked, share_func, fragments, fragment_constituent_counts,
1001 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001003 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001004 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005 }
1006
1007 /*
1008 * Create a local pool so any freed memory can't be used by another
1009 * thread. This is to ensure the original mapping can be restored if the
1010 * clear fails.
1011 */
1012 mpool_init_with_fallback(&local_page_pool, page_pool);
1013
1014 /*
1015 * First reserve all required memory for the new page table entries in
1016 * the recipient page tables without committing, to make sure the entire
1017 * operation will succeed without exhausting the page pool.
1018 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 if (!ffa_region_group_identity_map(
1020 to_locked, fragments, fragment_constituent_counts,
1021 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 /* TODO: partial defrag of failed range. */
1023 dlog_verbose(
1024 "Insufficient memory to update recipient page "
1025 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027 goto out;
1028 }
1029
1030 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001032 fragments, fragment_constituent_counts,
1033 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 goto out;
1036 }
1037
Jose Marinho09b1db82019-08-08 09:16:59 +01001038 /*
1039 * Complete the transfer by mapping the memory into the recipient. This
1040 * won't allocate because the transaction was already prepared above, so
1041 * it doesn't need to use the `local_page_pool`.
1042 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001043 CHECK(ffa_region_group_identity_map(
1044 to_locked, fragments, fragment_constituent_counts,
1045 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001046
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001048
1049out:
1050 mpool_fini(&local_page_pool);
1051
1052 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001053 * Tidy up the page table by reclaiming failed mappings (if there was an
1054 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001055 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001056 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001057
1058 return ret;
1059}
1060
Andrew Walbran290b0c92020-02-03 16:37:14 +00001061/**
1062 * Reclaims the given memory from the TEE. To do this space is first reserved in
1063 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1064 * then (if that is successful) the memory is mapped back into the <to> VM's
1065 * page table.
1066 *
1067 * This function requires the calling context to hold the <to> lock.
1068 *
1069 * Returns:
1070 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001072 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001074 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001075 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001076 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001077static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 struct vm_locked to_locked, ffa_memory_handle_t handle,
1079 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001080 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1081 struct mpool *page_pool)
1082{
1083 struct vm *to = to_locked.vm;
1084 uint32_t to_mode;
1085 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 struct ffa_value ret;
1087 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001088
1089 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001090 * Make sure constituents are properly aligned to a 64-bit boundary. If
1091 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001092 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001093 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001094 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001096 }
1097
1098 /*
1099 * Check if the state transition is lawful for the recipient, and ensure
1100 * that all constituents of the memory region being retrieved are at the
1101 * same state.
1102 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001103 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001104 &constituents, &constituent_count,
1105 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001107 dlog_verbose("Invalid transition.\n");
1108 return ret;
1109 }
1110
1111 /*
1112 * Create a local pool so any freed memory can't be used by another
1113 * thread. This is to ensure the original mapping can be restored if the
1114 * clear fails.
1115 */
1116 mpool_init_with_fallback(&local_page_pool, page_pool);
1117
1118 /*
1119 * First reserve all required memory for the new page table entries in
1120 * the recipient page tables without committing, to make sure the entire
1121 * operation will succeed without exhausting the page pool.
1122 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001123 if (!ffa_region_group_identity_map(to_locked, &constituents,
1124 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001125 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001126 /* TODO: partial defrag of failed range. */
1127 dlog_verbose(
1128 "Insufficient memory to update recipient page "
1129 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001130 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001131 goto out;
1132 }
1133
1134 /*
1135 * Forward the request to the TEE and see what happens.
1136 */
1137 tee_flags = 0;
1138 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001140 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001141 ret = arch_other_world_call(
1142 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1143 .arg1 = (uint32_t)handle,
1144 .arg2 = (uint32_t)(handle >> 32),
1145 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001146
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001148 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001149 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1150 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001151 ret.func, ret.arg2);
1152 goto out;
1153 }
1154
1155 /*
1156 * The TEE was happy with it, so complete the reclaim by mapping the
1157 * memory into the recipient. This won't allocate because the
1158 * transaction was already prepared above, so it doesn't need to use the
1159 * `local_page_pool`.
1160 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001161 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1162 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001163 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001164
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001165 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001166
1167out:
1168 mpool_fini(&local_page_pool);
1169
1170 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001171 * Tidy up the page table by reclaiming failed mappings (if there was an
1172 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001173 */
1174 mm_vm_defrag(&to->ptable, page_pool);
1175
1176 return ret;
1177}
1178
Andrew Walbran996d1d12020-05-27 14:08:43 +01001179static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001180 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001181 struct ffa_memory_region_constituent **fragments,
1182 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1183 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001184{
1185 uint32_t orig_from_mode;
1186 uint32_t from_mode;
1187 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001189
Andrew Walbranca808b12020-05-15 17:22:28 +01001190 ret = ffa_relinquish_check_transition(
1191 from_locked, &orig_from_mode, fragments,
1192 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001194 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001195 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001196 }
1197
1198 /*
1199 * Create a local pool so any freed memory can't be used by another
1200 * thread. This is to ensure the original mapping can be restored if the
1201 * clear fails.
1202 */
1203 mpool_init_with_fallback(&local_page_pool, page_pool);
1204
1205 /*
1206 * First reserve all required memory for the new page table entries
1207 * without committing, to make sure the entire operation will succeed
1208 * without exhausting the page pool.
1209 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001210 if (!ffa_region_group_identity_map(
1211 from_locked, fragments, fragment_constituent_counts,
1212 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001213 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001215 goto out;
1216 }
1217
1218 /*
1219 * Update the mapping for the sender. This won't allocate because the
1220 * transaction was already prepared above, but may free pages in the
1221 * case that a whole block is being unmapped that was previously
1222 * partially mapped.
1223 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001224 CHECK(ffa_region_group_identity_map(
1225 from_locked, fragments, fragment_constituent_counts,
1226 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001227
1228 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001229 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 fragments, fragment_constituent_counts,
1231 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001232 /*
1233 * On failure, roll back by returning memory to the sender. This
1234 * may allocate pages which were previously freed into
1235 * `local_page_pool` by the call above, but will never allocate
1236 * more pages than that so can never fail.
1237 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001238 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001239 from_locked, fragments, fragment_constituent_counts,
1240 fragment_count, orig_from_mode, &local_page_pool,
1241 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001242
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001243 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001244 goto out;
1245 }
1246
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001247 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001248
1249out:
1250 mpool_fini(&local_page_pool);
1251
1252 /*
1253 * Tidy up the page table by reclaiming failed mappings (if there was an
1254 * error) or merging entries into blocks where possible (on success).
1255 */
1256 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001257
1258 return ret;
1259}
1260
1261/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001262 * Complete a memory sending operation by checking that it is valid, updating
1263 * the sender page table, and then either marking the share state as having
1264 * completed sending (on success) or freeing it (on failure).
1265 *
1266 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1267 */
1268static struct ffa_value ffa_memory_send_complete(
1269 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001270 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1271 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001272{
1273 struct ffa_memory_region *memory_region = share_state->memory_region;
1274 struct ffa_value ret;
1275
1276 /* Lock must be held. */
1277 CHECK(share_states.share_states != NULL);
1278
1279 /* Check that state is valid in sender page table and update. */
1280 ret = ffa_send_check_update(
1281 from_locked, share_state->fragments,
1282 share_state->fragment_constituent_counts,
1283 share_state->fragment_count, share_state->share_func,
1284 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001285 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1286 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001287 if (ret.func != FFA_SUCCESS_32) {
1288 /*
1289 * Free share state, it failed to send so it can't be retrieved.
1290 */
1291 dlog_verbose("Complete failed, freeing share state.\n");
1292 share_state_free(share_states, share_state, page_pool);
1293 return ret;
1294 }
1295
1296 share_state->sending_complete = true;
1297 dlog_verbose("Marked sending complete.\n");
1298
1299 return ffa_mem_success(share_state->handle);
1300}
1301
1302/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001303 * Check that the given `memory_region` represents a valid memory send request
1304 * of the given `share_func` type, return the clear flag and permissions via the
1305 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001306 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001307 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001308 * not.
1309 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001310static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001311 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1312 uint32_t memory_share_length, uint32_t fragment_length,
1313 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001314{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001315 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001316 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001317 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001318 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001319 enum ffa_data_access data_access;
1320 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321
Andrew Walbrana65a1322020-04-06 19:32:32 +01001322 CHECK(permissions != NULL);
1323
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001324 /*
1325 * This should already be checked by the caller, just making the
1326 * assumption clear here.
1327 */
1328 CHECK(memory_region->receiver_count == 1);
1329
Andrew Walbrana65a1322020-04-06 19:32:32 +01001330 /* The sender must match the message sender. */
1331 if (memory_region->sender != from_locked.vm->id) {
1332 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001333 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001334 }
1335
Andrew Walbrana65a1322020-04-06 19:32:32 +01001336 /*
1337 * Ensure that the composite header is within the memory bounds and
1338 * doesn't overlap the first part of the message.
1339 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001340 receivers_length = sizeof(struct ffa_memory_access) *
1341 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001342 constituents_offset =
1343 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001344 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001345 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001346 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001347 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001348 "Invalid composite memory region descriptor offset "
1349 "%d.\n",
1350 memory_region->receivers[0]
1351 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001352 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001353 }
1354
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001355 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001356
1357 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001358 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001359 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001360 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1361 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001362 if (memory_share_length != constituents_offset + constituents_length) {
1363 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001364 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001365 memory_region->receivers[0]
1366 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001367 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001368 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001369 if (fragment_length < memory_share_length &&
1370 fragment_length < HF_MAILBOX_SIZE) {
1371 dlog_warning(
1372 "Initial fragment length %d smaller than mailbox "
1373 "size.\n",
1374 fragment_length);
1375 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001376
Andrew Walbrana65a1322020-04-06 19:32:32 +01001377 /*
1378 * Clear is not allowed for memory sharing, as the sender still has
1379 * access to the memory.
1380 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001381 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1382 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001383 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001384 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001385 }
1386
1387 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001390 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001391 }
1392
1393 /* Check that the permissions are valid. */
1394 *permissions =
1395 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001396 data_access = ffa_get_data_access_attr(*permissions);
1397 instruction_access = ffa_get_instruction_access_attr(*permissions);
1398 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1399 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001400 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1401 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001402 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001403 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001405 dlog_verbose(
1406 "Invalid instruction access permissions %#x for "
1407 "sending memory.\n",
1408 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 if (share_func == FFA_MEM_SHARE_32) {
1412 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001413 dlog_verbose(
1414 "Invalid data access permissions %#x for "
1415 "sharing memory.\n",
1416 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001417 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001418 }
1419 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001420 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1421 * required for share operations (but must not be specified by
1422 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001423 * returned to the retriever.
1424 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 ffa_set_instruction_access_attr(permissions,
1426 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001427 memory_region->receivers[0].receiver_permissions.permissions =
1428 *permissions;
1429 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001430 if (share_func == FFA_MEM_LEND_32 &&
1431 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001432 dlog_verbose(
1433 "Invalid data access permissions %#x for lending "
1434 "memory.\n",
1435 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 if (share_func == FFA_MEM_DONATE_32 &&
1439 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001440 dlog_verbose(
1441 "Invalid data access permissions %#x for donating "
1442 "memory.\n",
1443 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001444 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001445 }
1446
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001447 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448}
1449
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001450/** Forwards a memory send message on to the TEE. */
1451static struct ffa_value memory_send_tee_forward(
1452 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1453 uint32_t share_func, struct ffa_memory_region *memory_region,
1454 uint32_t memory_share_length, uint32_t fragment_length)
1455{
1456 struct ffa_value ret;
1457
1458 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1459 memory_region, fragment_length);
1460 tee_locked.vm->mailbox.recv_size = fragment_length;
1461 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1462 tee_locked.vm->mailbox.recv_func = share_func;
1463 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001464 ret = arch_other_world_call(
1465 (struct ffa_value){.func = share_func,
1466 .arg1 = memory_share_length,
1467 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001468 /*
1469 * After the call to the TEE completes it must have finished reading its
1470 * RX buffer, so it is ready for another message.
1471 */
1472 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1473
1474 return ret;
1475}
1476
Andrew Walbrana65a1322020-04-06 19:32:32 +01001477/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001478 * Gets the share state for continuing an operation to donate, lend or share
1479 * memory, and checks that it is a valid request.
1480 *
1481 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1482 * not.
1483 */
1484static struct ffa_value ffa_memory_send_continue_validate(
1485 struct share_states_locked share_states, ffa_memory_handle_t handle,
1486 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1487 struct mpool *page_pool)
1488{
1489 struct ffa_memory_share_state *share_state;
1490 struct ffa_memory_region *memory_region;
1491
1492 CHECK(share_state_ret != NULL);
1493
1494 /*
1495 * Look up the share state by handle and make sure that the VM ID
1496 * matches.
1497 */
1498 if (!get_share_state(share_states, handle, &share_state)) {
1499 dlog_verbose(
1500 "Invalid handle %#x for memory send continuation.\n",
1501 handle);
1502 return ffa_error(FFA_INVALID_PARAMETERS);
1503 }
1504 memory_region = share_state->memory_region;
1505
1506 if (memory_region->sender != from_vm_id) {
1507 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1508 return ffa_error(FFA_INVALID_PARAMETERS);
1509 }
1510
1511 if (share_state->sending_complete) {
1512 dlog_verbose(
1513 "Sending of memory handle %#x is already complete.\n",
1514 handle);
1515 return ffa_error(FFA_INVALID_PARAMETERS);
1516 }
1517
1518 if (share_state->fragment_count == MAX_FRAGMENTS) {
1519 /*
1520 * Log a warning as this is a sign that MAX_FRAGMENTS should
1521 * probably be increased.
1522 */
1523 dlog_warning(
1524 "Too many fragments for memory share with handle %#x; "
1525 "only %d supported.\n",
1526 handle, MAX_FRAGMENTS);
1527 /* Free share state, as it's not possible to complete it. */
1528 share_state_free(share_states, share_state, page_pool);
1529 return ffa_error(FFA_NO_MEMORY);
1530 }
1531
1532 *share_state_ret = share_state;
1533
1534 return (struct ffa_value){.func = FFA_SUCCESS_32};
1535}
1536
1537/**
1538 * Forwards a memory send continuation message on to the TEE.
1539 */
1540static struct ffa_value memory_send_continue_tee_forward(
1541 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1542 uint32_t fragment_length, ffa_memory_handle_t handle)
1543{
1544 struct ffa_value ret;
1545
1546 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1547 fragment_length);
1548 tee_locked.vm->mailbox.recv_size = fragment_length;
1549 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1550 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1551 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001552 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001553 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1554 .arg1 = (uint32_t)handle,
1555 .arg2 = (uint32_t)(handle >> 32),
1556 .arg3 = fragment_length,
1557 .arg4 = (uint64_t)sender_vm_id << 16});
1558 /*
1559 * After the call to the TEE completes it must have finished reading its
1560 * RX buffer, so it is ready for another message.
1561 */
1562 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1563
1564 return ret;
1565}
1566
1567/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001568 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1569 * updates the stage-2 page tables. Specifically, check if the message length
1570 * and number of memory region constituents match, and if the transition is
1571 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001572 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001573 * Assumes that the caller has already found and locked the sender VM and copied
1574 * the memory region descriptor from the sender's TX buffer to a freshly
1575 * allocated page from Hafnium's internal pool. The caller must have also
1576 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001577 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001578 * This function takes ownership of the `memory_region` passed in and will free
1579 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001580 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001581struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001583 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001584 uint32_t fragment_length, uint32_t share_func,
1585 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001586{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001587 ffa_memory_access_permissions_t permissions;
1588 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001589 struct share_states_locked share_states;
1590 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001591
1592 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001593 * If there is an error validating the `memory_region` then we need to
1594 * free it because we own it but we won't be storing it in a share state
1595 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001596 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001597 ret = ffa_memory_send_validate(from_locked, memory_region,
1598 memory_share_length, fragment_length,
1599 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001600 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001602 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001603 }
1604
Andrew Walbrana65a1322020-04-06 19:32:32 +01001605 /* Set flag for share function, ready to be retrieved later. */
1606 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001608 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001609 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001610 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001611 case FFA_MEM_LEND_32:
1612 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001613 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001615 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001616 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001617 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001618 }
1619
Andrew Walbranca808b12020-05-15 17:22:28 +01001620 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001621 /*
1622 * Allocate a share state before updating the page table. Otherwise if
1623 * updating the page table succeeded but allocating the share state
1624 * failed then it would leave the memory in a state where nobody could
1625 * get it back.
1626 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001627 if (!allocate_share_state(share_states, share_func, memory_region,
1628 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1629 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001630 dlog_verbose("Failed to allocate share state.\n");
1631 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001632 ret = ffa_error(FFA_NO_MEMORY);
1633 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001634 }
1635
Andrew Walbranca808b12020-05-15 17:22:28 +01001636 if (fragment_length == memory_share_length) {
1637 /* No more fragments to come, everything fit in one message. */
1638 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001639 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 } else {
1641 ret = (struct ffa_value){
1642 .func = FFA_MEM_FRAG_RX_32,
1643 .arg1 = (uint32_t)share_state->handle,
1644 .arg2 = (uint32_t)(share_state->handle >> 32),
1645 .arg3 = fragment_length};
1646 }
1647
1648out:
1649 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001650 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001651 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001652}
1653
1654/**
1655 * Validates a call to donate, lend or share memory to the TEE and then updates
1656 * the stage-2 page tables. Specifically, check if the message length and number
1657 * of memory region constituents match, and if the transition is valid for the
1658 * type of memory sending operation.
1659 *
1660 * Assumes that the caller has already found and locked the sender VM and the
1661 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1662 * to a freshly allocated page from Hafnium's internal pool. The caller must
1663 * have also validated that the receiver VM ID is valid.
1664 *
1665 * This function takes ownership of the `memory_region` passed in and will free
1666 * it when necessary; it must not be freed by the caller.
1667 */
1668struct ffa_value ffa_memory_tee_send(
1669 struct vm_locked from_locked, struct vm_locked to_locked,
1670 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1671 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1672{
1673 ffa_memory_access_permissions_t permissions;
1674 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001675
1676 /*
1677 * If there is an error validating the `memory_region` then we need to
1678 * free it because we own it but we won't be storing it in a share state
1679 * after all.
1680 */
1681 ret = ffa_memory_send_validate(from_locked, memory_region,
1682 memory_share_length, fragment_length,
1683 share_func, &permissions);
1684 if (ret.func != FFA_SUCCESS_32) {
1685 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001686 }
1687
Andrew Walbranca808b12020-05-15 17:22:28 +01001688 if (fragment_length == memory_share_length) {
1689 /* No more fragments to come, everything fit in one message. */
1690 struct ffa_composite_memory_region *composite =
1691 ffa_memory_region_get_composite(memory_region, 0);
1692 struct ffa_memory_region_constituent *constituents =
1693 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001694 struct mpool local_page_pool;
1695 uint32_t orig_from_mode;
1696
1697 /*
1698 * Use a local page pool so that we can roll back if necessary.
1699 */
1700 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001701
1702 ret = ffa_send_check_update(
1703 from_locked, &constituents,
1704 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001705 permissions, &local_page_pool,
1706 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1707 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001708 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001709 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001710 goto out;
1711 }
1712
1713 /* Forward memory send message on to TEE. */
1714 ret = memory_send_tee_forward(
1715 to_locked, from_locked.vm->id, share_func,
1716 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001717
1718 if (ret.func != FFA_SUCCESS_32) {
1719 dlog_verbose(
1720 "TEE didn't successfully complete memory send "
1721 "operation; returned %#x (%d). Rolling back.\n",
1722 ret.func, ret.arg2);
1723
1724 /*
1725 * The TEE failed to complete the send operation, so
1726 * roll back the page table update for the VM. This
1727 * can't fail because it won't try to allocate more
1728 * memory than was freed into the `local_page_pool` by
1729 * `ffa_send_check_update` in the initial update.
1730 */
1731 CHECK(ffa_region_group_identity_map(
1732 from_locked, &constituents,
1733 &composite->constituent_count, 1,
1734 orig_from_mode, &local_page_pool, true));
1735 }
1736
1737 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001738 } else {
1739 struct share_states_locked share_states = share_states_lock();
1740 ffa_memory_handle_t handle;
1741
1742 /*
1743 * We need to wait for the rest of the fragments before we can
1744 * check whether the transaction is valid and unmap the memory.
1745 * Call the TEE so it can do its initial validation and assign a
1746 * handle, and allocate a share state to keep what we have so
1747 * far.
1748 */
1749 ret = memory_send_tee_forward(
1750 to_locked, from_locked.vm->id, share_func,
1751 memory_region, memory_share_length, fragment_length);
1752 if (ret.func == FFA_ERROR_32) {
1753 goto out_unlock;
1754 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1755 dlog_warning(
1756 "Got %#x from TEE in response to %#x for "
1757 "fragment with with %d/%d, expected "
1758 "FFA_MEM_FRAG_RX.\n",
1759 ret.func, share_func, fragment_length,
1760 memory_share_length);
1761 ret = ffa_error(FFA_INVALID_PARAMETERS);
1762 goto out_unlock;
1763 }
1764 handle = ffa_frag_handle(ret);
1765 if (ret.arg3 != fragment_length) {
1766 dlog_warning(
1767 "Got unexpected fragment offset %d for "
1768 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1769 ret.arg3, fragment_length);
1770 ret = ffa_error(FFA_INVALID_PARAMETERS);
1771 goto out_unlock;
1772 }
1773 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1774 dlog_warning(
1775 "Got unexpected sender ID %d for "
1776 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1777 ffa_frag_sender(ret), from_locked.vm->id);
1778 ret = ffa_error(FFA_INVALID_PARAMETERS);
1779 goto out_unlock;
1780 }
1781
1782 if (!allocate_share_state(share_states, share_func,
1783 memory_region, fragment_length,
1784 handle, NULL)) {
1785 dlog_verbose("Failed to allocate share state.\n");
1786 ret = ffa_error(FFA_NO_MEMORY);
1787 goto out_unlock;
1788 }
1789 /*
1790 * Don't free the memory region fragment, as it has been stored
1791 * in the share state.
1792 */
1793 memory_region = NULL;
1794 out_unlock:
1795 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001796 }
1797
Andrew Walbranca808b12020-05-15 17:22:28 +01001798out:
1799 if (memory_region != NULL) {
1800 mpool_free(page_pool, memory_region);
1801 }
1802 dump_share_states();
1803 return ret;
1804}
1805
1806/**
1807 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1808 * this is the last fragment then checks that the transition is valid for the
1809 * type of memory sending operation and updates the stage-2 page tables of the
1810 * sender.
1811 *
1812 * Assumes that the caller has already found and locked the sender VM and copied
1813 * the memory region descriptor from the sender's TX buffer to a freshly
1814 * allocated page from Hafnium's internal pool.
1815 *
1816 * This function takes ownership of the `fragment` passed in; it must not be
1817 * freed by the caller.
1818 */
1819struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1820 void *fragment,
1821 uint32_t fragment_length,
1822 ffa_memory_handle_t handle,
1823 struct mpool *page_pool)
1824{
1825 struct share_states_locked share_states = share_states_lock();
1826 struct ffa_memory_share_state *share_state;
1827 struct ffa_value ret;
1828 struct ffa_memory_region *memory_region;
1829
1830 ret = ffa_memory_send_continue_validate(share_states, handle,
1831 &share_state,
1832 from_locked.vm->id, page_pool);
1833 if (ret.func != FFA_SUCCESS_32) {
1834 goto out_free_fragment;
1835 }
1836 memory_region = share_state->memory_region;
1837
1838 if (memory_region->receivers[0].receiver_permissions.receiver ==
1839 HF_TEE_VM_ID) {
1840 dlog_error(
1841 "Got hypervisor-allocated handle for memory send to "
1842 "TEE. This should never happen, and indicates a bug in "
1843 "EL3 code.\n");
1844 ret = ffa_error(FFA_INVALID_PARAMETERS);
1845 goto out_free_fragment;
1846 }
1847
1848 /* Add this fragment. */
1849 share_state->fragments[share_state->fragment_count] = fragment;
1850 share_state->fragment_constituent_counts[share_state->fragment_count] =
1851 fragment_length / sizeof(struct ffa_memory_region_constituent);
1852 share_state->fragment_count++;
1853
1854 /* Check whether the memory send operation is now ready to complete. */
1855 if (share_state_sending_complete(share_states, share_state)) {
1856 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001857 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001858 } else {
1859 ret = (struct ffa_value){
1860 .func = FFA_MEM_FRAG_RX_32,
1861 .arg1 = (uint32_t)handle,
1862 .arg2 = (uint32_t)(handle >> 32),
1863 .arg3 = share_state_next_fragment_offset(share_states,
1864 share_state)};
1865 }
1866 goto out;
1867
1868out_free_fragment:
1869 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001870
1871out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001872 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001873 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001874}
1875
Andrew Walbranca808b12020-05-15 17:22:28 +01001876/**
1877 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1878 * is the last fragment then checks that the transition is valid for the type of
1879 * memory sending operation and updates the stage-2 page tables of the sender.
1880 *
1881 * Assumes that the caller has already found and locked the sender VM and copied
1882 * the memory region descriptor from the sender's TX buffer to a freshly
1883 * allocated page from Hafnium's internal pool.
1884 *
1885 * This function takes ownership of the `memory_region` passed in and will free
1886 * it when necessary; it must not be freed by the caller.
1887 */
1888struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1889 struct vm_locked to_locked,
1890 void *fragment,
1891 uint32_t fragment_length,
1892 ffa_memory_handle_t handle,
1893 struct mpool *page_pool)
1894{
1895 struct share_states_locked share_states = share_states_lock();
1896 struct ffa_memory_share_state *share_state;
1897 struct ffa_value ret;
1898 struct ffa_memory_region *memory_region;
1899
1900 ret = ffa_memory_send_continue_validate(share_states, handle,
1901 &share_state,
1902 from_locked.vm->id, page_pool);
1903 if (ret.func != FFA_SUCCESS_32) {
1904 goto out_free_fragment;
1905 }
1906 memory_region = share_state->memory_region;
1907
1908 if (memory_region->receivers[0].receiver_permissions.receiver !=
1909 HF_TEE_VM_ID) {
1910 dlog_error(
1911 "Got SPM-allocated handle for memory send to non-TEE "
1912 "VM. This should never happen, and indicates a bug.\n");
1913 ret = ffa_error(FFA_INVALID_PARAMETERS);
1914 goto out_free_fragment;
1915 }
1916
1917 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1918 to_locked.vm->mailbox.recv == NULL) {
1919 /*
1920 * If the TEE RX buffer is not available, tell the sender to
1921 * retry by returning the current offset again.
1922 */
1923 ret = (struct ffa_value){
1924 .func = FFA_MEM_FRAG_RX_32,
1925 .arg1 = (uint32_t)handle,
1926 .arg2 = (uint32_t)(handle >> 32),
1927 .arg3 = share_state_next_fragment_offset(share_states,
1928 share_state),
1929 };
1930 goto out_free_fragment;
1931 }
1932
1933 /* Add this fragment. */
1934 share_state->fragments[share_state->fragment_count] = fragment;
1935 share_state->fragment_constituent_counts[share_state->fragment_count] =
1936 fragment_length / sizeof(struct ffa_memory_region_constituent);
1937 share_state->fragment_count++;
1938
1939 /* Check whether the memory send operation is now ready to complete. */
1940 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001941 struct mpool local_page_pool;
1942 uint32_t orig_from_mode;
1943
1944 /*
1945 * Use a local page pool so that we can roll back if necessary.
1946 */
1947 mpool_init_with_fallback(&local_page_pool, page_pool);
1948
Andrew Walbranca808b12020-05-15 17:22:28 +01001949 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001950 share_state, &local_page_pool,
1951 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001952
1953 if (ret.func == FFA_SUCCESS_32) {
1954 /*
1955 * Forward final fragment on to the TEE so that
1956 * it can complete the memory sending operation.
1957 */
1958 ret = memory_send_continue_tee_forward(
1959 to_locked, from_locked.vm->id, fragment,
1960 fragment_length, handle);
1961
1962 if (ret.func != FFA_SUCCESS_32) {
1963 /*
1964 * The error will be passed on to the caller,
1965 * but log it here too.
1966 */
1967 dlog_verbose(
1968 "TEE didn't successfully complete "
1969 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001970 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001971 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001972
1973 /*
1974 * The TEE failed to complete the send
1975 * operation, so roll back the page table update
1976 * for the VM. This can't fail because it won't
1977 * try to allocate more memory than was freed
1978 * into the `local_page_pool` by
1979 * `ffa_send_check_update` in the initial
1980 * update.
1981 */
1982 CHECK(ffa_region_group_identity_map(
1983 from_locked, share_state->fragments,
1984 share_state
1985 ->fragment_constituent_counts,
1986 share_state->fragment_count,
1987 orig_from_mode, &local_page_pool,
1988 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001989 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001990
Andrew Walbranca808b12020-05-15 17:22:28 +01001991 /* Free share state. */
1992 share_state_free(share_states, share_state, page_pool);
1993 } else {
1994 /* Abort sending to TEE. */
1995 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02001996 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01001997 .func = FFA_MEM_RECLAIM_32,
1998 .arg1 = (uint32_t)handle,
1999 .arg2 = (uint32_t)(handle >> 32)});
2000
2001 if (tee_ret.func != FFA_SUCCESS_32) {
2002 /*
2003 * Nothing we can do if TEE doesn't abort
2004 * properly, just log it.
2005 */
2006 dlog_verbose(
2007 "TEE didn't successfully abort failed "
2008 "memory send operation; returned %#x "
2009 "(%d).\n",
2010 tee_ret.func, tee_ret.arg2);
2011 }
2012 /*
2013 * We don't need to free the share state in this case
2014 * because ffa_memory_send_complete does that already.
2015 */
2016 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002017
2018 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002019 } else {
2020 uint32_t next_fragment_offset =
2021 share_state_next_fragment_offset(share_states,
2022 share_state);
2023
2024 ret = memory_send_continue_tee_forward(
2025 to_locked, from_locked.vm->id, fragment,
2026 fragment_length, handle);
2027
2028 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2029 ffa_frag_handle(ret) != handle ||
2030 ret.arg3 != next_fragment_offset ||
2031 ffa_frag_sender(ret) != from_locked.vm->id) {
2032 dlog_verbose(
2033 "Got unexpected result from forwarding "
2034 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2035 "offset %d, sender %d); expected "
2036 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2037 "sender %d).\n",
2038 ret.func, ffa_frag_handle(ret), ret.arg3,
2039 ffa_frag_sender(ret), handle,
2040 next_fragment_offset, from_locked.vm->id);
2041 /* Free share state. */
2042 share_state_free(share_states, share_state, page_pool);
2043 ret = ffa_error(FFA_INVALID_PARAMETERS);
2044 goto out;
2045 }
2046
2047 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2048 .arg1 = (uint32_t)handle,
2049 .arg2 = (uint32_t)(handle >> 32),
2050 .arg3 = next_fragment_offset};
2051 }
2052 goto out;
2053
2054out_free_fragment:
2055 mpool_free(page_pool, fragment);
2056
2057out:
2058 share_states_unlock(&share_states);
2059 return ret;
2060}
2061
2062/** Clean up after the receiver has finished retrieving a memory region. */
2063static void ffa_memory_retrieve_complete(
2064 struct share_states_locked share_states,
2065 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2066{
2067 if (share_state->share_func == FFA_MEM_DONATE_32) {
2068 /*
2069 * Memory that has been donated can't be relinquished,
2070 * so no need to keep the share state around.
2071 */
2072 share_state_free(share_states, share_state, page_pool);
2073 dlog_verbose("Freed share state for donate.\n");
2074 }
2075}
2076
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2078 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002079 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002080 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002081{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002082 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002084 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002085 sizeof(struct ffa_memory_access);
2086 ffa_memory_handle_t handle = retrieve_request->handle;
2087 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002088 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2090 struct ffa_memory_region *memory_region;
2091 ffa_memory_access_permissions_t sent_permissions;
2092 enum ffa_data_access sent_data_access;
2093 enum ffa_instruction_access sent_instruction_access;
2094 ffa_memory_access_permissions_t requested_permissions;
2095 enum ffa_data_access requested_data_access;
2096 enum ffa_instruction_access requested_instruction_access;
2097 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002098 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002099 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002100 struct ffa_memory_share_state *share_state;
2101 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002102 struct ffa_composite_memory_region *composite;
2103 uint32_t total_length;
2104 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002105
2106 dump_share_states();
2107
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002108 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002109 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002110 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002111 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002112 expected_retrieve_request_length,
2113 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002114 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002115 }
2116
Andrew Walbrana65a1322020-04-06 19:32:32 +01002117 if (retrieve_request->receiver_count != 1) {
2118 dlog_verbose(
2119 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002121 "expected 1).\n",
2122 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002123 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002124 }
2125
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002126 share_states = share_states_lock();
2127 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002128 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002129 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002130 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002131 goto out;
2132 }
2133
Andrew Walbrana65a1322020-04-06 19:32:32 +01002134 memory_region = share_state->memory_region;
2135 CHECK(memory_region != NULL);
2136
2137 /*
2138 * Check that the transaction type expected by the receiver is correct,
2139 * if it has been specified.
2140 */
2141 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002142 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002143 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002144 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002145 dlog_verbose(
2146 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002147 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002148 transaction_type,
2149 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002151 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002152 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002153 goto out;
2154 }
2155
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002156 if (retrieve_request->sender != memory_region->sender) {
2157 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002159 "expected %d for handle %#x.\n",
2160 retrieve_request->sender, memory_region->sender,
2161 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002162 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002163 goto out;
2164 }
2165
2166 if (retrieve_request->tag != memory_region->tag) {
2167 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169 "%d for handle %#x.\n",
2170 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002171 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002172 goto out;
2173 }
2174
Andrew Walbrana65a1322020-04-06 19:32:32 +01002175 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2176 to_locked.vm->id) {
2177 dlog_verbose(
2178 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002180 retrieve_request->receivers[0]
2181 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002182 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002183 goto out;
2184 }
2185
2186 if (memory_region->receivers[0].receiver_permissions.receiver !=
2187 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002188 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002189 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2190 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002191 to_locked.vm->id,
2192 memory_region->receivers[0]
2193 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002195 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002196 goto out;
2197 }
2198
Andrew Walbranca808b12020-05-15 17:22:28 +01002199 if (!share_state->sending_complete) {
2200 dlog_verbose(
2201 "Memory with handle %#x not fully sent, can't "
2202 "retrieve.\n",
2203 handle);
2204 ret = ffa_error(FFA_INVALID_PARAMETERS);
2205 goto out;
2206 }
2207
2208 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002209 dlog_verbose("Memory with handle %#x already retrieved.\n",
2210 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002211 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002212 goto out;
2213 }
2214
Andrew Walbrana65a1322020-04-06 19:32:32 +01002215 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2216 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002217 dlog_verbose(
2218 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002219 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002220 retrieve_request->receivers[0]
2221 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002222 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002223 goto out;
2224 }
2225
Andrew Walbrana65a1322020-04-06 19:32:32 +01002226 /*
2227 * Check permissions from sender against permissions requested by
2228 * receiver.
2229 */
2230 /* TODO: Check attributes too. */
2231 sent_permissions =
2232 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002233 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002234 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002235 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 requested_permissions =
2237 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002238 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002239 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002240 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002241 permissions = 0;
2242 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002243 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2244 case FFA_DATA_ACCESS_RW:
2245 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2246 requested_data_access == FFA_DATA_ACCESS_RW) {
2247 ffa_set_data_access_attr(&permissions,
2248 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002249 break;
2250 }
2251 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002252 case FFA_DATA_ACCESS_RO:
2253 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2254 requested_data_access == FFA_DATA_ACCESS_RO) {
2255 ffa_set_data_access_attr(&permissions,
2256 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002257 break;
2258 }
2259 dlog_verbose(
2260 "Invalid data access requested; sender specified "
2261 "permissions %#x but receiver requested %#x.\n",
2262 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002263 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002264 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002265 case FFA_DATA_ACCESS_RESERVED:
2266 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002267 "checked before this point.");
2268 }
2269 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002270 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2271 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002272 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002273 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2274 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2275 ffa_set_instruction_access_attr(
2276 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002277 break;
2278 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002279 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002280 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002281 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2282 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2283 ffa_set_instruction_access_attr(
2284 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002285 break;
2286 }
2287 dlog_verbose(
2288 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002289 "specified permissions %#x but receiver requested "
2290 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002291 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002292 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002293 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002294 case FFA_INSTRUCTION_ACCESS_RESERVED:
2295 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002296 "be checked before this point.");
2297 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002298 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002299
Andrew Walbran996d1d12020-05-27 14:08:43 +01002300 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002301 to_locked, share_state->fragments,
2302 share_state->fragment_constituent_counts,
2303 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002304 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002305 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002306 goto out;
2307 }
2308
2309 /*
2310 * Copy response to RX buffer of caller and deliver the message. This
2311 * must be done before the share_state is (possibly) freed.
2312 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002313 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002314 composite = ffa_memory_region_get_composite(memory_region, 0);
2315 /*
2316 * Constituents which we received in the first fragment should always
2317 * fit in the first fragment we are sending, because the header is the
2318 * same size in both cases and we have a fixed message buffer size. So
2319 * `ffa_retrieved_memory_region_init` should never fail.
2320 */
2321 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002322 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2323 memory_region->sender, memory_region->attributes,
2324 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002325 composite->page_count, composite->constituent_count,
2326 share_state->fragments[0],
2327 share_state->fragment_constituent_counts[0], &total_length,
2328 &fragment_length));
2329 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002330 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002331 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002332 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2333
Andrew Walbranca808b12020-05-15 17:22:28 +01002334 share_state->retrieved_fragment_count[0] = 1;
2335 if (share_state->retrieved_fragment_count[0] ==
2336 share_state->fragment_count) {
2337 ffa_memory_retrieve_complete(share_states, share_state,
2338 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002339 }
2340
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002341 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002342 .arg1 = total_length,
2343 .arg2 = fragment_length};
2344
2345out:
2346 share_states_unlock(&share_states);
2347 dump_share_states();
2348 return ret;
2349}
2350
2351struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2352 ffa_memory_handle_t handle,
2353 uint32_t fragment_offset,
2354 struct mpool *page_pool)
2355{
2356 struct ffa_memory_region *memory_region;
2357 struct share_states_locked share_states;
2358 struct ffa_memory_share_state *share_state;
2359 struct ffa_value ret;
2360 uint32_t fragment_index;
2361 uint32_t retrieved_constituents_count;
2362 uint32_t i;
2363 uint32_t expected_fragment_offset;
2364 uint32_t remaining_constituent_count;
2365 uint32_t fragment_length;
2366
2367 dump_share_states();
2368
2369 share_states = share_states_lock();
2370 if (!get_share_state(share_states, handle, &share_state)) {
2371 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2372 handle);
2373 ret = ffa_error(FFA_INVALID_PARAMETERS);
2374 goto out;
2375 }
2376
2377 memory_region = share_state->memory_region;
2378 CHECK(memory_region != NULL);
2379
2380 if (memory_region->receivers[0].receiver_permissions.receiver !=
2381 to_locked.vm->id) {
2382 dlog_verbose(
2383 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2384 "of handle %#x.\n",
2385 to_locked.vm->id,
2386 memory_region->receivers[0]
2387 .receiver_permissions.receiver,
2388 handle);
2389 ret = ffa_error(FFA_INVALID_PARAMETERS);
2390 goto out;
2391 }
2392
2393 if (!share_state->sending_complete) {
2394 dlog_verbose(
2395 "Memory with handle %#x not fully sent, can't "
2396 "retrieve.\n",
2397 handle);
2398 ret = ffa_error(FFA_INVALID_PARAMETERS);
2399 goto out;
2400 }
2401
2402 if (share_state->retrieved_fragment_count[0] == 0 ||
2403 share_state->retrieved_fragment_count[0] >=
2404 share_state->fragment_count) {
2405 dlog_verbose(
2406 "Retrieval of memory with handle %#x not yet started "
2407 "or already completed (%d/%d fragments retrieved).\n",
2408 handle, share_state->retrieved_fragment_count[0],
2409 share_state->fragment_count);
2410 ret = ffa_error(FFA_INVALID_PARAMETERS);
2411 goto out;
2412 }
2413
2414 fragment_index = share_state->retrieved_fragment_count[0];
2415
2416 /*
2417 * Check that the given fragment offset is correct by counting how many
2418 * constituents were in the fragments previously sent.
2419 */
2420 retrieved_constituents_count = 0;
2421 for (i = 0; i < fragment_index; ++i) {
2422 retrieved_constituents_count +=
2423 share_state->fragment_constituent_counts[i];
2424 }
2425 expected_fragment_offset =
2426 ffa_composite_constituent_offset(memory_region, 0) +
2427 retrieved_constituents_count *
2428 sizeof(struct ffa_memory_region_constituent);
2429 if (fragment_offset != expected_fragment_offset) {
2430 dlog_verbose("Fragment offset was %d but expected %d.\n",
2431 fragment_offset, expected_fragment_offset);
2432 ret = ffa_error(FFA_INVALID_PARAMETERS);
2433 goto out;
2434 }
2435
2436 remaining_constituent_count = ffa_memory_fragment_init(
2437 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2438 share_state->fragments[fragment_index],
2439 share_state->fragment_constituent_counts[fragment_index],
2440 &fragment_length);
2441 CHECK(remaining_constituent_count == 0);
2442 to_locked.vm->mailbox.recv_size = fragment_length;
2443 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2444 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2445 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2446 share_state->retrieved_fragment_count[0]++;
2447 if (share_state->retrieved_fragment_count[0] ==
2448 share_state->fragment_count) {
2449 ffa_memory_retrieve_complete(share_states, share_state,
2450 page_pool);
2451 }
2452
2453 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2454 .arg1 = (uint32_t)handle,
2455 .arg2 = (uint32_t)(handle >> 32),
2456 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002457
2458out:
2459 share_states_unlock(&share_states);
2460 dump_share_states();
2461 return ret;
2462}
2463
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002464struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002465 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002467{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002468 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002469 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002470 struct ffa_memory_share_state *share_state;
2471 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002473 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474
Andrew Walbrana65a1322020-04-06 19:32:32 +01002475 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002476 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002477 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002478 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002479 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002480 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002481 }
2482
Andrew Walbrana65a1322020-04-06 19:32:32 +01002483 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002484 dlog_verbose(
2485 "VM ID %d in relinquish message doesn't match calling "
2486 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002487 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002488 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002489 }
2490
2491 dump_share_states();
2492
2493 share_states = share_states_lock();
2494 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002495 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002496 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002497 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002498 goto out;
2499 }
2500
Andrew Walbranca808b12020-05-15 17:22:28 +01002501 if (!share_state->sending_complete) {
2502 dlog_verbose(
2503 "Memory with handle %#x not fully sent, can't "
2504 "relinquish.\n",
2505 handle);
2506 ret = ffa_error(FFA_INVALID_PARAMETERS);
2507 goto out;
2508 }
2509
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002510 memory_region = share_state->memory_region;
2511 CHECK(memory_region != NULL);
2512
Andrew Walbrana65a1322020-04-06 19:32:32 +01002513 if (memory_region->receivers[0].receiver_permissions.receiver !=
2514 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002515 dlog_verbose(
2516 "VM ID %d tried to relinquish memory region with "
2517 "handle %#x but receiver was %d.\n",
2518 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002519 memory_region->receivers[0]
2520 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002521 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002522 goto out;
2523 }
2524
Andrew Walbranca808b12020-05-15 17:22:28 +01002525 if (share_state->retrieved_fragment_count[0] !=
2526 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002528 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002529 "relinquish.\n",
2530 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002531 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002532 goto out;
2533 }
2534
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002535 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002536
2537 /*
2538 * Clear is not allowed for memory that was shared, as the original
2539 * sender still has access to the memory.
2540 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002541 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544 goto out;
2545 }
2546
Andrew Walbranca808b12020-05-15 17:22:28 +01002547 ret = ffa_relinquish_check_update(
2548 from_locked, share_state->fragments,
2549 share_state->fragment_constituent_counts,
2550 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002552 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553 /*
2554 * Mark memory handle as not retrieved, so it can be reclaimed
2555 * (or retrieved again).
2556 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002557 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 }
2559
2560out:
2561 share_states_unlock(&share_states);
2562 dump_share_states();
2563 return ret;
2564}
2565
2566/**
2567 * Validates that the reclaim transition is allowed for the given handle,
2568 * updates the page table of the reclaiming VM, and frees the internal state
2569 * associated with the handle.
2570 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002571struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002572 ffa_memory_handle_t handle,
2573 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002574 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002575{
2576 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002577 struct ffa_memory_share_state *share_state;
2578 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002579 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581
2582 dump_share_states();
2583
2584 share_states = share_states_lock();
2585 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002586 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002587 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002588 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002589 goto out;
2590 }
2591
2592 memory_region = share_state->memory_region;
2593 CHECK(memory_region != NULL);
2594
2595 if (to_locked.vm->id != memory_region->sender) {
2596 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002597 "VM %#x attempted to reclaim memory handle %#x "
2598 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002599 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002600 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002601 goto out;
2602 }
2603
Andrew Walbranca808b12020-05-15 17:22:28 +01002604 if (!share_state->sending_complete) {
2605 dlog_verbose(
2606 "Memory with handle %#x not fully sent, can't "
2607 "reclaim.\n",
2608 handle);
2609 ret = ffa_error(FFA_INVALID_PARAMETERS);
2610 goto out;
2611 }
2612
2613 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002614 dlog_verbose(
2615 "Tried to reclaim memory handle %#x that has not been "
2616 "relinquished.\n",
2617 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002618 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002619 goto out;
2620 }
2621
Andrew Walbranca808b12020-05-15 17:22:28 +01002622 ret = ffa_retrieve_check_update(
2623 to_locked, share_state->fragments,
2624 share_state->fragment_constituent_counts,
2625 share_state->fragment_count, memory_to_attributes,
2626 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002627
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002628 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002629 share_state_free(share_states, share_state, page_pool);
2630 dlog_verbose("Freed share state after successful reclaim.\n");
2631 }
2632
2633out:
2634 share_states_unlock(&share_states);
2635 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002636}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002637
2638/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002639 * Validates that the reclaim transition is allowed for the memory region with
2640 * the given handle which was previously shared with the TEE, tells the TEE to
2641 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2642 *
2643 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002644 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002646 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002647 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002648 ffa_memory_region_flags_t flags,
2649 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002650{
Andrew Walbranca808b12020-05-15 17:22:28 +01002651 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2652 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2653 struct ffa_value tee_ret;
2654 uint32_t length;
2655 uint32_t fragment_length;
2656 uint32_t fragment_offset;
2657 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002658 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002659 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2660
2661 CHECK(request_length <= HF_MAILBOX_SIZE);
2662 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2663
2664 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002665 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002666 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2667 .arg1 = request_length,
2668 .arg2 = request_length});
2669 if (tee_ret.func == FFA_ERROR_32) {
2670 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2671 return tee_ret;
2672 }
2673 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2674 dlog_verbose(
2675 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2676 tee_ret.func);
2677 return ffa_error(FFA_INVALID_PARAMETERS);
2678 }
2679
2680 length = tee_ret.arg1;
2681 fragment_length = tee_ret.arg2;
2682
2683 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2684 length > sizeof(tee_retrieve_buffer)) {
2685 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2686 fragment_length, length, HF_MAILBOX_SIZE,
2687 sizeof(tee_retrieve_buffer));
2688 return ffa_error(FFA_INVALID_PARAMETERS);
2689 }
2690
2691 /*
2692 * Copy the first fragment of the memory region descriptor to an
2693 * internal buffer.
2694 */
2695 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2696 from_locked.vm->mailbox.send, fragment_length);
2697
2698 /* Fetch the remaining fragments into the same buffer. */
2699 fragment_offset = fragment_length;
2700 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002701 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002702 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2703 .arg1 = (uint32_t)handle,
2704 .arg2 = (uint32_t)(handle >> 32),
2705 .arg3 = fragment_offset});
2706 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2707 dlog_verbose(
2708 "Got %#x (%d) from TEE in response to "
2709 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2710 tee_ret.func, tee_ret.arg2);
2711 return tee_ret;
2712 }
2713 if (ffa_frag_handle(tee_ret) != handle) {
2714 dlog_verbose(
2715 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2716 "in response to FFA_MEM_FRAG_RX for handle "
2717 "%#x.\n",
2718 ffa_frag_handle(tee_ret), handle);
2719 return ffa_error(FFA_INVALID_PARAMETERS);
2720 }
2721 if (ffa_frag_sender(tee_ret) != 0) {
2722 dlog_verbose(
2723 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2724 "(expected 0).\n",
2725 ffa_frag_sender(tee_ret));
2726 return ffa_error(FFA_INVALID_PARAMETERS);
2727 }
2728 fragment_length = tee_ret.arg3;
2729 if (fragment_length > HF_MAILBOX_SIZE ||
2730 fragment_offset + fragment_length > length) {
2731 dlog_verbose(
2732 "Invalid fragment length %d at offset %d (max "
2733 "%d).\n",
2734 fragment_length, fragment_offset,
2735 HF_MAILBOX_SIZE);
2736 return ffa_error(FFA_INVALID_PARAMETERS);
2737 }
2738 memcpy_s(tee_retrieve_buffer + fragment_offset,
2739 sizeof(tee_retrieve_buffer) - fragment_offset,
2740 from_locked.vm->mailbox.send, fragment_length);
2741
2742 fragment_offset += fragment_length;
2743 }
2744
2745 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002746
2747 if (memory_region->receiver_count != 1) {
2748 /* Only one receiver supported by Hafnium for now. */
2749 dlog_verbose(
2750 "Multiple recipients not supported (got %d, expected "
2751 "1).\n",
2752 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002753 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002754 }
2755
2756 if (memory_region->handle != handle) {
2757 dlog_verbose(
2758 "Got memory region handle %#x from TEE but requested "
2759 "handle %#x.\n",
2760 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002761 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002762 }
2763
2764 /* The original sender must match the caller. */
2765 if (to_locked.vm->id != memory_region->sender) {
2766 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002767 "VM %#x attempted to reclaim memory handle %#x "
2768 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002769 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002770 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002771 }
2772
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002773 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002774
2775 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002776 * Validate that the reclaim transition is allowed for the given memory
2777 * region, forward the request to the TEE and then map the memory back
2778 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002779 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002780 return ffa_tee_reclaim_check_update(
2781 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002782 composite->constituent_count, memory_to_attributes,
2783 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002784}