blob: 62e219aebb227736fc620a6fcbd08f7455a40aa8 [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"
J-Alves66652252022-07-06 09:49:51 +010020#include "hf/ffa_memory_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000021#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000023#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010024
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000025/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010026 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000027 * by this lock.
28 */
29static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000031
32/**
J-Alves917d2f22020-10-30 18:39:30 +000033 * Extracts the index from a memory handle allocated by Hafnium's current world.
34 */
35uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
36{
37 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
38}
39
40/**
Andrew Walbranca808b12020-05-15 17:22:28 +010041 * Initialises the next available `struct ffa_memory_share_state` and sets
42 * `share_state_ret` to a pointer to it. If `handle` is
43 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
44 * uses the provided handle which is assumed to be globally unique.
45 *
46 * Returns true on success or false if none are available.
47 */
J-Alves66652252022-07-06 09:49:51 +010048bool allocate_share_state(struct share_states_locked share_states,
49 uint32_t share_func,
50 struct ffa_memory_region *memory_region,
51 uint32_t fragment_length, ffa_memory_handle_t handle,
52 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000053{
Andrew Walbrana65a1322020-04-06 19:32:32 +010054 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000055
Daniel Boulbya2f8c662021-11-26 17:52:53 +000056 assert(share_states.share_states != NULL);
57 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000058
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010060 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010062 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010063 &share_states.share_states[i];
64 struct ffa_composite_memory_region *composite =
65 ffa_memory_region_get_composite(memory_region,
66 0);
67
68 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000069 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020070 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010071 } else {
J-Alvesee68c542020-10-29 17:48:20 +000072 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010073 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 allocated_state->share_func = share_func;
75 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010076 allocated_state->fragment_count = 1;
77 allocated_state->fragments[0] = composite->constituents;
78 allocated_state->fragment_constituent_counts[0] =
79 (fragment_length -
80 ffa_composite_constituent_offset(memory_region,
81 0)) /
82 sizeof(struct ffa_memory_region_constituent);
83 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000084 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010085 allocated_state->retrieved_fragment_count[j] =
86 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000087 }
Andrew Walbranca808b12020-05-15 17:22:28 +010088 if (share_state_ret != NULL) {
89 *share_state_ret = allocated_state;
90 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091 return true;
92 }
93 }
94
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000095 return false;
96}
97
98/** Locks the share states lock. */
99struct share_states_locked share_states_lock(void)
100{
101 sl_lock(&share_states_lock_instance);
102
103 return (struct share_states_locked){.share_states = share_states};
104}
105
106/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100107void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000109 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 share_states->share_states = NULL;
111 sl_unlock(&share_states_lock_instance);
112}
113
114/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100115 * If the given handle is a valid handle for an allocated share state then
116 * initialises `share_state_ret` to point to the share state and returns true.
117 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000118 */
J-Alvesfdd29272022-07-19 13:16:31 +0100119bool get_share_state(struct share_states_locked share_states,
120 ffa_memory_handle_t handle,
121 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100123 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000124 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000125
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000126 assert(share_states.share_states != NULL);
127 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100128
129 /*
130 * First look for a share_state allocated by us, in which case the
131 * handle is based on the index.
132 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200133 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000134 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 if (index < MAX_MEM_SHARES) {
136 share_state = &share_states.share_states[index];
137 if (share_state->share_func != 0) {
138 *share_state_ret = share_state;
139 return true;
140 }
141 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 }
143
Andrew Walbranca808b12020-05-15 17:22:28 +0100144 /* Fall back to a linear scan. */
145 for (index = 0; index < MAX_MEM_SHARES; ++index) {
146 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000147 if (share_state->memory_region != NULL &&
148 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100149 share_state->share_func != 0) {
150 *share_state_ret = share_state;
151 return true;
152 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153 }
154
Andrew Walbranca808b12020-05-15 17:22:28 +0100155 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000156}
157
158/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100159void share_state_free(struct share_states_locked share_states,
160 struct ffa_memory_share_state *share_state,
161 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000162{
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 uint32_t i;
164
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000165 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000166 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 /*
170 * First fragment is part of the same page as the `memory_region`, so it
171 * doesn't need to be freed separately.
172 */
173 share_state->fragments[0] = NULL;
174 share_state->fragment_constituent_counts[0] = 0;
175 for (i = 1; i < share_state->fragment_count; ++i) {
176 mpool_free(page_pool, share_state->fragments[i]);
177 share_state->fragments[i] = NULL;
178 share_state->fragment_constituent_counts[i] = 0;
179 }
180 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100182 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
Andrew Walbranca808b12020-05-15 17:22:28 +0100185/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186bool share_state_sending_complete(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188{
Andrew Walbranca808b12020-05-15 17:22:28 +0100189 struct ffa_composite_memory_region *composite;
190 uint32_t expected_constituent_count;
191 uint32_t fragment_constituent_count_total = 0;
192 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000195 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196
197 /*
198 * Share state must already be valid, or it's not possible to get hold
199 * of it.
200 */
201 CHECK(share_state->memory_region != NULL &&
202 share_state->share_func != 0);
203
204 composite =
205 ffa_memory_region_get_composite(share_state->memory_region, 0);
206 expected_constituent_count = composite->constituent_count;
207 for (i = 0; i < share_state->fragment_count; ++i) {
208 fragment_constituent_count_total +=
209 share_state->fragment_constituent_counts[i];
210 }
211 dlog_verbose(
212 "Checking completion: constituent count %d/%d from %d "
213 "fragments.\n",
214 fragment_constituent_count_total, expected_constituent_count,
215 share_state->fragment_count);
216
217 return fragment_constituent_count_total == expected_constituent_count;
218}
219
220/**
221 * Calculates the offset of the next fragment expected for the given share
222 * state.
223 */
J-Alvesfdd29272022-07-19 13:16:31 +0100224uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100225 struct share_states_locked share_states,
226 struct ffa_memory_share_state *share_state)
227{
228 uint32_t next_fragment_offset;
229 uint32_t i;
230
231 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000232 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100233
234 next_fragment_offset =
235 ffa_composite_constituent_offset(share_state->memory_region, 0);
236 for (i = 0; i < share_state->fragment_count; ++i) {
237 next_fragment_offset +=
238 share_state->fragment_constituent_counts[i] *
239 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000240 }
241
Andrew Walbranca808b12020-05-15 17:22:28 +0100242 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000243}
244
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246{
247 uint32_t i;
248
249 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
250 return;
251 }
252
Olivier Deprez935e1b12020-12-22 18:01:29 +0100253 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100254 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100255 "recipients [",
256 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100257 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100258 memory_region->receiver_count);
259 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 if (i != 0) {
261 dlog(", ");
262 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100263 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100264 memory_region->receivers[i].receiver_permissions.receiver,
265 memory_region->receivers[i]
266 .receiver_permissions.permissions,
267 memory_region->receivers[i]
268 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269 }
270 dlog("]");
271}
272
J-Alves66652252022-07-06 09:49:51 +0100273void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274{
275 uint32_t i;
276
277 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
278 return;
279 }
280
281 dlog("Current share states:\n");
282 sl_lock(&share_states_lock_instance);
283 for (i = 0; i < MAX_MEM_SHARES; ++i) {
284 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000285 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100286 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000287 dlog("SHARE");
288 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100289 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000290 dlog("LEND");
291 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000293 dlog("DONATE");
294 break;
295 default:
296 dlog("invalid share_func %#x",
297 share_states[i].share_func);
298 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100299 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000300 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100301 if (share_states[i].sending_complete) {
302 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000303 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100304 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305 }
J-Alves2a0d2882020-10-29 14:49:50 +0000306 dlog(" with %d fragments, %d retrieved, "
307 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100308 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000309 share_states[i].retrieved_fragment_count[0],
310 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000311 }
312 }
313 sl_unlock(&share_states_lock_instance);
314}
315
Andrew Walbran475c1452020-02-07 13:22:22 +0000316/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100317static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100318 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000319{
320 uint32_t mode = 0;
321
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100322 switch (ffa_get_data_access_attr(permissions)) {
323 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000324 mode = MM_MODE_R;
325 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000327 mode = MM_MODE_R | MM_MODE_W;
328 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100329 case FFA_DATA_ACCESS_NOT_SPECIFIED:
330 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
331 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_DATA_ACCESS_RESERVED:
333 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100334 }
335
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 switch (ffa_get_instruction_access_attr(permissions)) {
337 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000338 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100340 mode |= MM_MODE_X;
341 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100342 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
343 mode |= (default_mode & MM_MODE_X);
344 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345 case FFA_INSTRUCTION_ACCESS_RESERVED:
346 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000347 }
348
349 return mode;
350}
351
Jose Marinho75509b42019-04-09 09:34:59 +0100352/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 * Get the current mode in the stage-2 page table of the given vm of all the
354 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100355 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100356 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000358 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100359 struct ffa_memory_region_constituent **fragments,
360 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100361{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100362 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100363 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100364
Andrew Walbranca808b12020-05-15 17:22:28 +0100365 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100366 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000367 * Fail if there are no constituents. Otherwise we would get an
368 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100369 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100371 }
372
Andrew Walbranca808b12020-05-15 17:22:28 +0100373 for (i = 0; i < fragment_count; ++i) {
374 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
375 ipaddr_t begin = ipa_init(fragments[i][j].address);
376 size_t size = fragments[i][j].page_count * PAGE_SIZE;
377 ipaddr_t end = ipa_add(begin, size);
378 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100379
Andrew Walbranca808b12020-05-15 17:22:28 +0100380 /* Fail if addresses are not page-aligned. */
381 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
382 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
383 return ffa_error(FFA_INVALID_PARAMETERS);
384 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100385
Andrew Walbranca808b12020-05-15 17:22:28 +0100386 /*
387 * Ensure that this constituent memory range is all
388 * mapped with the same mode.
389 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800390 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100391 return ffa_error(FFA_DENIED);
392 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100393
Andrew Walbranca808b12020-05-15 17:22:28 +0100394 /*
395 * Ensure that all constituents are mapped with the same
396 * mode.
397 */
398 if (i == 0) {
399 *orig_mode = current_mode;
400 } else if (current_mode != *orig_mode) {
401 dlog_verbose(
402 "Expected mode %#x but was %#x for %d "
403 "pages at %#x.\n",
404 *orig_mode, current_mode,
405 fragments[i][j].page_count,
406 ipa_addr(begin));
407 return ffa_error(FFA_DENIED);
408 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100409 }
Jose Marinho75509b42019-04-09 09:34:59 +0100410 }
411
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100412 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000413}
414
415/**
416 * Verify that all pages have the same mode, that the starting mode
417 * constitutes a valid state and obtain the next mode to apply
418 * to the sending VM.
419 *
420 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100421 * 1) FFA_DENIED if a state transition was not found;
422 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100423 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100424 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100425 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
427 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000428 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100429static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100430 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100431 struct ffa_memory_access *receivers, uint32_t receivers_count,
432 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100433 struct ffa_memory_region_constituent **fragments,
434 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
435 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000436{
437 const uint32_t state_mask =
438 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100439 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000440
Andrew Walbranca808b12020-05-15 17:22:28 +0100441 ret = constituents_get_mode(from, orig_from_mode, fragments,
442 fragment_constituent_counts,
443 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100444 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100445 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100446 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100447 }
448
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000449 /* Ensure the address range is normal memory and not a device. */
450 if (*orig_from_mode & MM_MODE_D) {
451 dlog_verbose("Can't share device memory (mode is %#x).\n",
452 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100453 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000454 }
455
456 /*
457 * Ensure the sender is the owner and has exclusive access to the
458 * memory.
459 */
460 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100461 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100462 }
463
J-Alves363f5722022-04-25 17:37:37 +0100464 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100465
J-Alves363f5722022-04-25 17:37:37 +0100466 for (uint32_t i = 0U; i < receivers_count; i++) {
467 ffa_memory_access_permissions_t permissions =
468 receivers[i].receiver_permissions.permissions;
469 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
470 permissions, *orig_from_mode);
471
472 if ((*orig_from_mode & required_from_mode) !=
473 required_from_mode) {
474 dlog_verbose(
475 "Sender tried to send memory with permissions "
476 "which "
477 "required mode %#x but only had %#x itself.\n",
478 required_from_mode, *orig_from_mode);
479 return ffa_error(FFA_DENIED);
480 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000481 }
482
483 /* Find the appropriate new mode. */
484 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000485 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100486 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000487 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100488 break;
489
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000491 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100492 break;
493
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000495 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100496 break;
497
Jose Marinho75509b42019-04-09 09:34:59 +0100498 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100499 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100500 }
501
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100502 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000503}
504
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000506 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100507 struct ffa_memory_region_constituent **fragments,
508 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
509 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000510{
511 const uint32_t state_mask =
512 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
513 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100514 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000515
Andrew Walbranca808b12020-05-15 17:22:28 +0100516 ret = constituents_get_mode(from, orig_from_mode, fragments,
517 fragment_constituent_counts,
518 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100520 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521 }
522
523 /* Ensure the address range is normal memory and not a device. */
524 if (*orig_from_mode & MM_MODE_D) {
525 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
526 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528 }
529
530 /*
531 * Ensure the relinquishing VM is not the owner but has access to the
532 * memory.
533 */
534 orig_from_state = *orig_from_mode & state_mask;
535 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
536 dlog_verbose(
537 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100538 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000539 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100540 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000541 }
542
543 /* Find the appropriate new mode. */
544 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
545
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547}
548
549/**
550 * Verify that all pages have the same mode, that the starting mode
551 * constitutes a valid state and obtain the next mode to apply
552 * to the retrieving VM.
553 *
554 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100555 * 1) FFA_DENIED if a state transition was not found;
556 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100557 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100558 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100559 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100560 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
561 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000562 */
J-Alvesfc19b372022-07-06 12:17:35 +0100563struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000564 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100565 struct ffa_memory_region_constituent **fragments,
566 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
567 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000568{
569 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100570 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000571
Andrew Walbranca808b12020-05-15 17:22:28 +0100572 ret = constituents_get_mode(to, &orig_to_mode, fragments,
573 fragment_constituent_counts,
574 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100576 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100577 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578 }
579
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100580 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000581 /*
582 * If the original ffa memory send call has been processed
583 * successfully, it is expected the orig_to_mode would overlay
584 * with `state_mask`, as a result of the function
585 * `ffa_send_check_transition`.
586 */
J-Alves59ed0042022-07-28 18:26:41 +0100587 if (vm_id_is_current_world(to.vm->id)) {
588 assert((orig_to_mode &
589 (MM_MODE_INVALID | MM_MODE_UNOWNED |
590 MM_MODE_SHARED)) != 0U);
591 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000592 } else {
593 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100594 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000595 * Ensure the retriever has the expected state. We don't care
596 * about the MM_MODE_SHARED bit; either with or without it set
597 * are both valid representations of the !O-NA state.
598 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100599 if (vm_id_is_current_world(to.vm->id) &&
600 to.vm->id != HF_PRIMARY_VM_ID &&
601 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
602 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000604 }
605 }
606
607 /* Find the appropriate new mode. */
608 *to_mode = memory_to_attributes;
609 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000611 *to_mode |= 0;
612 break;
613
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100614 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000615 *to_mode |= MM_MODE_UNOWNED;
616 break;
617
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000619 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
620 break;
621
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100622 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000623 *to_mode |= 0;
624 break;
625
626 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100627 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100628 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000629 }
630
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100631 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100632}
Jose Marinho09b1db82019-08-08 09:16:59 +0100633
634/**
635 * Updates a VM's page table such that the given set of physical address ranges
636 * are mapped in the address space at the corresponding address ranges, in the
637 * mode provided.
638 *
639 * If commit is false, the page tables will be allocated from the mpool but no
640 * mappings will actually be updated. This function must always be called first
641 * with commit false to check that it will succeed before calling with commit
642 * true, to avoid leaving the page table in a half-updated state. To make a
643 * series of changes atomically you can call them all with commit false before
644 * calling them all with commit true.
645 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700646 * vm_ptable_defrag should always be called after a series of page table
647 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100648 *
649 * Returns true on success, or false if the update failed and no changes were
650 * made to memory mappings.
651 */
J-Alves66652252022-07-06 09:49:51 +0100652bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000653 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100654 struct ffa_memory_region_constituent **fragments,
655 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100656 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100657{
Andrew Walbranca808b12020-05-15 17:22:28 +0100658 uint32_t i;
659 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100660
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700661 if (vm_locked.vm->el0_partition) {
662 mode |= MM_MODE_USER | MM_MODE_NG;
663 }
664
Andrew Walbranca808b12020-05-15 17:22:28 +0100665 /* Iterate over the memory region constituents within each fragment. */
666 for (i = 0; i < fragment_count; ++i) {
667 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
668 size_t size = fragments[i][j].page_count * PAGE_SIZE;
669 paddr_t pa_begin =
670 pa_from_ipa(ipa_init(fragments[i][j].address));
671 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200672 uint32_t pa_bits =
673 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100674
675 /*
676 * Ensure the requested region falls into system's PA
677 * range.
678 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200679 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
680 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100681 dlog_error("Region is outside of PA Range\n");
682 return false;
683 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100684
685 if (commit) {
686 vm_identity_commit(vm_locked, pa_begin, pa_end,
687 mode, ppool, NULL);
688 } else if (!vm_identity_prepare(vm_locked, pa_begin,
689 pa_end, mode, ppool)) {
690 return false;
691 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100692 }
693 }
694
695 return true;
696}
697
698/**
699 * Clears a region of physical memory by overwriting it with zeros. The data is
700 * flushed from the cache so the memory has been cleared across the system.
701 */
J-Alves7db32002021-12-14 14:44:50 +0000702static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
703 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100704{
705 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000706 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100707 * global mapping of the whole range. Such an approach will limit
708 * the changes to stage-1 tables and will allow only local
709 * invalidation.
710 */
711 bool ret;
712 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000713 void *ptr = mm_identity_map(stage1_locked, begin, end,
714 MM_MODE_W | (extra_mode_attributes &
715 plat_ffa_other_world_mode()),
716 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100717 size_t size = pa_difference(begin, end);
718
719 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100720 goto fail;
721 }
722
723 memset_s(ptr, size, 0, size);
724 arch_mm_flush_dcache(ptr, size);
725 mm_unmap(stage1_locked, begin, end, ppool);
726
727 ret = true;
728 goto out;
729
730fail:
731 ret = false;
732
733out:
734 mm_unlock_stage1(&stage1_locked);
735
736 return ret;
737}
738
739/**
740 * Clears a region of physical memory by overwriting it with zeros. The data is
741 * flushed from the cache so the memory has been cleared across the system.
742 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100743static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000744 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100745 struct ffa_memory_region_constituent **fragments,
746 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
747 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100748{
749 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100750 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100751 bool ret = false;
752
753 /*
754 * Create a local pool so any freed memory can't be used by another
755 * thread. This is to ensure each constituent that is mapped can be
756 * unmapped again afterwards.
757 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000758 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100759
Andrew Walbranca808b12020-05-15 17:22:28 +0100760 /* Iterate over the memory region constituents within each fragment. */
761 for (i = 0; i < fragment_count; ++i) {
762 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100763
Andrew Walbranca808b12020-05-15 17:22:28 +0100764 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
765 size_t size = fragments[i][j].page_count * PAGE_SIZE;
766 paddr_t begin =
767 pa_from_ipa(ipa_init(fragments[i][j].address));
768 paddr_t end = pa_add(begin, size);
769
J-Alves7db32002021-12-14 14:44:50 +0000770 if (!clear_memory(begin, end, &local_page_pool,
771 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100772 /*
773 * api_clear_memory will defrag on failure, so
774 * no need to do it here.
775 */
776 goto out;
777 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100778 }
779 }
780
Jose Marinho09b1db82019-08-08 09:16:59 +0100781 ret = true;
782
783out:
784 mpool_fini(&local_page_pool);
785 return ret;
786}
787
788/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000789 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100790 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000791 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 *
793 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000794 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100795 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100796 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100797 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
798 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100800 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100801 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100802 */
J-Alves66652252022-07-06 09:49:51 +0100803struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000804 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100805 struct ffa_memory_region_constituent **fragments,
806 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100807 uint32_t share_func, struct ffa_memory_access *receivers,
808 uint32_t receivers_count, struct mpool *page_pool, bool clear,
809 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100810{
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100812 uint32_t orig_from_mode;
813 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100814 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100815 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100816
817 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100818 * Make sure constituents are properly aligned to a 64-bit boundary. If
819 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100820 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100821 for (i = 0; i < fragment_count; ++i) {
822 if (!is_aligned(fragments[i], 8)) {
823 dlog_verbose("Constituents not aligned.\n");
824 return ffa_error(FFA_INVALID_PARAMETERS);
825 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100826 }
827
828 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000829 * Check if the state transition is lawful for the sender, ensure that
830 * all constituents of a memory region being shared are at the same
831 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100832 */
J-Alves363f5722022-04-25 17:37:37 +0100833 ret = ffa_send_check_transition(from_locked, share_func, receivers,
834 receivers_count, &orig_from_mode,
835 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100836 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100837 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100838 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100839 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100840 }
841
Andrew Walbran37c574e2020-06-03 11:45:46 +0100842 if (orig_from_mode_ret != NULL) {
843 *orig_from_mode_ret = orig_from_mode;
844 }
845
Jose Marinho09b1db82019-08-08 09:16:59 +0100846 /*
847 * Create a local pool so any freed memory can't be used by another
848 * thread. This is to ensure the original mapping can be restored if the
849 * clear fails.
850 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000851 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100852
853 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000854 * First reserve all required memory for the new page table entries
855 * without committing, to make sure the entire operation will succeed
856 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100857 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100858 if (!ffa_region_group_identity_map(
859 from_locked, fragments, fragment_constituent_counts,
860 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100862 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 goto out;
864 }
865
866 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867 * Update the mapping for the sender. This won't allocate because the
868 * transaction was already prepared above, but may free pages in the
869 * case that a whole block is being unmapped that was previously
870 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100871 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100872 CHECK(ffa_region_group_identity_map(
873 from_locked, fragments, fragment_constituent_counts,
874 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100875
876 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000877 if (clear &&
878 !ffa_clear_memory_constituents(
879 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
880 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100881 /*
882 * On failure, roll back by returning memory to the sender. This
883 * may allocate pages which were previously freed into
884 * `local_page_pool` by the call above, but will never allocate
885 * more pages than that so can never fail.
886 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100887 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100888 from_locked, fragments, fragment_constituent_counts,
889 fragment_count, orig_from_mode, &local_page_pool,
890 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100891
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100893 goto out;
894 }
895
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000897
898out:
899 mpool_fini(&local_page_pool);
900
901 /*
902 * Tidy up the page table by reclaiming failed mappings (if there was an
903 * error) or merging entries into blocks where possible (on success).
904 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700905 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000906
907 return ret;
908}
909
910/**
911 * Validates and maps memory shared from one VM to another.
912 *
913 * This function requires the calling context to hold the <to> lock.
914 *
915 * Returns:
916 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100917 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000918 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 */
J-Alvesb5084cf2022-07-06 14:20:12 +0100923struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +0000924 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +0100925 struct ffa_memory_region_constituent **fragments,
926 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
927 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
928 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929{
Andrew Walbranca808b12020-05-15 17:22:28 +0100930 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 uint32_t to_mode;
932 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100933 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000934
935 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 * Make sure constituents are properly aligned to a 64-bit boundary. If
937 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000938 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100939 for (i = 0; i < fragment_count; ++i) {
940 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100941 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 return ffa_error(FFA_INVALID_PARAMETERS);
943 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000944 }
945
946 /*
947 * Check if the state transition is lawful for the recipient, and ensure
948 * that all constituents of the memory region being retrieved are at the
949 * same state.
950 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100951 ret = ffa_retrieve_check_transition(
952 to_locked, share_func, fragments, fragment_constituent_counts,
953 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100954 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100955 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100956 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000957 }
958
959 /*
960 * Create a local pool so any freed memory can't be used by another
961 * thread. This is to ensure the original mapping can be restored if the
962 * clear fails.
963 */
964 mpool_init_with_fallback(&local_page_pool, page_pool);
965
966 /*
967 * First reserve all required memory for the new page table entries in
968 * the recipient page tables without committing, to make sure the entire
969 * operation will succeed without exhausting the page pool.
970 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100971 if (!ffa_region_group_identity_map(
972 to_locked, fragments, fragment_constituent_counts,
973 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000974 /* TODO: partial defrag of failed range. */
975 dlog_verbose(
976 "Insufficient memory to update recipient page "
977 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100978 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000979 goto out;
980 }
981
982 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000983 if (clear &&
984 !ffa_clear_memory_constituents(
985 plat_ffa_owner_world_mode(from_id), fragments,
986 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100987 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000989 goto out;
990 }
991
Jose Marinho09b1db82019-08-08 09:16:59 +0100992 /*
993 * Complete the transfer by mapping the memory into the recipient. This
994 * won't allocate because the transaction was already prepared above, so
995 * it doesn't need to use the `local_page_pool`.
996 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100997 CHECK(ffa_region_group_identity_map(
998 to_locked, fragments, fragment_constituent_counts,
999 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001000
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001002
1003out:
1004 mpool_fini(&local_page_pool);
1005
1006 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001007 * Tidy up the page table by reclaiming failed mappings (if there was an
1008 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001009 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001010 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011
1012 return ret;
1013}
1014
Andrew Walbran996d1d12020-05-27 14:08:43 +01001015static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001016 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001017 struct ffa_memory_region_constituent **fragments,
1018 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1019 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001020{
1021 uint32_t orig_from_mode;
1022 uint32_t from_mode;
1023 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001025
Andrew Walbranca808b12020-05-15 17:22:28 +01001026 ret = ffa_relinquish_check_transition(
1027 from_locked, &orig_from_mode, fragments,
1028 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001030 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001031 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001032 }
1033
1034 /*
1035 * Create a local pool so any freed memory can't be used by another
1036 * thread. This is to ensure the original mapping can be restored if the
1037 * clear fails.
1038 */
1039 mpool_init_with_fallback(&local_page_pool, page_pool);
1040
1041 /*
1042 * First reserve all required memory for the new page table entries
1043 * without committing, to make sure the entire operation will succeed
1044 * without exhausting the page pool.
1045 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001046 if (!ffa_region_group_identity_map(
1047 from_locked, fragments, fragment_constituent_counts,
1048 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001051 goto out;
1052 }
1053
1054 /*
1055 * Update the mapping for the sender. This won't allocate because the
1056 * transaction was already prepared above, but may free pages in the
1057 * case that a whole block is being unmapped that was previously
1058 * partially mapped.
1059 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001060 CHECK(ffa_region_group_identity_map(
1061 from_locked, fragments, fragment_constituent_counts,
1062 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001063
1064 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001065 if (clear &&
1066 !ffa_clear_memory_constituents(
1067 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1068 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001069 /*
1070 * On failure, roll back by returning memory to the sender. This
1071 * may allocate pages which were previously freed into
1072 * `local_page_pool` by the call above, but will never allocate
1073 * more pages than that so can never fail.
1074 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001075 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001076 from_locked, fragments, fragment_constituent_counts,
1077 fragment_count, orig_from_mode, &local_page_pool,
1078 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001079
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001080 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001081 goto out;
1082 }
1083
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001084 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001085
1086out:
1087 mpool_fini(&local_page_pool);
1088
1089 /*
1090 * Tidy up the page table by reclaiming failed mappings (if there was an
1091 * error) or merging entries into blocks where possible (on success).
1092 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001093 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001094
1095 return ret;
1096}
1097
1098/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001099 * Complete a memory sending operation by checking that it is valid, updating
1100 * the sender page table, and then either marking the share state as having
1101 * completed sending (on success) or freeing it (on failure).
1102 *
1103 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1104 */
J-Alvesfdd29272022-07-19 13:16:31 +01001105struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001106 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001107 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1108 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001109{
1110 struct ffa_memory_region *memory_region = share_state->memory_region;
1111 struct ffa_value ret;
1112
1113 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001114 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001115
1116 /* Check that state is valid in sender page table and update. */
1117 ret = ffa_send_check_update(
1118 from_locked, share_state->fragments,
1119 share_state->fragment_constituent_counts,
1120 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001121 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001122 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1123 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001124 if (ret.func != FFA_SUCCESS_32) {
1125 /*
1126 * Free share state, it failed to send so it can't be retrieved.
1127 */
1128 dlog_verbose("Complete failed, freeing share state.\n");
1129 share_state_free(share_states, share_state, page_pool);
1130 return ret;
1131 }
1132
1133 share_state->sending_complete = true;
1134 dlog_verbose("Marked sending complete.\n");
1135
J-Alvesee68c542020-10-29 17:48:20 +00001136 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001137}
1138
1139/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001140 * Check that the memory attributes match Hafnium expectations:
1141 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1142 * Write-Allocate Cacheable.
1143 */
1144static struct ffa_value ffa_memory_attributes_validate(
1145 ffa_memory_access_permissions_t attributes)
1146{
1147 enum ffa_memory_type memory_type;
1148 enum ffa_memory_cacheability cacheability;
1149 enum ffa_memory_shareability shareability;
1150
1151 memory_type = ffa_get_memory_type_attr(attributes);
1152 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1153 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1154 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001155 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001156 }
1157
1158 cacheability = ffa_get_memory_cacheability_attr(attributes);
1159 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1160 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1161 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001162 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001163 }
1164
1165 shareability = ffa_get_memory_shareability_attr(attributes);
1166 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1167 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1168 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001169 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001170 }
1171
1172 return (struct ffa_value){.func = FFA_SUCCESS_32};
1173}
1174
1175/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001176 * Check that the given `memory_region` represents a valid memory send request
1177 * of the given `share_func` type, return the clear flag and permissions via the
1178 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001179 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001181 * not.
1182 */
J-Alves66652252022-07-06 09:49:51 +01001183struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001184 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1185 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001186 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001187{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001188 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001189 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001190 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001191 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001192 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193 enum ffa_data_access data_access;
1194 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001195 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001196
J-Alves95df0ef2022-12-07 10:09:48 +00001197 /* The sender must match the caller. */
1198 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1199 vm_id_is_current_world(memory_region->sender)) ||
1200 (vm_id_is_current_world(from_locked.vm->id) &&
1201 memory_region->sender != from_locked.vm->id)) {
1202 dlog_verbose("Invalid memory sender ID.\n");
1203 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001204 }
1205
Andrew Walbrana65a1322020-04-06 19:32:32 +01001206 /*
1207 * Ensure that the composite header is within the memory bounds and
1208 * doesn't overlap the first part of the message.
1209 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001210 receivers_length = sizeof(struct ffa_memory_access) *
1211 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001212 constituents_offset =
1213 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001214 composite_memory_region_offset =
1215 memory_region->receivers[0].composite_memory_region_offset;
1216 if ((composite_memory_region_offset == 0) ||
1217 (composite_memory_region_offset <
1218 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001219 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001220 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001221 "Invalid composite memory region descriptor offset "
1222 "%d.\n",
1223 memory_region->receivers[0]
1224 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001225 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001226 }
1227
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001228 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001229
1230 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001231 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001232 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001233 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1234 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001235 if (memory_share_length != constituents_offset + constituents_length) {
1236 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001237 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001238 memory_region->receivers[0]
1239 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001240 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001241 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001242 if (fragment_length < memory_share_length &&
1243 fragment_length < HF_MAILBOX_SIZE) {
1244 dlog_warning(
1245 "Initial fragment length %d smaller than mailbox "
1246 "size.\n",
1247 fragment_length);
1248 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001249
Andrew Walbrana65a1322020-04-06 19:32:32 +01001250 /*
1251 * Clear is not allowed for memory sharing, as the sender still has
1252 * access to the memory.
1253 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001254 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1255 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001256 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001257 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001258 }
1259
1260 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001261 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001262 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001263 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001264 }
1265
J-Alves363f5722022-04-25 17:37:37 +01001266 /* Check that the permissions are valid, for each specified receiver. */
1267 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1268 ffa_memory_access_permissions_t permissions =
1269 memory_region->receivers[i]
1270 .receiver_permissions.permissions;
1271 ffa_vm_id_t receiver_id =
1272 memory_region->receivers[i]
1273 .receiver_permissions.receiver;
1274
1275 if (memory_region->sender == receiver_id) {
1276 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001277 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001278 }
Federico Recanati85090c42021-12-15 13:17:54 +01001279
J-Alves363f5722022-04-25 17:37:37 +01001280 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1281 j++) {
1282 if (receiver_id ==
1283 memory_region->receivers[j]
1284 .receiver_permissions.receiver) {
1285 dlog_verbose(
1286 "Repeated receiver(%x) in memory send "
1287 "operation.\n",
1288 memory_region->receivers[j]
1289 .receiver_permissions.receiver);
1290 return ffa_error(FFA_INVALID_PARAMETERS);
1291 }
1292 }
1293
1294 if (composite_memory_region_offset !=
1295 memory_region->receivers[i]
1296 .composite_memory_region_offset) {
1297 dlog_verbose(
1298 "All ffa_memory_access should point to the "
1299 "same composite memory region offset.\n");
1300 return ffa_error(FFA_INVALID_PARAMETERS);
1301 }
1302
1303 data_access = ffa_get_data_access_attr(permissions);
1304 instruction_access =
1305 ffa_get_instruction_access_attr(permissions);
1306 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1307 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1308 dlog_verbose(
1309 "Reserved value for receiver permissions "
1310 "%#x.\n",
1311 permissions);
1312 return ffa_error(FFA_INVALID_PARAMETERS);
1313 }
1314 if (instruction_access !=
1315 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1316 dlog_verbose(
1317 "Invalid instruction access permissions %#x "
1318 "for sending memory.\n",
1319 permissions);
1320 return ffa_error(FFA_INVALID_PARAMETERS);
1321 }
1322 if (share_func == FFA_MEM_SHARE_32) {
1323 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1324 dlog_verbose(
1325 "Invalid data access permissions %#x "
1326 "for sharing memory.\n",
1327 permissions);
1328 return ffa_error(FFA_INVALID_PARAMETERS);
1329 }
1330 /*
1331 * According to section 10.10.3 of the FF-A v1.1 EAC0
1332 * spec, NX is required for share operations (but must
1333 * not be specified by the sender) so set it in the
1334 * copy that we store, ready to be returned to the
1335 * retriever.
1336 */
J-Alvesb19731a2022-06-20 17:30:33 +01001337 if (vm_id_is_current_world(receiver_id)) {
1338 ffa_set_instruction_access_attr(
1339 &permissions,
1340 FFA_INSTRUCTION_ACCESS_NX);
1341 memory_region->receivers[i]
1342 .receiver_permissions.permissions =
1343 permissions;
1344 }
J-Alves363f5722022-04-25 17:37:37 +01001345 }
1346 if (share_func == FFA_MEM_LEND_32 &&
1347 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1348 dlog_verbose(
1349 "Invalid data access permissions %#x for "
1350 "lending memory.\n",
1351 permissions);
1352 return ffa_error(FFA_INVALID_PARAMETERS);
1353 }
1354
1355 if (share_func == FFA_MEM_DONATE_32 &&
1356 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1357 dlog_verbose(
1358 "Invalid data access permissions %#x for "
1359 "donating memory.\n",
1360 permissions);
1361 return ffa_error(FFA_INVALID_PARAMETERS);
1362 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001363 }
1364
Federico Recanatid937f5e2021-12-20 17:38:23 +01001365 /*
J-Alves807794e2022-06-16 13:42:47 +01001366 * If a memory donate or lend with single borrower, the memory type
1367 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001368 */
J-Alves807794e2022-06-16 13:42:47 +01001369 if (share_func == FFA_MEM_DONATE_32 ||
1370 (share_func == FFA_MEM_LEND_32 &&
1371 memory_region->receiver_count == 1)) {
1372 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1373 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1374 dlog_verbose(
1375 "Memory type shall not be specified by "
1376 "sender.\n");
1377 return ffa_error(FFA_INVALID_PARAMETERS);
1378 }
1379 } else {
1380 /*
1381 * Check that sender's memory attributes match Hafnium
1382 * expectations: Normal Memory, Inner shareable, Write-Back
1383 * Read-Allocate Write-Allocate Cacheable.
1384 */
1385 ret = ffa_memory_attributes_validate(memory_region->attributes);
1386 if (ret.func != FFA_SUCCESS_32) {
1387 return ret;
1388 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001389 }
1390
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001392}
1393
1394/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001395 * Gets the share state for continuing an operation to donate, lend or share
1396 * memory, and checks that it is a valid request.
1397 *
1398 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1399 * not.
1400 */
J-Alvesfdd29272022-07-19 13:16:31 +01001401struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001402 struct share_states_locked share_states, ffa_memory_handle_t handle,
1403 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1404 struct mpool *page_pool)
1405{
1406 struct ffa_memory_share_state *share_state;
1407 struct ffa_memory_region *memory_region;
1408
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001409 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001410
1411 /*
1412 * Look up the share state by handle and make sure that the VM ID
1413 * matches.
1414 */
1415 if (!get_share_state(share_states, handle, &share_state)) {
1416 dlog_verbose(
1417 "Invalid handle %#x for memory send continuation.\n",
1418 handle);
1419 return ffa_error(FFA_INVALID_PARAMETERS);
1420 }
1421 memory_region = share_state->memory_region;
1422
J-Alvesfdd29272022-07-19 13:16:31 +01001423 if (vm_id_is_current_world(from_vm_id) &&
1424 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001425 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1426 return ffa_error(FFA_INVALID_PARAMETERS);
1427 }
1428
1429 if (share_state->sending_complete) {
1430 dlog_verbose(
1431 "Sending of memory handle %#x is already complete.\n",
1432 handle);
1433 return ffa_error(FFA_INVALID_PARAMETERS);
1434 }
1435
1436 if (share_state->fragment_count == MAX_FRAGMENTS) {
1437 /*
1438 * Log a warning as this is a sign that MAX_FRAGMENTS should
1439 * probably be increased.
1440 */
1441 dlog_warning(
1442 "Too many fragments for memory share with handle %#x; "
1443 "only %d supported.\n",
1444 handle, MAX_FRAGMENTS);
1445 /* Free share state, as it's not possible to complete it. */
1446 share_state_free(share_states, share_state, page_pool);
1447 return ffa_error(FFA_NO_MEMORY);
1448 }
1449
1450 *share_state_ret = share_state;
1451
1452 return (struct ffa_value){.func = FFA_SUCCESS_32};
1453}
1454
1455/**
J-Alves95df0ef2022-12-07 10:09:48 +00001456 * Checks if there is at least one receiver from the other world.
1457 */
J-Alvesfdd29272022-07-19 13:16:31 +01001458bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001459 struct ffa_memory_region *memory_region)
1460{
1461 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1462 ffa_vm_id_t receiver = memory_region->receivers[i]
1463 .receiver_permissions.receiver;
1464 if (!vm_id_is_current_world(receiver)) {
1465 return true;
1466 }
1467 }
1468 return false;
1469}
1470
1471/**
J-Alves8505a8a2022-06-15 18:10:18 +01001472 * Validates a call to donate, lend or share memory to a non-other world VM and
1473 * then updates the stage-2 page tables. Specifically, check if the message
1474 * length and number of memory region constituents match, and if the transition
1475 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001476 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001477 * Assumes that the caller has already found and locked the sender VM and copied
1478 * the memory region descriptor from the sender's TX buffer to a freshly
1479 * allocated page from Hafnium's internal pool. The caller must have also
1480 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001481 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001482 * This function takes ownership of the `memory_region` passed in and will free
1483 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001484 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001485struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001486 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001487 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001488 uint32_t fragment_length, uint32_t share_func,
1489 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001490{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001491 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001492 struct share_states_locked share_states;
1493 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001494
1495 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001496 * If there is an error validating the `memory_region` then we need to
1497 * free it because we own it but we won't be storing it in a share state
1498 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001499 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001500 ret = ffa_memory_send_validate(from_locked, memory_region,
1501 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001502 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001504 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001505 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001506 }
1507
Andrew Walbrana65a1322020-04-06 19:32:32 +01001508 /* Set flag for share function, ready to be retrieved later. */
1509 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001510 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001511 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001512 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001513 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001514 case FFA_MEM_LEND_32:
1515 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001516 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001517 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001518 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001519 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001520 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001521 }
1522
Andrew Walbranca808b12020-05-15 17:22:28 +01001523 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001524 /*
1525 * Allocate a share state before updating the page table. Otherwise if
1526 * updating the page table succeeded but allocating the share state
1527 * failed then it would leave the memory in a state where nobody could
1528 * get it back.
1529 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001530 if (!allocate_share_state(share_states, share_func, memory_region,
1531 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1532 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001533 dlog_verbose("Failed to allocate share state.\n");
1534 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001535 ret = ffa_error(FFA_NO_MEMORY);
1536 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001537 }
1538
Andrew Walbranca808b12020-05-15 17:22:28 +01001539 if (fragment_length == memory_share_length) {
1540 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001541 ret = ffa_memory_send_complete(
1542 from_locked, share_states, share_state, page_pool,
1543 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001544 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001545 /*
1546 * Use sender ID from 'memory_region' assuming
1547 * that at this point it has been validated:
1548 * - MBZ at virtual FF-A instance.
1549 */
1550 ffa_vm_id_t sender_to_ret =
1551 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1552 ? memory_region->sender
1553 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001554 ret = (struct ffa_value){
1555 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001556 .arg1 = (uint32_t)memory_region->handle,
1557 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001558 .arg3 = fragment_length,
1559 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 }
1561
1562out:
1563 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001564 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001565 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001566}
1567
1568/**
J-Alves8505a8a2022-06-15 18:10:18 +01001569 * Continues an operation to donate, lend or share memory to a VM from current
1570 * world. If this is the last fragment then checks that the transition is valid
1571 * for the type of memory sending operation and updates the stage-2 page tables
1572 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001573 *
1574 * Assumes that the caller has already found and locked the sender VM and copied
1575 * the memory region descriptor from the sender's TX buffer to a freshly
1576 * allocated page from Hafnium's internal pool.
1577 *
1578 * This function takes ownership of the `fragment` passed in; it must not be
1579 * freed by the caller.
1580 */
1581struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1582 void *fragment,
1583 uint32_t fragment_length,
1584 ffa_memory_handle_t handle,
1585 struct mpool *page_pool)
1586{
1587 struct share_states_locked share_states = share_states_lock();
1588 struct ffa_memory_share_state *share_state;
1589 struct ffa_value ret;
1590 struct ffa_memory_region *memory_region;
1591
1592 ret = ffa_memory_send_continue_validate(share_states, handle,
1593 &share_state,
1594 from_locked.vm->id, page_pool);
1595 if (ret.func != FFA_SUCCESS_32) {
1596 goto out_free_fragment;
1597 }
1598 memory_region = share_state->memory_region;
1599
J-Alves95df0ef2022-12-07 10:09:48 +00001600 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001601 dlog_error(
1602 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001603 "other world. This should never happen, and indicates "
1604 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001605 "EL3 code.\n");
1606 ret = ffa_error(FFA_INVALID_PARAMETERS);
1607 goto out_free_fragment;
1608 }
1609
1610 /* Add this fragment. */
1611 share_state->fragments[share_state->fragment_count] = fragment;
1612 share_state->fragment_constituent_counts[share_state->fragment_count] =
1613 fragment_length / sizeof(struct ffa_memory_region_constituent);
1614 share_state->fragment_count++;
1615
1616 /* Check whether the memory send operation is now ready to complete. */
1617 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001618 ret = ffa_memory_send_complete(
1619 from_locked, share_states, share_state, page_pool,
1620 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001621 } else {
1622 ret = (struct ffa_value){
1623 .func = FFA_MEM_FRAG_RX_32,
1624 .arg1 = (uint32_t)handle,
1625 .arg2 = (uint32_t)(handle >> 32),
1626 .arg3 = share_state_next_fragment_offset(share_states,
1627 share_state)};
1628 }
1629 goto out;
1630
1631out_free_fragment:
1632 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001633
1634out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001635 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001636 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001637}
1638
Andrew Walbranca808b12020-05-15 17:22:28 +01001639/** Clean up after the receiver has finished retrieving a memory region. */
1640static void ffa_memory_retrieve_complete(
1641 struct share_states_locked share_states,
1642 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1643{
1644 if (share_state->share_func == FFA_MEM_DONATE_32) {
1645 /*
1646 * Memory that has been donated can't be relinquished,
1647 * so no need to keep the share state around.
1648 */
1649 share_state_free(share_states, share_state, page_pool);
1650 dlog_verbose("Freed share state for donate.\n");
1651 }
1652}
1653
J-Alves96de29f2022-04-26 16:05:24 +01001654/*
1655 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1656 * returns its index in the receiver's array. If receiver's ID doesn't exist
1657 * in the array, return the region's 'receiver_count'.
1658 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001659uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1660 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001661{
1662 struct ffa_memory_access *receivers;
1663 uint32_t i;
1664
1665 assert(memory_region != NULL);
1666
1667 receivers = memory_region->receivers;
1668
1669 for (i = 0U; i < memory_region->receiver_count; i++) {
1670 if (receivers[i].receiver_permissions.receiver == receiver) {
1671 break;
1672 }
1673 }
1674
1675 return i;
1676}
1677
1678/**
1679 * Validates the retrieved permissions against those specified by the lender
1680 * of memory share operation. Optionally can help set the permissions to be used
1681 * for the S2 mapping, through the `permissions` argument.
1682 * Returns true if permissions are valid, false otherwise.
1683 */
1684static bool ffa_memory_retrieve_is_memory_access_valid(
1685 enum ffa_data_access sent_data_access,
1686 enum ffa_data_access requested_data_access,
1687 enum ffa_instruction_access sent_instruction_access,
1688 enum ffa_instruction_access requested_instruction_access,
1689 ffa_memory_access_permissions_t *permissions)
1690{
1691 switch (sent_data_access) {
1692 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1693 case FFA_DATA_ACCESS_RW:
1694 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1695 requested_data_access == FFA_DATA_ACCESS_RW) {
1696 if (permissions != NULL) {
1697 ffa_set_data_access_attr(permissions,
1698 FFA_DATA_ACCESS_RW);
1699 }
1700 break;
1701 }
1702 /* Intentional fall-through. */
1703 case FFA_DATA_ACCESS_RO:
1704 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1705 requested_data_access == FFA_DATA_ACCESS_RO) {
1706 if (permissions != NULL) {
1707 ffa_set_data_access_attr(permissions,
1708 FFA_DATA_ACCESS_RO);
1709 }
1710 break;
1711 }
1712 dlog_verbose(
1713 "Invalid data access requested; sender specified "
1714 "permissions %#x but receiver requested %#x.\n",
1715 sent_data_access, requested_data_access);
1716 return false;
1717 case FFA_DATA_ACCESS_RESERVED:
1718 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1719 "checked before this point.");
1720 }
1721
1722 switch (sent_instruction_access) {
1723 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1724 case FFA_INSTRUCTION_ACCESS_X:
1725 if (requested_instruction_access ==
1726 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1727 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1728 if (permissions != NULL) {
1729 ffa_set_instruction_access_attr(
1730 permissions, FFA_INSTRUCTION_ACCESS_X);
1731 }
1732 break;
1733 }
1734 case FFA_INSTRUCTION_ACCESS_NX:
1735 if (requested_instruction_access ==
1736 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1737 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1738 if (permissions != NULL) {
1739 ffa_set_instruction_access_attr(
1740 permissions, FFA_INSTRUCTION_ACCESS_NX);
1741 }
1742 break;
1743 }
1744 dlog_verbose(
1745 "Invalid instruction access requested; sender "
1746 "specified permissions %#x but receiver requested "
1747 "%#x.\n",
1748 sent_instruction_access, requested_instruction_access);
1749 return false;
1750 case FFA_INSTRUCTION_ACCESS_RESERVED:
1751 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1752 "be checked before this point.");
1753 }
1754
1755 return true;
1756}
1757
1758/**
1759 * Validate the receivers' permissions in the retrieve request against those
1760 * specified by the lender.
1761 * In the `permissions` argument returns the permissions to set at S2 for the
1762 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1763 * Returns FFA_SUCCESS if all specified permissions are valid.
1764 */
1765static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1766 struct ffa_memory_region *memory_region,
1767 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1768 ffa_memory_access_permissions_t *permissions)
1769{
1770 uint32_t retrieve_receiver_index;
1771
1772 assert(permissions != NULL);
1773
1774 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1775 dlog_verbose(
1776 "Retrieve request should contain same list of "
1777 "borrowers, as specified by the lender.\n");
1778 return ffa_error(FFA_INVALID_PARAMETERS);
1779 }
1780
1781 retrieve_receiver_index = retrieve_request->receiver_count;
1782
1783 /* Should be populated with the permissions of the retriever. */
1784 *permissions = 0;
1785
1786 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1787 ffa_memory_access_permissions_t sent_permissions;
1788 struct ffa_memory_access *current_receiver =
1789 &retrieve_request->receivers[i];
1790 ffa_memory_access_permissions_t requested_permissions =
1791 current_receiver->receiver_permissions.permissions;
1792 ffa_vm_id_t current_receiver_id =
1793 current_receiver->receiver_permissions.receiver;
1794 bool found_to_id = current_receiver_id == to_vm_id;
1795
1796 /*
1797 * Find the current receiver in the transaction descriptor from
1798 * sender.
1799 */
1800 uint32_t mem_region_receiver_index =
1801 ffa_memory_region_get_receiver(memory_region,
1802 current_receiver_id);
1803
1804 if (mem_region_receiver_index ==
1805 memory_region->receiver_count) {
1806 dlog_verbose("%s: receiver %x not found\n", __func__,
1807 current_receiver_id);
1808 return ffa_error(FFA_DENIED);
1809 }
1810
1811 sent_permissions =
1812 memory_region->receivers[mem_region_receiver_index]
1813 .receiver_permissions.permissions;
1814
1815 if (found_to_id) {
1816 retrieve_receiver_index = i;
1817 }
1818
1819 /*
1820 * Since we are traversing the list of receivers, save the index
1821 * of the caller. As it needs to be there.
1822 */
1823
1824 if (current_receiver->composite_memory_region_offset != 0U) {
1825 dlog_verbose(
1826 "Retriever specified address ranges not "
1827 "supported (got offset %d).\n",
1828 current_receiver
1829 ->composite_memory_region_offset);
1830 return ffa_error(FFA_INVALID_PARAMETERS);
1831 }
1832
1833 /*
1834 * Check permissions from sender against permissions requested
1835 * by receiver.
1836 */
1837 if (!ffa_memory_retrieve_is_memory_access_valid(
1838 ffa_get_data_access_attr(sent_permissions),
1839 ffa_get_data_access_attr(requested_permissions),
1840 ffa_get_instruction_access_attr(sent_permissions),
1841 ffa_get_instruction_access_attr(
1842 requested_permissions),
1843 found_to_id ? permissions : NULL)) {
1844 return ffa_error(FFA_DENIED);
1845 }
1846
1847 /*
1848 * Can't request PM to clear memory if only provided with RO
1849 * permissions.
1850 */
1851 if (found_to_id &&
1852 (ffa_get_data_access_attr(*permissions) ==
1853 FFA_DATA_ACCESS_RO) &&
1854 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
1855 0U) {
1856 dlog_verbose(
1857 "Receiver has RO permissions can not request "
1858 "clear.\n");
1859 return ffa_error(FFA_DENIED);
1860 }
1861 }
1862
1863 if (retrieve_receiver_index == retrieve_request->receiver_count) {
1864 dlog_verbose(
1865 "Retrieve request does not contain caller's (%x) "
1866 "permissions\n",
1867 to_vm_id);
1868 return ffa_error(FFA_INVALID_PARAMETERS);
1869 }
1870
1871 return (struct ffa_value){.func = FFA_SUCCESS_32};
1872}
1873
J-Alvesa9cd7e32022-07-01 13:49:33 +01001874/*
1875 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
1876 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
1877 * of a pending memory sharing operation whose allocator is the SPM, for
1878 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
1879 * the memory region descriptor of the retrieve request must be zeroed with the
1880 * exception of the sender ID and handle.
1881 */
1882bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
1883 struct vm_locked to_locked)
1884{
1885 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
1886 request->attributes == 0U && request->flags == 0U &&
1887 request->tag == 0U && request->receiver_count == 0U &&
1888 plat_ffa_memory_handle_allocated_by_current_world(
1889 request->handle);
1890}
1891
1892/*
1893 * Helper to reset count of fragments retrieved by the hypervisor.
1894 */
1895static void ffa_memory_retrieve_complete_from_hyp(
1896 struct ffa_memory_share_state *share_state)
1897{
1898 if (share_state->hypervisor_fragment_count ==
1899 share_state->fragment_count) {
1900 share_state->hypervisor_fragment_count = 0;
1901 }
1902}
1903
J-Alves089004f2022-07-13 14:25:44 +01001904/**
1905 * Validate that the memory region descriptor provided by the borrower on
1906 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
1907 * memory sharing call.
1908 */
1909static struct ffa_value ffa_memory_retrieve_validate(
1910 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
1911 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
1912 uint32_t share_func)
1913{
1914 ffa_memory_region_flags_t transaction_type =
1915 retrieve_request->flags &
1916 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
1917
1918 assert(retrieve_request != NULL);
1919 assert(memory_region != NULL);
1920 assert(receiver_index != NULL);
1921 assert(retrieve_request->sender == memory_region->sender);
1922
1923 /*
1924 * Check that the transaction type expected by the receiver is
1925 * correct, if it has been specified.
1926 */
1927 if (transaction_type !=
1928 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
1929 transaction_type != (memory_region->flags &
1930 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
1931 dlog_verbose(
1932 "Incorrect transaction type %#x for "
1933 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
1934 transaction_type,
1935 memory_region->flags &
1936 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
1937 retrieve_request->handle);
1938 return ffa_error(FFA_INVALID_PARAMETERS);
1939 }
1940
1941 if (retrieve_request->tag != memory_region->tag) {
1942 dlog_verbose(
1943 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
1944 "%d for handle %#x.\n",
1945 retrieve_request->tag, memory_region->tag,
1946 retrieve_request->handle);
1947 return ffa_error(FFA_INVALID_PARAMETERS);
1948 }
1949
1950 *receiver_index =
1951 ffa_memory_region_get_receiver(memory_region, receiver_id);
1952
1953 if (*receiver_index == memory_region->receiver_count) {
1954 dlog_verbose(
1955 "Incorrect receiver VM ID %d for "
1956 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01001957 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01001958 return ffa_error(FFA_INVALID_PARAMETERS);
1959 }
1960
1961 if ((retrieve_request->flags &
1962 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
1963 dlog_verbose(
1964 "Retriever specified 'address range alignment 'hint' "
1965 "not supported.\n");
1966 return ffa_error(FFA_INVALID_PARAMETERS);
1967 }
1968 if ((retrieve_request->flags &
1969 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
1970 dlog_verbose(
1971 "Bits 8-5 must be zero in memory region's flags "
1972 "(address range alignment hint not supported).\n");
1973 return ffa_error(FFA_INVALID_PARAMETERS);
1974 }
1975
1976 if ((retrieve_request->flags & ~0x7FF) != 0U) {
1977 dlog_verbose(
1978 "Bits 31-10 must be zero in memory region's flags.\n");
1979 return ffa_error(FFA_INVALID_PARAMETERS);
1980 }
1981
1982 if (share_func == FFA_MEM_SHARE_32 &&
1983 (retrieve_request->flags &
1984 (FFA_MEMORY_REGION_FLAG_CLEAR |
1985 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
1986 dlog_verbose(
1987 "Memory Share operation can't clean after relinquish "
1988 "memory region.\n");
1989 return ffa_error(FFA_INVALID_PARAMETERS);
1990 }
1991
1992 /*
1993 * If the borrower needs the memory to be cleared before mapping
1994 * to its address space, the sender should have set the flag
1995 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
1996 * FFA_DENIED.
1997 */
1998 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
1999 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2000 dlog_verbose(
2001 "Borrower needs memory cleared. Sender needs to set "
2002 "flag for clearing memory.\n");
2003 return ffa_error(FFA_DENIED);
2004 }
2005
2006 /*
2007 * If memory type is not specified, bypass validation of memory
2008 * attributes in the retrieve request. The retriever is expecting to
2009 * obtain this information from the SPMC.
2010 */
2011 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2012 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2013 return (struct ffa_value){.func = FFA_SUCCESS_32};
2014 }
2015
2016 /*
2017 * Ensure receiver's attributes are compatible with how
2018 * Hafnium maps memory: Normal Memory, Inner shareable,
2019 * Write-Back Read-Allocate Write-Allocate Cacheable.
2020 */
2021 return ffa_memory_attributes_validate(retrieve_request->attributes);
2022}
2023
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002024struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2025 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002026 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002027 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002028{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002029 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002030 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002031 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002032 sizeof(struct ffa_memory_access);
2033 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002034 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002035 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002036 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002037 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002038 struct ffa_memory_share_state *share_state;
2039 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002040 struct ffa_composite_memory_region *composite;
2041 uint32_t total_length;
2042 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002043 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002044 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002045
2046 dump_share_states();
2047
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002048 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002049 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002050 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002051 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002052 expected_retrieve_request_length,
2053 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002054 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002055 }
2056
2057 share_states = share_states_lock();
2058 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002059 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002060 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002061 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002062 goto out;
2063 }
2064
J-Alves96de29f2022-04-26 16:05:24 +01002065 if (!share_state->sending_complete) {
2066 dlog_verbose(
2067 "Memory with handle %#x not fully sent, can't "
2068 "retrieve.\n",
2069 handle);
2070 ret = ffa_error(FFA_INVALID_PARAMETERS);
2071 goto out;
2072 }
2073
Andrew Walbrana65a1322020-04-06 19:32:32 +01002074 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002075
Andrew Walbrana65a1322020-04-06 19:32:32 +01002076 CHECK(memory_region != NULL);
2077
J-Alves089004f2022-07-13 14:25:44 +01002078 if (retrieve_request->sender != memory_region->sender) {
2079 dlog_verbose(
2080 "Memory with handle %#x not fully sent, can't "
2081 "retrieve.\n",
2082 handle);
2083 ret = ffa_error(FFA_INVALID_PARAMETERS);
2084 goto out;
2085 }
J-Alves96de29f2022-04-26 16:05:24 +01002086
J-Alvesa9cd7e32022-07-01 13:49:33 +01002087 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2088 to_locked)) {
2089 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002090
J-Alvesb5084cf2022-07-06 14:20:12 +01002091 /*
2092 * The SPMC can only process retrieve requests to memory share
2093 * operations with one borrower from the other world. It can't
2094 * determine the ID of the NWd VM that invoked the retrieve
2095 * request interface call. It relies on the hypervisor to
2096 * validate the caller's ID against that provided in the
2097 * `receivers` list of the retrieve response.
2098 * In case there is only one borrower from the NWd in the
2099 * transaction descriptor, record that in the `receiver_id` for
2100 * later use, and validate in the retrieve request message.
2101 */
2102 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2103 uint32_t other_world_count = 0;
2104
2105 for (uint32_t i = 0; i < memory_region->receiver_count;
2106 i++) {
2107 receiver_id =
2108 retrieve_request->receivers[0]
2109 .receiver_permissions.receiver;
2110 if (!vm_id_is_current_world(receiver_id)) {
2111 other_world_count++;
2112 }
2113 }
2114 if (other_world_count > 1) {
2115 dlog_verbose(
2116 "Support one receiver from the other "
2117 "world.\n");
2118 return ffa_error(FFA_NOT_SUPPORTED);
2119 }
2120 }
2121
2122 /*
2123 * Validate retrieve request, according to what was sent by the
2124 * sender. Function will output the `receiver_index` from the
2125 * provided memory region, and will output `permissions` from
2126 * the validated requested permissions.
2127 */
J-Alves089004f2022-07-13 14:25:44 +01002128 ret = ffa_memory_retrieve_validate(
2129 receiver_id, retrieve_request, memory_region,
2130 &receiver_index, share_state->share_func);
2131 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002132 goto out;
2133 }
2134
2135 if (share_state->retrieved_fragment_count[receiver_index] !=
2136 0U) {
2137 dlog_verbose(
2138 "Memory with handle %#x already retrieved.\n",
2139 handle);
2140 ret = ffa_error(FFA_DENIED);
2141 goto out;
2142 }
2143
J-Alvesa9cd7e32022-07-01 13:49:33 +01002144 ret = ffa_memory_retrieve_validate_memory_access_list(
2145 memory_region, retrieve_request, receiver_id,
2146 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002147 if (ret.func != FFA_SUCCESS_32) {
2148 goto out;
2149 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002150
J-Alvesa9cd7e32022-07-01 13:49:33 +01002151 memory_to_attributes = ffa_memory_permissions_to_mode(
2152 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002153
2154 if (to_locked.vm->el0_partition) {
2155 /*
2156 * Get extra mapping attributes for the given VM ID.
2157 * If the memory is shared by a VM executing in non
2158 * secure world, attribute MM_MODE_NS has to be set
2159 * while mapping that in a SP executing in secure world.
2160 */
2161 memory_to_attributes |=
2162 arch_mm_extra_attributes_from_vm(
2163 retrieve_request->sender);
2164 }
2165
J-Alvesa9cd7e32022-07-01 13:49:33 +01002166 ret = ffa_retrieve_check_update(
2167 to_locked, memory_region->sender,
2168 share_state->fragments,
2169 share_state->fragment_constituent_counts,
2170 share_state->fragment_count, memory_to_attributes,
2171 share_state->share_func, false, page_pool);
2172
2173 if (ret.func != FFA_SUCCESS_32) {
2174 goto out;
2175 }
2176
2177 share_state->retrieved_fragment_count[receiver_index] = 1;
2178 is_send_complete =
2179 share_state->retrieved_fragment_count[receiver_index] ==
2180 share_state->fragment_count;
2181 } else {
2182 if (share_state->hypervisor_fragment_count != 0U) {
2183 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002184 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002185 "the hypervisor.\n",
2186 handle);
2187 ret = ffa_error(FFA_DENIED);
2188 goto out;
2189 }
2190
2191 share_state->hypervisor_fragment_count = 1;
2192
2193 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 }
2195
J-Alvesb5084cf2022-07-06 14:20:12 +01002196 /* VMs acquire the RX buffer from SPMC. */
2197 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2198
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002199 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002200 * Copy response to RX buffer of caller and deliver the message.
2201 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002202 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002203 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002204 composite = ffa_memory_region_get_composite(memory_region, 0);
2205 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002206 * Constituents which we received in the first fragment should
2207 * always fit in the first fragment we are sending, because the
2208 * header is the same size in both cases and we have a fixed
2209 * message buffer size. So `ffa_retrieved_memory_region_init`
2210 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002211 */
2212 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002213 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2214 memory_region->sender, memory_region->attributes,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002215 memory_region->flags, handle, receiver_id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002216 composite->page_count, composite->constituent_count,
2217 share_state->fragments[0],
2218 share_state->fragment_constituent_counts[0], &total_length,
2219 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002220
Andrew Walbranca808b12020-05-15 17:22:28 +01002221 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002222 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002223 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002224 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2225
J-Alvesa9cd7e32022-07-01 13:49:33 +01002226 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002227 ffa_memory_retrieve_complete(share_states, share_state,
2228 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002229 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002231 .arg1 = total_length,
2232 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002233out:
2234 share_states_unlock(&share_states);
2235 dump_share_states();
2236 return ret;
2237}
2238
2239struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2240 ffa_memory_handle_t handle,
2241 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002242 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002243 struct mpool *page_pool)
2244{
2245 struct ffa_memory_region *memory_region;
2246 struct share_states_locked share_states;
2247 struct ffa_memory_share_state *share_state;
2248 struct ffa_value ret;
2249 uint32_t fragment_index;
2250 uint32_t retrieved_constituents_count;
2251 uint32_t i;
2252 uint32_t expected_fragment_offset;
2253 uint32_t remaining_constituent_count;
2254 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002255 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002256 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002257
2258 dump_share_states();
2259
2260 share_states = share_states_lock();
2261 if (!get_share_state(share_states, handle, &share_state)) {
2262 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2263 handle);
2264 ret = ffa_error(FFA_INVALID_PARAMETERS);
2265 goto out;
2266 }
2267
2268 memory_region = share_state->memory_region;
2269 CHECK(memory_region != NULL);
2270
Andrew Walbranca808b12020-05-15 17:22:28 +01002271 if (!share_state->sending_complete) {
2272 dlog_verbose(
2273 "Memory with handle %#x not fully sent, can't "
2274 "retrieve.\n",
2275 handle);
2276 ret = ffa_error(FFA_INVALID_PARAMETERS);
2277 goto out;
2278 }
2279
J-Alves59ed0042022-07-28 18:26:41 +01002280 /*
2281 * If retrieve request from the hypervisor has been initiated in the
2282 * given share_state, continue it, else assume it is a continuation of
2283 * retrieve request from a NWd VM.
2284 */
2285 continue_ffa_hyp_mem_retrieve_req =
2286 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2287 (share_state->hypervisor_fragment_count != 0U) &&
2288 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002289
J-Alves59ed0042022-07-28 18:26:41 +01002290 if (!continue_ffa_hyp_mem_retrieve_req) {
2291 receiver_index = ffa_memory_region_get_receiver(
2292 memory_region, to_locked.vm->id);
2293
2294 if (receiver_index == memory_region->receiver_count) {
2295 dlog_verbose(
2296 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2297 "borrower to memory sharing transaction (%x)\n",
2298 to_locked.vm->id, handle);
2299 ret = ffa_error(FFA_INVALID_PARAMETERS);
2300 goto out;
2301 }
2302
2303 if (share_state->retrieved_fragment_count[receiver_index] ==
2304 0 ||
2305 share_state->retrieved_fragment_count[receiver_index] >=
2306 share_state->fragment_count) {
2307 dlog_verbose(
2308 "Retrieval of memory with handle %#x not yet "
2309 "started or already completed (%d/%d fragments "
2310 "retrieved).\n",
2311 handle,
2312 share_state->retrieved_fragment_count
2313 [receiver_index],
2314 share_state->fragment_count);
2315 ret = ffa_error(FFA_INVALID_PARAMETERS);
2316 goto out;
2317 }
2318
2319 fragment_index =
2320 share_state->retrieved_fragment_count[receiver_index];
2321 } else {
2322 if (share_state->hypervisor_fragment_count == 0 ||
2323 share_state->hypervisor_fragment_count >=
2324 share_state->fragment_count) {
2325 dlog_verbose(
2326 "Retrieve of memory with handle %x not "
2327 "started from hypervisor.\n",
2328 handle);
2329 ret = ffa_error(FFA_INVALID_PARAMETERS);
2330 goto out;
2331 }
2332
2333 if (memory_region->sender != sender_vm_id) {
2334 dlog_verbose(
2335 "Sender ID (%x) is not as expected for memory "
2336 "handle %x\n",
2337 sender_vm_id, handle);
2338 ret = ffa_error(FFA_INVALID_PARAMETERS);
2339 goto out;
2340 }
2341
2342 fragment_index = share_state->hypervisor_fragment_count;
2343
2344 receiver_index = 0;
2345 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002346
2347 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002348 * Check that the given fragment offset is correct by counting
2349 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002350 */
2351 retrieved_constituents_count = 0;
2352 for (i = 0; i < fragment_index; ++i) {
2353 retrieved_constituents_count +=
2354 share_state->fragment_constituent_counts[i];
2355 }
J-Alvesc7484f12022-05-13 12:41:14 +01002356
2357 CHECK(memory_region->receiver_count > 0);
2358
Andrew Walbranca808b12020-05-15 17:22:28 +01002359 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002360 ffa_composite_constituent_offset(memory_region,
2361 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002362 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002363 sizeof(struct ffa_memory_region_constituent) -
2364 sizeof(struct ffa_memory_access) *
2365 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 if (fragment_offset != expected_fragment_offset) {
2367 dlog_verbose("Fragment offset was %d but expected %d.\n",
2368 fragment_offset, expected_fragment_offset);
2369 ret = ffa_error(FFA_INVALID_PARAMETERS);
2370 goto out;
2371 }
2372
J-Alves59ed0042022-07-28 18:26:41 +01002373 /* VMs acquire the RX buffer from SPMC. */
2374 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2375
Andrew Walbranca808b12020-05-15 17:22:28 +01002376 remaining_constituent_count = ffa_memory_fragment_init(
2377 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2378 share_state->fragments[fragment_index],
2379 share_state->fragment_constituent_counts[fragment_index],
2380 &fragment_length);
2381 CHECK(remaining_constituent_count == 0);
2382 to_locked.vm->mailbox.recv_size = fragment_length;
2383 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2384 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2385 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
Andrew Walbranca808b12020-05-15 17:22:28 +01002386
J-Alves59ed0042022-07-28 18:26:41 +01002387 if (!continue_ffa_hyp_mem_retrieve_req) {
2388 share_state->retrieved_fragment_count[receiver_index]++;
2389 if (share_state->retrieved_fragment_count[receiver_index] ==
2390 share_state->fragment_count) {
2391 ffa_memory_retrieve_complete(share_states, share_state,
2392 page_pool);
2393 }
2394 } else {
2395 share_state->hypervisor_fragment_count++;
2396
2397 ffa_memory_retrieve_complete_from_hyp(share_state);
2398 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002399 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2400 .arg1 = (uint32_t)handle,
2401 .arg2 = (uint32_t)(handle >> 32),
2402 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002403
2404out:
2405 share_states_unlock(&share_states);
2406 dump_share_states();
2407 return ret;
2408}
2409
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002410struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002411 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002412 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002413{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002414 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002415 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 struct ffa_memory_share_state *share_state;
2417 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002418 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002419 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002420 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002421
Andrew Walbrana65a1322020-04-06 19:32:32 +01002422 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002423 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002424 "Stream endpoints not supported (got %d "
2425 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002426 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002427 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002428 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002429 }
2430
Andrew Walbrana65a1322020-04-06 19:32:32 +01002431 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002432 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002433 "VM ID %d in relinquish message doesn't match "
2434 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002435 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002436 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002437 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002438 }
2439
2440 dump_share_states();
2441
2442 share_states = share_states_lock();
2443 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002444 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002445 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002446 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002447 goto out;
2448 }
2449
Andrew Walbranca808b12020-05-15 17:22:28 +01002450 if (!share_state->sending_complete) {
2451 dlog_verbose(
2452 "Memory with handle %#x not fully sent, can't "
2453 "relinquish.\n",
2454 handle);
2455 ret = ffa_error(FFA_INVALID_PARAMETERS);
2456 goto out;
2457 }
2458
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002459 memory_region = share_state->memory_region;
2460 CHECK(memory_region != NULL);
2461
J-Alves8eb19162022-04-28 10:56:48 +01002462 receiver_index = ffa_memory_region_get_receiver(memory_region,
2463 from_locked.vm->id);
2464
2465 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002466 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002467 "VM ID %d tried to relinquish memory region "
2468 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002469 "handle %#x and it is not a valid borrower.\n",
2470 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002471 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 goto out;
2473 }
2474
J-Alves8eb19162022-04-28 10:56:48 +01002475 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002476 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002477 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002478 "Memory with handle %#x not yet fully "
2479 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002480 "receiver %x can't relinquish.\n",
2481 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002482 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 goto out;
2484 }
2485
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002486 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002487
2488 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002489 * Clear is not allowed for memory that was shared, as the
2490 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002491 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002492 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002493 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 goto out;
2496 }
2497
Andrew Walbranca808b12020-05-15 17:22:28 +01002498 ret = ffa_relinquish_check_update(
2499 from_locked, share_state->fragments,
2500 share_state->fragment_constituent_counts,
2501 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002503 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002504 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002505 * Mark memory handle as not retrieved, so it can be
2506 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507 */
J-Alves8eb19162022-04-28 10:56:48 +01002508 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002509 }
2510
2511out:
2512 share_states_unlock(&share_states);
2513 dump_share_states();
2514 return ret;
2515}
2516
2517/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002518 * Validates that the reclaim transition is allowed for the given
2519 * handle, updates the page table of the reclaiming VM, and frees the
2520 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002522struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002523 ffa_memory_handle_t handle,
2524 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002525 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526{
2527 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002528 struct ffa_memory_share_state *share_state;
2529 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531
2532 dump_share_states();
2533
2534 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002535 if (get_share_state(share_states, handle, &share_state)) {
2536 memory_region = share_state->memory_region;
2537 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002538 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002541 goto out;
2542 }
2543
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544 CHECK(memory_region != NULL);
2545
J-Alvesa9cd7e32022-07-01 13:49:33 +01002546 if (vm_id_is_current_world(to_locked.vm->id) &&
2547 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002548 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002549 "VM %#x attempted to reclaim memory handle %#x "
2550 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002552 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553 goto out;
2554 }
2555
Andrew Walbranca808b12020-05-15 17:22:28 +01002556 if (!share_state->sending_complete) {
2557 dlog_verbose(
2558 "Memory with handle %#x not fully sent, can't "
2559 "reclaim.\n",
2560 handle);
2561 ret = ffa_error(FFA_INVALID_PARAMETERS);
2562 goto out;
2563 }
2564
J-Alves752236c2022-04-28 11:07:47 +01002565 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2566 if (share_state->retrieved_fragment_count[i] != 0) {
2567 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002568 "Tried to reclaim memory handle %#x "
2569 "that has "
2570 "not been relinquished by all "
2571 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002572 handle,
2573 memory_region->receivers[i]
2574 .receiver_permissions.receiver);
2575 ret = ffa_error(FFA_DENIED);
2576 goto out;
2577 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002578 }
2579
Andrew Walbranca808b12020-05-15 17:22:28 +01002580 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002581 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002582 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002583 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002584 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002585
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002586 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002587 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002588 dlog_verbose(
2589 "Freed share state after successful "
2590 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002591 }
2592
2593out:
2594 share_states_unlock(&share_states);
2595 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002596}