blob: 07a3842e75c2f71197d9d0b69200a202edd11ad4 [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 break;
388 }
389 }
390 sl_unlock(&share_states_lock_instance);
391}
392
Andrew Walbran475c1452020-02-07 13:22:22 +0000393/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100394static inline uint32_t ffa_memory_permissions_to_mode(
395 ffa_memory_access_permissions_t permissions)
Andrew Walbran475c1452020-02-07 13:22:22 +0000396{
397 uint32_t mode = 0;
398
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100399 switch (ffa_get_data_access_attr(permissions)) {
400 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000401 mode = MM_MODE_R;
402 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100403 case FFA_DATA_ACCESS_RW:
404 case FFA_DATA_ACCESS_NOT_SPECIFIED:
Andrew Walbran475c1452020-02-07 13:22:22 +0000405 mode = MM_MODE_R | MM_MODE_W;
406 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100407 case FFA_DATA_ACCESS_RESERVED:
408 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100409 }
410
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 switch (ffa_get_instruction_access_attr(permissions)) {
412 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000413 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 case FFA_INSTRUCTION_ACCESS_X:
415 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100416 mode |= MM_MODE_X;
417 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100418 case FFA_INSTRUCTION_ACCESS_RESERVED:
419 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000420 }
421
422 return mode;
423}
424
Jose Marinho75509b42019-04-09 09:34:59 +0100425/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000426 * Get the current mode in the stage-2 page table of the given vm of all the
427 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100429 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000431 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100432 struct ffa_memory_region_constituent **fragments,
433 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100434{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100435 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100436 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100437
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100439 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000440 * Fail if there are no constituents. Otherwise we would get an
441 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100442 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100443 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100444 }
445
Andrew Walbranca808b12020-05-15 17:22:28 +0100446 for (i = 0; i < fragment_count; ++i) {
447 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
448 ipaddr_t begin = ipa_init(fragments[i][j].address);
449 size_t size = fragments[i][j].page_count * PAGE_SIZE;
450 ipaddr_t end = ipa_add(begin, size);
451 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100452
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 /* Fail if addresses are not page-aligned. */
454 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
455 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
456 return ffa_error(FFA_INVALID_PARAMETERS);
457 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100458
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 /*
460 * Ensure that this constituent memory range is all
461 * mapped with the same mode.
462 */
463 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
464 &current_mode)) {
465 return ffa_error(FFA_DENIED);
466 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100467
Andrew Walbranca808b12020-05-15 17:22:28 +0100468 /*
469 * Ensure that all constituents are mapped with the same
470 * mode.
471 */
472 if (i == 0) {
473 *orig_mode = current_mode;
474 } else if (current_mode != *orig_mode) {
475 dlog_verbose(
476 "Expected mode %#x but was %#x for %d "
477 "pages at %#x.\n",
478 *orig_mode, current_mode,
479 fragments[i][j].page_count,
480 ipa_addr(begin));
481 return ffa_error(FFA_DENIED);
482 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100483 }
Jose Marinho75509b42019-04-09 09:34:59 +0100484 }
485
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100486 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000487}
488
489/**
490 * Verify that all pages have the same mode, that the starting mode
491 * constitutes a valid state and obtain the next mode to apply
492 * to the sending VM.
493 *
494 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495 * 1) FFA_DENIED if a state transition was not found;
496 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100497 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100499 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100500 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
501 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000502 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100504 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100506 struct ffa_memory_region_constituent **fragments,
507 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
508 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000509{
510 const uint32_t state_mask =
511 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100512 const uint32_t required_from_mode =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513 ffa_memory_permissions_to_mode(permissions);
514 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000515
Andrew Walbranca808b12020-05-15 17:22:28 +0100516 ret = constituents_get_mode(from, orig_from_mode, fragments,
517 fragment_constituent_counts,
518 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100520 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100521 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100522 }
523
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000524 /* Ensure the address range is normal memory and not a device. */
525 if (*orig_from_mode & MM_MODE_D) {
526 dlog_verbose("Can't share device memory (mode is %#x).\n",
527 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000529 }
530
531 /*
532 * Ensure the sender is the owner and has exclusive access to the
533 * memory.
534 */
535 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100537 }
538
539 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
540 dlog_verbose(
541 "Sender tried to send memory with permissions which "
542 "required mode %#x but only had %#x itself.\n",
543 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 }
546
547 /* Find the appropriate new mode. */
548 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000549 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100550 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000551 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100552 break;
553
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000555 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100556 break;
557
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100558 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000559 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100560 break;
561
Jose Marinho75509b42019-04-09 09:34:59 +0100562 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100563 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100564 }
565
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567}
568
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000570 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100571 struct ffa_memory_region_constituent **fragments,
572 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
573 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000574{
575 const uint32_t state_mask =
576 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
577 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100578 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000579
Andrew Walbranca808b12020-05-15 17:22:28 +0100580 ret = constituents_get_mode(from, orig_from_mode, fragments,
581 fragment_constituent_counts,
582 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100584 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000585 }
586
587 /* Ensure the address range is normal memory and not a device. */
588 if (*orig_from_mode & MM_MODE_D) {
589 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
590 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000592 }
593
594 /*
595 * Ensure the relinquishing VM is not the owner but has access to the
596 * memory.
597 */
598 orig_from_state = *orig_from_mode & state_mask;
599 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
600 dlog_verbose(
601 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100602 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000603 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000605 }
606
607 /* Find the appropriate new mode. */
608 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
609
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000611}
612
613/**
614 * Verify that all pages have the same mode, that the starting mode
615 * constitutes a valid state and obtain the next mode to apply
616 * to the retrieving VM.
617 *
618 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100619 * 1) FFA_DENIED if a state transition was not found;
620 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100621 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100622 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100623 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
625 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000628 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100629 struct ffa_memory_region_constituent **fragments,
630 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
631 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632{
633 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100634 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000635
Andrew Walbranca808b12020-05-15 17:22:28 +0100636 ret = constituents_get_mode(to, &orig_to_mode, fragments,
637 fragment_constituent_counts,
638 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100639 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100640 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100641 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000642 }
643
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100644 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000645 const uint32_t state_mask =
646 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
647 uint32_t orig_to_state = orig_to_mode & state_mask;
648
649 if (orig_to_state != MM_MODE_INVALID &&
650 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000652 }
653 } else {
654 /*
655 * Ensure the retriever has the expected state. We don't care
656 * about the MM_MODE_SHARED bit; either with or without it set
657 * are both valid representations of the !O-NA state.
658 */
659 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
660 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662 }
663 }
664
665 /* Find the appropriate new mode. */
666 *to_mode = memory_to_attributes;
667 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100668 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669 *to_mode |= 0;
670 break;
671
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100672 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000673 *to_mode |= MM_MODE_UNOWNED;
674 break;
675
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000677 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
678 break;
679
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000681 *to_mode |= 0;
682 break;
683
684 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100685 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000687 }
688
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100689 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100690}
Jose Marinho09b1db82019-08-08 09:16:59 +0100691
692/**
693 * Updates a VM's page table such that the given set of physical address ranges
694 * are mapped in the address space at the corresponding address ranges, in the
695 * mode provided.
696 *
697 * If commit is false, the page tables will be allocated from the mpool but no
698 * mappings will actually be updated. This function must always be called first
699 * with commit false to check that it will succeed before calling with commit
700 * true, to avoid leaving the page table in a half-updated state. To make a
701 * series of changes atomically you can call them all with commit false before
702 * calling them all with commit true.
703 *
704 * mm_vm_defrag should always be called after a series of page table updates,
705 * whether they succeed or fail.
706 *
707 * Returns true on success, or false if the update failed and no changes were
708 * made to memory mappings.
709 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000711 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100712 struct ffa_memory_region_constituent **fragments,
713 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
714 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100715{
Andrew Walbranca808b12020-05-15 17:22:28 +0100716 uint32_t i;
717 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100718
Andrew Walbranca808b12020-05-15 17:22:28 +0100719 /* Iterate over the memory region constituents within each fragment. */
720 for (i = 0; i < fragment_count; ++i) {
721 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
722 size_t size = fragments[i][j].page_count * PAGE_SIZE;
723 paddr_t pa_begin =
724 pa_from_ipa(ipa_init(fragments[i][j].address));
725 paddr_t pa_end = pa_add(pa_begin, size);
726
727 if (commit) {
728 vm_identity_commit(vm_locked, pa_begin, pa_end,
729 mode, ppool, NULL);
730 } else if (!vm_identity_prepare(vm_locked, pa_begin,
731 pa_end, mode, ppool)) {
732 return false;
733 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100734 }
735 }
736
737 return true;
738}
739
740/**
741 * Clears a region of physical memory by overwriting it with zeros. The data is
742 * flushed from the cache so the memory has been cleared across the system.
743 */
744static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
745{
746 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000747 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100748 * global mapping of the whole range. Such an approach will limit
749 * the changes to stage-1 tables and will allow only local
750 * invalidation.
751 */
752 bool ret;
753 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
754 void *ptr =
755 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
756 size_t size = pa_difference(begin, end);
757
758 if (!ptr) {
759 /* TODO: partial defrag of failed range. */
760 /* Recover any memory consumed in failed mapping. */
761 mm_defrag(stage1_locked, ppool);
762 goto fail;
763 }
764
765 memset_s(ptr, size, 0, size);
766 arch_mm_flush_dcache(ptr, size);
767 mm_unmap(stage1_locked, begin, end, ppool);
768
769 ret = true;
770 goto out;
771
772fail:
773 ret = false;
774
775out:
776 mm_unlock_stage1(&stage1_locked);
777
778 return ret;
779}
780
781/**
782 * Clears a region of physical memory by overwriting it with zeros. The data is
783 * flushed from the cache so the memory has been cleared across the system.
784 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100786 struct ffa_memory_region_constituent **fragments,
787 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
788 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100789{
790 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100791 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 struct mm_stage1_locked stage1_locked;
793 bool ret = false;
794
795 /*
796 * Create a local pool so any freed memory can't be used by another
797 * thread. This is to ensure each constituent that is mapped can be
798 * unmapped again afterwards.
799 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000800 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100801
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 /* Iterate over the memory region constituents within each fragment. */
803 for (i = 0; i < fragment_count; ++i) {
804 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100805
Andrew Walbranca808b12020-05-15 17:22:28 +0100806 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
807 size_t size = fragments[i][j].page_count * PAGE_SIZE;
808 paddr_t begin =
809 pa_from_ipa(ipa_init(fragments[i][j].address));
810 paddr_t end = pa_add(begin, size);
811
812 if (!clear_memory(begin, end, &local_page_pool)) {
813 /*
814 * api_clear_memory will defrag on failure, so
815 * no need to do it here.
816 */
817 goto out;
818 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100819 }
820 }
821
822 /*
823 * Need to defrag after clearing, as it may have added extra mappings to
824 * the stage 1 page table.
825 */
826 stage1_locked = mm_lock_stage1();
827 mm_defrag(stage1_locked, &local_page_pool);
828 mm_unlock_stage1(&stage1_locked);
829
830 ret = true;
831
832out:
833 mpool_fini(&local_page_pool);
834 return ret;
835}
836
837/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000838 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100839 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000840 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100841 *
842 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000843 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100844 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100845 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100846 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
847 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100848 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100849 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100851 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100852static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000853 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100854 struct ffa_memory_region_constituent **fragments,
855 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
856 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100857 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100858{
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 uint32_t orig_from_mode;
862 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100865
866 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100867 * Make sure constituents are properly aligned to a 64-bit boundary. If
868 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100870 for (i = 0; i < fragment_count; ++i) {
871 if (!is_aligned(fragments[i], 8)) {
872 dlog_verbose("Constituents not aligned.\n");
873 return ffa_error(FFA_INVALID_PARAMETERS);
874 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100875 }
876
877 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000878 * Check if the state transition is lawful for the sender, ensure that
879 * all constituents of a memory region being shared are at the same
880 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100883 &orig_from_mode, fragments,
884 fragment_constituent_counts,
885 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100888 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 }
890
Andrew Walbran37c574e2020-06-03 11:45:46 +0100891 if (orig_from_mode_ret != NULL) {
892 *orig_from_mode_ret = orig_from_mode;
893 }
894
Jose Marinho09b1db82019-08-08 09:16:59 +0100895 /*
896 * Create a local pool so any freed memory can't be used by another
897 * thread. This is to ensure the original mapping can be restored if the
898 * clear fails.
899 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000900 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100901
902 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000903 * First reserve all required memory for the new page table entries
904 * without committing, to make sure the entire operation will succeed
905 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100906 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100907 if (!ffa_region_group_identity_map(
908 from_locked, fragments, fragment_constituent_counts,
909 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100910 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100911 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100912 goto out;
913 }
914
915 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 * Update the mapping for the sender. This won't allocate because the
917 * transaction was already prepared above, but may free pages in the
918 * case that a whole block is being unmapped that was previously
919 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100920 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100921 CHECK(ffa_region_group_identity_map(
922 from_locked, fragments, fragment_constituent_counts,
923 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100924
925 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 fragments, fragment_constituent_counts,
928 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100929 /*
930 * On failure, roll back by returning memory to the sender. This
931 * may allocate pages which were previously freed into
932 * `local_page_pool` by the call above, but will never allocate
933 * more pages than that so can never fail.
934 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 from_locked, fragments, fragment_constituent_counts,
937 fragment_count, orig_from_mode, &local_page_pool,
938 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100939
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100941 goto out;
942 }
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000945
946out:
947 mpool_fini(&local_page_pool);
948
949 /*
950 * Tidy up the page table by reclaiming failed mappings (if there was an
951 * error) or merging entries into blocks where possible (on success).
952 */
953 mm_vm_defrag(&from->ptable, page_pool);
954
955 return ret;
956}
957
958/**
959 * Validates and maps memory shared from one VM to another.
960 *
961 * This function requires the calling context to hold the <to> lock.
962 *
963 * Returns:
964 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000968 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100969 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000970 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100971static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100973 struct ffa_memory_region_constituent **fragments,
974 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
975 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
976 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000977{
978 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100979 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000980 uint32_t to_mode;
981 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000983
984 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100985 * Make sure constituents are properly aligned to a 64-bit boundary. If
986 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000987 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100988 for (i = 0; i < fragment_count; ++i) {
989 if (!is_aligned(fragments[i], 8)) {
990 return ffa_error(FFA_INVALID_PARAMETERS);
991 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000992 }
993
994 /*
995 * Check if the state transition is lawful for the recipient, and ensure
996 * that all constituents of the memory region being retrieved are at the
997 * same state.
998 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100999 ret = ffa_retrieve_check_transition(
1000 to_locked, share_func, fragments, fragment_constituent_counts,
1001 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001003 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001004 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005 }
1006
1007 /*
1008 * Create a local pool so any freed memory can't be used by another
1009 * thread. This is to ensure the original mapping can be restored if the
1010 * clear fails.
1011 */
1012 mpool_init_with_fallback(&local_page_pool, page_pool);
1013
1014 /*
1015 * First reserve all required memory for the new page table entries in
1016 * the recipient page tables without committing, to make sure the entire
1017 * operation will succeed without exhausting the page pool.
1018 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 if (!ffa_region_group_identity_map(
1020 to_locked, fragments, fragment_constituent_counts,
1021 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 /* TODO: partial defrag of failed range. */
1023 dlog_verbose(
1024 "Insufficient memory to update recipient page "
1025 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027 goto out;
1028 }
1029
1030 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001032 fragments, fragment_constituent_counts,
1033 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 goto out;
1036 }
1037
Jose Marinho09b1db82019-08-08 09:16:59 +01001038 /*
1039 * Complete the transfer by mapping the memory into the recipient. This
1040 * won't allocate because the transaction was already prepared above, so
1041 * it doesn't need to use the `local_page_pool`.
1042 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001043 CHECK(ffa_region_group_identity_map(
1044 to_locked, fragments, fragment_constituent_counts,
1045 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001046
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001048
1049out:
1050 mpool_fini(&local_page_pool);
1051
1052 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001053 * Tidy up the page table by reclaiming failed mappings (if there was an
1054 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001055 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001056 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001057
1058 return ret;
1059}
1060
Andrew Walbran290b0c92020-02-03 16:37:14 +00001061/**
1062 * Reclaims the given memory from the TEE. To do this space is first reserved in
1063 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1064 * then (if that is successful) the memory is mapped back into the <to> VM's
1065 * page table.
1066 *
1067 * This function requires the calling context to hold the <to> lock.
1068 *
1069 * Returns:
1070 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001072 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001074 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001075 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001076 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001077static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 struct vm_locked to_locked, ffa_memory_handle_t handle,
1079 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001080 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1081 struct mpool *page_pool)
1082{
1083 struct vm *to = to_locked.vm;
1084 uint32_t to_mode;
1085 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 struct ffa_value ret;
1087 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001088
1089 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001090 * Make sure constituents are properly aligned to a 64-bit boundary. If
1091 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001092 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001093 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001094 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001095 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001096 }
1097
1098 /*
1099 * Check if the state transition is lawful for the recipient, and ensure
1100 * that all constituents of the memory region being retrieved are at the
1101 * same state.
1102 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001103 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001104 &constituents, &constituent_count,
1105 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001107 dlog_verbose("Invalid transition.\n");
1108 return ret;
1109 }
1110
1111 /*
1112 * Create a local pool so any freed memory can't be used by another
1113 * thread. This is to ensure the original mapping can be restored if the
1114 * clear fails.
1115 */
1116 mpool_init_with_fallback(&local_page_pool, page_pool);
1117
1118 /*
1119 * First reserve all required memory for the new page table entries in
1120 * the recipient page tables without committing, to make sure the entire
1121 * operation will succeed without exhausting the page pool.
1122 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001123 if (!ffa_region_group_identity_map(to_locked, &constituents,
1124 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001125 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001126 /* TODO: partial defrag of failed range. */
1127 dlog_verbose(
1128 "Insufficient memory to update recipient page "
1129 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001130 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001131 goto out;
1132 }
1133
1134 /*
1135 * Forward the request to the TEE and see what happens.
1136 */
1137 tee_flags = 0;
1138 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001140 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001141 ret = arch_tee_call((struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1142 .arg1 = (uint32_t)handle,
1143 .arg2 = (uint32_t)(handle >> 32),
1144 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001145
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001146 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001147 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1149 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001150 ret.func, ret.arg2);
1151 goto out;
1152 }
1153
1154 /*
1155 * The TEE was happy with it, so complete the reclaim by mapping the
1156 * memory into the recipient. This won't allocate because the
1157 * transaction was already prepared above, so it doesn't need to use the
1158 * `local_page_pool`.
1159 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001160 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1161 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001163
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001165
1166out:
1167 mpool_fini(&local_page_pool);
1168
1169 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001170 * Tidy up the page table by reclaiming failed mappings (if there was an
1171 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001172 */
1173 mm_vm_defrag(&to->ptable, page_pool);
1174
1175 return ret;
1176}
1177
Andrew Walbran996d1d12020-05-27 14:08:43 +01001178static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001179 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001180 struct ffa_memory_region_constituent **fragments,
1181 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1182 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001183{
1184 uint32_t orig_from_mode;
1185 uint32_t from_mode;
1186 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001188
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 ret = ffa_relinquish_check_transition(
1190 from_locked, &orig_from_mode, fragments,
1191 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001192 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001193 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001194 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001195 }
1196
1197 /*
1198 * Create a local pool so any freed memory can't be used by another
1199 * thread. This is to ensure the original mapping can be restored if the
1200 * clear fails.
1201 */
1202 mpool_init_with_fallback(&local_page_pool, page_pool);
1203
1204 /*
1205 * First reserve all required memory for the new page table entries
1206 * without committing, to make sure the entire operation will succeed
1207 * without exhausting the page pool.
1208 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001209 if (!ffa_region_group_identity_map(
1210 from_locked, fragments, fragment_constituent_counts,
1211 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001212 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001214 goto out;
1215 }
1216
1217 /*
1218 * Update the mapping for the sender. This won't allocate because the
1219 * transaction was already prepared above, but may free pages in the
1220 * case that a whole block is being unmapped that was previously
1221 * partially mapped.
1222 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001223 CHECK(ffa_region_group_identity_map(
1224 from_locked, fragments, fragment_constituent_counts,
1225 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001226
1227 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001228 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 fragments, fragment_constituent_counts,
1230 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001231 /*
1232 * On failure, roll back by returning memory to the sender. This
1233 * may allocate pages which were previously freed into
1234 * `local_page_pool` by the call above, but will never allocate
1235 * more pages than that so can never fail.
1236 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001238 from_locked, fragments, fragment_constituent_counts,
1239 fragment_count, orig_from_mode, &local_page_pool,
1240 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001241
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001242 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001243 goto out;
1244 }
1245
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001247
1248out:
1249 mpool_fini(&local_page_pool);
1250
1251 /*
1252 * Tidy up the page table by reclaiming failed mappings (if there was an
1253 * error) or merging entries into blocks where possible (on success).
1254 */
1255 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001256
1257 return ret;
1258}
1259
1260/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 * Complete a memory sending operation by checking that it is valid, updating
1262 * the sender page table, and then either marking the share state as having
1263 * completed sending (on success) or freeing it (on failure).
1264 *
1265 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1266 */
1267static struct ffa_value ffa_memory_send_complete(
1268 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001269 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1270 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001271{
1272 struct ffa_memory_region *memory_region = share_state->memory_region;
1273 struct ffa_value ret;
1274
1275 /* Lock must be held. */
1276 CHECK(share_states.share_states != NULL);
1277
1278 /* Check that state is valid in sender page table and update. */
1279 ret = ffa_send_check_update(
1280 from_locked, share_state->fragments,
1281 share_state->fragment_constituent_counts,
1282 share_state->fragment_count, share_state->share_func,
1283 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001284 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1285 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001286 if (ret.func != FFA_SUCCESS_32) {
1287 /*
1288 * Free share state, it failed to send so it can't be retrieved.
1289 */
1290 dlog_verbose("Complete failed, freeing share state.\n");
1291 share_state_free(share_states, share_state, page_pool);
1292 return ret;
1293 }
1294
1295 share_state->sending_complete = true;
1296 dlog_verbose("Marked sending complete.\n");
1297
1298 return ffa_mem_success(share_state->handle);
1299}
1300
1301/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001302 * Check that the given `memory_region` represents a valid memory send request
1303 * of the given `share_func` type, return the clear flag and permissions via the
1304 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001305 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001306 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001307 * not.
1308 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001309static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001310 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1311 uint32_t memory_share_length, uint32_t fragment_length,
1312 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001313{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001314 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001315 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001316 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001317 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001318 enum ffa_data_access data_access;
1319 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321 CHECK(permissions != NULL);
1322
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001323 /*
1324 * This should already be checked by the caller, just making the
1325 * assumption clear here.
1326 */
1327 CHECK(memory_region->receiver_count == 1);
1328
Andrew Walbrana65a1322020-04-06 19:32:32 +01001329 /* The sender must match the message sender. */
1330 if (memory_region->sender != from_locked.vm->id) {
1331 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001333 }
1334
Andrew Walbrana65a1322020-04-06 19:32:32 +01001335 /*
1336 * Ensure that the composite header is within the memory bounds and
1337 * doesn't overlap the first part of the message.
1338 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001339 receivers_length = sizeof(struct ffa_memory_access) *
1340 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001341 constituents_offset =
1342 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001343 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001344 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001345 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001346 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001347 "Invalid composite memory region descriptor offset "
1348 "%d.\n",
1349 memory_region->receivers[0]
1350 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001351 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001352 }
1353
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001355
1356 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001357 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001358 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001359 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1360 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001361 if (memory_share_length != constituents_offset + constituents_length) {
1362 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001363 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001364 memory_region->receivers[0]
1365 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001366 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001368 if (fragment_length < memory_share_length &&
1369 fragment_length < HF_MAILBOX_SIZE) {
1370 dlog_warning(
1371 "Initial fragment length %d smaller than mailbox "
1372 "size.\n",
1373 fragment_length);
1374 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375
Andrew Walbrana65a1322020-04-06 19:32:32 +01001376 /*
1377 * Clear is not allowed for memory sharing, as the sender still has
1378 * access to the memory.
1379 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001380 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1381 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001382 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001383 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 }
1385
1386 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 }
1391
1392 /* Check that the permissions are valid. */
1393 *permissions =
1394 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001395 data_access = ffa_get_data_access_attr(*permissions);
1396 instruction_access = ffa_get_instruction_access_attr(*permissions);
1397 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1398 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001399 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1400 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001404 dlog_verbose(
1405 "Invalid instruction access permissions %#x for "
1406 "sending memory.\n",
1407 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001409 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001410 if (share_func == FFA_MEM_SHARE_32) {
1411 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 dlog_verbose(
1413 "Invalid data access permissions %#x for "
1414 "sharing memory.\n",
1415 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001416 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001417 }
1418 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001419 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1420 * required for share operations (but must not be specified by
1421 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 * returned to the retriever.
1423 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 ffa_set_instruction_access_attr(permissions,
1425 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001426 memory_region->receivers[0].receiver_permissions.permissions =
1427 *permissions;
1428 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 if (share_func == FFA_MEM_LEND_32 &&
1430 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001431 dlog_verbose(
1432 "Invalid data access permissions %#x for lending "
1433 "memory.\n",
1434 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 if (share_func == FFA_MEM_DONATE_32 &&
1438 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 dlog_verbose(
1440 "Invalid data access permissions %#x for donating "
1441 "memory.\n",
1442 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444 }
1445
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447}
1448
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001449/** Forwards a memory send message on to the TEE. */
1450static struct ffa_value memory_send_tee_forward(
1451 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1452 uint32_t share_func, struct ffa_memory_region *memory_region,
1453 uint32_t memory_share_length, uint32_t fragment_length)
1454{
1455 struct ffa_value ret;
1456
1457 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1458 memory_region, fragment_length);
1459 tee_locked.vm->mailbox.recv_size = fragment_length;
1460 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1461 tee_locked.vm->mailbox.recv_func = share_func;
1462 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1463 ret = arch_tee_call((struct ffa_value){.func = share_func,
1464 .arg1 = memory_share_length,
1465 .arg2 = fragment_length});
1466 /*
1467 * After the call to the TEE completes it must have finished reading its
1468 * RX buffer, so it is ready for another message.
1469 */
1470 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1471
1472 return ret;
1473}
1474
Andrew Walbrana65a1322020-04-06 19:32:32 +01001475/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001476 * Gets the share state for continuing an operation to donate, lend or share
1477 * memory, and checks that it is a valid request.
1478 *
1479 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1480 * not.
1481 */
1482static struct ffa_value ffa_memory_send_continue_validate(
1483 struct share_states_locked share_states, ffa_memory_handle_t handle,
1484 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1485 struct mpool *page_pool)
1486{
1487 struct ffa_memory_share_state *share_state;
1488 struct ffa_memory_region *memory_region;
1489
1490 CHECK(share_state_ret != NULL);
1491
1492 /*
1493 * Look up the share state by handle and make sure that the VM ID
1494 * matches.
1495 */
1496 if (!get_share_state(share_states, handle, &share_state)) {
1497 dlog_verbose(
1498 "Invalid handle %#x for memory send continuation.\n",
1499 handle);
1500 return ffa_error(FFA_INVALID_PARAMETERS);
1501 }
1502 memory_region = share_state->memory_region;
1503
1504 if (memory_region->sender != from_vm_id) {
1505 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1506 return ffa_error(FFA_INVALID_PARAMETERS);
1507 }
1508
1509 if (share_state->sending_complete) {
1510 dlog_verbose(
1511 "Sending of memory handle %#x is already complete.\n",
1512 handle);
1513 return ffa_error(FFA_INVALID_PARAMETERS);
1514 }
1515
1516 if (share_state->fragment_count == MAX_FRAGMENTS) {
1517 /*
1518 * Log a warning as this is a sign that MAX_FRAGMENTS should
1519 * probably be increased.
1520 */
1521 dlog_warning(
1522 "Too many fragments for memory share with handle %#x; "
1523 "only %d supported.\n",
1524 handle, MAX_FRAGMENTS);
1525 /* Free share state, as it's not possible to complete it. */
1526 share_state_free(share_states, share_state, page_pool);
1527 return ffa_error(FFA_NO_MEMORY);
1528 }
1529
1530 *share_state_ret = share_state;
1531
1532 return (struct ffa_value){.func = FFA_SUCCESS_32};
1533}
1534
1535/**
1536 * Forwards a memory send continuation message on to the TEE.
1537 */
1538static struct ffa_value memory_send_continue_tee_forward(
1539 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1540 uint32_t fragment_length, ffa_memory_handle_t handle)
1541{
1542 struct ffa_value ret;
1543
1544 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1545 fragment_length);
1546 tee_locked.vm->mailbox.recv_size = fragment_length;
1547 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1548 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1549 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1550 ret = arch_tee_call(
1551 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1552 .arg1 = (uint32_t)handle,
1553 .arg2 = (uint32_t)(handle >> 32),
1554 .arg3 = fragment_length,
1555 .arg4 = (uint64_t)sender_vm_id << 16});
1556 /*
1557 * After the call to the TEE completes it must have finished reading its
1558 * RX buffer, so it is ready for another message.
1559 */
1560 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1561
1562 return ret;
1563}
1564
1565/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001566 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1567 * updates the stage-2 page tables. Specifically, check if the message length
1568 * and number of memory region constituents match, and if the transition is
1569 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001570 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001571 * Assumes that the caller has already found and locked the sender VM and copied
1572 * the memory region descriptor from the sender's TX buffer to a freshly
1573 * allocated page from Hafnium's internal pool. The caller must have also
1574 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001575 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001576 * This function takes ownership of the `memory_region` passed in and will free
1577 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001578 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001579struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001580 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001581 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001582 uint32_t fragment_length, uint32_t share_func,
1583 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001584{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585 ffa_memory_access_permissions_t permissions;
1586 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001587 struct share_states_locked share_states;
1588 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001589
1590 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001591 * If there is an error validating the `memory_region` then we need to
1592 * free it because we own it but we won't be storing it in a share state
1593 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001594 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001595 ret = ffa_memory_send_validate(from_locked, memory_region,
1596 memory_share_length, fragment_length,
1597 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001599 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001600 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001601 }
1602
Andrew Walbrana65a1322020-04-06 19:32:32 +01001603 /* Set flag for share function, ready to be retrieved later. */
1604 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001606 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001608 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001609 case FFA_MEM_LEND_32:
1610 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001611 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001612 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001613 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001615 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001616 }
1617
Andrew Walbranca808b12020-05-15 17:22:28 +01001618 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001619 /*
1620 * Allocate a share state before updating the page table. Otherwise if
1621 * updating the page table succeeded but allocating the share state
1622 * failed then it would leave the memory in a state where nobody could
1623 * get it back.
1624 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001625 if (!allocate_share_state(share_states, share_func, memory_region,
1626 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1627 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001628 dlog_verbose("Failed to allocate share state.\n");
1629 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001630 ret = ffa_error(FFA_NO_MEMORY);
1631 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001632 }
1633
Andrew Walbranca808b12020-05-15 17:22:28 +01001634 if (fragment_length == memory_share_length) {
1635 /* No more fragments to come, everything fit in one message. */
1636 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001637 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 } else {
1639 ret = (struct ffa_value){
1640 .func = FFA_MEM_FRAG_RX_32,
1641 .arg1 = (uint32_t)share_state->handle,
1642 .arg2 = (uint32_t)(share_state->handle >> 32),
1643 .arg3 = fragment_length};
1644 }
1645
1646out:
1647 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001649 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001650}
1651
1652/**
1653 * Validates a call to donate, lend or share memory to the TEE and then updates
1654 * the stage-2 page tables. Specifically, check if the message length and number
1655 * of memory region constituents match, and if the transition is valid for the
1656 * type of memory sending operation.
1657 *
1658 * Assumes that the caller has already found and locked the sender VM and the
1659 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1660 * to a freshly allocated page from Hafnium's internal pool. The caller must
1661 * have also validated that the receiver VM ID is valid.
1662 *
1663 * This function takes ownership of the `memory_region` passed in and will free
1664 * it when necessary; it must not be freed by the caller.
1665 */
1666struct ffa_value ffa_memory_tee_send(
1667 struct vm_locked from_locked, struct vm_locked to_locked,
1668 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1669 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1670{
1671 ffa_memory_access_permissions_t permissions;
1672 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001673
1674 /*
1675 * If there is an error validating the `memory_region` then we need to
1676 * free it because we own it but we won't be storing it in a share state
1677 * after all.
1678 */
1679 ret = ffa_memory_send_validate(from_locked, memory_region,
1680 memory_share_length, fragment_length,
1681 share_func, &permissions);
1682 if (ret.func != FFA_SUCCESS_32) {
1683 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001684 }
1685
Andrew Walbranca808b12020-05-15 17:22:28 +01001686 if (fragment_length == memory_share_length) {
1687 /* No more fragments to come, everything fit in one message. */
1688 struct ffa_composite_memory_region *composite =
1689 ffa_memory_region_get_composite(memory_region, 0);
1690 struct ffa_memory_region_constituent *constituents =
1691 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001692 struct mpool local_page_pool;
1693 uint32_t orig_from_mode;
1694
1695 /*
1696 * Use a local page pool so that we can roll back if necessary.
1697 */
1698 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001699
1700 ret = ffa_send_check_update(
1701 from_locked, &constituents,
1702 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001703 permissions, &local_page_pool,
1704 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1705 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001706 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001707 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001708 goto out;
1709 }
1710
1711 /* Forward memory send message on to TEE. */
1712 ret = memory_send_tee_forward(
1713 to_locked, from_locked.vm->id, share_func,
1714 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001715
1716 if (ret.func != FFA_SUCCESS_32) {
1717 dlog_verbose(
1718 "TEE didn't successfully complete memory send "
1719 "operation; returned %#x (%d). Rolling back.\n",
1720 ret.func, ret.arg2);
1721
1722 /*
1723 * The TEE failed to complete the send operation, so
1724 * roll back the page table update for the VM. This
1725 * can't fail because it won't try to allocate more
1726 * memory than was freed into the `local_page_pool` by
1727 * `ffa_send_check_update` in the initial update.
1728 */
1729 CHECK(ffa_region_group_identity_map(
1730 from_locked, &constituents,
1731 &composite->constituent_count, 1,
1732 orig_from_mode, &local_page_pool, true));
1733 }
1734
1735 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001736 } else {
1737 struct share_states_locked share_states = share_states_lock();
1738 ffa_memory_handle_t handle;
1739
1740 /*
1741 * We need to wait for the rest of the fragments before we can
1742 * check whether the transaction is valid and unmap the memory.
1743 * Call the TEE so it can do its initial validation and assign a
1744 * handle, and allocate a share state to keep what we have so
1745 * far.
1746 */
1747 ret = memory_send_tee_forward(
1748 to_locked, from_locked.vm->id, share_func,
1749 memory_region, memory_share_length, fragment_length);
1750 if (ret.func == FFA_ERROR_32) {
1751 goto out_unlock;
1752 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1753 dlog_warning(
1754 "Got %#x from TEE in response to %#x for "
1755 "fragment with with %d/%d, expected "
1756 "FFA_MEM_FRAG_RX.\n",
1757 ret.func, share_func, fragment_length,
1758 memory_share_length);
1759 ret = ffa_error(FFA_INVALID_PARAMETERS);
1760 goto out_unlock;
1761 }
1762 handle = ffa_frag_handle(ret);
1763 if (ret.arg3 != fragment_length) {
1764 dlog_warning(
1765 "Got unexpected fragment offset %d for "
1766 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1767 ret.arg3, fragment_length);
1768 ret = ffa_error(FFA_INVALID_PARAMETERS);
1769 goto out_unlock;
1770 }
1771 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1772 dlog_warning(
1773 "Got unexpected sender ID %d for "
1774 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1775 ffa_frag_sender(ret), from_locked.vm->id);
1776 ret = ffa_error(FFA_INVALID_PARAMETERS);
1777 goto out_unlock;
1778 }
1779
1780 if (!allocate_share_state(share_states, share_func,
1781 memory_region, fragment_length,
1782 handle, NULL)) {
1783 dlog_verbose("Failed to allocate share state.\n");
1784 ret = ffa_error(FFA_NO_MEMORY);
1785 goto out_unlock;
1786 }
1787 /*
1788 * Don't free the memory region fragment, as it has been stored
1789 * in the share state.
1790 */
1791 memory_region = NULL;
1792 out_unlock:
1793 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001794 }
1795
Andrew Walbranca808b12020-05-15 17:22:28 +01001796out:
1797 if (memory_region != NULL) {
1798 mpool_free(page_pool, memory_region);
1799 }
1800 dump_share_states();
1801 return ret;
1802}
1803
1804/**
1805 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1806 * this is the last fragment then checks that the transition is valid for the
1807 * type of memory sending operation and updates the stage-2 page tables of the
1808 * sender.
1809 *
1810 * Assumes that the caller has already found and locked the sender VM and copied
1811 * the memory region descriptor from the sender's TX buffer to a freshly
1812 * allocated page from Hafnium's internal pool.
1813 *
1814 * This function takes ownership of the `fragment` passed in; it must not be
1815 * freed by the caller.
1816 */
1817struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1818 void *fragment,
1819 uint32_t fragment_length,
1820 ffa_memory_handle_t handle,
1821 struct mpool *page_pool)
1822{
1823 struct share_states_locked share_states = share_states_lock();
1824 struct ffa_memory_share_state *share_state;
1825 struct ffa_value ret;
1826 struct ffa_memory_region *memory_region;
1827
1828 ret = ffa_memory_send_continue_validate(share_states, handle,
1829 &share_state,
1830 from_locked.vm->id, page_pool);
1831 if (ret.func != FFA_SUCCESS_32) {
1832 goto out_free_fragment;
1833 }
1834 memory_region = share_state->memory_region;
1835
1836 if (memory_region->receivers[0].receiver_permissions.receiver ==
1837 HF_TEE_VM_ID) {
1838 dlog_error(
1839 "Got hypervisor-allocated handle for memory send to "
1840 "TEE. This should never happen, and indicates a bug in "
1841 "EL3 code.\n");
1842 ret = ffa_error(FFA_INVALID_PARAMETERS);
1843 goto out_free_fragment;
1844 }
1845
1846 /* Add this fragment. */
1847 share_state->fragments[share_state->fragment_count] = fragment;
1848 share_state->fragment_constituent_counts[share_state->fragment_count] =
1849 fragment_length / sizeof(struct ffa_memory_region_constituent);
1850 share_state->fragment_count++;
1851
1852 /* Check whether the memory send operation is now ready to complete. */
1853 if (share_state_sending_complete(share_states, share_state)) {
1854 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001855 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001856 } else {
1857 ret = (struct ffa_value){
1858 .func = FFA_MEM_FRAG_RX_32,
1859 .arg1 = (uint32_t)handle,
1860 .arg2 = (uint32_t)(handle >> 32),
1861 .arg3 = share_state_next_fragment_offset(share_states,
1862 share_state)};
1863 }
1864 goto out;
1865
1866out_free_fragment:
1867 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001868
1869out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001870 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001871 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001872}
1873
Andrew Walbranca808b12020-05-15 17:22:28 +01001874/**
1875 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1876 * is the last fragment then checks that the transition is valid for the type of
1877 * memory sending operation and updates the stage-2 page tables of the sender.
1878 *
1879 * Assumes that the caller has already found and locked the sender VM and copied
1880 * the memory region descriptor from the sender's TX buffer to a freshly
1881 * allocated page from Hafnium's internal pool.
1882 *
1883 * This function takes ownership of the `memory_region` passed in and will free
1884 * it when necessary; it must not be freed by the caller.
1885 */
1886struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1887 struct vm_locked to_locked,
1888 void *fragment,
1889 uint32_t fragment_length,
1890 ffa_memory_handle_t handle,
1891 struct mpool *page_pool)
1892{
1893 struct share_states_locked share_states = share_states_lock();
1894 struct ffa_memory_share_state *share_state;
1895 struct ffa_value ret;
1896 struct ffa_memory_region *memory_region;
1897
1898 ret = ffa_memory_send_continue_validate(share_states, handle,
1899 &share_state,
1900 from_locked.vm->id, page_pool);
1901 if (ret.func != FFA_SUCCESS_32) {
1902 goto out_free_fragment;
1903 }
1904 memory_region = share_state->memory_region;
1905
1906 if (memory_region->receivers[0].receiver_permissions.receiver !=
1907 HF_TEE_VM_ID) {
1908 dlog_error(
1909 "Got SPM-allocated handle for memory send to non-TEE "
1910 "VM. This should never happen, and indicates a bug.\n");
1911 ret = ffa_error(FFA_INVALID_PARAMETERS);
1912 goto out_free_fragment;
1913 }
1914
1915 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1916 to_locked.vm->mailbox.recv == NULL) {
1917 /*
1918 * If the TEE RX buffer is not available, tell the sender to
1919 * retry by returning the current offset again.
1920 */
1921 ret = (struct ffa_value){
1922 .func = FFA_MEM_FRAG_RX_32,
1923 .arg1 = (uint32_t)handle,
1924 .arg2 = (uint32_t)(handle >> 32),
1925 .arg3 = share_state_next_fragment_offset(share_states,
1926 share_state),
1927 };
1928 goto out_free_fragment;
1929 }
1930
1931 /* Add this fragment. */
1932 share_state->fragments[share_state->fragment_count] = fragment;
1933 share_state->fragment_constituent_counts[share_state->fragment_count] =
1934 fragment_length / sizeof(struct ffa_memory_region_constituent);
1935 share_state->fragment_count++;
1936
1937 /* Check whether the memory send operation is now ready to complete. */
1938 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001939 struct mpool local_page_pool;
1940 uint32_t orig_from_mode;
1941
1942 /*
1943 * Use a local page pool so that we can roll back if necessary.
1944 */
1945 mpool_init_with_fallback(&local_page_pool, page_pool);
1946
Andrew Walbranca808b12020-05-15 17:22:28 +01001947 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001948 share_state, &local_page_pool,
1949 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001950
1951 if (ret.func == FFA_SUCCESS_32) {
1952 /*
1953 * Forward final fragment on to the TEE so that
1954 * it can complete the memory sending operation.
1955 */
1956 ret = memory_send_continue_tee_forward(
1957 to_locked, from_locked.vm->id, fragment,
1958 fragment_length, handle);
1959
1960 if (ret.func != FFA_SUCCESS_32) {
1961 /*
1962 * The error will be passed on to the caller,
1963 * but log it here too.
1964 */
1965 dlog_verbose(
1966 "TEE didn't successfully complete "
1967 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001968 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001969 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001970
1971 /*
1972 * The TEE failed to complete the send
1973 * operation, so roll back the page table update
1974 * for the VM. This can't fail because it won't
1975 * try to allocate more memory than was freed
1976 * into the `local_page_pool` by
1977 * `ffa_send_check_update` in the initial
1978 * update.
1979 */
1980 CHECK(ffa_region_group_identity_map(
1981 from_locked, share_state->fragments,
1982 share_state
1983 ->fragment_constituent_counts,
1984 share_state->fragment_count,
1985 orig_from_mode, &local_page_pool,
1986 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001987 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001988
Andrew Walbranca808b12020-05-15 17:22:28 +01001989 /* Free share state. */
1990 share_state_free(share_states, share_state, page_pool);
1991 } else {
1992 /* Abort sending to TEE. */
1993 struct ffa_value tee_ret =
1994 arch_tee_call((struct ffa_value){
1995 .func = FFA_MEM_RECLAIM_32,
1996 .arg1 = (uint32_t)handle,
1997 .arg2 = (uint32_t)(handle >> 32)});
1998
1999 if (tee_ret.func != FFA_SUCCESS_32) {
2000 /*
2001 * Nothing we can do if TEE doesn't abort
2002 * properly, just log it.
2003 */
2004 dlog_verbose(
2005 "TEE didn't successfully abort failed "
2006 "memory send operation; returned %#x "
2007 "(%d).\n",
2008 tee_ret.func, tee_ret.arg2);
2009 }
2010 /*
2011 * We don't need to free the share state in this case
2012 * because ffa_memory_send_complete does that already.
2013 */
2014 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002015
2016 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002017 } else {
2018 uint32_t next_fragment_offset =
2019 share_state_next_fragment_offset(share_states,
2020 share_state);
2021
2022 ret = memory_send_continue_tee_forward(
2023 to_locked, from_locked.vm->id, fragment,
2024 fragment_length, handle);
2025
2026 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2027 ffa_frag_handle(ret) != handle ||
2028 ret.arg3 != next_fragment_offset ||
2029 ffa_frag_sender(ret) != from_locked.vm->id) {
2030 dlog_verbose(
2031 "Got unexpected result from forwarding "
2032 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2033 "offset %d, sender %d); expected "
2034 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2035 "sender %d).\n",
2036 ret.func, ffa_frag_handle(ret), ret.arg3,
2037 ffa_frag_sender(ret), handle,
2038 next_fragment_offset, from_locked.vm->id);
2039 /* Free share state. */
2040 share_state_free(share_states, share_state, page_pool);
2041 ret = ffa_error(FFA_INVALID_PARAMETERS);
2042 goto out;
2043 }
2044
2045 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2046 .arg1 = (uint32_t)handle,
2047 .arg2 = (uint32_t)(handle >> 32),
2048 .arg3 = next_fragment_offset};
2049 }
2050 goto out;
2051
2052out_free_fragment:
2053 mpool_free(page_pool, fragment);
2054
2055out:
2056 share_states_unlock(&share_states);
2057 return ret;
2058}
2059
2060/** Clean up after the receiver has finished retrieving a memory region. */
2061static void ffa_memory_retrieve_complete(
2062 struct share_states_locked share_states,
2063 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2064{
2065 if (share_state->share_func == FFA_MEM_DONATE_32) {
2066 /*
2067 * Memory that has been donated can't be relinquished,
2068 * so no need to keep the share state around.
2069 */
2070 share_state_free(share_states, share_state, page_pool);
2071 dlog_verbose("Freed share state for donate.\n");
2072 }
2073}
2074
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002075struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2076 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002077 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002078 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002079{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002080 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002081 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002082 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083 sizeof(struct ffa_memory_access);
2084 ffa_memory_handle_t handle = retrieve_request->handle;
2085 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002086 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002087 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2088 struct ffa_memory_region *memory_region;
2089 ffa_memory_access_permissions_t sent_permissions;
2090 enum ffa_data_access sent_data_access;
2091 enum ffa_instruction_access sent_instruction_access;
2092 ffa_memory_access_permissions_t requested_permissions;
2093 enum ffa_data_access requested_data_access;
2094 enum ffa_instruction_access requested_instruction_access;
2095 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002096 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002097 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002098 struct ffa_memory_share_state *share_state;
2099 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002100 struct ffa_composite_memory_region *composite;
2101 uint32_t total_length;
2102 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002103
2104 dump_share_states();
2105
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002106 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002107 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002108 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002109 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002110 expected_retrieve_request_length,
2111 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002112 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002113 }
2114
Andrew Walbrana65a1322020-04-06 19:32:32 +01002115 if (retrieve_request->receiver_count != 1) {
2116 dlog_verbose(
2117 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002118 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002119 "expected 1).\n",
2120 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002121 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002122 }
2123
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002124 share_states = share_states_lock();
2125 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002126 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002127 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002128 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002129 goto out;
2130 }
2131
Andrew Walbrana65a1322020-04-06 19:32:32 +01002132 memory_region = share_state->memory_region;
2133 CHECK(memory_region != NULL);
2134
2135 /*
2136 * Check that the transaction type expected by the receiver is correct,
2137 * if it has been specified.
2138 */
2139 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002140 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002141 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002142 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002143 dlog_verbose(
2144 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002145 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002146 transaction_type,
2147 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002149 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002151 goto out;
2152 }
2153
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002154 if (retrieve_request->sender != memory_region->sender) {
2155 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002156 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002157 "expected %d for handle %#x.\n",
2158 retrieve_request->sender, memory_region->sender,
2159 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002160 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002161 goto out;
2162 }
2163
2164 if (retrieve_request->tag != memory_region->tag) {
2165 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002166 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002167 "%d for handle %#x.\n",
2168 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002169 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002170 goto out;
2171 }
2172
Andrew Walbrana65a1322020-04-06 19:32:32 +01002173 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2174 to_locked.vm->id) {
2175 dlog_verbose(
2176 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002177 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002178 retrieve_request->receivers[0]
2179 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002180 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002181 goto out;
2182 }
2183
2184 if (memory_region->receivers[0].receiver_permissions.receiver !=
2185 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002186 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002187 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2188 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002189 to_locked.vm->id,
2190 memory_region->receivers[0]
2191 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002192 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002193 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 goto out;
2195 }
2196
Andrew Walbranca808b12020-05-15 17:22:28 +01002197 if (!share_state->sending_complete) {
2198 dlog_verbose(
2199 "Memory with handle %#x not fully sent, can't "
2200 "retrieve.\n",
2201 handle);
2202 ret = ffa_error(FFA_INVALID_PARAMETERS);
2203 goto out;
2204 }
2205
2206 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002207 dlog_verbose("Memory with handle %#x already retrieved.\n",
2208 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002209 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002210 goto out;
2211 }
2212
Andrew Walbrana65a1322020-04-06 19:32:32 +01002213 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2214 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002215 dlog_verbose(
2216 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002217 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002218 retrieve_request->receivers[0]
2219 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002220 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 goto out;
2222 }
2223
Andrew Walbrana65a1322020-04-06 19:32:32 +01002224 /*
2225 * Check permissions from sender against permissions requested by
2226 * receiver.
2227 */
2228 /* TODO: Check attributes too. */
2229 sent_permissions =
2230 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002231 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002232 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002233 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002234 requested_permissions =
2235 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002236 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002237 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002238 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002239 permissions = 0;
2240 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002241 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2242 case FFA_DATA_ACCESS_RW:
2243 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2244 requested_data_access == FFA_DATA_ACCESS_RW) {
2245 ffa_set_data_access_attr(&permissions,
2246 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002247 break;
2248 }
2249 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002250 case FFA_DATA_ACCESS_RO:
2251 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2252 requested_data_access == FFA_DATA_ACCESS_RO) {
2253 ffa_set_data_access_attr(&permissions,
2254 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002255 break;
2256 }
2257 dlog_verbose(
2258 "Invalid data access requested; sender specified "
2259 "permissions %#x but receiver requested %#x.\n",
2260 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002261 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002262 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002263 case FFA_DATA_ACCESS_RESERVED:
2264 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002265 "checked before this point.");
2266 }
2267 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002268 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2269 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002270 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002271 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2272 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2273 ffa_set_instruction_access_attr(
2274 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002275 break;
2276 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002277 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002278 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002279 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2280 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2281 ffa_set_instruction_access_attr(
2282 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002283 break;
2284 }
2285 dlog_verbose(
2286 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002287 "specified permissions %#x but receiver requested "
2288 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002289 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002290 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002291 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002292 case FFA_INSTRUCTION_ACCESS_RESERVED:
2293 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002294 "be checked before this point.");
2295 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002296 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002297
Andrew Walbran996d1d12020-05-27 14:08:43 +01002298 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002299 to_locked, share_state->fragments,
2300 share_state->fragment_constituent_counts,
2301 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002302 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002303 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002304 goto out;
2305 }
2306
2307 /*
2308 * Copy response to RX buffer of caller and deliver the message. This
2309 * must be done before the share_state is (possibly) freed.
2310 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002311 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002312 composite = ffa_memory_region_get_composite(memory_region, 0);
2313 /*
2314 * Constituents which we received in the first fragment should always
2315 * fit in the first fragment we are sending, because the header is the
2316 * same size in both cases and we have a fixed message buffer size. So
2317 * `ffa_retrieved_memory_region_init` should never fail.
2318 */
2319 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002320 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2321 memory_region->sender, memory_region->attributes,
2322 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002323 composite->page_count, composite->constituent_count,
2324 share_state->fragments[0],
2325 share_state->fragment_constituent_counts[0], &total_length,
2326 &fragment_length));
2327 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002328 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002329 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002330 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2331
Andrew Walbranca808b12020-05-15 17:22:28 +01002332 share_state->retrieved_fragment_count[0] = 1;
2333 if (share_state->retrieved_fragment_count[0] ==
2334 share_state->fragment_count) {
2335 ffa_memory_retrieve_complete(share_states, share_state,
2336 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002337 }
2338
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002339 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002340 .arg1 = total_length,
2341 .arg2 = fragment_length};
2342
2343out:
2344 share_states_unlock(&share_states);
2345 dump_share_states();
2346 return ret;
2347}
2348
2349struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2350 ffa_memory_handle_t handle,
2351 uint32_t fragment_offset,
2352 struct mpool *page_pool)
2353{
2354 struct ffa_memory_region *memory_region;
2355 struct share_states_locked share_states;
2356 struct ffa_memory_share_state *share_state;
2357 struct ffa_value ret;
2358 uint32_t fragment_index;
2359 uint32_t retrieved_constituents_count;
2360 uint32_t i;
2361 uint32_t expected_fragment_offset;
2362 uint32_t remaining_constituent_count;
2363 uint32_t fragment_length;
2364
2365 dump_share_states();
2366
2367 share_states = share_states_lock();
2368 if (!get_share_state(share_states, handle, &share_state)) {
2369 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2370 handle);
2371 ret = ffa_error(FFA_INVALID_PARAMETERS);
2372 goto out;
2373 }
2374
2375 memory_region = share_state->memory_region;
2376 CHECK(memory_region != NULL);
2377
2378 if (memory_region->receivers[0].receiver_permissions.receiver !=
2379 to_locked.vm->id) {
2380 dlog_verbose(
2381 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2382 "of handle %#x.\n",
2383 to_locked.vm->id,
2384 memory_region->receivers[0]
2385 .receiver_permissions.receiver,
2386 handle);
2387 ret = ffa_error(FFA_INVALID_PARAMETERS);
2388 goto out;
2389 }
2390
2391 if (!share_state->sending_complete) {
2392 dlog_verbose(
2393 "Memory with handle %#x not fully sent, can't "
2394 "retrieve.\n",
2395 handle);
2396 ret = ffa_error(FFA_INVALID_PARAMETERS);
2397 goto out;
2398 }
2399
2400 if (share_state->retrieved_fragment_count[0] == 0 ||
2401 share_state->retrieved_fragment_count[0] >=
2402 share_state->fragment_count) {
2403 dlog_verbose(
2404 "Retrieval of memory with handle %#x not yet started "
2405 "or already completed (%d/%d fragments retrieved).\n",
2406 handle, share_state->retrieved_fragment_count[0],
2407 share_state->fragment_count);
2408 ret = ffa_error(FFA_INVALID_PARAMETERS);
2409 goto out;
2410 }
2411
2412 fragment_index = share_state->retrieved_fragment_count[0];
2413
2414 /*
2415 * Check that the given fragment offset is correct by counting how many
2416 * constituents were in the fragments previously sent.
2417 */
2418 retrieved_constituents_count = 0;
2419 for (i = 0; i < fragment_index; ++i) {
2420 retrieved_constituents_count +=
2421 share_state->fragment_constituent_counts[i];
2422 }
2423 expected_fragment_offset =
2424 ffa_composite_constituent_offset(memory_region, 0) +
2425 retrieved_constituents_count *
2426 sizeof(struct ffa_memory_region_constituent);
2427 if (fragment_offset != expected_fragment_offset) {
2428 dlog_verbose("Fragment offset was %d but expected %d.\n",
2429 fragment_offset, expected_fragment_offset);
2430 ret = ffa_error(FFA_INVALID_PARAMETERS);
2431 goto out;
2432 }
2433
2434 remaining_constituent_count = ffa_memory_fragment_init(
2435 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2436 share_state->fragments[fragment_index],
2437 share_state->fragment_constituent_counts[fragment_index],
2438 &fragment_length);
2439 CHECK(remaining_constituent_count == 0);
2440 to_locked.vm->mailbox.recv_size = fragment_length;
2441 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2442 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2443 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2444 share_state->retrieved_fragment_count[0]++;
2445 if (share_state->retrieved_fragment_count[0] ==
2446 share_state->fragment_count) {
2447 ffa_memory_retrieve_complete(share_states, share_state,
2448 page_pool);
2449 }
2450
2451 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2452 .arg1 = (uint32_t)handle,
2453 .arg2 = (uint32_t)(handle >> 32),
2454 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002455
2456out:
2457 share_states_unlock(&share_states);
2458 dump_share_states();
2459 return ret;
2460}
2461
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002462struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002463 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002464 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002465{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002466 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002467 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002468 struct ffa_memory_share_state *share_state;
2469 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002470 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002471 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472
Andrew Walbrana65a1322020-04-06 19:32:32 +01002473 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002474 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002475 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002476 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002477 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002478 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002479 }
2480
Andrew Walbrana65a1322020-04-06 19:32:32 +01002481 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002482 dlog_verbose(
2483 "VM ID %d in relinquish message doesn't match calling "
2484 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002485 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002486 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002487 }
2488
2489 dump_share_states();
2490
2491 share_states = share_states_lock();
2492 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002493 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002494 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002495 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002496 goto out;
2497 }
2498
Andrew Walbranca808b12020-05-15 17:22:28 +01002499 if (!share_state->sending_complete) {
2500 dlog_verbose(
2501 "Memory with handle %#x not fully sent, can't "
2502 "relinquish.\n",
2503 handle);
2504 ret = ffa_error(FFA_INVALID_PARAMETERS);
2505 goto out;
2506 }
2507
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002508 memory_region = share_state->memory_region;
2509 CHECK(memory_region != NULL);
2510
Andrew Walbrana65a1322020-04-06 19:32:32 +01002511 if (memory_region->receivers[0].receiver_permissions.receiver !=
2512 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002513 dlog_verbose(
2514 "VM ID %d tried to relinquish memory region with "
2515 "handle %#x but receiver was %d.\n",
2516 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002517 memory_region->receivers[0]
2518 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002519 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002520 goto out;
2521 }
2522
Andrew Walbranca808b12020-05-15 17:22:28 +01002523 if (share_state->retrieved_fragment_count[0] !=
2524 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002526 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 "relinquish.\n",
2528 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002529 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530 goto out;
2531 }
2532
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002533 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002534
2535 /*
2536 * Clear is not allowed for memory that was shared, as the original
2537 * sender still has access to the memory.
2538 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002539 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002540 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002541 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 goto out;
2543 }
2544
Andrew Walbranca808b12020-05-15 17:22:28 +01002545 ret = ffa_relinquish_check_update(
2546 from_locked, share_state->fragments,
2547 share_state->fragment_constituent_counts,
2548 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002549
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002550 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551 /*
2552 * Mark memory handle as not retrieved, so it can be reclaimed
2553 * (or retrieved again).
2554 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002555 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556 }
2557
2558out:
2559 share_states_unlock(&share_states);
2560 dump_share_states();
2561 return ret;
2562}
2563
2564/**
2565 * Validates that the reclaim transition is allowed for the given handle,
2566 * updates the page table of the reclaiming VM, and frees the internal state
2567 * associated with the handle.
2568 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002569struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002570 ffa_memory_handle_t handle,
2571 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002572 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002573{
2574 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002575 struct ffa_memory_share_state *share_state;
2576 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002577 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002578 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002579
2580 dump_share_states();
2581
2582 share_states = share_states_lock();
2583 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002584 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002585 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002586 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002587 goto out;
2588 }
2589
2590 memory_region = share_state->memory_region;
2591 CHECK(memory_region != NULL);
2592
2593 if (to_locked.vm->id != memory_region->sender) {
2594 dlog_verbose(
2595 "VM %d attempted to reclaim memory handle %#x "
2596 "originally sent by VM %d.\n",
2597 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002598 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002599 goto out;
2600 }
2601
Andrew Walbranca808b12020-05-15 17:22:28 +01002602 if (!share_state->sending_complete) {
2603 dlog_verbose(
2604 "Memory with handle %#x not fully sent, can't "
2605 "reclaim.\n",
2606 handle);
2607 ret = ffa_error(FFA_INVALID_PARAMETERS);
2608 goto out;
2609 }
2610
2611 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002612 dlog_verbose(
2613 "Tried to reclaim memory handle %#x that has not been "
2614 "relinquished.\n",
2615 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002616 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002617 goto out;
2618 }
2619
Andrew Walbranca808b12020-05-15 17:22:28 +01002620 ret = ffa_retrieve_check_update(
2621 to_locked, share_state->fragments,
2622 share_state->fragment_constituent_counts,
2623 share_state->fragment_count, memory_to_attributes,
2624 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002625
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002626 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002627 share_state_free(share_states, share_state, page_pool);
2628 dlog_verbose("Freed share state after successful reclaim.\n");
2629 }
2630
2631out:
2632 share_states_unlock(&share_states);
2633 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002634}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002635
2636/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002637 * Validates that the reclaim transition is allowed for the memory region with
2638 * the given handle which was previously shared with the TEE, tells the TEE to
2639 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2640 *
2641 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002642 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002643struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002644 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002646 ffa_memory_region_flags_t flags,
2647 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002648{
Andrew Walbranca808b12020-05-15 17:22:28 +01002649 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2650 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2651 struct ffa_value tee_ret;
2652 uint32_t length;
2653 uint32_t fragment_length;
2654 uint32_t fragment_offset;
2655 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002656 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002657 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2658
2659 CHECK(request_length <= HF_MAILBOX_SIZE);
2660 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2661
2662 /* Retrieve memory region information from the TEE. */
2663 tee_ret = arch_tee_call(
2664 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2665 .arg1 = request_length,
2666 .arg2 = request_length});
2667 if (tee_ret.func == FFA_ERROR_32) {
2668 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2669 return tee_ret;
2670 }
2671 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2672 dlog_verbose(
2673 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2674 tee_ret.func);
2675 return ffa_error(FFA_INVALID_PARAMETERS);
2676 }
2677
2678 length = tee_ret.arg1;
2679 fragment_length = tee_ret.arg2;
2680
2681 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2682 length > sizeof(tee_retrieve_buffer)) {
2683 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2684 fragment_length, length, HF_MAILBOX_SIZE,
2685 sizeof(tee_retrieve_buffer));
2686 return ffa_error(FFA_INVALID_PARAMETERS);
2687 }
2688
2689 /*
2690 * Copy the first fragment of the memory region descriptor to an
2691 * internal buffer.
2692 */
2693 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2694 from_locked.vm->mailbox.send, fragment_length);
2695
2696 /* Fetch the remaining fragments into the same buffer. */
2697 fragment_offset = fragment_length;
2698 while (fragment_offset < length) {
2699 tee_ret = arch_tee_call(
2700 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2701 .arg1 = (uint32_t)handle,
2702 .arg2 = (uint32_t)(handle >> 32),
2703 .arg3 = fragment_offset});
2704 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2705 dlog_verbose(
2706 "Got %#x (%d) from TEE in response to "
2707 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2708 tee_ret.func, tee_ret.arg2);
2709 return tee_ret;
2710 }
2711 if (ffa_frag_handle(tee_ret) != handle) {
2712 dlog_verbose(
2713 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2714 "in response to FFA_MEM_FRAG_RX for handle "
2715 "%#x.\n",
2716 ffa_frag_handle(tee_ret), handle);
2717 return ffa_error(FFA_INVALID_PARAMETERS);
2718 }
2719 if (ffa_frag_sender(tee_ret) != 0) {
2720 dlog_verbose(
2721 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2722 "(expected 0).\n",
2723 ffa_frag_sender(tee_ret));
2724 return ffa_error(FFA_INVALID_PARAMETERS);
2725 }
2726 fragment_length = tee_ret.arg3;
2727 if (fragment_length > HF_MAILBOX_SIZE ||
2728 fragment_offset + fragment_length > length) {
2729 dlog_verbose(
2730 "Invalid fragment length %d at offset %d (max "
2731 "%d).\n",
2732 fragment_length, fragment_offset,
2733 HF_MAILBOX_SIZE);
2734 return ffa_error(FFA_INVALID_PARAMETERS);
2735 }
2736 memcpy_s(tee_retrieve_buffer + fragment_offset,
2737 sizeof(tee_retrieve_buffer) - fragment_offset,
2738 from_locked.vm->mailbox.send, fragment_length);
2739
2740 fragment_offset += fragment_length;
2741 }
2742
2743 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002744
2745 if (memory_region->receiver_count != 1) {
2746 /* Only one receiver supported by Hafnium for now. */
2747 dlog_verbose(
2748 "Multiple recipients not supported (got %d, expected "
2749 "1).\n",
2750 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002751 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002752 }
2753
2754 if (memory_region->handle != handle) {
2755 dlog_verbose(
2756 "Got memory region handle %#x from TEE but requested "
2757 "handle %#x.\n",
2758 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002759 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002760 }
2761
2762 /* The original sender must match the caller. */
2763 if (to_locked.vm->id != memory_region->sender) {
2764 dlog_verbose(
2765 "VM %d attempted to reclaim memory handle %#x "
2766 "originally sent by VM %d.\n",
2767 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002768 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002769 }
2770
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002771 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002772
2773 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002774 * Validate that the reclaim transition is allowed for the given memory
2775 * region, forward the request to the TEE and then map the memory back
2776 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002777 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002778 return ffa_tee_reclaim_check_update(
2779 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002780 composite->constituent_count, memory_to_attributes,
2781 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002782}