blob: 7e8d3e1a01c702144772bea137581d91eea4d17e [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
J-Alves7b9cc432024-04-04 10:57:17 +010011#include "hf/arch/memcpy_trapped.h"
Federico Recanati4fd065d2021-12-13 20:06:23 +010012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020014#include "hf/arch/plat/ffa.h"
Karl Meakin64cadf52024-07-24 17:42:57 +010015#include "hf/arch/plat/ffa/ffa_memory.h"
Karl Meakin9724b362024-10-15 14:35:02 +010016#include "hf/arch/plat/ffa/indirect_messaging.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000017
J-Alves5952d942022-12-22 16:03:00 +000018#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000020#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010021#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010023#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010024#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010025#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010026#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000027#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000028#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000029#include "hf/panic.h"
30#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010031#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000032#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000033#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010034
J-Alves2d8457f2022-10-05 11:06:41 +010035#include "vmapi/hf/ffa_v1_0.h"
36
J-Alves5da37d92022-10-24 16:33:48 +010037#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
38
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000039/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000041 * by this lock.
42 */
43static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010044static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000045
46/**
J-Alvesed508c82023-05-04 16:09:48 +010047 * Return the offset to the first constituent within the
48 * `ffa_composite_memory_region` for the given receiver from an
49 * `ffa_memory_region`. The caller must check that the receiver_index is within
50 * bounds, and that it has a composite memory region offset.
51 */
52static uint32_t ffa_composite_constituent_offset(
53 struct ffa_memory_region *memory_region, uint32_t receiver_index)
54{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000055 struct ffa_memory_access *receiver;
56 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010057
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000058 CHECK(receiver_index < memory_region->receiver_count);
59
60 receiver =
61 ffa_memory_region_get_receiver(memory_region, receiver_index);
62 CHECK(receiver != NULL);
63
64 composite_offset = receiver->composite_memory_region_offset;
65
66 CHECK(composite_offset != 0);
67
68 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010069}
70
71/**
J-Alves917d2f22020-10-30 18:39:30 +000072 * Extracts the index from a memory handle allocated by Hafnium's current world.
73 */
74uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
75{
76 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
77}
78
79/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010080 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
81 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
82 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010083 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010084 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
85 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010086 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010087struct ffa_memory_share_state *allocate_share_state(
88 struct share_states_locked share_states, uint32_t share_func,
89 struct ffa_memory_region *memory_region, uint32_t fragment_length,
90 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000092 assert(share_states.share_states != NULL);
93 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000094
Karl Meakin52cdfe72023-06-30 14:49:10 +010095 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010096 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010097 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010098 &share_states.share_states[i];
99 struct ffa_composite_memory_region *composite =
100 ffa_memory_region_get_composite(memory_region,
101 0);
102
103 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000104 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200105 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100106 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000107 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100108 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000109 allocated_state->share_func = share_func;
110 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100111 allocated_state->fragment_count = 1;
112 allocated_state->fragments[0] = composite->constituents;
113 allocated_state->fragment_constituent_counts[0] =
114 (fragment_length -
115 ffa_composite_constituent_offset(memory_region,
116 0)) /
117 sizeof(struct ffa_memory_region_constituent);
118 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100119 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
120 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100121 allocated_state->retrieved_fragment_count[j] =
122 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000123 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100124 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000125 }
126 }
127
Karl Meakin52cdfe72023-06-30 14:49:10 +0100128 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000129}
130
131/** Locks the share states lock. */
132struct share_states_locked share_states_lock(void)
133{
134 sl_lock(&share_states_lock_instance);
135
136 return (struct share_states_locked){.share_states = share_states};
137}
138
139/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100140void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000141{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000142 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000143 share_states->share_states = NULL;
144 sl_unlock(&share_states_lock_instance);
145}
146
147/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100148 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100149 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100151struct ffa_memory_share_state *get_share_state(
152 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100154 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000155
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000156 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100157
158 /*
159 * First look for a share_state allocated by us, in which case the
160 * handle is based on the index.
161 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200162 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100163 uint64_t index = ffa_memory_handle_get_index(handle);
164
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 if (index < MAX_MEM_SHARES) {
166 share_state = &share_states.share_states[index];
167 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100168 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 }
170 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000171 }
172
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100174 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100175 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000176 if (share_state->memory_region != NULL &&
177 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100179 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100180 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 }
182
Karl Meakin4a2854a2023-06-30 16:26:52 +0100183 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000184}
185
186/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100187void share_state_free(struct share_states_locked share_states,
188 struct ffa_memory_share_state *share_state,
189 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000190{
Andrew Walbranca808b12020-05-15 17:22:28 +0100191 uint32_t i;
192
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000193 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100195 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000196 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100197 /*
198 * First fragment is part of the same page as the `memory_region`, so it
199 * doesn't need to be freed separately.
200 */
201 share_state->fragments[0] = NULL;
202 share_state->fragment_constituent_counts[0] = 0;
203 for (i = 1; i < share_state->fragment_count; ++i) {
204 mpool_free(page_pool, share_state->fragments[i]);
205 share_state->fragments[i] = NULL;
206 share_state->fragment_constituent_counts[i] = 0;
207 }
208 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100210 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000211}
212
Andrew Walbranca808b12020-05-15 17:22:28 +0100213/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100214bool share_state_sending_complete(struct share_states_locked share_states,
215 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216{
Andrew Walbranca808b12020-05-15 17:22:28 +0100217 struct ffa_composite_memory_region *composite;
218 uint32_t expected_constituent_count;
219 uint32_t fragment_constituent_count_total = 0;
220 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000221
Andrew Walbranca808b12020-05-15 17:22:28 +0100222 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000223 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100224
225 /*
226 * Share state must already be valid, or it's not possible to get hold
227 * of it.
228 */
229 CHECK(share_state->memory_region != NULL &&
230 share_state->share_func != 0);
231
232 composite =
233 ffa_memory_region_get_composite(share_state->memory_region, 0);
234 expected_constituent_count = composite->constituent_count;
235 for (i = 0; i < share_state->fragment_count; ++i) {
236 fragment_constituent_count_total +=
237 share_state->fragment_constituent_counts[i];
238 }
239 dlog_verbose(
240 "Checking completion: constituent count %d/%d from %d "
241 "fragments.\n",
242 fragment_constituent_count_total, expected_constituent_count,
243 share_state->fragment_count);
244
245 return fragment_constituent_count_total == expected_constituent_count;
246}
247
248/**
249 * Calculates the offset of the next fragment expected for the given share
250 * state.
251 */
J-Alvesfdd29272022-07-19 13:16:31 +0100252uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100253 struct share_states_locked share_states,
254 struct ffa_memory_share_state *share_state)
255{
256 uint32_t next_fragment_offset;
257 uint32_t i;
258
259 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000260 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100261
262 next_fragment_offset =
263 ffa_composite_constituent_offset(share_state->memory_region, 0);
264 for (i = 0; i < share_state->fragment_count; ++i) {
265 next_fragment_offset +=
266 share_state->fragment_constituent_counts[i] *
267 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000268 }
269
Andrew Walbranca808b12020-05-15 17:22:28 +0100270 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000271}
272
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100273static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274{
275 uint32_t i;
276
277 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
278 return;
279 }
280
Karl Meakine8937d92024-03-19 16:04:25 +0000281 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
282 "type = %s, security = %s), flags %#x, handle %#lx "
283 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100284 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000285 memory_region->sender,
286 ffa_memory_shareability_name(
287 memory_region->attributes.shareability),
288 ffa_memory_cacheability_name(
289 memory_region->attributes.cacheability),
290 ffa_memory_type_name(memory_region->attributes.type),
291 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000292 memory_region->flags, memory_region->handle, memory_region->tag,
293 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100294 memory_region->receiver_count);
295 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000296 struct ffa_memory_access *receiver =
297 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000298 if (i != 0) {
299 dlog(", ");
300 }
Karl Meakine8937d92024-03-19 16:04:25 +0000301 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000302 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000303 ffa_data_access_name(receiver->receiver_permissions
304 .permissions.data_access),
305 ffa_instruction_access_name(
306 receiver->receiver_permissions.permissions
307 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000308 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000309 /* The impdef field is only present from v1.2 and later */
310 if (ffa_version_from_memory_access_desc_size(
311 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100312 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000313 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000314 receiver->impdef.val[1]);
315 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000316 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000317 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000318}
319
J-Alves66652252022-07-06 09:49:51 +0100320void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000321{
322 uint32_t i;
323
324 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
325 return;
326 }
327
328 dlog("Current share states:\n");
329 sl_lock(&share_states_lock_instance);
330 for (i = 0; i < MAX_MEM_SHARES; ++i) {
331 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000332 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000333 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100334 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335 dlog("SHARE");
336 break;
J-Alves95fbb312024-03-20 15:19:16 +0000337 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000339 dlog("LEND");
340 break;
J-Alves95fbb312024-03-20 15:19:16 +0000341 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100342 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000343 dlog("DONATE");
344 break;
345 default:
346 dlog("invalid share_func %#x",
347 share_states[i].share_func);
348 }
Karl Meakine8937d92024-03-19 16:04:25 +0000349 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000350 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100351 if (share_states[i].sending_complete) {
352 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100354 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000355 }
J-Alves2a0d2882020-10-29 14:49:50 +0000356 dlog(" with %d fragments, %d retrieved, "
357 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100358 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000359 share_states[i].retrieved_fragment_count[0],
360 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000361 }
362 }
363 sl_unlock(&share_states_lock_instance);
364}
365
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100366static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100367 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000368{
369 uint32_t mode = 0;
370
Karl Meakin84710f32023-10-12 15:14:49 +0100371 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000373 mode = MM_MODE_R;
374 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100375 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000376 mode = MM_MODE_R | MM_MODE_W;
377 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100378 case FFA_DATA_ACCESS_NOT_SPECIFIED:
379 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
380 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 case FFA_DATA_ACCESS_RESERVED:
382 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100383 default:
384 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100385 }
386
Karl Meakin84710f32023-10-12 15:14:49 +0100387 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100388 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000389 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100390 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100391 mode |= MM_MODE_X;
392 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100393 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
394 mode |= (default_mode & MM_MODE_X);
395 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100396 case FFA_INSTRUCTION_ACCESS_RESERVED:
397 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100398 default:
399 panic("Unknown instruction access %#x\n",
400 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000401 }
402
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200403 /* Set the security state bit if necessary. */
404 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
405 mode |= plat_ffa_other_world_mode();
406 }
407
Daniel Boulby6e261362024-06-13 16:53:00 +0100408 mode |= default_mode & MM_MODE_D;
409
Andrew Walbran475c1452020-02-07 13:22:22 +0000410 return mode;
411}
412
Jose Marinho75509b42019-04-09 09:34:59 +0100413/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000414 * Get the current mode in the stage-2 page table of the given vm of all the
415 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100417 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100418static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000419 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100420 struct ffa_memory_region_constituent **fragments,
421 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100422{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100423 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100424 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100425
Andrew Walbranca808b12020-05-15 17:22:28 +0100426 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100427 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000428 * Fail if there are no constituents. Otherwise we would get an
429 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100430 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100431 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100433 }
434
Andrew Walbranca808b12020-05-15 17:22:28 +0100435 for (i = 0; i < fragment_count; ++i) {
436 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
437 ipaddr_t begin = ipa_init(fragments[i][j].address);
438 size_t size = fragments[i][j].page_count * PAGE_SIZE;
439 ipaddr_t end = ipa_add(begin, size);
440 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100441
Andrew Walbranca808b12020-05-15 17:22:28 +0100442 /* Fail if addresses are not page-aligned. */
443 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
444 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100445 dlog_verbose("%s: addresses not page-aligned\n",
446 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100447 return ffa_error(FFA_INVALID_PARAMETERS);
448 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100449
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 /*
451 * Ensure that this constituent memory range is all
452 * mapped with the same mode.
453 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800454 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100455 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000456 "%s: constituent memory range "
457 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100458 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000459 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100460 return ffa_error(FFA_DENIED);
461 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100462
Andrew Walbranca808b12020-05-15 17:22:28 +0100463 /*
464 * Ensure that all constituents are mapped with the same
465 * mode.
466 */
467 if (i == 0) {
468 *orig_mode = current_mode;
469 } else if (current_mode != *orig_mode) {
470 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100471 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000472 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100473 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100474 fragments[i][j].page_count,
475 ipa_addr(begin));
476 return ffa_error(FFA_DENIED);
477 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100478 }
Jose Marinho75509b42019-04-09 09:34:59 +0100479 }
480
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100481 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000482}
483
Karl Meakin0e617d92024-04-05 12:55:22 +0100484enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100485 uint32_t memory_access_desc_size)
486{
487 switch (memory_access_desc_size) {
488 /*
489 * v1.0 and v1.1 memory access descriptors are the same size however
490 * v1.1 is the first version to include the memory access descriptor
491 * size field so return v1.1.
492 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000493 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100494 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000495 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100496 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100497 default:
498 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100499 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100500}
501
502/**
503 * Check if the receivers size and offset given is valid for the senders
504 * FF-A version.
505 */
506static bool receiver_size_and_offset_valid_for_version(
507 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100508 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100509{
510 /*
511 * Check that the version that the memory access descriptor size belongs
512 * to is compatible with the FF-A version we believe the sender to be.
513 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100514 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100515 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100516 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100517 return false;
518 }
519
520 /*
521 * Check the receivers_offset matches the version we found from
522 * memory access descriptor size.
523 */
524 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100525 case FFA_VERSION_1_1:
526 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100527 return receivers_offset == sizeof(struct ffa_memory_region);
528 default:
529 return false;
530 }
531}
532
533/**
534 * Check the values set for fields in the memory region are valid and safe.
535 * Offset values are within safe bounds, receiver count will not cause overflows
536 * and reserved fields are 0.
537 */
538bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100539 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100540 uint32_t fragment_length,
541 bool send_transaction)
542{
543 uint32_t receiver_count;
544 struct ffa_memory_access *receiver;
545 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000546 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
547 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100548
Karl Meakin0e617d92024-04-05 12:55:22 +0100549 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100550 /* Check the reserved fields are 0. */
551 if (memory_region_v1_0->reserved_0 != 0 ||
552 memory_region_v1_0->reserved_1 != 0) {
553 dlog_verbose("Reserved fields must be 0.\n");
554 return false;
555 }
556
557 receiver_count = memory_region_v1_0->receiver_count;
558 } else {
559 uint32_t receivers_size =
560 memory_region->memory_access_desc_size;
561 uint32_t receivers_offset = memory_region->receivers_offset;
562
563 /* Check the reserved field is 0. */
564 if (memory_region->reserved[0] != 0 ||
565 memory_region->reserved[1] != 0 ||
566 memory_region->reserved[2] != 0) {
567 dlog_verbose("Reserved fields must be 0.\n");
568 return false;
569 }
570
571 /*
572 * Check memory_access_desc_size matches the size of the struct
573 * for the senders FF-A version.
574 */
575 if (!receiver_size_and_offset_valid_for_version(
576 receivers_size, receivers_offset, ffa_version)) {
577 dlog_verbose(
578 "Invalid memory access descriptor size %d, "
579 " or receiver offset %d, "
580 "for FF-A version %#x\n",
581 receivers_size, receivers_offset, ffa_version);
582 return false;
583 }
584
585 receiver_count = memory_region->receiver_count;
586 }
587
588 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000589 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100590 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000591 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100592 "specified %u\n",
593 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
594 return false;
595 }
596
597 /* Check values in the memory access descriptors. */
598 /*
599 * The composite offset values must be the same for all recievers so
600 * check the first one is valid and then they are all the same.
601 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100602 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000603 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100604 ->receivers[0]
605 : ffa_memory_region_get_receiver(memory_region, 0);
606 assert(receiver != NULL);
607 composite_offset_0 = receiver->composite_memory_region_offset;
608
609 if (!send_transaction) {
610 if (composite_offset_0 != 0) {
611 dlog_verbose(
612 "Composite offset memory region descriptor "
613 "offset must be 0 for retrieve requests. "
614 "Currently %d",
615 composite_offset_0);
616 return false;
617 }
618 } else {
619 bool comp_offset_is_zero = composite_offset_0 == 0U;
620 bool comp_offset_lt_transaction_descriptor_size =
621 composite_offset_0 <
622 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100623 (size_t)(memory_region->memory_access_desc_size *
624 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100625 bool comp_offset_with_comp_gt_fragment_length =
626 composite_offset_0 +
627 sizeof(struct ffa_composite_memory_region) >
628 fragment_length;
629 if (comp_offset_is_zero ||
630 comp_offset_lt_transaction_descriptor_size ||
631 comp_offset_with_comp_gt_fragment_length) {
632 dlog_verbose(
633 "Invalid composite memory region descriptor "
634 "offset for send transaction %u\n",
635 composite_offset_0);
636 return false;
637 }
638 }
639
Karl Meakin824b63d2024-06-03 19:04:53 +0100640 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100641 uint32_t composite_offset;
642
Karl Meakin0e617d92024-04-05 12:55:22 +0100643 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100644 struct ffa_memory_access_v1_0 *receiver_v1_0 =
645 &memory_region_v1_0->receivers[i];
646 /* Check reserved fields are 0 */
647 if (receiver_v1_0->reserved_0 != 0) {
648 dlog_verbose(
649 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000650 "descriptor must be zero. Currently "
651 "reciever %zu has a reserved field "
652 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100653 i, receiver_v1_0->reserved_0);
654 return false;
655 }
656 /*
657 * We can cast to the current version receiver as the
658 * remaining fields we are checking have the same
659 * offsets for all versions since memory access
660 * descriptors are forwards compatible.
661 */
662 receiver = (struct ffa_memory_access *)receiver_v1_0;
663 } else {
664 receiver = ffa_memory_region_get_receiver(memory_region,
665 i);
666 assert(receiver != NULL);
667
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100668 if (ffa_version == FFA_VERSION_1_1) {
669 /*
670 * Since the reserved field is at the end of the
671 * Endpoint Memory Access Descriptor we must
672 * cast to ffa_memory_access_v1_0 as they match.
673 * Since all fields except reserved in the
674 * Endpoint Memory Access Descriptor have the
675 * same offsets across all versions this cast is
676 * not required when accessing other fields in
677 * the future.
678 */
679 struct ffa_memory_access_v1_0 *receiver_v1_0 =
680 (struct ffa_memory_access_v1_0 *)
681 receiver;
682 if (receiver_v1_0->reserved_0 != 0) {
683 dlog_verbose(
684 "Reserved field in the memory "
685 "access descriptor must be "
686 "zero. Currently reciever %zu "
687 "has a reserved field with a "
688 "value of %lu\n",
689 i, receiver_v1_0->reserved_0);
690 return false;
691 }
692
693 } else {
694 if (receiver->reserved_0 != 0) {
695 dlog_verbose(
696 "Reserved field in the memory "
697 "access descriptor must be "
698 "zero. Currently reciever %zu "
699 "has a reserved field with a "
700 "value of %lu\n",
701 i, receiver->reserved_0);
702 return false;
703 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100704 }
705 }
706
707 /* Check composite offset values are equal for all receivers. */
708 composite_offset = receiver->composite_memory_region_offset;
709 if (composite_offset != composite_offset_0) {
710 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000711 "Composite offset %x differs from %x in "
712 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100713 composite_offset, composite_offset_0);
714 return false;
715 }
716 }
717 return true;
718}
719
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000720/**
J-Alves460d36c2023-10-12 17:02:15 +0100721 * If the receivers for the memory management operation are all from the
Daniel Boulby734981e2024-07-22 11:06:35 +0100722 * secure world, the memory is not device memory (as it isn't covered by the
723 * granule page table) and this isn't a FFA_MEM_SHARE, then request memory
724 * security state update by returning MAP_ACTION_CHECK_PROTECT.
J-Alves460d36c2023-10-12 17:02:15 +0100725 */
726static enum ffa_map_action ffa_mem_send_get_map_action(
727 bool all_receivers_from_current_world, ffa_id_t sender_id,
Daniel Boulby734981e2024-07-22 11:06:35 +0100728 uint32_t mem_func_id, bool is_normal_memory)
J-Alves460d36c2023-10-12 17:02:15 +0100729{
J-Alves95fbb312024-03-20 15:19:16 +0000730 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
731 mem_func_id == FFA_MEM_SHARE_64;
732 const bool protect_memory =
733 (!is_memory_share_abi && all_receivers_from_current_world &&
Daniel Boulby734981e2024-07-22 11:06:35 +0100734 ffa_is_vm_id(sender_id) && is_normal_memory);
J-Alves460d36c2023-10-12 17:02:15 +0100735
736 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
737}
738
739/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000740 * Verify that all pages have the same mode, that the starting mode
741 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100742 * to the sending VM. It outputs the mapping action that needs to be
743 * invoked for the given memory range. On memory lend/donate there
744 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000745 *
746 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100747 * 1) FFA_DENIED if a state transition was not found;
748 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100749 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100750 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100751 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100752 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
753 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000754 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100755static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100756 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000757 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100758 struct ffa_memory_region_constituent **fragments,
759 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100760 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000761{
762 const uint32_t state_mask =
763 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100764 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100765 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000766 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000767 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
768 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000769
Andrew Walbranca808b12020-05-15 17:22:28 +0100770 ret = constituents_get_mode(from, orig_from_mode, fragments,
771 fragment_constituent_counts,
772 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100773 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100774 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100775 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100776 }
777
Daniel Boulby6e261362024-06-13 16:53:00 +0100778 /*
779 * Check requested memory type is valid with the memory type of the
780 * owner. E.g. they follow the memory type precedence where Normal
781 * memory is more permissive than device and therefore device memory
782 * can only be shared as device memory.
783 */
784 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
785 (*orig_from_mode & MM_MODE_D) != 0U) {
786 dlog_verbose(
787 "Send device memory as Normal memory is not allowed\n");
788 return ffa_error(FFA_DENIED);
789 }
790
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000791 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000792 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000793 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000794 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000795 "Device memory can only be lent to a single borrower "
796 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000797 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100798 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000799 }
800
801 /*
802 * Ensure the sender is the owner and has exclusive access to the
803 * memory.
804 */
805 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100806 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100807 }
808
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100809 /*
810 * Memory cannot be zeroed during the lend/donate operation if the
811 * sender only has RO access.
812 */
813 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
814 dlog_verbose(
815 "Cannot zero memory when the sender doesn't have "
816 "write access\n");
817 return ffa_error(FFA_DENIED);
818 }
819
Daniel Boulbya76fd912024-02-22 14:22:15 +0000820 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100821
J-Alves363f5722022-04-25 17:37:37 +0100822 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000823 struct ffa_memory_access *receiver =
824 ffa_memory_region_get_receiver(memory_region, i);
825 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100826 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000827 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100828 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
829 permissions, *orig_from_mode);
830
J-Alves788b4492023-04-18 14:01:23 +0100831 /*
832 * The assumption is that at this point, the operation from
833 * SP to a receiver VM, should have returned an FFA_ERROR
834 * already.
835 */
836 if (!ffa_is_vm_id(from.vm->id)) {
837 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000838 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100839 }
840
J-Alves460d36c2023-10-12 17:02:15 +0100841 /* Track if all senders are from current world. */
842 all_receivers_from_current_world =
843 all_receivers_from_current_world &&
844 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000845 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100846
J-Alves363f5722022-04-25 17:37:37 +0100847 if ((*orig_from_mode & required_from_mode) !=
848 required_from_mode) {
849 dlog_verbose(
850 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100851 "which required mode %#x but only had %#x "
852 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100853 required_from_mode, *orig_from_mode);
854 return ffa_error(FFA_DENIED);
855 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000856 }
857
J-Alves460d36c2023-10-12 17:02:15 +0100858 *map_action = ffa_mem_send_get_map_action(
Daniel Boulby734981e2024-07-22 11:06:35 +0100859 all_receivers_from_current_world, from.vm->id, share_func,
860 (*orig_from_mode & MM_MODE_D) == 0U);
J-Alves460d36c2023-10-12 17:02:15 +0100861
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000862 /* Find the appropriate new mode. */
863 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000864 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000865 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100868 break;
J-Alves95fbb312024-03-20 15:19:16 +0000869 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100870 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100872 break;
J-Alves95fbb312024-03-20 15:19:16 +0000873 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100874 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000875 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100876 break;
877
Jose Marinho75509b42019-04-09 09:34:59 +0100878 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100880 }
881
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000883}
884
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100885static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 struct ffa_memory_region_constituent **fragments,
888 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100889 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000890{
891 const uint32_t state_mask =
892 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
893 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000895
J-Alves69cdfd92024-04-26 11:40:59 +0100896 assert(map_action != NULL);
897 if (vm_id_is_current_world(from.vm->id)) {
898 *map_action = MAP_ACTION_COMMIT;
899 } else {
900 /*
901 * No need to check the attributes of caller.
902 * The assumption is that the retrieve request of the receiver
903 * also used the MAP_ACTION_NONE, and no update was done to the
904 * page tables. When the receiver is not at the secure virtual
905 * instance SPMC doesn't manage its S2 translation (i.e. when
906 * the receiver is a VM).
907 */
908 *map_action = MAP_ACTION_NONE;
909
910 return (struct ffa_value){.func = FFA_SUCCESS_32};
911 }
912
Andrew Walbranca808b12020-05-15 17:22:28 +0100913 ret = constituents_get_mode(from, orig_from_mode, fragments,
914 fragment_constituent_counts,
915 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100916 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100917 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000918 }
919
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 /*
921 * Ensure the relinquishing VM is not the owner but has access to the
922 * memory.
923 */
924 orig_from_state = *orig_from_mode & state_mask;
925 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
926 dlog_verbose(
927 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100928 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100930 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 }
932
933 /* Find the appropriate new mode. */
934 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937}
938
939/**
940 * Verify that all pages have the same mode, that the starting mode
941 * constitutes a valid state and obtain the next mode to apply
942 * to the retrieving VM.
943 *
944 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100945 * 1) FFA_DENIED if a state transition was not found;
946 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100947 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100949 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
951 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000952 */
J-Alvesfc19b372022-07-06 12:17:35 +0100953struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000954 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100955 struct ffa_memory_region_constituent **fragments,
956 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100957 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100958 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000959{
960 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000962
Andrew Walbranca808b12020-05-15 17:22:28 +0100963 ret = constituents_get_mode(to, &orig_to_mode, fragments,
964 fragment_constituent_counts,
965 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100966 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100967 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100968 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000969 }
970
J-Alves460d36c2023-10-12 17:02:15 +0100971 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100972 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100973
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000975 /*
976 * If the original ffa memory send call has been processed
977 * successfully, it is expected the orig_to_mode would overlay
978 * with `state_mask`, as a result of the function
979 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100980 *
981 * If Hafnium is the SPMC:
982 * - Caller of the reclaim interface is an SP, the memory shall
983 * have been protected throughout the flow.
984 * - Caller of the reclaim is from the NWd, the memory may have
985 * been protected at the time of lending/donating the memory.
986 * In such case, set action to unprotect memory in the
987 * handling of reclaim operation.
988 * - If Hafnium is the hypervisor memory shall never have been
989 * protected in memory lend/share/donate.
990 *
991 * More details in the doc comment of the function
992 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000993 */
J-Alves59ed0042022-07-28 18:26:41 +0100994 if (vm_id_is_current_world(to.vm->id)) {
995 assert((orig_to_mode &
996 (MM_MODE_INVALID | MM_MODE_UNOWNED |
997 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100998 assert(!memory_protected);
999 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
1000 map_action != NULL && memory_protected) {
1001 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +01001002 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001003 } else {
J-Alves69cdfd92024-04-26 11:40:59 +01001004 if (!vm_id_is_current_world(to.vm->id)) {
1005 assert(map_action != NULL);
1006 *map_action = MAP_ACTION_NONE;
1007 return (struct ffa_value){.func = FFA_SUCCESS_32};
1008 }
1009
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001010 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001011 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001012 * Ensure the retriever has the expected state. We don't care
1013 * about the MM_MODE_SHARED bit; either with or without it set
1014 * are both valid representations of the !O-NA state.
1015 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001016 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001017 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001018 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1019 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021 }
J-Alves460d36c2023-10-12 17:02:15 +01001022
1023 /*
1024 * If memory has been protected before, clear the NS bit to
1025 * allow the secure access from the SP.
1026 */
1027 if (memory_protected) {
1028 *to_mode &= ~plat_ffa_other_world_mode();
1029 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 }
1031
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001032 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001033 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 *to_mode |= 0;
1036 break;
J-Alves95fbb312024-03-20 15:19:16 +00001037 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001038 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001039 *to_mode |= MM_MODE_UNOWNED;
1040 break;
J-Alves95fbb312024-03-20 15:19:16 +00001041 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1044 break;
1045
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001046 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001047 *to_mode |= 0;
1048 break;
1049
1050 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001051 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 }
1054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001056}
Jose Marinho09b1db82019-08-08 09:16:59 +01001057
J-Alvescf6253e2024-01-03 13:48:48 +00001058/*
1059 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1060 * Returns:
1061 * - FFA_SUCCESS_32: if all goes well.
1062 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1063 * the page table update. Or error code provided by the function
1064 * `arch_memory_protect`.
1065 */
1066static struct ffa_value ffa_region_group_check_actions(
1067 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1068 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1069 bool *memory_protected)
1070{
1071 struct ffa_value ret;
1072 bool is_memory_protected;
1073
1074 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1075 dlog_verbose(
1076 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001077 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001078 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1079 pa_addr(pa_end));
1080 return ffa_error(FFA_NO_MEMORY);
1081 }
1082
1083 switch (action) {
1084 case MAP_ACTION_CHECK:
1085 /* No protect requested. */
1086 is_memory_protected = false;
1087 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1088 break;
1089 case MAP_ACTION_CHECK_PROTECT: {
1090 paddr_t last_protected_pa = pa_init(0);
1091
1092 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1093
1094 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1095
1096 /*
1097 * - If protect memory has failed with FFA_DENIED, means some
1098 * range of memory was in the wrong state. In such case, SPM
1099 * reverts the state of the pages that were successfully
1100 * updated.
1101 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1102 * means the platform doesn't support the protection mechanism.
1103 * That said, it still permits the page table update to go
1104 * through. The variable
1105 * `is_memory_protected` will be equal to false.
1106 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1107 * break from switch and return the error.
1108 */
1109 if (ret.func == FFA_ERROR_32) {
1110 assert(!is_memory_protected);
1111 if (ffa_error_code(ret) == FFA_DENIED &&
1112 pa_addr(last_protected_pa) != (uintptr_t)0) {
1113 CHECK(arch_memory_unprotect(
1114 pa_begin,
1115 pa_add(last_protected_pa, PAGE_SIZE)));
1116 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1117 ret = (struct ffa_value){
1118 .func = FFA_SUCCESS_32,
1119 };
1120 }
1121 }
1122 } break;
1123 default:
1124 panic("%s: invalid action to process %x\n", __func__, action);
1125 }
1126
1127 if (memory_protected != NULL) {
1128 *memory_protected = is_memory_protected;
1129 }
1130
1131 return ret;
1132}
1133
1134static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1135 paddr_t pa_begin, paddr_t pa_end,
1136 struct mpool *ppool, uint32_t mode,
1137 enum ffa_map_action action)
1138{
1139 switch (action) {
1140 case MAP_ACTION_COMMIT_UNPROTECT:
1141 /*
1142 * Checking that it should succeed because SPM should be
1143 * unprotecting memory that it had protected before.
1144 */
1145 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1146 case MAP_ACTION_COMMIT:
1147 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1148 NULL);
1149 break;
1150 default:
1151 panic("%s: invalid action to process %x\n", __func__, action);
1152 }
1153}
1154
Jose Marinho09b1db82019-08-08 09:16:59 +01001155/**
J-Alves063ad832023-10-03 18:05:40 +01001156 * Helper function to revert a failed "Protect" action from the SPMC:
1157 * - `fragment_count`: should specify the number of fragments to traverse from
1158 * `fragments`. This may not be the full amount of fragments that are part of
1159 * the share_state structure.
1160 * - `fragment_constituent_counts`: array holding the amount of constituents
1161 * per fragment.
1162 * - `end`: pointer to the constituent that failed the "protect" action. It
1163 * shall be part of the last fragment, and it shall make the loop below break.
1164 */
1165static void ffa_region_group_fragments_revert_protect(
1166 struct ffa_memory_region_constituent **fragments,
1167 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1168 const struct ffa_memory_region_constituent *end)
1169{
1170 for (uint32_t i = 0; i < fragment_count; ++i) {
1171 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1172 struct ffa_memory_region_constituent *constituent =
1173 &fragments[i][j];
1174 size_t size = constituent->page_count * PAGE_SIZE;
1175 paddr_t pa_begin =
1176 pa_from_ipa(ipa_init(constituent->address));
1177 paddr_t pa_end = pa_add(pa_begin, size);
1178
Karl Meakine8937d92024-03-19 16:04:25 +00001179 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001180 __func__, pa_addr(pa_begin), size);
1181
1182 if (constituent == end) {
1183 /*
1184 * The last constituent is expected to be in the
1185 * last fragment.
1186 */
1187 assert(i == fragment_count - 1);
1188 break;
1189 }
1190
1191 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1192 }
1193 }
1194}
1195
1196/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001197 * Updates a VM's page table such that the given set of physical address ranges
1198 * are mapped in the address space at the corresponding address ranges, in the
1199 * mode provided.
1200 *
J-Alves0a83dc22023-05-05 09:50:37 +01001201 * The enum ffa_map_action determines the action taken from a call to the
1202 * function below:
1203 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1204 * mpool but no mappings will actually be updated. This function must always
1205 * be called first with action set to MAP_ACTION_CHECK to check that it will
1206 * succeed before calling ffa_region_group_identity_map with whichever one of
1207 * the remaining actions, to avoid leaving the page table in a half-updated
1208 * state.
1209 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1210 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001211 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1212 * invocation to the monitor to update the security state of the memory,
1213 * to that of the SPMC.
1214 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1215 * with a call into the monitor, to reset the security state of memory
1216 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001217 * vm_ptable_defrag should always be called after a series of page table
1218 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001219 *
J-Alvescf6253e2024-01-03 13:48:48 +00001220 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1221 * error codes:
1222 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1223 * - FFA_DENIED:
1224 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001225 * made to memory mappings.
1226 */
J-Alvescf6253e2024-01-03 13:48:48 +00001227struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001228 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 struct ffa_memory_region_constituent **fragments,
1230 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001231 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1232 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001233{
Andrew Walbranca808b12020-05-15 17:22:28 +01001234 uint32_t i;
1235 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001236 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001237
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001238 if (vm_locked.vm->el0_partition) {
1239 mode |= MM_MODE_USER | MM_MODE_NG;
1240 }
1241
Andrew Walbranca808b12020-05-15 17:22:28 +01001242 /* Iterate over the memory region constituents within each fragment. */
1243 for (i = 0; i < fragment_count; ++i) {
1244 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001245 struct ffa_memory_region_constituent *constituent =
1246 &fragments[i][j];
1247 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001249 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001250 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001251 uint32_t pa_bits =
1252 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001253
1254 /*
1255 * Ensure the requested region falls into system's PA
1256 * range.
1257 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001258 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1259 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001260 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001261 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001262 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001263
J-Alvescf6253e2024-01-03 13:48:48 +00001264 if (action <= MAP_ACTION_CHECK_PROTECT) {
1265 ret = ffa_region_group_check_actions(
1266 vm_locked, pa_begin, pa_end, ppool,
1267 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001268
1269 if (ret.func == FFA_ERROR_32 &&
1270 ffa_error_code(ret) == FFA_DENIED) {
1271 if (memory_protected != NULL) {
1272 assert(!*memory_protected);
1273 }
1274
1275 ffa_region_group_fragments_revert_protect(
1276 fragments,
1277 fragment_constituent_counts,
1278 i + 1, constituent);
1279 break;
1280 }
J-Alvescf6253e2024-01-03 13:48:48 +00001281 } else if (action >= MAP_ACTION_COMMIT &&
1282 action < MAP_ACTION_MAX) {
1283 ffa_region_group_commit_actions(
1284 vm_locked, pa_begin, pa_end, ppool,
1285 mode, action);
1286 ret = (struct ffa_value){
1287 .func = FFA_SUCCESS_32};
1288 } else {
1289 panic("%s: Unknown ffa_map_action.\n",
1290 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001291 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001292 }
1293 }
1294
J-Alvescf6253e2024-01-03 13:48:48 +00001295 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001296}
1297
1298/**
1299 * Clears a region of physical memory by overwriting it with zeros. The data is
1300 * flushed from the cache so the memory has been cleared across the system.
1301 */
J-Alves7db32002021-12-14 14:44:50 +00001302static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1303 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001304{
1305 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001306 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001307 * global mapping of the whole range. Such an approach will limit
1308 * the changes to stage-1 tables and will allow only local
1309 * invalidation.
1310 */
1311 bool ret;
1312 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001313 void *ptr = mm_identity_map(stage1_locked, begin, end,
1314 MM_MODE_W | (extra_mode_attributes &
1315 plat_ffa_other_world_mode()),
1316 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001317 size_t size = pa_difference(begin, end);
1318
1319 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001320 goto fail;
1321 }
1322
1323 memset_s(ptr, size, 0, size);
1324 arch_mm_flush_dcache(ptr, size);
1325 mm_unmap(stage1_locked, begin, end, ppool);
1326
1327 ret = true;
1328 goto out;
1329
1330fail:
1331 ret = false;
1332
1333out:
1334 mm_unlock_stage1(&stage1_locked);
1335
1336 return ret;
1337}
1338
1339/**
1340 * Clears a region of physical memory by overwriting it with zeros. The data is
1341 * flushed from the cache so the memory has been cleared across the system.
1342 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001343static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001344 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001345 struct ffa_memory_region_constituent **fragments,
1346 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1347 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001348{
1349 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001350 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001351 bool ret = false;
1352
1353 /*
1354 * Create a local pool so any freed memory can't be used by another
1355 * thread. This is to ensure each constituent that is mapped can be
1356 * unmapped again afterwards.
1357 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001358 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001359
Andrew Walbranca808b12020-05-15 17:22:28 +01001360 /* Iterate over the memory region constituents within each fragment. */
1361 for (i = 0; i < fragment_count; ++i) {
1362 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001363
J-Alves8457f932023-10-11 16:41:45 +01001364 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001365 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1366 paddr_t begin =
1367 pa_from_ipa(ipa_init(fragments[i][j].address));
1368 paddr_t end = pa_add(begin, size);
1369
J-Alves7db32002021-12-14 14:44:50 +00001370 if (!clear_memory(begin, end, &local_page_pool,
1371 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001372 /*
1373 * api_clear_memory will defrag on failure, so
1374 * no need to do it here.
1375 */
1376 goto out;
1377 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001378 }
1379 }
1380
Jose Marinho09b1db82019-08-08 09:16:59 +01001381 ret = true;
1382
1383out:
1384 mpool_fini(&local_page_pool);
1385 return ret;
1386}
1387
J-Alves5952d942022-12-22 16:03:00 +00001388static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1389 ipaddr_t in_begin, ipaddr_t in_end)
1390{
1391 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1392 ipa_addr(begin) < ipa_addr(in_end)) ||
1393 (ipa_addr(end) <= ipa_addr(in_end) &&
1394 ipa_addr(end) > ipa_addr(in_begin));
1395}
1396
1397/**
1398 * Receives a memory range and looks for overlaps with the remainder
1399 * constituents of the memory share/lend/donate operation. Assumes they are
1400 * passed in order to avoid having to loop over all the elements at each call.
1401 * The function only compares the received memory ranges with those that follow
1402 * within the same fragment, and subsequent fragments from the same operation.
1403 */
1404static bool ffa_memory_check_overlap(
1405 struct ffa_memory_region_constituent **fragments,
1406 const uint32_t *fragment_constituent_counts,
1407 const uint32_t fragment_count, const uint32_t current_fragment,
1408 const uint32_t current_constituent)
1409{
1410 uint32_t i = current_fragment;
1411 uint32_t j = current_constituent;
1412 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1413 const uint32_t current_page_count = fragments[i][j].page_count;
1414 size_t current_size = current_page_count * PAGE_SIZE;
1415 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1416
1417 if (current_size == 0 ||
1418 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001419 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1420 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001421 return false;
1422 }
1423
1424 for (; i < fragment_count; i++) {
1425 j = (i == current_fragment) ? j + 1 : 0;
1426
1427 for (; j < fragment_constituent_counts[i]; j++) {
1428 ipaddr_t begin = ipa_init(fragments[i][j].address);
1429 const uint32_t page_count = fragments[i][j].page_count;
1430 size_t size = page_count * PAGE_SIZE;
1431 ipaddr_t end = ipa_add(begin, size - 1);
1432
1433 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1434 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001435 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001436 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001437 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001438 return false;
1439 }
1440
1441 /*
1442 * Check if current ranges is within begin and end, as
1443 * well as the reverse. This should help optimize the
1444 * loop, and reduce the number of iterations.
1445 */
1446 if (is_memory_range_within(begin, end, current_begin,
1447 current_end) ||
1448 is_memory_range_within(current_begin, current_end,
1449 begin, end)) {
1450 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001451 "Overlapping memory ranges: %#lx - "
1452 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001453 ipa_addr(begin), ipa_addr(end),
1454 ipa_addr(current_begin),
1455 ipa_addr(current_end));
1456 return true;
1457 }
1458 }
1459 }
1460
1461 return false;
1462}
1463
Jose Marinho09b1db82019-08-08 09:16:59 +01001464/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001465 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001466 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001467 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001468 *
1469 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001470 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001471 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001472 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001473 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1474 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001476 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001477 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001478 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001479static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001480 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001481 struct ffa_memory_region_constituent **fragments,
1482 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001483 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001484 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1485 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001486{
Andrew Walbranca808b12020-05-15 17:22:28 +01001487 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001488 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001489 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001490 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001491 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001492 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001493 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001494 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001495 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001496 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001497
1498 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001499 * Make sure constituents are properly aligned to a 64-bit boundary. If
1500 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001501 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001502 for (i = 0; i < fragment_count; ++i) {
1503 if (!is_aligned(fragments[i], 8)) {
1504 dlog_verbose("Constituents not aligned.\n");
1505 return ffa_error(FFA_INVALID_PARAMETERS);
1506 }
J-Alves8f11cde2022-12-21 16:18:22 +00001507 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1508 constituents_total_page_count +=
1509 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001510 if (ffa_memory_check_overlap(
1511 fragments, fragment_constituent_counts,
1512 fragment_count, i, j)) {
1513 return ffa_error(FFA_INVALID_PARAMETERS);
1514 }
J-Alves8f11cde2022-12-21 16:18:22 +00001515 }
1516 }
1517
1518 if (constituents_total_page_count != composite_total_page_count) {
1519 dlog_verbose(
1520 "Composite page count differs from calculated page "
1521 "count from constituents.\n");
1522 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001523 }
1524
1525 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001526 * Check if the state transition is lawful for the sender, ensure that
1527 * all constituents of a memory region being shared are at the same
1528 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001529 */
J-Alves460d36c2023-10-12 17:02:15 +01001530 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001531 from_locked, share_func, memory_region, &orig_from_mode,
1532 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001533 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001535 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001536 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001537 }
1538
Andrew Walbran37c574e2020-06-03 11:45:46 +01001539 if (orig_from_mode_ret != NULL) {
1540 *orig_from_mode_ret = orig_from_mode;
1541 }
1542
Jose Marinho09b1db82019-08-08 09:16:59 +01001543 /*
1544 * Create a local pool so any freed memory can't be used by another
1545 * thread. This is to ensure the original mapping can be restored if the
1546 * clear fails.
1547 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001548 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001549
1550 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001551 * First reserve all required memory for the new page table entries
1552 * without committing, to make sure the entire operation will succeed
1553 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001554 * Provide the map_action as populated by 'ffa_send_check_transition'.
1555 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001556 */
J-Alvescf6253e2024-01-03 13:48:48 +00001557 ret = ffa_region_group_identity_map(
1558 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001559 fragment_count, from_mode, page_pool, map_action,
1560 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001561 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001562 goto out;
1563 }
1564
1565 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001566 * Update the mapping for the sender. This won't allocate because the
1567 * transaction was already prepared above, but may free pages in the
1568 * case that a whole block is being unmapped that was previously
1569 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001570 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001571 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001572 from_locked, fragments, fragment_constituent_counts,
1573 fragment_count, from_mode, &local_page_pool,
1574 MAP_ACTION_COMMIT, NULL)
1575 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001576
J-Alves460d36c2023-10-12 17:02:15 +01001577 /*
1578 * If memory has been protected, it is now part of the secure PAS
1579 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1580 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1581 * SPM's S1 translation.
1582 * In case memory hasn't been protected, and it is in the non-secure
1583 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1584 * perform a non-secure memory access. In such case `clean_mode` takes
1585 * the same mode as `orig_from_mode`.
1586 */
1587 clean_mode = (memory_protected != NULL && *memory_protected)
1588 ? orig_from_mode & ~plat_ffa_other_world_mode()
1589 : orig_from_mode;
1590
Jose Marinho09b1db82019-08-08 09:16:59 +01001591 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001592 if (clear && !ffa_clear_memory_constituents(
1593 clean_mode, fragments, fragment_constituent_counts,
1594 fragment_count, page_pool)) {
1595 map_action = (memory_protected != NULL && *memory_protected)
1596 ? MAP_ACTION_COMMIT_UNPROTECT
1597 : MAP_ACTION_COMMIT;
1598
Jose Marinho09b1db82019-08-08 09:16:59 +01001599 /*
1600 * On failure, roll back by returning memory to the sender. This
1601 * may allocate pages which were previously freed into
1602 * `local_page_pool` by the call above, but will never allocate
1603 * more pages than that so can never fail.
1604 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001606 from_locked, fragments,
1607 fragment_constituent_counts, fragment_count,
1608 orig_from_mode, &local_page_pool,
1609 MAP_ACTION_COMMIT, NULL)
1610 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001611 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001612 goto out;
1613 }
1614
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001616
1617out:
1618 mpool_fini(&local_page_pool);
1619
1620 /*
1621 * Tidy up the page table by reclaiming failed mappings (if there was an
1622 * error) or merging entries into blocks where possible (on success).
1623 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001624 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001625
1626 return ret;
1627}
1628
1629/**
1630 * Validates and maps memory shared from one VM to another.
1631 *
1632 * This function requires the calling context to hold the <to> lock.
1633 *
1634 * Returns:
1635 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001637 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001638 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001639 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001640 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001641 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001642struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001643 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001644 struct ffa_memory_region_constituent **fragments,
1645 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001646 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001647 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648{
Andrew Walbranca808b12020-05-15 17:22:28 +01001649 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001650 uint32_t to_mode;
1651 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001653 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001654
1655 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001656 * Make sure constituents are properly aligned to a 64-bit boundary. If
1657 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001658 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001659 for (i = 0; i < fragment_count; ++i) {
1660 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001661 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001662 return ffa_error(FFA_INVALID_PARAMETERS);
1663 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001664 }
1665
1666 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001667 * Ensure the sender has write permissions if the memory needs to be
1668 * cleared.
1669 */
1670 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1671 dlog_verbose(
1672 "Cannot zero memory when the sender does not have "
1673 "write access\n");
1674 return ffa_error(FFA_DENIED);
1675 }
1676
1677 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001678 * Check if the state transition is lawful for the recipient, and ensure
1679 * that all constituents of the memory region being retrieved are at the
1680 * same state.
1681 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 ret = ffa_retrieve_check_transition(
1683 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001684 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1685 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001686
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001687 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001688 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001689 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001690 }
1691
1692 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001693 * Create a local pool so any freed memory can't be used by
1694 * another thread. This is to ensure the original mapping can be
1695 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001696 */
1697 mpool_init_with_fallback(&local_page_pool, page_pool);
1698
1699 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001700 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1701 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001702 */
J-Alves69cdfd92024-04-26 11:40:59 +01001703 if (map_action != MAP_ACTION_NONE) {
1704 /*
1705 * First reserve all required memory for the new page table
1706 * entries in the recipient page tables without committing, to
1707 * make sure the entire operation will succeed without
1708 * exhausting the page pool.
1709 */
1710 ret = ffa_region_group_identity_map(
1711 to_locked, fragments, fragment_constituent_counts,
1712 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1713 NULL);
1714 if (ret.func == FFA_ERROR_32) {
1715 /* TODO: partial defrag of failed range. */
1716 goto out;
1717 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001718 }
1719
1720 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001721 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001722 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1723 fragment_constituent_counts,
1724 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001725 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001726 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727 goto out;
1728 }
1729
J-Alves69cdfd92024-04-26 11:40:59 +01001730 if (map_action != MAP_ACTION_NONE) {
1731 /*
1732 * Complete the transfer by mapping the memory into the
1733 * recipient. This won't allocate because the transaction was
1734 * already prepared above, so it doesn't need to use the
1735 * `local_page_pool`.
1736 */
1737 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1738 fragment_constituent_counts,
1739 fragment_count, to_mode,
1740 page_pool, map_action, NULL)
1741 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001742
J-Alves69cdfd92024-04-26 11:40:59 +01001743 /*
1744 * Return the mode used in mapping the memory in retriever's PT.
1745 */
1746 if (response_mode != NULL) {
1747 *response_mode = to_mode;
1748 }
J-Alves460d36c2023-10-12 17:02:15 +01001749 }
1750
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001751 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001752
1753out:
1754 mpool_fini(&local_page_pool);
1755
1756 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001757 * Tidy up the page table by reclaiming failed mappings (if there was an
1758 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001759 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001760 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001761
1762 return ret;
1763}
1764
Andrew Walbran996d1d12020-05-27 14:08:43 +01001765static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001766 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001767 struct ffa_memory_region_constituent **fragments,
1768 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001769 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001770{
1771 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001772 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001773 uint32_t from_mode;
1774 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001775 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001776 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001777
Andrew Walbranca808b12020-05-15 17:22:28 +01001778 ret = ffa_relinquish_check_transition(
1779 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001780 fragment_constituent_counts, fragment_count, &from_mode,
1781 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001783 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001784 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001785 }
1786
1787 /*
1788 * Create a local pool so any freed memory can't be used by another
1789 * thread. This is to ensure the original mapping can be restored if the
1790 * clear fails.
1791 */
1792 mpool_init_with_fallback(&local_page_pool, page_pool);
1793
J-Alves69cdfd92024-04-26 11:40:59 +01001794 if (map_action != MAP_ACTION_NONE) {
1795 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001796
J-Alves69cdfd92024-04-26 11:40:59 +01001797 /*
1798 * First reserve all required memory for the new page table
1799 * entries without committing, to make sure the entire operation
1800 * will succeed without exhausting the page pool.
1801 */
1802 ret = ffa_region_group_identity_map(
1803 from_locked, fragments, fragment_constituent_counts,
1804 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1805 NULL);
1806 if (ret.func == FFA_ERROR_32) {
1807 goto out;
1808 }
1809
1810 /*
1811 * Update the mapping for the sender. This won't allocate
1812 * because the transaction was already prepared above, but may
1813 * free pages in the case that a whole block is being unmapped
1814 * that was previously partially mapped.
1815 */
1816 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1817 fragment_constituent_counts,
1818 fragment_count, from_mode,
1819 &local_page_pool,
1820 MAP_ACTION_COMMIT, NULL)
1821 .func == FFA_SUCCESS_32);
1822 } else {
1823 /*
1824 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1825 * were not updated on retrieve/relinquish. These were updating
1826 * only the `share_state` structures. As such, use the sender's
1827 * original mode.
1828 */
1829 clearing_mode = sender_orig_mode;
1830 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001831
1832 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001833 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001834 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001835 fragment_constituent_counts,
1836 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001837 if (map_action != MAP_ACTION_NONE) {
1838 /*
1839 * On failure, roll back by returning memory to the
1840 * sender. This may allocate pages which were previously
1841 * freed into `local_page_pool` by the call above, but
1842 * will never allocate more pages than that so can never
1843 * fail.
1844 */
1845 CHECK(ffa_region_group_identity_map(
1846 from_locked, fragments,
1847 fragment_constituent_counts,
1848 fragment_count, orig_from_mode,
1849 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1850 .func == FFA_SUCCESS_32);
1851 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001853 goto out;
1854 }
1855
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001856 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001857
1858out:
1859 mpool_fini(&local_page_pool);
1860
1861 /*
1862 * Tidy up the page table by reclaiming failed mappings (if there was an
1863 * error) or merging entries into blocks where possible (on success).
1864 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001865 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001866
1867 return ret;
1868}
1869
1870/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001871 * Complete a memory sending operation by checking that it is valid, updating
1872 * the sender page table, and then either marking the share state as having
1873 * completed sending (on success) or freeing it (on failure).
1874 *
1875 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1876 */
J-Alvesfdd29272022-07-19 13:16:31 +01001877struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001878 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001879 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1880 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001881{
1882 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001883 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001884 struct ffa_value ret;
1885
1886 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001887 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001888 assert(memory_region != NULL);
1889 composite = ffa_memory_region_get_composite(memory_region, 0);
1890 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001891
1892 /* Check that state is valid in sender page table and update. */
1893 ret = ffa_send_check_update(
1894 from_locked, share_state->fragments,
1895 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001896 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001897 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001898 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001899 if (ret.func != FFA_SUCCESS_32) {
1900 /*
1901 * Free share state, it failed to send so it can't be retrieved.
1902 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001903 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1904 __func__, ffa_func_name(ret.func),
1905 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001906 share_state_free(share_states, share_state, page_pool);
1907 return ret;
1908 }
1909
1910 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001911 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001912
J-Alvesee68c542020-10-29 17:48:20 +00001913 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001914}
1915
1916/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001917 * Check that the memory attributes match Hafnium expectations.
1918 * Cacheability:
1919 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1920 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1921 *
1922 * Shareability:
1923 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001924 */
1925static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001926 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001927{
1928 enum ffa_memory_type memory_type;
1929 enum ffa_memory_cacheability cacheability;
1930 enum ffa_memory_shareability shareability;
1931
Karl Meakin84710f32023-10-12 15:14:49 +01001932 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001933 cacheability = attributes.cacheability;
1934 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1935 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1936 dlog_verbose(
1937 "Normal Memory: Invalid cacheability %s, "
1938 "expected %s.\n",
1939 ffa_memory_cacheability_name(cacheability),
1940 ffa_memory_cacheability_name(
1941 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001942 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001943 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001944 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1945 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1946 dlog_verbose(
1947 "Device Memory: Invalid cacheability %s, "
1948 "expected %s.\n",
1949 ffa_device_memory_cacheability_name(cacheability),
1950 ffa_device_memory_cacheability_name(
1951 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001952 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001953 }
1954
Karl Meakin84710f32023-10-12 15:14:49 +01001955 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001956 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001957 dlog_verbose("Invalid shareability %s, expected %s.\n",
1958 ffa_memory_shareability_name(shareability),
1959 ffa_memory_shareability_name(
1960 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001961 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001962 }
1963
1964 return (struct ffa_value){.func = FFA_SUCCESS_32};
1965}
1966
1967/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001968 * Check that the given `memory_region` represents a valid memory send request
1969 * of the given `share_func` type, return the clear flag and permissions via the
1970 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001971 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001972 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001973 * not.
1974 */
J-Alves66652252022-07-06 09:49:51 +01001975struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001976 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1977 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001978 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001979{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001980 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001981 struct ffa_memory_access *receiver =
1982 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001983 uint64_t receivers_end;
1984 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001985 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001986 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001987 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001988 enum ffa_data_access data_access;
1989 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001990 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001991 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001992 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001993 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001994 memory_region->receivers_offset +
1995 memory_region->memory_access_desc_size +
1996 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001997
1998 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001999 dlog_verbose("Fragment length %u too short (min %zu).\n",
2000 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002001 return ffa_error(FFA_INVALID_PARAMETERS);
2002 }
2003
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002004 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
2005 "struct ffa_memory_region_constituent must be 16 bytes");
2006 if (!is_aligned(fragment_length,
2007 sizeof(struct ffa_memory_region_constituent)) ||
2008 !is_aligned(memory_share_length,
2009 sizeof(struct ffa_memory_region_constituent))) {
2010 dlog_verbose(
2011 "Fragment length %u or total length %u"
2012 " is not 16-byte aligned.\n",
2013 fragment_length, memory_share_length);
2014 return ffa_error(FFA_INVALID_PARAMETERS);
2015 }
2016
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002017 if (fragment_length > memory_share_length) {
2018 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002019 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002020 (size_t)fragment_length, (size_t)memory_share_length);
2021 return ffa_error(FFA_INVALID_PARAMETERS);
2022 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002023
J-Alves95df0ef2022-12-07 10:09:48 +00002024 /* The sender must match the caller. */
2025 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2026 vm_id_is_current_world(memory_region->sender)) ||
2027 (vm_id_is_current_world(from_locked.vm->id) &&
2028 memory_region->sender != from_locked.vm->id)) {
2029 dlog_verbose("Invalid memory sender ID.\n");
2030 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002031 }
2032
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002033 if (memory_region->receiver_count <= 0) {
2034 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002035 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002036 }
2037
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002038 /*
2039 * Ensure that the composite header is within the memory bounds and
2040 * doesn't overlap the first part of the message. Cast to uint64_t
2041 * to prevent overflow.
2042 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002043 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002044 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002045 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002046 min_length = receivers_end +
2047 sizeof(struct ffa_composite_memory_region) +
2048 sizeof(struct ffa_memory_region_constituent);
2049 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002050 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002051 (size_t)memory_share_length, (size_t)min_length);
2052 return ffa_error(FFA_INVALID_PARAMETERS);
2053 }
2054
2055 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002056 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002057
2058 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002059 * Check that the composite memory region descriptor is after the access
2060 * descriptors, is at least 16-byte aligned, and fits in the first
2061 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002062 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002063 if ((composite_memory_region_offset < receivers_end) ||
2064 (composite_memory_region_offset % 16 != 0) ||
2065 (composite_memory_region_offset >
2066 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2067 dlog_verbose(
2068 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002069 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002070 (size_t)composite_memory_region_offset);
2071 return ffa_error(FFA_INVALID_PARAMETERS);
2072 }
2073
2074 /*
2075 * Compute the start of the constituent regions. Already checked
2076 * to be not more than fragment_length and thus not more than
2077 * memory_share_length.
2078 */
2079 constituents_start = composite_memory_region_offset +
2080 sizeof(struct ffa_composite_memory_region);
2081 constituents_length = memory_share_length - constituents_start;
2082
2083 /*
2084 * Check that the number of constituents is consistent with the length
2085 * of the constituent region.
2086 */
2087 composite = ffa_memory_region_get_composite(memory_region, 0);
2088 if ((constituents_length %
2089 sizeof(struct ffa_memory_region_constituent) !=
2090 0) ||
2091 ((constituents_length /
2092 sizeof(struct ffa_memory_region_constituent)) !=
2093 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002094 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002095 (size_t)memory_share_length,
2096 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002097 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002098 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002099 if (fragment_length < memory_share_length &&
2100 fragment_length < HF_MAILBOX_SIZE) {
2101 dlog_warning(
2102 "Initial fragment length %d smaller than mailbox "
2103 "size.\n",
2104 fragment_length);
2105 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002106
Andrew Walbrana65a1322020-04-06 19:32:32 +01002107 /*
2108 * Clear is not allowed for memory sharing, as the sender still has
2109 * access to the memory.
2110 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002111 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002112 (share_func == FFA_MEM_SHARE_32 ||
2113 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002114 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002115 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002116 }
2117
2118 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002119 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002120 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002121 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002122 }
2123
J-Alves363f5722022-04-25 17:37:37 +01002124 /* Check that the permissions are valid, for each specified receiver. */
2125 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002126 struct ffa_memory_region_attributes receiver_permissions;
2127
2128 receiver = ffa_memory_region_get_receiver(memory_region, i);
2129 assert(receiver != NULL);
2130 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002131 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002132 receiver_permissions.permissions;
2133 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002134
2135 if (memory_region->sender == receiver_id) {
2136 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002137 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002138 }
Federico Recanati85090c42021-12-15 13:17:54 +01002139
J-Alves363f5722022-04-25 17:37:37 +01002140 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2141 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002142 struct ffa_memory_access *other_receiver =
2143 ffa_memory_region_get_receiver(memory_region,
2144 j);
2145 assert(other_receiver != NULL);
2146
J-Alves363f5722022-04-25 17:37:37 +01002147 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002148 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002149 dlog_verbose(
2150 "Repeated receiver(%x) in memory send "
2151 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002152 other_receiver->receiver_permissions
2153 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002154 return ffa_error(FFA_INVALID_PARAMETERS);
2155 }
2156 }
2157
2158 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002159 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002160 dlog_verbose(
2161 "All ffa_memory_access should point to the "
2162 "same composite memory region offset.\n");
2163 return ffa_error(FFA_INVALID_PARAMETERS);
2164 }
2165
Karl Meakin84710f32023-10-12 15:14:49 +01002166 data_access = permissions.data_access;
2167 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002168 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2169 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2170 dlog_verbose(
2171 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002172 "(data_access = %s, instruction_access = %s)\n",
2173 ffa_data_access_name(data_access),
2174 ffa_instruction_access_name(
2175 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002176 return ffa_error(FFA_INVALID_PARAMETERS);
2177 }
2178 if (instruction_access !=
2179 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2180 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002181 "Invalid instruction access permissions %s "
2182 "for sending memory, expected %s.\n",
2183 ffa_instruction_access_name(instruction_access),
2184 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002185 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
J-Alves95fbb312024-03-20 15:19:16 +00002188 if (share_func == FFA_MEM_SHARE_32 ||
2189 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002190 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2191 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002192 "Invalid data access permissions %s "
2193 "for sharing memory, expected %s.\n",
2194 ffa_data_access_name(data_access),
2195 ffa_data_access_name(
2196 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002197 return ffa_error(FFA_INVALID_PARAMETERS);
2198 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002199 /*
2200 * According to section 10.10.3 of the FF-A v1.1 EAC0
2201 * spec, NX is required for share operations (but must
2202 * not be specified by the sender) so set it in the
2203 * copy that we store, ready to be returned to the
2204 * retriever.
2205 */
2206 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002207 permissions.instruction_access =
2208 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002209 receiver_permissions.permissions = permissions;
2210 }
J-Alves363f5722022-04-25 17:37:37 +01002211 }
J-Alves95fbb312024-03-20 15:19:16 +00002212 if ((share_func == FFA_MEM_LEND_32 ||
2213 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002214 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2215 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002216 "Invalid data access permissions %s for "
2217 "lending memory, expected %s.\n",
2218 ffa_data_access_name(data_access),
2219 ffa_data_access_name(
2220 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002221 return ffa_error(FFA_INVALID_PARAMETERS);
2222 }
2223
J-Alves95fbb312024-03-20 15:19:16 +00002224 if ((share_func == FFA_MEM_DONATE_32 ||
2225 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002226 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2227 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002228 "Invalid data access permissions %s for "
2229 "donating memory, expected %s.\n",
2230 ffa_data_access_name(data_access),
2231 ffa_data_access_name(
2232 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002233 return ffa_error(FFA_INVALID_PARAMETERS);
2234 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002235 }
2236
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002237 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002238 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002239 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2240 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002241 "Invalid security state %s for memory share operation, "
2242 "expected %s.\n",
2243 ffa_memory_security_name(security_state),
2244 ffa_memory_security_name(
2245 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002246 return ffa_error(FFA_INVALID_PARAMETERS);
2247 }
2248
Federico Recanatid937f5e2021-12-20 17:38:23 +01002249 /*
J-Alves807794e2022-06-16 13:42:47 +01002250 * If a memory donate or lend with single borrower, the memory type
2251 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002252 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002253 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002254 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002255 share_func == FFA_MEM_DONATE_64 ||
2256 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002257 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002258 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002259 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002260 "Invalid memory type %s for memory share "
2261 "operation, expected %s.\n",
2262 ffa_memory_type_name(type),
2263 ffa_memory_type_name(
2264 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002265 return ffa_error(FFA_INVALID_PARAMETERS);
2266 }
2267 } else {
2268 /*
2269 * Check that sender's memory attributes match Hafnium
2270 * expectations: Normal Memory, Inner shareable, Write-Back
2271 * Read-Allocate Write-Allocate Cacheable.
2272 */
2273 ret = ffa_memory_attributes_validate(memory_region->attributes);
2274 if (ret.func != FFA_SUCCESS_32) {
2275 return ret;
2276 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002277 }
2278
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002279 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002280}
2281
2282/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002283 * Gets the share state for continuing an operation to donate, lend or share
2284 * memory, and checks that it is a valid request.
2285 *
2286 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2287 * not.
2288 */
J-Alvesfdd29272022-07-19 13:16:31 +01002289struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002290 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002291 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002292 struct mpool *page_pool)
2293{
2294 struct ffa_memory_share_state *share_state;
2295 struct ffa_memory_region *memory_region;
2296
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002297 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002298
2299 /*
2300 * Look up the share state by handle and make sure that the VM ID
2301 * matches.
2302 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002303 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002304 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002305 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002306 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002307 handle);
2308 return ffa_error(FFA_INVALID_PARAMETERS);
2309 }
2310 memory_region = share_state->memory_region;
2311
J-Alvesfdd29272022-07-19 13:16:31 +01002312 if (vm_id_is_current_world(from_vm_id) &&
2313 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002314 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2315 return ffa_error(FFA_INVALID_PARAMETERS);
2316 }
2317
2318 if (share_state->sending_complete) {
2319 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002320 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002321 handle);
2322 return ffa_error(FFA_INVALID_PARAMETERS);
2323 }
2324
2325 if (share_state->fragment_count == MAX_FRAGMENTS) {
2326 /*
2327 * Log a warning as this is a sign that MAX_FRAGMENTS should
2328 * probably be increased.
2329 */
2330 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002331 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002332 "only %d supported.\n",
2333 handle, MAX_FRAGMENTS);
2334 /* Free share state, as it's not possible to complete it. */
2335 share_state_free(share_states, share_state, page_pool);
2336 return ffa_error(FFA_NO_MEMORY);
2337 }
2338
2339 *share_state_ret = share_state;
2340
2341 return (struct ffa_value){.func = FFA_SUCCESS_32};
2342}
2343
2344/**
J-Alves95df0ef2022-12-07 10:09:48 +00002345 * Checks if there is at least one receiver from the other world.
2346 */
J-Alvesfdd29272022-07-19 13:16:31 +01002347bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002348 struct ffa_memory_region *memory_region)
2349{
2350 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002351 struct ffa_memory_access *receiver =
2352 ffa_memory_region_get_receiver(memory_region, i);
2353 assert(receiver != NULL);
2354 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2355
2356 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002357 return true;
2358 }
2359 }
2360 return false;
2361}
2362
2363/**
J-Alves9da280b2022-12-21 14:55:39 +00002364 * Validates a call to donate, lend or share memory in which Hafnium is the
2365 * designated allocator of the memory handle. In practice, this also means
2366 * Hafnium is responsible for managing the state structures for the transaction.
2367 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2368 * sender is an SP or there is at least one borrower that is an SP.
2369 * If Hafnium is the hypervisor, it should allocate the memory handle when
2370 * operation involves only NWd VMs.
2371 *
2372 * If validation goes well, Hafnium updates the stage-2 page tables of the
2373 * sender. Validation consists of checking if the message length and number of
2374 * memory region constituents match, and if the transition is valid for the
2375 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002376 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002377 * Assumes that the caller has already found and locked the sender VM and copied
2378 * the memory region descriptor from the sender's TX buffer to a freshly
2379 * allocated page from Hafnium's internal pool. The caller must have also
2380 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002381 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002382 * This function takes ownership of the `memory_region` passed in and will free
2383 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002384 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002385struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002386 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002387 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002388 uint32_t fragment_length, uint32_t share_func,
2389 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002390{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002391 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002392 struct share_states_locked share_states;
2393 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002394
2395 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002396 * If there is an error validating the `memory_region` then we need to
2397 * free it because we own it but we won't be storing it in a share state
2398 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002399 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002400 ret = ffa_memory_send_validate(from_locked, memory_region,
2401 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002402 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002403 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002404 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002405 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002406 }
2407
Andrew Walbrana65a1322020-04-06 19:32:32 +01002408 /* Set flag for share function, ready to be retrieved later. */
2409 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002410 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002411 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002412 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002413 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002414 break;
J-Alves95fbb312024-03-20 15:19:16 +00002415 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 case FFA_MEM_LEND_32:
2417 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002418 break;
J-Alves95fbb312024-03-20 15:19:16 +00002419 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002420 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002421 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002422 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002423 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002424 default:
2425 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2426 ffa_func_name(share_func));
2427 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002428 }
2429
Andrew Walbranca808b12020-05-15 17:22:28 +01002430 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002431 /*
2432 * Allocate a share state before updating the page table. Otherwise if
2433 * updating the page table succeeded but allocating the share state
2434 * failed then it would leave the memory in a state where nobody could
2435 * get it back.
2436 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002437 share_state = allocate_share_state(share_states, share_func,
2438 memory_region, fragment_length,
2439 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002440 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002441 dlog_verbose("Failed to allocate share state.\n");
2442 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002443 ret = ffa_error(FFA_NO_MEMORY);
2444 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002445 }
2446
Andrew Walbranca808b12020-05-15 17:22:28 +01002447 if (fragment_length == memory_share_length) {
2448 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002449 ret = ffa_memory_send_complete(
2450 from_locked, share_states, share_state, page_pool,
2451 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002452 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002453 /*
2454 * Use sender ID from 'memory_region' assuming
2455 * that at this point it has been validated:
2456 * - MBZ at virtual FF-A instance.
2457 */
J-Alves19e20cf2023-08-02 12:48:55 +01002458 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002459 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2460 ? memory_region->sender
2461 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002462 ret = (struct ffa_value){
2463 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002464 .arg1 = (uint32_t)memory_region->handle,
2465 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002466 .arg3 = fragment_length,
2467 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002468 }
2469
2470out:
2471 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002472 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002473 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002474}
2475
2476/**
J-Alves8505a8a2022-06-15 18:10:18 +01002477 * Continues an operation to donate, lend or share memory to a VM from current
2478 * world. If this is the last fragment then checks that the transition is valid
2479 * for the type of memory sending operation and updates the stage-2 page tables
2480 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002481 *
2482 * Assumes that the caller has already found and locked the sender VM and copied
2483 * the memory region descriptor from the sender's TX buffer to a freshly
2484 * allocated page from Hafnium's internal pool.
2485 *
2486 * This function takes ownership of the `fragment` passed in; it must not be
2487 * freed by the caller.
2488 */
2489struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2490 void *fragment,
2491 uint32_t fragment_length,
2492 ffa_memory_handle_t handle,
2493 struct mpool *page_pool)
2494{
2495 struct share_states_locked share_states = share_states_lock();
2496 struct ffa_memory_share_state *share_state;
2497 struct ffa_value ret;
2498 struct ffa_memory_region *memory_region;
2499
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002500 CHECK(is_aligned(fragment,
2501 alignof(struct ffa_memory_region_constituent)));
2502 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2503 0) {
2504 dlog_verbose("Fragment length %u misaligned.\n",
2505 fragment_length);
2506 ret = ffa_error(FFA_INVALID_PARAMETERS);
2507 goto out_free_fragment;
2508 }
2509
Andrew Walbranca808b12020-05-15 17:22:28 +01002510 ret = ffa_memory_send_continue_validate(share_states, handle,
2511 &share_state,
2512 from_locked.vm->id, page_pool);
2513 if (ret.func != FFA_SUCCESS_32) {
2514 goto out_free_fragment;
2515 }
2516 memory_region = share_state->memory_region;
2517
J-Alves95df0ef2022-12-07 10:09:48 +00002518 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002519 dlog_error(
2520 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002521 "other world. This should never happen, and indicates "
2522 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002523 "EL3 code.\n");
2524 ret = ffa_error(FFA_INVALID_PARAMETERS);
2525 goto out_free_fragment;
2526 }
2527
2528 /* Add this fragment. */
2529 share_state->fragments[share_state->fragment_count] = fragment;
2530 share_state->fragment_constituent_counts[share_state->fragment_count] =
2531 fragment_length / sizeof(struct ffa_memory_region_constituent);
2532 share_state->fragment_count++;
2533
2534 /* Check whether the memory send operation is now ready to complete. */
2535 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002536 ret = ffa_memory_send_complete(
2537 from_locked, share_states, share_state, page_pool,
2538 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002539 } else {
2540 ret = (struct ffa_value){
2541 .func = FFA_MEM_FRAG_RX_32,
2542 .arg1 = (uint32_t)handle,
2543 .arg2 = (uint32_t)(handle >> 32),
2544 .arg3 = share_state_next_fragment_offset(share_states,
2545 share_state)};
2546 }
2547 goto out;
2548
2549out_free_fragment:
2550 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002551
2552out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002553 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002554 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555}
2556
Andrew Walbranca808b12020-05-15 17:22:28 +01002557/** Clean up after the receiver has finished retrieving a memory region. */
2558static void ffa_memory_retrieve_complete(
2559 struct share_states_locked share_states,
2560 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2561{
J-Alves95fbb312024-03-20 15:19:16 +00002562 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2563 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002564 /*
2565 * Memory that has been donated can't be relinquished,
2566 * so no need to keep the share state around.
2567 */
2568 share_state_free(share_states, share_state, page_pool);
2569 dlog_verbose("Freed share state for donate.\n");
2570 }
2571}
2572
J-Alves2d8457f2022-10-05 11:06:41 +01002573/**
2574 * Initialises the given memory region descriptor to be used for an
2575 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2576 * fragment.
2577 * The memory region descriptor is initialized according to retriever's
2578 * FF-A version.
2579 *
2580 * Returns true on success, or false if the given constituents won't all fit in
2581 * the first fragment.
2582 */
2583static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002584 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002585 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002586 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002587 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002588 struct ffa_memory_access *receivers, size_t receiver_count,
2589 uint32_t memory_access_desc_size, uint32_t page_count,
2590 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002591 const struct ffa_memory_region_constituent constituents[],
2592 uint32_t fragment_constituent_count, uint32_t *total_length,
2593 uint32_t *fragment_length)
2594{
2595 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002596 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002597 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002598 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002599
2600 assert(response != NULL);
2601
Karl Meakin0e617d92024-04-05 12:55:22 +01002602 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002603 struct ffa_memory_region_v1_0 *retrieve_response =
2604 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002605 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002606
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002607 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2608 attributes, flags, handle, 0,
2609 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002610
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002611 receiver = (struct ffa_memory_access_v1_0 *)
2612 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002613 receiver_count = retrieve_response->receiver_count;
2614
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002615 for (uint32_t i = 0; i < receiver_count; i++) {
2616 ffa_id_t receiver_id =
2617 receivers[i].receiver_permissions.receiver;
2618 ffa_memory_receiver_flags_t recv_flags =
2619 receivers[i].receiver_permissions.flags;
2620
2621 /*
2622 * Initialized here as in memory retrieve responses we
2623 * currently expect one borrower to be specified.
2624 */
2625 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002626 receiver, receiver_id, permissions.data_access,
2627 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002628 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002629
2630 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002631 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002632 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2633 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002634
2635 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2636 retrieve_response, 0);
2637 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002638 struct ffa_memory_region *retrieve_response =
2639 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002640 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002641
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002642 ffa_memory_region_init_header(
2643 retrieve_response, sender, attributes, flags, handle, 0,
2644 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002645
2646 /*
2647 * Note that `sizeof(struct_ffa_memory_region)` and
2648 * `sizeof(struct ffa_memory_access)` must both be multiples of
2649 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2650 * guaranteed that the offset we calculate here is aligned to a
2651 * 64-bit boundary and so 64-bit values can be copied without
2652 * alignment faults.
2653 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002654 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002655 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002656 (uint32_t)(receiver_count *
2657 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002658
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002659 retrieve_response_receivers =
2660 ffa_memory_region_get_receiver(retrieve_response, 0);
2661 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002662
2663 /*
2664 * Initialized here as in memory retrieve responses we currently
2665 * expect one borrower to be specified.
2666 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002667 memcpy_s(retrieve_response_receivers,
2668 sizeof(struct ffa_memory_access) * receiver_count,
2669 receivers,
2670 sizeof(struct ffa_memory_access) * receiver_count);
2671
2672 retrieve_response_receivers->composite_memory_region_offset =
2673 composite_offset;
2674
J-Alves2d8457f2022-10-05 11:06:41 +01002675 composite_memory_region =
2676 ffa_memory_region_get_composite(retrieve_response, 0);
2677 }
2678
J-Alves2d8457f2022-10-05 11:06:41 +01002679 assert(composite_memory_region != NULL);
2680
J-Alves2d8457f2022-10-05 11:06:41 +01002681 composite_memory_region->page_count = page_count;
2682 composite_memory_region->constituent_count = total_constituent_count;
2683 composite_memory_region->reserved_0 = 0;
2684
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002685 constituents_offset =
2686 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002687 if (constituents_offset +
2688 fragment_constituent_count *
2689 sizeof(struct ffa_memory_region_constituent) >
2690 response_max_size) {
2691 return false;
2692 }
2693
2694 for (i = 0; i < fragment_constituent_count; ++i) {
2695 composite_memory_region->constituents[i] = constituents[i];
2696 }
2697
2698 if (total_length != NULL) {
2699 *total_length =
2700 constituents_offset +
2701 composite_memory_region->constituent_count *
2702 sizeof(struct ffa_memory_region_constituent);
2703 }
2704 if (fragment_length != NULL) {
2705 *fragment_length =
2706 constituents_offset +
2707 fragment_constituent_count *
2708 sizeof(struct ffa_memory_region_constituent);
2709 }
2710
2711 return true;
2712}
2713
J-Alves96de29f2022-04-26 16:05:24 +01002714/**
2715 * Validates the retrieved permissions against those specified by the lender
2716 * of memory share operation. Optionally can help set the permissions to be used
2717 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002718 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2719 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2720 * specification for each ABI.
2721 * - FFA_DENIED -> if the permissions specified by the retriever are not
2722 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002723 */
J-Alvesdcad8992023-09-15 14:10:35 +01002724static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2725 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002726 enum ffa_data_access requested_data_access,
2727 enum ffa_instruction_access sent_instruction_access,
2728 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002729 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002730{
2731 switch (sent_data_access) {
2732 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2733 case FFA_DATA_ACCESS_RW:
2734 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2735 requested_data_access == FFA_DATA_ACCESS_RW) {
2736 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002737 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002738 }
2739 break;
2740 }
2741 /* Intentional fall-through. */
2742 case FFA_DATA_ACCESS_RO:
2743 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2744 requested_data_access == FFA_DATA_ACCESS_RO) {
2745 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002746 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002747 }
2748 break;
2749 }
2750 dlog_verbose(
2751 "Invalid data access requested; sender specified "
2752 "permissions %#x but receiver requested %#x.\n",
2753 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002754 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002755 case FFA_DATA_ACCESS_RESERVED:
2756 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2757 "checked before this point.");
2758 }
2759
J-Alvesdcad8992023-09-15 14:10:35 +01002760 /*
2761 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2762 * or FFA_MEMORY_DONATE the retriever should have specifed the
2763 * instruction permissions it wishes to receive.
2764 */
2765 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002766 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002767 case FFA_MEM_SHARE_32:
2768 if (requested_instruction_access !=
2769 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2770 dlog_verbose(
2771 "%s: for share instruction permissions must "
2772 "NOT be specified.\n",
2773 __func__);
2774 return ffa_error(FFA_INVALID_PARAMETERS);
2775 }
2776 break;
J-Alves95fbb312024-03-20 15:19:16 +00002777 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002778 case FFA_MEM_LEND_32:
2779 /*
2780 * For operations with multiple borrowers only permit XN
2781 * permissions, and both Sender and borrower should have used
2782 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2783 */
2784 if (multiple_borrowers) {
2785 if (requested_instruction_access !=
2786 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2787 dlog_verbose(
2788 "%s: lend/share/donate with multiple "
2789 "borrowers "
2790 "instruction permissions must NOT be "
2791 "specified.\n",
2792 __func__);
2793 return ffa_error(FFA_INVALID_PARAMETERS);
2794 }
2795 break;
2796 }
2797 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002798 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002799 case FFA_MEM_DONATE_32:
2800 if (!multiple_borrowers &&
2801 requested_instruction_access ==
2802 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2803 dlog_verbose(
2804 "%s: for lend/donate with single borrower "
2805 "instruction permissions must be speficified "
2806 "by borrower\n",
2807 __func__);
2808 return ffa_error(FFA_INVALID_PARAMETERS);
2809 }
2810 break;
2811 default:
2812 panic("%s: Wrong func id provided.\n", __func__);
2813 }
2814
J-Alves96de29f2022-04-26 16:05:24 +01002815 switch (sent_instruction_access) {
2816 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2817 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002818 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002819 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002820 permissions->instruction_access =
2821 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002822 }
2823 break;
2824 }
J-Alvesdcad8992023-09-15 14:10:35 +01002825 /*
2826 * Fall through if requested permissions are less
2827 * permissive than those provided by the sender.
2828 */
J-Alves96de29f2022-04-26 16:05:24 +01002829 case FFA_INSTRUCTION_ACCESS_NX:
2830 if (requested_instruction_access ==
2831 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2832 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2833 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002834 permissions->instruction_access =
2835 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002836 }
2837 break;
2838 }
2839 dlog_verbose(
2840 "Invalid instruction access requested; sender "
2841 "specified permissions %#x but receiver requested "
2842 "%#x.\n",
2843 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002844 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002845 case FFA_INSTRUCTION_ACCESS_RESERVED:
2846 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2847 "be checked before this point.");
2848 }
2849
J-Alvesdcad8992023-09-15 14:10:35 +01002850 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002851}
2852
2853/**
2854 * Validate the receivers' permissions in the retrieve request against those
2855 * specified by the lender.
2856 * In the `permissions` argument returns the permissions to set at S2 for the
2857 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002858 * The function looks into the flag to bypass multiple borrower checks:
2859 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2860 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2861 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2862 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002863 */
2864static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2865 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002866 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002867 ffa_memory_access_permissions_t *permissions,
2868 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002869{
2870 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002871 bool bypass_multi_receiver_check =
2872 (retrieve_request->flags &
2873 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002874 const uint32_t region_receiver_count = memory_region->receiver_count;
2875 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002876
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002877 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002878 assert(permissions != NULL);
2879
Karl Meakin84710f32023-10-12 15:14:49 +01002880 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002881
J-Alves3456e032023-07-20 12:20:05 +01002882 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002883 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002884 dlog_verbose(
2885 "Retrieve request should contain same list of "
2886 "borrowers, as specified by the lender.\n");
2887 return ffa_error(FFA_INVALID_PARAMETERS);
2888 }
2889 } else {
2890 if (retrieve_request->receiver_count != 1) {
2891 dlog_verbose(
2892 "Set bypass multiple borrower check, receiver "
2893 "list must be sized 1 (%x)\n",
2894 memory_region->receiver_count);
2895 return ffa_error(FFA_INVALID_PARAMETERS);
2896 }
J-Alves96de29f2022-04-26 16:05:24 +01002897 }
2898
2899 retrieve_receiver_index = retrieve_request->receiver_count;
2900
J-Alves96de29f2022-04-26 16:05:24 +01002901 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2902 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002903 struct ffa_memory_access *retrieve_request_receiver =
2904 ffa_memory_region_get_receiver(retrieve_request, i);
2905 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002906 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002907 retrieve_request_receiver->receiver_permissions
2908 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002909 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002910 retrieve_request_receiver->receiver_permissions
2911 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002912 struct ffa_memory_access *receiver;
2913 uint32_t mem_region_receiver_index;
2914 bool permissions_RO;
2915 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002916 /*
2917 * If the call is at the virtual FF-A instance the caller's
2918 * ID must match an entry in the memory access list.
2919 * In the SPMC, one of the specified receivers could be from
2920 * the NWd.
2921 */
2922 bool found_to_id = vm_id_is_current_world(to_vm_id)
2923 ? (current_receiver_id == to_vm_id)
2924 : (!vm_id_is_current_world(
2925 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002926
J-Alves3456e032023-07-20 12:20:05 +01002927 if (bypass_multi_receiver_check && !found_to_id) {
2928 dlog_verbose(
2929 "Bypass multiple borrower check for id %x.\n",
2930 current_receiver_id);
2931 continue;
2932 }
2933
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002934 if (retrieve_request_receiver->composite_memory_region_offset !=
2935 0U) {
2936 dlog_verbose(
2937 "Retriever specified address ranges not "
2938 "supported (got offset %d).\n",
2939 retrieve_request_receiver
2940 ->composite_memory_region_offset);
2941 return ffa_error(FFA_INVALID_PARAMETERS);
2942 }
2943
J-Alves96de29f2022-04-26 16:05:24 +01002944 /*
2945 * Find the current receiver in the transaction descriptor from
2946 * sender.
2947 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002948 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002949 ffa_memory_region_get_receiver_index(
2950 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002951
2952 if (mem_region_receiver_index ==
2953 memory_region->receiver_count) {
2954 dlog_verbose("%s: receiver %x not found\n", __func__,
2955 current_receiver_id);
2956 return ffa_error(FFA_DENIED);
2957 }
2958
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002959 receiver = ffa_memory_region_get_receiver(
2960 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002961 assert(receiver != NULL);
2962
2963 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002964
2965 if (found_to_id) {
2966 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002967
2968 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002969 }
2970
2971 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002972 * Check if retrieve request memory access list is valid:
2973 * - The retrieve request complies with the specification.
2974 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002975 */
J-Alvesdcad8992023-09-15 14:10:35 +01002976 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002977 func_id, sent_permissions.data_access,
2978 requested_permissions.data_access,
2979 sent_permissions.instruction_access,
2980 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002981 found_to_id ? permissions : NULL,
2982 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002983
J-Alvesdcad8992023-09-15 14:10:35 +01002984 if (ret.func != FFA_SUCCESS_32) {
2985 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002986 }
2987
Karl Meakin84710f32023-10-12 15:14:49 +01002988 permissions_RO =
2989 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002990 clear_memory_flags =
2991 (retrieve_request->flags &
2992 (FFA_MEMORY_REGION_FLAG_CLEAR |
2993 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002994
J-Alves96de29f2022-04-26 16:05:24 +01002995 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002996 * Can't request PM to clear memory if only provided
2997 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002998 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002999 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01003000 dlog_verbose(
3001 "Receiver has RO permissions can not request "
3002 "clear.\n");
3003 return ffa_error(FFA_DENIED);
3004 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003005
3006 /*
3007 * Check the impdef in the retrieve_request matches the value in
3008 * the original memory send.
3009 */
3010 if (ffa_version_from_memory_access_desc_size(
3011 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003012 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003013 ffa_version_from_memory_access_desc_size(
3014 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003015 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003016 if (receiver->impdef.val[0] !=
3017 retrieve_request_receiver->impdef.val[0] ||
3018 receiver->impdef.val[1] !=
3019 retrieve_request_receiver->impdef.val[1]) {
3020 dlog_verbose(
3021 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003022 "match retrieve request value send "
3023 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003024 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003025 receiver->impdef.val[0],
3026 receiver->impdef.val[1],
3027 retrieve_request_receiver->impdef
3028 .val[0],
3029 retrieve_request_receiver->impdef
3030 .val[1]);
3031 return ffa_error(FFA_INVALID_PARAMETERS);
3032 }
3033 }
J-Alves96de29f2022-04-26 16:05:24 +01003034 }
3035
3036 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3037 dlog_verbose(
3038 "Retrieve request does not contain caller's (%x) "
3039 "permissions\n",
3040 to_vm_id);
3041 return ffa_error(FFA_INVALID_PARAMETERS);
3042 }
3043
3044 return (struct ffa_value){.func = FFA_SUCCESS_32};
3045}
3046
Daniel Boulby296ee702023-11-28 13:36:55 +00003047/**
3048 * According to section 17.4.3 of the FF-A v1.2 ALP0 specification, the
3049 * hypervisor may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region
3050 * description of a pending memory sharing operation whose allocator is the SPM,
3051 * for validation purposes before forwarding an FFA_MEM_RECLAIM call. For a
3052 * hypervisor retrieve request the endpoint memory access descriptor count must
3053 * be 0 (for any other retrieve request it must be >= 1).
J-Alvesa9cd7e32022-07-01 13:49:33 +01003054 */
Daniel Boulby296ee702023-11-28 13:36:55 +00003055bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003056{
Daniel Boulby296ee702023-11-28 13:36:55 +00003057 return request->receiver_count == 0U;
3058}
3059
J-Alvesa9cd7e32022-07-01 13:49:33 +01003060/*
3061 * Helper to reset count of fragments retrieved by the hypervisor.
3062 */
3063static void ffa_memory_retrieve_complete_from_hyp(
3064 struct ffa_memory_share_state *share_state)
3065{
3066 if (share_state->hypervisor_fragment_count ==
3067 share_state->fragment_count) {
3068 share_state->hypervisor_fragment_count = 0;
3069 }
3070}
3071
J-Alves089004f2022-07-13 14:25:44 +01003072/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003073 * Prepares the return of the ffa_value for the memory retrieve response.
3074 */
3075static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3076 uint32_t fragment_length)
3077{
3078 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3079 .arg1 = total_length,
3080 .arg2 = fragment_length};
3081}
3082
3083/**
J-Alves089004f2022-07-13 14:25:44 +01003084 * Validate that the memory region descriptor provided by the borrower on
3085 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3086 * memory sharing call.
3087 */
3088static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003089 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3090 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003091 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3092 uint32_t share_func)
3093{
3094 ffa_memory_region_flags_t transaction_type =
3095 retrieve_request->flags &
3096 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003097 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003098 const uint64_t memory_access_desc_size =
3099 retrieve_request->memory_access_desc_size;
3100 const uint32_t expected_retrieve_request_length =
3101 retrieve_request->receivers_offset +
3102 (uint32_t)(retrieve_request->receiver_count *
3103 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003104
3105 assert(retrieve_request != NULL);
3106 assert(memory_region != NULL);
3107 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003108
J-Alves4f0d9c12024-01-17 17:23:11 +00003109 if (retrieve_request_length != expected_retrieve_request_length) {
3110 dlog_verbose(
3111 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3112 "but was %d.\n",
3113 expected_retrieve_request_length,
3114 retrieve_request_length);
3115 return ffa_error(FFA_INVALID_PARAMETERS);
3116 }
3117
3118 if (retrieve_request->sender != memory_region->sender) {
3119 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003120 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003121 "retrieve.\n",
3122 memory_region->handle);
3123 return ffa_error(FFA_DENIED);
3124 }
3125
3126 /*
3127 * The SPMC can only process retrieve requests to memory share
3128 * operations with one borrower from the other world. It can't
3129 * determine the ID of the NWd VM that invoked the retrieve
3130 * request interface call. It relies on the hypervisor to
3131 * validate the caller's ID against that provided in the
3132 * `receivers` list of the retrieve response.
3133 * In case there is only one borrower from the NWd in the
3134 * transaction descriptor, record that in the `receiver_id` for
3135 * later use, and validate in the retrieve request message.
3136 * This limitation is due to the fact SPMC can't determine the
3137 * index in the memory share structures state to update.
3138 */
3139 if (to_id == HF_HYPERVISOR_VM_ID) {
3140 uint32_t other_world_count = 0;
3141
3142 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3143 struct ffa_memory_access *receiver =
3144 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003145 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003146 assert(receiver != NULL);
3147
J-Alvesf220d572024-04-24 22:15:14 +01003148 if (!vm_id_is_current_world(
3149 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003150 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003151 /* Set it to be used later. */
3152 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003153 }
3154 }
3155
3156 if (other_world_count > 1) {
3157 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003158 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003159 return ffa_error(FFA_NOT_SUPPORTED);
3160 }
3161 }
J-Alves089004f2022-07-13 14:25:44 +01003162 /*
3163 * Check that the transaction type expected by the receiver is
3164 * correct, if it has been specified.
3165 */
3166 if (transaction_type !=
3167 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3168 transaction_type != (memory_region->flags &
3169 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3170 dlog_verbose(
3171 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003172 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003173 transaction_type,
3174 memory_region->flags &
3175 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3176 retrieve_request->handle);
3177 return ffa_error(FFA_INVALID_PARAMETERS);
3178 }
3179
3180 if (retrieve_request->tag != memory_region->tag) {
3181 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003182 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3183 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003184 retrieve_request->tag, memory_region->tag,
3185 retrieve_request->handle);
3186 return ffa_error(FFA_INVALID_PARAMETERS);
3187 }
3188
J-Alves4f0d9c12024-01-17 17:23:11 +00003189 *receiver_index =
3190 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003191
3192 if (*receiver_index == memory_region->receiver_count) {
3193 dlog_verbose(
3194 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003195 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003196 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003197 return ffa_error(FFA_INVALID_PARAMETERS);
3198 }
3199
3200 if ((retrieve_request->flags &
3201 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3202 dlog_verbose(
3203 "Retriever specified 'address range alignment 'hint' "
3204 "not supported.\n");
3205 return ffa_error(FFA_INVALID_PARAMETERS);
3206 }
3207 if ((retrieve_request->flags &
3208 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3209 dlog_verbose(
3210 "Bits 8-5 must be zero in memory region's flags "
3211 "(address range alignment hint not supported).\n");
3212 return ffa_error(FFA_INVALID_PARAMETERS);
3213 }
3214
3215 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3216 dlog_verbose(
3217 "Bits 31-10 must be zero in memory region's flags.\n");
3218 return ffa_error(FFA_INVALID_PARAMETERS);
3219 }
3220
J-Alves95fbb312024-03-20 15:19:16 +00003221 if ((share_func == FFA_MEM_SHARE_32 ||
3222 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003223 (retrieve_request->flags &
3224 (FFA_MEMORY_REGION_FLAG_CLEAR |
3225 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3226 dlog_verbose(
3227 "Memory Share operation can't clean after relinquish "
3228 "memory region.\n");
3229 return ffa_error(FFA_INVALID_PARAMETERS);
3230 }
3231
3232 /*
3233 * If the borrower needs the memory to be cleared before mapping
3234 * to its address space, the sender should have set the flag
3235 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3236 * FFA_DENIED.
3237 */
3238 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3239 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3240 dlog_verbose(
3241 "Borrower needs memory cleared. Sender needs to set "
3242 "flag for clearing memory.\n");
3243 return ffa_error(FFA_DENIED);
3244 }
3245
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003246 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003247 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003248 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3249 dlog_verbose(
3250 "Invalid security state for memory retrieve request "
3251 "operation.\n");
3252 return ffa_error(FFA_INVALID_PARAMETERS);
3253 }
3254
J-Alves089004f2022-07-13 14:25:44 +01003255 /*
3256 * If memory type is not specified, bypass validation of memory
3257 * attributes in the retrieve request. The retriever is expecting to
3258 * obtain this information from the SPMC.
3259 */
Karl Meakin84710f32023-10-12 15:14:49 +01003260 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003261 return (struct ffa_value){.func = FFA_SUCCESS_32};
3262 }
3263
3264 /*
3265 * Ensure receiver's attributes are compatible with how
3266 * Hafnium maps memory: Normal Memory, Inner shareable,
3267 * Write-Back Read-Allocate Write-Allocate Cacheable.
3268 */
3269 return ffa_memory_attributes_validate(retrieve_request->attributes);
3270}
3271
J-Alves3f6527c2024-04-25 17:10:57 +01003272/**
3273 * Whilst processing the retrieve request, the operation could be aborted, and
3274 * changes to page tables and the share state structures need to be reverted.
3275 */
3276static void ffa_partition_memory_retrieve_request_undo(
3277 struct vm_locked from_locked,
3278 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3279{
3280 /*
3281 * Currently this operation is expected for operations involving the
3282 * 'other_world' vm.
3283 */
3284 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3285 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3286
3287 /* Decrement the retrieved fragment count for the given receiver. */
3288 share_state->retrieved_fragment_count[receiver_index]--;
3289}
3290
3291/**
3292 * Whilst processing an hypervisor retrieve request the operation could be
3293 * aborted. There were no updates to PTs in this case, so decrementing the
3294 * fragment count retrieved by the hypervisor should be enough.
3295 */
3296static void ffa_hypervisor_memory_retrieve_request_undo(
3297 struct ffa_memory_share_state *share_state)
3298{
3299 assert(share_state->hypervisor_fragment_count > 0);
3300 share_state->hypervisor_fragment_count--;
3301}
3302
J-Alves4f0d9c12024-01-17 17:23:11 +00003303static struct ffa_value ffa_partition_retrieve_request(
3304 struct share_states_locked share_states,
3305 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3306 struct ffa_memory_region *retrieve_request,
3307 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003308{
Karl Meakin84710f32023-10-12 15:14:49 +01003309 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003310 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003311 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003312 struct ffa_composite_memory_region *composite;
3313 uint32_t total_length;
3314 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003315 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003316 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003317 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003318 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003319 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003320 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003321 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003322 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003323 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003324 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003325
J-Alves96de29f2022-04-26 16:05:24 +01003326 if (!share_state->sending_complete) {
3327 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003328 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003329 "retrieve.\n",
3330 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003331 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003332 }
3333
J-Alves4f0d9c12024-01-17 17:23:11 +00003334 /*
3335 * Validate retrieve request, according to what was sent by the
3336 * sender. Function will output the `receiver_index` from the
3337 * provided memory region.
3338 */
3339 ret = ffa_memory_retrieve_validate(
3340 receiver_id, retrieve_request, retrieve_request_length,
3341 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003342
J-Alves4f0d9c12024-01-17 17:23:11 +00003343 if (ret.func != FFA_SUCCESS_32) {
3344 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003345 }
J-Alves96de29f2022-04-26 16:05:24 +01003346
J-Alves4f0d9c12024-01-17 17:23:11 +00003347 /*
3348 * Validate the requested permissions against the sent
3349 * permissions.
3350 * Outputs the permissions to give to retriever at S2
3351 * PTs.
3352 */
3353 ret = ffa_memory_retrieve_validate_memory_access_list(
3354 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003355 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003356 if (ret.func != FFA_SUCCESS_32) {
3357 return ret;
3358 }
3359
3360 memory_to_mode = ffa_memory_permissions_to_mode(
3361 permissions, share_state->sender_orig_mode);
3362
Daniel Boulby6e261362024-06-13 16:53:00 +01003363 /*
3364 * Check requested memory type is valid with the memory type of the
3365 * owner. E.g. they follow the memory type precedence where Normal
3366 * memory is more permissive than device and therefore device memory
3367 * can only be shared as device memory.
3368 */
3369 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3370 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3371 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3372 dlog_verbose(
3373 "Retrieving device memory as Normal memory is not "
3374 "allowed\n");
3375 return ffa_error(FFA_DENIED);
3376 }
3377
J-Alves4f0d9c12024-01-17 17:23:11 +00003378 ret = ffa_retrieve_check_update(
3379 to_locked, share_state->fragments,
3380 share_state->fragment_constituent_counts,
3381 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003382 share_state->share_func, false, page_pool, &retrieve_mode,
3383 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003384
3385 if (ret.func != FFA_SUCCESS_32) {
3386 return ret;
3387 }
3388
3389 share_state->retrieved_fragment_count[receiver_index] = 1;
3390
3391 is_retrieve_complete =
3392 share_state->retrieved_fragment_count[receiver_index] ==
3393 share_state->fragment_count;
3394
J-Alvesb5084cf2022-07-06 14:20:12 +01003395 /* VMs acquire the RX buffer from SPMC. */
3396 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3397
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003398 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003399 * Copy response to RX buffer of caller and deliver the message.
3400 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003401 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003402 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003403
Andrew Walbranca808b12020-05-15 17:22:28 +01003404 /*
J-Alves460d36c2023-10-12 17:02:15 +01003405 * Set the security state in the memory retrieve response attributes
3406 * if specified by the target mode.
3407 */
Karl Meakin3d32eef2024-11-25 16:40:09 +00003408 attributes = plat_ffa_memory_add_security_bit_from_mode(
3409 memory_region->attributes, retrieve_mode);
J-Alves460d36c2023-10-12 17:02:15 +01003410
3411 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003412 * Constituents which we received in the first fragment should
3413 * always fit in the first fragment we are sending, because the
3414 * header is the same size in both cases and we have a fixed
3415 * message buffer size. So `ffa_retrieved_memory_region_init`
3416 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003417 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003418
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003419 /* Provide the permissions that had been provided. */
3420 receiver->receiver_permissions.permissions = permissions;
3421
3422 /*
3423 * Prepare the memory region descriptor for the retrieve response.
3424 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003425 * structures.
3426 * At this point the retrieve request descriptor from the partition
3427 * has been processed. The `retrieve_request` is expected to be in
3428 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3429 * prepare the retrieve response before copying it to the RX buffer of
3430 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003431 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003432 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003433 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3434 memory_region->sender, attributes, memory_region->flags, handle,
3435 permissions, receiver, 1, memory_access_desc_size,
3436 composite->page_count, composite->constituent_count,
3437 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003438 share_state->fragment_constituent_counts[0], &total_length,
3439 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003440
J-Alves7b9cc432024-04-04 10:57:17 +01003441 /*
3442 * Copy the message from the buffer into the partition's mailbox.
3443 * The operation might fail unexpectedly due to change in PAS address
3444 * space, or improper values to the sizes of the structures.
3445 */
3446 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3447 retrieve_request, fragment_length)) {
3448 dlog_error(
3449 "%s: aborted the copy of response to RX buffer of "
3450 "%x.\n",
3451 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003452
3453 ffa_partition_memory_retrieve_request_undo(
3454 to_locked, share_state, receiver_index);
3455
J-Alves7b9cc432024-04-04 10:57:17 +01003456 return ffa_error(FFA_ABORTED);
3457 }
3458
J-Alves4f0d9c12024-01-17 17:23:11 +00003459 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003460 ffa_memory_retrieve_complete(share_states, share_state,
3461 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003462 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003463
3464 return ffa_memory_retrieve_resp(total_length, fragment_length);
3465}
3466
3467static struct ffa_value ffa_hypervisor_retrieve_request(
3468 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3469 struct ffa_memory_region *retrieve_request)
3470{
3471 struct ffa_value ret;
3472 struct ffa_composite_memory_region *composite;
3473 uint32_t total_length;
3474 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003475 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003476 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003477 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003478 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003479 ffa_memory_handle_t handle = retrieve_request->handle;
3480
J-Alves4f0d9c12024-01-17 17:23:11 +00003481 memory_region = share_state->memory_region;
3482
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003483 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3484
J-Alves7b6ab612024-01-24 09:54:54 +00003485 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003486 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003487 memory_access_desc_size = sizeof(struct ffa_memory_access);
3488 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003489 case FFA_VERSION_1_0:
3490 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003491 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3492 break;
3493 default:
3494 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3495 }
3496
J-Alves4f0d9c12024-01-17 17:23:11 +00003497 if (share_state->hypervisor_fragment_count != 0U) {
3498 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003499 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003500 "the hypervisor.\n",
3501 handle);
3502 return ffa_error(FFA_DENIED);
3503 }
3504
3505 share_state->hypervisor_fragment_count = 1;
3506
J-Alves4f0d9c12024-01-17 17:23:11 +00003507 /* VMs acquire the RX buffer from SPMC. */
3508 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3509
3510 /*
3511 * Copy response to RX buffer of caller and deliver the message.
3512 * This must be done before the share_state is (possibly) freed.
3513 */
3514 composite = ffa_memory_region_get_composite(memory_region, 0);
3515
3516 /*
3517 * Constituents which we received in the first fragment should
3518 * always fit in the first fragment we are sending, because the
3519 * header is the same size in both cases and we have a fixed
3520 * message buffer size. So `ffa_retrieved_memory_region_init`
3521 * should never fail.
3522 */
3523
3524 /*
3525 * Set the security state in the memory retrieve response attributes
3526 * if specified by the target mode.
3527 */
Karl Meakin3d32eef2024-11-25 16:40:09 +00003528 attributes = plat_ffa_memory_add_security_bit_from_mode(
J-Alves4f0d9c12024-01-17 17:23:11 +00003529 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003530
3531 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3532
J-Alves7b9cc432024-04-04 10:57:17 +01003533 /*
3534 * At this point the `retrieve_request` is expected to be in a section
3535 * managed by the hypervisor.
3536 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003537 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003538 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3539 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003540 receiver->receiver_permissions.permissions, receiver,
3541 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003542 composite->page_count, composite->constituent_count,
3543 share_state->fragments[0],
3544 share_state->fragment_constituent_counts[0], &total_length,
3545 &fragment_length));
3546
J-Alves7b9cc432024-04-04 10:57:17 +01003547 /*
3548 * Copy the message from the buffer into the hypervisor's mailbox.
3549 * The operation might fail unexpectedly due to change in PAS, or
3550 * improper values for the sizes of the structures.
3551 */
3552 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3553 retrieve_request, fragment_length)) {
3554 dlog_error(
3555 "%s: aborted the copy of response to RX buffer of "
3556 "%x.\n",
3557 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003558
3559 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3560
J-Alves7b9cc432024-04-04 10:57:17 +01003561 return ffa_error(FFA_ABORTED);
3562 }
3563
J-Alves3f6527c2024-04-25 17:10:57 +01003564 ffa_memory_retrieve_complete_from_hyp(share_state);
3565
J-Alves4f0d9c12024-01-17 17:23:11 +00003566 return ffa_memory_retrieve_resp(total_length, fragment_length);
3567}
3568
3569struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3570 struct ffa_memory_region *retrieve_request,
3571 uint32_t retrieve_request_length,
3572 struct mpool *page_pool)
3573{
3574 ffa_memory_handle_t handle = retrieve_request->handle;
3575 struct share_states_locked share_states;
3576 struct ffa_memory_share_state *share_state;
3577 struct ffa_value ret;
3578
3579 dump_share_states();
3580
3581 share_states = share_states_lock();
3582 share_state = get_share_state(share_states, handle);
3583 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003584 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003585 handle);
3586 ret = ffa_error(FFA_INVALID_PARAMETERS);
3587 goto out;
3588 }
3589
Daniel Boulby296ee702023-11-28 13:36:55 +00003590 if (is_ffa_hypervisor_retrieve_request(retrieve_request)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003591 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3592 retrieve_request);
3593 } else {
3594 ret = ffa_partition_retrieve_request(
3595 share_states, share_state, to_locked, retrieve_request,
3596 retrieve_request_length, page_pool);
3597 }
3598
3599 /* Track use of the RX buffer if the handling has succeeded. */
3600 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3601 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3602 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3603 }
3604
Andrew Walbranca808b12020-05-15 17:22:28 +01003605out:
3606 share_states_unlock(&share_states);
3607 dump_share_states();
3608 return ret;
3609}
3610
J-Alves5da37d92022-10-24 16:33:48 +01003611/**
3612 * Determine expected fragment offset according to the FF-A version of
3613 * the caller.
3614 */
3615static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3616 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003617 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003618{
3619 uint32_t expected_fragment_offset;
3620 uint32_t composite_constituents_offset;
3621
Karl Meakin0e617d92024-04-05 12:55:22 +01003622 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003623 /*
3624 * Hafnium operates memory regions in FF-A v1.1 format, so we
3625 * can retrieve the constituents offset from descriptor.
3626 */
3627 composite_constituents_offset =
3628 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003629 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003630 /*
3631 * If retriever is FF-A v1.0, determine the composite offset
3632 * as it is expected to have been configured in the
3633 * retrieve response.
3634 */
3635 composite_constituents_offset =
3636 sizeof(struct ffa_memory_region_v1_0) +
3637 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003638 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003639 sizeof(struct ffa_composite_memory_region);
3640 } else {
3641 panic("%s received an invalid FF-A version.\n", __func__);
3642 }
3643
3644 expected_fragment_offset =
3645 composite_constituents_offset +
3646 retrieved_constituents_count *
3647 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003648 (size_t)(memory_region->memory_access_desc_size *
3649 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003650
3651 return expected_fragment_offset;
3652}
3653
Andrew Walbranca808b12020-05-15 17:22:28 +01003654struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3655 ffa_memory_handle_t handle,
3656 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003657 ffa_id_t sender_vm_id,
J-Alvesc3fd9752024-04-04 11:45:33 +01003658 void *retrieve_continue_page,
Andrew Walbranca808b12020-05-15 17:22:28 +01003659 struct mpool *page_pool)
3660{
3661 struct ffa_memory_region *memory_region;
3662 struct share_states_locked share_states;
3663 struct ffa_memory_share_state *share_state;
3664 struct ffa_value ret;
3665 uint32_t fragment_index;
3666 uint32_t retrieved_constituents_count;
3667 uint32_t i;
3668 uint32_t expected_fragment_offset;
3669 uint32_t remaining_constituent_count;
3670 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003671 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003672 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003673
3674 dump_share_states();
3675
3676 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003677 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003678 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003679 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003680 handle);
3681 ret = ffa_error(FFA_INVALID_PARAMETERS);
3682 goto out;
3683 }
3684
3685 memory_region = share_state->memory_region;
3686 CHECK(memory_region != NULL);
3687
Andrew Walbranca808b12020-05-15 17:22:28 +01003688 if (!share_state->sending_complete) {
3689 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003690 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003691 "retrieve.\n",
3692 handle);
3693 ret = ffa_error(FFA_INVALID_PARAMETERS);
3694 goto out;
3695 }
3696
J-Alves59ed0042022-07-28 18:26:41 +01003697 /*
3698 * If retrieve request from the hypervisor has been initiated in the
3699 * given share_state, continue it, else assume it is a continuation of
J-Alvesc3fd9752024-04-04 11:45:33 +01003700 * retrieve request from a partition.
J-Alves59ed0042022-07-28 18:26:41 +01003701 */
3702 continue_ffa_hyp_mem_retrieve_req =
3703 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3704 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003705 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003706
J-Alves59ed0042022-07-28 18:26:41 +01003707 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003708 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003709 memory_region, to_locked.vm->id);
3710
3711 if (receiver_index == memory_region->receiver_count) {
3712 dlog_verbose(
3713 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003714 "borrower to memory sharing transaction "
3715 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003716 to_locked.vm->id, handle);
3717 ret = ffa_error(FFA_INVALID_PARAMETERS);
3718 goto out;
3719 }
3720
J-Alvesc3fd9752024-04-04 11:45:33 +01003721 fragment_index =
3722 share_state->retrieved_fragment_count[receiver_index];
3723
3724 if (fragment_index == 0 ||
3725 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003726 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003727 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003728 "started or already completed (%d/%d fragments "
3729 "retrieved).\n",
3730 handle,
3731 share_state->retrieved_fragment_count
3732 [receiver_index],
3733 share_state->fragment_count);
3734 ret = ffa_error(FFA_INVALID_PARAMETERS);
3735 goto out;
3736 }
J-Alves59ed0042022-07-28 18:26:41 +01003737 } else {
J-Alvesc3fd9752024-04-04 11:45:33 +01003738 fragment_index = share_state->hypervisor_fragment_count;
3739
3740 if (fragment_index == 0 ||
3741 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003742 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003743 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003744 "started from hypervisor.\n",
3745 handle);
3746 ret = ffa_error(FFA_INVALID_PARAMETERS);
3747 goto out;
3748 }
3749
3750 if (memory_region->sender != sender_vm_id) {
3751 dlog_verbose(
3752 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003753 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003754 sender_vm_id, handle);
3755 ret = ffa_error(FFA_INVALID_PARAMETERS);
3756 goto out;
3757 }
3758
J-Alves59ed0042022-07-28 18:26:41 +01003759 receiver_index = 0;
3760 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003761
3762 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003763 * Check that the given fragment offset is correct by counting
3764 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003765 */
3766 retrieved_constituents_count = 0;
3767 for (i = 0; i < fragment_index; ++i) {
3768 retrieved_constituents_count +=
3769 share_state->fragment_constituent_counts[i];
3770 }
J-Alvesc7484f12022-05-13 12:41:14 +01003771
3772 CHECK(memory_region->receiver_count > 0);
3773
Andrew Walbranca808b12020-05-15 17:22:28 +01003774 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003775 ffa_memory_retrieve_expected_offset_per_ffa_version(
3776 memory_region, retrieved_constituents_count,
3777 to_locked.vm->ffa_version);
3778
Andrew Walbranca808b12020-05-15 17:22:28 +01003779 if (fragment_offset != expected_fragment_offset) {
3780 dlog_verbose("Fragment offset was %d but expected %d.\n",
3781 fragment_offset, expected_fragment_offset);
3782 ret = ffa_error(FFA_INVALID_PARAMETERS);
3783 goto out;
3784 }
3785
J-Alves4f0d9c12024-01-17 17:23:11 +00003786 /*
3787 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3788 * is currently ownder by the SPMC.
3789 */
3790 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003791
Andrew Walbranca808b12020-05-15 17:22:28 +01003792 remaining_constituent_count = ffa_memory_fragment_init(
J-Alvesc3fd9752024-04-04 11:45:33 +01003793 (struct ffa_memory_region_constituent *)retrieve_continue_page,
3794 HF_MAILBOX_SIZE, share_state->fragments[fragment_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01003795 share_state->fragment_constituent_counts[fragment_index],
3796 &fragment_length);
3797 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003798
J-Alvesc3fd9752024-04-04 11:45:33 +01003799 /*
3800 * Return FFA_ERROR(FFA_ABORTED) in case the access to the partition's
3801 * RX buffer results in a GPF exception. Could happen if the retrieve
3802 * request is for a VM or the Hypervisor retrieve request, if the PAS
3803 * has been changed externally.
3804 */
3805 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3806 retrieve_continue_page, fragment_length)) {
3807 dlog_error(
3808 "%s: aborted copying fragment to RX buffer of %#x.\n",
3809 __func__, to_locked.vm->id);
3810 ret = ffa_error(FFA_ABORTED);
3811 goto out;
3812 }
3813
Andrew Walbranca808b12020-05-15 17:22:28 +01003814 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003815 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003816
J-Alves59ed0042022-07-28 18:26:41 +01003817 if (!continue_ffa_hyp_mem_retrieve_req) {
3818 share_state->retrieved_fragment_count[receiver_index]++;
3819 if (share_state->retrieved_fragment_count[receiver_index] ==
3820 share_state->fragment_count) {
3821 ffa_memory_retrieve_complete(share_states, share_state,
3822 page_pool);
3823 }
3824 } else {
3825 share_state->hypervisor_fragment_count++;
3826
3827 ffa_memory_retrieve_complete_from_hyp(share_state);
3828 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003829 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3830 .arg1 = (uint32_t)handle,
3831 .arg2 = (uint32_t)(handle >> 32),
3832 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003833
3834out:
3835 share_states_unlock(&share_states);
3836 dump_share_states();
3837 return ret;
3838}
3839
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003840struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003841 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003842 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003843{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003844 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003845 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003846 struct ffa_memory_share_state *share_state;
3847 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003848 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003849 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003850 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003851 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003852 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003853
Andrew Walbrana65a1322020-04-06 19:32:32 +01003854 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003855 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003856 "Stream endpoints not supported (got %d endpoints on "
3857 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003858 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003859 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003860 }
3861
J-Alvesbd060342024-04-26 18:44:31 +01003862 if (vm_id_is_current_world(from_locked.vm->id) &&
3863 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003864 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003865 "VM ID %d in relinquish message doesn't match calling "
3866 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003867 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003868 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003869 }
3870
3871 dump_share_states();
3872
3873 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003874 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003875 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003876 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003877 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003878 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003879 goto out;
3880 }
3881
Andrew Walbranca808b12020-05-15 17:22:28 +01003882 if (!share_state->sending_complete) {
3883 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003884 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003885 "relinquish.\n",
3886 handle);
3887 ret = ffa_error(FFA_INVALID_PARAMETERS);
3888 goto out;
3889 }
3890
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003891 memory_region = share_state->memory_region;
3892 CHECK(memory_region != NULL);
3893
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003894 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003895 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003896
3897 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003898 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003899 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003900 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003901 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003902 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003903 goto out;
3904 }
3905
J-Alves8eb19162022-04-28 10:56:48 +01003906 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003907 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003908 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003909 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003910 "receiver %x can't relinquish.\n",
3911 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003912 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003913 goto out;
3914 }
3915
J-Alves3c5b2072022-11-21 12:45:40 +00003916 /*
3917 * Either clear if requested in relinquish call, or in a retrieve
3918 * request from one of the borrowers.
3919 */
3920 receivers_relinquished_memory = true;
3921
3922 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3923 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003924 ffa_memory_region_get_receiver(memory_region, i);
3925 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003926 if (receiver->receiver_permissions.receiver ==
3927 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003928 receiver_permissions =
3929 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003930 continue;
3931 }
3932
3933 if (share_state->retrieved_fragment_count[i] != 0U) {
3934 receivers_relinquished_memory = false;
3935 break;
3936 }
3937 }
3938
3939 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003940 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3941 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003942
3943 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003944 * Clear is not allowed for memory that was shared, as the
3945 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003946 */
J-Alves95fbb312024-03-20 15:19:16 +00003947 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3948 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003949 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003950 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003951 goto out;
3952 }
3953
J-Alvesb886d492024-04-15 10:55:29 +01003954 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003955 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3956 __func__);
3957 ret = ffa_error(FFA_DENIED);
3958 goto out;
3959 }
3960
Andrew Walbranca808b12020-05-15 17:22:28 +01003961 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003962 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003963 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003964 share_state->fragment_count, share_state->sender_orig_mode,
3965 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003966
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003967 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003968 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003969 * Mark memory handle as not retrieved, so it can be
3970 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003971 */
J-Alves8eb19162022-04-28 10:56:48 +01003972 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003973 }
3974
3975out:
3976 share_states_unlock(&share_states);
3977 dump_share_states();
3978 return ret;
3979}
3980
3981/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003982 * Validates that the reclaim transition is allowed for the given
3983 * handle, updates the page table of the reclaiming VM, and frees the
3984 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003985 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003986struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003987 ffa_memory_handle_t handle,
3988 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003989 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003990{
3991 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003992 struct ffa_memory_share_state *share_state;
3993 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003994 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003995
3996 dump_share_states();
3997
3998 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003999
Karl Meakin4a2854a2023-06-30 16:26:52 +01004000 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00004001 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00004002 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004003 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004004 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004005 goto out;
4006 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01004007 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004008
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004009 CHECK(memory_region != NULL);
4010
J-Alvesa9cd7e32022-07-01 13:49:33 +01004011 if (vm_id_is_current_world(to_locked.vm->id) &&
4012 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004013 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004014 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004015 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004016 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004017 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004018 goto out;
4019 }
4020
Andrew Walbranca808b12020-05-15 17:22:28 +01004021 if (!share_state->sending_complete) {
4022 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004023 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004024 "reclaim.\n",
4025 handle);
4026 ret = ffa_error(FFA_INVALID_PARAMETERS);
4027 goto out;
4028 }
4029
J-Alves752236c2022-04-28 11:07:47 +01004030 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4031 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004032 struct ffa_memory_access *receiver =
4033 ffa_memory_region_get_receiver(memory_region,
4034 i);
4035
4036 assert(receiver != NULL);
4037 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004038 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004039 "Tried to reclaim memory handle %#lx that has "
4040 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004041 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004042 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004043 ret = ffa_error(FFA_DENIED);
4044 goto out;
4045 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004046 }
4047
Andrew Walbranca808b12020-05-15 17:22:28 +01004048 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004049 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004050 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004051 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004052 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004053 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004055 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004056 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004057 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004058 }
4059
4060out:
4061 share_states_unlock(&share_states);
4062 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004063}