blob: c3d388423c82f84ec203d6491a59bd66340fc1d8 [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-Alves460d36c2023-10-12 17:02:15 +010011#include <stdint.h>
12
Federico Recanati4fd065d2021-12-13 20:06:23 +010013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000016
J-Alves5952d942022-12-22 16:03:00 +000017#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000019#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010020#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010022#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010024#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010025#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000026#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000027#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000028#include "hf/panic.h"
29#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010030#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000031#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000032#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010033
J-Alves2d8457f2022-10-05 11:06:41 +010034#include "vmapi/hf/ffa_v1_0.h"
35
J-Alves5da37d92022-10-24 16:33:48 +010036#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
37
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000038/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040 * by this lock.
41 */
42static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000044
45/**
J-Alvesed508c82023-05-04 16:09:48 +010046 * Return the offset to the first constituent within the
47 * `ffa_composite_memory_region` for the given receiver from an
48 * `ffa_memory_region`. The caller must check that the receiver_index is within
49 * bounds, and that it has a composite memory region offset.
50 */
51static uint32_t ffa_composite_constituent_offset(
52 struct ffa_memory_region *memory_region, uint32_t receiver_index)
53{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000054 struct ffa_memory_access *receiver;
55 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010056
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000057 CHECK(receiver_index < memory_region->receiver_count);
58
59 receiver =
60 ffa_memory_region_get_receiver(memory_region, receiver_index);
61 CHECK(receiver != NULL);
62
63 composite_offset = receiver->composite_memory_region_offset;
64
65 CHECK(composite_offset != 0);
66
67 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010068}
69
70/**
J-Alves917d2f22020-10-30 18:39:30 +000071 * Extracts the index from a memory handle allocated by Hafnium's current world.
72 */
73uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
74{
75 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
76}
77
78/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010079 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
80 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
81 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010082 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010083 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
84 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010085 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010086struct ffa_memory_share_state *allocate_share_state(
87 struct share_states_locked share_states, uint32_t share_func,
88 struct ffa_memory_region *memory_region, uint32_t fragment_length,
89 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000091 assert(share_states.share_states != NULL);
92 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093
Karl Meakin52cdfe72023-06-30 14:49:10 +010094 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010095 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010097 &share_states.share_states[i];
98 struct ffa_composite_memory_region *composite =
99 ffa_memory_region_get_composite(memory_region,
100 0);
101
102 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000103 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200104 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100105 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000106 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100107 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108 allocated_state->share_func = share_func;
109 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100110 allocated_state->fragment_count = 1;
111 allocated_state->fragments[0] = composite->constituents;
112 allocated_state->fragment_constituent_counts[0] =
113 (fragment_length -
114 ffa_composite_constituent_offset(memory_region,
115 0)) /
116 sizeof(struct ffa_memory_region_constituent);
117 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100118 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
119 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100120 allocated_state->retrieved_fragment_count[j] =
121 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100123 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 }
125 }
126
Karl Meakin52cdfe72023-06-30 14:49:10 +0100127 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128}
129
130/** Locks the share states lock. */
131struct share_states_locked share_states_lock(void)
132{
133 sl_lock(&share_states_lock_instance);
134
135 return (struct share_states_locked){.share_states = share_states};
136}
137
138/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100139void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000141 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 share_states->share_states = NULL;
143 sl_unlock(&share_states_lock_instance);
144}
145
146/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100147 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100148 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150struct ffa_memory_share_state *get_share_state(
151 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000155 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100156
157 /*
158 * First look for a share_state allocated by us, in which case the
159 * handle is based on the index.
160 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100162 uint64_t index = ffa_memory_handle_get_index(handle);
163
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 if (index < MAX_MEM_SHARES) {
165 share_state = &share_states.share_states[index];
166 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100167 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 }
169 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 }
171
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100173 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000175 if (share_state->memory_region != NULL &&
176 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100178 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
181
Karl Meakin4a2854a2023-06-30 16:26:52 +0100182 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
185/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186void share_state_free(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state,
188 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189{
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 uint32_t i;
191
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000192 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 /*
197 * First fragment is part of the same page as the `memory_region`, so it
198 * doesn't need to be freed separately.
199 */
200 share_state->fragments[0] = NULL;
201 share_state->fragment_constituent_counts[0] = 0;
202 for (i = 1; i < share_state->fragment_count; ++i) {
203 mpool_free(page_pool, share_state->fragments[i]);
204 share_state->fragments[i] = NULL;
205 share_state->fragment_constituent_counts[i] = 0;
206 }
207 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100209 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210}
211
Andrew Walbranca808b12020-05-15 17:22:28 +0100212/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100213bool share_state_sending_complete(struct share_states_locked share_states,
214 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranca808b12020-05-15 17:22:28 +0100216 struct ffa_composite_memory_region *composite;
217 uint32_t expected_constituent_count;
218 uint32_t fragment_constituent_count_total = 0;
219 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000220
Andrew Walbranca808b12020-05-15 17:22:28 +0100221 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000222 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100223
224 /*
225 * Share state must already be valid, or it's not possible to get hold
226 * of it.
227 */
228 CHECK(share_state->memory_region != NULL &&
229 share_state->share_func != 0);
230
231 composite =
232 ffa_memory_region_get_composite(share_state->memory_region, 0);
233 expected_constituent_count = composite->constituent_count;
234 for (i = 0; i < share_state->fragment_count; ++i) {
235 fragment_constituent_count_total +=
236 share_state->fragment_constituent_counts[i];
237 }
238 dlog_verbose(
239 "Checking completion: constituent count %d/%d from %d "
240 "fragments.\n",
241 fragment_constituent_count_total, expected_constituent_count,
242 share_state->fragment_count);
243
244 return fragment_constituent_count_total == expected_constituent_count;
245}
246
247/**
248 * Calculates the offset of the next fragment expected for the given share
249 * state.
250 */
J-Alvesfdd29272022-07-19 13:16:31 +0100251uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100252 struct share_states_locked share_states,
253 struct ffa_memory_share_state *share_state)
254{
255 uint32_t next_fragment_offset;
256 uint32_t i;
257
258 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260
261 next_fragment_offset =
262 ffa_composite_constituent_offset(share_state->memory_region, 0);
263 for (i = 0; i < share_state->fragment_count; ++i) {
264 next_fragment_offset +=
265 share_state->fragment_constituent_counts[i] *
266 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 }
268
Andrew Walbranca808b12020-05-15 17:22:28 +0100269 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000270}
271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100272static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273{
274 uint32_t i;
275
276 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
277 return;
278 }
279
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000280 dlog("from VM %#x, attributes %#x, flags %#x, handle %#x "
281 "tag %u, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 "recipients [",
283 memory_region->sender, memory_region->attributes,
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000284 memory_region->flags, memory_region->handle, memory_region->tag,
285 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100286 memory_region->receiver_count);
287 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000288 struct ffa_memory_access *receiver =
289 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000290 if (i != 0) {
291 dlog(", ");
292 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000293 dlog("Receiver %#x: %#x (offset %u)",
294 receiver->receiver_permissions.receiver,
295 receiver->receiver_permissions.permissions,
296 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000297 /* The impdef field is only present from v1.2 and later */
298 if (ffa_version_from_memory_access_desc_size(
299 memory_region->memory_access_desc_size) >=
300 MAKE_FFA_VERSION(1, 2)) {
301 dlog(", impdef: %#x %#x", receiver->impdef.val[0],
302 receiver->impdef.val[1]);
303 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000304 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000305 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306}
307
J-Alves66652252022-07-06 09:49:51 +0100308void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309{
310 uint32_t i;
311
312 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
313 return;
314 }
315
316 dlog("Current share states:\n");
317 sl_lock(&share_states_lock_instance);
318 for (i = 0; i < MAX_MEM_SHARES; ++i) {
319 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000322 dlog("SHARE");
323 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000325 dlog("LEND");
326 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000328 dlog("DONATE");
329 break;
330 default:
331 dlog("invalid share_func %#x",
332 share_states[i].share_func);
333 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100334 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100336 if (share_states[i].sending_complete) {
337 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100339 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 }
J-Alves2a0d2882020-10-29 14:49:50 +0000341 dlog(" with %d fragments, %d retrieved, "
342 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100343 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000344 share_states[i].retrieved_fragment_count[0],
345 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346 }
347 }
348 sl_unlock(&share_states_lock_instance);
349}
350
Andrew Walbran475c1452020-02-07 13:22:22 +0000351/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100352static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100353 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000354{
355 uint32_t mode = 0;
356
Karl Meakin84710f32023-10-12 15:14:49 +0100357 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000359 mode = MM_MODE_R;
360 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000362 mode = MM_MODE_R | MM_MODE_W;
363 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100364 case FFA_DATA_ACCESS_NOT_SPECIFIED:
365 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
366 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_DATA_ACCESS_RESERVED:
368 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100369 }
370
Karl Meakin84710f32023-10-12 15:14:49 +0100371 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000373 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100375 mode |= MM_MODE_X;
376 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100377 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
378 mode |= (default_mode & MM_MODE_X);
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_INSTRUCTION_ACCESS_RESERVED:
381 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000382 }
383
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200384 /* Set the security state bit if necessary. */
385 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
386 mode |= plat_ffa_other_world_mode();
387 }
388
Andrew Walbran475c1452020-02-07 13:22:22 +0000389 return mode;
390}
391
Jose Marinho75509b42019-04-09 09:34:59 +0100392/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 * Get the current mode in the stage-2 page table of the given vm of all the
394 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100396 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000398 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 struct ffa_memory_region_constituent **fragments,
400 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100401{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100402 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100403 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100404
Andrew Walbranca808b12020-05-15 17:22:28 +0100405 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100406 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000407 * Fail if there are no constituents. Otherwise we would get an
408 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100409 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100410 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100412 }
413
Andrew Walbranca808b12020-05-15 17:22:28 +0100414 for (i = 0; i < fragment_count; ++i) {
415 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
416 ipaddr_t begin = ipa_init(fragments[i][j].address);
417 size_t size = fragments[i][j].page_count * PAGE_SIZE;
418 ipaddr_t end = ipa_add(begin, size);
419 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420
Andrew Walbranca808b12020-05-15 17:22:28 +0100421 /* Fail if addresses are not page-aligned. */
422 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
423 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100424 dlog_verbose("%s: addresses not page-aligned\n",
425 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100426 return ffa_error(FFA_INVALID_PARAMETERS);
427 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428
Andrew Walbranca808b12020-05-15 17:22:28 +0100429 /*
430 * Ensure that this constituent memory range is all
431 * mapped with the same mode.
432 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800433 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100434 dlog_verbose(
435 "%s: constituent memory range %#x..%#x "
436 "not mapped with the same mode\n",
437 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 return ffa_error(FFA_DENIED);
439 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100440
Andrew Walbranca808b12020-05-15 17:22:28 +0100441 /*
442 * Ensure that all constituents are mapped with the same
443 * mode.
444 */
445 if (i == 0) {
446 *orig_mode = current_mode;
447 } else if (current_mode != *orig_mode) {
448 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100449 "%s: expected mode %#x but was %#x for "
450 "%d pages at %#x.\n",
451 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 fragments[i][j].page_count,
453 ipa_addr(begin));
454 return ffa_error(FFA_DENIED);
455 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100456 }
Jose Marinho75509b42019-04-09 09:34:59 +0100457 }
458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460}
461
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100462uint32_t ffa_version_from_memory_access_desc_size(
463 uint32_t memory_access_desc_size)
464{
465 switch (memory_access_desc_size) {
466 /*
467 * v1.0 and v1.1 memory access descriptors are the same size however
468 * v1.1 is the first version to include the memory access descriptor
469 * size field so return v1.1.
470 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000471 case sizeof(struct ffa_memory_access_v1_0):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100472 return MAKE_FFA_VERSION(1, 1);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000473 case sizeof(struct ffa_memory_access):
474 return MAKE_FFA_VERSION(1, 2);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100475 }
476 return 0;
477}
478
479/**
480 * Check if the receivers size and offset given is valid for the senders
481 * FF-A version.
482 */
483static bool receiver_size_and_offset_valid_for_version(
484 uint32_t receivers_size, uint32_t receivers_offset,
485 uint32_t ffa_version)
486{
487 /*
488 * Check that the version that the memory access descriptor size belongs
489 * to is compatible with the FF-A version we believe the sender to be.
490 */
491 uint32_t expected_ffa_version =
492 ffa_version_from_memory_access_desc_size(receivers_size);
493 if (!FFA_VERSIONS_ARE_COMPATIBLE(expected_ffa_version, ffa_version)) {
494 return false;
495 }
496
497 /*
498 * Check the receivers_offset matches the version we found from
499 * memory access descriptor size.
500 */
501 switch (expected_ffa_version) {
502 case MAKE_FFA_VERSION(1, 1):
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000503 case MAKE_FFA_VERSION(1, 2):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100504 return receivers_offset == sizeof(struct ffa_memory_region);
505 default:
506 return false;
507 }
508}
509
510/**
511 * Check the values set for fields in the memory region are valid and safe.
512 * Offset values are within safe bounds, receiver count will not cause overflows
513 * and reserved fields are 0.
514 */
515bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
516 uint32_t ffa_version,
517 uint32_t fragment_length,
518 bool send_transaction)
519{
520 uint32_t receiver_count;
521 struct ffa_memory_access *receiver;
522 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000523 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
524 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100525
526 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100527 /* Check the reserved fields are 0. */
528 if (memory_region_v1_0->reserved_0 != 0 ||
529 memory_region_v1_0->reserved_1 != 0) {
530 dlog_verbose("Reserved fields must be 0.\n");
531 return false;
532 }
533
534 receiver_count = memory_region_v1_0->receiver_count;
535 } else {
536 uint32_t receivers_size =
537 memory_region->memory_access_desc_size;
538 uint32_t receivers_offset = memory_region->receivers_offset;
539
540 /* Check the reserved field is 0. */
541 if (memory_region->reserved[0] != 0 ||
542 memory_region->reserved[1] != 0 ||
543 memory_region->reserved[2] != 0) {
544 dlog_verbose("Reserved fields must be 0.\n");
545 return false;
546 }
547
548 /*
549 * Check memory_access_desc_size matches the size of the struct
550 * for the senders FF-A version.
551 */
552 if (!receiver_size_and_offset_valid_for_version(
553 receivers_size, receivers_offset, ffa_version)) {
554 dlog_verbose(
555 "Invalid memory access descriptor size %d, "
556 " or receiver offset %d, "
557 "for FF-A version %#x\n",
558 receivers_size, receivers_offset, ffa_version);
559 return false;
560 }
561
562 receiver_count = memory_region->receiver_count;
563 }
564
565 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000566 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100567 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000568 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100569 "specified %u\n",
570 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
571 return false;
572 }
573
574 /* Check values in the memory access descriptors. */
575 /*
576 * The composite offset values must be the same for all recievers so
577 * check the first one is valid and then they are all the same.
578 */
579 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000580 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100581 ->receivers[0]
582 : ffa_memory_region_get_receiver(memory_region, 0);
583 assert(receiver != NULL);
584 composite_offset_0 = receiver->composite_memory_region_offset;
585
586 if (!send_transaction) {
587 if (composite_offset_0 != 0) {
588 dlog_verbose(
589 "Composite offset memory region descriptor "
590 "offset must be 0 for retrieve requests. "
591 "Currently %d",
592 composite_offset_0);
593 return false;
594 }
595 } else {
596 bool comp_offset_is_zero = composite_offset_0 == 0U;
597 bool comp_offset_lt_transaction_descriptor_size =
598 composite_offset_0 <
599 (sizeof(struct ffa_memory_region) +
600 (uint32_t)(memory_region->memory_access_desc_size *
601 memory_region->receiver_count));
602 bool comp_offset_with_comp_gt_fragment_length =
603 composite_offset_0 +
604 sizeof(struct ffa_composite_memory_region) >
605 fragment_length;
606 if (comp_offset_is_zero ||
607 comp_offset_lt_transaction_descriptor_size ||
608 comp_offset_with_comp_gt_fragment_length) {
609 dlog_verbose(
610 "Invalid composite memory region descriptor "
611 "offset for send transaction %u\n",
612 composite_offset_0);
613 return false;
614 }
615 }
616
617 for (int i = 0; i < memory_region->receiver_count; i++) {
618 uint32_t composite_offset;
619
620 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100621 struct ffa_memory_access_v1_0 *receiver_v1_0 =
622 &memory_region_v1_0->receivers[i];
623 /* Check reserved fields are 0 */
624 if (receiver_v1_0->reserved_0 != 0) {
625 dlog_verbose(
626 "Reserved field in the memory access "
627 " descriptor must be zero "
628 " Currently reciever %d has a reserved "
629 " field with a value of %d\n",
630 i, receiver_v1_0->reserved_0);
631 return false;
632 }
633 /*
634 * We can cast to the current version receiver as the
635 * remaining fields we are checking have the same
636 * offsets for all versions since memory access
637 * descriptors are forwards compatible.
638 */
639 receiver = (struct ffa_memory_access *)receiver_v1_0;
640 } else {
641 receiver = ffa_memory_region_get_receiver(memory_region,
642 i);
643 assert(receiver != NULL);
644
645 if (receiver->reserved_0 != 0) {
646 dlog_verbose(
647 "Reserved field in the memory access "
648 " descriptor must be zero "
649 " Currently reciever %d has a reserved "
650 " field with a value of %d\n",
651 i, receiver->reserved_0);
652 return false;
653 }
654 }
655
656 /* Check composite offset values are equal for all receivers. */
657 composite_offset = receiver->composite_memory_region_offset;
658 if (composite_offset != composite_offset_0) {
659 dlog_verbose(
660 "Composite offset %x differs from %x in index "
661 "%u\n",
662 composite_offset, composite_offset_0);
663 return false;
664 }
665 }
666 return true;
667}
668
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669/**
J-Alves460d36c2023-10-12 17:02:15 +0100670 * If the receivers for the memory management operation are all from the
671 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
672 * state update by returning MAP_ACTION_CHECK_PROTECT.
673 */
674static enum ffa_map_action ffa_mem_send_get_map_action(
675 bool all_receivers_from_current_world, ffa_id_t sender_id,
676 uint32_t mem_func_id)
677{
678 bool protect_memory =
679 (mem_func_id != FFA_MEM_SHARE_32 &&
680 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
681
682 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
683}
684
685/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 * Verify that all pages have the same mode, that the starting mode
687 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100688 * to the sending VM. It outputs the mapping action that needs to be
689 * invoked for the given memory range. On memory lend/donate there
690 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 *
692 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 * 1) FFA_DENIED if a state transition was not found;
694 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100695 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100697 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
699 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100702 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000703 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100704 struct ffa_memory_region_constituent **fragments,
705 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100706 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000707{
708 const uint32_t state_mask =
709 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100711 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000712 uint32_t receivers_count = memory_region->receiver_count;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000713
Andrew Walbranca808b12020-05-15 17:22:28 +0100714 ret = constituents_get_mode(from, orig_from_mode, fragments,
715 fragment_constituent_counts,
716 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100717 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100718 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100719 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100720 }
721
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000722 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100723 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000724 dlog_verbose("Can't share device memory (mode is %#x).\n",
725 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100726 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 }
728
729 /*
730 * Ensure the sender is the owner and has exclusive access to the
731 * memory.
732 */
733 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100735 }
736
Daniel Boulbya76fd912024-02-22 14:22:15 +0000737 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100738
J-Alves363f5722022-04-25 17:37:37 +0100739 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000740 struct ffa_memory_access *receiver =
741 ffa_memory_region_get_receiver(memory_region, i);
742 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100743 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000744 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100745 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
746 permissions, *orig_from_mode);
747
J-Alves788b4492023-04-18 14:01:23 +0100748 /*
749 * The assumption is that at this point, the operation from
750 * SP to a receiver VM, should have returned an FFA_ERROR
751 * already.
752 */
753 if (!ffa_is_vm_id(from.vm->id)) {
754 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000755 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100756 }
757
J-Alves460d36c2023-10-12 17:02:15 +0100758 /* Track if all senders are from current world. */
759 all_receivers_from_current_world =
760 all_receivers_from_current_world &&
761 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000762 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100763
J-Alves363f5722022-04-25 17:37:37 +0100764 if ((*orig_from_mode & required_from_mode) !=
765 required_from_mode) {
766 dlog_verbose(
767 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100768 "which required mode %#x but only had %#x "
769 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100770 required_from_mode, *orig_from_mode);
771 return ffa_error(FFA_DENIED);
772 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000773 }
774
J-Alves460d36c2023-10-12 17:02:15 +0100775 *map_action = ffa_mem_send_get_map_action(
776 all_receivers_from_current_world, from.vm->id, share_func);
777
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000778 /* Find the appropriate new mode. */
779 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000780 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100781 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000782 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100783 break;
784
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000786 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100787 break;
788
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100789 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000790 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100791 break;
792
Jose Marinho75509b42019-04-09 09:34:59 +0100793 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100794 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100795 }
796
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798}
799
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100800static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000801 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 struct ffa_memory_region_constituent **fragments,
803 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
804 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000805{
806 const uint32_t state_mask =
807 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
808 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000810
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 ret = constituents_get_mode(from, orig_from_mode, fragments,
812 fragment_constituent_counts,
813 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100815 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000816 }
817
818 /* Ensure the address range is normal memory and not a device. */
819 if (*orig_from_mode & MM_MODE_D) {
820 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
821 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100822 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000823 }
824
825 /*
826 * Ensure the relinquishing VM is not the owner but has access to the
827 * memory.
828 */
829 orig_from_state = *orig_from_mode & state_mask;
830 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
831 dlog_verbose(
832 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100833 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000834 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000836 }
837
838 /* Find the appropriate new mode. */
839 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
840
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100841 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000842}
843
844/**
845 * Verify that all pages have the same mode, that the starting mode
846 * constitutes a valid state and obtain the next mode to apply
847 * to the retrieving VM.
848 *
849 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 * 1) FFA_DENIED if a state transition was not found;
851 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100852 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100854 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
856 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857 */
J-Alvesfc19b372022-07-06 12:17:35 +0100858struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000859 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 struct ffa_memory_region_constituent **fragments,
861 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100862 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
863 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000864{
865 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867
Andrew Walbranca808b12020-05-15 17:22:28 +0100868 ret = constituents_get_mode(to, &orig_to_mode, fragments,
869 fragment_constituent_counts,
870 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100872 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100873 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 }
875
J-Alves460d36c2023-10-12 17:02:15 +0100876 /* Find the appropriate new mode. */
877 *to_mode = memory_to_attributes;
878
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000880 /*
881 * If the original ffa memory send call has been processed
882 * successfully, it is expected the orig_to_mode would overlay
883 * with `state_mask`, as a result of the function
884 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100885 *
886 * If Hafnium is the SPMC:
887 * - Caller of the reclaim interface is an SP, the memory shall
888 * have been protected throughout the flow.
889 * - Caller of the reclaim is from the NWd, the memory may have
890 * been protected at the time of lending/donating the memory.
891 * In such case, set action to unprotect memory in the
892 * handling of reclaim operation.
893 * - If Hafnium is the hypervisor memory shall never have been
894 * protected in memory lend/share/donate.
895 *
896 * More details in the doc comment of the function
897 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000898 */
J-Alves59ed0042022-07-28 18:26:41 +0100899 if (vm_id_is_current_world(to.vm->id)) {
900 assert((orig_to_mode &
901 (MM_MODE_INVALID | MM_MODE_UNOWNED |
902 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100903 assert(!memory_protected);
904 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
905 map_action != NULL && memory_protected) {
906 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100907 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000908 } else {
909 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100910 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000911 * Ensure the retriever has the expected state. We don't care
912 * about the MM_MODE_SHARED bit; either with or without it set
913 * are both valid representations of the !O-NA state.
914 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100915 if (vm_id_is_current_world(to.vm->id) &&
916 to.vm->id != HF_PRIMARY_VM_ID &&
917 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
918 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 }
J-Alves460d36c2023-10-12 17:02:15 +0100921
922 /*
923 * If memory has been protected before, clear the NS bit to
924 * allow the secure access from the SP.
925 */
926 if (memory_protected) {
927 *to_mode &= ~plat_ffa_other_world_mode();
928 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 }
930
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933 *to_mode |= 0;
934 break;
935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 *to_mode |= MM_MODE_UNOWNED;
938 break;
939
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000941 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
942 break;
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000945 *to_mode |= 0;
946 break;
947
948 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100949 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 }
952
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100954}
Jose Marinho09b1db82019-08-08 09:16:59 +0100955
J-Alvescf6253e2024-01-03 13:48:48 +0000956/*
957 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
958 * Returns:
959 * - FFA_SUCCESS_32: if all goes well.
960 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
961 * the page table update. Or error code provided by the function
962 * `arch_memory_protect`.
963 */
964static struct ffa_value ffa_region_group_check_actions(
965 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
966 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
967 bool *memory_protected)
968{
969 struct ffa_value ret;
970 bool is_memory_protected;
971
972 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
973 dlog_verbose(
974 "%s: memory can't be mapped to %x due to lack of "
975 "memory. Base: %lx end: %x\n",
976 __func__, vm_locked.vm->id, pa_addr(pa_begin),
977 pa_addr(pa_end));
978 return ffa_error(FFA_NO_MEMORY);
979 }
980
981 switch (action) {
982 case MAP_ACTION_CHECK:
983 /* No protect requested. */
984 is_memory_protected = false;
985 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
986 break;
987 case MAP_ACTION_CHECK_PROTECT: {
988 paddr_t last_protected_pa = pa_init(0);
989
990 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
991
992 is_memory_protected = (ret.func == FFA_SUCCESS_32);
993
994 /*
995 * - If protect memory has failed with FFA_DENIED, means some
996 * range of memory was in the wrong state. In such case, SPM
997 * reverts the state of the pages that were successfully
998 * updated.
999 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1000 * means the platform doesn't support the protection mechanism.
1001 * That said, it still permits the page table update to go
1002 * through. The variable
1003 * `is_memory_protected` will be equal to false.
1004 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1005 * break from switch and return the error.
1006 */
1007 if (ret.func == FFA_ERROR_32) {
1008 assert(!is_memory_protected);
1009 if (ffa_error_code(ret) == FFA_DENIED &&
1010 pa_addr(last_protected_pa) != (uintptr_t)0) {
1011 CHECK(arch_memory_unprotect(
1012 pa_begin,
1013 pa_add(last_protected_pa, PAGE_SIZE)));
1014 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1015 ret = (struct ffa_value){
1016 .func = FFA_SUCCESS_32,
1017 };
1018 }
1019 }
1020 } break;
1021 default:
1022 panic("%s: invalid action to process %x\n", __func__, action);
1023 }
1024
1025 if (memory_protected != NULL) {
1026 *memory_protected = is_memory_protected;
1027 }
1028
1029 return ret;
1030}
1031
1032static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1033 paddr_t pa_begin, paddr_t pa_end,
1034 struct mpool *ppool, uint32_t mode,
1035 enum ffa_map_action action)
1036{
1037 switch (action) {
1038 case MAP_ACTION_COMMIT_UNPROTECT:
1039 /*
1040 * Checking that it should succeed because SPM should be
1041 * unprotecting memory that it had protected before.
1042 */
1043 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1044 case MAP_ACTION_COMMIT:
1045 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1046 NULL);
1047 break;
1048 default:
1049 panic("%s: invalid action to process %x\n", __func__, action);
1050 }
1051}
1052
Jose Marinho09b1db82019-08-08 09:16:59 +01001053/**
J-Alves063ad832023-10-03 18:05:40 +01001054 * Helper function to revert a failed "Protect" action from the SPMC:
1055 * - `fragment_count`: should specify the number of fragments to traverse from
1056 * `fragments`. This may not be the full amount of fragments that are part of
1057 * the share_state structure.
1058 * - `fragment_constituent_counts`: array holding the amount of constituents
1059 * per fragment.
1060 * - `end`: pointer to the constituent that failed the "protect" action. It
1061 * shall be part of the last fragment, and it shall make the loop below break.
1062 */
1063static void ffa_region_group_fragments_revert_protect(
1064 struct ffa_memory_region_constituent **fragments,
1065 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1066 const struct ffa_memory_region_constituent *end)
1067{
1068 for (uint32_t i = 0; i < fragment_count; ++i) {
1069 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1070 struct ffa_memory_region_constituent *constituent =
1071 &fragments[i][j];
1072 size_t size = constituent->page_count * PAGE_SIZE;
1073 paddr_t pa_begin =
1074 pa_from_ipa(ipa_init(constituent->address));
1075 paddr_t pa_end = pa_add(pa_begin, size);
1076
1077 dlog_verbose("%s: reverting fragment %x size %x\n",
1078 __func__, pa_addr(pa_begin), size);
1079
1080 if (constituent == end) {
1081 /*
1082 * The last constituent is expected to be in the
1083 * last fragment.
1084 */
1085 assert(i == fragment_count - 1);
1086 break;
1087 }
1088
1089 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1090 }
1091 }
1092}
1093
1094/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001095 * Updates a VM's page table such that the given set of physical address ranges
1096 * are mapped in the address space at the corresponding address ranges, in the
1097 * mode provided.
1098 *
J-Alves0a83dc22023-05-05 09:50:37 +01001099 * The enum ffa_map_action determines the action taken from a call to the
1100 * function below:
1101 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1102 * mpool but no mappings will actually be updated. This function must always
1103 * be called first with action set to MAP_ACTION_CHECK to check that it will
1104 * succeed before calling ffa_region_group_identity_map with whichever one of
1105 * the remaining actions, to avoid leaving the page table in a half-updated
1106 * state.
1107 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1108 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001109 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1110 * invocation to the monitor to update the security state of the memory,
1111 * to that of the SPMC.
1112 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1113 * with a call into the monitor, to reset the security state of memory
1114 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001115 * vm_ptable_defrag should always be called after a series of page table
1116 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001117 *
J-Alvescf6253e2024-01-03 13:48:48 +00001118 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1119 * error codes:
1120 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1121 * - FFA_DENIED:
1122 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001123 * made to memory mappings.
1124 */
J-Alvescf6253e2024-01-03 13:48:48 +00001125struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001126 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001127 struct ffa_memory_region_constituent **fragments,
1128 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001129 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1130 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001131{
Andrew Walbranca808b12020-05-15 17:22:28 +01001132 uint32_t i;
1133 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001134 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001135
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001136 if (vm_locked.vm->el0_partition) {
1137 mode |= MM_MODE_USER | MM_MODE_NG;
1138 }
1139
Andrew Walbranca808b12020-05-15 17:22:28 +01001140 /* Iterate over the memory region constituents within each fragment. */
1141 for (i = 0; i < fragment_count; ++i) {
1142 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001143 struct ffa_memory_region_constituent *constituent =
1144 &fragments[i][j];
1145 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001146 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001147 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001149 uint32_t pa_bits =
1150 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001151
1152 /*
1153 * Ensure the requested region falls into system's PA
1154 * range.
1155 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001156 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1157 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001158 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001159 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001160 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001161
J-Alvescf6253e2024-01-03 13:48:48 +00001162 if (action <= MAP_ACTION_CHECK_PROTECT) {
1163 ret = ffa_region_group_check_actions(
1164 vm_locked, pa_begin, pa_end, ppool,
1165 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001166
1167 if (ret.func == FFA_ERROR_32 &&
1168 ffa_error_code(ret) == FFA_DENIED) {
1169 if (memory_protected != NULL) {
1170 assert(!*memory_protected);
1171 }
1172
1173 ffa_region_group_fragments_revert_protect(
1174 fragments,
1175 fragment_constituent_counts,
1176 i + 1, constituent);
1177 break;
1178 }
J-Alvescf6253e2024-01-03 13:48:48 +00001179 } else if (action >= MAP_ACTION_COMMIT &&
1180 action < MAP_ACTION_MAX) {
1181 ffa_region_group_commit_actions(
1182 vm_locked, pa_begin, pa_end, ppool,
1183 mode, action);
1184 ret = (struct ffa_value){
1185 .func = FFA_SUCCESS_32};
1186 } else {
1187 panic("%s: Unknown ffa_map_action.\n",
1188 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001190 }
1191 }
1192
J-Alvescf6253e2024-01-03 13:48:48 +00001193 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001194}
1195
1196/**
1197 * Clears a region of physical memory by overwriting it with zeros. The data is
1198 * flushed from the cache so the memory has been cleared across the system.
1199 */
J-Alves7db32002021-12-14 14:44:50 +00001200static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1201 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001202{
1203 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001204 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001205 * global mapping of the whole range. Such an approach will limit
1206 * the changes to stage-1 tables and will allow only local
1207 * invalidation.
1208 */
1209 bool ret;
1210 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001211 void *ptr = mm_identity_map(stage1_locked, begin, end,
1212 MM_MODE_W | (extra_mode_attributes &
1213 plat_ffa_other_world_mode()),
1214 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001215 size_t size = pa_difference(begin, end);
1216
1217 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001218 goto fail;
1219 }
1220
1221 memset_s(ptr, size, 0, size);
1222 arch_mm_flush_dcache(ptr, size);
1223 mm_unmap(stage1_locked, begin, end, ppool);
1224
1225 ret = true;
1226 goto out;
1227
1228fail:
1229 ret = false;
1230
1231out:
1232 mm_unlock_stage1(&stage1_locked);
1233
1234 return ret;
1235}
1236
1237/**
1238 * Clears a region of physical memory by overwriting it with zeros. The data is
1239 * flushed from the cache so the memory has been cleared across the system.
1240 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001242 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 struct ffa_memory_region_constituent **fragments,
1244 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1245 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001246{
1247 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001249 bool ret = false;
1250
1251 /*
1252 * Create a local pool so any freed memory can't be used by another
1253 * thread. This is to ensure each constituent that is mapped can be
1254 * unmapped again afterwards.
1255 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001256 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001257
Andrew Walbranca808b12020-05-15 17:22:28 +01001258 /* Iterate over the memory region constituents within each fragment. */
1259 for (i = 0; i < fragment_count; ++i) {
1260 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001261
J-Alves8457f932023-10-11 16:41:45 +01001262 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001263 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1264 paddr_t begin =
1265 pa_from_ipa(ipa_init(fragments[i][j].address));
1266 paddr_t end = pa_add(begin, size);
1267
J-Alves7db32002021-12-14 14:44:50 +00001268 if (!clear_memory(begin, end, &local_page_pool,
1269 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001270 /*
1271 * api_clear_memory will defrag on failure, so
1272 * no need to do it here.
1273 */
1274 goto out;
1275 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001276 }
1277 }
1278
Jose Marinho09b1db82019-08-08 09:16:59 +01001279 ret = true;
1280
1281out:
1282 mpool_fini(&local_page_pool);
1283 return ret;
1284}
1285
J-Alves5952d942022-12-22 16:03:00 +00001286static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1287 ipaddr_t in_begin, ipaddr_t in_end)
1288{
1289 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1290 ipa_addr(begin) < ipa_addr(in_end)) ||
1291 (ipa_addr(end) <= ipa_addr(in_end) &&
1292 ipa_addr(end) > ipa_addr(in_begin));
1293}
1294
1295/**
1296 * Receives a memory range and looks for overlaps with the remainder
1297 * constituents of the memory share/lend/donate operation. Assumes they are
1298 * passed in order to avoid having to loop over all the elements at each call.
1299 * The function only compares the received memory ranges with those that follow
1300 * within the same fragment, and subsequent fragments from the same operation.
1301 */
1302static bool ffa_memory_check_overlap(
1303 struct ffa_memory_region_constituent **fragments,
1304 const uint32_t *fragment_constituent_counts,
1305 const uint32_t fragment_count, const uint32_t current_fragment,
1306 const uint32_t current_constituent)
1307{
1308 uint32_t i = current_fragment;
1309 uint32_t j = current_constituent;
1310 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1311 const uint32_t current_page_count = fragments[i][j].page_count;
1312 size_t current_size = current_page_count * PAGE_SIZE;
1313 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1314
1315 if (current_size == 0 ||
1316 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1317 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1318 current_begin, current_page_count);
1319 return false;
1320 }
1321
1322 for (; i < fragment_count; i++) {
1323 j = (i == current_fragment) ? j + 1 : 0;
1324
1325 for (; j < fragment_constituent_counts[i]; j++) {
1326 ipaddr_t begin = ipa_init(fragments[i][j].address);
1327 const uint32_t page_count = fragments[i][j].page_count;
1328 size_t size = page_count * PAGE_SIZE;
1329 ipaddr_t end = ipa_add(begin, size - 1);
1330
1331 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1332 dlog_verbose(
1333 "Invalid page count. Addr: %x "
1334 "page_count: %x\n",
1335 begin, page_count);
1336 return false;
1337 }
1338
1339 /*
1340 * Check if current ranges is within begin and end, as
1341 * well as the reverse. This should help optimize the
1342 * loop, and reduce the number of iterations.
1343 */
1344 if (is_memory_range_within(begin, end, current_begin,
1345 current_end) ||
1346 is_memory_range_within(current_begin, current_end,
1347 begin, end)) {
1348 dlog_verbose(
1349 "Overlapping memory ranges: %#x - %#x "
1350 "with %#x - %#x\n",
1351 ipa_addr(begin), ipa_addr(end),
1352 ipa_addr(current_begin),
1353 ipa_addr(current_end));
1354 return true;
1355 }
1356 }
1357 }
1358
1359 return false;
1360}
1361
Jose Marinho09b1db82019-08-08 09:16:59 +01001362/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001363 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001364 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001365 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001366 *
1367 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001368 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001369 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001370 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001371 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1372 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001376 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001377static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001378 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001379 struct ffa_memory_region_constituent **fragments,
1380 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001381 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001382 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1383 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001384{
Andrew Walbranca808b12020-05-15 17:22:28 +01001385 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001386 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001387 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001388 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001389 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001390 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001392 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001393 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001394 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001395
1396 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397 * Make sure constituents are properly aligned to a 64-bit boundary. If
1398 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001399 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001400 for (i = 0; i < fragment_count; ++i) {
1401 if (!is_aligned(fragments[i], 8)) {
1402 dlog_verbose("Constituents not aligned.\n");
1403 return ffa_error(FFA_INVALID_PARAMETERS);
1404 }
J-Alves8f11cde2022-12-21 16:18:22 +00001405 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1406 constituents_total_page_count +=
1407 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001408 if (ffa_memory_check_overlap(
1409 fragments, fragment_constituent_counts,
1410 fragment_count, i, j)) {
1411 return ffa_error(FFA_INVALID_PARAMETERS);
1412 }
J-Alves8f11cde2022-12-21 16:18:22 +00001413 }
1414 }
1415
1416 if (constituents_total_page_count != composite_total_page_count) {
1417 dlog_verbose(
1418 "Composite page count differs from calculated page "
1419 "count from constituents.\n");
1420 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001421 }
1422
1423 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001424 * Check if the state transition is lawful for the sender, ensure that
1425 * all constituents of a memory region being shared are at the same
1426 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001427 */
J-Alves460d36c2023-10-12 17:02:15 +01001428 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001429 from_locked, share_func, memory_region, &orig_from_mode,
1430 fragments, fragment_constituent_counts, fragment_count,
1431 &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001433 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001435 }
1436
Andrew Walbran37c574e2020-06-03 11:45:46 +01001437 if (orig_from_mode_ret != NULL) {
1438 *orig_from_mode_ret = orig_from_mode;
1439 }
1440
Jose Marinho09b1db82019-08-08 09:16:59 +01001441 /*
1442 * Create a local pool so any freed memory can't be used by another
1443 * thread. This is to ensure the original mapping can be restored if the
1444 * clear fails.
1445 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001446 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001447
1448 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001449 * First reserve all required memory for the new page table entries
1450 * without committing, to make sure the entire operation will succeed
1451 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001452 * Provide the map_action as populated by 'ffa_send_check_transition'.
1453 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001454 */
J-Alvescf6253e2024-01-03 13:48:48 +00001455 ret = ffa_region_group_identity_map(
1456 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001457 fragment_count, from_mode, page_pool, map_action,
1458 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001459 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001460 goto out;
1461 }
1462
1463 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 * Update the mapping for the sender. This won't allocate because the
1465 * transaction was already prepared above, but may free pages in the
1466 * case that a whole block is being unmapped that was previously
1467 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001468 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001469 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001470 from_locked, fragments, fragment_constituent_counts,
1471 fragment_count, from_mode, &local_page_pool,
1472 MAP_ACTION_COMMIT, NULL)
1473 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001474
J-Alves460d36c2023-10-12 17:02:15 +01001475 /*
1476 * If memory has been protected, it is now part of the secure PAS
1477 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1478 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1479 * SPM's S1 translation.
1480 * In case memory hasn't been protected, and it is in the non-secure
1481 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1482 * perform a non-secure memory access. In such case `clean_mode` takes
1483 * the same mode as `orig_from_mode`.
1484 */
1485 clean_mode = (memory_protected != NULL && *memory_protected)
1486 ? orig_from_mode & ~plat_ffa_other_world_mode()
1487 : orig_from_mode;
1488
Jose Marinho09b1db82019-08-08 09:16:59 +01001489 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001490 if (clear && !ffa_clear_memory_constituents(
1491 clean_mode, fragments, fragment_constituent_counts,
1492 fragment_count, page_pool)) {
1493 map_action = (memory_protected != NULL && *memory_protected)
1494 ? MAP_ACTION_COMMIT_UNPROTECT
1495 : MAP_ACTION_COMMIT;
1496
Jose Marinho09b1db82019-08-08 09:16:59 +01001497 /*
1498 * On failure, roll back by returning memory to the sender. This
1499 * may allocate pages which were previously freed into
1500 * `local_page_pool` by the call above, but will never allocate
1501 * more pages than that so can never fail.
1502 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001504 from_locked, fragments,
1505 fragment_constituent_counts, fragment_count,
1506 orig_from_mode, &local_page_pool,
1507 MAP_ACTION_COMMIT, NULL)
1508 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001509 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001510 goto out;
1511 }
1512
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001514
1515out:
1516 mpool_fini(&local_page_pool);
1517
1518 /*
1519 * Tidy up the page table by reclaiming failed mappings (if there was an
1520 * error) or merging entries into blocks where possible (on success).
1521 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001522 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001523
1524 return ret;
1525}
1526
1527/**
1528 * Validates and maps memory shared from one VM to another.
1529 *
1530 * This function requires the calling context to hold the <to> lock.
1531 *
1532 * Returns:
1533 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001535 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001537 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001539 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001540struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001541 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001542 struct ffa_memory_region_constituent **fragments,
1543 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001544 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001545 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001546{
Andrew Walbranca808b12020-05-15 17:22:28 +01001547 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001548 uint32_t to_mode;
1549 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001551 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552
1553 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001554 * Make sure constituents are properly aligned to a 64-bit boundary. If
1555 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001556 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001557 for (i = 0; i < fragment_count; ++i) {
1558 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001559 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 return ffa_error(FFA_INVALID_PARAMETERS);
1561 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 }
1563
1564 /*
1565 * Check if the state transition is lawful for the recipient, and ensure
1566 * that all constituents of the memory region being retrieved are at the
1567 * same state.
1568 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 ret = ffa_retrieve_check_transition(
1570 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001571 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1572 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001573
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001574 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001575 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001576 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001577 }
1578
1579 /*
1580 * Create a local pool so any freed memory can't be used by another
1581 * thread. This is to ensure the original mapping can be restored if the
1582 * clear fails.
1583 */
1584 mpool_init_with_fallback(&local_page_pool, page_pool);
1585
1586 /*
1587 * First reserve all required memory for the new page table entries in
1588 * the recipient page tables without committing, to make sure the entire
1589 * operation will succeed without exhausting the page pool.
1590 */
J-Alvescf6253e2024-01-03 13:48:48 +00001591 ret = ffa_region_group_identity_map(
1592 to_locked, fragments, fragment_constituent_counts,
1593 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1594 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001595 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001596 goto out;
1597 }
1598
1599 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001600 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001601 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1602 fragment_constituent_counts,
1603 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001604 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001606 goto out;
1607 }
1608
Jose Marinho09b1db82019-08-08 09:16:59 +01001609 /*
1610 * Complete the transfer by mapping the memory into the recipient. This
1611 * won't allocate because the transaction was already prepared above, so
1612 * it doesn't need to use the `local_page_pool`.
1613 */
J-Alvesfd206052023-05-22 16:45:00 +01001614 CHECK(ffa_region_group_identity_map(
1615 to_locked, fragments, fragment_constituent_counts,
1616 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001617 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001618
J-Alves460d36c2023-10-12 17:02:15 +01001619 /* Return the mode used in mapping the memory in retriever's PT. */
1620 if (response_mode != NULL) {
1621 *response_mode = to_mode;
1622 }
1623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001624 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001625
1626out:
1627 mpool_fini(&local_page_pool);
1628
1629 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001630 * Tidy up the page table by reclaiming failed mappings (if there was an
1631 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001632 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001633 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001634
1635 return ret;
1636}
1637
Andrew Walbran996d1d12020-05-27 14:08:43 +01001638static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001639 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 struct ffa_memory_region_constituent **fragments,
1641 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1642 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001643{
1644 uint32_t orig_from_mode;
1645 uint32_t from_mode;
1646 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648
Andrew Walbranca808b12020-05-15 17:22:28 +01001649 ret = ffa_relinquish_check_transition(
1650 from_locked, &orig_from_mode, fragments,
1651 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001653 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001654 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 }
1656
1657 /*
1658 * Create a local pool so any freed memory can't be used by another
1659 * thread. This is to ensure the original mapping can be restored if the
1660 * clear fails.
1661 */
1662 mpool_init_with_fallback(&local_page_pool, page_pool);
1663
1664 /*
1665 * First reserve all required memory for the new page table entries
1666 * without committing, to make sure the entire operation will succeed
1667 * without exhausting the page pool.
1668 */
J-Alvescf6253e2024-01-03 13:48:48 +00001669 ret = ffa_region_group_identity_map(
1670 from_locked, fragments, fragment_constituent_counts,
1671 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1672 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001673 goto out;
1674 }
1675
1676 /*
1677 * Update the mapping for the sender. This won't allocate because the
1678 * transaction was already prepared above, but may free pages in the
1679 * case that a whole block is being unmapped that was previously
1680 * partially mapped.
1681 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001683 from_locked, fragments, fragment_constituent_counts,
1684 fragment_count, from_mode, &local_page_pool,
1685 MAP_ACTION_COMMIT, NULL)
1686 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687
1688 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001689 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001690 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1691 fragment_constituent_counts,
1692 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001693 /*
1694 * On failure, roll back by returning memory to the sender. This
1695 * may allocate pages which were previously freed into
1696 * `local_page_pool` by the call above, but will never allocate
1697 * more pages than that so can never fail.
1698 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001699 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001700 from_locked, fragments,
1701 fragment_constituent_counts, fragment_count,
1702 orig_from_mode, &local_page_pool,
1703 MAP_ACTION_COMMIT, NULL)
1704 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001705
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001706 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 goto out;
1708 }
1709
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001711
1712out:
1713 mpool_fini(&local_page_pool);
1714
1715 /*
1716 * Tidy up the page table by reclaiming failed mappings (if there was an
1717 * error) or merging entries into blocks where possible (on success).
1718 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001719 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001720
1721 return ret;
1722}
1723
1724/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 * Complete a memory sending operation by checking that it is valid, updating
1726 * the sender page table, and then either marking the share state as having
1727 * completed sending (on success) or freeing it (on failure).
1728 *
1729 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1730 */
J-Alvesfdd29272022-07-19 13:16:31 +01001731struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001732 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001733 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1734 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001735{
1736 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001737 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001738 struct ffa_value ret;
1739
1740 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001741 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001742 assert(memory_region != NULL);
1743 composite = ffa_memory_region_get_composite(memory_region, 0);
1744 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001745
1746 /* Check that state is valid in sender page table and update. */
1747 ret = ffa_send_check_update(
1748 from_locked, share_state->fragments,
1749 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001750 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001751 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001752 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001753 if (ret.func != FFA_SUCCESS_32) {
1754 /*
1755 * Free share state, it failed to send so it can't be retrieved.
1756 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001757 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1758 __func__, ffa_func_name(ret.func),
1759 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001760 share_state_free(share_states, share_state, page_pool);
1761 return ret;
1762 }
1763
1764 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001765 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001766
J-Alvesee68c542020-10-29 17:48:20 +00001767 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001768}
1769
1770/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001771 * Check that the memory attributes match Hafnium expectations:
1772 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1773 * Write-Allocate Cacheable.
1774 */
1775static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001776 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001777{
1778 enum ffa_memory_type memory_type;
1779 enum ffa_memory_cacheability cacheability;
1780 enum ffa_memory_shareability shareability;
1781
Karl Meakin84710f32023-10-12 15:14:49 +01001782 memory_type = attributes.type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001783 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001784 dlog_verbose("Invalid memory type %s, expected %s\n",
1785 ffa_memory_type_name(memory_type),
1786 ffa_memory_type_name(FFA_MEMORY_NORMAL_MEM));
Federico Recanati3d953f32022-02-17 09:31:29 +01001787 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001788 }
1789
Karl Meakin84710f32023-10-12 15:14:49 +01001790 cacheability = attributes.cacheability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001791 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001792 dlog_verbose("Invalid cacheability %s, expected %s.\n",
1793 ffa_memory_cacheability_name(cacheability),
1794 ffa_memory_cacheability_name(
1795 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001796 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001797 }
1798
Karl Meakin84710f32023-10-12 15:14:49 +01001799 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001800 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001801 dlog_verbose("Invalid shareability %s, expected %s.\n",
1802 ffa_memory_shareability_name(shareability),
1803 ffa_memory_shareability_name(
1804 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001805 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001806 }
1807
1808 return (struct ffa_value){.func = FFA_SUCCESS_32};
1809}
1810
1811/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001812 * Check that the given `memory_region` represents a valid memory send request
1813 * of the given `share_func` type, return the clear flag and permissions via the
1814 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001815 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001816 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001817 * not.
1818 */
J-Alves66652252022-07-06 09:49:51 +01001819struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001820 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1821 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001822 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001823{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001824 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001825 struct ffa_memory_access *receiver =
1826 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001827 uint64_t receivers_end;
1828 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001829 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001830 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001831 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001832 enum ffa_data_access data_access;
1833 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001834 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001835 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001836 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001837 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001838 memory_region->receivers_offset +
1839 memory_region->memory_access_desc_size +
1840 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001841
1842 if (fragment_length < minimum_first_fragment_length) {
1843 dlog_verbose("Fragment length %u too short (min %u).\n",
1844 (size_t)fragment_length,
1845 minimum_first_fragment_length);
1846 return ffa_error(FFA_INVALID_PARAMETERS);
1847 }
1848
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001849 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1850 "struct ffa_memory_region_constituent must be 16 bytes");
1851 if (!is_aligned(fragment_length,
1852 sizeof(struct ffa_memory_region_constituent)) ||
1853 !is_aligned(memory_share_length,
1854 sizeof(struct ffa_memory_region_constituent))) {
1855 dlog_verbose(
1856 "Fragment length %u or total length %u"
1857 " is not 16-byte aligned.\n",
1858 fragment_length, memory_share_length);
1859 return ffa_error(FFA_INVALID_PARAMETERS);
1860 }
1861
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001862 if (fragment_length > memory_share_length) {
1863 dlog_verbose(
1864 "Fragment length %u greater than total length %u.\n",
1865 (size_t)fragment_length, (size_t)memory_share_length);
1866 return ffa_error(FFA_INVALID_PARAMETERS);
1867 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001868
J-Alves95df0ef2022-12-07 10:09:48 +00001869 /* The sender must match the caller. */
1870 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1871 vm_id_is_current_world(memory_region->sender)) ||
1872 (vm_id_is_current_world(from_locked.vm->id) &&
1873 memory_region->sender != from_locked.vm->id)) {
1874 dlog_verbose("Invalid memory sender ID.\n");
1875 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001876 }
1877
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001878 if (memory_region->receiver_count <= 0) {
1879 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001880 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001881 }
1882
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001883 /*
1884 * Ensure that the composite header is within the memory bounds and
1885 * doesn't overlap the first part of the message. Cast to uint64_t
1886 * to prevent overflow.
1887 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001888 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001889 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001890 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001891 min_length = receivers_end +
1892 sizeof(struct ffa_composite_memory_region) +
1893 sizeof(struct ffa_memory_region_constituent);
1894 if (min_length > memory_share_length) {
1895 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1896 (size_t)memory_share_length, (size_t)min_length);
1897 return ffa_error(FFA_INVALID_PARAMETERS);
1898 }
1899
1900 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001901 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001902
1903 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001904 * Check that the composite memory region descriptor is after the access
1905 * descriptors, is at least 16-byte aligned, and fits in the first
1906 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001907 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001908 if ((composite_memory_region_offset < receivers_end) ||
1909 (composite_memory_region_offset % 16 != 0) ||
1910 (composite_memory_region_offset >
1911 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1912 dlog_verbose(
1913 "Invalid composite memory region descriptor offset "
1914 "%u.\n",
1915 (size_t)composite_memory_region_offset);
1916 return ffa_error(FFA_INVALID_PARAMETERS);
1917 }
1918
1919 /*
1920 * Compute the start of the constituent regions. Already checked
1921 * to be not more than fragment_length and thus not more than
1922 * memory_share_length.
1923 */
1924 constituents_start = composite_memory_region_offset +
1925 sizeof(struct ffa_composite_memory_region);
1926 constituents_length = memory_share_length - constituents_start;
1927
1928 /*
1929 * Check that the number of constituents is consistent with the length
1930 * of the constituent region.
1931 */
1932 composite = ffa_memory_region_get_composite(memory_region, 0);
1933 if ((constituents_length %
1934 sizeof(struct ffa_memory_region_constituent) !=
1935 0) ||
1936 ((constituents_length /
1937 sizeof(struct ffa_memory_region_constituent)) !=
1938 composite->constituent_count)) {
1939 dlog_verbose("Invalid length %u or composite offset %u.\n",
1940 (size_t)memory_share_length,
1941 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001942 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001943 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001944 if (fragment_length < memory_share_length &&
1945 fragment_length < HF_MAILBOX_SIZE) {
1946 dlog_warning(
1947 "Initial fragment length %d smaller than mailbox "
1948 "size.\n",
1949 fragment_length);
1950 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001951
Andrew Walbrana65a1322020-04-06 19:32:32 +01001952 /*
1953 * Clear is not allowed for memory sharing, as the sender still has
1954 * access to the memory.
1955 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001956 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1957 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001958 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001960 }
1961
1962 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001963 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001964 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001965 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001966 }
1967
J-Alves363f5722022-04-25 17:37:37 +01001968 /* Check that the permissions are valid, for each specified receiver. */
1969 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001970 struct ffa_memory_region_attributes receiver_permissions;
1971
1972 receiver = ffa_memory_region_get_receiver(memory_region, i);
1973 assert(receiver != NULL);
1974 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001975 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001976 receiver_permissions.permissions;
1977 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001978
1979 if (memory_region->sender == receiver_id) {
1980 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001981 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001982 }
Federico Recanati85090c42021-12-15 13:17:54 +01001983
J-Alves363f5722022-04-25 17:37:37 +01001984 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1985 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001986 struct ffa_memory_access *other_receiver =
1987 ffa_memory_region_get_receiver(memory_region,
1988 j);
1989 assert(other_receiver != NULL);
1990
J-Alves363f5722022-04-25 17:37:37 +01001991 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001992 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001993 dlog_verbose(
1994 "Repeated receiver(%x) in memory send "
1995 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001996 other_receiver->receiver_permissions
1997 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001998 return ffa_error(FFA_INVALID_PARAMETERS);
1999 }
2000 }
2001
2002 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002003 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002004 dlog_verbose(
2005 "All ffa_memory_access should point to the "
2006 "same composite memory region offset.\n");
2007 return ffa_error(FFA_INVALID_PARAMETERS);
2008 }
2009
Karl Meakin84710f32023-10-12 15:14:49 +01002010 data_access = permissions.data_access;
2011 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002012 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2013 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2014 dlog_verbose(
2015 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002016 "(data_access = %s, instruction_access = %s)\n",
2017 ffa_data_access_name(data_access),
2018 ffa_instruction_access_name(
2019 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002020 return ffa_error(FFA_INVALID_PARAMETERS);
2021 }
2022 if (instruction_access !=
2023 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2024 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002025 "Invalid instruction access permissions %s "
2026 "for sending memory, expected %s.\n",
2027 ffa_instruction_access_name(instruction_access),
2028 ffa_instruction_access_name(
2029 FFA_INSTRUCTION_ACCESS_RESERVED));
J-Alves363f5722022-04-25 17:37:37 +01002030 return ffa_error(FFA_INVALID_PARAMETERS);
2031 }
2032 if (share_func == FFA_MEM_SHARE_32) {
2033 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2034 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002035 "Invalid data access permissions %s "
2036 "for sharing memory, expected %s.\n",
2037 ffa_data_access_name(data_access),
2038 ffa_data_access_name(
2039 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002040 return ffa_error(FFA_INVALID_PARAMETERS);
2041 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002042 /*
2043 * According to section 10.10.3 of the FF-A v1.1 EAC0
2044 * spec, NX is required for share operations (but must
2045 * not be specified by the sender) so set it in the
2046 * copy that we store, ready to be returned to the
2047 * retriever.
2048 */
2049 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002050 permissions.instruction_access =
2051 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002052 receiver_permissions.permissions = permissions;
2053 }
J-Alves363f5722022-04-25 17:37:37 +01002054 }
2055 if (share_func == FFA_MEM_LEND_32 &&
2056 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2057 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002058 "Invalid data access permissions %s for "
2059 "lending memory, expected %s.\n",
2060 ffa_data_access_name(data_access),
2061 ffa_data_access_name(
2062 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002063 return ffa_error(FFA_INVALID_PARAMETERS);
2064 }
2065
2066 if (share_func == FFA_MEM_DONATE_32 &&
2067 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2068 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002069 "Invalid data access permissions %s for "
2070 "donating memory, expected %s.\n",
2071 ffa_data_access_name(data_access),
2072 ffa_data_access_name(
2073 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002074 return ffa_error(FFA_INVALID_PARAMETERS);
2075 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002076 }
2077
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002078 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002079 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002080 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2081 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002082 "Invalid security state %s for memory share operation, "
2083 "expected %s.\n",
2084 ffa_memory_security_name(security_state),
2085 ffa_memory_security_name(
2086 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002087 return ffa_error(FFA_INVALID_PARAMETERS);
2088 }
2089
Federico Recanatid937f5e2021-12-20 17:38:23 +01002090 /*
J-Alves807794e2022-06-16 13:42:47 +01002091 * If a memory donate or lend with single borrower, the memory type
2092 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002093 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002094 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002095 if (share_func == FFA_MEM_DONATE_32 ||
2096 (share_func == FFA_MEM_LEND_32 &&
2097 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002098 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002099 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002100 "Invalid memory type %s for memory share "
2101 "operation, expected %s.\n",
2102 ffa_memory_type_name(type),
2103 ffa_memory_type_name(
2104 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002105 return ffa_error(FFA_INVALID_PARAMETERS);
2106 }
2107 } else {
2108 /*
2109 * Check that sender's memory attributes match Hafnium
2110 * expectations: Normal Memory, Inner shareable, Write-Back
2111 * Read-Allocate Write-Allocate Cacheable.
2112 */
2113 ret = ffa_memory_attributes_validate(memory_region->attributes);
2114 if (ret.func != FFA_SUCCESS_32) {
2115 return ret;
2116 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002117 }
2118
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002119 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002120}
2121
2122/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002123 * Gets the share state for continuing an operation to donate, lend or share
2124 * memory, and checks that it is a valid request.
2125 *
2126 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2127 * not.
2128 */
J-Alvesfdd29272022-07-19 13:16:31 +01002129struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002130 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002131 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002132 struct mpool *page_pool)
2133{
2134 struct ffa_memory_share_state *share_state;
2135 struct ffa_memory_region *memory_region;
2136
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002137 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002138
2139 /*
2140 * Look up the share state by handle and make sure that the VM ID
2141 * matches.
2142 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002143 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002144 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002145 dlog_verbose(
2146 "Invalid handle %#x for memory send continuation.\n",
2147 handle);
2148 return ffa_error(FFA_INVALID_PARAMETERS);
2149 }
2150 memory_region = share_state->memory_region;
2151
J-Alvesfdd29272022-07-19 13:16:31 +01002152 if (vm_id_is_current_world(from_vm_id) &&
2153 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002154 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2155 return ffa_error(FFA_INVALID_PARAMETERS);
2156 }
2157
2158 if (share_state->sending_complete) {
2159 dlog_verbose(
2160 "Sending of memory handle %#x is already complete.\n",
2161 handle);
2162 return ffa_error(FFA_INVALID_PARAMETERS);
2163 }
2164
2165 if (share_state->fragment_count == MAX_FRAGMENTS) {
2166 /*
2167 * Log a warning as this is a sign that MAX_FRAGMENTS should
2168 * probably be increased.
2169 */
2170 dlog_warning(
2171 "Too many fragments for memory share with handle %#x; "
2172 "only %d supported.\n",
2173 handle, MAX_FRAGMENTS);
2174 /* Free share state, as it's not possible to complete it. */
2175 share_state_free(share_states, share_state, page_pool);
2176 return ffa_error(FFA_NO_MEMORY);
2177 }
2178
2179 *share_state_ret = share_state;
2180
2181 return (struct ffa_value){.func = FFA_SUCCESS_32};
2182}
2183
2184/**
J-Alves95df0ef2022-12-07 10:09:48 +00002185 * Checks if there is at least one receiver from the other world.
2186 */
J-Alvesfdd29272022-07-19 13:16:31 +01002187bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002188 struct ffa_memory_region *memory_region)
2189{
2190 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002191 struct ffa_memory_access *receiver =
2192 ffa_memory_region_get_receiver(memory_region, i);
2193 assert(receiver != NULL);
2194 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2195
2196 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002197 return true;
2198 }
2199 }
2200 return false;
2201}
2202
2203/**
J-Alves9da280b2022-12-21 14:55:39 +00002204 * Validates a call to donate, lend or share memory in which Hafnium is the
2205 * designated allocator of the memory handle. In practice, this also means
2206 * Hafnium is responsible for managing the state structures for the transaction.
2207 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2208 * sender is an SP or there is at least one borrower that is an SP.
2209 * If Hafnium is the hypervisor, it should allocate the memory handle when
2210 * operation involves only NWd VMs.
2211 *
2212 * If validation goes well, Hafnium updates the stage-2 page tables of the
2213 * sender. Validation consists of checking if the message length and number of
2214 * memory region constituents match, and if the transition is valid for the
2215 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002216 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002217 * Assumes that the caller has already found and locked the sender VM and copied
2218 * the memory region descriptor from the sender's TX buffer to a freshly
2219 * allocated page from Hafnium's internal pool. The caller must have also
2220 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002222 * This function takes ownership of the `memory_region` passed in and will free
2223 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002224 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002225struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002226 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002227 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002228 uint32_t fragment_length, uint32_t share_func,
2229 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002230{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002231 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002232 struct share_states_locked share_states;
2233 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002234
2235 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 * If there is an error validating the `memory_region` then we need to
2237 * free it because we own it but we won't be storing it in a share state
2238 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002239 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002240 ret = ffa_memory_send_validate(from_locked, memory_region,
2241 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002242 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002243 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002244 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002245 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002246 }
2247
Andrew Walbrana65a1322020-04-06 19:32:32 +01002248 /* Set flag for share function, ready to be retrieved later. */
2249 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002250 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002251 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002252 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002253 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002254 case FFA_MEM_LEND_32:
2255 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002256 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002257 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002258 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002259 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002260 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002261 }
2262
Andrew Walbranca808b12020-05-15 17:22:28 +01002263 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002264 /*
2265 * Allocate a share state before updating the page table. Otherwise if
2266 * updating the page table succeeded but allocating the share state
2267 * failed then it would leave the memory in a state where nobody could
2268 * get it back.
2269 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002270 share_state = allocate_share_state(share_states, share_func,
2271 memory_region, fragment_length,
2272 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002273 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002274 dlog_verbose("Failed to allocate share state.\n");
2275 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002276 ret = ffa_error(FFA_NO_MEMORY);
2277 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002278 }
2279
Andrew Walbranca808b12020-05-15 17:22:28 +01002280 if (fragment_length == memory_share_length) {
2281 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002282 ret = ffa_memory_send_complete(
2283 from_locked, share_states, share_state, page_pool,
2284 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002285 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002286 /*
2287 * Use sender ID from 'memory_region' assuming
2288 * that at this point it has been validated:
2289 * - MBZ at virtual FF-A instance.
2290 */
J-Alves19e20cf2023-08-02 12:48:55 +01002291 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002292 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2293 ? memory_region->sender
2294 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002295 ret = (struct ffa_value){
2296 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002297 .arg1 = (uint32_t)memory_region->handle,
2298 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002299 .arg3 = fragment_length,
2300 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002301 }
2302
2303out:
2304 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002305 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002306 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002307}
2308
2309/**
J-Alves8505a8a2022-06-15 18:10:18 +01002310 * Continues an operation to donate, lend or share memory to a VM from current
2311 * world. If this is the last fragment then checks that the transition is valid
2312 * for the type of memory sending operation and updates the stage-2 page tables
2313 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002314 *
2315 * Assumes that the caller has already found and locked the sender VM and copied
2316 * the memory region descriptor from the sender's TX buffer to a freshly
2317 * allocated page from Hafnium's internal pool.
2318 *
2319 * This function takes ownership of the `fragment` passed in; it must not be
2320 * freed by the caller.
2321 */
2322struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2323 void *fragment,
2324 uint32_t fragment_length,
2325 ffa_memory_handle_t handle,
2326 struct mpool *page_pool)
2327{
2328 struct share_states_locked share_states = share_states_lock();
2329 struct ffa_memory_share_state *share_state;
2330 struct ffa_value ret;
2331 struct ffa_memory_region *memory_region;
2332
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002333 CHECK(is_aligned(fragment,
2334 alignof(struct ffa_memory_region_constituent)));
2335 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2336 0) {
2337 dlog_verbose("Fragment length %u misaligned.\n",
2338 fragment_length);
2339 ret = ffa_error(FFA_INVALID_PARAMETERS);
2340 goto out_free_fragment;
2341 }
2342
Andrew Walbranca808b12020-05-15 17:22:28 +01002343 ret = ffa_memory_send_continue_validate(share_states, handle,
2344 &share_state,
2345 from_locked.vm->id, page_pool);
2346 if (ret.func != FFA_SUCCESS_32) {
2347 goto out_free_fragment;
2348 }
2349 memory_region = share_state->memory_region;
2350
J-Alves95df0ef2022-12-07 10:09:48 +00002351 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002352 dlog_error(
2353 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002354 "other world. This should never happen, and indicates "
2355 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002356 "EL3 code.\n");
2357 ret = ffa_error(FFA_INVALID_PARAMETERS);
2358 goto out_free_fragment;
2359 }
2360
2361 /* Add this fragment. */
2362 share_state->fragments[share_state->fragment_count] = fragment;
2363 share_state->fragment_constituent_counts[share_state->fragment_count] =
2364 fragment_length / sizeof(struct ffa_memory_region_constituent);
2365 share_state->fragment_count++;
2366
2367 /* Check whether the memory send operation is now ready to complete. */
2368 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002369 ret = ffa_memory_send_complete(
2370 from_locked, share_states, share_state, page_pool,
2371 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002372 } else {
2373 ret = (struct ffa_value){
2374 .func = FFA_MEM_FRAG_RX_32,
2375 .arg1 = (uint32_t)handle,
2376 .arg2 = (uint32_t)(handle >> 32),
2377 .arg3 = share_state_next_fragment_offset(share_states,
2378 share_state)};
2379 }
2380 goto out;
2381
2382out_free_fragment:
2383 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002384
2385out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002386 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002387 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002388}
2389
Andrew Walbranca808b12020-05-15 17:22:28 +01002390/** Clean up after the receiver has finished retrieving a memory region. */
2391static void ffa_memory_retrieve_complete(
2392 struct share_states_locked share_states,
2393 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2394{
2395 if (share_state->share_func == FFA_MEM_DONATE_32) {
2396 /*
2397 * Memory that has been donated can't be relinquished,
2398 * so no need to keep the share state around.
2399 */
2400 share_state_free(share_states, share_state, page_pool);
2401 dlog_verbose("Freed share state for donate.\n");
2402 }
2403}
2404
J-Alves2d8457f2022-10-05 11:06:41 +01002405/**
2406 * Initialises the given memory region descriptor to be used for an
2407 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2408 * fragment.
2409 * The memory region descriptor is initialized according to retriever's
2410 * FF-A version.
2411 *
2412 * Returns true on success, or false if the given constituents won't all fit in
2413 * the first fragment.
2414 */
2415static bool ffa_retrieved_memory_region_init(
2416 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002417 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002418 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002419 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002420 struct ffa_memory_access *receivers, size_t receiver_count,
2421 uint32_t memory_access_desc_size, uint32_t page_count,
2422 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002423 const struct ffa_memory_region_constituent constituents[],
2424 uint32_t fragment_constituent_count, uint32_t *total_length,
2425 uint32_t *fragment_length)
2426{
2427 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002428 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002429 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002430 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002431
2432 assert(response != NULL);
2433
2434 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2435 struct ffa_memory_region_v1_0 *retrieve_response =
2436 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002437 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002438
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002439 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2440 attributes, flags, handle, 0,
2441 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002442
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002443 receiver = (struct ffa_memory_access_v1_0 *)
2444 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002445 receiver_count = retrieve_response->receiver_count;
2446
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002447 for (uint32_t i = 0; i < receiver_count; i++) {
2448 ffa_id_t receiver_id =
2449 receivers[i].receiver_permissions.receiver;
2450 ffa_memory_receiver_flags_t recv_flags =
2451 receivers[i].receiver_permissions.flags;
2452
2453 /*
2454 * Initialized here as in memory retrieve responses we
2455 * currently expect one borrower to be specified.
2456 */
2457 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002458 receiver, receiver_id, permissions.data_access,
2459 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002460 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002461
2462 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002463 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002464 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2465 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002466
2467 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2468 retrieve_response, 0);
2469 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002470 struct ffa_memory_region *retrieve_response =
2471 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002472 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002473
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002474 ffa_memory_region_init_header(
2475 retrieve_response, sender, attributes, flags, handle, 0,
2476 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002477
2478 /*
2479 * Note that `sizeof(struct_ffa_memory_region)` and
2480 * `sizeof(struct ffa_memory_access)` must both be multiples of
2481 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2482 * guaranteed that the offset we calculate here is aligned to a
2483 * 64-bit boundary and so 64-bit values can be copied without
2484 * alignment faults.
2485 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002486 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002487 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002488 (uint32_t)(receiver_count *
2489 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002490
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002491 retrieve_response_receivers =
2492 ffa_memory_region_get_receiver(retrieve_response, 0);
2493 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002494
2495 /*
2496 * Initialized here as in memory retrieve responses we currently
2497 * expect one borrower to be specified.
2498 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002499 memcpy_s(retrieve_response_receivers,
2500 sizeof(struct ffa_memory_access) * receiver_count,
2501 receivers,
2502 sizeof(struct ffa_memory_access) * receiver_count);
2503
2504 retrieve_response_receivers->composite_memory_region_offset =
2505 composite_offset;
2506
J-Alves2d8457f2022-10-05 11:06:41 +01002507 composite_memory_region =
2508 ffa_memory_region_get_composite(retrieve_response, 0);
2509 }
2510
J-Alves2d8457f2022-10-05 11:06:41 +01002511 assert(composite_memory_region != NULL);
2512
J-Alves2d8457f2022-10-05 11:06:41 +01002513 composite_memory_region->page_count = page_count;
2514 composite_memory_region->constituent_count = total_constituent_count;
2515 composite_memory_region->reserved_0 = 0;
2516
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002517 constituents_offset =
2518 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002519 if (constituents_offset +
2520 fragment_constituent_count *
2521 sizeof(struct ffa_memory_region_constituent) >
2522 response_max_size) {
2523 return false;
2524 }
2525
2526 for (i = 0; i < fragment_constituent_count; ++i) {
2527 composite_memory_region->constituents[i] = constituents[i];
2528 }
2529
2530 if (total_length != NULL) {
2531 *total_length =
2532 constituents_offset +
2533 composite_memory_region->constituent_count *
2534 sizeof(struct ffa_memory_region_constituent);
2535 }
2536 if (fragment_length != NULL) {
2537 *fragment_length =
2538 constituents_offset +
2539 fragment_constituent_count *
2540 sizeof(struct ffa_memory_region_constituent);
2541 }
2542
2543 return true;
2544}
2545
J-Alves96de29f2022-04-26 16:05:24 +01002546/**
2547 * Validates the retrieved permissions against those specified by the lender
2548 * of memory share operation. Optionally can help set the permissions to be used
2549 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002550 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2551 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2552 * specification for each ABI.
2553 * - FFA_DENIED -> if the permissions specified by the retriever are not
2554 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002555 */
J-Alvesdcad8992023-09-15 14:10:35 +01002556static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2557 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002558 enum ffa_data_access requested_data_access,
2559 enum ffa_instruction_access sent_instruction_access,
2560 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002561 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002562{
2563 switch (sent_data_access) {
2564 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2565 case FFA_DATA_ACCESS_RW:
2566 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2567 requested_data_access == FFA_DATA_ACCESS_RW) {
2568 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002569 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002570 }
2571 break;
2572 }
2573 /* Intentional fall-through. */
2574 case FFA_DATA_ACCESS_RO:
2575 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2576 requested_data_access == FFA_DATA_ACCESS_RO) {
2577 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002578 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002579 }
2580 break;
2581 }
2582 dlog_verbose(
2583 "Invalid data access requested; sender specified "
2584 "permissions %#x but receiver requested %#x.\n",
2585 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002586 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002587 case FFA_DATA_ACCESS_RESERVED:
2588 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2589 "checked before this point.");
2590 }
2591
J-Alvesdcad8992023-09-15 14:10:35 +01002592 /*
2593 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2594 * or FFA_MEMORY_DONATE the retriever should have specifed the
2595 * instruction permissions it wishes to receive.
2596 */
2597 switch (share_func) {
2598 case FFA_MEM_SHARE_32:
2599 if (requested_instruction_access !=
2600 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2601 dlog_verbose(
2602 "%s: for share instruction permissions must "
2603 "NOT be specified.\n",
2604 __func__);
2605 return ffa_error(FFA_INVALID_PARAMETERS);
2606 }
2607 break;
2608 case FFA_MEM_LEND_32:
2609 /*
2610 * For operations with multiple borrowers only permit XN
2611 * permissions, and both Sender and borrower should have used
2612 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2613 */
2614 if (multiple_borrowers) {
2615 if (requested_instruction_access !=
2616 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2617 dlog_verbose(
2618 "%s: lend/share/donate with multiple "
2619 "borrowers "
2620 "instruction permissions must NOT be "
2621 "specified.\n",
2622 __func__);
2623 return ffa_error(FFA_INVALID_PARAMETERS);
2624 }
2625 break;
2626 }
2627 /* Fall through if the operation targets a single borrower. */
2628 case FFA_MEM_DONATE_32:
2629 if (!multiple_borrowers &&
2630 requested_instruction_access ==
2631 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2632 dlog_verbose(
2633 "%s: for lend/donate with single borrower "
2634 "instruction permissions must be speficified "
2635 "by borrower\n",
2636 __func__);
2637 return ffa_error(FFA_INVALID_PARAMETERS);
2638 }
2639 break;
2640 default:
2641 panic("%s: Wrong func id provided.\n", __func__);
2642 }
2643
J-Alves96de29f2022-04-26 16:05:24 +01002644 switch (sent_instruction_access) {
2645 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2646 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002647 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002648 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002649 permissions->instruction_access =
2650 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002651 }
2652 break;
2653 }
J-Alvesdcad8992023-09-15 14:10:35 +01002654 /*
2655 * Fall through if requested permissions are less
2656 * permissive than those provided by the sender.
2657 */
J-Alves96de29f2022-04-26 16:05:24 +01002658 case FFA_INSTRUCTION_ACCESS_NX:
2659 if (requested_instruction_access ==
2660 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2661 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2662 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002663 permissions->instruction_access =
2664 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002665 }
2666 break;
2667 }
2668 dlog_verbose(
2669 "Invalid instruction access requested; sender "
2670 "specified permissions %#x but receiver requested "
2671 "%#x.\n",
2672 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002673 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002674 case FFA_INSTRUCTION_ACCESS_RESERVED:
2675 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2676 "be checked before this point.");
2677 }
2678
J-Alvesdcad8992023-09-15 14:10:35 +01002679 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002680}
2681
2682/**
2683 * Validate the receivers' permissions in the retrieve request against those
2684 * specified by the lender.
2685 * In the `permissions` argument returns the permissions to set at S2 for the
2686 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002687 * The function looks into the flag to bypass multiple borrower checks:
2688 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2689 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2690 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2691 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002692 */
2693static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2694 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002695 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002696 ffa_memory_access_permissions_t *permissions,
2697 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002698{
2699 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002700 bool bypass_multi_receiver_check =
2701 (retrieve_request->flags &
2702 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002703 const uint32_t region_receiver_count = memory_region->receiver_count;
2704 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002705
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002706 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002707 assert(permissions != NULL);
2708
Karl Meakin84710f32023-10-12 15:14:49 +01002709 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002710
J-Alves3456e032023-07-20 12:20:05 +01002711 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002712 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002713 dlog_verbose(
2714 "Retrieve request should contain same list of "
2715 "borrowers, as specified by the lender.\n");
2716 return ffa_error(FFA_INVALID_PARAMETERS);
2717 }
2718 } else {
2719 if (retrieve_request->receiver_count != 1) {
2720 dlog_verbose(
2721 "Set bypass multiple borrower check, receiver "
2722 "list must be sized 1 (%x)\n",
2723 memory_region->receiver_count);
2724 return ffa_error(FFA_INVALID_PARAMETERS);
2725 }
J-Alves96de29f2022-04-26 16:05:24 +01002726 }
2727
2728 retrieve_receiver_index = retrieve_request->receiver_count;
2729
J-Alves96de29f2022-04-26 16:05:24 +01002730 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2731 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002732 struct ffa_memory_access *retrieve_request_receiver =
2733 ffa_memory_region_get_receiver(retrieve_request, i);
2734 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002735 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002736 retrieve_request_receiver->receiver_permissions
2737 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002738 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002739 retrieve_request_receiver->receiver_permissions
2740 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002741 struct ffa_memory_access *receiver;
2742 uint32_t mem_region_receiver_index;
2743 bool permissions_RO;
2744 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002745 bool found_to_id = current_receiver_id == to_vm_id;
2746
J-Alves3456e032023-07-20 12:20:05 +01002747 if (bypass_multi_receiver_check && !found_to_id) {
2748 dlog_verbose(
2749 "Bypass multiple borrower check for id %x.\n",
2750 current_receiver_id);
2751 continue;
2752 }
2753
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002754 if (retrieve_request_receiver->composite_memory_region_offset !=
2755 0U) {
2756 dlog_verbose(
2757 "Retriever specified address ranges not "
2758 "supported (got offset %d).\n",
2759 retrieve_request_receiver
2760 ->composite_memory_region_offset);
2761 return ffa_error(FFA_INVALID_PARAMETERS);
2762 }
2763
J-Alves96de29f2022-04-26 16:05:24 +01002764 /*
2765 * Find the current receiver in the transaction descriptor from
2766 * sender.
2767 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002768 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002769 ffa_memory_region_get_receiver_index(
2770 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002771
2772 if (mem_region_receiver_index ==
2773 memory_region->receiver_count) {
2774 dlog_verbose("%s: receiver %x not found\n", __func__,
2775 current_receiver_id);
2776 return ffa_error(FFA_DENIED);
2777 }
2778
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002779 receiver = ffa_memory_region_get_receiver(
2780 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002781 assert(receiver != NULL);
2782
2783 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002784
2785 if (found_to_id) {
2786 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002787
2788 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002789 }
2790
2791 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002792 * Check if retrieve request memory access list is valid:
2793 * - The retrieve request complies with the specification.
2794 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002795 */
J-Alvesdcad8992023-09-15 14:10:35 +01002796 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002797 func_id, sent_permissions.data_access,
2798 requested_permissions.data_access,
2799 sent_permissions.instruction_access,
2800 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002801 found_to_id ? permissions : NULL,
2802 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002803
J-Alvesdcad8992023-09-15 14:10:35 +01002804 if (ret.func != FFA_SUCCESS_32) {
2805 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002806 }
2807
Karl Meakin84710f32023-10-12 15:14:49 +01002808 permissions_RO =
2809 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002810 clear_memory_flags =
2811 (retrieve_request->flags &
2812 (FFA_MEMORY_REGION_FLAG_CLEAR |
2813 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002814
J-Alves96de29f2022-04-26 16:05:24 +01002815 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002816 * Can't request PM to clear memory if only provided
2817 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002818 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002819 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002820 dlog_verbose(
2821 "Receiver has RO permissions can not request "
2822 "clear.\n");
2823 return ffa_error(FFA_DENIED);
2824 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002825
2826 /*
2827 * Check the impdef in the retrieve_request matches the value in
2828 * the original memory send.
2829 */
2830 if (ffa_version_from_memory_access_desc_size(
2831 memory_region->memory_access_desc_size) >=
2832 MAKE_FFA_VERSION(1, 2) &&
2833 ffa_version_from_memory_access_desc_size(
2834 retrieve_request->memory_access_desc_size) >=
2835 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002836 if (receiver->impdef.val[0] !=
2837 retrieve_request_receiver->impdef.val[0] ||
2838 receiver->impdef.val[1] !=
2839 retrieve_request_receiver->impdef.val[1]) {
2840 dlog_verbose(
2841 "Impdef value in memory send does not "
2842 "match retrieve request value "
2843 "send value %#x %#x retrieve request "
2844 "value %#x %#x\n",
2845 receiver->impdef.val[0],
2846 receiver->impdef.val[1],
2847 retrieve_request_receiver->impdef
2848 .val[0],
2849 retrieve_request_receiver->impdef
2850 .val[1]);
2851 return ffa_error(FFA_INVALID_PARAMETERS);
2852 }
2853 }
J-Alves96de29f2022-04-26 16:05:24 +01002854 }
2855
2856 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2857 dlog_verbose(
2858 "Retrieve request does not contain caller's (%x) "
2859 "permissions\n",
2860 to_vm_id);
2861 return ffa_error(FFA_INVALID_PARAMETERS);
2862 }
2863
2864 return (struct ffa_value){.func = FFA_SUCCESS_32};
2865}
2866
J-Alvesa9cd7e32022-07-01 13:49:33 +01002867/*
2868 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2869 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2870 * of a pending memory sharing operation whose allocator is the SPM, for
2871 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2872 * the memory region descriptor of the retrieve request must be zeroed with the
2873 * exception of the sender ID and handle.
2874 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002875bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2876 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002877{
2878 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01002879 request->attributes.shareability == 0U &&
2880 request->attributes.cacheability == 0U &&
2881 request->attributes.type == 0U &&
2882 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01002883 request->tag == 0U && request->receiver_count == 0U &&
2884 plat_ffa_memory_handle_allocated_by_current_world(
2885 request->handle);
2886}
2887
2888/*
2889 * Helper to reset count of fragments retrieved by the hypervisor.
2890 */
2891static void ffa_memory_retrieve_complete_from_hyp(
2892 struct ffa_memory_share_state *share_state)
2893{
2894 if (share_state->hypervisor_fragment_count ==
2895 share_state->fragment_count) {
2896 share_state->hypervisor_fragment_count = 0;
2897 }
2898}
2899
J-Alves089004f2022-07-13 14:25:44 +01002900/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002901 * Prepares the return of the ffa_value for the memory retrieve response.
2902 */
2903static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2904 uint32_t fragment_length)
2905{
2906 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2907 .arg1 = total_length,
2908 .arg2 = fragment_length};
2909}
2910
2911/**
J-Alves089004f2022-07-13 14:25:44 +01002912 * Validate that the memory region descriptor provided by the borrower on
2913 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2914 * memory sharing call.
2915 */
2916static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002917 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2918 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002919 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2920 uint32_t share_func)
2921{
2922 ffa_memory_region_flags_t transaction_type =
2923 retrieve_request->flags &
2924 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002925 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002926 const uint64_t memory_access_desc_size =
2927 retrieve_request->memory_access_desc_size;
2928 const uint32_t expected_retrieve_request_length =
2929 retrieve_request->receivers_offset +
2930 (uint32_t)(retrieve_request->receiver_count *
2931 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002932
2933 assert(retrieve_request != NULL);
2934 assert(memory_region != NULL);
2935 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01002936
J-Alves4f0d9c12024-01-17 17:23:11 +00002937 if (retrieve_request_length != expected_retrieve_request_length) {
2938 dlog_verbose(
2939 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2940 "but was %d.\n",
2941 expected_retrieve_request_length,
2942 retrieve_request_length);
2943 return ffa_error(FFA_INVALID_PARAMETERS);
2944 }
2945
2946 if (retrieve_request->sender != memory_region->sender) {
2947 dlog_verbose(
2948 "Memory with handle %#x not fully sent, can't "
2949 "retrieve.\n",
2950 memory_region->handle);
2951 return ffa_error(FFA_DENIED);
2952 }
2953
2954 /*
2955 * The SPMC can only process retrieve requests to memory share
2956 * operations with one borrower from the other world. It can't
2957 * determine the ID of the NWd VM that invoked the retrieve
2958 * request interface call. It relies on the hypervisor to
2959 * validate the caller's ID against that provided in the
2960 * `receivers` list of the retrieve response.
2961 * In case there is only one borrower from the NWd in the
2962 * transaction descriptor, record that in the `receiver_id` for
2963 * later use, and validate in the retrieve request message.
2964 * This limitation is due to the fact SPMC can't determine the
2965 * index in the memory share structures state to update.
2966 */
2967 if (to_id == HF_HYPERVISOR_VM_ID) {
2968 uint32_t other_world_count = 0;
2969
2970 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2971 struct ffa_memory_access *receiver =
2972 ffa_memory_region_get_receiver(retrieve_request,
2973 0);
2974 assert(receiver != NULL);
2975
2976 to_id = receiver->receiver_permissions.receiver;
2977
2978 if (!vm_id_is_current_world(to_id)) {
2979 other_world_count++;
2980 }
2981 }
2982
2983 if (other_world_count > 1) {
2984 dlog_verbose(
2985 "Support one receiver from the other "
2986 "world.\n");
2987 return ffa_error(FFA_NOT_SUPPORTED);
2988 }
2989 }
J-Alves089004f2022-07-13 14:25:44 +01002990 /*
2991 * Check that the transaction type expected by the receiver is
2992 * correct, if it has been specified.
2993 */
2994 if (transaction_type !=
2995 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2996 transaction_type != (memory_region->flags &
2997 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2998 dlog_verbose(
2999 "Incorrect transaction type %#x for "
3000 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
3001 transaction_type,
3002 memory_region->flags &
3003 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3004 retrieve_request->handle);
3005 return ffa_error(FFA_INVALID_PARAMETERS);
3006 }
3007
3008 if (retrieve_request->tag != memory_region->tag) {
3009 dlog_verbose(
3010 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
3011 "%d for handle %#x.\n",
3012 retrieve_request->tag, memory_region->tag,
3013 retrieve_request->handle);
3014 return ffa_error(FFA_INVALID_PARAMETERS);
3015 }
3016
J-Alves4f0d9c12024-01-17 17:23:11 +00003017 *receiver_index =
3018 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003019
3020 if (*receiver_index == memory_region->receiver_count) {
3021 dlog_verbose(
3022 "Incorrect receiver VM ID %d for "
3023 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003024 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003025 return ffa_error(FFA_INVALID_PARAMETERS);
3026 }
3027
3028 if ((retrieve_request->flags &
3029 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3030 dlog_verbose(
3031 "Retriever specified 'address range alignment 'hint' "
3032 "not supported.\n");
3033 return ffa_error(FFA_INVALID_PARAMETERS);
3034 }
3035 if ((retrieve_request->flags &
3036 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3037 dlog_verbose(
3038 "Bits 8-5 must be zero in memory region's flags "
3039 "(address range alignment hint not supported).\n");
3040 return ffa_error(FFA_INVALID_PARAMETERS);
3041 }
3042
3043 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3044 dlog_verbose(
3045 "Bits 31-10 must be zero in memory region's flags.\n");
3046 return ffa_error(FFA_INVALID_PARAMETERS);
3047 }
3048
3049 if (share_func == FFA_MEM_SHARE_32 &&
3050 (retrieve_request->flags &
3051 (FFA_MEMORY_REGION_FLAG_CLEAR |
3052 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3053 dlog_verbose(
3054 "Memory Share operation can't clean after relinquish "
3055 "memory region.\n");
3056 return ffa_error(FFA_INVALID_PARAMETERS);
3057 }
3058
3059 /*
3060 * If the borrower needs the memory to be cleared before mapping
3061 * to its address space, the sender should have set the flag
3062 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3063 * FFA_DENIED.
3064 */
3065 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3066 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3067 dlog_verbose(
3068 "Borrower needs memory cleared. Sender needs to set "
3069 "flag for clearing memory.\n");
3070 return ffa_error(FFA_DENIED);
3071 }
3072
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003073 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003074 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003075 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3076 dlog_verbose(
3077 "Invalid security state for memory retrieve request "
3078 "operation.\n");
3079 return ffa_error(FFA_INVALID_PARAMETERS);
3080 }
3081
J-Alves089004f2022-07-13 14:25:44 +01003082 /*
3083 * If memory type is not specified, bypass validation of memory
3084 * attributes in the retrieve request. The retriever is expecting to
3085 * obtain this information from the SPMC.
3086 */
Karl Meakin84710f32023-10-12 15:14:49 +01003087 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003088 return (struct ffa_value){.func = FFA_SUCCESS_32};
3089 }
3090
3091 /*
3092 * Ensure receiver's attributes are compatible with how
3093 * Hafnium maps memory: Normal Memory, Inner shareable,
3094 * Write-Back Read-Allocate Write-Allocate Cacheable.
3095 */
3096 return ffa_memory_attributes_validate(retrieve_request->attributes);
3097}
3098
J-Alves4f0d9c12024-01-17 17:23:11 +00003099static struct ffa_value ffa_partition_retrieve_request(
3100 struct share_states_locked share_states,
3101 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3102 struct ffa_memory_region *retrieve_request,
3103 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003104{
Karl Meakin84710f32023-10-12 15:14:49 +01003105 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003106 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003107 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003108 struct ffa_composite_memory_region *composite;
3109 uint32_t total_length;
3110 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003111 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003112 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003113 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003114 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003115 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003116 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003117 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003118 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003119 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003120 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003121
J-Alves96de29f2022-04-26 16:05:24 +01003122 if (!share_state->sending_complete) {
3123 dlog_verbose(
3124 "Memory with handle %#x not fully sent, can't "
3125 "retrieve.\n",
3126 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003127 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003128 }
3129
J-Alves4f0d9c12024-01-17 17:23:11 +00003130 /*
3131 * Validate retrieve request, according to what was sent by the
3132 * sender. Function will output the `receiver_index` from the
3133 * provided memory region.
3134 */
3135 ret = ffa_memory_retrieve_validate(
3136 receiver_id, retrieve_request, retrieve_request_length,
3137 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003138
J-Alves4f0d9c12024-01-17 17:23:11 +00003139 if (ret.func != FFA_SUCCESS_32) {
3140 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003141 }
J-Alves96de29f2022-04-26 16:05:24 +01003142
J-Alves4f0d9c12024-01-17 17:23:11 +00003143 /*
3144 * Validate the requested permissions against the sent
3145 * permissions.
3146 * Outputs the permissions to give to retriever at S2
3147 * PTs.
3148 */
3149 ret = ffa_memory_retrieve_validate_memory_access_list(
3150 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003151 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003152 if (ret.func != FFA_SUCCESS_32) {
3153 return ret;
3154 }
3155
3156 memory_to_mode = ffa_memory_permissions_to_mode(
3157 permissions, share_state->sender_orig_mode);
3158
3159 ret = ffa_retrieve_check_update(
3160 to_locked, share_state->fragments,
3161 share_state->fragment_constituent_counts,
3162 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003163 share_state->share_func, false, page_pool, &retrieve_mode,
3164 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003165
3166 if (ret.func != FFA_SUCCESS_32) {
3167 return ret;
3168 }
3169
3170 share_state->retrieved_fragment_count[receiver_index] = 1;
3171
3172 is_retrieve_complete =
3173 share_state->retrieved_fragment_count[receiver_index] ==
3174 share_state->fragment_count;
3175
J-Alvesb5084cf2022-07-06 14:20:12 +01003176 /* VMs acquire the RX buffer from SPMC. */
3177 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3178
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003179 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003180 * Copy response to RX buffer of caller and deliver the message.
3181 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003182 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003183 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003184
Andrew Walbranca808b12020-05-15 17:22:28 +01003185 /*
J-Alves460d36c2023-10-12 17:02:15 +01003186 * Set the security state in the memory retrieve response attributes
3187 * if specified by the target mode.
3188 */
3189 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3190 retrieve_mode);
3191
3192 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003193 * Constituents which we received in the first fragment should
3194 * always fit in the first fragment we are sending, because the
3195 * header is the same size in both cases and we have a fixed
3196 * message buffer size. So `ffa_retrieved_memory_region_init`
3197 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003198 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003199
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003200 /* Provide the permissions that had been provided. */
3201 receiver->receiver_permissions.permissions = permissions;
3202
3203 /*
3204 * Prepare the memory region descriptor for the retrieve response.
3205 * Provide the pointer to the receiver tracked in the share state
3206 * strucutures.
3207 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003208 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003209 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003210 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003211 memory_region->flags, handle, permissions, receiver, 1,
3212 memory_access_desc_size, composite->page_count,
3213 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003214 share_state->fragment_constituent_counts[0], &total_length,
3215 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003216
J-Alves4f0d9c12024-01-17 17:23:11 +00003217 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003218 ffa_memory_retrieve_complete(share_states, share_state,
3219 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003220 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003221
3222 return ffa_memory_retrieve_resp(total_length, fragment_length);
3223}
3224
3225static struct ffa_value ffa_hypervisor_retrieve_request(
3226 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3227 struct ffa_memory_region *retrieve_request)
3228{
3229 struct ffa_value ret;
3230 struct ffa_composite_memory_region *composite;
3231 uint32_t total_length;
3232 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003233 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003234 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003235 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003236 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003237 ffa_memory_handle_t handle = retrieve_request->handle;
3238
J-Alves4f0d9c12024-01-17 17:23:11 +00003239 memory_region = share_state->memory_region;
3240
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003241 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3242
J-Alves7b6ab612024-01-24 09:54:54 +00003243 switch (to_locked.vm->ffa_version) {
3244 case MAKE_FFA_VERSION(1, 2):
3245 memory_access_desc_size = sizeof(struct ffa_memory_access);
3246 break;
3247 case MAKE_FFA_VERSION(1, 0):
3248 case MAKE_FFA_VERSION(1, 1):
3249 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3250 break;
3251 default:
3252 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3253 }
3254
J-Alves4f0d9c12024-01-17 17:23:11 +00003255 if (share_state->hypervisor_fragment_count != 0U) {
3256 dlog_verbose(
3257 "Memory with handle %#x already retrieved by "
3258 "the hypervisor.\n",
3259 handle);
3260 return ffa_error(FFA_DENIED);
3261 }
3262
3263 share_state->hypervisor_fragment_count = 1;
3264
3265 ffa_memory_retrieve_complete_from_hyp(share_state);
3266
3267 /* VMs acquire the RX buffer from SPMC. */
3268 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3269
3270 /*
3271 * Copy response to RX buffer of caller and deliver the message.
3272 * This must be done before the share_state is (possibly) freed.
3273 */
3274 composite = ffa_memory_region_get_composite(memory_region, 0);
3275
3276 /*
3277 * Constituents which we received in the first fragment should
3278 * always fit in the first fragment we are sending, because the
3279 * header is the same size in both cases and we have a fixed
3280 * message buffer size. So `ffa_retrieved_memory_region_init`
3281 * should never fail.
3282 */
3283
3284 /*
3285 * Set the security state in the memory retrieve response attributes
3286 * if specified by the target mode.
3287 */
3288 attributes = plat_ffa_memory_security_mode(
3289 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003290
3291 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3292
J-Alves4f0d9c12024-01-17 17:23:11 +00003293 CHECK(ffa_retrieved_memory_region_init(
3294 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3295 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003296 memory_region->flags, handle,
3297 receiver->receiver_permissions.permissions, receiver,
3298 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003299 composite->page_count, composite->constituent_count,
3300 share_state->fragments[0],
3301 share_state->fragment_constituent_counts[0], &total_length,
3302 &fragment_length));
3303
3304 return ffa_memory_retrieve_resp(total_length, fragment_length);
3305}
3306
3307struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3308 struct ffa_memory_region *retrieve_request,
3309 uint32_t retrieve_request_length,
3310 struct mpool *page_pool)
3311{
3312 ffa_memory_handle_t handle = retrieve_request->handle;
3313 struct share_states_locked share_states;
3314 struct ffa_memory_share_state *share_state;
3315 struct ffa_value ret;
3316
3317 dump_share_states();
3318
3319 share_states = share_states_lock();
3320 share_state = get_share_state(share_states, handle);
3321 if (share_state == NULL) {
3322 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3323 handle);
3324 ret = ffa_error(FFA_INVALID_PARAMETERS);
3325 goto out;
3326 }
3327
3328 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3329 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3330 retrieve_request);
3331 } else {
3332 ret = ffa_partition_retrieve_request(
3333 share_states, share_state, to_locked, retrieve_request,
3334 retrieve_request_length, page_pool);
3335 }
3336
3337 /* Track use of the RX buffer if the handling has succeeded. */
3338 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3339 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3340 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3341 }
3342
Andrew Walbranca808b12020-05-15 17:22:28 +01003343out:
3344 share_states_unlock(&share_states);
3345 dump_share_states();
3346 return ret;
3347}
3348
J-Alves5da37d92022-10-24 16:33:48 +01003349/**
3350 * Determine expected fragment offset according to the FF-A version of
3351 * the caller.
3352 */
3353static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3354 struct ffa_memory_region *memory_region,
3355 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3356{
3357 uint32_t expected_fragment_offset;
3358 uint32_t composite_constituents_offset;
3359
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003360 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003361 /*
3362 * Hafnium operates memory regions in FF-A v1.1 format, so we
3363 * can retrieve the constituents offset from descriptor.
3364 */
3365 composite_constituents_offset =
3366 ffa_composite_constituent_offset(memory_region, 0);
3367 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3368 /*
3369 * If retriever is FF-A v1.0, determine the composite offset
3370 * as it is expected to have been configured in the
3371 * retrieve response.
3372 */
3373 composite_constituents_offset =
3374 sizeof(struct ffa_memory_region_v1_0) +
3375 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003376 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003377 sizeof(struct ffa_composite_memory_region);
3378 } else {
3379 panic("%s received an invalid FF-A version.\n", __func__);
3380 }
3381
3382 expected_fragment_offset =
3383 composite_constituents_offset +
3384 retrieved_constituents_count *
3385 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003386 (uint32_t)(memory_region->memory_access_desc_size *
3387 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003388
3389 return expected_fragment_offset;
3390}
3391
Andrew Walbranca808b12020-05-15 17:22:28 +01003392struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3393 ffa_memory_handle_t handle,
3394 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003395 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003396 struct mpool *page_pool)
3397{
3398 struct ffa_memory_region *memory_region;
3399 struct share_states_locked share_states;
3400 struct ffa_memory_share_state *share_state;
3401 struct ffa_value ret;
3402 uint32_t fragment_index;
3403 uint32_t retrieved_constituents_count;
3404 uint32_t i;
3405 uint32_t expected_fragment_offset;
3406 uint32_t remaining_constituent_count;
3407 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003408 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003409 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003410
3411 dump_share_states();
3412
3413 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003414 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003415 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003416 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3417 handle);
3418 ret = ffa_error(FFA_INVALID_PARAMETERS);
3419 goto out;
3420 }
3421
3422 memory_region = share_state->memory_region;
3423 CHECK(memory_region != NULL);
3424
Andrew Walbranca808b12020-05-15 17:22:28 +01003425 if (!share_state->sending_complete) {
3426 dlog_verbose(
3427 "Memory with handle %#x not fully sent, can't "
3428 "retrieve.\n",
3429 handle);
3430 ret = ffa_error(FFA_INVALID_PARAMETERS);
3431 goto out;
3432 }
3433
J-Alves59ed0042022-07-28 18:26:41 +01003434 /*
3435 * If retrieve request from the hypervisor has been initiated in the
3436 * given share_state, continue it, else assume it is a continuation of
3437 * retrieve request from a NWd VM.
3438 */
3439 continue_ffa_hyp_mem_retrieve_req =
3440 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3441 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003442 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003443
J-Alves59ed0042022-07-28 18:26:41 +01003444 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003445 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003446 memory_region, to_locked.vm->id);
3447
3448 if (receiver_index == memory_region->receiver_count) {
3449 dlog_verbose(
3450 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3451 "borrower to memory sharing transaction (%x)\n",
3452 to_locked.vm->id, handle);
3453 ret = ffa_error(FFA_INVALID_PARAMETERS);
3454 goto out;
3455 }
3456
3457 if (share_state->retrieved_fragment_count[receiver_index] ==
3458 0 ||
3459 share_state->retrieved_fragment_count[receiver_index] >=
3460 share_state->fragment_count) {
3461 dlog_verbose(
3462 "Retrieval of memory with handle %#x not yet "
3463 "started or already completed (%d/%d fragments "
3464 "retrieved).\n",
3465 handle,
3466 share_state->retrieved_fragment_count
3467 [receiver_index],
3468 share_state->fragment_count);
3469 ret = ffa_error(FFA_INVALID_PARAMETERS);
3470 goto out;
3471 }
3472
3473 fragment_index =
3474 share_state->retrieved_fragment_count[receiver_index];
3475 } else {
3476 if (share_state->hypervisor_fragment_count == 0 ||
3477 share_state->hypervisor_fragment_count >=
3478 share_state->fragment_count) {
3479 dlog_verbose(
3480 "Retrieve of memory with handle %x not "
3481 "started from hypervisor.\n",
3482 handle);
3483 ret = ffa_error(FFA_INVALID_PARAMETERS);
3484 goto out;
3485 }
3486
3487 if (memory_region->sender != sender_vm_id) {
3488 dlog_verbose(
3489 "Sender ID (%x) is not as expected for memory "
3490 "handle %x\n",
3491 sender_vm_id, handle);
3492 ret = ffa_error(FFA_INVALID_PARAMETERS);
3493 goto out;
3494 }
3495
3496 fragment_index = share_state->hypervisor_fragment_count;
3497
3498 receiver_index = 0;
3499 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003500
3501 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003502 * Check that the given fragment offset is correct by counting
3503 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003504 */
3505 retrieved_constituents_count = 0;
3506 for (i = 0; i < fragment_index; ++i) {
3507 retrieved_constituents_count +=
3508 share_state->fragment_constituent_counts[i];
3509 }
J-Alvesc7484f12022-05-13 12:41:14 +01003510
3511 CHECK(memory_region->receiver_count > 0);
3512
Andrew Walbranca808b12020-05-15 17:22:28 +01003513 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003514 ffa_memory_retrieve_expected_offset_per_ffa_version(
3515 memory_region, retrieved_constituents_count,
3516 to_locked.vm->ffa_version);
3517
Andrew Walbranca808b12020-05-15 17:22:28 +01003518 if (fragment_offset != expected_fragment_offset) {
3519 dlog_verbose("Fragment offset was %d but expected %d.\n",
3520 fragment_offset, expected_fragment_offset);
3521 ret = ffa_error(FFA_INVALID_PARAMETERS);
3522 goto out;
3523 }
3524
J-Alves4f0d9c12024-01-17 17:23:11 +00003525 /*
3526 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3527 * is currently ownder by the SPMC.
3528 */
3529 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003530
Andrew Walbranca808b12020-05-15 17:22:28 +01003531 remaining_constituent_count = ffa_memory_fragment_init(
3532 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3533 share_state->fragments[fragment_index],
3534 share_state->fragment_constituent_counts[fragment_index],
3535 &fragment_length);
3536 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003537
Andrew Walbranca808b12020-05-15 17:22:28 +01003538 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003539 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003540
J-Alves59ed0042022-07-28 18:26:41 +01003541 if (!continue_ffa_hyp_mem_retrieve_req) {
3542 share_state->retrieved_fragment_count[receiver_index]++;
3543 if (share_state->retrieved_fragment_count[receiver_index] ==
3544 share_state->fragment_count) {
3545 ffa_memory_retrieve_complete(share_states, share_state,
3546 page_pool);
3547 }
3548 } else {
3549 share_state->hypervisor_fragment_count++;
3550
3551 ffa_memory_retrieve_complete_from_hyp(share_state);
3552 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003553 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3554 .arg1 = (uint32_t)handle,
3555 .arg2 = (uint32_t)(handle >> 32),
3556 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003557
3558out:
3559 share_states_unlock(&share_states);
3560 dump_share_states();
3561 return ret;
3562}
3563
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003564struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003565 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003566 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003567{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003568 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003569 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003570 struct ffa_memory_share_state *share_state;
3571 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003572 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003573 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003574 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003575 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003576 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003577
Andrew Walbrana65a1322020-04-06 19:32:32 +01003578 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003579 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003580 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003581 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003582 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003583 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003584 }
3585
Andrew Walbrana65a1322020-04-06 19:32:32 +01003586 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003587 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003588 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003589 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003590 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003591 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003592 }
3593
3594 dump_share_states();
3595
3596 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003597 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003598 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003599 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003600 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003601 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003602 goto out;
3603 }
3604
Andrew Walbranca808b12020-05-15 17:22:28 +01003605 if (!share_state->sending_complete) {
3606 dlog_verbose(
3607 "Memory with handle %#x not fully sent, can't "
3608 "relinquish.\n",
3609 handle);
3610 ret = ffa_error(FFA_INVALID_PARAMETERS);
3611 goto out;
3612 }
3613
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003614 memory_region = share_state->memory_region;
3615 CHECK(memory_region != NULL);
3616
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003617 receiver_index = ffa_memory_region_get_receiver_index(
3618 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003619
3620 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003621 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003622 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003623 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003624 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003625 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003626 goto out;
3627 }
3628
J-Alves8eb19162022-04-28 10:56:48 +01003629 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003630 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003631 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003632 "Memory with handle %#x not yet fully "
3633 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003634 "receiver %x can't relinquish.\n",
3635 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003636 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003637 goto out;
3638 }
3639
J-Alves3c5b2072022-11-21 12:45:40 +00003640 /*
3641 * Either clear if requested in relinquish call, or in a retrieve
3642 * request from one of the borrowers.
3643 */
3644 receivers_relinquished_memory = true;
3645
3646 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3647 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003648 ffa_memory_region_get_receiver(memory_region, i);
3649 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003650 if (receiver->receiver_permissions.receiver ==
3651 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003652 receiver_permissions =
3653 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003654 continue;
3655 }
3656
3657 if (share_state->retrieved_fragment_count[i] != 0U) {
3658 receivers_relinquished_memory = false;
3659 break;
3660 }
3661 }
3662
3663 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003664 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3665 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003666
3667 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003668 * Clear is not allowed for memory that was shared, as the
3669 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003670 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003671 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003672 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003673 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003674 goto out;
3675 }
3676
Karl Meakin84710f32023-10-12 15:14:49 +01003677 if (clear && receiver_permissions.instruction_access == 0 &&
3678 receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003679 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3680 __func__);
3681 ret = ffa_error(FFA_DENIED);
3682 goto out;
3683 }
3684
Andrew Walbranca808b12020-05-15 17:22:28 +01003685 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003686 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003687 share_state->fragment_constituent_counts,
3688 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003689
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003690 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003691 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003692 * Mark memory handle as not retrieved, so it can be
3693 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003694 */
J-Alves8eb19162022-04-28 10:56:48 +01003695 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003696 }
3697
3698out:
3699 share_states_unlock(&share_states);
3700 dump_share_states();
3701 return ret;
3702}
3703
3704/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003705 * Validates that the reclaim transition is allowed for the given
3706 * handle, updates the page table of the reclaiming VM, and frees the
3707 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003708 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003709struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003710 ffa_memory_handle_t handle,
3711 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003712 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003713{
3714 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003715 struct ffa_memory_share_state *share_state;
3716 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003717 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003718
3719 dump_share_states();
3720
3721 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003722
Karl Meakin4a2854a2023-06-30 16:26:52 +01003723 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003724 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003725 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003726 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003727 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003728 goto out;
3729 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003730 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003731
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003732 CHECK(memory_region != NULL);
3733
J-Alvesa9cd7e32022-07-01 13:49:33 +01003734 if (vm_id_is_current_world(to_locked.vm->id) &&
3735 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003736 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003737 "VM %#x attempted to reclaim memory handle %#x "
3738 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003739 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003740 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003741 goto out;
3742 }
3743
Andrew Walbranca808b12020-05-15 17:22:28 +01003744 if (!share_state->sending_complete) {
3745 dlog_verbose(
3746 "Memory with handle %#x not fully sent, can't "
3747 "reclaim.\n",
3748 handle);
3749 ret = ffa_error(FFA_INVALID_PARAMETERS);
3750 goto out;
3751 }
3752
J-Alves752236c2022-04-28 11:07:47 +01003753 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3754 if (share_state->retrieved_fragment_count[i] != 0) {
3755 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003756 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003757 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003758 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003759 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003760 ffa_memory_region_get_receiver(memory_region, i)
3761 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003762 ret = ffa_error(FFA_DENIED);
3763 goto out;
3764 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003765 }
3766
Andrew Walbranca808b12020-05-15 17:22:28 +01003767 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003768 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003769 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003770 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003771 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003772 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003773
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003774 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003775 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003776 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003777 }
3778
3779out:
3780 share_states_unlock(&share_states);
3781 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003782}