blob: 4e0f6841682d72980c096eaa6a698923f5fc2901 [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 */
J-Alvesb5084cf2022-07-06 14:20:12 +0100921struct 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)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100939 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +0100940 return ffa_error(FFA_INVALID_PARAMETERS);
941 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000942 }
943
944 /*
945 * Check if the state transition is lawful for the recipient, and ensure
946 * that all constituents of the memory region being retrieved are at the
947 * same state.
948 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100949 ret = ffa_retrieve_check_transition(
950 to_locked, share_func, fragments, fragment_constituent_counts,
951 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100952 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100953 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100954 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000955 }
956
957 /*
958 * Create a local pool so any freed memory can't be used by another
959 * thread. This is to ensure the original mapping can be restored if the
960 * clear fails.
961 */
962 mpool_init_with_fallback(&local_page_pool, page_pool);
963
964 /*
965 * First reserve all required memory for the new page table entries in
966 * the recipient page tables without committing, to make sure the entire
967 * operation will succeed without exhausting the page pool.
968 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100969 if (!ffa_region_group_identity_map(
970 to_locked, fragments, fragment_constituent_counts,
971 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972 /* TODO: partial defrag of failed range. */
973 dlog_verbose(
974 "Insufficient memory to update recipient page "
975 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100976 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000977 goto out;
978 }
979
980 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000981 if (clear &&
982 !ffa_clear_memory_constituents(
983 plat_ffa_owner_world_mode(from_id), fragments,
984 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100985 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000987 goto out;
988 }
989
Jose Marinho09b1db82019-08-08 09:16:59 +0100990 /*
991 * Complete the transfer by mapping the memory into the recipient. This
992 * won't allocate because the transaction was already prepared above, so
993 * it doesn't need to use the `local_page_pool`.
994 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100995 CHECK(ffa_region_group_identity_map(
996 to_locked, fragments, fragment_constituent_counts,
997 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100998
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100999 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001000
1001out:
1002 mpool_fini(&local_page_pool);
1003
1004 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001005 * Tidy up the page table by reclaiming failed mappings (if there was an
1006 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001007 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001008 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009
1010 return ret;
1011}
1012
Andrew Walbran996d1d12020-05-27 14:08:43 +01001013static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001014 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001015 struct ffa_memory_region_constituent **fragments,
1016 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1017 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018{
1019 uint32_t orig_from_mode;
1020 uint32_t from_mode;
1021 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001022 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001023
Andrew Walbranca808b12020-05-15 17:22:28 +01001024 ret = ffa_relinquish_check_transition(
1025 from_locked, &orig_from_mode, fragments,
1026 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001028 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001029 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 }
1031
1032 /*
1033 * Create a local pool so any freed memory can't be used by another
1034 * thread. This is to ensure the original mapping can be restored if the
1035 * clear fails.
1036 */
1037 mpool_init_with_fallback(&local_page_pool, page_pool);
1038
1039 /*
1040 * First reserve all required memory for the new page table entries
1041 * without committing, to make sure the entire operation will succeed
1042 * without exhausting the page pool.
1043 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001044 if (!ffa_region_group_identity_map(
1045 from_locked, fragments, fragment_constituent_counts,
1046 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001047 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 goto out;
1050 }
1051
1052 /*
1053 * Update the mapping for the sender. This won't allocate because the
1054 * transaction was already prepared above, but may free pages in the
1055 * case that a whole block is being unmapped that was previously
1056 * partially mapped.
1057 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001058 CHECK(ffa_region_group_identity_map(
1059 from_locked, fragments, fragment_constituent_counts,
1060 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001061
1062 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001063 if (clear &&
1064 !ffa_clear_memory_constituents(
1065 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1066 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001067 /*
1068 * On failure, roll back by returning memory to the sender. This
1069 * may allocate pages which were previously freed into
1070 * `local_page_pool` by the call above, but will never allocate
1071 * more pages than that so can never fail.
1072 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001074 from_locked, fragments, fragment_constituent_counts,
1075 fragment_count, orig_from_mode, &local_page_pool,
1076 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001077
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001079 goto out;
1080 }
1081
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083
1084out:
1085 mpool_fini(&local_page_pool);
1086
1087 /*
1088 * Tidy up the page table by reclaiming failed mappings (if there was an
1089 * error) or merging entries into blocks where possible (on success).
1090 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001091 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001092
1093 return ret;
1094}
1095
1096/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001097 * Complete a memory sending operation by checking that it is valid, updating
1098 * the sender page table, and then either marking the share state as having
1099 * completed sending (on success) or freeing it (on failure).
1100 *
1101 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1102 */
1103static struct ffa_value ffa_memory_send_complete(
1104 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001105 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1106 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001107{
1108 struct ffa_memory_region *memory_region = share_state->memory_region;
1109 struct ffa_value ret;
1110
1111 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001112 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001113
1114 /* Check that state is valid in sender page table and update. */
1115 ret = ffa_send_check_update(
1116 from_locked, share_state->fragments,
1117 share_state->fragment_constituent_counts,
1118 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001119 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001120 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1121 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001122 if (ret.func != FFA_SUCCESS_32) {
1123 /*
1124 * Free share state, it failed to send so it can't be retrieved.
1125 */
1126 dlog_verbose("Complete failed, freeing share state.\n");
1127 share_state_free(share_states, share_state, page_pool);
1128 return ret;
1129 }
1130
1131 share_state->sending_complete = true;
1132 dlog_verbose("Marked sending complete.\n");
1133
J-Alvesee68c542020-10-29 17:48:20 +00001134 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001135}
1136
1137/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001138 * Check that the memory attributes match Hafnium expectations:
1139 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1140 * Write-Allocate Cacheable.
1141 */
1142static struct ffa_value ffa_memory_attributes_validate(
1143 ffa_memory_access_permissions_t attributes)
1144{
1145 enum ffa_memory_type memory_type;
1146 enum ffa_memory_cacheability cacheability;
1147 enum ffa_memory_shareability shareability;
1148
1149 memory_type = ffa_get_memory_type_attr(attributes);
1150 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1151 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1152 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001153 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001154 }
1155
1156 cacheability = ffa_get_memory_cacheability_attr(attributes);
1157 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1158 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1159 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001160 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001161 }
1162
1163 shareability = ffa_get_memory_shareability_attr(attributes);
1164 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1165 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1166 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001167 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001168 }
1169
1170 return (struct ffa_value){.func = FFA_SUCCESS_32};
1171}
1172
1173/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001174 * Check that the given `memory_region` represents a valid memory send request
1175 * of the given `share_func` type, return the clear flag and permissions via the
1176 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001177 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001179 * not.
1180 */
J-Alves66652252022-07-06 09:49:51 +01001181struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001182 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1183 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001184 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001185{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001187 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001188 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001189 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001190 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001191 enum ffa_data_access data_access;
1192 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001193 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001194
J-Alves95df0ef2022-12-07 10:09:48 +00001195 /* The sender must match the caller. */
1196 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1197 vm_id_is_current_world(memory_region->sender)) ||
1198 (vm_id_is_current_world(from_locked.vm->id) &&
1199 memory_region->sender != from_locked.vm->id)) {
1200 dlog_verbose("Invalid memory sender ID.\n");
1201 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001202 }
1203
Andrew Walbrana65a1322020-04-06 19:32:32 +01001204 /*
1205 * Ensure that the composite header is within the memory bounds and
1206 * doesn't overlap the first part of the message.
1207 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001208 receivers_length = sizeof(struct ffa_memory_access) *
1209 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001210 constituents_offset =
1211 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001212 composite_memory_region_offset =
1213 memory_region->receivers[0].composite_memory_region_offset;
1214 if ((composite_memory_region_offset == 0) ||
1215 (composite_memory_region_offset <
1216 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001217 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001218 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001219 "Invalid composite memory region descriptor offset "
1220 "%d.\n",
1221 memory_region->receivers[0]
1222 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001223 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001224 }
1225
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001226 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001227
1228 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001229 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001230 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001231 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1232 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001233 if (memory_share_length != constituents_offset + constituents_length) {
1234 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001235 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001236 memory_region->receivers[0]
1237 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001238 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001239 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001240 if (fragment_length < memory_share_length &&
1241 fragment_length < HF_MAILBOX_SIZE) {
1242 dlog_warning(
1243 "Initial fragment length %d smaller than mailbox "
1244 "size.\n",
1245 fragment_length);
1246 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001247
Andrew Walbrana65a1322020-04-06 19:32:32 +01001248 /*
1249 * Clear is not allowed for memory sharing, as the sender still has
1250 * access to the memory.
1251 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001252 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1253 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001254 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001255 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001256 }
1257
1258 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001259 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001260 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001261 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001262 }
1263
J-Alves363f5722022-04-25 17:37:37 +01001264 /* Check that the permissions are valid, for each specified receiver. */
1265 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1266 ffa_memory_access_permissions_t permissions =
1267 memory_region->receivers[i]
1268 .receiver_permissions.permissions;
1269 ffa_vm_id_t receiver_id =
1270 memory_region->receivers[i]
1271 .receiver_permissions.receiver;
1272
1273 if (memory_region->sender == receiver_id) {
1274 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001275 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001276 }
Federico Recanati85090c42021-12-15 13:17:54 +01001277
J-Alves363f5722022-04-25 17:37:37 +01001278 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1279 j++) {
1280 if (receiver_id ==
1281 memory_region->receivers[j]
1282 .receiver_permissions.receiver) {
1283 dlog_verbose(
1284 "Repeated receiver(%x) in memory send "
1285 "operation.\n",
1286 memory_region->receivers[j]
1287 .receiver_permissions.receiver);
1288 return ffa_error(FFA_INVALID_PARAMETERS);
1289 }
1290 }
1291
1292 if (composite_memory_region_offset !=
1293 memory_region->receivers[i]
1294 .composite_memory_region_offset) {
1295 dlog_verbose(
1296 "All ffa_memory_access should point to the "
1297 "same composite memory region offset.\n");
1298 return ffa_error(FFA_INVALID_PARAMETERS);
1299 }
1300
1301 data_access = ffa_get_data_access_attr(permissions);
1302 instruction_access =
1303 ffa_get_instruction_access_attr(permissions);
1304 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1305 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1306 dlog_verbose(
1307 "Reserved value for receiver permissions "
1308 "%#x.\n",
1309 permissions);
1310 return ffa_error(FFA_INVALID_PARAMETERS);
1311 }
1312 if (instruction_access !=
1313 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1314 dlog_verbose(
1315 "Invalid instruction access permissions %#x "
1316 "for sending memory.\n",
1317 permissions);
1318 return ffa_error(FFA_INVALID_PARAMETERS);
1319 }
1320 if (share_func == FFA_MEM_SHARE_32) {
1321 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1322 dlog_verbose(
1323 "Invalid data access permissions %#x "
1324 "for sharing memory.\n",
1325 permissions);
1326 return ffa_error(FFA_INVALID_PARAMETERS);
1327 }
1328 /*
1329 * According to section 10.10.3 of the FF-A v1.1 EAC0
1330 * spec, NX is required for share operations (but must
1331 * not be specified by the sender) so set it in the
1332 * copy that we store, ready to be returned to the
1333 * retriever.
1334 */
J-Alvesb19731a2022-06-20 17:30:33 +01001335 if (vm_id_is_current_world(receiver_id)) {
1336 ffa_set_instruction_access_attr(
1337 &permissions,
1338 FFA_INSTRUCTION_ACCESS_NX);
1339 memory_region->receivers[i]
1340 .receiver_permissions.permissions =
1341 permissions;
1342 }
J-Alves363f5722022-04-25 17:37:37 +01001343 }
1344 if (share_func == FFA_MEM_LEND_32 &&
1345 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1346 dlog_verbose(
1347 "Invalid data access permissions %#x for "
1348 "lending memory.\n",
1349 permissions);
1350 return ffa_error(FFA_INVALID_PARAMETERS);
1351 }
1352
1353 if (share_func == FFA_MEM_DONATE_32 &&
1354 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1355 dlog_verbose(
1356 "Invalid data access permissions %#x for "
1357 "donating memory.\n",
1358 permissions);
1359 return ffa_error(FFA_INVALID_PARAMETERS);
1360 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001361 }
1362
Federico Recanatid937f5e2021-12-20 17:38:23 +01001363 /*
J-Alves807794e2022-06-16 13:42:47 +01001364 * If a memory donate or lend with single borrower, the memory type
1365 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001366 */
J-Alves807794e2022-06-16 13:42:47 +01001367 if (share_func == FFA_MEM_DONATE_32 ||
1368 (share_func == FFA_MEM_LEND_32 &&
1369 memory_region->receiver_count == 1)) {
1370 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1371 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1372 dlog_verbose(
1373 "Memory type shall not be specified by "
1374 "sender.\n");
1375 return ffa_error(FFA_INVALID_PARAMETERS);
1376 }
1377 } else {
1378 /*
1379 * Check that sender's memory attributes match Hafnium
1380 * expectations: Normal Memory, Inner shareable, Write-Back
1381 * Read-Allocate Write-Allocate Cacheable.
1382 */
1383 ret = ffa_memory_attributes_validate(memory_region->attributes);
1384 if (ret.func != FFA_SUCCESS_32) {
1385 return ret;
1386 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001387 }
1388
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390}
1391
1392/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001393 * Gets the share state for continuing an operation to donate, lend or share
1394 * memory, and checks that it is a valid request.
1395 *
1396 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1397 * not.
1398 */
1399static struct ffa_value ffa_memory_send_continue_validate(
1400 struct share_states_locked share_states, ffa_memory_handle_t handle,
1401 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1402 struct mpool *page_pool)
1403{
1404 struct ffa_memory_share_state *share_state;
1405 struct ffa_memory_region *memory_region;
1406
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001407 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001408
1409 /*
1410 * Look up the share state by handle and make sure that the VM ID
1411 * matches.
1412 */
1413 if (!get_share_state(share_states, handle, &share_state)) {
1414 dlog_verbose(
1415 "Invalid handle %#x for memory send continuation.\n",
1416 handle);
1417 return ffa_error(FFA_INVALID_PARAMETERS);
1418 }
1419 memory_region = share_state->memory_region;
1420
1421 if (memory_region->sender != from_vm_id) {
1422 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1423 return ffa_error(FFA_INVALID_PARAMETERS);
1424 }
1425
1426 if (share_state->sending_complete) {
1427 dlog_verbose(
1428 "Sending of memory handle %#x is already complete.\n",
1429 handle);
1430 return ffa_error(FFA_INVALID_PARAMETERS);
1431 }
1432
1433 if (share_state->fragment_count == MAX_FRAGMENTS) {
1434 /*
1435 * Log a warning as this is a sign that MAX_FRAGMENTS should
1436 * probably be increased.
1437 */
1438 dlog_warning(
1439 "Too many fragments for memory share with handle %#x; "
1440 "only %d supported.\n",
1441 handle, MAX_FRAGMENTS);
1442 /* Free share state, as it's not possible to complete it. */
1443 share_state_free(share_states, share_state, page_pool);
1444 return ffa_error(FFA_NO_MEMORY);
1445 }
1446
1447 *share_state_ret = share_state;
1448
1449 return (struct ffa_value){.func = FFA_SUCCESS_32};
1450}
1451
1452/**
J-Alves8505a8a2022-06-15 18:10:18 +01001453 * Forwards a memory send continuation message on to the other world.
Andrew Walbranca808b12020-05-15 17:22:28 +01001454 */
J-Alves8505a8a2022-06-15 18:10:18 +01001455static struct ffa_value memory_send_continue_other_world_forward(
1456 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
1457 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle)
Andrew Walbranca808b12020-05-15 17:22:28 +01001458{
1459 struct ffa_value ret;
1460
J-Alves8505a8a2022-06-15 18:10:18 +01001461 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1462 fragment, fragment_length);
1463 other_world_locked.vm->mailbox.recv_size = fragment_length;
1464 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1465 other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1466 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001467 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001468 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1469 .arg1 = (uint32_t)handle,
1470 .arg2 = (uint32_t)(handle >> 32),
1471 .arg3 = fragment_length,
1472 .arg4 = (uint64_t)sender_vm_id << 16});
1473 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001474 * After the call to the other world completes it must have finished
1475 * reading its RX buffer, so it is ready for another message.
Andrew Walbranca808b12020-05-15 17:22:28 +01001476 */
J-Alves8505a8a2022-06-15 18:10:18 +01001477 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbranca808b12020-05-15 17:22:28 +01001478
1479 return ret;
1480}
1481
1482/**
J-Alves95df0ef2022-12-07 10:09:48 +00001483 * Checks if there is at least one receiver from the other world.
1484 */
1485static bool memory_region_receivers_from_other_world(
1486 struct ffa_memory_region *memory_region)
1487{
1488 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1489 ffa_vm_id_t receiver = memory_region->receivers[i]
1490 .receiver_permissions.receiver;
1491 if (!vm_id_is_current_world(receiver)) {
1492 return true;
1493 }
1494 }
1495 return false;
1496}
1497
1498/**
J-Alves8505a8a2022-06-15 18:10:18 +01001499 * Validates a call to donate, lend or share memory to a non-other world VM and
1500 * then updates the stage-2 page tables. Specifically, check if the message
1501 * length and number of memory region constituents match, and if the transition
1502 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001503 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001504 * Assumes that the caller has already found and locked the sender VM and copied
1505 * the memory region descriptor from the sender's TX buffer to a freshly
1506 * allocated page from Hafnium's internal pool. The caller must have also
1507 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001508 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001509 * This function takes ownership of the `memory_region` passed in and will free
1510 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001511 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001512struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001514 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001515 uint32_t fragment_length, uint32_t share_func,
1516 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001517{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001519 struct share_states_locked share_states;
1520 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001521
1522 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001523 * If there is an error validating the `memory_region` then we need to
1524 * free it because we own it but we won't be storing it in a share state
1525 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001526 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001527 ret = ffa_memory_send_validate(from_locked, memory_region,
1528 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001529 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001530 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001531 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001532 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001533 }
1534
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 /* Set flag for share function, ready to be retrieved later. */
1536 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001538 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001540 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001541 case FFA_MEM_LEND_32:
1542 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001543 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001544 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001545 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001547 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001548 }
1549
Andrew Walbranca808b12020-05-15 17:22:28 +01001550 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001551 /*
1552 * Allocate a share state before updating the page table. Otherwise if
1553 * updating the page table succeeded but allocating the share state
1554 * failed then it would leave the memory in a state where nobody could
1555 * get it back.
1556 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001557 if (!allocate_share_state(share_states, share_func, memory_region,
1558 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1559 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001560 dlog_verbose("Failed to allocate share state.\n");
1561 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001562 ret = ffa_error(FFA_NO_MEMORY);
1563 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001564 }
1565
Andrew Walbranca808b12020-05-15 17:22:28 +01001566 if (fragment_length == memory_share_length) {
1567 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001568 ret = ffa_memory_send_complete(
1569 from_locked, share_states, share_state, page_pool,
1570 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001571 } else {
1572 ret = (struct ffa_value){
1573 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001574 .arg1 = (uint32_t)memory_region->handle,
1575 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001576 .arg3 = fragment_length};
1577 }
1578
1579out:
1580 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001581 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001582 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001583}
1584
1585/**
J-Alves8505a8a2022-06-15 18:10:18 +01001586 * Continues an operation to donate, lend or share memory to a VM from current
1587 * world. If this is the last fragment then checks that the transition is valid
1588 * for the type of memory sending operation and updates the stage-2 page tables
1589 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001590 *
1591 * Assumes that the caller has already found and locked the sender VM and copied
1592 * the memory region descriptor from the sender's TX buffer to a freshly
1593 * allocated page from Hafnium's internal pool.
1594 *
1595 * This function takes ownership of the `fragment` passed in; it must not be
1596 * freed by the caller.
1597 */
1598struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1599 void *fragment,
1600 uint32_t fragment_length,
1601 ffa_memory_handle_t handle,
1602 struct mpool *page_pool)
1603{
1604 struct share_states_locked share_states = share_states_lock();
1605 struct ffa_memory_share_state *share_state;
1606 struct ffa_value ret;
1607 struct ffa_memory_region *memory_region;
1608
1609 ret = ffa_memory_send_continue_validate(share_states, handle,
1610 &share_state,
1611 from_locked.vm->id, page_pool);
1612 if (ret.func != FFA_SUCCESS_32) {
1613 goto out_free_fragment;
1614 }
1615 memory_region = share_state->memory_region;
1616
J-Alves95df0ef2022-12-07 10:09:48 +00001617 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001618 dlog_error(
1619 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001620 "other world. This should never happen, and indicates "
1621 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001622 "EL3 code.\n");
1623 ret = ffa_error(FFA_INVALID_PARAMETERS);
1624 goto out_free_fragment;
1625 }
1626
1627 /* Add this fragment. */
1628 share_state->fragments[share_state->fragment_count] = fragment;
1629 share_state->fragment_constituent_counts[share_state->fragment_count] =
1630 fragment_length / sizeof(struct ffa_memory_region_constituent);
1631 share_state->fragment_count++;
1632
1633 /* Check whether the memory send operation is now ready to complete. */
1634 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001635 ret = ffa_memory_send_complete(
1636 from_locked, share_states, share_state, page_pool,
1637 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 } else {
1639 ret = (struct ffa_value){
1640 .func = FFA_MEM_FRAG_RX_32,
1641 .arg1 = (uint32_t)handle,
1642 .arg2 = (uint32_t)(handle >> 32),
1643 .arg3 = share_state_next_fragment_offset(share_states,
1644 share_state)};
1645 }
1646 goto out;
1647
1648out_free_fragment:
1649 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001650
1651out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001652 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001653 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001654}
1655
Andrew Walbranca808b12020-05-15 17:22:28 +01001656/**
J-Alves8505a8a2022-06-15 18:10:18 +01001657 * Continues an operation to donate, lend or share memory to the other world VM.
1658 * If this is the last fragment then checks that the transition is valid for the
1659 * type of memory sending operation and updates the stage-2 page tables of the
1660 * sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001661 *
1662 * Assumes that the caller has already found and locked the sender VM and copied
1663 * the memory region descriptor from the sender's TX buffer to a freshly
1664 * allocated page from Hafnium's internal pool.
1665 *
1666 * This function takes ownership of the `memory_region` passed in and will free
1667 * it when necessary; it must not be freed by the caller.
1668 */
J-Alves8505a8a2022-06-15 18:10:18 +01001669struct ffa_value ffa_memory_other_world_send_continue(
1670 struct vm_locked from_locked, struct vm_locked to_locked,
1671 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
1672 struct mpool *page_pool)
Andrew Walbranca808b12020-05-15 17:22:28 +01001673{
1674 struct share_states_locked share_states = share_states_lock();
1675 struct ffa_memory_share_state *share_state;
1676 struct ffa_value ret;
1677 struct ffa_memory_region *memory_region;
1678
1679 ret = ffa_memory_send_continue_validate(share_states, handle,
1680 &share_state,
1681 from_locked.vm->id, page_pool);
1682 if (ret.func != FFA_SUCCESS_32) {
1683 goto out_free_fragment;
1684 }
1685 memory_region = share_state->memory_region;
1686
J-Alves95df0ef2022-12-07 10:09:48 +00001687 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001688 dlog_error(
J-Alves8505a8a2022-06-15 18:10:18 +01001689 "Got SPM-allocated handle for memory send to non-other "
1690 "world VM. This should never happen, and indicates a "
1691 "bug.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001692 ret = ffa_error(FFA_INVALID_PARAMETERS);
1693 goto out_free_fragment;
1694 }
1695
1696 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1697 to_locked.vm->mailbox.recv == NULL) {
1698 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001699 * If the other world RX buffer is not available, tell the
1700 * sender to retry by returning the current offset again.
Andrew Walbranca808b12020-05-15 17:22:28 +01001701 */
1702 ret = (struct ffa_value){
1703 .func = FFA_MEM_FRAG_RX_32,
1704 .arg1 = (uint32_t)handle,
1705 .arg2 = (uint32_t)(handle >> 32),
1706 .arg3 = share_state_next_fragment_offset(share_states,
1707 share_state),
1708 };
1709 goto out_free_fragment;
1710 }
1711
1712 /* Add this fragment. */
1713 share_state->fragments[share_state->fragment_count] = fragment;
1714 share_state->fragment_constituent_counts[share_state->fragment_count] =
1715 fragment_length / sizeof(struct ffa_memory_region_constituent);
1716 share_state->fragment_count++;
1717
1718 /* Check whether the memory send operation is now ready to complete. */
1719 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001720 struct mpool local_page_pool;
1721 uint32_t orig_from_mode;
1722
1723 /*
1724 * Use a local page pool so that we can roll back if necessary.
1725 */
1726 mpool_init_with_fallback(&local_page_pool, page_pool);
1727
Andrew Walbranca808b12020-05-15 17:22:28 +01001728 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001729 share_state, &local_page_pool,
1730 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001731
1732 if (ret.func == FFA_SUCCESS_32) {
1733 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001734 * Forward final fragment on to the other world so that
Andrew Walbranca808b12020-05-15 17:22:28 +01001735 * it can complete the memory sending operation.
1736 */
J-Alves8505a8a2022-06-15 18:10:18 +01001737 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001738 to_locked, from_locked.vm->id, fragment,
1739 fragment_length, handle);
1740
1741 if (ret.func != FFA_SUCCESS_32) {
1742 /*
1743 * The error will be passed on to the caller,
1744 * but log it here too.
1745 */
1746 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001747 "other world didn't successfully "
1748 "complete "
Andrew Walbranca808b12020-05-15 17:22:28 +01001749 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001750 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001751 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001752
1753 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001754 * The other world failed to complete the send
Andrew Walbran37c574e2020-06-03 11:45:46 +01001755 * operation, so roll back the page table update
1756 * for the VM. This can't fail because it won't
1757 * try to allocate more memory than was freed
1758 * into the `local_page_pool` by
1759 * `ffa_send_check_update` in the initial
1760 * update.
1761 */
1762 CHECK(ffa_region_group_identity_map(
1763 from_locked, share_state->fragments,
1764 share_state
1765 ->fragment_constituent_counts,
1766 share_state->fragment_count,
1767 orig_from_mode, &local_page_pool,
1768 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001769 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001770
Andrew Walbranca808b12020-05-15 17:22:28 +01001771 /* Free share state. */
1772 share_state_free(share_states, share_state, page_pool);
1773 } else {
J-Alves8505a8a2022-06-15 18:10:18 +01001774 /* Abort sending to other world. */
1775 struct ffa_value other_world_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02001776 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01001777 .func = FFA_MEM_RECLAIM_32,
1778 .arg1 = (uint32_t)handle,
1779 .arg2 = (uint32_t)(handle >> 32)});
1780
J-Alves8505a8a2022-06-15 18:10:18 +01001781 if (other_world_ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001782 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001783 * Nothing we can do if other world doesn't
1784 * abort properly, just log it.
Andrew Walbranca808b12020-05-15 17:22:28 +01001785 */
1786 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001787 "other world didn't successfully abort "
1788 "failed "
Andrew Walbranca808b12020-05-15 17:22:28 +01001789 "memory send operation; returned %#x "
1790 "(%d).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01001791 other_world_ret.func,
1792 other_world_ret.arg2);
Andrew Walbranca808b12020-05-15 17:22:28 +01001793 }
1794 /*
1795 * We don't need to free the share state in this case
1796 * because ffa_memory_send_complete does that already.
1797 */
1798 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001799
1800 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001801 } else {
1802 uint32_t next_fragment_offset =
1803 share_state_next_fragment_offset(share_states,
1804 share_state);
1805
J-Alves8505a8a2022-06-15 18:10:18 +01001806 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001807 to_locked, from_locked.vm->id, fragment,
1808 fragment_length, handle);
1809
1810 if (ret.func != FFA_MEM_FRAG_RX_32 ||
1811 ffa_frag_handle(ret) != handle ||
1812 ret.arg3 != next_fragment_offset ||
1813 ffa_frag_sender(ret) != from_locked.vm->id) {
1814 dlog_verbose(
1815 "Got unexpected result from forwarding "
J-Alves8505a8a2022-06-15 18:10:18 +01001816 "FFA_MEM_FRAG_TX to other world. %#x (handle "
1817 "%#x, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001818 "offset %d, sender %d); expected "
1819 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
1820 "sender %d).\n",
1821 ret.func, ffa_frag_handle(ret), ret.arg3,
1822 ffa_frag_sender(ret), handle,
1823 next_fragment_offset, from_locked.vm->id);
1824 /* Free share state. */
1825 share_state_free(share_states, share_state, page_pool);
1826 ret = ffa_error(FFA_INVALID_PARAMETERS);
1827 goto out;
1828 }
1829
1830 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
1831 .arg1 = (uint32_t)handle,
1832 .arg2 = (uint32_t)(handle >> 32),
1833 .arg3 = next_fragment_offset};
1834 }
1835 goto out;
1836
1837out_free_fragment:
1838 mpool_free(page_pool, fragment);
1839
1840out:
1841 share_states_unlock(&share_states);
1842 return ret;
1843}
1844
1845/** Clean up after the receiver has finished retrieving a memory region. */
1846static void ffa_memory_retrieve_complete(
1847 struct share_states_locked share_states,
1848 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1849{
1850 if (share_state->share_func == FFA_MEM_DONATE_32) {
1851 /*
1852 * Memory that has been donated can't be relinquished,
1853 * so no need to keep the share state around.
1854 */
1855 share_state_free(share_states, share_state, page_pool);
1856 dlog_verbose("Freed share state for donate.\n");
1857 }
1858}
1859
J-Alves96de29f2022-04-26 16:05:24 +01001860/*
1861 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1862 * returns its index in the receiver's array. If receiver's ID doesn't exist
1863 * in the array, return the region's 'receiver_count'.
1864 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001865uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1866 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001867{
1868 struct ffa_memory_access *receivers;
1869 uint32_t i;
1870
1871 assert(memory_region != NULL);
1872
1873 receivers = memory_region->receivers;
1874
1875 for (i = 0U; i < memory_region->receiver_count; i++) {
1876 if (receivers[i].receiver_permissions.receiver == receiver) {
1877 break;
1878 }
1879 }
1880
1881 return i;
1882}
1883
1884/**
1885 * Validates the retrieved permissions against those specified by the lender
1886 * of memory share operation. Optionally can help set the permissions to be used
1887 * for the S2 mapping, through the `permissions` argument.
1888 * Returns true if permissions are valid, false otherwise.
1889 */
1890static bool ffa_memory_retrieve_is_memory_access_valid(
1891 enum ffa_data_access sent_data_access,
1892 enum ffa_data_access requested_data_access,
1893 enum ffa_instruction_access sent_instruction_access,
1894 enum ffa_instruction_access requested_instruction_access,
1895 ffa_memory_access_permissions_t *permissions)
1896{
1897 switch (sent_data_access) {
1898 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1899 case FFA_DATA_ACCESS_RW:
1900 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1901 requested_data_access == FFA_DATA_ACCESS_RW) {
1902 if (permissions != NULL) {
1903 ffa_set_data_access_attr(permissions,
1904 FFA_DATA_ACCESS_RW);
1905 }
1906 break;
1907 }
1908 /* Intentional fall-through. */
1909 case FFA_DATA_ACCESS_RO:
1910 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1911 requested_data_access == FFA_DATA_ACCESS_RO) {
1912 if (permissions != NULL) {
1913 ffa_set_data_access_attr(permissions,
1914 FFA_DATA_ACCESS_RO);
1915 }
1916 break;
1917 }
1918 dlog_verbose(
1919 "Invalid data access requested; sender specified "
1920 "permissions %#x but receiver requested %#x.\n",
1921 sent_data_access, requested_data_access);
1922 return false;
1923 case FFA_DATA_ACCESS_RESERVED:
1924 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1925 "checked before this point.");
1926 }
1927
1928 switch (sent_instruction_access) {
1929 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1930 case FFA_INSTRUCTION_ACCESS_X:
1931 if (requested_instruction_access ==
1932 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1933 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1934 if (permissions != NULL) {
1935 ffa_set_instruction_access_attr(
1936 permissions, FFA_INSTRUCTION_ACCESS_X);
1937 }
1938 break;
1939 }
1940 case FFA_INSTRUCTION_ACCESS_NX:
1941 if (requested_instruction_access ==
1942 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1943 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1944 if (permissions != NULL) {
1945 ffa_set_instruction_access_attr(
1946 permissions, FFA_INSTRUCTION_ACCESS_NX);
1947 }
1948 break;
1949 }
1950 dlog_verbose(
1951 "Invalid instruction access requested; sender "
1952 "specified permissions %#x but receiver requested "
1953 "%#x.\n",
1954 sent_instruction_access, requested_instruction_access);
1955 return false;
1956 case FFA_INSTRUCTION_ACCESS_RESERVED:
1957 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1958 "be checked before this point.");
1959 }
1960
1961 return true;
1962}
1963
1964/**
1965 * Validate the receivers' permissions in the retrieve request against those
1966 * specified by the lender.
1967 * In the `permissions` argument returns the permissions to set at S2 for the
1968 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1969 * Returns FFA_SUCCESS if all specified permissions are valid.
1970 */
1971static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1972 struct ffa_memory_region *memory_region,
1973 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1974 ffa_memory_access_permissions_t *permissions)
1975{
1976 uint32_t retrieve_receiver_index;
1977
1978 assert(permissions != NULL);
1979
1980 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1981 dlog_verbose(
1982 "Retrieve request should contain same list of "
1983 "borrowers, as specified by the lender.\n");
1984 return ffa_error(FFA_INVALID_PARAMETERS);
1985 }
1986
1987 retrieve_receiver_index = retrieve_request->receiver_count;
1988
1989 /* Should be populated with the permissions of the retriever. */
1990 *permissions = 0;
1991
1992 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1993 ffa_memory_access_permissions_t sent_permissions;
1994 struct ffa_memory_access *current_receiver =
1995 &retrieve_request->receivers[i];
1996 ffa_memory_access_permissions_t requested_permissions =
1997 current_receiver->receiver_permissions.permissions;
1998 ffa_vm_id_t current_receiver_id =
1999 current_receiver->receiver_permissions.receiver;
2000 bool found_to_id = current_receiver_id == to_vm_id;
2001
2002 /*
2003 * Find the current receiver in the transaction descriptor from
2004 * sender.
2005 */
2006 uint32_t mem_region_receiver_index =
2007 ffa_memory_region_get_receiver(memory_region,
2008 current_receiver_id);
2009
2010 if (mem_region_receiver_index ==
2011 memory_region->receiver_count) {
2012 dlog_verbose("%s: receiver %x not found\n", __func__,
2013 current_receiver_id);
2014 return ffa_error(FFA_DENIED);
2015 }
2016
2017 sent_permissions =
2018 memory_region->receivers[mem_region_receiver_index]
2019 .receiver_permissions.permissions;
2020
2021 if (found_to_id) {
2022 retrieve_receiver_index = i;
2023 }
2024
2025 /*
2026 * Since we are traversing the list of receivers, save the index
2027 * of the caller. As it needs to be there.
2028 */
2029
2030 if (current_receiver->composite_memory_region_offset != 0U) {
2031 dlog_verbose(
2032 "Retriever specified address ranges not "
2033 "supported (got offset %d).\n",
2034 current_receiver
2035 ->composite_memory_region_offset);
2036 return ffa_error(FFA_INVALID_PARAMETERS);
2037 }
2038
2039 /*
2040 * Check permissions from sender against permissions requested
2041 * by receiver.
2042 */
2043 if (!ffa_memory_retrieve_is_memory_access_valid(
2044 ffa_get_data_access_attr(sent_permissions),
2045 ffa_get_data_access_attr(requested_permissions),
2046 ffa_get_instruction_access_attr(sent_permissions),
2047 ffa_get_instruction_access_attr(
2048 requested_permissions),
2049 found_to_id ? permissions : NULL)) {
2050 return ffa_error(FFA_DENIED);
2051 }
2052
2053 /*
2054 * Can't request PM to clear memory if only provided with RO
2055 * permissions.
2056 */
2057 if (found_to_id &&
2058 (ffa_get_data_access_attr(*permissions) ==
2059 FFA_DATA_ACCESS_RO) &&
2060 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2061 0U) {
2062 dlog_verbose(
2063 "Receiver has RO permissions can not request "
2064 "clear.\n");
2065 return ffa_error(FFA_DENIED);
2066 }
2067 }
2068
2069 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2070 dlog_verbose(
2071 "Retrieve request does not contain caller's (%x) "
2072 "permissions\n",
2073 to_vm_id);
2074 return ffa_error(FFA_INVALID_PARAMETERS);
2075 }
2076
2077 return (struct ffa_value){.func = FFA_SUCCESS_32};
2078}
2079
J-Alvesa9cd7e32022-07-01 13:49:33 +01002080/*
2081 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2082 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2083 * of a pending memory sharing operation whose allocator is the SPM, for
2084 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2085 * the memory region descriptor of the retrieve request must be zeroed with the
2086 * exception of the sender ID and handle.
2087 */
2088bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2089 struct vm_locked to_locked)
2090{
2091 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2092 request->attributes == 0U && request->flags == 0U &&
2093 request->tag == 0U && request->receiver_count == 0U &&
2094 plat_ffa_memory_handle_allocated_by_current_world(
2095 request->handle);
2096}
2097
2098/*
2099 * Helper to reset count of fragments retrieved by the hypervisor.
2100 */
2101static void ffa_memory_retrieve_complete_from_hyp(
2102 struct ffa_memory_share_state *share_state)
2103{
2104 if (share_state->hypervisor_fragment_count ==
2105 share_state->fragment_count) {
2106 share_state->hypervisor_fragment_count = 0;
2107 }
2108}
2109
J-Alves089004f2022-07-13 14:25:44 +01002110/**
2111 * Validate that the memory region descriptor provided by the borrower on
2112 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2113 * memory sharing call.
2114 */
2115static struct ffa_value ffa_memory_retrieve_validate(
2116 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2117 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2118 uint32_t share_func)
2119{
2120 ffa_memory_region_flags_t transaction_type =
2121 retrieve_request->flags &
2122 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2123
2124 assert(retrieve_request != NULL);
2125 assert(memory_region != NULL);
2126 assert(receiver_index != NULL);
2127 assert(retrieve_request->sender == memory_region->sender);
2128
2129 /*
2130 * Check that the transaction type expected by the receiver is
2131 * correct, if it has been specified.
2132 */
2133 if (transaction_type !=
2134 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2135 transaction_type != (memory_region->flags &
2136 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2137 dlog_verbose(
2138 "Incorrect transaction type %#x for "
2139 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2140 transaction_type,
2141 memory_region->flags &
2142 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2143 retrieve_request->handle);
2144 return ffa_error(FFA_INVALID_PARAMETERS);
2145 }
2146
2147 if (retrieve_request->tag != memory_region->tag) {
2148 dlog_verbose(
2149 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2150 "%d for handle %#x.\n",
2151 retrieve_request->tag, memory_region->tag,
2152 retrieve_request->handle);
2153 return ffa_error(FFA_INVALID_PARAMETERS);
2154 }
2155
2156 *receiver_index =
2157 ffa_memory_region_get_receiver(memory_region, receiver_id);
2158
2159 if (*receiver_index == memory_region->receiver_count) {
2160 dlog_verbose(
2161 "Incorrect receiver VM ID %d for "
2162 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
2163 receiver_id, handle);
2164 return ffa_error(FFA_INVALID_PARAMETERS);
2165 }
2166
2167 if ((retrieve_request->flags &
2168 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2169 dlog_verbose(
2170 "Retriever specified 'address range alignment 'hint' "
2171 "not supported.\n");
2172 return ffa_error(FFA_INVALID_PARAMETERS);
2173 }
2174 if ((retrieve_request->flags &
2175 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2176 dlog_verbose(
2177 "Bits 8-5 must be zero in memory region's flags "
2178 "(address range alignment hint not supported).\n");
2179 return ffa_error(FFA_INVALID_PARAMETERS);
2180 }
2181
2182 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2183 dlog_verbose(
2184 "Bits 31-10 must be zero in memory region's flags.\n");
2185 return ffa_error(FFA_INVALID_PARAMETERS);
2186 }
2187
2188 if (share_func == FFA_MEM_SHARE_32 &&
2189 (retrieve_request->flags &
2190 (FFA_MEMORY_REGION_FLAG_CLEAR |
2191 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2192 dlog_verbose(
2193 "Memory Share operation can't clean after relinquish "
2194 "memory region.\n");
2195 return ffa_error(FFA_INVALID_PARAMETERS);
2196 }
2197
2198 /*
2199 * If the borrower needs the memory to be cleared before mapping
2200 * to its address space, the sender should have set the flag
2201 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2202 * FFA_DENIED.
2203 */
2204 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2205 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2206 dlog_verbose(
2207 "Borrower needs memory cleared. Sender needs to set "
2208 "flag for clearing memory.\n");
2209 return ffa_error(FFA_DENIED);
2210 }
2211
2212 /*
2213 * If memory type is not specified, bypass validation of memory
2214 * attributes in the retrieve request. The retriever is expecting to
2215 * obtain this information from the SPMC.
2216 */
2217 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2218 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2219 return (struct ffa_value){.func = FFA_SUCCESS_32};
2220 }
2221
2222 /*
2223 * Ensure receiver's attributes are compatible with how
2224 * Hafnium maps memory: Normal Memory, Inner shareable,
2225 * Write-Back Read-Allocate Write-Allocate Cacheable.
2226 */
2227 return ffa_memory_attributes_validate(retrieve_request->attributes);
2228}
2229
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2231 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002232 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002233 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002234{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002235 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002236 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002237 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002238 sizeof(struct ffa_memory_access);
2239 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002240 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002241 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002242 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002243 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 struct ffa_memory_share_state *share_state;
2245 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002246 struct ffa_composite_memory_region *composite;
2247 uint32_t total_length;
2248 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002249 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002250 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002251
2252 dump_share_states();
2253
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002254 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002255 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002256 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002257 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002258 expected_retrieve_request_length,
2259 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002260 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002261 }
2262
2263 share_states = share_states_lock();
2264 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002265 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002266 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002267 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002268 goto out;
2269 }
2270
J-Alves96de29f2022-04-26 16:05:24 +01002271 if (!share_state->sending_complete) {
2272 dlog_verbose(
2273 "Memory with handle %#x not fully sent, can't "
2274 "retrieve.\n",
2275 handle);
2276 ret = ffa_error(FFA_INVALID_PARAMETERS);
2277 goto out;
2278 }
2279
Andrew Walbrana65a1322020-04-06 19:32:32 +01002280 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002281
Andrew Walbrana65a1322020-04-06 19:32:32 +01002282 CHECK(memory_region != NULL);
2283
J-Alves089004f2022-07-13 14:25:44 +01002284 if (retrieve_request->sender != memory_region->sender) {
2285 dlog_verbose(
2286 "Memory with handle %#x not fully sent, can't "
2287 "retrieve.\n",
2288 handle);
2289 ret = ffa_error(FFA_INVALID_PARAMETERS);
2290 goto out;
2291 }
J-Alves96de29f2022-04-26 16:05:24 +01002292
J-Alvesa9cd7e32022-07-01 13:49:33 +01002293 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2294 to_locked)) {
2295 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002296
J-Alvesb5084cf2022-07-06 14:20:12 +01002297 /*
2298 * The SPMC can only process retrieve requests to memory share
2299 * operations with one borrower from the other world. It can't
2300 * determine the ID of the NWd VM that invoked the retrieve
2301 * request interface call. It relies on the hypervisor to
2302 * validate the caller's ID against that provided in the
2303 * `receivers` list of the retrieve response.
2304 * In case there is only one borrower from the NWd in the
2305 * transaction descriptor, record that in the `receiver_id` for
2306 * later use, and validate in the retrieve request message.
2307 */
2308 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2309 uint32_t other_world_count = 0;
2310
2311 for (uint32_t i = 0; i < memory_region->receiver_count;
2312 i++) {
2313 receiver_id =
2314 retrieve_request->receivers[0]
2315 .receiver_permissions.receiver;
2316 if (!vm_id_is_current_world(receiver_id)) {
2317 other_world_count++;
2318 }
2319 }
2320 if (other_world_count > 1) {
2321 dlog_verbose(
2322 "Support one receiver from the other "
2323 "world.\n");
2324 return ffa_error(FFA_NOT_SUPPORTED);
2325 }
2326 }
2327
2328 /*
2329 * Validate retrieve request, according to what was sent by the
2330 * sender. Function will output the `receiver_index` from the
2331 * provided memory region, and will output `permissions` from
2332 * the validated requested permissions.
2333 */
J-Alves089004f2022-07-13 14:25:44 +01002334 ret = ffa_memory_retrieve_validate(
2335 receiver_id, retrieve_request, memory_region,
2336 &receiver_index, share_state->share_func);
2337 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002338 goto out;
2339 }
2340
2341 if (share_state->retrieved_fragment_count[receiver_index] !=
2342 0U) {
2343 dlog_verbose(
2344 "Memory with handle %#x already retrieved.\n",
2345 handle);
2346 ret = ffa_error(FFA_DENIED);
2347 goto out;
2348 }
2349
J-Alvesa9cd7e32022-07-01 13:49:33 +01002350 ret = ffa_memory_retrieve_validate_memory_access_list(
2351 memory_region, retrieve_request, receiver_id,
2352 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002353 if (ret.func != FFA_SUCCESS_32) {
2354 goto out;
2355 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002356
J-Alvesa9cd7e32022-07-01 13:49:33 +01002357 memory_to_attributes = ffa_memory_permissions_to_mode(
2358 permissions, share_state->sender_orig_mode);
2359 ret = ffa_retrieve_check_update(
2360 to_locked, memory_region->sender,
2361 share_state->fragments,
2362 share_state->fragment_constituent_counts,
2363 share_state->fragment_count, memory_to_attributes,
2364 share_state->share_func, false, page_pool);
2365
2366 if (ret.func != FFA_SUCCESS_32) {
2367 goto out;
2368 }
2369
2370 share_state->retrieved_fragment_count[receiver_index] = 1;
2371 is_send_complete =
2372 share_state->retrieved_fragment_count[receiver_index] ==
2373 share_state->fragment_count;
2374 } else {
2375 if (share_state->hypervisor_fragment_count != 0U) {
2376 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002377 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002378 "the hypervisor.\n",
2379 handle);
2380 ret = ffa_error(FFA_DENIED);
2381 goto out;
2382 }
2383
2384 share_state->hypervisor_fragment_count = 1;
2385
2386 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002387 }
2388
J-Alvesb5084cf2022-07-06 14:20:12 +01002389 /* VMs acquire the RX buffer from SPMC. */
2390 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2391
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002392 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002393 * Copy response to RX buffer of caller and deliver the message.
2394 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002395 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002396 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002397 composite = ffa_memory_region_get_composite(memory_region, 0);
2398 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002399 * Constituents which we received in the first fragment should
2400 * always fit in the first fragment we are sending, because the
2401 * header is the same size in both cases and we have a fixed
2402 * message buffer size. So `ffa_retrieved_memory_region_init`
2403 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002404 */
2405 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002406 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2407 memory_region->sender, memory_region->attributes,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002408 memory_region->flags, handle, receiver_id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002409 composite->page_count, composite->constituent_count,
2410 share_state->fragments[0],
2411 share_state->fragment_constituent_counts[0], &total_length,
2412 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002413
Andrew Walbranca808b12020-05-15 17:22:28 +01002414 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002415 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002417 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2418
J-Alvesa9cd7e32022-07-01 13:49:33 +01002419 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002420 ffa_memory_retrieve_complete(share_states, share_state,
2421 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002422 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002423 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002424 .arg1 = total_length,
2425 .arg2 = fragment_length};
2426
2427out:
2428 share_states_unlock(&share_states);
2429 dump_share_states();
2430 return ret;
2431}
2432
2433struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2434 ffa_memory_handle_t handle,
2435 uint32_t fragment_offset,
2436 struct mpool *page_pool)
2437{
2438 struct ffa_memory_region *memory_region;
2439 struct share_states_locked share_states;
2440 struct ffa_memory_share_state *share_state;
2441 struct ffa_value ret;
2442 uint32_t fragment_index;
2443 uint32_t retrieved_constituents_count;
2444 uint32_t i;
2445 uint32_t expected_fragment_offset;
2446 uint32_t remaining_constituent_count;
2447 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002448 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002449
2450 dump_share_states();
2451
2452 share_states = share_states_lock();
2453 if (!get_share_state(share_states, handle, &share_state)) {
2454 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2455 handle);
2456 ret = ffa_error(FFA_INVALID_PARAMETERS);
2457 goto out;
2458 }
2459
2460 memory_region = share_state->memory_region;
2461 CHECK(memory_region != NULL);
2462
J-Alvesc7484f12022-05-13 12:41:14 +01002463 receiver_index =
2464 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2465
2466 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002467 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002468 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2469 "memory sharing transaction (%x)\n",
2470 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002471 ret = ffa_error(FFA_INVALID_PARAMETERS);
2472 goto out;
2473 }
2474
2475 if (!share_state->sending_complete) {
2476 dlog_verbose(
2477 "Memory with handle %#x not fully sent, can't "
2478 "retrieve.\n",
2479 handle);
2480 ret = ffa_error(FFA_INVALID_PARAMETERS);
2481 goto out;
2482 }
2483
J-Alvesc7484f12022-05-13 12:41:14 +01002484 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2485 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002486 share_state->fragment_count) {
2487 dlog_verbose(
2488 "Retrieval of memory with handle %#x not yet started "
2489 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002490 handle,
2491 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002492 share_state->fragment_count);
2493 ret = ffa_error(FFA_INVALID_PARAMETERS);
2494 goto out;
2495 }
2496
J-Alvesc7484f12022-05-13 12:41:14 +01002497 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002498
2499 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002500 * Check that the given fragment offset is correct by counting
2501 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002502 */
2503 retrieved_constituents_count = 0;
2504 for (i = 0; i < fragment_index; ++i) {
2505 retrieved_constituents_count +=
2506 share_state->fragment_constituent_counts[i];
2507 }
J-Alvesc7484f12022-05-13 12:41:14 +01002508
2509 CHECK(memory_region->receiver_count > 0);
2510
Andrew Walbranca808b12020-05-15 17:22:28 +01002511 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002512 ffa_composite_constituent_offset(memory_region,
2513 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002514 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002515 sizeof(struct ffa_memory_region_constituent) -
2516 sizeof(struct ffa_memory_access) *
2517 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002518 if (fragment_offset != expected_fragment_offset) {
2519 dlog_verbose("Fragment offset was %d but expected %d.\n",
2520 fragment_offset, expected_fragment_offset);
2521 ret = ffa_error(FFA_INVALID_PARAMETERS);
2522 goto out;
2523 }
2524
2525 remaining_constituent_count = ffa_memory_fragment_init(
2526 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2527 share_state->fragments[fragment_index],
2528 share_state->fragment_constituent_counts[fragment_index],
2529 &fragment_length);
2530 CHECK(remaining_constituent_count == 0);
2531 to_locked.vm->mailbox.recv_size = fragment_length;
2532 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2533 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2534 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002535 share_state->retrieved_fragment_count[receiver_index]++;
2536 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002537 share_state->fragment_count) {
2538 ffa_memory_retrieve_complete(share_states, share_state,
2539 page_pool);
2540 }
2541
2542 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2543 .arg1 = (uint32_t)handle,
2544 .arg2 = (uint32_t)(handle >> 32),
2545 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546
2547out:
2548 share_states_unlock(&share_states);
2549 dump_share_states();
2550 return ret;
2551}
2552
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002553struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002555 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002557 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 struct ffa_memory_share_state *share_state;
2560 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002561 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002562 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002563 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002564
Andrew Walbrana65a1322020-04-06 19:32:32 +01002565 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002566 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002567 "Stream endpoints not supported (got %d "
2568 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002569 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002570 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002571 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572 }
2573
Andrew Walbrana65a1322020-04-06 19:32:32 +01002574 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002575 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002576 "VM ID %d in relinquish message doesn't match "
2577 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002578 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002579 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581 }
2582
2583 dump_share_states();
2584
2585 share_states = share_states_lock();
2586 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002587 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002588 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002589 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002590 goto out;
2591 }
2592
Andrew Walbranca808b12020-05-15 17:22:28 +01002593 if (!share_state->sending_complete) {
2594 dlog_verbose(
2595 "Memory with handle %#x not fully sent, can't "
2596 "relinquish.\n",
2597 handle);
2598 ret = ffa_error(FFA_INVALID_PARAMETERS);
2599 goto out;
2600 }
2601
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002602 memory_region = share_state->memory_region;
2603 CHECK(memory_region != NULL);
2604
J-Alves8eb19162022-04-28 10:56:48 +01002605 receiver_index = ffa_memory_region_get_receiver(memory_region,
2606 from_locked.vm->id);
2607
2608 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002609 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002610 "VM ID %d tried to relinquish memory region "
2611 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002612 "handle %#x and it is not a valid borrower.\n",
2613 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002614 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002615 goto out;
2616 }
2617
J-Alves8eb19162022-04-28 10:56:48 +01002618 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002619 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002620 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002621 "Memory with handle %#x not yet fully "
2622 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002623 "receiver %x can't relinquish.\n",
2624 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002625 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002626 goto out;
2627 }
2628
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630
2631 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002632 * Clear is not allowed for memory that was shared, as the
2633 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002634 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636 dlog_verbose("Memory which was shared can't be cleared.\n");
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
Andrew Walbranca808b12020-05-15 17:22:28 +01002641 ret = ffa_relinquish_check_update(
2642 from_locked, share_state->fragments,
2643 share_state->fragment_constituent_counts,
2644 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002645
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002646 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002647 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002648 * Mark memory handle as not retrieved, so it can be
2649 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 */
J-Alves8eb19162022-04-28 10:56:48 +01002651 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002652 }
2653
2654out:
2655 share_states_unlock(&share_states);
2656 dump_share_states();
2657 return ret;
2658}
2659
2660/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002661 * Validates that the reclaim transition is allowed for the given
2662 * handle, updates the page table of the reclaiming VM, and frees the
2663 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002664 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002665struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002666 ffa_memory_handle_t handle,
2667 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002668 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002669{
2670 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002671 struct ffa_memory_share_state *share_state;
2672 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002673 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674
2675 dump_share_states();
2676
2677 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002678 if (get_share_state(share_states, handle, &share_state)) {
2679 memory_region = share_state->memory_region;
2680 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002681 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002682 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002683 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 goto out;
2685 }
2686
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002687 CHECK(memory_region != NULL);
2688
J-Alvesa9cd7e32022-07-01 13:49:33 +01002689 if (vm_id_is_current_world(to_locked.vm->id) &&
2690 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002691 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002692 "VM %#x attempted to reclaim memory handle %#x "
2693 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002694 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002695 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002696 goto out;
2697 }
2698
Andrew Walbranca808b12020-05-15 17:22:28 +01002699 if (!share_state->sending_complete) {
2700 dlog_verbose(
2701 "Memory with handle %#x not fully sent, can't "
2702 "reclaim.\n",
2703 handle);
2704 ret = ffa_error(FFA_INVALID_PARAMETERS);
2705 goto out;
2706 }
2707
J-Alves752236c2022-04-28 11:07:47 +01002708 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2709 if (share_state->retrieved_fragment_count[i] != 0) {
2710 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002711 "Tried to reclaim memory handle %#x "
2712 "that has "
2713 "not been relinquished by all "
2714 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002715 handle,
2716 memory_region->receivers[i]
2717 .receiver_permissions.receiver);
2718 ret = ffa_error(FFA_DENIED);
2719 goto out;
2720 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002721 }
2722
Andrew Walbranca808b12020-05-15 17:22:28 +01002723 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002724 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002725 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002726 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002727 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002729 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002731 dlog_verbose(
2732 "Freed share state after successful "
2733 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002734 }
2735
2736out:
2737 share_states_unlock(&share_states);
2738 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002739}