blob: 802d26f7b6d6043b80a008df3132136d889f6144 [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
J-Alves7b9cc432024-04-04 10:57:17 +010011#include "hf/arch/memcpy_trapped.h"
Federico Recanati4fd065d2021-12-13 20:06:23 +010012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020014#include "hf/arch/plat/ffa.h"
Karl Meakin64cadf52024-07-24 17:42:57 +010015#include "hf/arch/plat/ffa/ffa_memory.h"
Karl Meakin9724b362024-10-15 14:35:02 +010016#include "hf/arch/plat/ffa/indirect_messaging.h"
Karl Meakin48e049c2024-07-25 18:07:41 +010017#include "hf/arch/plat/ffa/setup_and_discovery.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000018
J-Alves5952d942022-12-22 16:03:00 +000019#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000021#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010022#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010024#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010025#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010026#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010027#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000028#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000029#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000030#include "hf/panic.h"
31#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010032#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000033#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000034#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010035
J-Alves2d8457f2022-10-05 11:06:41 +010036#include "vmapi/hf/ffa_v1_0.h"
37
J-Alves5da37d92022-10-24 16:33:48 +010038#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
39
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010041 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000042 * by this lock.
43 */
44static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000046
47/**
J-Alvesed508c82023-05-04 16:09:48 +010048 * Return the offset to the first constituent within the
49 * `ffa_composite_memory_region` for the given receiver from an
50 * `ffa_memory_region`. The caller must check that the receiver_index is within
51 * bounds, and that it has a composite memory region offset.
52 */
53static uint32_t ffa_composite_constituent_offset(
54 struct ffa_memory_region *memory_region, uint32_t receiver_index)
55{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000056 struct ffa_memory_access *receiver;
57 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010058
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000059 CHECK(receiver_index < memory_region->receiver_count);
60
61 receiver =
62 ffa_memory_region_get_receiver(memory_region, receiver_index);
63 CHECK(receiver != NULL);
64
65 composite_offset = receiver->composite_memory_region_offset;
66
67 CHECK(composite_offset != 0);
68
69 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010070}
71
72/**
J-Alves917d2f22020-10-30 18:39:30 +000073 * Extracts the index from a memory handle allocated by Hafnium's current world.
74 */
75uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
76{
77 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
78}
79
80/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010081 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
82 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
83 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010084 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010085 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
86 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010087 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010088struct ffa_memory_share_state *allocate_share_state(
89 struct share_states_locked share_states, uint32_t share_func,
90 struct ffa_memory_region *memory_region, uint32_t fragment_length,
91 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000093 assert(share_states.share_states != NULL);
94 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000095
Karl Meakin52cdfe72023-06-30 14:49:10 +010096 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010097 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010098 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010099 &share_states.share_states[i];
100 struct ffa_composite_memory_region *composite =
101 ffa_memory_region_get_composite(memory_region,
102 0);
103
104 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000105 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200106 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100107 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000108 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100109 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 allocated_state->share_func = share_func;
111 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100112 allocated_state->fragment_count = 1;
113 allocated_state->fragments[0] = composite->constituents;
114 allocated_state->fragment_constituent_counts[0] =
115 (fragment_length -
116 ffa_composite_constituent_offset(memory_region,
117 0)) /
118 sizeof(struct ffa_memory_region_constituent);
119 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100120 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
121 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100122 allocated_state->retrieved_fragment_count[j] =
123 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100125 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000126 }
127 }
128
Karl Meakin52cdfe72023-06-30 14:49:10 +0100129 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000130}
131
132/** Locks the share states lock. */
133struct share_states_locked share_states_lock(void)
134{
135 sl_lock(&share_states_lock_instance);
136
137 return (struct share_states_locked){.share_states = share_states};
138}
139
140/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100141void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000143 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144 share_states->share_states = NULL;
145 sl_unlock(&share_states_lock_instance);
146}
147
148/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100149 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100152struct ffa_memory_share_state *get_share_state(
153 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100155 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000156
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000157 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100158
159 /*
160 * First look for a share_state allocated by us, in which case the
161 * handle is based on the index.
162 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200163 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100164 uint64_t index = ffa_memory_handle_get_index(handle);
165
Andrew Walbranca808b12020-05-15 17:22:28 +0100166 if (index < MAX_MEM_SHARES) {
167 share_state = &share_states.share_states[index];
168 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100169 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100170 }
171 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000172 }
173
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100175 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100176 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000177 if (share_state->memory_region != NULL &&
178 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100180 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100181 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000182 }
183
Karl Meakin4a2854a2023-06-30 16:26:52 +0100184 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185}
186
187/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100188void share_state_free(struct share_states_locked share_states,
189 struct ffa_memory_share_state *share_state,
190 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000191{
Andrew Walbranca808b12020-05-15 17:22:28 +0100192 uint32_t i;
193
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000194 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000197 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100198 /*
199 * First fragment is part of the same page as the `memory_region`, so it
200 * doesn't need to be freed separately.
201 */
202 share_state->fragments[0] = NULL;
203 share_state->fragment_constituent_counts[0] = 0;
204 for (i = 1; i < share_state->fragment_count; ++i) {
205 mpool_free(page_pool, share_state->fragments[i]);
206 share_state->fragments[i] = NULL;
207 share_state->fragment_constituent_counts[i] = 0;
208 }
209 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100211 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000212}
213
Andrew Walbranca808b12020-05-15 17:22:28 +0100214/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100215bool share_state_sending_complete(struct share_states_locked share_states,
216 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000217{
Andrew Walbranca808b12020-05-15 17:22:28 +0100218 struct ffa_composite_memory_region *composite;
219 uint32_t expected_constituent_count;
220 uint32_t fragment_constituent_count_total = 0;
221 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000222
Andrew Walbranca808b12020-05-15 17:22:28 +0100223 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000224 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100225
226 /*
227 * Share state must already be valid, or it's not possible to get hold
228 * of it.
229 */
230 CHECK(share_state->memory_region != NULL &&
231 share_state->share_func != 0);
232
233 composite =
234 ffa_memory_region_get_composite(share_state->memory_region, 0);
235 expected_constituent_count = composite->constituent_count;
236 for (i = 0; i < share_state->fragment_count; ++i) {
237 fragment_constituent_count_total +=
238 share_state->fragment_constituent_counts[i];
239 }
240 dlog_verbose(
241 "Checking completion: constituent count %d/%d from %d "
242 "fragments.\n",
243 fragment_constituent_count_total, expected_constituent_count,
244 share_state->fragment_count);
245
246 return fragment_constituent_count_total == expected_constituent_count;
247}
248
249/**
250 * Calculates the offset of the next fragment expected for the given share
251 * state.
252 */
J-Alvesfdd29272022-07-19 13:16:31 +0100253uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100254 struct share_states_locked share_states,
255 struct ffa_memory_share_state *share_state)
256{
257 uint32_t next_fragment_offset;
258 uint32_t i;
259
260 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000261 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100262
263 next_fragment_offset =
264 ffa_composite_constituent_offset(share_state->memory_region, 0);
265 for (i = 0; i < share_state->fragment_count; ++i) {
266 next_fragment_offset +=
267 share_state->fragment_constituent_counts[i] *
268 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269 }
270
Andrew Walbranca808b12020-05-15 17:22:28 +0100271 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000272}
273
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100274static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275{
276 uint32_t i;
277
278 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
279 return;
280 }
281
Karl Meakine8937d92024-03-19 16:04:25 +0000282 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
283 "type = %s, security = %s), flags %#x, handle %#lx "
284 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100285 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000286 memory_region->sender,
287 ffa_memory_shareability_name(
288 memory_region->attributes.shareability),
289 ffa_memory_cacheability_name(
290 memory_region->attributes.cacheability),
291 ffa_memory_type_name(memory_region->attributes.type),
292 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000293 memory_region->flags, memory_region->handle, memory_region->tag,
294 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100295 memory_region->receiver_count);
296 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000297 struct ffa_memory_access *receiver =
298 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000299 if (i != 0) {
300 dlog(", ");
301 }
Karl Meakine8937d92024-03-19 16:04:25 +0000302 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000303 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000304 ffa_data_access_name(receiver->receiver_permissions
305 .permissions.data_access),
306 ffa_instruction_access_name(
307 receiver->receiver_permissions.permissions
308 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000309 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000310 /* The impdef field is only present from v1.2 and later */
311 if (ffa_version_from_memory_access_desc_size(
312 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100313 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000314 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000315 receiver->impdef.val[1]);
316 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000317 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000318 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000319}
320
J-Alves66652252022-07-06 09:49:51 +0100321void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000322{
323 uint32_t i;
324
325 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
326 return;
327 }
328
329 dlog("Current share states:\n");
330 sl_lock(&share_states_lock_instance);
331 for (i = 0; i < MAX_MEM_SHARES; ++i) {
332 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000334 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000336 dlog("SHARE");
337 break;
J-Alves95fbb312024-03-20 15:19:16 +0000338 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100339 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 dlog("LEND");
341 break;
J-Alves95fbb312024-03-20 15:19:16 +0000342 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100343 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000344 dlog("DONATE");
345 break;
346 default:
347 dlog("invalid share_func %#x",
348 share_states[i].share_func);
349 }
Karl Meakine8937d92024-03-19 16:04:25 +0000350 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100352 if (share_states[i].sending_complete) {
353 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100355 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000356 }
J-Alves2a0d2882020-10-29 14:49:50 +0000357 dlog(" with %d fragments, %d retrieved, "
358 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100359 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000360 share_states[i].retrieved_fragment_count[0],
361 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000362 }
363 }
364 sl_unlock(&share_states_lock_instance);
365}
366
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100368 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000369{
370 uint32_t mode = 0;
371
Karl Meakin84710f32023-10-12 15:14:49 +0100372 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100373 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000374 mode = MM_MODE_R;
375 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000377 mode = MM_MODE_R | MM_MODE_W;
378 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100379 case FFA_DATA_ACCESS_NOT_SPECIFIED:
380 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
381 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100382 case FFA_DATA_ACCESS_RESERVED:
383 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100384 default:
385 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100386 }
387
Karl Meakin84710f32023-10-12 15:14:49 +0100388 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100389 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000390 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100391 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100392 mode |= MM_MODE_X;
393 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100394 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
395 mode |= (default_mode & MM_MODE_X);
396 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397 case FFA_INSTRUCTION_ACCESS_RESERVED:
398 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100399 default:
400 panic("Unknown instruction access %#x\n",
401 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000402 }
403
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200404 /* Set the security state bit if necessary. */
405 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
406 mode |= plat_ffa_other_world_mode();
407 }
408
Daniel Boulby6e261362024-06-13 16:53:00 +0100409 mode |= default_mode & MM_MODE_D;
410
Andrew Walbran475c1452020-02-07 13:22:22 +0000411 return mode;
412}
413
Jose Marinho75509b42019-04-09 09:34:59 +0100414/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000415 * Get the current mode in the stage-2 page table of the given vm of all the
416 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100418 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000420 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100421 struct ffa_memory_region_constituent **fragments,
422 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100423{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100424 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100425 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100426
Andrew Walbranca808b12020-05-15 17:22:28 +0100427 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000429 * Fail if there are no constituents. Otherwise we would get an
430 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100431 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100432 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100434 }
435
Andrew Walbranca808b12020-05-15 17:22:28 +0100436 for (i = 0; i < fragment_count; ++i) {
437 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
438 ipaddr_t begin = ipa_init(fragments[i][j].address);
439 size_t size = fragments[i][j].page_count * PAGE_SIZE;
440 ipaddr_t end = ipa_add(begin, size);
441 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100442
Andrew Walbranca808b12020-05-15 17:22:28 +0100443 /* Fail if addresses are not page-aligned. */
444 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
445 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100446 dlog_verbose("%s: addresses not page-aligned\n",
447 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100448 return ffa_error(FFA_INVALID_PARAMETERS);
449 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100450
Andrew Walbranca808b12020-05-15 17:22:28 +0100451 /*
452 * Ensure that this constituent memory range is all
453 * mapped with the same mode.
454 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800455 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100456 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000457 "%s: constituent memory range "
458 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100459 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000460 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100461 return ffa_error(FFA_DENIED);
462 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100463
Andrew Walbranca808b12020-05-15 17:22:28 +0100464 /*
465 * Ensure that all constituents are mapped with the same
466 * mode.
467 */
468 if (i == 0) {
469 *orig_mode = current_mode;
470 } else if (current_mode != *orig_mode) {
471 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100472 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000473 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100474 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100475 fragments[i][j].page_count,
476 ipa_addr(begin));
477 return ffa_error(FFA_DENIED);
478 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100479 }
Jose Marinho75509b42019-04-09 09:34:59 +0100480 }
481
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100482 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000483}
484
Karl Meakin0e617d92024-04-05 12:55:22 +0100485enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100486 uint32_t memory_access_desc_size)
487{
488 switch (memory_access_desc_size) {
489 /*
490 * v1.0 and v1.1 memory access descriptors are the same size however
491 * v1.1 is the first version to include the memory access descriptor
492 * size field so return v1.1.
493 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000494 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100495 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000496 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100497 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100498 default:
499 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100500 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100501}
502
503/**
504 * Check if the receivers size and offset given is valid for the senders
505 * FF-A version.
506 */
507static bool receiver_size_and_offset_valid_for_version(
508 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100509 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100510{
511 /*
512 * Check that the version that the memory access descriptor size belongs
513 * to is compatible with the FF-A version we believe the sender to be.
514 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100515 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100516 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100517 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100518 return false;
519 }
520
521 /*
522 * Check the receivers_offset matches the version we found from
523 * memory access descriptor size.
524 */
525 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100526 case FFA_VERSION_1_1:
527 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100528 return receivers_offset == sizeof(struct ffa_memory_region);
529 default:
530 return false;
531 }
532}
533
534/**
535 * Check the values set for fields in the memory region are valid and safe.
536 * Offset values are within safe bounds, receiver count will not cause overflows
537 * and reserved fields are 0.
538 */
539bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100540 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100541 uint32_t fragment_length,
542 bool send_transaction)
543{
544 uint32_t receiver_count;
545 struct ffa_memory_access *receiver;
546 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000547 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
548 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100549
Karl Meakin0e617d92024-04-05 12:55:22 +0100550 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100551 /* Check the reserved fields are 0. */
552 if (memory_region_v1_0->reserved_0 != 0 ||
553 memory_region_v1_0->reserved_1 != 0) {
554 dlog_verbose("Reserved fields must be 0.\n");
555 return false;
556 }
557
558 receiver_count = memory_region_v1_0->receiver_count;
559 } else {
560 uint32_t receivers_size =
561 memory_region->memory_access_desc_size;
562 uint32_t receivers_offset = memory_region->receivers_offset;
563
564 /* Check the reserved field is 0. */
565 if (memory_region->reserved[0] != 0 ||
566 memory_region->reserved[1] != 0 ||
567 memory_region->reserved[2] != 0) {
568 dlog_verbose("Reserved fields must be 0.\n");
569 return false;
570 }
571
572 /*
573 * Check memory_access_desc_size matches the size of the struct
574 * for the senders FF-A version.
575 */
576 if (!receiver_size_and_offset_valid_for_version(
577 receivers_size, receivers_offset, ffa_version)) {
578 dlog_verbose(
579 "Invalid memory access descriptor size %d, "
580 " or receiver offset %d, "
581 "for FF-A version %#x\n",
582 receivers_size, receivers_offset, ffa_version);
583 return false;
584 }
585
586 receiver_count = memory_region->receiver_count;
587 }
588
589 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000590 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100591 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000592 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100593 "specified %u\n",
594 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
595 return false;
596 }
597
598 /* Check values in the memory access descriptors. */
599 /*
600 * The composite offset values must be the same for all recievers so
601 * check the first one is valid and then they are all the same.
602 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100603 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000604 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100605 ->receivers[0]
606 : ffa_memory_region_get_receiver(memory_region, 0);
607 assert(receiver != NULL);
608 composite_offset_0 = receiver->composite_memory_region_offset;
609
610 if (!send_transaction) {
611 if (composite_offset_0 != 0) {
612 dlog_verbose(
613 "Composite offset memory region descriptor "
614 "offset must be 0 for retrieve requests. "
615 "Currently %d",
616 composite_offset_0);
617 return false;
618 }
619 } else {
620 bool comp_offset_is_zero = composite_offset_0 == 0U;
621 bool comp_offset_lt_transaction_descriptor_size =
622 composite_offset_0 <
623 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100624 (size_t)(memory_region->memory_access_desc_size *
625 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100626 bool comp_offset_with_comp_gt_fragment_length =
627 composite_offset_0 +
628 sizeof(struct ffa_composite_memory_region) >
629 fragment_length;
630 if (comp_offset_is_zero ||
631 comp_offset_lt_transaction_descriptor_size ||
632 comp_offset_with_comp_gt_fragment_length) {
633 dlog_verbose(
634 "Invalid composite memory region descriptor "
635 "offset for send transaction %u\n",
636 composite_offset_0);
637 return false;
638 }
639 }
640
Karl Meakin824b63d2024-06-03 19:04:53 +0100641 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100642 uint32_t composite_offset;
643
Karl Meakin0e617d92024-04-05 12:55:22 +0100644 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100645 struct ffa_memory_access_v1_0 *receiver_v1_0 =
646 &memory_region_v1_0->receivers[i];
647 /* Check reserved fields are 0 */
648 if (receiver_v1_0->reserved_0 != 0) {
649 dlog_verbose(
650 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000651 "descriptor must be zero. Currently "
652 "reciever %zu has a reserved field "
653 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100654 i, receiver_v1_0->reserved_0);
655 return false;
656 }
657 /*
658 * We can cast to the current version receiver as the
659 * remaining fields we are checking have the same
660 * offsets for all versions since memory access
661 * descriptors are forwards compatible.
662 */
663 receiver = (struct ffa_memory_access *)receiver_v1_0;
664 } else {
665 receiver = ffa_memory_region_get_receiver(memory_region,
666 i);
667 assert(receiver != NULL);
668
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100669 if (ffa_version == FFA_VERSION_1_1) {
670 /*
671 * Since the reserved field is at the end of the
672 * Endpoint Memory Access Descriptor we must
673 * cast to ffa_memory_access_v1_0 as they match.
674 * Since all fields except reserved in the
675 * Endpoint Memory Access Descriptor have the
676 * same offsets across all versions this cast is
677 * not required when accessing other fields in
678 * the future.
679 */
680 struct ffa_memory_access_v1_0 *receiver_v1_0 =
681 (struct ffa_memory_access_v1_0 *)
682 receiver;
683 if (receiver_v1_0->reserved_0 != 0) {
684 dlog_verbose(
685 "Reserved field in the memory "
686 "access descriptor must be "
687 "zero. Currently reciever %zu "
688 "has a reserved field with a "
689 "value of %lu\n",
690 i, receiver_v1_0->reserved_0);
691 return false;
692 }
693
694 } else {
695 if (receiver->reserved_0 != 0) {
696 dlog_verbose(
697 "Reserved field in the memory "
698 "access descriptor must be "
699 "zero. Currently reciever %zu "
700 "has a reserved field with a "
701 "value of %lu\n",
702 i, receiver->reserved_0);
703 return false;
704 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100705 }
706 }
707
708 /* Check composite offset values are equal for all receivers. */
709 composite_offset = receiver->composite_memory_region_offset;
710 if (composite_offset != composite_offset_0) {
711 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000712 "Composite offset %x differs from %x in "
713 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100714 composite_offset, composite_offset_0);
715 return false;
716 }
717 }
718 return true;
719}
720
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000721/**
J-Alves460d36c2023-10-12 17:02:15 +0100722 * If the receivers for the memory management operation are all from the
Daniel Boulby734981e2024-07-22 11:06:35 +0100723 * secure world, the memory is not device memory (as it isn't covered by the
724 * granule page table) and this isn't a FFA_MEM_SHARE, then request memory
725 * security state update by returning MAP_ACTION_CHECK_PROTECT.
J-Alves460d36c2023-10-12 17:02:15 +0100726 */
727static enum ffa_map_action ffa_mem_send_get_map_action(
728 bool all_receivers_from_current_world, ffa_id_t sender_id,
Daniel Boulby734981e2024-07-22 11:06:35 +0100729 uint32_t mem_func_id, bool is_normal_memory)
J-Alves460d36c2023-10-12 17:02:15 +0100730{
J-Alves95fbb312024-03-20 15:19:16 +0000731 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
732 mem_func_id == FFA_MEM_SHARE_64;
733 const bool protect_memory =
734 (!is_memory_share_abi && all_receivers_from_current_world &&
Daniel Boulby734981e2024-07-22 11:06:35 +0100735 ffa_is_vm_id(sender_id) && is_normal_memory);
J-Alves460d36c2023-10-12 17:02:15 +0100736
737 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
738}
739
740/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000741 * Verify that all pages have the same mode, that the starting mode
742 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100743 * to the sending VM. It outputs the mapping action that needs to be
744 * invoked for the given memory range. On memory lend/donate there
745 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000746 *
747 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100748 * 1) FFA_DENIED if a state transition was not found;
749 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100750 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100752 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100753 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
754 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000755 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100756static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100757 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000758 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100759 struct ffa_memory_region_constituent **fragments,
760 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100761 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000762{
763 const uint32_t state_mask =
764 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100765 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100766 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000767 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000768 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
769 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000770
Andrew Walbranca808b12020-05-15 17:22:28 +0100771 ret = constituents_get_mode(from, orig_from_mode, fragments,
772 fragment_constituent_counts,
773 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100774 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100775 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100776 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100777 }
778
Daniel Boulby6e261362024-06-13 16:53:00 +0100779 /*
780 * Check requested memory type is valid with the memory type of the
781 * owner. E.g. they follow the memory type precedence where Normal
782 * memory is more permissive than device and therefore device memory
783 * can only be shared as device memory.
784 */
785 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
786 (*orig_from_mode & MM_MODE_D) != 0U) {
787 dlog_verbose(
788 "Send device memory as Normal memory is not allowed\n");
789 return ffa_error(FFA_DENIED);
790 }
791
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000792 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000793 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000794 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000795 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000796 "Device memory can only be lent to a single borrower "
797 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000798 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000800 }
801
802 /*
803 * Ensure the sender is the owner and has exclusive access to the
804 * memory.
805 */
806 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100807 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100808 }
809
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100810 /*
811 * Memory cannot be zeroed during the lend/donate operation if the
812 * sender only has RO access.
813 */
814 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
815 dlog_verbose(
816 "Cannot zero memory when the sender doesn't have "
817 "write access\n");
818 return ffa_error(FFA_DENIED);
819 }
820
Daniel Boulbya76fd912024-02-22 14:22:15 +0000821 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100822
J-Alves363f5722022-04-25 17:37:37 +0100823 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000824 struct ffa_memory_access *receiver =
825 ffa_memory_region_get_receiver(memory_region, i);
826 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100827 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000828 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100829 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
830 permissions, *orig_from_mode);
831
J-Alves788b4492023-04-18 14:01:23 +0100832 /*
833 * The assumption is that at this point, the operation from
834 * SP to a receiver VM, should have returned an FFA_ERROR
835 * already.
836 */
837 if (!ffa_is_vm_id(from.vm->id)) {
838 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000839 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100840 }
841
J-Alves460d36c2023-10-12 17:02:15 +0100842 /* Track if all senders are from current world. */
843 all_receivers_from_current_world =
844 all_receivers_from_current_world &&
845 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000846 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100847
J-Alves363f5722022-04-25 17:37:37 +0100848 if ((*orig_from_mode & required_from_mode) !=
849 required_from_mode) {
850 dlog_verbose(
851 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100852 "which required mode %#x but only had %#x "
853 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100854 required_from_mode, *orig_from_mode);
855 return ffa_error(FFA_DENIED);
856 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857 }
858
J-Alves460d36c2023-10-12 17:02:15 +0100859 *map_action = ffa_mem_send_get_map_action(
Daniel Boulby734981e2024-07-22 11:06:35 +0100860 all_receivers_from_current_world, from.vm->id, share_func,
861 (*orig_from_mode & MM_MODE_D) == 0U);
J-Alves460d36c2023-10-12 17:02:15 +0100862
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000863 /* Find the appropriate new mode. */
864 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000865 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000866 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100867 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000868 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100869 break;
J-Alves95fbb312024-03-20 15:19:16 +0000870 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000872 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100873 break;
J-Alves95fbb312024-03-20 15:19:16 +0000874 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100875 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100877 break;
878
Jose Marinho75509b42019-04-09 09:34:59 +0100879 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100880 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100881 }
882
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000884}
885
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000887 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100888 struct ffa_memory_region_constituent **fragments,
889 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100890 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000891{
892 const uint32_t state_mask =
893 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
894 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000896
J-Alves69cdfd92024-04-26 11:40:59 +0100897 assert(map_action != NULL);
898 if (vm_id_is_current_world(from.vm->id)) {
899 *map_action = MAP_ACTION_COMMIT;
900 } else {
901 /*
902 * No need to check the attributes of caller.
903 * The assumption is that the retrieve request of the receiver
904 * also used the MAP_ACTION_NONE, and no update was done to the
905 * page tables. When the receiver is not at the secure virtual
906 * instance SPMC doesn't manage its S2 translation (i.e. when
907 * the receiver is a VM).
908 */
909 *map_action = MAP_ACTION_NONE;
910
911 return (struct ffa_value){.func = FFA_SUCCESS_32};
912 }
913
Andrew Walbranca808b12020-05-15 17:22:28 +0100914 ret = constituents_get_mode(from, orig_from_mode, fragments,
915 fragment_constituent_counts,
916 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100917 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100918 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000919 }
920
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000921 /*
922 * Ensure the relinquishing VM is not the owner but has access to the
923 * memory.
924 */
925 orig_from_state = *orig_from_mode & state_mask;
926 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
927 dlog_verbose(
928 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100929 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000930 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000932 }
933
934 /* Find the appropriate new mode. */
935 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
936
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000938}
939
940/**
941 * Verify that all pages have the same mode, that the starting mode
942 * constitutes a valid state and obtain the next mode to apply
943 * to the retrieving VM.
944 *
945 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 * 1) FFA_DENIED if a state transition was not found;
947 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100948 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100950 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100951 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
952 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000953 */
J-Alvesfc19b372022-07-06 12:17:35 +0100954struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000955 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100956 struct ffa_memory_region_constituent **fragments,
957 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100958 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100959 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000960{
961 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000963
Andrew Walbranca808b12020-05-15 17:22:28 +0100964 ret = constituents_get_mode(to, &orig_to_mode, fragments,
965 fragment_constituent_counts,
966 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100968 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100969 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000970 }
971
J-Alves460d36c2023-10-12 17:02:15 +0100972 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100973 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100974
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100975 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000976 /*
977 * If the original ffa memory send call has been processed
978 * successfully, it is expected the orig_to_mode would overlay
979 * with `state_mask`, as a result of the function
980 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100981 *
982 * If Hafnium is the SPMC:
983 * - Caller of the reclaim interface is an SP, the memory shall
984 * have been protected throughout the flow.
985 * - Caller of the reclaim is from the NWd, the memory may have
986 * been protected at the time of lending/donating the memory.
987 * In such case, set action to unprotect memory in the
988 * handling of reclaim operation.
989 * - If Hafnium is the hypervisor memory shall never have been
990 * protected in memory lend/share/donate.
991 *
992 * More details in the doc comment of the function
993 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000994 */
J-Alves59ed0042022-07-28 18:26:41 +0100995 if (vm_id_is_current_world(to.vm->id)) {
996 assert((orig_to_mode &
997 (MM_MODE_INVALID | MM_MODE_UNOWNED |
998 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100999 assert(!memory_protected);
1000 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
1001 map_action != NULL && memory_protected) {
1002 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +01001003 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004 } else {
J-Alves69cdfd92024-04-26 11:40:59 +01001005 if (!vm_id_is_current_world(to.vm->id)) {
1006 assert(map_action != NULL);
1007 *map_action = MAP_ACTION_NONE;
1008 return (struct ffa_value){.func = FFA_SUCCESS_32};
1009 }
1010
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001012 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 * Ensure the retriever has the expected state. We don't care
1014 * about the MM_MODE_SHARED bit; either with or without it set
1015 * are both valid representations of the !O-NA state.
1016 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001017 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001018 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001019 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1020 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 }
J-Alves460d36c2023-10-12 17:02:15 +01001023
1024 /*
1025 * If memory has been protected before, clear the NS bit to
1026 * allow the secure access from the SP.
1027 */
1028 if (memory_protected) {
1029 *to_mode &= ~plat_ffa_other_world_mode();
1030 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 }
1032
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001033 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001034 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001035 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001036 *to_mode |= 0;
1037 break;
J-Alves95fbb312024-03-20 15:19:16 +00001038 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001039 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001040 *to_mode |= MM_MODE_UNOWNED;
1041 break;
J-Alves95fbb312024-03-20 15:19:16 +00001042 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001043 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001044 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1045 break;
1046
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001048 *to_mode |= 0;
1049 break;
1050
1051 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001052 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001053 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001054 }
1055
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001056 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001057}
Jose Marinho09b1db82019-08-08 09:16:59 +01001058
J-Alvescf6253e2024-01-03 13:48:48 +00001059/*
1060 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1061 * Returns:
1062 * - FFA_SUCCESS_32: if all goes well.
1063 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1064 * the page table update. Or error code provided by the function
1065 * `arch_memory_protect`.
1066 */
1067static struct ffa_value ffa_region_group_check_actions(
1068 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1069 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1070 bool *memory_protected)
1071{
1072 struct ffa_value ret;
1073 bool is_memory_protected;
1074
1075 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1076 dlog_verbose(
1077 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001078 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001079 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1080 pa_addr(pa_end));
1081 return ffa_error(FFA_NO_MEMORY);
1082 }
1083
1084 switch (action) {
1085 case MAP_ACTION_CHECK:
1086 /* No protect requested. */
1087 is_memory_protected = false;
1088 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1089 break;
1090 case MAP_ACTION_CHECK_PROTECT: {
1091 paddr_t last_protected_pa = pa_init(0);
1092
1093 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1094
1095 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1096
1097 /*
1098 * - If protect memory has failed with FFA_DENIED, means some
1099 * range of memory was in the wrong state. In such case, SPM
1100 * reverts the state of the pages that were successfully
1101 * updated.
1102 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1103 * means the platform doesn't support the protection mechanism.
1104 * That said, it still permits the page table update to go
1105 * through. The variable
1106 * `is_memory_protected` will be equal to false.
1107 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1108 * break from switch and return the error.
1109 */
1110 if (ret.func == FFA_ERROR_32) {
1111 assert(!is_memory_protected);
1112 if (ffa_error_code(ret) == FFA_DENIED &&
1113 pa_addr(last_protected_pa) != (uintptr_t)0) {
1114 CHECK(arch_memory_unprotect(
1115 pa_begin,
1116 pa_add(last_protected_pa, PAGE_SIZE)));
1117 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1118 ret = (struct ffa_value){
1119 .func = FFA_SUCCESS_32,
1120 };
1121 }
1122 }
1123 } break;
1124 default:
1125 panic("%s: invalid action to process %x\n", __func__, action);
1126 }
1127
1128 if (memory_protected != NULL) {
1129 *memory_protected = is_memory_protected;
1130 }
1131
1132 return ret;
1133}
1134
1135static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1136 paddr_t pa_begin, paddr_t pa_end,
1137 struct mpool *ppool, uint32_t mode,
1138 enum ffa_map_action action)
1139{
1140 switch (action) {
1141 case MAP_ACTION_COMMIT_UNPROTECT:
1142 /*
1143 * Checking that it should succeed because SPM should be
1144 * unprotecting memory that it had protected before.
1145 */
1146 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1147 case MAP_ACTION_COMMIT:
1148 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1149 NULL);
1150 break;
1151 default:
1152 panic("%s: invalid action to process %x\n", __func__, action);
1153 }
1154}
1155
Jose Marinho09b1db82019-08-08 09:16:59 +01001156/**
J-Alves063ad832023-10-03 18:05:40 +01001157 * Helper function to revert a failed "Protect" action from the SPMC:
1158 * - `fragment_count`: should specify the number of fragments to traverse from
1159 * `fragments`. This may not be the full amount of fragments that are part of
1160 * the share_state structure.
1161 * - `fragment_constituent_counts`: array holding the amount of constituents
1162 * per fragment.
1163 * - `end`: pointer to the constituent that failed the "protect" action. It
1164 * shall be part of the last fragment, and it shall make the loop below break.
1165 */
1166static void ffa_region_group_fragments_revert_protect(
1167 struct ffa_memory_region_constituent **fragments,
1168 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1169 const struct ffa_memory_region_constituent *end)
1170{
1171 for (uint32_t i = 0; i < fragment_count; ++i) {
1172 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1173 struct ffa_memory_region_constituent *constituent =
1174 &fragments[i][j];
1175 size_t size = constituent->page_count * PAGE_SIZE;
1176 paddr_t pa_begin =
1177 pa_from_ipa(ipa_init(constituent->address));
1178 paddr_t pa_end = pa_add(pa_begin, size);
1179
Karl Meakine8937d92024-03-19 16:04:25 +00001180 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001181 __func__, pa_addr(pa_begin), size);
1182
1183 if (constituent == end) {
1184 /*
1185 * The last constituent is expected to be in the
1186 * last fragment.
1187 */
1188 assert(i == fragment_count - 1);
1189 break;
1190 }
1191
1192 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1193 }
1194 }
1195}
1196
1197/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001198 * Updates a VM's page table such that the given set of physical address ranges
1199 * are mapped in the address space at the corresponding address ranges, in the
1200 * mode provided.
1201 *
J-Alves0a83dc22023-05-05 09:50:37 +01001202 * The enum ffa_map_action determines the action taken from a call to the
1203 * function below:
1204 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1205 * mpool but no mappings will actually be updated. This function must always
1206 * be called first with action set to MAP_ACTION_CHECK to check that it will
1207 * succeed before calling ffa_region_group_identity_map with whichever one of
1208 * the remaining actions, to avoid leaving the page table in a half-updated
1209 * state.
1210 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1211 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001212 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1213 * invocation to the monitor to update the security state of the memory,
1214 * to that of the SPMC.
1215 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1216 * with a call into the monitor, to reset the security state of memory
1217 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001218 * vm_ptable_defrag should always be called after a series of page table
1219 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001220 *
J-Alvescf6253e2024-01-03 13:48:48 +00001221 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1222 * error codes:
1223 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1224 * - FFA_DENIED:
1225 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001226 * made to memory mappings.
1227 */
J-Alvescf6253e2024-01-03 13:48:48 +00001228struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001229 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 struct ffa_memory_region_constituent **fragments,
1231 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001232 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1233 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001234{
Andrew Walbranca808b12020-05-15 17:22:28 +01001235 uint32_t i;
1236 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001237 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001238
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001239 if (vm_locked.vm->el0_partition) {
1240 mode |= MM_MODE_USER | MM_MODE_NG;
1241 }
1242
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 /* Iterate over the memory region constituents within each fragment. */
1244 for (i = 0; i < fragment_count; ++i) {
1245 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001246 struct ffa_memory_region_constituent *constituent =
1247 &fragments[i][j];
1248 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001249 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001250 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001251 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001252 uint32_t pa_bits =
1253 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001254
1255 /*
1256 * Ensure the requested region falls into system's PA
1257 * range.
1258 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001259 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1260 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001261 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001262 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001263 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001264
J-Alvescf6253e2024-01-03 13:48:48 +00001265 if (action <= MAP_ACTION_CHECK_PROTECT) {
1266 ret = ffa_region_group_check_actions(
1267 vm_locked, pa_begin, pa_end, ppool,
1268 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001269
1270 if (ret.func == FFA_ERROR_32 &&
1271 ffa_error_code(ret) == FFA_DENIED) {
1272 if (memory_protected != NULL) {
1273 assert(!*memory_protected);
1274 }
1275
1276 ffa_region_group_fragments_revert_protect(
1277 fragments,
1278 fragment_constituent_counts,
1279 i + 1, constituent);
1280 break;
1281 }
J-Alvescf6253e2024-01-03 13:48:48 +00001282 } else if (action >= MAP_ACTION_COMMIT &&
1283 action < MAP_ACTION_MAX) {
1284 ffa_region_group_commit_actions(
1285 vm_locked, pa_begin, pa_end, ppool,
1286 mode, action);
1287 ret = (struct ffa_value){
1288 .func = FFA_SUCCESS_32};
1289 } else {
1290 panic("%s: Unknown ffa_map_action.\n",
1291 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001292 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001293 }
1294 }
1295
J-Alvescf6253e2024-01-03 13:48:48 +00001296 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001297}
1298
1299/**
1300 * Clears a region of physical memory by overwriting it with zeros. The data is
1301 * flushed from the cache so the memory has been cleared across the system.
1302 */
J-Alves7db32002021-12-14 14:44:50 +00001303static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1304 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001305{
1306 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001307 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001308 * global mapping of the whole range. Such an approach will limit
1309 * the changes to stage-1 tables and will allow only local
1310 * invalidation.
1311 */
1312 bool ret;
1313 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001314 void *ptr = mm_identity_map(stage1_locked, begin, end,
1315 MM_MODE_W | (extra_mode_attributes &
1316 plat_ffa_other_world_mode()),
1317 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001318 size_t size = pa_difference(begin, end);
1319
1320 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001321 goto fail;
1322 }
1323
1324 memset_s(ptr, size, 0, size);
1325 arch_mm_flush_dcache(ptr, size);
1326 mm_unmap(stage1_locked, begin, end, ppool);
1327
1328 ret = true;
1329 goto out;
1330
1331fail:
1332 ret = false;
1333
1334out:
1335 mm_unlock_stage1(&stage1_locked);
1336
1337 return ret;
1338}
1339
1340/**
1341 * Clears a region of physical memory by overwriting it with zeros. The data is
1342 * flushed from the cache so the memory has been cleared across the system.
1343 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001344static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001345 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001346 struct ffa_memory_region_constituent **fragments,
1347 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1348 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001349{
1350 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001351 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001352 bool ret = false;
1353
1354 /*
1355 * Create a local pool so any freed memory can't be used by another
1356 * thread. This is to ensure each constituent that is mapped can be
1357 * unmapped again afterwards.
1358 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001359 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001360
Andrew Walbranca808b12020-05-15 17:22:28 +01001361 /* Iterate over the memory region constituents within each fragment. */
1362 for (i = 0; i < fragment_count; ++i) {
1363 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001364
J-Alves8457f932023-10-11 16:41:45 +01001365 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001366 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1367 paddr_t begin =
1368 pa_from_ipa(ipa_init(fragments[i][j].address));
1369 paddr_t end = pa_add(begin, size);
1370
J-Alves7db32002021-12-14 14:44:50 +00001371 if (!clear_memory(begin, end, &local_page_pool,
1372 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001373 /*
1374 * api_clear_memory will defrag on failure, so
1375 * no need to do it here.
1376 */
1377 goto out;
1378 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001379 }
1380 }
1381
Jose Marinho09b1db82019-08-08 09:16:59 +01001382 ret = true;
1383
1384out:
1385 mpool_fini(&local_page_pool);
1386 return ret;
1387}
1388
J-Alves5952d942022-12-22 16:03:00 +00001389static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1390 ipaddr_t in_begin, ipaddr_t in_end)
1391{
1392 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1393 ipa_addr(begin) < ipa_addr(in_end)) ||
1394 (ipa_addr(end) <= ipa_addr(in_end) &&
1395 ipa_addr(end) > ipa_addr(in_begin));
1396}
1397
1398/**
1399 * Receives a memory range and looks for overlaps with the remainder
1400 * constituents of the memory share/lend/donate operation. Assumes they are
1401 * passed in order to avoid having to loop over all the elements at each call.
1402 * The function only compares the received memory ranges with those that follow
1403 * within the same fragment, and subsequent fragments from the same operation.
1404 */
1405static bool ffa_memory_check_overlap(
1406 struct ffa_memory_region_constituent **fragments,
1407 const uint32_t *fragment_constituent_counts,
1408 const uint32_t fragment_count, const uint32_t current_fragment,
1409 const uint32_t current_constituent)
1410{
1411 uint32_t i = current_fragment;
1412 uint32_t j = current_constituent;
1413 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1414 const uint32_t current_page_count = fragments[i][j].page_count;
1415 size_t current_size = current_page_count * PAGE_SIZE;
1416 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1417
1418 if (current_size == 0 ||
1419 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001420 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1421 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001422 return false;
1423 }
1424
1425 for (; i < fragment_count; i++) {
1426 j = (i == current_fragment) ? j + 1 : 0;
1427
1428 for (; j < fragment_constituent_counts[i]; j++) {
1429 ipaddr_t begin = ipa_init(fragments[i][j].address);
1430 const uint32_t page_count = fragments[i][j].page_count;
1431 size_t size = page_count * PAGE_SIZE;
1432 ipaddr_t end = ipa_add(begin, size - 1);
1433
1434 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1435 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001436 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001437 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001438 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001439 return false;
1440 }
1441
1442 /*
1443 * Check if current ranges is within begin and end, as
1444 * well as the reverse. This should help optimize the
1445 * loop, and reduce the number of iterations.
1446 */
1447 if (is_memory_range_within(begin, end, current_begin,
1448 current_end) ||
1449 is_memory_range_within(current_begin, current_end,
1450 begin, end)) {
1451 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001452 "Overlapping memory ranges: %#lx - "
1453 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001454 ipa_addr(begin), ipa_addr(end),
1455 ipa_addr(current_begin),
1456 ipa_addr(current_end));
1457 return true;
1458 }
1459 }
1460 }
1461
1462 return false;
1463}
1464
Jose Marinho09b1db82019-08-08 09:16:59 +01001465/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001466 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001467 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001468 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001469 *
1470 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001471 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001473 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001474 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1475 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001477 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001478 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001479 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001480static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001481 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001482 struct ffa_memory_region_constituent **fragments,
1483 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001484 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001485 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1486 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001487{
Andrew Walbranca808b12020-05-15 17:22:28 +01001488 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001489 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001490 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001491 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001492 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001493 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001494 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001495 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001496 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001497 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001498
1499 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001500 * Make sure constituents are properly aligned to a 64-bit boundary. If
1501 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001502 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001503 for (i = 0; i < fragment_count; ++i) {
1504 if (!is_aligned(fragments[i], 8)) {
1505 dlog_verbose("Constituents not aligned.\n");
1506 return ffa_error(FFA_INVALID_PARAMETERS);
1507 }
J-Alves8f11cde2022-12-21 16:18:22 +00001508 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1509 constituents_total_page_count +=
1510 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001511 if (ffa_memory_check_overlap(
1512 fragments, fragment_constituent_counts,
1513 fragment_count, i, j)) {
1514 return ffa_error(FFA_INVALID_PARAMETERS);
1515 }
J-Alves8f11cde2022-12-21 16:18:22 +00001516 }
1517 }
1518
1519 if (constituents_total_page_count != composite_total_page_count) {
1520 dlog_verbose(
1521 "Composite page count differs from calculated page "
1522 "count from constituents.\n");
1523 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001524 }
1525
1526 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001527 * Check if the state transition is lawful for the sender, ensure that
1528 * all constituents of a memory region being shared are at the same
1529 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001530 */
J-Alves460d36c2023-10-12 17:02:15 +01001531 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001532 from_locked, share_func, memory_region, &orig_from_mode,
1533 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001534 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001535 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001536 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001537 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001538 }
1539
Andrew Walbran37c574e2020-06-03 11:45:46 +01001540 if (orig_from_mode_ret != NULL) {
1541 *orig_from_mode_ret = orig_from_mode;
1542 }
1543
Jose Marinho09b1db82019-08-08 09:16:59 +01001544 /*
1545 * Create a local pool so any freed memory can't be used by another
1546 * thread. This is to ensure the original mapping can be restored if the
1547 * clear fails.
1548 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001549 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001550
1551 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552 * First reserve all required memory for the new page table entries
1553 * without committing, to make sure the entire operation will succeed
1554 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001555 * Provide the map_action as populated by 'ffa_send_check_transition'.
1556 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001557 */
J-Alvescf6253e2024-01-03 13:48:48 +00001558 ret = ffa_region_group_identity_map(
1559 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001560 fragment_count, from_mode, page_pool, map_action,
1561 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001562 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001563 goto out;
1564 }
1565
1566 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001567 * Update the mapping for the sender. This won't allocate because the
1568 * transaction was already prepared above, but may free pages in the
1569 * case that a whole block is being unmapped that was previously
1570 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001571 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001572 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001573 from_locked, fragments, fragment_constituent_counts,
1574 fragment_count, from_mode, &local_page_pool,
1575 MAP_ACTION_COMMIT, NULL)
1576 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001577
J-Alves460d36c2023-10-12 17:02:15 +01001578 /*
1579 * If memory has been protected, it is now part of the secure PAS
1580 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1581 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1582 * SPM's S1 translation.
1583 * In case memory hasn't been protected, and it is in the non-secure
1584 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1585 * perform a non-secure memory access. In such case `clean_mode` takes
1586 * the same mode as `orig_from_mode`.
1587 */
1588 clean_mode = (memory_protected != NULL && *memory_protected)
1589 ? orig_from_mode & ~plat_ffa_other_world_mode()
1590 : orig_from_mode;
1591
Jose Marinho09b1db82019-08-08 09:16:59 +01001592 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001593 if (clear && !ffa_clear_memory_constituents(
1594 clean_mode, fragments, fragment_constituent_counts,
1595 fragment_count, page_pool)) {
1596 map_action = (memory_protected != NULL && *memory_protected)
1597 ? MAP_ACTION_COMMIT_UNPROTECT
1598 : MAP_ACTION_COMMIT;
1599
Jose Marinho09b1db82019-08-08 09:16:59 +01001600 /*
1601 * On failure, roll back by returning memory to the sender. This
1602 * may allocate pages which were previously freed into
1603 * `local_page_pool` by the call above, but will never allocate
1604 * more pages than that so can never fail.
1605 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001607 from_locked, fragments,
1608 fragment_constituent_counts, fragment_count,
1609 orig_from_mode, &local_page_pool,
1610 MAP_ACTION_COMMIT, NULL)
1611 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001612 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001613 goto out;
1614 }
1615
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001616 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001617
1618out:
1619 mpool_fini(&local_page_pool);
1620
1621 /*
1622 * Tidy up the page table by reclaiming failed mappings (if there was an
1623 * error) or merging entries into blocks where possible (on success).
1624 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001625 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001626
1627 return ret;
1628}
1629
1630/**
1631 * Validates and maps memory shared from one VM to another.
1632 *
1633 * This function requires the calling context to hold the <to> lock.
1634 *
1635 * Returns:
1636 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001638 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001639 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001641 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001642 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001643struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001644 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 struct ffa_memory_region_constituent **fragments,
1646 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001647 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001648 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001649{
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001651 uint32_t to_mode;
1652 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001653 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001654 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655
1656 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001657 * Make sure constituents are properly aligned to a 64-bit boundary. If
1658 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001659 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001660 for (i = 0; i < fragment_count; ++i) {
1661 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001662 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001663 return ffa_error(FFA_INVALID_PARAMETERS);
1664 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001665 }
1666
1667 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001668 * Ensure the sender has write permissions if the memory needs to be
1669 * cleared.
1670 */
1671 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1672 dlog_verbose(
1673 "Cannot zero memory when the sender does not have "
1674 "write access\n");
1675 return ffa_error(FFA_DENIED);
1676 }
1677
1678 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001679 * Check if the state transition is lawful for the recipient, and ensure
1680 * that all constituents of the memory region being retrieved are at the
1681 * same state.
1682 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001683 ret = ffa_retrieve_check_transition(
1684 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001685 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1686 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001687
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001688 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001689 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001690 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001691 }
1692
1693 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001694 * Create a local pool so any freed memory can't be used by
1695 * another thread. This is to ensure the original mapping can be
1696 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001697 */
1698 mpool_init_with_fallback(&local_page_pool, page_pool);
1699
1700 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001701 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1702 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001703 */
J-Alves69cdfd92024-04-26 11:40:59 +01001704 if (map_action != MAP_ACTION_NONE) {
1705 /*
1706 * First reserve all required memory for the new page table
1707 * entries in the recipient page tables without committing, to
1708 * make sure the entire operation will succeed without
1709 * exhausting the page pool.
1710 */
1711 ret = ffa_region_group_identity_map(
1712 to_locked, fragments, fragment_constituent_counts,
1713 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1714 NULL);
1715 if (ret.func == FFA_ERROR_32) {
1716 /* TODO: partial defrag of failed range. */
1717 goto out;
1718 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001719 }
1720
1721 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001722 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001723 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1724 fragment_constituent_counts,
1725 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001726 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001727 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001728 goto out;
1729 }
1730
J-Alves69cdfd92024-04-26 11:40:59 +01001731 if (map_action != MAP_ACTION_NONE) {
1732 /*
1733 * Complete the transfer by mapping the memory into the
1734 * recipient. This won't allocate because the transaction was
1735 * already prepared above, so it doesn't need to use the
1736 * `local_page_pool`.
1737 */
1738 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1739 fragment_constituent_counts,
1740 fragment_count, to_mode,
1741 page_pool, map_action, NULL)
1742 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001743
J-Alves69cdfd92024-04-26 11:40:59 +01001744 /*
1745 * Return the mode used in mapping the memory in retriever's PT.
1746 */
1747 if (response_mode != NULL) {
1748 *response_mode = to_mode;
1749 }
J-Alves460d36c2023-10-12 17:02:15 +01001750 }
1751
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001752 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001753
1754out:
1755 mpool_fini(&local_page_pool);
1756
1757 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001758 * Tidy up the page table by reclaiming failed mappings (if there was an
1759 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001760 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001761 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001762
1763 return ret;
1764}
1765
Andrew Walbran996d1d12020-05-27 14:08:43 +01001766static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001767 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001768 struct ffa_memory_region_constituent **fragments,
1769 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001770 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001771{
1772 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001773 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001774 uint32_t from_mode;
1775 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001776 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001777 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001778
Andrew Walbranca808b12020-05-15 17:22:28 +01001779 ret = ffa_relinquish_check_transition(
1780 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001781 fragment_constituent_counts, fragment_count, &from_mode,
1782 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001783 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001784 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001785 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001786 }
1787
1788 /*
1789 * Create a local pool so any freed memory can't be used by another
1790 * thread. This is to ensure the original mapping can be restored if the
1791 * clear fails.
1792 */
1793 mpool_init_with_fallback(&local_page_pool, page_pool);
1794
J-Alves69cdfd92024-04-26 11:40:59 +01001795 if (map_action != MAP_ACTION_NONE) {
1796 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001797
J-Alves69cdfd92024-04-26 11:40:59 +01001798 /*
1799 * First reserve all required memory for the new page table
1800 * entries without committing, to make sure the entire operation
1801 * will succeed without exhausting the page pool.
1802 */
1803 ret = ffa_region_group_identity_map(
1804 from_locked, fragments, fragment_constituent_counts,
1805 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1806 NULL);
1807 if (ret.func == FFA_ERROR_32) {
1808 goto out;
1809 }
1810
1811 /*
1812 * Update the mapping for the sender. This won't allocate
1813 * because the transaction was already prepared above, but may
1814 * free pages in the case that a whole block is being unmapped
1815 * that was previously partially mapped.
1816 */
1817 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1818 fragment_constituent_counts,
1819 fragment_count, from_mode,
1820 &local_page_pool,
1821 MAP_ACTION_COMMIT, NULL)
1822 .func == FFA_SUCCESS_32);
1823 } else {
1824 /*
1825 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1826 * were not updated on retrieve/relinquish. These were updating
1827 * only the `share_state` structures. As such, use the sender's
1828 * original mode.
1829 */
1830 clearing_mode = sender_orig_mode;
1831 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001832
1833 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001834 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001835 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001836 fragment_constituent_counts,
1837 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001838 if (map_action != MAP_ACTION_NONE) {
1839 /*
1840 * On failure, roll back by returning memory to the
1841 * sender. This may allocate pages which were previously
1842 * freed into `local_page_pool` by the call above, but
1843 * will never allocate more pages than that so can never
1844 * fail.
1845 */
1846 CHECK(ffa_region_group_identity_map(
1847 from_locked, fragments,
1848 fragment_constituent_counts,
1849 fragment_count, orig_from_mode,
1850 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1851 .func == FFA_SUCCESS_32);
1852 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001853 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001854 goto out;
1855 }
1856
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001857 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001858
1859out:
1860 mpool_fini(&local_page_pool);
1861
1862 /*
1863 * Tidy up the page table by reclaiming failed mappings (if there was an
1864 * error) or merging entries into blocks where possible (on success).
1865 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001866 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001867
1868 return ret;
1869}
1870
1871/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001872 * Complete a memory sending operation by checking that it is valid, updating
1873 * the sender page table, and then either marking the share state as having
1874 * completed sending (on success) or freeing it (on failure).
1875 *
1876 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1877 */
J-Alvesfdd29272022-07-19 13:16:31 +01001878struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001879 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001880 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1881 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001882{
1883 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001884 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001885 struct ffa_value ret;
1886
1887 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001888 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001889 assert(memory_region != NULL);
1890 composite = ffa_memory_region_get_composite(memory_region, 0);
1891 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001892
1893 /* Check that state is valid in sender page table and update. */
1894 ret = ffa_send_check_update(
1895 from_locked, share_state->fragments,
1896 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001897 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001898 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001899 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001900 if (ret.func != FFA_SUCCESS_32) {
1901 /*
1902 * Free share state, it failed to send so it can't be retrieved.
1903 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001904 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1905 __func__, ffa_func_name(ret.func),
1906 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001907 share_state_free(share_states, share_state, page_pool);
1908 return ret;
1909 }
1910
1911 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001912 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001913
J-Alvesee68c542020-10-29 17:48:20 +00001914 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001915}
1916
1917/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001918 * Check that the memory attributes match Hafnium expectations.
1919 * Cacheability:
1920 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1921 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1922 *
1923 * Shareability:
1924 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001925 */
1926static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001927 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001928{
1929 enum ffa_memory_type memory_type;
1930 enum ffa_memory_cacheability cacheability;
1931 enum ffa_memory_shareability shareability;
1932
Karl Meakin84710f32023-10-12 15:14:49 +01001933 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001934 cacheability = attributes.cacheability;
1935 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1936 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1937 dlog_verbose(
1938 "Normal Memory: Invalid cacheability %s, "
1939 "expected %s.\n",
1940 ffa_memory_cacheability_name(cacheability),
1941 ffa_memory_cacheability_name(
1942 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001943 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001944 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001945 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1946 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1947 dlog_verbose(
1948 "Device Memory: Invalid cacheability %s, "
1949 "expected %s.\n",
1950 ffa_device_memory_cacheability_name(cacheability),
1951 ffa_device_memory_cacheability_name(
1952 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001953 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001954 }
1955
Karl Meakin84710f32023-10-12 15:14:49 +01001956 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001957 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001958 dlog_verbose("Invalid shareability %s, expected %s.\n",
1959 ffa_memory_shareability_name(shareability),
1960 ffa_memory_shareability_name(
1961 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001962 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001963 }
1964
1965 return (struct ffa_value){.func = FFA_SUCCESS_32};
1966}
1967
1968/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001969 * Check that the given `memory_region` represents a valid memory send request
1970 * of the given `share_func` type, return the clear flag and permissions via the
1971 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001972 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001973 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001974 * not.
1975 */
J-Alves66652252022-07-06 09:49:51 +01001976struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001977 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1978 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001979 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001980{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001981 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001982 struct ffa_memory_access *receiver =
1983 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001984 uint64_t receivers_end;
1985 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001986 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001987 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001988 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989 enum ffa_data_access data_access;
1990 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001991 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001992 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001993 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001994 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001995 memory_region->receivers_offset +
1996 memory_region->memory_access_desc_size +
1997 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001998
1999 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002000 dlog_verbose("Fragment length %u too short (min %zu).\n",
2001 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002002 return ffa_error(FFA_INVALID_PARAMETERS);
2003 }
2004
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002005 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
2006 "struct ffa_memory_region_constituent must be 16 bytes");
2007 if (!is_aligned(fragment_length,
2008 sizeof(struct ffa_memory_region_constituent)) ||
2009 !is_aligned(memory_share_length,
2010 sizeof(struct ffa_memory_region_constituent))) {
2011 dlog_verbose(
2012 "Fragment length %u or total length %u"
2013 " is not 16-byte aligned.\n",
2014 fragment_length, memory_share_length);
2015 return ffa_error(FFA_INVALID_PARAMETERS);
2016 }
2017
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002018 if (fragment_length > memory_share_length) {
2019 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002020 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002021 (size_t)fragment_length, (size_t)memory_share_length);
2022 return ffa_error(FFA_INVALID_PARAMETERS);
2023 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002024
J-Alves95df0ef2022-12-07 10:09:48 +00002025 /* The sender must match the caller. */
2026 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2027 vm_id_is_current_world(memory_region->sender)) ||
2028 (vm_id_is_current_world(from_locked.vm->id) &&
2029 memory_region->sender != from_locked.vm->id)) {
2030 dlog_verbose("Invalid memory sender ID.\n");
2031 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002032 }
2033
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002034 if (memory_region->receiver_count <= 0) {
2035 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002036 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002037 }
2038
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002039 /*
2040 * Ensure that the composite header is within the memory bounds and
2041 * doesn't overlap the first part of the message. Cast to uint64_t
2042 * to prevent overflow.
2043 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002044 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002045 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002046 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002047 min_length = receivers_end +
2048 sizeof(struct ffa_composite_memory_region) +
2049 sizeof(struct ffa_memory_region_constituent);
2050 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002051 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002052 (size_t)memory_share_length, (size_t)min_length);
2053 return ffa_error(FFA_INVALID_PARAMETERS);
2054 }
2055
2056 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002057 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002058
2059 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002060 * Check that the composite memory region descriptor is after the access
2061 * descriptors, is at least 16-byte aligned, and fits in the first
2062 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002063 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002064 if ((composite_memory_region_offset < receivers_end) ||
2065 (composite_memory_region_offset % 16 != 0) ||
2066 (composite_memory_region_offset >
2067 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2068 dlog_verbose(
2069 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002070 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002071 (size_t)composite_memory_region_offset);
2072 return ffa_error(FFA_INVALID_PARAMETERS);
2073 }
2074
2075 /*
2076 * Compute the start of the constituent regions. Already checked
2077 * to be not more than fragment_length and thus not more than
2078 * memory_share_length.
2079 */
2080 constituents_start = composite_memory_region_offset +
2081 sizeof(struct ffa_composite_memory_region);
2082 constituents_length = memory_share_length - constituents_start;
2083
2084 /*
2085 * Check that the number of constituents is consistent with the length
2086 * of the constituent region.
2087 */
2088 composite = ffa_memory_region_get_composite(memory_region, 0);
2089 if ((constituents_length %
2090 sizeof(struct ffa_memory_region_constituent) !=
2091 0) ||
2092 ((constituents_length /
2093 sizeof(struct ffa_memory_region_constituent)) !=
2094 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002095 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002096 (size_t)memory_share_length,
2097 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002098 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002099 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002100 if (fragment_length < memory_share_length &&
2101 fragment_length < HF_MAILBOX_SIZE) {
2102 dlog_warning(
2103 "Initial fragment length %d smaller than mailbox "
2104 "size.\n",
2105 fragment_length);
2106 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002107
Andrew Walbrana65a1322020-04-06 19:32:32 +01002108 /*
2109 * Clear is not allowed for memory sharing, as the sender still has
2110 * access to the memory.
2111 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002112 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002113 (share_func == FFA_MEM_SHARE_32 ||
2114 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002115 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002116 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002117 }
2118
2119 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002121 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002122 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002123 }
2124
J-Alves363f5722022-04-25 17:37:37 +01002125 /* Check that the permissions are valid, for each specified receiver. */
2126 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002127 struct ffa_memory_region_attributes receiver_permissions;
2128
2129 receiver = ffa_memory_region_get_receiver(memory_region, i);
2130 assert(receiver != NULL);
2131 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002132 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002133 receiver_permissions.permissions;
2134 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002135
2136 if (memory_region->sender == receiver_id) {
2137 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002138 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002139 }
Federico Recanati85090c42021-12-15 13:17:54 +01002140
J-Alves363f5722022-04-25 17:37:37 +01002141 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2142 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002143 struct ffa_memory_access *other_receiver =
2144 ffa_memory_region_get_receiver(memory_region,
2145 j);
2146 assert(other_receiver != NULL);
2147
J-Alves363f5722022-04-25 17:37:37 +01002148 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002149 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002150 dlog_verbose(
2151 "Repeated receiver(%x) in memory send "
2152 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002153 other_receiver->receiver_permissions
2154 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002155 return ffa_error(FFA_INVALID_PARAMETERS);
2156 }
2157 }
2158
2159 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002160 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002161 dlog_verbose(
2162 "All ffa_memory_access should point to the "
2163 "same composite memory region offset.\n");
2164 return ffa_error(FFA_INVALID_PARAMETERS);
2165 }
2166
Karl Meakin84710f32023-10-12 15:14:49 +01002167 data_access = permissions.data_access;
2168 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002169 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2170 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2171 dlog_verbose(
2172 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002173 "(data_access = %s, instruction_access = %s)\n",
2174 ffa_data_access_name(data_access),
2175 ffa_instruction_access_name(
2176 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002177 return ffa_error(FFA_INVALID_PARAMETERS);
2178 }
2179 if (instruction_access !=
2180 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2181 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002182 "Invalid instruction access permissions %s "
2183 "for sending memory, expected %s.\n",
2184 ffa_instruction_access_name(instruction_access),
2185 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002186 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002187 return ffa_error(FFA_INVALID_PARAMETERS);
2188 }
J-Alves95fbb312024-03-20 15:19:16 +00002189 if (share_func == FFA_MEM_SHARE_32 ||
2190 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002191 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2192 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002193 "Invalid data access permissions %s "
2194 "for sharing memory, expected %s.\n",
2195 ffa_data_access_name(data_access),
2196 ffa_data_access_name(
2197 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002198 return ffa_error(FFA_INVALID_PARAMETERS);
2199 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002200 /*
2201 * According to section 10.10.3 of the FF-A v1.1 EAC0
2202 * spec, NX is required for share operations (but must
2203 * not be specified by the sender) so set it in the
2204 * copy that we store, ready to be returned to the
2205 * retriever.
2206 */
2207 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002208 permissions.instruction_access =
2209 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002210 receiver_permissions.permissions = permissions;
2211 }
J-Alves363f5722022-04-25 17:37:37 +01002212 }
J-Alves95fbb312024-03-20 15:19:16 +00002213 if ((share_func == FFA_MEM_LEND_32 ||
2214 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002215 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2216 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002217 "Invalid data access permissions %s for "
2218 "lending memory, expected %s.\n",
2219 ffa_data_access_name(data_access),
2220 ffa_data_access_name(
2221 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002222 return ffa_error(FFA_INVALID_PARAMETERS);
2223 }
2224
J-Alves95fbb312024-03-20 15:19:16 +00002225 if ((share_func == FFA_MEM_DONATE_32 ||
2226 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002227 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2228 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002229 "Invalid data access permissions %s for "
2230 "donating memory, expected %s.\n",
2231 ffa_data_access_name(data_access),
2232 ffa_data_access_name(
2233 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002234 return ffa_error(FFA_INVALID_PARAMETERS);
2235 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 }
2237
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002238 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002239 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002240 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2241 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002242 "Invalid security state %s for memory share operation, "
2243 "expected %s.\n",
2244 ffa_memory_security_name(security_state),
2245 ffa_memory_security_name(
2246 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002247 return ffa_error(FFA_INVALID_PARAMETERS);
2248 }
2249
Federico Recanatid937f5e2021-12-20 17:38:23 +01002250 /*
J-Alves807794e2022-06-16 13:42:47 +01002251 * If a memory donate or lend with single borrower, the memory type
2252 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002253 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002254 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002255 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002256 share_func == FFA_MEM_DONATE_64 ||
2257 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002258 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002259 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002260 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002261 "Invalid memory type %s for memory share "
2262 "operation, expected %s.\n",
2263 ffa_memory_type_name(type),
2264 ffa_memory_type_name(
2265 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002266 return ffa_error(FFA_INVALID_PARAMETERS);
2267 }
2268 } else {
2269 /*
2270 * Check that sender's memory attributes match Hafnium
2271 * expectations: Normal Memory, Inner shareable, Write-Back
2272 * Read-Allocate Write-Allocate Cacheable.
2273 */
2274 ret = ffa_memory_attributes_validate(memory_region->attributes);
2275 if (ret.func != FFA_SUCCESS_32) {
2276 return ret;
2277 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002278 }
2279
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002280 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002281}
2282
2283/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002284 * Gets the share state for continuing an operation to donate, lend or share
2285 * memory, and checks that it is a valid request.
2286 *
2287 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2288 * not.
2289 */
J-Alvesfdd29272022-07-19 13:16:31 +01002290struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002291 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002292 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002293 struct mpool *page_pool)
2294{
2295 struct ffa_memory_share_state *share_state;
2296 struct ffa_memory_region *memory_region;
2297
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002298 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002299
2300 /*
2301 * Look up the share state by handle and make sure that the VM ID
2302 * matches.
2303 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002304 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002305 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002306 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002307 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002308 handle);
2309 return ffa_error(FFA_INVALID_PARAMETERS);
2310 }
2311 memory_region = share_state->memory_region;
2312
J-Alvesfdd29272022-07-19 13:16:31 +01002313 if (vm_id_is_current_world(from_vm_id) &&
2314 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002315 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2316 return ffa_error(FFA_INVALID_PARAMETERS);
2317 }
2318
2319 if (share_state->sending_complete) {
2320 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002321 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002322 handle);
2323 return ffa_error(FFA_INVALID_PARAMETERS);
2324 }
2325
2326 if (share_state->fragment_count == MAX_FRAGMENTS) {
2327 /*
2328 * Log a warning as this is a sign that MAX_FRAGMENTS should
2329 * probably be increased.
2330 */
2331 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002332 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002333 "only %d supported.\n",
2334 handle, MAX_FRAGMENTS);
2335 /* Free share state, as it's not possible to complete it. */
2336 share_state_free(share_states, share_state, page_pool);
2337 return ffa_error(FFA_NO_MEMORY);
2338 }
2339
2340 *share_state_ret = share_state;
2341
2342 return (struct ffa_value){.func = FFA_SUCCESS_32};
2343}
2344
2345/**
J-Alves95df0ef2022-12-07 10:09:48 +00002346 * Checks if there is at least one receiver from the other world.
2347 */
J-Alvesfdd29272022-07-19 13:16:31 +01002348bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002349 struct ffa_memory_region *memory_region)
2350{
2351 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002352 struct ffa_memory_access *receiver =
2353 ffa_memory_region_get_receiver(memory_region, i);
2354 assert(receiver != NULL);
2355 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2356
2357 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002358 return true;
2359 }
2360 }
2361 return false;
2362}
2363
2364/**
J-Alves9da280b2022-12-21 14:55:39 +00002365 * Validates a call to donate, lend or share memory in which Hafnium is the
2366 * designated allocator of the memory handle. In practice, this also means
2367 * Hafnium is responsible for managing the state structures for the transaction.
2368 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2369 * sender is an SP or there is at least one borrower that is an SP.
2370 * If Hafnium is the hypervisor, it should allocate the memory handle when
2371 * operation involves only NWd VMs.
2372 *
2373 * If validation goes well, Hafnium updates the stage-2 page tables of the
2374 * sender. Validation consists of checking if the message length and number of
2375 * memory region constituents match, and if the transition is valid for the
2376 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002377 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002378 * Assumes that the caller has already found and locked the sender VM and copied
2379 * the memory region descriptor from the sender's TX buffer to a freshly
2380 * allocated page from Hafnium's internal pool. The caller must have also
2381 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002382 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002383 * This function takes ownership of the `memory_region` passed in and will free
2384 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002385 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002386struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002387 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002388 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002389 uint32_t fragment_length, uint32_t share_func,
2390 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002391{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002392 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002393 struct share_states_locked share_states;
2394 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002395
2396 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002397 * If there is an error validating the `memory_region` then we need to
2398 * free it because we own it but we won't be storing it in a share state
2399 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002400 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002401 ret = ffa_memory_send_validate(from_locked, memory_region,
2402 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002403 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002404 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002405 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002406 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002407 }
2408
Andrew Walbrana65a1322020-04-06 19:32:32 +01002409 /* Set flag for share function, ready to be retrieved later. */
2410 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002411 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002412 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002413 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002414 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002415 break;
J-Alves95fbb312024-03-20 15:19:16 +00002416 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002417 case FFA_MEM_LEND_32:
2418 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002419 break;
J-Alves95fbb312024-03-20 15:19:16 +00002420 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002421 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002422 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002423 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002424 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002425 default:
2426 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2427 ffa_func_name(share_func));
2428 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002429 }
2430
Andrew Walbranca808b12020-05-15 17:22:28 +01002431 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002432 /*
2433 * Allocate a share state before updating the page table. Otherwise if
2434 * updating the page table succeeded but allocating the share state
2435 * failed then it would leave the memory in a state where nobody could
2436 * get it back.
2437 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002438 share_state = allocate_share_state(share_states, share_func,
2439 memory_region, fragment_length,
2440 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002441 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002442 dlog_verbose("Failed to allocate share state.\n");
2443 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002444 ret = ffa_error(FFA_NO_MEMORY);
2445 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002446 }
2447
Andrew Walbranca808b12020-05-15 17:22:28 +01002448 if (fragment_length == memory_share_length) {
2449 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002450 ret = ffa_memory_send_complete(
2451 from_locked, share_states, share_state, page_pool,
2452 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002453 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002454 /*
2455 * Use sender ID from 'memory_region' assuming
2456 * that at this point it has been validated:
2457 * - MBZ at virtual FF-A instance.
2458 */
J-Alves19e20cf2023-08-02 12:48:55 +01002459 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002460 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2461 ? memory_region->sender
2462 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002463 ret = (struct ffa_value){
2464 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002465 .arg1 = (uint32_t)memory_region->handle,
2466 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002467 .arg3 = fragment_length,
2468 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002469 }
2470
2471out:
2472 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002473 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002474 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002475}
2476
2477/**
J-Alves8505a8a2022-06-15 18:10:18 +01002478 * Continues an operation to donate, lend or share memory to a VM from current
2479 * world. If this is the last fragment then checks that the transition is valid
2480 * for the type of memory sending operation and updates the stage-2 page tables
2481 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002482 *
2483 * Assumes that the caller has already found and locked the sender VM and copied
2484 * the memory region descriptor from the sender's TX buffer to a freshly
2485 * allocated page from Hafnium's internal pool.
2486 *
2487 * This function takes ownership of the `fragment` passed in; it must not be
2488 * freed by the caller.
2489 */
2490struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2491 void *fragment,
2492 uint32_t fragment_length,
2493 ffa_memory_handle_t handle,
2494 struct mpool *page_pool)
2495{
2496 struct share_states_locked share_states = share_states_lock();
2497 struct ffa_memory_share_state *share_state;
2498 struct ffa_value ret;
2499 struct ffa_memory_region *memory_region;
2500
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002501 CHECK(is_aligned(fragment,
2502 alignof(struct ffa_memory_region_constituent)));
2503 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2504 0) {
2505 dlog_verbose("Fragment length %u misaligned.\n",
2506 fragment_length);
2507 ret = ffa_error(FFA_INVALID_PARAMETERS);
2508 goto out_free_fragment;
2509 }
2510
Andrew Walbranca808b12020-05-15 17:22:28 +01002511 ret = ffa_memory_send_continue_validate(share_states, handle,
2512 &share_state,
2513 from_locked.vm->id, page_pool);
2514 if (ret.func != FFA_SUCCESS_32) {
2515 goto out_free_fragment;
2516 }
2517 memory_region = share_state->memory_region;
2518
J-Alves95df0ef2022-12-07 10:09:48 +00002519 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002520 dlog_error(
2521 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002522 "other world. This should never happen, and indicates "
2523 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002524 "EL3 code.\n");
2525 ret = ffa_error(FFA_INVALID_PARAMETERS);
2526 goto out_free_fragment;
2527 }
2528
2529 /* Add this fragment. */
2530 share_state->fragments[share_state->fragment_count] = fragment;
2531 share_state->fragment_constituent_counts[share_state->fragment_count] =
2532 fragment_length / sizeof(struct ffa_memory_region_constituent);
2533 share_state->fragment_count++;
2534
2535 /* Check whether the memory send operation is now ready to complete. */
2536 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002537 ret = ffa_memory_send_complete(
2538 from_locked, share_states, share_state, page_pool,
2539 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002540 } else {
2541 ret = (struct ffa_value){
2542 .func = FFA_MEM_FRAG_RX_32,
2543 .arg1 = (uint32_t)handle,
2544 .arg2 = (uint32_t)(handle >> 32),
2545 .arg3 = share_state_next_fragment_offset(share_states,
2546 share_state)};
2547 }
2548 goto out;
2549
2550out_free_fragment:
2551 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002552
2553out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002554 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002555 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556}
2557
Andrew Walbranca808b12020-05-15 17:22:28 +01002558/** Clean up after the receiver has finished retrieving a memory region. */
2559static void ffa_memory_retrieve_complete(
2560 struct share_states_locked share_states,
2561 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2562{
J-Alves95fbb312024-03-20 15:19:16 +00002563 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2564 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002565 /*
2566 * Memory that has been donated can't be relinquished,
2567 * so no need to keep the share state around.
2568 */
2569 share_state_free(share_states, share_state, page_pool);
2570 dlog_verbose("Freed share state for donate.\n");
2571 }
2572}
2573
J-Alves2d8457f2022-10-05 11:06:41 +01002574/**
2575 * Initialises the given memory region descriptor to be used for an
2576 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2577 * fragment.
2578 * The memory region descriptor is initialized according to retriever's
2579 * FF-A version.
2580 *
2581 * Returns true on success, or false if the given constituents won't all fit in
2582 * the first fragment.
2583 */
2584static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002585 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002586 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002587 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002588 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002589 struct ffa_memory_access *receivers, size_t receiver_count,
2590 uint32_t memory_access_desc_size, uint32_t page_count,
2591 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002592 const struct ffa_memory_region_constituent constituents[],
2593 uint32_t fragment_constituent_count, uint32_t *total_length,
2594 uint32_t *fragment_length)
2595{
2596 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002597 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002598 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002599 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002600
2601 assert(response != NULL);
2602
Karl Meakin0e617d92024-04-05 12:55:22 +01002603 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002604 struct ffa_memory_region_v1_0 *retrieve_response =
2605 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002606 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002607
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002608 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2609 attributes, flags, handle, 0,
2610 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002611
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002612 receiver = (struct ffa_memory_access_v1_0 *)
2613 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002614 receiver_count = retrieve_response->receiver_count;
2615
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002616 for (uint32_t i = 0; i < receiver_count; i++) {
2617 ffa_id_t receiver_id =
2618 receivers[i].receiver_permissions.receiver;
2619 ffa_memory_receiver_flags_t recv_flags =
2620 receivers[i].receiver_permissions.flags;
2621
2622 /*
2623 * Initialized here as in memory retrieve responses we
2624 * currently expect one borrower to be specified.
2625 */
2626 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002627 receiver, receiver_id, permissions.data_access,
2628 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002629 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002630
2631 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002632 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002633 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2634 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002635
2636 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2637 retrieve_response, 0);
2638 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002639 struct ffa_memory_region *retrieve_response =
2640 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002641 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002642
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002643 ffa_memory_region_init_header(
2644 retrieve_response, sender, attributes, flags, handle, 0,
2645 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002646
2647 /*
2648 * Note that `sizeof(struct_ffa_memory_region)` and
2649 * `sizeof(struct ffa_memory_access)` must both be multiples of
2650 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2651 * guaranteed that the offset we calculate here is aligned to a
2652 * 64-bit boundary and so 64-bit values can be copied without
2653 * alignment faults.
2654 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002655 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002656 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002657 (uint32_t)(receiver_count *
2658 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002659
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002660 retrieve_response_receivers =
2661 ffa_memory_region_get_receiver(retrieve_response, 0);
2662 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002663
2664 /*
2665 * Initialized here as in memory retrieve responses we currently
2666 * expect one borrower to be specified.
2667 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002668 memcpy_s(retrieve_response_receivers,
2669 sizeof(struct ffa_memory_access) * receiver_count,
2670 receivers,
2671 sizeof(struct ffa_memory_access) * receiver_count);
2672
2673 retrieve_response_receivers->composite_memory_region_offset =
2674 composite_offset;
2675
J-Alves2d8457f2022-10-05 11:06:41 +01002676 composite_memory_region =
2677 ffa_memory_region_get_composite(retrieve_response, 0);
2678 }
2679
J-Alves2d8457f2022-10-05 11:06:41 +01002680 assert(composite_memory_region != NULL);
2681
J-Alves2d8457f2022-10-05 11:06:41 +01002682 composite_memory_region->page_count = page_count;
2683 composite_memory_region->constituent_count = total_constituent_count;
2684 composite_memory_region->reserved_0 = 0;
2685
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002686 constituents_offset =
2687 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002688 if (constituents_offset +
2689 fragment_constituent_count *
2690 sizeof(struct ffa_memory_region_constituent) >
2691 response_max_size) {
2692 return false;
2693 }
2694
2695 for (i = 0; i < fragment_constituent_count; ++i) {
2696 composite_memory_region->constituents[i] = constituents[i];
2697 }
2698
2699 if (total_length != NULL) {
2700 *total_length =
2701 constituents_offset +
2702 composite_memory_region->constituent_count *
2703 sizeof(struct ffa_memory_region_constituent);
2704 }
2705 if (fragment_length != NULL) {
2706 *fragment_length =
2707 constituents_offset +
2708 fragment_constituent_count *
2709 sizeof(struct ffa_memory_region_constituent);
2710 }
2711
2712 return true;
2713}
2714
J-Alves96de29f2022-04-26 16:05:24 +01002715/**
2716 * Validates the retrieved permissions against those specified by the lender
2717 * of memory share operation. Optionally can help set the permissions to be used
2718 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002719 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2720 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2721 * specification for each ABI.
2722 * - FFA_DENIED -> if the permissions specified by the retriever are not
2723 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002724 */
J-Alvesdcad8992023-09-15 14:10:35 +01002725static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2726 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002727 enum ffa_data_access requested_data_access,
2728 enum ffa_instruction_access sent_instruction_access,
2729 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002730 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002731{
2732 switch (sent_data_access) {
2733 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2734 case FFA_DATA_ACCESS_RW:
2735 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2736 requested_data_access == FFA_DATA_ACCESS_RW) {
2737 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002738 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002739 }
2740 break;
2741 }
2742 /* Intentional fall-through. */
2743 case FFA_DATA_ACCESS_RO:
2744 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2745 requested_data_access == FFA_DATA_ACCESS_RO) {
2746 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002747 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002748 }
2749 break;
2750 }
2751 dlog_verbose(
2752 "Invalid data access requested; sender specified "
2753 "permissions %#x but receiver requested %#x.\n",
2754 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002755 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002756 case FFA_DATA_ACCESS_RESERVED:
2757 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2758 "checked before this point.");
2759 }
2760
J-Alvesdcad8992023-09-15 14:10:35 +01002761 /*
2762 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2763 * or FFA_MEMORY_DONATE the retriever should have specifed the
2764 * instruction permissions it wishes to receive.
2765 */
2766 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002767 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002768 case FFA_MEM_SHARE_32:
2769 if (requested_instruction_access !=
2770 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2771 dlog_verbose(
2772 "%s: for share instruction permissions must "
2773 "NOT be specified.\n",
2774 __func__);
2775 return ffa_error(FFA_INVALID_PARAMETERS);
2776 }
2777 break;
J-Alves95fbb312024-03-20 15:19:16 +00002778 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002779 case FFA_MEM_LEND_32:
2780 /*
2781 * For operations with multiple borrowers only permit XN
2782 * permissions, and both Sender and borrower should have used
2783 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2784 */
2785 if (multiple_borrowers) {
2786 if (requested_instruction_access !=
2787 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2788 dlog_verbose(
2789 "%s: lend/share/donate with multiple "
2790 "borrowers "
2791 "instruction permissions must NOT be "
2792 "specified.\n",
2793 __func__);
2794 return ffa_error(FFA_INVALID_PARAMETERS);
2795 }
2796 break;
2797 }
2798 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002799 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002800 case FFA_MEM_DONATE_32:
2801 if (!multiple_borrowers &&
2802 requested_instruction_access ==
2803 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2804 dlog_verbose(
2805 "%s: for lend/donate with single borrower "
2806 "instruction permissions must be speficified "
2807 "by borrower\n",
2808 __func__);
2809 return ffa_error(FFA_INVALID_PARAMETERS);
2810 }
2811 break;
2812 default:
2813 panic("%s: Wrong func id provided.\n", __func__);
2814 }
2815
J-Alves96de29f2022-04-26 16:05:24 +01002816 switch (sent_instruction_access) {
2817 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2818 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002819 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002820 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002821 permissions->instruction_access =
2822 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002823 }
2824 break;
2825 }
J-Alvesdcad8992023-09-15 14:10:35 +01002826 /*
2827 * Fall through if requested permissions are less
2828 * permissive than those provided by the sender.
2829 */
J-Alves96de29f2022-04-26 16:05:24 +01002830 case FFA_INSTRUCTION_ACCESS_NX:
2831 if (requested_instruction_access ==
2832 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2833 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2834 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002835 permissions->instruction_access =
2836 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002837 }
2838 break;
2839 }
2840 dlog_verbose(
2841 "Invalid instruction access requested; sender "
2842 "specified permissions %#x but receiver requested "
2843 "%#x.\n",
2844 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002845 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002846 case FFA_INSTRUCTION_ACCESS_RESERVED:
2847 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2848 "be checked before this point.");
2849 }
2850
J-Alvesdcad8992023-09-15 14:10:35 +01002851 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002852}
2853
2854/**
2855 * Validate the receivers' permissions in the retrieve request against those
2856 * specified by the lender.
2857 * In the `permissions` argument returns the permissions to set at S2 for the
2858 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002859 * The function looks into the flag to bypass multiple borrower checks:
2860 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2861 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2862 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2863 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002864 */
2865static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2866 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002867 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002868 ffa_memory_access_permissions_t *permissions,
2869 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002870{
2871 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002872 bool bypass_multi_receiver_check =
2873 (retrieve_request->flags &
2874 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002875 const uint32_t region_receiver_count = memory_region->receiver_count;
2876 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002877
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002878 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002879 assert(permissions != NULL);
2880
Karl Meakin84710f32023-10-12 15:14:49 +01002881 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002882
J-Alves3456e032023-07-20 12:20:05 +01002883 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002884 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002885 dlog_verbose(
2886 "Retrieve request should contain same list of "
2887 "borrowers, as specified by the lender.\n");
2888 return ffa_error(FFA_INVALID_PARAMETERS);
2889 }
2890 } else {
2891 if (retrieve_request->receiver_count != 1) {
2892 dlog_verbose(
2893 "Set bypass multiple borrower check, receiver "
2894 "list must be sized 1 (%x)\n",
2895 memory_region->receiver_count);
2896 return ffa_error(FFA_INVALID_PARAMETERS);
2897 }
J-Alves96de29f2022-04-26 16:05:24 +01002898 }
2899
2900 retrieve_receiver_index = retrieve_request->receiver_count;
2901
J-Alves96de29f2022-04-26 16:05:24 +01002902 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2903 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002904 struct ffa_memory_access *retrieve_request_receiver =
2905 ffa_memory_region_get_receiver(retrieve_request, i);
2906 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002907 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002908 retrieve_request_receiver->receiver_permissions
2909 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002910 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002911 retrieve_request_receiver->receiver_permissions
2912 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002913 struct ffa_memory_access *receiver;
2914 uint32_t mem_region_receiver_index;
2915 bool permissions_RO;
2916 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002917 /*
2918 * If the call is at the virtual FF-A instance the caller's
2919 * ID must match an entry in the memory access list.
2920 * In the SPMC, one of the specified receivers could be from
2921 * the NWd.
2922 */
2923 bool found_to_id = vm_id_is_current_world(to_vm_id)
2924 ? (current_receiver_id == to_vm_id)
2925 : (!vm_id_is_current_world(
2926 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002927
J-Alves3456e032023-07-20 12:20:05 +01002928 if (bypass_multi_receiver_check && !found_to_id) {
2929 dlog_verbose(
2930 "Bypass multiple borrower check for id %x.\n",
2931 current_receiver_id);
2932 continue;
2933 }
2934
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002935 if (retrieve_request_receiver->composite_memory_region_offset !=
2936 0U) {
2937 dlog_verbose(
2938 "Retriever specified address ranges not "
2939 "supported (got offset %d).\n",
2940 retrieve_request_receiver
2941 ->composite_memory_region_offset);
2942 return ffa_error(FFA_INVALID_PARAMETERS);
2943 }
2944
J-Alves96de29f2022-04-26 16:05:24 +01002945 /*
2946 * Find the current receiver in the transaction descriptor from
2947 * sender.
2948 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002949 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002950 ffa_memory_region_get_receiver_index(
2951 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002952
2953 if (mem_region_receiver_index ==
2954 memory_region->receiver_count) {
2955 dlog_verbose("%s: receiver %x not found\n", __func__,
2956 current_receiver_id);
2957 return ffa_error(FFA_DENIED);
2958 }
2959
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002960 receiver = ffa_memory_region_get_receiver(
2961 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002962 assert(receiver != NULL);
2963
2964 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002965
2966 if (found_to_id) {
2967 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002968
2969 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002970 }
2971
2972 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002973 * Check if retrieve request memory access list is valid:
2974 * - The retrieve request complies with the specification.
2975 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002976 */
J-Alvesdcad8992023-09-15 14:10:35 +01002977 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002978 func_id, sent_permissions.data_access,
2979 requested_permissions.data_access,
2980 sent_permissions.instruction_access,
2981 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002982 found_to_id ? permissions : NULL,
2983 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002984
J-Alvesdcad8992023-09-15 14:10:35 +01002985 if (ret.func != FFA_SUCCESS_32) {
2986 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002987 }
2988
Karl Meakin84710f32023-10-12 15:14:49 +01002989 permissions_RO =
2990 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002991 clear_memory_flags =
2992 (retrieve_request->flags &
2993 (FFA_MEMORY_REGION_FLAG_CLEAR |
2994 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002995
J-Alves96de29f2022-04-26 16:05:24 +01002996 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002997 * Can't request PM to clear memory if only provided
2998 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002999 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003000 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01003001 dlog_verbose(
3002 "Receiver has RO permissions can not request "
3003 "clear.\n");
3004 return ffa_error(FFA_DENIED);
3005 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003006
3007 /*
3008 * Check the impdef in the retrieve_request matches the value in
3009 * the original memory send.
3010 */
3011 if (ffa_version_from_memory_access_desc_size(
3012 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003013 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003014 ffa_version_from_memory_access_desc_size(
3015 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003016 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003017 if (receiver->impdef.val[0] !=
3018 retrieve_request_receiver->impdef.val[0] ||
3019 receiver->impdef.val[1] !=
3020 retrieve_request_receiver->impdef.val[1]) {
3021 dlog_verbose(
3022 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003023 "match retrieve request value send "
3024 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003025 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003026 receiver->impdef.val[0],
3027 receiver->impdef.val[1],
3028 retrieve_request_receiver->impdef
3029 .val[0],
3030 retrieve_request_receiver->impdef
3031 .val[1]);
3032 return ffa_error(FFA_INVALID_PARAMETERS);
3033 }
3034 }
J-Alves96de29f2022-04-26 16:05:24 +01003035 }
3036
3037 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3038 dlog_verbose(
3039 "Retrieve request does not contain caller's (%x) "
3040 "permissions\n",
3041 to_vm_id);
3042 return ffa_error(FFA_INVALID_PARAMETERS);
3043 }
3044
3045 return (struct ffa_value){.func = FFA_SUCCESS_32};
3046}
3047
Daniel Boulby296ee702023-11-28 13:36:55 +00003048/**
3049 * According to section 17.4.3 of the FF-A v1.2 ALP0 specification, the
3050 * hypervisor may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region
3051 * description of a pending memory sharing operation whose allocator is the SPM,
3052 * for validation purposes before forwarding an FFA_MEM_RECLAIM call. For a
3053 * hypervisor retrieve request the endpoint memory access descriptor count must
3054 * be 0 (for any other retrieve request it must be >= 1).
J-Alvesa9cd7e32022-07-01 13:49:33 +01003055 */
Daniel Boulby296ee702023-11-28 13:36:55 +00003056bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003057{
Daniel Boulby296ee702023-11-28 13:36:55 +00003058 return request->receiver_count == 0U;
3059}
3060
J-Alvesa9cd7e32022-07-01 13:49:33 +01003061/*
3062 * Helper to reset count of fragments retrieved by the hypervisor.
3063 */
3064static void ffa_memory_retrieve_complete_from_hyp(
3065 struct ffa_memory_share_state *share_state)
3066{
3067 if (share_state->hypervisor_fragment_count ==
3068 share_state->fragment_count) {
3069 share_state->hypervisor_fragment_count = 0;
3070 }
3071}
3072
J-Alves089004f2022-07-13 14:25:44 +01003073/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003074 * Prepares the return of the ffa_value for the memory retrieve response.
3075 */
3076static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3077 uint32_t fragment_length)
3078{
3079 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3080 .arg1 = total_length,
3081 .arg2 = fragment_length};
3082}
3083
3084/**
J-Alves089004f2022-07-13 14:25:44 +01003085 * Validate that the memory region descriptor provided by the borrower on
3086 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3087 * memory sharing call.
3088 */
3089static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003090 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3091 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003092 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3093 uint32_t share_func)
3094{
3095 ffa_memory_region_flags_t transaction_type =
3096 retrieve_request->flags &
3097 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003098 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003099 const uint64_t memory_access_desc_size =
3100 retrieve_request->memory_access_desc_size;
3101 const uint32_t expected_retrieve_request_length =
3102 retrieve_request->receivers_offset +
3103 (uint32_t)(retrieve_request->receiver_count *
3104 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003105
3106 assert(retrieve_request != NULL);
3107 assert(memory_region != NULL);
3108 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003109
J-Alves4f0d9c12024-01-17 17:23:11 +00003110 if (retrieve_request_length != expected_retrieve_request_length) {
3111 dlog_verbose(
3112 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3113 "but was %d.\n",
3114 expected_retrieve_request_length,
3115 retrieve_request_length);
3116 return ffa_error(FFA_INVALID_PARAMETERS);
3117 }
3118
3119 if (retrieve_request->sender != memory_region->sender) {
3120 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003121 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003122 "retrieve.\n",
3123 memory_region->handle);
3124 return ffa_error(FFA_DENIED);
3125 }
3126
3127 /*
3128 * The SPMC can only process retrieve requests to memory share
3129 * operations with one borrower from the other world. It can't
3130 * determine the ID of the NWd VM that invoked the retrieve
3131 * request interface call. It relies on the hypervisor to
3132 * validate the caller's ID against that provided in the
3133 * `receivers` list of the retrieve response.
3134 * In case there is only one borrower from the NWd in the
3135 * transaction descriptor, record that in the `receiver_id` for
3136 * later use, and validate in the retrieve request message.
3137 * This limitation is due to the fact SPMC can't determine the
3138 * index in the memory share structures state to update.
3139 */
3140 if (to_id == HF_HYPERVISOR_VM_ID) {
3141 uint32_t other_world_count = 0;
3142
3143 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3144 struct ffa_memory_access *receiver =
3145 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003146 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003147 assert(receiver != NULL);
3148
J-Alvesf220d572024-04-24 22:15:14 +01003149 if (!vm_id_is_current_world(
3150 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003151 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003152 /* Set it to be used later. */
3153 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003154 }
3155 }
3156
3157 if (other_world_count > 1) {
3158 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003159 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003160 return ffa_error(FFA_NOT_SUPPORTED);
3161 }
3162 }
J-Alves089004f2022-07-13 14:25:44 +01003163 /*
3164 * Check that the transaction type expected by the receiver is
3165 * correct, if it has been specified.
3166 */
3167 if (transaction_type !=
3168 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3169 transaction_type != (memory_region->flags &
3170 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3171 dlog_verbose(
3172 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003173 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003174 transaction_type,
3175 memory_region->flags &
3176 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3177 retrieve_request->handle);
3178 return ffa_error(FFA_INVALID_PARAMETERS);
3179 }
3180
3181 if (retrieve_request->tag != memory_region->tag) {
3182 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003183 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3184 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003185 retrieve_request->tag, memory_region->tag,
3186 retrieve_request->handle);
3187 return ffa_error(FFA_INVALID_PARAMETERS);
3188 }
3189
J-Alves4f0d9c12024-01-17 17:23:11 +00003190 *receiver_index =
3191 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003192
3193 if (*receiver_index == memory_region->receiver_count) {
3194 dlog_verbose(
3195 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003196 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003197 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003198 return ffa_error(FFA_INVALID_PARAMETERS);
3199 }
3200
3201 if ((retrieve_request->flags &
3202 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3203 dlog_verbose(
3204 "Retriever specified 'address range alignment 'hint' "
3205 "not supported.\n");
3206 return ffa_error(FFA_INVALID_PARAMETERS);
3207 }
3208 if ((retrieve_request->flags &
3209 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3210 dlog_verbose(
3211 "Bits 8-5 must be zero in memory region's flags "
3212 "(address range alignment hint not supported).\n");
3213 return ffa_error(FFA_INVALID_PARAMETERS);
3214 }
3215
3216 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3217 dlog_verbose(
3218 "Bits 31-10 must be zero in memory region's flags.\n");
3219 return ffa_error(FFA_INVALID_PARAMETERS);
3220 }
3221
J-Alves95fbb312024-03-20 15:19:16 +00003222 if ((share_func == FFA_MEM_SHARE_32 ||
3223 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003224 (retrieve_request->flags &
3225 (FFA_MEMORY_REGION_FLAG_CLEAR |
3226 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3227 dlog_verbose(
3228 "Memory Share operation can't clean after relinquish "
3229 "memory region.\n");
3230 return ffa_error(FFA_INVALID_PARAMETERS);
3231 }
3232
3233 /*
3234 * If the borrower needs the memory to be cleared before mapping
3235 * to its address space, the sender should have set the flag
3236 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3237 * FFA_DENIED.
3238 */
3239 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3240 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3241 dlog_verbose(
3242 "Borrower needs memory cleared. Sender needs to set "
3243 "flag for clearing memory.\n");
3244 return ffa_error(FFA_DENIED);
3245 }
3246
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003247 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003248 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003249 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3250 dlog_verbose(
3251 "Invalid security state for memory retrieve request "
3252 "operation.\n");
3253 return ffa_error(FFA_INVALID_PARAMETERS);
3254 }
3255
J-Alves089004f2022-07-13 14:25:44 +01003256 /*
3257 * If memory type is not specified, bypass validation of memory
3258 * attributes in the retrieve request. The retriever is expecting to
3259 * obtain this information from the SPMC.
3260 */
Karl Meakin84710f32023-10-12 15:14:49 +01003261 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003262 return (struct ffa_value){.func = FFA_SUCCESS_32};
3263 }
3264
3265 /*
3266 * Ensure receiver's attributes are compatible with how
3267 * Hafnium maps memory: Normal Memory, Inner shareable,
3268 * Write-Back Read-Allocate Write-Allocate Cacheable.
3269 */
3270 return ffa_memory_attributes_validate(retrieve_request->attributes);
3271}
3272
J-Alves3f6527c2024-04-25 17:10:57 +01003273/**
3274 * Whilst processing the retrieve request, the operation could be aborted, and
3275 * changes to page tables and the share state structures need to be reverted.
3276 */
3277static void ffa_partition_memory_retrieve_request_undo(
3278 struct vm_locked from_locked,
3279 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3280{
3281 /*
3282 * Currently this operation is expected for operations involving the
3283 * 'other_world' vm.
3284 */
3285 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3286 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3287
3288 /* Decrement the retrieved fragment count for the given receiver. */
3289 share_state->retrieved_fragment_count[receiver_index]--;
3290}
3291
3292/**
3293 * Whilst processing an hypervisor retrieve request the operation could be
3294 * aborted. There were no updates to PTs in this case, so decrementing the
3295 * fragment count retrieved by the hypervisor should be enough.
3296 */
3297static void ffa_hypervisor_memory_retrieve_request_undo(
3298 struct ffa_memory_share_state *share_state)
3299{
3300 assert(share_state->hypervisor_fragment_count > 0);
3301 share_state->hypervisor_fragment_count--;
3302}
3303
J-Alves4f0d9c12024-01-17 17:23:11 +00003304static struct ffa_value ffa_partition_retrieve_request(
3305 struct share_states_locked share_states,
3306 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3307 struct ffa_memory_region *retrieve_request,
3308 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003309{
Karl Meakin84710f32023-10-12 15:14:49 +01003310 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003311 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003312 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003313 struct ffa_composite_memory_region *composite;
3314 uint32_t total_length;
3315 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003316 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003317 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003318 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003319 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003320 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003321 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003322 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003323 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003324 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003325 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003326
J-Alves96de29f2022-04-26 16:05:24 +01003327 if (!share_state->sending_complete) {
3328 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003329 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003330 "retrieve.\n",
3331 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003332 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003333 }
3334
J-Alves4f0d9c12024-01-17 17:23:11 +00003335 /*
3336 * Validate retrieve request, according to what was sent by the
3337 * sender. Function will output the `receiver_index` from the
3338 * provided memory region.
3339 */
3340 ret = ffa_memory_retrieve_validate(
3341 receiver_id, retrieve_request, retrieve_request_length,
3342 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003343
J-Alves4f0d9c12024-01-17 17:23:11 +00003344 if (ret.func != FFA_SUCCESS_32) {
3345 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003346 }
J-Alves96de29f2022-04-26 16:05:24 +01003347
J-Alves4f0d9c12024-01-17 17:23:11 +00003348 /*
3349 * Validate the requested permissions against the sent
3350 * permissions.
3351 * Outputs the permissions to give to retriever at S2
3352 * PTs.
3353 */
3354 ret = ffa_memory_retrieve_validate_memory_access_list(
3355 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003356 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003357 if (ret.func != FFA_SUCCESS_32) {
3358 return ret;
3359 }
3360
3361 memory_to_mode = ffa_memory_permissions_to_mode(
3362 permissions, share_state->sender_orig_mode);
3363
Daniel Boulby6e261362024-06-13 16:53:00 +01003364 /*
3365 * Check requested memory type is valid with the memory type of the
3366 * owner. E.g. they follow the memory type precedence where Normal
3367 * memory is more permissive than device and therefore device memory
3368 * can only be shared as device memory.
3369 */
3370 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3371 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3372 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3373 dlog_verbose(
3374 "Retrieving device memory as Normal memory is not "
3375 "allowed\n");
3376 return ffa_error(FFA_DENIED);
3377 }
3378
J-Alves4f0d9c12024-01-17 17:23:11 +00003379 ret = ffa_retrieve_check_update(
3380 to_locked, share_state->fragments,
3381 share_state->fragment_constituent_counts,
3382 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003383 share_state->share_func, false, page_pool, &retrieve_mode,
3384 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003385
3386 if (ret.func != FFA_SUCCESS_32) {
3387 return ret;
3388 }
3389
3390 share_state->retrieved_fragment_count[receiver_index] = 1;
3391
3392 is_retrieve_complete =
3393 share_state->retrieved_fragment_count[receiver_index] ==
3394 share_state->fragment_count;
3395
J-Alvesb5084cf2022-07-06 14:20:12 +01003396 /* VMs acquire the RX buffer from SPMC. */
3397 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3398
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003399 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003400 * Copy response to RX buffer of caller and deliver the message.
3401 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003402 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003403 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003404
Andrew Walbranca808b12020-05-15 17:22:28 +01003405 /*
J-Alves460d36c2023-10-12 17:02:15 +01003406 * Set the security state in the memory retrieve response attributes
3407 * if specified by the target mode.
3408 */
Karl Meakin3d32eef2024-11-25 16:40:09 +00003409 attributes = plat_ffa_memory_add_security_bit_from_mode(
3410 memory_region->attributes, retrieve_mode);
J-Alves460d36c2023-10-12 17:02:15 +01003411
3412 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003413 * Constituents which we received in the first fragment should
3414 * always fit in the first fragment we are sending, because the
3415 * header is the same size in both cases and we have a fixed
3416 * message buffer size. So `ffa_retrieved_memory_region_init`
3417 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003418 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003419
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003420 /* Provide the permissions that had been provided. */
3421 receiver->receiver_permissions.permissions = permissions;
3422
3423 /*
3424 * Prepare the memory region descriptor for the retrieve response.
3425 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003426 * structures.
3427 * At this point the retrieve request descriptor from the partition
3428 * has been processed. The `retrieve_request` is expected to be in
3429 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3430 * prepare the retrieve response before copying it to the RX buffer of
3431 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003432 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003433 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003434 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3435 memory_region->sender, attributes, memory_region->flags, handle,
3436 permissions, receiver, 1, memory_access_desc_size,
3437 composite->page_count, composite->constituent_count,
3438 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003439 share_state->fragment_constituent_counts[0], &total_length,
3440 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003441
J-Alves7b9cc432024-04-04 10:57:17 +01003442 /*
3443 * Copy the message from the buffer into the partition's mailbox.
3444 * The operation might fail unexpectedly due to change in PAS address
3445 * space, or improper values to the sizes of the structures.
3446 */
3447 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3448 retrieve_request, fragment_length)) {
3449 dlog_error(
3450 "%s: aborted the copy of response to RX buffer of "
3451 "%x.\n",
3452 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003453
3454 ffa_partition_memory_retrieve_request_undo(
3455 to_locked, share_state, receiver_index);
3456
J-Alves7b9cc432024-04-04 10:57:17 +01003457 return ffa_error(FFA_ABORTED);
3458 }
3459
J-Alves4f0d9c12024-01-17 17:23:11 +00003460 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003461 ffa_memory_retrieve_complete(share_states, share_state,
3462 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003463 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003464
3465 return ffa_memory_retrieve_resp(total_length, fragment_length);
3466}
3467
3468static struct ffa_value ffa_hypervisor_retrieve_request(
3469 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3470 struct ffa_memory_region *retrieve_request)
3471{
3472 struct ffa_value ret;
3473 struct ffa_composite_memory_region *composite;
3474 uint32_t total_length;
3475 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003476 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003477 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003478 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003479 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003480 ffa_memory_handle_t handle = retrieve_request->handle;
3481
J-Alves4f0d9c12024-01-17 17:23:11 +00003482 memory_region = share_state->memory_region;
3483
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003484 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3485
J-Alves7b6ab612024-01-24 09:54:54 +00003486 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003487 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003488 memory_access_desc_size = sizeof(struct ffa_memory_access);
3489 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003490 case FFA_VERSION_1_0:
3491 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003492 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3493 break;
3494 default:
3495 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3496 }
3497
J-Alves4f0d9c12024-01-17 17:23:11 +00003498 if (share_state->hypervisor_fragment_count != 0U) {
3499 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003500 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003501 "the hypervisor.\n",
3502 handle);
3503 return ffa_error(FFA_DENIED);
3504 }
3505
3506 share_state->hypervisor_fragment_count = 1;
3507
J-Alves4f0d9c12024-01-17 17:23:11 +00003508 /* VMs acquire the RX buffer from SPMC. */
3509 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3510
3511 /*
3512 * Copy response to RX buffer of caller and deliver the message.
3513 * This must be done before the share_state is (possibly) freed.
3514 */
3515 composite = ffa_memory_region_get_composite(memory_region, 0);
3516
3517 /*
3518 * Constituents which we received in the first fragment should
3519 * always fit in the first fragment we are sending, because the
3520 * header is the same size in both cases and we have a fixed
3521 * message buffer size. So `ffa_retrieved_memory_region_init`
3522 * should never fail.
3523 */
3524
3525 /*
3526 * Set the security state in the memory retrieve response attributes
3527 * if specified by the target mode.
3528 */
Karl Meakin3d32eef2024-11-25 16:40:09 +00003529 attributes = plat_ffa_memory_add_security_bit_from_mode(
J-Alves4f0d9c12024-01-17 17:23:11 +00003530 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003531
3532 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3533
J-Alves7b9cc432024-04-04 10:57:17 +01003534 /*
3535 * At this point the `retrieve_request` is expected to be in a section
3536 * managed by the hypervisor.
3537 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003538 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003539 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3540 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003541 receiver->receiver_permissions.permissions, receiver,
3542 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003543 composite->page_count, composite->constituent_count,
3544 share_state->fragments[0],
3545 share_state->fragment_constituent_counts[0], &total_length,
3546 &fragment_length));
3547
J-Alves7b9cc432024-04-04 10:57:17 +01003548 /*
3549 * Copy the message from the buffer into the hypervisor's mailbox.
3550 * The operation might fail unexpectedly due to change in PAS, or
3551 * improper values for the sizes of the structures.
3552 */
3553 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3554 retrieve_request, fragment_length)) {
3555 dlog_error(
3556 "%s: aborted the copy of response to RX buffer of "
3557 "%x.\n",
3558 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003559
3560 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3561
J-Alves7b9cc432024-04-04 10:57:17 +01003562 return ffa_error(FFA_ABORTED);
3563 }
3564
J-Alves3f6527c2024-04-25 17:10:57 +01003565 ffa_memory_retrieve_complete_from_hyp(share_state);
3566
J-Alves4f0d9c12024-01-17 17:23:11 +00003567 return ffa_memory_retrieve_resp(total_length, fragment_length);
3568}
3569
3570struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3571 struct ffa_memory_region *retrieve_request,
3572 uint32_t retrieve_request_length,
3573 struct mpool *page_pool)
3574{
3575 ffa_memory_handle_t handle = retrieve_request->handle;
3576 struct share_states_locked share_states;
3577 struct ffa_memory_share_state *share_state;
3578 struct ffa_value ret;
3579
3580 dump_share_states();
3581
3582 share_states = share_states_lock();
3583 share_state = get_share_state(share_states, handle);
3584 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003585 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003586 handle);
3587 ret = ffa_error(FFA_INVALID_PARAMETERS);
3588 goto out;
3589 }
3590
Daniel Boulby296ee702023-11-28 13:36:55 +00003591 if (is_ffa_hypervisor_retrieve_request(retrieve_request)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003592 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3593 retrieve_request);
3594 } else {
3595 ret = ffa_partition_retrieve_request(
3596 share_states, share_state, to_locked, retrieve_request,
3597 retrieve_request_length, page_pool);
3598 }
3599
3600 /* Track use of the RX buffer if the handling has succeeded. */
3601 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3602 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3603 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3604 }
3605
Andrew Walbranca808b12020-05-15 17:22:28 +01003606out:
3607 share_states_unlock(&share_states);
3608 dump_share_states();
3609 return ret;
3610}
3611
J-Alves5da37d92022-10-24 16:33:48 +01003612/**
3613 * Determine expected fragment offset according to the FF-A version of
3614 * the caller.
3615 */
3616static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3617 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003618 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003619{
3620 uint32_t expected_fragment_offset;
3621 uint32_t composite_constituents_offset;
3622
Karl Meakin0e617d92024-04-05 12:55:22 +01003623 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003624 /*
3625 * Hafnium operates memory regions in FF-A v1.1 format, so we
3626 * can retrieve the constituents offset from descriptor.
3627 */
3628 composite_constituents_offset =
3629 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003630 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003631 /*
3632 * If retriever is FF-A v1.0, determine the composite offset
3633 * as it is expected to have been configured in the
3634 * retrieve response.
3635 */
3636 composite_constituents_offset =
3637 sizeof(struct ffa_memory_region_v1_0) +
3638 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003639 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003640 sizeof(struct ffa_composite_memory_region);
3641 } else {
3642 panic("%s received an invalid FF-A version.\n", __func__);
3643 }
3644
3645 expected_fragment_offset =
3646 composite_constituents_offset +
3647 retrieved_constituents_count *
3648 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003649 (size_t)(memory_region->memory_access_desc_size *
3650 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003651
3652 return expected_fragment_offset;
3653}
3654
Andrew Walbranca808b12020-05-15 17:22:28 +01003655struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3656 ffa_memory_handle_t handle,
3657 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003658 ffa_id_t sender_vm_id,
J-Alvesc3fd9752024-04-04 11:45:33 +01003659 void *retrieve_continue_page,
Andrew Walbranca808b12020-05-15 17:22:28 +01003660 struct mpool *page_pool)
3661{
3662 struct ffa_memory_region *memory_region;
3663 struct share_states_locked share_states;
3664 struct ffa_memory_share_state *share_state;
3665 struct ffa_value ret;
3666 uint32_t fragment_index;
3667 uint32_t retrieved_constituents_count;
3668 uint32_t i;
3669 uint32_t expected_fragment_offset;
3670 uint32_t remaining_constituent_count;
3671 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003672 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003673 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003674
3675 dump_share_states();
3676
3677 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003678 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003679 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003680 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003681 handle);
3682 ret = ffa_error(FFA_INVALID_PARAMETERS);
3683 goto out;
3684 }
3685
3686 memory_region = share_state->memory_region;
3687 CHECK(memory_region != NULL);
3688
Andrew Walbranca808b12020-05-15 17:22:28 +01003689 if (!share_state->sending_complete) {
3690 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003691 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003692 "retrieve.\n",
3693 handle);
3694 ret = ffa_error(FFA_INVALID_PARAMETERS);
3695 goto out;
3696 }
3697
J-Alves59ed0042022-07-28 18:26:41 +01003698 /*
3699 * If retrieve request from the hypervisor has been initiated in the
3700 * given share_state, continue it, else assume it is a continuation of
J-Alvesc3fd9752024-04-04 11:45:33 +01003701 * retrieve request from a partition.
J-Alves59ed0042022-07-28 18:26:41 +01003702 */
3703 continue_ffa_hyp_mem_retrieve_req =
3704 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3705 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003706 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003707
J-Alves59ed0042022-07-28 18:26:41 +01003708 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003709 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003710 memory_region, to_locked.vm->id);
3711
3712 if (receiver_index == memory_region->receiver_count) {
3713 dlog_verbose(
3714 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003715 "borrower to memory sharing transaction "
3716 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003717 to_locked.vm->id, handle);
3718 ret = ffa_error(FFA_INVALID_PARAMETERS);
3719 goto out;
3720 }
3721
J-Alvesc3fd9752024-04-04 11:45:33 +01003722 fragment_index =
3723 share_state->retrieved_fragment_count[receiver_index];
3724
3725 if (fragment_index == 0 ||
3726 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003727 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003728 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003729 "started or already completed (%d/%d fragments "
3730 "retrieved).\n",
3731 handle,
3732 share_state->retrieved_fragment_count
3733 [receiver_index],
3734 share_state->fragment_count);
3735 ret = ffa_error(FFA_INVALID_PARAMETERS);
3736 goto out;
3737 }
J-Alves59ed0042022-07-28 18:26:41 +01003738 } else {
J-Alvesc3fd9752024-04-04 11:45:33 +01003739 fragment_index = share_state->hypervisor_fragment_count;
3740
3741 if (fragment_index == 0 ||
3742 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003743 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003744 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003745 "started from hypervisor.\n",
3746 handle);
3747 ret = ffa_error(FFA_INVALID_PARAMETERS);
3748 goto out;
3749 }
3750
3751 if (memory_region->sender != sender_vm_id) {
3752 dlog_verbose(
3753 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003754 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003755 sender_vm_id, handle);
3756 ret = ffa_error(FFA_INVALID_PARAMETERS);
3757 goto out;
3758 }
3759
J-Alves59ed0042022-07-28 18:26:41 +01003760 receiver_index = 0;
3761 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003762
3763 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003764 * Check that the given fragment offset is correct by counting
3765 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003766 */
3767 retrieved_constituents_count = 0;
3768 for (i = 0; i < fragment_index; ++i) {
3769 retrieved_constituents_count +=
3770 share_state->fragment_constituent_counts[i];
3771 }
J-Alvesc7484f12022-05-13 12:41:14 +01003772
3773 CHECK(memory_region->receiver_count > 0);
3774
Andrew Walbranca808b12020-05-15 17:22:28 +01003775 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003776 ffa_memory_retrieve_expected_offset_per_ffa_version(
3777 memory_region, retrieved_constituents_count,
3778 to_locked.vm->ffa_version);
3779
Andrew Walbranca808b12020-05-15 17:22:28 +01003780 if (fragment_offset != expected_fragment_offset) {
3781 dlog_verbose("Fragment offset was %d but expected %d.\n",
3782 fragment_offset, expected_fragment_offset);
3783 ret = ffa_error(FFA_INVALID_PARAMETERS);
3784 goto out;
3785 }
3786
J-Alves4f0d9c12024-01-17 17:23:11 +00003787 /*
3788 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3789 * is currently ownder by the SPMC.
3790 */
3791 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003792
Andrew Walbranca808b12020-05-15 17:22:28 +01003793 remaining_constituent_count = ffa_memory_fragment_init(
J-Alvesc3fd9752024-04-04 11:45:33 +01003794 (struct ffa_memory_region_constituent *)retrieve_continue_page,
3795 HF_MAILBOX_SIZE, share_state->fragments[fragment_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01003796 share_state->fragment_constituent_counts[fragment_index],
3797 &fragment_length);
3798 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003799
J-Alvesc3fd9752024-04-04 11:45:33 +01003800 /*
3801 * Return FFA_ERROR(FFA_ABORTED) in case the access to the partition's
3802 * RX buffer results in a GPF exception. Could happen if the retrieve
3803 * request is for a VM or the Hypervisor retrieve request, if the PAS
3804 * has been changed externally.
3805 */
3806 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3807 retrieve_continue_page, fragment_length)) {
3808 dlog_error(
3809 "%s: aborted copying fragment to RX buffer of %#x.\n",
3810 __func__, to_locked.vm->id);
3811 ret = ffa_error(FFA_ABORTED);
3812 goto out;
3813 }
3814
Andrew Walbranca808b12020-05-15 17:22:28 +01003815 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003816 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003817
J-Alves59ed0042022-07-28 18:26:41 +01003818 if (!continue_ffa_hyp_mem_retrieve_req) {
3819 share_state->retrieved_fragment_count[receiver_index]++;
3820 if (share_state->retrieved_fragment_count[receiver_index] ==
3821 share_state->fragment_count) {
3822 ffa_memory_retrieve_complete(share_states, share_state,
3823 page_pool);
3824 }
3825 } else {
3826 share_state->hypervisor_fragment_count++;
3827
3828 ffa_memory_retrieve_complete_from_hyp(share_state);
3829 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003830 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3831 .arg1 = (uint32_t)handle,
3832 .arg2 = (uint32_t)(handle >> 32),
3833 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003834
3835out:
3836 share_states_unlock(&share_states);
3837 dump_share_states();
3838 return ret;
3839}
3840
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003841struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003842 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003843 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003844{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003845 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003846 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003847 struct ffa_memory_share_state *share_state;
3848 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003849 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003850 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003851 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003852 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003853 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003854
Andrew Walbrana65a1322020-04-06 19:32:32 +01003855 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003856 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003857 "Stream endpoints not supported (got %d endpoints on "
3858 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003859 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003860 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003861 }
3862
J-Alvesbd060342024-04-26 18:44:31 +01003863 if (vm_id_is_current_world(from_locked.vm->id) &&
3864 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003865 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003866 "VM ID %d in relinquish message doesn't match calling "
3867 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003868 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003869 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003870 }
3871
3872 dump_share_states();
3873
3874 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003875 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003876 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003877 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003878 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003879 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003880 goto out;
3881 }
3882
Andrew Walbranca808b12020-05-15 17:22:28 +01003883 if (!share_state->sending_complete) {
3884 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003885 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003886 "relinquish.\n",
3887 handle);
3888 ret = ffa_error(FFA_INVALID_PARAMETERS);
3889 goto out;
3890 }
3891
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003892 memory_region = share_state->memory_region;
3893 CHECK(memory_region != NULL);
3894
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003895 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003896 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003897
3898 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003899 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003900 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003901 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003902 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003903 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003904 goto out;
3905 }
3906
J-Alves8eb19162022-04-28 10:56:48 +01003907 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003908 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003909 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003910 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003911 "receiver %x can't relinquish.\n",
3912 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003913 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003914 goto out;
3915 }
3916
J-Alves3c5b2072022-11-21 12:45:40 +00003917 /*
3918 * Either clear if requested in relinquish call, or in a retrieve
3919 * request from one of the borrowers.
3920 */
3921 receivers_relinquished_memory = true;
3922
3923 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3924 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003925 ffa_memory_region_get_receiver(memory_region, i);
3926 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003927 if (receiver->receiver_permissions.receiver ==
3928 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003929 receiver_permissions =
3930 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003931 continue;
3932 }
3933
3934 if (share_state->retrieved_fragment_count[i] != 0U) {
3935 receivers_relinquished_memory = false;
3936 break;
3937 }
3938 }
3939
3940 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003941 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3942 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003943
3944 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003945 * Clear is not allowed for memory that was shared, as the
3946 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003947 */
J-Alves95fbb312024-03-20 15:19:16 +00003948 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3949 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003950 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003951 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003952 goto out;
3953 }
3954
J-Alvesb886d492024-04-15 10:55:29 +01003955 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003956 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3957 __func__);
3958 ret = ffa_error(FFA_DENIED);
3959 goto out;
3960 }
3961
Andrew Walbranca808b12020-05-15 17:22:28 +01003962 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003963 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003964 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003965 share_state->fragment_count, share_state->sender_orig_mode,
3966 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003967
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003968 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003969 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003970 * Mark memory handle as not retrieved, so it can be
3971 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003972 */
J-Alves8eb19162022-04-28 10:56:48 +01003973 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003974 }
3975
3976out:
3977 share_states_unlock(&share_states);
3978 dump_share_states();
3979 return ret;
3980}
3981
3982/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003983 * Validates that the reclaim transition is allowed for the given
3984 * handle, updates the page table of the reclaiming VM, and frees the
3985 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003986 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003987struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003988 ffa_memory_handle_t handle,
3989 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003990 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003991{
3992 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003993 struct ffa_memory_share_state *share_state;
3994 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003995 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003996
3997 dump_share_states();
3998
3999 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01004000
Karl Meakin4a2854a2023-06-30 16:26:52 +01004001 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00004002 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00004003 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004004 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004005 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004006 goto out;
4007 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01004008 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004009
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004010 CHECK(memory_region != NULL);
4011
J-Alvesa9cd7e32022-07-01 13:49:33 +01004012 if (vm_id_is_current_world(to_locked.vm->id) &&
4013 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004014 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004015 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004016 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004017 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004018 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004019 goto out;
4020 }
4021
Andrew Walbranca808b12020-05-15 17:22:28 +01004022 if (!share_state->sending_complete) {
4023 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004024 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004025 "reclaim.\n",
4026 handle);
4027 ret = ffa_error(FFA_INVALID_PARAMETERS);
4028 goto out;
4029 }
4030
J-Alves752236c2022-04-28 11:07:47 +01004031 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4032 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004033 struct ffa_memory_access *receiver =
4034 ffa_memory_region_get_receiver(memory_region,
4035 i);
4036
4037 assert(receiver != NULL);
4038 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004039 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004040 "Tried to reclaim memory handle %#lx that has "
4041 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004042 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004043 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004044 ret = ffa_error(FFA_DENIED);
4045 goto out;
4046 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004047 }
4048
Andrew Walbranca808b12020-05-15 17:22:28 +01004049 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004050 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004051 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004052 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004053 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004054 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004055
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004056 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004057 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004058 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004059 }
4060
4061out:
4062 share_states_unlock(&share_states);
4063 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004064}