blob: 056fd86e9c9bccfe36fb6eacab21e638f71315d0 [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"
Andrew Walbran290b0c92020-02-03 16:37:14 +000015
J-Alves5952d942022-12-22 16:03:00 +000016#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010017#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000018#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010019#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010021#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010022#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010023#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010024#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000025#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000026#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000027#include "hf/panic.h"
28#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010029#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000030#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000031#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010032
J-Alves2d8457f2022-10-05 11:06:41 +010033#include "vmapi/hf/ffa_v1_0.h"
34
J-Alves5da37d92022-10-24 16:33:48 +010035#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
36
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000037/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000039 * by this lock.
40 */
41static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010042static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000043
44/**
J-Alvesed508c82023-05-04 16:09:48 +010045 * Return the offset to the first constituent within the
46 * `ffa_composite_memory_region` for the given receiver from an
47 * `ffa_memory_region`. The caller must check that the receiver_index is within
48 * bounds, and that it has a composite memory region offset.
49 */
50static uint32_t ffa_composite_constituent_offset(
51 struct ffa_memory_region *memory_region, uint32_t receiver_index)
52{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000053 struct ffa_memory_access *receiver;
54 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010055
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000056 CHECK(receiver_index < memory_region->receiver_count);
57
58 receiver =
59 ffa_memory_region_get_receiver(memory_region, receiver_index);
60 CHECK(receiver != NULL);
61
62 composite_offset = receiver->composite_memory_region_offset;
63
64 CHECK(composite_offset != 0);
65
66 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010067}
68
69/**
J-Alves917d2f22020-10-30 18:39:30 +000070 * Extracts the index from a memory handle allocated by Hafnium's current world.
71 */
72uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
73{
74 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
75}
76
77/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010078 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
79 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
80 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010081 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010082 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
83 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010084 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010085struct ffa_memory_share_state *allocate_share_state(
86 struct share_states_locked share_states, uint32_t share_func,
87 struct ffa_memory_region *memory_region, uint32_t fragment_length,
88 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000090 assert(share_states.share_states != NULL);
91 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092
Karl Meakin52cdfe72023-06-30 14:49:10 +010093 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010094 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010095 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010096 &share_states.share_states[i];
97 struct ffa_composite_memory_region *composite =
98 ffa_memory_region_get_composite(memory_region,
99 0);
100
101 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000102 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200103 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100104 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000105 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100106 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000107 allocated_state->share_func = share_func;
108 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100109 allocated_state->fragment_count = 1;
110 allocated_state->fragments[0] = composite->constituents;
111 allocated_state->fragment_constituent_counts[0] =
112 (fragment_length -
113 ffa_composite_constituent_offset(memory_region,
114 0)) /
115 sizeof(struct ffa_memory_region_constituent);
116 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100117 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
118 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100119 allocated_state->retrieved_fragment_count[j] =
120 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000121 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100122 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000123 }
124 }
125
Karl Meakin52cdfe72023-06-30 14:49:10 +0100126 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000127}
128
129/** Locks the share states lock. */
130struct share_states_locked share_states_lock(void)
131{
132 sl_lock(&share_states_lock_instance);
133
134 return (struct share_states_locked){.share_states = share_states};
135}
136
137/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100138void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000139{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000140 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000141 share_states->share_states = NULL;
142 sl_unlock(&share_states_lock_instance);
143}
144
145/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100146 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100147 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100149struct ffa_memory_share_state *get_share_state(
150 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100152 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000154 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100155
156 /*
157 * First look for a share_state allocated by us, in which case the
158 * handle is based on the index.
159 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200160 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100161 uint64_t index = ffa_memory_handle_get_index(handle);
162
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 if (index < MAX_MEM_SHARES) {
164 share_state = &share_states.share_states[index];
165 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100166 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
168 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000169 }
170
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100172 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000174 if (share_state->memory_region != NULL &&
175 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100176 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100177 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000179 }
180
Karl Meakin4a2854a2023-06-30 16:26:52 +0100181 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000182}
183
184/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100185void share_state_free(struct share_states_locked share_states,
186 struct ffa_memory_share_state *share_state,
187 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188{
Andrew Walbranca808b12020-05-15 17:22:28 +0100189 uint32_t i;
190
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000191 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000192 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100193 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100195 /*
196 * First fragment is part of the same page as the `memory_region`, so it
197 * doesn't need to be freed separately.
198 */
199 share_state->fragments[0] = NULL;
200 share_state->fragment_constituent_counts[0] = 0;
201 for (i = 1; i < share_state->fragment_count; ++i) {
202 mpool_free(page_pool, share_state->fragments[i]);
203 share_state->fragments[i] = NULL;
204 share_state->fragment_constituent_counts[i] = 0;
205 }
206 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000207 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100208 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209}
210
Andrew Walbranca808b12020-05-15 17:22:28 +0100211/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100212bool share_state_sending_complete(struct share_states_locked share_states,
213 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000214{
Andrew Walbranca808b12020-05-15 17:22:28 +0100215 struct ffa_composite_memory_region *composite;
216 uint32_t expected_constituent_count;
217 uint32_t fragment_constituent_count_total = 0;
218 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000219
Andrew Walbranca808b12020-05-15 17:22:28 +0100220 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000221 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100222
223 /*
224 * Share state must already be valid, or it's not possible to get hold
225 * of it.
226 */
227 CHECK(share_state->memory_region != NULL &&
228 share_state->share_func != 0);
229
230 composite =
231 ffa_memory_region_get_composite(share_state->memory_region, 0);
232 expected_constituent_count = composite->constituent_count;
233 for (i = 0; i < share_state->fragment_count; ++i) {
234 fragment_constituent_count_total +=
235 share_state->fragment_constituent_counts[i];
236 }
237 dlog_verbose(
238 "Checking completion: constituent count %d/%d from %d "
239 "fragments.\n",
240 fragment_constituent_count_total, expected_constituent_count,
241 share_state->fragment_count);
242
243 return fragment_constituent_count_total == expected_constituent_count;
244}
245
246/**
247 * Calculates the offset of the next fragment expected for the given share
248 * state.
249 */
J-Alvesfdd29272022-07-19 13:16:31 +0100250uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100251 struct share_states_locked share_states,
252 struct ffa_memory_share_state *share_state)
253{
254 uint32_t next_fragment_offset;
255 uint32_t i;
256
257 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000258 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100259
260 next_fragment_offset =
261 ffa_composite_constituent_offset(share_state->memory_region, 0);
262 for (i = 0; i < share_state->fragment_count; ++i) {
263 next_fragment_offset +=
264 share_state->fragment_constituent_counts[i] *
265 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000266 }
267
Andrew Walbranca808b12020-05-15 17:22:28 +0100268 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269}
270
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100271static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000272{
273 uint32_t i;
274
275 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
276 return;
277 }
278
Karl Meakine8937d92024-03-19 16:04:25 +0000279 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
280 "type = %s, security = %s), flags %#x, handle %#lx "
281 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000283 memory_region->sender,
284 ffa_memory_shareability_name(
285 memory_region->attributes.shareability),
286 ffa_memory_cacheability_name(
287 memory_region->attributes.cacheability),
288 ffa_memory_type_name(memory_region->attributes.type),
289 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000290 memory_region->flags, memory_region->handle, memory_region->tag,
291 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100292 memory_region->receiver_count);
293 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000294 struct ffa_memory_access *receiver =
295 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000296 if (i != 0) {
297 dlog(", ");
298 }
Karl Meakine8937d92024-03-19 16:04:25 +0000299 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000300 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000301 ffa_data_access_name(receiver->receiver_permissions
302 .permissions.data_access),
303 ffa_instruction_access_name(
304 receiver->receiver_permissions.permissions
305 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000306 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000307 /* The impdef field is only present from v1.2 and later */
308 if (ffa_version_from_memory_access_desc_size(
309 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100310 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000311 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000312 receiver->impdef.val[1]);
313 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000314 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000315 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000316}
317
J-Alves66652252022-07-06 09:49:51 +0100318void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000319{
320 uint32_t i;
321
322 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
323 return;
324 }
325
326 dlog("Current share states:\n");
327 sl_lock(&share_states_lock_instance);
328 for (i = 0; i < MAX_MEM_SHARES; ++i) {
329 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000330 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000331 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 dlog("SHARE");
334 break;
J-Alves95fbb312024-03-20 15:19:16 +0000335 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337 dlog("LEND");
338 break;
J-Alves95fbb312024-03-20 15:19:16 +0000339 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100340 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000341 dlog("DONATE");
342 break;
343 default:
344 dlog("invalid share_func %#x",
345 share_states[i].share_func);
346 }
Karl Meakine8937d92024-03-19 16:04:25 +0000347 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000348 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100349 if (share_states[i].sending_complete) {
350 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100352 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 }
J-Alves2a0d2882020-10-29 14:49:50 +0000354 dlog(" with %d fragments, %d retrieved, "
355 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100356 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000357 share_states[i].retrieved_fragment_count[0],
358 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000359 }
360 }
361 sl_unlock(&share_states_lock_instance);
362}
363
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100365 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000366{
367 uint32_t mode = 0;
368
Karl Meakin84710f32023-10-12 15:14:49 +0100369 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000371 mode = MM_MODE_R;
372 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100373 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000374 mode = MM_MODE_R | MM_MODE_W;
375 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100376 case FFA_DATA_ACCESS_NOT_SPECIFIED:
377 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
378 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 case FFA_DATA_ACCESS_RESERVED:
380 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100381 default:
382 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100383 }
384
Karl Meakin84710f32023-10-12 15:14:49 +0100385 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000387 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100388 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100389 mode |= MM_MODE_X;
390 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100391 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
392 mode |= (default_mode & MM_MODE_X);
393 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100394 case FFA_INSTRUCTION_ACCESS_RESERVED:
395 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100396 default:
397 panic("Unknown instruction access %#x\n",
398 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000399 }
400
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200401 /* Set the security state bit if necessary. */
402 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
403 mode |= plat_ffa_other_world_mode();
404 }
405
Daniel Boulby6e261362024-06-13 16:53:00 +0100406 mode |= default_mode & MM_MODE_D;
407
Andrew Walbran475c1452020-02-07 13:22:22 +0000408 return mode;
409}
410
Jose Marinho75509b42019-04-09 09:34:59 +0100411/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000412 * Get the current mode in the stage-2 page table of the given vm of all the
413 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100415 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000417 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100418 struct ffa_memory_region_constituent **fragments,
419 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100420{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100421 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100422 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100423
Andrew Walbranca808b12020-05-15 17:22:28 +0100424 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100425 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000426 * Fail if there are no constituents. Otherwise we would get an
427 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100429 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100431 }
432
Andrew Walbranca808b12020-05-15 17:22:28 +0100433 for (i = 0; i < fragment_count; ++i) {
434 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
435 ipaddr_t begin = ipa_init(fragments[i][j].address);
436 size_t size = fragments[i][j].page_count * PAGE_SIZE;
437 ipaddr_t end = ipa_add(begin, size);
438 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100439
Andrew Walbranca808b12020-05-15 17:22:28 +0100440 /* Fail if addresses are not page-aligned. */
441 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
442 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100443 dlog_verbose("%s: addresses not page-aligned\n",
444 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100445 return ffa_error(FFA_INVALID_PARAMETERS);
446 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100447
Andrew Walbranca808b12020-05-15 17:22:28 +0100448 /*
449 * Ensure that this constituent memory range is all
450 * mapped with the same mode.
451 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800452 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100453 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000454 "%s: constituent memory range "
455 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100456 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000457 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100458 return ffa_error(FFA_DENIED);
459 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460
Andrew Walbranca808b12020-05-15 17:22:28 +0100461 /*
462 * Ensure that all constituents are mapped with the same
463 * mode.
464 */
465 if (i == 0) {
466 *orig_mode = current_mode;
467 } else if (current_mode != *orig_mode) {
468 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100469 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000470 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100471 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100472 fragments[i][j].page_count,
473 ipa_addr(begin));
474 return ffa_error(FFA_DENIED);
475 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100476 }
Jose Marinho75509b42019-04-09 09:34:59 +0100477 }
478
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100479 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000480}
481
Karl Meakin0e617d92024-04-05 12:55:22 +0100482enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100483 uint32_t memory_access_desc_size)
484{
485 switch (memory_access_desc_size) {
486 /*
487 * v1.0 and v1.1 memory access descriptors are the same size however
488 * v1.1 is the first version to include the memory access descriptor
489 * size field so return v1.1.
490 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000491 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100492 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000493 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100494 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100495 default:
496 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100497 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100498}
499
500/**
501 * Check if the receivers size and offset given is valid for the senders
502 * FF-A version.
503 */
504static bool receiver_size_and_offset_valid_for_version(
505 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100506 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100507{
508 /*
509 * Check that the version that the memory access descriptor size belongs
510 * to is compatible with the FF-A version we believe the sender to be.
511 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100512 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100513 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100514 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100515 return false;
516 }
517
518 /*
519 * Check the receivers_offset matches the version we found from
520 * memory access descriptor size.
521 */
522 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100523 case FFA_VERSION_1_1:
524 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100525 return receivers_offset == sizeof(struct ffa_memory_region);
526 default:
527 return false;
528 }
529}
530
531/**
532 * Check the values set for fields in the memory region are valid and safe.
533 * Offset values are within safe bounds, receiver count will not cause overflows
534 * and reserved fields are 0.
535 */
536bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100537 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100538 uint32_t fragment_length,
539 bool send_transaction)
540{
541 uint32_t receiver_count;
542 struct ffa_memory_access *receiver;
543 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000544 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
545 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100546
Karl Meakin0e617d92024-04-05 12:55:22 +0100547 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100548 /* Check the reserved fields are 0. */
549 if (memory_region_v1_0->reserved_0 != 0 ||
550 memory_region_v1_0->reserved_1 != 0) {
551 dlog_verbose("Reserved fields must be 0.\n");
552 return false;
553 }
554
555 receiver_count = memory_region_v1_0->receiver_count;
556 } else {
557 uint32_t receivers_size =
558 memory_region->memory_access_desc_size;
559 uint32_t receivers_offset = memory_region->receivers_offset;
560
561 /* Check the reserved field is 0. */
562 if (memory_region->reserved[0] != 0 ||
563 memory_region->reserved[1] != 0 ||
564 memory_region->reserved[2] != 0) {
565 dlog_verbose("Reserved fields must be 0.\n");
566 return false;
567 }
568
569 /*
570 * Check memory_access_desc_size matches the size of the struct
571 * for the senders FF-A version.
572 */
573 if (!receiver_size_and_offset_valid_for_version(
574 receivers_size, receivers_offset, ffa_version)) {
575 dlog_verbose(
576 "Invalid memory access descriptor size %d, "
577 " or receiver offset %d, "
578 "for FF-A version %#x\n",
579 receivers_size, receivers_offset, ffa_version);
580 return false;
581 }
582
583 receiver_count = memory_region->receiver_count;
584 }
585
586 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000587 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100588 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000589 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100590 "specified %u\n",
591 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
592 return false;
593 }
594
595 /* Check values in the memory access descriptors. */
596 /*
597 * The composite offset values must be the same for all recievers so
598 * check the first one is valid and then they are all the same.
599 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100600 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000601 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100602 ->receivers[0]
603 : ffa_memory_region_get_receiver(memory_region, 0);
604 assert(receiver != NULL);
605 composite_offset_0 = receiver->composite_memory_region_offset;
606
607 if (!send_transaction) {
608 if (composite_offset_0 != 0) {
609 dlog_verbose(
610 "Composite offset memory region descriptor "
611 "offset must be 0 for retrieve requests. "
612 "Currently %d",
613 composite_offset_0);
614 return false;
615 }
616 } else {
617 bool comp_offset_is_zero = composite_offset_0 == 0U;
618 bool comp_offset_lt_transaction_descriptor_size =
619 composite_offset_0 <
620 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100621 (size_t)(memory_region->memory_access_desc_size *
622 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100623 bool comp_offset_with_comp_gt_fragment_length =
624 composite_offset_0 +
625 sizeof(struct ffa_composite_memory_region) >
626 fragment_length;
627 if (comp_offset_is_zero ||
628 comp_offset_lt_transaction_descriptor_size ||
629 comp_offset_with_comp_gt_fragment_length) {
630 dlog_verbose(
631 "Invalid composite memory region descriptor "
632 "offset for send transaction %u\n",
633 composite_offset_0);
634 return false;
635 }
636 }
637
Karl Meakin824b63d2024-06-03 19:04:53 +0100638 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100639 uint32_t composite_offset;
640
Karl Meakin0e617d92024-04-05 12:55:22 +0100641 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100642 struct ffa_memory_access_v1_0 *receiver_v1_0 =
643 &memory_region_v1_0->receivers[i];
644 /* Check reserved fields are 0 */
645 if (receiver_v1_0->reserved_0 != 0) {
646 dlog_verbose(
647 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000648 "descriptor must be zero. Currently "
649 "reciever %zu has a reserved field "
650 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100651 i, receiver_v1_0->reserved_0);
652 return false;
653 }
654 /*
655 * We can cast to the current version receiver as the
656 * remaining fields we are checking have the same
657 * offsets for all versions since memory access
658 * descriptors are forwards compatible.
659 */
660 receiver = (struct ffa_memory_access *)receiver_v1_0;
661 } else {
662 receiver = ffa_memory_region_get_receiver(memory_region,
663 i);
664 assert(receiver != NULL);
665
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100666 if (ffa_version == FFA_VERSION_1_1) {
667 /*
668 * Since the reserved field is at the end of the
669 * Endpoint Memory Access Descriptor we must
670 * cast to ffa_memory_access_v1_0 as they match.
671 * Since all fields except reserved in the
672 * Endpoint Memory Access Descriptor have the
673 * same offsets across all versions this cast is
674 * not required when accessing other fields in
675 * the future.
676 */
677 struct ffa_memory_access_v1_0 *receiver_v1_0 =
678 (struct ffa_memory_access_v1_0 *)
679 receiver;
680 if (receiver_v1_0->reserved_0 != 0) {
681 dlog_verbose(
682 "Reserved field in the memory "
683 "access descriptor must be "
684 "zero. Currently reciever %zu "
685 "has a reserved field with a "
686 "value of %lu\n",
687 i, receiver_v1_0->reserved_0);
688 return false;
689 }
690
691 } else {
692 if (receiver->reserved_0 != 0) {
693 dlog_verbose(
694 "Reserved field in the memory "
695 "access descriptor must be "
696 "zero. Currently reciever %zu "
697 "has a reserved field with a "
698 "value of %lu\n",
699 i, receiver->reserved_0);
700 return false;
701 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100702 }
703 }
704
705 /* Check composite offset values are equal for all receivers. */
706 composite_offset = receiver->composite_memory_region_offset;
707 if (composite_offset != composite_offset_0) {
708 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000709 "Composite offset %x differs from %x in "
710 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100711 composite_offset, composite_offset_0);
712 return false;
713 }
714 }
715 return true;
716}
717
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000718/**
J-Alves460d36c2023-10-12 17:02:15 +0100719 * If the receivers for the memory management operation are all from the
Daniel Boulby734981e2024-07-22 11:06:35 +0100720 * secure world, the memory is not device memory (as it isn't covered by the
721 * granule page table) and this isn't a FFA_MEM_SHARE, then request memory
722 * security state update by returning MAP_ACTION_CHECK_PROTECT.
J-Alves460d36c2023-10-12 17:02:15 +0100723 */
724static enum ffa_map_action ffa_mem_send_get_map_action(
725 bool all_receivers_from_current_world, ffa_id_t sender_id,
Daniel Boulby734981e2024-07-22 11:06:35 +0100726 uint32_t mem_func_id, bool is_normal_memory)
J-Alves460d36c2023-10-12 17:02:15 +0100727{
J-Alves95fbb312024-03-20 15:19:16 +0000728 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
729 mem_func_id == FFA_MEM_SHARE_64;
730 const bool protect_memory =
731 (!is_memory_share_abi && all_receivers_from_current_world &&
Daniel Boulby734981e2024-07-22 11:06:35 +0100732 ffa_is_vm_id(sender_id) && is_normal_memory);
J-Alves460d36c2023-10-12 17:02:15 +0100733
734 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
735}
736
737/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000738 * Verify that all pages have the same mode, that the starting mode
739 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100740 * to the sending VM. It outputs the mapping action that needs to be
741 * invoked for the given memory range. On memory lend/donate there
742 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000743 *
744 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100745 * 1) FFA_DENIED if a state transition was not found;
746 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100747 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100748 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100749 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100750 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
751 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000752 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100753static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100754 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000755 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100756 struct ffa_memory_region_constituent **fragments,
757 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100758 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000759{
760 const uint32_t state_mask =
761 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100762 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100763 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000764 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000765 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
766 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000767
Andrew Walbranca808b12020-05-15 17:22:28 +0100768 ret = constituents_get_mode(from, orig_from_mode, fragments,
769 fragment_constituent_counts,
770 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100771 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100772 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100773 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100774 }
775
Daniel Boulby6e261362024-06-13 16:53:00 +0100776 /*
777 * Check requested memory type is valid with the memory type of the
778 * owner. E.g. they follow the memory type precedence where Normal
779 * memory is more permissive than device and therefore device memory
780 * can only be shared as device memory.
781 */
782 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
783 (*orig_from_mode & MM_MODE_D) != 0U) {
784 dlog_verbose(
785 "Send device memory as Normal memory is not allowed\n");
786 return ffa_error(FFA_DENIED);
787 }
788
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000789 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000790 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000791 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000792 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000793 "Device memory can only be lent to a single borrower "
794 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000795 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100796 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000797 }
798
799 /*
800 * Ensure the sender is the owner and has exclusive access to the
801 * memory.
802 */
803 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100804 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100805 }
806
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100807 /*
808 * Memory cannot be zeroed during the lend/donate operation if the
809 * sender only has RO access.
810 */
811 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
812 dlog_verbose(
813 "Cannot zero memory when the sender doesn't have "
814 "write access\n");
815 return ffa_error(FFA_DENIED);
816 }
817
Daniel Boulbya76fd912024-02-22 14:22:15 +0000818 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100819
J-Alves363f5722022-04-25 17:37:37 +0100820 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000821 struct ffa_memory_access *receiver =
822 ffa_memory_region_get_receiver(memory_region, i);
823 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100824 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000825 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100826 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
827 permissions, *orig_from_mode);
828
J-Alves788b4492023-04-18 14:01:23 +0100829 /*
830 * The assumption is that at this point, the operation from
831 * SP to a receiver VM, should have returned an FFA_ERROR
832 * already.
833 */
834 if (!ffa_is_vm_id(from.vm->id)) {
835 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000836 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100837 }
838
J-Alves460d36c2023-10-12 17:02:15 +0100839 /* Track if all senders are from current world. */
840 all_receivers_from_current_world =
841 all_receivers_from_current_world &&
842 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000843 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100844
J-Alves363f5722022-04-25 17:37:37 +0100845 if ((*orig_from_mode & required_from_mode) !=
846 required_from_mode) {
847 dlog_verbose(
848 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100849 "which required mode %#x but only had %#x "
850 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100851 required_from_mode, *orig_from_mode);
852 return ffa_error(FFA_DENIED);
853 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000854 }
855
J-Alves460d36c2023-10-12 17:02:15 +0100856 *map_action = ffa_mem_send_get_map_action(
Daniel Boulby734981e2024-07-22 11:06:35 +0100857 all_receivers_from_current_world, from.vm->id, share_func,
858 (*orig_from_mode & MM_MODE_D) == 0U);
J-Alves460d36c2023-10-12 17:02:15 +0100859
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000860 /* Find the appropriate new mode. */
861 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000862 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000863 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000865 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100866 break;
J-Alves95fbb312024-03-20 15:19:16 +0000867 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000869 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100870 break;
J-Alves95fbb312024-03-20 15:19:16 +0000871 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100872 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000873 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100874 break;
875
Jose Marinho75509b42019-04-09 09:34:59 +0100876 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100877 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100878 }
879
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100880 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000881}
882
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000884 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100885 struct ffa_memory_region_constituent **fragments,
886 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100887 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888{
889 const uint32_t state_mask =
890 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
891 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000893
J-Alves69cdfd92024-04-26 11:40:59 +0100894 assert(map_action != NULL);
895 if (vm_id_is_current_world(from.vm->id)) {
896 *map_action = MAP_ACTION_COMMIT;
897 } else {
898 /*
899 * No need to check the attributes of caller.
900 * The assumption is that the retrieve request of the receiver
901 * also used the MAP_ACTION_NONE, and no update was done to the
902 * page tables. When the receiver is not at the secure virtual
903 * instance SPMC doesn't manage its S2 translation (i.e. when
904 * the receiver is a VM).
905 */
906 *map_action = MAP_ACTION_NONE;
907
908 return (struct ffa_value){.func = FFA_SUCCESS_32};
909 }
910
Andrew Walbranca808b12020-05-15 17:22:28 +0100911 ret = constituents_get_mode(from, orig_from_mode, fragments,
912 fragment_constituent_counts,
913 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100915 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 }
917
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000918 /*
919 * Ensure the relinquishing VM is not the owner but has access to the
920 * memory.
921 */
922 orig_from_state = *orig_from_mode & state_mask;
923 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
924 dlog_verbose(
925 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100926 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000927 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100928 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 }
930
931 /* Find the appropriate new mode. */
932 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
933
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000935}
936
937/**
938 * Verify that all pages have the same mode, that the starting mode
939 * constitutes a valid state and obtain the next mode to apply
940 * to the retrieving VM.
941 *
942 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100943 * 1) FFA_DENIED if a state transition was not found;
944 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100945 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100947 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
949 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000950 */
J-Alvesfc19b372022-07-06 12:17:35 +0100951struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000952 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100953 struct ffa_memory_region_constituent **fragments,
954 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100955 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100956 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000957{
958 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100959 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000960
Andrew Walbranca808b12020-05-15 17:22:28 +0100961 ret = constituents_get_mode(to, &orig_to_mode, fragments,
962 fragment_constituent_counts,
963 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100964 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100965 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100966 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000967 }
968
J-Alves460d36c2023-10-12 17:02:15 +0100969 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100970 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100971
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100972 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000973 /*
974 * If the original ffa memory send call has been processed
975 * successfully, it is expected the orig_to_mode would overlay
976 * with `state_mask`, as a result of the function
977 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100978 *
979 * If Hafnium is the SPMC:
980 * - Caller of the reclaim interface is an SP, the memory shall
981 * have been protected throughout the flow.
982 * - Caller of the reclaim is from the NWd, the memory may have
983 * been protected at the time of lending/donating the memory.
984 * In such case, set action to unprotect memory in the
985 * handling of reclaim operation.
986 * - If Hafnium is the hypervisor memory shall never have been
987 * protected in memory lend/share/donate.
988 *
989 * More details in the doc comment of the function
990 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000991 */
J-Alves59ed0042022-07-28 18:26:41 +0100992 if (vm_id_is_current_world(to.vm->id)) {
993 assert((orig_to_mode &
994 (MM_MODE_INVALID | MM_MODE_UNOWNED |
995 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100996 assert(!memory_protected);
997 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
998 map_action != NULL && memory_protected) {
999 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +01001000 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001001 } else {
J-Alves69cdfd92024-04-26 11:40:59 +01001002 if (!vm_id_is_current_world(to.vm->id)) {
1003 assert(map_action != NULL);
1004 *map_action = MAP_ACTION_NONE;
1005 return (struct ffa_value){.func = FFA_SUCCESS_32};
1006 }
1007
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001008 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001009 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001010 * Ensure the retriever has the expected state. We don't care
1011 * about the MM_MODE_SHARED bit; either with or without it set
1012 * are both valid representations of the !O-NA state.
1013 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001014 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001015 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001016 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1017 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001018 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019 }
J-Alves460d36c2023-10-12 17:02:15 +01001020
1021 /*
1022 * If memory has been protected before, clear the NS bit to
1023 * allow the secure access from the SP.
1024 */
1025 if (memory_protected) {
1026 *to_mode &= ~plat_ffa_other_world_mode();
1027 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 }
1029
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001031 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001032 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001033 *to_mode |= 0;
1034 break;
J-Alves95fbb312024-03-20 15:19:16 +00001035 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001037 *to_mode |= MM_MODE_UNOWNED;
1038 break;
J-Alves95fbb312024-03-20 15:19:16 +00001039 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001041 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1042 break;
1043
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001044 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001045 *to_mode |= 0;
1046 break;
1047
1048 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001049 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001051 }
1052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001053 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001054}
Jose Marinho09b1db82019-08-08 09:16:59 +01001055
J-Alvescf6253e2024-01-03 13:48:48 +00001056/*
1057 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1058 * Returns:
1059 * - FFA_SUCCESS_32: if all goes well.
1060 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1061 * the page table update. Or error code provided by the function
1062 * `arch_memory_protect`.
1063 */
1064static struct ffa_value ffa_region_group_check_actions(
1065 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1066 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1067 bool *memory_protected)
1068{
1069 struct ffa_value ret;
1070 bool is_memory_protected;
1071
1072 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1073 dlog_verbose(
1074 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001075 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001076 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1077 pa_addr(pa_end));
1078 return ffa_error(FFA_NO_MEMORY);
1079 }
1080
1081 switch (action) {
1082 case MAP_ACTION_CHECK:
1083 /* No protect requested. */
1084 is_memory_protected = false;
1085 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1086 break;
1087 case MAP_ACTION_CHECK_PROTECT: {
1088 paddr_t last_protected_pa = pa_init(0);
1089
1090 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1091
1092 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1093
1094 /*
1095 * - If protect memory has failed with FFA_DENIED, means some
1096 * range of memory was in the wrong state. In such case, SPM
1097 * reverts the state of the pages that were successfully
1098 * updated.
1099 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1100 * means the platform doesn't support the protection mechanism.
1101 * That said, it still permits the page table update to go
1102 * through. The variable
1103 * `is_memory_protected` will be equal to false.
1104 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1105 * break from switch and return the error.
1106 */
1107 if (ret.func == FFA_ERROR_32) {
1108 assert(!is_memory_protected);
1109 if (ffa_error_code(ret) == FFA_DENIED &&
1110 pa_addr(last_protected_pa) != (uintptr_t)0) {
1111 CHECK(arch_memory_unprotect(
1112 pa_begin,
1113 pa_add(last_protected_pa, PAGE_SIZE)));
1114 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1115 ret = (struct ffa_value){
1116 .func = FFA_SUCCESS_32,
1117 };
1118 }
1119 }
1120 } break;
1121 default:
1122 panic("%s: invalid action to process %x\n", __func__, action);
1123 }
1124
1125 if (memory_protected != NULL) {
1126 *memory_protected = is_memory_protected;
1127 }
1128
1129 return ret;
1130}
1131
1132static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1133 paddr_t pa_begin, paddr_t pa_end,
1134 struct mpool *ppool, uint32_t mode,
1135 enum ffa_map_action action)
1136{
1137 switch (action) {
1138 case MAP_ACTION_COMMIT_UNPROTECT:
1139 /*
1140 * Checking that it should succeed because SPM should be
1141 * unprotecting memory that it had protected before.
1142 */
1143 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1144 case MAP_ACTION_COMMIT:
1145 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1146 NULL);
1147 break;
1148 default:
1149 panic("%s: invalid action to process %x\n", __func__, action);
1150 }
1151}
1152
Jose Marinho09b1db82019-08-08 09:16:59 +01001153/**
J-Alves063ad832023-10-03 18:05:40 +01001154 * Helper function to revert a failed "Protect" action from the SPMC:
1155 * - `fragment_count`: should specify the number of fragments to traverse from
1156 * `fragments`. This may not be the full amount of fragments that are part of
1157 * the share_state structure.
1158 * - `fragment_constituent_counts`: array holding the amount of constituents
1159 * per fragment.
1160 * - `end`: pointer to the constituent that failed the "protect" action. It
1161 * shall be part of the last fragment, and it shall make the loop below break.
1162 */
1163static void ffa_region_group_fragments_revert_protect(
1164 struct ffa_memory_region_constituent **fragments,
1165 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1166 const struct ffa_memory_region_constituent *end)
1167{
1168 for (uint32_t i = 0; i < fragment_count; ++i) {
1169 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1170 struct ffa_memory_region_constituent *constituent =
1171 &fragments[i][j];
1172 size_t size = constituent->page_count * PAGE_SIZE;
1173 paddr_t pa_begin =
1174 pa_from_ipa(ipa_init(constituent->address));
1175 paddr_t pa_end = pa_add(pa_begin, size);
1176
Karl Meakine8937d92024-03-19 16:04:25 +00001177 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001178 __func__, pa_addr(pa_begin), size);
1179
1180 if (constituent == end) {
1181 /*
1182 * The last constituent is expected to be in the
1183 * last fragment.
1184 */
1185 assert(i == fragment_count - 1);
1186 break;
1187 }
1188
1189 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1190 }
1191 }
1192}
1193
1194/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001195 * Updates a VM's page table such that the given set of physical address ranges
1196 * are mapped in the address space at the corresponding address ranges, in the
1197 * mode provided.
1198 *
J-Alves0a83dc22023-05-05 09:50:37 +01001199 * The enum ffa_map_action determines the action taken from a call to the
1200 * function below:
1201 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1202 * mpool but no mappings will actually be updated. This function must always
1203 * be called first with action set to MAP_ACTION_CHECK to check that it will
1204 * succeed before calling ffa_region_group_identity_map with whichever one of
1205 * the remaining actions, to avoid leaving the page table in a half-updated
1206 * state.
1207 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1208 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001209 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1210 * invocation to the monitor to update the security state of the memory,
1211 * to that of the SPMC.
1212 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1213 * with a call into the monitor, to reset the security state of memory
1214 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001215 * vm_ptable_defrag should always be called after a series of page table
1216 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001217 *
J-Alvescf6253e2024-01-03 13:48:48 +00001218 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1219 * error codes:
1220 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1221 * - FFA_DENIED:
1222 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001223 * made to memory mappings.
1224 */
J-Alvescf6253e2024-01-03 13:48:48 +00001225struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001226 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001227 struct ffa_memory_region_constituent **fragments,
1228 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001229 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1230 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001231{
Andrew Walbranca808b12020-05-15 17:22:28 +01001232 uint32_t i;
1233 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001234 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001235
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001236 if (vm_locked.vm->el0_partition) {
1237 mode |= MM_MODE_USER | MM_MODE_NG;
1238 }
1239
Andrew Walbranca808b12020-05-15 17:22:28 +01001240 /* Iterate over the memory region constituents within each fragment. */
1241 for (i = 0; i < fragment_count; ++i) {
1242 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001243 struct ffa_memory_region_constituent *constituent =
1244 &fragments[i][j];
1245 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001246 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001247 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001249 uint32_t pa_bits =
1250 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001251
1252 /*
1253 * Ensure the requested region falls into system's PA
1254 * range.
1255 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001256 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1257 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001258 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001259 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001260 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001261
J-Alvescf6253e2024-01-03 13:48:48 +00001262 if (action <= MAP_ACTION_CHECK_PROTECT) {
1263 ret = ffa_region_group_check_actions(
1264 vm_locked, pa_begin, pa_end, ppool,
1265 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001266
1267 if (ret.func == FFA_ERROR_32 &&
1268 ffa_error_code(ret) == FFA_DENIED) {
1269 if (memory_protected != NULL) {
1270 assert(!*memory_protected);
1271 }
1272
1273 ffa_region_group_fragments_revert_protect(
1274 fragments,
1275 fragment_constituent_counts,
1276 i + 1, constituent);
1277 break;
1278 }
J-Alvescf6253e2024-01-03 13:48:48 +00001279 } else if (action >= MAP_ACTION_COMMIT &&
1280 action < MAP_ACTION_MAX) {
1281 ffa_region_group_commit_actions(
1282 vm_locked, pa_begin, pa_end, ppool,
1283 mode, action);
1284 ret = (struct ffa_value){
1285 .func = FFA_SUCCESS_32};
1286 } else {
1287 panic("%s: Unknown ffa_map_action.\n",
1288 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001289 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001290 }
1291 }
1292
J-Alvescf6253e2024-01-03 13:48:48 +00001293 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001294}
1295
1296/**
1297 * Clears a region of physical memory by overwriting it with zeros. The data is
1298 * flushed from the cache so the memory has been cleared across the system.
1299 */
J-Alves7db32002021-12-14 14:44:50 +00001300static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1301 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001302{
1303 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001304 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001305 * global mapping of the whole range. Such an approach will limit
1306 * the changes to stage-1 tables and will allow only local
1307 * invalidation.
1308 */
1309 bool ret;
1310 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001311 void *ptr = mm_identity_map(stage1_locked, begin, end,
1312 MM_MODE_W | (extra_mode_attributes &
1313 plat_ffa_other_world_mode()),
1314 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001315 size_t size = pa_difference(begin, end);
1316
1317 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001318 goto fail;
1319 }
1320
1321 memset_s(ptr, size, 0, size);
1322 arch_mm_flush_dcache(ptr, size);
1323 mm_unmap(stage1_locked, begin, end, ppool);
1324
1325 ret = true;
1326 goto out;
1327
1328fail:
1329 ret = false;
1330
1331out:
1332 mm_unlock_stage1(&stage1_locked);
1333
1334 return ret;
1335}
1336
1337/**
1338 * Clears a region of physical memory by overwriting it with zeros. The data is
1339 * flushed from the cache so the memory has been cleared across the system.
1340 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001341static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001342 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001343 struct ffa_memory_region_constituent **fragments,
1344 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1345 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001346{
1347 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001348 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001349 bool ret = false;
1350
1351 /*
1352 * Create a local pool so any freed memory can't be used by another
1353 * thread. This is to ensure each constituent that is mapped can be
1354 * unmapped again afterwards.
1355 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001356 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001357
Andrew Walbranca808b12020-05-15 17:22:28 +01001358 /* Iterate over the memory region constituents within each fragment. */
1359 for (i = 0; i < fragment_count; ++i) {
1360 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001361
J-Alves8457f932023-10-11 16:41:45 +01001362 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001363 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1364 paddr_t begin =
1365 pa_from_ipa(ipa_init(fragments[i][j].address));
1366 paddr_t end = pa_add(begin, size);
1367
J-Alves7db32002021-12-14 14:44:50 +00001368 if (!clear_memory(begin, end, &local_page_pool,
1369 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001370 /*
1371 * api_clear_memory will defrag on failure, so
1372 * no need to do it here.
1373 */
1374 goto out;
1375 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001376 }
1377 }
1378
Jose Marinho09b1db82019-08-08 09:16:59 +01001379 ret = true;
1380
1381out:
1382 mpool_fini(&local_page_pool);
1383 return ret;
1384}
1385
J-Alves5952d942022-12-22 16:03:00 +00001386static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1387 ipaddr_t in_begin, ipaddr_t in_end)
1388{
1389 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1390 ipa_addr(begin) < ipa_addr(in_end)) ||
1391 (ipa_addr(end) <= ipa_addr(in_end) &&
1392 ipa_addr(end) > ipa_addr(in_begin));
1393}
1394
1395/**
1396 * Receives a memory range and looks for overlaps with the remainder
1397 * constituents of the memory share/lend/donate operation. Assumes they are
1398 * passed in order to avoid having to loop over all the elements at each call.
1399 * The function only compares the received memory ranges with those that follow
1400 * within the same fragment, and subsequent fragments from the same operation.
1401 */
1402static bool ffa_memory_check_overlap(
1403 struct ffa_memory_region_constituent **fragments,
1404 const uint32_t *fragment_constituent_counts,
1405 const uint32_t fragment_count, const uint32_t current_fragment,
1406 const uint32_t current_constituent)
1407{
1408 uint32_t i = current_fragment;
1409 uint32_t j = current_constituent;
1410 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1411 const uint32_t current_page_count = fragments[i][j].page_count;
1412 size_t current_size = current_page_count * PAGE_SIZE;
1413 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1414
1415 if (current_size == 0 ||
1416 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001417 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1418 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001419 return false;
1420 }
1421
1422 for (; i < fragment_count; i++) {
1423 j = (i == current_fragment) ? j + 1 : 0;
1424
1425 for (; j < fragment_constituent_counts[i]; j++) {
1426 ipaddr_t begin = ipa_init(fragments[i][j].address);
1427 const uint32_t page_count = fragments[i][j].page_count;
1428 size_t size = page_count * PAGE_SIZE;
1429 ipaddr_t end = ipa_add(begin, size - 1);
1430
1431 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1432 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001433 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001434 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001435 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001436 return false;
1437 }
1438
1439 /*
1440 * Check if current ranges is within begin and end, as
1441 * well as the reverse. This should help optimize the
1442 * loop, and reduce the number of iterations.
1443 */
1444 if (is_memory_range_within(begin, end, current_begin,
1445 current_end) ||
1446 is_memory_range_within(current_begin, current_end,
1447 begin, end)) {
1448 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001449 "Overlapping memory ranges: %#lx - "
1450 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001451 ipa_addr(begin), ipa_addr(end),
1452 ipa_addr(current_begin),
1453 ipa_addr(current_end));
1454 return true;
1455 }
1456 }
1457 }
1458
1459 return false;
1460}
1461
Jose Marinho09b1db82019-08-08 09:16:59 +01001462/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001463 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001464 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001465 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001466 *
1467 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001468 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001470 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001471 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1472 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001476 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001477static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001478 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001479 struct ffa_memory_region_constituent **fragments,
1480 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001481 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001482 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1483 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001484{
Andrew Walbranca808b12020-05-15 17:22:28 +01001485 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001486 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001487 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001488 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001489 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001490 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001491 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001492 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001493 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001494 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001495
1496 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001497 * Make sure constituents are properly aligned to a 64-bit boundary. If
1498 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001499 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001500 for (i = 0; i < fragment_count; ++i) {
1501 if (!is_aligned(fragments[i], 8)) {
1502 dlog_verbose("Constituents not aligned.\n");
1503 return ffa_error(FFA_INVALID_PARAMETERS);
1504 }
J-Alves8f11cde2022-12-21 16:18:22 +00001505 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1506 constituents_total_page_count +=
1507 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001508 if (ffa_memory_check_overlap(
1509 fragments, fragment_constituent_counts,
1510 fragment_count, i, j)) {
1511 return ffa_error(FFA_INVALID_PARAMETERS);
1512 }
J-Alves8f11cde2022-12-21 16:18:22 +00001513 }
1514 }
1515
1516 if (constituents_total_page_count != composite_total_page_count) {
1517 dlog_verbose(
1518 "Composite page count differs from calculated page "
1519 "count from constituents.\n");
1520 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001521 }
1522
1523 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001524 * Check if the state transition is lawful for the sender, ensure that
1525 * all constituents of a memory region being shared are at the same
1526 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001527 */
J-Alves460d36c2023-10-12 17:02:15 +01001528 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001529 from_locked, share_func, memory_region, &orig_from_mode,
1530 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001531 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001532 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001533 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001534 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001535 }
1536
Andrew Walbran37c574e2020-06-03 11:45:46 +01001537 if (orig_from_mode_ret != NULL) {
1538 *orig_from_mode_ret = orig_from_mode;
1539 }
1540
Jose Marinho09b1db82019-08-08 09:16:59 +01001541 /*
1542 * Create a local pool so any freed memory can't be used by another
1543 * thread. This is to ensure the original mapping can be restored if the
1544 * clear fails.
1545 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001546 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001547
1548 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001549 * First reserve all required memory for the new page table entries
1550 * without committing, to make sure the entire operation will succeed
1551 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001552 * Provide the map_action as populated by 'ffa_send_check_transition'.
1553 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001554 */
J-Alvescf6253e2024-01-03 13:48:48 +00001555 ret = ffa_region_group_identity_map(
1556 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001557 fragment_count, from_mode, page_pool, map_action,
1558 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001559 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001560 goto out;
1561 }
1562
1563 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001564 * Update the mapping for the sender. This won't allocate because the
1565 * transaction was already prepared above, but may free pages in the
1566 * case that a whole block is being unmapped that was previously
1567 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001568 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001570 from_locked, fragments, fragment_constituent_counts,
1571 fragment_count, from_mode, &local_page_pool,
1572 MAP_ACTION_COMMIT, NULL)
1573 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001574
J-Alves460d36c2023-10-12 17:02:15 +01001575 /*
1576 * If memory has been protected, it is now part of the secure PAS
1577 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1578 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1579 * SPM's S1 translation.
1580 * In case memory hasn't been protected, and it is in the non-secure
1581 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1582 * perform a non-secure memory access. In such case `clean_mode` takes
1583 * the same mode as `orig_from_mode`.
1584 */
1585 clean_mode = (memory_protected != NULL && *memory_protected)
1586 ? orig_from_mode & ~plat_ffa_other_world_mode()
1587 : orig_from_mode;
1588
Jose Marinho09b1db82019-08-08 09:16:59 +01001589 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001590 if (clear && !ffa_clear_memory_constituents(
1591 clean_mode, fragments, fragment_constituent_counts,
1592 fragment_count, page_pool)) {
1593 map_action = (memory_protected != NULL && *memory_protected)
1594 ? MAP_ACTION_COMMIT_UNPROTECT
1595 : MAP_ACTION_COMMIT;
1596
Jose Marinho09b1db82019-08-08 09:16:59 +01001597 /*
1598 * On failure, roll back by returning memory to the sender. This
1599 * may allocate pages which were previously freed into
1600 * `local_page_pool` by the call above, but will never allocate
1601 * more pages than that so can never fail.
1602 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001603 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001604 from_locked, fragments,
1605 fragment_constituent_counts, fragment_count,
1606 orig_from_mode, &local_page_pool,
1607 MAP_ACTION_COMMIT, NULL)
1608 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001609 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001610 goto out;
1611 }
1612
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001614
1615out:
1616 mpool_fini(&local_page_pool);
1617
1618 /*
1619 * Tidy up the page table by reclaiming failed mappings (if there was an
1620 * error) or merging entries into blocks where possible (on success).
1621 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001622 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001623
1624 return ret;
1625}
1626
1627/**
1628 * Validates and maps memory shared from one VM to another.
1629 *
1630 * This function requires the calling context to hold the <to> lock.
1631 *
1632 * Returns:
1633 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001634 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001635 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001637 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001638 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001639 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001640struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001641 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001642 struct ffa_memory_region_constituent **fragments,
1643 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001644 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001645 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001646{
Andrew Walbranca808b12020-05-15 17:22:28 +01001647 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648 uint32_t to_mode;
1649 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001650 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001651 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001652
1653 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001654 * Make sure constituents are properly aligned to a 64-bit boundary. If
1655 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001657 for (i = 0; i < fragment_count; ++i) {
1658 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001659 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001660 return ffa_error(FFA_INVALID_PARAMETERS);
1661 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001662 }
1663
1664 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001665 * Ensure the sender has write permissions if the memory needs to be
1666 * cleared.
1667 */
1668 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1669 dlog_verbose(
1670 "Cannot zero memory when the sender does not have "
1671 "write access\n");
1672 return ffa_error(FFA_DENIED);
1673 }
1674
1675 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001676 * Check if the state transition is lawful for the recipient, and ensure
1677 * that all constituents of the memory region being retrieved are at the
1678 * same state.
1679 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001680 ret = ffa_retrieve_check_transition(
1681 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001682 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1683 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001684
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001685 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001686 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001687 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001688 }
1689
1690 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001691 * Create a local pool so any freed memory can't be used by
1692 * another thread. This is to ensure the original mapping can be
1693 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001694 */
1695 mpool_init_with_fallback(&local_page_pool, page_pool);
1696
1697 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001698 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1699 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001700 */
J-Alves69cdfd92024-04-26 11:40:59 +01001701 if (map_action != MAP_ACTION_NONE) {
1702 /*
1703 * First reserve all required memory for the new page table
1704 * entries in the recipient page tables without committing, to
1705 * make sure the entire operation will succeed without
1706 * exhausting the page pool.
1707 */
1708 ret = ffa_region_group_identity_map(
1709 to_locked, fragments, fragment_constituent_counts,
1710 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1711 NULL);
1712 if (ret.func == FFA_ERROR_32) {
1713 /* TODO: partial defrag of failed range. */
1714 goto out;
1715 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001716 }
1717
1718 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001719 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001720 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1721 fragment_constituent_counts,
1722 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001723 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001724 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001725 goto out;
1726 }
1727
J-Alves69cdfd92024-04-26 11:40:59 +01001728 if (map_action != MAP_ACTION_NONE) {
1729 /*
1730 * Complete the transfer by mapping the memory into the
1731 * recipient. This won't allocate because the transaction was
1732 * already prepared above, so it doesn't need to use the
1733 * `local_page_pool`.
1734 */
1735 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1736 fragment_constituent_counts,
1737 fragment_count, to_mode,
1738 page_pool, map_action, NULL)
1739 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001740
J-Alves69cdfd92024-04-26 11:40:59 +01001741 /*
1742 * Return the mode used in mapping the memory in retriever's PT.
1743 */
1744 if (response_mode != NULL) {
1745 *response_mode = to_mode;
1746 }
J-Alves460d36c2023-10-12 17:02:15 +01001747 }
1748
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001749 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001750
1751out:
1752 mpool_fini(&local_page_pool);
1753
1754 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001755 * Tidy up the page table by reclaiming failed mappings (if there was an
1756 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001757 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001758 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001759
1760 return ret;
1761}
1762
Andrew Walbran996d1d12020-05-27 14:08:43 +01001763static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001764 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001765 struct ffa_memory_region_constituent **fragments,
1766 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001767 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001768{
1769 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001770 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001771 uint32_t from_mode;
1772 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001773 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001774 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001775
Andrew Walbranca808b12020-05-15 17:22:28 +01001776 ret = ffa_relinquish_check_transition(
1777 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001778 fragment_constituent_counts, fragment_count, &from_mode,
1779 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001780 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001781 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001782 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001783 }
1784
1785 /*
1786 * Create a local pool so any freed memory can't be used by another
1787 * thread. This is to ensure the original mapping can be restored if the
1788 * clear fails.
1789 */
1790 mpool_init_with_fallback(&local_page_pool, page_pool);
1791
J-Alves69cdfd92024-04-26 11:40:59 +01001792 if (map_action != MAP_ACTION_NONE) {
1793 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001794
J-Alves69cdfd92024-04-26 11:40:59 +01001795 /*
1796 * First reserve all required memory for the new page table
1797 * entries without committing, to make sure the entire operation
1798 * will succeed without exhausting the page pool.
1799 */
1800 ret = ffa_region_group_identity_map(
1801 from_locked, fragments, fragment_constituent_counts,
1802 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1803 NULL);
1804 if (ret.func == FFA_ERROR_32) {
1805 goto out;
1806 }
1807
1808 /*
1809 * Update the mapping for the sender. This won't allocate
1810 * because the transaction was already prepared above, but may
1811 * free pages in the case that a whole block is being unmapped
1812 * that was previously partially mapped.
1813 */
1814 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1815 fragment_constituent_counts,
1816 fragment_count, from_mode,
1817 &local_page_pool,
1818 MAP_ACTION_COMMIT, NULL)
1819 .func == FFA_SUCCESS_32);
1820 } else {
1821 /*
1822 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1823 * were not updated on retrieve/relinquish. These were updating
1824 * only the `share_state` structures. As such, use the sender's
1825 * original mode.
1826 */
1827 clearing_mode = sender_orig_mode;
1828 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001829
1830 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001831 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001832 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001833 fragment_constituent_counts,
1834 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001835 if (map_action != MAP_ACTION_NONE) {
1836 /*
1837 * On failure, roll back by returning memory to the
1838 * sender. This may allocate pages which were previously
1839 * freed into `local_page_pool` by the call above, but
1840 * will never allocate more pages than that so can never
1841 * fail.
1842 */
1843 CHECK(ffa_region_group_identity_map(
1844 from_locked, fragments,
1845 fragment_constituent_counts,
1846 fragment_count, orig_from_mode,
1847 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1848 .func == FFA_SUCCESS_32);
1849 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001850 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001851 goto out;
1852 }
1853
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001854 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001855
1856out:
1857 mpool_fini(&local_page_pool);
1858
1859 /*
1860 * Tidy up the page table by reclaiming failed mappings (if there was an
1861 * error) or merging entries into blocks where possible (on success).
1862 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001863 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001864
1865 return ret;
1866}
1867
1868/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001869 * Complete a memory sending operation by checking that it is valid, updating
1870 * the sender page table, and then either marking the share state as having
1871 * completed sending (on success) or freeing it (on failure).
1872 *
1873 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1874 */
J-Alvesfdd29272022-07-19 13:16:31 +01001875struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001876 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001877 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1878 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001879{
1880 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001881 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001882 struct ffa_value ret;
1883
1884 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001885 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001886 assert(memory_region != NULL);
1887 composite = ffa_memory_region_get_composite(memory_region, 0);
1888 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001889
1890 /* Check that state is valid in sender page table and update. */
1891 ret = ffa_send_check_update(
1892 from_locked, share_state->fragments,
1893 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001894 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001895 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001896 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001897 if (ret.func != FFA_SUCCESS_32) {
1898 /*
1899 * Free share state, it failed to send so it can't be retrieved.
1900 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001901 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1902 __func__, ffa_func_name(ret.func),
1903 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001904 share_state_free(share_states, share_state, page_pool);
1905 return ret;
1906 }
1907
1908 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001909 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001910
J-Alvesee68c542020-10-29 17:48:20 +00001911 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001912}
1913
1914/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001915 * Check that the memory attributes match Hafnium expectations.
1916 * Cacheability:
1917 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1918 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1919 *
1920 * Shareability:
1921 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001922 */
1923static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001924 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001925{
1926 enum ffa_memory_type memory_type;
1927 enum ffa_memory_cacheability cacheability;
1928 enum ffa_memory_shareability shareability;
1929
Karl Meakin84710f32023-10-12 15:14:49 +01001930 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001931 cacheability = attributes.cacheability;
1932 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1933 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1934 dlog_verbose(
1935 "Normal Memory: Invalid cacheability %s, "
1936 "expected %s.\n",
1937 ffa_memory_cacheability_name(cacheability),
1938 ffa_memory_cacheability_name(
1939 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001940 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001941 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001942 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1943 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1944 dlog_verbose(
1945 "Device Memory: Invalid cacheability %s, "
1946 "expected %s.\n",
1947 ffa_device_memory_cacheability_name(cacheability),
1948 ffa_device_memory_cacheability_name(
1949 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001950 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001951 }
1952
Karl Meakin84710f32023-10-12 15:14:49 +01001953 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001954 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001955 dlog_verbose("Invalid shareability %s, expected %s.\n",
1956 ffa_memory_shareability_name(shareability),
1957 ffa_memory_shareability_name(
1958 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001959 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001960 }
1961
1962 return (struct ffa_value){.func = FFA_SUCCESS_32};
1963}
1964
1965/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001966 * Check that the given `memory_region` represents a valid memory send request
1967 * of the given `share_func` type, return the clear flag and permissions via the
1968 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001969 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001970 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001971 * not.
1972 */
J-Alves66652252022-07-06 09:49:51 +01001973struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001974 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1975 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001976 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001977{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001978 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001979 struct ffa_memory_access *receiver =
1980 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001981 uint64_t receivers_end;
1982 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001983 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001984 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001985 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001986 enum ffa_data_access data_access;
1987 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001988 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001989 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001990 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001991 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001992 memory_region->receivers_offset +
1993 memory_region->memory_access_desc_size +
1994 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001995
1996 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001997 dlog_verbose("Fragment length %u too short (min %zu).\n",
1998 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001999 return ffa_error(FFA_INVALID_PARAMETERS);
2000 }
2001
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002002 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
2003 "struct ffa_memory_region_constituent must be 16 bytes");
2004 if (!is_aligned(fragment_length,
2005 sizeof(struct ffa_memory_region_constituent)) ||
2006 !is_aligned(memory_share_length,
2007 sizeof(struct ffa_memory_region_constituent))) {
2008 dlog_verbose(
2009 "Fragment length %u or total length %u"
2010 " is not 16-byte aligned.\n",
2011 fragment_length, memory_share_length);
2012 return ffa_error(FFA_INVALID_PARAMETERS);
2013 }
2014
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002015 if (fragment_length > memory_share_length) {
2016 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002017 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002018 (size_t)fragment_length, (size_t)memory_share_length);
2019 return ffa_error(FFA_INVALID_PARAMETERS);
2020 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002021
J-Alves95df0ef2022-12-07 10:09:48 +00002022 /* The sender must match the caller. */
2023 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2024 vm_id_is_current_world(memory_region->sender)) ||
2025 (vm_id_is_current_world(from_locked.vm->id) &&
2026 memory_region->sender != from_locked.vm->id)) {
2027 dlog_verbose("Invalid memory sender ID.\n");
2028 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002029 }
2030
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002031 if (memory_region->receiver_count <= 0) {
2032 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002033 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002034 }
2035
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002036 /*
2037 * Ensure that the composite header is within the memory bounds and
2038 * doesn't overlap the first part of the message. Cast to uint64_t
2039 * to prevent overflow.
2040 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002041 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002042 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002043 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002044 min_length = receivers_end +
2045 sizeof(struct ffa_composite_memory_region) +
2046 sizeof(struct ffa_memory_region_constituent);
2047 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002048 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002049 (size_t)memory_share_length, (size_t)min_length);
2050 return ffa_error(FFA_INVALID_PARAMETERS);
2051 }
2052
2053 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002054 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002055
2056 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002057 * Check that the composite memory region descriptor is after the access
2058 * descriptors, is at least 16-byte aligned, and fits in the first
2059 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002060 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002061 if ((composite_memory_region_offset < receivers_end) ||
2062 (composite_memory_region_offset % 16 != 0) ||
2063 (composite_memory_region_offset >
2064 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2065 dlog_verbose(
2066 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002067 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002068 (size_t)composite_memory_region_offset);
2069 return ffa_error(FFA_INVALID_PARAMETERS);
2070 }
2071
2072 /*
2073 * Compute the start of the constituent regions. Already checked
2074 * to be not more than fragment_length and thus not more than
2075 * memory_share_length.
2076 */
2077 constituents_start = composite_memory_region_offset +
2078 sizeof(struct ffa_composite_memory_region);
2079 constituents_length = memory_share_length - constituents_start;
2080
2081 /*
2082 * Check that the number of constituents is consistent with the length
2083 * of the constituent region.
2084 */
2085 composite = ffa_memory_region_get_composite(memory_region, 0);
2086 if ((constituents_length %
2087 sizeof(struct ffa_memory_region_constituent) !=
2088 0) ||
2089 ((constituents_length /
2090 sizeof(struct ffa_memory_region_constituent)) !=
2091 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002092 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002093 (size_t)memory_share_length,
2094 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002095 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002096 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002097 if (fragment_length < memory_share_length &&
2098 fragment_length < HF_MAILBOX_SIZE) {
2099 dlog_warning(
2100 "Initial fragment length %d smaller than mailbox "
2101 "size.\n",
2102 fragment_length);
2103 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002104
Andrew Walbrana65a1322020-04-06 19:32:32 +01002105 /*
2106 * Clear is not allowed for memory sharing, as the sender still has
2107 * access to the memory.
2108 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002109 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002110 (share_func == FFA_MEM_SHARE_32 ||
2111 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002112 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002113 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002114 }
2115
2116 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002117 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002118 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002119 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002120 }
2121
J-Alves363f5722022-04-25 17:37:37 +01002122 /* Check that the permissions are valid, for each specified receiver. */
2123 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002124 struct ffa_memory_region_attributes receiver_permissions;
2125
2126 receiver = ffa_memory_region_get_receiver(memory_region, i);
2127 assert(receiver != NULL);
2128 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002129 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002130 receiver_permissions.permissions;
2131 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002132
2133 if (memory_region->sender == receiver_id) {
2134 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002135 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002136 }
Federico Recanati85090c42021-12-15 13:17:54 +01002137
J-Alves363f5722022-04-25 17:37:37 +01002138 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2139 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002140 struct ffa_memory_access *other_receiver =
2141 ffa_memory_region_get_receiver(memory_region,
2142 j);
2143 assert(other_receiver != NULL);
2144
J-Alves363f5722022-04-25 17:37:37 +01002145 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002146 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002147 dlog_verbose(
2148 "Repeated receiver(%x) in memory send "
2149 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002150 other_receiver->receiver_permissions
2151 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002152 return ffa_error(FFA_INVALID_PARAMETERS);
2153 }
2154 }
2155
2156 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002157 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002158 dlog_verbose(
2159 "All ffa_memory_access should point to the "
2160 "same composite memory region offset.\n");
2161 return ffa_error(FFA_INVALID_PARAMETERS);
2162 }
2163
Karl Meakin84710f32023-10-12 15:14:49 +01002164 data_access = permissions.data_access;
2165 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002166 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2167 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2168 dlog_verbose(
2169 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002170 "(data_access = %s, instruction_access = %s)\n",
2171 ffa_data_access_name(data_access),
2172 ffa_instruction_access_name(
2173 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002174 return ffa_error(FFA_INVALID_PARAMETERS);
2175 }
2176 if (instruction_access !=
2177 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2178 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002179 "Invalid instruction access permissions %s "
2180 "for sending memory, expected %s.\n",
2181 ffa_instruction_access_name(instruction_access),
2182 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002183 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002184 return ffa_error(FFA_INVALID_PARAMETERS);
2185 }
J-Alves95fbb312024-03-20 15:19:16 +00002186 if (share_func == FFA_MEM_SHARE_32 ||
2187 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002188 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2189 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002190 "Invalid data access permissions %s "
2191 "for sharing memory, expected %s.\n",
2192 ffa_data_access_name(data_access),
2193 ffa_data_access_name(
2194 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002195 return ffa_error(FFA_INVALID_PARAMETERS);
2196 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002197 /*
2198 * According to section 10.10.3 of the FF-A v1.1 EAC0
2199 * spec, NX is required for share operations (but must
2200 * not be specified by the sender) so set it in the
2201 * copy that we store, ready to be returned to the
2202 * retriever.
2203 */
2204 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002205 permissions.instruction_access =
2206 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002207 receiver_permissions.permissions = permissions;
2208 }
J-Alves363f5722022-04-25 17:37:37 +01002209 }
J-Alves95fbb312024-03-20 15:19:16 +00002210 if ((share_func == FFA_MEM_LEND_32 ||
2211 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002212 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2213 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002214 "Invalid data access permissions %s for "
2215 "lending memory, expected %s.\n",
2216 ffa_data_access_name(data_access),
2217 ffa_data_access_name(
2218 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002219 return ffa_error(FFA_INVALID_PARAMETERS);
2220 }
2221
J-Alves95fbb312024-03-20 15:19:16 +00002222 if ((share_func == FFA_MEM_DONATE_32 ||
2223 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002224 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2225 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002226 "Invalid data access permissions %s for "
2227 "donating memory, expected %s.\n",
2228 ffa_data_access_name(data_access),
2229 ffa_data_access_name(
2230 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002231 return ffa_error(FFA_INVALID_PARAMETERS);
2232 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 }
2234
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002235 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002236 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002237 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2238 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002239 "Invalid security state %s for memory share operation, "
2240 "expected %s.\n",
2241 ffa_memory_security_name(security_state),
2242 ffa_memory_security_name(
2243 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002244 return ffa_error(FFA_INVALID_PARAMETERS);
2245 }
2246
Federico Recanatid937f5e2021-12-20 17:38:23 +01002247 /*
J-Alves807794e2022-06-16 13:42:47 +01002248 * If a memory donate or lend with single borrower, the memory type
2249 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002250 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002251 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002252 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002253 share_func == FFA_MEM_DONATE_64 ||
2254 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002255 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002256 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002257 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002258 "Invalid memory type %s for memory share "
2259 "operation, expected %s.\n",
2260 ffa_memory_type_name(type),
2261 ffa_memory_type_name(
2262 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002263 return ffa_error(FFA_INVALID_PARAMETERS);
2264 }
2265 } else {
2266 /*
2267 * Check that sender's memory attributes match Hafnium
2268 * expectations: Normal Memory, Inner shareable, Write-Back
2269 * Read-Allocate Write-Allocate Cacheable.
2270 */
2271 ret = ffa_memory_attributes_validate(memory_region->attributes);
2272 if (ret.func != FFA_SUCCESS_32) {
2273 return ret;
2274 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002275 }
2276
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002277 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002278}
2279
2280/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002281 * Gets the share state for continuing an operation to donate, lend or share
2282 * memory, and checks that it is a valid request.
2283 *
2284 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2285 * not.
2286 */
J-Alvesfdd29272022-07-19 13:16:31 +01002287struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002288 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002289 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002290 struct mpool *page_pool)
2291{
2292 struct ffa_memory_share_state *share_state;
2293 struct ffa_memory_region *memory_region;
2294
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002295 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002296
2297 /*
2298 * Look up the share state by handle and make sure that the VM ID
2299 * matches.
2300 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002301 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002302 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002303 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002304 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002305 handle);
2306 return ffa_error(FFA_INVALID_PARAMETERS);
2307 }
2308 memory_region = share_state->memory_region;
2309
J-Alvesfdd29272022-07-19 13:16:31 +01002310 if (vm_id_is_current_world(from_vm_id) &&
2311 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002312 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2313 return ffa_error(FFA_INVALID_PARAMETERS);
2314 }
2315
2316 if (share_state->sending_complete) {
2317 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002318 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002319 handle);
2320 return ffa_error(FFA_INVALID_PARAMETERS);
2321 }
2322
2323 if (share_state->fragment_count == MAX_FRAGMENTS) {
2324 /*
2325 * Log a warning as this is a sign that MAX_FRAGMENTS should
2326 * probably be increased.
2327 */
2328 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002329 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002330 "only %d supported.\n",
2331 handle, MAX_FRAGMENTS);
2332 /* Free share state, as it's not possible to complete it. */
2333 share_state_free(share_states, share_state, page_pool);
2334 return ffa_error(FFA_NO_MEMORY);
2335 }
2336
2337 *share_state_ret = share_state;
2338
2339 return (struct ffa_value){.func = FFA_SUCCESS_32};
2340}
2341
2342/**
J-Alves95df0ef2022-12-07 10:09:48 +00002343 * Checks if there is at least one receiver from the other world.
2344 */
J-Alvesfdd29272022-07-19 13:16:31 +01002345bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002346 struct ffa_memory_region *memory_region)
2347{
2348 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002349 struct ffa_memory_access *receiver =
2350 ffa_memory_region_get_receiver(memory_region, i);
2351 assert(receiver != NULL);
2352 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2353
2354 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002355 return true;
2356 }
2357 }
2358 return false;
2359}
2360
2361/**
J-Alves9da280b2022-12-21 14:55:39 +00002362 * Validates a call to donate, lend or share memory in which Hafnium is the
2363 * designated allocator of the memory handle. In practice, this also means
2364 * Hafnium is responsible for managing the state structures for the transaction.
2365 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2366 * sender is an SP or there is at least one borrower that is an SP.
2367 * If Hafnium is the hypervisor, it should allocate the memory handle when
2368 * operation involves only NWd VMs.
2369 *
2370 * If validation goes well, Hafnium updates the stage-2 page tables of the
2371 * sender. Validation consists of checking if the message length and number of
2372 * memory region constituents match, and if the transition is valid for the
2373 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002374 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002375 * Assumes that the caller has already found and locked the sender VM and copied
2376 * the memory region descriptor from the sender's TX buffer to a freshly
2377 * allocated page from Hafnium's internal pool. The caller must have also
2378 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002379 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002380 * This function takes ownership of the `memory_region` passed in and will free
2381 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002382 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002383struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002384 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002385 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002386 uint32_t fragment_length, uint32_t share_func,
2387 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002388{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002389 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002390 struct share_states_locked share_states;
2391 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002392
2393 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002394 * If there is an error validating the `memory_region` then we need to
2395 * free it because we own it but we won't be storing it in a share state
2396 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002397 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002398 ret = ffa_memory_send_validate(from_locked, memory_region,
2399 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002400 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002401 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002402 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002403 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002404 }
2405
Andrew Walbrana65a1322020-04-06 19:32:32 +01002406 /* Set flag for share function, ready to be retrieved later. */
2407 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002408 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002409 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002410 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002411 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002412 break;
J-Alves95fbb312024-03-20 15:19:16 +00002413 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002414 case FFA_MEM_LEND_32:
2415 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002416 break;
J-Alves95fbb312024-03-20 15:19:16 +00002417 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002418 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002419 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002420 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002421 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002422 default:
2423 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2424 ffa_func_name(share_func));
2425 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002426 }
2427
Andrew Walbranca808b12020-05-15 17:22:28 +01002428 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002429 /*
2430 * Allocate a share state before updating the page table. Otherwise if
2431 * updating the page table succeeded but allocating the share state
2432 * failed then it would leave the memory in a state where nobody could
2433 * get it back.
2434 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002435 share_state = allocate_share_state(share_states, share_func,
2436 memory_region, fragment_length,
2437 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002438 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002439 dlog_verbose("Failed to allocate share state.\n");
2440 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002441 ret = ffa_error(FFA_NO_MEMORY);
2442 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002443 }
2444
Andrew Walbranca808b12020-05-15 17:22:28 +01002445 if (fragment_length == memory_share_length) {
2446 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002447 ret = ffa_memory_send_complete(
2448 from_locked, share_states, share_state, page_pool,
2449 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002450 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002451 /*
2452 * Use sender ID from 'memory_region' assuming
2453 * that at this point it has been validated:
2454 * - MBZ at virtual FF-A instance.
2455 */
J-Alves19e20cf2023-08-02 12:48:55 +01002456 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002457 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2458 ? memory_region->sender
2459 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002460 ret = (struct ffa_value){
2461 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002462 .arg1 = (uint32_t)memory_region->handle,
2463 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002464 .arg3 = fragment_length,
2465 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002466 }
2467
2468out:
2469 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002470 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002471 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002472}
2473
2474/**
J-Alves8505a8a2022-06-15 18:10:18 +01002475 * Continues an operation to donate, lend or share memory to a VM from current
2476 * world. If this is the last fragment then checks that the transition is valid
2477 * for the type of memory sending operation and updates the stage-2 page tables
2478 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002479 *
2480 * Assumes that the caller has already found and locked the sender VM and copied
2481 * the memory region descriptor from the sender's TX buffer to a freshly
2482 * allocated page from Hafnium's internal pool.
2483 *
2484 * This function takes ownership of the `fragment` passed in; it must not be
2485 * freed by the caller.
2486 */
2487struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2488 void *fragment,
2489 uint32_t fragment_length,
2490 ffa_memory_handle_t handle,
2491 struct mpool *page_pool)
2492{
2493 struct share_states_locked share_states = share_states_lock();
2494 struct ffa_memory_share_state *share_state;
2495 struct ffa_value ret;
2496 struct ffa_memory_region *memory_region;
2497
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002498 CHECK(is_aligned(fragment,
2499 alignof(struct ffa_memory_region_constituent)));
2500 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2501 0) {
2502 dlog_verbose("Fragment length %u misaligned.\n",
2503 fragment_length);
2504 ret = ffa_error(FFA_INVALID_PARAMETERS);
2505 goto out_free_fragment;
2506 }
2507
Andrew Walbranca808b12020-05-15 17:22:28 +01002508 ret = ffa_memory_send_continue_validate(share_states, handle,
2509 &share_state,
2510 from_locked.vm->id, page_pool);
2511 if (ret.func != FFA_SUCCESS_32) {
2512 goto out_free_fragment;
2513 }
2514 memory_region = share_state->memory_region;
2515
J-Alves95df0ef2022-12-07 10:09:48 +00002516 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002517 dlog_error(
2518 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002519 "other world. This should never happen, and indicates "
2520 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002521 "EL3 code.\n");
2522 ret = ffa_error(FFA_INVALID_PARAMETERS);
2523 goto out_free_fragment;
2524 }
2525
2526 /* Add this fragment. */
2527 share_state->fragments[share_state->fragment_count] = fragment;
2528 share_state->fragment_constituent_counts[share_state->fragment_count] =
2529 fragment_length / sizeof(struct ffa_memory_region_constituent);
2530 share_state->fragment_count++;
2531
2532 /* Check whether the memory send operation is now ready to complete. */
2533 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002534 ret = ffa_memory_send_complete(
2535 from_locked, share_states, share_state, page_pool,
2536 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002537 } else {
2538 ret = (struct ffa_value){
2539 .func = FFA_MEM_FRAG_RX_32,
2540 .arg1 = (uint32_t)handle,
2541 .arg2 = (uint32_t)(handle >> 32),
2542 .arg3 = share_state_next_fragment_offset(share_states,
2543 share_state)};
2544 }
2545 goto out;
2546
2547out_free_fragment:
2548 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002549
2550out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002551 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002552 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553}
2554
Andrew Walbranca808b12020-05-15 17:22:28 +01002555/** Clean up after the receiver has finished retrieving a memory region. */
2556static void ffa_memory_retrieve_complete(
2557 struct share_states_locked share_states,
2558 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2559{
J-Alves95fbb312024-03-20 15:19:16 +00002560 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2561 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002562 /*
2563 * Memory that has been donated can't be relinquished,
2564 * so no need to keep the share state around.
2565 */
2566 share_state_free(share_states, share_state, page_pool);
2567 dlog_verbose("Freed share state for donate.\n");
2568 }
2569}
2570
J-Alves2d8457f2022-10-05 11:06:41 +01002571/**
2572 * Initialises the given memory region descriptor to be used for an
2573 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2574 * fragment.
2575 * The memory region descriptor is initialized according to retriever's
2576 * FF-A version.
2577 *
2578 * Returns true on success, or false if the given constituents won't all fit in
2579 * the first fragment.
2580 */
2581static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002582 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002583 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002584 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002585 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002586 struct ffa_memory_access *receivers, size_t receiver_count,
2587 uint32_t memory_access_desc_size, uint32_t page_count,
2588 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002589 const struct ffa_memory_region_constituent constituents[],
2590 uint32_t fragment_constituent_count, uint32_t *total_length,
2591 uint32_t *fragment_length)
2592{
2593 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002594 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002595 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002596 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002597
2598 assert(response != NULL);
2599
Karl Meakin0e617d92024-04-05 12:55:22 +01002600 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002601 struct ffa_memory_region_v1_0 *retrieve_response =
2602 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002603 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002604
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002605 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2606 attributes, flags, handle, 0,
2607 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002608
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002609 receiver = (struct ffa_memory_access_v1_0 *)
2610 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002611 receiver_count = retrieve_response->receiver_count;
2612
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002613 for (uint32_t i = 0; i < receiver_count; i++) {
2614 ffa_id_t receiver_id =
2615 receivers[i].receiver_permissions.receiver;
2616 ffa_memory_receiver_flags_t recv_flags =
2617 receivers[i].receiver_permissions.flags;
2618
2619 /*
2620 * Initialized here as in memory retrieve responses we
2621 * currently expect one borrower to be specified.
2622 */
2623 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002624 receiver, receiver_id, permissions.data_access,
2625 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002626 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002627
2628 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002629 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002630 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2631 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002632
2633 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2634 retrieve_response, 0);
2635 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002636 struct ffa_memory_region *retrieve_response =
2637 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002638 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002639
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002640 ffa_memory_region_init_header(
2641 retrieve_response, sender, attributes, flags, handle, 0,
2642 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002643
2644 /*
2645 * Note that `sizeof(struct_ffa_memory_region)` and
2646 * `sizeof(struct ffa_memory_access)` must both be multiples of
2647 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2648 * guaranteed that the offset we calculate here is aligned to a
2649 * 64-bit boundary and so 64-bit values can be copied without
2650 * alignment faults.
2651 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002652 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002653 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002654 (uint32_t)(receiver_count *
2655 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002656
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002657 retrieve_response_receivers =
2658 ffa_memory_region_get_receiver(retrieve_response, 0);
2659 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002660
2661 /*
2662 * Initialized here as in memory retrieve responses we currently
2663 * expect one borrower to be specified.
2664 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002665 memcpy_s(retrieve_response_receivers,
2666 sizeof(struct ffa_memory_access) * receiver_count,
2667 receivers,
2668 sizeof(struct ffa_memory_access) * receiver_count);
2669
2670 retrieve_response_receivers->composite_memory_region_offset =
2671 composite_offset;
2672
J-Alves2d8457f2022-10-05 11:06:41 +01002673 composite_memory_region =
2674 ffa_memory_region_get_composite(retrieve_response, 0);
2675 }
2676
J-Alves2d8457f2022-10-05 11:06:41 +01002677 assert(composite_memory_region != NULL);
2678
J-Alves2d8457f2022-10-05 11:06:41 +01002679 composite_memory_region->page_count = page_count;
2680 composite_memory_region->constituent_count = total_constituent_count;
2681 composite_memory_region->reserved_0 = 0;
2682
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002683 constituents_offset =
2684 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002685 if (constituents_offset +
2686 fragment_constituent_count *
2687 sizeof(struct ffa_memory_region_constituent) >
2688 response_max_size) {
2689 return false;
2690 }
2691
2692 for (i = 0; i < fragment_constituent_count; ++i) {
2693 composite_memory_region->constituents[i] = constituents[i];
2694 }
2695
2696 if (total_length != NULL) {
2697 *total_length =
2698 constituents_offset +
2699 composite_memory_region->constituent_count *
2700 sizeof(struct ffa_memory_region_constituent);
2701 }
2702 if (fragment_length != NULL) {
2703 *fragment_length =
2704 constituents_offset +
2705 fragment_constituent_count *
2706 sizeof(struct ffa_memory_region_constituent);
2707 }
2708
2709 return true;
2710}
2711
J-Alves96de29f2022-04-26 16:05:24 +01002712/**
2713 * Validates the retrieved permissions against those specified by the lender
2714 * of memory share operation. Optionally can help set the permissions to be used
2715 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002716 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2717 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2718 * specification for each ABI.
2719 * - FFA_DENIED -> if the permissions specified by the retriever are not
2720 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002721 */
J-Alvesdcad8992023-09-15 14:10:35 +01002722static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2723 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002724 enum ffa_data_access requested_data_access,
2725 enum ffa_instruction_access sent_instruction_access,
2726 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002727 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002728{
2729 switch (sent_data_access) {
2730 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2731 case FFA_DATA_ACCESS_RW:
2732 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2733 requested_data_access == FFA_DATA_ACCESS_RW) {
2734 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002735 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002736 }
2737 break;
2738 }
2739 /* Intentional fall-through. */
2740 case FFA_DATA_ACCESS_RO:
2741 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2742 requested_data_access == FFA_DATA_ACCESS_RO) {
2743 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002744 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002745 }
2746 break;
2747 }
2748 dlog_verbose(
2749 "Invalid data access requested; sender specified "
2750 "permissions %#x but receiver requested %#x.\n",
2751 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002752 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002753 case FFA_DATA_ACCESS_RESERVED:
2754 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2755 "checked before this point.");
2756 }
2757
J-Alvesdcad8992023-09-15 14:10:35 +01002758 /*
2759 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2760 * or FFA_MEMORY_DONATE the retriever should have specifed the
2761 * instruction permissions it wishes to receive.
2762 */
2763 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002764 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002765 case FFA_MEM_SHARE_32:
2766 if (requested_instruction_access !=
2767 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2768 dlog_verbose(
2769 "%s: for share instruction permissions must "
2770 "NOT be specified.\n",
2771 __func__);
2772 return ffa_error(FFA_INVALID_PARAMETERS);
2773 }
2774 break;
J-Alves95fbb312024-03-20 15:19:16 +00002775 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002776 case FFA_MEM_LEND_32:
2777 /*
2778 * For operations with multiple borrowers only permit XN
2779 * permissions, and both Sender and borrower should have used
2780 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2781 */
2782 if (multiple_borrowers) {
2783 if (requested_instruction_access !=
2784 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2785 dlog_verbose(
2786 "%s: lend/share/donate with multiple "
2787 "borrowers "
2788 "instruction permissions must NOT be "
2789 "specified.\n",
2790 __func__);
2791 return ffa_error(FFA_INVALID_PARAMETERS);
2792 }
2793 break;
2794 }
2795 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002796 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002797 case FFA_MEM_DONATE_32:
2798 if (!multiple_borrowers &&
2799 requested_instruction_access ==
2800 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2801 dlog_verbose(
2802 "%s: for lend/donate with single borrower "
2803 "instruction permissions must be speficified "
2804 "by borrower\n",
2805 __func__);
2806 return ffa_error(FFA_INVALID_PARAMETERS);
2807 }
2808 break;
2809 default:
2810 panic("%s: Wrong func id provided.\n", __func__);
2811 }
2812
J-Alves96de29f2022-04-26 16:05:24 +01002813 switch (sent_instruction_access) {
2814 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2815 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002816 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002817 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002818 permissions->instruction_access =
2819 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002820 }
2821 break;
2822 }
J-Alvesdcad8992023-09-15 14:10:35 +01002823 /*
2824 * Fall through if requested permissions are less
2825 * permissive than those provided by the sender.
2826 */
J-Alves96de29f2022-04-26 16:05:24 +01002827 case FFA_INSTRUCTION_ACCESS_NX:
2828 if (requested_instruction_access ==
2829 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2830 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2831 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002832 permissions->instruction_access =
2833 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002834 }
2835 break;
2836 }
2837 dlog_verbose(
2838 "Invalid instruction access requested; sender "
2839 "specified permissions %#x but receiver requested "
2840 "%#x.\n",
2841 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002842 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002843 case FFA_INSTRUCTION_ACCESS_RESERVED:
2844 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2845 "be checked before this point.");
2846 }
2847
J-Alvesdcad8992023-09-15 14:10:35 +01002848 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002849}
2850
2851/**
2852 * Validate the receivers' permissions in the retrieve request against those
2853 * specified by the lender.
2854 * In the `permissions` argument returns the permissions to set at S2 for the
2855 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002856 * The function looks into the flag to bypass multiple borrower checks:
2857 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2858 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2859 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2860 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002861 */
2862static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2863 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002864 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002865 ffa_memory_access_permissions_t *permissions,
2866 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002867{
2868 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002869 bool bypass_multi_receiver_check =
2870 (retrieve_request->flags &
2871 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002872 const uint32_t region_receiver_count = memory_region->receiver_count;
2873 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002874
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002875 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002876 assert(permissions != NULL);
2877
Karl Meakin84710f32023-10-12 15:14:49 +01002878 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002879
J-Alves3456e032023-07-20 12:20:05 +01002880 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002881 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002882 dlog_verbose(
2883 "Retrieve request should contain same list of "
2884 "borrowers, as specified by the lender.\n");
2885 return ffa_error(FFA_INVALID_PARAMETERS);
2886 }
2887 } else {
2888 if (retrieve_request->receiver_count != 1) {
2889 dlog_verbose(
2890 "Set bypass multiple borrower check, receiver "
2891 "list must be sized 1 (%x)\n",
2892 memory_region->receiver_count);
2893 return ffa_error(FFA_INVALID_PARAMETERS);
2894 }
J-Alves96de29f2022-04-26 16:05:24 +01002895 }
2896
2897 retrieve_receiver_index = retrieve_request->receiver_count;
2898
J-Alves96de29f2022-04-26 16:05:24 +01002899 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2900 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002901 struct ffa_memory_access *retrieve_request_receiver =
2902 ffa_memory_region_get_receiver(retrieve_request, i);
2903 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002904 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002905 retrieve_request_receiver->receiver_permissions
2906 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002907 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002908 retrieve_request_receiver->receiver_permissions
2909 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002910 struct ffa_memory_access *receiver;
2911 uint32_t mem_region_receiver_index;
2912 bool permissions_RO;
2913 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002914 /*
2915 * If the call is at the virtual FF-A instance the caller's
2916 * ID must match an entry in the memory access list.
2917 * In the SPMC, one of the specified receivers could be from
2918 * the NWd.
2919 */
2920 bool found_to_id = vm_id_is_current_world(to_vm_id)
2921 ? (current_receiver_id == to_vm_id)
2922 : (!vm_id_is_current_world(
2923 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002924
J-Alves3456e032023-07-20 12:20:05 +01002925 if (bypass_multi_receiver_check && !found_to_id) {
2926 dlog_verbose(
2927 "Bypass multiple borrower check for id %x.\n",
2928 current_receiver_id);
2929 continue;
2930 }
2931
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002932 if (retrieve_request_receiver->composite_memory_region_offset !=
2933 0U) {
2934 dlog_verbose(
2935 "Retriever specified address ranges not "
2936 "supported (got offset %d).\n",
2937 retrieve_request_receiver
2938 ->composite_memory_region_offset);
2939 return ffa_error(FFA_INVALID_PARAMETERS);
2940 }
2941
J-Alves96de29f2022-04-26 16:05:24 +01002942 /*
2943 * Find the current receiver in the transaction descriptor from
2944 * sender.
2945 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002946 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002947 ffa_memory_region_get_receiver_index(
2948 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002949
2950 if (mem_region_receiver_index ==
2951 memory_region->receiver_count) {
2952 dlog_verbose("%s: receiver %x not found\n", __func__,
2953 current_receiver_id);
2954 return ffa_error(FFA_DENIED);
2955 }
2956
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002957 receiver = ffa_memory_region_get_receiver(
2958 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002959 assert(receiver != NULL);
2960
2961 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002962
2963 if (found_to_id) {
2964 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002965
2966 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002967 }
2968
2969 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002970 * Check if retrieve request memory access list is valid:
2971 * - The retrieve request complies with the specification.
2972 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002973 */
J-Alvesdcad8992023-09-15 14:10:35 +01002974 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002975 func_id, sent_permissions.data_access,
2976 requested_permissions.data_access,
2977 sent_permissions.instruction_access,
2978 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002979 found_to_id ? permissions : NULL,
2980 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002981
J-Alvesdcad8992023-09-15 14:10:35 +01002982 if (ret.func != FFA_SUCCESS_32) {
2983 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002984 }
2985
Karl Meakin84710f32023-10-12 15:14:49 +01002986 permissions_RO =
2987 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002988 clear_memory_flags =
2989 (retrieve_request->flags &
2990 (FFA_MEMORY_REGION_FLAG_CLEAR |
2991 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002992
J-Alves96de29f2022-04-26 16:05:24 +01002993 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002994 * Can't request PM to clear memory if only provided
2995 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002996 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002997 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002998 dlog_verbose(
2999 "Receiver has RO permissions can not request "
3000 "clear.\n");
3001 return ffa_error(FFA_DENIED);
3002 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003003
3004 /*
3005 * Check the impdef in the retrieve_request matches the value in
3006 * the original memory send.
3007 */
3008 if (ffa_version_from_memory_access_desc_size(
3009 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003010 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003011 ffa_version_from_memory_access_desc_size(
3012 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003013 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003014 if (receiver->impdef.val[0] !=
3015 retrieve_request_receiver->impdef.val[0] ||
3016 receiver->impdef.val[1] !=
3017 retrieve_request_receiver->impdef.val[1]) {
3018 dlog_verbose(
3019 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003020 "match retrieve request value send "
3021 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003022 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003023 receiver->impdef.val[0],
3024 receiver->impdef.val[1],
3025 retrieve_request_receiver->impdef
3026 .val[0],
3027 retrieve_request_receiver->impdef
3028 .val[1]);
3029 return ffa_error(FFA_INVALID_PARAMETERS);
3030 }
3031 }
J-Alves96de29f2022-04-26 16:05:24 +01003032 }
3033
3034 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3035 dlog_verbose(
3036 "Retrieve request does not contain caller's (%x) "
3037 "permissions\n",
3038 to_vm_id);
3039 return ffa_error(FFA_INVALID_PARAMETERS);
3040 }
3041
3042 return (struct ffa_value){.func = FFA_SUCCESS_32};
3043}
3044
Daniel Boulby296ee702023-11-28 13:36:55 +00003045/**
3046 * According to section 17.4.3 of the FF-A v1.2 ALP0 specification, the
3047 * hypervisor may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region
3048 * description of a pending memory sharing operation whose allocator is the SPM,
3049 * for validation purposes before forwarding an FFA_MEM_RECLAIM call. For a
3050 * hypervisor retrieve request the endpoint memory access descriptor count must
3051 * be 0 (for any other retrieve request it must be >= 1).
J-Alvesa9cd7e32022-07-01 13:49:33 +01003052 */
Daniel Boulby296ee702023-11-28 13:36:55 +00003053bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003054{
Daniel Boulby296ee702023-11-28 13:36:55 +00003055 return request->receiver_count == 0U;
3056}
3057
J-Alvesa9cd7e32022-07-01 13:49:33 +01003058/*
3059 * Helper to reset count of fragments retrieved by the hypervisor.
3060 */
3061static void ffa_memory_retrieve_complete_from_hyp(
3062 struct ffa_memory_share_state *share_state)
3063{
3064 if (share_state->hypervisor_fragment_count ==
3065 share_state->fragment_count) {
3066 share_state->hypervisor_fragment_count = 0;
3067 }
3068}
3069
J-Alves089004f2022-07-13 14:25:44 +01003070/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003071 * Prepares the return of the ffa_value for the memory retrieve response.
3072 */
3073static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3074 uint32_t fragment_length)
3075{
3076 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3077 .arg1 = total_length,
3078 .arg2 = fragment_length};
3079}
3080
3081/**
J-Alves089004f2022-07-13 14:25:44 +01003082 * Validate that the memory region descriptor provided by the borrower on
3083 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3084 * memory sharing call.
3085 */
3086static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003087 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3088 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003089 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3090 uint32_t share_func)
3091{
3092 ffa_memory_region_flags_t transaction_type =
3093 retrieve_request->flags &
3094 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003095 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003096 const uint64_t memory_access_desc_size =
3097 retrieve_request->memory_access_desc_size;
3098 const uint32_t expected_retrieve_request_length =
3099 retrieve_request->receivers_offset +
3100 (uint32_t)(retrieve_request->receiver_count *
3101 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003102
3103 assert(retrieve_request != NULL);
3104 assert(memory_region != NULL);
3105 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003106
J-Alves4f0d9c12024-01-17 17:23:11 +00003107 if (retrieve_request_length != expected_retrieve_request_length) {
3108 dlog_verbose(
3109 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3110 "but was %d.\n",
3111 expected_retrieve_request_length,
3112 retrieve_request_length);
3113 return ffa_error(FFA_INVALID_PARAMETERS);
3114 }
3115
3116 if (retrieve_request->sender != memory_region->sender) {
3117 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003118 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003119 "retrieve.\n",
3120 memory_region->handle);
3121 return ffa_error(FFA_DENIED);
3122 }
3123
3124 /*
3125 * The SPMC can only process retrieve requests to memory share
3126 * operations with one borrower from the other world. It can't
3127 * determine the ID of the NWd VM that invoked the retrieve
3128 * request interface call. It relies on the hypervisor to
3129 * validate the caller's ID against that provided in the
3130 * `receivers` list of the retrieve response.
3131 * In case there is only one borrower from the NWd in the
3132 * transaction descriptor, record that in the `receiver_id` for
3133 * later use, and validate in the retrieve request message.
3134 * This limitation is due to the fact SPMC can't determine the
3135 * index in the memory share structures state to update.
3136 */
3137 if (to_id == HF_HYPERVISOR_VM_ID) {
3138 uint32_t other_world_count = 0;
3139
3140 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3141 struct ffa_memory_access *receiver =
3142 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003143 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003144 assert(receiver != NULL);
3145
J-Alvesf220d572024-04-24 22:15:14 +01003146 if (!vm_id_is_current_world(
3147 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003148 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003149 /* Set it to be used later. */
3150 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003151 }
3152 }
3153
3154 if (other_world_count > 1) {
3155 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003156 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003157 return ffa_error(FFA_NOT_SUPPORTED);
3158 }
3159 }
J-Alves089004f2022-07-13 14:25:44 +01003160 /*
3161 * Check that the transaction type expected by the receiver is
3162 * correct, if it has been specified.
3163 */
3164 if (transaction_type !=
3165 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3166 transaction_type != (memory_region->flags &
3167 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3168 dlog_verbose(
3169 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003170 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003171 transaction_type,
3172 memory_region->flags &
3173 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3174 retrieve_request->handle);
3175 return ffa_error(FFA_INVALID_PARAMETERS);
3176 }
3177
3178 if (retrieve_request->tag != memory_region->tag) {
3179 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003180 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3181 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003182 retrieve_request->tag, memory_region->tag,
3183 retrieve_request->handle);
3184 return ffa_error(FFA_INVALID_PARAMETERS);
3185 }
3186
J-Alves4f0d9c12024-01-17 17:23:11 +00003187 *receiver_index =
3188 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003189
3190 if (*receiver_index == memory_region->receiver_count) {
3191 dlog_verbose(
3192 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003193 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003194 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003195 return ffa_error(FFA_INVALID_PARAMETERS);
3196 }
3197
3198 if ((retrieve_request->flags &
3199 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3200 dlog_verbose(
3201 "Retriever specified 'address range alignment 'hint' "
3202 "not supported.\n");
3203 return ffa_error(FFA_INVALID_PARAMETERS);
3204 }
3205 if ((retrieve_request->flags &
3206 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3207 dlog_verbose(
3208 "Bits 8-5 must be zero in memory region's flags "
3209 "(address range alignment hint not supported).\n");
3210 return ffa_error(FFA_INVALID_PARAMETERS);
3211 }
3212
3213 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3214 dlog_verbose(
3215 "Bits 31-10 must be zero in memory region's flags.\n");
3216 return ffa_error(FFA_INVALID_PARAMETERS);
3217 }
3218
J-Alves95fbb312024-03-20 15:19:16 +00003219 if ((share_func == FFA_MEM_SHARE_32 ||
3220 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003221 (retrieve_request->flags &
3222 (FFA_MEMORY_REGION_FLAG_CLEAR |
3223 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3224 dlog_verbose(
3225 "Memory Share operation can't clean after relinquish "
3226 "memory region.\n");
3227 return ffa_error(FFA_INVALID_PARAMETERS);
3228 }
3229
3230 /*
3231 * If the borrower needs the memory to be cleared before mapping
3232 * to its address space, the sender should have set the flag
3233 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3234 * FFA_DENIED.
3235 */
3236 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3237 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3238 dlog_verbose(
3239 "Borrower needs memory cleared. Sender needs to set "
3240 "flag for clearing memory.\n");
3241 return ffa_error(FFA_DENIED);
3242 }
3243
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003244 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003245 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003246 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3247 dlog_verbose(
3248 "Invalid security state for memory retrieve request "
3249 "operation.\n");
3250 return ffa_error(FFA_INVALID_PARAMETERS);
3251 }
3252
J-Alves089004f2022-07-13 14:25:44 +01003253 /*
3254 * If memory type is not specified, bypass validation of memory
3255 * attributes in the retrieve request. The retriever is expecting to
3256 * obtain this information from the SPMC.
3257 */
Karl Meakin84710f32023-10-12 15:14:49 +01003258 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003259 return (struct ffa_value){.func = FFA_SUCCESS_32};
3260 }
3261
3262 /*
3263 * Ensure receiver's attributes are compatible with how
3264 * Hafnium maps memory: Normal Memory, Inner shareable,
3265 * Write-Back Read-Allocate Write-Allocate Cacheable.
3266 */
3267 return ffa_memory_attributes_validate(retrieve_request->attributes);
3268}
3269
J-Alves3f6527c2024-04-25 17:10:57 +01003270/**
3271 * Whilst processing the retrieve request, the operation could be aborted, and
3272 * changes to page tables and the share state structures need to be reverted.
3273 */
3274static void ffa_partition_memory_retrieve_request_undo(
3275 struct vm_locked from_locked,
3276 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3277{
3278 /*
3279 * Currently this operation is expected for operations involving the
3280 * 'other_world' vm.
3281 */
3282 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3283 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3284
3285 /* Decrement the retrieved fragment count for the given receiver. */
3286 share_state->retrieved_fragment_count[receiver_index]--;
3287}
3288
3289/**
3290 * Whilst processing an hypervisor retrieve request the operation could be
3291 * aborted. There were no updates to PTs in this case, so decrementing the
3292 * fragment count retrieved by the hypervisor should be enough.
3293 */
3294static void ffa_hypervisor_memory_retrieve_request_undo(
3295 struct ffa_memory_share_state *share_state)
3296{
3297 assert(share_state->hypervisor_fragment_count > 0);
3298 share_state->hypervisor_fragment_count--;
3299}
3300
J-Alves4f0d9c12024-01-17 17:23:11 +00003301static struct ffa_value ffa_partition_retrieve_request(
3302 struct share_states_locked share_states,
3303 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3304 struct ffa_memory_region *retrieve_request,
3305 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003306{
Karl Meakin84710f32023-10-12 15:14:49 +01003307 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003308 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003309 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003310 struct ffa_composite_memory_region *composite;
3311 uint32_t total_length;
3312 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003313 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003314 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003315 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003316 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003317 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003318 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003319 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003320 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003321 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003322 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003323
J-Alves96de29f2022-04-26 16:05:24 +01003324 if (!share_state->sending_complete) {
3325 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003326 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003327 "retrieve.\n",
3328 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003329 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003330 }
3331
J-Alves4f0d9c12024-01-17 17:23:11 +00003332 /*
3333 * Validate retrieve request, according to what was sent by the
3334 * sender. Function will output the `receiver_index` from the
3335 * provided memory region.
3336 */
3337 ret = ffa_memory_retrieve_validate(
3338 receiver_id, retrieve_request, retrieve_request_length,
3339 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003340
J-Alves4f0d9c12024-01-17 17:23:11 +00003341 if (ret.func != FFA_SUCCESS_32) {
3342 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003343 }
J-Alves96de29f2022-04-26 16:05:24 +01003344
J-Alves4f0d9c12024-01-17 17:23:11 +00003345 /*
3346 * Validate the requested permissions against the sent
3347 * permissions.
3348 * Outputs the permissions to give to retriever at S2
3349 * PTs.
3350 */
3351 ret = ffa_memory_retrieve_validate_memory_access_list(
3352 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003353 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003354 if (ret.func != FFA_SUCCESS_32) {
3355 return ret;
3356 }
3357
3358 memory_to_mode = ffa_memory_permissions_to_mode(
3359 permissions, share_state->sender_orig_mode);
3360
Daniel Boulby6e261362024-06-13 16:53:00 +01003361 /*
3362 * Check requested memory type is valid with the memory type of the
3363 * owner. E.g. they follow the memory type precedence where Normal
3364 * memory is more permissive than device and therefore device memory
3365 * can only be shared as device memory.
3366 */
3367 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3368 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3369 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3370 dlog_verbose(
3371 "Retrieving device memory as Normal memory is not "
3372 "allowed\n");
3373 return ffa_error(FFA_DENIED);
3374 }
3375
J-Alves4f0d9c12024-01-17 17:23:11 +00003376 ret = ffa_retrieve_check_update(
3377 to_locked, share_state->fragments,
3378 share_state->fragment_constituent_counts,
3379 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003380 share_state->share_func, false, page_pool, &retrieve_mode,
3381 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003382
3383 if (ret.func != FFA_SUCCESS_32) {
3384 return ret;
3385 }
3386
3387 share_state->retrieved_fragment_count[receiver_index] = 1;
3388
3389 is_retrieve_complete =
3390 share_state->retrieved_fragment_count[receiver_index] ==
3391 share_state->fragment_count;
3392
J-Alvesb5084cf2022-07-06 14:20:12 +01003393 /* VMs acquire the RX buffer from SPMC. */
3394 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3395
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003396 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003397 * Copy response to RX buffer of caller and deliver the message.
3398 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003399 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003400 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003401
Andrew Walbranca808b12020-05-15 17:22:28 +01003402 /*
J-Alves460d36c2023-10-12 17:02:15 +01003403 * Set the security state in the memory retrieve response attributes
3404 * if specified by the target mode.
3405 */
3406 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3407 retrieve_mode);
3408
3409 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003410 * Constituents which we received in the first fragment should
3411 * always fit in the first fragment we are sending, because the
3412 * header is the same size in both cases and we have a fixed
3413 * message buffer size. So `ffa_retrieved_memory_region_init`
3414 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003415 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003416
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003417 /* Provide the permissions that had been provided. */
3418 receiver->receiver_permissions.permissions = permissions;
3419
3420 /*
3421 * Prepare the memory region descriptor for the retrieve response.
3422 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003423 * structures.
3424 * At this point the retrieve request descriptor from the partition
3425 * has been processed. The `retrieve_request` is expected to be in
3426 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3427 * prepare the retrieve response before copying it to the RX buffer of
3428 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003429 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003430 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003431 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3432 memory_region->sender, attributes, memory_region->flags, handle,
3433 permissions, receiver, 1, memory_access_desc_size,
3434 composite->page_count, composite->constituent_count,
3435 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003436 share_state->fragment_constituent_counts[0], &total_length,
3437 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003438
J-Alves7b9cc432024-04-04 10:57:17 +01003439 /*
3440 * Copy the message from the buffer into the partition's mailbox.
3441 * The operation might fail unexpectedly due to change in PAS address
3442 * space, or improper values to the sizes of the structures.
3443 */
3444 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3445 retrieve_request, fragment_length)) {
3446 dlog_error(
3447 "%s: aborted the copy of response to RX buffer of "
3448 "%x.\n",
3449 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003450
3451 ffa_partition_memory_retrieve_request_undo(
3452 to_locked, share_state, receiver_index);
3453
J-Alves7b9cc432024-04-04 10:57:17 +01003454 return ffa_error(FFA_ABORTED);
3455 }
3456
J-Alves4f0d9c12024-01-17 17:23:11 +00003457 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003458 ffa_memory_retrieve_complete(share_states, share_state,
3459 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003460 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003461
3462 return ffa_memory_retrieve_resp(total_length, fragment_length);
3463}
3464
3465static struct ffa_value ffa_hypervisor_retrieve_request(
3466 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3467 struct ffa_memory_region *retrieve_request)
3468{
3469 struct ffa_value ret;
3470 struct ffa_composite_memory_region *composite;
3471 uint32_t total_length;
3472 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003473 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003474 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003475 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003476 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003477 ffa_memory_handle_t handle = retrieve_request->handle;
3478
J-Alves4f0d9c12024-01-17 17:23:11 +00003479 memory_region = share_state->memory_region;
3480
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003481 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3482
J-Alves7b6ab612024-01-24 09:54:54 +00003483 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003484 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003485 memory_access_desc_size = sizeof(struct ffa_memory_access);
3486 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003487 case FFA_VERSION_1_0:
3488 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003489 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3490 break;
3491 default:
3492 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3493 }
3494
J-Alves4f0d9c12024-01-17 17:23:11 +00003495 if (share_state->hypervisor_fragment_count != 0U) {
3496 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003497 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003498 "the hypervisor.\n",
3499 handle);
3500 return ffa_error(FFA_DENIED);
3501 }
3502
3503 share_state->hypervisor_fragment_count = 1;
3504
J-Alves4f0d9c12024-01-17 17:23:11 +00003505 /* VMs acquire the RX buffer from SPMC. */
3506 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3507
3508 /*
3509 * Copy response to RX buffer of caller and deliver the message.
3510 * This must be done before the share_state is (possibly) freed.
3511 */
3512 composite = ffa_memory_region_get_composite(memory_region, 0);
3513
3514 /*
3515 * Constituents which we received in the first fragment should
3516 * always fit in the first fragment we are sending, because the
3517 * header is the same size in both cases and we have a fixed
3518 * message buffer size. So `ffa_retrieved_memory_region_init`
3519 * should never fail.
3520 */
3521
3522 /*
3523 * Set the security state in the memory retrieve response attributes
3524 * if specified by the target mode.
3525 */
3526 attributes = plat_ffa_memory_security_mode(
3527 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003528
3529 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3530
J-Alves7b9cc432024-04-04 10:57:17 +01003531 /*
3532 * At this point the `retrieve_request` is expected to be in a section
3533 * managed by the hypervisor.
3534 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003535 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003536 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3537 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003538 receiver->receiver_permissions.permissions, receiver,
3539 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003540 composite->page_count, composite->constituent_count,
3541 share_state->fragments[0],
3542 share_state->fragment_constituent_counts[0], &total_length,
3543 &fragment_length));
3544
J-Alves7b9cc432024-04-04 10:57:17 +01003545 /*
3546 * Copy the message from the buffer into the hypervisor's mailbox.
3547 * The operation might fail unexpectedly due to change in PAS, or
3548 * improper values for the sizes of the structures.
3549 */
3550 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3551 retrieve_request, fragment_length)) {
3552 dlog_error(
3553 "%s: aborted the copy of response to RX buffer of "
3554 "%x.\n",
3555 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003556
3557 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3558
J-Alves7b9cc432024-04-04 10:57:17 +01003559 return ffa_error(FFA_ABORTED);
3560 }
3561
J-Alves3f6527c2024-04-25 17:10:57 +01003562 ffa_memory_retrieve_complete_from_hyp(share_state);
3563
J-Alves4f0d9c12024-01-17 17:23:11 +00003564 return ffa_memory_retrieve_resp(total_length, fragment_length);
3565}
3566
3567struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3568 struct ffa_memory_region *retrieve_request,
3569 uint32_t retrieve_request_length,
3570 struct mpool *page_pool)
3571{
3572 ffa_memory_handle_t handle = retrieve_request->handle;
3573 struct share_states_locked share_states;
3574 struct ffa_memory_share_state *share_state;
3575 struct ffa_value ret;
3576
3577 dump_share_states();
3578
3579 share_states = share_states_lock();
3580 share_state = get_share_state(share_states, handle);
3581 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003582 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003583 handle);
3584 ret = ffa_error(FFA_INVALID_PARAMETERS);
3585 goto out;
3586 }
3587
Daniel Boulby296ee702023-11-28 13:36:55 +00003588 if (is_ffa_hypervisor_retrieve_request(retrieve_request)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003589 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3590 retrieve_request);
3591 } else {
3592 ret = ffa_partition_retrieve_request(
3593 share_states, share_state, to_locked, retrieve_request,
3594 retrieve_request_length, page_pool);
3595 }
3596
3597 /* Track use of the RX buffer if the handling has succeeded. */
3598 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3599 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3600 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3601 }
3602
Andrew Walbranca808b12020-05-15 17:22:28 +01003603out:
3604 share_states_unlock(&share_states);
3605 dump_share_states();
3606 return ret;
3607}
3608
J-Alves5da37d92022-10-24 16:33:48 +01003609/**
3610 * Determine expected fragment offset according to the FF-A version of
3611 * the caller.
3612 */
3613static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3614 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003615 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003616{
3617 uint32_t expected_fragment_offset;
3618 uint32_t composite_constituents_offset;
3619
Karl Meakin0e617d92024-04-05 12:55:22 +01003620 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003621 /*
3622 * Hafnium operates memory regions in FF-A v1.1 format, so we
3623 * can retrieve the constituents offset from descriptor.
3624 */
3625 composite_constituents_offset =
3626 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003627 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003628 /*
3629 * If retriever is FF-A v1.0, determine the composite offset
3630 * as it is expected to have been configured in the
3631 * retrieve response.
3632 */
3633 composite_constituents_offset =
3634 sizeof(struct ffa_memory_region_v1_0) +
3635 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003636 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003637 sizeof(struct ffa_composite_memory_region);
3638 } else {
3639 panic("%s received an invalid FF-A version.\n", __func__);
3640 }
3641
3642 expected_fragment_offset =
3643 composite_constituents_offset +
3644 retrieved_constituents_count *
3645 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003646 (size_t)(memory_region->memory_access_desc_size *
3647 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003648
3649 return expected_fragment_offset;
3650}
3651
Andrew Walbranca808b12020-05-15 17:22:28 +01003652struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3653 ffa_memory_handle_t handle,
3654 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003655 ffa_id_t sender_vm_id,
J-Alvesc3fd9752024-04-04 11:45:33 +01003656 void *retrieve_continue_page,
Andrew Walbranca808b12020-05-15 17:22:28 +01003657 struct mpool *page_pool)
3658{
3659 struct ffa_memory_region *memory_region;
3660 struct share_states_locked share_states;
3661 struct ffa_memory_share_state *share_state;
3662 struct ffa_value ret;
3663 uint32_t fragment_index;
3664 uint32_t retrieved_constituents_count;
3665 uint32_t i;
3666 uint32_t expected_fragment_offset;
3667 uint32_t remaining_constituent_count;
3668 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003669 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003670 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003671
3672 dump_share_states();
3673
3674 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003675 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003676 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003677 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003678 handle);
3679 ret = ffa_error(FFA_INVALID_PARAMETERS);
3680 goto out;
3681 }
3682
3683 memory_region = share_state->memory_region;
3684 CHECK(memory_region != NULL);
3685
Andrew Walbranca808b12020-05-15 17:22:28 +01003686 if (!share_state->sending_complete) {
3687 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003688 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003689 "retrieve.\n",
3690 handle);
3691 ret = ffa_error(FFA_INVALID_PARAMETERS);
3692 goto out;
3693 }
3694
J-Alves59ed0042022-07-28 18:26:41 +01003695 /*
3696 * If retrieve request from the hypervisor has been initiated in the
3697 * given share_state, continue it, else assume it is a continuation of
J-Alvesc3fd9752024-04-04 11:45:33 +01003698 * retrieve request from a partition.
J-Alves59ed0042022-07-28 18:26:41 +01003699 */
3700 continue_ffa_hyp_mem_retrieve_req =
3701 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3702 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003703 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003704
J-Alves59ed0042022-07-28 18:26:41 +01003705 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003706 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003707 memory_region, to_locked.vm->id);
3708
3709 if (receiver_index == memory_region->receiver_count) {
3710 dlog_verbose(
3711 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003712 "borrower to memory sharing transaction "
3713 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003714 to_locked.vm->id, handle);
3715 ret = ffa_error(FFA_INVALID_PARAMETERS);
3716 goto out;
3717 }
3718
J-Alvesc3fd9752024-04-04 11:45:33 +01003719 fragment_index =
3720 share_state->retrieved_fragment_count[receiver_index];
3721
3722 if (fragment_index == 0 ||
3723 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003724 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003725 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003726 "started or already completed (%d/%d fragments "
3727 "retrieved).\n",
3728 handle,
3729 share_state->retrieved_fragment_count
3730 [receiver_index],
3731 share_state->fragment_count);
3732 ret = ffa_error(FFA_INVALID_PARAMETERS);
3733 goto out;
3734 }
J-Alves59ed0042022-07-28 18:26:41 +01003735 } else {
J-Alvesc3fd9752024-04-04 11:45:33 +01003736 fragment_index = share_state->hypervisor_fragment_count;
3737
3738 if (fragment_index == 0 ||
3739 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003740 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003741 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003742 "started from hypervisor.\n",
3743 handle);
3744 ret = ffa_error(FFA_INVALID_PARAMETERS);
3745 goto out;
3746 }
3747
3748 if (memory_region->sender != sender_vm_id) {
3749 dlog_verbose(
3750 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003751 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003752 sender_vm_id, handle);
3753 ret = ffa_error(FFA_INVALID_PARAMETERS);
3754 goto out;
3755 }
3756
J-Alves59ed0042022-07-28 18:26:41 +01003757 receiver_index = 0;
3758 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003759
3760 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003761 * Check that the given fragment offset is correct by counting
3762 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003763 */
3764 retrieved_constituents_count = 0;
3765 for (i = 0; i < fragment_index; ++i) {
3766 retrieved_constituents_count +=
3767 share_state->fragment_constituent_counts[i];
3768 }
J-Alvesc7484f12022-05-13 12:41:14 +01003769
3770 CHECK(memory_region->receiver_count > 0);
3771
Andrew Walbranca808b12020-05-15 17:22:28 +01003772 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003773 ffa_memory_retrieve_expected_offset_per_ffa_version(
3774 memory_region, retrieved_constituents_count,
3775 to_locked.vm->ffa_version);
3776
Andrew Walbranca808b12020-05-15 17:22:28 +01003777 if (fragment_offset != expected_fragment_offset) {
3778 dlog_verbose("Fragment offset was %d but expected %d.\n",
3779 fragment_offset, expected_fragment_offset);
3780 ret = ffa_error(FFA_INVALID_PARAMETERS);
3781 goto out;
3782 }
3783
J-Alves4f0d9c12024-01-17 17:23:11 +00003784 /*
3785 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3786 * is currently ownder by the SPMC.
3787 */
3788 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003789
Andrew Walbranca808b12020-05-15 17:22:28 +01003790 remaining_constituent_count = ffa_memory_fragment_init(
J-Alvesc3fd9752024-04-04 11:45:33 +01003791 (struct ffa_memory_region_constituent *)retrieve_continue_page,
3792 HF_MAILBOX_SIZE, share_state->fragments[fragment_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01003793 share_state->fragment_constituent_counts[fragment_index],
3794 &fragment_length);
3795 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003796
J-Alvesc3fd9752024-04-04 11:45:33 +01003797 /*
3798 * Return FFA_ERROR(FFA_ABORTED) in case the access to the partition's
3799 * RX buffer results in a GPF exception. Could happen if the retrieve
3800 * request is for a VM or the Hypervisor retrieve request, if the PAS
3801 * has been changed externally.
3802 */
3803 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3804 retrieve_continue_page, fragment_length)) {
3805 dlog_error(
3806 "%s: aborted copying fragment to RX buffer of %#x.\n",
3807 __func__, to_locked.vm->id);
3808 ret = ffa_error(FFA_ABORTED);
3809 goto out;
3810 }
3811
Andrew Walbranca808b12020-05-15 17:22:28 +01003812 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003813 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003814
J-Alves59ed0042022-07-28 18:26:41 +01003815 if (!continue_ffa_hyp_mem_retrieve_req) {
3816 share_state->retrieved_fragment_count[receiver_index]++;
3817 if (share_state->retrieved_fragment_count[receiver_index] ==
3818 share_state->fragment_count) {
3819 ffa_memory_retrieve_complete(share_states, share_state,
3820 page_pool);
3821 }
3822 } else {
3823 share_state->hypervisor_fragment_count++;
3824
3825 ffa_memory_retrieve_complete_from_hyp(share_state);
3826 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003827 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3828 .arg1 = (uint32_t)handle,
3829 .arg2 = (uint32_t)(handle >> 32),
3830 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003831
3832out:
3833 share_states_unlock(&share_states);
3834 dump_share_states();
3835 return ret;
3836}
3837
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003838struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003839 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003840 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003841{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003842 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003843 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003844 struct ffa_memory_share_state *share_state;
3845 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003846 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003847 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003848 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003849 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003850 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003851
Andrew Walbrana65a1322020-04-06 19:32:32 +01003852 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003853 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003854 "Stream endpoints not supported (got %d endpoints on "
3855 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003856 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003857 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003858 }
3859
J-Alvesbd060342024-04-26 18:44:31 +01003860 if (vm_id_is_current_world(from_locked.vm->id) &&
3861 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003862 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003863 "VM ID %d in relinquish message doesn't match calling "
3864 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003865 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003866 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003867 }
3868
3869 dump_share_states();
3870
3871 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003872 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003873 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003874 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003875 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003876 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003877 goto out;
3878 }
3879
Andrew Walbranca808b12020-05-15 17:22:28 +01003880 if (!share_state->sending_complete) {
3881 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003882 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003883 "relinquish.\n",
3884 handle);
3885 ret = ffa_error(FFA_INVALID_PARAMETERS);
3886 goto out;
3887 }
3888
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003889 memory_region = share_state->memory_region;
3890 CHECK(memory_region != NULL);
3891
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003892 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003893 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003894
3895 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003896 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003897 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003898 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003899 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003900 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003901 goto out;
3902 }
3903
J-Alves8eb19162022-04-28 10:56:48 +01003904 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003905 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003906 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003907 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003908 "receiver %x can't relinquish.\n",
3909 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003910 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003911 goto out;
3912 }
3913
J-Alves3c5b2072022-11-21 12:45:40 +00003914 /*
3915 * Either clear if requested in relinquish call, or in a retrieve
3916 * request from one of the borrowers.
3917 */
3918 receivers_relinquished_memory = true;
3919
3920 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3921 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003922 ffa_memory_region_get_receiver(memory_region, i);
3923 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003924 if (receiver->receiver_permissions.receiver ==
3925 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003926 receiver_permissions =
3927 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003928 continue;
3929 }
3930
3931 if (share_state->retrieved_fragment_count[i] != 0U) {
3932 receivers_relinquished_memory = false;
3933 break;
3934 }
3935 }
3936
3937 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003938 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3939 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003940
3941 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003942 * Clear is not allowed for memory that was shared, as the
3943 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003944 */
J-Alves95fbb312024-03-20 15:19:16 +00003945 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3946 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003947 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003948 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003949 goto out;
3950 }
3951
J-Alvesb886d492024-04-15 10:55:29 +01003952 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003953 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3954 __func__);
3955 ret = ffa_error(FFA_DENIED);
3956 goto out;
3957 }
3958
Andrew Walbranca808b12020-05-15 17:22:28 +01003959 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003960 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003961 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003962 share_state->fragment_count, share_state->sender_orig_mode,
3963 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003964
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003965 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003966 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003967 * Mark memory handle as not retrieved, so it can be
3968 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003969 */
J-Alves8eb19162022-04-28 10:56:48 +01003970 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003971 }
3972
3973out:
3974 share_states_unlock(&share_states);
3975 dump_share_states();
3976 return ret;
3977}
3978
3979/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003980 * Validates that the reclaim transition is allowed for the given
3981 * handle, updates the page table of the reclaiming VM, and frees the
3982 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003983 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003984struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003985 ffa_memory_handle_t handle,
3986 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003987 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003988{
3989 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003990 struct ffa_memory_share_state *share_state;
3991 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003992 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003993
3994 dump_share_states();
3995
3996 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003997
Karl Meakin4a2854a2023-06-30 16:26:52 +01003998 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003999 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00004000 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004001 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004002 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004003 goto out;
4004 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01004005 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004006
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004007 CHECK(memory_region != NULL);
4008
J-Alvesa9cd7e32022-07-01 13:49:33 +01004009 if (vm_id_is_current_world(to_locked.vm->id) &&
4010 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004011 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004012 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004013 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004014 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004015 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004016 goto out;
4017 }
4018
Andrew Walbranca808b12020-05-15 17:22:28 +01004019 if (!share_state->sending_complete) {
4020 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004021 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004022 "reclaim.\n",
4023 handle);
4024 ret = ffa_error(FFA_INVALID_PARAMETERS);
4025 goto out;
4026 }
4027
J-Alves752236c2022-04-28 11:07:47 +01004028 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4029 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004030 struct ffa_memory_access *receiver =
4031 ffa_memory_region_get_receiver(memory_region,
4032 i);
4033
4034 assert(receiver != NULL);
4035 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004036 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004037 "Tried to reclaim memory handle %#lx that has "
4038 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004039 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004040 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004041 ret = ffa_error(FFA_DENIED);
4042 goto out;
4043 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004044 }
4045
Andrew Walbranca808b12020-05-15 17:22:28 +01004046 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004047 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004048 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004049 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004050 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004051 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004053 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004054 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004055 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004056 }
4057
4058out:
4059 share_states_unlock(&share_states);
4060 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004061}