blob: 699b63bf240db298a89adb9d863ca9ab6793ef2a [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
Andrew Walbran290b0c92020-02-03 16:37:14 +000011#include "hf/arch/tee.h"
12
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 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001140 ret = arch_tee_call((struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1141 .arg1 = (uint32_t)handle,
1142 .arg2 = (uint32_t)(handle >> 32),
1143 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001144
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001145 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001146 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001147 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1148 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001149 ret.func, ret.arg2);
1150 goto out;
1151 }
1152
1153 /*
1154 * The TEE was happy with it, so complete the reclaim by mapping the
1155 * memory into the recipient. This won't allocate because the
1156 * transaction was already prepared above, so it doesn't need to use the
1157 * `local_page_pool`.
1158 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001159 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1160 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001161 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001162
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001163 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001164
1165out:
1166 mpool_fini(&local_page_pool);
1167
1168 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001169 * Tidy up the page table by reclaiming failed mappings (if there was an
1170 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001171 */
1172 mm_vm_defrag(&to->ptable, page_pool);
1173
1174 return ret;
1175}
1176
Andrew Walbran996d1d12020-05-27 14:08:43 +01001177static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001178 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001179 struct ffa_memory_region_constituent **fragments,
1180 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1181 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001182{
1183 uint32_t orig_from_mode;
1184 uint32_t from_mode;
1185 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001187
Andrew Walbranca808b12020-05-15 17:22:28 +01001188 ret = ffa_relinquish_check_transition(
1189 from_locked, &orig_from_mode, fragments,
1190 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001191 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001192 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001193 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001194 }
1195
1196 /*
1197 * Create a local pool so any freed memory can't be used by another
1198 * thread. This is to ensure the original mapping can be restored if the
1199 * clear fails.
1200 */
1201 mpool_init_with_fallback(&local_page_pool, page_pool);
1202
1203 /*
1204 * First reserve all required memory for the new page table entries
1205 * without committing, to make sure the entire operation will succeed
1206 * without exhausting the page pool.
1207 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001208 if (!ffa_region_group_identity_map(
1209 from_locked, fragments, fragment_constituent_counts,
1210 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001211 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001213 goto out;
1214 }
1215
1216 /*
1217 * Update the mapping for the sender. This won't allocate because the
1218 * transaction was already prepared above, but may free pages in the
1219 * case that a whole block is being unmapped that was previously
1220 * partially mapped.
1221 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001222 CHECK(ffa_region_group_identity_map(
1223 from_locked, fragments, fragment_constituent_counts,
1224 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001225
1226 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001227 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001228 fragments, fragment_constituent_counts,
1229 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001230 /*
1231 * On failure, roll back by returning memory to the sender. This
1232 * may allocate pages which were previously freed into
1233 * `local_page_pool` by the call above, but will never allocate
1234 * more pages than that so can never fail.
1235 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001236 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001237 from_locked, fragments, fragment_constituent_counts,
1238 fragment_count, orig_from_mode, &local_page_pool,
1239 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001242 goto out;
1243 }
1244
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001245 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001246
1247out:
1248 mpool_fini(&local_page_pool);
1249
1250 /*
1251 * Tidy up the page table by reclaiming failed mappings (if there was an
1252 * error) or merging entries into blocks where possible (on success).
1253 */
1254 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001255
1256 return ret;
1257}
1258
1259/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001260 * Complete a memory sending operation by checking that it is valid, updating
1261 * the sender page table, and then either marking the share state as having
1262 * completed sending (on success) or freeing it (on failure).
1263 *
1264 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1265 */
1266static struct ffa_value ffa_memory_send_complete(
1267 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001268 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1269 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001270{
1271 struct ffa_memory_region *memory_region = share_state->memory_region;
1272 struct ffa_value ret;
1273
1274 /* Lock must be held. */
1275 CHECK(share_states.share_states != NULL);
1276
1277 /* Check that state is valid in sender page table and update. */
1278 ret = ffa_send_check_update(
1279 from_locked, share_state->fragments,
1280 share_state->fragment_constituent_counts,
1281 share_state->fragment_count, share_state->share_func,
1282 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001283 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1284 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001285 if (ret.func != FFA_SUCCESS_32) {
1286 /*
1287 * Free share state, it failed to send so it can't be retrieved.
1288 */
1289 dlog_verbose("Complete failed, freeing share state.\n");
1290 share_state_free(share_states, share_state, page_pool);
1291 return ret;
1292 }
1293
1294 share_state->sending_complete = true;
1295 dlog_verbose("Marked sending complete.\n");
1296
1297 return ffa_mem_success(share_state->handle);
1298}
1299
1300/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001301 * Check that the given `memory_region` represents a valid memory send request
1302 * of the given `share_func` type, return the clear flag and permissions via the
1303 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001304 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001306 * not.
1307 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001308static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001309 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1310 uint32_t memory_share_length, uint32_t fragment_length,
1311 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001312{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001313 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001314 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001315 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001316 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001317 enum ffa_data_access data_access;
1318 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001319
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320 CHECK(permissions != NULL);
1321
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001322 /*
1323 * This should already be checked by the caller, just making the
1324 * assumption clear here.
1325 */
1326 CHECK(memory_region->receiver_count == 1);
1327
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 /* The sender must match the message sender. */
1329 if (memory_region->sender != from_locked.vm->id) {
1330 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001331 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001332 }
1333
Andrew Walbrana65a1322020-04-06 19:32:32 +01001334 /*
1335 * Ensure that the composite header is within the memory bounds and
1336 * doesn't overlap the first part of the message.
1337 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001338 receivers_length = sizeof(struct ffa_memory_access) *
1339 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001340 constituents_offset =
1341 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001342 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001343 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001344 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001345 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001346 "Invalid composite memory region descriptor offset "
1347 "%d.\n",
1348 memory_region->receivers[0]
1349 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001350 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001351 }
1352
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001353 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001354
1355 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001356 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001357 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001358 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1359 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001360 if (memory_share_length != constituents_offset + constituents_length) {
1361 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001362 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001363 memory_region->receivers[0]
1364 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001365 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001366 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001367 if (fragment_length < memory_share_length &&
1368 fragment_length < HF_MAILBOX_SIZE) {
1369 dlog_warning(
1370 "Initial fragment length %d smaller than mailbox "
1371 "size.\n",
1372 fragment_length);
1373 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375 /*
1376 * Clear is not allowed for memory sharing, as the sender still has
1377 * access to the memory.
1378 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001379 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1380 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001383 }
1384
1385 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001386 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001387 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 }
1390
1391 /* Check that the permissions are valid. */
1392 *permissions =
1393 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001394 data_access = ffa_get_data_access_attr(*permissions);
1395 instruction_access = ffa_get_instruction_access_attr(*permissions);
1396 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1397 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001398 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1399 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001401 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001402 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001403 dlog_verbose(
1404 "Invalid instruction access permissions %#x for "
1405 "sending memory.\n",
1406 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001407 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001408 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 if (share_func == FFA_MEM_SHARE_32) {
1410 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001411 dlog_verbose(
1412 "Invalid data access permissions %#x for "
1413 "sharing memory.\n",
1414 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001416 }
1417 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001418 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1419 * required for share operations (but must not be specified by
1420 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421 * returned to the retriever.
1422 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001423 ffa_set_instruction_access_attr(permissions,
1424 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 memory_region->receivers[0].receiver_permissions.permissions =
1426 *permissions;
1427 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001428 if (share_func == FFA_MEM_LEND_32 &&
1429 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 dlog_verbose(
1431 "Invalid data access permissions %#x for lending "
1432 "memory.\n",
1433 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001434 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 if (share_func == FFA_MEM_DONATE_32 &&
1437 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001438 dlog_verbose(
1439 "Invalid data access permissions %#x for donating "
1440 "memory.\n",
1441 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001442 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001443 }
1444
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001445 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001446}
1447
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001448/** Forwards a memory send message on to the TEE. */
1449static struct ffa_value memory_send_tee_forward(
1450 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1451 uint32_t share_func, struct ffa_memory_region *memory_region,
1452 uint32_t memory_share_length, uint32_t fragment_length)
1453{
1454 struct ffa_value ret;
1455
1456 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1457 memory_region, fragment_length);
1458 tee_locked.vm->mailbox.recv_size = fragment_length;
1459 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1460 tee_locked.vm->mailbox.recv_func = share_func;
1461 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1462 ret = arch_tee_call((struct ffa_value){.func = share_func,
1463 .arg1 = memory_share_length,
1464 .arg2 = fragment_length});
1465 /*
1466 * After the call to the TEE completes it must have finished reading its
1467 * RX buffer, so it is ready for another message.
1468 */
1469 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1470
1471 return ret;
1472}
1473
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001475 * Gets the share state for continuing an operation to donate, lend or share
1476 * memory, and checks that it is a valid request.
1477 *
1478 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1479 * not.
1480 */
1481static struct ffa_value ffa_memory_send_continue_validate(
1482 struct share_states_locked share_states, ffa_memory_handle_t handle,
1483 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1484 struct mpool *page_pool)
1485{
1486 struct ffa_memory_share_state *share_state;
1487 struct ffa_memory_region *memory_region;
1488
1489 CHECK(share_state_ret != NULL);
1490
1491 /*
1492 * Look up the share state by handle and make sure that the VM ID
1493 * matches.
1494 */
1495 if (!get_share_state(share_states, handle, &share_state)) {
1496 dlog_verbose(
1497 "Invalid handle %#x for memory send continuation.\n",
1498 handle);
1499 return ffa_error(FFA_INVALID_PARAMETERS);
1500 }
1501 memory_region = share_state->memory_region;
1502
1503 if (memory_region->sender != from_vm_id) {
1504 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1505 return ffa_error(FFA_INVALID_PARAMETERS);
1506 }
1507
1508 if (share_state->sending_complete) {
1509 dlog_verbose(
1510 "Sending of memory handle %#x is already complete.\n",
1511 handle);
1512 return ffa_error(FFA_INVALID_PARAMETERS);
1513 }
1514
1515 if (share_state->fragment_count == MAX_FRAGMENTS) {
1516 /*
1517 * Log a warning as this is a sign that MAX_FRAGMENTS should
1518 * probably be increased.
1519 */
1520 dlog_warning(
1521 "Too many fragments for memory share with handle %#x; "
1522 "only %d supported.\n",
1523 handle, MAX_FRAGMENTS);
1524 /* Free share state, as it's not possible to complete it. */
1525 share_state_free(share_states, share_state, page_pool);
1526 return ffa_error(FFA_NO_MEMORY);
1527 }
1528
1529 *share_state_ret = share_state;
1530
1531 return (struct ffa_value){.func = FFA_SUCCESS_32};
1532}
1533
1534/**
1535 * Forwards a memory send continuation message on to the TEE.
1536 */
1537static struct ffa_value memory_send_continue_tee_forward(
1538 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1539 uint32_t fragment_length, ffa_memory_handle_t handle)
1540{
1541 struct ffa_value ret;
1542
1543 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1544 fragment_length);
1545 tee_locked.vm->mailbox.recv_size = fragment_length;
1546 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1547 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1548 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1549 ret = arch_tee_call(
1550 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1551 .arg1 = (uint32_t)handle,
1552 .arg2 = (uint32_t)(handle >> 32),
1553 .arg3 = fragment_length,
1554 .arg4 = (uint64_t)sender_vm_id << 16});
1555 /*
1556 * After the call to the TEE completes it must have finished reading its
1557 * RX buffer, so it is ready for another message.
1558 */
1559 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1560
1561 return ret;
1562}
1563
1564/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001565 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1566 * updates the stage-2 page tables. Specifically, check if the message length
1567 * and number of memory region constituents match, and if the transition is
1568 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001569 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001570 * Assumes that the caller has already found and locked the sender VM and copied
1571 * the memory region descriptor from the sender's TX buffer to a freshly
1572 * allocated page from Hafnium's internal pool. The caller must have also
1573 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001574 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001575 * This function takes ownership of the `memory_region` passed in and will free
1576 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001577 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001578struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001579 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001580 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001581 uint32_t fragment_length, uint32_t share_func,
1582 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001583{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001584 ffa_memory_access_permissions_t permissions;
1585 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001586 struct share_states_locked share_states;
1587 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001588
1589 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001590 * If there is an error validating the `memory_region` then we need to
1591 * free it because we own it but we won't be storing it in a share state
1592 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001593 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001594 ret = ffa_memory_send_validate(from_locked, memory_region,
1595 memory_share_length, fragment_length,
1596 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001597 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001598 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001599 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001600 }
1601
Andrew Walbrana65a1322020-04-06 19:32:32 +01001602 /* Set flag for share function, ready to be retrieved later. */
1603 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001604 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001605 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001607 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001608 case FFA_MEM_LEND_32:
1609 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001610 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001611 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001612 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001614 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001615 }
1616
Andrew Walbranca808b12020-05-15 17:22:28 +01001617 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001618 /*
1619 * Allocate a share state before updating the page table. Otherwise if
1620 * updating the page table succeeded but allocating the share state
1621 * failed then it would leave the memory in a state where nobody could
1622 * get it back.
1623 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001624 if (!allocate_share_state(share_states, share_func, memory_region,
1625 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1626 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001627 dlog_verbose("Failed to allocate share state.\n");
1628 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001629 ret = ffa_error(FFA_NO_MEMORY);
1630 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001631 }
1632
Andrew Walbranca808b12020-05-15 17:22:28 +01001633 if (fragment_length == memory_share_length) {
1634 /* No more fragments to come, everything fit in one message. */
1635 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001636 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001637 } else {
1638 ret = (struct ffa_value){
1639 .func = FFA_MEM_FRAG_RX_32,
1640 .arg1 = (uint32_t)share_state->handle,
1641 .arg2 = (uint32_t)(share_state->handle >> 32),
1642 .arg3 = fragment_length};
1643 }
1644
1645out:
1646 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001647 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001648 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001649}
1650
1651/**
1652 * Validates a call to donate, lend or share memory to the TEE and then updates
1653 * the stage-2 page tables. Specifically, check if the message length and number
1654 * of memory region constituents match, and if the transition is valid for the
1655 * type of memory sending operation.
1656 *
1657 * Assumes that the caller has already found and locked the sender VM and the
1658 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1659 * to a freshly allocated page from Hafnium's internal pool. The caller must
1660 * have also validated that the receiver VM ID is valid.
1661 *
1662 * This function takes ownership of the `memory_region` passed in and will free
1663 * it when necessary; it must not be freed by the caller.
1664 */
1665struct ffa_value ffa_memory_tee_send(
1666 struct vm_locked from_locked, struct vm_locked to_locked,
1667 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1668 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1669{
1670 ffa_memory_access_permissions_t permissions;
1671 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001672
1673 /*
1674 * If there is an error validating the `memory_region` then we need to
1675 * free it because we own it but we won't be storing it in a share state
1676 * after all.
1677 */
1678 ret = ffa_memory_send_validate(from_locked, memory_region,
1679 memory_share_length, fragment_length,
1680 share_func, &permissions);
1681 if (ret.func != FFA_SUCCESS_32) {
1682 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001683 }
1684
Andrew Walbranca808b12020-05-15 17:22:28 +01001685 if (fragment_length == memory_share_length) {
1686 /* No more fragments to come, everything fit in one message. */
1687 struct ffa_composite_memory_region *composite =
1688 ffa_memory_region_get_composite(memory_region, 0);
1689 struct ffa_memory_region_constituent *constituents =
1690 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001691 struct mpool local_page_pool;
1692 uint32_t orig_from_mode;
1693
1694 /*
1695 * Use a local page pool so that we can roll back if necessary.
1696 */
1697 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001698
1699 ret = ffa_send_check_update(
1700 from_locked, &constituents,
1701 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001702 permissions, &local_page_pool,
1703 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1704 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001705 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001706 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001707 goto out;
1708 }
1709
1710 /* Forward memory send message on to TEE. */
1711 ret = memory_send_tee_forward(
1712 to_locked, from_locked.vm->id, share_func,
1713 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001714
1715 if (ret.func != FFA_SUCCESS_32) {
1716 dlog_verbose(
1717 "TEE didn't successfully complete memory send "
1718 "operation; returned %#x (%d). Rolling back.\n",
1719 ret.func, ret.arg2);
1720
1721 /*
1722 * The TEE failed to complete the send operation, so
1723 * roll back the page table update for the VM. This
1724 * can't fail because it won't try to allocate more
1725 * memory than was freed into the `local_page_pool` by
1726 * `ffa_send_check_update` in the initial update.
1727 */
1728 CHECK(ffa_region_group_identity_map(
1729 from_locked, &constituents,
1730 &composite->constituent_count, 1,
1731 orig_from_mode, &local_page_pool, true));
1732 }
1733
1734 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001735 } else {
1736 struct share_states_locked share_states = share_states_lock();
1737 ffa_memory_handle_t handle;
1738
1739 /*
1740 * We need to wait for the rest of the fragments before we can
1741 * check whether the transaction is valid and unmap the memory.
1742 * Call the TEE so it can do its initial validation and assign a
1743 * handle, and allocate a share state to keep what we have so
1744 * far.
1745 */
1746 ret = memory_send_tee_forward(
1747 to_locked, from_locked.vm->id, share_func,
1748 memory_region, memory_share_length, fragment_length);
1749 if (ret.func == FFA_ERROR_32) {
1750 goto out_unlock;
1751 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1752 dlog_warning(
1753 "Got %#x from TEE in response to %#x for "
1754 "fragment with with %d/%d, expected "
1755 "FFA_MEM_FRAG_RX.\n",
1756 ret.func, share_func, fragment_length,
1757 memory_share_length);
1758 ret = ffa_error(FFA_INVALID_PARAMETERS);
1759 goto out_unlock;
1760 }
1761 handle = ffa_frag_handle(ret);
1762 if (ret.arg3 != fragment_length) {
1763 dlog_warning(
1764 "Got unexpected fragment offset %d for "
1765 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1766 ret.arg3, fragment_length);
1767 ret = ffa_error(FFA_INVALID_PARAMETERS);
1768 goto out_unlock;
1769 }
1770 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1771 dlog_warning(
1772 "Got unexpected sender ID %d for "
1773 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1774 ffa_frag_sender(ret), from_locked.vm->id);
1775 ret = ffa_error(FFA_INVALID_PARAMETERS);
1776 goto out_unlock;
1777 }
1778
1779 if (!allocate_share_state(share_states, share_func,
1780 memory_region, fragment_length,
1781 handle, NULL)) {
1782 dlog_verbose("Failed to allocate share state.\n");
1783 ret = ffa_error(FFA_NO_MEMORY);
1784 goto out_unlock;
1785 }
1786 /*
1787 * Don't free the memory region fragment, as it has been stored
1788 * in the share state.
1789 */
1790 memory_region = NULL;
1791 out_unlock:
1792 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001793 }
1794
Andrew Walbranca808b12020-05-15 17:22:28 +01001795out:
1796 if (memory_region != NULL) {
1797 mpool_free(page_pool, memory_region);
1798 }
1799 dump_share_states();
1800 return ret;
1801}
1802
1803/**
1804 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1805 * this is the last fragment then checks that the transition is valid for the
1806 * type of memory sending operation and updates the stage-2 page tables of the
1807 * sender.
1808 *
1809 * Assumes that the caller has already found and locked the sender VM and copied
1810 * the memory region descriptor from the sender's TX buffer to a freshly
1811 * allocated page from Hafnium's internal pool.
1812 *
1813 * This function takes ownership of the `fragment` passed in; it must not be
1814 * freed by the caller.
1815 */
1816struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1817 void *fragment,
1818 uint32_t fragment_length,
1819 ffa_memory_handle_t handle,
1820 struct mpool *page_pool)
1821{
1822 struct share_states_locked share_states = share_states_lock();
1823 struct ffa_memory_share_state *share_state;
1824 struct ffa_value ret;
1825 struct ffa_memory_region *memory_region;
1826
1827 ret = ffa_memory_send_continue_validate(share_states, handle,
1828 &share_state,
1829 from_locked.vm->id, page_pool);
1830 if (ret.func != FFA_SUCCESS_32) {
1831 goto out_free_fragment;
1832 }
1833 memory_region = share_state->memory_region;
1834
1835 if (memory_region->receivers[0].receiver_permissions.receiver ==
1836 HF_TEE_VM_ID) {
1837 dlog_error(
1838 "Got hypervisor-allocated handle for memory send to "
1839 "TEE. This should never happen, and indicates a bug in "
1840 "EL3 code.\n");
1841 ret = ffa_error(FFA_INVALID_PARAMETERS);
1842 goto out_free_fragment;
1843 }
1844
1845 /* Add this fragment. */
1846 share_state->fragments[share_state->fragment_count] = fragment;
1847 share_state->fragment_constituent_counts[share_state->fragment_count] =
1848 fragment_length / sizeof(struct ffa_memory_region_constituent);
1849 share_state->fragment_count++;
1850
1851 /* Check whether the memory send operation is now ready to complete. */
1852 if (share_state_sending_complete(share_states, share_state)) {
1853 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001854 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001855 } else {
1856 ret = (struct ffa_value){
1857 .func = FFA_MEM_FRAG_RX_32,
1858 .arg1 = (uint32_t)handle,
1859 .arg2 = (uint32_t)(handle >> 32),
1860 .arg3 = share_state_next_fragment_offset(share_states,
1861 share_state)};
1862 }
1863 goto out;
1864
1865out_free_fragment:
1866 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001867
1868out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001869 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001870 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001871}
1872
Andrew Walbranca808b12020-05-15 17:22:28 +01001873/**
1874 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1875 * is the last fragment then checks that the transition is valid for the type of
1876 * memory sending operation and updates the stage-2 page tables of the sender.
1877 *
1878 * Assumes that the caller has already found and locked the sender VM and copied
1879 * the memory region descriptor from the sender's TX buffer to a freshly
1880 * allocated page from Hafnium's internal pool.
1881 *
1882 * This function takes ownership of the `memory_region` passed in and will free
1883 * it when necessary; it must not be freed by the caller.
1884 */
1885struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1886 struct vm_locked to_locked,
1887 void *fragment,
1888 uint32_t fragment_length,
1889 ffa_memory_handle_t handle,
1890 struct mpool *page_pool)
1891{
1892 struct share_states_locked share_states = share_states_lock();
1893 struct ffa_memory_share_state *share_state;
1894 struct ffa_value ret;
1895 struct ffa_memory_region *memory_region;
1896
1897 ret = ffa_memory_send_continue_validate(share_states, handle,
1898 &share_state,
1899 from_locked.vm->id, page_pool);
1900 if (ret.func != FFA_SUCCESS_32) {
1901 goto out_free_fragment;
1902 }
1903 memory_region = share_state->memory_region;
1904
1905 if (memory_region->receivers[0].receiver_permissions.receiver !=
1906 HF_TEE_VM_ID) {
1907 dlog_error(
1908 "Got SPM-allocated handle for memory send to non-TEE "
1909 "VM. This should never happen, and indicates a bug.\n");
1910 ret = ffa_error(FFA_INVALID_PARAMETERS);
1911 goto out_free_fragment;
1912 }
1913
1914 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1915 to_locked.vm->mailbox.recv == NULL) {
1916 /*
1917 * If the TEE RX buffer is not available, tell the sender to
1918 * retry by returning the current offset again.
1919 */
1920 ret = (struct ffa_value){
1921 .func = FFA_MEM_FRAG_RX_32,
1922 .arg1 = (uint32_t)handle,
1923 .arg2 = (uint32_t)(handle >> 32),
1924 .arg3 = share_state_next_fragment_offset(share_states,
1925 share_state),
1926 };
1927 goto out_free_fragment;
1928 }
1929
1930 /* Add this fragment. */
1931 share_state->fragments[share_state->fragment_count] = fragment;
1932 share_state->fragment_constituent_counts[share_state->fragment_count] =
1933 fragment_length / sizeof(struct ffa_memory_region_constituent);
1934 share_state->fragment_count++;
1935
1936 /* Check whether the memory send operation is now ready to complete. */
1937 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001938 struct mpool local_page_pool;
1939 uint32_t orig_from_mode;
1940
1941 /*
1942 * Use a local page pool so that we can roll back if necessary.
1943 */
1944 mpool_init_with_fallback(&local_page_pool, page_pool);
1945
Andrew Walbranca808b12020-05-15 17:22:28 +01001946 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001947 share_state, &local_page_pool,
1948 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001949
1950 if (ret.func == FFA_SUCCESS_32) {
1951 /*
1952 * Forward final fragment on to the TEE so that
1953 * it can complete the memory sending operation.
1954 */
1955 ret = memory_send_continue_tee_forward(
1956 to_locked, from_locked.vm->id, fragment,
1957 fragment_length, handle);
1958
1959 if (ret.func != FFA_SUCCESS_32) {
1960 /*
1961 * The error will be passed on to the caller,
1962 * but log it here too.
1963 */
1964 dlog_verbose(
1965 "TEE didn't successfully complete "
1966 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001967 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001968 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001969
1970 /*
1971 * The TEE failed to complete the send
1972 * operation, so roll back the page table update
1973 * for the VM. This can't fail because it won't
1974 * try to allocate more memory than was freed
1975 * into the `local_page_pool` by
1976 * `ffa_send_check_update` in the initial
1977 * update.
1978 */
1979 CHECK(ffa_region_group_identity_map(
1980 from_locked, share_state->fragments,
1981 share_state
1982 ->fragment_constituent_counts,
1983 share_state->fragment_count,
1984 orig_from_mode, &local_page_pool,
1985 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001986 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001987
Andrew Walbranca808b12020-05-15 17:22:28 +01001988 /* Free share state. */
1989 share_state_free(share_states, share_state, page_pool);
1990 } else {
1991 /* Abort sending to TEE. */
1992 struct ffa_value tee_ret =
1993 arch_tee_call((struct ffa_value){
1994 .func = FFA_MEM_RECLAIM_32,
1995 .arg1 = (uint32_t)handle,
1996 .arg2 = (uint32_t)(handle >> 32)});
1997
1998 if (tee_ret.func != FFA_SUCCESS_32) {
1999 /*
2000 * Nothing we can do if TEE doesn't abort
2001 * properly, just log it.
2002 */
2003 dlog_verbose(
2004 "TEE didn't successfully abort failed "
2005 "memory send operation; returned %#x "
2006 "(%d).\n",
2007 tee_ret.func, tee_ret.arg2);
2008 }
2009 /*
2010 * We don't need to free the share state in this case
2011 * because ffa_memory_send_complete does that already.
2012 */
2013 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002014
2015 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002016 } else {
2017 uint32_t next_fragment_offset =
2018 share_state_next_fragment_offset(share_states,
2019 share_state);
2020
2021 ret = memory_send_continue_tee_forward(
2022 to_locked, from_locked.vm->id, fragment,
2023 fragment_length, handle);
2024
2025 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2026 ffa_frag_handle(ret) != handle ||
2027 ret.arg3 != next_fragment_offset ||
2028 ffa_frag_sender(ret) != from_locked.vm->id) {
2029 dlog_verbose(
2030 "Got unexpected result from forwarding "
2031 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2032 "offset %d, sender %d); expected "
2033 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2034 "sender %d).\n",
2035 ret.func, ffa_frag_handle(ret), ret.arg3,
2036 ffa_frag_sender(ret), handle,
2037 next_fragment_offset, from_locked.vm->id);
2038 /* Free share state. */
2039 share_state_free(share_states, share_state, page_pool);
2040 ret = ffa_error(FFA_INVALID_PARAMETERS);
2041 goto out;
2042 }
2043
2044 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2045 .arg1 = (uint32_t)handle,
2046 .arg2 = (uint32_t)(handle >> 32),
2047 .arg3 = next_fragment_offset};
2048 }
2049 goto out;
2050
2051out_free_fragment:
2052 mpool_free(page_pool, fragment);
2053
2054out:
2055 share_states_unlock(&share_states);
2056 return ret;
2057}
2058
2059/** Clean up after the receiver has finished retrieving a memory region. */
2060static void ffa_memory_retrieve_complete(
2061 struct share_states_locked share_states,
2062 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2063{
2064 if (share_state->share_func == FFA_MEM_DONATE_32) {
2065 /*
2066 * Memory that has been donated can't be relinquished,
2067 * so no need to keep the share state around.
2068 */
2069 share_state_free(share_states, share_state, page_pool);
2070 dlog_verbose("Freed share state for donate.\n");
2071 }
2072}
2073
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002074struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2075 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002076 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002078{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002079 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002080 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002081 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002082 sizeof(struct ffa_memory_access);
2083 ffa_memory_handle_t handle = retrieve_request->handle;
2084 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002085 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002086 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2087 struct ffa_memory_region *memory_region;
2088 ffa_memory_access_permissions_t sent_permissions;
2089 enum ffa_data_access sent_data_access;
2090 enum ffa_instruction_access sent_instruction_access;
2091 ffa_memory_access_permissions_t requested_permissions;
2092 enum ffa_data_access requested_data_access;
2093 enum ffa_instruction_access requested_instruction_access;
2094 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002095 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002096 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002097 struct ffa_memory_share_state *share_state;
2098 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002099 struct ffa_composite_memory_region *composite;
2100 uint32_t total_length;
2101 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002102
2103 dump_share_states();
2104
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002105 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002106 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002107 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002108 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002109 expected_retrieve_request_length,
2110 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002112 }
2113
Andrew Walbrana65a1322020-04-06 19:32:32 +01002114 if (retrieve_request->receiver_count != 1) {
2115 dlog_verbose(
2116 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002117 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002118 "expected 1).\n",
2119 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002121 }
2122
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002123 share_states = share_states_lock();
2124 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002126 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002127 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002128 goto out;
2129 }
2130
Andrew Walbrana65a1322020-04-06 19:32:32 +01002131 memory_region = share_state->memory_region;
2132 CHECK(memory_region != NULL);
2133
2134 /*
2135 * Check that the transaction type expected by the receiver is correct,
2136 * if it has been specified.
2137 */
2138 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002139 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002140 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002141 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002142 dlog_verbose(
2143 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002144 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002145 transaction_type,
2146 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002147 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002148 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002149 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002150 goto out;
2151 }
2152
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002153 if (retrieve_request->sender != memory_region->sender) {
2154 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002155 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002156 "expected %d for handle %#x.\n",
2157 retrieve_request->sender, memory_region->sender,
2158 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002159 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002160 goto out;
2161 }
2162
2163 if (retrieve_request->tag != memory_region->tag) {
2164 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002165 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002166 "%d for handle %#x.\n",
2167 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169 goto out;
2170 }
2171
Andrew Walbrana65a1322020-04-06 19:32:32 +01002172 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2173 to_locked.vm->id) {
2174 dlog_verbose(
2175 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002176 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002177 retrieve_request->receivers[0]
2178 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002180 goto out;
2181 }
2182
2183 if (memory_region->receivers[0].receiver_permissions.receiver !=
2184 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002185 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002186 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2187 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002188 to_locked.vm->id,
2189 memory_region->receivers[0]
2190 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002191 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002192 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002193 goto out;
2194 }
2195
Andrew Walbranca808b12020-05-15 17:22:28 +01002196 if (!share_state->sending_complete) {
2197 dlog_verbose(
2198 "Memory with handle %#x not fully sent, can't "
2199 "retrieve.\n",
2200 handle);
2201 ret = ffa_error(FFA_INVALID_PARAMETERS);
2202 goto out;
2203 }
2204
2205 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002206 dlog_verbose("Memory with handle %#x already retrieved.\n",
2207 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002208 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002209 goto out;
2210 }
2211
Andrew Walbrana65a1322020-04-06 19:32:32 +01002212 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2213 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002214 dlog_verbose(
2215 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002216 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002217 retrieve_request->receivers[0]
2218 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002219 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002220 goto out;
2221 }
2222
Andrew Walbrana65a1322020-04-06 19:32:32 +01002223 /*
2224 * Check permissions from sender against permissions requested by
2225 * receiver.
2226 */
2227 /* TODO: Check attributes too. */
2228 sent_permissions =
2229 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002231 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002232 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 requested_permissions =
2234 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002235 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002237 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002238 permissions = 0;
2239 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002240 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2241 case FFA_DATA_ACCESS_RW:
2242 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2243 requested_data_access == FFA_DATA_ACCESS_RW) {
2244 ffa_set_data_access_attr(&permissions,
2245 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002246 break;
2247 }
2248 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002249 case FFA_DATA_ACCESS_RO:
2250 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2251 requested_data_access == FFA_DATA_ACCESS_RO) {
2252 ffa_set_data_access_attr(&permissions,
2253 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002254 break;
2255 }
2256 dlog_verbose(
2257 "Invalid data access requested; sender specified "
2258 "permissions %#x but receiver requested %#x.\n",
2259 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002260 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002261 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002262 case FFA_DATA_ACCESS_RESERVED:
2263 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002264 "checked before this point.");
2265 }
2266 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002267 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2268 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002269 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002270 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2271 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2272 ffa_set_instruction_access_attr(
2273 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002274 break;
2275 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002276 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002277 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2279 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2280 ffa_set_instruction_access_attr(
2281 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002282 break;
2283 }
2284 dlog_verbose(
2285 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002286 "specified permissions %#x but receiver requested "
2287 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002288 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002289 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002290 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002291 case FFA_INSTRUCTION_ACCESS_RESERVED:
2292 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002293 "be checked before this point.");
2294 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002295 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002296
Andrew Walbran996d1d12020-05-27 14:08:43 +01002297 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002298 to_locked, share_state->fragments,
2299 share_state->fragment_constituent_counts,
2300 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002301 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002302 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002303 goto out;
2304 }
2305
2306 /*
2307 * Copy response to RX buffer of caller and deliver the message. This
2308 * must be done before the share_state is (possibly) freed.
2309 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002310 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002311 composite = ffa_memory_region_get_composite(memory_region, 0);
2312 /*
2313 * Constituents which we received in the first fragment should always
2314 * fit in the first fragment we are sending, because the header is the
2315 * same size in both cases and we have a fixed message buffer size. So
2316 * `ffa_retrieved_memory_region_init` should never fail.
2317 */
2318 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002319 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2320 memory_region->sender, memory_region->attributes,
2321 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002322 composite->page_count, composite->constituent_count,
2323 share_state->fragments[0],
2324 share_state->fragment_constituent_counts[0], &total_length,
2325 &fragment_length));
2326 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002327 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002328 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2330
Andrew Walbranca808b12020-05-15 17:22:28 +01002331 share_state->retrieved_fragment_count[0] = 1;
2332 if (share_state->retrieved_fragment_count[0] ==
2333 share_state->fragment_count) {
2334 ffa_memory_retrieve_complete(share_states, share_state,
2335 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002336 }
2337
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002338 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002339 .arg1 = total_length,
2340 .arg2 = fragment_length};
2341
2342out:
2343 share_states_unlock(&share_states);
2344 dump_share_states();
2345 return ret;
2346}
2347
2348struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2349 ffa_memory_handle_t handle,
2350 uint32_t fragment_offset,
2351 struct mpool *page_pool)
2352{
2353 struct ffa_memory_region *memory_region;
2354 struct share_states_locked share_states;
2355 struct ffa_memory_share_state *share_state;
2356 struct ffa_value ret;
2357 uint32_t fragment_index;
2358 uint32_t retrieved_constituents_count;
2359 uint32_t i;
2360 uint32_t expected_fragment_offset;
2361 uint32_t remaining_constituent_count;
2362 uint32_t fragment_length;
2363
2364 dump_share_states();
2365
2366 share_states = share_states_lock();
2367 if (!get_share_state(share_states, handle, &share_state)) {
2368 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2369 handle);
2370 ret = ffa_error(FFA_INVALID_PARAMETERS);
2371 goto out;
2372 }
2373
2374 memory_region = share_state->memory_region;
2375 CHECK(memory_region != NULL);
2376
2377 if (memory_region->receivers[0].receiver_permissions.receiver !=
2378 to_locked.vm->id) {
2379 dlog_verbose(
2380 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2381 "of handle %#x.\n",
2382 to_locked.vm->id,
2383 memory_region->receivers[0]
2384 .receiver_permissions.receiver,
2385 handle);
2386 ret = ffa_error(FFA_INVALID_PARAMETERS);
2387 goto out;
2388 }
2389
2390 if (!share_state->sending_complete) {
2391 dlog_verbose(
2392 "Memory with handle %#x not fully sent, can't "
2393 "retrieve.\n",
2394 handle);
2395 ret = ffa_error(FFA_INVALID_PARAMETERS);
2396 goto out;
2397 }
2398
2399 if (share_state->retrieved_fragment_count[0] == 0 ||
2400 share_state->retrieved_fragment_count[0] >=
2401 share_state->fragment_count) {
2402 dlog_verbose(
2403 "Retrieval of memory with handle %#x not yet started "
2404 "or already completed (%d/%d fragments retrieved).\n",
2405 handle, share_state->retrieved_fragment_count[0],
2406 share_state->fragment_count);
2407 ret = ffa_error(FFA_INVALID_PARAMETERS);
2408 goto out;
2409 }
2410
2411 fragment_index = share_state->retrieved_fragment_count[0];
2412
2413 /*
2414 * Check that the given fragment offset is correct by counting how many
2415 * constituents were in the fragments previously sent.
2416 */
2417 retrieved_constituents_count = 0;
2418 for (i = 0; i < fragment_index; ++i) {
2419 retrieved_constituents_count +=
2420 share_state->fragment_constituent_counts[i];
2421 }
2422 expected_fragment_offset =
2423 ffa_composite_constituent_offset(memory_region, 0) +
2424 retrieved_constituents_count *
2425 sizeof(struct ffa_memory_region_constituent);
2426 if (fragment_offset != expected_fragment_offset) {
2427 dlog_verbose("Fragment offset was %d but expected %d.\n",
2428 fragment_offset, expected_fragment_offset);
2429 ret = ffa_error(FFA_INVALID_PARAMETERS);
2430 goto out;
2431 }
2432
2433 remaining_constituent_count = ffa_memory_fragment_init(
2434 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2435 share_state->fragments[fragment_index],
2436 share_state->fragment_constituent_counts[fragment_index],
2437 &fragment_length);
2438 CHECK(remaining_constituent_count == 0);
2439 to_locked.vm->mailbox.recv_size = fragment_length;
2440 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2441 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2442 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2443 share_state->retrieved_fragment_count[0]++;
2444 if (share_state->retrieved_fragment_count[0] ==
2445 share_state->fragment_count) {
2446 ffa_memory_retrieve_complete(share_states, share_state,
2447 page_pool);
2448 }
2449
2450 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2451 .arg1 = (uint32_t)handle,
2452 .arg2 = (uint32_t)(handle >> 32),
2453 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002454
2455out:
2456 share_states_unlock(&share_states);
2457 dump_share_states();
2458 return ret;
2459}
2460
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002461struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002462 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002464{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467 struct ffa_memory_share_state *share_state;
2468 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002469 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002470 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471
Andrew Walbrana65a1322020-04-06 19:32:32 +01002472 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002473 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002474 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002475 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002476 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002477 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002478 }
2479
Andrew Walbrana65a1322020-04-06 19:32:32 +01002480 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002481 dlog_verbose(
2482 "VM ID %d in relinquish message doesn't match calling "
2483 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002484 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002485 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002486 }
2487
2488 dump_share_states();
2489
2490 share_states = share_states_lock();
2491 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002492 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002493 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 goto out;
2496 }
2497
Andrew Walbranca808b12020-05-15 17:22:28 +01002498 if (!share_state->sending_complete) {
2499 dlog_verbose(
2500 "Memory with handle %#x not fully sent, can't "
2501 "relinquish.\n",
2502 handle);
2503 ret = ffa_error(FFA_INVALID_PARAMETERS);
2504 goto out;
2505 }
2506
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507 memory_region = share_state->memory_region;
2508 CHECK(memory_region != NULL);
2509
Andrew Walbrana65a1322020-04-06 19:32:32 +01002510 if (memory_region->receivers[0].receiver_permissions.receiver !=
2511 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002512 dlog_verbose(
2513 "VM ID %d tried to relinquish memory region with "
2514 "handle %#x but receiver was %d.\n",
2515 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002516 memory_region->receivers[0]
2517 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 goto out;
2520 }
2521
Andrew Walbranca808b12020-05-15 17:22:28 +01002522 if (share_state->retrieved_fragment_count[0] !=
2523 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002524 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002525 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526 "relinquish.\n",
2527 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002528 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002529 goto out;
2530 }
2531
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002532 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533
2534 /*
2535 * Clear is not allowed for memory that was shared, as the original
2536 * sender still has access to the memory.
2537 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002541 goto out;
2542 }
2543
Andrew Walbranca808b12020-05-15 17:22:28 +01002544 ret = ffa_relinquish_check_update(
2545 from_locked, share_state->fragments,
2546 share_state->fragment_constituent_counts,
2547 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002548
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002549 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 /*
2551 * Mark memory handle as not retrieved, so it can be reclaimed
2552 * (or retrieved again).
2553 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002554 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555 }
2556
2557out:
2558 share_states_unlock(&share_states);
2559 dump_share_states();
2560 return ret;
2561}
2562
2563/**
2564 * Validates that the reclaim transition is allowed for the given handle,
2565 * updates the page table of the reclaiming VM, and frees the internal state
2566 * associated with the handle.
2567 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002568struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002569 ffa_memory_handle_t handle,
2570 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002571 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572{
2573 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002574 struct ffa_memory_share_state *share_state;
2575 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002576 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002577 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002578
2579 dump_share_states();
2580
2581 share_states = share_states_lock();
2582 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002583 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002584 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002585 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002586 goto out;
2587 }
2588
2589 memory_region = share_state->memory_region;
2590 CHECK(memory_region != NULL);
2591
2592 if (to_locked.vm->id != memory_region->sender) {
2593 dlog_verbose(
2594 "VM %d attempted to reclaim memory handle %#x "
2595 "originally sent by VM %d.\n",
2596 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002597 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002598 goto out;
2599 }
2600
Andrew Walbranca808b12020-05-15 17:22:28 +01002601 if (!share_state->sending_complete) {
2602 dlog_verbose(
2603 "Memory with handle %#x not fully sent, can't "
2604 "reclaim.\n",
2605 handle);
2606 ret = ffa_error(FFA_INVALID_PARAMETERS);
2607 goto out;
2608 }
2609
2610 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002611 dlog_verbose(
2612 "Tried to reclaim memory handle %#x that has not been "
2613 "relinquished.\n",
2614 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002615 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002616 goto out;
2617 }
2618
Andrew Walbranca808b12020-05-15 17:22:28 +01002619 ret = ffa_retrieve_check_update(
2620 to_locked, share_state->fragments,
2621 share_state->fragment_constituent_counts,
2622 share_state->fragment_count, memory_to_attributes,
2623 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002624
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002625 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002626 share_state_free(share_states, share_state, page_pool);
2627 dlog_verbose("Freed share state after successful reclaim.\n");
2628 }
2629
2630out:
2631 share_states_unlock(&share_states);
2632 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002633}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002634
2635/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002636 * Validates that the reclaim transition is allowed for the memory region with
2637 * the given handle which was previously shared with the TEE, tells the TEE to
2638 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2639 *
2640 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002641 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002642struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002643 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002644 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002645 ffa_memory_region_flags_t flags,
2646 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002647{
Andrew Walbranca808b12020-05-15 17:22:28 +01002648 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2649 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2650 struct ffa_value tee_ret;
2651 uint32_t length;
2652 uint32_t fragment_length;
2653 uint32_t fragment_offset;
2654 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002655 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002656 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2657
2658 CHECK(request_length <= HF_MAILBOX_SIZE);
2659 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2660
2661 /* Retrieve memory region information from the TEE. */
2662 tee_ret = arch_tee_call(
2663 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2664 .arg1 = request_length,
2665 .arg2 = request_length});
2666 if (tee_ret.func == FFA_ERROR_32) {
2667 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2668 return tee_ret;
2669 }
2670 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2671 dlog_verbose(
2672 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2673 tee_ret.func);
2674 return ffa_error(FFA_INVALID_PARAMETERS);
2675 }
2676
2677 length = tee_ret.arg1;
2678 fragment_length = tee_ret.arg2;
2679
2680 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2681 length > sizeof(tee_retrieve_buffer)) {
2682 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2683 fragment_length, length, HF_MAILBOX_SIZE,
2684 sizeof(tee_retrieve_buffer));
2685 return ffa_error(FFA_INVALID_PARAMETERS);
2686 }
2687
2688 /*
2689 * Copy the first fragment of the memory region descriptor to an
2690 * internal buffer.
2691 */
2692 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2693 from_locked.vm->mailbox.send, fragment_length);
2694
2695 /* Fetch the remaining fragments into the same buffer. */
2696 fragment_offset = fragment_length;
2697 while (fragment_offset < length) {
2698 tee_ret = arch_tee_call(
2699 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2700 .arg1 = (uint32_t)handle,
2701 .arg2 = (uint32_t)(handle >> 32),
2702 .arg3 = fragment_offset});
2703 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2704 dlog_verbose(
2705 "Got %#x (%d) from TEE in response to "
2706 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2707 tee_ret.func, tee_ret.arg2);
2708 return tee_ret;
2709 }
2710 if (ffa_frag_handle(tee_ret) != handle) {
2711 dlog_verbose(
2712 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2713 "in response to FFA_MEM_FRAG_RX for handle "
2714 "%#x.\n",
2715 ffa_frag_handle(tee_ret), handle);
2716 return ffa_error(FFA_INVALID_PARAMETERS);
2717 }
2718 if (ffa_frag_sender(tee_ret) != 0) {
2719 dlog_verbose(
2720 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2721 "(expected 0).\n",
2722 ffa_frag_sender(tee_ret));
2723 return ffa_error(FFA_INVALID_PARAMETERS);
2724 }
2725 fragment_length = tee_ret.arg3;
2726 if (fragment_length > HF_MAILBOX_SIZE ||
2727 fragment_offset + fragment_length > length) {
2728 dlog_verbose(
2729 "Invalid fragment length %d at offset %d (max "
2730 "%d).\n",
2731 fragment_length, fragment_offset,
2732 HF_MAILBOX_SIZE);
2733 return ffa_error(FFA_INVALID_PARAMETERS);
2734 }
2735 memcpy_s(tee_retrieve_buffer + fragment_offset,
2736 sizeof(tee_retrieve_buffer) - fragment_offset,
2737 from_locked.vm->mailbox.send, fragment_length);
2738
2739 fragment_offset += fragment_length;
2740 }
2741
2742 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002743
2744 if (memory_region->receiver_count != 1) {
2745 /* Only one receiver supported by Hafnium for now. */
2746 dlog_verbose(
2747 "Multiple recipients not supported (got %d, expected "
2748 "1).\n",
2749 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002750 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002751 }
2752
2753 if (memory_region->handle != handle) {
2754 dlog_verbose(
2755 "Got memory region handle %#x from TEE but requested "
2756 "handle %#x.\n",
2757 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002758 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002759 }
2760
2761 /* The original sender must match the caller. */
2762 if (to_locked.vm->id != memory_region->sender) {
2763 dlog_verbose(
2764 "VM %d attempted to reclaim memory handle %#x "
2765 "originally sent by VM %d.\n",
2766 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002767 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002768 }
2769
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002770 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002771
2772 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002773 * Validate that the reclaim transition is allowed for the given memory
2774 * region, forward the request to the TEE and then map the memory back
2775 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002776 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002777 return ffa_tee_reclaim_check_update(
2778 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002779 composite->constituent_count, memory_to_attributes,
2780 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002781}