blob: 52969bd0515d0a6f9d23b1c18698cebb552d8e6b [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"
Andrew Walbran290b0c92020-02-03 16:37:14 +000013
J-Alves5952d942022-12-22 16:03:00 +000014#include "hf/addr.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010015#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010017#include "hf/ffa.h"
Karl Meakin902af082024-11-28 14:58:38 +000018#include "hf/ffa/ffa_memory.h"
19#include "hf/ffa/setup_and_discovery.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010021#include "hf/ffa_memory_internal.h"
J-Alves5952d942022-12-22 16:03:00 +000022#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000023#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000024#include "hf/panic.h"
25#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000027#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000028#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010029
J-Alves2d8457f2022-10-05 11:06:41 +010030#include "vmapi/hf/ffa_v1_0.h"
31
J-Alves5da37d92022-10-24 16:33:48 +010032#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
33
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000034/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010035 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000036 * by this lock.
37 */
38static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040
41/**
J-Alvesed508c82023-05-04 16:09:48 +010042 * Return the offset to the first constituent within the
43 * `ffa_composite_memory_region` for the given receiver from an
44 * `ffa_memory_region`. The caller must check that the receiver_index is within
45 * bounds, and that it has a composite memory region offset.
46 */
47static uint32_t ffa_composite_constituent_offset(
48 struct ffa_memory_region *memory_region, uint32_t receiver_index)
49{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000050 struct ffa_memory_access *receiver;
51 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010052
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000053 CHECK(receiver_index < memory_region->receiver_count);
54
55 receiver =
56 ffa_memory_region_get_receiver(memory_region, receiver_index);
57 CHECK(receiver != NULL);
58
59 composite_offset = receiver->composite_memory_region_offset;
60
61 CHECK(composite_offset != 0);
62
63 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010064}
65
66/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010067 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
68 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
69 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010070 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010071 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
72 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010073 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010074struct ffa_memory_share_state *allocate_share_state(
75 struct share_states_locked share_states, uint32_t share_func,
76 struct ffa_memory_region *memory_region, uint32_t fragment_length,
77 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000078{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000079 assert(share_states.share_states != NULL);
80 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000081
Karl Meakin52cdfe72023-06-30 14:49:10 +010082 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010083 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010084 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010085 &share_states.share_states[i];
86 struct ffa_composite_memory_region *composite =
87 ffa_memory_region_get_composite(memory_region,
88 0);
89
90 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000091 memory_region->handle =
Karl Meakin117c8082024-12-04 16:03:28 +000092 ffa_memory_make_handle(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010093 } else {
J-Alvesee68c542020-10-29 17:48:20 +000094 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010095 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000096 allocated_state->share_func = share_func;
97 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010098 allocated_state->fragment_count = 1;
99 allocated_state->fragments[0] = composite->constituents;
100 allocated_state->fragment_constituent_counts[0] =
101 (fragment_length -
102 ffa_composite_constituent_offset(memory_region,
103 0)) /
104 sizeof(struct ffa_memory_region_constituent);
105 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100106 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
107 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100108 allocated_state->retrieved_fragment_count[j] =
109 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100111 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 }
113 }
114
Karl Meakin52cdfe72023-06-30 14:49:10 +0100115 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116}
117
118/** Locks the share states lock. */
119struct share_states_locked share_states_lock(void)
120{
121 sl_lock(&share_states_lock_instance);
122
123 return (struct share_states_locked){.share_states = share_states};
124}
125
126/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100127void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000129 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000130 share_states->share_states = NULL;
131 sl_unlock(&share_states_lock_instance);
132}
133
134/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100136 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000137 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100138struct ffa_memory_share_state *get_share_state(
139 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100141 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000143 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100144
145 /*
146 * First look for a share_state allocated by us, in which case the
147 * handle is based on the index.
148 */
Karl Meakin117c8082024-12-04 16:03:28 +0000149 if (ffa_memory_is_handle_allocated_by_current_world(handle)) {
Karl Meakin1a760e72024-07-25 18:58:37 +0100150 uint64_t index = ffa_memory_handle_index(handle);
Karl Meakin4a2854a2023-06-30 16:26:52 +0100151
Andrew Walbranca808b12020-05-15 17:22:28 +0100152 if (index < MAX_MEM_SHARES) {
153 share_state = &share_states.share_states[index];
154 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100155 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100156 }
157 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000158 }
159
Andrew Walbranca808b12020-05-15 17:22:28 +0100160 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100161 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000163 if (share_state->memory_region != NULL &&
164 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100166 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 }
169
Karl Meakin4a2854a2023-06-30 16:26:52 +0100170 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000171}
172
173/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100174void share_state_free(struct share_states_locked share_states,
175 struct ffa_memory_share_state *share_state,
176 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000177{
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 uint32_t i;
179
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000180 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100182 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100184 /*
185 * First fragment is part of the same page as the `memory_region`, so it
186 * doesn't need to be freed separately.
187 */
188 share_state->fragments[0] = NULL;
189 share_state->fragment_constituent_counts[0] = 0;
190 for (i = 1; i < share_state->fragment_count; ++i) {
191 mpool_free(page_pool, share_state->fragments[i]);
192 share_state->fragments[i] = NULL;
193 share_state->fragment_constituent_counts[i] = 0;
194 }
195 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000196 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100197 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000198}
199
Andrew Walbranca808b12020-05-15 17:22:28 +0100200/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100201bool share_state_sending_complete(struct share_states_locked share_states,
202 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203{
Andrew Walbranca808b12020-05-15 17:22:28 +0100204 struct ffa_composite_memory_region *composite;
205 uint32_t expected_constituent_count;
206 uint32_t fragment_constituent_count_total = 0;
207 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208
Andrew Walbranca808b12020-05-15 17:22:28 +0100209 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000210 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100211
212 /*
213 * Share state must already be valid, or it's not possible to get hold
214 * of it.
215 */
216 CHECK(share_state->memory_region != NULL &&
217 share_state->share_func != 0);
218
219 composite =
220 ffa_memory_region_get_composite(share_state->memory_region, 0);
221 expected_constituent_count = composite->constituent_count;
222 for (i = 0; i < share_state->fragment_count; ++i) {
223 fragment_constituent_count_total +=
224 share_state->fragment_constituent_counts[i];
225 }
226 dlog_verbose(
227 "Checking completion: constituent count %d/%d from %d "
228 "fragments.\n",
229 fragment_constituent_count_total, expected_constituent_count,
230 share_state->fragment_count);
231
232 return fragment_constituent_count_total == expected_constituent_count;
233}
234
235/**
236 * Calculates the offset of the next fragment expected for the given share
237 * state.
238 */
J-Alvesfdd29272022-07-19 13:16:31 +0100239uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100240 struct share_states_locked share_states,
241 struct ffa_memory_share_state *share_state)
242{
243 uint32_t next_fragment_offset;
244 uint32_t i;
245
246 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000247 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100248
249 next_fragment_offset =
250 ffa_composite_constituent_offset(share_state->memory_region, 0);
251 for (i = 0; i < share_state->fragment_count; ++i) {
252 next_fragment_offset +=
253 share_state->fragment_constituent_counts[i] *
254 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255 }
256
Andrew Walbranca808b12020-05-15 17:22:28 +0100257 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000258}
259
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100260static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000261{
262 uint32_t i;
263
264 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
265 return;
266 }
267
Karl Meakine8937d92024-03-19 16:04:25 +0000268 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
269 "type = %s, security = %s), flags %#x, handle %#lx "
270 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100271 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000272 memory_region->sender,
273 ffa_memory_shareability_name(
274 memory_region->attributes.shareability),
275 ffa_memory_cacheability_name(
276 memory_region->attributes.cacheability),
277 ffa_memory_type_name(memory_region->attributes.type),
278 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000279 memory_region->flags, memory_region->handle, memory_region->tag,
280 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100281 memory_region->receiver_count);
282 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000283 struct ffa_memory_access *receiver =
284 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000285 if (i != 0) {
286 dlog(", ");
287 }
Karl Meakine8937d92024-03-19 16:04:25 +0000288 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000289 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000290 ffa_data_access_name(receiver->receiver_permissions
291 .permissions.data_access),
292 ffa_instruction_access_name(
293 receiver->receiver_permissions.permissions
294 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000295 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000296 /* The impdef field is only present from v1.2 and later */
297 if (ffa_version_from_memory_access_desc_size(
298 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100299 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000300 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000301 receiver->impdef.val[1]);
302 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000303 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000304 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305}
306
J-Alves66652252022-07-06 09:49:51 +0100307void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000308{
309 uint32_t i;
310
311 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
312 return;
313 }
314
315 dlog("Current share states:\n");
316 sl_lock(&share_states_lock_instance);
317 for (i = 0; i < MAX_MEM_SHARES; ++i) {
318 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000319 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000320 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000322 dlog("SHARE");
323 break;
J-Alves95fbb312024-03-20 15:19:16 +0000324 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100325 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000326 dlog("LEND");
327 break;
J-Alves95fbb312024-03-20 15:19:16 +0000328 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100329 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000330 dlog("DONATE");
331 break;
332 default:
333 dlog("invalid share_func %#x",
334 share_states[i].share_func);
335 }
Karl Meakine8937d92024-03-19 16:04:25 +0000336 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100338 if (share_states[i].sending_complete) {
339 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100341 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000342 }
J-Alves2a0d2882020-10-29 14:49:50 +0000343 dlog(" with %d fragments, %d retrieved, "
344 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100345 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000346 share_states[i].retrieved_fragment_count[0],
347 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000348 }
349 }
350 sl_unlock(&share_states_lock_instance);
351}
352
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100353static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100354 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000355{
Karl Meakin07a69ab2025-02-07 14:53:19 +0000356 mm_mode_t mode = 0;
Andrew Walbran475c1452020-02-07 13:22:22 +0000357
Karl Meakin84710f32023-10-12 15:14:49 +0100358 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000360 mode = MM_MODE_R;
361 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000363 mode = MM_MODE_R | MM_MODE_W;
364 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100365 case FFA_DATA_ACCESS_NOT_SPECIFIED:
366 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
367 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100368 case FFA_DATA_ACCESS_RESERVED:
369 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100370 default:
371 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100372 }
373
Karl Meakin84710f32023-10-12 15:14:49 +0100374 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100375 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000376 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100378 mode |= MM_MODE_X;
379 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100380 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
381 mode |= (default_mode & MM_MODE_X);
382 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_INSTRUCTION_ACCESS_RESERVED:
384 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100385 default:
386 panic("Unknown instruction access %#x\n",
387 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000388 }
389
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200390 /* Set the security state bit if necessary. */
Karl Meakin117c8082024-12-04 16:03:28 +0000391 if ((default_mode & ffa_memory_get_other_world_mode()) != 0) {
392 mode |= ffa_memory_get_other_world_mode();
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200393 }
394
Daniel Boulby6e261362024-06-13 16:53:00 +0100395 mode |= default_mode & MM_MODE_D;
396
Andrew Walbran475c1452020-02-07 13:22:22 +0000397 return mode;
398}
399
Jose Marinho75509b42019-04-09 09:34:59 +0100400/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000401 * Get the current mode in the stage-2 page table of the given vm of all the
402 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100403 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100404 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100405static struct ffa_value constituents_get_mode(
Karl Meakin07a69ab2025-02-07 14:53:19 +0000406 struct vm_locked vm, mm_mode_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100407 struct ffa_memory_region_constituent **fragments,
408 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100409{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100410 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100411 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100412
Andrew Walbranca808b12020-05-15 17:22:28 +0100413 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100414 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000415 * Fail if there are no constituents. Otherwise we would get an
416 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100417 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100418 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100420 }
421
Andrew Walbranca808b12020-05-15 17:22:28 +0100422 for (i = 0; i < fragment_count; ++i) {
423 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
424 ipaddr_t begin = ipa_init(fragments[i][j].address);
425 size_t size = fragments[i][j].page_count * PAGE_SIZE;
426 ipaddr_t end = ipa_add(begin, size);
427 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428
Andrew Walbranca808b12020-05-15 17:22:28 +0100429 /* Fail if addresses are not page-aligned. */
430 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
431 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100432 dlog_verbose("%s: addresses not page-aligned\n",
433 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100434 return ffa_error(FFA_INVALID_PARAMETERS);
435 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100436
Andrew Walbranca808b12020-05-15 17:22:28 +0100437 /*
438 * Ensure that this constituent memory range is all
439 * mapped with the same mode.
440 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800441 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100442 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000443 "%s: constituent memory range "
444 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100445 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000446 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100447 return ffa_error(FFA_DENIED);
448 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100449
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 /*
451 * Ensure that all constituents are mapped with the same
452 * mode.
453 */
454 if (i == 0) {
455 *orig_mode = current_mode;
456 } else if (current_mode != *orig_mode) {
457 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100458 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000459 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100460 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100461 fragments[i][j].page_count,
462 ipa_addr(begin));
463 return ffa_error(FFA_DENIED);
464 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100465 }
Jose Marinho75509b42019-04-09 09:34:59 +0100466 }
467
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100468 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000469}
470
Karl Meakin0e617d92024-04-05 12:55:22 +0100471enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100472 uint32_t memory_access_desc_size)
473{
474 switch (memory_access_desc_size) {
475 /*
476 * v1.0 and v1.1 memory access descriptors are the same size however
477 * v1.1 is the first version to include the memory access descriptor
478 * size field so return v1.1.
479 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000480 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100481 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000482 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100483 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100484 default:
485 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100486 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100487}
488
489/**
490 * Check if the receivers size and offset given is valid for the senders
491 * FF-A version.
492 */
493static bool receiver_size_and_offset_valid_for_version(
494 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100495 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100496{
497 /*
498 * Check that the version that the memory access descriptor size belongs
499 * to is compatible with the FF-A version we believe the sender to be.
500 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100501 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100502 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100503 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100504 return false;
505 }
506
507 /*
508 * Check the receivers_offset matches the version we found from
509 * memory access descriptor size.
510 */
511 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100512 case FFA_VERSION_1_1:
513 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100514 return receivers_offset == sizeof(struct ffa_memory_region);
515 default:
516 return false;
517 }
518}
519
520/**
521 * Check the values set for fields in the memory region are valid and safe.
522 * Offset values are within safe bounds, receiver count will not cause overflows
523 * and reserved fields are 0.
524 */
525bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100526 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100527 uint32_t fragment_length,
528 bool send_transaction)
529{
530 uint32_t receiver_count;
531 struct ffa_memory_access *receiver;
532 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000533 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
534 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100535
Karl Meakin0e617d92024-04-05 12:55:22 +0100536 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100537 /* Check the reserved fields are 0. */
538 if (memory_region_v1_0->reserved_0 != 0 ||
539 memory_region_v1_0->reserved_1 != 0) {
540 dlog_verbose("Reserved fields must be 0.\n");
541 return false;
542 }
543
544 receiver_count = memory_region_v1_0->receiver_count;
545 } else {
546 uint32_t receivers_size =
547 memory_region->memory_access_desc_size;
548 uint32_t receivers_offset = memory_region->receivers_offset;
549
550 /* Check the reserved field is 0. */
551 if (memory_region->reserved[0] != 0 ||
552 memory_region->reserved[1] != 0 ||
553 memory_region->reserved[2] != 0) {
554 dlog_verbose("Reserved fields must be 0.\n");
555 return false;
556 }
557
558 /*
559 * Check memory_access_desc_size matches the size of the struct
560 * for the senders FF-A version.
561 */
562 if (!receiver_size_and_offset_valid_for_version(
563 receivers_size, receivers_offset, ffa_version)) {
564 dlog_verbose(
565 "Invalid memory access descriptor size %d, "
566 " or receiver offset %d, "
567 "for FF-A version %#x\n",
568 receivers_size, receivers_offset, ffa_version);
569 return false;
570 }
571
572 receiver_count = memory_region->receiver_count;
573 }
574
575 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000576 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100577 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000578 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100579 "specified %u\n",
580 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
581 return false;
582 }
583
584 /* Check values in the memory access descriptors. */
585 /*
586 * The composite offset values must be the same for all recievers so
587 * check the first one is valid and then they are all the same.
588 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100589 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000590 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100591 ->receivers[0]
592 : ffa_memory_region_get_receiver(memory_region, 0);
593 assert(receiver != NULL);
594 composite_offset_0 = receiver->composite_memory_region_offset;
595
596 if (!send_transaction) {
597 if (composite_offset_0 != 0) {
598 dlog_verbose(
599 "Composite offset memory region descriptor "
600 "offset must be 0 for retrieve requests. "
601 "Currently %d",
602 composite_offset_0);
603 return false;
604 }
605 } else {
606 bool comp_offset_is_zero = composite_offset_0 == 0U;
607 bool comp_offset_lt_transaction_descriptor_size =
608 composite_offset_0 <
609 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100610 (size_t)(memory_region->memory_access_desc_size *
611 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100612 bool comp_offset_with_comp_gt_fragment_length =
613 composite_offset_0 +
614 sizeof(struct ffa_composite_memory_region) >
615 fragment_length;
616 if (comp_offset_is_zero ||
617 comp_offset_lt_transaction_descriptor_size ||
618 comp_offset_with_comp_gt_fragment_length) {
619 dlog_verbose(
620 "Invalid composite memory region descriptor "
621 "offset for send transaction %u\n",
622 composite_offset_0);
623 return false;
624 }
625 }
626
Karl Meakin824b63d2024-06-03 19:04:53 +0100627 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100628 uint32_t composite_offset;
629
Karl Meakin0e617d92024-04-05 12:55:22 +0100630 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100631 struct ffa_memory_access_v1_0 *receiver_v1_0 =
632 &memory_region_v1_0->receivers[i];
633 /* Check reserved fields are 0 */
634 if (receiver_v1_0->reserved_0 != 0) {
635 dlog_verbose(
636 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000637 "descriptor must be zero. Currently "
638 "reciever %zu has a reserved field "
639 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100640 i, receiver_v1_0->reserved_0);
641 return false;
642 }
643 /*
644 * We can cast to the current version receiver as the
645 * remaining fields we are checking have the same
646 * offsets for all versions since memory access
647 * descriptors are forwards compatible.
648 */
649 receiver = (struct ffa_memory_access *)receiver_v1_0;
650 } else {
651 receiver = ffa_memory_region_get_receiver(memory_region,
652 i);
653 assert(receiver != NULL);
654
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100655 if (ffa_version == FFA_VERSION_1_1) {
656 /*
657 * Since the reserved field is at the end of the
658 * Endpoint Memory Access Descriptor we must
659 * cast to ffa_memory_access_v1_0 as they match.
660 * Since all fields except reserved in the
661 * Endpoint Memory Access Descriptor have the
662 * same offsets across all versions this cast is
663 * not required when accessing other fields in
664 * the future.
665 */
666 struct ffa_memory_access_v1_0 *receiver_v1_0 =
667 (struct ffa_memory_access_v1_0 *)
668 receiver;
669 if (receiver_v1_0->reserved_0 != 0) {
670 dlog_verbose(
671 "Reserved field in the memory "
672 "access descriptor must be "
673 "zero. Currently reciever %zu "
674 "has a reserved field with a "
675 "value of %lu\n",
676 i, receiver_v1_0->reserved_0);
677 return false;
678 }
679
680 } else {
681 if (receiver->reserved_0 != 0) {
682 dlog_verbose(
683 "Reserved field in the memory "
684 "access descriptor must be "
685 "zero. Currently reciever %zu "
686 "has a reserved field with a "
687 "value of %lu\n",
688 i, receiver->reserved_0);
689 return false;
690 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100691 }
692 }
693
694 /* Check composite offset values are equal for all receivers. */
695 composite_offset = receiver->composite_memory_region_offset;
696 if (composite_offset != composite_offset_0) {
697 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000698 "Composite offset %x differs from %x in "
699 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100700 composite_offset, composite_offset_0);
701 return false;
702 }
703 }
704 return true;
705}
706
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000707/**
J-Alves460d36c2023-10-12 17:02:15 +0100708 * If the receivers for the memory management operation are all from the
Daniel Boulby734981e2024-07-22 11:06:35 +0100709 * secure world, the memory is not device memory (as it isn't covered by the
710 * granule page table) and this isn't a FFA_MEM_SHARE, then request memory
711 * security state update by returning MAP_ACTION_CHECK_PROTECT.
J-Alves460d36c2023-10-12 17:02:15 +0100712 */
713static enum ffa_map_action ffa_mem_send_get_map_action(
714 bool all_receivers_from_current_world, ffa_id_t sender_id,
Daniel Boulby734981e2024-07-22 11:06:35 +0100715 uint32_t mem_func_id, bool is_normal_memory)
J-Alves460d36c2023-10-12 17:02:15 +0100716{
J-Alves95fbb312024-03-20 15:19:16 +0000717 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
718 mem_func_id == FFA_MEM_SHARE_64;
719 const bool protect_memory =
720 (!is_memory_share_abi && all_receivers_from_current_world &&
Daniel Boulby734981e2024-07-22 11:06:35 +0100721 ffa_is_vm_id(sender_id) && is_normal_memory);
J-Alves460d36c2023-10-12 17:02:15 +0100722
723 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
724}
725
726/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 * Verify that all pages have the same mode, that the starting mode
728 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100729 * to the sending VM. It outputs the mapping action that needs to be
730 * invoked for the given memory range. On memory lend/donate there
731 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000732 *
733 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734 * 1) FFA_DENIED if a state transition was not found;
735 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100736 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100737 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100738 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100739 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
740 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000741 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100742static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100743 struct vm_locked from, uint32_t share_func,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000744 struct ffa_memory_region *memory_region, mm_mode_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100745 struct ffa_memory_region_constituent **fragments,
746 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000747 mm_mode_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000748{
Karl Meakin07a69ab2025-02-07 14:53:19 +0000749 const mm_mode_t state_mask =
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000750 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100752 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000753 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000754 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
755 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000756
Andrew Walbranca808b12020-05-15 17:22:28 +0100757 ret = constituents_get_mode(from, orig_from_mode, fragments,
758 fragment_constituent_counts,
759 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100760 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100761 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100762 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100763 }
764
Daniel Boulby6e261362024-06-13 16:53:00 +0100765 /*
766 * Check requested memory type is valid with the memory type of the
767 * owner. E.g. they follow the memory type precedence where Normal
768 * memory is more permissive than device and therefore device memory
769 * can only be shared as device memory.
770 */
771 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
772 (*orig_from_mode & MM_MODE_D) != 0U) {
773 dlog_verbose(
774 "Send device memory as Normal memory is not allowed\n");
775 return ffa_error(FFA_DENIED);
776 }
777
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000778 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000779 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000780 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000781 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000782 "Device memory can only be lent to a single borrower "
783 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000784 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000786 }
787
788 /*
789 * Ensure the sender is the owner and has exclusive access to the
790 * memory.
791 */
792 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100793 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100794 }
795
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100796 /*
797 * Memory cannot be zeroed during the lend/donate operation if the
798 * sender only has RO access.
799 */
800 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
801 dlog_verbose(
802 "Cannot zero memory when the sender doesn't have "
803 "write access\n");
804 return ffa_error(FFA_DENIED);
805 }
806
Daniel Boulbya76fd912024-02-22 14:22:15 +0000807 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100808
J-Alves363f5722022-04-25 17:37:37 +0100809 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000810 struct ffa_memory_access *receiver =
811 ffa_memory_region_get_receiver(memory_region, i);
812 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100813 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000814 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100815 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
816 permissions, *orig_from_mode);
817
J-Alves788b4492023-04-18 14:01:23 +0100818 /*
819 * The assumption is that at this point, the operation from
820 * SP to a receiver VM, should have returned an FFA_ERROR
821 * already.
822 */
823 if (!ffa_is_vm_id(from.vm->id)) {
824 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000825 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100826 }
827
J-Alves460d36c2023-10-12 17:02:15 +0100828 /* Track if all senders are from current world. */
829 all_receivers_from_current_world =
830 all_receivers_from_current_world &&
831 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000832 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100833
J-Alves363f5722022-04-25 17:37:37 +0100834 if ((*orig_from_mode & required_from_mode) !=
835 required_from_mode) {
836 dlog_verbose(
837 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100838 "which required mode %#x but only had %#x "
839 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100840 required_from_mode, *orig_from_mode);
841 return ffa_error(FFA_DENIED);
842 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000843 }
844
J-Alves460d36c2023-10-12 17:02:15 +0100845 *map_action = ffa_mem_send_get_map_action(
Daniel Boulby734981e2024-07-22 11:06:35 +0100846 all_receivers_from_current_world, from.vm->id, share_func,
847 (*orig_from_mode & MM_MODE_D) == 0U);
J-Alves460d36c2023-10-12 17:02:15 +0100848
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000849 /* Find the appropriate new mode. */
850 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000851 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000852 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000854 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100855 break;
J-Alves95fbb312024-03-20 15:19:16 +0000856 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100857 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000858 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100859 break;
J-Alves95fbb312024-03-20 15:19:16 +0000860 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100861 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000862 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100863 break;
864
Jose Marinho75509b42019-04-09 09:34:59 +0100865 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100867 }
868
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100869 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000870}
871
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100872static struct ffa_value ffa_relinquish_check_transition(
Karl Meakin07a69ab2025-02-07 14:53:19 +0000873 struct vm_locked from, mm_mode_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100874 struct ffa_memory_region_constituent **fragments,
875 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000876 mm_mode_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000877{
878 const uint32_t state_mask =
879 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
880 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882
J-Alves69cdfd92024-04-26 11:40:59 +0100883 assert(map_action != NULL);
884 if (vm_id_is_current_world(from.vm->id)) {
885 *map_action = MAP_ACTION_COMMIT;
886 } else {
887 /*
888 * No need to check the attributes of caller.
889 * The assumption is that the retrieve request of the receiver
890 * also used the MAP_ACTION_NONE, and no update was done to the
891 * page tables. When the receiver is not at the secure virtual
892 * instance SPMC doesn't manage its S2 translation (i.e. when
893 * the receiver is a VM).
894 */
895 *map_action = MAP_ACTION_NONE;
896
897 return (struct ffa_value){.func = FFA_SUCCESS_32};
898 }
899
Andrew Walbranca808b12020-05-15 17:22:28 +0100900 ret = constituents_get_mode(from, orig_from_mode, fragments,
901 fragment_constituent_counts,
902 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100904 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000905 }
906
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000907 /*
908 * Ensure the relinquishing VM is not the owner but has access to the
909 * memory.
910 */
911 orig_from_state = *orig_from_mode & state_mask;
912 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
913 dlog_verbose(
914 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100915 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100917 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000918 }
919
920 /* Find the appropriate new mode. */
921 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
922
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924}
925
926/**
927 * Verify that all pages have the same mode, that the starting mode
928 * constitutes a valid state and obtain the next mode to apply
929 * to the retrieving VM.
930 *
931 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 * 1) FFA_DENIED if a state transition was not found;
933 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100934 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100936 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
938 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000939 */
J-Alvesfc19b372022-07-06 12:17:35 +0100940struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000941 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 struct ffa_memory_region_constituent **fragments,
943 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +0000944 mm_mode_t sender_orig_mode, mm_mode_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100945 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000946{
Karl Meakin07a69ab2025-02-07 14:53:19 +0000947 mm_mode_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000949
Andrew Walbranca808b12020-05-15 17:22:28 +0100950 ret = constituents_get_mode(to, &orig_to_mode, fragments,
951 fragment_constituent_counts,
952 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100955 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000956 }
957
J-Alves460d36c2023-10-12 17:02:15 +0100958 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100959 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100960
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000962 /*
963 * If the original ffa memory send call has been processed
964 * successfully, it is expected the orig_to_mode would overlay
965 * with `state_mask`, as a result of the function
966 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100967 *
968 * If Hafnium is the SPMC:
969 * - Caller of the reclaim interface is an SP, the memory shall
970 * have been protected throughout the flow.
971 * - Caller of the reclaim is from the NWd, the memory may have
972 * been protected at the time of lending/donating the memory.
973 * In such case, set action to unprotect memory in the
974 * handling of reclaim operation.
975 * - If Hafnium is the hypervisor memory shall never have been
976 * protected in memory lend/share/donate.
977 *
978 * More details in the doc comment of the function
979 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000980 */
J-Alves59ed0042022-07-28 18:26:41 +0100981 if (vm_id_is_current_world(to.vm->id)) {
982 assert((orig_to_mode &
983 (MM_MODE_INVALID | MM_MODE_UNOWNED |
984 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100985 assert(!memory_protected);
986 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
987 map_action != NULL && memory_protected) {
988 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100989 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000990 } else {
J-Alves69cdfd92024-04-26 11:40:59 +0100991 if (!vm_id_is_current_world(to.vm->id)) {
992 assert(map_action != NULL);
993 *map_action = MAP_ACTION_NONE;
994 return (struct ffa_value){.func = FFA_SUCCESS_32};
995 }
996
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100998 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000999 * Ensure the retriever has the expected state. We don't care
1000 * about the MM_MODE_SHARED bit; either with or without it set
1001 * are both valid representations of the !O-NA state.
1002 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001003 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001004 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001005 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1006 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001007 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001008 }
J-Alves460d36c2023-10-12 17:02:15 +01001009
1010 /*
1011 * If memory has been protected before, clear the NS bit to
1012 * allow the secure access from the SP.
1013 */
1014 if (memory_protected) {
Karl Meakin117c8082024-12-04 16:03:28 +00001015 *to_mode &= ~ffa_memory_get_other_world_mode();
J-Alves460d36c2023-10-12 17:02:15 +01001016 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001017 }
1018
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001020 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 *to_mode |= 0;
1023 break;
J-Alves95fbb312024-03-20 15:19:16 +00001024 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001025 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001026 *to_mode |= MM_MODE_UNOWNED;
1027 break;
J-Alves95fbb312024-03-20 15:19:16 +00001028 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1031 break;
1032
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034 *to_mode |= 0;
1035 break;
1036
1037 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001038 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001039 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001040 }
1041
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001043}
Jose Marinho09b1db82019-08-08 09:16:59 +01001044
J-Alvescf6253e2024-01-03 13:48:48 +00001045/*
1046 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1047 * Returns:
1048 * - FFA_SUCCESS_32: if all goes well.
1049 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1050 * the page table update. Or error code provided by the function
1051 * `arch_memory_protect`.
1052 */
1053static struct ffa_value ffa_region_group_check_actions(
1054 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001055 struct mpool *ppool, mm_mode_t mode, enum ffa_map_action action,
J-Alvescf6253e2024-01-03 13:48:48 +00001056 bool *memory_protected)
1057{
1058 struct ffa_value ret;
1059 bool is_memory_protected;
1060
1061 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1062 dlog_verbose(
1063 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001064 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001065 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1066 pa_addr(pa_end));
1067 return ffa_error(FFA_NO_MEMORY);
1068 }
1069
1070 switch (action) {
1071 case MAP_ACTION_CHECK:
1072 /* No protect requested. */
1073 is_memory_protected = false;
1074 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1075 break;
1076 case MAP_ACTION_CHECK_PROTECT: {
1077 paddr_t last_protected_pa = pa_init(0);
1078
1079 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1080
1081 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1082
1083 /*
1084 * - If protect memory has failed with FFA_DENIED, means some
1085 * range of memory was in the wrong state. In such case, SPM
1086 * reverts the state of the pages that were successfully
1087 * updated.
1088 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1089 * means the platform doesn't support the protection mechanism.
1090 * That said, it still permits the page table update to go
1091 * through. The variable
1092 * `is_memory_protected` will be equal to false.
1093 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1094 * break from switch and return the error.
1095 */
1096 if (ret.func == FFA_ERROR_32) {
1097 assert(!is_memory_protected);
1098 if (ffa_error_code(ret) == FFA_DENIED &&
1099 pa_addr(last_protected_pa) != (uintptr_t)0) {
1100 CHECK(arch_memory_unprotect(
1101 pa_begin,
1102 pa_add(last_protected_pa, PAGE_SIZE)));
1103 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1104 ret = (struct ffa_value){
1105 .func = FFA_SUCCESS_32,
1106 };
1107 }
1108 }
1109 } break;
1110 default:
1111 panic("%s: invalid action to process %x\n", __func__, action);
1112 }
1113
1114 if (memory_protected != NULL) {
1115 *memory_protected = is_memory_protected;
1116 }
1117
1118 return ret;
1119}
1120
1121static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1122 paddr_t pa_begin, paddr_t pa_end,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001123 struct mpool *ppool, mm_mode_t mode,
J-Alvescf6253e2024-01-03 13:48:48 +00001124 enum ffa_map_action action)
1125{
1126 switch (action) {
1127 case MAP_ACTION_COMMIT_UNPROTECT:
1128 /*
1129 * Checking that it should succeed because SPM should be
1130 * unprotecting memory that it had protected before.
1131 */
1132 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1133 case MAP_ACTION_COMMIT:
1134 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1135 NULL);
1136 break;
1137 default:
1138 panic("%s: invalid action to process %x\n", __func__, action);
1139 }
1140}
1141
Jose Marinho09b1db82019-08-08 09:16:59 +01001142/**
J-Alves063ad832023-10-03 18:05:40 +01001143 * Helper function to revert a failed "Protect" action from the SPMC:
1144 * - `fragment_count`: should specify the number of fragments to traverse from
1145 * `fragments`. This may not be the full amount of fragments that are part of
1146 * the share_state structure.
1147 * - `fragment_constituent_counts`: array holding the amount of constituents
1148 * per fragment.
1149 * - `end`: pointer to the constituent that failed the "protect" action. It
1150 * shall be part of the last fragment, and it shall make the loop below break.
1151 */
1152static void ffa_region_group_fragments_revert_protect(
1153 struct ffa_memory_region_constituent **fragments,
1154 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1155 const struct ffa_memory_region_constituent *end)
1156{
1157 for (uint32_t i = 0; i < fragment_count; ++i) {
1158 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1159 struct ffa_memory_region_constituent *constituent =
1160 &fragments[i][j];
1161 size_t size = constituent->page_count * PAGE_SIZE;
1162 paddr_t pa_begin =
1163 pa_from_ipa(ipa_init(constituent->address));
1164 paddr_t pa_end = pa_add(pa_begin, size);
1165
Karl Meakine8937d92024-03-19 16:04:25 +00001166 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001167 __func__, pa_addr(pa_begin), size);
1168
1169 if (constituent == end) {
1170 /*
1171 * The last constituent is expected to be in the
1172 * last fragment.
1173 */
1174 assert(i == fragment_count - 1);
1175 break;
1176 }
1177
1178 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1179 }
1180 }
1181}
1182
1183/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001184 * Updates a VM's page table such that the given set of physical address ranges
1185 * are mapped in the address space at the corresponding address ranges, in the
1186 * mode provided.
1187 *
J-Alves0a83dc22023-05-05 09:50:37 +01001188 * The enum ffa_map_action determines the action taken from a call to the
1189 * function below:
1190 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1191 * mpool but no mappings will actually be updated. This function must always
1192 * be called first with action set to MAP_ACTION_CHECK to check that it will
1193 * succeed before calling ffa_region_group_identity_map with whichever one of
1194 * the remaining actions, to avoid leaving the page table in a half-updated
1195 * state.
1196 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1197 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001198 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1199 * invocation to the monitor to update the security state of the memory,
1200 * to that of the SPMC.
1201 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1202 * with a call into the monitor, to reset the security state of memory
1203 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001204 * vm_ptable_defrag should always be called after a series of page table
1205 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001206 *
J-Alvescf6253e2024-01-03 13:48:48 +00001207 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1208 * error codes:
1209 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1210 * - FFA_DENIED:
1211 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001212 * made to memory mappings.
1213 */
J-Alvescf6253e2024-01-03 13:48:48 +00001214struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001215 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001216 struct ffa_memory_region_constituent **fragments,
1217 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001218 mm_mode_t mode, struct mpool *ppool, enum ffa_map_action action,
J-Alvescf6253e2024-01-03 13:48:48 +00001219 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001220{
Andrew Walbranca808b12020-05-15 17:22:28 +01001221 uint32_t i;
1222 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001223 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001224
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001225 if (vm_locked.vm->el0_partition) {
1226 mode |= MM_MODE_USER | MM_MODE_NG;
1227 }
1228
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 /* Iterate over the memory region constituents within each fragment. */
1230 for (i = 0; i < fragment_count; ++i) {
1231 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001232 struct ffa_memory_region_constituent *constituent =
1233 &fragments[i][j];
1234 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001235 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001236 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001237 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001238 uint32_t pa_bits =
1239 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001240
1241 /*
1242 * Ensure the requested region falls into system's PA
1243 * range.
1244 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001245 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1246 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001247 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001248 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001249 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001250
J-Alvescf6253e2024-01-03 13:48:48 +00001251 if (action <= MAP_ACTION_CHECK_PROTECT) {
1252 ret = ffa_region_group_check_actions(
1253 vm_locked, pa_begin, pa_end, ppool,
1254 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001255
1256 if (ret.func == FFA_ERROR_32 &&
1257 ffa_error_code(ret) == FFA_DENIED) {
1258 if (memory_protected != NULL) {
1259 assert(!*memory_protected);
1260 }
1261
1262 ffa_region_group_fragments_revert_protect(
1263 fragments,
1264 fragment_constituent_counts,
1265 i + 1, constituent);
1266 break;
1267 }
J-Alvescf6253e2024-01-03 13:48:48 +00001268 } else if (action >= MAP_ACTION_COMMIT &&
1269 action < MAP_ACTION_MAX) {
1270 ffa_region_group_commit_actions(
1271 vm_locked, pa_begin, pa_end, ppool,
1272 mode, action);
1273 ret = (struct ffa_value){
1274 .func = FFA_SUCCESS_32};
1275 } else {
1276 panic("%s: Unknown ffa_map_action.\n",
1277 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001278 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001279 }
1280 }
1281
J-Alvescf6253e2024-01-03 13:48:48 +00001282 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001283}
1284
1285/**
1286 * Clears a region of physical memory by overwriting it with zeros. The data is
1287 * flushed from the cache so the memory has been cleared across the system.
1288 */
J-Alves7db32002021-12-14 14:44:50 +00001289static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001290 mm_mode_t extra_mode)
Jose Marinho09b1db82019-08-08 09:16:59 +01001291{
1292 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001293 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001294 * global mapping of the whole range. Such an approach will limit
1295 * the changes to stage-1 tables and will allow only local
1296 * invalidation.
1297 */
1298 bool ret;
1299 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
Karl Meakin07a69ab2025-02-07 14:53:19 +00001300 void *ptr = mm_identity_map(
1301 stage1_locked, begin, end,
1302 MM_MODE_W | (extra_mode & ffa_memory_get_other_world_mode()),
1303 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001304 size_t size = pa_difference(begin, end);
1305
1306 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001307 goto fail;
1308 }
1309
1310 memset_s(ptr, size, 0, size);
1311 arch_mm_flush_dcache(ptr, size);
1312 mm_unmap(stage1_locked, begin, end, ppool);
1313
1314 ret = true;
1315 goto out;
1316
1317fail:
1318 ret = false;
1319
1320out:
1321 mm_unlock_stage1(&stage1_locked);
1322
1323 return ret;
1324}
1325
1326/**
1327 * Clears a region of physical memory by overwriting it with zeros. The data is
1328 * flushed from the cache so the memory has been cleared across the system.
1329 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330static bool ffa_clear_memory_constituents(
Karl Meakin07a69ab2025-02-07 14:53:19 +00001331 mm_mode_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001332 struct ffa_memory_region_constituent **fragments,
1333 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1334 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001335{
1336 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001337 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001338 bool ret = false;
1339
1340 /*
1341 * Create a local pool so any freed memory can't be used by another
1342 * thread. This is to ensure each constituent that is mapped can be
1343 * unmapped again afterwards.
1344 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001345 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001346
Andrew Walbranca808b12020-05-15 17:22:28 +01001347 /* Iterate over the memory region constituents within each fragment. */
1348 for (i = 0; i < fragment_count; ++i) {
1349 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001350
J-Alves8457f932023-10-11 16:41:45 +01001351 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001352 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1353 paddr_t begin =
1354 pa_from_ipa(ipa_init(fragments[i][j].address));
1355 paddr_t end = pa_add(begin, size);
1356
J-Alves7db32002021-12-14 14:44:50 +00001357 if (!clear_memory(begin, end, &local_page_pool,
1358 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001359 /*
1360 * api_clear_memory will defrag on failure, so
1361 * no need to do it here.
1362 */
1363 goto out;
1364 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001365 }
1366 }
1367
Jose Marinho09b1db82019-08-08 09:16:59 +01001368 ret = true;
1369
1370out:
1371 mpool_fini(&local_page_pool);
1372 return ret;
1373}
1374
J-Alves5952d942022-12-22 16:03:00 +00001375static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1376 ipaddr_t in_begin, ipaddr_t in_end)
1377{
1378 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1379 ipa_addr(begin) < ipa_addr(in_end)) ||
1380 (ipa_addr(end) <= ipa_addr(in_end) &&
1381 ipa_addr(end) > ipa_addr(in_begin));
1382}
1383
1384/**
1385 * Receives a memory range and looks for overlaps with the remainder
1386 * constituents of the memory share/lend/donate operation. Assumes they are
1387 * passed in order to avoid having to loop over all the elements at each call.
1388 * The function only compares the received memory ranges with those that follow
1389 * within the same fragment, and subsequent fragments from the same operation.
1390 */
1391static bool ffa_memory_check_overlap(
1392 struct ffa_memory_region_constituent **fragments,
1393 const uint32_t *fragment_constituent_counts,
1394 const uint32_t fragment_count, const uint32_t current_fragment,
1395 const uint32_t current_constituent)
1396{
1397 uint32_t i = current_fragment;
1398 uint32_t j = current_constituent;
1399 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1400 const uint32_t current_page_count = fragments[i][j].page_count;
1401 size_t current_size = current_page_count * PAGE_SIZE;
1402 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1403
1404 if (current_size == 0 ||
1405 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001406 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1407 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001408 return false;
1409 }
1410
1411 for (; i < fragment_count; i++) {
1412 j = (i == current_fragment) ? j + 1 : 0;
1413
1414 for (; j < fragment_constituent_counts[i]; j++) {
1415 ipaddr_t begin = ipa_init(fragments[i][j].address);
1416 const uint32_t page_count = fragments[i][j].page_count;
1417 size_t size = page_count * PAGE_SIZE;
1418 ipaddr_t end = ipa_add(begin, size - 1);
1419
1420 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1421 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001422 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001423 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001424 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001425 return false;
1426 }
1427
1428 /*
1429 * Check if current ranges is within begin and end, as
1430 * well as the reverse. This should help optimize the
1431 * loop, and reduce the number of iterations.
1432 */
1433 if (is_memory_range_within(begin, end, current_begin,
1434 current_end) ||
1435 is_memory_range_within(current_begin, current_end,
1436 begin, end)) {
1437 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001438 "Overlapping memory ranges: %#lx - "
1439 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001440 ipa_addr(begin), ipa_addr(end),
1441 ipa_addr(current_begin),
1442 ipa_addr(current_end));
1443 return true;
1444 }
1445 }
1446 }
1447
1448 return false;
1449}
1450
Jose Marinho09b1db82019-08-08 09:16:59 +01001451/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001452 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001453 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001454 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001455 *
1456 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001457 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001459 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001460 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1461 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001463 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001464 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001465 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001466static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001467 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001468 struct ffa_memory_region_constituent **fragments,
1469 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001470 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001471 struct ffa_memory_region *memory_region, struct mpool *page_pool,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001472 mm_mode_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001473{
Andrew Walbranca808b12020-05-15 17:22:28 +01001474 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001475 uint32_t j;
Karl Meakin07a69ab2025-02-07 14:53:19 +00001476 mm_mode_t orig_from_mode;
1477 mm_mode_t clean_mode;
1478 mm_mode_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001479 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001480 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001481 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001482 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001483 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001484
1485 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 * Make sure constituents are properly aligned to a 64-bit boundary. If
1487 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001488 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001489 for (i = 0; i < fragment_count; ++i) {
1490 if (!is_aligned(fragments[i], 8)) {
1491 dlog_verbose("Constituents not aligned.\n");
1492 return ffa_error(FFA_INVALID_PARAMETERS);
1493 }
J-Alves8f11cde2022-12-21 16:18:22 +00001494 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1495 constituents_total_page_count +=
1496 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001497 if (ffa_memory_check_overlap(
1498 fragments, fragment_constituent_counts,
1499 fragment_count, i, j)) {
1500 return ffa_error(FFA_INVALID_PARAMETERS);
1501 }
J-Alves8f11cde2022-12-21 16:18:22 +00001502 }
1503 }
1504
1505 if (constituents_total_page_count != composite_total_page_count) {
1506 dlog_verbose(
1507 "Composite page count differs from calculated page "
1508 "count from constituents.\n");
1509 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001510 }
1511
1512 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001513 * Check if the state transition is lawful for the sender, ensure that
1514 * all constituents of a memory region being shared are at the same
1515 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001516 */
J-Alves460d36c2023-10-12 17:02:15 +01001517 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001518 from_locked, share_func, memory_region, &orig_from_mode,
1519 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001520 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001521 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001522 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001523 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001524 }
1525
Andrew Walbran37c574e2020-06-03 11:45:46 +01001526 if (orig_from_mode_ret != NULL) {
1527 *orig_from_mode_ret = orig_from_mode;
1528 }
1529
Jose Marinho09b1db82019-08-08 09:16:59 +01001530 /*
1531 * Create a local pool so any freed memory can't be used by another
1532 * thread. This is to ensure the original mapping can be restored if the
1533 * clear fails.
1534 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001535 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001536
1537 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001538 * First reserve all required memory for the new page table entries
1539 * without committing, to make sure the entire operation will succeed
1540 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001541 * Provide the map_action as populated by 'ffa_send_check_transition'.
1542 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001543 */
J-Alvescf6253e2024-01-03 13:48:48 +00001544 ret = ffa_region_group_identity_map(
1545 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001546 fragment_count, from_mode, page_pool, map_action,
1547 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001548 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001549 goto out;
1550 }
1551
1552 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001553 * Update the mapping for the sender. This won't allocate because the
1554 * transaction was already prepared above, but may free pages in the
1555 * case that a whole block is being unmapped that was previously
1556 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001557 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001558 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001559 from_locked, fragments, fragment_constituent_counts,
1560 fragment_count, from_mode, &local_page_pool,
1561 MAP_ACTION_COMMIT, NULL)
1562 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001563
J-Alves460d36c2023-10-12 17:02:15 +01001564 /*
1565 * If memory has been protected, it is now part of the secure PAS
1566 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1567 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1568 * SPM's S1 translation.
1569 * In case memory hasn't been protected, and it is in the non-secure
1570 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1571 * perform a non-secure memory access. In such case `clean_mode` takes
1572 * the same mode as `orig_from_mode`.
1573 */
Karl Meakin117c8082024-12-04 16:03:28 +00001574 clean_mode =
1575 (memory_protected != NULL && *memory_protected)
1576 ? orig_from_mode & ~ffa_memory_get_other_world_mode()
1577 : orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001578
Jose Marinho09b1db82019-08-08 09:16:59 +01001579 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001580 if (clear && !ffa_clear_memory_constituents(
1581 clean_mode, fragments, fragment_constituent_counts,
1582 fragment_count, page_pool)) {
1583 map_action = (memory_protected != NULL && *memory_protected)
1584 ? MAP_ACTION_COMMIT_UNPROTECT
1585 : MAP_ACTION_COMMIT;
1586
Jose Marinho09b1db82019-08-08 09:16:59 +01001587 /*
1588 * On failure, roll back by returning memory to the sender. This
1589 * may allocate pages which were previously freed into
1590 * `local_page_pool` by the call above, but will never allocate
1591 * more pages than that so can never fail.
1592 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001593 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001594 from_locked, fragments,
1595 fragment_constituent_counts, fragment_count,
1596 orig_from_mode, &local_page_pool,
1597 MAP_ACTION_COMMIT, NULL)
1598 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001600 goto out;
1601 }
1602
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001603 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001604
1605out:
1606 mpool_fini(&local_page_pool);
1607
1608 /*
1609 * Tidy up the page table by reclaiming failed mappings (if there was an
1610 * error) or merging entries into blocks where possible (on success).
1611 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001612 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001613
1614 return ret;
1615}
1616
1617/**
1618 * Validates and maps memory shared from one VM to another.
1619 *
1620 * This function requires the calling context to hold the <to> lock.
1621 *
1622 * Returns:
1623 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001624 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001625 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001626 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001627 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001628 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001629 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001630struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001631 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001632 struct ffa_memory_region_constituent **fragments,
1633 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001634 mm_mode_t sender_orig_mode, uint32_t share_func, bool clear,
1635 struct mpool *page_pool, mm_mode_t *response_mode,
1636 bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001637{
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 uint32_t i;
Karl Meakin07a69ab2025-02-07 14:53:19 +00001639 mm_mode_t to_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001641 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001642 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001643
1644 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 * Make sure constituents are properly aligned to a 64-bit boundary. If
1646 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001647 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001648 for (i = 0; i < fragment_count; ++i) {
1649 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001650 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001651 return ffa_error(FFA_INVALID_PARAMETERS);
1652 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653 }
1654
1655 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001656 * Ensure the sender has write permissions if the memory needs to be
1657 * cleared.
1658 */
1659 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1660 dlog_verbose(
1661 "Cannot zero memory when the sender does not have "
1662 "write access\n");
1663 return ffa_error(FFA_DENIED);
1664 }
1665
1666 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001667 * Check if the state transition is lawful for the recipient, and ensure
1668 * that all constituents of the memory region being retrieved are at the
1669 * same state.
1670 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001671 ret = ffa_retrieve_check_transition(
1672 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001673 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1674 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001675
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001676 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001677 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001678 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001679 }
1680
1681 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001682 * Create a local pool so any freed memory can't be used by
1683 * another thread. This is to ensure the original mapping can be
1684 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001685 */
1686 mpool_init_with_fallback(&local_page_pool, page_pool);
1687
1688 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001689 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1690 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001691 */
J-Alves69cdfd92024-04-26 11:40:59 +01001692 if (map_action != MAP_ACTION_NONE) {
1693 /*
1694 * First reserve all required memory for the new page table
1695 * entries in the recipient page tables without committing, to
1696 * make sure the entire operation will succeed without
1697 * exhausting the page pool.
1698 */
1699 ret = ffa_region_group_identity_map(
1700 to_locked, fragments, fragment_constituent_counts,
1701 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1702 NULL);
1703 if (ret.func == FFA_ERROR_32) {
1704 /* TODO: partial defrag of failed range. */
1705 goto out;
1706 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 }
1708
1709 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001710 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001711 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1712 fragment_constituent_counts,
1713 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001714 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001715 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001716 goto out;
1717 }
1718
J-Alves69cdfd92024-04-26 11:40:59 +01001719 if (map_action != MAP_ACTION_NONE) {
1720 /*
1721 * Complete the transfer by mapping the memory into the
1722 * recipient. This won't allocate because the transaction was
1723 * already prepared above, so it doesn't need to use the
1724 * `local_page_pool`.
1725 */
1726 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1727 fragment_constituent_counts,
1728 fragment_count, to_mode,
1729 page_pool, map_action, NULL)
1730 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001731
J-Alves69cdfd92024-04-26 11:40:59 +01001732 /*
1733 * Return the mode used in mapping the memory in retriever's PT.
1734 */
1735 if (response_mode != NULL) {
1736 *response_mode = to_mode;
1737 }
J-Alves460d36c2023-10-12 17:02:15 +01001738 }
1739
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001740 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001741
1742out:
1743 mpool_fini(&local_page_pool);
1744
1745 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001746 * Tidy up the page table by reclaiming failed mappings (if there was an
1747 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001748 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001749 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001750
1751 return ret;
1752}
1753
Andrew Walbran996d1d12020-05-27 14:08:43 +01001754static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001755 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001756 struct ffa_memory_region_constituent **fragments,
1757 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001758 mm_mode_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001759{
Karl Meakin07a69ab2025-02-07 14:53:19 +00001760 mm_mode_t orig_from_mode;
1761 mm_mode_t clearing_mode;
1762 mm_mode_t from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001763 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001764 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001765 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001766
Andrew Walbranca808b12020-05-15 17:22:28 +01001767 ret = ffa_relinquish_check_transition(
1768 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001769 fragment_constituent_counts, fragment_count, &from_mode,
1770 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001771 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001772 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001773 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001774 }
1775
1776 /*
1777 * Create a local pool so any freed memory can't be used by another
1778 * thread. This is to ensure the original mapping can be restored if the
1779 * clear fails.
1780 */
1781 mpool_init_with_fallback(&local_page_pool, page_pool);
1782
J-Alves69cdfd92024-04-26 11:40:59 +01001783 if (map_action != MAP_ACTION_NONE) {
1784 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001785
J-Alves69cdfd92024-04-26 11:40:59 +01001786 /*
1787 * First reserve all required memory for the new page table
1788 * entries without committing, to make sure the entire operation
1789 * will succeed without exhausting the page pool.
1790 */
1791 ret = ffa_region_group_identity_map(
1792 from_locked, fragments, fragment_constituent_counts,
1793 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1794 NULL);
1795 if (ret.func == FFA_ERROR_32) {
1796 goto out;
1797 }
1798
1799 /*
1800 * Update the mapping for the sender. This won't allocate
1801 * because the transaction was already prepared above, but may
1802 * free pages in the case that a whole block is being unmapped
1803 * that was previously partially mapped.
1804 */
1805 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1806 fragment_constituent_counts,
1807 fragment_count, from_mode,
1808 &local_page_pool,
1809 MAP_ACTION_COMMIT, NULL)
1810 .func == FFA_SUCCESS_32);
1811 } else {
1812 /*
1813 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1814 * were not updated on retrieve/relinquish. These were updating
1815 * only the `share_state` structures. As such, use the sender's
1816 * original mode.
1817 */
1818 clearing_mode = sender_orig_mode;
1819 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001820
1821 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001822 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001823 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001824 fragment_constituent_counts,
1825 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001826 if (map_action != MAP_ACTION_NONE) {
1827 /*
1828 * On failure, roll back by returning memory to the
1829 * sender. This may allocate pages which were previously
1830 * freed into `local_page_pool` by the call above, but
1831 * will never allocate more pages than that so can never
1832 * fail.
1833 */
1834 CHECK(ffa_region_group_identity_map(
1835 from_locked, fragments,
1836 fragment_constituent_counts,
1837 fragment_count, orig_from_mode,
1838 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1839 .func == FFA_SUCCESS_32);
1840 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001841 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001842 goto out;
1843 }
1844
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001845 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001846
1847out:
1848 mpool_fini(&local_page_pool);
1849
1850 /*
1851 * Tidy up the page table by reclaiming failed mappings (if there was an
1852 * error) or merging entries into blocks where possible (on success).
1853 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001854 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001855
1856 return ret;
1857}
1858
1859/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001860 * Complete a memory sending operation by checking that it is valid, updating
1861 * the sender page table, and then either marking the share state as having
1862 * completed sending (on success) or freeing it (on failure).
1863 *
1864 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1865 */
J-Alvesfdd29272022-07-19 13:16:31 +01001866struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001867 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001868 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
Karl Meakin07a69ab2025-02-07 14:53:19 +00001869 mm_mode_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001870{
1871 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001872 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001873 struct ffa_value ret;
1874
1875 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001876 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001877 assert(memory_region != NULL);
1878 composite = ffa_memory_region_get_composite(memory_region, 0);
1879 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001880
1881 /* Check that state is valid in sender page table and update. */
1882 ret = ffa_send_check_update(
1883 from_locked, share_state->fragments,
1884 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001885 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001886 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001887 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001888 if (ret.func != FFA_SUCCESS_32) {
1889 /*
1890 * Free share state, it failed to send so it can't be retrieved.
1891 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001892 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1893 __func__, ffa_func_name(ret.func),
1894 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001895 share_state_free(share_states, share_state, page_pool);
1896 return ret;
1897 }
1898
1899 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001900 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001901
J-Alvesee68c542020-10-29 17:48:20 +00001902 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001903}
1904
1905/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001906 * Check that the memory attributes match Hafnium expectations.
1907 * Cacheability:
1908 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1909 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1910 *
1911 * Shareability:
1912 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001913 */
1914static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001915 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001916{
1917 enum ffa_memory_type memory_type;
1918 enum ffa_memory_cacheability cacheability;
1919 enum ffa_memory_shareability shareability;
1920
Karl Meakin84710f32023-10-12 15:14:49 +01001921 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001922 cacheability = attributes.cacheability;
1923 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1924 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1925 dlog_verbose(
1926 "Normal Memory: Invalid cacheability %s, "
1927 "expected %s.\n",
1928 ffa_memory_cacheability_name(cacheability),
1929 ffa_memory_cacheability_name(
1930 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001931 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001932 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001933 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1934 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1935 dlog_verbose(
1936 "Device Memory: Invalid cacheability %s, "
1937 "expected %s.\n",
1938 ffa_device_memory_cacheability_name(cacheability),
1939 ffa_device_memory_cacheability_name(
1940 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001941 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001942 }
1943
Karl Meakin84710f32023-10-12 15:14:49 +01001944 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001945 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001946 dlog_verbose("Invalid shareability %s, expected %s.\n",
1947 ffa_memory_shareability_name(shareability),
1948 ffa_memory_shareability_name(
1949 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001950 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001951 }
1952
1953 return (struct ffa_value){.func = FFA_SUCCESS_32};
1954}
1955
1956/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001957 * Check that the given `memory_region` represents a valid memory send request
1958 * of the given `share_func` type, return the clear flag and permissions via the
1959 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001960 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001961 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001962 * not.
1963 */
J-Alves66652252022-07-06 09:49:51 +01001964struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001965 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1966 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001967 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001968{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001969 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001970 struct ffa_memory_access *receiver =
1971 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001972 uint64_t receivers_end;
1973 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001974 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001975 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001976 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001977 enum ffa_data_access data_access;
1978 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001979 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001980 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001981 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001982 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001983 memory_region->receivers_offset +
1984 memory_region->memory_access_desc_size +
1985 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001986
1987 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001988 dlog_verbose("Fragment length %u too short (min %zu).\n",
1989 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001990 return ffa_error(FFA_INVALID_PARAMETERS);
1991 }
1992
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001993 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1994 "struct ffa_memory_region_constituent must be 16 bytes");
1995 if (!is_aligned(fragment_length,
1996 sizeof(struct ffa_memory_region_constituent)) ||
1997 !is_aligned(memory_share_length,
1998 sizeof(struct ffa_memory_region_constituent))) {
1999 dlog_verbose(
2000 "Fragment length %u or total length %u"
2001 " is not 16-byte aligned.\n",
2002 fragment_length, memory_share_length);
2003 return ffa_error(FFA_INVALID_PARAMETERS);
2004 }
2005
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002006 if (fragment_length > memory_share_length) {
2007 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002008 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002009 (size_t)fragment_length, (size_t)memory_share_length);
2010 return ffa_error(FFA_INVALID_PARAMETERS);
2011 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002012
J-Alves95df0ef2022-12-07 10:09:48 +00002013 /* The sender must match the caller. */
2014 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2015 vm_id_is_current_world(memory_region->sender)) ||
2016 (vm_id_is_current_world(from_locked.vm->id) &&
2017 memory_region->sender != from_locked.vm->id)) {
2018 dlog_verbose("Invalid memory sender ID.\n");
2019 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002020 }
2021
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002022 if (memory_region->receiver_count <= 0) {
2023 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002024 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002025 }
2026
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002027 /*
2028 * Ensure that the composite header is within the memory bounds and
2029 * doesn't overlap the first part of the message. Cast to uint64_t
2030 * to prevent overflow.
2031 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002032 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002033 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002034 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002035 min_length = receivers_end +
2036 sizeof(struct ffa_composite_memory_region) +
2037 sizeof(struct ffa_memory_region_constituent);
2038 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002039 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002040 (size_t)memory_share_length, (size_t)min_length);
2041 return ffa_error(FFA_INVALID_PARAMETERS);
2042 }
2043
2044 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002045 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002046
2047 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002048 * Check that the composite memory region descriptor is after the access
2049 * descriptors, is at least 16-byte aligned, and fits in the first
2050 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002051 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002052 if ((composite_memory_region_offset < receivers_end) ||
2053 (composite_memory_region_offset % 16 != 0) ||
2054 (composite_memory_region_offset >
2055 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2056 dlog_verbose(
2057 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002058 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002059 (size_t)composite_memory_region_offset);
2060 return ffa_error(FFA_INVALID_PARAMETERS);
2061 }
2062
2063 /*
2064 * Compute the start of the constituent regions. Already checked
2065 * to be not more than fragment_length and thus not more than
2066 * memory_share_length.
2067 */
2068 constituents_start = composite_memory_region_offset +
2069 sizeof(struct ffa_composite_memory_region);
2070 constituents_length = memory_share_length - constituents_start;
2071
2072 /*
2073 * Check that the number of constituents is consistent with the length
2074 * of the constituent region.
2075 */
2076 composite = ffa_memory_region_get_composite(memory_region, 0);
2077 if ((constituents_length %
2078 sizeof(struct ffa_memory_region_constituent) !=
2079 0) ||
2080 ((constituents_length /
2081 sizeof(struct ffa_memory_region_constituent)) !=
2082 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002083 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002084 (size_t)memory_share_length,
2085 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002086 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002087 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002088 if (fragment_length < memory_share_length &&
2089 fragment_length < HF_MAILBOX_SIZE) {
2090 dlog_warning(
2091 "Initial fragment length %d smaller than mailbox "
2092 "size.\n",
2093 fragment_length);
2094 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002095
Andrew Walbrana65a1322020-04-06 19:32:32 +01002096 /*
2097 * Clear is not allowed for memory sharing, as the sender still has
2098 * access to the memory.
2099 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002100 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002101 (share_func == FFA_MEM_SHARE_32 ||
2102 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002103 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002104 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002105 }
2106
2107 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002108 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002109 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002110 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002111 }
2112
J-Alves363f5722022-04-25 17:37:37 +01002113 /* Check that the permissions are valid, for each specified receiver. */
2114 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002115 struct ffa_memory_region_attributes receiver_permissions;
2116
2117 receiver = ffa_memory_region_get_receiver(memory_region, i);
2118 assert(receiver != NULL);
2119 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002120 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002121 receiver_permissions.permissions;
2122 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002123
2124 if (memory_region->sender == receiver_id) {
2125 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002126 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002127 }
Federico Recanati85090c42021-12-15 13:17:54 +01002128
J-Alves363f5722022-04-25 17:37:37 +01002129 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2130 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002131 struct ffa_memory_access *other_receiver =
2132 ffa_memory_region_get_receiver(memory_region,
2133 j);
2134 assert(other_receiver != NULL);
2135
J-Alves363f5722022-04-25 17:37:37 +01002136 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002137 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002138 dlog_verbose(
2139 "Repeated receiver(%x) in memory send "
2140 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002141 other_receiver->receiver_permissions
2142 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002143 return ffa_error(FFA_INVALID_PARAMETERS);
2144 }
2145 }
2146
2147 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002148 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002149 dlog_verbose(
2150 "All ffa_memory_access should point to the "
2151 "same composite memory region offset.\n");
2152 return ffa_error(FFA_INVALID_PARAMETERS);
2153 }
2154
Karl Meakin84710f32023-10-12 15:14:49 +01002155 data_access = permissions.data_access;
2156 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002157 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2158 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2159 dlog_verbose(
2160 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002161 "(data_access = %s, instruction_access = %s)\n",
2162 ffa_data_access_name(data_access),
2163 ffa_instruction_access_name(
2164 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002165 return ffa_error(FFA_INVALID_PARAMETERS);
2166 }
2167 if (instruction_access !=
2168 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2169 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002170 "Invalid instruction access permissions %s "
2171 "for sending memory, expected %s.\n",
2172 ffa_instruction_access_name(instruction_access),
2173 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002174 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002175 return ffa_error(FFA_INVALID_PARAMETERS);
2176 }
J-Alves95fbb312024-03-20 15:19:16 +00002177 if (share_func == FFA_MEM_SHARE_32 ||
2178 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002179 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2180 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002181 "Invalid data access permissions %s "
2182 "for sharing memory, expected %s.\n",
2183 ffa_data_access_name(data_access),
2184 ffa_data_access_name(
2185 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002188 /*
2189 * According to section 10.10.3 of the FF-A v1.1 EAC0
2190 * spec, NX is required for share operations (but must
2191 * not be specified by the sender) so set it in the
2192 * copy that we store, ready to be returned to the
2193 * retriever.
2194 */
2195 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002196 permissions.instruction_access =
2197 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002198 receiver_permissions.permissions = permissions;
2199 }
J-Alves363f5722022-04-25 17:37:37 +01002200 }
J-Alves95fbb312024-03-20 15:19:16 +00002201 if ((share_func == FFA_MEM_LEND_32 ||
2202 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002203 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2204 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002205 "Invalid data access permissions %s for "
2206 "lending memory, expected %s.\n",
2207 ffa_data_access_name(data_access),
2208 ffa_data_access_name(
2209 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002210 return ffa_error(FFA_INVALID_PARAMETERS);
2211 }
2212
J-Alves95fbb312024-03-20 15:19:16 +00002213 if ((share_func == FFA_MEM_DONATE_32 ||
2214 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002215 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2216 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002217 "Invalid data access permissions %s for "
2218 "donating memory, expected %s.\n",
2219 ffa_data_access_name(data_access),
2220 ffa_data_access_name(
2221 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002222 return ffa_error(FFA_INVALID_PARAMETERS);
2223 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002224 }
2225
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002226 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002227 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002228 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2229 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002230 "Invalid security state %s for memory share operation, "
2231 "expected %s.\n",
2232 ffa_memory_security_name(security_state),
2233 ffa_memory_security_name(
2234 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002235 return ffa_error(FFA_INVALID_PARAMETERS);
2236 }
2237
Federico Recanatid937f5e2021-12-20 17:38:23 +01002238 /*
J-Alves807794e2022-06-16 13:42:47 +01002239 * If a memory donate or lend with single borrower, the memory type
2240 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002241 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002242 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002243 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002244 share_func == FFA_MEM_DONATE_64 ||
2245 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002246 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002247 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002248 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002249 "Invalid memory type %s for memory share "
2250 "operation, expected %s.\n",
2251 ffa_memory_type_name(type),
2252 ffa_memory_type_name(
2253 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002254 return ffa_error(FFA_INVALID_PARAMETERS);
2255 }
2256 } else {
2257 /*
2258 * Check that sender's memory attributes match Hafnium
2259 * expectations: Normal Memory, Inner shareable, Write-Back
2260 * Read-Allocate Write-Allocate Cacheable.
2261 */
2262 ret = ffa_memory_attributes_validate(memory_region->attributes);
2263 if (ret.func != FFA_SUCCESS_32) {
2264 return ret;
2265 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002266 }
2267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002268 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002269}
2270
2271/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002272 * Gets the share state for continuing an operation to donate, lend or share
2273 * memory, and checks that it is a valid request.
2274 *
2275 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2276 * not.
2277 */
J-Alvesfdd29272022-07-19 13:16:31 +01002278struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002279 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002280 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002281 struct mpool *page_pool)
2282{
2283 struct ffa_memory_share_state *share_state;
2284 struct ffa_memory_region *memory_region;
2285
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002286 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002287
2288 /*
2289 * Look up the share state by handle and make sure that the VM ID
2290 * matches.
2291 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002292 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002293 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002294 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002295 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002296 handle);
2297 return ffa_error(FFA_INVALID_PARAMETERS);
2298 }
2299 memory_region = share_state->memory_region;
2300
J-Alvesfdd29272022-07-19 13:16:31 +01002301 if (vm_id_is_current_world(from_vm_id) &&
2302 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002303 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2304 return ffa_error(FFA_INVALID_PARAMETERS);
2305 }
2306
2307 if (share_state->sending_complete) {
2308 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002309 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002310 handle);
2311 return ffa_error(FFA_INVALID_PARAMETERS);
2312 }
2313
2314 if (share_state->fragment_count == MAX_FRAGMENTS) {
2315 /*
2316 * Log a warning as this is a sign that MAX_FRAGMENTS should
2317 * probably be increased.
2318 */
2319 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002320 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002321 "only %d supported.\n",
2322 handle, MAX_FRAGMENTS);
2323 /* Free share state, as it's not possible to complete it. */
2324 share_state_free(share_states, share_state, page_pool);
2325 return ffa_error(FFA_NO_MEMORY);
2326 }
2327
2328 *share_state_ret = share_state;
2329
2330 return (struct ffa_value){.func = FFA_SUCCESS_32};
2331}
2332
2333/**
J-Alves95df0ef2022-12-07 10:09:48 +00002334 * Checks if there is at least one receiver from the other world.
2335 */
J-Alvesfdd29272022-07-19 13:16:31 +01002336bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002337 struct ffa_memory_region *memory_region)
2338{
2339 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002340 struct ffa_memory_access *receiver =
2341 ffa_memory_region_get_receiver(memory_region, i);
2342 assert(receiver != NULL);
2343 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2344
2345 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002346 return true;
2347 }
2348 }
2349 return false;
2350}
2351
2352/**
J-Alves9da280b2022-12-21 14:55:39 +00002353 * Validates a call to donate, lend or share memory in which Hafnium is the
2354 * designated allocator of the memory handle. In practice, this also means
2355 * Hafnium is responsible for managing the state structures for the transaction.
2356 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2357 * sender is an SP or there is at least one borrower that is an SP.
2358 * If Hafnium is the hypervisor, it should allocate the memory handle when
2359 * operation involves only NWd VMs.
2360 *
2361 * If validation goes well, Hafnium updates the stage-2 page tables of the
2362 * sender. Validation consists of checking if the message length and number of
2363 * memory region constituents match, and if the transition is valid for the
2364 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002365 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002366 * Assumes that the caller has already found and locked the sender VM and copied
2367 * the memory region descriptor from the sender's TX buffer to a freshly
2368 * allocated page from Hafnium's internal pool. The caller must have also
2369 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002370 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002371 * This function takes ownership of the `memory_region` passed in and will free
2372 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002373 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002374struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002375 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002376 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002377 uint32_t fragment_length, uint32_t share_func,
2378 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002379{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002380 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002381 struct share_states_locked share_states;
2382 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002383
2384 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002385 * If there is an error validating the `memory_region` then we need to
2386 * free it because we own it but we won't be storing it in a share state
2387 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002388 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002389 ret = ffa_memory_send_validate(from_locked, memory_region,
2390 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002391 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002392 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002393 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002394 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002395 }
2396
Andrew Walbrana65a1322020-04-06 19:32:32 +01002397 /* Set flag for share function, ready to be retrieved later. */
2398 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002399 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002400 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002401 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002402 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002403 break;
J-Alves95fbb312024-03-20 15:19:16 +00002404 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002405 case FFA_MEM_LEND_32:
2406 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002407 break;
J-Alves95fbb312024-03-20 15:19:16 +00002408 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002409 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002410 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002411 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002412 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002413 default:
2414 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2415 ffa_func_name(share_func));
2416 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002417 }
2418
Andrew Walbranca808b12020-05-15 17:22:28 +01002419 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002420 /*
2421 * Allocate a share state before updating the page table. Otherwise if
2422 * updating the page table succeeded but allocating the share state
2423 * failed then it would leave the memory in a state where nobody could
2424 * get it back.
2425 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002426 share_state = allocate_share_state(share_states, share_func,
2427 memory_region, fragment_length,
2428 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002429 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002430 dlog_verbose("Failed to allocate share state.\n");
2431 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002432 ret = ffa_error(FFA_NO_MEMORY);
2433 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002434 }
2435
Andrew Walbranca808b12020-05-15 17:22:28 +01002436 if (fragment_length == memory_share_length) {
2437 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002438 ret = ffa_memory_send_complete(
2439 from_locked, share_states, share_state, page_pool,
2440 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002441 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002442 /*
2443 * Use sender ID from 'memory_region' assuming
2444 * that at this point it has been validated:
2445 * - MBZ at virtual FF-A instance.
2446 */
J-Alves19e20cf2023-08-02 12:48:55 +01002447 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002448 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2449 ? memory_region->sender
2450 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002451 ret = (struct ffa_value){
2452 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002453 .arg1 = (uint32_t)memory_region->handle,
2454 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002455 .arg3 = fragment_length,
2456 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002457 }
2458
2459out:
2460 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002461 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002462 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002463}
2464
2465/**
J-Alves8505a8a2022-06-15 18:10:18 +01002466 * Continues an operation to donate, lend or share memory to a VM from current
2467 * world. If this is the last fragment then checks that the transition is valid
2468 * for the type of memory sending operation and updates the stage-2 page tables
2469 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002470 *
2471 * Assumes that the caller has already found and locked the sender VM and copied
2472 * the memory region descriptor from the sender's TX buffer to a freshly
2473 * allocated page from Hafnium's internal pool.
2474 *
2475 * This function takes ownership of the `fragment` passed in; it must not be
2476 * freed by the caller.
2477 */
2478struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2479 void *fragment,
2480 uint32_t fragment_length,
2481 ffa_memory_handle_t handle,
2482 struct mpool *page_pool)
2483{
2484 struct share_states_locked share_states = share_states_lock();
2485 struct ffa_memory_share_state *share_state;
2486 struct ffa_value ret;
2487 struct ffa_memory_region *memory_region;
2488
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002489 CHECK(is_aligned(fragment,
2490 alignof(struct ffa_memory_region_constituent)));
2491 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2492 0) {
2493 dlog_verbose("Fragment length %u misaligned.\n",
2494 fragment_length);
2495 ret = ffa_error(FFA_INVALID_PARAMETERS);
2496 goto out_free_fragment;
2497 }
2498
Andrew Walbranca808b12020-05-15 17:22:28 +01002499 ret = ffa_memory_send_continue_validate(share_states, handle,
2500 &share_state,
2501 from_locked.vm->id, page_pool);
2502 if (ret.func != FFA_SUCCESS_32) {
2503 goto out_free_fragment;
2504 }
2505 memory_region = share_state->memory_region;
2506
J-Alves95df0ef2022-12-07 10:09:48 +00002507 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002508 dlog_error(
2509 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002510 "other world. This should never happen, and indicates "
2511 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002512 "EL3 code.\n");
2513 ret = ffa_error(FFA_INVALID_PARAMETERS);
2514 goto out_free_fragment;
2515 }
2516
2517 /* Add this fragment. */
2518 share_state->fragments[share_state->fragment_count] = fragment;
2519 share_state->fragment_constituent_counts[share_state->fragment_count] =
2520 fragment_length / sizeof(struct ffa_memory_region_constituent);
2521 share_state->fragment_count++;
2522
2523 /* Check whether the memory send operation is now ready to complete. */
2524 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002525 ret = ffa_memory_send_complete(
2526 from_locked, share_states, share_state, page_pool,
2527 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002528 } else {
2529 ret = (struct ffa_value){
2530 .func = FFA_MEM_FRAG_RX_32,
2531 .arg1 = (uint32_t)handle,
2532 .arg2 = (uint32_t)(handle >> 32),
2533 .arg3 = share_state_next_fragment_offset(share_states,
2534 share_state)};
2535 }
2536 goto out;
2537
2538out_free_fragment:
2539 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002540
2541out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002542 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002543 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002544}
2545
Andrew Walbranca808b12020-05-15 17:22:28 +01002546/** Clean up after the receiver has finished retrieving a memory region. */
2547static void ffa_memory_retrieve_complete(
2548 struct share_states_locked share_states,
2549 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2550{
J-Alves95fbb312024-03-20 15:19:16 +00002551 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2552 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002553 /*
2554 * Memory that has been donated can't be relinquished,
2555 * so no need to keep the share state around.
2556 */
2557 share_state_free(share_states, share_state, page_pool);
2558 dlog_verbose("Freed share state for donate.\n");
2559 }
2560}
2561
J-Alves2d8457f2022-10-05 11:06:41 +01002562/**
2563 * Initialises the given memory region descriptor to be used for an
2564 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2565 * fragment.
2566 * The memory region descriptor is initialized according to retriever's
2567 * FF-A version.
2568 *
2569 * Returns true on success, or false if the given constituents won't all fit in
2570 * the first fragment.
2571 */
2572static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002573 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002574 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002575 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002576 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002577 struct ffa_memory_access *receivers, size_t receiver_count,
2578 uint32_t memory_access_desc_size, uint32_t page_count,
2579 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002580 const struct ffa_memory_region_constituent constituents[],
2581 uint32_t fragment_constituent_count, uint32_t *total_length,
2582 uint32_t *fragment_length)
2583{
2584 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002585 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002586 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002587 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002588
2589 assert(response != NULL);
2590
Karl Meakin0e617d92024-04-05 12:55:22 +01002591 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002592 struct ffa_memory_region_v1_0 *retrieve_response =
2593 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002594 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002595
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002596 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2597 attributes, flags, handle, 0,
2598 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002599
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002600 receiver = (struct ffa_memory_access_v1_0 *)
2601 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002602 receiver_count = retrieve_response->receiver_count;
2603
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002604 for (uint32_t i = 0; i < receiver_count; i++) {
2605 ffa_id_t receiver_id =
2606 receivers[i].receiver_permissions.receiver;
2607 ffa_memory_receiver_flags_t recv_flags =
2608 receivers[i].receiver_permissions.flags;
2609
2610 /*
2611 * Initialized here as in memory retrieve responses we
2612 * currently expect one borrower to be specified.
2613 */
2614 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002615 receiver, receiver_id, permissions.data_access,
2616 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002617 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002618
2619 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002620 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002621 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2622 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002623
2624 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2625 retrieve_response, 0);
2626 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002627 struct ffa_memory_region *retrieve_response =
2628 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002629 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002630
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002631 ffa_memory_region_init_header(
2632 retrieve_response, sender, attributes, flags, handle, 0,
2633 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002634
2635 /*
2636 * Note that `sizeof(struct_ffa_memory_region)` and
2637 * `sizeof(struct ffa_memory_access)` must both be multiples of
2638 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2639 * guaranteed that the offset we calculate here is aligned to a
2640 * 64-bit boundary and so 64-bit values can be copied without
2641 * alignment faults.
2642 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002643 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002644 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002645 (uint32_t)(receiver_count *
2646 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002647
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002648 retrieve_response_receivers =
2649 ffa_memory_region_get_receiver(retrieve_response, 0);
2650 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002651
2652 /*
2653 * Initialized here as in memory retrieve responses we currently
2654 * expect one borrower to be specified.
2655 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002656 memcpy_s(retrieve_response_receivers,
2657 sizeof(struct ffa_memory_access) * receiver_count,
2658 receivers,
2659 sizeof(struct ffa_memory_access) * receiver_count);
2660
2661 retrieve_response_receivers->composite_memory_region_offset =
2662 composite_offset;
2663
J-Alves2d8457f2022-10-05 11:06:41 +01002664 composite_memory_region =
2665 ffa_memory_region_get_composite(retrieve_response, 0);
2666 }
2667
J-Alves2d8457f2022-10-05 11:06:41 +01002668 assert(composite_memory_region != NULL);
2669
J-Alves2d8457f2022-10-05 11:06:41 +01002670 composite_memory_region->page_count = page_count;
2671 composite_memory_region->constituent_count = total_constituent_count;
2672 composite_memory_region->reserved_0 = 0;
2673
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002674 constituents_offset =
2675 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002676 if (constituents_offset +
2677 fragment_constituent_count *
2678 sizeof(struct ffa_memory_region_constituent) >
2679 response_max_size) {
2680 return false;
2681 }
2682
2683 for (i = 0; i < fragment_constituent_count; ++i) {
2684 composite_memory_region->constituents[i] = constituents[i];
2685 }
2686
2687 if (total_length != NULL) {
2688 *total_length =
2689 constituents_offset +
2690 composite_memory_region->constituent_count *
2691 sizeof(struct ffa_memory_region_constituent);
2692 }
2693 if (fragment_length != NULL) {
2694 *fragment_length =
2695 constituents_offset +
2696 fragment_constituent_count *
2697 sizeof(struct ffa_memory_region_constituent);
2698 }
2699
2700 return true;
2701}
2702
J-Alves96de29f2022-04-26 16:05:24 +01002703/**
2704 * Validates the retrieved permissions against those specified by the lender
2705 * of memory share operation. Optionally can help set the permissions to be used
2706 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002707 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2708 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2709 * specification for each ABI.
2710 * - FFA_DENIED -> if the permissions specified by the retriever are not
2711 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002712 */
J-Alvesdcad8992023-09-15 14:10:35 +01002713static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2714 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002715 enum ffa_data_access requested_data_access,
2716 enum ffa_instruction_access sent_instruction_access,
2717 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002718 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002719{
2720 switch (sent_data_access) {
2721 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2722 case FFA_DATA_ACCESS_RW:
2723 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2724 requested_data_access == FFA_DATA_ACCESS_RW) {
2725 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002726 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002727 }
2728 break;
2729 }
2730 /* Intentional fall-through. */
2731 case FFA_DATA_ACCESS_RO:
2732 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2733 requested_data_access == FFA_DATA_ACCESS_RO) {
2734 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002735 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002736 }
2737 break;
2738 }
2739 dlog_verbose(
2740 "Invalid data access requested; sender specified "
2741 "permissions %#x but receiver requested %#x.\n",
2742 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002743 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002744 case FFA_DATA_ACCESS_RESERVED:
2745 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2746 "checked before this point.");
2747 }
2748
J-Alvesdcad8992023-09-15 14:10:35 +01002749 /*
2750 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2751 * or FFA_MEMORY_DONATE the retriever should have specifed the
2752 * instruction permissions it wishes to receive.
2753 */
2754 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002755 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002756 case FFA_MEM_SHARE_32:
2757 if (requested_instruction_access !=
2758 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2759 dlog_verbose(
2760 "%s: for share instruction permissions must "
2761 "NOT be specified.\n",
2762 __func__);
2763 return ffa_error(FFA_INVALID_PARAMETERS);
2764 }
2765 break;
J-Alves95fbb312024-03-20 15:19:16 +00002766 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002767 case FFA_MEM_LEND_32:
2768 /*
2769 * For operations with multiple borrowers only permit XN
2770 * permissions, and both Sender and borrower should have used
2771 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2772 */
2773 if (multiple_borrowers) {
2774 if (requested_instruction_access !=
2775 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2776 dlog_verbose(
2777 "%s: lend/share/donate with multiple "
2778 "borrowers "
2779 "instruction permissions must NOT be "
2780 "specified.\n",
2781 __func__);
2782 return ffa_error(FFA_INVALID_PARAMETERS);
2783 }
2784 break;
2785 }
2786 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002787 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002788 case FFA_MEM_DONATE_32:
2789 if (!multiple_borrowers &&
2790 requested_instruction_access ==
2791 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2792 dlog_verbose(
2793 "%s: for lend/donate with single borrower "
2794 "instruction permissions must be speficified "
2795 "by borrower\n",
2796 __func__);
2797 return ffa_error(FFA_INVALID_PARAMETERS);
2798 }
2799 break;
2800 default:
2801 panic("%s: Wrong func id provided.\n", __func__);
2802 }
2803
J-Alves96de29f2022-04-26 16:05:24 +01002804 switch (sent_instruction_access) {
2805 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2806 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002807 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002808 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002809 permissions->instruction_access =
2810 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002811 }
2812 break;
2813 }
J-Alvesdcad8992023-09-15 14:10:35 +01002814 /*
2815 * Fall through if requested permissions are less
2816 * permissive than those provided by the sender.
2817 */
J-Alves96de29f2022-04-26 16:05:24 +01002818 case FFA_INSTRUCTION_ACCESS_NX:
2819 if (requested_instruction_access ==
2820 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2821 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2822 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002823 permissions->instruction_access =
2824 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002825 }
2826 break;
2827 }
2828 dlog_verbose(
2829 "Invalid instruction access requested; sender "
2830 "specified permissions %#x but receiver requested "
2831 "%#x.\n",
2832 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002833 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002834 case FFA_INSTRUCTION_ACCESS_RESERVED:
2835 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2836 "be checked before this point.");
2837 }
2838
J-Alvesdcad8992023-09-15 14:10:35 +01002839 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002840}
2841
2842/**
2843 * Validate the receivers' permissions in the retrieve request against those
2844 * specified by the lender.
2845 * In the `permissions` argument returns the permissions to set at S2 for the
2846 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002847 * The function looks into the flag to bypass multiple borrower checks:
2848 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2849 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2850 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2851 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002852 */
2853static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2854 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002855 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002856 ffa_memory_access_permissions_t *permissions,
2857 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002858{
2859 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002860 bool bypass_multi_receiver_check =
2861 (retrieve_request->flags &
2862 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002863 const uint32_t region_receiver_count = memory_region->receiver_count;
2864 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002865
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002866 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002867 assert(permissions != NULL);
2868
Karl Meakin84710f32023-10-12 15:14:49 +01002869 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002870
J-Alves3456e032023-07-20 12:20:05 +01002871 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002872 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002873 dlog_verbose(
2874 "Retrieve request should contain same list of "
2875 "borrowers, as specified by the lender.\n");
2876 return ffa_error(FFA_INVALID_PARAMETERS);
2877 }
2878 } else {
2879 if (retrieve_request->receiver_count != 1) {
2880 dlog_verbose(
2881 "Set bypass multiple borrower check, receiver "
Daniel Boulbyc0c8f8c2024-12-31 10:51:35 +00002882 "list must be sized 1 in the retrieve request "
2883 "not %x.\n",
J-Alves3456e032023-07-20 12:20:05 +01002884 memory_region->receiver_count);
2885 return ffa_error(FFA_INVALID_PARAMETERS);
2886 }
Daniel Boulbyc0c8f8c2024-12-31 10:51:35 +00002887 if (memory_region->receiver_count == 1) {
2888 dlog_verbose(
2889 "Setting the bypass multiple borrower check "
2890 "flag for a transaction with a single borrower "
2891 "is not allowed.\n");
2892 return ffa_error(FFA_INVALID_PARAMETERS);
2893 }
J-Alves96de29f2022-04-26 16:05:24 +01002894 }
2895
2896 retrieve_receiver_index = retrieve_request->receiver_count;
2897
J-Alves96de29f2022-04-26 16:05:24 +01002898 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2899 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002900 struct ffa_memory_access *retrieve_request_receiver =
2901 ffa_memory_region_get_receiver(retrieve_request, i);
2902 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002903 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002904 retrieve_request_receiver->receiver_permissions
2905 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002906 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002907 retrieve_request_receiver->receiver_permissions
2908 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002909 struct ffa_memory_access *receiver;
2910 uint32_t mem_region_receiver_index;
2911 bool permissions_RO;
2912 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002913 /*
2914 * If the call is at the virtual FF-A instance the caller's
2915 * ID must match an entry in the memory access list.
2916 * In the SPMC, one of the specified receivers could be from
2917 * the NWd.
2918 */
2919 bool found_to_id = vm_id_is_current_world(to_vm_id)
2920 ? (current_receiver_id == to_vm_id)
2921 : (!vm_id_is_current_world(
2922 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002923
J-Alves3456e032023-07-20 12:20:05 +01002924 if (bypass_multi_receiver_check && !found_to_id) {
2925 dlog_verbose(
2926 "Bypass multiple borrower check for id %x.\n",
2927 current_receiver_id);
2928 continue;
2929 }
2930
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002931 if (retrieve_request_receiver->composite_memory_region_offset !=
2932 0U) {
2933 dlog_verbose(
2934 "Retriever specified address ranges not "
2935 "supported (got offset %d).\n",
2936 retrieve_request_receiver
2937 ->composite_memory_region_offset);
2938 return ffa_error(FFA_INVALID_PARAMETERS);
2939 }
2940
J-Alves96de29f2022-04-26 16:05:24 +01002941 /*
2942 * Find the current receiver in the transaction descriptor from
2943 * sender.
2944 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002945 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002946 ffa_memory_region_get_receiver_index(
2947 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002948
2949 if (mem_region_receiver_index ==
2950 memory_region->receiver_count) {
2951 dlog_verbose("%s: receiver %x not found\n", __func__,
2952 current_receiver_id);
2953 return ffa_error(FFA_DENIED);
2954 }
2955
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002956 receiver = ffa_memory_region_get_receiver(
2957 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002958 assert(receiver != NULL);
2959
2960 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002961
2962 if (found_to_id) {
2963 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002964
2965 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002966 }
2967
2968 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002969 * Check if retrieve request memory access list is valid:
2970 * - The retrieve request complies with the specification.
2971 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002972 */
J-Alvesdcad8992023-09-15 14:10:35 +01002973 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002974 func_id, sent_permissions.data_access,
2975 requested_permissions.data_access,
2976 sent_permissions.instruction_access,
2977 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002978 found_to_id ? permissions : NULL,
2979 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002980
J-Alvesdcad8992023-09-15 14:10:35 +01002981 if (ret.func != FFA_SUCCESS_32) {
2982 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002983 }
2984
Karl Meakin84710f32023-10-12 15:14:49 +01002985 permissions_RO =
2986 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002987 clear_memory_flags =
2988 (retrieve_request->flags &
2989 (FFA_MEMORY_REGION_FLAG_CLEAR |
2990 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002991
J-Alves96de29f2022-04-26 16:05:24 +01002992 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002993 * Can't request PM to clear memory if only provided
2994 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002995 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002996 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002997 dlog_verbose(
2998 "Receiver has RO permissions can not request "
2999 "clear.\n");
3000 return ffa_error(FFA_DENIED);
3001 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003002
3003 /*
3004 * Check the impdef in the retrieve_request matches the value in
3005 * the original memory send.
3006 */
3007 if (ffa_version_from_memory_access_desc_size(
3008 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003009 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003010 ffa_version_from_memory_access_desc_size(
3011 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003012 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003013 if (receiver->impdef.val[0] !=
3014 retrieve_request_receiver->impdef.val[0] ||
3015 receiver->impdef.val[1] !=
3016 retrieve_request_receiver->impdef.val[1]) {
3017 dlog_verbose(
3018 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003019 "match retrieve request value send "
3020 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003021 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003022 receiver->impdef.val[0],
3023 receiver->impdef.val[1],
3024 retrieve_request_receiver->impdef
3025 .val[0],
3026 retrieve_request_receiver->impdef
3027 .val[1]);
3028 return ffa_error(FFA_INVALID_PARAMETERS);
3029 }
3030 }
J-Alves96de29f2022-04-26 16:05:24 +01003031 }
3032
3033 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3034 dlog_verbose(
3035 "Retrieve request does not contain caller's (%x) "
3036 "permissions\n",
3037 to_vm_id);
3038 return ffa_error(FFA_INVALID_PARAMETERS);
3039 }
3040
3041 return (struct ffa_value){.func = FFA_SUCCESS_32};
3042}
3043
Daniel Boulby296ee702023-11-28 13:36:55 +00003044/**
3045 * According to section 17.4.3 of the FF-A v1.2 ALP0 specification, the
3046 * hypervisor may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region
3047 * description of a pending memory sharing operation whose allocator is the SPM,
3048 * for validation purposes before forwarding an FFA_MEM_RECLAIM call. For a
3049 * hypervisor retrieve request the endpoint memory access descriptor count must
3050 * be 0 (for any other retrieve request it must be >= 1).
J-Alvesa9cd7e32022-07-01 13:49:33 +01003051 */
Daniel Boulby296ee702023-11-28 13:36:55 +00003052bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003053{
Daniel Boulby296ee702023-11-28 13:36:55 +00003054 return request->receiver_count == 0U;
3055}
3056
J-Alvesa9cd7e32022-07-01 13:49:33 +01003057/*
3058 * Helper to reset count of fragments retrieved by the hypervisor.
3059 */
3060static void ffa_memory_retrieve_complete_from_hyp(
3061 struct ffa_memory_share_state *share_state)
3062{
3063 if (share_state->hypervisor_fragment_count ==
3064 share_state->fragment_count) {
3065 share_state->hypervisor_fragment_count = 0;
3066 }
3067}
3068
J-Alves089004f2022-07-13 14:25:44 +01003069/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003070 * Prepares the return of the ffa_value for the memory retrieve response.
3071 */
3072static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3073 uint32_t fragment_length)
3074{
3075 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3076 .arg1 = total_length,
3077 .arg2 = fragment_length};
3078}
3079
3080/**
J-Alves089004f2022-07-13 14:25:44 +01003081 * Validate that the memory region descriptor provided by the borrower on
3082 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3083 * memory sharing call.
3084 */
3085static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003086 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3087 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003088 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3089 uint32_t share_func)
3090{
3091 ffa_memory_region_flags_t transaction_type =
3092 retrieve_request->flags &
3093 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003094 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003095 const uint64_t memory_access_desc_size =
3096 retrieve_request->memory_access_desc_size;
3097 const uint32_t expected_retrieve_request_length =
3098 retrieve_request->receivers_offset +
3099 (uint32_t)(retrieve_request->receiver_count *
3100 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003101
3102 assert(retrieve_request != NULL);
3103 assert(memory_region != NULL);
3104 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003105
J-Alves4f0d9c12024-01-17 17:23:11 +00003106 if (retrieve_request_length != expected_retrieve_request_length) {
3107 dlog_verbose(
3108 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3109 "but was %d.\n",
3110 expected_retrieve_request_length,
3111 retrieve_request_length);
3112 return ffa_error(FFA_INVALID_PARAMETERS);
3113 }
3114
3115 if (retrieve_request->sender != memory_region->sender) {
3116 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003117 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003118 "retrieve.\n",
3119 memory_region->handle);
3120 return ffa_error(FFA_DENIED);
3121 }
3122
3123 /*
3124 * The SPMC can only process retrieve requests to memory share
3125 * operations with one borrower from the other world. It can't
3126 * determine the ID of the NWd VM that invoked the retrieve
3127 * request interface call. It relies on the hypervisor to
3128 * validate the caller's ID against that provided in the
3129 * `receivers` list of the retrieve response.
3130 * In case there is only one borrower from the NWd in the
3131 * transaction descriptor, record that in the `receiver_id` for
3132 * later use, and validate in the retrieve request message.
3133 * This limitation is due to the fact SPMC can't determine the
3134 * index in the memory share structures state to update.
3135 */
3136 if (to_id == HF_HYPERVISOR_VM_ID) {
3137 uint32_t other_world_count = 0;
3138
3139 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3140 struct ffa_memory_access *receiver =
3141 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003142 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003143 assert(receiver != NULL);
3144
J-Alvesf220d572024-04-24 22:15:14 +01003145 if (!vm_id_is_current_world(
3146 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003147 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003148 /* Set it to be used later. */
3149 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003150 }
3151 }
3152
3153 if (other_world_count > 1) {
3154 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003155 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003156 return ffa_error(FFA_NOT_SUPPORTED);
3157 }
3158 }
J-Alves089004f2022-07-13 14:25:44 +01003159 /*
3160 * Check that the transaction type expected by the receiver is
3161 * correct, if it has been specified.
3162 */
3163 if (transaction_type !=
3164 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3165 transaction_type != (memory_region->flags &
3166 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3167 dlog_verbose(
3168 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003169 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003170 transaction_type,
3171 memory_region->flags &
3172 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3173 retrieve_request->handle);
3174 return ffa_error(FFA_INVALID_PARAMETERS);
3175 }
3176
3177 if (retrieve_request->tag != memory_region->tag) {
3178 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003179 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3180 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003181 retrieve_request->tag, memory_region->tag,
3182 retrieve_request->handle);
3183 return ffa_error(FFA_INVALID_PARAMETERS);
3184 }
3185
J-Alves4f0d9c12024-01-17 17:23:11 +00003186 *receiver_index =
3187 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003188
3189 if (*receiver_index == memory_region->receiver_count) {
3190 dlog_verbose(
3191 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003192 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003193 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003194 return ffa_error(FFA_INVALID_PARAMETERS);
3195 }
3196
3197 if ((retrieve_request->flags &
3198 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3199 dlog_verbose(
3200 "Retriever specified 'address range alignment 'hint' "
3201 "not supported.\n");
3202 return ffa_error(FFA_INVALID_PARAMETERS);
3203 }
3204 if ((retrieve_request->flags &
3205 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3206 dlog_verbose(
3207 "Bits 8-5 must be zero in memory region's flags "
3208 "(address range alignment hint not supported).\n");
3209 return ffa_error(FFA_INVALID_PARAMETERS);
3210 }
3211
3212 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3213 dlog_verbose(
3214 "Bits 31-10 must be zero in memory region's flags.\n");
3215 return ffa_error(FFA_INVALID_PARAMETERS);
3216 }
3217
J-Alves95fbb312024-03-20 15:19:16 +00003218 if ((share_func == FFA_MEM_SHARE_32 ||
3219 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003220 (retrieve_request->flags &
3221 (FFA_MEMORY_REGION_FLAG_CLEAR |
3222 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3223 dlog_verbose(
3224 "Memory Share operation can't clean after relinquish "
3225 "memory region.\n");
3226 return ffa_error(FFA_INVALID_PARAMETERS);
3227 }
3228
3229 /*
3230 * If the borrower needs the memory to be cleared before mapping
3231 * to its address space, the sender should have set the flag
3232 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3233 * FFA_DENIED.
3234 */
3235 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3236 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3237 dlog_verbose(
3238 "Borrower needs memory cleared. Sender needs to set "
3239 "flag for clearing memory.\n");
3240 return ffa_error(FFA_DENIED);
3241 }
3242
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003243 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003244 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003245 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3246 dlog_verbose(
3247 "Invalid security state for memory retrieve request "
3248 "operation.\n");
3249 return ffa_error(FFA_INVALID_PARAMETERS);
3250 }
3251
J-Alves089004f2022-07-13 14:25:44 +01003252 /*
3253 * If memory type is not specified, bypass validation of memory
3254 * attributes in the retrieve request. The retriever is expecting to
3255 * obtain this information from the SPMC.
3256 */
Karl Meakin84710f32023-10-12 15:14:49 +01003257 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003258 return (struct ffa_value){.func = FFA_SUCCESS_32};
3259 }
3260
3261 /*
3262 * Ensure receiver's attributes are compatible with how
3263 * Hafnium maps memory: Normal Memory, Inner shareable,
3264 * Write-Back Read-Allocate Write-Allocate Cacheable.
3265 */
3266 return ffa_memory_attributes_validate(retrieve_request->attributes);
3267}
3268
J-Alves3f6527c2024-04-25 17:10:57 +01003269/**
3270 * Whilst processing the retrieve request, the operation could be aborted, and
3271 * changes to page tables and the share state structures need to be reverted.
3272 */
3273static void ffa_partition_memory_retrieve_request_undo(
3274 struct vm_locked from_locked,
3275 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3276{
3277 /*
3278 * Currently this operation is expected for operations involving the
3279 * 'other_world' vm.
3280 */
3281 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3282 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3283
3284 /* Decrement the retrieved fragment count for the given receiver. */
3285 share_state->retrieved_fragment_count[receiver_index]--;
3286}
3287
3288/**
3289 * Whilst processing an hypervisor retrieve request the operation could be
3290 * aborted. There were no updates to PTs in this case, so decrementing the
3291 * fragment count retrieved by the hypervisor should be enough.
3292 */
3293static void ffa_hypervisor_memory_retrieve_request_undo(
3294 struct ffa_memory_share_state *share_state)
3295{
3296 assert(share_state->hypervisor_fragment_count > 0);
3297 share_state->hypervisor_fragment_count--;
3298}
3299
J-Alves4f0d9c12024-01-17 17:23:11 +00003300static struct ffa_value ffa_partition_retrieve_request(
3301 struct share_states_locked share_states,
3302 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3303 struct ffa_memory_region *retrieve_request,
3304 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003305{
Karl Meakin84710f32023-10-12 15:14:49 +01003306 ffa_memory_access_permissions_t permissions = {0};
Karl Meakin07a69ab2025-02-07 14:53:19 +00003307 mm_mode_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003308 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003309 struct ffa_composite_memory_region *composite;
3310 uint32_t total_length;
3311 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003312 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003313 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003314 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003315 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003316 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003317 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003318 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003319 ffa_memory_attributes_t attributes = {0};
Karl Meakin07a69ab2025-02-07 14:53:19 +00003320 mm_mode_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003321 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003322
J-Alves96de29f2022-04-26 16:05:24 +01003323 if (!share_state->sending_complete) {
3324 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003325 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003326 "retrieve.\n",
3327 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003328 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003329 }
3330
J-Alves4f0d9c12024-01-17 17:23:11 +00003331 /*
3332 * Validate retrieve request, according to what was sent by the
3333 * sender. Function will output the `receiver_index` from the
3334 * provided memory region.
3335 */
3336 ret = ffa_memory_retrieve_validate(
3337 receiver_id, retrieve_request, retrieve_request_length,
3338 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003339
J-Alves4f0d9c12024-01-17 17:23:11 +00003340 if (ret.func != FFA_SUCCESS_32) {
3341 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003342 }
J-Alves96de29f2022-04-26 16:05:24 +01003343
J-Alves4f0d9c12024-01-17 17:23:11 +00003344 /*
3345 * Validate the requested permissions against the sent
3346 * permissions.
3347 * Outputs the permissions to give to retriever at S2
3348 * PTs.
3349 */
3350 ret = ffa_memory_retrieve_validate_memory_access_list(
3351 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003352 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003353 if (ret.func != FFA_SUCCESS_32) {
3354 return ret;
3355 }
3356
3357 memory_to_mode = ffa_memory_permissions_to_mode(
3358 permissions, share_state->sender_orig_mode);
3359
Daniel Boulby6e261362024-06-13 16:53:00 +01003360 /*
3361 * Check requested memory type is valid with the memory type of the
3362 * owner. E.g. they follow the memory type precedence where Normal
3363 * memory is more permissive than device and therefore device memory
3364 * can only be shared as device memory.
3365 */
3366 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3367 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3368 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3369 dlog_verbose(
3370 "Retrieving device memory as Normal memory is not "
3371 "allowed\n");
3372 return ffa_error(FFA_DENIED);
3373 }
3374
J-Alves4f0d9c12024-01-17 17:23:11 +00003375 ret = ffa_retrieve_check_update(
3376 to_locked, share_state->fragments,
3377 share_state->fragment_constituent_counts,
3378 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003379 share_state->share_func, false, page_pool, &retrieve_mode,
3380 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003381
3382 if (ret.func != FFA_SUCCESS_32) {
3383 return ret;
3384 }
3385
3386 share_state->retrieved_fragment_count[receiver_index] = 1;
3387
3388 is_retrieve_complete =
3389 share_state->retrieved_fragment_count[receiver_index] ==
3390 share_state->fragment_count;
3391
J-Alvesb5084cf2022-07-06 14:20:12 +01003392 /* VMs acquire the RX buffer from SPMC. */
Karl Meakin117c8082024-12-04 16:03:28 +00003393 CHECK(ffa_setup_acquire_receiver_rx(to_locked, &ret));
J-Alvesb5084cf2022-07-06 14:20:12 +01003394
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003395 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003396 * Copy response to RX buffer of caller and deliver the message.
3397 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003398 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003399 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003400
Andrew Walbranca808b12020-05-15 17:22:28 +01003401 /*
J-Alves460d36c2023-10-12 17:02:15 +01003402 * Set the security state in the memory retrieve response attributes
3403 * if specified by the target mode.
3404 */
Karl Meakin117c8082024-12-04 16:03:28 +00003405 attributes = ffa_memory_add_security_bit_from_mode(
Karl Meakin3d32eef2024-11-25 16:40:09 +00003406 memory_region->attributes, retrieve_mode);
J-Alves460d36c2023-10-12 17:02:15 +01003407
3408 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003409 * Constituents which we received in the first fragment should
3410 * always fit in the first fragment we are sending, because the
3411 * header is the same size in both cases and we have a fixed
3412 * message buffer size. So `ffa_retrieved_memory_region_init`
3413 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003414 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003415
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003416 /* Provide the permissions that had been provided. */
3417 receiver->receiver_permissions.permissions = permissions;
3418
3419 /*
3420 * Prepare the memory region descriptor for the retrieve response.
3421 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003422 * structures.
3423 * At this point the retrieve request descriptor from the partition
3424 * has been processed. The `retrieve_request` is expected to be in
3425 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3426 * prepare the retrieve response before copying it to the RX buffer of
3427 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003428 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003429 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003430 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3431 memory_region->sender, attributes, memory_region->flags, handle,
3432 permissions, receiver, 1, memory_access_desc_size,
3433 composite->page_count, composite->constituent_count,
3434 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003435 share_state->fragment_constituent_counts[0], &total_length,
3436 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003437
J-Alves7b9cc432024-04-04 10:57:17 +01003438 /*
3439 * Copy the message from the buffer into the partition's mailbox.
3440 * The operation might fail unexpectedly due to change in PAS address
3441 * space, or improper values to the sizes of the structures.
3442 */
3443 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3444 retrieve_request, fragment_length)) {
3445 dlog_error(
3446 "%s: aborted the copy of response to RX buffer of "
3447 "%x.\n",
3448 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003449
3450 ffa_partition_memory_retrieve_request_undo(
3451 to_locked, share_state, receiver_index);
3452
J-Alves7b9cc432024-04-04 10:57:17 +01003453 return ffa_error(FFA_ABORTED);
3454 }
3455
J-Alves4f0d9c12024-01-17 17:23:11 +00003456 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003457 ffa_memory_retrieve_complete(share_states, share_state,
3458 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003459 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003460
3461 return ffa_memory_retrieve_resp(total_length, fragment_length);
3462}
3463
3464static struct ffa_value ffa_hypervisor_retrieve_request(
3465 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3466 struct ffa_memory_region *retrieve_request)
3467{
3468 struct ffa_value ret;
3469 struct ffa_composite_memory_region *composite;
3470 uint32_t total_length;
3471 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003472 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003473 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003474 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003475 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003476 ffa_memory_handle_t handle = retrieve_request->handle;
3477
J-Alves4f0d9c12024-01-17 17:23:11 +00003478 memory_region = share_state->memory_region;
3479
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003480 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3481
J-Alves7b6ab612024-01-24 09:54:54 +00003482 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003483 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003484 memory_access_desc_size = sizeof(struct ffa_memory_access);
3485 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003486 case FFA_VERSION_1_0:
3487 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003488 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3489 break;
3490 default:
3491 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3492 }
3493
J-Alves4f0d9c12024-01-17 17:23:11 +00003494 if (share_state->hypervisor_fragment_count != 0U) {
3495 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003496 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003497 "the hypervisor.\n",
3498 handle);
3499 return ffa_error(FFA_DENIED);
3500 }
3501
3502 share_state->hypervisor_fragment_count = 1;
3503
J-Alves4f0d9c12024-01-17 17:23:11 +00003504 /* VMs acquire the RX buffer from SPMC. */
Karl Meakin117c8082024-12-04 16:03:28 +00003505 CHECK(ffa_setup_acquire_receiver_rx(to_locked, &ret));
J-Alves4f0d9c12024-01-17 17:23:11 +00003506
3507 /*
3508 * Copy response to RX buffer of caller and deliver the message.
3509 * This must be done before the share_state is (possibly) freed.
3510 */
3511 composite = ffa_memory_region_get_composite(memory_region, 0);
3512
3513 /*
3514 * Constituents which we received in the first fragment should
3515 * always fit in the first fragment we are sending, because the
3516 * header is the same size in both cases and we have a fixed
3517 * message buffer size. So `ffa_retrieved_memory_region_init`
3518 * should never fail.
3519 */
3520
3521 /*
3522 * Set the security state in the memory retrieve response attributes
3523 * if specified by the target mode.
3524 */
Karl Meakin117c8082024-12-04 16:03:28 +00003525 attributes = ffa_memory_add_security_bit_from_mode(
J-Alves4f0d9c12024-01-17 17:23:11 +00003526 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003527
3528 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3529
J-Alves7b9cc432024-04-04 10:57:17 +01003530 /*
3531 * At this point the `retrieve_request` is expected to be in a section
3532 * managed by the hypervisor.
3533 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003534 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003535 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3536 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003537 receiver->receiver_permissions.permissions, receiver,
3538 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003539 composite->page_count, composite->constituent_count,
3540 share_state->fragments[0],
3541 share_state->fragment_constituent_counts[0], &total_length,
3542 &fragment_length));
3543
J-Alves7b9cc432024-04-04 10:57:17 +01003544 /*
3545 * Copy the message from the buffer into the hypervisor's mailbox.
3546 * The operation might fail unexpectedly due to change in PAS, or
3547 * improper values for the sizes of the structures.
3548 */
3549 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3550 retrieve_request, fragment_length)) {
3551 dlog_error(
3552 "%s: aborted the copy of response to RX buffer of "
3553 "%x.\n",
3554 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003555
3556 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3557
J-Alves7b9cc432024-04-04 10:57:17 +01003558 return ffa_error(FFA_ABORTED);
3559 }
3560
J-Alves3f6527c2024-04-25 17:10:57 +01003561 ffa_memory_retrieve_complete_from_hyp(share_state);
3562
J-Alves4f0d9c12024-01-17 17:23:11 +00003563 return ffa_memory_retrieve_resp(total_length, fragment_length);
3564}
3565
3566struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3567 struct ffa_memory_region *retrieve_request,
3568 uint32_t retrieve_request_length,
3569 struct mpool *page_pool)
3570{
3571 ffa_memory_handle_t handle = retrieve_request->handle;
3572 struct share_states_locked share_states;
3573 struct ffa_memory_share_state *share_state;
3574 struct ffa_value ret;
3575
3576 dump_share_states();
3577
3578 share_states = share_states_lock();
3579 share_state = get_share_state(share_states, handle);
3580 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003581 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003582 handle);
3583 ret = ffa_error(FFA_INVALID_PARAMETERS);
3584 goto out;
3585 }
3586
Daniel Boulby296ee702023-11-28 13:36:55 +00003587 if (is_ffa_hypervisor_retrieve_request(retrieve_request)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003588 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3589 retrieve_request);
3590 } else {
3591 ret = ffa_partition_retrieve_request(
3592 share_states, share_state, to_locked, retrieve_request,
3593 retrieve_request_length, page_pool);
3594 }
3595
3596 /* Track use of the RX buffer if the handling has succeeded. */
3597 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3598 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3599 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3600 }
3601
Andrew Walbranca808b12020-05-15 17:22:28 +01003602out:
3603 share_states_unlock(&share_states);
3604 dump_share_states();
3605 return ret;
3606}
3607
J-Alves5da37d92022-10-24 16:33:48 +01003608/**
3609 * Determine expected fragment offset according to the FF-A version of
3610 * the caller.
3611 */
3612static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3613 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003614 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003615{
3616 uint32_t expected_fragment_offset;
3617 uint32_t composite_constituents_offset;
3618
Karl Meakin0e617d92024-04-05 12:55:22 +01003619 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003620 /*
3621 * Hafnium operates memory regions in FF-A v1.1 format, so we
3622 * can retrieve the constituents offset from descriptor.
3623 */
3624 composite_constituents_offset =
3625 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003626 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003627 /*
3628 * If retriever is FF-A v1.0, determine the composite offset
3629 * as it is expected to have been configured in the
3630 * retrieve response.
3631 */
3632 composite_constituents_offset =
3633 sizeof(struct ffa_memory_region_v1_0) +
3634 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003635 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003636 sizeof(struct ffa_composite_memory_region);
3637 } else {
3638 panic("%s received an invalid FF-A version.\n", __func__);
3639 }
3640
3641 expected_fragment_offset =
3642 composite_constituents_offset +
3643 retrieved_constituents_count *
3644 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003645 (size_t)(memory_region->memory_access_desc_size *
3646 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003647
3648 return expected_fragment_offset;
3649}
3650
Andrew Walbranca808b12020-05-15 17:22:28 +01003651struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3652 ffa_memory_handle_t handle,
3653 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003654 ffa_id_t sender_vm_id,
J-Alvesc3fd9752024-04-04 11:45:33 +01003655 void *retrieve_continue_page,
Andrew Walbranca808b12020-05-15 17:22:28 +01003656 struct mpool *page_pool)
3657{
3658 struct ffa_memory_region *memory_region;
3659 struct share_states_locked share_states;
3660 struct ffa_memory_share_state *share_state;
3661 struct ffa_value ret;
3662 uint32_t fragment_index;
3663 uint32_t retrieved_constituents_count;
3664 uint32_t i;
3665 uint32_t expected_fragment_offset;
3666 uint32_t remaining_constituent_count;
3667 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003668 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003669 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003670
3671 dump_share_states();
3672
3673 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003674 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003675 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003676 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003677 handle);
3678 ret = ffa_error(FFA_INVALID_PARAMETERS);
3679 goto out;
3680 }
3681
3682 memory_region = share_state->memory_region;
3683 CHECK(memory_region != NULL);
3684
Andrew Walbranca808b12020-05-15 17:22:28 +01003685 if (!share_state->sending_complete) {
3686 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003687 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003688 "retrieve.\n",
3689 handle);
3690 ret = ffa_error(FFA_INVALID_PARAMETERS);
3691 goto out;
3692 }
3693
J-Alves59ed0042022-07-28 18:26:41 +01003694 /*
3695 * If retrieve request from the hypervisor has been initiated in the
3696 * given share_state, continue it, else assume it is a continuation of
J-Alvesc3fd9752024-04-04 11:45:33 +01003697 * retrieve request from a partition.
J-Alves59ed0042022-07-28 18:26:41 +01003698 */
3699 continue_ffa_hyp_mem_retrieve_req =
3700 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3701 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003702 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003703
J-Alves59ed0042022-07-28 18:26:41 +01003704 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003705 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003706 memory_region, to_locked.vm->id);
3707
3708 if (receiver_index == memory_region->receiver_count) {
3709 dlog_verbose(
3710 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003711 "borrower to memory sharing transaction "
3712 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003713 to_locked.vm->id, handle);
3714 ret = ffa_error(FFA_INVALID_PARAMETERS);
3715 goto out;
3716 }
3717
J-Alvesc3fd9752024-04-04 11:45:33 +01003718 fragment_index =
3719 share_state->retrieved_fragment_count[receiver_index];
3720
3721 if (fragment_index == 0 ||
3722 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003723 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003724 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003725 "started or already completed (%d/%d fragments "
3726 "retrieved).\n",
3727 handle,
3728 share_state->retrieved_fragment_count
3729 [receiver_index],
3730 share_state->fragment_count);
3731 ret = ffa_error(FFA_INVALID_PARAMETERS);
3732 goto out;
3733 }
J-Alves59ed0042022-07-28 18:26:41 +01003734 } else {
J-Alvesc3fd9752024-04-04 11:45:33 +01003735 fragment_index = share_state->hypervisor_fragment_count;
3736
3737 if (fragment_index == 0 ||
3738 fragment_index >= share_state->fragment_count) {
J-Alves59ed0042022-07-28 18:26:41 +01003739 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003740 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003741 "started from hypervisor.\n",
3742 handle);
3743 ret = ffa_error(FFA_INVALID_PARAMETERS);
3744 goto out;
3745 }
3746
3747 if (memory_region->sender != sender_vm_id) {
3748 dlog_verbose(
3749 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003750 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003751 sender_vm_id, handle);
3752 ret = ffa_error(FFA_INVALID_PARAMETERS);
3753 goto out;
3754 }
3755
J-Alves59ed0042022-07-28 18:26:41 +01003756 receiver_index = 0;
3757 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003758
3759 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003760 * Check that the given fragment offset is correct by counting
3761 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003762 */
3763 retrieved_constituents_count = 0;
3764 for (i = 0; i < fragment_index; ++i) {
3765 retrieved_constituents_count +=
3766 share_state->fragment_constituent_counts[i];
3767 }
J-Alvesc7484f12022-05-13 12:41:14 +01003768
3769 CHECK(memory_region->receiver_count > 0);
3770
Andrew Walbranca808b12020-05-15 17:22:28 +01003771 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003772 ffa_memory_retrieve_expected_offset_per_ffa_version(
3773 memory_region, retrieved_constituents_count,
3774 to_locked.vm->ffa_version);
3775
Andrew Walbranca808b12020-05-15 17:22:28 +01003776 if (fragment_offset != expected_fragment_offset) {
3777 dlog_verbose("Fragment offset was %d but expected %d.\n",
3778 fragment_offset, expected_fragment_offset);
3779 ret = ffa_error(FFA_INVALID_PARAMETERS);
3780 goto out;
3781 }
3782
J-Alves4f0d9c12024-01-17 17:23:11 +00003783 /*
3784 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3785 * is currently ownder by the SPMC.
3786 */
Karl Meakin117c8082024-12-04 16:03:28 +00003787 assert(ffa_setup_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003788
Andrew Walbranca808b12020-05-15 17:22:28 +01003789 remaining_constituent_count = ffa_memory_fragment_init(
J-Alvesc3fd9752024-04-04 11:45:33 +01003790 (struct ffa_memory_region_constituent *)retrieve_continue_page,
3791 HF_MAILBOX_SIZE, share_state->fragments[fragment_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01003792 share_state->fragment_constituent_counts[fragment_index],
3793 &fragment_length);
3794 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003795
J-Alvesc3fd9752024-04-04 11:45:33 +01003796 /*
3797 * Return FFA_ERROR(FFA_ABORTED) in case the access to the partition's
3798 * RX buffer results in a GPF exception. Could happen if the retrieve
3799 * request is for a VM or the Hypervisor retrieve request, if the PAS
3800 * has been changed externally.
3801 */
3802 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3803 retrieve_continue_page, fragment_length)) {
3804 dlog_error(
3805 "%s: aborted copying fragment to RX buffer of %#x.\n",
3806 __func__, to_locked.vm->id);
3807 ret = ffa_error(FFA_ABORTED);
3808 goto out;
3809 }
3810
Andrew Walbranca808b12020-05-15 17:22:28 +01003811 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003812 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003813
J-Alves59ed0042022-07-28 18:26:41 +01003814 if (!continue_ffa_hyp_mem_retrieve_req) {
3815 share_state->retrieved_fragment_count[receiver_index]++;
3816 if (share_state->retrieved_fragment_count[receiver_index] ==
3817 share_state->fragment_count) {
3818 ffa_memory_retrieve_complete(share_states, share_state,
3819 page_pool);
3820 }
3821 } else {
3822 share_state->hypervisor_fragment_count++;
3823
3824 ffa_memory_retrieve_complete_from_hyp(share_state);
3825 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003826 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3827 .arg1 = (uint32_t)handle,
3828 .arg2 = (uint32_t)(handle >> 32),
3829 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003830
3831out:
3832 share_states_unlock(&share_states);
3833 dump_share_states();
3834 return ret;
3835}
3836
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003837struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003838 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003839 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003840{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003841 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003842 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003843 struct ffa_memory_share_state *share_state;
3844 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003845 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003846 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003847 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003848 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003849 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003850
Andrew Walbrana65a1322020-04-06 19:32:32 +01003851 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003852 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003853 "Stream endpoints not supported (got %d endpoints on "
3854 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003855 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003856 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003857 }
3858
J-Alvesbd060342024-04-26 18:44:31 +01003859 if (vm_id_is_current_world(from_locked.vm->id) &&
3860 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003861 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003862 "VM ID %d in relinquish message doesn't match calling "
3863 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003864 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003865 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003866 }
3867
3868 dump_share_states();
3869
3870 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003871 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003872 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003873 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003874 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003875 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003876 goto out;
3877 }
3878
Andrew Walbranca808b12020-05-15 17:22:28 +01003879 if (!share_state->sending_complete) {
3880 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003881 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003882 "relinquish.\n",
3883 handle);
3884 ret = ffa_error(FFA_INVALID_PARAMETERS);
3885 goto out;
3886 }
3887
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003888 memory_region = share_state->memory_region;
3889 CHECK(memory_region != NULL);
3890
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003891 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003892 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003893
3894 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003895 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003896 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003897 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003898 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003899 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003900 goto out;
3901 }
3902
J-Alves8eb19162022-04-28 10:56:48 +01003903 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003904 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003905 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003906 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003907 "receiver %x can't relinquish.\n",
3908 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003909 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003910 goto out;
3911 }
3912
J-Alves3c5b2072022-11-21 12:45:40 +00003913 /*
3914 * Either clear if requested in relinquish call, or in a retrieve
3915 * request from one of the borrowers.
3916 */
3917 receivers_relinquished_memory = true;
3918
3919 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3920 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003921 ffa_memory_region_get_receiver(memory_region, i);
3922 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003923 if (receiver->receiver_permissions.receiver ==
3924 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003925 receiver_permissions =
3926 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003927 continue;
3928 }
3929
3930 if (share_state->retrieved_fragment_count[i] != 0U) {
3931 receivers_relinquished_memory = false;
3932 break;
3933 }
3934 }
3935
3936 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003937 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3938 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003939
3940 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003941 * Clear is not allowed for memory that was shared, as the
3942 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003943 */
J-Alves95fbb312024-03-20 15:19:16 +00003944 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3945 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003946 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003947 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003948 goto out;
3949 }
3950
J-Alvesb886d492024-04-15 10:55:29 +01003951 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003952 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3953 __func__);
3954 ret = ffa_error(FFA_DENIED);
3955 goto out;
3956 }
3957
Andrew Walbranca808b12020-05-15 17:22:28 +01003958 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003959 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003960 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003961 share_state->fragment_count, share_state->sender_orig_mode,
3962 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003963
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003964 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003965 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003966 * Mark memory handle as not retrieved, so it can be
3967 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003968 */
J-Alves8eb19162022-04-28 10:56:48 +01003969 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003970 }
3971
3972out:
3973 share_states_unlock(&share_states);
3974 dump_share_states();
3975 return ret;
3976}
3977
3978/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003979 * Validates that the reclaim transition is allowed for the given
3980 * handle, updates the page table of the reclaiming VM, and frees the
3981 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003982 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003983struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003984 ffa_memory_handle_t handle,
3985 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003986 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003987{
3988 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003989 struct ffa_memory_share_state *share_state;
3990 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003991 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003992
3993 dump_share_states();
3994
3995 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003996
Karl Meakin4a2854a2023-06-30 16:26:52 +01003997 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003998 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003999 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004000 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004001 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004002 goto out;
4003 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01004004 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004005
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004006 CHECK(memory_region != NULL);
4007
J-Alvesa9cd7e32022-07-01 13:49:33 +01004008 if (vm_id_is_current_world(to_locked.vm->id) &&
4009 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004010 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004011 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004012 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004013 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004014 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004015 goto out;
4016 }
4017
Andrew Walbranca808b12020-05-15 17:22:28 +01004018 if (!share_state->sending_complete) {
4019 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004020 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004021 "reclaim.\n",
4022 handle);
4023 ret = ffa_error(FFA_INVALID_PARAMETERS);
4024 goto out;
4025 }
4026
J-Alves752236c2022-04-28 11:07:47 +01004027 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4028 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004029 struct ffa_memory_access *receiver =
4030 ffa_memory_region_get_receiver(memory_region,
4031 i);
4032
4033 assert(receiver != NULL);
4034 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004035 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004036 "Tried to reclaim memory handle %#lx that has "
4037 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004038 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004039 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004040 ret = ffa_error(FFA_DENIED);
4041 goto out;
4042 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004043 }
4044
Andrew Walbranca808b12020-05-15 17:22:28 +01004045 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004046 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004047 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004048 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004049 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004050 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004051
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004052 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004053 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004054 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004055 }
4056
4057out:
4058 share_states_unlock(&share_states);
4059 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004060}