blob: 7de8a7f21e27d36cdcc36f4e93bbc69df4fc9ec7 [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
J-Alves5952d942022-12-22 16:03:00 +000015#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000017#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010018#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010021#include "hf/ffa_memory_internal.h"
J-Alves5952d942022-12-22 16:03:00 +000022#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000023#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010024#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000025#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026
J-Alves2d8457f2022-10-05 11:06:41 +010027#include "vmapi/hf/ffa_v1_0.h"
28
J-Alves5da37d92022-10-24 16:33:48 +010029#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
30
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000031/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000033 * by this lock.
34 */
35static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010036static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000037
38/**
J-Alves917d2f22020-10-30 18:39:30 +000039 * Extracts the index from a memory handle allocated by Hafnium's current world.
40 */
41uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
42{
43 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
44}
45
46/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010047 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
48 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
49 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010050 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010051 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
52 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010053 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010054struct ffa_memory_share_state *allocate_share_state(
55 struct share_states_locked share_states, uint32_t share_func,
56 struct ffa_memory_region *memory_region, uint32_t fragment_length,
57 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000058{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000059 assert(share_states.share_states != NULL);
60 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061
Karl Meakin52cdfe72023-06-30 14:49:10 +010062 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010063 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010064 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010065 &share_states.share_states[i];
66 struct ffa_composite_memory_region *composite =
67 ffa_memory_region_get_composite(memory_region,
68 0);
69
70 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000071 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020072 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010073 } else {
J-Alvesee68c542020-10-29 17:48:20 +000074 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010075 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000076 allocated_state->share_func = share_func;
77 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010078 allocated_state->fragment_count = 1;
79 allocated_state->fragments[0] = composite->constituents;
80 allocated_state->fragment_constituent_counts[0] =
81 (fragment_length -
82 ffa_composite_constituent_offset(memory_region,
83 0)) /
84 sizeof(struct ffa_memory_region_constituent);
85 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +010086 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
87 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010088 allocated_state->retrieved_fragment_count[j] =
89 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090 }
Karl Meakin52cdfe72023-06-30 14:49:10 +010091 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092 }
93 }
94
Karl Meakin52cdfe72023-06-30 14:49:10 +010095 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000096}
97
98/** Locks the share states lock. */
99struct share_states_locked share_states_lock(void)
100{
101 sl_lock(&share_states_lock_instance);
102
103 return (struct share_states_locked){.share_states = share_states};
104}
105
106/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100107void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000109 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 share_states->share_states = NULL;
111 sl_unlock(&share_states_lock_instance);
112}
113
114/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100115 * If the given handle is a valid handle for an allocated share state then
116 * initialises `share_state_ret` to point to the share state and returns true.
117 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000118 */
J-Alvesfdd29272022-07-19 13:16:31 +0100119bool get_share_state(struct share_states_locked share_states,
120 ffa_memory_handle_t handle,
121 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100123 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000124 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000125
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000126 assert(share_states.share_states != NULL);
127 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100128
129 /*
130 * First look for a share_state allocated by us, in which case the
131 * handle is based on the index.
132 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200133 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000134 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 if (index < MAX_MEM_SHARES) {
136 share_state = &share_states.share_states[index];
137 if (share_state->share_func != 0) {
138 *share_state_ret = share_state;
139 return true;
140 }
141 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 }
143
Andrew Walbranca808b12020-05-15 17:22:28 +0100144 /* Fall back to a linear scan. */
145 for (index = 0; index < MAX_MEM_SHARES; ++index) {
146 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000147 if (share_state->memory_region != NULL &&
148 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100149 share_state->share_func != 0) {
150 *share_state_ret = share_state;
151 return true;
152 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153 }
154
Andrew Walbranca808b12020-05-15 17:22:28 +0100155 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000156}
157
158/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100159void share_state_free(struct share_states_locked share_states,
160 struct ffa_memory_share_state *share_state,
161 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000162{
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 uint32_t i;
164
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000165 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000166 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 /*
170 * First fragment is part of the same page as the `memory_region`, so it
171 * doesn't need to be freed separately.
172 */
173 share_state->fragments[0] = NULL;
174 share_state->fragment_constituent_counts[0] = 0;
175 for (i = 1; i < share_state->fragment_count; ++i) {
176 mpool_free(page_pool, share_state->fragments[i]);
177 share_state->fragments[i] = NULL;
178 share_state->fragment_constituent_counts[i] = 0;
179 }
180 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100182 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
Andrew Walbranca808b12020-05-15 17:22:28 +0100185/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186bool share_state_sending_complete(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188{
Andrew Walbranca808b12020-05-15 17:22:28 +0100189 struct ffa_composite_memory_region *composite;
190 uint32_t expected_constituent_count;
191 uint32_t fragment_constituent_count_total = 0;
192 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000195 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196
197 /*
198 * Share state must already be valid, or it's not possible to get hold
199 * of it.
200 */
201 CHECK(share_state->memory_region != NULL &&
202 share_state->share_func != 0);
203
204 composite =
205 ffa_memory_region_get_composite(share_state->memory_region, 0);
206 expected_constituent_count = composite->constituent_count;
207 for (i = 0; i < share_state->fragment_count; ++i) {
208 fragment_constituent_count_total +=
209 share_state->fragment_constituent_counts[i];
210 }
211 dlog_verbose(
212 "Checking completion: constituent count %d/%d from %d "
213 "fragments.\n",
214 fragment_constituent_count_total, expected_constituent_count,
215 share_state->fragment_count);
216
217 return fragment_constituent_count_total == expected_constituent_count;
218}
219
220/**
221 * Calculates the offset of the next fragment expected for the given share
222 * state.
223 */
J-Alvesfdd29272022-07-19 13:16:31 +0100224uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100225 struct share_states_locked share_states,
226 struct ffa_memory_share_state *share_state)
227{
228 uint32_t next_fragment_offset;
229 uint32_t i;
230
231 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000232 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100233
234 next_fragment_offset =
235 ffa_composite_constituent_offset(share_state->memory_region, 0);
236 for (i = 0; i < share_state->fragment_count; ++i) {
237 next_fragment_offset +=
238 share_state->fragment_constituent_counts[i] *
239 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000240 }
241
Andrew Walbranca808b12020-05-15 17:22:28 +0100242 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000243}
244
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246{
247 uint32_t i;
248
249 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
250 return;
251 }
252
Olivier Deprez935e1b12020-12-22 18:01:29 +0100253 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100254 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100255 "recipients [",
256 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100257 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100258 memory_region->receiver_count);
259 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 if (i != 0) {
261 dlog(", ");
262 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100263 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100264 memory_region->receivers[i].receiver_permissions.receiver,
265 memory_region->receivers[i]
266 .receiver_permissions.permissions,
267 memory_region->receivers[i]
268 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269 }
270 dlog("]");
271}
272
J-Alves66652252022-07-06 09:49:51 +0100273void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274{
275 uint32_t i;
276
277 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
278 return;
279 }
280
281 dlog("Current share states:\n");
282 sl_lock(&share_states_lock_instance);
283 for (i = 0; i < MAX_MEM_SHARES; ++i) {
284 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000285 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100286 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000287 dlog("SHARE");
288 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100289 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000290 dlog("LEND");
291 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000293 dlog("DONATE");
294 break;
295 default:
296 dlog("invalid share_func %#x",
297 share_states[i].share_func);
298 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100299 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000300 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100301 if (share_states[i].sending_complete) {
302 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000303 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100304 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305 }
J-Alves2a0d2882020-10-29 14:49:50 +0000306 dlog(" with %d fragments, %d retrieved, "
307 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100308 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000309 share_states[i].retrieved_fragment_count[0],
310 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000311 }
312 }
313 sl_unlock(&share_states_lock_instance);
314}
315
Andrew Walbran475c1452020-02-07 13:22:22 +0000316/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100317static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100318 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000319{
320 uint32_t mode = 0;
321
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100322 switch (ffa_get_data_access_attr(permissions)) {
323 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000324 mode = MM_MODE_R;
325 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000327 mode = MM_MODE_R | MM_MODE_W;
328 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100329 case FFA_DATA_ACCESS_NOT_SPECIFIED:
330 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
331 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_DATA_ACCESS_RESERVED:
333 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100334 }
335
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 switch (ffa_get_instruction_access_attr(permissions)) {
337 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000338 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100340 mode |= MM_MODE_X;
341 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100342 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
343 mode |= (default_mode & MM_MODE_X);
344 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345 case FFA_INSTRUCTION_ACCESS_RESERVED:
346 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000347 }
348
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200349 /* Set the security state bit if necessary. */
350 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
351 mode |= plat_ffa_other_world_mode();
352 }
353
Andrew Walbran475c1452020-02-07 13:22:22 +0000354 return mode;
355}
356
Jose Marinho75509b42019-04-09 09:34:59 +0100357/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000358 * Get the current mode in the stage-2 page table of the given vm of all the
359 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100360 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100361 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000363 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100364 struct ffa_memory_region_constituent **fragments,
365 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100366{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100367 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100368 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100369
Andrew Walbranca808b12020-05-15 17:22:28 +0100370 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100371 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000372 * Fail if there are no constituents. Otherwise we would get an
373 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100374 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100375 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100376 }
377
Andrew Walbranca808b12020-05-15 17:22:28 +0100378 for (i = 0; i < fragment_count; ++i) {
379 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
380 ipaddr_t begin = ipa_init(fragments[i][j].address);
381 size_t size = fragments[i][j].page_count * PAGE_SIZE;
382 ipaddr_t end = ipa_add(begin, size);
383 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100384
Andrew Walbranca808b12020-05-15 17:22:28 +0100385 /* Fail if addresses are not page-aligned. */
386 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
387 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
388 return ffa_error(FFA_INVALID_PARAMETERS);
389 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100390
Andrew Walbranca808b12020-05-15 17:22:28 +0100391 /*
392 * Ensure that this constituent memory range is all
393 * mapped with the same mode.
394 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800395 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100396 return ffa_error(FFA_DENIED);
397 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100398
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 /*
400 * Ensure that all constituents are mapped with the same
401 * mode.
402 */
403 if (i == 0) {
404 *orig_mode = current_mode;
405 } else if (current_mode != *orig_mode) {
406 dlog_verbose(
407 "Expected mode %#x but was %#x for %d "
408 "pages at %#x.\n",
409 *orig_mode, current_mode,
410 fragments[i][j].page_count,
411 ipa_addr(begin));
412 return ffa_error(FFA_DENIED);
413 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100414 }
Jose Marinho75509b42019-04-09 09:34:59 +0100415 }
416
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000418}
419
420/**
421 * Verify that all pages have the same mode, that the starting mode
422 * constitutes a valid state and obtain the next mode to apply
423 * to the sending VM.
424 *
425 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 * 1) FFA_DENIED if a state transition was not found;
427 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100428 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100429 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100430 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100431 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
432 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000433 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100434static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100435 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100436 struct ffa_memory_access *receivers, uint32_t receivers_count,
437 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 struct ffa_memory_region_constituent **fragments,
439 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
440 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000441{
442 const uint32_t state_mask =
443 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100444 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000445
Andrew Walbranca808b12020-05-15 17:22:28 +0100446 ret = constituents_get_mode(from, orig_from_mode, fragments,
447 fragment_constituent_counts,
448 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100449 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100450 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100451 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100452 }
453
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000454 /* Ensure the address range is normal memory and not a device. */
455 if (*orig_from_mode & MM_MODE_D) {
456 dlog_verbose("Can't share device memory (mode is %#x).\n",
457 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100458 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000459 }
460
461 /*
462 * Ensure the sender is the owner and has exclusive access to the
463 * memory.
464 */
465 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100466 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100467 }
468
J-Alves363f5722022-04-25 17:37:37 +0100469 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100470
J-Alves363f5722022-04-25 17:37:37 +0100471 for (uint32_t i = 0U; i < receivers_count; i++) {
472 ffa_memory_access_permissions_t permissions =
473 receivers[i].receiver_permissions.permissions;
474 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
475 permissions, *orig_from_mode);
476
477 if ((*orig_from_mode & required_from_mode) !=
478 required_from_mode) {
479 dlog_verbose(
480 "Sender tried to send memory with permissions "
481 "which "
482 "required mode %#x but only had %#x itself.\n",
483 required_from_mode, *orig_from_mode);
484 return ffa_error(FFA_DENIED);
485 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000486 }
487
488 /* Find the appropriate new mode. */
489 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000490 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100491 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000492 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100493 break;
494
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000496 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100497 break;
498
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100499 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000500 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100501 break;
502
Jose Marinho75509b42019-04-09 09:34:59 +0100503 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100504 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100505 }
506
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100507 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000508}
509
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100510static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000511 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100512 struct ffa_memory_region_constituent **fragments,
513 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
514 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000515{
516 const uint32_t state_mask =
517 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
518 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000520
Andrew Walbranca808b12020-05-15 17:22:28 +0100521 ret = constituents_get_mode(from, orig_from_mode, fragments,
522 fragment_constituent_counts,
523 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100525 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000526 }
527
528 /* Ensure the address range is normal memory and not a device. */
529 if (*orig_from_mode & MM_MODE_D) {
530 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
531 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100532 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000533 }
534
535 /*
536 * Ensure the relinquishing VM is not the owner but has access to the
537 * memory.
538 */
539 orig_from_state = *orig_from_mode & state_mask;
540 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
541 dlog_verbose(
542 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100543 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000544 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100545 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000546 }
547
548 /* Find the appropriate new mode. */
549 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
550
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100551 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000552}
553
554/**
555 * Verify that all pages have the same mode, that the starting mode
556 * constitutes a valid state and obtain the next mode to apply
557 * to the retrieving VM.
558 *
559 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100560 * 1) FFA_DENIED if a state transition was not found;
561 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100562 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100563 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100564 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100565 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
566 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567 */
J-Alvesfc19b372022-07-06 12:17:35 +0100568struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000569 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100570 struct ffa_memory_region_constituent **fragments,
571 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
572 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573{
574 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000576
Andrew Walbranca808b12020-05-15 17:22:28 +0100577 ret = constituents_get_mode(to, &orig_to_mode, fragments,
578 fragment_constituent_counts,
579 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100580 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100581 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100582 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000583 }
584
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100585 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000586 /*
587 * If the original ffa memory send call has been processed
588 * successfully, it is expected the orig_to_mode would overlay
589 * with `state_mask`, as a result of the function
590 * `ffa_send_check_transition`.
591 */
J-Alves59ed0042022-07-28 18:26:41 +0100592 if (vm_id_is_current_world(to.vm->id)) {
593 assert((orig_to_mode &
594 (MM_MODE_INVALID | MM_MODE_UNOWNED |
595 MM_MODE_SHARED)) != 0U);
596 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000597 } else {
598 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100599 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000600 * Ensure the retriever has the expected state. We don't care
601 * about the MM_MODE_SHARED bit; either with or without it set
602 * are both valid representations of the !O-NA state.
603 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100604 if (vm_id_is_current_world(to.vm->id) &&
605 to.vm->id != HF_PRIMARY_VM_ID &&
606 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
607 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100608 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000609 }
610 }
611
612 /* Find the appropriate new mode. */
613 *to_mode = memory_to_attributes;
614 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100615 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000616 *to_mode |= 0;
617 break;
618
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100619 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000620 *to_mode |= MM_MODE_UNOWNED;
621 break;
622
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100623 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000624 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
625 break;
626
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000628 *to_mode |= 0;
629 break;
630
631 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100632 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100633 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000634 }
635
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100636 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100637}
Jose Marinho09b1db82019-08-08 09:16:59 +0100638
639/**
640 * Updates a VM's page table such that the given set of physical address ranges
641 * are mapped in the address space at the corresponding address ranges, in the
642 * mode provided.
643 *
644 * If commit is false, the page tables will be allocated from the mpool but no
645 * mappings will actually be updated. This function must always be called first
646 * with commit false to check that it will succeed before calling with commit
647 * true, to avoid leaving the page table in a half-updated state. To make a
648 * series of changes atomically you can call them all with commit false before
649 * calling them all with commit true.
650 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700651 * vm_ptable_defrag should always be called after a series of page table
652 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100653 *
654 * Returns true on success, or false if the update failed and no changes were
655 * made to memory mappings.
656 */
J-Alves66652252022-07-06 09:49:51 +0100657bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000658 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100659 struct ffa_memory_region_constituent **fragments,
660 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100661 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100662{
Andrew Walbranca808b12020-05-15 17:22:28 +0100663 uint32_t i;
664 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100665
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700666 if (vm_locked.vm->el0_partition) {
667 mode |= MM_MODE_USER | MM_MODE_NG;
668 }
669
Andrew Walbranca808b12020-05-15 17:22:28 +0100670 /* Iterate over the memory region constituents within each fragment. */
671 for (i = 0; i < fragment_count; ++i) {
672 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
673 size_t size = fragments[i][j].page_count * PAGE_SIZE;
674 paddr_t pa_begin =
675 pa_from_ipa(ipa_init(fragments[i][j].address));
676 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200677 uint32_t pa_bits =
678 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100679
680 /*
681 * Ensure the requested region falls into system's PA
682 * range.
683 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200684 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
685 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100686 dlog_error("Region is outside of PA Range\n");
687 return false;
688 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100689
690 if (commit) {
691 vm_identity_commit(vm_locked, pa_begin, pa_end,
692 mode, ppool, NULL);
693 } else if (!vm_identity_prepare(vm_locked, pa_begin,
694 pa_end, mode, ppool)) {
695 return false;
696 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100697 }
698 }
699
700 return true;
701}
702
703/**
704 * Clears a region of physical memory by overwriting it with zeros. The data is
705 * flushed from the cache so the memory has been cleared across the system.
706 */
J-Alves7db32002021-12-14 14:44:50 +0000707static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
708 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100709{
710 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000711 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100712 * global mapping of the whole range. Such an approach will limit
713 * the changes to stage-1 tables and will allow only local
714 * invalidation.
715 */
716 bool ret;
717 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000718 void *ptr = mm_identity_map(stage1_locked, begin, end,
719 MM_MODE_W | (extra_mode_attributes &
720 plat_ffa_other_world_mode()),
721 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100722 size_t size = pa_difference(begin, end);
723
724 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100725 goto fail;
726 }
727
728 memset_s(ptr, size, 0, size);
729 arch_mm_flush_dcache(ptr, size);
730 mm_unmap(stage1_locked, begin, end, ppool);
731
732 ret = true;
733 goto out;
734
735fail:
736 ret = false;
737
738out:
739 mm_unlock_stage1(&stage1_locked);
740
741 return ret;
742}
743
744/**
745 * Clears a region of physical memory by overwriting it with zeros. The data is
746 * flushed from the cache so the memory has been cleared across the system.
747 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100748static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000749 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100750 struct ffa_memory_region_constituent **fragments,
751 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
752 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100753{
754 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100755 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100756 bool ret = false;
757
758 /*
759 * Create a local pool so any freed memory can't be used by another
760 * thread. This is to ensure each constituent that is mapped can be
761 * unmapped again afterwards.
762 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000763 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100764
Andrew Walbranca808b12020-05-15 17:22:28 +0100765 /* Iterate over the memory region constituents within each fragment. */
766 for (i = 0; i < fragment_count; ++i) {
767 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100768
Andrew Walbranca808b12020-05-15 17:22:28 +0100769 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
770 size_t size = fragments[i][j].page_count * PAGE_SIZE;
771 paddr_t begin =
772 pa_from_ipa(ipa_init(fragments[i][j].address));
773 paddr_t end = pa_add(begin, size);
774
J-Alves7db32002021-12-14 14:44:50 +0000775 if (!clear_memory(begin, end, &local_page_pool,
776 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100777 /*
778 * api_clear_memory will defrag on failure, so
779 * no need to do it here.
780 */
781 goto out;
782 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100783 }
784 }
785
Jose Marinho09b1db82019-08-08 09:16:59 +0100786 ret = true;
787
788out:
789 mpool_fini(&local_page_pool);
790 return ret;
791}
792
J-Alves5952d942022-12-22 16:03:00 +0000793static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
794 ipaddr_t in_begin, ipaddr_t in_end)
795{
796 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
797 ipa_addr(begin) < ipa_addr(in_end)) ||
798 (ipa_addr(end) <= ipa_addr(in_end) &&
799 ipa_addr(end) > ipa_addr(in_begin));
800}
801
802/**
803 * Receives a memory range and looks for overlaps with the remainder
804 * constituents of the memory share/lend/donate operation. Assumes they are
805 * passed in order to avoid having to loop over all the elements at each call.
806 * The function only compares the received memory ranges with those that follow
807 * within the same fragment, and subsequent fragments from the same operation.
808 */
809static bool ffa_memory_check_overlap(
810 struct ffa_memory_region_constituent **fragments,
811 const uint32_t *fragment_constituent_counts,
812 const uint32_t fragment_count, const uint32_t current_fragment,
813 const uint32_t current_constituent)
814{
815 uint32_t i = current_fragment;
816 uint32_t j = current_constituent;
817 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
818 const uint32_t current_page_count = fragments[i][j].page_count;
819 size_t current_size = current_page_count * PAGE_SIZE;
820 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
821
822 if (current_size == 0 ||
823 current_size > UINT64_MAX - ipa_addr(current_begin)) {
824 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
825 current_begin, current_page_count);
826 return false;
827 }
828
829 for (; i < fragment_count; i++) {
830 j = (i == current_fragment) ? j + 1 : 0;
831
832 for (; j < fragment_constituent_counts[i]; j++) {
833 ipaddr_t begin = ipa_init(fragments[i][j].address);
834 const uint32_t page_count = fragments[i][j].page_count;
835 size_t size = page_count * PAGE_SIZE;
836 ipaddr_t end = ipa_add(begin, size - 1);
837
838 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
839 dlog_verbose(
840 "Invalid page count. Addr: %x "
841 "page_count: %x\n",
842 begin, page_count);
843 return false;
844 }
845
846 /*
847 * Check if current ranges is within begin and end, as
848 * well as the reverse. This should help optimize the
849 * loop, and reduce the number of iterations.
850 */
851 if (is_memory_range_within(begin, end, current_begin,
852 current_end) ||
853 is_memory_range_within(current_begin, current_end,
854 begin, end)) {
855 dlog_verbose(
856 "Overlapping memory ranges: %#x - %#x "
857 "with %#x - %#x\n",
858 ipa_addr(begin), ipa_addr(end),
859 ipa_addr(current_begin),
860 ipa_addr(current_end));
861 return true;
862 }
863 }
864 }
865
866 return false;
867}
868
Jose Marinho09b1db82019-08-08 09:16:59 +0100869/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000870 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100871 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000872 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100873 *
874 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000875 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100876 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100877 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100878 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
879 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100880 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100881 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 */
J-Alves66652252022-07-06 09:49:51 +0100884struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100886 struct ffa_memory_region_constituent **fragments,
887 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000888 uint32_t composite_total_page_count, uint32_t share_func,
889 struct ffa_memory_access *receivers, uint32_t receivers_count,
890 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100891{
Andrew Walbranca808b12020-05-15 17:22:28 +0100892 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +0000893 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100894 uint32_t orig_from_mode;
895 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100896 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100897 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +0000898 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +0100899
900 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100901 * Make sure constituents are properly aligned to a 64-bit boundary. If
902 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100903 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100904 for (i = 0; i < fragment_count; ++i) {
905 if (!is_aligned(fragments[i], 8)) {
906 dlog_verbose("Constituents not aligned.\n");
907 return ffa_error(FFA_INVALID_PARAMETERS);
908 }
J-Alves8f11cde2022-12-21 16:18:22 +0000909 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
910 constituents_total_page_count +=
911 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +0000912 if (ffa_memory_check_overlap(
913 fragments, fragment_constituent_counts,
914 fragment_count, i, j)) {
915 return ffa_error(FFA_INVALID_PARAMETERS);
916 }
J-Alves8f11cde2022-12-21 16:18:22 +0000917 }
918 }
919
920 if (constituents_total_page_count != composite_total_page_count) {
921 dlog_verbose(
922 "Composite page count differs from calculated page "
923 "count from constituents.\n");
924 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100925 }
926
927 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000928 * Check if the state transition is lawful for the sender, ensure that
929 * all constituents of a memory region being shared are at the same
930 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100931 */
J-Alves363f5722022-04-25 17:37:37 +0100932 ret = ffa_send_check_transition(from_locked, share_func, receivers,
933 receivers_count, &orig_from_mode,
934 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100935 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100937 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100938 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100939 }
940
Andrew Walbran37c574e2020-06-03 11:45:46 +0100941 if (orig_from_mode_ret != NULL) {
942 *orig_from_mode_ret = orig_from_mode;
943 }
944
Jose Marinho09b1db82019-08-08 09:16:59 +0100945 /*
946 * Create a local pool so any freed memory can't be used by another
947 * thread. This is to ensure the original mapping can be restored if the
948 * clear fails.
949 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000950 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100951
952 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000953 * First reserve all required memory for the new page table entries
954 * without committing, to make sure the entire operation will succeed
955 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100956 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100957 if (!ffa_region_group_identity_map(
958 from_locked, fragments, fragment_constituent_counts,
959 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100960 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100962 goto out;
963 }
964
965 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966 * Update the mapping for the sender. This won't allocate because the
967 * transaction was already prepared above, but may free pages in the
968 * case that a whole block is being unmapped that was previously
969 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100970 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100971 CHECK(ffa_region_group_identity_map(
972 from_locked, fragments, fragment_constituent_counts,
973 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100974
975 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000976 if (clear &&
977 !ffa_clear_memory_constituents(
978 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
979 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100980 /*
981 * On failure, roll back by returning memory to the sender. This
982 * may allocate pages which were previously freed into
983 * `local_page_pool` by the call above, but will never allocate
984 * more pages than that so can never fail.
985 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100986 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100987 from_locked, fragments, fragment_constituent_counts,
988 fragment_count, orig_from_mode, &local_page_pool,
989 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100990
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100991 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100992 goto out;
993 }
994
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100995 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000996
997out:
998 mpool_fini(&local_page_pool);
999
1000 /*
1001 * Tidy up the page table by reclaiming failed mappings (if there was an
1002 * error) or merging entries into blocks where possible (on success).
1003 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001004 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005
1006 return ret;
1007}
1008
1009/**
1010 * Validates and maps memory shared from one VM to another.
1011 *
1012 * This function requires the calling context to hold the <to> lock.
1013 *
1014 * Returns:
1015 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001016 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001017 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001018 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001022struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001023 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001024 struct ffa_memory_region_constituent **fragments,
1025 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1026 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1027 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028{
Andrew Walbranca808b12020-05-15 17:22:28 +01001029 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 uint32_t to_mode;
1031 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001032 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001033
1034 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001035 * Make sure constituents are properly aligned to a 64-bit boundary. If
1036 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001037 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001038 for (i = 0; i < fragment_count; ++i) {
1039 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001040 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001041 return ffa_error(FFA_INVALID_PARAMETERS);
1042 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 }
1044
1045 /*
1046 * Check if the state transition is lawful for the recipient, and ensure
1047 * that all constituents of the memory region being retrieved are at the
1048 * same state.
1049 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 ret = ffa_retrieve_check_transition(
1051 to_locked, share_func, fragments, fragment_constituent_counts,
1052 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001053 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001054 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001055 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001056 }
1057
1058 /*
1059 * Create a local pool so any freed memory can't be used by another
1060 * thread. This is to ensure the original mapping can be restored if the
1061 * clear fails.
1062 */
1063 mpool_init_with_fallback(&local_page_pool, page_pool);
1064
1065 /*
1066 * First reserve all required memory for the new page table entries in
1067 * the recipient page tables without committing, to make sure the entire
1068 * operation will succeed without exhausting the page pool.
1069 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001070 if (!ffa_region_group_identity_map(
1071 to_locked, fragments, fragment_constituent_counts,
1072 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001073 /* TODO: partial defrag of failed range. */
1074 dlog_verbose(
1075 "Insufficient memory to update recipient page "
1076 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001077 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001078 goto out;
1079 }
1080
1081 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001082 if (clear &&
1083 !ffa_clear_memory_constituents(
1084 plat_ffa_owner_world_mode(from_id), fragments,
1085 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001086 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001087 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001088 goto out;
1089 }
1090
Jose Marinho09b1db82019-08-08 09:16:59 +01001091 /*
1092 * Complete the transfer by mapping the memory into the recipient. This
1093 * won't allocate because the transaction was already prepared above, so
1094 * it doesn't need to use the `local_page_pool`.
1095 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001096 CHECK(ffa_region_group_identity_map(
1097 to_locked, fragments, fragment_constituent_counts,
1098 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001099
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001100 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001101
1102out:
1103 mpool_fini(&local_page_pool);
1104
1105 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001106 * Tidy up the page table by reclaiming failed mappings (if there was an
1107 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001108 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001109 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001110
1111 return ret;
1112}
1113
Andrew Walbran996d1d12020-05-27 14:08:43 +01001114static struct ffa_value ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00001115 struct vm_locked from_locked, ffa_vm_id_t owner_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001116 struct ffa_memory_region_constituent **fragments,
1117 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1118 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001119{
1120 uint32_t orig_from_mode;
1121 uint32_t from_mode;
1122 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001124
Andrew Walbranca808b12020-05-15 17:22:28 +01001125 ret = ffa_relinquish_check_transition(
1126 from_locked, &orig_from_mode, fragments,
1127 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001129 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001130 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001131 }
1132
1133 /*
1134 * Create a local pool so any freed memory can't be used by another
1135 * thread. This is to ensure the original mapping can be restored if the
1136 * clear fails.
1137 */
1138 mpool_init_with_fallback(&local_page_pool, page_pool);
1139
1140 /*
1141 * First reserve all required memory for the new page table entries
1142 * without committing, to make sure the entire operation will succeed
1143 * without exhausting the page pool.
1144 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001145 if (!ffa_region_group_identity_map(
1146 from_locked, fragments, fragment_constituent_counts,
1147 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001148 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001149 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001150 goto out;
1151 }
1152
1153 /*
1154 * Update the mapping for the sender. This won't allocate because the
1155 * transaction was already prepared above, but may free pages in the
1156 * case that a whole block is being unmapped that was previously
1157 * partially mapped.
1158 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001159 CHECK(ffa_region_group_identity_map(
1160 from_locked, fragments, fragment_constituent_counts,
1161 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001162
1163 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001164 if (clear &&
1165 !ffa_clear_memory_constituents(
J-Alves3c5b2072022-11-21 12:45:40 +00001166 plat_ffa_owner_world_mode(owner_id), fragments,
J-Alves7db32002021-12-14 14:44:50 +00001167 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001168 /*
1169 * On failure, roll back by returning memory to the sender. This
1170 * may allocate pages which were previously freed into
1171 * `local_page_pool` by the call above, but will never allocate
1172 * more pages than that so can never fail.
1173 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001174 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001175 from_locked, fragments, fragment_constituent_counts,
1176 fragment_count, orig_from_mode, &local_page_pool,
1177 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001178
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001179 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001180 goto out;
1181 }
1182
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001183 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001184
1185out:
1186 mpool_fini(&local_page_pool);
1187
1188 /*
1189 * Tidy up the page table by reclaiming failed mappings (if there was an
1190 * error) or merging entries into blocks where possible (on success).
1191 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001192 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001193
1194 return ret;
1195}
1196
1197/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001198 * Complete a memory sending operation by checking that it is valid, updating
1199 * the sender page table, and then either marking the share state as having
1200 * completed sending (on success) or freeing it (on failure).
1201 *
1202 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1203 */
J-Alvesfdd29272022-07-19 13:16:31 +01001204struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001205 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001206 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1207 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001208{
1209 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001210 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001211 struct ffa_value ret;
1212
1213 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001214 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001215 assert(memory_region != NULL);
1216 composite = ffa_memory_region_get_composite(memory_region, 0);
1217 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001218
1219 /* Check that state is valid in sender page table and update. */
1220 ret = ffa_send_check_update(
1221 from_locked, share_state->fragments,
1222 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001223 share_state->fragment_count, composite->page_count,
1224 share_state->share_func, memory_region->receivers,
1225 memory_region->receiver_count, page_pool,
1226 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001227 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001228 if (ret.func != FFA_SUCCESS_32) {
1229 /*
1230 * Free share state, it failed to send so it can't be retrieved.
1231 */
1232 dlog_verbose("Complete failed, freeing share state.\n");
1233 share_state_free(share_states, share_state, page_pool);
1234 return ret;
1235 }
1236
1237 share_state->sending_complete = true;
1238 dlog_verbose("Marked sending complete.\n");
1239
J-Alvesee68c542020-10-29 17:48:20 +00001240 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001241}
1242
1243/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001244 * Check that the memory attributes match Hafnium expectations:
1245 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1246 * Write-Allocate Cacheable.
1247 */
1248static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001249 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001250{
1251 enum ffa_memory_type memory_type;
1252 enum ffa_memory_cacheability cacheability;
1253 enum ffa_memory_shareability shareability;
1254
1255 memory_type = ffa_get_memory_type_attr(attributes);
1256 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1257 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1258 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001259 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001260 }
1261
1262 cacheability = ffa_get_memory_cacheability_attr(attributes);
1263 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1264 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1265 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001266 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001267 }
1268
1269 shareability = ffa_get_memory_shareability_attr(attributes);
1270 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1271 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1272 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001273 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001274 }
1275
1276 return (struct ffa_value){.func = FFA_SUCCESS_32};
1277}
1278
1279/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001280 * Check that the given `memory_region` represents a valid memory send request
1281 * of the given `share_func` type, return the clear flag and permissions via the
1282 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001283 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001284 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001285 * not.
1286 */
J-Alves66652252022-07-06 09:49:51 +01001287struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001288 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1289 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001290 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001291{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 struct ffa_composite_memory_region *composite;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001293 uint64_t receivers_end;
1294 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001295 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001296 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001297 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001298 enum ffa_data_access data_access;
1299 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001300 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001301 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001302 const size_t minimum_first_fragment_length =
1303 (sizeof(struct ffa_memory_region) +
1304 sizeof(struct ffa_memory_access) +
1305 sizeof(struct ffa_composite_memory_region));
1306
1307 if (fragment_length < minimum_first_fragment_length) {
1308 dlog_verbose("Fragment length %u too short (min %u).\n",
1309 (size_t)fragment_length,
1310 minimum_first_fragment_length);
1311 return ffa_error(FFA_INVALID_PARAMETERS);
1312 }
1313
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001314 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1315 "struct ffa_memory_region_constituent must be 16 bytes");
1316 if (!is_aligned(fragment_length,
1317 sizeof(struct ffa_memory_region_constituent)) ||
1318 !is_aligned(memory_share_length,
1319 sizeof(struct ffa_memory_region_constituent))) {
1320 dlog_verbose(
1321 "Fragment length %u or total length %u"
1322 " is not 16-byte aligned.\n",
1323 fragment_length, memory_share_length);
1324 return ffa_error(FFA_INVALID_PARAMETERS);
1325 }
1326
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001327 if (fragment_length > memory_share_length) {
1328 dlog_verbose(
1329 "Fragment length %u greater than total length %u.\n",
1330 (size_t)fragment_length, (size_t)memory_share_length);
1331 return ffa_error(FFA_INVALID_PARAMETERS);
1332 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001333
J-Alves0b6653d2022-04-22 13:17:38 +01001334 assert(memory_region->receivers_offset ==
1335 offsetof(struct ffa_memory_region, receivers));
1336 assert(memory_region->memory_access_desc_size ==
1337 sizeof(struct ffa_memory_access));
1338
J-Alves95df0ef2022-12-07 10:09:48 +00001339 /* The sender must match the caller. */
1340 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1341 vm_id_is_current_world(memory_region->sender)) ||
1342 (vm_id_is_current_world(from_locked.vm->id) &&
1343 memory_region->sender != from_locked.vm->id)) {
1344 dlog_verbose("Invalid memory sender ID.\n");
1345 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001346 }
1347
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001348 if (memory_region->receiver_count <= 0) {
1349 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001350 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001351 }
1352
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001353 /*
1354 * Ensure that the composite header is within the memory bounds and
1355 * doesn't overlap the first part of the message. Cast to uint64_t
1356 * to prevent overflow.
1357 */
1358 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1359 (uint64_t)memory_region->receiver_count) +
1360 sizeof(struct ffa_memory_region);
1361 min_length = receivers_end +
1362 sizeof(struct ffa_composite_memory_region) +
1363 sizeof(struct ffa_memory_region_constituent);
1364 if (min_length > memory_share_length) {
1365 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1366 (size_t)memory_share_length, (size_t)min_length);
1367 return ffa_error(FFA_INVALID_PARAMETERS);
1368 }
1369
1370 composite_memory_region_offset =
1371 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001372
1373 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001374 * Check that the composite memory region descriptor is after the access
1375 * descriptors, is at least 16-byte aligned, and fits in the first
1376 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001377 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001378 if ((composite_memory_region_offset < receivers_end) ||
1379 (composite_memory_region_offset % 16 != 0) ||
1380 (composite_memory_region_offset >
1381 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1382 dlog_verbose(
1383 "Invalid composite memory region descriptor offset "
1384 "%u.\n",
1385 (size_t)composite_memory_region_offset);
1386 return ffa_error(FFA_INVALID_PARAMETERS);
1387 }
1388
1389 /*
1390 * Compute the start of the constituent regions. Already checked
1391 * to be not more than fragment_length and thus not more than
1392 * memory_share_length.
1393 */
1394 constituents_start = composite_memory_region_offset +
1395 sizeof(struct ffa_composite_memory_region);
1396 constituents_length = memory_share_length - constituents_start;
1397
1398 /*
1399 * Check that the number of constituents is consistent with the length
1400 * of the constituent region.
1401 */
1402 composite = ffa_memory_region_get_composite(memory_region, 0);
1403 if ((constituents_length %
1404 sizeof(struct ffa_memory_region_constituent) !=
1405 0) ||
1406 ((constituents_length /
1407 sizeof(struct ffa_memory_region_constituent)) !=
1408 composite->constituent_count)) {
1409 dlog_verbose("Invalid length %u or composite offset %u.\n",
1410 (size_t)memory_share_length,
1411 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001412 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001413 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001414 if (fragment_length < memory_share_length &&
1415 fragment_length < HF_MAILBOX_SIZE) {
1416 dlog_warning(
1417 "Initial fragment length %d smaller than mailbox "
1418 "size.\n",
1419 fragment_length);
1420 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 /*
1423 * Clear is not allowed for memory sharing, as the sender still has
1424 * access to the memory.
1425 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001426 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1427 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001430 }
1431
1432 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
1437
J-Alves363f5722022-04-25 17:37:37 +01001438 /* Check that the permissions are valid, for each specified receiver. */
1439 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1440 ffa_memory_access_permissions_t permissions =
1441 memory_region->receivers[i]
1442 .receiver_permissions.permissions;
1443 ffa_vm_id_t receiver_id =
1444 memory_region->receivers[i]
1445 .receiver_permissions.receiver;
1446
1447 if (memory_region->sender == receiver_id) {
1448 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001450 }
Federico Recanati85090c42021-12-15 13:17:54 +01001451
J-Alves363f5722022-04-25 17:37:37 +01001452 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1453 j++) {
1454 if (receiver_id ==
1455 memory_region->receivers[j]
1456 .receiver_permissions.receiver) {
1457 dlog_verbose(
1458 "Repeated receiver(%x) in memory send "
1459 "operation.\n",
1460 memory_region->receivers[j]
1461 .receiver_permissions.receiver);
1462 return ffa_error(FFA_INVALID_PARAMETERS);
1463 }
1464 }
1465
1466 if (composite_memory_region_offset !=
1467 memory_region->receivers[i]
1468 .composite_memory_region_offset) {
1469 dlog_verbose(
1470 "All ffa_memory_access should point to the "
1471 "same composite memory region offset.\n");
1472 return ffa_error(FFA_INVALID_PARAMETERS);
1473 }
1474
1475 data_access = ffa_get_data_access_attr(permissions);
1476 instruction_access =
1477 ffa_get_instruction_access_attr(permissions);
1478 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1479 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1480 dlog_verbose(
1481 "Reserved value for receiver permissions "
1482 "%#x.\n",
1483 permissions);
1484 return ffa_error(FFA_INVALID_PARAMETERS);
1485 }
1486 if (instruction_access !=
1487 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1488 dlog_verbose(
1489 "Invalid instruction access permissions %#x "
1490 "for sending memory.\n",
1491 permissions);
1492 return ffa_error(FFA_INVALID_PARAMETERS);
1493 }
1494 if (share_func == FFA_MEM_SHARE_32) {
1495 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1496 dlog_verbose(
1497 "Invalid data access permissions %#x "
1498 "for sharing memory.\n",
1499 permissions);
1500 return ffa_error(FFA_INVALID_PARAMETERS);
1501 }
1502 /*
1503 * According to section 10.10.3 of the FF-A v1.1 EAC0
1504 * spec, NX is required for share operations (but must
1505 * not be specified by the sender) so set it in the
1506 * copy that we store, ready to be returned to the
1507 * retriever.
1508 */
J-Alvesb19731a2022-06-20 17:30:33 +01001509 if (vm_id_is_current_world(receiver_id)) {
1510 ffa_set_instruction_access_attr(
1511 &permissions,
1512 FFA_INSTRUCTION_ACCESS_NX);
1513 memory_region->receivers[i]
1514 .receiver_permissions.permissions =
1515 permissions;
1516 }
J-Alves363f5722022-04-25 17:37:37 +01001517 }
1518 if (share_func == FFA_MEM_LEND_32 &&
1519 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1520 dlog_verbose(
1521 "Invalid data access permissions %#x for "
1522 "lending memory.\n",
1523 permissions);
1524 return ffa_error(FFA_INVALID_PARAMETERS);
1525 }
1526
1527 if (share_func == FFA_MEM_DONATE_32 &&
1528 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1529 dlog_verbose(
1530 "Invalid data access permissions %#x for "
1531 "donating memory.\n",
1532 permissions);
1533 return ffa_error(FFA_INVALID_PARAMETERS);
1534 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 }
1536
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001537 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1538 security_state =
1539 ffa_get_memory_security_attr(memory_region->attributes);
1540 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1541 dlog_verbose(
1542 "Invalid security state for memory share operation.\n");
1543 return ffa_error(FFA_INVALID_PARAMETERS);
1544 }
1545
Federico Recanatid937f5e2021-12-20 17:38:23 +01001546 /*
J-Alves807794e2022-06-16 13:42:47 +01001547 * If a memory donate or lend with single borrower, the memory type
1548 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001549 */
J-Alves807794e2022-06-16 13:42:47 +01001550 if (share_func == FFA_MEM_DONATE_32 ||
1551 (share_func == FFA_MEM_LEND_32 &&
1552 memory_region->receiver_count == 1)) {
1553 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1554 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1555 dlog_verbose(
1556 "Memory type shall not be specified by "
1557 "sender.\n");
1558 return ffa_error(FFA_INVALID_PARAMETERS);
1559 }
1560 } else {
1561 /*
1562 * Check that sender's memory attributes match Hafnium
1563 * expectations: Normal Memory, Inner shareable, Write-Back
1564 * Read-Allocate Write-Allocate Cacheable.
1565 */
1566 ret = ffa_memory_attributes_validate(memory_region->attributes);
1567 if (ret.func != FFA_SUCCESS_32) {
1568 return ret;
1569 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001570 }
1571
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001573}
1574
1575/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001576 * Gets the share state for continuing an operation to donate, lend or share
1577 * memory, and checks that it is a valid request.
1578 *
1579 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1580 * not.
1581 */
J-Alvesfdd29272022-07-19 13:16:31 +01001582struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001583 struct share_states_locked share_states, ffa_memory_handle_t handle,
1584 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1585 struct mpool *page_pool)
1586{
1587 struct ffa_memory_share_state *share_state;
1588 struct ffa_memory_region *memory_region;
1589
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001590 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001591
1592 /*
1593 * Look up the share state by handle and make sure that the VM ID
1594 * matches.
1595 */
1596 if (!get_share_state(share_states, handle, &share_state)) {
1597 dlog_verbose(
1598 "Invalid handle %#x for memory send continuation.\n",
1599 handle);
1600 return ffa_error(FFA_INVALID_PARAMETERS);
1601 }
1602 memory_region = share_state->memory_region;
1603
J-Alvesfdd29272022-07-19 13:16:31 +01001604 if (vm_id_is_current_world(from_vm_id) &&
1605 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001606 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1607 return ffa_error(FFA_INVALID_PARAMETERS);
1608 }
1609
1610 if (share_state->sending_complete) {
1611 dlog_verbose(
1612 "Sending of memory handle %#x is already complete.\n",
1613 handle);
1614 return ffa_error(FFA_INVALID_PARAMETERS);
1615 }
1616
1617 if (share_state->fragment_count == MAX_FRAGMENTS) {
1618 /*
1619 * Log a warning as this is a sign that MAX_FRAGMENTS should
1620 * probably be increased.
1621 */
1622 dlog_warning(
1623 "Too many fragments for memory share with handle %#x; "
1624 "only %d supported.\n",
1625 handle, MAX_FRAGMENTS);
1626 /* Free share state, as it's not possible to complete it. */
1627 share_state_free(share_states, share_state, page_pool);
1628 return ffa_error(FFA_NO_MEMORY);
1629 }
1630
1631 *share_state_ret = share_state;
1632
1633 return (struct ffa_value){.func = FFA_SUCCESS_32};
1634}
1635
1636/**
J-Alves95df0ef2022-12-07 10:09:48 +00001637 * Checks if there is at least one receiver from the other world.
1638 */
J-Alvesfdd29272022-07-19 13:16:31 +01001639bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001640 struct ffa_memory_region *memory_region)
1641{
1642 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1643 ffa_vm_id_t receiver = memory_region->receivers[i]
1644 .receiver_permissions.receiver;
1645 if (!vm_id_is_current_world(receiver)) {
1646 return true;
1647 }
1648 }
1649 return false;
1650}
1651
1652/**
J-Alves9da280b2022-12-21 14:55:39 +00001653 * Validates a call to donate, lend or share memory in which Hafnium is the
1654 * designated allocator of the memory handle. In practice, this also means
1655 * Hafnium is responsible for managing the state structures for the transaction.
1656 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1657 * sender is an SP or there is at least one borrower that is an SP.
1658 * If Hafnium is the hypervisor, it should allocate the memory handle when
1659 * operation involves only NWd VMs.
1660 *
1661 * If validation goes well, Hafnium updates the stage-2 page tables of the
1662 * sender. Validation consists of checking if the message length and number of
1663 * memory region constituents match, and if the transition is valid for the
1664 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001665 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001666 * Assumes that the caller has already found and locked the sender VM and copied
1667 * the memory region descriptor from the sender's TX buffer to a freshly
1668 * allocated page from Hafnium's internal pool. The caller must have also
1669 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001670 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001671 * This function takes ownership of the `memory_region` passed in and will free
1672 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001673 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001674struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001675 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001676 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001677 uint32_t fragment_length, uint32_t share_func,
1678 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001679{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001680 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001681 struct share_states_locked share_states;
1682 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001683
1684 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001685 * If there is an error validating the `memory_region` then we need to
1686 * free it because we own it but we won't be storing it in a share state
1687 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001688 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001689 ret = ffa_memory_send_validate(from_locked, memory_region,
1690 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001691 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001692 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001693 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001694 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001695 }
1696
Andrew Walbrana65a1322020-04-06 19:32:32 +01001697 /* Set flag for share function, ready to be retrieved later. */
1698 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001699 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001700 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001701 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001702 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001703 case FFA_MEM_LEND_32:
1704 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001705 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001706 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001707 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001708 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001709 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001710 }
1711
Andrew Walbranca808b12020-05-15 17:22:28 +01001712 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001713 /*
1714 * Allocate a share state before updating the page table. Otherwise if
1715 * updating the page table succeeded but allocating the share state
1716 * failed then it would leave the memory in a state where nobody could
1717 * get it back.
1718 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01001719 share_state = allocate_share_state(share_states, share_func,
1720 memory_region, fragment_length,
1721 FFA_MEMORY_HANDLE_INVALID);
1722 if (!share_state) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001723 dlog_verbose("Failed to allocate share state.\n");
1724 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 ret = ffa_error(FFA_NO_MEMORY);
1726 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727 }
1728
Andrew Walbranca808b12020-05-15 17:22:28 +01001729 if (fragment_length == memory_share_length) {
1730 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001731 ret = ffa_memory_send_complete(
1732 from_locked, share_states, share_state, page_pool,
1733 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001734 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001735 /*
1736 * Use sender ID from 'memory_region' assuming
1737 * that at this point it has been validated:
1738 * - MBZ at virtual FF-A instance.
1739 */
1740 ffa_vm_id_t sender_to_ret =
1741 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1742 ? memory_region->sender
1743 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001744 ret = (struct ffa_value){
1745 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001746 .arg1 = (uint32_t)memory_region->handle,
1747 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001748 .arg3 = fragment_length,
1749 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001750 }
1751
1752out:
1753 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001754 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001755 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001756}
1757
1758/**
J-Alves8505a8a2022-06-15 18:10:18 +01001759 * Continues an operation to donate, lend or share memory to a VM from current
1760 * world. If this is the last fragment then checks that the transition is valid
1761 * for the type of memory sending operation and updates the stage-2 page tables
1762 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001763 *
1764 * Assumes that the caller has already found and locked the sender VM and copied
1765 * the memory region descriptor from the sender's TX buffer to a freshly
1766 * allocated page from Hafnium's internal pool.
1767 *
1768 * This function takes ownership of the `fragment` passed in; it must not be
1769 * freed by the caller.
1770 */
1771struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1772 void *fragment,
1773 uint32_t fragment_length,
1774 ffa_memory_handle_t handle,
1775 struct mpool *page_pool)
1776{
1777 struct share_states_locked share_states = share_states_lock();
1778 struct ffa_memory_share_state *share_state;
1779 struct ffa_value ret;
1780 struct ffa_memory_region *memory_region;
1781
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001782 CHECK(is_aligned(fragment,
1783 alignof(struct ffa_memory_region_constituent)));
1784 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1785 0) {
1786 dlog_verbose("Fragment length %u misaligned.\n",
1787 fragment_length);
1788 ret = ffa_error(FFA_INVALID_PARAMETERS);
1789 goto out_free_fragment;
1790 }
1791
Andrew Walbranca808b12020-05-15 17:22:28 +01001792 ret = ffa_memory_send_continue_validate(share_states, handle,
1793 &share_state,
1794 from_locked.vm->id, page_pool);
1795 if (ret.func != FFA_SUCCESS_32) {
1796 goto out_free_fragment;
1797 }
1798 memory_region = share_state->memory_region;
1799
J-Alves95df0ef2022-12-07 10:09:48 +00001800 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001801 dlog_error(
1802 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001803 "other world. This should never happen, and indicates "
1804 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001805 "EL3 code.\n");
1806 ret = ffa_error(FFA_INVALID_PARAMETERS);
1807 goto out_free_fragment;
1808 }
1809
1810 /* Add this fragment. */
1811 share_state->fragments[share_state->fragment_count] = fragment;
1812 share_state->fragment_constituent_counts[share_state->fragment_count] =
1813 fragment_length / sizeof(struct ffa_memory_region_constituent);
1814 share_state->fragment_count++;
1815
1816 /* Check whether the memory send operation is now ready to complete. */
1817 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001818 ret = ffa_memory_send_complete(
1819 from_locked, share_states, share_state, page_pool,
1820 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001821 } else {
1822 ret = (struct ffa_value){
1823 .func = FFA_MEM_FRAG_RX_32,
1824 .arg1 = (uint32_t)handle,
1825 .arg2 = (uint32_t)(handle >> 32),
1826 .arg3 = share_state_next_fragment_offset(share_states,
1827 share_state)};
1828 }
1829 goto out;
1830
1831out_free_fragment:
1832 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001833
1834out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001835 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001836 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001837}
1838
Andrew Walbranca808b12020-05-15 17:22:28 +01001839/** Clean up after the receiver has finished retrieving a memory region. */
1840static void ffa_memory_retrieve_complete(
1841 struct share_states_locked share_states,
1842 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1843{
1844 if (share_state->share_func == FFA_MEM_DONATE_32) {
1845 /*
1846 * Memory that has been donated can't be relinquished,
1847 * so no need to keep the share state around.
1848 */
1849 share_state_free(share_states, share_state, page_pool);
1850 dlog_verbose("Freed share state for donate.\n");
1851 }
1852}
1853
J-Alves2d8457f2022-10-05 11:06:41 +01001854/**
1855 * Initialises the given memory region descriptor to be used for an
1856 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1857 * fragment.
1858 * The memory region descriptor is initialized according to retriever's
1859 * FF-A version.
1860 *
1861 * Returns true on success, or false if the given constituents won't all fit in
1862 * the first fragment.
1863 */
1864static bool ffa_retrieved_memory_region_init(
1865 void *response, uint32_t ffa_version, size_t response_max_size,
1866 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1867 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1868 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1869 uint32_t page_count, uint32_t total_constituent_count,
1870 const struct ffa_memory_region_constituent constituents[],
1871 uint32_t fragment_constituent_count, uint32_t *total_length,
1872 uint32_t *fragment_length)
1873{
1874 struct ffa_composite_memory_region *composite_memory_region;
1875 struct ffa_memory_access *receiver;
1876 uint32_t i;
1877 uint32_t constituents_offset;
1878 uint32_t receiver_count;
1879
1880 assert(response != NULL);
1881
1882 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1883 struct ffa_memory_region_v1_0 *retrieve_response =
1884 (struct ffa_memory_region_v1_0 *)response;
1885
J-Alves5da37d92022-10-24 16:33:48 +01001886 ffa_memory_region_init_header_v1_0(
1887 retrieve_response, sender, attributes, flags, handle, 0,
1888 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001889
1890 receiver = &retrieve_response->receivers[0];
1891 receiver_count = retrieve_response->receiver_count;
1892
1893 receiver->composite_memory_region_offset =
1894 sizeof(struct ffa_memory_region_v1_0) +
1895 receiver_count * sizeof(struct ffa_memory_access);
1896
1897 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1898 retrieve_response, 0);
1899 } else {
1900 /* Default to FF-A v1.1 version. */
1901 struct ffa_memory_region *retrieve_response =
1902 (struct ffa_memory_region *)response;
1903
1904 ffa_memory_region_init_header(retrieve_response, sender,
1905 attributes, flags, handle, 0, 1);
1906
1907 receiver = &retrieve_response->receivers[0];
1908 receiver_count = retrieve_response->receiver_count;
1909
1910 /*
1911 * Note that `sizeof(struct_ffa_memory_region)` and
1912 * `sizeof(struct ffa_memory_access)` must both be multiples of
1913 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1914 * guaranteed that the offset we calculate here is aligned to a
1915 * 64-bit boundary and so 64-bit values can be copied without
1916 * alignment faults.
1917 */
1918 receiver->composite_memory_region_offset =
1919 sizeof(struct ffa_memory_region) +
1920 receiver_count * sizeof(struct ffa_memory_access);
1921
1922 composite_memory_region =
1923 ffa_memory_region_get_composite(retrieve_response, 0);
1924 }
1925
1926 assert(receiver != NULL);
1927 assert(composite_memory_region != NULL);
1928
1929 /*
1930 * Initialized here as in memory retrieve responses we currently expect
1931 * one borrower to be specified.
1932 */
1933 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1934 receiver->receiver_permissions.permissions = permissions;
1935
1936 composite_memory_region->page_count = page_count;
1937 composite_memory_region->constituent_count = total_constituent_count;
1938 composite_memory_region->reserved_0 = 0;
1939
1940 constituents_offset = receiver->composite_memory_region_offset +
1941 sizeof(struct ffa_composite_memory_region);
1942 if (constituents_offset +
1943 fragment_constituent_count *
1944 sizeof(struct ffa_memory_region_constituent) >
1945 response_max_size) {
1946 return false;
1947 }
1948
1949 for (i = 0; i < fragment_constituent_count; ++i) {
1950 composite_memory_region->constituents[i] = constituents[i];
1951 }
1952
1953 if (total_length != NULL) {
1954 *total_length =
1955 constituents_offset +
1956 composite_memory_region->constituent_count *
1957 sizeof(struct ffa_memory_region_constituent);
1958 }
1959 if (fragment_length != NULL) {
1960 *fragment_length =
1961 constituents_offset +
1962 fragment_constituent_count *
1963 sizeof(struct ffa_memory_region_constituent);
1964 }
1965
1966 return true;
1967}
1968
J-Alves96de29f2022-04-26 16:05:24 +01001969/*
1970 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1971 * returns its index in the receiver's array. If receiver's ID doesn't exist
1972 * in the array, return the region's 'receiver_count'.
1973 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001974uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1975 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001976{
1977 struct ffa_memory_access *receivers;
1978 uint32_t i;
1979
1980 assert(memory_region != NULL);
1981
1982 receivers = memory_region->receivers;
1983
1984 for (i = 0U; i < memory_region->receiver_count; i++) {
1985 if (receivers[i].receiver_permissions.receiver == receiver) {
1986 break;
1987 }
1988 }
1989
1990 return i;
1991}
1992
1993/**
1994 * Validates the retrieved permissions against those specified by the lender
1995 * of memory share operation. Optionally can help set the permissions to be used
1996 * for the S2 mapping, through the `permissions` argument.
1997 * Returns true if permissions are valid, false otherwise.
1998 */
1999static bool ffa_memory_retrieve_is_memory_access_valid(
2000 enum ffa_data_access sent_data_access,
2001 enum ffa_data_access requested_data_access,
2002 enum ffa_instruction_access sent_instruction_access,
2003 enum ffa_instruction_access requested_instruction_access,
2004 ffa_memory_access_permissions_t *permissions)
2005{
2006 switch (sent_data_access) {
2007 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2008 case FFA_DATA_ACCESS_RW:
2009 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2010 requested_data_access == FFA_DATA_ACCESS_RW) {
2011 if (permissions != NULL) {
2012 ffa_set_data_access_attr(permissions,
2013 FFA_DATA_ACCESS_RW);
2014 }
2015 break;
2016 }
2017 /* Intentional fall-through. */
2018 case FFA_DATA_ACCESS_RO:
2019 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2020 requested_data_access == FFA_DATA_ACCESS_RO) {
2021 if (permissions != NULL) {
2022 ffa_set_data_access_attr(permissions,
2023 FFA_DATA_ACCESS_RO);
2024 }
2025 break;
2026 }
2027 dlog_verbose(
2028 "Invalid data access requested; sender specified "
2029 "permissions %#x but receiver requested %#x.\n",
2030 sent_data_access, requested_data_access);
2031 return false;
2032 case FFA_DATA_ACCESS_RESERVED:
2033 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2034 "checked before this point.");
2035 }
2036
2037 switch (sent_instruction_access) {
2038 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2039 case FFA_INSTRUCTION_ACCESS_X:
2040 if (requested_instruction_access ==
2041 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2042 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2043 if (permissions != NULL) {
2044 ffa_set_instruction_access_attr(
2045 permissions, FFA_INSTRUCTION_ACCESS_X);
2046 }
2047 break;
2048 }
2049 case FFA_INSTRUCTION_ACCESS_NX:
2050 if (requested_instruction_access ==
2051 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2052 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2053 if (permissions != NULL) {
2054 ffa_set_instruction_access_attr(
2055 permissions, FFA_INSTRUCTION_ACCESS_NX);
2056 }
2057 break;
2058 }
2059 dlog_verbose(
2060 "Invalid instruction access requested; sender "
2061 "specified permissions %#x but receiver requested "
2062 "%#x.\n",
2063 sent_instruction_access, requested_instruction_access);
2064 return false;
2065 case FFA_INSTRUCTION_ACCESS_RESERVED:
2066 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2067 "be checked before this point.");
2068 }
2069
2070 return true;
2071}
2072
2073/**
2074 * Validate the receivers' permissions in the retrieve request against those
2075 * specified by the lender.
2076 * In the `permissions` argument returns the permissions to set at S2 for the
2077 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2078 * Returns FFA_SUCCESS if all specified permissions are valid.
2079 */
2080static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2081 struct ffa_memory_region *memory_region,
2082 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2083 ffa_memory_access_permissions_t *permissions)
2084{
2085 uint32_t retrieve_receiver_index;
2086
2087 assert(permissions != NULL);
2088
2089 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2090 dlog_verbose(
2091 "Retrieve request should contain same list of "
2092 "borrowers, as specified by the lender.\n");
2093 return ffa_error(FFA_INVALID_PARAMETERS);
2094 }
2095
2096 retrieve_receiver_index = retrieve_request->receiver_count;
2097
2098 /* Should be populated with the permissions of the retriever. */
2099 *permissions = 0;
2100
2101 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2102 ffa_memory_access_permissions_t sent_permissions;
2103 struct ffa_memory_access *current_receiver =
2104 &retrieve_request->receivers[i];
2105 ffa_memory_access_permissions_t requested_permissions =
2106 current_receiver->receiver_permissions.permissions;
2107 ffa_vm_id_t current_receiver_id =
2108 current_receiver->receiver_permissions.receiver;
2109 bool found_to_id = current_receiver_id == to_vm_id;
2110
2111 /*
2112 * Find the current receiver in the transaction descriptor from
2113 * sender.
2114 */
2115 uint32_t mem_region_receiver_index =
2116 ffa_memory_region_get_receiver(memory_region,
2117 current_receiver_id);
2118
2119 if (mem_region_receiver_index ==
2120 memory_region->receiver_count) {
2121 dlog_verbose("%s: receiver %x not found\n", __func__,
2122 current_receiver_id);
2123 return ffa_error(FFA_DENIED);
2124 }
2125
2126 sent_permissions =
2127 memory_region->receivers[mem_region_receiver_index]
2128 .receiver_permissions.permissions;
2129
2130 if (found_to_id) {
2131 retrieve_receiver_index = i;
2132 }
2133
2134 /*
2135 * Since we are traversing the list of receivers, save the index
2136 * of the caller. As it needs to be there.
2137 */
2138
2139 if (current_receiver->composite_memory_region_offset != 0U) {
2140 dlog_verbose(
2141 "Retriever specified address ranges not "
2142 "supported (got offset %d).\n",
2143 current_receiver
2144 ->composite_memory_region_offset);
2145 return ffa_error(FFA_INVALID_PARAMETERS);
2146 }
2147
2148 /*
2149 * Check permissions from sender against permissions requested
2150 * by receiver.
2151 */
2152 if (!ffa_memory_retrieve_is_memory_access_valid(
2153 ffa_get_data_access_attr(sent_permissions),
2154 ffa_get_data_access_attr(requested_permissions),
2155 ffa_get_instruction_access_attr(sent_permissions),
2156 ffa_get_instruction_access_attr(
2157 requested_permissions),
2158 found_to_id ? permissions : NULL)) {
2159 return ffa_error(FFA_DENIED);
2160 }
2161
2162 /*
2163 * Can't request PM to clear memory if only provided with RO
2164 * permissions.
2165 */
2166 if (found_to_id &&
2167 (ffa_get_data_access_attr(*permissions) ==
2168 FFA_DATA_ACCESS_RO) &&
2169 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2170 0U) {
2171 dlog_verbose(
2172 "Receiver has RO permissions can not request "
2173 "clear.\n");
2174 return ffa_error(FFA_DENIED);
2175 }
2176 }
2177
2178 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2179 dlog_verbose(
2180 "Retrieve request does not contain caller's (%x) "
2181 "permissions\n",
2182 to_vm_id);
2183 return ffa_error(FFA_INVALID_PARAMETERS);
2184 }
2185
2186 return (struct ffa_value){.func = FFA_SUCCESS_32};
2187}
2188
J-Alvesa9cd7e32022-07-01 13:49:33 +01002189/*
2190 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2191 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2192 * of a pending memory sharing operation whose allocator is the SPM, for
2193 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2194 * the memory region descriptor of the retrieve request must be zeroed with the
2195 * exception of the sender ID and handle.
2196 */
2197bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2198 struct vm_locked to_locked)
2199{
2200 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2201 request->attributes == 0U && request->flags == 0U &&
2202 request->tag == 0U && request->receiver_count == 0U &&
2203 plat_ffa_memory_handle_allocated_by_current_world(
2204 request->handle);
2205}
2206
2207/*
2208 * Helper to reset count of fragments retrieved by the hypervisor.
2209 */
2210static void ffa_memory_retrieve_complete_from_hyp(
2211 struct ffa_memory_share_state *share_state)
2212{
2213 if (share_state->hypervisor_fragment_count ==
2214 share_state->fragment_count) {
2215 share_state->hypervisor_fragment_count = 0;
2216 }
2217}
2218
J-Alves089004f2022-07-13 14:25:44 +01002219/**
2220 * Validate that the memory region descriptor provided by the borrower on
2221 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2222 * memory sharing call.
2223 */
2224static struct ffa_value ffa_memory_retrieve_validate(
2225 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2226 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2227 uint32_t share_func)
2228{
2229 ffa_memory_region_flags_t transaction_type =
2230 retrieve_request->flags &
2231 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002232 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002233
2234 assert(retrieve_request != NULL);
2235 assert(memory_region != NULL);
2236 assert(receiver_index != NULL);
2237 assert(retrieve_request->sender == memory_region->sender);
2238
2239 /*
2240 * Check that the transaction type expected by the receiver is
2241 * correct, if it has been specified.
2242 */
2243 if (transaction_type !=
2244 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2245 transaction_type != (memory_region->flags &
2246 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2247 dlog_verbose(
2248 "Incorrect transaction type %#x for "
2249 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2250 transaction_type,
2251 memory_region->flags &
2252 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2253 retrieve_request->handle);
2254 return ffa_error(FFA_INVALID_PARAMETERS);
2255 }
2256
2257 if (retrieve_request->tag != memory_region->tag) {
2258 dlog_verbose(
2259 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2260 "%d for handle %#x.\n",
2261 retrieve_request->tag, memory_region->tag,
2262 retrieve_request->handle);
2263 return ffa_error(FFA_INVALID_PARAMETERS);
2264 }
2265
2266 *receiver_index =
2267 ffa_memory_region_get_receiver(memory_region, receiver_id);
2268
2269 if (*receiver_index == memory_region->receiver_count) {
2270 dlog_verbose(
2271 "Incorrect receiver VM ID %d for "
2272 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002273 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002274 return ffa_error(FFA_INVALID_PARAMETERS);
2275 }
2276
2277 if ((retrieve_request->flags &
2278 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2279 dlog_verbose(
2280 "Retriever specified 'address range alignment 'hint' "
2281 "not supported.\n");
2282 return ffa_error(FFA_INVALID_PARAMETERS);
2283 }
2284 if ((retrieve_request->flags &
2285 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2286 dlog_verbose(
2287 "Bits 8-5 must be zero in memory region's flags "
2288 "(address range alignment hint not supported).\n");
2289 return ffa_error(FFA_INVALID_PARAMETERS);
2290 }
2291
2292 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2293 dlog_verbose(
2294 "Bits 31-10 must be zero in memory region's flags.\n");
2295 return ffa_error(FFA_INVALID_PARAMETERS);
2296 }
2297
2298 if (share_func == FFA_MEM_SHARE_32 &&
2299 (retrieve_request->flags &
2300 (FFA_MEMORY_REGION_FLAG_CLEAR |
2301 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2302 dlog_verbose(
2303 "Memory Share operation can't clean after relinquish "
2304 "memory region.\n");
2305 return ffa_error(FFA_INVALID_PARAMETERS);
2306 }
2307
2308 /*
2309 * If the borrower needs the memory to be cleared before mapping
2310 * to its address space, the sender should have set the flag
2311 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2312 * FFA_DENIED.
2313 */
2314 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2315 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2316 dlog_verbose(
2317 "Borrower needs memory cleared. Sender needs to set "
2318 "flag for clearing memory.\n");
2319 return ffa_error(FFA_DENIED);
2320 }
2321
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002322 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2323 security_state =
2324 ffa_get_memory_security_attr(retrieve_request->attributes);
2325 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2326 dlog_verbose(
2327 "Invalid security state for memory retrieve request "
2328 "operation.\n");
2329 return ffa_error(FFA_INVALID_PARAMETERS);
2330 }
2331
J-Alves089004f2022-07-13 14:25:44 +01002332 /*
2333 * If memory type is not specified, bypass validation of memory
2334 * attributes in the retrieve request. The retriever is expecting to
2335 * obtain this information from the SPMC.
2336 */
2337 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2338 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2339 return (struct ffa_value){.func = FFA_SUCCESS_32};
2340 }
2341
2342 /*
2343 * Ensure receiver's attributes are compatible with how
2344 * Hafnium maps memory: Normal Memory, Inner shareable,
2345 * Write-Back Read-Allocate Write-Allocate Cacheable.
2346 */
2347 return ffa_memory_attributes_validate(retrieve_request->attributes);
2348}
2349
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002350struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2351 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002352 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002353 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002354{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002355 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002356 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002357 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002358 sizeof(struct ffa_memory_access);
2359 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002360 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002361 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002362 uint32_t memory_to_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002363 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002364 struct ffa_memory_share_state *share_state;
2365 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 struct ffa_composite_memory_region *composite;
2367 uint32_t total_length;
2368 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002369 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002370 bool is_send_complete = false;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002371 ffa_memory_attributes_t attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002372
2373 dump_share_states();
2374
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002375 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002376 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002377 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002378 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002379 expected_retrieve_request_length,
2380 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002381 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002382 }
2383
2384 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01002385 if (!get_share_state(share_states, handle, share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002386 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002387 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002388 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002389 goto out;
2390 }
2391
J-Alves96de29f2022-04-26 16:05:24 +01002392 if (!share_state->sending_complete) {
2393 dlog_verbose(
2394 "Memory with handle %#x not fully sent, can't "
2395 "retrieve.\n",
2396 handle);
2397 ret = ffa_error(FFA_INVALID_PARAMETERS);
2398 goto out;
2399 }
2400
Andrew Walbrana65a1322020-04-06 19:32:32 +01002401 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002402
Andrew Walbrana65a1322020-04-06 19:32:32 +01002403 CHECK(memory_region != NULL);
2404
J-Alves089004f2022-07-13 14:25:44 +01002405 if (retrieve_request->sender != memory_region->sender) {
2406 dlog_verbose(
2407 "Memory with handle %#x not fully sent, can't "
2408 "retrieve.\n",
2409 handle);
2410 ret = ffa_error(FFA_INVALID_PARAMETERS);
2411 goto out;
2412 }
J-Alves96de29f2022-04-26 16:05:24 +01002413
J-Alvesa9cd7e32022-07-01 13:49:33 +01002414 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2415 to_locked)) {
2416 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002417
J-Alvesb5084cf2022-07-06 14:20:12 +01002418 /*
2419 * The SPMC can only process retrieve requests to memory share
2420 * operations with one borrower from the other world. It can't
2421 * determine the ID of the NWd VM that invoked the retrieve
2422 * request interface call. It relies on the hypervisor to
2423 * validate the caller's ID against that provided in the
2424 * `receivers` list of the retrieve response.
2425 * In case there is only one borrower from the NWd in the
2426 * transaction descriptor, record that in the `receiver_id` for
2427 * later use, and validate in the retrieve request message.
2428 */
2429 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2430 uint32_t other_world_count = 0;
2431
2432 for (uint32_t i = 0; i < memory_region->receiver_count;
2433 i++) {
2434 receiver_id =
2435 retrieve_request->receivers[0]
2436 .receiver_permissions.receiver;
2437 if (!vm_id_is_current_world(receiver_id)) {
2438 other_world_count++;
2439 }
2440 }
2441 if (other_world_count > 1) {
2442 dlog_verbose(
2443 "Support one receiver from the other "
2444 "world.\n");
2445 return ffa_error(FFA_NOT_SUPPORTED);
2446 }
2447 }
2448
2449 /*
2450 * Validate retrieve request, according to what was sent by the
2451 * sender. Function will output the `receiver_index` from the
2452 * provided memory region, and will output `permissions` from
2453 * the validated requested permissions.
2454 */
J-Alves089004f2022-07-13 14:25:44 +01002455 ret = ffa_memory_retrieve_validate(
2456 receiver_id, retrieve_request, memory_region,
2457 &receiver_index, share_state->share_func);
2458 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002459 goto out;
2460 }
2461
2462 if (share_state->retrieved_fragment_count[receiver_index] !=
2463 0U) {
2464 dlog_verbose(
2465 "Memory with handle %#x already retrieved.\n",
2466 handle);
2467 ret = ffa_error(FFA_DENIED);
2468 goto out;
2469 }
2470
J-Alvesa9cd7e32022-07-01 13:49:33 +01002471 ret = ffa_memory_retrieve_validate_memory_access_list(
2472 memory_region, retrieve_request, receiver_id,
2473 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002474 if (ret.func != FFA_SUCCESS_32) {
2475 goto out;
2476 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002477
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002478 memory_to_mode = ffa_memory_permissions_to_mode(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002479 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002480
J-Alvesa9cd7e32022-07-01 13:49:33 +01002481 ret = ffa_retrieve_check_update(
2482 to_locked, memory_region->sender,
2483 share_state->fragments,
2484 share_state->fragment_constituent_counts,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002485 share_state->fragment_count, memory_to_mode,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002486 share_state->share_func, false, page_pool);
2487
2488 if (ret.func != FFA_SUCCESS_32) {
2489 goto out;
2490 }
2491
2492 share_state->retrieved_fragment_count[receiver_index] = 1;
2493 is_send_complete =
2494 share_state->retrieved_fragment_count[receiver_index] ==
2495 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002496
2497 share_state->clear_after_relinquish =
2498 (retrieve_request->flags &
2499 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2500
J-Alvesa9cd7e32022-07-01 13:49:33 +01002501 } else {
2502 if (share_state->hypervisor_fragment_count != 0U) {
2503 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002504 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002505 "the hypervisor.\n",
2506 handle);
2507 ret = ffa_error(FFA_DENIED);
2508 goto out;
2509 }
2510
2511 share_state->hypervisor_fragment_count = 1;
2512
2513 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002514 }
2515
J-Alvesb5084cf2022-07-06 14:20:12 +01002516 /* VMs acquire the RX buffer from SPMC. */
2517 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2518
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002520 * Copy response to RX buffer of caller and deliver the message.
2521 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002522 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002523 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002524 composite = ffa_memory_region_get_composite(memory_region, 0);
2525 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002526 * Constituents which we received in the first fragment should
2527 * always fit in the first fragment we are sending, because the
2528 * header is the same size in both cases and we have a fixed
2529 * message buffer size. So `ffa_retrieved_memory_region_init`
2530 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002531 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002532
2533 /*
2534 * Set the security state in the memory retrieve response attributes
2535 * if specified by the target mode.
2536 */
2537 attributes = plat_ffa_memory_security_mode(
2538 memory_region->attributes, share_state->sender_orig_mode);
2539
Andrew Walbranca808b12020-05-15 17:22:28 +01002540 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002541 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002542 HF_MAILBOX_SIZE, memory_region->sender, attributes,
2543 memory_region->flags, handle, receiver_id, permissions,
2544 composite->page_count, composite->constituent_count,
2545 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002546 share_state->fragment_constituent_counts[0], &total_length,
2547 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002548
Andrew Walbranca808b12020-05-15 17:22:28 +01002549 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002552 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553
J-Alvesa9cd7e32022-07-01 13:49:33 +01002554 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002555 ffa_memory_retrieve_complete(share_states, share_state,
2556 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002558 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002559 .arg1 = total_length,
2560 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002561out:
2562 share_states_unlock(&share_states);
2563 dump_share_states();
2564 return ret;
2565}
2566
J-Alves5da37d92022-10-24 16:33:48 +01002567/**
2568 * Determine expected fragment offset according to the FF-A version of
2569 * the caller.
2570 */
2571static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2572 struct ffa_memory_region *memory_region,
2573 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2574{
2575 uint32_t expected_fragment_offset;
2576 uint32_t composite_constituents_offset;
2577
2578 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2579 /*
2580 * Hafnium operates memory regions in FF-A v1.1 format, so we
2581 * can retrieve the constituents offset from descriptor.
2582 */
2583 composite_constituents_offset =
2584 ffa_composite_constituent_offset(memory_region, 0);
2585 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2586 /*
2587 * If retriever is FF-A v1.0, determine the composite offset
2588 * as it is expected to have been configured in the
2589 * retrieve response.
2590 */
2591 composite_constituents_offset =
2592 sizeof(struct ffa_memory_region_v1_0) +
2593 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2594 sizeof(struct ffa_memory_access) +
2595 sizeof(struct ffa_composite_memory_region);
2596 } else {
2597 panic("%s received an invalid FF-A version.\n", __func__);
2598 }
2599
2600 expected_fragment_offset =
2601 composite_constituents_offset +
2602 retrieved_constituents_count *
2603 sizeof(struct ffa_memory_region_constituent) -
2604 sizeof(struct ffa_memory_access) *
2605 (memory_region->receiver_count - 1);
2606
2607 return expected_fragment_offset;
2608}
2609
Andrew Walbranca808b12020-05-15 17:22:28 +01002610struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2611 ffa_memory_handle_t handle,
2612 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002613 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002614 struct mpool *page_pool)
2615{
2616 struct ffa_memory_region *memory_region;
2617 struct share_states_locked share_states;
2618 struct ffa_memory_share_state *share_state;
2619 struct ffa_value ret;
2620 uint32_t fragment_index;
2621 uint32_t retrieved_constituents_count;
2622 uint32_t i;
2623 uint32_t expected_fragment_offset;
2624 uint32_t remaining_constituent_count;
2625 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002626 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002627 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002628
2629 dump_share_states();
2630
2631 share_states = share_states_lock();
2632 if (!get_share_state(share_states, handle, &share_state)) {
2633 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2634 handle);
2635 ret = ffa_error(FFA_INVALID_PARAMETERS);
2636 goto out;
2637 }
2638
2639 memory_region = share_state->memory_region;
2640 CHECK(memory_region != NULL);
2641
Andrew Walbranca808b12020-05-15 17:22:28 +01002642 if (!share_state->sending_complete) {
2643 dlog_verbose(
2644 "Memory with handle %#x not fully sent, can't "
2645 "retrieve.\n",
2646 handle);
2647 ret = ffa_error(FFA_INVALID_PARAMETERS);
2648 goto out;
2649 }
2650
J-Alves59ed0042022-07-28 18:26:41 +01002651 /*
2652 * If retrieve request from the hypervisor has been initiated in the
2653 * given share_state, continue it, else assume it is a continuation of
2654 * retrieve request from a NWd VM.
2655 */
2656 continue_ffa_hyp_mem_retrieve_req =
2657 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2658 (share_state->hypervisor_fragment_count != 0U) &&
2659 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002660
J-Alves59ed0042022-07-28 18:26:41 +01002661 if (!continue_ffa_hyp_mem_retrieve_req) {
2662 receiver_index = ffa_memory_region_get_receiver(
2663 memory_region, to_locked.vm->id);
2664
2665 if (receiver_index == memory_region->receiver_count) {
2666 dlog_verbose(
2667 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2668 "borrower to memory sharing transaction (%x)\n",
2669 to_locked.vm->id, handle);
2670 ret = ffa_error(FFA_INVALID_PARAMETERS);
2671 goto out;
2672 }
2673
2674 if (share_state->retrieved_fragment_count[receiver_index] ==
2675 0 ||
2676 share_state->retrieved_fragment_count[receiver_index] >=
2677 share_state->fragment_count) {
2678 dlog_verbose(
2679 "Retrieval of memory with handle %#x not yet "
2680 "started or already completed (%d/%d fragments "
2681 "retrieved).\n",
2682 handle,
2683 share_state->retrieved_fragment_count
2684 [receiver_index],
2685 share_state->fragment_count);
2686 ret = ffa_error(FFA_INVALID_PARAMETERS);
2687 goto out;
2688 }
2689
2690 fragment_index =
2691 share_state->retrieved_fragment_count[receiver_index];
2692 } else {
2693 if (share_state->hypervisor_fragment_count == 0 ||
2694 share_state->hypervisor_fragment_count >=
2695 share_state->fragment_count) {
2696 dlog_verbose(
2697 "Retrieve of memory with handle %x not "
2698 "started from hypervisor.\n",
2699 handle);
2700 ret = ffa_error(FFA_INVALID_PARAMETERS);
2701 goto out;
2702 }
2703
2704 if (memory_region->sender != sender_vm_id) {
2705 dlog_verbose(
2706 "Sender ID (%x) is not as expected for memory "
2707 "handle %x\n",
2708 sender_vm_id, handle);
2709 ret = ffa_error(FFA_INVALID_PARAMETERS);
2710 goto out;
2711 }
2712
2713 fragment_index = share_state->hypervisor_fragment_count;
2714
2715 receiver_index = 0;
2716 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002717
2718 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002719 * Check that the given fragment offset is correct by counting
2720 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002721 */
2722 retrieved_constituents_count = 0;
2723 for (i = 0; i < fragment_index; ++i) {
2724 retrieved_constituents_count +=
2725 share_state->fragment_constituent_counts[i];
2726 }
J-Alvesc7484f12022-05-13 12:41:14 +01002727
2728 CHECK(memory_region->receiver_count > 0);
2729
Andrew Walbranca808b12020-05-15 17:22:28 +01002730 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002731 ffa_memory_retrieve_expected_offset_per_ffa_version(
2732 memory_region, retrieved_constituents_count,
2733 to_locked.vm->ffa_version);
2734
Andrew Walbranca808b12020-05-15 17:22:28 +01002735 if (fragment_offset != expected_fragment_offset) {
2736 dlog_verbose("Fragment offset was %d but expected %d.\n",
2737 fragment_offset, expected_fragment_offset);
2738 ret = ffa_error(FFA_INVALID_PARAMETERS);
2739 goto out;
2740 }
2741
J-Alves59ed0042022-07-28 18:26:41 +01002742 /* VMs acquire the RX buffer from SPMC. */
2743 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2744
Andrew Walbranca808b12020-05-15 17:22:28 +01002745 remaining_constituent_count = ffa_memory_fragment_init(
2746 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2747 share_state->fragments[fragment_index],
2748 share_state->fragment_constituent_counts[fragment_index],
2749 &fragment_length);
2750 CHECK(remaining_constituent_count == 0);
2751 to_locked.vm->mailbox.recv_size = fragment_length;
2752 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2753 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002754 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002755
J-Alves59ed0042022-07-28 18:26:41 +01002756 if (!continue_ffa_hyp_mem_retrieve_req) {
2757 share_state->retrieved_fragment_count[receiver_index]++;
2758 if (share_state->retrieved_fragment_count[receiver_index] ==
2759 share_state->fragment_count) {
2760 ffa_memory_retrieve_complete(share_states, share_state,
2761 page_pool);
2762 }
2763 } else {
2764 share_state->hypervisor_fragment_count++;
2765
2766 ffa_memory_retrieve_complete_from_hyp(share_state);
2767 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002768 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2769 .arg1 = (uint32_t)handle,
2770 .arg2 = (uint32_t)(handle >> 32),
2771 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002772
2773out:
2774 share_states_unlock(&share_states);
2775 dump_share_states();
2776 return ret;
2777}
2778
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002779struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002780 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002781 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002782{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002783 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002784 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002785 struct ffa_memory_share_state *share_state;
2786 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002787 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002788 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002789 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002790 bool receivers_relinquished_memory;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002791
Andrew Walbrana65a1322020-04-06 19:32:32 +01002792 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002793 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002794 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002795 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002796 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002797 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002798 }
2799
Andrew Walbrana65a1322020-04-06 19:32:32 +01002800 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002801 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002802 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002803 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002804 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002805 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002806 }
2807
2808 dump_share_states();
2809
2810 share_states = share_states_lock();
2811 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002812 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002813 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002814 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002815 goto out;
2816 }
2817
Andrew Walbranca808b12020-05-15 17:22:28 +01002818 if (!share_state->sending_complete) {
2819 dlog_verbose(
2820 "Memory with handle %#x not fully sent, can't "
2821 "relinquish.\n",
2822 handle);
2823 ret = ffa_error(FFA_INVALID_PARAMETERS);
2824 goto out;
2825 }
2826
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002827 memory_region = share_state->memory_region;
2828 CHECK(memory_region != NULL);
2829
J-Alves8eb19162022-04-28 10:56:48 +01002830 receiver_index = ffa_memory_region_get_receiver(memory_region,
2831 from_locked.vm->id);
2832
2833 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002834 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002835 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01002836 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01002837 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002838 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002839 goto out;
2840 }
2841
J-Alves8eb19162022-04-28 10:56:48 +01002842 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002843 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002844 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002845 "Memory with handle %#x not yet fully "
2846 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002847 "receiver %x can't relinquish.\n",
2848 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002849 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002850 goto out;
2851 }
2852
J-Alves3c5b2072022-11-21 12:45:40 +00002853 /*
2854 * Either clear if requested in relinquish call, or in a retrieve
2855 * request from one of the borrowers.
2856 */
2857 receivers_relinquished_memory = true;
2858
2859 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2860 struct ffa_memory_access *receiver =
2861 &memory_region->receivers[i];
2862
2863 if (receiver->receiver_permissions.receiver ==
2864 from_locked.vm->id) {
2865 continue;
2866 }
2867
2868 if (share_state->retrieved_fragment_count[i] != 0U) {
2869 receivers_relinquished_memory = false;
2870 break;
2871 }
2872 }
2873
2874 clear = receivers_relinquished_memory &&
2875 (share_state->clear_after_relinquish ||
2876 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2877 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002878
2879 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002880 * Clear is not allowed for memory that was shared, as the
2881 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002882 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002883 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002884 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002885 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002886 goto out;
2887 }
2888
Andrew Walbranca808b12020-05-15 17:22:28 +01002889 ret = ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00002890 from_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002891 share_state->fragment_constituent_counts,
2892 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002893
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002894 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002895 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002896 * Mark memory handle as not retrieved, so it can be
2897 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002898 */
J-Alves8eb19162022-04-28 10:56:48 +01002899 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002900 }
2901
2902out:
2903 share_states_unlock(&share_states);
2904 dump_share_states();
2905 return ret;
2906}
2907
2908/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002909 * Validates that the reclaim transition is allowed for the given
2910 * handle, updates the page table of the reclaiming VM, and frees the
2911 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002912 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002913struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002914 ffa_memory_handle_t handle,
2915 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002916 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002917{
2918 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002919 struct ffa_memory_share_state *share_state;
2920 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002921 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002922
2923 dump_share_states();
2924
2925 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01002926
J-Alvesb5084cf2022-07-06 14:20:12 +01002927 if (get_share_state(share_states, handle, &share_state)) {
2928 memory_region = share_state->memory_region;
2929 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002930 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002931 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002932 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002933 goto out;
2934 }
2935
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002936 CHECK(memory_region != NULL);
2937
J-Alvesa9cd7e32022-07-01 13:49:33 +01002938 if (vm_id_is_current_world(to_locked.vm->id) &&
2939 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002940 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002941 "VM %#x attempted to reclaim memory handle %#x "
2942 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002943 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002944 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002945 goto out;
2946 }
2947
Andrew Walbranca808b12020-05-15 17:22:28 +01002948 if (!share_state->sending_complete) {
2949 dlog_verbose(
2950 "Memory with handle %#x not fully sent, can't "
2951 "reclaim.\n",
2952 handle);
2953 ret = ffa_error(FFA_INVALID_PARAMETERS);
2954 goto out;
2955 }
2956
J-Alves752236c2022-04-28 11:07:47 +01002957 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2958 if (share_state->retrieved_fragment_count[i] != 0) {
2959 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002960 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00002961 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002962 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002963 handle,
2964 memory_region->receivers[i]
2965 .receiver_permissions.receiver);
2966 ret = ffa_error(FFA_DENIED);
2967 goto out;
2968 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002969 }
2970
Andrew Walbranca808b12020-05-15 17:22:28 +01002971 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002972 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002973 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002974 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002975 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002976
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002977 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002978 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00002979 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002980 }
2981
2982out:
2983 share_states_unlock(&share_states);
2984 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002985}