blob: f51bbd8be77fb5612aaeec93f205ab3603954566 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
Olivier Deprez112d2b52020-09-30 07:39:23 +020011#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020012#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000013
Jose Marinho75509b42019-04-09 09:34:59 +010014#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000015#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010016#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010017#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000019#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000021#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000023/** The maximum number of recipients a memory region may be sent to. */
24#define MAX_MEM_SHARE_RECIPIENTS 1
25
26/**
27 * The maximum number of memory sharing handles which may be active at once. A
28 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
29 * or LEND handle is active from when it is sent to when it is reclaimed.
30 */
31#define MAX_MEM_SHARES 100
32
Andrew Walbranca808b12020-05-15 17:22:28 +010033/**
34 * The maximum number of fragments into which a memory sharing message may be
35 * broken.
36 */
37#define MAX_FRAGMENTS 20
38
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
40 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000041 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010042static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
43 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000044 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010046 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010047static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
48 "struct ffa_memory_access must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
50 "struct ffa_memory_region must be a multiple of 16 bytes long.");
51static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
52 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000053 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010055struct ffa_memory_share_state {
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 /**
J-Alves2a0d2882020-10-29 14:49:50 +000081 * The sender's original mode before invoking the FF-A function for
82 * sharing the memory.
83 * This is used to reset the original configuration when sender invokes
84 * FFA_MEM_RECLAIM_32.
85 */
86 uint32_t sender_orig_mode;
87
88 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010089 * True if all the fragments of this sharing request have been sent and
90 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091 */
Andrew Walbranca808b12020-05-15 17:22:28 +010092 bool sending_complete;
93
94 /**
95 * How many fragments of the memory region each recipient has retrieved
96 * so far. The order of this array matches the order of the endpoint
97 * memory access descriptors in the memory region descriptor. Any
98 * entries beyond the receiver_count will always be 0.
99 */
100 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000101};
102
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000103/**
104 * Encapsulates the set of share states while the `share_states_lock` is held.
105 */
106struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100107 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108};
109
110/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100111 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 * by this lock.
113 */
114static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100115static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116
117/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100118 * Buffer for retrieving memory region information from the TEE for when a
119 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
120 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000121 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100122alignas(PAGE_SIZE) static uint8_t
123 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
124
125/**
J-Alves917d2f22020-10-30 18:39:30 +0000126 * Extracts the index from a memory handle allocated by Hafnium's current world.
127 */
128uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
129{
130 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
131}
132
133/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100134 * Initialises the next available `struct ffa_memory_share_state` and sets
135 * `share_state_ret` to a pointer to it. If `handle` is
136 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
137 * uses the provided handle which is assumed to be globally unique.
138 *
139 * Returns true on success or false if none are available.
140 */
141static bool allocate_share_state(
142 struct share_states_locked share_states, uint32_t share_func,
143 struct ffa_memory_region *memory_region, uint32_t fragment_length,
144 ffa_memory_handle_t handle,
145 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100147 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000149 assert(share_states.share_states != NULL);
150 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100153 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100155 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100156 &share_states.share_states[i];
157 struct ffa_composite_memory_region *composite =
158 ffa_memory_region_get_composite(memory_region,
159 0);
160
161 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000162 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200163 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000165 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100166 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000167 allocated_state->share_func = share_func;
168 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 allocated_state->fragment_count = 1;
170 allocated_state->fragments[0] = composite->constituents;
171 allocated_state->fragment_constituent_counts[0] =
172 (fragment_length -
173 ffa_composite_constituent_offset(memory_region,
174 0)) /
175 sizeof(struct ffa_memory_region_constituent);
176 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000177 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 allocated_state->retrieved_fragment_count[j] =
179 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100181 if (share_state_ret != NULL) {
182 *share_state_ret = allocated_state;
183 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000184 return true;
185 }
186 }
187
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188 return false;
189}
190
191/** Locks the share states lock. */
192struct share_states_locked share_states_lock(void)
193{
194 sl_lock(&share_states_lock_instance);
195
196 return (struct share_states_locked){.share_states = share_states};
197}
198
199/** Unlocks the share states lock. */
200static void share_states_unlock(struct share_states_locked *share_states)
201{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000202 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203 share_states->share_states = NULL;
204 sl_unlock(&share_states_lock_instance);
205}
206
207/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100208 * If the given handle is a valid handle for an allocated share state then
209 * initialises `share_state_ret` to point to the share state and returns true.
210 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000211 */
212static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100213 ffa_memory_handle_t handle,
214 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100216 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000217 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000218
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000219 assert(share_states.share_states != NULL);
220 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100221
222 /*
223 * First look for a share_state allocated by us, in which case the
224 * handle is based on the index.
225 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200226 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000227 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100228 if (index < MAX_MEM_SHARES) {
229 share_state = &share_states.share_states[index];
230 if (share_state->share_func != 0) {
231 *share_state_ret = share_state;
232 return true;
233 }
234 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000235 }
236
Andrew Walbranca808b12020-05-15 17:22:28 +0100237 /* Fall back to a linear scan. */
238 for (index = 0; index < MAX_MEM_SHARES; ++index) {
239 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000240 if (share_state->memory_region != NULL &&
241 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100242 share_state->share_func != 0) {
243 *share_state_ret = share_state;
244 return true;
245 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246 }
247
Andrew Walbranca808b12020-05-15 17:22:28 +0100248 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000249}
250
251/** Marks a share state as unallocated. */
252static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100253 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000254 struct mpool *page_pool)
255{
Andrew Walbranca808b12020-05-15 17:22:28 +0100256 uint32_t i;
257
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000258 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000259 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100260 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000261 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100262 /*
263 * First fragment is part of the same page as the `memory_region`, so it
264 * doesn't need to be freed separately.
265 */
266 share_state->fragments[0] = NULL;
267 share_state->fragment_constituent_counts[0] = 0;
268 for (i = 1; i < share_state->fragment_count; ++i) {
269 mpool_free(page_pool, share_state->fragments[i]);
270 share_state->fragments[i] = NULL;
271 share_state->fragment_constituent_counts[i] = 0;
272 }
273 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274 share_state->memory_region = NULL;
275}
276
Andrew Walbranca808b12020-05-15 17:22:28 +0100277/** Checks whether the given share state has been fully sent. */
278static bool share_state_sending_complete(
279 struct share_states_locked share_states,
280 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000281{
Andrew Walbranca808b12020-05-15 17:22:28 +0100282 struct ffa_composite_memory_region *composite;
283 uint32_t expected_constituent_count;
284 uint32_t fragment_constituent_count_total = 0;
285 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000286
Andrew Walbranca808b12020-05-15 17:22:28 +0100287 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000288 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100289
290 /*
291 * Share state must already be valid, or it's not possible to get hold
292 * of it.
293 */
294 CHECK(share_state->memory_region != NULL &&
295 share_state->share_func != 0);
296
297 composite =
298 ffa_memory_region_get_composite(share_state->memory_region, 0);
299 expected_constituent_count = composite->constituent_count;
300 for (i = 0; i < share_state->fragment_count; ++i) {
301 fragment_constituent_count_total +=
302 share_state->fragment_constituent_counts[i];
303 }
304 dlog_verbose(
305 "Checking completion: constituent count %d/%d from %d "
306 "fragments.\n",
307 fragment_constituent_count_total, expected_constituent_count,
308 share_state->fragment_count);
309
310 return fragment_constituent_count_total == expected_constituent_count;
311}
312
313/**
314 * Calculates the offset of the next fragment expected for the given share
315 * state.
316 */
317static uint32_t share_state_next_fragment_offset(
318 struct share_states_locked share_states,
319 struct ffa_memory_share_state *share_state)
320{
321 uint32_t next_fragment_offset;
322 uint32_t i;
323
324 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000325 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100326
327 next_fragment_offset =
328 ffa_composite_constituent_offset(share_state->memory_region, 0);
329 for (i = 0; i < share_state->fragment_count; ++i) {
330 next_fragment_offset +=
331 share_state->fragment_constituent_counts[i] *
332 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 }
334
Andrew Walbranca808b12020-05-15 17:22:28 +0100335 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000336}
337
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000339{
340 uint32_t i;
341
342 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
343 return;
344 }
345
Olivier Deprez935e1b12020-12-22 18:01:29 +0100346 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100347 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100348 "recipients [",
349 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100350 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100351 memory_region->receiver_count);
352 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 if (i != 0) {
354 dlog(", ");
355 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100356 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100357 memory_region->receivers[i].receiver_permissions.receiver,
358 memory_region->receivers[i]
359 .receiver_permissions.permissions,
360 memory_region->receivers[i]
361 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000362 }
363 dlog("]");
364}
365
366static void dump_share_states(void)
367{
368 uint32_t i;
369
370 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
371 return;
372 }
373
374 dlog("Current share states:\n");
375 sl_lock(&share_states_lock_instance);
376 for (i = 0; i < MAX_MEM_SHARES; ++i) {
377 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000380 dlog("SHARE");
381 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100382 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000383 dlog("LEND");
384 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100385 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000386 dlog("DONATE");
387 break;
388 default:
389 dlog("invalid share_func %#x",
390 share_states[i].share_func);
391 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100392 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100394 if (share_states[i].sending_complete) {
395 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000396 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100397 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000398 }
J-Alves2a0d2882020-10-29 14:49:50 +0000399 dlog(" with %d fragments, %d retrieved, "
400 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100401 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000402 share_states[i].retrieved_fragment_count[0],
403 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000404 }
405 }
406 sl_unlock(&share_states_lock_instance);
407}
408
Andrew Walbran475c1452020-02-07 13:22:22 +0000409/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100410static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100411 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000412{
413 uint32_t mode = 0;
414
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415 switch (ffa_get_data_access_attr(permissions)) {
416 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000417 mode = MM_MODE_R;
418 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000420 mode = MM_MODE_R | MM_MODE_W;
421 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100422 case FFA_DATA_ACCESS_NOT_SPECIFIED:
423 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
424 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100425 case FFA_DATA_ACCESS_RESERVED:
426 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100427 }
428
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100429 switch (ffa_get_instruction_access_attr(permissions)) {
430 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000431 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100433 mode |= MM_MODE_X;
434 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100435 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
436 mode |= (default_mode & MM_MODE_X);
437 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100438 case FFA_INSTRUCTION_ACCESS_RESERVED:
439 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000440 }
441
442 return mode;
443}
444
Jose Marinho75509b42019-04-09 09:34:59 +0100445/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000446 * Get the current mode in the stage-2 page table of the given vm of all the
447 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100449 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000451 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 struct ffa_memory_region_constituent **fragments,
453 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100454{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100455 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100456 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100457
Andrew Walbranca808b12020-05-15 17:22:28 +0100458 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100459 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460 * Fail if there are no constituents. Otherwise we would get an
461 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100462 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100463 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100464 }
465
Andrew Walbranca808b12020-05-15 17:22:28 +0100466 for (i = 0; i < fragment_count; ++i) {
467 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
468 ipaddr_t begin = ipa_init(fragments[i][j].address);
469 size_t size = fragments[i][j].page_count * PAGE_SIZE;
470 ipaddr_t end = ipa_add(begin, size);
471 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100472
Andrew Walbranca808b12020-05-15 17:22:28 +0100473 /* Fail if addresses are not page-aligned. */
474 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
475 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
476 return ffa_error(FFA_INVALID_PARAMETERS);
477 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100478
Andrew Walbranca808b12020-05-15 17:22:28 +0100479 /*
480 * Ensure that this constituent memory range is all
481 * mapped with the same mode.
482 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800483 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100484 return ffa_error(FFA_DENIED);
485 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100486
Andrew Walbranca808b12020-05-15 17:22:28 +0100487 /*
488 * Ensure that all constituents are mapped with the same
489 * mode.
490 */
491 if (i == 0) {
492 *orig_mode = current_mode;
493 } else if (current_mode != *orig_mode) {
494 dlog_verbose(
495 "Expected mode %#x but was %#x for %d "
496 "pages at %#x.\n",
497 *orig_mode, current_mode,
498 fragments[i][j].page_count,
499 ipa_addr(begin));
500 return ffa_error(FFA_DENIED);
501 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100502 }
Jose Marinho75509b42019-04-09 09:34:59 +0100503 }
504
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000506}
507
508/**
509 * Verify that all pages have the same mode, that the starting mode
510 * constitutes a valid state and obtain the next mode to apply
511 * to the sending VM.
512 *
513 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100514 * 1) FFA_DENIED if a state transition was not found;
515 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100516 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100517 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100518 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
520 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100522static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100523 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100525 struct ffa_memory_region_constituent **fragments,
526 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
527 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528{
529 const uint32_t state_mask =
530 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
J-Alves7cd5eb32020-10-16 19:06:10 +0100531 uint32_t required_from_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100532 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000533
Andrew Walbranca808b12020-05-15 17:22:28 +0100534 ret = constituents_get_mode(from, orig_from_mode, fragments,
535 fragment_constituent_counts,
536 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100538 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100539 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100540 }
541
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000542 /* Ensure the address range is normal memory and not a device. */
543 if (*orig_from_mode & MM_MODE_D) {
544 dlog_verbose("Can't share device memory (mode is %#x).\n",
545 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547 }
548
549 /*
550 * Ensure the sender is the owner and has exclusive access to the
551 * memory.
552 */
553 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100554 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100555 }
556
J-Alves7cd5eb32020-10-16 19:06:10 +0100557 required_from_mode =
558 ffa_memory_permissions_to_mode(permissions, *orig_from_mode);
559
Andrew Walbrana65a1322020-04-06 19:32:32 +0100560 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
561 dlog_verbose(
562 "Sender tried to send memory with permissions which "
563 "required mode %#x but only had %#x itself.\n",
564 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100565 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566 }
567
568 /* Find the appropriate new mode. */
569 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000570 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100573 break;
574
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000576 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100577 break;
578
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000580 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100581 break;
582
Jose Marinho75509b42019-04-09 09:34:59 +0100583 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100585 }
586
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100587 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000588}
589
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000591 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100592 struct ffa_memory_region_constituent **fragments,
593 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
594 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000595{
596 const uint32_t state_mask =
597 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
598 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000600
Andrew Walbranca808b12020-05-15 17:22:28 +0100601 ret = constituents_get_mode(from, orig_from_mode, fragments,
602 fragment_constituent_counts,
603 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100604 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100605 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 }
607
608 /* Ensure the address range is normal memory and not a device. */
609 if (*orig_from_mode & MM_MODE_D) {
610 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
611 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 }
614
615 /*
616 * Ensure the relinquishing VM is not the owner but has access to the
617 * memory.
618 */
619 orig_from_state = *orig_from_mode & state_mask;
620 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
621 dlog_verbose(
622 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100623 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000624 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100625 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 }
627
628 /* Find the appropriate new mode. */
629 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
630
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632}
633
634/**
635 * Verify that all pages have the same mode, that the starting mode
636 * constitutes a valid state and obtain the next mode to apply
637 * to the retrieving VM.
638 *
639 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 * 1) FFA_DENIED if a state transition was not found;
641 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100642 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100644 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100645 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
646 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000647 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100648static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000649 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100650 struct ffa_memory_region_constituent **fragments,
651 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
652 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653{
654 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000656
Andrew Walbranca808b12020-05-15 17:22:28 +0100657 ret = constituents_get_mode(to, &orig_to_mode, fragments,
658 fragment_constituent_counts,
659 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100661 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100662 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000663 }
664
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100665 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000666 const uint32_t state_mask =
667 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
668 uint32_t orig_to_state = orig_to_mode & state_mask;
669
J-Alves9256f162021-12-09 13:18:43 +0000670 /*
671 * If the original ffa memory send call has been processed
672 * successfully, it is expected the orig_to_mode would overlay
673 * with `state_mask`, as a result of the function
674 * `ffa_send_check_transition`.
675 */
676 assert(orig_to_state != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000677 } else {
678 /*
679 * Ensure the retriever has the expected state. We don't care
680 * about the MM_MODE_SHARED bit; either with or without it set
681 * are both valid representations of the !O-NA state.
682 */
683 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
684 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100685 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 }
687 }
688
689 /* Find the appropriate new mode. */
690 *to_mode = memory_to_attributes;
691 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100692 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000693 *to_mode |= 0;
694 break;
695
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000697 *to_mode |= MM_MODE_UNOWNED;
698 break;
699
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100700 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000701 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
702 break;
703
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100704 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000705 *to_mode |= 0;
706 break;
707
708 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100709 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000711 }
712
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100714}
Jose Marinho09b1db82019-08-08 09:16:59 +0100715
716/**
717 * Updates a VM's page table such that the given set of physical address ranges
718 * are mapped in the address space at the corresponding address ranges, in the
719 * mode provided.
720 *
721 * If commit is false, the page tables will be allocated from the mpool but no
722 * mappings will actually be updated. This function must always be called first
723 * with commit false to check that it will succeed before calling with commit
724 * true, to avoid leaving the page table in a half-updated state. To make a
725 * series of changes atomically you can call them all with commit false before
726 * calling them all with commit true.
727 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700728 * vm_ptable_defrag should always be called after a series of page table
729 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100730 *
731 * Returns true on success, or false if the update failed and no changes were
732 * made to memory mappings.
733 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000735 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100736 struct ffa_memory_region_constituent **fragments,
737 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100738 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100739{
Andrew Walbranca808b12020-05-15 17:22:28 +0100740 uint32_t i;
741 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100742
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700743 if (vm_locked.vm->el0_partition) {
744 mode |= MM_MODE_USER | MM_MODE_NG;
745 }
746
Andrew Walbranca808b12020-05-15 17:22:28 +0100747 /* Iterate over the memory region constituents within each fragment. */
748 for (i = 0; i < fragment_count; ++i) {
749 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
750 size_t size = fragments[i][j].page_count * PAGE_SIZE;
751 paddr_t pa_begin =
752 pa_from_ipa(ipa_init(fragments[i][j].address));
753 paddr_t pa_end = pa_add(pa_begin, size);
754
755 if (commit) {
756 vm_identity_commit(vm_locked, pa_begin, pa_end,
757 mode, ppool, NULL);
758 } else if (!vm_identity_prepare(vm_locked, pa_begin,
759 pa_end, mode, ppool)) {
760 return false;
761 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100762 }
763 }
764
765 return true;
766}
767
768/**
769 * Clears a region of physical memory by overwriting it with zeros. The data is
770 * flushed from the cache so the memory has been cleared across the system.
771 */
772static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
773{
774 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000775 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100776 * global mapping of the whole range. Such an approach will limit
777 * the changes to stage-1 tables and will allow only local
778 * invalidation.
779 */
780 bool ret;
781 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
782 void *ptr =
783 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
784 size_t size = pa_difference(begin, end);
785
786 if (!ptr) {
787 /* TODO: partial defrag of failed range. */
788 /* Recover any memory consumed in failed mapping. */
789 mm_defrag(stage1_locked, ppool);
790 goto fail;
791 }
792
793 memset_s(ptr, size, 0, size);
794 arch_mm_flush_dcache(ptr, size);
795 mm_unmap(stage1_locked, begin, end, ppool);
796
797 ret = true;
798 goto out;
799
800fail:
801 ret = false;
802
803out:
804 mm_unlock_stage1(&stage1_locked);
805
806 return ret;
807}
808
809/**
810 * Clears a region of physical memory by overwriting it with zeros. The data is
811 * flushed from the cache so the memory has been cleared across the system.
812 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100813static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100814 struct ffa_memory_region_constituent **fragments,
815 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
816 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100817{
818 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100819 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100820 struct mm_stage1_locked stage1_locked;
821 bool ret = false;
822
823 /*
824 * Create a local pool so any freed memory can't be used by another
825 * thread. This is to ensure each constituent that is mapped can be
826 * unmapped again afterwards.
827 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000828 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100829
Andrew Walbranca808b12020-05-15 17:22:28 +0100830 /* Iterate over the memory region constituents within each fragment. */
831 for (i = 0; i < fragment_count; ++i) {
832 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100833
Andrew Walbranca808b12020-05-15 17:22:28 +0100834 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
835 size_t size = fragments[i][j].page_count * PAGE_SIZE;
836 paddr_t begin =
837 pa_from_ipa(ipa_init(fragments[i][j].address));
838 paddr_t end = pa_add(begin, size);
839
840 if (!clear_memory(begin, end, &local_page_pool)) {
841 /*
842 * api_clear_memory will defrag on failure, so
843 * no need to do it here.
844 */
845 goto out;
846 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100847 }
848 }
849
850 /*
851 * Need to defrag after clearing, as it may have added extra mappings to
852 * the stage 1 page table.
853 */
854 stage1_locked = mm_lock_stage1();
855 mm_defrag(stage1_locked, &local_page_pool);
856 mm_unlock_stage1(&stage1_locked);
857
858 ret = true;
859
860out:
861 mpool_fini(&local_page_pool);
862 return ret;
863}
864
865/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000866 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100867 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000868 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 *
870 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100872 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100873 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100874 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
875 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100876 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100877 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100878 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100880static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000881 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100882 struct ffa_memory_region_constituent **fragments,
883 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
884 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100885 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100886{
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100888 uint32_t orig_from_mode;
889 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100890 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100892
893 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100894 * Make sure constituents are properly aligned to a 64-bit boundary. If
895 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100897 for (i = 0; i < fragment_count; ++i) {
898 if (!is_aligned(fragments[i], 8)) {
899 dlog_verbose("Constituents not aligned.\n");
900 return ffa_error(FFA_INVALID_PARAMETERS);
901 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 }
903
904 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000905 * Check if the state transition is lawful for the sender, ensure that
906 * all constituents of a memory region being shared are at the same
907 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100908 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100909 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100910 &orig_from_mode, fragments,
911 fragment_constituent_counts,
912 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100914 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100915 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100916 }
917
Andrew Walbran37c574e2020-06-03 11:45:46 +0100918 if (orig_from_mode_ret != NULL) {
919 *orig_from_mode_ret = orig_from_mode;
920 }
921
Jose Marinho09b1db82019-08-08 09:16:59 +0100922 /*
923 * Create a local pool so any freed memory can't be used by another
924 * thread. This is to ensure the original mapping can be restored if the
925 * clear fails.
926 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000927 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100928
929 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000930 * First reserve all required memory for the new page table entries
931 * without committing, to make sure the entire operation will succeed
932 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100933 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 if (!ffa_region_group_identity_map(
935 from_locked, fragments, fragment_constituent_counts,
936 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100937 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100939 goto out;
940 }
941
942 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000943 * Update the mapping for the sender. This won't allocate because the
944 * transaction was already prepared above, but may free pages in the
945 * case that a whole block is being unmapped that was previously
946 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100947 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100948 CHECK(ffa_region_group_identity_map(
949 from_locked, fragments, fragment_constituent_counts,
950 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100951
952 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 fragments, fragment_constituent_counts,
955 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100956 /*
957 * On failure, roll back by returning memory to the sender. This
958 * may allocate pages which were previously freed into
959 * `local_page_pool` by the call above, but will never allocate
960 * more pages than that so can never fail.
961 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100963 from_locked, fragments, fragment_constituent_counts,
964 fragment_count, orig_from_mode, &local_page_pool,
965 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100966
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100968 goto out;
969 }
970
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100971 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972
973out:
974 mpool_fini(&local_page_pool);
975
976 /*
977 * Tidy up the page table by reclaiming failed mappings (if there was an
978 * error) or merging entries into blocks where possible (on success).
979 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700980 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000981
982 return ret;
983}
984
985/**
986 * Validates and maps memory shared from one VM to another.
987 *
988 * This function requires the calling context to hold the <to> lock.
989 *
990 * Returns:
991 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100994 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000995 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100998static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000999 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001000 struct ffa_memory_region_constituent **fragments,
1001 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1002 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1003 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004{
Andrew Walbranca808b12020-05-15 17:22:28 +01001005 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001006 uint32_t to_mode;
1007 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009
1010 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001011 * Make sure constituents are properly aligned to a 64-bit boundary. If
1012 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001014 for (i = 0; i < fragment_count; ++i) {
1015 if (!is_aligned(fragments[i], 8)) {
1016 return ffa_error(FFA_INVALID_PARAMETERS);
1017 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 }
1019
1020 /*
1021 * Check if the state transition is lawful for the recipient, and ensure
1022 * that all constituents of the memory region being retrieved are at the
1023 * same state.
1024 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001025 ret = ffa_retrieve_check_transition(
1026 to_locked, share_func, fragments, fragment_constituent_counts,
1027 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001028 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001029 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001030 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 }
1032
1033 /*
1034 * Create a local pool so any freed memory can't be used by another
1035 * thread. This is to ensure the original mapping can be restored if the
1036 * clear fails.
1037 */
1038 mpool_init_with_fallback(&local_page_pool, page_pool);
1039
1040 /*
1041 * First reserve all required memory for the new page table entries in
1042 * the recipient page tables without committing, to make sure the entire
1043 * operation will succeed without exhausting the page pool.
1044 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001045 if (!ffa_region_group_identity_map(
1046 to_locked, fragments, fragment_constituent_counts,
1047 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001048 /* TODO: partial defrag of failed range. */
1049 dlog_verbose(
1050 "Insufficient memory to update recipient page "
1051 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 goto out;
1054 }
1055
1056 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001057 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001058 fragments, fragment_constituent_counts,
1059 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001060 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001061 goto out;
1062 }
1063
Jose Marinho09b1db82019-08-08 09:16:59 +01001064 /*
1065 * Complete the transfer by mapping the memory into the recipient. This
1066 * won't allocate because the transaction was already prepared above, so
1067 * it doesn't need to use the `local_page_pool`.
1068 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001069 CHECK(ffa_region_group_identity_map(
1070 to_locked, fragments, fragment_constituent_counts,
1071 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001072
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001074
1075out:
1076 mpool_fini(&local_page_pool);
1077
1078 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001079 * Tidy up the page table by reclaiming failed mappings (if there was an
1080 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001081 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001082 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083
1084 return ret;
1085}
1086
Andrew Walbran290b0c92020-02-03 16:37:14 +00001087/**
1088 * Reclaims the given memory from the TEE. To do this space is first reserved in
1089 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1090 * then (if that is successful) the memory is mapped back into the <to> VM's
1091 * page table.
1092 *
1093 * This function requires the calling context to hold the <to> lock.
1094 *
1095 * Returns:
1096 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001097 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001098 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001099 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001100 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001101 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001102 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001103static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001104 struct vm_locked to_locked, ffa_memory_handle_t handle,
1105 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001106 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1107 struct mpool *page_pool)
1108{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001109 uint32_t to_mode;
1110 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 struct ffa_value ret;
1112 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001113
1114 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001115 * Make sure constituents are properly aligned to a 64-bit boundary. If
1116 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001117 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001118 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001119 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001121 }
1122
1123 /*
1124 * Check if the state transition is lawful for the recipient, and ensure
1125 * that all constituents of the memory region being retrieved are at the
1126 * same state.
1127 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001129 &constituents, &constituent_count,
1130 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001131 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001132 dlog_verbose("Invalid transition.\n");
1133 return ret;
1134 }
1135
1136 /*
1137 * Create a local pool so any freed memory can't be used by another
1138 * thread. This is to ensure the original mapping can be restored if the
1139 * clear fails.
1140 */
1141 mpool_init_with_fallback(&local_page_pool, page_pool);
1142
1143 /*
1144 * First reserve all required memory for the new page table entries in
1145 * the recipient page tables without committing, to make sure the entire
1146 * operation will succeed without exhausting the page pool.
1147 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 if (!ffa_region_group_identity_map(to_locked, &constituents,
1149 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001151 /* TODO: partial defrag of failed range. */
1152 dlog_verbose(
1153 "Insufficient memory to update recipient page "
1154 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001156 goto out;
1157 }
1158
1159 /*
1160 * Forward the request to the TEE and see what happens.
1161 */
1162 tee_flags = 0;
1163 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001165 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001166 ret = arch_other_world_call(
1167 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1168 .arg1 = (uint32_t)handle,
1169 .arg2 = (uint32_t)(handle >> 32),
1170 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001171
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001173 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001174 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1175 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001176 ret.func, ret.arg2);
1177 goto out;
1178 }
1179
1180 /*
1181 * The TEE was happy with it, so complete the reclaim by mapping the
1182 * memory into the recipient. This won't allocate because the
1183 * transaction was already prepared above, so it doesn't need to use the
1184 * `local_page_pool`.
1185 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001186 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1187 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001189
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001191
1192out:
1193 mpool_fini(&local_page_pool);
1194
1195 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001196 * Tidy up the page table by reclaiming failed mappings (if there was an
1197 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001198 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001199 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001200
1201 return ret;
1202}
1203
Andrew Walbran996d1d12020-05-27 14:08:43 +01001204static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001205 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001206 struct ffa_memory_region_constituent **fragments,
1207 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1208 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001209{
1210 uint32_t orig_from_mode;
1211 uint32_t from_mode;
1212 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001213 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001214
Andrew Walbranca808b12020-05-15 17:22:28 +01001215 ret = ffa_relinquish_check_transition(
1216 from_locked, &orig_from_mode, fragments,
1217 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001218 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001219 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001220 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001221 }
1222
1223 /*
1224 * Create a local pool so any freed memory can't be used by another
1225 * thread. This is to ensure the original mapping can be restored if the
1226 * clear fails.
1227 */
1228 mpool_init_with_fallback(&local_page_pool, page_pool);
1229
1230 /*
1231 * First reserve all required memory for the new page table entries
1232 * without committing, to make sure the entire operation will succeed
1233 * without exhausting the page pool.
1234 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001235 if (!ffa_region_group_identity_map(
1236 from_locked, fragments, fragment_constituent_counts,
1237 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001238 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240 goto out;
1241 }
1242
1243 /*
1244 * Update the mapping for the sender. This won't allocate because the
1245 * transaction was already prepared above, but may free pages in the
1246 * case that a whole block is being unmapped that was previously
1247 * partially mapped.
1248 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001249 CHECK(ffa_region_group_identity_map(
1250 from_locked, fragments, fragment_constituent_counts,
1251 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252
1253 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001255 fragments, fragment_constituent_counts,
1256 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001257 /*
1258 * On failure, roll back by returning memory to the sender. This
1259 * may allocate pages which were previously freed into
1260 * `local_page_pool` by the call above, but will never allocate
1261 * more pages than that so can never fail.
1262 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001263 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001264 from_locked, fragments, fragment_constituent_counts,
1265 fragment_count, orig_from_mode, &local_page_pool,
1266 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001268 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001269 goto out;
1270 }
1271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001273
1274out:
1275 mpool_fini(&local_page_pool);
1276
1277 /*
1278 * Tidy up the page table by reclaiming failed mappings (if there was an
1279 * error) or merging entries into blocks where possible (on success).
1280 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001281 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001282
1283 return ret;
1284}
1285
1286/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001287 * Complete a memory sending operation by checking that it is valid, updating
1288 * the sender page table, and then either marking the share state as having
1289 * completed sending (on success) or freeing it (on failure).
1290 *
1291 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1292 */
1293static struct ffa_value ffa_memory_send_complete(
1294 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001295 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1296 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001297{
1298 struct ffa_memory_region *memory_region = share_state->memory_region;
1299 struct ffa_value ret;
1300
1301 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001302 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001303
1304 /* Check that state is valid in sender page table and update. */
1305 ret = ffa_send_check_update(
1306 from_locked, share_state->fragments,
1307 share_state->fragment_constituent_counts,
1308 share_state->fragment_count, share_state->share_func,
1309 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001310 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1311 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001312 if (ret.func != FFA_SUCCESS_32) {
1313 /*
1314 * Free share state, it failed to send so it can't be retrieved.
1315 */
1316 dlog_verbose("Complete failed, freeing share state.\n");
1317 share_state_free(share_states, share_state, page_pool);
1318 return ret;
1319 }
1320
1321 share_state->sending_complete = true;
1322 dlog_verbose("Marked sending complete.\n");
1323
J-Alvesee68c542020-10-29 17:48:20 +00001324 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001325}
1326
1327/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 * Check that the given `memory_region` represents a valid memory send request
1329 * of the given `share_func` type, return the clear flag and permissions via the
1330 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001331 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001333 * not.
1334 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001336 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1337 uint32_t memory_share_length, uint32_t fragment_length,
1338 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001339{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001341 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001342 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001343 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001344 enum ffa_data_access data_access;
1345 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001346
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001347 assert(permissions != NULL);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001348
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001349 /*
1350 * This should already be checked by the caller, just making the
1351 * assumption clear here.
1352 */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001353 assert(memory_region->receiver_count == 1);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001354
Andrew Walbrana65a1322020-04-06 19:32:32 +01001355 /* The sender must match the message sender. */
1356 if (memory_region->sender != from_locked.vm->id) {
1357 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001358 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001359 }
1360
Andrew Walbrana65a1322020-04-06 19:32:32 +01001361 /*
1362 * Ensure that the composite header is within the memory bounds and
1363 * doesn't overlap the first part of the message.
1364 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001365 receivers_length = sizeof(struct ffa_memory_access) *
1366 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001367 constituents_offset =
1368 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001369 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001370 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001371 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001372 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001373 "Invalid composite memory region descriptor offset "
1374 "%d.\n",
1375 memory_region->receivers[0]
1376 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001377 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001378 }
1379
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001380 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001381
1382 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001383 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001385 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1386 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001387 if (memory_share_length != constituents_offset + constituents_length) {
1388 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001389 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 memory_region->receivers[0]
1391 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001392 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001393 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001394 if (fragment_length < memory_share_length &&
1395 fragment_length < HF_MAILBOX_SIZE) {
1396 dlog_warning(
1397 "Initial fragment length %d smaller than mailbox "
1398 "size.\n",
1399 fragment_length);
1400 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001401
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 /*
1403 * Clear is not allowed for memory sharing, as the sender still has
1404 * access to the memory.
1405 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001406 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1407 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001408 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 }
1411
1412 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001413 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001416 }
1417
1418 /* Check that the permissions are valid. */
1419 *permissions =
1420 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 data_access = ffa_get_data_access_attr(*permissions);
1422 instruction_access = ffa_get_instruction_access_attr(*permissions);
1423 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1424 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1426 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 dlog_verbose(
1431 "Invalid instruction access permissions %#x for "
1432 "sending memory.\n",
1433 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001434 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 if (share_func == FFA_MEM_SHARE_32) {
1437 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001438 dlog_verbose(
1439 "Invalid data access permissions %#x for "
1440 "sharing memory.\n",
1441 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001442 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001443 }
1444 /*
Andrew Walbrandd8248f2020-06-22 13:39:30 +01001445 * According to section 5.11.3 of the FF-A 1.0 spec NX is
1446 * required for share operations (but must not be specified by
1447 * the sender) so set it in the copy that we store, ready to be
Andrew Walbrana65a1322020-04-06 19:32:32 +01001448 * returned to the retriever.
1449 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450 ffa_set_instruction_access_attr(permissions,
1451 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 memory_region->receivers[0].receiver_permissions.permissions =
1453 *permissions;
1454 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 if (share_func == FFA_MEM_LEND_32 &&
1456 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 dlog_verbose(
1458 "Invalid data access permissions %#x for lending "
1459 "memory.\n",
1460 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001462 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463 if (share_func == FFA_MEM_DONATE_32 &&
1464 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001465 dlog_verbose(
1466 "Invalid data access permissions %#x for donating "
1467 "memory.\n",
1468 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001470 }
1471
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001473}
1474
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001475/** Forwards a memory send message on to the TEE. */
1476static struct ffa_value memory_send_tee_forward(
1477 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1478 uint32_t share_func, struct ffa_memory_region *memory_region,
1479 uint32_t memory_share_length, uint32_t fragment_length)
1480{
1481 struct ffa_value ret;
1482
1483 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1484 memory_region, fragment_length);
1485 tee_locked.vm->mailbox.recv_size = fragment_length;
1486 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1487 tee_locked.vm->mailbox.recv_func = share_func;
1488 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001489 ret = arch_other_world_call(
1490 (struct ffa_value){.func = share_func,
1491 .arg1 = memory_share_length,
1492 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001493 /*
1494 * After the call to the TEE completes it must have finished reading its
1495 * RX buffer, so it is ready for another message.
1496 */
1497 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1498
1499 return ret;
1500}
1501
Andrew Walbrana65a1322020-04-06 19:32:32 +01001502/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001503 * Gets the share state for continuing an operation to donate, lend or share
1504 * memory, and checks that it is a valid request.
1505 *
1506 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1507 * not.
1508 */
1509static struct ffa_value ffa_memory_send_continue_validate(
1510 struct share_states_locked share_states, ffa_memory_handle_t handle,
1511 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1512 struct mpool *page_pool)
1513{
1514 struct ffa_memory_share_state *share_state;
1515 struct ffa_memory_region *memory_region;
1516
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001517 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001518
1519 /*
1520 * Look up the share state by handle and make sure that the VM ID
1521 * matches.
1522 */
1523 if (!get_share_state(share_states, handle, &share_state)) {
1524 dlog_verbose(
1525 "Invalid handle %#x for memory send continuation.\n",
1526 handle);
1527 return ffa_error(FFA_INVALID_PARAMETERS);
1528 }
1529 memory_region = share_state->memory_region;
1530
1531 if (memory_region->sender != from_vm_id) {
1532 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1533 return ffa_error(FFA_INVALID_PARAMETERS);
1534 }
1535
1536 if (share_state->sending_complete) {
1537 dlog_verbose(
1538 "Sending of memory handle %#x is already complete.\n",
1539 handle);
1540 return ffa_error(FFA_INVALID_PARAMETERS);
1541 }
1542
1543 if (share_state->fragment_count == MAX_FRAGMENTS) {
1544 /*
1545 * Log a warning as this is a sign that MAX_FRAGMENTS should
1546 * probably be increased.
1547 */
1548 dlog_warning(
1549 "Too many fragments for memory share with handle %#x; "
1550 "only %d supported.\n",
1551 handle, MAX_FRAGMENTS);
1552 /* Free share state, as it's not possible to complete it. */
1553 share_state_free(share_states, share_state, page_pool);
1554 return ffa_error(FFA_NO_MEMORY);
1555 }
1556
1557 *share_state_ret = share_state;
1558
1559 return (struct ffa_value){.func = FFA_SUCCESS_32};
1560}
1561
1562/**
1563 * Forwards a memory send continuation message on to the TEE.
1564 */
1565static struct ffa_value memory_send_continue_tee_forward(
1566 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1567 uint32_t fragment_length, ffa_memory_handle_t handle)
1568{
1569 struct ffa_value ret;
1570
1571 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1572 fragment_length);
1573 tee_locked.vm->mailbox.recv_size = fragment_length;
1574 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1575 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1576 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001577 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001578 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1579 .arg1 = (uint32_t)handle,
1580 .arg2 = (uint32_t)(handle >> 32),
1581 .arg3 = fragment_length,
1582 .arg4 = (uint64_t)sender_vm_id << 16});
1583 /*
1584 * After the call to the TEE completes it must have finished reading its
1585 * RX buffer, so it is ready for another message.
1586 */
1587 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1588
1589 return ret;
1590}
1591
1592/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001593 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1594 * updates the stage-2 page tables. Specifically, check if the message length
1595 * and number of memory region constituents match, and if the transition is
1596 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001597 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001598 * Assumes that the caller has already found and locked the sender VM and copied
1599 * the memory region descriptor from the sender's TX buffer to a freshly
1600 * allocated page from Hafnium's internal pool. The caller must have also
1601 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001602 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001603 * This function takes ownership of the `memory_region` passed in and will free
1604 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001605 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001606struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001608 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001609 uint32_t fragment_length, uint32_t share_func,
1610 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001611{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001612 ffa_memory_access_permissions_t permissions;
1613 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001614 struct share_states_locked share_states;
1615 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001616
1617 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001618 * If there is an error validating the `memory_region` then we need to
1619 * free it because we own it but we won't be storing it in a share state
1620 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001621 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001622 ret = ffa_memory_send_validate(from_locked, memory_region,
1623 memory_share_length, fragment_length,
1624 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001625 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001626 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001627 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001628 }
1629
Andrew Walbrana65a1322020-04-06 19:32:32 +01001630 /* Set flag for share function, ready to be retrieved later. */
1631 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001632 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001633 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001634 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001635 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 case FFA_MEM_LEND_32:
1637 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001638 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001639 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001640 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001641 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001642 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001643 }
1644
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001646 /*
1647 * Allocate a share state before updating the page table. Otherwise if
1648 * updating the page table succeeded but allocating the share state
1649 * failed then it would leave the memory in a state where nobody could
1650 * get it back.
1651 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001652 if (!allocate_share_state(share_states, share_func, memory_region,
1653 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1654 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 dlog_verbose("Failed to allocate share state.\n");
1656 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001657 ret = ffa_error(FFA_NO_MEMORY);
1658 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001659 }
1660
Andrew Walbranca808b12020-05-15 17:22:28 +01001661 if (fragment_length == memory_share_length) {
1662 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001663 ret = ffa_memory_send_complete(
1664 from_locked, share_states, share_state, page_pool,
1665 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001666 } else {
1667 ret = (struct ffa_value){
1668 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001669 .arg1 = (uint32_t)memory_region->handle,
1670 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001671 .arg3 = fragment_length};
1672 }
1673
1674out:
1675 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001676 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001677 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001678}
1679
1680/**
1681 * Validates a call to donate, lend or share memory to the TEE and then updates
1682 * the stage-2 page tables. Specifically, check if the message length and number
1683 * of memory region constituents match, and if the transition is valid for the
1684 * type of memory sending operation.
1685 *
1686 * Assumes that the caller has already found and locked the sender VM and the
1687 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1688 * to a freshly allocated page from Hafnium's internal pool. The caller must
1689 * have also validated that the receiver VM ID is valid.
1690 *
1691 * This function takes ownership of the `memory_region` passed in and will free
1692 * it when necessary; it must not be freed by the caller.
1693 */
1694struct ffa_value ffa_memory_tee_send(
1695 struct vm_locked from_locked, struct vm_locked to_locked,
1696 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1697 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1698{
1699 ffa_memory_access_permissions_t permissions;
1700 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001701
1702 /*
1703 * If there is an error validating the `memory_region` then we need to
1704 * free it because we own it but we won't be storing it in a share state
1705 * after all.
1706 */
1707 ret = ffa_memory_send_validate(from_locked, memory_region,
1708 memory_share_length, fragment_length,
1709 share_func, &permissions);
1710 if (ret.func != FFA_SUCCESS_32) {
1711 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001712 }
1713
Andrew Walbranca808b12020-05-15 17:22:28 +01001714 if (fragment_length == memory_share_length) {
1715 /* No more fragments to come, everything fit in one message. */
1716 struct ffa_composite_memory_region *composite =
1717 ffa_memory_region_get_composite(memory_region, 0);
1718 struct ffa_memory_region_constituent *constituents =
1719 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001720 struct mpool local_page_pool;
1721 uint32_t orig_from_mode;
1722
1723 /*
1724 * Use a local page pool so that we can roll back if necessary.
1725 */
1726 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001727
1728 ret = ffa_send_check_update(
1729 from_locked, &constituents,
1730 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001731 permissions, &local_page_pool,
1732 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1733 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001734 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001735 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001736 goto out;
1737 }
1738
1739 /* Forward memory send message on to TEE. */
1740 ret = memory_send_tee_forward(
1741 to_locked, from_locked.vm->id, share_func,
1742 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001743
1744 if (ret.func != FFA_SUCCESS_32) {
1745 dlog_verbose(
1746 "TEE didn't successfully complete memory send "
1747 "operation; returned %#x (%d). Rolling back.\n",
1748 ret.func, ret.arg2);
1749
1750 /*
1751 * The TEE failed to complete the send operation, so
1752 * roll back the page table update for the VM. This
1753 * can't fail because it won't try to allocate more
1754 * memory than was freed into the `local_page_pool` by
1755 * `ffa_send_check_update` in the initial update.
1756 */
1757 CHECK(ffa_region_group_identity_map(
1758 from_locked, &constituents,
1759 &composite->constituent_count, 1,
1760 orig_from_mode, &local_page_pool, true));
1761 }
1762
1763 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001764 } else {
1765 struct share_states_locked share_states = share_states_lock();
1766 ffa_memory_handle_t handle;
1767
1768 /*
1769 * We need to wait for the rest of the fragments before we can
1770 * check whether the transaction is valid and unmap the memory.
1771 * Call the TEE so it can do its initial validation and assign a
1772 * handle, and allocate a share state to keep what we have so
1773 * far.
1774 */
1775 ret = memory_send_tee_forward(
1776 to_locked, from_locked.vm->id, share_func,
1777 memory_region, memory_share_length, fragment_length);
1778 if (ret.func == FFA_ERROR_32) {
1779 goto out_unlock;
1780 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1781 dlog_warning(
1782 "Got %#x from TEE in response to %#x for "
1783 "fragment with with %d/%d, expected "
1784 "FFA_MEM_FRAG_RX.\n",
1785 ret.func, share_func, fragment_length,
1786 memory_share_length);
1787 ret = ffa_error(FFA_INVALID_PARAMETERS);
1788 goto out_unlock;
1789 }
1790 handle = ffa_frag_handle(ret);
1791 if (ret.arg3 != fragment_length) {
1792 dlog_warning(
1793 "Got unexpected fragment offset %d for "
1794 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1795 ret.arg3, fragment_length);
1796 ret = ffa_error(FFA_INVALID_PARAMETERS);
1797 goto out_unlock;
1798 }
1799 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1800 dlog_warning(
1801 "Got unexpected sender ID %d for "
1802 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1803 ffa_frag_sender(ret), from_locked.vm->id);
1804 ret = ffa_error(FFA_INVALID_PARAMETERS);
1805 goto out_unlock;
1806 }
1807
1808 if (!allocate_share_state(share_states, share_func,
1809 memory_region, fragment_length,
1810 handle, NULL)) {
1811 dlog_verbose("Failed to allocate share state.\n");
1812 ret = ffa_error(FFA_NO_MEMORY);
1813 goto out_unlock;
1814 }
1815 /*
1816 * Don't free the memory region fragment, as it has been stored
1817 * in the share state.
1818 */
1819 memory_region = NULL;
1820 out_unlock:
1821 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001822 }
1823
Andrew Walbranca808b12020-05-15 17:22:28 +01001824out:
1825 if (memory_region != NULL) {
1826 mpool_free(page_pool, memory_region);
1827 }
1828 dump_share_states();
1829 return ret;
1830}
1831
1832/**
1833 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1834 * this is the last fragment then checks that the transition is valid for the
1835 * type of memory sending operation and updates the stage-2 page tables of the
1836 * sender.
1837 *
1838 * Assumes that the caller has already found and locked the sender VM and copied
1839 * the memory region descriptor from the sender's TX buffer to a freshly
1840 * allocated page from Hafnium's internal pool.
1841 *
1842 * This function takes ownership of the `fragment` passed in; it must not be
1843 * freed by the caller.
1844 */
1845struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1846 void *fragment,
1847 uint32_t fragment_length,
1848 ffa_memory_handle_t handle,
1849 struct mpool *page_pool)
1850{
1851 struct share_states_locked share_states = share_states_lock();
1852 struct ffa_memory_share_state *share_state;
1853 struct ffa_value ret;
1854 struct ffa_memory_region *memory_region;
1855
1856 ret = ffa_memory_send_continue_validate(share_states, handle,
1857 &share_state,
1858 from_locked.vm->id, page_pool);
1859 if (ret.func != FFA_SUCCESS_32) {
1860 goto out_free_fragment;
1861 }
1862 memory_region = share_state->memory_region;
1863
1864 if (memory_region->receivers[0].receiver_permissions.receiver ==
1865 HF_TEE_VM_ID) {
1866 dlog_error(
1867 "Got hypervisor-allocated handle for memory send to "
1868 "TEE. This should never happen, and indicates a bug in "
1869 "EL3 code.\n");
1870 ret = ffa_error(FFA_INVALID_PARAMETERS);
1871 goto out_free_fragment;
1872 }
1873
1874 /* Add this fragment. */
1875 share_state->fragments[share_state->fragment_count] = fragment;
1876 share_state->fragment_constituent_counts[share_state->fragment_count] =
1877 fragment_length / sizeof(struct ffa_memory_region_constituent);
1878 share_state->fragment_count++;
1879
1880 /* Check whether the memory send operation is now ready to complete. */
1881 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001882 ret = ffa_memory_send_complete(
1883 from_locked, share_states, share_state, page_pool,
1884 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001885 } else {
1886 ret = (struct ffa_value){
1887 .func = FFA_MEM_FRAG_RX_32,
1888 .arg1 = (uint32_t)handle,
1889 .arg2 = (uint32_t)(handle >> 32),
1890 .arg3 = share_state_next_fragment_offset(share_states,
1891 share_state)};
1892 }
1893 goto out;
1894
1895out_free_fragment:
1896 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001897
1898out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001899 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001900 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001901}
1902
Andrew Walbranca808b12020-05-15 17:22:28 +01001903/**
1904 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1905 * is the last fragment then checks that the transition is valid for the type of
1906 * memory sending operation and updates the stage-2 page tables of the sender.
1907 *
1908 * Assumes that the caller has already found and locked the sender VM and copied
1909 * the memory region descriptor from the sender's TX buffer to a freshly
1910 * allocated page from Hafnium's internal pool.
1911 *
1912 * This function takes ownership of the `memory_region` passed in and will free
1913 * it when necessary; it must not be freed by the caller.
1914 */
1915struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1916 struct vm_locked to_locked,
1917 void *fragment,
1918 uint32_t fragment_length,
1919 ffa_memory_handle_t handle,
1920 struct mpool *page_pool)
1921{
1922 struct share_states_locked share_states = share_states_lock();
1923 struct ffa_memory_share_state *share_state;
1924 struct ffa_value ret;
1925 struct ffa_memory_region *memory_region;
1926
1927 ret = ffa_memory_send_continue_validate(share_states, handle,
1928 &share_state,
1929 from_locked.vm->id, page_pool);
1930 if (ret.func != FFA_SUCCESS_32) {
1931 goto out_free_fragment;
1932 }
1933 memory_region = share_state->memory_region;
1934
1935 if (memory_region->receivers[0].receiver_permissions.receiver !=
1936 HF_TEE_VM_ID) {
1937 dlog_error(
1938 "Got SPM-allocated handle for memory send to non-TEE "
1939 "VM. This should never happen, and indicates a bug.\n");
1940 ret = ffa_error(FFA_INVALID_PARAMETERS);
1941 goto out_free_fragment;
1942 }
1943
1944 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1945 to_locked.vm->mailbox.recv == NULL) {
1946 /*
1947 * If the TEE RX buffer is not available, tell the sender to
1948 * retry by returning the current offset again.
1949 */
1950 ret = (struct ffa_value){
1951 .func = FFA_MEM_FRAG_RX_32,
1952 .arg1 = (uint32_t)handle,
1953 .arg2 = (uint32_t)(handle >> 32),
1954 .arg3 = share_state_next_fragment_offset(share_states,
1955 share_state),
1956 };
1957 goto out_free_fragment;
1958 }
1959
1960 /* Add this fragment. */
1961 share_state->fragments[share_state->fragment_count] = fragment;
1962 share_state->fragment_constituent_counts[share_state->fragment_count] =
1963 fragment_length / sizeof(struct ffa_memory_region_constituent);
1964 share_state->fragment_count++;
1965
1966 /* Check whether the memory send operation is now ready to complete. */
1967 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001968 struct mpool local_page_pool;
1969 uint32_t orig_from_mode;
1970
1971 /*
1972 * Use a local page pool so that we can roll back if necessary.
1973 */
1974 mpool_init_with_fallback(&local_page_pool, page_pool);
1975
Andrew Walbranca808b12020-05-15 17:22:28 +01001976 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001977 share_state, &local_page_pool,
1978 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001979
1980 if (ret.func == FFA_SUCCESS_32) {
1981 /*
1982 * Forward final fragment on to the TEE so that
1983 * it can complete the memory sending operation.
1984 */
1985 ret = memory_send_continue_tee_forward(
1986 to_locked, from_locked.vm->id, fragment,
1987 fragment_length, handle);
1988
1989 if (ret.func != FFA_SUCCESS_32) {
1990 /*
1991 * The error will be passed on to the caller,
1992 * but log it here too.
1993 */
1994 dlog_verbose(
1995 "TEE didn't successfully complete "
1996 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001997 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001998 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001999
2000 /*
2001 * The TEE failed to complete the send
2002 * operation, so roll back the page table update
2003 * for the VM. This can't fail because it won't
2004 * try to allocate more memory than was freed
2005 * into the `local_page_pool` by
2006 * `ffa_send_check_update` in the initial
2007 * update.
2008 */
2009 CHECK(ffa_region_group_identity_map(
2010 from_locked, share_state->fragments,
2011 share_state
2012 ->fragment_constituent_counts,
2013 share_state->fragment_count,
2014 orig_from_mode, &local_page_pool,
2015 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002016 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002017
Andrew Walbranca808b12020-05-15 17:22:28 +01002018 /* Free share state. */
2019 share_state_free(share_states, share_state, page_pool);
2020 } else {
2021 /* Abort sending to TEE. */
2022 struct ffa_value tee_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002023 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002024 .func = FFA_MEM_RECLAIM_32,
2025 .arg1 = (uint32_t)handle,
2026 .arg2 = (uint32_t)(handle >> 32)});
2027
2028 if (tee_ret.func != FFA_SUCCESS_32) {
2029 /*
2030 * Nothing we can do if TEE doesn't abort
2031 * properly, just log it.
2032 */
2033 dlog_verbose(
2034 "TEE didn't successfully abort failed "
2035 "memory send operation; returned %#x "
2036 "(%d).\n",
2037 tee_ret.func, tee_ret.arg2);
2038 }
2039 /*
2040 * We don't need to free the share state in this case
2041 * because ffa_memory_send_complete does that already.
2042 */
2043 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002044
2045 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002046 } else {
2047 uint32_t next_fragment_offset =
2048 share_state_next_fragment_offset(share_states,
2049 share_state);
2050
2051 ret = memory_send_continue_tee_forward(
2052 to_locked, from_locked.vm->id, fragment,
2053 fragment_length, handle);
2054
2055 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2056 ffa_frag_handle(ret) != handle ||
2057 ret.arg3 != next_fragment_offset ||
2058 ffa_frag_sender(ret) != from_locked.vm->id) {
2059 dlog_verbose(
2060 "Got unexpected result from forwarding "
2061 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2062 "offset %d, sender %d); expected "
2063 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2064 "sender %d).\n",
2065 ret.func, ffa_frag_handle(ret), ret.arg3,
2066 ffa_frag_sender(ret), handle,
2067 next_fragment_offset, from_locked.vm->id);
2068 /* Free share state. */
2069 share_state_free(share_states, share_state, page_pool);
2070 ret = ffa_error(FFA_INVALID_PARAMETERS);
2071 goto out;
2072 }
2073
2074 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2075 .arg1 = (uint32_t)handle,
2076 .arg2 = (uint32_t)(handle >> 32),
2077 .arg3 = next_fragment_offset};
2078 }
2079 goto out;
2080
2081out_free_fragment:
2082 mpool_free(page_pool, fragment);
2083
2084out:
2085 share_states_unlock(&share_states);
2086 return ret;
2087}
2088
2089/** Clean up after the receiver has finished retrieving a memory region. */
2090static void ffa_memory_retrieve_complete(
2091 struct share_states_locked share_states,
2092 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2093{
2094 if (share_state->share_func == FFA_MEM_DONATE_32) {
2095 /*
2096 * Memory that has been donated can't be relinquished,
2097 * so no need to keep the share state around.
2098 */
2099 share_state_free(share_states, share_state, page_pool);
2100 dlog_verbose("Freed share state for donate.\n");
2101 }
2102}
2103
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002104struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2105 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002106 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002107 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002108{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002109 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002110 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002111 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002112 sizeof(struct ffa_memory_access);
2113 ffa_memory_handle_t handle = retrieve_request->handle;
2114 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002115 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002116 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2117 struct ffa_memory_region *memory_region;
2118 ffa_memory_access_permissions_t sent_permissions;
2119 enum ffa_data_access sent_data_access;
2120 enum ffa_instruction_access sent_instruction_access;
2121 ffa_memory_access_permissions_t requested_permissions;
2122 enum ffa_data_access requested_data_access;
2123 enum ffa_instruction_access requested_instruction_access;
2124 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002125 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002126 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002127 struct ffa_memory_share_state *share_state;
2128 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002129 struct ffa_composite_memory_region *composite;
2130 uint32_t total_length;
2131 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002132
2133 dump_share_states();
2134
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002135 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002136 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002137 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002138 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002139 expected_retrieve_request_length,
2140 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002141 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002142 }
2143
Andrew Walbrana65a1322020-04-06 19:32:32 +01002144 if (retrieve_request->receiver_count != 1) {
2145 dlog_verbose(
2146 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002147 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002148 "expected 1).\n",
2149 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002151 }
2152
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002153 share_states = share_states_lock();
2154 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002155 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002156 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002157 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002158 goto out;
2159 }
2160
Andrew Walbrana65a1322020-04-06 19:32:32 +01002161 memory_region = share_state->memory_region;
2162 CHECK(memory_region != NULL);
2163
2164 /*
2165 * Check that the transaction type expected by the receiver is correct,
2166 * if it has been specified.
2167 */
2168 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002169 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002170 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002171 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002172 dlog_verbose(
2173 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002174 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002175 transaction_type,
2176 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002177 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002178 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002180 goto out;
2181 }
2182
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002183 if (retrieve_request->sender != memory_region->sender) {
2184 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002185 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002186 "expected %d for handle %#x.\n",
2187 retrieve_request->sender, memory_region->sender,
2188 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002189 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002190 goto out;
2191 }
2192
2193 if (retrieve_request->tag != memory_region->tag) {
2194 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002195 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002196 "%d for handle %#x.\n",
2197 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002198 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002199 goto out;
2200 }
2201
Andrew Walbrana65a1322020-04-06 19:32:32 +01002202 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2203 to_locked.vm->id) {
2204 dlog_verbose(
2205 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002207 retrieve_request->receivers[0]
2208 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002209 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002210 goto out;
2211 }
2212
2213 if (memory_region->receivers[0].receiver_permissions.receiver !=
2214 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002215 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002216 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2217 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002218 to_locked.vm->id,
2219 memory_region->receivers[0]
2220 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002222 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002223 goto out;
2224 }
2225
Andrew Walbranca808b12020-05-15 17:22:28 +01002226 if (!share_state->sending_complete) {
2227 dlog_verbose(
2228 "Memory with handle %#x not fully sent, can't "
2229 "retrieve.\n",
2230 handle);
2231 ret = ffa_error(FFA_INVALID_PARAMETERS);
2232 goto out;
2233 }
2234
2235 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002236 dlog_verbose("Memory with handle %#x already retrieved.\n",
2237 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002238 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002239 goto out;
2240 }
2241
Andrew Walbrana65a1322020-04-06 19:32:32 +01002242 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2243 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002244 dlog_verbose(
2245 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002246 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002247 retrieve_request->receivers[0]
2248 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002249 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002250 goto out;
2251 }
2252
J-Alves84658fc2021-06-17 14:37:32 +01002253 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2254 dlog_verbose(
2255 "Bits 31-10 must be zero in memory region's flags.\n");
2256 ret = ffa_error(FFA_INVALID_PARAMETERS);
2257 goto out;
2258 }
2259
2260 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2261 (retrieve_request->flags &
2262 (FFA_MEMORY_REGION_FLAG_CLEAR |
2263 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2264 dlog_verbose(
2265 "Memory Share operation can't clean after relinquish "
2266 "memory region.\n");
2267 ret = ffa_error(FFA_INVALID_PARAMETERS);
2268 goto out;
2269 }
2270
Andrew Walbrana65a1322020-04-06 19:32:32 +01002271 /*
J-Alves17c069c2021-12-07 16:00:38 +00002272 * If the borrower needs the memory to be cleared before mapping to its
2273 * address space, the sender should have set the flag when calling
2274 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2275 */
2276 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2277 (share_state->memory_region->flags &
2278 FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2279 dlog_verbose(
2280 "Borrower needs memory cleared. Sender needs to set "
2281 "flag for clearing memory.\n");
2282 ret = ffa_error(FFA_DENIED);
2283 goto out;
2284 }
2285
2286 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002287 * Check permissions from sender against permissions requested by
2288 * receiver.
2289 */
2290 /* TODO: Check attributes too. */
2291 sent_permissions =
2292 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002293 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002294 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002295 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002296 requested_permissions =
2297 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002298 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002299 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002300 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002301 permissions = 0;
J-Alves84658fc2021-06-17 14:37:32 +01002302
2303 if ((sent_data_access == FFA_DATA_ACCESS_RO ||
2304 requested_permissions == FFA_DATA_ACCESS_RO) &&
2305 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U) {
2306 dlog_verbose(
2307 "Receiver has RO permissions can not request clear.\n");
2308 ret = ffa_error(FFA_DENIED);
2309 goto out;
2310 }
2311
Andrew Walbrana65a1322020-04-06 19:32:32 +01002312 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002313 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2314 case FFA_DATA_ACCESS_RW:
2315 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2316 requested_data_access == FFA_DATA_ACCESS_RW) {
2317 ffa_set_data_access_attr(&permissions,
2318 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002319 break;
2320 }
2321 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002322 case FFA_DATA_ACCESS_RO:
2323 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2324 requested_data_access == FFA_DATA_ACCESS_RO) {
2325 ffa_set_data_access_attr(&permissions,
2326 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002327 break;
2328 }
2329 dlog_verbose(
2330 "Invalid data access requested; sender specified "
2331 "permissions %#x but receiver requested %#x.\n",
2332 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002333 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002334 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002335 case FFA_DATA_ACCESS_RESERVED:
2336 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002337 "checked before this point.");
2338 }
2339 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002340 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2341 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002342 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002343 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2344 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2345 ffa_set_instruction_access_attr(
2346 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002347 break;
2348 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002349 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002350 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002351 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2352 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2353 ffa_set_instruction_access_attr(
2354 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002355 break;
2356 }
2357 dlog_verbose(
2358 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002359 "specified permissions %#x but receiver requested "
2360 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002361 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002362 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002363 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002364 case FFA_INSTRUCTION_ACCESS_RESERVED:
2365 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002366 "be checked before this point.");
2367 }
J-Alves7cd5eb32020-10-16 19:06:10 +01002368 memory_to_attributes = ffa_memory_permissions_to_mode(
2369 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002370 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002371 to_locked, share_state->fragments,
2372 share_state->fragment_constituent_counts,
2373 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002374 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002375 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002376 goto out;
2377 }
2378
2379 /*
2380 * Copy response to RX buffer of caller and deliver the message. This
2381 * must be done before the share_state is (possibly) freed.
2382 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002383 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002384 composite = ffa_memory_region_get_composite(memory_region, 0);
2385 /*
2386 * Constituents which we received in the first fragment should always
2387 * fit in the first fragment we are sending, because the header is the
2388 * same size in both cases and we have a fixed message buffer size. So
2389 * `ffa_retrieved_memory_region_init` should never fail.
2390 */
2391 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002392 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2393 memory_region->sender, memory_region->attributes,
2394 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002395 composite->page_count, composite->constituent_count,
2396 share_state->fragments[0],
2397 share_state->fragment_constituent_counts[0], &total_length,
2398 &fragment_length));
2399 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002400 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002401 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002402 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2403
Andrew Walbranca808b12020-05-15 17:22:28 +01002404 share_state->retrieved_fragment_count[0] = 1;
2405 if (share_state->retrieved_fragment_count[0] ==
2406 share_state->fragment_count) {
2407 ffa_memory_retrieve_complete(share_states, share_state,
2408 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002409 }
2410
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002411 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002412 .arg1 = total_length,
2413 .arg2 = fragment_length};
2414
2415out:
2416 share_states_unlock(&share_states);
2417 dump_share_states();
2418 return ret;
2419}
2420
2421struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2422 ffa_memory_handle_t handle,
2423 uint32_t fragment_offset,
2424 struct mpool *page_pool)
2425{
2426 struct ffa_memory_region *memory_region;
2427 struct share_states_locked share_states;
2428 struct ffa_memory_share_state *share_state;
2429 struct ffa_value ret;
2430 uint32_t fragment_index;
2431 uint32_t retrieved_constituents_count;
2432 uint32_t i;
2433 uint32_t expected_fragment_offset;
2434 uint32_t remaining_constituent_count;
2435 uint32_t fragment_length;
2436
2437 dump_share_states();
2438
2439 share_states = share_states_lock();
2440 if (!get_share_state(share_states, handle, &share_state)) {
2441 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2442 handle);
2443 ret = ffa_error(FFA_INVALID_PARAMETERS);
2444 goto out;
2445 }
2446
2447 memory_region = share_state->memory_region;
2448 CHECK(memory_region != NULL);
2449
2450 if (memory_region->receivers[0].receiver_permissions.receiver !=
2451 to_locked.vm->id) {
2452 dlog_verbose(
2453 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2454 "of handle %#x.\n",
2455 to_locked.vm->id,
2456 memory_region->receivers[0]
2457 .receiver_permissions.receiver,
2458 handle);
2459 ret = ffa_error(FFA_INVALID_PARAMETERS);
2460 goto out;
2461 }
2462
2463 if (!share_state->sending_complete) {
2464 dlog_verbose(
2465 "Memory with handle %#x not fully sent, can't "
2466 "retrieve.\n",
2467 handle);
2468 ret = ffa_error(FFA_INVALID_PARAMETERS);
2469 goto out;
2470 }
2471
2472 if (share_state->retrieved_fragment_count[0] == 0 ||
2473 share_state->retrieved_fragment_count[0] >=
2474 share_state->fragment_count) {
2475 dlog_verbose(
2476 "Retrieval of memory with handle %#x not yet started "
2477 "or already completed (%d/%d fragments retrieved).\n",
2478 handle, share_state->retrieved_fragment_count[0],
2479 share_state->fragment_count);
2480 ret = ffa_error(FFA_INVALID_PARAMETERS);
2481 goto out;
2482 }
2483
2484 fragment_index = share_state->retrieved_fragment_count[0];
2485
2486 /*
2487 * Check that the given fragment offset is correct by counting how many
2488 * constituents were in the fragments previously sent.
2489 */
2490 retrieved_constituents_count = 0;
2491 for (i = 0; i < fragment_index; ++i) {
2492 retrieved_constituents_count +=
2493 share_state->fragment_constituent_counts[i];
2494 }
2495 expected_fragment_offset =
2496 ffa_composite_constituent_offset(memory_region, 0) +
2497 retrieved_constituents_count *
2498 sizeof(struct ffa_memory_region_constituent);
2499 if (fragment_offset != expected_fragment_offset) {
2500 dlog_verbose("Fragment offset was %d but expected %d.\n",
2501 fragment_offset, expected_fragment_offset);
2502 ret = ffa_error(FFA_INVALID_PARAMETERS);
2503 goto out;
2504 }
2505
2506 remaining_constituent_count = ffa_memory_fragment_init(
2507 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2508 share_state->fragments[fragment_index],
2509 share_state->fragment_constituent_counts[fragment_index],
2510 &fragment_length);
2511 CHECK(remaining_constituent_count == 0);
2512 to_locked.vm->mailbox.recv_size = fragment_length;
2513 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2514 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2515 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2516 share_state->retrieved_fragment_count[0]++;
2517 if (share_state->retrieved_fragment_count[0] ==
2518 share_state->fragment_count) {
2519 ffa_memory_retrieve_complete(share_states, share_state,
2520 page_pool);
2521 }
2522
2523 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2524 .arg1 = (uint32_t)handle,
2525 .arg2 = (uint32_t)(handle >> 32),
2526 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527
2528out:
2529 share_states_unlock(&share_states);
2530 dump_share_states();
2531 return ret;
2532}
2533
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002534struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002536 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002537{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 struct ffa_memory_share_state *share_state;
2541 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544
Andrew Walbrana65a1322020-04-06 19:32:32 +01002545 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002547 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002548 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002549 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002550 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551 }
2552
Andrew Walbrana65a1322020-04-06 19:32:32 +01002553 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554 dlog_verbose(
2555 "VM ID %d in relinquish message doesn't match calling "
2556 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002557 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002558 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002559 }
2560
2561 dump_share_states();
2562
2563 share_states = share_states_lock();
2564 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002565 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002566 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002567 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002568 goto out;
2569 }
2570
Andrew Walbranca808b12020-05-15 17:22:28 +01002571 if (!share_state->sending_complete) {
2572 dlog_verbose(
2573 "Memory with handle %#x not fully sent, can't "
2574 "relinquish.\n",
2575 handle);
2576 ret = ffa_error(FFA_INVALID_PARAMETERS);
2577 goto out;
2578 }
2579
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002580 memory_region = share_state->memory_region;
2581 CHECK(memory_region != NULL);
2582
Andrew Walbrana65a1322020-04-06 19:32:32 +01002583 if (memory_region->receivers[0].receiver_permissions.receiver !=
2584 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002585 dlog_verbose(
2586 "VM ID %d tried to relinquish memory region with "
2587 "handle %#x but receiver was %d.\n",
2588 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002589 memory_region->receivers[0]
2590 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002591 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002592 goto out;
2593 }
2594
Andrew Walbranca808b12020-05-15 17:22:28 +01002595 if (share_state->retrieved_fragment_count[0] !=
2596 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002597 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002598 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002599 "relinquish.\n",
2600 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002601 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002602 goto out;
2603 }
2604
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002605 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002606
2607 /*
2608 * Clear is not allowed for memory that was shared, as the original
2609 * sender still has access to the memory.
2610 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002611 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002612 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002613 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002614 goto out;
2615 }
2616
Andrew Walbranca808b12020-05-15 17:22:28 +01002617 ret = ffa_relinquish_check_update(
2618 from_locked, share_state->fragments,
2619 share_state->fragment_constituent_counts,
2620 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002621
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002622 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002623 /*
2624 * Mark memory handle as not retrieved, so it can be reclaimed
2625 * (or retrieved again).
2626 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002627 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002628 }
2629
2630out:
2631 share_states_unlock(&share_states);
2632 dump_share_states();
2633 return ret;
2634}
2635
2636/**
2637 * Validates that the reclaim transition is allowed for the given handle,
2638 * updates the page table of the reclaiming VM, and frees the internal state
2639 * associated with the handle.
2640 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002641struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002642 ffa_memory_handle_t handle,
2643 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002644 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002645{
2646 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002647 struct ffa_memory_share_state *share_state;
2648 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002649 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650
2651 dump_share_states();
2652
2653 share_states = share_states_lock();
2654 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002655 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002657 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002658 goto out;
2659 }
2660
2661 memory_region = share_state->memory_region;
2662 CHECK(memory_region != NULL);
2663
2664 if (to_locked.vm->id != memory_region->sender) {
2665 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002666 "VM %#x attempted to reclaim memory handle %#x "
2667 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002668 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002669 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002670 goto out;
2671 }
2672
Andrew Walbranca808b12020-05-15 17:22:28 +01002673 if (!share_state->sending_complete) {
2674 dlog_verbose(
2675 "Memory with handle %#x not fully sent, can't "
2676 "reclaim.\n",
2677 handle);
2678 ret = ffa_error(FFA_INVALID_PARAMETERS);
2679 goto out;
2680 }
2681
2682 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002683 dlog_verbose(
2684 "Tried to reclaim memory handle %#x that has not been "
2685 "relinquished.\n",
2686 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002687 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688 goto out;
2689 }
2690
Andrew Walbranca808b12020-05-15 17:22:28 +01002691 ret = ffa_retrieve_check_update(
2692 to_locked, share_state->fragments,
2693 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002694 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002695 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002696
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002697 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002698 share_state_free(share_states, share_state, page_pool);
2699 dlog_verbose("Freed share state after successful reclaim.\n");
2700 }
2701
2702out:
2703 share_states_unlock(&share_states);
2704 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002705}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002706
2707/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002708 * Validates that the reclaim transition is allowed for the memory region with
2709 * the given handle which was previously shared with the TEE, tells the TEE to
2710 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2711 *
2712 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002713 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002714struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002715 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002716 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002717 ffa_memory_region_flags_t flags,
2718 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002719{
Andrew Walbranca808b12020-05-15 17:22:28 +01002720 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2721 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2722 struct ffa_value tee_ret;
2723 uint32_t length;
2724 uint32_t fragment_length;
2725 uint32_t fragment_offset;
2726 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002727 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002728 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2729
2730 CHECK(request_length <= HF_MAILBOX_SIZE);
2731 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2732
2733 /* Retrieve memory region information from the TEE. */
Olivier Deprez112d2b52020-09-30 07:39:23 +02002734 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002735 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2736 .arg1 = request_length,
2737 .arg2 = request_length});
2738 if (tee_ret.func == FFA_ERROR_32) {
2739 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2740 return tee_ret;
2741 }
2742 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2743 dlog_verbose(
2744 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2745 tee_ret.func);
2746 return ffa_error(FFA_INVALID_PARAMETERS);
2747 }
2748
2749 length = tee_ret.arg1;
2750 fragment_length = tee_ret.arg2;
2751
2752 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2753 length > sizeof(tee_retrieve_buffer)) {
2754 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2755 fragment_length, length, HF_MAILBOX_SIZE,
2756 sizeof(tee_retrieve_buffer));
2757 return ffa_error(FFA_INVALID_PARAMETERS);
2758 }
2759
2760 /*
2761 * Copy the first fragment of the memory region descriptor to an
2762 * internal buffer.
2763 */
2764 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2765 from_locked.vm->mailbox.send, fragment_length);
2766
2767 /* Fetch the remaining fragments into the same buffer. */
2768 fragment_offset = fragment_length;
2769 while (fragment_offset < length) {
Olivier Deprez112d2b52020-09-30 07:39:23 +02002770 tee_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002771 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2772 .arg1 = (uint32_t)handle,
2773 .arg2 = (uint32_t)(handle >> 32),
2774 .arg3 = fragment_offset});
2775 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2776 dlog_verbose(
2777 "Got %#x (%d) from TEE in response to "
2778 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2779 tee_ret.func, tee_ret.arg2);
2780 return tee_ret;
2781 }
2782 if (ffa_frag_handle(tee_ret) != handle) {
2783 dlog_verbose(
2784 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2785 "in response to FFA_MEM_FRAG_RX for handle "
2786 "%#x.\n",
2787 ffa_frag_handle(tee_ret), handle);
2788 return ffa_error(FFA_INVALID_PARAMETERS);
2789 }
2790 if (ffa_frag_sender(tee_ret) != 0) {
2791 dlog_verbose(
2792 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2793 "(expected 0).\n",
2794 ffa_frag_sender(tee_ret));
2795 return ffa_error(FFA_INVALID_PARAMETERS);
2796 }
2797 fragment_length = tee_ret.arg3;
2798 if (fragment_length > HF_MAILBOX_SIZE ||
2799 fragment_offset + fragment_length > length) {
2800 dlog_verbose(
2801 "Invalid fragment length %d at offset %d (max "
2802 "%d).\n",
2803 fragment_length, fragment_offset,
2804 HF_MAILBOX_SIZE);
2805 return ffa_error(FFA_INVALID_PARAMETERS);
2806 }
2807 memcpy_s(tee_retrieve_buffer + fragment_offset,
2808 sizeof(tee_retrieve_buffer) - fragment_offset,
2809 from_locked.vm->mailbox.send, fragment_length);
2810
2811 fragment_offset += fragment_length;
2812 }
2813
2814 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002815
2816 if (memory_region->receiver_count != 1) {
2817 /* Only one receiver supported by Hafnium for now. */
2818 dlog_verbose(
2819 "Multiple recipients not supported (got %d, expected "
2820 "1).\n",
2821 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002822 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002823 }
2824
2825 if (memory_region->handle != handle) {
2826 dlog_verbose(
2827 "Got memory region handle %#x from TEE but requested "
2828 "handle %#x.\n",
2829 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002830 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002831 }
2832
2833 /* The original sender must match the caller. */
2834 if (to_locked.vm->id != memory_region->sender) {
2835 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002836 "VM %#x attempted to reclaim memory handle %#x "
2837 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002838 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002839 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002840 }
2841
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002842 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002843
2844 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002845 * Validate that the reclaim transition is allowed for the given memory
2846 * region, forward the request to the TEE and then map the memory back
2847 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002848 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002849 return ffa_tee_reclaim_check_update(
2850 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002851 composite->constituent_count, memory_to_attributes,
2852 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002853}