blob: 53130f207304abcf3da1f69461cdf064148a3483 [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
Andrew Walbrana65a1322020-04-06 19:32:32 +0100331 dlog("from VM %d, attributes %#x, flags %#x, handle %#x, tag %d, to %d "
332 "recipients [",
333 memory_region->sender, memory_region->attributes,
334 memory_region->flags, memory_region->handle, memory_region->tag,
335 memory_region->receiver_count);
336 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337 if (i != 0) {
338 dlog(", ");
339 }
Andrew Walbrana65a1322020-04-06 19:32:32 +0100340 dlog("VM %d: %#x (offset %d)",
341 memory_region->receivers[i].receiver_permissions.receiver,
342 memory_region->receivers[i]
343 .receiver_permissions.permissions,
344 memory_region->receivers[i]
345 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346 }
347 dlog("]");
348}
349
350static void dump_share_states(void)
351{
352 uint32_t i;
353
354 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
355 return;
356 }
357
358 dlog("Current share states:\n");
359 sl_lock(&share_states_lock_instance);
360 for (i = 0; i < MAX_MEM_SHARES; ++i) {
361 if (share_states[i].share_func != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100362 dlog("%#x: ", share_states[i].handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000363 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000365 dlog("SHARE");
366 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000368 dlog("LEND");
369 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000371 dlog("DONATE");
372 break;
373 default:
374 dlog("invalid share_func %#x",
375 share_states[i].share_func);
376 }
377 dlog(" (");
378 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100379 if (share_states[i].sending_complete) {
380 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000381 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100382 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000383 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100384 dlog(" with %d fragments, %d retrieved\n",
385 share_states[i].fragment_count,
386 share_states[i].retrieved_fragment_count[0]);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000387 }
388 }
389 sl_unlock(&share_states_lock_instance);
390}
391
Andrew Walbran475c1452020-02-07 13:22:22 +0000392/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100393static inline uint32_t ffa_memory_permissions_to_mode(
394 ffa_memory_access_permissions_t permissions)
Andrew Walbran475c1452020-02-07 13:22:22 +0000395{
396 uint32_t mode = 0;
397
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100398 switch (ffa_get_data_access_attr(permissions)) {
399 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000400 mode = MM_MODE_R;
401 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100402 case FFA_DATA_ACCESS_RW:
403 case FFA_DATA_ACCESS_NOT_SPECIFIED:
Andrew Walbran475c1452020-02-07 13:22:22 +0000404 mode = MM_MODE_R | MM_MODE_W;
405 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100406 case FFA_DATA_ACCESS_RESERVED:
407 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100408 }
409
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100410 switch (ffa_get_instruction_access_attr(permissions)) {
411 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000412 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100413 case FFA_INSTRUCTION_ACCESS_X:
414 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100415 mode |= MM_MODE_X;
416 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417 case FFA_INSTRUCTION_ACCESS_RESERVED:
418 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000419 }
420
421 return mode;
422}
423
Jose Marinho75509b42019-04-09 09:34:59 +0100424/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000425 * Get the current mode in the stage-2 page table of the given vm of all the
426 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100428 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100429static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000430 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100431 struct ffa_memory_region_constituent **fragments,
432 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100433{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100434 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100435 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100436
Andrew Walbranca808b12020-05-15 17:22:28 +0100437 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100438 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000439 * Fail if there are no constituents. Otherwise we would get an
440 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100441 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100442 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100443 }
444
Andrew Walbranca808b12020-05-15 17:22:28 +0100445 for (i = 0; i < fragment_count; ++i) {
446 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
447 ipaddr_t begin = ipa_init(fragments[i][j].address);
448 size_t size = fragments[i][j].page_count * PAGE_SIZE;
449 ipaddr_t end = ipa_add(begin, size);
450 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100451
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 /* Fail if addresses are not page-aligned. */
453 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
454 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
455 return ffa_error(FFA_INVALID_PARAMETERS);
456 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100457
Andrew Walbranca808b12020-05-15 17:22:28 +0100458 /*
459 * Ensure that this constituent memory range is all
460 * mapped with the same mode.
461 */
462 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
463 &current_mode)) {
464 return ffa_error(FFA_DENIED);
465 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100466
Andrew Walbranca808b12020-05-15 17:22:28 +0100467 /*
468 * Ensure that all constituents are mapped with the same
469 * mode.
470 */
471 if (i == 0) {
472 *orig_mode = current_mode;
473 } else if (current_mode != *orig_mode) {
474 dlog_verbose(
475 "Expected mode %#x but was %#x for %d "
476 "pages at %#x.\n",
477 *orig_mode, current_mode,
478 fragments[i][j].page_count,
479 ipa_addr(begin));
480 return ffa_error(FFA_DENIED);
481 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100482 }
Jose Marinho75509b42019-04-09 09:34:59 +0100483 }
484
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100485 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000486}
487
488/**
489 * Verify that all pages have the same mode, that the starting mode
490 * constitutes a valid state and obtain the next mode to apply
491 * to the sending VM.
492 *
493 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 * 1) FFA_DENIED if a state transition was not found;
495 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100496 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100497 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100498 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100499 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
500 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000501 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100502static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100503 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100504 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100505 struct ffa_memory_region_constituent **fragments,
506 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
507 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000508{
509 const uint32_t state_mask =
510 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100511 const uint32_t required_from_mode =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512 ffa_memory_permissions_to_mode(permissions);
513 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000514
Andrew Walbranca808b12020-05-15 17:22:28 +0100515 ret = constituents_get_mode(from, orig_from_mode, fragments,
516 fragment_constituent_counts,
517 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100518 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100519 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100520 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100521 }
522
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000523 /* Ensure the address range is normal memory and not a device. */
524 if (*orig_from_mode & MM_MODE_D) {
525 dlog_verbose("Can't share device memory (mode is %#x).\n",
526 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528 }
529
530 /*
531 * Ensure the sender is the owner and has exclusive access to the
532 * memory.
533 */
534 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100536 }
537
538 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
539 dlog_verbose(
540 "Sender tried to send memory with permissions which "
541 "required mode %#x but only had %#x itself.\n",
542 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100543 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000544 }
545
546 /* Find the appropriate new mode. */
547 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000548 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100549 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000550 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100551 break;
552
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100553 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000554 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100555 break;
556
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100557 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000558 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100559 break;
560
Jose Marinho75509b42019-04-09 09:34:59 +0100561 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100563 }
564
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100565 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566}
567
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100568static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000569 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100570 struct ffa_memory_region_constituent **fragments,
571 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
572 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573{
574 const uint32_t state_mask =
575 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
576 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578
Andrew Walbranca808b12020-05-15 17:22:28 +0100579 ret = constituents_get_mode(from, orig_from_mode, fragments,
580 fragment_constituent_counts,
581 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100582 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100583 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000584 }
585
586 /* Ensure the address range is normal memory and not a device. */
587 if (*orig_from_mode & MM_MODE_D) {
588 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
589 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000591 }
592
593 /*
594 * Ensure the relinquishing VM is not the owner but has access to the
595 * memory.
596 */
597 orig_from_state = *orig_from_mode & state_mask;
598 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
599 dlog_verbose(
600 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100601 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000602 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000604 }
605
606 /* Find the appropriate new mode. */
607 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
608
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100609 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000610}
611
612/**
613 * Verify that all pages have the same mode, that the starting mode
614 * constitutes a valid state and obtain the next mode to apply
615 * to the retrieving VM.
616 *
617 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 * 1) FFA_DENIED if a state transition was not found;
619 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100620 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100621 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100622 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100623 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
624 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100626static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000627 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100628 struct ffa_memory_region_constituent **fragments,
629 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
630 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000631{
632 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100633 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000634
Andrew Walbranca808b12020-05-15 17:22:28 +0100635 ret = constituents_get_mode(to, &orig_to_mode, fragments,
636 fragment_constituent_counts,
637 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100638 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100639 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100640 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000641 }
642
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000644 const uint32_t state_mask =
645 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
646 uint32_t orig_to_state = orig_to_mode & state_mask;
647
648 if (orig_to_state != MM_MODE_INVALID &&
649 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100650 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000651 }
652 } else {
653 /*
654 * Ensure the retriever has the expected state. We don't care
655 * about the MM_MODE_SHARED bit; either with or without it set
656 * are both valid representations of the !O-NA state.
657 */
658 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
659 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000661 }
662 }
663
664 /* Find the appropriate new mode. */
665 *to_mode = memory_to_attributes;
666 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100667 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000668 *to_mode |= 0;
669 break;
670
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000672 *to_mode |= MM_MODE_UNOWNED;
673 break;
674
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100675 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000676 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
677 break;
678
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100679 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000680 *to_mode |= 0;
681 break;
682
683 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100684 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100685 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 }
687
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100689}
Jose Marinho09b1db82019-08-08 09:16:59 +0100690
691/**
692 * Updates a VM's page table such that the given set of physical address ranges
693 * are mapped in the address space at the corresponding address ranges, in the
694 * mode provided.
695 *
696 * If commit is false, the page tables will be allocated from the mpool but no
697 * mappings will actually be updated. This function must always be called first
698 * with commit false to check that it will succeed before calling with commit
699 * true, to avoid leaving the page table in a half-updated state. To make a
700 * series of changes atomically you can call them all with commit false before
701 * calling them all with commit true.
702 *
703 * mm_vm_defrag should always be called after a series of page table updates,
704 * whether they succeed or fail.
705 *
706 * Returns true on success, or false if the update failed and no changes were
707 * made to memory mappings.
708 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100709static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000710 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100711 struct ffa_memory_region_constituent **fragments,
712 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
713 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100714{
Andrew Walbranca808b12020-05-15 17:22:28 +0100715 uint32_t i;
716 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100717
Andrew Walbranca808b12020-05-15 17:22:28 +0100718 /* Iterate over the memory region constituents within each fragment. */
719 for (i = 0; i < fragment_count; ++i) {
720 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
721 size_t size = fragments[i][j].page_count * PAGE_SIZE;
722 paddr_t pa_begin =
723 pa_from_ipa(ipa_init(fragments[i][j].address));
724 paddr_t pa_end = pa_add(pa_begin, size);
725
726 if (commit) {
727 vm_identity_commit(vm_locked, pa_begin, pa_end,
728 mode, ppool, NULL);
729 } else if (!vm_identity_prepare(vm_locked, pa_begin,
730 pa_end, mode, ppool)) {
731 return false;
732 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100733 }
734 }
735
736 return true;
737}
738
739/**
740 * Clears a region of physical memory by overwriting it with zeros. The data is
741 * flushed from the cache so the memory has been cleared across the system.
742 */
743static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
744{
745 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000746 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100747 * global mapping of the whole range. Such an approach will limit
748 * the changes to stage-1 tables and will allow only local
749 * invalidation.
750 */
751 bool ret;
752 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
753 void *ptr =
754 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
755 size_t size = pa_difference(begin, end);
756
757 if (!ptr) {
758 /* TODO: partial defrag of failed range. */
759 /* Recover any memory consumed in failed mapping. */
760 mm_defrag(stage1_locked, ppool);
761 goto fail;
762 }
763
764 memset_s(ptr, size, 0, size);
765 arch_mm_flush_dcache(ptr, size);
766 mm_unmap(stage1_locked, begin, end, ppool);
767
768 ret = true;
769 goto out;
770
771fail:
772 ret = false;
773
774out:
775 mm_unlock_stage1(&stage1_locked);
776
777 return ret;
778}
779
780/**
781 * Clears a region of physical memory by overwriting it with zeros. The data is
782 * flushed from the cache so the memory has been cleared across the system.
783 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100784static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100785 struct ffa_memory_region_constituent **fragments,
786 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
787 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100788{
789 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100790 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100791 struct mm_stage1_locked stage1_locked;
792 bool ret = false;
793
794 /*
795 * Create a local pool so any freed memory can't be used by another
796 * thread. This is to ensure each constituent that is mapped can be
797 * unmapped again afterwards.
798 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000799 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100800
Andrew Walbranca808b12020-05-15 17:22:28 +0100801 /* Iterate over the memory region constituents within each fragment. */
802 for (i = 0; i < fragment_count; ++i) {
803 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100804
Andrew Walbranca808b12020-05-15 17:22:28 +0100805 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
806 size_t size = fragments[i][j].page_count * PAGE_SIZE;
807 paddr_t begin =
808 pa_from_ipa(ipa_init(fragments[i][j].address));
809 paddr_t end = pa_add(begin, size);
810
811 if (!clear_memory(begin, end, &local_page_pool)) {
812 /*
813 * api_clear_memory will defrag on failure, so
814 * no need to do it here.
815 */
816 goto out;
817 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100818 }
819 }
820
821 /*
822 * Need to defrag after clearing, as it may have added extra mappings to
823 * the stage 1 page table.
824 */
825 stage1_locked = mm_lock_stage1();
826 mm_defrag(stage1_locked, &local_page_pool);
827 mm_unlock_stage1(&stage1_locked);
828
829 ret = true;
830
831out:
832 mpool_fini(&local_page_pool);
833 return ret;
834}
835
836/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000837 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100838 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000839 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100840 *
841 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000842 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100843 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100844 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100845 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
846 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100847 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100848 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100849 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100850 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100851static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000852 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100853 struct ffa_memory_region_constituent **fragments,
854 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
855 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100856 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100857{
Jose Marinho09b1db82019-08-08 09:16:59 +0100858 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100859 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100860 uint32_t orig_from_mode;
861 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100862 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100863 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100864
865 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100866 * Make sure constituents are properly aligned to a 64-bit boundary. If
867 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100868 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100869 for (i = 0; i < fragment_count; ++i) {
870 if (!is_aligned(fragments[i], 8)) {
871 dlog_verbose("Constituents not aligned.\n");
872 return ffa_error(FFA_INVALID_PARAMETERS);
873 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100874 }
875
876 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000877 * Check if the state transition is lawful for the sender, ensure that
878 * all constituents of a memory region being shared are at the same
879 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100880 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100882 &orig_from_mode, fragments,
883 fragment_constituent_counts,
884 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100886 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100887 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100888 }
889
Andrew Walbran37c574e2020-06-03 11:45:46 +0100890 if (orig_from_mode_ret != NULL) {
891 *orig_from_mode_ret = orig_from_mode;
892 }
893
Jose Marinho09b1db82019-08-08 09:16:59 +0100894 /*
895 * Create a local pool so any freed memory can't be used by another
896 * thread. This is to ensure the original mapping can be restored if the
897 * clear fails.
898 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000899 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100900
901 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000902 * First reserve all required memory for the new page table entries
903 * without committing, to make sure the entire operation will succeed
904 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100905 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100906 if (!ffa_region_group_identity_map(
907 from_locked, fragments, fragment_constituent_counts,
908 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100910 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100911 goto out;
912 }
913
914 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000915 * Update the mapping for the sender. This won't allocate because the
916 * transaction was already prepared above, but may free pages in the
917 * case that a whole block is being unmapped that was previously
918 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100919 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100920 CHECK(ffa_region_group_identity_map(
921 from_locked, fragments, fragment_constituent_counts,
922 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100923
924 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100926 fragments, fragment_constituent_counts,
927 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100928 /*
929 * On failure, roll back by returning memory to the sender. This
930 * may allocate pages which were previously freed into
931 * `local_page_pool` by the call above, but will never allocate
932 * more pages than that so can never fail.
933 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100935 from_locked, fragments, fragment_constituent_counts,
936 fragment_count, orig_from_mode, &local_page_pool,
937 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100938
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100939 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100940 goto out;
941 }
942
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100943 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000944
945out:
946 mpool_fini(&local_page_pool);
947
948 /*
949 * Tidy up the page table by reclaiming failed mappings (if there was an
950 * error) or merging entries into blocks where possible (on success).
951 */
952 mm_vm_defrag(&from->ptable, page_pool);
953
954 return ret;
955}
956
957/**
958 * Validates and maps memory shared from one VM to another.
959 *
960 * This function requires the calling context to hold the <to> lock.
961 *
962 * Returns:
963 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100964 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000965 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100966 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000967 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100968 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000969 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100970static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000971 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100972 struct ffa_memory_region_constituent **fragments,
973 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
974 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
975 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000976{
977 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100978 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000979 uint32_t to_mode;
980 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000982
983 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100984 * Make sure constituents are properly aligned to a 64-bit boundary. If
985 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000986 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100987 for (i = 0; i < fragment_count; ++i) {
988 if (!is_aligned(fragments[i], 8)) {
989 return ffa_error(FFA_INVALID_PARAMETERS);
990 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991 }
992
993 /*
994 * Check if the state transition is lawful for the recipient, and ensure
995 * that all constituents of the memory region being retrieved are at the
996 * same state.
997 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100998 ret = ffa_retrieve_check_transition(
999 to_locked, share_func, fragments, fragment_constituent_counts,
1000 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001002 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001003 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004 }
1005
1006 /*
1007 * Create a local pool so any freed memory can't be used by another
1008 * thread. This is to ensure the original mapping can be restored if the
1009 * clear fails.
1010 */
1011 mpool_init_with_fallback(&local_page_pool, page_pool);
1012
1013 /*
1014 * First reserve all required memory for the new page table entries in
1015 * the recipient page tables without committing, to make sure the entire
1016 * operation will succeed without exhausting the page pool.
1017 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001018 if (!ffa_region_group_identity_map(
1019 to_locked, fragments, fragment_constituent_counts,
1020 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021 /* TODO: partial defrag of failed range. */
1022 dlog_verbose(
1023 "Insufficient memory to update recipient page "
1024 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001025 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001026 goto out;
1027 }
1028
1029 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001030 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001031 fragments, fragment_constituent_counts,
1032 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034 goto out;
1035 }
1036
Jose Marinho09b1db82019-08-08 09:16:59 +01001037 /*
1038 * Complete the transfer by mapping the memory into the recipient. This
1039 * won't allocate because the transaction was already prepared above, so
1040 * it doesn't need to use the `local_page_pool`.
1041 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001042 CHECK(ffa_region_group_identity_map(
1043 to_locked, fragments, fragment_constituent_counts,
1044 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001045
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001047
1048out:
1049 mpool_fini(&local_page_pool);
1050
1051 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001052 * Tidy up the page table by reclaiming failed mappings (if there was an
1053 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001054 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001055 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001056
1057 return ret;
1058}
1059
Andrew Walbran290b0c92020-02-03 16:37:14 +00001060/**
1061 * Reclaims the given memory from the TEE. To do this space is first reserved in
1062 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1063 * then (if that is successful) the memory is mapped back into the <to> VM's
1064 * page table.
1065 *
1066 * This function requires the calling context to hold the <to> lock.
1067 *
1068 * Returns:
1069 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001071 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001072 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001073 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001074 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001075 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001076static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001077 struct vm_locked to_locked, ffa_memory_handle_t handle,
1078 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001079 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1080 struct mpool *page_pool)
1081{
1082 struct vm *to = to_locked.vm;
1083 uint32_t to_mode;
1084 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085 struct ffa_value ret;
1086 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001087
1088 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001089 * Make sure constituents are properly aligned to a 64-bit boundary. If
1090 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001091 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001092 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001093 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001094 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001095 }
1096
1097 /*
1098 * Check if the state transition is lawful for the recipient, and ensure
1099 * that all constituents of the memory region being retrieved are at the
1100 * same state.
1101 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001102 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001103 &constituents, &constituent_count,
1104 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001105 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001106 dlog_verbose("Invalid transition.\n");
1107 return ret;
1108 }
1109
1110 /*
1111 * Create a local pool so any freed memory can't be used by another
1112 * thread. This is to ensure the original mapping can be restored if the
1113 * clear fails.
1114 */
1115 mpool_init_with_fallback(&local_page_pool, page_pool);
1116
1117 /*
1118 * First reserve all required memory for the new page table entries in
1119 * the recipient page tables without committing, to make sure the entire
1120 * operation will succeed without exhausting the page pool.
1121 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001122 if (!ffa_region_group_identity_map(to_locked, &constituents,
1123 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001124 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001125 /* TODO: partial defrag of failed range. */
1126 dlog_verbose(
1127 "Insufficient memory to update recipient page "
1128 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001130 goto out;
1131 }
1132
1133 /*
1134 * Forward the request to the TEE and see what happens.
1135 */
1136 tee_flags = 0;
1137 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001139 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001140 ret = arch_other_world_call(
1141 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1142 .arg1 = (uint32_t)handle,
1143 .arg2 = (uint32_t)(handle >> 32),
1144 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001145
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001146 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001147 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1149 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001150 ret.func, ret.arg2);
1151 goto out;
1152 }
1153
1154 /*
1155 * The TEE was happy with it, so complete the reclaim by mapping the
1156 * memory into the recipient. This won't allocate because the
1157 * transaction was already prepared above, so it doesn't need to use the
1158 * `local_page_pool`.
1159 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001160 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1161 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001163
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001165
1166out:
1167 mpool_fini(&local_page_pool);
1168
1169 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001170 * Tidy up the page table by reclaiming failed mappings (if there was an
1171 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001172 */
1173 mm_vm_defrag(&to->ptable, page_pool);
1174
1175 return ret;
1176}
1177
Andrew Walbran996d1d12020-05-27 14:08:43 +01001178static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001179 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001180 struct ffa_memory_region_constituent **fragments,
1181 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1182 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001183{
1184 uint32_t orig_from_mode;
1185 uint32_t from_mode;
1186 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001188
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 ret = ffa_relinquish_check_transition(
1190 from_locked, &orig_from_mode, fragments,
1191 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001192 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001193 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001194 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001195 }
1196
1197 /*
1198 * Create a local pool so any freed memory can't be used by another
1199 * thread. This is to ensure the original mapping can be restored if the
1200 * clear fails.
1201 */
1202 mpool_init_with_fallback(&local_page_pool, page_pool);
1203
1204 /*
1205 * First reserve all required memory for the new page table entries
1206 * without committing, to make sure the entire operation will succeed
1207 * without exhausting the page pool.
1208 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001209 if (!ffa_region_group_identity_map(
1210 from_locked, fragments, fragment_constituent_counts,
1211 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001212 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001214 goto out;
1215 }
1216
1217 /*
1218 * Update the mapping for the sender. This won't allocate because the
1219 * transaction was already prepared above, but may free pages in the
1220 * case that a whole block is being unmapped that was previously
1221 * partially mapped.
1222 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001223 CHECK(ffa_region_group_identity_map(
1224 from_locked, fragments, fragment_constituent_counts,
1225 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001226
1227 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001228 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 fragments, fragment_constituent_counts,
1230 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001231 /*
1232 * On failure, roll back by returning memory to the sender. This
1233 * may allocate pages which were previously freed into
1234 * `local_page_pool` by the call above, but will never allocate
1235 * more pages than that so can never fail.
1236 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001238 from_locked, fragments, fragment_constituent_counts,
1239 fragment_count, orig_from_mode, &local_page_pool,
1240 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001241
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001242 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001243 goto out;
1244 }
1245
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001247
1248out:
1249 mpool_fini(&local_page_pool);
1250
1251 /*
1252 * Tidy up the page table by reclaiming failed mappings (if there was an
1253 * error) or merging entries into blocks where possible (on success).
1254 */
1255 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001256
1257 return ret;
1258}
1259
1260/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 * Complete a memory sending operation by checking that it is valid, updating
1262 * the sender page table, and then either marking the share state as having
1263 * completed sending (on success) or freeing it (on failure).
1264 *
1265 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1266 */
1267static struct ffa_value ffa_memory_send_complete(
1268 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001269 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1270 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001271{
1272 struct ffa_memory_region *memory_region = share_state->memory_region;
1273 struct ffa_value ret;
1274
1275 /* Lock must be held. */
1276 CHECK(share_states.share_states != NULL);
1277
1278 /* Check that state is valid in sender page table and update. */
1279 ret = ffa_send_check_update(
1280 from_locked, share_state->fragments,
1281 share_state->fragment_constituent_counts,
1282 share_state->fragment_count, share_state->share_func,
1283 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001284 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1285 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001286 if (ret.func != FFA_SUCCESS_32) {
1287 /*
1288 * Free share state, it failed to send so it can't be retrieved.
1289 */
1290 dlog_verbose("Complete failed, freeing share state.\n");
1291 share_state_free(share_states, share_state, page_pool);
1292 return ret;
1293 }
1294
1295 share_state->sending_complete = true;
1296 dlog_verbose("Marked sending complete.\n");
1297
1298 return ffa_mem_success(share_state->handle);
1299}
1300
1301/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001302 * Check that the given `memory_region` represents a valid memory send request
1303 * of the given `share_func` type, return the clear flag and permissions via the
1304 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001305 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001306 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001307 * not.
1308 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001309static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001310 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1311 uint32_t memory_share_length, uint32_t fragment_length,
1312 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001313{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001314 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001315 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001316 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001317 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001318 enum ffa_data_access data_access;
1319 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321 CHECK(permissions != NULL);
1322
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001323 /*
1324 * This should already be checked by the caller, just making the
1325 * assumption clear here.
1326 */
1327 CHECK(memory_region->receiver_count == 1);
1328
Andrew Walbrana65a1322020-04-06 19:32:32 +01001329 /* The sender must match the message sender. */
1330 if (memory_region->sender != from_locked.vm->id) {
1331 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001333 }
1334
Andrew Walbrana65a1322020-04-06 19:32:32 +01001335 /*
1336 * Ensure that the composite header is within the memory bounds and
1337 * doesn't overlap the first part of the message.
1338 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001339 receivers_length = sizeof(struct ffa_memory_access) *
1340 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001341 constituents_offset =
1342 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001343 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001344 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001345 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001346 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001347 "Invalid composite memory region descriptor offset "
1348 "%d.\n",
1349 memory_region->receivers[0]
1350 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001351 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001352 }
1353
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001355
1356 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001357 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001358 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001359 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1360 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001361 if (memory_share_length != constituents_offset + constituents_length) {
1362 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001363 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001364 memory_region->receivers[0]
1365 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001366 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001368 if (fragment_length < memory_share_length &&
1369 fragment_length < HF_MAILBOX_SIZE) {
1370 dlog_warning(
1371 "Initial fragment length %d smaller than mailbox "
1372 "size.\n",
1373 fragment_length);
1374 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375
Andrew Walbrana65a1322020-04-06 19:32:32 +01001376 /*
1377 * Clear is not allowed for memory sharing, as the sender still has
1378 * access to the memory.
1379 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001380 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1381 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001382 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001383 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 }
1385
1386 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 }
1391
1392 /* Check that the permissions are valid. */
1393 *permissions =
1394 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001395 data_access = ffa_get_data_access_attr(*permissions);
1396 instruction_access = ffa_get_instruction_access_attr(*permissions);
1397 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1398 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001399 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1400 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001404 dlog_verbose(
1405 "Invalid instruction access permissions %#x for "
1406 "sending memory.\n",
1407 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001409 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001410 if (share_func == FFA_MEM_SHARE_32) {
1411 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 dlog_verbose(
1413 "Invalid data access permissions %#x for "
1414 "sharing memory.\n",
1415 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001416 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001417 }
1418 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001419 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1420 * required for share operations (but must not be specified by
1421 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 * returned to the retriever.
1423 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 ffa_set_instruction_access_attr(permissions,
1425 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001426 memory_region->receivers[0].receiver_permissions.permissions =
1427 *permissions;
1428 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 if (share_func == FFA_MEM_LEND_32 &&
1430 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001431 dlog_verbose(
1432 "Invalid data access permissions %#x for lending "
1433 "memory.\n",
1434 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 if (share_func == FFA_MEM_DONATE_32 &&
1438 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 dlog_verbose(
1440 "Invalid data access permissions %#x for donating "
1441 "memory.\n",
1442 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444 }
1445
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447}
1448
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001449/** Forwards a memory send message on to the TEE. */
1450static struct ffa_value memory_send_tee_forward(
1451 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1452 uint32_t share_func, struct ffa_memory_region *memory_region,
1453 uint32_t memory_share_length, uint32_t fragment_length)
1454{
1455 struct ffa_value ret;
1456
1457 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1458 memory_region, fragment_length);
1459 tee_locked.vm->mailbox.recv_size = fragment_length;
1460 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1461 tee_locked.vm->mailbox.recv_func = share_func;
1462 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001463 ret = arch_other_world_call(
1464 (struct ffa_value){.func = share_func,
1465 .arg1 = memory_share_length,
1466 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001467 /*
1468 * After the call to the TEE completes it must have finished reading its
1469 * RX buffer, so it is ready for another message.
1470 */
1471 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1472
1473 return ret;
1474}
1475
Andrew Walbrana65a1322020-04-06 19:32:32 +01001476/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001477 * Gets the share state for continuing an operation to donate, lend or share
1478 * memory, and checks that it is a valid request.
1479 *
1480 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1481 * not.
1482 */
1483static struct ffa_value ffa_memory_send_continue_validate(
1484 struct share_states_locked share_states, ffa_memory_handle_t handle,
1485 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1486 struct mpool *page_pool)
1487{
1488 struct ffa_memory_share_state *share_state;
1489 struct ffa_memory_region *memory_region;
1490
1491 CHECK(share_state_ret != NULL);
1492
1493 /*
1494 * Look up the share state by handle and make sure that the VM ID
1495 * matches.
1496 */
1497 if (!get_share_state(share_states, handle, &share_state)) {
1498 dlog_verbose(
1499 "Invalid handle %#x for memory send continuation.\n",
1500 handle);
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
1503 memory_region = share_state->memory_region;
1504
1505 if (memory_region->sender != from_vm_id) {
1506 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1507 return ffa_error(FFA_INVALID_PARAMETERS);
1508 }
1509
1510 if (share_state->sending_complete) {
1511 dlog_verbose(
1512 "Sending of memory handle %#x is already complete.\n",
1513 handle);
1514 return ffa_error(FFA_INVALID_PARAMETERS);
1515 }
1516
1517 if (share_state->fragment_count == MAX_FRAGMENTS) {
1518 /*
1519 * Log a warning as this is a sign that MAX_FRAGMENTS should
1520 * probably be increased.
1521 */
1522 dlog_warning(
1523 "Too many fragments for memory share with handle %#x; "
1524 "only %d supported.\n",
1525 handle, MAX_FRAGMENTS);
1526 /* Free share state, as it's not possible to complete it. */
1527 share_state_free(share_states, share_state, page_pool);
1528 return ffa_error(FFA_NO_MEMORY);
1529 }
1530
1531 *share_state_ret = share_state;
1532
1533 return (struct ffa_value){.func = FFA_SUCCESS_32};
1534}
1535
1536/**
1537 * Forwards a memory send continuation message on to the TEE.
1538 */
1539static struct ffa_value memory_send_continue_tee_forward(
1540 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1541 uint32_t fragment_length, ffa_memory_handle_t handle)
1542{
1543 struct ffa_value ret;
1544
1545 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1546 fragment_length);
1547 tee_locked.vm->mailbox.recv_size = fragment_length;
1548 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1549 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1550 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001551 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001552 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1553 .arg1 = (uint32_t)handle,
1554 .arg2 = (uint32_t)(handle >> 32),
1555 .arg3 = fragment_length,
1556 .arg4 = (uint64_t)sender_vm_id << 16});
1557 /*
1558 * After the call to the TEE completes it must have finished reading its
1559 * RX buffer, so it is ready for another message.
1560 */
1561 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1562
1563 return ret;
1564}
1565
1566/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001567 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1568 * updates the stage-2 page tables. Specifically, check if the message length
1569 * and number of memory region constituents match, and if the transition is
1570 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001571 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001572 * Assumes that the caller has already found and locked the sender VM and copied
1573 * the memory region descriptor from the sender's TX buffer to a freshly
1574 * allocated page from Hafnium's internal pool. The caller must have also
1575 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001576 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001577 * This function takes ownership of the `memory_region` passed in and will free
1578 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001579 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001580struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001582 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001583 uint32_t fragment_length, uint32_t share_func,
1584 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001585{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001586 ffa_memory_access_permissions_t permissions;
1587 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001588 struct share_states_locked share_states;
1589 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001590
1591 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001592 * If there is an error validating the `memory_region` then we need to
1593 * free it because we own it but we won't be storing it in a share state
1594 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001595 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001596 ret = ffa_memory_send_validate(from_locked, memory_region,
1597 memory_share_length, fragment_length,
1598 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001600 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001601 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001602 }
1603
Andrew Walbrana65a1322020-04-06 19:32:32 +01001604 /* Set flag for share function, ready to be retrieved later. */
1605 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001607 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001608 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001609 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001610 case FFA_MEM_LEND_32:
1611 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001612 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001614 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001617 }
1618
Andrew Walbranca808b12020-05-15 17:22:28 +01001619 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001620 /*
1621 * Allocate a share state before updating the page table. Otherwise if
1622 * updating the page table succeeded but allocating the share state
1623 * failed then it would leave the memory in a state where nobody could
1624 * get it back.
1625 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001626 if (!allocate_share_state(share_states, share_func, memory_region,
1627 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1628 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001629 dlog_verbose("Failed to allocate share state.\n");
1630 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001631 ret = ffa_error(FFA_NO_MEMORY);
1632 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001633 }
1634
Andrew Walbranca808b12020-05-15 17:22:28 +01001635 if (fragment_length == memory_share_length) {
1636 /* No more fragments to come, everything fit in one message. */
1637 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001638 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001639 } else {
1640 ret = (struct ffa_value){
1641 .func = FFA_MEM_FRAG_RX_32,
1642 .arg1 = (uint32_t)share_state->handle,
1643 .arg2 = (uint32_t)(share_state->handle >> 32),
1644 .arg3 = fragment_length};
1645 }
1646
1647out:
1648 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001649 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001651}
1652
1653/**
1654 * Validates a call to donate, lend or share memory to the TEE and then updates
1655 * the stage-2 page tables. Specifically, check if the message length and number
1656 * of memory region constituents match, and if the transition is valid for the
1657 * type of memory sending operation.
1658 *
1659 * Assumes that the caller has already found and locked the sender VM and the
1660 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1661 * to a freshly allocated page from Hafnium's internal pool. The caller must
1662 * have also validated that the receiver VM ID is valid.
1663 *
1664 * This function takes ownership of the `memory_region` passed in and will free
1665 * it when necessary; it must not be freed by the caller.
1666 */
1667struct ffa_value ffa_memory_tee_send(
1668 struct vm_locked from_locked, struct vm_locked to_locked,
1669 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1670 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1671{
1672 ffa_memory_access_permissions_t permissions;
1673 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001674
1675 /*
1676 * If there is an error validating the `memory_region` then we need to
1677 * free it because we own it but we won't be storing it in a share state
1678 * after all.
1679 */
1680 ret = ffa_memory_send_validate(from_locked, memory_region,
1681 memory_share_length, fragment_length,
1682 share_func, &permissions);
1683 if (ret.func != FFA_SUCCESS_32) {
1684 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001685 }
1686
Andrew Walbranca808b12020-05-15 17:22:28 +01001687 if (fragment_length == memory_share_length) {
1688 /* No more fragments to come, everything fit in one message. */
1689 struct ffa_composite_memory_region *composite =
1690 ffa_memory_region_get_composite(memory_region, 0);
1691 struct ffa_memory_region_constituent *constituents =
1692 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001693 struct mpool local_page_pool;
1694 uint32_t orig_from_mode;
1695
1696 /*
1697 * Use a local page pool so that we can roll back if necessary.
1698 */
1699 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001700
1701 ret = ffa_send_check_update(
1702 from_locked, &constituents,
1703 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001704 permissions, &local_page_pool,
1705 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1706 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001707 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001708 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001709 goto out;
1710 }
1711
1712 /* Forward memory send message on to TEE. */
1713 ret = memory_send_tee_forward(
1714 to_locked, from_locked.vm->id, share_func,
1715 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001716
1717 if (ret.func != FFA_SUCCESS_32) {
1718 dlog_verbose(
1719 "TEE didn't successfully complete memory send "
1720 "operation; returned %#x (%d). Rolling back.\n",
1721 ret.func, ret.arg2);
1722
1723 /*
1724 * The TEE failed to complete the send operation, so
1725 * roll back the page table update for the VM. This
1726 * can't fail because it won't try to allocate more
1727 * memory than was freed into the `local_page_pool` by
1728 * `ffa_send_check_update` in the initial update.
1729 */
1730 CHECK(ffa_region_group_identity_map(
1731 from_locked, &constituents,
1732 &composite->constituent_count, 1,
1733 orig_from_mode, &local_page_pool, true));
1734 }
1735
1736 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001737 } else {
1738 struct share_states_locked share_states = share_states_lock();
1739 ffa_memory_handle_t handle;
1740
1741 /*
1742 * We need to wait for the rest of the fragments before we can
1743 * check whether the transaction is valid and unmap the memory.
1744 * Call the TEE so it can do its initial validation and assign a
1745 * handle, and allocate a share state to keep what we have so
1746 * far.
1747 */
1748 ret = memory_send_tee_forward(
1749 to_locked, from_locked.vm->id, share_func,
1750 memory_region, memory_share_length, fragment_length);
1751 if (ret.func == FFA_ERROR_32) {
1752 goto out_unlock;
1753 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1754 dlog_warning(
1755 "Got %#x from TEE in response to %#x for "
1756 "fragment with with %d/%d, expected "
1757 "FFA_MEM_FRAG_RX.\n",
1758 ret.func, share_func, fragment_length,
1759 memory_share_length);
1760 ret = ffa_error(FFA_INVALID_PARAMETERS);
1761 goto out_unlock;
1762 }
1763 handle = ffa_frag_handle(ret);
1764 if (ret.arg3 != fragment_length) {
1765 dlog_warning(
1766 "Got unexpected fragment offset %d for "
1767 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1768 ret.arg3, fragment_length);
1769 ret = ffa_error(FFA_INVALID_PARAMETERS);
1770 goto out_unlock;
1771 }
1772 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1773 dlog_warning(
1774 "Got unexpected sender ID %d for "
1775 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1776 ffa_frag_sender(ret), from_locked.vm->id);
1777 ret = ffa_error(FFA_INVALID_PARAMETERS);
1778 goto out_unlock;
1779 }
1780
1781 if (!allocate_share_state(share_states, share_func,
1782 memory_region, fragment_length,
1783 handle, NULL)) {
1784 dlog_verbose("Failed to allocate share state.\n");
1785 ret = ffa_error(FFA_NO_MEMORY);
1786 goto out_unlock;
1787 }
1788 /*
1789 * Don't free the memory region fragment, as it has been stored
1790 * in the share state.
1791 */
1792 memory_region = NULL;
1793 out_unlock:
1794 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001795 }
1796
Andrew Walbranca808b12020-05-15 17:22:28 +01001797out:
1798 if (memory_region != NULL) {
1799 mpool_free(page_pool, memory_region);
1800 }
1801 dump_share_states();
1802 return ret;
1803}
1804
1805/**
1806 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1807 * this is the last fragment then checks that the transition is valid for the
1808 * type of memory sending operation and updates the stage-2 page tables of the
1809 * sender.
1810 *
1811 * Assumes that the caller has already found and locked the sender VM and copied
1812 * the memory region descriptor from the sender's TX buffer to a freshly
1813 * allocated page from Hafnium's internal pool.
1814 *
1815 * This function takes ownership of the `fragment` passed in; it must not be
1816 * freed by the caller.
1817 */
1818struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1819 void *fragment,
1820 uint32_t fragment_length,
1821 ffa_memory_handle_t handle,
1822 struct mpool *page_pool)
1823{
1824 struct share_states_locked share_states = share_states_lock();
1825 struct ffa_memory_share_state *share_state;
1826 struct ffa_value ret;
1827 struct ffa_memory_region *memory_region;
1828
1829 ret = ffa_memory_send_continue_validate(share_states, handle,
1830 &share_state,
1831 from_locked.vm->id, page_pool);
1832 if (ret.func != FFA_SUCCESS_32) {
1833 goto out_free_fragment;
1834 }
1835 memory_region = share_state->memory_region;
1836
1837 if (memory_region->receivers[0].receiver_permissions.receiver ==
1838 HF_TEE_VM_ID) {
1839 dlog_error(
1840 "Got hypervisor-allocated handle for memory send to "
1841 "TEE. This should never happen, and indicates a bug in "
1842 "EL3 code.\n");
1843 ret = ffa_error(FFA_INVALID_PARAMETERS);
1844 goto out_free_fragment;
1845 }
1846
1847 /* Add this fragment. */
1848 share_state->fragments[share_state->fragment_count] = fragment;
1849 share_state->fragment_constituent_counts[share_state->fragment_count] =
1850 fragment_length / sizeof(struct ffa_memory_region_constituent);
1851 share_state->fragment_count++;
1852
1853 /* Check whether the memory send operation is now ready to complete. */
1854 if (share_state_sending_complete(share_states, share_state)) {
1855 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001856 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001857 } else {
1858 ret = (struct ffa_value){
1859 .func = FFA_MEM_FRAG_RX_32,
1860 .arg1 = (uint32_t)handle,
1861 .arg2 = (uint32_t)(handle >> 32),
1862 .arg3 = share_state_next_fragment_offset(share_states,
1863 share_state)};
1864 }
1865 goto out;
1866
1867out_free_fragment:
1868 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001869
1870out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001871 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001872 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001873}
1874
Andrew Walbranca808b12020-05-15 17:22:28 +01001875/**
1876 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1877 * is the last fragment then checks that the transition is valid for the type of
1878 * memory sending operation and updates the stage-2 page tables of the sender.
1879 *
1880 * Assumes that the caller has already found and locked the sender VM and copied
1881 * the memory region descriptor from the sender's TX buffer to a freshly
1882 * allocated page from Hafnium's internal pool.
1883 *
1884 * This function takes ownership of the `memory_region` passed in and will free
1885 * it when necessary; it must not be freed by the caller.
1886 */
1887struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1888 struct vm_locked to_locked,
1889 void *fragment,
1890 uint32_t fragment_length,
1891 ffa_memory_handle_t handle,
1892 struct mpool *page_pool)
1893{
1894 struct share_states_locked share_states = share_states_lock();
1895 struct ffa_memory_share_state *share_state;
1896 struct ffa_value ret;
1897 struct ffa_memory_region *memory_region;
1898
1899 ret = ffa_memory_send_continue_validate(share_states, handle,
1900 &share_state,
1901 from_locked.vm->id, page_pool);
1902 if (ret.func != FFA_SUCCESS_32) {
1903 goto out_free_fragment;
1904 }
1905 memory_region = share_state->memory_region;
1906
1907 if (memory_region->receivers[0].receiver_permissions.receiver !=
1908 HF_TEE_VM_ID) {
1909 dlog_error(
1910 "Got SPM-allocated handle for memory send to non-TEE "
1911 "VM. This should never happen, and indicates a bug.\n");
1912 ret = ffa_error(FFA_INVALID_PARAMETERS);
1913 goto out_free_fragment;
1914 }
1915
1916 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1917 to_locked.vm->mailbox.recv == NULL) {
1918 /*
1919 * If the TEE RX buffer is not available, tell the sender to
1920 * retry by returning the current offset again.
1921 */
1922 ret = (struct ffa_value){
1923 .func = FFA_MEM_FRAG_RX_32,
1924 .arg1 = (uint32_t)handle,
1925 .arg2 = (uint32_t)(handle >> 32),
1926 .arg3 = share_state_next_fragment_offset(share_states,
1927 share_state),
1928 };
1929 goto out_free_fragment;
1930 }
1931
1932 /* Add this fragment. */
1933 share_state->fragments[share_state->fragment_count] = fragment;
1934 share_state->fragment_constituent_counts[share_state->fragment_count] =
1935 fragment_length / sizeof(struct ffa_memory_region_constituent);
1936 share_state->fragment_count++;
1937
1938 /* Check whether the memory send operation is now ready to complete. */
1939 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001940 struct mpool local_page_pool;
1941 uint32_t orig_from_mode;
1942
1943 /*
1944 * Use a local page pool so that we can roll back if necessary.
1945 */
1946 mpool_init_with_fallback(&local_page_pool, page_pool);
1947
Andrew Walbranca808b12020-05-15 17:22:28 +01001948 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001949 share_state, &local_page_pool,
1950 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001951
1952 if (ret.func == FFA_SUCCESS_32) {
1953 /*
1954 * Forward final fragment on to the TEE so that
1955 * it can complete the memory sending operation.
1956 */
1957 ret = memory_send_continue_tee_forward(
1958 to_locked, from_locked.vm->id, fragment,
1959 fragment_length, handle);
1960
1961 if (ret.func != FFA_SUCCESS_32) {
1962 /*
1963 * The error will be passed on to the caller,
1964 * but log it here too.
1965 */
1966 dlog_verbose(
1967 "TEE didn't successfully complete "
1968 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001969 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001970 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001971
1972 /*
1973 * The TEE failed to complete the send
1974 * operation, so roll back the page table update
1975 * for the VM. This can't fail because it won't
1976 * try to allocate more memory than was freed
1977 * into the `local_page_pool` by
1978 * `ffa_send_check_update` in the initial
1979 * update.
1980 */
1981 CHECK(ffa_region_group_identity_map(
1982 from_locked, share_state->fragments,
1983 share_state
1984 ->fragment_constituent_counts,
1985 share_state->fragment_count,
1986 orig_from_mode, &local_page_pool,
1987 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001988 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001989
Andrew Walbranca808b12020-05-15 17:22:28 +01001990 /* Free share state. */
1991 share_state_free(share_states, share_state, page_pool);
1992 } else {
1993 /* Abort sending to TEE. */
1994 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02001995 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01001996 .func = FFA_MEM_RECLAIM_32,
1997 .arg1 = (uint32_t)handle,
1998 .arg2 = (uint32_t)(handle >> 32)});
1999
2000 if (tee_ret.func != FFA_SUCCESS_32) {
2001 /*
2002 * Nothing we can do if TEE doesn't abort
2003 * properly, just log it.
2004 */
2005 dlog_verbose(
2006 "TEE didn't successfully abort failed "
2007 "memory send operation; returned %#x "
2008 "(%d).\n",
2009 tee_ret.func, tee_ret.arg2);
2010 }
2011 /*
2012 * We don't need to free the share state in this case
2013 * because ffa_memory_send_complete does that already.
2014 */
2015 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002016
2017 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002018 } else {
2019 uint32_t next_fragment_offset =
2020 share_state_next_fragment_offset(share_states,
2021 share_state);
2022
2023 ret = memory_send_continue_tee_forward(
2024 to_locked, from_locked.vm->id, fragment,
2025 fragment_length, handle);
2026
2027 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2028 ffa_frag_handle(ret) != handle ||
2029 ret.arg3 != next_fragment_offset ||
2030 ffa_frag_sender(ret) != from_locked.vm->id) {
2031 dlog_verbose(
2032 "Got unexpected result from forwarding "
2033 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2034 "offset %d, sender %d); expected "
2035 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2036 "sender %d).\n",
2037 ret.func, ffa_frag_handle(ret), ret.arg3,
2038 ffa_frag_sender(ret), handle,
2039 next_fragment_offset, from_locked.vm->id);
2040 /* Free share state. */
2041 share_state_free(share_states, share_state, page_pool);
2042 ret = ffa_error(FFA_INVALID_PARAMETERS);
2043 goto out;
2044 }
2045
2046 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2047 .arg1 = (uint32_t)handle,
2048 .arg2 = (uint32_t)(handle >> 32),
2049 .arg3 = next_fragment_offset};
2050 }
2051 goto out;
2052
2053out_free_fragment:
2054 mpool_free(page_pool, fragment);
2055
2056out:
2057 share_states_unlock(&share_states);
2058 return ret;
2059}
2060
2061/** Clean up after the receiver has finished retrieving a memory region. */
2062static void ffa_memory_retrieve_complete(
2063 struct share_states_locked share_states,
2064 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2065{
2066 if (share_state->share_func == FFA_MEM_DONATE_32) {
2067 /*
2068 * Memory that has been donated can't be relinquished,
2069 * so no need to keep the share state around.
2070 */
2071 share_state_free(share_states, share_state, page_pool);
2072 dlog_verbose("Freed share state for donate.\n");
2073 }
2074}
2075
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002076struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2077 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002078 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002079 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002080{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002081 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002082 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002083 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002084 sizeof(struct ffa_memory_access);
2085 ffa_memory_handle_t handle = retrieve_request->handle;
2086 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002087 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002088 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2089 struct ffa_memory_region *memory_region;
2090 ffa_memory_access_permissions_t sent_permissions;
2091 enum ffa_data_access sent_data_access;
2092 enum ffa_instruction_access sent_instruction_access;
2093 ffa_memory_access_permissions_t requested_permissions;
2094 enum ffa_data_access requested_data_access;
2095 enum ffa_instruction_access requested_instruction_access;
2096 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002097 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002098 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002099 struct ffa_memory_share_state *share_state;
2100 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002101 struct ffa_composite_memory_region *composite;
2102 uint32_t total_length;
2103 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002104
2105 dump_share_states();
2106
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002107 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002108 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002109 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002110 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002111 expected_retrieve_request_length,
2112 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002113 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002114 }
2115
Andrew Walbrana65a1322020-04-06 19:32:32 +01002116 if (retrieve_request->receiver_count != 1) {
2117 dlog_verbose(
2118 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002119 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002120 "expected 1).\n",
2121 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002122 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002123 }
2124
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002125 share_states = share_states_lock();
2126 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002127 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002128 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002129 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002130 goto out;
2131 }
2132
Andrew Walbrana65a1322020-04-06 19:32:32 +01002133 memory_region = share_state->memory_region;
2134 CHECK(memory_region != NULL);
2135
2136 /*
2137 * Check that the transaction type expected by the receiver is correct,
2138 * if it has been specified.
2139 */
2140 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002141 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002142 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002143 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002144 dlog_verbose(
2145 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002146 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002147 transaction_type,
2148 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002149 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002150 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002151 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002152 goto out;
2153 }
2154
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002155 if (retrieve_request->sender != memory_region->sender) {
2156 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002157 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002158 "expected %d for handle %#x.\n",
2159 retrieve_request->sender, memory_region->sender,
2160 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002161 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002162 goto out;
2163 }
2164
2165 if (retrieve_request->tag != memory_region->tag) {
2166 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002167 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002168 "%d for handle %#x.\n",
2169 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002170 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002171 goto out;
2172 }
2173
Andrew Walbrana65a1322020-04-06 19:32:32 +01002174 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2175 to_locked.vm->id) {
2176 dlog_verbose(
2177 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002178 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002179 retrieve_request->receivers[0]
2180 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002181 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002182 goto out;
2183 }
2184
2185 if (memory_region->receivers[0].receiver_permissions.receiver !=
2186 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002187 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002188 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2189 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002190 to_locked.vm->id,
2191 memory_region->receivers[0]
2192 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002193 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002194 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002195 goto out;
2196 }
2197
Andrew Walbranca808b12020-05-15 17:22:28 +01002198 if (!share_state->sending_complete) {
2199 dlog_verbose(
2200 "Memory with handle %#x not fully sent, can't "
2201 "retrieve.\n",
2202 handle);
2203 ret = ffa_error(FFA_INVALID_PARAMETERS);
2204 goto out;
2205 }
2206
2207 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002208 dlog_verbose("Memory with handle %#x already retrieved.\n",
2209 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002210 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002211 goto out;
2212 }
2213
Andrew Walbrana65a1322020-04-06 19:32:32 +01002214 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2215 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002216 dlog_verbose(
2217 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002218 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002219 retrieve_request->receivers[0]
2220 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002221 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002222 goto out;
2223 }
2224
Andrew Walbrana65a1322020-04-06 19:32:32 +01002225 /*
2226 * Check permissions from sender against permissions requested by
2227 * receiver.
2228 */
2229 /* TODO: Check attributes too. */
2230 sent_permissions =
2231 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002232 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002234 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002235 requested_permissions =
2236 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002237 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002238 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002240 permissions = 0;
2241 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002242 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2243 case FFA_DATA_ACCESS_RW:
2244 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2245 requested_data_access == FFA_DATA_ACCESS_RW) {
2246 ffa_set_data_access_attr(&permissions,
2247 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002248 break;
2249 }
2250 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002251 case FFA_DATA_ACCESS_RO:
2252 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2253 requested_data_access == FFA_DATA_ACCESS_RO) {
2254 ffa_set_data_access_attr(&permissions,
2255 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002256 break;
2257 }
2258 dlog_verbose(
2259 "Invalid data access requested; sender specified "
2260 "permissions %#x but receiver requested %#x.\n",
2261 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002263 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002264 case FFA_DATA_ACCESS_RESERVED:
2265 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002266 "checked before this point.");
2267 }
2268 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002269 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2270 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002271 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002272 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2273 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2274 ffa_set_instruction_access_attr(
2275 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002276 break;
2277 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002279 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2281 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2282 ffa_set_instruction_access_attr(
2283 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002284 break;
2285 }
2286 dlog_verbose(
2287 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002288 "specified permissions %#x but receiver requested "
2289 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002290 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002291 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002292 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002293 case FFA_INSTRUCTION_ACCESS_RESERVED:
2294 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002295 "be checked before this point.");
2296 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002297 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002298
Andrew Walbran996d1d12020-05-27 14:08:43 +01002299 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002300 to_locked, share_state->fragments,
2301 share_state->fragment_constituent_counts,
2302 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002303 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002304 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002305 goto out;
2306 }
2307
2308 /*
2309 * Copy response to RX buffer of caller and deliver the message. This
2310 * must be done before the share_state is (possibly) freed.
2311 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002312 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002313 composite = ffa_memory_region_get_composite(memory_region, 0);
2314 /*
2315 * Constituents which we received in the first fragment should always
2316 * fit in the first fragment we are sending, because the header is the
2317 * same size in both cases and we have a fixed message buffer size. So
2318 * `ffa_retrieved_memory_region_init` should never fail.
2319 */
2320 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002321 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2322 memory_region->sender, memory_region->attributes,
2323 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002324 composite->page_count, composite->constituent_count,
2325 share_state->fragments[0],
2326 share_state->fragment_constituent_counts[0], &total_length,
2327 &fragment_length));
2328 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002330 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002331 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2332
Andrew Walbranca808b12020-05-15 17:22:28 +01002333 share_state->retrieved_fragment_count[0] = 1;
2334 if (share_state->retrieved_fragment_count[0] ==
2335 share_state->fragment_count) {
2336 ffa_memory_retrieve_complete(share_states, share_state,
2337 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002338 }
2339
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002340 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002341 .arg1 = total_length,
2342 .arg2 = fragment_length};
2343
2344out:
2345 share_states_unlock(&share_states);
2346 dump_share_states();
2347 return ret;
2348}
2349
2350struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2351 ffa_memory_handle_t handle,
2352 uint32_t fragment_offset,
2353 struct mpool *page_pool)
2354{
2355 struct ffa_memory_region *memory_region;
2356 struct share_states_locked share_states;
2357 struct ffa_memory_share_state *share_state;
2358 struct ffa_value ret;
2359 uint32_t fragment_index;
2360 uint32_t retrieved_constituents_count;
2361 uint32_t i;
2362 uint32_t expected_fragment_offset;
2363 uint32_t remaining_constituent_count;
2364 uint32_t fragment_length;
2365
2366 dump_share_states();
2367
2368 share_states = share_states_lock();
2369 if (!get_share_state(share_states, handle, &share_state)) {
2370 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2371 handle);
2372 ret = ffa_error(FFA_INVALID_PARAMETERS);
2373 goto out;
2374 }
2375
2376 memory_region = share_state->memory_region;
2377 CHECK(memory_region != NULL);
2378
2379 if (memory_region->receivers[0].receiver_permissions.receiver !=
2380 to_locked.vm->id) {
2381 dlog_verbose(
2382 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2383 "of handle %#x.\n",
2384 to_locked.vm->id,
2385 memory_region->receivers[0]
2386 .receiver_permissions.receiver,
2387 handle);
2388 ret = ffa_error(FFA_INVALID_PARAMETERS);
2389 goto out;
2390 }
2391
2392 if (!share_state->sending_complete) {
2393 dlog_verbose(
2394 "Memory with handle %#x not fully sent, can't "
2395 "retrieve.\n",
2396 handle);
2397 ret = ffa_error(FFA_INVALID_PARAMETERS);
2398 goto out;
2399 }
2400
2401 if (share_state->retrieved_fragment_count[0] == 0 ||
2402 share_state->retrieved_fragment_count[0] >=
2403 share_state->fragment_count) {
2404 dlog_verbose(
2405 "Retrieval of memory with handle %#x not yet started "
2406 "or already completed (%d/%d fragments retrieved).\n",
2407 handle, share_state->retrieved_fragment_count[0],
2408 share_state->fragment_count);
2409 ret = ffa_error(FFA_INVALID_PARAMETERS);
2410 goto out;
2411 }
2412
2413 fragment_index = share_state->retrieved_fragment_count[0];
2414
2415 /*
2416 * Check that the given fragment offset is correct by counting how many
2417 * constituents were in the fragments previously sent.
2418 */
2419 retrieved_constituents_count = 0;
2420 for (i = 0; i < fragment_index; ++i) {
2421 retrieved_constituents_count +=
2422 share_state->fragment_constituent_counts[i];
2423 }
2424 expected_fragment_offset =
2425 ffa_composite_constituent_offset(memory_region, 0) +
2426 retrieved_constituents_count *
2427 sizeof(struct ffa_memory_region_constituent);
2428 if (fragment_offset != expected_fragment_offset) {
2429 dlog_verbose("Fragment offset was %d but expected %d.\n",
2430 fragment_offset, expected_fragment_offset);
2431 ret = ffa_error(FFA_INVALID_PARAMETERS);
2432 goto out;
2433 }
2434
2435 remaining_constituent_count = ffa_memory_fragment_init(
2436 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2437 share_state->fragments[fragment_index],
2438 share_state->fragment_constituent_counts[fragment_index],
2439 &fragment_length);
2440 CHECK(remaining_constituent_count == 0);
2441 to_locked.vm->mailbox.recv_size = fragment_length;
2442 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2443 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2444 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2445 share_state->retrieved_fragment_count[0]++;
2446 if (share_state->retrieved_fragment_count[0] ==
2447 share_state->fragment_count) {
2448 ffa_memory_retrieve_complete(share_states, share_state,
2449 page_pool);
2450 }
2451
2452 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2453 .arg1 = (uint32_t)handle,
2454 .arg2 = (uint32_t)(handle >> 32),
2455 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002456
2457out:
2458 share_states_unlock(&share_states);
2459 dump_share_states();
2460 return ret;
2461}
2462
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002464 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002468 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002469 struct ffa_memory_share_state *share_state;
2470 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002472 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002473
Andrew Walbrana65a1322020-04-06 19:32:32 +01002474 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002475 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002476 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002477 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002478 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002479 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480 }
2481
Andrew Walbrana65a1322020-04-06 19:32:32 +01002482 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 dlog_verbose(
2484 "VM ID %d in relinquish message doesn't match calling "
2485 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002486 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002487 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002488 }
2489
2490 dump_share_states();
2491
2492 share_states = share_states_lock();
2493 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002496 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002497 goto out;
2498 }
2499
Andrew Walbranca808b12020-05-15 17:22:28 +01002500 if (!share_state->sending_complete) {
2501 dlog_verbose(
2502 "Memory with handle %#x not fully sent, can't "
2503 "relinquish.\n",
2504 handle);
2505 ret = ffa_error(FFA_INVALID_PARAMETERS);
2506 goto out;
2507 }
2508
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002509 memory_region = share_state->memory_region;
2510 CHECK(memory_region != NULL);
2511
Andrew Walbrana65a1322020-04-06 19:32:32 +01002512 if (memory_region->receivers[0].receiver_permissions.receiver !=
2513 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002514 dlog_verbose(
2515 "VM ID %d tried to relinquish memory region with "
2516 "handle %#x but receiver was %d.\n",
2517 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002518 memory_region->receivers[0]
2519 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002520 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521 goto out;
2522 }
2523
Andrew Walbranca808b12020-05-15 17:22:28 +01002524 if (share_state->retrieved_fragment_count[0] !=
2525 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002527 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002528 "relinquish.\n",
2529 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531 goto out;
2532 }
2533
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002534 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535
2536 /*
2537 * Clear is not allowed for memory that was shared, as the original
2538 * sender still has access to the memory.
2539 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002541 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002542 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002543 goto out;
2544 }
2545
Andrew Walbranca808b12020-05-15 17:22:28 +01002546 ret = ffa_relinquish_check_update(
2547 from_locked, share_state->fragments,
2548 share_state->fragment_constituent_counts,
2549 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 /*
2553 * Mark memory handle as not retrieved, so it can be reclaimed
2554 * (or retrieved again).
2555 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002556 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557 }
2558
2559out:
2560 share_states_unlock(&share_states);
2561 dump_share_states();
2562 return ret;
2563}
2564
2565/**
2566 * Validates that the reclaim transition is allowed for the given handle,
2567 * updates the page table of the reclaiming VM, and frees the internal state
2568 * associated with the handle.
2569 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002570struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002571 ffa_memory_handle_t handle,
2572 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002573 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002574{
2575 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002576 struct ffa_memory_share_state *share_state;
2577 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002578 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002579 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002580
2581 dump_share_states();
2582
2583 share_states = share_states_lock();
2584 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002585 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002586 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002587 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002588 goto out;
2589 }
2590
2591 memory_region = share_state->memory_region;
2592 CHECK(memory_region != NULL);
2593
2594 if (to_locked.vm->id != memory_region->sender) {
2595 dlog_verbose(
2596 "VM %d attempted to reclaim memory handle %#x "
2597 "originally sent by VM %d.\n",
2598 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002599 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002600 goto out;
2601 }
2602
Andrew Walbranca808b12020-05-15 17:22:28 +01002603 if (!share_state->sending_complete) {
2604 dlog_verbose(
2605 "Memory with handle %#x not fully sent, can't "
2606 "reclaim.\n",
2607 handle);
2608 ret = ffa_error(FFA_INVALID_PARAMETERS);
2609 goto out;
2610 }
2611
2612 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002613 dlog_verbose(
2614 "Tried to reclaim memory handle %#x that has not been "
2615 "relinquished.\n",
2616 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002617 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002618 goto out;
2619 }
2620
Andrew Walbranca808b12020-05-15 17:22:28 +01002621 ret = ffa_retrieve_check_update(
2622 to_locked, share_state->fragments,
2623 share_state->fragment_constituent_counts,
2624 share_state->fragment_count, memory_to_attributes,
2625 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002626
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002627 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002628 share_state_free(share_states, share_state, page_pool);
2629 dlog_verbose("Freed share state after successful reclaim.\n");
2630 }
2631
2632out:
2633 share_states_unlock(&share_states);
2634 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002635}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002636
2637/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002638 * Validates that the reclaim transition is allowed for the memory region with
2639 * the given handle which was previously shared with the TEE, tells the TEE to
2640 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2641 *
2642 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002643 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002644struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002645 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002646 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002647 ffa_memory_region_flags_t flags,
2648 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002649{
Andrew Walbranca808b12020-05-15 17:22:28 +01002650 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2651 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2652 struct ffa_value tee_ret;
2653 uint32_t length;
2654 uint32_t fragment_length;
2655 uint32_t fragment_offset;
2656 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002657 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002658 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2659
2660 CHECK(request_length <= HF_MAILBOX_SIZE);
2661 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2662
2663 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002664 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002665 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2666 .arg1 = request_length,
2667 .arg2 = request_length});
2668 if (tee_ret.func == FFA_ERROR_32) {
2669 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2670 return tee_ret;
2671 }
2672 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2673 dlog_verbose(
2674 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2675 tee_ret.func);
2676 return ffa_error(FFA_INVALID_PARAMETERS);
2677 }
2678
2679 length = tee_ret.arg1;
2680 fragment_length = tee_ret.arg2;
2681
2682 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2683 length > sizeof(tee_retrieve_buffer)) {
2684 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2685 fragment_length, length, HF_MAILBOX_SIZE,
2686 sizeof(tee_retrieve_buffer));
2687 return ffa_error(FFA_INVALID_PARAMETERS);
2688 }
2689
2690 /*
2691 * Copy the first fragment of the memory region descriptor to an
2692 * internal buffer.
2693 */
2694 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2695 from_locked.vm->mailbox.send, fragment_length);
2696
2697 /* Fetch the remaining fragments into the same buffer. */
2698 fragment_offset = fragment_length;
2699 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002700 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002701 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2702 .arg1 = (uint32_t)handle,
2703 .arg2 = (uint32_t)(handle >> 32),
2704 .arg3 = fragment_offset});
2705 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2706 dlog_verbose(
2707 "Got %#x (%d) from TEE in response to "
2708 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2709 tee_ret.func, tee_ret.arg2);
2710 return tee_ret;
2711 }
2712 if (ffa_frag_handle(tee_ret) != handle) {
2713 dlog_verbose(
2714 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2715 "in response to FFA_MEM_FRAG_RX for handle "
2716 "%#x.\n",
2717 ffa_frag_handle(tee_ret), handle);
2718 return ffa_error(FFA_INVALID_PARAMETERS);
2719 }
2720 if (ffa_frag_sender(tee_ret) != 0) {
2721 dlog_verbose(
2722 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2723 "(expected 0).\n",
2724 ffa_frag_sender(tee_ret));
2725 return ffa_error(FFA_INVALID_PARAMETERS);
2726 }
2727 fragment_length = tee_ret.arg3;
2728 if (fragment_length > HF_MAILBOX_SIZE ||
2729 fragment_offset + fragment_length > length) {
2730 dlog_verbose(
2731 "Invalid fragment length %d at offset %d (max "
2732 "%d).\n",
2733 fragment_length, fragment_offset,
2734 HF_MAILBOX_SIZE);
2735 return ffa_error(FFA_INVALID_PARAMETERS);
2736 }
2737 memcpy_s(tee_retrieve_buffer + fragment_offset,
2738 sizeof(tee_retrieve_buffer) - fragment_offset,
2739 from_locked.vm->mailbox.send, fragment_length);
2740
2741 fragment_offset += fragment_length;
2742 }
2743
2744 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002745
2746 if (memory_region->receiver_count != 1) {
2747 /* Only one receiver supported by Hafnium for now. */
2748 dlog_verbose(
2749 "Multiple recipients not supported (got %d, expected "
2750 "1).\n",
2751 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002752 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002753 }
2754
2755 if (memory_region->handle != handle) {
2756 dlog_verbose(
2757 "Got memory region handle %#x from TEE but requested "
2758 "handle %#x.\n",
2759 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002760 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002761 }
2762
2763 /* The original sender must match the caller. */
2764 if (to_locked.vm->id != memory_region->sender) {
2765 dlog_verbose(
2766 "VM %d attempted to reclaim memory handle %#x "
2767 "originally sent by VM %d.\n",
2768 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002769 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002770 }
2771
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002772 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002773
2774 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002775 * Validate that the reclaim transition is allowed for the given memory
2776 * region, forward the request to the TEE and then map the memory back
2777 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002778 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002779 return ffa_tee_reclaim_check_update(
2780 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002781 composite->constituent_count, memory_to_attributes,
2782 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002783}