blob: ac2ccf9c4ed0504ac78d1096fbb8f0f2c05e5103 [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
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000016#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010017#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000020#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000022#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000024/**
25 * The maximum number of memory sharing handles which may be active at once. A
26 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
27 * or LEND handle is active from when it is sent to when it is reclaimed.
28 */
29#define MAX_MEM_SHARES 100
30
Andrew Walbranca808b12020-05-15 17:22:28 +010031/**
32 * The maximum number of fragments into which a memory sharing message may be
33 * broken.
34 */
35#define MAX_FRAGMENTS 20
36
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
38 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000039 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
41 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000042 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010044 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
46 "struct ffa_memory_access must be a multiple of 16 bytes long.");
47static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
48 "struct ffa_memory_region must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
50 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010053struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 /**
55 * The memory region being shared, or NULL if this share state is
56 * unallocated.
57 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010058 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059
Andrew Walbranca808b12020-05-15 17:22:28 +010060 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
61
62 /** The number of constituents in each fragment. */
63 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
64
65 /**
66 * The number of valid elements in the `fragments` and
67 * `fragment_constituent_counts` arrays.
68 */
69 uint32_t fragment_count;
70
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000071 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 * The FF-A function used for sharing the memory. Must be one of
73 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 * share state is allocated, or 0.
75 */
76 uint32_t share_func;
77
78 /**
J-Alves2a0d2882020-10-29 14:49:50 +000079 * The sender's original mode before invoking the FF-A function for
80 * sharing the memory.
81 * This is used to reset the original configuration when sender invokes
82 * FFA_MEM_RECLAIM_32.
83 */
84 uint32_t sender_orig_mode;
85
86 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010087 * True if all the fragments of this sharing request have been sent and
88 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089 */
Andrew Walbranca808b12020-05-15 17:22:28 +010090 bool sending_complete;
91
92 /**
93 * How many fragments of the memory region each recipient has retrieved
94 * so far. The order of this array matches the order of the endpoint
95 * memory access descriptors in the memory region descriptor. Any
96 * entries beyond the receiver_count will always be 0.
97 */
98 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +000099};
100
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000101/**
102 * Encapsulates the set of share states while the `share_states_lock` is held.
103 */
104struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100105 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000106};
107
108/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100109 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 * by this lock.
111 */
112static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100113static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000114
115/**
J-Alves8505a8a2022-06-15 18:10:18 +0100116 * Buffer for retrieving memory region information from the other world for when
117 * a region is reclaimed by a VM. Access to this buffer must be guarded by the
118 * VM lock of the other world VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000119 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100120alignas(PAGE_SIZE) static uint8_t
J-Alves8505a8a2022-06-15 18:10:18 +0100121 other_world_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
Andrew Walbranca808b12020-05-15 17:22:28 +0100122
123/**
J-Alves917d2f22020-10-30 18:39:30 +0000124 * Extracts the index from a memory handle allocated by Hafnium's current world.
125 */
126uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
127{
128 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
129}
130
131/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100132 * Initialises the next available `struct ffa_memory_share_state` and sets
133 * `share_state_ret` to a pointer to it. If `handle` is
134 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
135 * uses the provided handle which is assumed to be globally unique.
136 *
137 * Returns true on success or false if none are available.
138 */
139static bool allocate_share_state(
140 struct share_states_locked share_states, uint32_t share_func,
141 struct ffa_memory_region *memory_region, uint32_t fragment_length,
142 ffa_memory_handle_t handle,
143 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100145 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000147 assert(share_states.share_states != NULL);
148 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100151 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100154 &share_states.share_states[i];
155 struct ffa_composite_memory_region *composite =
156 ffa_memory_region_get_composite(memory_region,
157 0);
158
159 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000160 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000163 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000165 allocated_state->share_func = share_func;
166 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 allocated_state->fragment_count = 1;
168 allocated_state->fragments[0] = composite->constituents;
169 allocated_state->fragment_constituent_counts[0] =
170 (fragment_length -
171 ffa_composite_constituent_offset(memory_region,
172 0)) /
173 sizeof(struct ffa_memory_region_constituent);
174 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000175 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100176 allocated_state->retrieved_fragment_count[j] =
177 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000178 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 if (share_state_ret != NULL) {
180 *share_state_ret = allocated_state;
181 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000182 return true;
183 }
184 }
185
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000186 return false;
187}
188
189/** Locks the share states lock. */
190struct share_states_locked share_states_lock(void)
191{
192 sl_lock(&share_states_lock_instance);
193
194 return (struct share_states_locked){.share_states = share_states};
195}
196
197/** Unlocks the share states lock. */
198static void share_states_unlock(struct share_states_locked *share_states)
199{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000200 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000201 share_states->share_states = NULL;
202 sl_unlock(&share_states_lock_instance);
203}
204
205/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100206 * If the given handle is a valid handle for an allocated share state then
207 * initialises `share_state_ret` to point to the share state and returns true.
208 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209 */
210static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100211 ffa_memory_handle_t handle,
212 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000213{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100214 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000215 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000217 assert(share_states.share_states != NULL);
218 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100219
220 /*
221 * First look for a share_state allocated by us, in which case the
222 * handle is based on the index.
223 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200224 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000225 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100226 if (index < MAX_MEM_SHARES) {
227 share_state = &share_states.share_states[index];
228 if (share_state->share_func != 0) {
229 *share_state_ret = share_state;
230 return true;
231 }
232 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000233 }
234
Andrew Walbranca808b12020-05-15 17:22:28 +0100235 /* Fall back to a linear scan. */
236 for (index = 0; index < MAX_MEM_SHARES; ++index) {
237 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000238 if (share_state->memory_region != NULL &&
239 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100240 share_state->share_func != 0) {
241 *share_state_ret = share_state;
242 return true;
243 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000244 }
245
Andrew Walbranca808b12020-05-15 17:22:28 +0100246 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247}
248
249/** Marks a share state as unallocated. */
250static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100251 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000252 struct mpool *page_pool)
253{
Andrew Walbranca808b12020-05-15 17:22:28 +0100254 uint32_t i;
255
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000256 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000257 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100258 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000259 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260 /*
261 * First fragment is part of the same page as the `memory_region`, so it
262 * doesn't need to be freed separately.
263 */
264 share_state->fragments[0] = NULL;
265 share_state->fragment_constituent_counts[0] = 0;
266 for (i = 1; i < share_state->fragment_count; ++i) {
267 mpool_free(page_pool, share_state->fragments[i]);
268 share_state->fragments[i] = NULL;
269 share_state->fragment_constituent_counts[i] = 0;
270 }
271 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000272 share_state->memory_region = NULL;
273}
274
Andrew Walbranca808b12020-05-15 17:22:28 +0100275/** Checks whether the given share state has been fully sent. */
276static bool share_state_sending_complete(
277 struct share_states_locked share_states,
278 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000279{
Andrew Walbranca808b12020-05-15 17:22:28 +0100280 struct ffa_composite_memory_region *composite;
281 uint32_t expected_constituent_count;
282 uint32_t fragment_constituent_count_total = 0;
283 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000284
Andrew Walbranca808b12020-05-15 17:22:28 +0100285 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000286 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100287
288 /*
289 * Share state must already be valid, or it's not possible to get hold
290 * of it.
291 */
292 CHECK(share_state->memory_region != NULL &&
293 share_state->share_func != 0);
294
295 composite =
296 ffa_memory_region_get_composite(share_state->memory_region, 0);
297 expected_constituent_count = composite->constituent_count;
298 for (i = 0; i < share_state->fragment_count; ++i) {
299 fragment_constituent_count_total +=
300 share_state->fragment_constituent_counts[i];
301 }
302 dlog_verbose(
303 "Checking completion: constituent count %d/%d from %d "
304 "fragments.\n",
305 fragment_constituent_count_total, expected_constituent_count,
306 share_state->fragment_count);
307
308 return fragment_constituent_count_total == expected_constituent_count;
309}
310
311/**
312 * Calculates the offset of the next fragment expected for the given share
313 * state.
314 */
315static uint32_t share_state_next_fragment_offset(
316 struct share_states_locked share_states,
317 struct ffa_memory_share_state *share_state)
318{
319 uint32_t next_fragment_offset;
320 uint32_t i;
321
322 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000323 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100324
325 next_fragment_offset =
326 ffa_composite_constituent_offset(share_state->memory_region, 0);
327 for (i = 0; i < share_state->fragment_count; ++i) {
328 next_fragment_offset +=
329 share_state->fragment_constituent_counts[i] *
330 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000331 }
332
Andrew Walbranca808b12020-05-15 17:22:28 +0100333 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334}
335
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337{
338 uint32_t i;
339
340 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
341 return;
342 }
343
Olivier Deprez935e1b12020-12-22 18:01:29 +0100344 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100345 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100346 "recipients [",
347 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100348 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 memory_region->receiver_count);
350 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 if (i != 0) {
352 dlog(", ");
353 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100354 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100355 memory_region->receivers[i].receiver_permissions.receiver,
356 memory_region->receivers[i]
357 .receiver_permissions.permissions,
358 memory_region->receivers[i]
359 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000360 }
361 dlog("]");
362}
363
364static void dump_share_states(void)
365{
366 uint32_t i;
367
368 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
369 return;
370 }
371
372 dlog("Current share states:\n");
373 sl_lock(&share_states_lock_instance);
374 for (i = 0; i < MAX_MEM_SHARES; ++i) {
375 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 dlog("SHARE");
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000381 dlog("LEND");
382 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 dlog("DONATE");
385 break;
386 default:
387 dlog("invalid share_func %#x",
388 share_states[i].share_func);
389 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100390 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000391 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100392 if (share_states[i].sending_complete) {
393 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000396 }
J-Alves2a0d2882020-10-29 14:49:50 +0000397 dlog(" with %d fragments, %d retrieved, "
398 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000400 share_states[i].retrieved_fragment_count[0],
401 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000402 }
403 }
404 sl_unlock(&share_states_lock_instance);
405}
406
Andrew Walbran475c1452020-02-07 13:22:22 +0000407/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100408static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100409 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000410{
411 uint32_t mode = 0;
412
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100413 switch (ffa_get_data_access_attr(permissions)) {
414 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000415 mode = MM_MODE_R;
416 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000418 mode = MM_MODE_R | MM_MODE_W;
419 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100420 case FFA_DATA_ACCESS_NOT_SPECIFIED:
421 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
422 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100423 case FFA_DATA_ACCESS_RESERVED:
424 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100425 }
426
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 switch (ffa_get_instruction_access_attr(permissions)) {
428 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000429 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100431 mode |= MM_MODE_X;
432 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100433 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
434 mode |= (default_mode & MM_MODE_X);
435 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436 case FFA_INSTRUCTION_ACCESS_RESERVED:
437 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000438 }
439
440 return mode;
441}
442
Jose Marinho75509b42019-04-09 09:34:59 +0100443/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000444 * Get the current mode in the stage-2 page table of the given vm of all the
445 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100446 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100447 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000449 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 struct ffa_memory_region_constituent **fragments,
451 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100452{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100453 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100454 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100455
Andrew Walbranca808b12020-05-15 17:22:28 +0100456 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100457 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000458 * Fail if there are no constituents. Otherwise we would get an
459 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100461 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100462 }
463
Andrew Walbranca808b12020-05-15 17:22:28 +0100464 for (i = 0; i < fragment_count; ++i) {
465 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
466 ipaddr_t begin = ipa_init(fragments[i][j].address);
467 size_t size = fragments[i][j].page_count * PAGE_SIZE;
468 ipaddr_t end = ipa_add(begin, size);
469 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100470
Andrew Walbranca808b12020-05-15 17:22:28 +0100471 /* Fail if addresses are not page-aligned. */
472 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
473 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
474 return ffa_error(FFA_INVALID_PARAMETERS);
475 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100476
Andrew Walbranca808b12020-05-15 17:22:28 +0100477 /*
478 * Ensure that this constituent memory range is all
479 * mapped with the same mode.
480 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800481 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100482 return ffa_error(FFA_DENIED);
483 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100484
Andrew Walbranca808b12020-05-15 17:22:28 +0100485 /*
486 * Ensure that all constituents are mapped with the same
487 * mode.
488 */
489 if (i == 0) {
490 *orig_mode = current_mode;
491 } else if (current_mode != *orig_mode) {
492 dlog_verbose(
493 "Expected mode %#x but was %#x for %d "
494 "pages at %#x.\n",
495 *orig_mode, current_mode,
496 fragments[i][j].page_count,
497 ipa_addr(begin));
498 return ffa_error(FFA_DENIED);
499 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100500 }
Jose Marinho75509b42019-04-09 09:34:59 +0100501 }
502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000504}
505
506/**
507 * Verify that all pages have the same mode, that the starting mode
508 * constitutes a valid state and obtain the next mode to apply
509 * to the sending VM.
510 *
511 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512 * 1) FFA_DENIED if a state transition was not found;
513 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100514 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100516 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100517 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
518 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000519 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100521 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100522 struct ffa_memory_access *receivers, uint32_t receivers_count,
523 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100524 struct ffa_memory_region_constituent **fragments,
525 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
526 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000527{
528 const uint32_t state_mask =
529 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100530 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000531
Andrew Walbranca808b12020-05-15 17:22:28 +0100532 ret = constituents_get_mode(from, orig_from_mode, fragments,
533 fragment_constituent_counts,
534 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100536 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100537 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100538 }
539
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000540 /* Ensure the address range is normal memory and not a device. */
541 if (*orig_from_mode & MM_MODE_D) {
542 dlog_verbose("Can't share device memory (mode is %#x).\n",
543 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 }
546
547 /*
548 * Ensure the sender is the owner and has exclusive access to the
549 * memory.
550 */
551 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100552 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100553 }
554
J-Alves363f5722022-04-25 17:37:37 +0100555 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100556
J-Alves363f5722022-04-25 17:37:37 +0100557 for (uint32_t i = 0U; i < receivers_count; i++) {
558 ffa_memory_access_permissions_t permissions =
559 receivers[i].receiver_permissions.permissions;
560 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
561 permissions, *orig_from_mode);
562
563 if ((*orig_from_mode & required_from_mode) !=
564 required_from_mode) {
565 dlog_verbose(
566 "Sender tried to send memory with permissions "
567 "which "
568 "required mode %#x but only had %#x itself.\n",
569 required_from_mode, *orig_from_mode);
570 return ffa_error(FFA_DENIED);
571 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572 }
573
574 /* Find the appropriate new mode. */
575 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000576 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100579 break;
580
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100583 break;
584
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000586 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100587 break;
588
Jose Marinho75509b42019-04-09 09:34:59 +0100589 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100591 }
592
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100593 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000594}
595
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100596static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000597 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100598 struct ffa_memory_region_constituent **fragments,
599 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
600 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601{
602 const uint32_t state_mask =
603 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
604 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606
Andrew Walbranca808b12020-05-15 17:22:28 +0100607 ret = constituents_get_mode(from, orig_from_mode, fragments,
608 fragment_constituent_counts,
609 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100611 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000612 }
613
614 /* Ensure the address range is normal memory and not a device. */
615 if (*orig_from_mode & MM_MODE_D) {
616 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
617 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000619 }
620
621 /*
622 * Ensure the relinquishing VM is not the owner but has access to the
623 * memory.
624 */
625 orig_from_state = *orig_from_mode & state_mask;
626 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
627 dlog_verbose(
628 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100629 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000630 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632 }
633
634 /* Find the appropriate new mode. */
635 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100637 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000638}
639
640/**
641 * Verify that all pages have the same mode, that the starting mode
642 * constitutes a valid state and obtain the next mode to apply
643 * to the retrieving VM.
644 *
645 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 * 1) FFA_DENIED if a state transition was not found;
647 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100648 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100649 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100650 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
652 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100654static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000655 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100656 struct ffa_memory_region_constituent **fragments,
657 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
658 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000659{
660 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662
Andrew Walbranca808b12020-05-15 17:22:28 +0100663 ret = constituents_get_mode(to, &orig_to_mode, fragments,
664 fragment_constituent_counts,
665 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100666 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100667 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100668 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669 }
670
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000672 /*
673 * If the original ffa memory send call has been processed
674 * successfully, it is expected the orig_to_mode would overlay
675 * with `state_mask`, as a result of the function
676 * `ffa_send_check_transition`.
677 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100678 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
679 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000680 } else {
681 /*
682 * Ensure the retriever has the expected state. We don't care
683 * about the MM_MODE_SHARED bit; either with or without it set
684 * are both valid representations of the !O-NA state.
685 */
686 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
687 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000689 }
690 }
691
692 /* Find the appropriate new mode. */
693 *to_mode = memory_to_attributes;
694 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100695 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000696 *to_mode |= 0;
697 break;
698
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100699 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000700 *to_mode |= MM_MODE_UNOWNED;
701 break;
702
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000704 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
705 break;
706
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000708 *to_mode |= 0;
709 break;
710
711 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100712 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000714 }
715
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100717}
Jose Marinho09b1db82019-08-08 09:16:59 +0100718
719/**
720 * Updates a VM's page table such that the given set of physical address ranges
721 * are mapped in the address space at the corresponding address ranges, in the
722 * mode provided.
723 *
724 * If commit is false, the page tables will be allocated from the mpool but no
725 * mappings will actually be updated. This function must always be called first
726 * with commit false to check that it will succeed before calling with commit
727 * true, to avoid leaving the page table in a half-updated state. To make a
728 * series of changes atomically you can call them all with commit false before
729 * calling them all with commit true.
730 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700731 * vm_ptable_defrag should always be called after a series of page table
732 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100733 *
734 * Returns true on success, or false if the update failed and no changes were
735 * made to memory mappings.
736 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100737static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000738 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100739 struct ffa_memory_region_constituent **fragments,
740 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100741 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100742{
Andrew Walbranca808b12020-05-15 17:22:28 +0100743 uint32_t i;
744 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100745
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700746 if (vm_locked.vm->el0_partition) {
747 mode |= MM_MODE_USER | MM_MODE_NG;
748 }
749
Andrew Walbranca808b12020-05-15 17:22:28 +0100750 /* Iterate over the memory region constituents within each fragment. */
751 for (i = 0; i < fragment_count; ++i) {
752 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
753 size_t size = fragments[i][j].page_count * PAGE_SIZE;
754 paddr_t pa_begin =
755 pa_from_ipa(ipa_init(fragments[i][j].address));
756 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200757 uint32_t pa_bits =
758 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100759
760 /*
761 * Ensure the requested region falls into system's PA
762 * range.
763 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200764 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
765 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100766 dlog_error("Region is outside of PA Range\n");
767 return false;
768 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100769
770 if (commit) {
771 vm_identity_commit(vm_locked, pa_begin, pa_end,
772 mode, ppool, NULL);
773 } else if (!vm_identity_prepare(vm_locked, pa_begin,
774 pa_end, mode, ppool)) {
775 return false;
776 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100777 }
778 }
779
780 return true;
781}
782
783/**
784 * Clears a region of physical memory by overwriting it with zeros. The data is
785 * flushed from the cache so the memory has been cleared across the system.
786 */
J-Alves7db32002021-12-14 14:44:50 +0000787static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
788 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100789{
790 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000791 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 * global mapping of the whole range. Such an approach will limit
793 * the changes to stage-1 tables and will allow only local
794 * invalidation.
795 */
796 bool ret;
797 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000798 void *ptr = mm_identity_map(stage1_locked, begin, end,
799 MM_MODE_W | (extra_mode_attributes &
800 plat_ffa_other_world_mode()),
801 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100802 size_t size = pa_difference(begin, end);
803
804 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100805 goto fail;
806 }
807
808 memset_s(ptr, size, 0, size);
809 arch_mm_flush_dcache(ptr, size);
810 mm_unmap(stage1_locked, begin, end, ppool);
811
812 ret = true;
813 goto out;
814
815fail:
816 ret = false;
817
818out:
819 mm_unlock_stage1(&stage1_locked);
820
821 return ret;
822}
823
824/**
825 * Clears a region of physical memory by overwriting it with zeros. The data is
826 * flushed from the cache so the memory has been cleared across the system.
827 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000829 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100830 struct ffa_memory_region_constituent **fragments,
831 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
832 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100833{
834 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100835 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100836 bool ret = false;
837
838 /*
839 * Create a local pool so any freed memory can't be used by another
840 * thread. This is to ensure each constituent that is mapped can be
841 * unmapped again afterwards.
842 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000843 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100844
Andrew Walbranca808b12020-05-15 17:22:28 +0100845 /* Iterate over the memory region constituents within each fragment. */
846 for (i = 0; i < fragment_count; ++i) {
847 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100848
Andrew Walbranca808b12020-05-15 17:22:28 +0100849 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
850 size_t size = fragments[i][j].page_count * PAGE_SIZE;
851 paddr_t begin =
852 pa_from_ipa(ipa_init(fragments[i][j].address));
853 paddr_t end = pa_add(begin, size);
854
J-Alves7db32002021-12-14 14:44:50 +0000855 if (!clear_memory(begin, end, &local_page_pool,
856 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100857 /*
858 * api_clear_memory will defrag on failure, so
859 * no need to do it here.
860 */
861 goto out;
862 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 }
864 }
865
Jose Marinho09b1db82019-08-08 09:16:59 +0100866 ret = true;
867
868out:
869 mpool_fini(&local_page_pool);
870 return ret;
871}
872
873/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100875 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100877 *
878 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000879 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100880 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100882 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
883 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100885 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100887 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100888static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000889 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100890 struct ffa_memory_region_constituent **fragments,
891 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100892 uint32_t share_func, struct ffa_memory_access *receivers,
893 uint32_t receivers_count, struct mpool *page_pool, bool clear,
894 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100895{
Andrew Walbranca808b12020-05-15 17:22:28 +0100896 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100897 uint32_t orig_from_mode;
898 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100899 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100900 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100901
902 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100903 * Make sure constituents are properly aligned to a 64-bit boundary. If
904 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100905 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100906 for (i = 0; i < fragment_count; ++i) {
907 if (!is_aligned(fragments[i], 8)) {
908 dlog_verbose("Constituents not aligned.\n");
909 return ffa_error(FFA_INVALID_PARAMETERS);
910 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100911 }
912
913 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000914 * Check if the state transition is lawful for the sender, ensure that
915 * all constituents of a memory region being shared are at the same
916 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100917 */
J-Alves363f5722022-04-25 17:37:37 +0100918 ret = ffa_send_check_transition(from_locked, share_func, receivers,
919 receivers_count, &orig_from_mode,
920 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100921 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100922 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100923 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100924 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100925 }
926
Andrew Walbran37c574e2020-06-03 11:45:46 +0100927 if (orig_from_mode_ret != NULL) {
928 *orig_from_mode_ret = orig_from_mode;
929 }
930
Jose Marinho09b1db82019-08-08 09:16:59 +0100931 /*
932 * Create a local pool so any freed memory can't be used by another
933 * thread. This is to ensure the original mapping can be restored if the
934 * clear fails.
935 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000936 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100937
938 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000939 * First reserve all required memory for the new page table entries
940 * without committing, to make sure the entire operation will succeed
941 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100942 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100943 if (!ffa_region_group_identity_map(
944 from_locked, fragments, fragment_constituent_counts,
945 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100946 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100947 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100948 goto out;
949 }
950
951 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000952 * Update the mapping for the sender. This won't allocate because the
953 * transaction was already prepared above, but may free pages in the
954 * case that a whole block is being unmapped that was previously
955 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100956 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100957 CHECK(ffa_region_group_identity_map(
958 from_locked, fragments, fragment_constituent_counts,
959 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100960
961 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000962 if (clear &&
963 !ffa_clear_memory_constituents(
964 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
965 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100966 /*
967 * On failure, roll back by returning memory to the sender. This
968 * may allocate pages which were previously freed into
969 * `local_page_pool` by the call above, but will never allocate
970 * more pages than that so can never fail.
971 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100972 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100973 from_locked, fragments, fragment_constituent_counts,
974 fragment_count, orig_from_mode, &local_page_pool,
975 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100976
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100978 goto out;
979 }
980
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000982
983out:
984 mpool_fini(&local_page_pool);
985
986 /*
987 * Tidy up the page table by reclaiming failed mappings (if there was an
988 * error) or merging entries into blocks where possible (on success).
989 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700990 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991
992 return ret;
993}
994
995/**
996 * Validates and maps memory shared from one VM to another.
997 *
998 * This function requires the calling context to hold the <to> lock.
999 *
1000 * Returns:
1001 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001003 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001006 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001008static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001009 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001010 struct ffa_memory_region_constituent **fragments,
1011 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1012 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1013 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001014{
Andrew Walbranca808b12020-05-15 17:22:28 +01001015 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001016 uint32_t to_mode;
1017 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001018 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019
1020 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001021 * Make sure constituents are properly aligned to a 64-bit boundary. If
1022 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001023 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001024 for (i = 0; i < fragment_count; ++i) {
1025 if (!is_aligned(fragments[i], 8)) {
1026 return ffa_error(FFA_INVALID_PARAMETERS);
1027 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 }
1029
1030 /*
1031 * Check if the state transition is lawful for the recipient, and ensure
1032 * that all constituents of the memory region being retrieved are at the
1033 * same state.
1034 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001035 ret = ffa_retrieve_check_transition(
1036 to_locked, share_func, fragments, fragment_constituent_counts,
1037 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001038 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001039 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001040 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001041 }
1042
1043 /*
1044 * Create a local pool so any freed memory can't be used by another
1045 * thread. This is to ensure the original mapping can be restored if the
1046 * clear fails.
1047 */
1048 mpool_init_with_fallback(&local_page_pool, page_pool);
1049
1050 /*
1051 * First reserve all required memory for the new page table entries in
1052 * the recipient page tables without committing, to make sure the entire
1053 * operation will succeed without exhausting the page pool.
1054 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001055 if (!ffa_region_group_identity_map(
1056 to_locked, fragments, fragment_constituent_counts,
1057 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001058 /* TODO: partial defrag of failed range. */
1059 dlog_verbose(
1060 "Insufficient memory to update recipient page "
1061 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001062 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001063 goto out;
1064 }
1065
1066 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001067 if (clear &&
1068 !ffa_clear_memory_constituents(
1069 plat_ffa_owner_world_mode(from_id), fragments,
1070 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001072 goto out;
1073 }
1074
Jose Marinho09b1db82019-08-08 09:16:59 +01001075 /*
1076 * Complete the transfer by mapping the memory into the recipient. This
1077 * won't allocate because the transaction was already prepared above, so
1078 * it doesn't need to use the `local_page_pool`.
1079 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001080 CHECK(ffa_region_group_identity_map(
1081 to_locked, fragments, fragment_constituent_counts,
1082 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001083
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001084 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001085
1086out:
1087 mpool_fini(&local_page_pool);
1088
1089 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001090 * Tidy up the page table by reclaiming failed mappings (if there was an
1091 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001092 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001093 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001094
1095 return ret;
1096}
1097
Andrew Walbran290b0c92020-02-03 16:37:14 +00001098/**
J-Alves8505a8a2022-06-15 18:10:18 +01001099 * Reclaims the given memory from the other world. To do this space is first
1100 * reserved in the <to> VM's page table, then the reclaim request is sent on to
1101 * the other world. then (if that is successful) the memory is mapped back into
1102 * the <to> VM's page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001103 *
1104 * This function requires the calling context to hold the <to> lock.
1105 *
1106 * Returns:
1107 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001108 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001109 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001110 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001111 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001112 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001113 */
J-Alves8505a8a2022-06-15 18:10:18 +01001114static struct ffa_value ffa_other_world_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 struct vm_locked to_locked, ffa_memory_handle_t handle,
1116 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001117 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1118 struct mpool *page_pool)
1119{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001120 uint32_t to_mode;
1121 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001122 struct ffa_value ret;
J-Alves8505a8a2022-06-15 18:10:18 +01001123 ffa_memory_region_flags_t other_world_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001124
1125 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001126 * Make sure constituents are properly aligned to a 64-bit boundary. If
1127 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001128 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001129 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001130 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001131 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001132 }
1133
1134 /*
1135 * Check if the state transition is lawful for the recipient, and ensure
1136 * that all constituents of the memory region being retrieved are at the
1137 * same state.
1138 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001140 &constituents, &constituent_count,
1141 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001142 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001143 dlog_verbose("Invalid transition.\n");
1144 return ret;
1145 }
1146
1147 /*
1148 * Create a local pool so any freed memory can't be used by another
1149 * thread. This is to ensure the original mapping can be restored if the
1150 * clear fails.
1151 */
1152 mpool_init_with_fallback(&local_page_pool, page_pool);
1153
1154 /*
1155 * First reserve all required memory for the new page table entries in
1156 * the recipient page tables without committing, to make sure the entire
1157 * operation will succeed without exhausting the page pool.
1158 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001159 if (!ffa_region_group_identity_map(to_locked, &constituents,
1160 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001161 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001162 /* TODO: partial defrag of failed range. */
1163 dlog_verbose(
1164 "Insufficient memory to update recipient page "
1165 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001166 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001167 goto out;
1168 }
1169
1170 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001171 * Forward the request to the other world and see what happens.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001172 */
J-Alves8505a8a2022-06-15 18:10:18 +01001173 other_world_flags = 0;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001174 if (clear) {
J-Alves8505a8a2022-06-15 18:10:18 +01001175 other_world_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001176 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001177 ret = arch_other_world_call(
1178 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1179 .arg1 = (uint32_t)handle,
1180 .arg2 = (uint32_t)(handle >> 32),
J-Alves8505a8a2022-06-15 18:10:18 +01001181 .arg3 = other_world_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001182
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001183 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001184 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001185 "Got %#x (%d) from other world in response to "
1186 "FFA_MEM_RECLAIM, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001187 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001188 ret.func, ret.arg2);
1189 goto out;
1190 }
1191
1192 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001193 * The other world was happy with it, so complete the reclaim by mapping
1194 * the memory into the recipient. This won't allocate because the
Andrew Walbran290b0c92020-02-03 16:37:14 +00001195 * transaction was already prepared above, so it doesn't need to use the
1196 * `local_page_pool`.
1197 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001198 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1199 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001200 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001201
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001202 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001203
1204out:
1205 mpool_fini(&local_page_pool);
1206
1207 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001208 * Tidy up the page table by reclaiming failed mappings (if there was an
1209 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001210 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001211 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001212
1213 return ret;
1214}
1215
Andrew Walbran996d1d12020-05-27 14:08:43 +01001216static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001217 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001218 struct ffa_memory_region_constituent **fragments,
1219 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1220 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001221{
1222 uint32_t orig_from_mode;
1223 uint32_t from_mode;
1224 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001225 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001226
Andrew Walbranca808b12020-05-15 17:22:28 +01001227 ret = ffa_relinquish_check_transition(
1228 from_locked, &orig_from_mode, fragments,
1229 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001230 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001231 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001232 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001233 }
1234
1235 /*
1236 * Create a local pool so any freed memory can't be used by another
1237 * thread. This is to ensure the original mapping can be restored if the
1238 * clear fails.
1239 */
1240 mpool_init_with_fallback(&local_page_pool, page_pool);
1241
1242 /*
1243 * First reserve all required memory for the new page table entries
1244 * without committing, to make sure the entire operation will succeed
1245 * without exhausting the page pool.
1246 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001247 if (!ffa_region_group_identity_map(
1248 from_locked, fragments, fragment_constituent_counts,
1249 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001250 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001251 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252 goto out;
1253 }
1254
1255 /*
1256 * Update the mapping for the sender. This won't allocate because the
1257 * transaction was already prepared above, but may free pages in the
1258 * case that a whole block is being unmapped that was previously
1259 * partially mapped.
1260 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 CHECK(ffa_region_group_identity_map(
1262 from_locked, fragments, fragment_constituent_counts,
1263 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001264
1265 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001266 if (clear &&
1267 !ffa_clear_memory_constituents(
1268 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1269 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001270 /*
1271 * On failure, roll back by returning memory to the sender. This
1272 * may allocate pages which were previously freed into
1273 * `local_page_pool` by the call above, but will never allocate
1274 * more pages than that so can never fail.
1275 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001276 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001277 from_locked, fragments, fragment_constituent_counts,
1278 fragment_count, orig_from_mode, &local_page_pool,
1279 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001280
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001281 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001282 goto out;
1283 }
1284
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001286
1287out:
1288 mpool_fini(&local_page_pool);
1289
1290 /*
1291 * Tidy up the page table by reclaiming failed mappings (if there was an
1292 * error) or merging entries into blocks where possible (on success).
1293 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001294 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001295
1296 return ret;
1297}
1298
1299/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001300 * Complete a memory sending operation by checking that it is valid, updating
1301 * the sender page table, and then either marking the share state as having
1302 * completed sending (on success) or freeing it (on failure).
1303 *
1304 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1305 */
1306static struct ffa_value ffa_memory_send_complete(
1307 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001308 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1309 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001310{
1311 struct ffa_memory_region *memory_region = share_state->memory_region;
1312 struct ffa_value ret;
1313
1314 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001315 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001316
1317 /* Check that state is valid in sender page table and update. */
1318 ret = ffa_send_check_update(
1319 from_locked, share_state->fragments,
1320 share_state->fragment_constituent_counts,
1321 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001322 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001323 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1324 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001325 if (ret.func != FFA_SUCCESS_32) {
1326 /*
1327 * Free share state, it failed to send so it can't be retrieved.
1328 */
1329 dlog_verbose("Complete failed, freeing share state.\n");
1330 share_state_free(share_states, share_state, page_pool);
1331 return ret;
1332 }
1333
1334 share_state->sending_complete = true;
1335 dlog_verbose("Marked sending complete.\n");
1336
J-Alvesee68c542020-10-29 17:48:20 +00001337 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001338}
1339
1340/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001341 * Check that the memory attributes match Hafnium expectations:
1342 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1343 * Write-Allocate Cacheable.
1344 */
1345static struct ffa_value ffa_memory_attributes_validate(
1346 ffa_memory_access_permissions_t attributes)
1347{
1348 enum ffa_memory_type memory_type;
1349 enum ffa_memory_cacheability cacheability;
1350 enum ffa_memory_shareability shareability;
1351
1352 memory_type = ffa_get_memory_type_attr(attributes);
1353 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1354 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1355 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001356 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001357 }
1358
1359 cacheability = ffa_get_memory_cacheability_attr(attributes);
1360 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1361 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1362 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001363 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001364 }
1365
1366 shareability = ffa_get_memory_shareability_attr(attributes);
1367 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1368 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1369 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001370 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001371 }
1372
1373 return (struct ffa_value){.func = FFA_SUCCESS_32};
1374}
1375
1376/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001377 * Check that the given `memory_region` represents a valid memory send request
1378 * of the given `share_func` type, return the clear flag and permissions via the
1379 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001380 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001382 * not.
1383 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001384static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001385 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1386 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001387 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001390 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001391 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001392 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001393 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001394 enum ffa_data_access data_access;
1395 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001396 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397
J-Alves95df0ef2022-12-07 10:09:48 +00001398 /* The sender must match the caller. */
1399 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1400 vm_id_is_current_world(memory_region->sender)) ||
1401 (vm_id_is_current_world(from_locked.vm->id) &&
1402 memory_region->sender != from_locked.vm->id)) {
1403 dlog_verbose("Invalid memory sender ID.\n");
1404 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001405 }
1406
Andrew Walbrana65a1322020-04-06 19:32:32 +01001407 /*
1408 * Ensure that the composite header is within the memory bounds and
1409 * doesn't overlap the first part of the message.
1410 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001411 receivers_length = sizeof(struct ffa_memory_access) *
1412 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001413 constituents_offset =
1414 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001415 composite_memory_region_offset =
1416 memory_region->receivers[0].composite_memory_region_offset;
1417 if ((composite_memory_region_offset == 0) ||
1418 (composite_memory_region_offset <
1419 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001420 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001422 "Invalid composite memory region descriptor offset "
1423 "%d.\n",
1424 memory_region->receivers[0]
1425 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001426 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001427 }
1428
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430
1431 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001432 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001433 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001434 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1435 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001436 if (memory_share_length != constituents_offset + constituents_length) {
1437 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001438 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 memory_region->receivers[0]
1440 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001442 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001443 if (fragment_length < memory_share_length &&
1444 fragment_length < HF_MAILBOX_SIZE) {
1445 dlog_warning(
1446 "Initial fragment length %d smaller than mailbox "
1447 "size.\n",
1448 fragment_length);
1449 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001450
Andrew Walbrana65a1322020-04-06 19:32:32 +01001451 /*
1452 * Clear is not allowed for memory sharing, as the sender still has
1453 * access to the memory.
1454 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001455 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1456 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001459 }
1460
1461 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001463 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001464 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001465 }
1466
J-Alves363f5722022-04-25 17:37:37 +01001467 /* Check that the permissions are valid, for each specified receiver. */
1468 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1469 ffa_memory_access_permissions_t permissions =
1470 memory_region->receivers[i]
1471 .receiver_permissions.permissions;
1472 ffa_vm_id_t receiver_id =
1473 memory_region->receivers[i]
1474 .receiver_permissions.receiver;
1475
1476 if (memory_region->sender == receiver_id) {
1477 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001478 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001479 }
Federico Recanati85090c42021-12-15 13:17:54 +01001480
J-Alves363f5722022-04-25 17:37:37 +01001481 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1482 j++) {
1483 if (receiver_id ==
1484 memory_region->receivers[j]
1485 .receiver_permissions.receiver) {
1486 dlog_verbose(
1487 "Repeated receiver(%x) in memory send "
1488 "operation.\n",
1489 memory_region->receivers[j]
1490 .receiver_permissions.receiver);
1491 return ffa_error(FFA_INVALID_PARAMETERS);
1492 }
1493 }
1494
1495 if (composite_memory_region_offset !=
1496 memory_region->receivers[i]
1497 .composite_memory_region_offset) {
1498 dlog_verbose(
1499 "All ffa_memory_access should point to the "
1500 "same composite memory region offset.\n");
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
1503
1504 data_access = ffa_get_data_access_attr(permissions);
1505 instruction_access =
1506 ffa_get_instruction_access_attr(permissions);
1507 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1508 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1509 dlog_verbose(
1510 "Reserved value for receiver permissions "
1511 "%#x.\n",
1512 permissions);
1513 return ffa_error(FFA_INVALID_PARAMETERS);
1514 }
1515 if (instruction_access !=
1516 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1517 dlog_verbose(
1518 "Invalid instruction access permissions %#x "
1519 "for sending memory.\n",
1520 permissions);
1521 return ffa_error(FFA_INVALID_PARAMETERS);
1522 }
1523 if (share_func == FFA_MEM_SHARE_32) {
1524 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1525 dlog_verbose(
1526 "Invalid data access permissions %#x "
1527 "for sharing memory.\n",
1528 permissions);
1529 return ffa_error(FFA_INVALID_PARAMETERS);
1530 }
1531 /*
1532 * According to section 10.10.3 of the FF-A v1.1 EAC0
1533 * spec, NX is required for share operations (but must
1534 * not be specified by the sender) so set it in the
1535 * copy that we store, ready to be returned to the
1536 * retriever.
1537 */
J-Alvesb19731a2022-06-20 17:30:33 +01001538 if (vm_id_is_current_world(receiver_id)) {
1539 ffa_set_instruction_access_attr(
1540 &permissions,
1541 FFA_INSTRUCTION_ACCESS_NX);
1542 memory_region->receivers[i]
1543 .receiver_permissions.permissions =
1544 permissions;
1545 }
J-Alves363f5722022-04-25 17:37:37 +01001546 }
1547 if (share_func == FFA_MEM_LEND_32 &&
1548 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1549 dlog_verbose(
1550 "Invalid data access permissions %#x for "
1551 "lending memory.\n",
1552 permissions);
1553 return ffa_error(FFA_INVALID_PARAMETERS);
1554 }
1555
1556 if (share_func == FFA_MEM_DONATE_32 &&
1557 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1558 dlog_verbose(
1559 "Invalid data access permissions %#x for "
1560 "donating memory.\n",
1561 permissions);
1562 return ffa_error(FFA_INVALID_PARAMETERS);
1563 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001564 }
1565
Federico Recanatid937f5e2021-12-20 17:38:23 +01001566 /*
J-Alves807794e2022-06-16 13:42:47 +01001567 * If a memory donate or lend with single borrower, the memory type
1568 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001569 */
J-Alves807794e2022-06-16 13:42:47 +01001570 if (share_func == FFA_MEM_DONATE_32 ||
1571 (share_func == FFA_MEM_LEND_32 &&
1572 memory_region->receiver_count == 1)) {
1573 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1574 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1575 dlog_verbose(
1576 "Memory type shall not be specified by "
1577 "sender.\n");
1578 return ffa_error(FFA_INVALID_PARAMETERS);
1579 }
1580 } else {
1581 /*
1582 * Check that sender's memory attributes match Hafnium
1583 * expectations: Normal Memory, Inner shareable, Write-Back
1584 * Read-Allocate Write-Allocate Cacheable.
1585 */
1586 ret = ffa_memory_attributes_validate(memory_region->attributes);
1587 if (ret.func != FFA_SUCCESS_32) {
1588 return ret;
1589 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001590 }
1591
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001592 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001593}
1594
J-Alves8505a8a2022-06-15 18:10:18 +01001595/** Forwards a memory send message on to the other world. */
1596static struct ffa_value memory_send_other_world_forward(
1597 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001598 uint32_t share_func, struct ffa_memory_region *memory_region,
1599 uint32_t memory_share_length, uint32_t fragment_length)
1600{
1601 struct ffa_value ret;
1602
J-Alves8505a8a2022-06-15 18:10:18 +01001603 /* Use its own RX buffer. */
1604 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001605 memory_region, fragment_length);
J-Alves8505a8a2022-06-15 18:10:18 +01001606 other_world_locked.vm->mailbox.recv_size = fragment_length;
1607 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1608 other_world_locked.vm->mailbox.recv_func = share_func;
1609 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001610 ret = arch_other_world_call(
1611 (struct ffa_value){.func = share_func,
1612 .arg1 = memory_share_length,
1613 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001614 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001615 * After the call to the other world completes it must have finished
1616 * reading its RX buffer, so it is ready for another message.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001617 */
J-Alves8505a8a2022-06-15 18:10:18 +01001618 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001619
1620 return ret;
1621}
1622
Andrew Walbrana65a1322020-04-06 19:32:32 +01001623/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001624 * Gets the share state for continuing an operation to donate, lend or share
1625 * memory, and checks that it is a valid request.
1626 *
1627 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1628 * not.
1629 */
1630static struct ffa_value ffa_memory_send_continue_validate(
1631 struct share_states_locked share_states, ffa_memory_handle_t handle,
1632 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1633 struct mpool *page_pool)
1634{
1635 struct ffa_memory_share_state *share_state;
1636 struct ffa_memory_region *memory_region;
1637
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001638 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001639
1640 /*
1641 * Look up the share state by handle and make sure that the VM ID
1642 * matches.
1643 */
1644 if (!get_share_state(share_states, handle, &share_state)) {
1645 dlog_verbose(
1646 "Invalid handle %#x for memory send continuation.\n",
1647 handle);
1648 return ffa_error(FFA_INVALID_PARAMETERS);
1649 }
1650 memory_region = share_state->memory_region;
1651
1652 if (memory_region->sender != from_vm_id) {
1653 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1654 return ffa_error(FFA_INVALID_PARAMETERS);
1655 }
1656
1657 if (share_state->sending_complete) {
1658 dlog_verbose(
1659 "Sending of memory handle %#x is already complete.\n",
1660 handle);
1661 return ffa_error(FFA_INVALID_PARAMETERS);
1662 }
1663
1664 if (share_state->fragment_count == MAX_FRAGMENTS) {
1665 /*
1666 * Log a warning as this is a sign that MAX_FRAGMENTS should
1667 * probably be increased.
1668 */
1669 dlog_warning(
1670 "Too many fragments for memory share with handle %#x; "
1671 "only %d supported.\n",
1672 handle, MAX_FRAGMENTS);
1673 /* Free share state, as it's not possible to complete it. */
1674 share_state_free(share_states, share_state, page_pool);
1675 return ffa_error(FFA_NO_MEMORY);
1676 }
1677
1678 *share_state_ret = share_state;
1679
1680 return (struct ffa_value){.func = FFA_SUCCESS_32};
1681}
1682
1683/**
J-Alves8505a8a2022-06-15 18:10:18 +01001684 * Forwards a memory send continuation message on to the other world.
Andrew Walbranca808b12020-05-15 17:22:28 +01001685 */
J-Alves8505a8a2022-06-15 18:10:18 +01001686static struct ffa_value memory_send_continue_other_world_forward(
1687 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
1688 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle)
Andrew Walbranca808b12020-05-15 17:22:28 +01001689{
1690 struct ffa_value ret;
1691
J-Alves8505a8a2022-06-15 18:10:18 +01001692 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1693 fragment, fragment_length);
1694 other_world_locked.vm->mailbox.recv_size = fragment_length;
1695 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1696 other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1697 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001698 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001699 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1700 .arg1 = (uint32_t)handle,
1701 .arg2 = (uint32_t)(handle >> 32),
1702 .arg3 = fragment_length,
1703 .arg4 = (uint64_t)sender_vm_id << 16});
1704 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001705 * After the call to the other world completes it must have finished
1706 * reading its RX buffer, so it is ready for another message.
Andrew Walbranca808b12020-05-15 17:22:28 +01001707 */
J-Alves8505a8a2022-06-15 18:10:18 +01001708 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbranca808b12020-05-15 17:22:28 +01001709
1710 return ret;
1711}
1712
1713/**
J-Alves95df0ef2022-12-07 10:09:48 +00001714 * Checks if there is at least one receiver from the other world.
1715 */
1716static bool memory_region_receivers_from_other_world(
1717 struct ffa_memory_region *memory_region)
1718{
1719 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1720 ffa_vm_id_t receiver = memory_region->receivers[i]
1721 .receiver_permissions.receiver;
1722 if (!vm_id_is_current_world(receiver)) {
1723 return true;
1724 }
1725 }
1726 return false;
1727}
1728
1729/**
J-Alves8505a8a2022-06-15 18:10:18 +01001730 * Validates a call to donate, lend or share memory to a non-other world VM and
1731 * then updates the stage-2 page tables. Specifically, check if the message
1732 * length and number of memory region constituents match, and if the transition
1733 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001734 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001735 * Assumes that the caller has already found and locked the sender VM and copied
1736 * the memory region descriptor from the sender's TX buffer to a freshly
1737 * allocated page from Hafnium's internal pool. The caller must have also
1738 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001739 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001740 * This function takes ownership of the `memory_region` passed in and will free
1741 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001742 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001743struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001744 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001745 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001746 uint32_t fragment_length, uint32_t share_func,
1747 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001748{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001749 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001750 struct share_states_locked share_states;
1751 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001752
1753 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001754 * If there is an error validating the `memory_region` then we need to
1755 * free it because we own it but we won't be storing it in a share state
1756 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001757 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001758 ret = ffa_memory_send_validate(from_locked, memory_region,
1759 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001760 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001761 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001762 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001763 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001764 }
1765
Andrew Walbrana65a1322020-04-06 19:32:32 +01001766 /* Set flag for share function, ready to be retrieved later. */
1767 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001768 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001769 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001770 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001771 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001772 case FFA_MEM_LEND_32:
1773 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001774 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001775 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001776 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001777 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001778 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001779 }
1780
Andrew Walbranca808b12020-05-15 17:22:28 +01001781 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001782 /*
1783 * Allocate a share state before updating the page table. Otherwise if
1784 * updating the page table succeeded but allocating the share state
1785 * failed then it would leave the memory in a state where nobody could
1786 * get it back.
1787 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001788 if (!allocate_share_state(share_states, share_func, memory_region,
1789 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1790 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001791 dlog_verbose("Failed to allocate share state.\n");
1792 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001793 ret = ffa_error(FFA_NO_MEMORY);
1794 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001795 }
1796
Andrew Walbranca808b12020-05-15 17:22:28 +01001797 if (fragment_length == memory_share_length) {
1798 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001799 ret = ffa_memory_send_complete(
1800 from_locked, share_states, share_state, page_pool,
1801 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001802 } else {
1803 ret = (struct ffa_value){
1804 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001805 .arg1 = (uint32_t)memory_region->handle,
1806 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001807 .arg3 = fragment_length};
1808 }
1809
1810out:
1811 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001812 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001813 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001814}
1815
1816/**
J-Alves8505a8a2022-06-15 18:10:18 +01001817 * Validates a call to donate, lend or share memory to the other world and then
1818 * updates the stage-2 page tables. Specifically, check if the message length
1819 * and number of memory region constituents match, and if the transition is
1820 * valid for the type of memory sending operation.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001821 *
1822 * Assumes that the caller has already found and locked the sender VM and the
J-Alves8505a8a2022-06-15 18:10:18 +01001823 * other world VM, and copied the memory region descriptor from the sender's TX
1824 * buffer to a freshly allocated page from Hafnium's internal pool. The caller
1825 * must have also validated that the receiver VM ID is valid.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001826 *
1827 * This function takes ownership of the `memory_region` passed in and will free
1828 * it when necessary; it must not be freed by the caller.
1829 */
J-Alves8505a8a2022-06-15 18:10:18 +01001830struct ffa_value ffa_memory_other_world_send(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001831 struct vm_locked from_locked, struct vm_locked to_locked,
1832 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1833 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1834{
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001835 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001836
1837 /*
1838 * If there is an error validating the `memory_region` then we need to
1839 * free it because we own it but we won't be storing it in a share state
1840 * after all.
1841 */
1842 ret = ffa_memory_send_validate(from_locked, memory_region,
1843 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001844 share_func);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001845 if (ret.func != FFA_SUCCESS_32) {
1846 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001847 }
1848
Andrew Walbranca808b12020-05-15 17:22:28 +01001849 if (fragment_length == memory_share_length) {
1850 /* No more fragments to come, everything fit in one message. */
1851 struct ffa_composite_memory_region *composite =
1852 ffa_memory_region_get_composite(memory_region, 0);
1853 struct ffa_memory_region_constituent *constituents =
1854 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001855 struct mpool local_page_pool;
1856 uint32_t orig_from_mode;
1857
1858 /*
1859 * Use a local page pool so that we can roll back if necessary.
1860 */
1861 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001862
1863 ret = ffa_send_check_update(
1864 from_locked, &constituents,
1865 &composite->constituent_count, 1, share_func,
J-Alves363f5722022-04-25 17:37:37 +01001866 memory_region->receivers, memory_region->receiver_count,
1867 &local_page_pool,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001868 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1869 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001870 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001871 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001872 goto out;
1873 }
1874
J-Alves8505a8a2022-06-15 18:10:18 +01001875 /* Forward memory send message on to other world. */
1876 ret = memory_send_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001877 to_locked, from_locked.vm->id, share_func,
1878 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001879
1880 if (ret.func != FFA_SUCCESS_32) {
1881 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001882 "Other world didn't successfully complete "
1883 "memory send operation; returned %#x (%d). "
1884 "Rolling back.\n",
Andrew Walbran37c574e2020-06-03 11:45:46 +01001885 ret.func, ret.arg2);
1886
1887 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001888 * The other world failed to complete the send
1889 * operation, so roll back the page table update for the
1890 * VM. This can't fail because it won't try to allocate
1891 * more memory than was freed into the `local_page_pool`
1892 * by `ffa_send_check_update` in the initial update.
Andrew Walbran37c574e2020-06-03 11:45:46 +01001893 */
1894 CHECK(ffa_region_group_identity_map(
1895 from_locked, &constituents,
1896 &composite->constituent_count, 1,
1897 orig_from_mode, &local_page_pool, true));
1898 }
1899
1900 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001901 } else {
1902 struct share_states_locked share_states = share_states_lock();
1903 ffa_memory_handle_t handle;
1904
1905 /*
1906 * We need to wait for the rest of the fragments before we can
1907 * check whether the transaction is valid and unmap the memory.
J-Alves8505a8a2022-06-15 18:10:18 +01001908 * Call the other world so it can do its initial validation and
1909 * assign a handle, and allocate a share state to keep what we
1910 * have so far.
Andrew Walbranca808b12020-05-15 17:22:28 +01001911 */
J-Alves8505a8a2022-06-15 18:10:18 +01001912 ret = memory_send_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001913 to_locked, from_locked.vm->id, share_func,
1914 memory_region, memory_share_length, fragment_length);
1915 if (ret.func == FFA_ERROR_32) {
1916 goto out_unlock;
1917 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1918 dlog_warning(
J-Alves8505a8a2022-06-15 18:10:18 +01001919 "Got %#x from other world in response to %#x "
1920 "for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001921 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001922 "FFA_MEM_FRAG_RX.\n",
1923 ret.func, share_func, fragment_length,
1924 memory_share_length);
1925 ret = ffa_error(FFA_INVALID_PARAMETERS);
1926 goto out_unlock;
1927 }
1928 handle = ffa_frag_handle(ret);
1929 if (ret.arg3 != fragment_length) {
1930 dlog_warning(
1931 "Got unexpected fragment offset %d for "
J-Alves8505a8a2022-06-15 18:10:18 +01001932 "FFA_MEM_FRAG_RX from other world (expected "
1933 "%d).\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001934 ret.arg3, fragment_length);
1935 ret = ffa_error(FFA_INVALID_PARAMETERS);
1936 goto out_unlock;
1937 }
1938 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1939 dlog_warning(
1940 "Got unexpected sender ID %d for "
J-Alves8505a8a2022-06-15 18:10:18 +01001941 "FFA_MEM_FRAG_RX from other world (expected "
1942 "%d).\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001943 ffa_frag_sender(ret), from_locked.vm->id);
1944 ret = ffa_error(FFA_INVALID_PARAMETERS);
1945 goto out_unlock;
1946 }
1947
1948 if (!allocate_share_state(share_states, share_func,
1949 memory_region, fragment_length,
1950 handle, NULL)) {
1951 dlog_verbose("Failed to allocate share state.\n");
1952 ret = ffa_error(FFA_NO_MEMORY);
1953 goto out_unlock;
1954 }
1955 /*
1956 * Don't free the memory region fragment, as it has been stored
1957 * in the share state.
1958 */
1959 memory_region = NULL;
1960 out_unlock:
1961 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001962 }
1963
Andrew Walbranca808b12020-05-15 17:22:28 +01001964out:
1965 if (memory_region != NULL) {
1966 mpool_free(page_pool, memory_region);
1967 }
1968 dump_share_states();
1969 return ret;
1970}
1971
1972/**
J-Alves8505a8a2022-06-15 18:10:18 +01001973 * Continues an operation to donate, lend or share memory to a VM from current
1974 * world. If this is the last fragment then checks that the transition is valid
1975 * for the type of memory sending operation and updates the stage-2 page tables
1976 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001977 *
1978 * Assumes that the caller has already found and locked the sender VM and copied
1979 * the memory region descriptor from the sender's TX buffer to a freshly
1980 * allocated page from Hafnium's internal pool.
1981 *
1982 * This function takes ownership of the `fragment` passed in; it must not be
1983 * freed by the caller.
1984 */
1985struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1986 void *fragment,
1987 uint32_t fragment_length,
1988 ffa_memory_handle_t handle,
1989 struct mpool *page_pool)
1990{
1991 struct share_states_locked share_states = share_states_lock();
1992 struct ffa_memory_share_state *share_state;
1993 struct ffa_value ret;
1994 struct ffa_memory_region *memory_region;
1995
1996 ret = ffa_memory_send_continue_validate(share_states, handle,
1997 &share_state,
1998 from_locked.vm->id, page_pool);
1999 if (ret.func != FFA_SUCCESS_32) {
2000 goto out_free_fragment;
2001 }
2002 memory_region = share_state->memory_region;
2003
J-Alves95df0ef2022-12-07 10:09:48 +00002004 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002005 dlog_error(
2006 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002007 "other world. This should never happen, and indicates "
2008 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002009 "EL3 code.\n");
2010 ret = ffa_error(FFA_INVALID_PARAMETERS);
2011 goto out_free_fragment;
2012 }
2013
2014 /* Add this fragment. */
2015 share_state->fragments[share_state->fragment_count] = fragment;
2016 share_state->fragment_constituent_counts[share_state->fragment_count] =
2017 fragment_length / sizeof(struct ffa_memory_region_constituent);
2018 share_state->fragment_count++;
2019
2020 /* Check whether the memory send operation is now ready to complete. */
2021 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002022 ret = ffa_memory_send_complete(
2023 from_locked, share_states, share_state, page_pool,
2024 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002025 } else {
2026 ret = (struct ffa_value){
2027 .func = FFA_MEM_FRAG_RX_32,
2028 .arg1 = (uint32_t)handle,
2029 .arg2 = (uint32_t)(handle >> 32),
2030 .arg3 = share_state_next_fragment_offset(share_states,
2031 share_state)};
2032 }
2033 goto out;
2034
2035out_free_fragment:
2036 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002037
2038out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002039 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002040 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002041}
2042
Andrew Walbranca808b12020-05-15 17:22:28 +01002043/**
J-Alves8505a8a2022-06-15 18:10:18 +01002044 * Continues an operation to donate, lend or share memory to the other world VM.
2045 * If this is the last fragment then checks that the transition is valid for the
2046 * type of memory sending operation and updates the stage-2 page tables of the
2047 * sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002048 *
2049 * Assumes that the caller has already found and locked the sender VM and copied
2050 * the memory region descriptor from the sender's TX buffer to a freshly
2051 * allocated page from Hafnium's internal pool.
2052 *
2053 * This function takes ownership of the `memory_region` passed in and will free
2054 * it when necessary; it must not be freed by the caller.
2055 */
J-Alves8505a8a2022-06-15 18:10:18 +01002056struct ffa_value ffa_memory_other_world_send_continue(
2057 struct vm_locked from_locked, struct vm_locked to_locked,
2058 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
2059 struct mpool *page_pool)
Andrew Walbranca808b12020-05-15 17:22:28 +01002060{
2061 struct share_states_locked share_states = share_states_lock();
2062 struct ffa_memory_share_state *share_state;
2063 struct ffa_value ret;
2064 struct ffa_memory_region *memory_region;
2065
2066 ret = ffa_memory_send_continue_validate(share_states, handle,
2067 &share_state,
2068 from_locked.vm->id, page_pool);
2069 if (ret.func != FFA_SUCCESS_32) {
2070 goto out_free_fragment;
2071 }
2072 memory_region = share_state->memory_region;
2073
J-Alves95df0ef2022-12-07 10:09:48 +00002074 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002075 dlog_error(
J-Alves8505a8a2022-06-15 18:10:18 +01002076 "Got SPM-allocated handle for memory send to non-other "
2077 "world VM. This should never happen, and indicates a "
2078 "bug.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01002079 ret = ffa_error(FFA_INVALID_PARAMETERS);
2080 goto out_free_fragment;
2081 }
2082
2083 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2084 to_locked.vm->mailbox.recv == NULL) {
2085 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002086 * If the other world RX buffer is not available, tell the
2087 * sender to retry by returning the current offset again.
Andrew Walbranca808b12020-05-15 17:22:28 +01002088 */
2089 ret = (struct ffa_value){
2090 .func = FFA_MEM_FRAG_RX_32,
2091 .arg1 = (uint32_t)handle,
2092 .arg2 = (uint32_t)(handle >> 32),
2093 .arg3 = share_state_next_fragment_offset(share_states,
2094 share_state),
2095 };
2096 goto out_free_fragment;
2097 }
2098
2099 /* Add this fragment. */
2100 share_state->fragments[share_state->fragment_count] = fragment;
2101 share_state->fragment_constituent_counts[share_state->fragment_count] =
2102 fragment_length / sizeof(struct ffa_memory_region_constituent);
2103 share_state->fragment_count++;
2104
2105 /* Check whether the memory send operation is now ready to complete. */
2106 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002107 struct mpool local_page_pool;
2108 uint32_t orig_from_mode;
2109
2110 /*
2111 * Use a local page pool so that we can roll back if necessary.
2112 */
2113 mpool_init_with_fallback(&local_page_pool, page_pool);
2114
Andrew Walbranca808b12020-05-15 17:22:28 +01002115 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002116 share_state, &local_page_pool,
2117 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002118
2119 if (ret.func == FFA_SUCCESS_32) {
2120 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002121 * Forward final fragment on to the other world so that
Andrew Walbranca808b12020-05-15 17:22:28 +01002122 * it can complete the memory sending operation.
2123 */
J-Alves8505a8a2022-06-15 18:10:18 +01002124 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01002125 to_locked, from_locked.vm->id, fragment,
2126 fragment_length, handle);
2127
2128 if (ret.func != FFA_SUCCESS_32) {
2129 /*
2130 * The error will be passed on to the caller,
2131 * but log it here too.
2132 */
2133 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01002134 "other world didn't successfully "
2135 "complete "
Andrew Walbranca808b12020-05-15 17:22:28 +01002136 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002137 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002138 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002139
2140 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002141 * The other world failed to complete the send
Andrew Walbran37c574e2020-06-03 11:45:46 +01002142 * operation, so roll back the page table update
2143 * for the VM. This can't fail because it won't
2144 * try to allocate more memory than was freed
2145 * into the `local_page_pool` by
2146 * `ffa_send_check_update` in the initial
2147 * update.
2148 */
2149 CHECK(ffa_region_group_identity_map(
2150 from_locked, share_state->fragments,
2151 share_state
2152 ->fragment_constituent_counts,
2153 share_state->fragment_count,
2154 orig_from_mode, &local_page_pool,
2155 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002156 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002157
Andrew Walbranca808b12020-05-15 17:22:28 +01002158 /* Free share state. */
2159 share_state_free(share_states, share_state, page_pool);
2160 } else {
J-Alves8505a8a2022-06-15 18:10:18 +01002161 /* Abort sending to other world. */
2162 struct ffa_value other_world_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002163 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002164 .func = FFA_MEM_RECLAIM_32,
2165 .arg1 = (uint32_t)handle,
2166 .arg2 = (uint32_t)(handle >> 32)});
2167
J-Alves8505a8a2022-06-15 18:10:18 +01002168 if (other_world_ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002169 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002170 * Nothing we can do if other world doesn't
2171 * abort properly, just log it.
Andrew Walbranca808b12020-05-15 17:22:28 +01002172 */
2173 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01002174 "other world didn't successfully abort "
2175 "failed "
Andrew Walbranca808b12020-05-15 17:22:28 +01002176 "memory send operation; returned %#x "
2177 "(%d).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002178 other_world_ret.func,
2179 other_world_ret.arg2);
Andrew Walbranca808b12020-05-15 17:22:28 +01002180 }
2181 /*
2182 * We don't need to free the share state in this case
2183 * because ffa_memory_send_complete does that already.
2184 */
2185 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002186
2187 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002188 } else {
2189 uint32_t next_fragment_offset =
2190 share_state_next_fragment_offset(share_states,
2191 share_state);
2192
J-Alves8505a8a2022-06-15 18:10:18 +01002193 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01002194 to_locked, from_locked.vm->id, fragment,
2195 fragment_length, handle);
2196
2197 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2198 ffa_frag_handle(ret) != handle ||
2199 ret.arg3 != next_fragment_offset ||
2200 ffa_frag_sender(ret) != from_locked.vm->id) {
2201 dlog_verbose(
2202 "Got unexpected result from forwarding "
J-Alves8505a8a2022-06-15 18:10:18 +01002203 "FFA_MEM_FRAG_TX to other world. %#x (handle "
2204 "%#x, "
Andrew Walbranca808b12020-05-15 17:22:28 +01002205 "offset %d, sender %d); expected "
2206 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2207 "sender %d).\n",
2208 ret.func, ffa_frag_handle(ret), ret.arg3,
2209 ffa_frag_sender(ret), handle,
2210 next_fragment_offset, from_locked.vm->id);
2211 /* Free share state. */
2212 share_state_free(share_states, share_state, page_pool);
2213 ret = ffa_error(FFA_INVALID_PARAMETERS);
2214 goto out;
2215 }
2216
2217 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2218 .arg1 = (uint32_t)handle,
2219 .arg2 = (uint32_t)(handle >> 32),
2220 .arg3 = next_fragment_offset};
2221 }
2222 goto out;
2223
2224out_free_fragment:
2225 mpool_free(page_pool, fragment);
2226
2227out:
2228 share_states_unlock(&share_states);
2229 return ret;
2230}
2231
2232/** Clean up after the receiver has finished retrieving a memory region. */
2233static void ffa_memory_retrieve_complete(
2234 struct share_states_locked share_states,
2235 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2236{
2237 if (share_state->share_func == FFA_MEM_DONATE_32) {
2238 /*
2239 * Memory that has been donated can't be relinquished,
2240 * so no need to keep the share state around.
2241 */
2242 share_state_free(share_states, share_state, page_pool);
2243 dlog_verbose("Freed share state for donate.\n");
2244 }
2245}
2246
J-Alves96de29f2022-04-26 16:05:24 +01002247/*
2248 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2249 * returns its index in the receiver's array. If receiver's ID doesn't exist
2250 * in the array, return the region's 'receiver_count'.
2251 */
2252static uint32_t ffa_memory_region_get_receiver(
2253 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
2254{
2255 struct ffa_memory_access *receivers;
2256 uint32_t i;
2257
2258 assert(memory_region != NULL);
2259
2260 receivers = memory_region->receivers;
2261
2262 for (i = 0U; i < memory_region->receiver_count; i++) {
2263 if (receivers[i].receiver_permissions.receiver == receiver) {
2264 break;
2265 }
2266 }
2267
2268 return i;
2269}
2270
2271/**
2272 * Validates the retrieved permissions against those specified by the lender
2273 * of memory share operation. Optionally can help set the permissions to be used
2274 * for the S2 mapping, through the `permissions` argument.
2275 * Returns true if permissions are valid, false otherwise.
2276 */
2277static bool ffa_memory_retrieve_is_memory_access_valid(
2278 enum ffa_data_access sent_data_access,
2279 enum ffa_data_access requested_data_access,
2280 enum ffa_instruction_access sent_instruction_access,
2281 enum ffa_instruction_access requested_instruction_access,
2282 ffa_memory_access_permissions_t *permissions)
2283{
2284 switch (sent_data_access) {
2285 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2286 case FFA_DATA_ACCESS_RW:
2287 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2288 requested_data_access == FFA_DATA_ACCESS_RW) {
2289 if (permissions != NULL) {
2290 ffa_set_data_access_attr(permissions,
2291 FFA_DATA_ACCESS_RW);
2292 }
2293 break;
2294 }
2295 /* Intentional fall-through. */
2296 case FFA_DATA_ACCESS_RO:
2297 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2298 requested_data_access == FFA_DATA_ACCESS_RO) {
2299 if (permissions != NULL) {
2300 ffa_set_data_access_attr(permissions,
2301 FFA_DATA_ACCESS_RO);
2302 }
2303 break;
2304 }
2305 dlog_verbose(
2306 "Invalid data access requested; sender specified "
2307 "permissions %#x but receiver requested %#x.\n",
2308 sent_data_access, requested_data_access);
2309 return false;
2310 case FFA_DATA_ACCESS_RESERVED:
2311 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2312 "checked before this point.");
2313 }
2314
2315 switch (sent_instruction_access) {
2316 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2317 case FFA_INSTRUCTION_ACCESS_X:
2318 if (requested_instruction_access ==
2319 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2320 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2321 if (permissions != NULL) {
2322 ffa_set_instruction_access_attr(
2323 permissions, FFA_INSTRUCTION_ACCESS_X);
2324 }
2325 break;
2326 }
2327 case FFA_INSTRUCTION_ACCESS_NX:
2328 if (requested_instruction_access ==
2329 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2330 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2331 if (permissions != NULL) {
2332 ffa_set_instruction_access_attr(
2333 permissions, FFA_INSTRUCTION_ACCESS_NX);
2334 }
2335 break;
2336 }
2337 dlog_verbose(
2338 "Invalid instruction access requested; sender "
2339 "specified permissions %#x but receiver requested "
2340 "%#x.\n",
2341 sent_instruction_access, requested_instruction_access);
2342 return false;
2343 case FFA_INSTRUCTION_ACCESS_RESERVED:
2344 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2345 "be checked before this point.");
2346 }
2347
2348 return true;
2349}
2350
2351/**
2352 * Validate the receivers' permissions in the retrieve request against those
2353 * specified by the lender.
2354 * In the `permissions` argument returns the permissions to set at S2 for the
2355 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2356 * Returns FFA_SUCCESS if all specified permissions are valid.
2357 */
2358static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2359 struct ffa_memory_region *memory_region,
2360 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2361 ffa_memory_access_permissions_t *permissions)
2362{
2363 uint32_t retrieve_receiver_index;
2364
2365 assert(permissions != NULL);
2366
2367 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2368 dlog_verbose(
2369 "Retrieve request should contain same list of "
2370 "borrowers, as specified by the lender.\n");
2371 return ffa_error(FFA_INVALID_PARAMETERS);
2372 }
2373
2374 retrieve_receiver_index = retrieve_request->receiver_count;
2375
2376 /* Should be populated with the permissions of the retriever. */
2377 *permissions = 0;
2378
2379 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2380 ffa_memory_access_permissions_t sent_permissions;
2381 struct ffa_memory_access *current_receiver =
2382 &retrieve_request->receivers[i];
2383 ffa_memory_access_permissions_t requested_permissions =
2384 current_receiver->receiver_permissions.permissions;
2385 ffa_vm_id_t current_receiver_id =
2386 current_receiver->receiver_permissions.receiver;
2387 bool found_to_id = current_receiver_id == to_vm_id;
2388
2389 /*
2390 * Find the current receiver in the transaction descriptor from
2391 * sender.
2392 */
2393 uint32_t mem_region_receiver_index =
2394 ffa_memory_region_get_receiver(memory_region,
2395 current_receiver_id);
2396
2397 if (mem_region_receiver_index ==
2398 memory_region->receiver_count) {
2399 dlog_verbose("%s: receiver %x not found\n", __func__,
2400 current_receiver_id);
2401 return ffa_error(FFA_DENIED);
2402 }
2403
2404 sent_permissions =
2405 memory_region->receivers[mem_region_receiver_index]
2406 .receiver_permissions.permissions;
2407
2408 if (found_to_id) {
2409 retrieve_receiver_index = i;
2410 }
2411
2412 /*
2413 * Since we are traversing the list of receivers, save the index
2414 * of the caller. As it needs to be there.
2415 */
2416
2417 if (current_receiver->composite_memory_region_offset != 0U) {
2418 dlog_verbose(
2419 "Retriever specified address ranges not "
2420 "supported (got offset %d).\n",
2421 current_receiver
2422 ->composite_memory_region_offset);
2423 return ffa_error(FFA_INVALID_PARAMETERS);
2424 }
2425
2426 /*
2427 * Check permissions from sender against permissions requested
2428 * by receiver.
2429 */
2430 if (!ffa_memory_retrieve_is_memory_access_valid(
2431 ffa_get_data_access_attr(sent_permissions),
2432 ffa_get_data_access_attr(requested_permissions),
2433 ffa_get_instruction_access_attr(sent_permissions),
2434 ffa_get_instruction_access_attr(
2435 requested_permissions),
2436 found_to_id ? permissions : NULL)) {
2437 return ffa_error(FFA_DENIED);
2438 }
2439
2440 /*
2441 * Can't request PM to clear memory if only provided with RO
2442 * permissions.
2443 */
2444 if (found_to_id &&
2445 (ffa_get_data_access_attr(*permissions) ==
2446 FFA_DATA_ACCESS_RO) &&
2447 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2448 0U) {
2449 dlog_verbose(
2450 "Receiver has RO permissions can not request "
2451 "clear.\n");
2452 return ffa_error(FFA_DENIED);
2453 }
2454 }
2455
2456 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2457 dlog_verbose(
2458 "Retrieve request does not contain caller's (%x) "
2459 "permissions\n",
2460 to_vm_id);
2461 return ffa_error(FFA_INVALID_PARAMETERS);
2462 }
2463
2464 return (struct ffa_value){.func = FFA_SUCCESS_32};
2465}
2466
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002467struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2468 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002469 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002470 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002472 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002473 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002474 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002475 sizeof(struct ffa_memory_access);
2476 ffa_memory_handle_t handle = retrieve_request->handle;
2477 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002478 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002479 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2480 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002481 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002482 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002484 struct ffa_memory_share_state *share_state;
2485 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002486 struct ffa_composite_memory_region *composite;
2487 uint32_t total_length;
2488 uint32_t fragment_length;
J-Alves96de29f2022-04-26 16:05:24 +01002489 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002490
2491 dump_share_states();
2492
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002493 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002494 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002495 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002496 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002497 expected_retrieve_request_length,
2498 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002499 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002500 }
2501
2502 share_states = share_states_lock();
2503 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002504 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002505 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002506 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507 goto out;
2508 }
2509
J-Alves96de29f2022-04-26 16:05:24 +01002510 if (!share_state->sending_complete) {
2511 dlog_verbose(
2512 "Memory with handle %#x not fully sent, can't "
2513 "retrieve.\n",
2514 handle);
2515 ret = ffa_error(FFA_INVALID_PARAMETERS);
2516 goto out;
2517 }
2518
Andrew Walbrana65a1322020-04-06 19:32:32 +01002519 memory_region = share_state->memory_region;
2520 CHECK(memory_region != NULL);
2521
2522 /*
J-Alves96de29f2022-04-26 16:05:24 +01002523 * Find receiver index in the receivers list specified by the sender.
2524 */
2525 receiver_index =
2526 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2527
2528 if (receiver_index == memory_region->receiver_count) {
2529 dlog_verbose(
2530 "Incorrect receiver VM ID %x for FFA_MEM_RETRIEVE_REQ, "
2531 "for handle %#x.\n",
2532 to_locked.vm->id, handle);
2533 ret = ffa_error(FFA_INVALID_PARAMETERS);
2534 goto out;
2535 }
2536
2537 if (share_state->retrieved_fragment_count[receiver_index] != 0U) {
2538 dlog_verbose("Memory with handle %#x already retrieved.\n",
2539 handle);
2540 ret = ffa_error(FFA_DENIED);
2541 goto out;
2542 }
2543
2544 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002545 * Check that the transaction type expected by the receiver is correct,
2546 * if it has been specified.
2547 */
2548 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002549 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002550 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 dlog_verbose(
2553 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002554 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002555 transaction_type,
2556 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002557 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 goto out;
2561 }
2562
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002563 if (retrieve_request->sender != memory_region->sender) {
2564 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002565 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002566 "expected %d for handle %#x.\n",
2567 retrieve_request->sender, memory_region->sender,
2568 handle);
J-Alves040c4ef2022-05-13 14:42:49 +01002569 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002570 goto out;
2571 }
2572
2573 if (retrieve_request->tag != memory_region->tag) {
2574 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002575 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002576 "%d for handle %#x.\n",
2577 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002578 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002579 goto out;
2580 }
2581
Federico Recanati85090c42021-12-15 13:17:54 +01002582 if ((retrieve_request->flags &
J-Alves96de29f2022-04-26 16:05:24 +01002583 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
Federico Recanati85090c42021-12-15 13:17:54 +01002584 dlog_verbose(
2585 "Retriever specified 'address range alignment hint'"
2586 " not supported.\n");
2587 ret = ffa_error(FFA_INVALID_PARAMETERS);
2588 goto out;
2589 }
2590 if ((retrieve_request->flags &
2591 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2592 dlog_verbose(
2593 "Bits 8-5 must be zero in memory region's flags "
2594 "(address range alignment hint not supported).\n");
2595 ret = ffa_error(FFA_INVALID_PARAMETERS);
2596 goto out;
2597 }
2598
J-Alves84658fc2021-06-17 14:37:32 +01002599 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2600 dlog_verbose(
2601 "Bits 31-10 must be zero in memory region's flags.\n");
2602 ret = ffa_error(FFA_INVALID_PARAMETERS);
2603 goto out;
2604 }
2605
2606 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2607 (retrieve_request->flags &
2608 (FFA_MEMORY_REGION_FLAG_CLEAR |
2609 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2610 dlog_verbose(
2611 "Memory Share operation can't clean after relinquish "
2612 "memory region.\n");
2613 ret = ffa_error(FFA_INVALID_PARAMETERS);
2614 goto out;
2615 }
2616
Andrew Walbrana65a1322020-04-06 19:32:32 +01002617 /*
J-Alves17c069c2021-12-07 16:00:38 +00002618 * If the borrower needs the memory to be cleared before mapping to its
2619 * address space, the sender should have set the flag when calling
2620 * FFA_MEM_LEND/FFA_MEM_DONATE, else return FFA_DENIED.
2621 */
2622 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
J-Alves96de29f2022-04-26 16:05:24 +01002623 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
J-Alves17c069c2021-12-07 16:00:38 +00002624 dlog_verbose(
2625 "Borrower needs memory cleared. Sender needs to set "
2626 "flag for clearing memory.\n");
2627 ret = ffa_error(FFA_DENIED);
2628 goto out;
2629 }
2630
J-Alves96de29f2022-04-26 16:05:24 +01002631 ret = ffa_memory_retrieve_validate_memory_access_list(
2632 memory_region, retrieve_request, to_locked.vm->id,
2633 &permissions);
2634 if (ret.func != FFA_SUCCESS_32) {
J-Alves84658fc2021-06-17 14:37:32 +01002635 goto out;
2636 }
2637
J-Alves614d9f42022-06-28 14:03:10 +01002638 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2639 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2640 /*
2641 * Ensure receiver's attributes are compatible with how Hafnium
2642 * maps memory: Normal Memory, Inner shareable, Write-Back
2643 * Read-Allocate Write-Allocate Cacheable.
2644 */
2645 ret = ffa_memory_attributes_validate(
2646 retrieve_request->attributes);
2647 if (ret.func != FFA_SUCCESS_32) {
2648 goto out;
2649 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002650 }
2651
J-Alves7cd5eb32020-10-16 19:06:10 +01002652 memory_to_attributes = ffa_memory_permissions_to_mode(
2653 permissions, share_state->sender_orig_mode);
Andrew Walbran996d1d12020-05-27 14:08:43 +01002654 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002655 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002656 share_state->fragment_constituent_counts,
2657 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002658 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002659 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002660 goto out;
2661 }
2662
2663 /*
2664 * Copy response to RX buffer of caller and deliver the message. This
2665 * must be done before the share_state is (possibly) freed.
2666 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002667 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002668 composite = ffa_memory_region_get_composite(memory_region, 0);
2669 /*
2670 * Constituents which we received in the first fragment should always
2671 * fit in the first fragment we are sending, because the header is the
2672 * same size in both cases and we have a fixed message buffer size. So
2673 * `ffa_retrieved_memory_region_init` should never fail.
2674 */
2675 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002676 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2677 memory_region->sender, memory_region->attributes,
2678 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002679 composite->page_count, composite->constituent_count,
2680 share_state->fragments[0],
2681 share_state->fragment_constituent_counts[0], &total_length,
2682 &fragment_length));
2683 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002685 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2687
J-Alves96de29f2022-04-26 16:05:24 +01002688 share_state->retrieved_fragment_count[receiver_index] = 1;
2689 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002690 share_state->fragment_count) {
2691 ffa_memory_retrieve_complete(share_states, share_state,
2692 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002693 }
2694
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002695 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002696 .arg1 = total_length,
2697 .arg2 = fragment_length};
2698
2699out:
2700 share_states_unlock(&share_states);
2701 dump_share_states();
2702 return ret;
2703}
2704
2705struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2706 ffa_memory_handle_t handle,
2707 uint32_t fragment_offset,
2708 struct mpool *page_pool)
2709{
2710 struct ffa_memory_region *memory_region;
2711 struct share_states_locked share_states;
2712 struct ffa_memory_share_state *share_state;
2713 struct ffa_value ret;
2714 uint32_t fragment_index;
2715 uint32_t retrieved_constituents_count;
2716 uint32_t i;
2717 uint32_t expected_fragment_offset;
2718 uint32_t remaining_constituent_count;
2719 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002720 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002721
2722 dump_share_states();
2723
2724 share_states = share_states_lock();
2725 if (!get_share_state(share_states, handle, &share_state)) {
2726 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2727 handle);
2728 ret = ffa_error(FFA_INVALID_PARAMETERS);
2729 goto out;
2730 }
2731
2732 memory_region = share_state->memory_region;
2733 CHECK(memory_region != NULL);
2734
J-Alvesc7484f12022-05-13 12:41:14 +01002735 receiver_index =
2736 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2737
2738 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002739 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002740 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2741 "memory sharing transaction (%x)\n",
2742 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002743 ret = ffa_error(FFA_INVALID_PARAMETERS);
2744 goto out;
2745 }
2746
2747 if (!share_state->sending_complete) {
2748 dlog_verbose(
2749 "Memory with handle %#x not fully sent, can't "
2750 "retrieve.\n",
2751 handle);
2752 ret = ffa_error(FFA_INVALID_PARAMETERS);
2753 goto out;
2754 }
2755
J-Alvesc7484f12022-05-13 12:41:14 +01002756 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2757 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002758 share_state->fragment_count) {
2759 dlog_verbose(
2760 "Retrieval of memory with handle %#x not yet started "
2761 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002762 handle,
2763 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002764 share_state->fragment_count);
2765 ret = ffa_error(FFA_INVALID_PARAMETERS);
2766 goto out;
2767 }
2768
J-Alvesc7484f12022-05-13 12:41:14 +01002769 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002770
2771 /*
2772 * Check that the given fragment offset is correct by counting how many
2773 * constituents were in the fragments previously sent.
2774 */
2775 retrieved_constituents_count = 0;
2776 for (i = 0; i < fragment_index; ++i) {
2777 retrieved_constituents_count +=
2778 share_state->fragment_constituent_counts[i];
2779 }
J-Alvesc7484f12022-05-13 12:41:14 +01002780
2781 CHECK(memory_region->receiver_count > 0);
2782
Andrew Walbranca808b12020-05-15 17:22:28 +01002783 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002784 ffa_composite_constituent_offset(memory_region,
2785 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002786 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002787 sizeof(struct ffa_memory_region_constituent) -
2788 sizeof(struct ffa_memory_access) *
2789 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002790 if (fragment_offset != expected_fragment_offset) {
2791 dlog_verbose("Fragment offset was %d but expected %d.\n",
2792 fragment_offset, expected_fragment_offset);
2793 ret = ffa_error(FFA_INVALID_PARAMETERS);
2794 goto out;
2795 }
2796
2797 remaining_constituent_count = ffa_memory_fragment_init(
2798 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2799 share_state->fragments[fragment_index],
2800 share_state->fragment_constituent_counts[fragment_index],
2801 &fragment_length);
2802 CHECK(remaining_constituent_count == 0);
2803 to_locked.vm->mailbox.recv_size = fragment_length;
2804 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2805 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2806 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002807 share_state->retrieved_fragment_count[receiver_index]++;
2808 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002809 share_state->fragment_count) {
2810 ffa_memory_retrieve_complete(share_states, share_state,
2811 page_pool);
2812 }
2813
2814 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2815 .arg1 = (uint32_t)handle,
2816 .arg2 = (uint32_t)(handle >> 32),
2817 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002818
2819out:
2820 share_states_unlock(&share_states);
2821 dump_share_states();
2822 return ret;
2823}
2824
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002825struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002826 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002827 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002828{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002829 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002830 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002831 struct ffa_memory_share_state *share_state;
2832 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002833 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002834 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002835 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002836
Andrew Walbrana65a1322020-04-06 19:32:32 +01002837 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002838 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002839 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002840 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002841 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002842 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002843 }
2844
Andrew Walbrana65a1322020-04-06 19:32:32 +01002845 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002846 dlog_verbose(
2847 "VM ID %d in relinquish message doesn't match calling "
2848 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002849 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002850 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002851 }
2852
2853 dump_share_states();
2854
2855 share_states = share_states_lock();
2856 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002857 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002858 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002859 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002860 goto out;
2861 }
2862
Andrew Walbranca808b12020-05-15 17:22:28 +01002863 if (!share_state->sending_complete) {
2864 dlog_verbose(
2865 "Memory with handle %#x not fully sent, can't "
2866 "relinquish.\n",
2867 handle);
2868 ret = ffa_error(FFA_INVALID_PARAMETERS);
2869 goto out;
2870 }
2871
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002872 memory_region = share_state->memory_region;
2873 CHECK(memory_region != NULL);
2874
J-Alves8eb19162022-04-28 10:56:48 +01002875 receiver_index = ffa_memory_region_get_receiver(memory_region,
2876 from_locked.vm->id);
2877
2878 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002879 dlog_verbose(
2880 "VM ID %d tried to relinquish memory region with "
J-Alves8eb19162022-04-28 10:56:48 +01002881 "handle %#x and it is not a valid borrower.\n",
2882 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002883 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002884 goto out;
2885 }
2886
J-Alves8eb19162022-04-28 10:56:48 +01002887 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002888 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002889 dlog_verbose(
J-Alves8eb19162022-04-28 10:56:48 +01002890 "Memory with handle %#x not yet fully retrieved, "
2891 "receiver %x can't relinquish.\n",
2892 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002893 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002894 goto out;
2895 }
2896
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002897 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002898
2899 /*
2900 * Clear is not allowed for memory that was shared, as the original
2901 * sender still has access to the memory.
2902 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002903 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002904 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002905 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002906 goto out;
2907 }
2908
Andrew Walbranca808b12020-05-15 17:22:28 +01002909 ret = ffa_relinquish_check_update(
2910 from_locked, share_state->fragments,
2911 share_state->fragment_constituent_counts,
2912 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002913
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002914 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915 /*
2916 * Mark memory handle as not retrieved, so it can be reclaimed
2917 * (or retrieved again).
2918 */
J-Alves8eb19162022-04-28 10:56:48 +01002919 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002920 }
2921
2922out:
2923 share_states_unlock(&share_states);
2924 dump_share_states();
2925 return ret;
2926}
2927
2928/**
2929 * Validates that the reclaim transition is allowed for the given handle,
2930 * updates the page table of the reclaiming VM, and frees the internal state
2931 * associated with the handle.
2932 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002933struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002934 ffa_memory_handle_t handle,
2935 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002936 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002937{
2938 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002939 struct ffa_memory_share_state *share_state;
2940 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002941 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002942
2943 dump_share_states();
2944
2945 share_states = share_states_lock();
2946 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002947 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002948 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002949 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002950 goto out;
2951 }
2952
2953 memory_region = share_state->memory_region;
2954 CHECK(memory_region != NULL);
2955
2956 if (to_locked.vm->id != memory_region->sender) {
2957 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002958 "VM %#x attempted to reclaim memory handle %#x "
2959 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002960 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002961 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002962 goto out;
2963 }
2964
Andrew Walbranca808b12020-05-15 17:22:28 +01002965 if (!share_state->sending_complete) {
2966 dlog_verbose(
2967 "Memory with handle %#x not fully sent, can't "
2968 "reclaim.\n",
2969 handle);
2970 ret = ffa_error(FFA_INVALID_PARAMETERS);
2971 goto out;
2972 }
2973
J-Alves752236c2022-04-28 11:07:47 +01002974 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2975 if (share_state->retrieved_fragment_count[i] != 0) {
2976 dlog_verbose(
2977 "Tried to reclaim memory handle %#x that has "
2978 "not been relinquished by all borrowers(%x).\n",
2979 handle,
2980 memory_region->receivers[i]
2981 .receiver_permissions.receiver);
2982 ret = ffa_error(FFA_DENIED);
2983 goto out;
2984 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002985 }
2986
Andrew Walbranca808b12020-05-15 17:22:28 +01002987 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002988 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002989 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002990 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002991 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002992
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002993 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002994 share_state_free(share_states, share_state, page_pool);
2995 dlog_verbose("Freed share state after successful reclaim.\n");
2996 }
2997
2998out:
2999 share_states_unlock(&share_states);
3000 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003001}
Andrew Walbran290b0c92020-02-03 16:37:14 +00003002
3003/**
Andrew Walbranca808b12020-05-15 17:22:28 +01003004 * Validates that the reclaim transition is allowed for the memory region with
J-Alves8505a8a2022-06-15 18:10:18 +01003005 * the given handle which was previously shared with the other world. tells the
3006 * other world to mark it as reclaimed, and updates the page table of the
3007 * reclaiming VM.
Andrew Walbranca808b12020-05-15 17:22:28 +01003008 *
J-Alves8505a8a2022-06-15 18:10:18 +01003009 * To do this information about the memory region is first fetched from the
3010 * other world.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003011 */
J-Alves8505a8a2022-06-15 18:10:18 +01003012struct ffa_value ffa_memory_other_world_reclaim(struct vm_locked to_locked,
3013 struct vm_locked from_locked,
3014 ffa_memory_handle_t handle,
3015 ffa_memory_region_flags_t flags,
3016 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00003017{
Andrew Walbranca808b12020-05-15 17:22:28 +01003018 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
3019 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
J-Alves8505a8a2022-06-15 18:10:18 +01003020 struct ffa_value other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003021 uint32_t length;
3022 uint32_t fragment_length;
3023 uint32_t fragment_offset;
3024 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003025 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01003026 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
3027
3028 CHECK(request_length <= HF_MAILBOX_SIZE);
J-Alves8505a8a2022-06-15 18:10:18 +01003029 CHECK(from_locked.vm->id == HF_OTHER_WORLD_ID);
Andrew Walbranca808b12020-05-15 17:22:28 +01003030
J-Alves8505a8a2022-06-15 18:10:18 +01003031 /* Retrieve memory region information from the other world. */
3032 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003033 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
3034 .arg1 = request_length,
3035 .arg2 = request_length});
J-Alves8505a8a2022-06-15 18:10:18 +01003036 if (other_world_ret.func == FFA_ERROR_32) {
3037 dlog_verbose("Got error %d from EL3.\n", other_world_ret.arg2);
3038 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003039 }
J-Alves8505a8a2022-06-15 18:10:18 +01003040 if (other_world_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003041 dlog_verbose(
3042 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003043 other_world_ret.func);
Andrew Walbranca808b12020-05-15 17:22:28 +01003044 return ffa_error(FFA_INVALID_PARAMETERS);
3045 }
3046
J-Alves8505a8a2022-06-15 18:10:18 +01003047 length = other_world_ret.arg1;
3048 fragment_length = other_world_ret.arg2;
Andrew Walbranca808b12020-05-15 17:22:28 +01003049
3050 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
J-Alves8505a8a2022-06-15 18:10:18 +01003051 length > sizeof(other_world_retrieve_buffer)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003052 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
3053 fragment_length, length, HF_MAILBOX_SIZE,
J-Alves8505a8a2022-06-15 18:10:18 +01003054 sizeof(other_world_retrieve_buffer));
Andrew Walbranca808b12020-05-15 17:22:28 +01003055 return ffa_error(FFA_INVALID_PARAMETERS);
3056 }
3057
3058 /*
3059 * Copy the first fragment of the memory region descriptor to an
3060 * internal buffer.
3061 */
J-Alves8505a8a2022-06-15 18:10:18 +01003062 memcpy_s(other_world_retrieve_buffer,
3063 sizeof(other_world_retrieve_buffer),
Andrew Walbranca808b12020-05-15 17:22:28 +01003064 from_locked.vm->mailbox.send, fragment_length);
3065
3066 /* Fetch the remaining fragments into the same buffer. */
3067 fragment_offset = fragment_length;
3068 while (fragment_offset < length) {
J-Alves8505a8a2022-06-15 18:10:18 +01003069 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003070 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
3071 .arg1 = (uint32_t)handle,
3072 .arg2 = (uint32_t)(handle >> 32),
3073 .arg3 = fragment_offset});
J-Alves8505a8a2022-06-15 18:10:18 +01003074 if (other_world_ret.func != FFA_MEM_FRAG_TX_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003075 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01003076 "Got %#x (%d) from other world in response to "
Andrew Walbranca808b12020-05-15 17:22:28 +01003077 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003078 other_world_ret.func, other_world_ret.arg2);
3079 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003080 }
J-Alves8505a8a2022-06-15 18:10:18 +01003081 if (ffa_frag_handle(other_world_ret) != handle) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003082 dlog_verbose(
3083 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
3084 "in response to FFA_MEM_FRAG_RX for handle "
3085 "%#x.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003086 ffa_frag_handle(other_world_ret), handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01003087 return ffa_error(FFA_INVALID_PARAMETERS);
3088 }
J-Alves8505a8a2022-06-15 18:10:18 +01003089 if (ffa_frag_sender(other_world_ret) != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003090 dlog_verbose(
3091 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
3092 "(expected 0).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003093 ffa_frag_sender(other_world_ret));
Andrew Walbranca808b12020-05-15 17:22:28 +01003094 return ffa_error(FFA_INVALID_PARAMETERS);
3095 }
J-Alves8505a8a2022-06-15 18:10:18 +01003096 fragment_length = other_world_ret.arg3;
Andrew Walbranca808b12020-05-15 17:22:28 +01003097 if (fragment_length > HF_MAILBOX_SIZE ||
3098 fragment_offset + fragment_length > length) {
3099 dlog_verbose(
3100 "Invalid fragment length %d at offset %d (max "
3101 "%d).\n",
3102 fragment_length, fragment_offset,
3103 HF_MAILBOX_SIZE);
3104 return ffa_error(FFA_INVALID_PARAMETERS);
3105 }
J-Alves8505a8a2022-06-15 18:10:18 +01003106 memcpy_s(other_world_retrieve_buffer + fragment_offset,
3107 sizeof(other_world_retrieve_buffer) - fragment_offset,
Andrew Walbranca808b12020-05-15 17:22:28 +01003108 from_locked.vm->mailbox.send, fragment_length);
3109
3110 fragment_offset += fragment_length;
3111 }
3112
J-Alves8505a8a2022-06-15 18:10:18 +01003113 memory_region = (struct ffa_memory_region *)other_world_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00003114
3115 if (memory_region->receiver_count != 1) {
3116 /* Only one receiver supported by Hafnium for now. */
3117 dlog_verbose(
3118 "Multiple recipients not supported (got %d, expected "
3119 "1).\n",
3120 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01003121 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003122 }
3123
3124 if (memory_region->handle != handle) {
3125 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01003126 "Got memory region handle %#x from other world but "
3127 "requested "
Andrew Walbran290b0c92020-02-03 16:37:14 +00003128 "handle %#x.\n",
3129 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003130 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003131 }
3132
3133 /* The original sender must match the caller. */
3134 if (to_locked.vm->id != memory_region->sender) {
3135 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003136 "VM %#x attempted to reclaim memory handle %#x "
3137 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003138 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003139 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003140 }
3141
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003142 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003143
3144 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01003145 * Validate that the reclaim transition is allowed for the given memory
J-Alves8505a8a2022-06-15 18:10:18 +01003146 * region, forward the request to the other world and then map the
3147 * memory back into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003148 */
J-Alves8505a8a2022-06-15 18:10:18 +01003149 return ffa_other_world_reclaim_check_update(
Andrew Walbran996d1d12020-05-27 14:08:43 +01003150 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01003151 composite->constituent_count, memory_to_attributes,
3152 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003153}