blob: fd3030b9a4aab8894ae6ea40652bca61b24767a4 [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 */
119static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100120 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. */
159static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100160 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000161 struct mpool *page_pool)
162{
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. */
186static bool share_state_sending_complete(
187 struct share_states_locked share_states,
188 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189{
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 struct ffa_composite_memory_region *composite;
191 uint32_t expected_constituent_count;
192 uint32_t fragment_constituent_count_total = 0;
193 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194
Andrew Walbranca808b12020-05-15 17:22:28 +0100195 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000196 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100197
198 /*
199 * Share state must already be valid, or it's not possible to get hold
200 * of it.
201 */
202 CHECK(share_state->memory_region != NULL &&
203 share_state->share_func != 0);
204
205 composite =
206 ffa_memory_region_get_composite(share_state->memory_region, 0);
207 expected_constituent_count = composite->constituent_count;
208 for (i = 0; i < share_state->fragment_count; ++i) {
209 fragment_constituent_count_total +=
210 share_state->fragment_constituent_counts[i];
211 }
212 dlog_verbose(
213 "Checking completion: constituent count %d/%d from %d "
214 "fragments.\n",
215 fragment_constituent_count_total, expected_constituent_count,
216 share_state->fragment_count);
217
218 return fragment_constituent_count_total == expected_constituent_count;
219}
220
221/**
222 * Calculates the offset of the next fragment expected for the given share
223 * state.
224 */
225static uint32_t share_state_next_fragment_offset(
226 struct share_states_locked share_states,
227 struct ffa_memory_share_state *share_state)
228{
229 uint32_t next_fragment_offset;
230 uint32_t i;
231
232 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000233 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100234
235 next_fragment_offset =
236 ffa_composite_constituent_offset(share_state->memory_region, 0);
237 for (i = 0; i < share_state->fragment_count; ++i) {
238 next_fragment_offset +=
239 share_state->fragment_constituent_counts[i] *
240 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000241 }
242
Andrew Walbranca808b12020-05-15 17:22:28 +0100243 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000244}
245
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100246static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247{
248 uint32_t i;
249
250 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
251 return;
252 }
253
Olivier Deprez935e1b12020-12-22 18:01:29 +0100254 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100255 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100256 "recipients [",
257 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100258 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100259 memory_region->receiver_count);
260 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000261 if (i != 0) {
262 dlog(", ");
263 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100264 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100265 memory_region->receivers[i].receiver_permissions.receiver,
266 memory_region->receivers[i]
267 .receiver_permissions.permissions,
268 memory_region->receivers[i]
269 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000270 }
271 dlog("]");
272}
273
J-Alves66652252022-07-06 09:49:51 +0100274void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275{
276 uint32_t i;
277
278 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
279 return;
280 }
281
282 dlog("Current share states:\n");
283 sl_lock(&share_states_lock_instance);
284 for (i = 0; i < MAX_MEM_SHARES; ++i) {
285 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000286 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100287 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000288 dlog("SHARE");
289 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100290 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000291 dlog("LEND");
292 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100293 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000294 dlog("DONATE");
295 break;
296 default:
297 dlog("invalid share_func %#x",
298 share_states[i].share_func);
299 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100300 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000301 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100302 if (share_states[i].sending_complete) {
303 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000304 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100305 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306 }
J-Alves2a0d2882020-10-29 14:49:50 +0000307 dlog(" with %d fragments, %d retrieved, "
308 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100309 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000310 share_states[i].retrieved_fragment_count[0],
311 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000312 }
313 }
314 sl_unlock(&share_states_lock_instance);
315}
316
Andrew Walbran475c1452020-02-07 13:22:22 +0000317/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100318static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100319 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000320{
321 uint32_t mode = 0;
322
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323 switch (ffa_get_data_access_attr(permissions)) {
324 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000325 mode = MM_MODE_R;
326 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000328 mode = MM_MODE_R | MM_MODE_W;
329 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100330 case FFA_DATA_ACCESS_NOT_SPECIFIED:
331 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
332 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100333 case FFA_DATA_ACCESS_RESERVED:
334 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100335 }
336
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 switch (ffa_get_instruction_access_attr(permissions)) {
338 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000339 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100340 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100341 mode |= MM_MODE_X;
342 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100343 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
344 mode |= (default_mode & MM_MODE_X);
345 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100346 case FFA_INSTRUCTION_ACCESS_RESERVED:
347 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000348 }
349
350 return mode;
351}
352
Jose Marinho75509b42019-04-09 09:34:59 +0100353/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 * Get the current mode in the stage-2 page table of the given vm of all the
355 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100356 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100357 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000359 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100360 struct ffa_memory_region_constituent **fragments,
361 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100362{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100363 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100364 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100365
Andrew Walbranca808b12020-05-15 17:22:28 +0100366 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100367 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000368 * Fail if there are no constituents. Otherwise we would get an
369 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100370 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100372 }
373
Andrew Walbranca808b12020-05-15 17:22:28 +0100374 for (i = 0; i < fragment_count; ++i) {
375 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
376 ipaddr_t begin = ipa_init(fragments[i][j].address);
377 size_t size = fragments[i][j].page_count * PAGE_SIZE;
378 ipaddr_t end = ipa_add(begin, size);
379 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100380
Andrew Walbranca808b12020-05-15 17:22:28 +0100381 /* Fail if addresses are not page-aligned. */
382 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
383 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
384 return ffa_error(FFA_INVALID_PARAMETERS);
385 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100386
Andrew Walbranca808b12020-05-15 17:22:28 +0100387 /*
388 * Ensure that this constituent memory range is all
389 * mapped with the same mode.
390 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800391 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100392 return ffa_error(FFA_DENIED);
393 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100394
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 /*
396 * Ensure that all constituents are mapped with the same
397 * mode.
398 */
399 if (i == 0) {
400 *orig_mode = current_mode;
401 } else if (current_mode != *orig_mode) {
402 dlog_verbose(
403 "Expected mode %#x but was %#x for %d "
404 "pages at %#x.\n",
405 *orig_mode, current_mode,
406 fragments[i][j].page_count,
407 ipa_addr(begin));
408 return ffa_error(FFA_DENIED);
409 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100410 }
Jose Marinho75509b42019-04-09 09:34:59 +0100411 }
412
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100413 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000414}
415
416/**
417 * Verify that all pages have the same mode, that the starting mode
418 * constitutes a valid state and obtain the next mode to apply
419 * to the sending VM.
420 *
421 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100422 * 1) FFA_DENIED if a state transition was not found;
423 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100424 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100425 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100426 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
428 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000429 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100431 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100432 struct ffa_memory_access *receivers, uint32_t receivers_count,
433 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100434 struct ffa_memory_region_constituent **fragments,
435 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
436 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000437{
438 const uint32_t state_mask =
439 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100440 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000441
Andrew Walbranca808b12020-05-15 17:22:28 +0100442 ret = constituents_get_mode(from, orig_from_mode, fragments,
443 fragment_constituent_counts,
444 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100445 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100446 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100447 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100448 }
449
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000450 /* Ensure the address range is normal memory and not a device. */
451 if (*orig_from_mode & MM_MODE_D) {
452 dlog_verbose("Can't share device memory (mode is %#x).\n",
453 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100454 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000455 }
456
457 /*
458 * Ensure the sender is the owner and has exclusive access to the
459 * memory.
460 */
461 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100463 }
464
J-Alves363f5722022-04-25 17:37:37 +0100465 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100466
J-Alves363f5722022-04-25 17:37:37 +0100467 for (uint32_t i = 0U; i < receivers_count; i++) {
468 ffa_memory_access_permissions_t permissions =
469 receivers[i].receiver_permissions.permissions;
470 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
471 permissions, *orig_from_mode);
472
473 if ((*orig_from_mode & required_from_mode) !=
474 required_from_mode) {
475 dlog_verbose(
476 "Sender tried to send memory with permissions "
477 "which "
478 "required mode %#x but only had %#x itself.\n",
479 required_from_mode, *orig_from_mode);
480 return ffa_error(FFA_DENIED);
481 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000482 }
483
484 /* Find the appropriate new mode. */
485 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000486 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100487 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000488 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100489 break;
490
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100491 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000492 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100493 break;
494
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000496 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100497 break;
498
Jose Marinho75509b42019-04-09 09:34:59 +0100499 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100500 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100501 }
502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000504}
505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000507 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100508 struct ffa_memory_region_constituent **fragments,
509 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
510 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000511{
512 const uint32_t state_mask =
513 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
514 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100515 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000516
Andrew Walbranca808b12020-05-15 17:22:28 +0100517 ret = constituents_get_mode(from, orig_from_mode, fragments,
518 fragment_constituent_counts,
519 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100521 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000522 }
523
524 /* Ensure the address range is normal memory and not a device. */
525 if (*orig_from_mode & MM_MODE_D) {
526 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
527 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000529 }
530
531 /*
532 * Ensure the relinquishing VM is not the owner but has access to the
533 * memory.
534 */
535 orig_from_state = *orig_from_mode & state_mask;
536 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
537 dlog_verbose(
538 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100539 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000540 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100541 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000542 }
543
544 /* Find the appropriate new mode. */
545 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
546
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100547 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000548}
549
550/**
551 * Verify that all pages have the same mode, that the starting mode
552 * constitutes a valid state and obtain the next mode to apply
553 * to the retrieving VM.
554 *
555 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100556 * 1) FFA_DENIED if a state transition was not found;
557 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100558 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100559 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100560 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100561 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
562 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000563 */
J-Alvesfc19b372022-07-06 12:17:35 +0100564struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000565 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100566 struct ffa_memory_region_constituent **fragments,
567 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
568 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000569{
570 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572
Andrew Walbranca808b12020-05-15 17:22:28 +0100573 ret = constituents_get_mode(to, &orig_to_mode, fragments,
574 fragment_constituent_counts,
575 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100577 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100578 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000579 }
580
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000582 /*
583 * If the original ffa memory send call has been processed
584 * successfully, it is expected the orig_to_mode would overlay
585 * with `state_mask`, as a result of the function
586 * `ffa_send_check_transition`.
587 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100588 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
589 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000590 } else {
591 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100592 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000593 * Ensure the retriever has the expected state. We don't care
594 * about the MM_MODE_SHARED bit; either with or without it set
595 * are both valid representations of the !O-NA state.
596 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100597 if (vm_id_is_current_world(to.vm->id) &&
598 to.vm->id != HF_PRIMARY_VM_ID &&
599 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
600 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100601 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000602 }
603 }
604
605 /* Find the appropriate new mode. */
606 *to_mode = memory_to_attributes;
607 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100608 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000609 *to_mode |= 0;
610 break;
611
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 *to_mode |= MM_MODE_UNOWNED;
614 break;
615
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000617 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
618 break;
619
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100620 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 *to_mode |= 0;
622 break;
623
624 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100625 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100626 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000627 }
628
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100629 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100630}
Jose Marinho09b1db82019-08-08 09:16:59 +0100631
632/**
633 * Updates a VM's page table such that the given set of physical address ranges
634 * are mapped in the address space at the corresponding address ranges, in the
635 * mode provided.
636 *
637 * If commit is false, the page tables will be allocated from the mpool but no
638 * mappings will actually be updated. This function must always be called first
639 * with commit false to check that it will succeed before calling with commit
640 * true, to avoid leaving the page table in a half-updated state. To make a
641 * series of changes atomically you can call them all with commit false before
642 * calling them all with commit true.
643 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700644 * vm_ptable_defrag should always be called after a series of page table
645 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100646 *
647 * Returns true on success, or false if the update failed and no changes were
648 * made to memory mappings.
649 */
J-Alves66652252022-07-06 09:49:51 +0100650bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000651 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100652 struct ffa_memory_region_constituent **fragments,
653 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100654 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100655{
Andrew Walbranca808b12020-05-15 17:22:28 +0100656 uint32_t i;
657 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100658
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700659 if (vm_locked.vm->el0_partition) {
660 mode |= MM_MODE_USER | MM_MODE_NG;
661 }
662
Andrew Walbranca808b12020-05-15 17:22:28 +0100663 /* Iterate over the memory region constituents within each fragment. */
664 for (i = 0; i < fragment_count; ++i) {
665 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
666 size_t size = fragments[i][j].page_count * PAGE_SIZE;
667 paddr_t pa_begin =
668 pa_from_ipa(ipa_init(fragments[i][j].address));
669 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200670 uint32_t pa_bits =
671 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100672
673 /*
674 * Ensure the requested region falls into system's PA
675 * range.
676 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200677 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
678 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100679 dlog_error("Region is outside of PA Range\n");
680 return false;
681 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100682
683 if (commit) {
684 vm_identity_commit(vm_locked, pa_begin, pa_end,
685 mode, ppool, NULL);
686 } else if (!vm_identity_prepare(vm_locked, pa_begin,
687 pa_end, mode, ppool)) {
688 return false;
689 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100690 }
691 }
692
693 return true;
694}
695
696/**
697 * Clears a region of physical memory by overwriting it with zeros. The data is
698 * flushed from the cache so the memory has been cleared across the system.
699 */
J-Alves7db32002021-12-14 14:44:50 +0000700static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
701 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100702{
703 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000704 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100705 * global mapping of the whole range. Such an approach will limit
706 * the changes to stage-1 tables and will allow only local
707 * invalidation.
708 */
709 bool ret;
710 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000711 void *ptr = mm_identity_map(stage1_locked, begin, end,
712 MM_MODE_W | (extra_mode_attributes &
713 plat_ffa_other_world_mode()),
714 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100715 size_t size = pa_difference(begin, end);
716
717 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100718 goto fail;
719 }
720
721 memset_s(ptr, size, 0, size);
722 arch_mm_flush_dcache(ptr, size);
723 mm_unmap(stage1_locked, begin, end, ppool);
724
725 ret = true;
726 goto out;
727
728fail:
729 ret = false;
730
731out:
732 mm_unlock_stage1(&stage1_locked);
733
734 return ret;
735}
736
737/**
738 * Clears a region of physical memory by overwriting it with zeros. The data is
739 * flushed from the cache so the memory has been cleared across the system.
740 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000742 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100743 struct ffa_memory_region_constituent **fragments,
744 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
745 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100746{
747 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100748 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100749 bool ret = false;
750
751 /*
752 * Create a local pool so any freed memory can't be used by another
753 * thread. This is to ensure each constituent that is mapped can be
754 * unmapped again afterwards.
755 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000756 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100757
Andrew Walbranca808b12020-05-15 17:22:28 +0100758 /* Iterate over the memory region constituents within each fragment. */
759 for (i = 0; i < fragment_count; ++i) {
760 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100761
Andrew Walbranca808b12020-05-15 17:22:28 +0100762 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
763 size_t size = fragments[i][j].page_count * PAGE_SIZE;
764 paddr_t begin =
765 pa_from_ipa(ipa_init(fragments[i][j].address));
766 paddr_t end = pa_add(begin, size);
767
J-Alves7db32002021-12-14 14:44:50 +0000768 if (!clear_memory(begin, end, &local_page_pool,
769 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100770 /*
771 * api_clear_memory will defrag on failure, so
772 * no need to do it here.
773 */
774 goto out;
775 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100776 }
777 }
778
Jose Marinho09b1db82019-08-08 09:16:59 +0100779 ret = true;
780
781out:
782 mpool_fini(&local_page_pool);
783 return ret;
784}
785
786/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000787 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100788 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000789 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100790 *
791 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000792 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100793 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100794 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100795 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
796 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100798 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100800 */
J-Alves66652252022-07-06 09:49:51 +0100801struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000802 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100803 struct ffa_memory_region_constituent **fragments,
804 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100805 uint32_t share_func, struct ffa_memory_access *receivers,
806 uint32_t receivers_count, struct mpool *page_pool, bool clear,
807 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100808{
Andrew Walbranca808b12020-05-15 17:22:28 +0100809 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100810 uint32_t orig_from_mode;
811 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100812 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100813 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100814
815 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100816 * Make sure constituents are properly aligned to a 64-bit boundary. If
817 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100818 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100819 for (i = 0; i < fragment_count; ++i) {
820 if (!is_aligned(fragments[i], 8)) {
821 dlog_verbose("Constituents not aligned.\n");
822 return ffa_error(FFA_INVALID_PARAMETERS);
823 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100824 }
825
826 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000827 * Check if the state transition is lawful for the sender, ensure that
828 * all constituents of a memory region being shared are at the same
829 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100830 */
J-Alves363f5722022-04-25 17:37:37 +0100831 ret = ffa_send_check_transition(from_locked, share_func, receivers,
832 receivers_count, &orig_from_mode,
833 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100834 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100836 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100837 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100838 }
839
Andrew Walbran37c574e2020-06-03 11:45:46 +0100840 if (orig_from_mode_ret != NULL) {
841 *orig_from_mode_ret = orig_from_mode;
842 }
843
Jose Marinho09b1db82019-08-08 09:16:59 +0100844 /*
845 * Create a local pool so any freed memory can't be used by another
846 * thread. This is to ensure the original mapping can be restored if the
847 * clear fails.
848 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000849 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100850
851 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000852 * First reserve all required memory for the new page table entries
853 * without committing, to make sure the entire operation will succeed
854 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100855 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100856 if (!ffa_region_group_identity_map(
857 from_locked, fragments, fragment_constituent_counts,
858 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100860 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 goto out;
862 }
863
864 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000865 * Update the mapping for the sender. This won't allocate because the
866 * transaction was already prepared above, but may free pages in the
867 * case that a whole block is being unmapped that was previously
868 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100870 CHECK(ffa_region_group_identity_map(
871 from_locked, fragments, fragment_constituent_counts,
872 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100873
874 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000875 if (clear &&
876 !ffa_clear_memory_constituents(
877 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
878 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 /*
880 * On failure, roll back by returning memory to the sender. This
881 * may allocate pages which were previously freed into
882 * `local_page_pool` by the call above, but will never allocate
883 * more pages than that so can never fail.
884 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100886 from_locked, fragments, fragment_constituent_counts,
887 fragment_count, orig_from_mode, &local_page_pool,
888 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100889
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100891 goto out;
892 }
893
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000895
896out:
897 mpool_fini(&local_page_pool);
898
899 /*
900 * Tidy up the page table by reclaiming failed mappings (if there was an
901 * error) or merging entries into blocks where possible (on success).
902 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700903 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000904
905 return ret;
906}
907
908/**
909 * Validates and maps memory shared from one VM to another.
910 *
911 * This function requires the calling context to hold the <to> lock.
912 *
913 * Returns:
914 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100915 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100917 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000918 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100921static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +0000922 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +0100923 struct ffa_memory_region_constituent **fragments,
924 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
925 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
926 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000927{
Andrew Walbranca808b12020-05-15 17:22:28 +0100928 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 uint32_t to_mode;
930 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000932
933 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 * Make sure constituents are properly aligned to a 64-bit boundary. If
935 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000936 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100937 for (i = 0; i < fragment_count; ++i) {
938 if (!is_aligned(fragments[i], 8)) {
939 return ffa_error(FFA_INVALID_PARAMETERS);
940 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000941 }
942
943 /*
944 * Check if the state transition is lawful for the recipient, and ensure
945 * that all constituents of the memory region being retrieved are at the
946 * same state.
947 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100948 ret = ffa_retrieve_check_transition(
949 to_locked, share_func, fragments, fragment_constituent_counts,
950 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100951 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100952 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100953 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000954 }
955
956 /*
957 * Create a local pool so any freed memory can't be used by another
958 * thread. This is to ensure the original mapping can be restored if the
959 * clear fails.
960 */
961 mpool_init_with_fallback(&local_page_pool, page_pool);
962
963 /*
964 * First reserve all required memory for the new page table entries in
965 * the recipient page tables without committing, to make sure the entire
966 * operation will succeed without exhausting the page pool.
967 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100968 if (!ffa_region_group_identity_map(
969 to_locked, fragments, fragment_constituent_counts,
970 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000971 /* TODO: partial defrag of failed range. */
972 dlog_verbose(
973 "Insufficient memory to update recipient page "
974 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100975 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000976 goto out;
977 }
978
979 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000980 if (clear &&
981 !ffa_clear_memory_constituents(
982 plat_ffa_owner_world_mode(from_id), fragments,
983 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100984 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000985 goto out;
986 }
987
Jose Marinho09b1db82019-08-08 09:16:59 +0100988 /*
989 * Complete the transfer by mapping the memory into the recipient. This
990 * won't allocate because the transaction was already prepared above, so
991 * it doesn't need to use the `local_page_pool`.
992 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100993 CHECK(ffa_region_group_identity_map(
994 to_locked, fragments, fragment_constituent_counts,
995 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100996
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100997 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +0100998
999out:
1000 mpool_fini(&local_page_pool);
1001
1002 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001003 * Tidy up the page table by reclaiming failed mappings (if there was an
1004 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001005 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001006 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007
1008 return ret;
1009}
1010
Andrew Walbran996d1d12020-05-27 14:08:43 +01001011static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001012 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001013 struct ffa_memory_region_constituent **fragments,
1014 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1015 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001016{
1017 uint32_t orig_from_mode;
1018 uint32_t from_mode;
1019 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021
Andrew Walbranca808b12020-05-15 17:22:28 +01001022 ret = ffa_relinquish_check_transition(
1023 from_locked, &orig_from_mode, fragments,
1024 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001025 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001026 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001027 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 }
1029
1030 /*
1031 * Create a local pool so any freed memory can't be used by another
1032 * thread. This is to ensure the original mapping can be restored if the
1033 * clear fails.
1034 */
1035 mpool_init_with_fallback(&local_page_pool, page_pool);
1036
1037 /*
1038 * First reserve all required memory for the new page table entries
1039 * without committing, to make sure the entire operation will succeed
1040 * without exhausting the page pool.
1041 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001042 if (!ffa_region_group_identity_map(
1043 from_locked, fragments, fragment_constituent_counts,
1044 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001045 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001047 goto out;
1048 }
1049
1050 /*
1051 * Update the mapping for the sender. This won't allocate because the
1052 * transaction was already prepared above, but may free pages in the
1053 * case that a whole block is being unmapped that was previously
1054 * partially mapped.
1055 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001056 CHECK(ffa_region_group_identity_map(
1057 from_locked, fragments, fragment_constituent_counts,
1058 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001059
1060 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001061 if (clear &&
1062 !ffa_clear_memory_constituents(
1063 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1064 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001065 /*
1066 * On failure, roll back by returning memory to the sender. This
1067 * may allocate pages which were previously freed into
1068 * `local_page_pool` by the call above, but will never allocate
1069 * more pages than that so can never fail.
1070 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001072 from_locked, fragments, fragment_constituent_counts,
1073 fragment_count, orig_from_mode, &local_page_pool,
1074 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001075
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001076 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001077 goto out;
1078 }
1079
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001080 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001081
1082out:
1083 mpool_fini(&local_page_pool);
1084
1085 /*
1086 * Tidy up the page table by reclaiming failed mappings (if there was an
1087 * error) or merging entries into blocks where possible (on success).
1088 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001089 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001090
1091 return ret;
1092}
1093
1094/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001095 * Complete a memory sending operation by checking that it is valid, updating
1096 * the sender page table, and then either marking the share state as having
1097 * completed sending (on success) or freeing it (on failure).
1098 *
1099 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1100 */
1101static struct ffa_value ffa_memory_send_complete(
1102 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001103 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1104 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001105{
1106 struct ffa_memory_region *memory_region = share_state->memory_region;
1107 struct ffa_value ret;
1108
1109 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001110 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001111
1112 /* Check that state is valid in sender page table and update. */
1113 ret = ffa_send_check_update(
1114 from_locked, share_state->fragments,
1115 share_state->fragment_constituent_counts,
1116 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001117 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001118 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1119 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001120 if (ret.func != FFA_SUCCESS_32) {
1121 /*
1122 * Free share state, it failed to send so it can't be retrieved.
1123 */
1124 dlog_verbose("Complete failed, freeing share state.\n");
1125 share_state_free(share_states, share_state, page_pool);
1126 return ret;
1127 }
1128
1129 share_state->sending_complete = true;
1130 dlog_verbose("Marked sending complete.\n");
1131
J-Alvesee68c542020-10-29 17:48:20 +00001132 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001133}
1134
1135/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001136 * Check that the memory attributes match Hafnium expectations:
1137 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1138 * Write-Allocate Cacheable.
1139 */
1140static struct ffa_value ffa_memory_attributes_validate(
1141 ffa_memory_access_permissions_t attributes)
1142{
1143 enum ffa_memory_type memory_type;
1144 enum ffa_memory_cacheability cacheability;
1145 enum ffa_memory_shareability shareability;
1146
1147 memory_type = ffa_get_memory_type_attr(attributes);
1148 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1149 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1150 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001151 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001152 }
1153
1154 cacheability = ffa_get_memory_cacheability_attr(attributes);
1155 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1156 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1157 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001158 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001159 }
1160
1161 shareability = ffa_get_memory_shareability_attr(attributes);
1162 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1163 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1164 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001165 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001166 }
1167
1168 return (struct ffa_value){.func = FFA_SUCCESS_32};
1169}
1170
1171/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001172 * Check that the given `memory_region` represents a valid memory send request
1173 * of the given `share_func` type, return the clear flag and permissions via the
1174 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001175 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001176 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001177 * not.
1178 */
J-Alves66652252022-07-06 09:49:51 +01001179struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001180 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1181 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001182 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001183{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001185 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001186 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001187 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001188 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001189 enum ffa_data_access data_access;
1190 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001191 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001192
J-Alves95df0ef2022-12-07 10:09:48 +00001193 /* The sender must match the caller. */
1194 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1195 vm_id_is_current_world(memory_region->sender)) ||
1196 (vm_id_is_current_world(from_locked.vm->id) &&
1197 memory_region->sender != from_locked.vm->id)) {
1198 dlog_verbose("Invalid memory sender ID.\n");
1199 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001200 }
1201
Andrew Walbrana65a1322020-04-06 19:32:32 +01001202 /*
1203 * Ensure that the composite header is within the memory bounds and
1204 * doesn't overlap the first part of the message.
1205 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001206 receivers_length = sizeof(struct ffa_memory_access) *
1207 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001208 constituents_offset =
1209 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001210 composite_memory_region_offset =
1211 memory_region->receivers[0].composite_memory_region_offset;
1212 if ((composite_memory_region_offset == 0) ||
1213 (composite_memory_region_offset <
1214 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001215 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001216 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001217 "Invalid composite memory region descriptor offset "
1218 "%d.\n",
1219 memory_region->receivers[0]
1220 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001221 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001222 }
1223
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001224 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001225
1226 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001227 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001228 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001229 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1230 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001231 if (memory_share_length != constituents_offset + constituents_length) {
1232 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001233 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001234 memory_region->receivers[0]
1235 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001236 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001237 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001238 if (fragment_length < memory_share_length &&
1239 fragment_length < HF_MAILBOX_SIZE) {
1240 dlog_warning(
1241 "Initial fragment length %d smaller than mailbox "
1242 "size.\n",
1243 fragment_length);
1244 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001245
Andrew Walbrana65a1322020-04-06 19:32:32 +01001246 /*
1247 * Clear is not allowed for memory sharing, as the sender still has
1248 * access to the memory.
1249 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001250 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1251 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001252 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001253 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001254 }
1255
1256 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001257 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001258 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001259 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001260 }
1261
J-Alves363f5722022-04-25 17:37:37 +01001262 /* Check that the permissions are valid, for each specified receiver. */
1263 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1264 ffa_memory_access_permissions_t permissions =
1265 memory_region->receivers[i]
1266 .receiver_permissions.permissions;
1267 ffa_vm_id_t receiver_id =
1268 memory_region->receivers[i]
1269 .receiver_permissions.receiver;
1270
1271 if (memory_region->sender == receiver_id) {
1272 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001273 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001274 }
Federico Recanati85090c42021-12-15 13:17:54 +01001275
J-Alves363f5722022-04-25 17:37:37 +01001276 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1277 j++) {
1278 if (receiver_id ==
1279 memory_region->receivers[j]
1280 .receiver_permissions.receiver) {
1281 dlog_verbose(
1282 "Repeated receiver(%x) in memory send "
1283 "operation.\n",
1284 memory_region->receivers[j]
1285 .receiver_permissions.receiver);
1286 return ffa_error(FFA_INVALID_PARAMETERS);
1287 }
1288 }
1289
1290 if (composite_memory_region_offset !=
1291 memory_region->receivers[i]
1292 .composite_memory_region_offset) {
1293 dlog_verbose(
1294 "All ffa_memory_access should point to the "
1295 "same composite memory region offset.\n");
1296 return ffa_error(FFA_INVALID_PARAMETERS);
1297 }
1298
1299 data_access = ffa_get_data_access_attr(permissions);
1300 instruction_access =
1301 ffa_get_instruction_access_attr(permissions);
1302 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1303 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1304 dlog_verbose(
1305 "Reserved value for receiver permissions "
1306 "%#x.\n",
1307 permissions);
1308 return ffa_error(FFA_INVALID_PARAMETERS);
1309 }
1310 if (instruction_access !=
1311 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1312 dlog_verbose(
1313 "Invalid instruction access permissions %#x "
1314 "for sending memory.\n",
1315 permissions);
1316 return ffa_error(FFA_INVALID_PARAMETERS);
1317 }
1318 if (share_func == FFA_MEM_SHARE_32) {
1319 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1320 dlog_verbose(
1321 "Invalid data access permissions %#x "
1322 "for sharing memory.\n",
1323 permissions);
1324 return ffa_error(FFA_INVALID_PARAMETERS);
1325 }
1326 /*
1327 * According to section 10.10.3 of the FF-A v1.1 EAC0
1328 * spec, NX is required for share operations (but must
1329 * not be specified by the sender) so set it in the
1330 * copy that we store, ready to be returned to the
1331 * retriever.
1332 */
J-Alvesb19731a2022-06-20 17:30:33 +01001333 if (vm_id_is_current_world(receiver_id)) {
1334 ffa_set_instruction_access_attr(
1335 &permissions,
1336 FFA_INSTRUCTION_ACCESS_NX);
1337 memory_region->receivers[i]
1338 .receiver_permissions.permissions =
1339 permissions;
1340 }
J-Alves363f5722022-04-25 17:37:37 +01001341 }
1342 if (share_func == FFA_MEM_LEND_32 &&
1343 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1344 dlog_verbose(
1345 "Invalid data access permissions %#x for "
1346 "lending memory.\n",
1347 permissions);
1348 return ffa_error(FFA_INVALID_PARAMETERS);
1349 }
1350
1351 if (share_func == FFA_MEM_DONATE_32 &&
1352 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1353 dlog_verbose(
1354 "Invalid data access permissions %#x for "
1355 "donating memory.\n",
1356 permissions);
1357 return ffa_error(FFA_INVALID_PARAMETERS);
1358 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001359 }
1360
Federico Recanatid937f5e2021-12-20 17:38:23 +01001361 /*
J-Alves807794e2022-06-16 13:42:47 +01001362 * If a memory donate or lend with single borrower, the memory type
1363 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001364 */
J-Alves807794e2022-06-16 13:42:47 +01001365 if (share_func == FFA_MEM_DONATE_32 ||
1366 (share_func == FFA_MEM_LEND_32 &&
1367 memory_region->receiver_count == 1)) {
1368 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1369 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1370 dlog_verbose(
1371 "Memory type shall not be specified by "
1372 "sender.\n");
1373 return ffa_error(FFA_INVALID_PARAMETERS);
1374 }
1375 } else {
1376 /*
1377 * Check that sender's memory attributes match Hafnium
1378 * expectations: Normal Memory, Inner shareable, Write-Back
1379 * Read-Allocate Write-Allocate Cacheable.
1380 */
1381 ret = ffa_memory_attributes_validate(memory_region->attributes);
1382 if (ret.func != FFA_SUCCESS_32) {
1383 return ret;
1384 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001385 }
1386
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388}
1389
1390/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001391 * Gets the share state for continuing an operation to donate, lend or share
1392 * memory, and checks that it is a valid request.
1393 *
1394 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1395 * not.
1396 */
1397static struct ffa_value ffa_memory_send_continue_validate(
1398 struct share_states_locked share_states, ffa_memory_handle_t handle,
1399 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1400 struct mpool *page_pool)
1401{
1402 struct ffa_memory_share_state *share_state;
1403 struct ffa_memory_region *memory_region;
1404
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001405 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001406
1407 /*
1408 * Look up the share state by handle and make sure that the VM ID
1409 * matches.
1410 */
1411 if (!get_share_state(share_states, handle, &share_state)) {
1412 dlog_verbose(
1413 "Invalid handle %#x for memory send continuation.\n",
1414 handle);
1415 return ffa_error(FFA_INVALID_PARAMETERS);
1416 }
1417 memory_region = share_state->memory_region;
1418
1419 if (memory_region->sender != from_vm_id) {
1420 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1421 return ffa_error(FFA_INVALID_PARAMETERS);
1422 }
1423
1424 if (share_state->sending_complete) {
1425 dlog_verbose(
1426 "Sending of memory handle %#x is already complete.\n",
1427 handle);
1428 return ffa_error(FFA_INVALID_PARAMETERS);
1429 }
1430
1431 if (share_state->fragment_count == MAX_FRAGMENTS) {
1432 /*
1433 * Log a warning as this is a sign that MAX_FRAGMENTS should
1434 * probably be increased.
1435 */
1436 dlog_warning(
1437 "Too many fragments for memory share with handle %#x; "
1438 "only %d supported.\n",
1439 handle, MAX_FRAGMENTS);
1440 /* Free share state, as it's not possible to complete it. */
1441 share_state_free(share_states, share_state, page_pool);
1442 return ffa_error(FFA_NO_MEMORY);
1443 }
1444
1445 *share_state_ret = share_state;
1446
1447 return (struct ffa_value){.func = FFA_SUCCESS_32};
1448}
1449
1450/**
J-Alves8505a8a2022-06-15 18:10:18 +01001451 * Forwards a memory send continuation message on to the other world.
Andrew Walbranca808b12020-05-15 17:22:28 +01001452 */
J-Alves8505a8a2022-06-15 18:10:18 +01001453static struct ffa_value memory_send_continue_other_world_forward(
1454 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
1455 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle)
Andrew Walbranca808b12020-05-15 17:22:28 +01001456{
1457 struct ffa_value ret;
1458
J-Alves8505a8a2022-06-15 18:10:18 +01001459 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1460 fragment, fragment_length);
1461 other_world_locked.vm->mailbox.recv_size = fragment_length;
1462 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1463 other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1464 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001465 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001466 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1467 .arg1 = (uint32_t)handle,
1468 .arg2 = (uint32_t)(handle >> 32),
1469 .arg3 = fragment_length,
1470 .arg4 = (uint64_t)sender_vm_id << 16});
1471 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001472 * After the call to the other world completes it must have finished
1473 * reading its RX buffer, so it is ready for another message.
Andrew Walbranca808b12020-05-15 17:22:28 +01001474 */
J-Alves8505a8a2022-06-15 18:10:18 +01001475 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbranca808b12020-05-15 17:22:28 +01001476
1477 return ret;
1478}
1479
1480/**
J-Alves95df0ef2022-12-07 10:09:48 +00001481 * Checks if there is at least one receiver from the other world.
1482 */
1483static bool memory_region_receivers_from_other_world(
1484 struct ffa_memory_region *memory_region)
1485{
1486 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1487 ffa_vm_id_t receiver = memory_region->receivers[i]
1488 .receiver_permissions.receiver;
1489 if (!vm_id_is_current_world(receiver)) {
1490 return true;
1491 }
1492 }
1493 return false;
1494}
1495
1496/**
J-Alves8505a8a2022-06-15 18:10:18 +01001497 * Validates a call to donate, lend or share memory to a non-other world VM and
1498 * then updates the stage-2 page tables. Specifically, check if the message
1499 * length and number of memory region constituents match, and if the transition
1500 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001501 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001502 * Assumes that the caller has already found and locked the sender VM and copied
1503 * the memory region descriptor from the sender's TX buffer to a freshly
1504 * allocated page from Hafnium's internal pool. The caller must have also
1505 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001506 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001507 * This function takes ownership of the `memory_region` passed in and will free
1508 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001509 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001510struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001511 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001512 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001513 uint32_t fragment_length, uint32_t share_func,
1514 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001515{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001516 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001517 struct share_states_locked share_states;
1518 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001519
1520 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001521 * If there is an error validating the `memory_region` then we need to
1522 * free it because we own it but we won't be storing it in a share state
1523 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001524 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001525 ret = ffa_memory_send_validate(from_locked, memory_region,
1526 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001527 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001528 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001529 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001530 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001531 }
1532
Andrew Walbrana65a1322020-04-06 19:32:32 +01001533 /* Set flag for share function, ready to be retrieved later. */
1534 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001535 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001536 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001538 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539 case FFA_MEM_LEND_32:
1540 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001541 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001543 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001544 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001545 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001546 }
1547
Andrew Walbranca808b12020-05-15 17:22:28 +01001548 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001549 /*
1550 * Allocate a share state before updating the page table. Otherwise if
1551 * updating the page table succeeded but allocating the share state
1552 * failed then it would leave the memory in a state where nobody could
1553 * get it back.
1554 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001555 if (!allocate_share_state(share_states, share_func, memory_region,
1556 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1557 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001558 dlog_verbose("Failed to allocate share state.\n");
1559 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 ret = ffa_error(FFA_NO_MEMORY);
1561 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 }
1563
Andrew Walbranca808b12020-05-15 17:22:28 +01001564 if (fragment_length == memory_share_length) {
1565 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001566 ret = ffa_memory_send_complete(
1567 from_locked, share_states, share_state, page_pool,
1568 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 } else {
1570 ret = (struct ffa_value){
1571 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001572 .arg1 = (uint32_t)memory_region->handle,
1573 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001574 .arg3 = fragment_length};
1575 }
1576
1577out:
1578 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001579 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001580 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001581}
1582
1583/**
J-Alves8505a8a2022-06-15 18:10:18 +01001584 * Continues an operation to donate, lend or share memory to a VM from current
1585 * world. If this is the last fragment then checks that the transition is valid
1586 * for the type of memory sending operation and updates the stage-2 page tables
1587 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001588 *
1589 * Assumes that the caller has already found and locked the sender VM and copied
1590 * the memory region descriptor from the sender's TX buffer to a freshly
1591 * allocated page from Hafnium's internal pool.
1592 *
1593 * This function takes ownership of the `fragment` passed in; it must not be
1594 * freed by the caller.
1595 */
1596struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1597 void *fragment,
1598 uint32_t fragment_length,
1599 ffa_memory_handle_t handle,
1600 struct mpool *page_pool)
1601{
1602 struct share_states_locked share_states = share_states_lock();
1603 struct ffa_memory_share_state *share_state;
1604 struct ffa_value ret;
1605 struct ffa_memory_region *memory_region;
1606
1607 ret = ffa_memory_send_continue_validate(share_states, handle,
1608 &share_state,
1609 from_locked.vm->id, page_pool);
1610 if (ret.func != FFA_SUCCESS_32) {
1611 goto out_free_fragment;
1612 }
1613 memory_region = share_state->memory_region;
1614
J-Alves95df0ef2022-12-07 10:09:48 +00001615 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001616 dlog_error(
1617 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001618 "other world. This should never happen, and indicates "
1619 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001620 "EL3 code.\n");
1621 ret = ffa_error(FFA_INVALID_PARAMETERS);
1622 goto out_free_fragment;
1623 }
1624
1625 /* Add this fragment. */
1626 share_state->fragments[share_state->fragment_count] = fragment;
1627 share_state->fragment_constituent_counts[share_state->fragment_count] =
1628 fragment_length / sizeof(struct ffa_memory_region_constituent);
1629 share_state->fragment_count++;
1630
1631 /* Check whether the memory send operation is now ready to complete. */
1632 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001633 ret = ffa_memory_send_complete(
1634 from_locked, share_states, share_state, page_pool,
1635 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001636 } else {
1637 ret = (struct ffa_value){
1638 .func = FFA_MEM_FRAG_RX_32,
1639 .arg1 = (uint32_t)handle,
1640 .arg2 = (uint32_t)(handle >> 32),
1641 .arg3 = share_state_next_fragment_offset(share_states,
1642 share_state)};
1643 }
1644 goto out;
1645
1646out_free_fragment:
1647 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001648
1649out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001651 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001652}
1653
Andrew Walbranca808b12020-05-15 17:22:28 +01001654/**
J-Alves8505a8a2022-06-15 18:10:18 +01001655 * Continues an operation to donate, lend or share memory to the other world VM.
1656 * If this is the last fragment then checks that the transition is valid for the
1657 * type of memory sending operation and updates the stage-2 page tables of the
1658 * sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001659 *
1660 * Assumes that the caller has already found and locked the sender VM and copied
1661 * the memory region descriptor from the sender's TX buffer to a freshly
1662 * allocated page from Hafnium's internal pool.
1663 *
1664 * This function takes ownership of the `memory_region` passed in and will free
1665 * it when necessary; it must not be freed by the caller.
1666 */
J-Alves8505a8a2022-06-15 18:10:18 +01001667struct ffa_value ffa_memory_other_world_send_continue(
1668 struct vm_locked from_locked, struct vm_locked to_locked,
1669 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
1670 struct mpool *page_pool)
Andrew Walbranca808b12020-05-15 17:22:28 +01001671{
1672 struct share_states_locked share_states = share_states_lock();
1673 struct ffa_memory_share_state *share_state;
1674 struct ffa_value ret;
1675 struct ffa_memory_region *memory_region;
1676
1677 ret = ffa_memory_send_continue_validate(share_states, handle,
1678 &share_state,
1679 from_locked.vm->id, page_pool);
1680 if (ret.func != FFA_SUCCESS_32) {
1681 goto out_free_fragment;
1682 }
1683 memory_region = share_state->memory_region;
1684
J-Alves95df0ef2022-12-07 10:09:48 +00001685 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001686 dlog_error(
J-Alves8505a8a2022-06-15 18:10:18 +01001687 "Got SPM-allocated handle for memory send to non-other "
1688 "world VM. This should never happen, and indicates a "
1689 "bug.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001690 ret = ffa_error(FFA_INVALID_PARAMETERS);
1691 goto out_free_fragment;
1692 }
1693
1694 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1695 to_locked.vm->mailbox.recv == NULL) {
1696 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001697 * If the other world RX buffer is not available, tell the
1698 * sender to retry by returning the current offset again.
Andrew Walbranca808b12020-05-15 17:22:28 +01001699 */
1700 ret = (struct ffa_value){
1701 .func = FFA_MEM_FRAG_RX_32,
1702 .arg1 = (uint32_t)handle,
1703 .arg2 = (uint32_t)(handle >> 32),
1704 .arg3 = share_state_next_fragment_offset(share_states,
1705 share_state),
1706 };
1707 goto out_free_fragment;
1708 }
1709
1710 /* Add this fragment. */
1711 share_state->fragments[share_state->fragment_count] = fragment;
1712 share_state->fragment_constituent_counts[share_state->fragment_count] =
1713 fragment_length / sizeof(struct ffa_memory_region_constituent);
1714 share_state->fragment_count++;
1715
1716 /* Check whether the memory send operation is now ready to complete. */
1717 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001718 struct mpool local_page_pool;
1719 uint32_t orig_from_mode;
1720
1721 /*
1722 * Use a local page pool so that we can roll back if necessary.
1723 */
1724 mpool_init_with_fallback(&local_page_pool, page_pool);
1725
Andrew Walbranca808b12020-05-15 17:22:28 +01001726 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001727 share_state, &local_page_pool,
1728 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001729
1730 if (ret.func == FFA_SUCCESS_32) {
1731 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001732 * Forward final fragment on to the other world so that
Andrew Walbranca808b12020-05-15 17:22:28 +01001733 * it can complete the memory sending operation.
1734 */
J-Alves8505a8a2022-06-15 18:10:18 +01001735 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001736 to_locked, from_locked.vm->id, fragment,
1737 fragment_length, handle);
1738
1739 if (ret.func != FFA_SUCCESS_32) {
1740 /*
1741 * The error will be passed on to the caller,
1742 * but log it here too.
1743 */
1744 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001745 "other world didn't successfully "
1746 "complete "
Andrew Walbranca808b12020-05-15 17:22:28 +01001747 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001748 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001749 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001750
1751 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001752 * The other world failed to complete the send
Andrew Walbran37c574e2020-06-03 11:45:46 +01001753 * operation, so roll back the page table update
1754 * for the VM. This can't fail because it won't
1755 * try to allocate more memory than was freed
1756 * into the `local_page_pool` by
1757 * `ffa_send_check_update` in the initial
1758 * update.
1759 */
1760 CHECK(ffa_region_group_identity_map(
1761 from_locked, share_state->fragments,
1762 share_state
1763 ->fragment_constituent_counts,
1764 share_state->fragment_count,
1765 orig_from_mode, &local_page_pool,
1766 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001767 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001768
Andrew Walbranca808b12020-05-15 17:22:28 +01001769 /* Free share state. */
1770 share_state_free(share_states, share_state, page_pool);
1771 } else {
J-Alves8505a8a2022-06-15 18:10:18 +01001772 /* Abort sending to other world. */
1773 struct ffa_value other_world_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02001774 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01001775 .func = FFA_MEM_RECLAIM_32,
1776 .arg1 = (uint32_t)handle,
1777 .arg2 = (uint32_t)(handle >> 32)});
1778
J-Alves8505a8a2022-06-15 18:10:18 +01001779 if (other_world_ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001780 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001781 * Nothing we can do if other world doesn't
1782 * abort properly, just log it.
Andrew Walbranca808b12020-05-15 17:22:28 +01001783 */
1784 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001785 "other world didn't successfully abort "
1786 "failed "
Andrew Walbranca808b12020-05-15 17:22:28 +01001787 "memory send operation; returned %#x "
1788 "(%d).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01001789 other_world_ret.func,
1790 other_world_ret.arg2);
Andrew Walbranca808b12020-05-15 17:22:28 +01001791 }
1792 /*
1793 * We don't need to free the share state in this case
1794 * because ffa_memory_send_complete does that already.
1795 */
1796 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001797
1798 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001799 } else {
1800 uint32_t next_fragment_offset =
1801 share_state_next_fragment_offset(share_states,
1802 share_state);
1803
J-Alves8505a8a2022-06-15 18:10:18 +01001804 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001805 to_locked, from_locked.vm->id, fragment,
1806 fragment_length, handle);
1807
1808 if (ret.func != FFA_MEM_FRAG_RX_32 ||
1809 ffa_frag_handle(ret) != handle ||
1810 ret.arg3 != next_fragment_offset ||
1811 ffa_frag_sender(ret) != from_locked.vm->id) {
1812 dlog_verbose(
1813 "Got unexpected result from forwarding "
J-Alves8505a8a2022-06-15 18:10:18 +01001814 "FFA_MEM_FRAG_TX to other world. %#x (handle "
1815 "%#x, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001816 "offset %d, sender %d); expected "
1817 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
1818 "sender %d).\n",
1819 ret.func, ffa_frag_handle(ret), ret.arg3,
1820 ffa_frag_sender(ret), handle,
1821 next_fragment_offset, from_locked.vm->id);
1822 /* Free share state. */
1823 share_state_free(share_states, share_state, page_pool);
1824 ret = ffa_error(FFA_INVALID_PARAMETERS);
1825 goto out;
1826 }
1827
1828 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
1829 .arg1 = (uint32_t)handle,
1830 .arg2 = (uint32_t)(handle >> 32),
1831 .arg3 = next_fragment_offset};
1832 }
1833 goto out;
1834
1835out_free_fragment:
1836 mpool_free(page_pool, fragment);
1837
1838out:
1839 share_states_unlock(&share_states);
1840 return ret;
1841}
1842
1843/** Clean up after the receiver has finished retrieving a memory region. */
1844static void ffa_memory_retrieve_complete(
1845 struct share_states_locked share_states,
1846 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1847{
1848 if (share_state->share_func == FFA_MEM_DONATE_32) {
1849 /*
1850 * Memory that has been donated can't be relinquished,
1851 * so no need to keep the share state around.
1852 */
1853 share_state_free(share_states, share_state, page_pool);
1854 dlog_verbose("Freed share state for donate.\n");
1855 }
1856}
1857
J-Alves96de29f2022-04-26 16:05:24 +01001858/*
1859 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1860 * returns its index in the receiver's array. If receiver's ID doesn't exist
1861 * in the array, return the region's 'receiver_count'.
1862 */
1863static uint32_t ffa_memory_region_get_receiver(
1864 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
1865{
1866 struct ffa_memory_access *receivers;
1867 uint32_t i;
1868
1869 assert(memory_region != NULL);
1870
1871 receivers = memory_region->receivers;
1872
1873 for (i = 0U; i < memory_region->receiver_count; i++) {
1874 if (receivers[i].receiver_permissions.receiver == receiver) {
1875 break;
1876 }
1877 }
1878
1879 return i;
1880}
1881
1882/**
1883 * Validates the retrieved permissions against those specified by the lender
1884 * of memory share operation. Optionally can help set the permissions to be used
1885 * for the S2 mapping, through the `permissions` argument.
1886 * Returns true if permissions are valid, false otherwise.
1887 */
1888static bool ffa_memory_retrieve_is_memory_access_valid(
1889 enum ffa_data_access sent_data_access,
1890 enum ffa_data_access requested_data_access,
1891 enum ffa_instruction_access sent_instruction_access,
1892 enum ffa_instruction_access requested_instruction_access,
1893 ffa_memory_access_permissions_t *permissions)
1894{
1895 switch (sent_data_access) {
1896 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1897 case FFA_DATA_ACCESS_RW:
1898 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1899 requested_data_access == FFA_DATA_ACCESS_RW) {
1900 if (permissions != NULL) {
1901 ffa_set_data_access_attr(permissions,
1902 FFA_DATA_ACCESS_RW);
1903 }
1904 break;
1905 }
1906 /* Intentional fall-through. */
1907 case FFA_DATA_ACCESS_RO:
1908 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1909 requested_data_access == FFA_DATA_ACCESS_RO) {
1910 if (permissions != NULL) {
1911 ffa_set_data_access_attr(permissions,
1912 FFA_DATA_ACCESS_RO);
1913 }
1914 break;
1915 }
1916 dlog_verbose(
1917 "Invalid data access requested; sender specified "
1918 "permissions %#x but receiver requested %#x.\n",
1919 sent_data_access, requested_data_access);
1920 return false;
1921 case FFA_DATA_ACCESS_RESERVED:
1922 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1923 "checked before this point.");
1924 }
1925
1926 switch (sent_instruction_access) {
1927 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1928 case FFA_INSTRUCTION_ACCESS_X:
1929 if (requested_instruction_access ==
1930 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1931 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1932 if (permissions != NULL) {
1933 ffa_set_instruction_access_attr(
1934 permissions, FFA_INSTRUCTION_ACCESS_X);
1935 }
1936 break;
1937 }
1938 case FFA_INSTRUCTION_ACCESS_NX:
1939 if (requested_instruction_access ==
1940 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1941 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1942 if (permissions != NULL) {
1943 ffa_set_instruction_access_attr(
1944 permissions, FFA_INSTRUCTION_ACCESS_NX);
1945 }
1946 break;
1947 }
1948 dlog_verbose(
1949 "Invalid instruction access requested; sender "
1950 "specified permissions %#x but receiver requested "
1951 "%#x.\n",
1952 sent_instruction_access, requested_instruction_access);
1953 return false;
1954 case FFA_INSTRUCTION_ACCESS_RESERVED:
1955 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1956 "be checked before this point.");
1957 }
1958
1959 return true;
1960}
1961
1962/**
1963 * Validate the receivers' permissions in the retrieve request against those
1964 * specified by the lender.
1965 * In the `permissions` argument returns the permissions to set at S2 for the
1966 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1967 * Returns FFA_SUCCESS if all specified permissions are valid.
1968 */
1969static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1970 struct ffa_memory_region *memory_region,
1971 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1972 ffa_memory_access_permissions_t *permissions)
1973{
1974 uint32_t retrieve_receiver_index;
1975
1976 assert(permissions != NULL);
1977
1978 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1979 dlog_verbose(
1980 "Retrieve request should contain same list of "
1981 "borrowers, as specified by the lender.\n");
1982 return ffa_error(FFA_INVALID_PARAMETERS);
1983 }
1984
1985 retrieve_receiver_index = retrieve_request->receiver_count;
1986
1987 /* Should be populated with the permissions of the retriever. */
1988 *permissions = 0;
1989
1990 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1991 ffa_memory_access_permissions_t sent_permissions;
1992 struct ffa_memory_access *current_receiver =
1993 &retrieve_request->receivers[i];
1994 ffa_memory_access_permissions_t requested_permissions =
1995 current_receiver->receiver_permissions.permissions;
1996 ffa_vm_id_t current_receiver_id =
1997 current_receiver->receiver_permissions.receiver;
1998 bool found_to_id = current_receiver_id == to_vm_id;
1999
2000 /*
2001 * Find the current receiver in the transaction descriptor from
2002 * sender.
2003 */
2004 uint32_t mem_region_receiver_index =
2005 ffa_memory_region_get_receiver(memory_region,
2006 current_receiver_id);
2007
2008 if (mem_region_receiver_index ==
2009 memory_region->receiver_count) {
2010 dlog_verbose("%s: receiver %x not found\n", __func__,
2011 current_receiver_id);
2012 return ffa_error(FFA_DENIED);
2013 }
2014
2015 sent_permissions =
2016 memory_region->receivers[mem_region_receiver_index]
2017 .receiver_permissions.permissions;
2018
2019 if (found_to_id) {
2020 retrieve_receiver_index = i;
2021 }
2022
2023 /*
2024 * Since we are traversing the list of receivers, save the index
2025 * of the caller. As it needs to be there.
2026 */
2027
2028 if (current_receiver->composite_memory_region_offset != 0U) {
2029 dlog_verbose(
2030 "Retriever specified address ranges not "
2031 "supported (got offset %d).\n",
2032 current_receiver
2033 ->composite_memory_region_offset);
2034 return ffa_error(FFA_INVALID_PARAMETERS);
2035 }
2036
2037 /*
2038 * Check permissions from sender against permissions requested
2039 * by receiver.
2040 */
2041 if (!ffa_memory_retrieve_is_memory_access_valid(
2042 ffa_get_data_access_attr(sent_permissions),
2043 ffa_get_data_access_attr(requested_permissions),
2044 ffa_get_instruction_access_attr(sent_permissions),
2045 ffa_get_instruction_access_attr(
2046 requested_permissions),
2047 found_to_id ? permissions : NULL)) {
2048 return ffa_error(FFA_DENIED);
2049 }
2050
2051 /*
2052 * Can't request PM to clear memory if only provided with RO
2053 * permissions.
2054 */
2055 if (found_to_id &&
2056 (ffa_get_data_access_attr(*permissions) ==
2057 FFA_DATA_ACCESS_RO) &&
2058 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2059 0U) {
2060 dlog_verbose(
2061 "Receiver has RO permissions can not request "
2062 "clear.\n");
2063 return ffa_error(FFA_DENIED);
2064 }
2065 }
2066
2067 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2068 dlog_verbose(
2069 "Retrieve request does not contain caller's (%x) "
2070 "permissions\n",
2071 to_vm_id);
2072 return ffa_error(FFA_INVALID_PARAMETERS);
2073 }
2074
2075 return (struct ffa_value){.func = FFA_SUCCESS_32};
2076}
2077
J-Alvesa9cd7e32022-07-01 13:49:33 +01002078/*
2079 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2080 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2081 * of a pending memory sharing operation whose allocator is the SPM, for
2082 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2083 * the memory region descriptor of the retrieve request must be zeroed with the
2084 * exception of the sender ID and handle.
2085 */
2086bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2087 struct vm_locked to_locked)
2088{
2089 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2090 request->attributes == 0U && request->flags == 0U &&
2091 request->tag == 0U && request->receiver_count == 0U &&
2092 plat_ffa_memory_handle_allocated_by_current_world(
2093 request->handle);
2094}
2095
2096/*
2097 * Helper to reset count of fragments retrieved by the hypervisor.
2098 */
2099static void ffa_memory_retrieve_complete_from_hyp(
2100 struct ffa_memory_share_state *share_state)
2101{
2102 if (share_state->hypervisor_fragment_count ==
2103 share_state->fragment_count) {
2104 share_state->hypervisor_fragment_count = 0;
2105 }
2106}
2107
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002108struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2109 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002110 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002112{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002113 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002114 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002115 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002116 sizeof(struct ffa_memory_access);
2117 ffa_memory_handle_t handle = retrieve_request->handle;
2118 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002119 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2121 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002122 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002123 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002124 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002125 struct ffa_memory_share_state *share_state;
2126 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002127 struct ffa_composite_memory_region *composite;
2128 uint32_t total_length;
2129 uint32_t fragment_length;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002130 ffa_vm_id_t receiver_id;
2131 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002132
2133 dump_share_states();
2134
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002135 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002136 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002137 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002138 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002139 expected_retrieve_request_length,
2140 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002141 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002142 }
2143
2144 share_states = share_states_lock();
2145 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002146 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002147 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002149 goto out;
2150 }
2151
J-Alves96de29f2022-04-26 16:05:24 +01002152 if (!share_state->sending_complete) {
2153 dlog_verbose(
2154 "Memory with handle %#x not fully sent, can't "
2155 "retrieve.\n",
2156 handle);
2157 ret = ffa_error(FFA_INVALID_PARAMETERS);
2158 goto out;
2159 }
2160
Andrew Walbrana65a1322020-04-06 19:32:32 +01002161 memory_region = share_state->memory_region;
2162 CHECK(memory_region != NULL);
2163
J-Alvesa9cd7e32022-07-01 13:49:33 +01002164 receiver_id = to_locked.vm->id;
J-Alves96de29f2022-04-26 16:05:24 +01002165
J-Alvesa9cd7e32022-07-01 13:49:33 +01002166 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2167 to_locked)) {
2168 uint32_t receiver_index;
J-Alves614d9f42022-06-28 14:03:10 +01002169 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002170 * Find receiver index in the receivers list specified by the
2171 * sender.
J-Alves614d9f42022-06-28 14:03:10 +01002172 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01002173 receiver_index = ffa_memory_region_get_receiver(
2174 memory_region, to_locked.vm->id);
2175
2176 if (receiver_index == memory_region->receiver_count) {
2177 dlog_verbose(
2178 "Incorrect receiver VM ID %x for "
2179 "FFA_MEM_RETRIEVE_REQ, "
2180 "for handle %#x.\n",
2181 to_locked.vm->id, handle);
2182 ret = ffa_error(FFA_INVALID_PARAMETERS);
2183 goto out;
2184 }
2185
2186 if (share_state->retrieved_fragment_count[receiver_index] !=
2187 0U) {
2188 dlog_verbose(
2189 "Memory with handle %#x already retrieved.\n",
2190 handle);
2191 ret = ffa_error(FFA_DENIED);
2192 goto out;
2193 }
2194
2195 /*
2196 * Check that the transaction type expected by the receiver is
2197 * correct, if it has been specified.
2198 */
2199 if (transaction_type !=
2200 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2201 transaction_type !=
2202 (memory_region->flags &
2203 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2204 dlog_verbose(
2205 "Incorrect transaction type %#x for "
2206 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle "
2207 "%#x.\n",
2208 transaction_type,
2209 memory_region->flags &
2210 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2211 handle);
2212 ret = ffa_error(FFA_INVALID_PARAMETERS);
2213 goto out;
2214 }
2215
2216 if (retrieve_request->sender != memory_region->sender) {
2217 dlog_verbose(
2218 "Incorrect sender ID %d for "
2219 "FFA_MEM_RETRIEVE_REQ, "
2220 "expected %d for handle %#x.\n",
2221 retrieve_request->sender, memory_region->sender,
2222 handle);
2223 ret = ffa_error(FFA_DENIED);
2224 goto out;
2225 }
2226
2227 if (retrieve_request->tag != memory_region->tag) {
2228 dlog_verbose(
2229 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, "
2230 "expected "
2231 "%d for handle %#x.\n",
2232 retrieve_request->tag, memory_region->tag,
2233 handle);
2234 ret = ffa_error(FFA_INVALID_PARAMETERS);
2235 goto out;
2236 }
2237
2238 if ((retrieve_request->flags &
2239 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2240 dlog_verbose(
2241 "Retriever specified 'address range alignment "
2242 "hint' not supported.\n");
2243 ret = ffa_error(FFA_INVALID_PARAMETERS);
2244 goto out;
2245 }
2246
2247 if ((retrieve_request->flags &
2248 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2249 dlog_verbose(
2250 "Bits 8-5 must be zero in memory region's "
2251 "flags (address range alignment hint not "
2252 "supported).\n");
2253 ret = ffa_error(FFA_INVALID_PARAMETERS);
2254 goto out;
2255 }
2256
2257 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2258 dlog_verbose(
2259 "Bits 31-10 must be zero in memory region's "
2260 "flags.\n");
2261 ret = ffa_error(FFA_INVALID_PARAMETERS);
2262 goto out;
2263 }
2264
2265 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2266 (retrieve_request->flags &
2267 (FFA_MEMORY_REGION_FLAG_CLEAR |
2268 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2269 dlog_verbose(
2270 "Memory Share operation can't clean after "
2271 "relinquish "
2272 "memory region.\n");
2273 ret = ffa_error(FFA_INVALID_PARAMETERS);
2274 goto out;
2275 }
2276
2277 /*
2278 * If the borrower needs the memory to be cleared before mapping
2279 * to its address space, the sender should have set the flag
2280 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2281 * FFA_DENIED.
2282 */
2283 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2284 0U &&
2285 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) ==
2286 0U) {
2287 dlog_verbose(
2288 "Borrower needs memory cleared. Sender needs "
2289 "to set "
2290 "flag for clearing memory.\n");
2291 ret = ffa_error(FFA_DENIED);
2292 goto out;
2293 }
2294
2295 ret = ffa_memory_retrieve_validate_memory_access_list(
2296 memory_region, retrieve_request, receiver_id,
2297 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002298 if (ret.func != FFA_SUCCESS_32) {
2299 goto out;
2300 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002301
J-Alvesa9cd7e32022-07-01 13:49:33 +01002302 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2303 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2304 /*
2305 * Ensure receiver's attributes are compatible with how
2306 * Hafnium maps memory: Normal Memory, Inner shareable,
2307 * Write-Back Read-Allocate Write-Allocate Cacheable.
2308 */
2309 ret = ffa_memory_attributes_validate(
2310 retrieve_request->attributes);
2311 if (ret.func != FFA_SUCCESS_32) {
2312 goto out;
2313 }
2314 }
2315
2316 memory_to_attributes = ffa_memory_permissions_to_mode(
2317 permissions, share_state->sender_orig_mode);
2318 ret = ffa_retrieve_check_update(
2319 to_locked, memory_region->sender,
2320 share_state->fragments,
2321 share_state->fragment_constituent_counts,
2322 share_state->fragment_count, memory_to_attributes,
2323 share_state->share_func, false, page_pool);
2324
2325 if (ret.func != FFA_SUCCESS_32) {
2326 goto out;
2327 }
2328
2329 share_state->retrieved_fragment_count[receiver_index] = 1;
2330 is_send_complete =
2331 share_state->retrieved_fragment_count[receiver_index] ==
2332 share_state->fragment_count;
2333 } else {
2334 if (share_state->hypervisor_fragment_count != 0U) {
2335 dlog_verbose(
2336 "Memory with handle %#x already "
2337 "retrieved by "
2338 "the hypervisor.\n",
2339 handle);
2340 ret = ffa_error(FFA_DENIED);
2341 goto out;
2342 }
2343
2344 share_state->hypervisor_fragment_count = 1;
2345
2346 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002347 }
2348
2349 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002350 * Copy response to RX buffer of caller and deliver the message.
2351 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002352 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002353 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002354 composite = ffa_memory_region_get_composite(memory_region, 0);
2355 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002356 * Constituents which we received in the first fragment should
2357 * always fit in the first fragment we are sending, because the
2358 * header is the same size in both cases and we have a fixed
2359 * message buffer size. So `ffa_retrieved_memory_region_init`
2360 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002361 */
2362 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002363 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2364 memory_region->sender, memory_region->attributes,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002365 memory_region->flags, handle, receiver_id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 composite->page_count, composite->constituent_count,
2367 share_state->fragments[0],
2368 share_state->fragment_constituent_counts[0], &total_length,
2369 &fragment_length));
2370 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002371 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002372 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002373 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2374
J-Alvesa9cd7e32022-07-01 13:49:33 +01002375 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002376 ffa_memory_retrieve_complete(share_states, share_state,
2377 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002378 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002379 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002380 .arg1 = total_length,
2381 .arg2 = fragment_length};
2382
2383out:
2384 share_states_unlock(&share_states);
2385 dump_share_states();
2386 return ret;
2387}
2388
2389struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2390 ffa_memory_handle_t handle,
2391 uint32_t fragment_offset,
2392 struct mpool *page_pool)
2393{
2394 struct ffa_memory_region *memory_region;
2395 struct share_states_locked share_states;
2396 struct ffa_memory_share_state *share_state;
2397 struct ffa_value ret;
2398 uint32_t fragment_index;
2399 uint32_t retrieved_constituents_count;
2400 uint32_t i;
2401 uint32_t expected_fragment_offset;
2402 uint32_t remaining_constituent_count;
2403 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002404 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002405
2406 dump_share_states();
2407
2408 share_states = share_states_lock();
2409 if (!get_share_state(share_states, handle, &share_state)) {
2410 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2411 handle);
2412 ret = ffa_error(FFA_INVALID_PARAMETERS);
2413 goto out;
2414 }
2415
2416 memory_region = share_state->memory_region;
2417 CHECK(memory_region != NULL);
2418
J-Alvesc7484f12022-05-13 12:41:14 +01002419 receiver_index =
2420 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2421
2422 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002423 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002424 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2425 "memory sharing transaction (%x)\n",
2426 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002427 ret = ffa_error(FFA_INVALID_PARAMETERS);
2428 goto out;
2429 }
2430
2431 if (!share_state->sending_complete) {
2432 dlog_verbose(
2433 "Memory with handle %#x not fully sent, can't "
2434 "retrieve.\n",
2435 handle);
2436 ret = ffa_error(FFA_INVALID_PARAMETERS);
2437 goto out;
2438 }
2439
J-Alvesc7484f12022-05-13 12:41:14 +01002440 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2441 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002442 share_state->fragment_count) {
2443 dlog_verbose(
2444 "Retrieval of memory with handle %#x not yet started "
2445 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002446 handle,
2447 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002448 share_state->fragment_count);
2449 ret = ffa_error(FFA_INVALID_PARAMETERS);
2450 goto out;
2451 }
2452
J-Alvesc7484f12022-05-13 12:41:14 +01002453 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002454
2455 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002456 * Check that the given fragment offset is correct by counting
2457 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002458 */
2459 retrieved_constituents_count = 0;
2460 for (i = 0; i < fragment_index; ++i) {
2461 retrieved_constituents_count +=
2462 share_state->fragment_constituent_counts[i];
2463 }
J-Alvesc7484f12022-05-13 12:41:14 +01002464
2465 CHECK(memory_region->receiver_count > 0);
2466
Andrew Walbranca808b12020-05-15 17:22:28 +01002467 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002468 ffa_composite_constituent_offset(memory_region,
2469 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002470 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002471 sizeof(struct ffa_memory_region_constituent) -
2472 sizeof(struct ffa_memory_access) *
2473 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002474 if (fragment_offset != expected_fragment_offset) {
2475 dlog_verbose("Fragment offset was %d but expected %d.\n",
2476 fragment_offset, expected_fragment_offset);
2477 ret = ffa_error(FFA_INVALID_PARAMETERS);
2478 goto out;
2479 }
2480
2481 remaining_constituent_count = ffa_memory_fragment_init(
2482 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2483 share_state->fragments[fragment_index],
2484 share_state->fragment_constituent_counts[fragment_index],
2485 &fragment_length);
2486 CHECK(remaining_constituent_count == 0);
2487 to_locked.vm->mailbox.recv_size = fragment_length;
2488 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2489 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2490 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002491 share_state->retrieved_fragment_count[receiver_index]++;
2492 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002493 share_state->fragment_count) {
2494 ffa_memory_retrieve_complete(share_states, share_state,
2495 page_pool);
2496 }
2497
2498 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2499 .arg1 = (uint32_t)handle,
2500 .arg2 = (uint32_t)(handle >> 32),
2501 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502
2503out:
2504 share_states_unlock(&share_states);
2505 dump_share_states();
2506 return ret;
2507}
2508
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002509struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002510 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002511 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002512{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002513 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002514 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002515 struct ffa_memory_share_state *share_state;
2516 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002517 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002518 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002519 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002520
Andrew Walbrana65a1322020-04-06 19:32:32 +01002521 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002522 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002523 "Stream endpoints not supported (got %d "
2524 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002525 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002527 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002528 }
2529
Andrew Walbrana65a1322020-04-06 19:32:32 +01002530 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002532 "VM ID %d in relinquish message doesn't match "
2533 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002534 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002535 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002536 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002537 }
2538
2539 dump_share_states();
2540
2541 share_states = share_states_lock();
2542 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002543 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002545 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546 goto out;
2547 }
2548
Andrew Walbranca808b12020-05-15 17:22:28 +01002549 if (!share_state->sending_complete) {
2550 dlog_verbose(
2551 "Memory with handle %#x not fully sent, can't "
2552 "relinquish.\n",
2553 handle);
2554 ret = ffa_error(FFA_INVALID_PARAMETERS);
2555 goto out;
2556 }
2557
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 memory_region = share_state->memory_region;
2559 CHECK(memory_region != NULL);
2560
J-Alves8eb19162022-04-28 10:56:48 +01002561 receiver_index = ffa_memory_region_get_receiver(memory_region,
2562 from_locked.vm->id);
2563
2564 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002565 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002566 "VM ID %d tried to relinquish memory region "
2567 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002568 "handle %#x and it is not a valid borrower.\n",
2569 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002570 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002571 goto out;
2572 }
2573
J-Alves8eb19162022-04-28 10:56:48 +01002574 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002575 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002576 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002577 "Memory with handle %#x not yet fully "
2578 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002579 "receiver %x can't relinquish.\n",
2580 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002581 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002582 goto out;
2583 }
2584
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002585 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002586
2587 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002588 * Clear is not allowed for memory that was shared, as the
2589 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002590 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002591 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002592 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002593 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002594 goto out;
2595 }
2596
Andrew Walbranca808b12020-05-15 17:22:28 +01002597 ret = ffa_relinquish_check_update(
2598 from_locked, share_state->fragments,
2599 share_state->fragment_constituent_counts,
2600 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002601
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002603 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002604 * Mark memory handle as not retrieved, so it can be
2605 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002606 */
J-Alves8eb19162022-04-28 10:56:48 +01002607 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002608 }
2609
2610out:
2611 share_states_unlock(&share_states);
2612 dump_share_states();
2613 return ret;
2614}
2615
2616/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002617 * Validates that the reclaim transition is allowed for the given
2618 * handle, updates the page table of the reclaiming VM, and frees the
2619 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002620 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002621struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002622 ffa_memory_handle_t handle,
2623 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002624 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002625{
2626 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002627 struct ffa_memory_share_state *share_state;
2628 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630
2631 dump_share_states();
2632
2633 share_states = share_states_lock();
2634 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002638 goto out;
2639 }
2640
2641 memory_region = share_state->memory_region;
2642 CHECK(memory_region != NULL);
2643
J-Alvesa9cd7e32022-07-01 13:49:33 +01002644 if (vm_id_is_current_world(to_locked.vm->id) &&
2645 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002646 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002647 "VM %#x attempted to reclaim memory handle %#x "
2648 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002649 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002650 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002651 goto out;
2652 }
2653
Andrew Walbranca808b12020-05-15 17:22:28 +01002654 if (!share_state->sending_complete) {
2655 dlog_verbose(
2656 "Memory with handle %#x not fully sent, can't "
2657 "reclaim.\n",
2658 handle);
2659 ret = ffa_error(FFA_INVALID_PARAMETERS);
2660 goto out;
2661 }
2662
J-Alves752236c2022-04-28 11:07:47 +01002663 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2664 if (share_state->retrieved_fragment_count[i] != 0) {
2665 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002666 "Tried to reclaim memory handle %#x "
2667 "that has "
2668 "not been relinquished by all "
2669 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002670 handle,
2671 memory_region->receivers[i]
2672 .receiver_permissions.receiver);
2673 ret = ffa_error(FFA_DENIED);
2674 goto out;
2675 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002676 }
2677
Andrew Walbranca808b12020-05-15 17:22:28 +01002678 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002679 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002680 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002681 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002682 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002683
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002684 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002685 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002686 dlog_verbose(
2687 "Freed share state after successful "
2688 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002689 }
2690
2691out:
2692 share_states_unlock(&share_states);
2693 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002694}