blob: cb216e26b9ef1490378ebd9a3fb9586f47b091e3 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
J-Alves7b9cc432024-04-04 10:57:17 +010011#include "hf/arch/memcpy_trapped.h"
Federico Recanati4fd065d2021-12-13 20:06:23 +010012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020014#include "hf/arch/plat/ffa.h"
Karl Meakin9724b362024-10-15 14:35:02 +010015#include "hf/arch/plat/ffa/indirect_messaging.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
Karl Meakine8937d92024-03-19 16:04:25 +0000280 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
281 "type = %s, security = %s), flags %#x, handle %#lx "
282 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100283 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000284 memory_region->sender,
285 ffa_memory_shareability_name(
286 memory_region->attributes.shareability),
287 ffa_memory_cacheability_name(
288 memory_region->attributes.cacheability),
289 ffa_memory_type_name(memory_region->attributes.type),
290 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000291 memory_region->flags, memory_region->handle, memory_region->tag,
292 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100293 memory_region->receiver_count);
294 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000295 struct ffa_memory_access *receiver =
296 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000297 if (i != 0) {
298 dlog(", ");
299 }
Karl Meakine8937d92024-03-19 16:04:25 +0000300 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000301 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000302 ffa_data_access_name(receiver->receiver_permissions
303 .permissions.data_access),
304 ffa_instruction_access_name(
305 receiver->receiver_permissions.permissions
306 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000307 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000308 /* The impdef field is only present from v1.2 and later */
309 if (ffa_version_from_memory_access_desc_size(
310 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100311 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000312 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000313 receiver->impdef.val[1]);
314 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000315 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000316 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000317}
318
J-Alves66652252022-07-06 09:49:51 +0100319void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320{
321 uint32_t i;
322
323 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
324 return;
325 }
326
327 dlog("Current share states:\n");
328 sl_lock(&share_states_lock_instance);
329 for (i = 0; i < MAX_MEM_SHARES; ++i) {
330 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000331 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000332 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100333 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334 dlog("SHARE");
335 break;
J-Alves95fbb312024-03-20 15:19:16 +0000336 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338 dlog("LEND");
339 break;
J-Alves95fbb312024-03-20 15:19:16 +0000340 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000342 dlog("DONATE");
343 break;
344 default:
345 dlog("invalid share_func %#x",
346 share_states[i].share_func);
347 }
Karl Meakine8937d92024-03-19 16:04:25 +0000348 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000349 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100350 if (share_states[i].sending_complete) {
351 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000352 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100353 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 }
J-Alves2a0d2882020-10-29 14:49:50 +0000355 dlog(" with %d fragments, %d retrieved, "
356 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100357 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000358 share_states[i].retrieved_fragment_count[0],
359 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000360 }
361 }
362 sl_unlock(&share_states_lock_instance);
363}
364
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100365static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100366 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000367{
368 uint32_t mode = 0;
369
Karl Meakin84710f32023-10-12 15:14:49 +0100370 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000372 mode = MM_MODE_R;
373 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000375 mode = MM_MODE_R | MM_MODE_W;
376 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100377 case FFA_DATA_ACCESS_NOT_SPECIFIED:
378 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_DATA_ACCESS_RESERVED:
381 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100382 default:
383 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100384 }
385
Karl Meakin84710f32023-10-12 15:14:49 +0100386 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100387 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000388 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100389 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100390 mode |= MM_MODE_X;
391 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100392 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
393 mode |= (default_mode & MM_MODE_X);
394 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395 case FFA_INSTRUCTION_ACCESS_RESERVED:
396 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100397 default:
398 panic("Unknown instruction access %#x\n",
399 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000400 }
401
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200402 /* Set the security state bit if necessary. */
403 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
404 mode |= plat_ffa_other_world_mode();
405 }
406
Daniel Boulby6e261362024-06-13 16:53:00 +0100407 mode |= default_mode & MM_MODE_D;
408
Andrew Walbran475c1452020-02-07 13:22:22 +0000409 return mode;
410}
411
Jose Marinho75509b42019-04-09 09:34:59 +0100412/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000413 * Get the current mode in the stage-2 page table of the given vm of all the
414 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100416 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000418 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100419 struct ffa_memory_region_constituent **fragments,
420 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100421{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100422 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100423 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100424
Andrew Walbranca808b12020-05-15 17:22:28 +0100425 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100426 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000427 * Fail if there are no constituents. Otherwise we would get an
428 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100429 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100430 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100431 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100432 }
433
Andrew Walbranca808b12020-05-15 17:22:28 +0100434 for (i = 0; i < fragment_count; ++i) {
435 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
436 ipaddr_t begin = ipa_init(fragments[i][j].address);
437 size_t size = fragments[i][j].page_count * PAGE_SIZE;
438 ipaddr_t end = ipa_add(begin, size);
439 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100440
Andrew Walbranca808b12020-05-15 17:22:28 +0100441 /* Fail if addresses are not page-aligned. */
442 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
443 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100444 dlog_verbose("%s: addresses not page-aligned\n",
445 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100446 return ffa_error(FFA_INVALID_PARAMETERS);
447 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100448
Andrew Walbranca808b12020-05-15 17:22:28 +0100449 /*
450 * Ensure that this constituent memory range is all
451 * mapped with the same mode.
452 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800453 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100454 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000455 "%s: constituent memory range "
456 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100457 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000458 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 return ffa_error(FFA_DENIED);
460 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100461
Andrew Walbranca808b12020-05-15 17:22:28 +0100462 /*
463 * Ensure that all constituents are mapped with the same
464 * mode.
465 */
466 if (i == 0) {
467 *orig_mode = current_mode;
468 } else if (current_mode != *orig_mode) {
469 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100470 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000471 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100472 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100473 fragments[i][j].page_count,
474 ipa_addr(begin));
475 return ffa_error(FFA_DENIED);
476 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100477 }
Jose Marinho75509b42019-04-09 09:34:59 +0100478 }
479
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100480 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000481}
482
Karl Meakin0e617d92024-04-05 12:55:22 +0100483enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100484 uint32_t memory_access_desc_size)
485{
486 switch (memory_access_desc_size) {
487 /*
488 * v1.0 and v1.1 memory access descriptors are the same size however
489 * v1.1 is the first version to include the memory access descriptor
490 * size field so return v1.1.
491 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000492 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100493 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000494 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100495 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100496 default:
497 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100498 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100499}
500
501/**
502 * Check if the receivers size and offset given is valid for the senders
503 * FF-A version.
504 */
505static bool receiver_size_and_offset_valid_for_version(
506 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100507 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100508{
509 /*
510 * Check that the version that the memory access descriptor size belongs
511 * to is compatible with the FF-A version we believe the sender to be.
512 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100513 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100514 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100515 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100516 return false;
517 }
518
519 /*
520 * Check the receivers_offset matches the version we found from
521 * memory access descriptor size.
522 */
523 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100524 case FFA_VERSION_1_1:
525 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100526 return receivers_offset == sizeof(struct ffa_memory_region);
527 default:
528 return false;
529 }
530}
531
532/**
533 * Check the values set for fields in the memory region are valid and safe.
534 * Offset values are within safe bounds, receiver count will not cause overflows
535 * and reserved fields are 0.
536 */
537bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100538 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100539 uint32_t fragment_length,
540 bool send_transaction)
541{
542 uint32_t receiver_count;
543 struct ffa_memory_access *receiver;
544 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000545 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
546 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100547
Karl Meakin0e617d92024-04-05 12:55:22 +0100548 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100549 /* Check the reserved fields are 0. */
550 if (memory_region_v1_0->reserved_0 != 0 ||
551 memory_region_v1_0->reserved_1 != 0) {
552 dlog_verbose("Reserved fields must be 0.\n");
553 return false;
554 }
555
556 receiver_count = memory_region_v1_0->receiver_count;
557 } else {
558 uint32_t receivers_size =
559 memory_region->memory_access_desc_size;
560 uint32_t receivers_offset = memory_region->receivers_offset;
561
562 /* Check the reserved field is 0. */
563 if (memory_region->reserved[0] != 0 ||
564 memory_region->reserved[1] != 0 ||
565 memory_region->reserved[2] != 0) {
566 dlog_verbose("Reserved fields must be 0.\n");
567 return false;
568 }
569
570 /*
571 * Check memory_access_desc_size matches the size of the struct
572 * for the senders FF-A version.
573 */
574 if (!receiver_size_and_offset_valid_for_version(
575 receivers_size, receivers_offset, ffa_version)) {
576 dlog_verbose(
577 "Invalid memory access descriptor size %d, "
578 " or receiver offset %d, "
579 "for FF-A version %#x\n",
580 receivers_size, receivers_offset, ffa_version);
581 return false;
582 }
583
584 receiver_count = memory_region->receiver_count;
585 }
586
587 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000588 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100589 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000590 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100591 "specified %u\n",
592 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
593 return false;
594 }
595
596 /* Check values in the memory access descriptors. */
597 /*
598 * The composite offset values must be the same for all recievers so
599 * check the first one is valid and then they are all the same.
600 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100601 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000602 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100603 ->receivers[0]
604 : ffa_memory_region_get_receiver(memory_region, 0);
605 assert(receiver != NULL);
606 composite_offset_0 = receiver->composite_memory_region_offset;
607
608 if (!send_transaction) {
609 if (composite_offset_0 != 0) {
610 dlog_verbose(
611 "Composite offset memory region descriptor "
612 "offset must be 0 for retrieve requests. "
613 "Currently %d",
614 composite_offset_0);
615 return false;
616 }
617 } else {
618 bool comp_offset_is_zero = composite_offset_0 == 0U;
619 bool comp_offset_lt_transaction_descriptor_size =
620 composite_offset_0 <
621 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100622 (size_t)(memory_region->memory_access_desc_size *
623 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100624 bool comp_offset_with_comp_gt_fragment_length =
625 composite_offset_0 +
626 sizeof(struct ffa_composite_memory_region) >
627 fragment_length;
628 if (comp_offset_is_zero ||
629 comp_offset_lt_transaction_descriptor_size ||
630 comp_offset_with_comp_gt_fragment_length) {
631 dlog_verbose(
632 "Invalid composite memory region descriptor "
633 "offset for send transaction %u\n",
634 composite_offset_0);
635 return false;
636 }
637 }
638
Karl Meakin824b63d2024-06-03 19:04:53 +0100639 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100640 uint32_t composite_offset;
641
Karl Meakin0e617d92024-04-05 12:55:22 +0100642 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100643 struct ffa_memory_access_v1_0 *receiver_v1_0 =
644 &memory_region_v1_0->receivers[i];
645 /* Check reserved fields are 0 */
646 if (receiver_v1_0->reserved_0 != 0) {
647 dlog_verbose(
648 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000649 "descriptor must be zero. Currently "
650 "reciever %zu has a reserved field "
651 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100652 i, receiver_v1_0->reserved_0);
653 return false;
654 }
655 /*
656 * We can cast to the current version receiver as the
657 * remaining fields we are checking have the same
658 * offsets for all versions since memory access
659 * descriptors are forwards compatible.
660 */
661 receiver = (struct ffa_memory_access *)receiver_v1_0;
662 } else {
663 receiver = ffa_memory_region_get_receiver(memory_region,
664 i);
665 assert(receiver != NULL);
666
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100667 if (ffa_version == FFA_VERSION_1_1) {
668 /*
669 * Since the reserved field is at the end of the
670 * Endpoint Memory Access Descriptor we must
671 * cast to ffa_memory_access_v1_0 as they match.
672 * Since all fields except reserved in the
673 * Endpoint Memory Access Descriptor have the
674 * same offsets across all versions this cast is
675 * not required when accessing other fields in
676 * the future.
677 */
678 struct ffa_memory_access_v1_0 *receiver_v1_0 =
679 (struct ffa_memory_access_v1_0 *)
680 receiver;
681 if (receiver_v1_0->reserved_0 != 0) {
682 dlog_verbose(
683 "Reserved field in the memory "
684 "access descriptor must be "
685 "zero. Currently reciever %zu "
686 "has a reserved field with a "
687 "value of %lu\n",
688 i, receiver_v1_0->reserved_0);
689 return false;
690 }
691
692 } else {
693 if (receiver->reserved_0 != 0) {
694 dlog_verbose(
695 "Reserved field in the memory "
696 "access descriptor must be "
697 "zero. Currently reciever %zu "
698 "has a reserved field with a "
699 "value of %lu\n",
700 i, receiver->reserved_0);
701 return false;
702 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100703 }
704 }
705
706 /* Check composite offset values are equal for all receivers. */
707 composite_offset = receiver->composite_memory_region_offset;
708 if (composite_offset != composite_offset_0) {
709 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000710 "Composite offset %x differs from %x in "
711 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100712 composite_offset, composite_offset_0);
713 return false;
714 }
715 }
716 return true;
717}
718
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000719/**
J-Alves460d36c2023-10-12 17:02:15 +0100720 * If the receivers for the memory management operation are all from the
Daniel Boulby734981e2024-07-22 11:06:35 +0100721 * secure world, the memory is not device memory (as it isn't covered by the
722 * granule page table) and this isn't a FFA_MEM_SHARE, then request memory
723 * security state update by returning MAP_ACTION_CHECK_PROTECT.
J-Alves460d36c2023-10-12 17:02:15 +0100724 */
725static enum ffa_map_action ffa_mem_send_get_map_action(
726 bool all_receivers_from_current_world, ffa_id_t sender_id,
Daniel Boulby734981e2024-07-22 11:06:35 +0100727 uint32_t mem_func_id, bool is_normal_memory)
J-Alves460d36c2023-10-12 17:02:15 +0100728{
J-Alves95fbb312024-03-20 15:19:16 +0000729 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
730 mem_func_id == FFA_MEM_SHARE_64;
731 const bool protect_memory =
732 (!is_memory_share_abi && all_receivers_from_current_world &&
Daniel Boulby734981e2024-07-22 11:06:35 +0100733 ffa_is_vm_id(sender_id) && is_normal_memory);
J-Alves460d36c2023-10-12 17:02:15 +0100734
735 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
736}
737
738/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000739 * Verify that all pages have the same mode, that the starting mode
740 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100741 * to the sending VM. It outputs the mapping action that needs to be
742 * invoked for the given memory range. On memory lend/donate there
743 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000744 *
745 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100746 * 1) FFA_DENIED if a state transition was not found;
747 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100748 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100750 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
752 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000753 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100754static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100755 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000756 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100757 struct ffa_memory_region_constituent **fragments,
758 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100759 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000760{
761 const uint32_t state_mask =
762 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100763 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100764 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000765 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000766 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
767 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000768
Andrew Walbranca808b12020-05-15 17:22:28 +0100769 ret = constituents_get_mode(from, orig_from_mode, fragments,
770 fragment_constituent_counts,
771 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100772 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100773 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100774 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100775 }
776
Daniel Boulby6e261362024-06-13 16:53:00 +0100777 /*
778 * Check requested memory type is valid with the memory type of the
779 * owner. E.g. they follow the memory type precedence where Normal
780 * memory is more permissive than device and therefore device memory
781 * can only be shared as device memory.
782 */
783 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
784 (*orig_from_mode & MM_MODE_D) != 0U) {
785 dlog_verbose(
786 "Send device memory as Normal memory is not allowed\n");
787 return ffa_error(FFA_DENIED);
788 }
789
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000790 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000791 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000792 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000793 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000794 "Device memory can only be lent to a single borrower "
795 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000796 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798 }
799
800 /*
801 * Ensure the sender is the owner and has exclusive access to the
802 * memory.
803 */
804 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100806 }
807
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100808 /*
809 * Memory cannot be zeroed during the lend/donate operation if the
810 * sender only has RO access.
811 */
812 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
813 dlog_verbose(
814 "Cannot zero memory when the sender doesn't have "
815 "write access\n");
816 return ffa_error(FFA_DENIED);
817 }
818
Daniel Boulbya76fd912024-02-22 14:22:15 +0000819 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100820
J-Alves363f5722022-04-25 17:37:37 +0100821 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000822 struct ffa_memory_access *receiver =
823 ffa_memory_region_get_receiver(memory_region, i);
824 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100825 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000826 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100827 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
828 permissions, *orig_from_mode);
829
J-Alves788b4492023-04-18 14:01:23 +0100830 /*
831 * The assumption is that at this point, the operation from
832 * SP to a receiver VM, should have returned an FFA_ERROR
833 * already.
834 */
835 if (!ffa_is_vm_id(from.vm->id)) {
836 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000837 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100838 }
839
J-Alves460d36c2023-10-12 17:02:15 +0100840 /* Track if all senders are from current world. */
841 all_receivers_from_current_world =
842 all_receivers_from_current_world &&
843 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000844 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100845
J-Alves363f5722022-04-25 17:37:37 +0100846 if ((*orig_from_mode & required_from_mode) !=
847 required_from_mode) {
848 dlog_verbose(
849 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100850 "which required mode %#x but only had %#x "
851 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100852 required_from_mode, *orig_from_mode);
853 return ffa_error(FFA_DENIED);
854 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000855 }
856
J-Alves460d36c2023-10-12 17:02:15 +0100857 *map_action = ffa_mem_send_get_map_action(
Daniel Boulby734981e2024-07-22 11:06:35 +0100858 all_receivers_from_current_world, from.vm->id, share_func,
859 (*orig_from_mode & MM_MODE_D) == 0U);
J-Alves460d36c2023-10-12 17:02:15 +0100860
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000861 /* Find the appropriate new mode. */
862 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000863 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000864 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100865 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000866 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100867 break;
J-Alves95fbb312024-03-20 15:19:16 +0000868 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100869 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000870 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100871 break;
J-Alves95fbb312024-03-20 15:19:16 +0000872 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100873 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100875 break;
876
Jose Marinho75509b42019-04-09 09:34:59 +0100877 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100878 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100879 }
880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882}
883
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100886 struct ffa_memory_region_constituent **fragments,
887 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100888 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000889{
890 const uint32_t state_mask =
891 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
892 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100893 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000894
J-Alves69cdfd92024-04-26 11:40:59 +0100895 assert(map_action != NULL);
896 if (vm_id_is_current_world(from.vm->id)) {
897 *map_action = MAP_ACTION_COMMIT;
898 } else {
899 /*
900 * No need to check the attributes of caller.
901 * The assumption is that the retrieve request of the receiver
902 * also used the MAP_ACTION_NONE, and no update was done to the
903 * page tables. When the receiver is not at the secure virtual
904 * instance SPMC doesn't manage its S2 translation (i.e. when
905 * the receiver is a VM).
906 */
907 *map_action = MAP_ACTION_NONE;
908
909 return (struct ffa_value){.func = FFA_SUCCESS_32};
910 }
911
Andrew Walbranca808b12020-05-15 17:22:28 +0100912 ret = constituents_get_mode(from, orig_from_mode, fragments,
913 fragment_constituent_counts,
914 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100915 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100916 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000917 }
918
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000919 /*
920 * Ensure the relinquishing VM is not the owner but has access to the
921 * memory.
922 */
923 orig_from_state = *orig_from_mode & state_mask;
924 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
925 dlog_verbose(
926 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000928 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100929 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000930 }
931
932 /* Find the appropriate new mode. */
933 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
934
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000936}
937
938/**
939 * Verify that all pages have the same mode, that the starting mode
940 * constitutes a valid state and obtain the next mode to apply
941 * to the retrieving VM.
942 *
943 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 * 1) FFA_DENIED if a state transition was not found;
945 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100946 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100947 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100948 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
950 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 */
J-Alvesfc19b372022-07-06 12:17:35 +0100952struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000953 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 struct ffa_memory_region_constituent **fragments,
955 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100956 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100957 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000958{
959 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100960 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000961
Andrew Walbranca808b12020-05-15 17:22:28 +0100962 ret = constituents_get_mode(to, &orig_to_mode, fragments,
963 fragment_constituent_counts,
964 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100966 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100967 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000968 }
969
J-Alves460d36c2023-10-12 17:02:15 +0100970 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100971 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100972
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000974 /*
975 * If the original ffa memory send call has been processed
976 * successfully, it is expected the orig_to_mode would overlay
977 * with `state_mask`, as a result of the function
978 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100979 *
980 * If Hafnium is the SPMC:
981 * - Caller of the reclaim interface is an SP, the memory shall
982 * have been protected throughout the flow.
983 * - Caller of the reclaim is from the NWd, the memory may have
984 * been protected at the time of lending/donating the memory.
985 * In such case, set action to unprotect memory in the
986 * handling of reclaim operation.
987 * - If Hafnium is the hypervisor memory shall never have been
988 * protected in memory lend/share/donate.
989 *
990 * More details in the doc comment of the function
991 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000992 */
J-Alves59ed0042022-07-28 18:26:41 +0100993 if (vm_id_is_current_world(to.vm->id)) {
994 assert((orig_to_mode &
995 (MM_MODE_INVALID | MM_MODE_UNOWNED |
996 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100997 assert(!memory_protected);
998 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
999 map_action != NULL && memory_protected) {
1000 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +01001001 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001002 } else {
J-Alves69cdfd92024-04-26 11:40:59 +01001003 if (!vm_id_is_current_world(to.vm->id)) {
1004 assert(map_action != NULL);
1005 *map_action = MAP_ACTION_NONE;
1006 return (struct ffa_value){.func = FFA_SUCCESS_32};
1007 }
1008
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001010 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011 * Ensure the retriever has the expected state. We don't care
1012 * about the MM_MODE_SHARED bit; either with or without it set
1013 * are both valid representations of the !O-NA state.
1014 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001015 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001016 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001017 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1018 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001019 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001020 }
J-Alves460d36c2023-10-12 17:02:15 +01001021
1022 /*
1023 * If memory has been protected before, clear the NS bit to
1024 * allow the secure access from the SP.
1025 */
1026 if (memory_protected) {
1027 *to_mode &= ~plat_ffa_other_world_mode();
1028 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001029 }
1030
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001032 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034 *to_mode |= 0;
1035 break;
J-Alves95fbb312024-03-20 15:19:16 +00001036 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001037 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001038 *to_mode |= MM_MODE_UNOWNED;
1039 break;
J-Alves95fbb312024-03-20 15:19:16 +00001040 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001041 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001042 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1043 break;
1044
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001045 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001046 *to_mode |= 0;
1047 break;
1048
1049 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001052 }
1053
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001054 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001055}
Jose Marinho09b1db82019-08-08 09:16:59 +01001056
J-Alvescf6253e2024-01-03 13:48:48 +00001057/*
1058 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1059 * Returns:
1060 * - FFA_SUCCESS_32: if all goes well.
1061 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1062 * the page table update. Or error code provided by the function
1063 * `arch_memory_protect`.
1064 */
1065static struct ffa_value ffa_region_group_check_actions(
1066 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1067 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1068 bool *memory_protected)
1069{
1070 struct ffa_value ret;
1071 bool is_memory_protected;
1072
1073 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1074 dlog_verbose(
1075 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001076 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001077 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1078 pa_addr(pa_end));
1079 return ffa_error(FFA_NO_MEMORY);
1080 }
1081
1082 switch (action) {
1083 case MAP_ACTION_CHECK:
1084 /* No protect requested. */
1085 is_memory_protected = false;
1086 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1087 break;
1088 case MAP_ACTION_CHECK_PROTECT: {
1089 paddr_t last_protected_pa = pa_init(0);
1090
1091 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1092
1093 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1094
1095 /*
1096 * - If protect memory has failed with FFA_DENIED, means some
1097 * range of memory was in the wrong state. In such case, SPM
1098 * reverts the state of the pages that were successfully
1099 * updated.
1100 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1101 * means the platform doesn't support the protection mechanism.
1102 * That said, it still permits the page table update to go
1103 * through. The variable
1104 * `is_memory_protected` will be equal to false.
1105 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1106 * break from switch and return the error.
1107 */
1108 if (ret.func == FFA_ERROR_32) {
1109 assert(!is_memory_protected);
1110 if (ffa_error_code(ret) == FFA_DENIED &&
1111 pa_addr(last_protected_pa) != (uintptr_t)0) {
1112 CHECK(arch_memory_unprotect(
1113 pa_begin,
1114 pa_add(last_protected_pa, PAGE_SIZE)));
1115 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1116 ret = (struct ffa_value){
1117 .func = FFA_SUCCESS_32,
1118 };
1119 }
1120 }
1121 } break;
1122 default:
1123 panic("%s: invalid action to process %x\n", __func__, action);
1124 }
1125
1126 if (memory_protected != NULL) {
1127 *memory_protected = is_memory_protected;
1128 }
1129
1130 return ret;
1131}
1132
1133static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1134 paddr_t pa_begin, paddr_t pa_end,
1135 struct mpool *ppool, uint32_t mode,
1136 enum ffa_map_action action)
1137{
1138 switch (action) {
1139 case MAP_ACTION_COMMIT_UNPROTECT:
1140 /*
1141 * Checking that it should succeed because SPM should be
1142 * unprotecting memory that it had protected before.
1143 */
1144 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1145 case MAP_ACTION_COMMIT:
1146 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1147 NULL);
1148 break;
1149 default:
1150 panic("%s: invalid action to process %x\n", __func__, action);
1151 }
1152}
1153
Jose Marinho09b1db82019-08-08 09:16:59 +01001154/**
J-Alves063ad832023-10-03 18:05:40 +01001155 * Helper function to revert a failed "Protect" action from the SPMC:
1156 * - `fragment_count`: should specify the number of fragments to traverse from
1157 * `fragments`. This may not be the full amount of fragments that are part of
1158 * the share_state structure.
1159 * - `fragment_constituent_counts`: array holding the amount of constituents
1160 * per fragment.
1161 * - `end`: pointer to the constituent that failed the "protect" action. It
1162 * shall be part of the last fragment, and it shall make the loop below break.
1163 */
1164static void ffa_region_group_fragments_revert_protect(
1165 struct ffa_memory_region_constituent **fragments,
1166 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1167 const struct ffa_memory_region_constituent *end)
1168{
1169 for (uint32_t i = 0; i < fragment_count; ++i) {
1170 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1171 struct ffa_memory_region_constituent *constituent =
1172 &fragments[i][j];
1173 size_t size = constituent->page_count * PAGE_SIZE;
1174 paddr_t pa_begin =
1175 pa_from_ipa(ipa_init(constituent->address));
1176 paddr_t pa_end = pa_add(pa_begin, size);
1177
Karl Meakine8937d92024-03-19 16:04:25 +00001178 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001179 __func__, pa_addr(pa_begin), size);
1180
1181 if (constituent == end) {
1182 /*
1183 * The last constituent is expected to be in the
1184 * last fragment.
1185 */
1186 assert(i == fragment_count - 1);
1187 break;
1188 }
1189
1190 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1191 }
1192 }
1193}
1194
1195/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001196 * Updates a VM's page table such that the given set of physical address ranges
1197 * are mapped in the address space at the corresponding address ranges, in the
1198 * mode provided.
1199 *
J-Alves0a83dc22023-05-05 09:50:37 +01001200 * The enum ffa_map_action determines the action taken from a call to the
1201 * function below:
1202 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1203 * mpool but no mappings will actually be updated. This function must always
1204 * be called first with action set to MAP_ACTION_CHECK to check that it will
1205 * succeed before calling ffa_region_group_identity_map with whichever one of
1206 * the remaining actions, to avoid leaving the page table in a half-updated
1207 * state.
1208 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1209 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001210 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1211 * invocation to the monitor to update the security state of the memory,
1212 * to that of the SPMC.
1213 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1214 * with a call into the monitor, to reset the security state of memory
1215 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001216 * vm_ptable_defrag should always be called after a series of page table
1217 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001218 *
J-Alvescf6253e2024-01-03 13:48:48 +00001219 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1220 * error codes:
1221 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1222 * - FFA_DENIED:
1223 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001224 * made to memory mappings.
1225 */
J-Alvescf6253e2024-01-03 13:48:48 +00001226struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001227 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001228 struct ffa_memory_region_constituent **fragments,
1229 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001230 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1231 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001232{
Andrew Walbranca808b12020-05-15 17:22:28 +01001233 uint32_t i;
1234 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001235 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001236
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001237 if (vm_locked.vm->el0_partition) {
1238 mode |= MM_MODE_USER | MM_MODE_NG;
1239 }
1240
Andrew Walbranca808b12020-05-15 17:22:28 +01001241 /* Iterate over the memory region constituents within each fragment. */
1242 for (i = 0; i < fragment_count; ++i) {
1243 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001244 struct ffa_memory_region_constituent *constituent =
1245 &fragments[i][j];
1246 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001247 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001248 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001249 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001250 uint32_t pa_bits =
1251 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001252
1253 /*
1254 * Ensure the requested region falls into system's PA
1255 * range.
1256 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001257 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1258 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001259 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001260 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001261 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001262
J-Alvescf6253e2024-01-03 13:48:48 +00001263 if (action <= MAP_ACTION_CHECK_PROTECT) {
1264 ret = ffa_region_group_check_actions(
1265 vm_locked, pa_begin, pa_end, ppool,
1266 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001267
1268 if (ret.func == FFA_ERROR_32 &&
1269 ffa_error_code(ret) == FFA_DENIED) {
1270 if (memory_protected != NULL) {
1271 assert(!*memory_protected);
1272 }
1273
1274 ffa_region_group_fragments_revert_protect(
1275 fragments,
1276 fragment_constituent_counts,
1277 i + 1, constituent);
1278 break;
1279 }
J-Alvescf6253e2024-01-03 13:48:48 +00001280 } else if (action >= MAP_ACTION_COMMIT &&
1281 action < MAP_ACTION_MAX) {
1282 ffa_region_group_commit_actions(
1283 vm_locked, pa_begin, pa_end, ppool,
1284 mode, action);
1285 ret = (struct ffa_value){
1286 .func = FFA_SUCCESS_32};
1287 } else {
1288 panic("%s: Unknown ffa_map_action.\n",
1289 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001290 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001291 }
1292 }
1293
J-Alvescf6253e2024-01-03 13:48:48 +00001294 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001295}
1296
1297/**
1298 * Clears a region of physical memory by overwriting it with zeros. The data is
1299 * flushed from the cache so the memory has been cleared across the system.
1300 */
J-Alves7db32002021-12-14 14:44:50 +00001301static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1302 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001303{
1304 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001305 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001306 * global mapping of the whole range. Such an approach will limit
1307 * the changes to stage-1 tables and will allow only local
1308 * invalidation.
1309 */
1310 bool ret;
1311 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001312 void *ptr = mm_identity_map(stage1_locked, begin, end,
1313 MM_MODE_W | (extra_mode_attributes &
1314 plat_ffa_other_world_mode()),
1315 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001316 size_t size = pa_difference(begin, end);
1317
1318 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001319 goto fail;
1320 }
1321
1322 memset_s(ptr, size, 0, size);
1323 arch_mm_flush_dcache(ptr, size);
1324 mm_unmap(stage1_locked, begin, end, ppool);
1325
1326 ret = true;
1327 goto out;
1328
1329fail:
1330 ret = false;
1331
1332out:
1333 mm_unlock_stage1(&stage1_locked);
1334
1335 return ret;
1336}
1337
1338/**
1339 * Clears a region of physical memory by overwriting it with zeros. The data is
1340 * flushed from the cache so the memory has been cleared across the system.
1341 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001342static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001343 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001344 struct ffa_memory_region_constituent **fragments,
1345 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1346 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001347{
1348 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001349 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001350 bool ret = false;
1351
1352 /*
1353 * Create a local pool so any freed memory can't be used by another
1354 * thread. This is to ensure each constituent that is mapped can be
1355 * unmapped again afterwards.
1356 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001357 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001358
Andrew Walbranca808b12020-05-15 17:22:28 +01001359 /* Iterate over the memory region constituents within each fragment. */
1360 for (i = 0; i < fragment_count; ++i) {
1361 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001362
J-Alves8457f932023-10-11 16:41:45 +01001363 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001364 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1365 paddr_t begin =
1366 pa_from_ipa(ipa_init(fragments[i][j].address));
1367 paddr_t end = pa_add(begin, size);
1368
J-Alves7db32002021-12-14 14:44:50 +00001369 if (!clear_memory(begin, end, &local_page_pool,
1370 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001371 /*
1372 * api_clear_memory will defrag on failure, so
1373 * no need to do it here.
1374 */
1375 goto out;
1376 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001377 }
1378 }
1379
Jose Marinho09b1db82019-08-08 09:16:59 +01001380 ret = true;
1381
1382out:
1383 mpool_fini(&local_page_pool);
1384 return ret;
1385}
1386
J-Alves5952d942022-12-22 16:03:00 +00001387static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1388 ipaddr_t in_begin, ipaddr_t in_end)
1389{
1390 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1391 ipa_addr(begin) < ipa_addr(in_end)) ||
1392 (ipa_addr(end) <= ipa_addr(in_end) &&
1393 ipa_addr(end) > ipa_addr(in_begin));
1394}
1395
1396/**
1397 * Receives a memory range and looks for overlaps with the remainder
1398 * constituents of the memory share/lend/donate operation. Assumes they are
1399 * passed in order to avoid having to loop over all the elements at each call.
1400 * The function only compares the received memory ranges with those that follow
1401 * within the same fragment, and subsequent fragments from the same operation.
1402 */
1403static bool ffa_memory_check_overlap(
1404 struct ffa_memory_region_constituent **fragments,
1405 const uint32_t *fragment_constituent_counts,
1406 const uint32_t fragment_count, const uint32_t current_fragment,
1407 const uint32_t current_constituent)
1408{
1409 uint32_t i = current_fragment;
1410 uint32_t j = current_constituent;
1411 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1412 const uint32_t current_page_count = fragments[i][j].page_count;
1413 size_t current_size = current_page_count * PAGE_SIZE;
1414 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1415
1416 if (current_size == 0 ||
1417 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001418 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1419 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001420 return false;
1421 }
1422
1423 for (; i < fragment_count; i++) {
1424 j = (i == current_fragment) ? j + 1 : 0;
1425
1426 for (; j < fragment_constituent_counts[i]; j++) {
1427 ipaddr_t begin = ipa_init(fragments[i][j].address);
1428 const uint32_t page_count = fragments[i][j].page_count;
1429 size_t size = page_count * PAGE_SIZE;
1430 ipaddr_t end = ipa_add(begin, size - 1);
1431
1432 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1433 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001434 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001435 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001436 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001437 return false;
1438 }
1439
1440 /*
1441 * Check if current ranges is within begin and end, as
1442 * well as the reverse. This should help optimize the
1443 * loop, and reduce the number of iterations.
1444 */
1445 if (is_memory_range_within(begin, end, current_begin,
1446 current_end) ||
1447 is_memory_range_within(current_begin, current_end,
1448 begin, end)) {
1449 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001450 "Overlapping memory ranges: %#lx - "
1451 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001452 ipa_addr(begin), ipa_addr(end),
1453 ipa_addr(current_begin),
1454 ipa_addr(current_end));
1455 return true;
1456 }
1457 }
1458 }
1459
1460 return false;
1461}
1462
Jose Marinho09b1db82019-08-08 09:16:59 +01001463/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001465 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001466 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001467 *
1468 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001469 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001470 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001471 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001472 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1473 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001474 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001475 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001477 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001478static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001479 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001480 struct ffa_memory_region_constituent **fragments,
1481 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001482 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001483 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1484 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001485{
Andrew Walbranca808b12020-05-15 17:22:28 +01001486 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001487 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001488 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001489 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001490 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001491 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001492 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001493 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001494 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001495 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001496
1497 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001498 * Make sure constituents are properly aligned to a 64-bit boundary. If
1499 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001500 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001501 for (i = 0; i < fragment_count; ++i) {
1502 if (!is_aligned(fragments[i], 8)) {
1503 dlog_verbose("Constituents not aligned.\n");
1504 return ffa_error(FFA_INVALID_PARAMETERS);
1505 }
J-Alves8f11cde2022-12-21 16:18:22 +00001506 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1507 constituents_total_page_count +=
1508 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001509 if (ffa_memory_check_overlap(
1510 fragments, fragment_constituent_counts,
1511 fragment_count, i, j)) {
1512 return ffa_error(FFA_INVALID_PARAMETERS);
1513 }
J-Alves8f11cde2022-12-21 16:18:22 +00001514 }
1515 }
1516
1517 if (constituents_total_page_count != composite_total_page_count) {
1518 dlog_verbose(
1519 "Composite page count differs from calculated page "
1520 "count from constituents.\n");
1521 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001522 }
1523
1524 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001525 * Check if the state transition is lawful for the sender, ensure that
1526 * all constituents of a memory region being shared are at the same
1527 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001528 */
J-Alves460d36c2023-10-12 17:02:15 +01001529 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001530 from_locked, share_func, memory_region, &orig_from_mode,
1531 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001532 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001533 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001534 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001536 }
1537
Andrew Walbran37c574e2020-06-03 11:45:46 +01001538 if (orig_from_mode_ret != NULL) {
1539 *orig_from_mode_ret = orig_from_mode;
1540 }
1541
Jose Marinho09b1db82019-08-08 09:16:59 +01001542 /*
1543 * Create a local pool so any freed memory can't be used by another
1544 * thread. This is to ensure the original mapping can be restored if the
1545 * clear fails.
1546 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001547 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001548
1549 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001550 * First reserve all required memory for the new page table entries
1551 * without committing, to make sure the entire operation will succeed
1552 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001553 * Provide the map_action as populated by 'ffa_send_check_transition'.
1554 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001555 */
J-Alvescf6253e2024-01-03 13:48:48 +00001556 ret = ffa_region_group_identity_map(
1557 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001558 fragment_count, from_mode, page_pool, map_action,
1559 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001560 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001561 goto out;
1562 }
1563
1564 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001565 * Update the mapping for the sender. This won't allocate because the
1566 * transaction was already prepared above, but may free pages in the
1567 * case that a whole block is being unmapped that was previously
1568 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001569 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001570 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001571 from_locked, fragments, fragment_constituent_counts,
1572 fragment_count, from_mode, &local_page_pool,
1573 MAP_ACTION_COMMIT, NULL)
1574 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001575
J-Alves460d36c2023-10-12 17:02:15 +01001576 /*
1577 * If memory has been protected, it is now part of the secure PAS
1578 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1579 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1580 * SPM's S1 translation.
1581 * In case memory hasn't been protected, and it is in the non-secure
1582 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1583 * perform a non-secure memory access. In such case `clean_mode` takes
1584 * the same mode as `orig_from_mode`.
1585 */
1586 clean_mode = (memory_protected != NULL && *memory_protected)
1587 ? orig_from_mode & ~plat_ffa_other_world_mode()
1588 : orig_from_mode;
1589
Jose Marinho09b1db82019-08-08 09:16:59 +01001590 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001591 if (clear && !ffa_clear_memory_constituents(
1592 clean_mode, fragments, fragment_constituent_counts,
1593 fragment_count, page_pool)) {
1594 map_action = (memory_protected != NULL && *memory_protected)
1595 ? MAP_ACTION_COMMIT_UNPROTECT
1596 : MAP_ACTION_COMMIT;
1597
Jose Marinho09b1db82019-08-08 09:16:59 +01001598 /*
1599 * On failure, roll back by returning memory to the sender. This
1600 * may allocate pages which were previously freed into
1601 * `local_page_pool` by the call above, but will never allocate
1602 * more pages than that so can never fail.
1603 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001604 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001605 from_locked, fragments,
1606 fragment_constituent_counts, fragment_count,
1607 orig_from_mode, &local_page_pool,
1608 MAP_ACTION_COMMIT, NULL)
1609 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001610 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001611 goto out;
1612 }
1613
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001615
1616out:
1617 mpool_fini(&local_page_pool);
1618
1619 /*
1620 * Tidy up the page table by reclaiming failed mappings (if there was an
1621 * error) or merging entries into blocks where possible (on success).
1622 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001623 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001624
1625 return ret;
1626}
1627
1628/**
1629 * Validates and maps memory shared from one VM to another.
1630 *
1631 * This function requires the calling context to hold the <to> lock.
1632 *
1633 * Returns:
1634 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001635 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001636 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001638 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001639 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001641struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001642 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001643 struct ffa_memory_region_constituent **fragments,
1644 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001645 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001646 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001647{
Andrew Walbranca808b12020-05-15 17:22:28 +01001648 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001649 uint32_t to_mode;
1650 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001651 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001652 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653
1654 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001655 * Make sure constituents are properly aligned to a 64-bit boundary. If
1656 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001657 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001658 for (i = 0; i < fragment_count; ++i) {
1659 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001660 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001661 return ffa_error(FFA_INVALID_PARAMETERS);
1662 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001663 }
1664
1665 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001666 * Ensure the sender has write permissions if the memory needs to be
1667 * cleared.
1668 */
1669 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1670 dlog_verbose(
1671 "Cannot zero memory when the sender does not have "
1672 "write access\n");
1673 return ffa_error(FFA_DENIED);
1674 }
1675
1676 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001677 * Check if the state transition is lawful for the recipient, and ensure
1678 * that all constituents of the memory region being retrieved are at the
1679 * same state.
1680 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001681 ret = ffa_retrieve_check_transition(
1682 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001683 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1684 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001685
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001686 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001687 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001688 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001689 }
1690
1691 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001692 * Create a local pool so any freed memory can't be used by
1693 * another thread. This is to ensure the original mapping can be
1694 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001695 */
1696 mpool_init_with_fallback(&local_page_pool, page_pool);
1697
1698 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001699 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1700 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001701 */
J-Alves69cdfd92024-04-26 11:40:59 +01001702 if (map_action != MAP_ACTION_NONE) {
1703 /*
1704 * First reserve all required memory for the new page table
1705 * entries in the recipient page tables without committing, to
1706 * make sure the entire operation will succeed without
1707 * exhausting the page pool.
1708 */
1709 ret = ffa_region_group_identity_map(
1710 to_locked, fragments, fragment_constituent_counts,
1711 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1712 NULL);
1713 if (ret.func == FFA_ERROR_32) {
1714 /* TODO: partial defrag of failed range. */
1715 goto out;
1716 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001717 }
1718
1719 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001720 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001721 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1722 fragment_constituent_counts,
1723 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001724 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001725 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001726 goto out;
1727 }
1728
J-Alves69cdfd92024-04-26 11:40:59 +01001729 if (map_action != MAP_ACTION_NONE) {
1730 /*
1731 * Complete the transfer by mapping the memory into the
1732 * recipient. This won't allocate because the transaction was
1733 * already prepared above, so it doesn't need to use the
1734 * `local_page_pool`.
1735 */
1736 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1737 fragment_constituent_counts,
1738 fragment_count, to_mode,
1739 page_pool, map_action, NULL)
1740 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001741
J-Alves69cdfd92024-04-26 11:40:59 +01001742 /*
1743 * Return the mode used in mapping the memory in retriever's PT.
1744 */
1745 if (response_mode != NULL) {
1746 *response_mode = to_mode;
1747 }
J-Alves460d36c2023-10-12 17:02:15 +01001748 }
1749
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001750 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001751
1752out:
1753 mpool_fini(&local_page_pool);
1754
1755 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001756 * Tidy up the page table by reclaiming failed mappings (if there was an
1757 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001758 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001759 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001760
1761 return ret;
1762}
1763
Andrew Walbran996d1d12020-05-27 14:08:43 +01001764static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001765 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001766 struct ffa_memory_region_constituent **fragments,
1767 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001768 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001769{
1770 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001771 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001772 uint32_t from_mode;
1773 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001774 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001775 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001776
Andrew Walbranca808b12020-05-15 17:22:28 +01001777 ret = ffa_relinquish_check_transition(
1778 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001779 fragment_constituent_counts, fragment_count, &from_mode,
1780 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001781 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001782 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001783 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001784 }
1785
1786 /*
1787 * Create a local pool so any freed memory can't be used by another
1788 * thread. This is to ensure the original mapping can be restored if the
1789 * clear fails.
1790 */
1791 mpool_init_with_fallback(&local_page_pool, page_pool);
1792
J-Alves69cdfd92024-04-26 11:40:59 +01001793 if (map_action != MAP_ACTION_NONE) {
1794 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001795
J-Alves69cdfd92024-04-26 11:40:59 +01001796 /*
1797 * First reserve all required memory for the new page table
1798 * entries without committing, to make sure the entire operation
1799 * will succeed without exhausting the page pool.
1800 */
1801 ret = ffa_region_group_identity_map(
1802 from_locked, fragments, fragment_constituent_counts,
1803 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1804 NULL);
1805 if (ret.func == FFA_ERROR_32) {
1806 goto out;
1807 }
1808
1809 /*
1810 * Update the mapping for the sender. This won't allocate
1811 * because the transaction was already prepared above, but may
1812 * free pages in the case that a whole block is being unmapped
1813 * that was previously partially mapped.
1814 */
1815 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1816 fragment_constituent_counts,
1817 fragment_count, from_mode,
1818 &local_page_pool,
1819 MAP_ACTION_COMMIT, NULL)
1820 .func == FFA_SUCCESS_32);
1821 } else {
1822 /*
1823 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1824 * were not updated on retrieve/relinquish. These were updating
1825 * only the `share_state` structures. As such, use the sender's
1826 * original mode.
1827 */
1828 clearing_mode = sender_orig_mode;
1829 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001830
1831 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001832 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001833 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001834 fragment_constituent_counts,
1835 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001836 if (map_action != MAP_ACTION_NONE) {
1837 /*
1838 * On failure, roll back by returning memory to the
1839 * sender. This may allocate pages which were previously
1840 * freed into `local_page_pool` by the call above, but
1841 * will never allocate more pages than that so can never
1842 * fail.
1843 */
1844 CHECK(ffa_region_group_identity_map(
1845 from_locked, fragments,
1846 fragment_constituent_counts,
1847 fragment_count, orig_from_mode,
1848 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1849 .func == FFA_SUCCESS_32);
1850 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001851 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001852 goto out;
1853 }
1854
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001855 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001856
1857out:
1858 mpool_fini(&local_page_pool);
1859
1860 /*
1861 * Tidy up the page table by reclaiming failed mappings (if there was an
1862 * error) or merging entries into blocks where possible (on success).
1863 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001864 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001865
1866 return ret;
1867}
1868
1869/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001870 * Complete a memory sending operation by checking that it is valid, updating
1871 * the sender page table, and then either marking the share state as having
1872 * completed sending (on success) or freeing it (on failure).
1873 *
1874 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1875 */
J-Alvesfdd29272022-07-19 13:16:31 +01001876struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001877 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001878 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1879 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001880{
1881 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001882 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001883 struct ffa_value ret;
1884
1885 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001886 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001887 assert(memory_region != NULL);
1888 composite = ffa_memory_region_get_composite(memory_region, 0);
1889 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001890
1891 /* Check that state is valid in sender page table and update. */
1892 ret = ffa_send_check_update(
1893 from_locked, share_state->fragments,
1894 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001895 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001896 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001897 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001898 if (ret.func != FFA_SUCCESS_32) {
1899 /*
1900 * Free share state, it failed to send so it can't be retrieved.
1901 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001902 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1903 __func__, ffa_func_name(ret.func),
1904 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001905 share_state_free(share_states, share_state, page_pool);
1906 return ret;
1907 }
1908
1909 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001910 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001911
J-Alvesee68c542020-10-29 17:48:20 +00001912 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001913}
1914
1915/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001916 * Check that the memory attributes match Hafnium expectations.
1917 * Cacheability:
1918 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1919 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1920 *
1921 * Shareability:
1922 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001923 */
1924static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001925 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001926{
1927 enum ffa_memory_type memory_type;
1928 enum ffa_memory_cacheability cacheability;
1929 enum ffa_memory_shareability shareability;
1930
Karl Meakin84710f32023-10-12 15:14:49 +01001931 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001932 cacheability = attributes.cacheability;
1933 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1934 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1935 dlog_verbose(
1936 "Normal Memory: Invalid cacheability %s, "
1937 "expected %s.\n",
1938 ffa_memory_cacheability_name(cacheability),
1939 ffa_memory_cacheability_name(
1940 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001941 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001942 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001943 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1944 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1945 dlog_verbose(
1946 "Device Memory: Invalid cacheability %s, "
1947 "expected %s.\n",
1948 ffa_device_memory_cacheability_name(cacheability),
1949 ffa_device_memory_cacheability_name(
1950 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001951 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001952 }
1953
Karl Meakin84710f32023-10-12 15:14:49 +01001954 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001955 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001956 dlog_verbose("Invalid shareability %s, expected %s.\n",
1957 ffa_memory_shareability_name(shareability),
1958 ffa_memory_shareability_name(
1959 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001960 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001961 }
1962
1963 return (struct ffa_value){.func = FFA_SUCCESS_32};
1964}
1965
1966/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001967 * Check that the given `memory_region` represents a valid memory send request
1968 * of the given `share_func` type, return the clear flag and permissions via the
1969 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001970 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001971 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001972 * not.
1973 */
J-Alves66652252022-07-06 09:49:51 +01001974struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001975 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1976 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001977 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001978{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001979 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001980 struct ffa_memory_access *receiver =
1981 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001982 uint64_t receivers_end;
1983 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001984 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001985 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001986 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 enum ffa_data_access data_access;
1988 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001989 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001990 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001991 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001992 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001993 memory_region->receivers_offset +
1994 memory_region->memory_access_desc_size +
1995 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001996
1997 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001998 dlog_verbose("Fragment length %u too short (min %zu).\n",
1999 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002000 return ffa_error(FFA_INVALID_PARAMETERS);
2001 }
2002
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002003 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
2004 "struct ffa_memory_region_constituent must be 16 bytes");
2005 if (!is_aligned(fragment_length,
2006 sizeof(struct ffa_memory_region_constituent)) ||
2007 !is_aligned(memory_share_length,
2008 sizeof(struct ffa_memory_region_constituent))) {
2009 dlog_verbose(
2010 "Fragment length %u or total length %u"
2011 " is not 16-byte aligned.\n",
2012 fragment_length, memory_share_length);
2013 return ffa_error(FFA_INVALID_PARAMETERS);
2014 }
2015
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002016 if (fragment_length > memory_share_length) {
2017 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002018 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002019 (size_t)fragment_length, (size_t)memory_share_length);
2020 return ffa_error(FFA_INVALID_PARAMETERS);
2021 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002022
J-Alves95df0ef2022-12-07 10:09:48 +00002023 /* The sender must match the caller. */
2024 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2025 vm_id_is_current_world(memory_region->sender)) ||
2026 (vm_id_is_current_world(from_locked.vm->id) &&
2027 memory_region->sender != from_locked.vm->id)) {
2028 dlog_verbose("Invalid memory sender ID.\n");
2029 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002030 }
2031
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002032 if (memory_region->receiver_count <= 0) {
2033 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002034 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002035 }
2036
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002037 /*
2038 * Ensure that the composite header is within the memory bounds and
2039 * doesn't overlap the first part of the message. Cast to uint64_t
2040 * to prevent overflow.
2041 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002042 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002043 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002044 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002045 min_length = receivers_end +
2046 sizeof(struct ffa_composite_memory_region) +
2047 sizeof(struct ffa_memory_region_constituent);
2048 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002049 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002050 (size_t)memory_share_length, (size_t)min_length);
2051 return ffa_error(FFA_INVALID_PARAMETERS);
2052 }
2053
2054 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002055 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002056
2057 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002058 * Check that the composite memory region descriptor is after the access
2059 * descriptors, is at least 16-byte aligned, and fits in the first
2060 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002061 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002062 if ((composite_memory_region_offset < receivers_end) ||
2063 (composite_memory_region_offset % 16 != 0) ||
2064 (composite_memory_region_offset >
2065 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2066 dlog_verbose(
2067 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002068 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002069 (size_t)composite_memory_region_offset);
2070 return ffa_error(FFA_INVALID_PARAMETERS);
2071 }
2072
2073 /*
2074 * Compute the start of the constituent regions. Already checked
2075 * to be not more than fragment_length and thus not more than
2076 * memory_share_length.
2077 */
2078 constituents_start = composite_memory_region_offset +
2079 sizeof(struct ffa_composite_memory_region);
2080 constituents_length = memory_share_length - constituents_start;
2081
2082 /*
2083 * Check that the number of constituents is consistent with the length
2084 * of the constituent region.
2085 */
2086 composite = ffa_memory_region_get_composite(memory_region, 0);
2087 if ((constituents_length %
2088 sizeof(struct ffa_memory_region_constituent) !=
2089 0) ||
2090 ((constituents_length /
2091 sizeof(struct ffa_memory_region_constituent)) !=
2092 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002093 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002094 (size_t)memory_share_length,
2095 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002096 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002097 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002098 if (fragment_length < memory_share_length &&
2099 fragment_length < HF_MAILBOX_SIZE) {
2100 dlog_warning(
2101 "Initial fragment length %d smaller than mailbox "
2102 "size.\n",
2103 fragment_length);
2104 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002105
Andrew Walbrana65a1322020-04-06 19:32:32 +01002106 /*
2107 * Clear is not allowed for memory sharing, as the sender still has
2108 * access to the memory.
2109 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002110 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002111 (share_func == FFA_MEM_SHARE_32 ||
2112 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002113 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002114 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002115 }
2116
2117 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002118 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002119 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002121 }
2122
J-Alves363f5722022-04-25 17:37:37 +01002123 /* Check that the permissions are valid, for each specified receiver. */
2124 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002125 struct ffa_memory_region_attributes receiver_permissions;
2126
2127 receiver = ffa_memory_region_get_receiver(memory_region, i);
2128 assert(receiver != NULL);
2129 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002130 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002131 receiver_permissions.permissions;
2132 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002133
2134 if (memory_region->sender == receiver_id) {
2135 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002136 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002137 }
Federico Recanati85090c42021-12-15 13:17:54 +01002138
J-Alves363f5722022-04-25 17:37:37 +01002139 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2140 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002141 struct ffa_memory_access *other_receiver =
2142 ffa_memory_region_get_receiver(memory_region,
2143 j);
2144 assert(other_receiver != NULL);
2145
J-Alves363f5722022-04-25 17:37:37 +01002146 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002147 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002148 dlog_verbose(
2149 "Repeated receiver(%x) in memory send "
2150 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002151 other_receiver->receiver_permissions
2152 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002153 return ffa_error(FFA_INVALID_PARAMETERS);
2154 }
2155 }
2156
2157 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002158 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002159 dlog_verbose(
2160 "All ffa_memory_access should point to the "
2161 "same composite memory region offset.\n");
2162 return ffa_error(FFA_INVALID_PARAMETERS);
2163 }
2164
Karl Meakin84710f32023-10-12 15:14:49 +01002165 data_access = permissions.data_access;
2166 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002167 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2168 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2169 dlog_verbose(
2170 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002171 "(data_access = %s, instruction_access = %s)\n",
2172 ffa_data_access_name(data_access),
2173 ffa_instruction_access_name(
2174 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002175 return ffa_error(FFA_INVALID_PARAMETERS);
2176 }
2177 if (instruction_access !=
2178 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2179 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002180 "Invalid instruction access permissions %s "
2181 "for sending memory, expected %s.\n",
2182 ffa_instruction_access_name(instruction_access),
2183 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002184 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002185 return ffa_error(FFA_INVALID_PARAMETERS);
2186 }
J-Alves95fbb312024-03-20 15:19:16 +00002187 if (share_func == FFA_MEM_SHARE_32 ||
2188 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002189 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2190 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002191 "Invalid data access permissions %s "
2192 "for sharing memory, expected %s.\n",
2193 ffa_data_access_name(data_access),
2194 ffa_data_access_name(
2195 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002196 return ffa_error(FFA_INVALID_PARAMETERS);
2197 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002198 /*
2199 * According to section 10.10.3 of the FF-A v1.1 EAC0
2200 * spec, NX is required for share operations (but must
2201 * not be specified by the sender) so set it in the
2202 * copy that we store, ready to be returned to the
2203 * retriever.
2204 */
2205 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002206 permissions.instruction_access =
2207 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002208 receiver_permissions.permissions = permissions;
2209 }
J-Alves363f5722022-04-25 17:37:37 +01002210 }
J-Alves95fbb312024-03-20 15:19:16 +00002211 if ((share_func == FFA_MEM_LEND_32 ||
2212 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002213 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2214 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002215 "Invalid data access permissions %s for "
2216 "lending memory, expected %s.\n",
2217 ffa_data_access_name(data_access),
2218 ffa_data_access_name(
2219 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002220 return ffa_error(FFA_INVALID_PARAMETERS);
2221 }
2222
J-Alves95fbb312024-03-20 15:19:16 +00002223 if ((share_func == FFA_MEM_DONATE_32 ||
2224 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002225 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2226 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002227 "Invalid data access permissions %s for "
2228 "donating memory, expected %s.\n",
2229 ffa_data_access_name(data_access),
2230 ffa_data_access_name(
2231 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002232 return ffa_error(FFA_INVALID_PARAMETERS);
2233 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002234 }
2235
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002236 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002237 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002238 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2239 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002240 "Invalid security state %s for memory share operation, "
2241 "expected %s.\n",
2242 ffa_memory_security_name(security_state),
2243 ffa_memory_security_name(
2244 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002245 return ffa_error(FFA_INVALID_PARAMETERS);
2246 }
2247
Federico Recanatid937f5e2021-12-20 17:38:23 +01002248 /*
J-Alves807794e2022-06-16 13:42:47 +01002249 * If a memory donate or lend with single borrower, the memory type
2250 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002251 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002252 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002253 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002254 share_func == FFA_MEM_DONATE_64 ||
2255 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002256 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002257 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002258 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002259 "Invalid memory type %s for memory share "
2260 "operation, expected %s.\n",
2261 ffa_memory_type_name(type),
2262 ffa_memory_type_name(
2263 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002264 return ffa_error(FFA_INVALID_PARAMETERS);
2265 }
2266 } else {
2267 /*
2268 * Check that sender's memory attributes match Hafnium
2269 * expectations: Normal Memory, Inner shareable, Write-Back
2270 * Read-Allocate Write-Allocate Cacheable.
2271 */
2272 ret = ffa_memory_attributes_validate(memory_region->attributes);
2273 if (ret.func != FFA_SUCCESS_32) {
2274 return ret;
2275 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002276 }
2277
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002279}
2280
2281/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002282 * Gets the share state for continuing an operation to donate, lend or share
2283 * memory, and checks that it is a valid request.
2284 *
2285 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2286 * not.
2287 */
J-Alvesfdd29272022-07-19 13:16:31 +01002288struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002289 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002290 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002291 struct mpool *page_pool)
2292{
2293 struct ffa_memory_share_state *share_state;
2294 struct ffa_memory_region *memory_region;
2295
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002296 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002297
2298 /*
2299 * Look up the share state by handle and make sure that the VM ID
2300 * matches.
2301 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002302 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002303 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002304 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002305 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002306 handle);
2307 return ffa_error(FFA_INVALID_PARAMETERS);
2308 }
2309 memory_region = share_state->memory_region;
2310
J-Alvesfdd29272022-07-19 13:16:31 +01002311 if (vm_id_is_current_world(from_vm_id) &&
2312 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002313 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2314 return ffa_error(FFA_INVALID_PARAMETERS);
2315 }
2316
2317 if (share_state->sending_complete) {
2318 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002319 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002320 handle);
2321 return ffa_error(FFA_INVALID_PARAMETERS);
2322 }
2323
2324 if (share_state->fragment_count == MAX_FRAGMENTS) {
2325 /*
2326 * Log a warning as this is a sign that MAX_FRAGMENTS should
2327 * probably be increased.
2328 */
2329 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002330 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002331 "only %d supported.\n",
2332 handle, MAX_FRAGMENTS);
2333 /* Free share state, as it's not possible to complete it. */
2334 share_state_free(share_states, share_state, page_pool);
2335 return ffa_error(FFA_NO_MEMORY);
2336 }
2337
2338 *share_state_ret = share_state;
2339
2340 return (struct ffa_value){.func = FFA_SUCCESS_32};
2341}
2342
2343/**
J-Alves95df0ef2022-12-07 10:09:48 +00002344 * Checks if there is at least one receiver from the other world.
2345 */
J-Alvesfdd29272022-07-19 13:16:31 +01002346bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002347 struct ffa_memory_region *memory_region)
2348{
2349 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002350 struct ffa_memory_access *receiver =
2351 ffa_memory_region_get_receiver(memory_region, i);
2352 assert(receiver != NULL);
2353 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2354
2355 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002356 return true;
2357 }
2358 }
2359 return false;
2360}
2361
2362/**
J-Alves9da280b2022-12-21 14:55:39 +00002363 * Validates a call to donate, lend or share memory in which Hafnium is the
2364 * designated allocator of the memory handle. In practice, this also means
2365 * Hafnium is responsible for managing the state structures for the transaction.
2366 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2367 * sender is an SP or there is at least one borrower that is an SP.
2368 * If Hafnium is the hypervisor, it should allocate the memory handle when
2369 * operation involves only NWd VMs.
2370 *
2371 * If validation goes well, Hafnium updates the stage-2 page tables of the
2372 * sender. Validation consists of checking if the message length and number of
2373 * memory region constituents match, and if the transition is valid for the
2374 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002375 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002376 * Assumes that the caller has already found and locked the sender VM and copied
2377 * the memory region descriptor from the sender's TX buffer to a freshly
2378 * allocated page from Hafnium's internal pool. The caller must have also
2379 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002380 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002381 * This function takes ownership of the `memory_region` passed in and will free
2382 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002383 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002384struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002385 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002386 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002387 uint32_t fragment_length, uint32_t share_func,
2388 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002389{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002390 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002391 struct share_states_locked share_states;
2392 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002393
2394 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002395 * If there is an error validating the `memory_region` then we need to
2396 * free it because we own it but we won't be storing it in a share state
2397 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002398 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002399 ret = ffa_memory_send_validate(from_locked, memory_region,
2400 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002401 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002402 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002403 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002404 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002405 }
2406
Andrew Walbrana65a1322020-04-06 19:32:32 +01002407 /* Set flag for share function, ready to be retrieved later. */
2408 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002409 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002410 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002411 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002412 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002413 break;
J-Alves95fbb312024-03-20 15:19:16 +00002414 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002415 case FFA_MEM_LEND_32:
2416 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002417 break;
J-Alves95fbb312024-03-20 15:19:16 +00002418 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002419 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002420 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002421 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002422 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002423 default:
2424 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2425 ffa_func_name(share_func));
2426 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002427 }
2428
Andrew Walbranca808b12020-05-15 17:22:28 +01002429 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002430 /*
2431 * Allocate a share state before updating the page table. Otherwise if
2432 * updating the page table succeeded but allocating the share state
2433 * failed then it would leave the memory in a state where nobody could
2434 * get it back.
2435 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002436 share_state = allocate_share_state(share_states, share_func,
2437 memory_region, fragment_length,
2438 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002439 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002440 dlog_verbose("Failed to allocate share state.\n");
2441 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002442 ret = ffa_error(FFA_NO_MEMORY);
2443 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002444 }
2445
Andrew Walbranca808b12020-05-15 17:22:28 +01002446 if (fragment_length == memory_share_length) {
2447 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002448 ret = ffa_memory_send_complete(
2449 from_locked, share_states, share_state, page_pool,
2450 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002451 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002452 /*
2453 * Use sender ID from 'memory_region' assuming
2454 * that at this point it has been validated:
2455 * - MBZ at virtual FF-A instance.
2456 */
J-Alves19e20cf2023-08-02 12:48:55 +01002457 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002458 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2459 ? memory_region->sender
2460 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002461 ret = (struct ffa_value){
2462 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002463 .arg1 = (uint32_t)memory_region->handle,
2464 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002465 .arg3 = fragment_length,
2466 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002467 }
2468
2469out:
2470 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002472 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002473}
2474
2475/**
J-Alves8505a8a2022-06-15 18:10:18 +01002476 * Continues an operation to donate, lend or share memory to a VM from current
2477 * world. If this is the last fragment then checks that the transition is valid
2478 * for the type of memory sending operation and updates the stage-2 page tables
2479 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002480 *
2481 * Assumes that the caller has already found and locked the sender VM and copied
2482 * the memory region descriptor from the sender's TX buffer to a freshly
2483 * allocated page from Hafnium's internal pool.
2484 *
2485 * This function takes ownership of the `fragment` passed in; it must not be
2486 * freed by the caller.
2487 */
2488struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2489 void *fragment,
2490 uint32_t fragment_length,
2491 ffa_memory_handle_t handle,
2492 struct mpool *page_pool)
2493{
2494 struct share_states_locked share_states = share_states_lock();
2495 struct ffa_memory_share_state *share_state;
2496 struct ffa_value ret;
2497 struct ffa_memory_region *memory_region;
2498
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002499 CHECK(is_aligned(fragment,
2500 alignof(struct ffa_memory_region_constituent)));
2501 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2502 0) {
2503 dlog_verbose("Fragment length %u misaligned.\n",
2504 fragment_length);
2505 ret = ffa_error(FFA_INVALID_PARAMETERS);
2506 goto out_free_fragment;
2507 }
2508
Andrew Walbranca808b12020-05-15 17:22:28 +01002509 ret = ffa_memory_send_continue_validate(share_states, handle,
2510 &share_state,
2511 from_locked.vm->id, page_pool);
2512 if (ret.func != FFA_SUCCESS_32) {
2513 goto out_free_fragment;
2514 }
2515 memory_region = share_state->memory_region;
2516
J-Alves95df0ef2022-12-07 10:09:48 +00002517 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002518 dlog_error(
2519 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002520 "other world. This should never happen, and indicates "
2521 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002522 "EL3 code.\n");
2523 ret = ffa_error(FFA_INVALID_PARAMETERS);
2524 goto out_free_fragment;
2525 }
2526
2527 /* Add this fragment. */
2528 share_state->fragments[share_state->fragment_count] = fragment;
2529 share_state->fragment_constituent_counts[share_state->fragment_count] =
2530 fragment_length / sizeof(struct ffa_memory_region_constituent);
2531 share_state->fragment_count++;
2532
2533 /* Check whether the memory send operation is now ready to complete. */
2534 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002535 ret = ffa_memory_send_complete(
2536 from_locked, share_states, share_state, page_pool,
2537 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002538 } else {
2539 ret = (struct ffa_value){
2540 .func = FFA_MEM_FRAG_RX_32,
2541 .arg1 = (uint32_t)handle,
2542 .arg2 = (uint32_t)(handle >> 32),
2543 .arg3 = share_state_next_fragment_offset(share_states,
2544 share_state)};
2545 }
2546 goto out;
2547
2548out_free_fragment:
2549 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002550
2551out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002552 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002553 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554}
2555
Andrew Walbranca808b12020-05-15 17:22:28 +01002556/** Clean up after the receiver has finished retrieving a memory region. */
2557static void ffa_memory_retrieve_complete(
2558 struct share_states_locked share_states,
2559 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2560{
J-Alves95fbb312024-03-20 15:19:16 +00002561 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2562 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002563 /*
2564 * Memory that has been donated can't be relinquished,
2565 * so no need to keep the share state around.
2566 */
2567 share_state_free(share_states, share_state, page_pool);
2568 dlog_verbose("Freed share state for donate.\n");
2569 }
2570}
2571
J-Alves2d8457f2022-10-05 11:06:41 +01002572/**
2573 * Initialises the given memory region descriptor to be used for an
2574 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2575 * fragment.
2576 * The memory region descriptor is initialized according to retriever's
2577 * FF-A version.
2578 *
2579 * Returns true on success, or false if the given constituents won't all fit in
2580 * the first fragment.
2581 */
2582static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002583 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002584 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002585 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002586 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002587 struct ffa_memory_access *receivers, size_t receiver_count,
2588 uint32_t memory_access_desc_size, uint32_t page_count,
2589 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002590 const struct ffa_memory_region_constituent constituents[],
2591 uint32_t fragment_constituent_count, uint32_t *total_length,
2592 uint32_t *fragment_length)
2593{
2594 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002595 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002596 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002597 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002598
2599 assert(response != NULL);
2600
Karl Meakin0e617d92024-04-05 12:55:22 +01002601 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002602 struct ffa_memory_region_v1_0 *retrieve_response =
2603 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002604 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002605
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002606 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2607 attributes, flags, handle, 0,
2608 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002609
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002610 receiver = (struct ffa_memory_access_v1_0 *)
2611 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002612 receiver_count = retrieve_response->receiver_count;
2613
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002614 for (uint32_t i = 0; i < receiver_count; i++) {
2615 ffa_id_t receiver_id =
2616 receivers[i].receiver_permissions.receiver;
2617 ffa_memory_receiver_flags_t recv_flags =
2618 receivers[i].receiver_permissions.flags;
2619
2620 /*
2621 * Initialized here as in memory retrieve responses we
2622 * currently expect one borrower to be specified.
2623 */
2624 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002625 receiver, receiver_id, permissions.data_access,
2626 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002627 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002628
2629 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002630 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002631 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2632 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002633
2634 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2635 retrieve_response, 0);
2636 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002637 struct ffa_memory_region *retrieve_response =
2638 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002639 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002640
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002641 ffa_memory_region_init_header(
2642 retrieve_response, sender, attributes, flags, handle, 0,
2643 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002644
2645 /*
2646 * Note that `sizeof(struct_ffa_memory_region)` and
2647 * `sizeof(struct ffa_memory_access)` must both be multiples of
2648 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2649 * guaranteed that the offset we calculate here is aligned to a
2650 * 64-bit boundary and so 64-bit values can be copied without
2651 * alignment faults.
2652 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002653 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002654 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002655 (uint32_t)(receiver_count *
2656 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002657
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002658 retrieve_response_receivers =
2659 ffa_memory_region_get_receiver(retrieve_response, 0);
2660 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002661
2662 /*
2663 * Initialized here as in memory retrieve responses we currently
2664 * expect one borrower to be specified.
2665 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002666 memcpy_s(retrieve_response_receivers,
2667 sizeof(struct ffa_memory_access) * receiver_count,
2668 receivers,
2669 sizeof(struct ffa_memory_access) * receiver_count);
2670
2671 retrieve_response_receivers->composite_memory_region_offset =
2672 composite_offset;
2673
J-Alves2d8457f2022-10-05 11:06:41 +01002674 composite_memory_region =
2675 ffa_memory_region_get_composite(retrieve_response, 0);
2676 }
2677
J-Alves2d8457f2022-10-05 11:06:41 +01002678 assert(composite_memory_region != NULL);
2679
J-Alves2d8457f2022-10-05 11:06:41 +01002680 composite_memory_region->page_count = page_count;
2681 composite_memory_region->constituent_count = total_constituent_count;
2682 composite_memory_region->reserved_0 = 0;
2683
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002684 constituents_offset =
2685 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002686 if (constituents_offset +
2687 fragment_constituent_count *
2688 sizeof(struct ffa_memory_region_constituent) >
2689 response_max_size) {
2690 return false;
2691 }
2692
2693 for (i = 0; i < fragment_constituent_count; ++i) {
2694 composite_memory_region->constituents[i] = constituents[i];
2695 }
2696
2697 if (total_length != NULL) {
2698 *total_length =
2699 constituents_offset +
2700 composite_memory_region->constituent_count *
2701 sizeof(struct ffa_memory_region_constituent);
2702 }
2703 if (fragment_length != NULL) {
2704 *fragment_length =
2705 constituents_offset +
2706 fragment_constituent_count *
2707 sizeof(struct ffa_memory_region_constituent);
2708 }
2709
2710 return true;
2711}
2712
J-Alves96de29f2022-04-26 16:05:24 +01002713/**
2714 * Validates the retrieved permissions against those specified by the lender
2715 * of memory share operation. Optionally can help set the permissions to be used
2716 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002717 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2718 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2719 * specification for each ABI.
2720 * - FFA_DENIED -> if the permissions specified by the retriever are not
2721 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002722 */
J-Alvesdcad8992023-09-15 14:10:35 +01002723static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2724 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002725 enum ffa_data_access requested_data_access,
2726 enum ffa_instruction_access sent_instruction_access,
2727 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002728 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002729{
2730 switch (sent_data_access) {
2731 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2732 case FFA_DATA_ACCESS_RW:
2733 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2734 requested_data_access == FFA_DATA_ACCESS_RW) {
2735 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002736 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002737 }
2738 break;
2739 }
2740 /* Intentional fall-through. */
2741 case FFA_DATA_ACCESS_RO:
2742 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2743 requested_data_access == FFA_DATA_ACCESS_RO) {
2744 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002745 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002746 }
2747 break;
2748 }
2749 dlog_verbose(
2750 "Invalid data access requested; sender specified "
2751 "permissions %#x but receiver requested %#x.\n",
2752 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002753 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002754 case FFA_DATA_ACCESS_RESERVED:
2755 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2756 "checked before this point.");
2757 }
2758
J-Alvesdcad8992023-09-15 14:10:35 +01002759 /*
2760 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2761 * or FFA_MEMORY_DONATE the retriever should have specifed the
2762 * instruction permissions it wishes to receive.
2763 */
2764 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002765 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002766 case FFA_MEM_SHARE_32:
2767 if (requested_instruction_access !=
2768 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2769 dlog_verbose(
2770 "%s: for share instruction permissions must "
2771 "NOT be specified.\n",
2772 __func__);
2773 return ffa_error(FFA_INVALID_PARAMETERS);
2774 }
2775 break;
J-Alves95fbb312024-03-20 15:19:16 +00002776 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002777 case FFA_MEM_LEND_32:
2778 /*
2779 * For operations with multiple borrowers only permit XN
2780 * permissions, and both Sender and borrower should have used
2781 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2782 */
2783 if (multiple_borrowers) {
2784 if (requested_instruction_access !=
2785 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2786 dlog_verbose(
2787 "%s: lend/share/donate with multiple "
2788 "borrowers "
2789 "instruction permissions must NOT be "
2790 "specified.\n",
2791 __func__);
2792 return ffa_error(FFA_INVALID_PARAMETERS);
2793 }
2794 break;
2795 }
2796 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002797 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002798 case FFA_MEM_DONATE_32:
2799 if (!multiple_borrowers &&
2800 requested_instruction_access ==
2801 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2802 dlog_verbose(
2803 "%s: for lend/donate with single borrower "
2804 "instruction permissions must be speficified "
2805 "by borrower\n",
2806 __func__);
2807 return ffa_error(FFA_INVALID_PARAMETERS);
2808 }
2809 break;
2810 default:
2811 panic("%s: Wrong func id provided.\n", __func__);
2812 }
2813
J-Alves96de29f2022-04-26 16:05:24 +01002814 switch (sent_instruction_access) {
2815 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2816 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002817 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002818 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002819 permissions->instruction_access =
2820 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002821 }
2822 break;
2823 }
J-Alvesdcad8992023-09-15 14:10:35 +01002824 /*
2825 * Fall through if requested permissions are less
2826 * permissive than those provided by the sender.
2827 */
J-Alves96de29f2022-04-26 16:05:24 +01002828 case FFA_INSTRUCTION_ACCESS_NX:
2829 if (requested_instruction_access ==
2830 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2831 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2832 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002833 permissions->instruction_access =
2834 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002835 }
2836 break;
2837 }
2838 dlog_verbose(
2839 "Invalid instruction access requested; sender "
2840 "specified permissions %#x but receiver requested "
2841 "%#x.\n",
2842 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002843 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002844 case FFA_INSTRUCTION_ACCESS_RESERVED:
2845 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2846 "be checked before this point.");
2847 }
2848
J-Alvesdcad8992023-09-15 14:10:35 +01002849 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002850}
2851
2852/**
2853 * Validate the receivers' permissions in the retrieve request against those
2854 * specified by the lender.
2855 * In the `permissions` argument returns the permissions to set at S2 for the
2856 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002857 * The function looks into the flag to bypass multiple borrower checks:
2858 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2859 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2860 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2861 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002862 */
2863static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2864 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002865 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002866 ffa_memory_access_permissions_t *permissions,
2867 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002868{
2869 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002870 bool bypass_multi_receiver_check =
2871 (retrieve_request->flags &
2872 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002873 const uint32_t region_receiver_count = memory_region->receiver_count;
2874 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002875
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002876 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002877 assert(permissions != NULL);
2878
Karl Meakin84710f32023-10-12 15:14:49 +01002879 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002880
J-Alves3456e032023-07-20 12:20:05 +01002881 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002882 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002883 dlog_verbose(
2884 "Retrieve request should contain same list of "
2885 "borrowers, as specified by the lender.\n");
2886 return ffa_error(FFA_INVALID_PARAMETERS);
2887 }
2888 } else {
2889 if (retrieve_request->receiver_count != 1) {
2890 dlog_verbose(
2891 "Set bypass multiple borrower check, receiver "
2892 "list must be sized 1 (%x)\n",
2893 memory_region->receiver_count);
2894 return ffa_error(FFA_INVALID_PARAMETERS);
2895 }
J-Alves96de29f2022-04-26 16:05:24 +01002896 }
2897
2898 retrieve_receiver_index = retrieve_request->receiver_count;
2899
J-Alves96de29f2022-04-26 16:05:24 +01002900 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2901 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002902 struct ffa_memory_access *retrieve_request_receiver =
2903 ffa_memory_region_get_receiver(retrieve_request, i);
2904 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002905 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002906 retrieve_request_receiver->receiver_permissions
2907 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002908 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002909 retrieve_request_receiver->receiver_permissions
2910 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002911 struct ffa_memory_access *receiver;
2912 uint32_t mem_region_receiver_index;
2913 bool permissions_RO;
2914 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002915 /*
2916 * If the call is at the virtual FF-A instance the caller's
2917 * ID must match an entry in the memory access list.
2918 * In the SPMC, one of the specified receivers could be from
2919 * the NWd.
2920 */
2921 bool found_to_id = vm_id_is_current_world(to_vm_id)
2922 ? (current_receiver_id == to_vm_id)
2923 : (!vm_id_is_current_world(
2924 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002925
J-Alves3456e032023-07-20 12:20:05 +01002926 if (bypass_multi_receiver_check && !found_to_id) {
2927 dlog_verbose(
2928 "Bypass multiple borrower check for id %x.\n",
2929 current_receiver_id);
2930 continue;
2931 }
2932
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002933 if (retrieve_request_receiver->composite_memory_region_offset !=
2934 0U) {
2935 dlog_verbose(
2936 "Retriever specified address ranges not "
2937 "supported (got offset %d).\n",
2938 retrieve_request_receiver
2939 ->composite_memory_region_offset);
2940 return ffa_error(FFA_INVALID_PARAMETERS);
2941 }
2942
J-Alves96de29f2022-04-26 16:05:24 +01002943 /*
2944 * Find the current receiver in the transaction descriptor from
2945 * sender.
2946 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002947 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002948 ffa_memory_region_get_receiver_index(
2949 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002950
2951 if (mem_region_receiver_index ==
2952 memory_region->receiver_count) {
2953 dlog_verbose("%s: receiver %x not found\n", __func__,
2954 current_receiver_id);
2955 return ffa_error(FFA_DENIED);
2956 }
2957
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002958 receiver = ffa_memory_region_get_receiver(
2959 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002960 assert(receiver != NULL);
2961
2962 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002963
2964 if (found_to_id) {
2965 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002966
2967 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002968 }
2969
2970 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002971 * Check if retrieve request memory access list is valid:
2972 * - The retrieve request complies with the specification.
2973 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002974 */
J-Alvesdcad8992023-09-15 14:10:35 +01002975 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002976 func_id, sent_permissions.data_access,
2977 requested_permissions.data_access,
2978 sent_permissions.instruction_access,
2979 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002980 found_to_id ? permissions : NULL,
2981 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002982
J-Alvesdcad8992023-09-15 14:10:35 +01002983 if (ret.func != FFA_SUCCESS_32) {
2984 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002985 }
2986
Karl Meakin84710f32023-10-12 15:14:49 +01002987 permissions_RO =
2988 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002989 clear_memory_flags =
2990 (retrieve_request->flags &
2991 (FFA_MEMORY_REGION_FLAG_CLEAR |
2992 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002993
J-Alves96de29f2022-04-26 16:05:24 +01002994 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002995 * Can't request PM to clear memory if only provided
2996 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002997 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002998 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002999 dlog_verbose(
3000 "Receiver has RO permissions can not request "
3001 "clear.\n");
3002 return ffa_error(FFA_DENIED);
3003 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003004
3005 /*
3006 * Check the impdef in the retrieve_request matches the value in
3007 * the original memory send.
3008 */
3009 if (ffa_version_from_memory_access_desc_size(
3010 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003011 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003012 ffa_version_from_memory_access_desc_size(
3013 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003014 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003015 if (receiver->impdef.val[0] !=
3016 retrieve_request_receiver->impdef.val[0] ||
3017 receiver->impdef.val[1] !=
3018 retrieve_request_receiver->impdef.val[1]) {
3019 dlog_verbose(
3020 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003021 "match retrieve request value send "
3022 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003023 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003024 receiver->impdef.val[0],
3025 receiver->impdef.val[1],
3026 retrieve_request_receiver->impdef
3027 .val[0],
3028 retrieve_request_receiver->impdef
3029 .val[1]);
3030 return ffa_error(FFA_INVALID_PARAMETERS);
3031 }
3032 }
J-Alves96de29f2022-04-26 16:05:24 +01003033 }
3034
3035 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3036 dlog_verbose(
3037 "Retrieve request does not contain caller's (%x) "
3038 "permissions\n",
3039 to_vm_id);
3040 return ffa_error(FFA_INVALID_PARAMETERS);
3041 }
3042
3043 return (struct ffa_value){.func = FFA_SUCCESS_32};
3044}
3045
Daniel Boulby296ee702023-11-28 13:36:55 +00003046/**
3047 * According to section 17.4.3 of the FF-A v1.2 ALP0 specification, the
3048 * hypervisor may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region
3049 * description of a pending memory sharing operation whose allocator is the SPM,
3050 * for validation purposes before forwarding an FFA_MEM_RECLAIM call. For a
3051 * hypervisor retrieve request the endpoint memory access descriptor count must
3052 * be 0 (for any other retrieve request it must be >= 1).
J-Alvesa9cd7e32022-07-01 13:49:33 +01003053 */
Daniel Boulby296ee702023-11-28 13:36:55 +00003054bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003055{
Daniel Boulby296ee702023-11-28 13:36:55 +00003056 return request->receiver_count == 0U;
3057}
3058
J-Alvesa9cd7e32022-07-01 13:49:33 +01003059/*
3060 * Helper to reset count of fragments retrieved by the hypervisor.
3061 */
3062static void ffa_memory_retrieve_complete_from_hyp(
3063 struct ffa_memory_share_state *share_state)
3064{
3065 if (share_state->hypervisor_fragment_count ==
3066 share_state->fragment_count) {
3067 share_state->hypervisor_fragment_count = 0;
3068 }
3069}
3070
J-Alves089004f2022-07-13 14:25:44 +01003071/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003072 * Prepares the return of the ffa_value for the memory retrieve response.
3073 */
3074static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3075 uint32_t fragment_length)
3076{
3077 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3078 .arg1 = total_length,
3079 .arg2 = fragment_length};
3080}
3081
3082/**
J-Alves089004f2022-07-13 14:25:44 +01003083 * Validate that the memory region descriptor provided by the borrower on
3084 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3085 * memory sharing call.
3086 */
3087static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003088 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3089 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003090 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3091 uint32_t share_func)
3092{
3093 ffa_memory_region_flags_t transaction_type =
3094 retrieve_request->flags &
3095 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003096 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003097 const uint64_t memory_access_desc_size =
3098 retrieve_request->memory_access_desc_size;
3099 const uint32_t expected_retrieve_request_length =
3100 retrieve_request->receivers_offset +
3101 (uint32_t)(retrieve_request->receiver_count *
3102 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003103
3104 assert(retrieve_request != NULL);
3105 assert(memory_region != NULL);
3106 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003107
J-Alves4f0d9c12024-01-17 17:23:11 +00003108 if (retrieve_request_length != expected_retrieve_request_length) {
3109 dlog_verbose(
3110 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3111 "but was %d.\n",
3112 expected_retrieve_request_length,
3113 retrieve_request_length);
3114 return ffa_error(FFA_INVALID_PARAMETERS);
3115 }
3116
3117 if (retrieve_request->sender != memory_region->sender) {
3118 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003119 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003120 "retrieve.\n",
3121 memory_region->handle);
3122 return ffa_error(FFA_DENIED);
3123 }
3124
3125 /*
3126 * The SPMC can only process retrieve requests to memory share
3127 * operations with one borrower from the other world. It can't
3128 * determine the ID of the NWd VM that invoked the retrieve
3129 * request interface call. It relies on the hypervisor to
3130 * validate the caller's ID against that provided in the
3131 * `receivers` list of the retrieve response.
3132 * In case there is only one borrower from the NWd in the
3133 * transaction descriptor, record that in the `receiver_id` for
3134 * later use, and validate in the retrieve request message.
3135 * This limitation is due to the fact SPMC can't determine the
3136 * index in the memory share structures state to update.
3137 */
3138 if (to_id == HF_HYPERVISOR_VM_ID) {
3139 uint32_t other_world_count = 0;
3140
3141 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3142 struct ffa_memory_access *receiver =
3143 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003144 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003145 assert(receiver != NULL);
3146
J-Alvesf220d572024-04-24 22:15:14 +01003147 if (!vm_id_is_current_world(
3148 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003149 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003150 /* Set it to be used later. */
3151 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003152 }
3153 }
3154
3155 if (other_world_count > 1) {
3156 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003157 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003158 return ffa_error(FFA_NOT_SUPPORTED);
3159 }
3160 }
J-Alves089004f2022-07-13 14:25:44 +01003161 /*
3162 * Check that the transaction type expected by the receiver is
3163 * correct, if it has been specified.
3164 */
3165 if (transaction_type !=
3166 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3167 transaction_type != (memory_region->flags &
3168 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3169 dlog_verbose(
3170 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003171 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003172 transaction_type,
3173 memory_region->flags &
3174 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3175 retrieve_request->handle);
3176 return ffa_error(FFA_INVALID_PARAMETERS);
3177 }
3178
3179 if (retrieve_request->tag != memory_region->tag) {
3180 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003181 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3182 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003183 retrieve_request->tag, memory_region->tag,
3184 retrieve_request->handle);
3185 return ffa_error(FFA_INVALID_PARAMETERS);
3186 }
3187
J-Alves4f0d9c12024-01-17 17:23:11 +00003188 *receiver_index =
3189 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003190
3191 if (*receiver_index == memory_region->receiver_count) {
3192 dlog_verbose(
3193 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003194 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003195 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003196 return ffa_error(FFA_INVALID_PARAMETERS);
3197 }
3198
3199 if ((retrieve_request->flags &
3200 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3201 dlog_verbose(
3202 "Retriever specified 'address range alignment 'hint' "
3203 "not supported.\n");
3204 return ffa_error(FFA_INVALID_PARAMETERS);
3205 }
3206 if ((retrieve_request->flags &
3207 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3208 dlog_verbose(
3209 "Bits 8-5 must be zero in memory region's flags "
3210 "(address range alignment hint not supported).\n");
3211 return ffa_error(FFA_INVALID_PARAMETERS);
3212 }
3213
3214 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3215 dlog_verbose(
3216 "Bits 31-10 must be zero in memory region's flags.\n");
3217 return ffa_error(FFA_INVALID_PARAMETERS);
3218 }
3219
J-Alves95fbb312024-03-20 15:19:16 +00003220 if ((share_func == FFA_MEM_SHARE_32 ||
3221 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003222 (retrieve_request->flags &
3223 (FFA_MEMORY_REGION_FLAG_CLEAR |
3224 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3225 dlog_verbose(
3226 "Memory Share operation can't clean after relinquish "
3227 "memory region.\n");
3228 return ffa_error(FFA_INVALID_PARAMETERS);
3229 }
3230
3231 /*
3232 * If the borrower needs the memory to be cleared before mapping
3233 * to its address space, the sender should have set the flag
3234 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3235 * FFA_DENIED.
3236 */
3237 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3238 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3239 dlog_verbose(
3240 "Borrower needs memory cleared. Sender needs to set "
3241 "flag for clearing memory.\n");
3242 return ffa_error(FFA_DENIED);
3243 }
3244
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003245 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003246 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003247 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3248 dlog_verbose(
3249 "Invalid security state for memory retrieve request "
3250 "operation.\n");
3251 return ffa_error(FFA_INVALID_PARAMETERS);
3252 }
3253
J-Alves089004f2022-07-13 14:25:44 +01003254 /*
3255 * If memory type is not specified, bypass validation of memory
3256 * attributes in the retrieve request. The retriever is expecting to
3257 * obtain this information from the SPMC.
3258 */
Karl Meakin84710f32023-10-12 15:14:49 +01003259 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003260 return (struct ffa_value){.func = FFA_SUCCESS_32};
3261 }
3262
3263 /*
3264 * Ensure receiver's attributes are compatible with how
3265 * Hafnium maps memory: Normal Memory, Inner shareable,
3266 * Write-Back Read-Allocate Write-Allocate Cacheable.
3267 */
3268 return ffa_memory_attributes_validate(retrieve_request->attributes);
3269}
3270
J-Alves3f6527c2024-04-25 17:10:57 +01003271/**
3272 * Whilst processing the retrieve request, the operation could be aborted, and
3273 * changes to page tables and the share state structures need to be reverted.
3274 */
3275static void ffa_partition_memory_retrieve_request_undo(
3276 struct vm_locked from_locked,
3277 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3278{
3279 /*
3280 * Currently this operation is expected for operations involving the
3281 * 'other_world' vm.
3282 */
3283 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3284 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3285
3286 /* Decrement the retrieved fragment count for the given receiver. */
3287 share_state->retrieved_fragment_count[receiver_index]--;
3288}
3289
3290/**
3291 * Whilst processing an hypervisor retrieve request the operation could be
3292 * aborted. There were no updates to PTs in this case, so decrementing the
3293 * fragment count retrieved by the hypervisor should be enough.
3294 */
3295static void ffa_hypervisor_memory_retrieve_request_undo(
3296 struct ffa_memory_share_state *share_state)
3297{
3298 assert(share_state->hypervisor_fragment_count > 0);
3299 share_state->hypervisor_fragment_count--;
3300}
3301
J-Alves4f0d9c12024-01-17 17:23:11 +00003302static struct ffa_value ffa_partition_retrieve_request(
3303 struct share_states_locked share_states,
3304 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3305 struct ffa_memory_region *retrieve_request,
3306 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003307{
Karl Meakin84710f32023-10-12 15:14:49 +01003308 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003309 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003310 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003311 struct ffa_composite_memory_region *composite;
3312 uint32_t total_length;
3313 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003314 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003315 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003316 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003317 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003318 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003319 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003320 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003321 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003322 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003323 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003324
J-Alves96de29f2022-04-26 16:05:24 +01003325 if (!share_state->sending_complete) {
3326 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003327 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003328 "retrieve.\n",
3329 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003330 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003331 }
3332
J-Alves4f0d9c12024-01-17 17:23:11 +00003333 /*
3334 * Validate retrieve request, according to what was sent by the
3335 * sender. Function will output the `receiver_index` from the
3336 * provided memory region.
3337 */
3338 ret = ffa_memory_retrieve_validate(
3339 receiver_id, retrieve_request, retrieve_request_length,
3340 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003341
J-Alves4f0d9c12024-01-17 17:23:11 +00003342 if (ret.func != FFA_SUCCESS_32) {
3343 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003344 }
J-Alves96de29f2022-04-26 16:05:24 +01003345
J-Alves4f0d9c12024-01-17 17:23:11 +00003346 /*
3347 * Validate the requested permissions against the sent
3348 * permissions.
3349 * Outputs the permissions to give to retriever at S2
3350 * PTs.
3351 */
3352 ret = ffa_memory_retrieve_validate_memory_access_list(
3353 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003354 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003355 if (ret.func != FFA_SUCCESS_32) {
3356 return ret;
3357 }
3358
3359 memory_to_mode = ffa_memory_permissions_to_mode(
3360 permissions, share_state->sender_orig_mode);
3361
Daniel Boulby6e261362024-06-13 16:53:00 +01003362 /*
3363 * Check requested memory type is valid with the memory type of the
3364 * owner. E.g. they follow the memory type precedence where Normal
3365 * memory is more permissive than device and therefore device memory
3366 * can only be shared as device memory.
3367 */
3368 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3369 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3370 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3371 dlog_verbose(
3372 "Retrieving device memory as Normal memory is not "
3373 "allowed\n");
3374 return ffa_error(FFA_DENIED);
3375 }
3376
J-Alves4f0d9c12024-01-17 17:23:11 +00003377 ret = ffa_retrieve_check_update(
3378 to_locked, share_state->fragments,
3379 share_state->fragment_constituent_counts,
3380 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003381 share_state->share_func, false, page_pool, &retrieve_mode,
3382 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003383
3384 if (ret.func != FFA_SUCCESS_32) {
3385 return ret;
3386 }
3387
3388 share_state->retrieved_fragment_count[receiver_index] = 1;
3389
3390 is_retrieve_complete =
3391 share_state->retrieved_fragment_count[receiver_index] ==
3392 share_state->fragment_count;
3393
J-Alvesb5084cf2022-07-06 14:20:12 +01003394 /* VMs acquire the RX buffer from SPMC. */
3395 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3396
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003397 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003398 * Copy response to RX buffer of caller and deliver the message.
3399 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003400 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003401 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003402
Andrew Walbranca808b12020-05-15 17:22:28 +01003403 /*
J-Alves460d36c2023-10-12 17:02:15 +01003404 * Set the security state in the memory retrieve response attributes
3405 * if specified by the target mode.
3406 */
3407 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3408 retrieve_mode);
3409
3410 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003411 * Constituents which we received in the first fragment should
3412 * always fit in the first fragment we are sending, because the
3413 * header is the same size in both cases and we have a fixed
3414 * message buffer size. So `ffa_retrieved_memory_region_init`
3415 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003416 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003417
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003418 /* Provide the permissions that had been provided. */
3419 receiver->receiver_permissions.permissions = permissions;
3420
3421 /*
3422 * Prepare the memory region descriptor for the retrieve response.
3423 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003424 * structures.
3425 * At this point the retrieve request descriptor from the partition
3426 * has been processed. The `retrieve_request` is expected to be in
3427 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3428 * prepare the retrieve response before copying it to the RX buffer of
3429 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003430 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003431 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003432 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3433 memory_region->sender, attributes, memory_region->flags, handle,
3434 permissions, receiver, 1, memory_access_desc_size,
3435 composite->page_count, composite->constituent_count,
3436 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003437 share_state->fragment_constituent_counts[0], &total_length,
3438 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003439
J-Alves7b9cc432024-04-04 10:57:17 +01003440 /*
3441 * Copy the message from the buffer into the partition's mailbox.
3442 * The operation might fail unexpectedly due to change in PAS address
3443 * space, or improper values to the sizes of the structures.
3444 */
3445 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3446 retrieve_request, fragment_length)) {
3447 dlog_error(
3448 "%s: aborted the copy of response to RX buffer of "
3449 "%x.\n",
3450 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003451
3452 ffa_partition_memory_retrieve_request_undo(
3453 to_locked, share_state, receiver_index);
3454
J-Alves7b9cc432024-04-04 10:57:17 +01003455 return ffa_error(FFA_ABORTED);
3456 }
3457
J-Alves4f0d9c12024-01-17 17:23:11 +00003458 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003459 ffa_memory_retrieve_complete(share_states, share_state,
3460 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003461 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003462
3463 return ffa_memory_retrieve_resp(total_length, fragment_length);
3464}
3465
3466static struct ffa_value ffa_hypervisor_retrieve_request(
3467 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3468 struct ffa_memory_region *retrieve_request)
3469{
3470 struct ffa_value ret;
3471 struct ffa_composite_memory_region *composite;
3472 uint32_t total_length;
3473 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003474 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003475 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003476 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003477 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003478 ffa_memory_handle_t handle = retrieve_request->handle;
3479
J-Alves4f0d9c12024-01-17 17:23:11 +00003480 memory_region = share_state->memory_region;
3481
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003482 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3483
J-Alves7b6ab612024-01-24 09:54:54 +00003484 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003485 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003486 memory_access_desc_size = sizeof(struct ffa_memory_access);
3487 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003488 case FFA_VERSION_1_0:
3489 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003490 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3491 break;
3492 default:
3493 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3494 }
3495
J-Alves4f0d9c12024-01-17 17:23:11 +00003496 if (share_state->hypervisor_fragment_count != 0U) {
3497 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003498 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003499 "the hypervisor.\n",
3500 handle);
3501 return ffa_error(FFA_DENIED);
3502 }
3503
3504 share_state->hypervisor_fragment_count = 1;
3505
J-Alves4f0d9c12024-01-17 17:23:11 +00003506 /* VMs acquire the RX buffer from SPMC. */
3507 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3508
3509 /*
3510 * Copy response to RX buffer of caller and deliver the message.
3511 * This must be done before the share_state is (possibly) freed.
3512 */
3513 composite = ffa_memory_region_get_composite(memory_region, 0);
3514
3515 /*
3516 * Constituents which we received in the first fragment should
3517 * always fit in the first fragment we are sending, because the
3518 * header is the same size in both cases and we have a fixed
3519 * message buffer size. So `ffa_retrieved_memory_region_init`
3520 * should never fail.
3521 */
3522
3523 /*
3524 * Set the security state in the memory retrieve response attributes
3525 * if specified by the target mode.
3526 */
3527 attributes = plat_ffa_memory_security_mode(
3528 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003529
3530 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3531
J-Alves7b9cc432024-04-04 10:57:17 +01003532 /*
3533 * At this point the `retrieve_request` is expected to be in a section
3534 * managed by the hypervisor.
3535 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003536 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003537 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3538 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003539 receiver->receiver_permissions.permissions, receiver,
3540 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003541 composite->page_count, composite->constituent_count,
3542 share_state->fragments[0],
3543 share_state->fragment_constituent_counts[0], &total_length,
3544 &fragment_length));
3545
J-Alves7b9cc432024-04-04 10:57:17 +01003546 /*
3547 * Copy the message from the buffer into the hypervisor's mailbox.
3548 * The operation might fail unexpectedly due to change in PAS, or
3549 * improper values for the sizes of the structures.
3550 */
3551 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3552 retrieve_request, fragment_length)) {
3553 dlog_error(
3554 "%s: aborted the copy of response to RX buffer of "
3555 "%x.\n",
3556 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003557
3558 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3559
J-Alves7b9cc432024-04-04 10:57:17 +01003560 return ffa_error(FFA_ABORTED);
3561 }
3562
J-Alves3f6527c2024-04-25 17:10:57 +01003563 ffa_memory_retrieve_complete_from_hyp(share_state);
3564
J-Alves4f0d9c12024-01-17 17:23:11 +00003565 return ffa_memory_retrieve_resp(total_length, fragment_length);
3566}
3567
3568struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3569 struct ffa_memory_region *retrieve_request,
3570 uint32_t retrieve_request_length,
3571 struct mpool *page_pool)
3572{
3573 ffa_memory_handle_t handle = retrieve_request->handle;
3574 struct share_states_locked share_states;
3575 struct ffa_memory_share_state *share_state;
3576 struct ffa_value ret;
3577
3578 dump_share_states();
3579
3580 share_states = share_states_lock();
3581 share_state = get_share_state(share_states, handle);
3582 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003583 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003584 handle);
3585 ret = ffa_error(FFA_INVALID_PARAMETERS);
3586 goto out;
3587 }
3588
Daniel Boulby296ee702023-11-28 13:36:55 +00003589 if (is_ffa_hypervisor_retrieve_request(retrieve_request)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003590 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3591 retrieve_request);
3592 } else {
3593 ret = ffa_partition_retrieve_request(
3594 share_states, share_state, to_locked, retrieve_request,
3595 retrieve_request_length, page_pool);
3596 }
3597
3598 /* Track use of the RX buffer if the handling has succeeded. */
3599 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3600 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3601 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3602 }
3603
Andrew Walbranca808b12020-05-15 17:22:28 +01003604out:
3605 share_states_unlock(&share_states);
3606 dump_share_states();
3607 return ret;
3608}
3609
J-Alves5da37d92022-10-24 16:33:48 +01003610/**
3611 * Determine expected fragment offset according to the FF-A version of
3612 * the caller.
3613 */
3614static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3615 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003616 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003617{
3618 uint32_t expected_fragment_offset;
3619 uint32_t composite_constituents_offset;
3620
Karl Meakin0e617d92024-04-05 12:55:22 +01003621 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003622 /*
3623 * Hafnium operates memory regions in FF-A v1.1 format, so we
3624 * can retrieve the constituents offset from descriptor.
3625 */
3626 composite_constituents_offset =
3627 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003628 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003629 /*
3630 * If retriever is FF-A v1.0, determine the composite offset
3631 * as it is expected to have been configured in the
3632 * retrieve response.
3633 */
3634 composite_constituents_offset =
3635 sizeof(struct ffa_memory_region_v1_0) +
3636 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003637 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003638 sizeof(struct ffa_composite_memory_region);
3639 } else {
3640 panic("%s received an invalid FF-A version.\n", __func__);
3641 }
3642
3643 expected_fragment_offset =
3644 composite_constituents_offset +
3645 retrieved_constituents_count *
3646 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003647 (size_t)(memory_region->memory_access_desc_size *
3648 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003649
3650 return expected_fragment_offset;
3651}
3652
Andrew Walbranca808b12020-05-15 17:22:28 +01003653struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3654 ffa_memory_handle_t handle,
3655 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003656 ffa_id_t sender_vm_id,
J-Alvesc3fd9752024-04-04 11:45:33 +01003657 void *retrieve_continue_page,
Andrew Walbranca808b12020-05-15 17:22:28 +01003658 struct mpool *page_pool)
3659{
3660 struct ffa_memory_region *memory_region;
3661 struct share_states_locked share_states;
3662 struct ffa_memory_share_state *share_state;
3663 struct ffa_value ret;
3664 uint32_t fragment_index;
3665 uint32_t retrieved_constituents_count;
3666 uint32_t i;
3667 uint32_t expected_fragment_offset;
3668 uint32_t remaining_constituent_count;
3669 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003670 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003671 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003672
3673 dump_share_states();
3674
3675 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003676 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003677 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003678 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003679 handle);
3680 ret = ffa_error(FFA_INVALID_PARAMETERS);
3681 goto out;
3682 }
3683
3684 memory_region = share_state->memory_region;
3685 CHECK(memory_region != NULL);
3686
Andrew Walbranca808b12020-05-15 17:22:28 +01003687 if (!share_state->sending_complete) {
3688 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003689 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003690 "retrieve.\n",
3691 handle);
3692 ret = ffa_error(FFA_INVALID_PARAMETERS);
3693 goto out;
3694 }
3695
J-Alves59ed0042022-07-28 18:26:41 +01003696 /*
3697 * If retrieve request from the hypervisor has been initiated in the
3698 * given share_state, continue it, else assume it is a continuation of
J-Alvesc3fd9752024-04-04 11:45:33 +01003699 * retrieve request from a partition.
J-Alves59ed0042022-07-28 18:26:41 +01003700 */
3701 continue_ffa_hyp_mem_retrieve_req =
3702 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3703 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003704 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003705
J-Alves59ed0042022-07-28 18:26:41 +01003706 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003707 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003708 memory_region, to_locked.vm->id);
3709
3710 if (receiver_index == memory_region->receiver_count) {
3711 dlog_verbose(
3712 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003713 "borrower to memory sharing transaction "
3714 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003715 to_locked.vm->id, handle);
3716 ret = ffa_error(FFA_INVALID_PARAMETERS);
3717 goto out;
3718 }
3719
J-Alvesc3fd9752024-04-04 11:45:33 +01003720 fragment_index =
3721 share_state->retrieved_fragment_count[receiver_index];
3722
3723 if (fragment_index == 0 ||
3724 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003725 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003726 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003727 "started or already completed (%d/%d fragments "
3728 "retrieved).\n",
3729 handle,
3730 share_state->retrieved_fragment_count
3731 [receiver_index],
3732 share_state->fragment_count);
3733 ret = ffa_error(FFA_INVALID_PARAMETERS);
3734 goto out;
3735 }
J-Alves59ed0042022-07-28 18:26:41 +01003736 } else {
J-Alvesc3fd9752024-04-04 11:45:33 +01003737 fragment_index = share_state->hypervisor_fragment_count;
3738
3739 if (fragment_index == 0 ||
3740 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003741 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003742 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003743 "started from hypervisor.\n",
3744 handle);
3745 ret = ffa_error(FFA_INVALID_PARAMETERS);
3746 goto out;
3747 }
3748
3749 if (memory_region->sender != sender_vm_id) {
3750 dlog_verbose(
3751 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003752 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003753 sender_vm_id, handle);
3754 ret = ffa_error(FFA_INVALID_PARAMETERS);
3755 goto out;
3756 }
3757
J-Alves59ed0042022-07-28 18:26:41 +01003758 receiver_index = 0;
3759 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003760
3761 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003762 * Check that the given fragment offset is correct by counting
3763 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003764 */
3765 retrieved_constituents_count = 0;
3766 for (i = 0; i < fragment_index; ++i) {
3767 retrieved_constituents_count +=
3768 share_state->fragment_constituent_counts[i];
3769 }
J-Alvesc7484f12022-05-13 12:41:14 +01003770
3771 CHECK(memory_region->receiver_count > 0);
3772
Andrew Walbranca808b12020-05-15 17:22:28 +01003773 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003774 ffa_memory_retrieve_expected_offset_per_ffa_version(
3775 memory_region, retrieved_constituents_count,
3776 to_locked.vm->ffa_version);
3777
Andrew Walbranca808b12020-05-15 17:22:28 +01003778 if (fragment_offset != expected_fragment_offset) {
3779 dlog_verbose("Fragment offset was %d but expected %d.\n",
3780 fragment_offset, expected_fragment_offset);
3781 ret = ffa_error(FFA_INVALID_PARAMETERS);
3782 goto out;
3783 }
3784
J-Alves4f0d9c12024-01-17 17:23:11 +00003785 /*
3786 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3787 * is currently ownder by the SPMC.
3788 */
3789 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003790
Andrew Walbranca808b12020-05-15 17:22:28 +01003791 remaining_constituent_count = ffa_memory_fragment_init(
J-Alvesc3fd9752024-04-04 11:45:33 +01003792 (struct ffa_memory_region_constituent *)retrieve_continue_page,
3793 HF_MAILBOX_SIZE, share_state->fragments[fragment_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01003794 share_state->fragment_constituent_counts[fragment_index],
3795 &fragment_length);
3796 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003797
J-Alvesc3fd9752024-04-04 11:45:33 +01003798 /*
3799 * Return FFA_ERROR(FFA_ABORTED) in case the access to the partition's
3800 * RX buffer results in a GPF exception. Could happen if the retrieve
3801 * request is for a VM or the Hypervisor retrieve request, if the PAS
3802 * has been changed externally.
3803 */
3804 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3805 retrieve_continue_page, fragment_length)) {
3806 dlog_error(
3807 "%s: aborted copying fragment to RX buffer of %#x.\n",
3808 __func__, to_locked.vm->id);
3809 ret = ffa_error(FFA_ABORTED);
3810 goto out;
3811 }
3812
Andrew Walbranca808b12020-05-15 17:22:28 +01003813 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003814 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003815
J-Alves59ed0042022-07-28 18:26:41 +01003816 if (!continue_ffa_hyp_mem_retrieve_req) {
3817 share_state->retrieved_fragment_count[receiver_index]++;
3818 if (share_state->retrieved_fragment_count[receiver_index] ==
3819 share_state->fragment_count) {
3820 ffa_memory_retrieve_complete(share_states, share_state,
3821 page_pool);
3822 }
3823 } else {
3824 share_state->hypervisor_fragment_count++;
3825
3826 ffa_memory_retrieve_complete_from_hyp(share_state);
3827 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003828 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3829 .arg1 = (uint32_t)handle,
3830 .arg2 = (uint32_t)(handle >> 32),
3831 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003832
3833out:
3834 share_states_unlock(&share_states);
3835 dump_share_states();
3836 return ret;
3837}
3838
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003839struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003840 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003841 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003842{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003843 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003844 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003845 struct ffa_memory_share_state *share_state;
3846 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003847 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003848 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003849 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003850 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003851 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003852
Andrew Walbrana65a1322020-04-06 19:32:32 +01003853 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003854 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003855 "Stream endpoints not supported (got %d endpoints on "
3856 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003857 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003858 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003859 }
3860
J-Alvesbd060342024-04-26 18:44:31 +01003861 if (vm_id_is_current_world(from_locked.vm->id) &&
3862 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003863 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003864 "VM ID %d in relinquish message doesn't match calling "
3865 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003866 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003867 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003868 }
3869
3870 dump_share_states();
3871
3872 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003873 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003874 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003875 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003876 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003877 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003878 goto out;
3879 }
3880
Andrew Walbranca808b12020-05-15 17:22:28 +01003881 if (!share_state->sending_complete) {
3882 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003883 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003884 "relinquish.\n",
3885 handle);
3886 ret = ffa_error(FFA_INVALID_PARAMETERS);
3887 goto out;
3888 }
3889
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003890 memory_region = share_state->memory_region;
3891 CHECK(memory_region != NULL);
3892
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003893 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003894 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003895
3896 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003897 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003898 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003899 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003900 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003901 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003902 goto out;
3903 }
3904
J-Alves8eb19162022-04-28 10:56:48 +01003905 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003906 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003907 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003908 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003909 "receiver %x can't relinquish.\n",
3910 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003911 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003912 goto out;
3913 }
3914
J-Alves3c5b2072022-11-21 12:45:40 +00003915 /*
3916 * Either clear if requested in relinquish call, or in a retrieve
3917 * request from one of the borrowers.
3918 */
3919 receivers_relinquished_memory = true;
3920
3921 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3922 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003923 ffa_memory_region_get_receiver(memory_region, i);
3924 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003925 if (receiver->receiver_permissions.receiver ==
3926 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003927 receiver_permissions =
3928 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003929 continue;
3930 }
3931
3932 if (share_state->retrieved_fragment_count[i] != 0U) {
3933 receivers_relinquished_memory = false;
3934 break;
3935 }
3936 }
3937
3938 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003939 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3940 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003941
3942 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003943 * Clear is not allowed for memory that was shared, as the
3944 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003945 */
J-Alves95fbb312024-03-20 15:19:16 +00003946 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3947 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003948 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003949 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003950 goto out;
3951 }
3952
J-Alvesb886d492024-04-15 10:55:29 +01003953 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003954 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3955 __func__);
3956 ret = ffa_error(FFA_DENIED);
3957 goto out;
3958 }
3959
Andrew Walbranca808b12020-05-15 17:22:28 +01003960 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003961 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003962 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003963 share_state->fragment_count, share_state->sender_orig_mode,
3964 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003965
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003966 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003967 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003968 * Mark memory handle as not retrieved, so it can be
3969 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003970 */
J-Alves8eb19162022-04-28 10:56:48 +01003971 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003972 }
3973
3974out:
3975 share_states_unlock(&share_states);
3976 dump_share_states();
3977 return ret;
3978}
3979
3980/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003981 * Validates that the reclaim transition is allowed for the given
3982 * handle, updates the page table of the reclaiming VM, and frees the
3983 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003984 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003985struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003986 ffa_memory_handle_t handle,
3987 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003988 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003989{
3990 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003991 struct ffa_memory_share_state *share_state;
3992 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003993 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003994
3995 dump_share_states();
3996
3997 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003998
Karl Meakin4a2854a2023-06-30 16:26:52 +01003999 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00004000 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00004001 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004002 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004003 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004004 goto out;
4005 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01004006 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004007
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004008 CHECK(memory_region != NULL);
4009
J-Alvesa9cd7e32022-07-01 13:49:33 +01004010 if (vm_id_is_current_world(to_locked.vm->id) &&
4011 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004012 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004013 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004014 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004015 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004016 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004017 goto out;
4018 }
4019
Andrew Walbranca808b12020-05-15 17:22:28 +01004020 if (!share_state->sending_complete) {
4021 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004022 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004023 "reclaim.\n",
4024 handle);
4025 ret = ffa_error(FFA_INVALID_PARAMETERS);
4026 goto out;
4027 }
4028
J-Alves752236c2022-04-28 11:07:47 +01004029 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4030 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004031 struct ffa_memory_access *receiver =
4032 ffa_memory_region_get_receiver(memory_region,
4033 i);
4034
4035 assert(receiver != NULL);
4036 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004037 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004038 "Tried to reclaim memory handle %#lx that has "
4039 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004040 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004041 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004042 ret = ffa_error(FFA_DENIED);
4043 goto out;
4044 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004045 }
4046
Andrew Walbranca808b12020-05-15 17:22:28 +01004047 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004048 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004049 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004050 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004051 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004052 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004053
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004054 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004055 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004056 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004057 }
4058
4059out:
4060 share_states_unlock(&share_states);
4061 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004062}