blob: e60c402ea029b7f16a351523391b466a6fdef352 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
J-Alves460d36c2023-10-12 17:02:15 +010011#include <stdint.h>
12
Federico Recanati4fd065d2021-12-13 20:06:23 +010013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000016
J-Alves5952d942022-12-22 16:03:00 +000017#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000019#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010020#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010022#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010024#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010025#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000026#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000027#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000028#include "hf/panic.h"
29#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010030#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000031#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000032#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010033
J-Alves2d8457f2022-10-05 11:06:41 +010034#include "vmapi/hf/ffa_v1_0.h"
35
J-Alves5da37d92022-10-24 16:33:48 +010036#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
37
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000038/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040 * by this lock.
41 */
42static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000044
45/**
J-Alvesed508c82023-05-04 16:09:48 +010046 * Return the offset to the first constituent within the
47 * `ffa_composite_memory_region` for the given receiver from an
48 * `ffa_memory_region`. The caller must check that the receiver_index is within
49 * bounds, and that it has a composite memory region offset.
50 */
51static uint32_t ffa_composite_constituent_offset(
52 struct ffa_memory_region *memory_region, uint32_t receiver_index)
53{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000054 struct ffa_memory_access *receiver;
55 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010056
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000057 CHECK(receiver_index < memory_region->receiver_count);
58
59 receiver =
60 ffa_memory_region_get_receiver(memory_region, receiver_index);
61 CHECK(receiver != NULL);
62
63 composite_offset = receiver->composite_memory_region_offset;
64
65 CHECK(composite_offset != 0);
66
67 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010068}
69
70/**
J-Alves917d2f22020-10-30 18:39:30 +000071 * Extracts the index from a memory handle allocated by Hafnium's current world.
72 */
73uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
74{
75 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
76}
77
78/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010079 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
80 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
81 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010082 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010083 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
84 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010085 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010086struct ffa_memory_share_state *allocate_share_state(
87 struct share_states_locked share_states, uint32_t share_func,
88 struct ffa_memory_region *memory_region, uint32_t fragment_length,
89 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000091 assert(share_states.share_states != NULL);
92 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093
Karl Meakin52cdfe72023-06-30 14:49:10 +010094 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010095 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010097 &share_states.share_states[i];
98 struct ffa_composite_memory_region *composite =
99 ffa_memory_region_get_composite(memory_region,
100 0);
101
102 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000103 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200104 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100105 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000106 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100107 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108 allocated_state->share_func = share_func;
109 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100110 allocated_state->fragment_count = 1;
111 allocated_state->fragments[0] = composite->constituents;
112 allocated_state->fragment_constituent_counts[0] =
113 (fragment_length -
114 ffa_composite_constituent_offset(memory_region,
115 0)) /
116 sizeof(struct ffa_memory_region_constituent);
117 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100118 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
119 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100120 allocated_state->retrieved_fragment_count[j] =
121 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100123 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 }
125 }
126
Karl Meakin52cdfe72023-06-30 14:49:10 +0100127 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128}
129
130/** Locks the share states lock. */
131struct share_states_locked share_states_lock(void)
132{
133 sl_lock(&share_states_lock_instance);
134
135 return (struct share_states_locked){.share_states = share_states};
136}
137
138/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100139void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000141 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 share_states->share_states = NULL;
143 sl_unlock(&share_states_lock_instance);
144}
145
146/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100147 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100148 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150struct ffa_memory_share_state *get_share_state(
151 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000155 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100156
157 /*
158 * First look for a share_state allocated by us, in which case the
159 * handle is based on the index.
160 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100162 uint64_t index = ffa_memory_handle_get_index(handle);
163
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 if (index < MAX_MEM_SHARES) {
165 share_state = &share_states.share_states[index];
166 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100167 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 }
169 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 }
171
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100173 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000175 if (share_state->memory_region != NULL &&
176 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100178 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
181
Karl Meakin4a2854a2023-06-30 16:26:52 +0100182 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
185/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186void share_state_free(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state,
188 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189{
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 uint32_t i;
191
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000192 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 /*
197 * First fragment is part of the same page as the `memory_region`, so it
198 * doesn't need to be freed separately.
199 */
200 share_state->fragments[0] = NULL;
201 share_state->fragment_constituent_counts[0] = 0;
202 for (i = 1; i < share_state->fragment_count; ++i) {
203 mpool_free(page_pool, share_state->fragments[i]);
204 share_state->fragments[i] = NULL;
205 share_state->fragment_constituent_counts[i] = 0;
206 }
207 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100209 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210}
211
Andrew Walbranca808b12020-05-15 17:22:28 +0100212/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100213bool share_state_sending_complete(struct share_states_locked share_states,
214 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranca808b12020-05-15 17:22:28 +0100216 struct ffa_composite_memory_region *composite;
217 uint32_t expected_constituent_count;
218 uint32_t fragment_constituent_count_total = 0;
219 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000220
Andrew Walbranca808b12020-05-15 17:22:28 +0100221 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000222 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100223
224 /*
225 * Share state must already be valid, or it's not possible to get hold
226 * of it.
227 */
228 CHECK(share_state->memory_region != NULL &&
229 share_state->share_func != 0);
230
231 composite =
232 ffa_memory_region_get_composite(share_state->memory_region, 0);
233 expected_constituent_count = composite->constituent_count;
234 for (i = 0; i < share_state->fragment_count; ++i) {
235 fragment_constituent_count_total +=
236 share_state->fragment_constituent_counts[i];
237 }
238 dlog_verbose(
239 "Checking completion: constituent count %d/%d from %d "
240 "fragments.\n",
241 fragment_constituent_count_total, expected_constituent_count,
242 share_state->fragment_count);
243
244 return fragment_constituent_count_total == expected_constituent_count;
245}
246
247/**
248 * Calculates the offset of the next fragment expected for the given share
249 * state.
250 */
J-Alvesfdd29272022-07-19 13:16:31 +0100251uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100252 struct share_states_locked share_states,
253 struct ffa_memory_share_state *share_state)
254{
255 uint32_t next_fragment_offset;
256 uint32_t i;
257
258 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260
261 next_fragment_offset =
262 ffa_composite_constituent_offset(share_state->memory_region, 0);
263 for (i = 0; i < share_state->fragment_count; ++i) {
264 next_fragment_offset +=
265 share_state->fragment_constituent_counts[i] *
266 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 }
268
Andrew Walbranca808b12020-05-15 17:22:28 +0100269 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000270}
271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100272static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273{
274 uint32_t i;
275
276 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
277 return;
278 }
279
Karl Meakine8937d92024-03-19 16:04:25 +0000280 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
281 "type = %s, security = %s), flags %#x, handle %#lx "
282 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100283 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000284 memory_region->sender,
285 ffa_memory_shareability_name(
286 memory_region->attributes.shareability),
287 ffa_memory_cacheability_name(
288 memory_region->attributes.cacheability),
289 ffa_memory_type_name(memory_region->attributes.type),
290 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000291 memory_region->flags, memory_region->handle, memory_region->tag,
292 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100293 memory_region->receiver_count);
294 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000295 struct ffa_memory_access *receiver =
296 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000297 if (i != 0) {
298 dlog(", ");
299 }
Karl Meakine8937d92024-03-19 16:04:25 +0000300 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000301 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000302 ffa_data_access_name(receiver->receiver_permissions
303 .permissions.data_access),
304 ffa_instruction_access_name(
305 receiver->receiver_permissions.permissions
306 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000307 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000308 /* The impdef field is only present from v1.2 and later */
309 if (ffa_version_from_memory_access_desc_size(
310 memory_region->memory_access_desc_size) >=
311 MAKE_FFA_VERSION(1, 2)) {
Karl Meakine8937d92024-03-19 16:04:25 +0000312 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000313 receiver->impdef.val[1]);
314 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000315 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000316 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000317}
318
J-Alves66652252022-07-06 09:49:51 +0100319void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320{
321 uint32_t i;
322
323 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
324 return;
325 }
326
327 dlog("Current share states:\n");
328 sl_lock(&share_states_lock_instance);
329 for (i = 0; i < MAX_MEM_SHARES; ++i) {
330 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000331 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 dlog("SHARE");
334 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000336 dlog("LEND");
337 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000339 dlog("DONATE");
340 break;
341 default:
342 dlog("invalid share_func %#x",
343 share_states[i].share_func);
344 }
Karl Meakine8937d92024-03-19 16:04:25 +0000345 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100347 if (share_states[i].sending_complete) {
348 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000349 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100350 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 }
J-Alves2a0d2882020-10-29 14:49:50 +0000352 dlog(" with %d fragments, %d retrieved, "
353 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100354 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000355 share_states[i].retrieved_fragment_count[0],
356 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000357 }
358 }
359 sl_unlock(&share_states_lock_instance);
360}
361
Andrew Walbran475c1452020-02-07 13:22:22 +0000362/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100363static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100364 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000365{
366 uint32_t mode = 0;
367
Karl Meakin84710f32023-10-12 15:14:49 +0100368 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100369 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000370 mode = MM_MODE_R;
371 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000373 mode = MM_MODE_R | MM_MODE_W;
374 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100375 case FFA_DATA_ACCESS_NOT_SPECIFIED:
376 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
377 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100378 case FFA_DATA_ACCESS_RESERVED:
379 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100380 }
381
Karl Meakin84710f32023-10-12 15:14:49 +0100382 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000384 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100385 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100386 mode |= MM_MODE_X;
387 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100388 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
389 mode |= (default_mode & MM_MODE_X);
390 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100391 case FFA_INSTRUCTION_ACCESS_RESERVED:
392 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000393 }
394
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200395 /* Set the security state bit if necessary. */
396 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
397 mode |= plat_ffa_other_world_mode();
398 }
399
Andrew Walbran475c1452020-02-07 13:22:22 +0000400 return mode;
401}
402
Jose Marinho75509b42019-04-09 09:34:59 +0100403/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000404 * Get the current mode in the stage-2 page table of the given vm of all the
405 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100406 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100407 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100408static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000409 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100410 struct ffa_memory_region_constituent **fragments,
411 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100412{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100413 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100414 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100415
Andrew Walbranca808b12020-05-15 17:22:28 +0100416 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100417 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000418 * Fail if there are no constituents. Otherwise we would get an
419 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100421 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100422 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100423 }
424
Andrew Walbranca808b12020-05-15 17:22:28 +0100425 for (i = 0; i < fragment_count; ++i) {
426 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
427 ipaddr_t begin = ipa_init(fragments[i][j].address);
428 size_t size = fragments[i][j].page_count * PAGE_SIZE;
429 ipaddr_t end = ipa_add(begin, size);
430 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100431
Andrew Walbranca808b12020-05-15 17:22:28 +0100432 /* Fail if addresses are not page-aligned. */
433 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
434 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100435 dlog_verbose("%s: addresses not page-aligned\n",
436 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100437 return ffa_error(FFA_INVALID_PARAMETERS);
438 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100439
Andrew Walbranca808b12020-05-15 17:22:28 +0100440 /*
441 * Ensure that this constituent memory range is all
442 * mapped with the same mode.
443 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800444 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100445 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000446 "%s: constituent memory range "
447 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100448 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000449 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 return ffa_error(FFA_DENIED);
451 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100452
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 /*
454 * Ensure that all constituents are mapped with the same
455 * mode.
456 */
457 if (i == 0) {
458 *orig_mode = current_mode;
459 } else if (current_mode != *orig_mode) {
460 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100461 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000462 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100463 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100464 fragments[i][j].page_count,
465 ipa_addr(begin));
466 return ffa_error(FFA_DENIED);
467 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100468 }
Jose Marinho75509b42019-04-09 09:34:59 +0100469 }
470
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100471 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000472}
473
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100474uint32_t ffa_version_from_memory_access_desc_size(
475 uint32_t memory_access_desc_size)
476{
477 switch (memory_access_desc_size) {
478 /*
479 * v1.0 and v1.1 memory access descriptors are the same size however
480 * v1.1 is the first version to include the memory access descriptor
481 * size field so return v1.1.
482 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000483 case sizeof(struct ffa_memory_access_v1_0):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100484 return MAKE_FFA_VERSION(1, 1);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000485 case sizeof(struct ffa_memory_access):
486 return MAKE_FFA_VERSION(1, 2);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100487 }
488 return 0;
489}
490
491/**
492 * Check if the receivers size and offset given is valid for the senders
493 * FF-A version.
494 */
495static bool receiver_size_and_offset_valid_for_version(
496 uint32_t receivers_size, uint32_t receivers_offset,
497 uint32_t ffa_version)
498{
499 /*
500 * Check that the version that the memory access descriptor size belongs
501 * to is compatible with the FF-A version we believe the sender to be.
502 */
503 uint32_t expected_ffa_version =
504 ffa_version_from_memory_access_desc_size(receivers_size);
505 if (!FFA_VERSIONS_ARE_COMPATIBLE(expected_ffa_version, ffa_version)) {
506 return false;
507 }
508
509 /*
510 * Check the receivers_offset matches the version we found from
511 * memory access descriptor size.
512 */
513 switch (expected_ffa_version) {
514 case MAKE_FFA_VERSION(1, 1):
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000515 case MAKE_FFA_VERSION(1, 2):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100516 return receivers_offset == sizeof(struct ffa_memory_region);
517 default:
518 return false;
519 }
520}
521
522/**
523 * Check the values set for fields in the memory region are valid and safe.
524 * Offset values are within safe bounds, receiver count will not cause overflows
525 * and reserved fields are 0.
526 */
527bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
528 uint32_t ffa_version,
529 uint32_t fragment_length,
530 bool send_transaction)
531{
532 uint32_t receiver_count;
533 struct ffa_memory_access *receiver;
534 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000535 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
536 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100537
538 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100539 /* Check the reserved fields are 0. */
540 if (memory_region_v1_0->reserved_0 != 0 ||
541 memory_region_v1_0->reserved_1 != 0) {
542 dlog_verbose("Reserved fields must be 0.\n");
543 return false;
544 }
545
546 receiver_count = memory_region_v1_0->receiver_count;
547 } else {
548 uint32_t receivers_size =
549 memory_region->memory_access_desc_size;
550 uint32_t receivers_offset = memory_region->receivers_offset;
551
552 /* Check the reserved field is 0. */
553 if (memory_region->reserved[0] != 0 ||
554 memory_region->reserved[1] != 0 ||
555 memory_region->reserved[2] != 0) {
556 dlog_verbose("Reserved fields must be 0.\n");
557 return false;
558 }
559
560 /*
561 * Check memory_access_desc_size matches the size of the struct
562 * for the senders FF-A version.
563 */
564 if (!receiver_size_and_offset_valid_for_version(
565 receivers_size, receivers_offset, ffa_version)) {
566 dlog_verbose(
567 "Invalid memory access descriptor size %d, "
568 " or receiver offset %d, "
569 "for FF-A version %#x\n",
570 receivers_size, receivers_offset, ffa_version);
571 return false;
572 }
573
574 receiver_count = memory_region->receiver_count;
575 }
576
577 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000578 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100579 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000580 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100581 "specified %u\n",
582 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
583 return false;
584 }
585
586 /* Check values in the memory access descriptors. */
587 /*
588 * The composite offset values must be the same for all recievers so
589 * check the first one is valid and then they are all the same.
590 */
591 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000592 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100593 ->receivers[0]
594 : ffa_memory_region_get_receiver(memory_region, 0);
595 assert(receiver != NULL);
596 composite_offset_0 = receiver->composite_memory_region_offset;
597
598 if (!send_transaction) {
599 if (composite_offset_0 != 0) {
600 dlog_verbose(
601 "Composite offset memory region descriptor "
602 "offset must be 0 for retrieve requests. "
603 "Currently %d",
604 composite_offset_0);
605 return false;
606 }
607 } else {
608 bool comp_offset_is_zero = composite_offset_0 == 0U;
609 bool comp_offset_lt_transaction_descriptor_size =
610 composite_offset_0 <
611 (sizeof(struct ffa_memory_region) +
612 (uint32_t)(memory_region->memory_access_desc_size *
613 memory_region->receiver_count));
614 bool comp_offset_with_comp_gt_fragment_length =
615 composite_offset_0 +
616 sizeof(struct ffa_composite_memory_region) >
617 fragment_length;
618 if (comp_offset_is_zero ||
619 comp_offset_lt_transaction_descriptor_size ||
620 comp_offset_with_comp_gt_fragment_length) {
621 dlog_verbose(
622 "Invalid composite memory region descriptor "
623 "offset for send transaction %u\n",
624 composite_offset_0);
625 return false;
626 }
627 }
628
Karl Meakin824b63d2024-06-03 19:04:53 +0100629 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100630 uint32_t composite_offset;
631
632 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100633 struct ffa_memory_access_v1_0 *receiver_v1_0 =
634 &memory_region_v1_0->receivers[i];
635 /* Check reserved fields are 0 */
636 if (receiver_v1_0->reserved_0 != 0) {
637 dlog_verbose(
638 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000639 "descriptor must be zero. Currently "
640 "reciever %zu has a reserved field "
641 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100642 i, receiver_v1_0->reserved_0);
643 return false;
644 }
645 /*
646 * We can cast to the current version receiver as the
647 * remaining fields we are checking have the same
648 * offsets for all versions since memory access
649 * descriptors are forwards compatible.
650 */
651 receiver = (struct ffa_memory_access *)receiver_v1_0;
652 } else {
653 receiver = ffa_memory_region_get_receiver(memory_region,
654 i);
655 assert(receiver != NULL);
656
657 if (receiver->reserved_0 != 0) {
658 dlog_verbose(
659 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000660 "descriptor must be zero. Currently "
661 "reciever %zu has a reserved field "
662 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100663 i, receiver->reserved_0);
664 return false;
665 }
666 }
667
668 /* Check composite offset values are equal for all receivers. */
669 composite_offset = receiver->composite_memory_region_offset;
670 if (composite_offset != composite_offset_0) {
671 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000672 "Composite offset %x differs from %x in "
673 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100674 composite_offset, composite_offset_0);
675 return false;
676 }
677 }
678 return true;
679}
680
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000681/**
J-Alves460d36c2023-10-12 17:02:15 +0100682 * If the receivers for the memory management operation are all from the
683 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
684 * state update by returning MAP_ACTION_CHECK_PROTECT.
685 */
686static enum ffa_map_action ffa_mem_send_get_map_action(
687 bool all_receivers_from_current_world, ffa_id_t sender_id,
688 uint32_t mem_func_id)
689{
690 bool protect_memory =
691 (mem_func_id != FFA_MEM_SHARE_32 &&
692 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
693
694 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
695}
696
697/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000698 * Verify that all pages have the same mode, that the starting mode
699 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100700 * to the sending VM. It outputs the mapping action that needs to be
701 * invoked for the given memory range. On memory lend/donate there
702 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000703 *
704 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100705 * 1) FFA_DENIED if a state transition was not found;
706 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100707 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100708 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100709 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
711 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000712 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100714 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000715 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100716 struct ffa_memory_region_constituent **fragments,
717 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100718 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000719{
720 const uint32_t state_mask =
721 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100722 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100723 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000724 uint32_t receivers_count = memory_region->receiver_count;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000725
Andrew Walbranca808b12020-05-15 17:22:28 +0100726 ret = constituents_get_mode(from, orig_from_mode, fragments,
727 fragment_constituent_counts,
728 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100729 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100730 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100731 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100732 }
733
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000734 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000735 if ((*orig_from_mode & MM_MODE_D) != 0U &&
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000736 !(share_func == FFA_MEM_LEND_32 && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000737 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000738 "Device memory can only be lent to a single borrower "
739 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000740 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000742 }
743
744 /*
745 * Ensure the sender is the owner and has exclusive access to the
746 * memory.
747 */
748 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100750 }
751
Daniel Boulbya76fd912024-02-22 14:22:15 +0000752 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100753
J-Alves363f5722022-04-25 17:37:37 +0100754 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000755 struct ffa_memory_access *receiver =
756 ffa_memory_region_get_receiver(memory_region, i);
757 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100758 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000759 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100760 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
761 permissions, *orig_from_mode);
762
J-Alves788b4492023-04-18 14:01:23 +0100763 /*
764 * The assumption is that at this point, the operation from
765 * SP to a receiver VM, should have returned an FFA_ERROR
766 * already.
767 */
768 if (!ffa_is_vm_id(from.vm->id)) {
769 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000770 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100771 }
772
J-Alves460d36c2023-10-12 17:02:15 +0100773 /* Track if all senders are from current world. */
774 all_receivers_from_current_world =
775 all_receivers_from_current_world &&
776 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000777 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100778
J-Alves363f5722022-04-25 17:37:37 +0100779 if ((*orig_from_mode & required_from_mode) !=
780 required_from_mode) {
781 dlog_verbose(
782 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100783 "which required mode %#x but only had %#x "
784 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100785 required_from_mode, *orig_from_mode);
786 return ffa_error(FFA_DENIED);
787 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000788 }
789
J-Alves460d36c2023-10-12 17:02:15 +0100790 *map_action = ffa_mem_send_get_map_action(
791 all_receivers_from_current_world, from.vm->id, share_func);
792
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000793 /* Find the appropriate new mode. */
794 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000795 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100796 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000797 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100798 break;
799
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100800 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000801 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100802 break;
803
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100804 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000805 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100806 break;
807
Jose Marinho75509b42019-04-09 09:34:59 +0100808 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100810 }
811
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100812 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000813}
814
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100815static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000816 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100817 struct ffa_memory_region_constituent **fragments,
818 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
819 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000820{
821 const uint32_t state_mask =
822 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
823 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100824 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000825
Andrew Walbranca808b12020-05-15 17:22:28 +0100826 ret = constituents_get_mode(from, orig_from_mode, fragments,
827 fragment_constituent_counts,
828 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100829 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100830 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000831 }
832
833 /* Ensure the address range is normal memory and not a device. */
834 if (*orig_from_mode & MM_MODE_D) {
835 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
836 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100837 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000838 }
839
840 /*
841 * Ensure the relinquishing VM is not the owner but has access to the
842 * memory.
843 */
844 orig_from_state = *orig_from_mode & state_mask;
845 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
846 dlog_verbose(
847 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100848 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000849 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000851 }
852
853 /* Find the appropriate new mode. */
854 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
855
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100856 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857}
858
859/**
860 * Verify that all pages have the same mode, that the starting mode
861 * constitutes a valid state and obtain the next mode to apply
862 * to the retrieving VM.
863 *
864 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100865 * 1) FFA_DENIED if a state transition was not found;
866 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100867 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100869 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100870 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
871 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000872 */
J-Alvesfc19b372022-07-06 12:17:35 +0100873struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100875 struct ffa_memory_region_constituent **fragments,
876 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100877 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
878 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000879{
880 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882
Andrew Walbranca808b12020-05-15 17:22:28 +0100883 ret = constituents_get_mode(to, &orig_to_mode, fragments,
884 fragment_constituent_counts,
885 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100888 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000889 }
890
J-Alves460d36c2023-10-12 17:02:15 +0100891 /* Find the appropriate new mode. */
892 *to_mode = memory_to_attributes;
893
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000895 /*
896 * If the original ffa memory send call has been processed
897 * successfully, it is expected the orig_to_mode would overlay
898 * with `state_mask`, as a result of the function
899 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100900 *
901 * If Hafnium is the SPMC:
902 * - Caller of the reclaim interface is an SP, the memory shall
903 * have been protected throughout the flow.
904 * - Caller of the reclaim is from the NWd, the memory may have
905 * been protected at the time of lending/donating the memory.
906 * In such case, set action to unprotect memory in the
907 * handling of reclaim operation.
908 * - If Hafnium is the hypervisor memory shall never have been
909 * protected in memory lend/share/donate.
910 *
911 * More details in the doc comment of the function
912 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000913 */
J-Alves59ed0042022-07-28 18:26:41 +0100914 if (vm_id_is_current_world(to.vm->id)) {
915 assert((orig_to_mode &
916 (MM_MODE_INVALID | MM_MODE_UNOWNED |
917 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100918 assert(!memory_protected);
919 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
920 map_action != NULL && memory_protected) {
921 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100922 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000923 } else {
924 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100925 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000926 * Ensure the retriever has the expected state. We don't care
927 * about the MM_MODE_SHARED bit; either with or without it set
928 * are both valid representations of the !O-NA state.
929 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100930 if (vm_id_is_current_world(to.vm->id) &&
931 to.vm->id != HF_PRIMARY_VM_ID &&
932 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
933 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000935 }
J-Alves460d36c2023-10-12 17:02:15 +0100936
937 /*
938 * If memory has been protected before, clear the NS bit to
939 * allow the secure access from the SP.
940 */
941 if (memory_protected) {
942 *to_mode &= ~plat_ffa_other_world_mode();
943 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000944 }
945
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000946 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100947 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000948 *to_mode |= 0;
949 break;
950
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100951 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000952 *to_mode |= MM_MODE_UNOWNED;
953 break;
954
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000956 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
957 break;
958
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100959 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000960 *to_mode |= 0;
961 break;
962
963 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100964 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966 }
967
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100968 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100969}
Jose Marinho09b1db82019-08-08 09:16:59 +0100970
J-Alvescf6253e2024-01-03 13:48:48 +0000971/*
972 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
973 * Returns:
974 * - FFA_SUCCESS_32: if all goes well.
975 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
976 * the page table update. Or error code provided by the function
977 * `arch_memory_protect`.
978 */
979static struct ffa_value ffa_region_group_check_actions(
980 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
981 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
982 bool *memory_protected)
983{
984 struct ffa_value ret;
985 bool is_memory_protected;
986
987 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
988 dlog_verbose(
989 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +0000990 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +0000991 __func__, vm_locked.vm->id, pa_addr(pa_begin),
992 pa_addr(pa_end));
993 return ffa_error(FFA_NO_MEMORY);
994 }
995
996 switch (action) {
997 case MAP_ACTION_CHECK:
998 /* No protect requested. */
999 is_memory_protected = false;
1000 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1001 break;
1002 case MAP_ACTION_CHECK_PROTECT: {
1003 paddr_t last_protected_pa = pa_init(0);
1004
1005 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1006
1007 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1008
1009 /*
1010 * - If protect memory has failed with FFA_DENIED, means some
1011 * range of memory was in the wrong state. In such case, SPM
1012 * reverts the state of the pages that were successfully
1013 * updated.
1014 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1015 * means the platform doesn't support the protection mechanism.
1016 * That said, it still permits the page table update to go
1017 * through. The variable
1018 * `is_memory_protected` will be equal to false.
1019 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1020 * break from switch and return the error.
1021 */
1022 if (ret.func == FFA_ERROR_32) {
1023 assert(!is_memory_protected);
1024 if (ffa_error_code(ret) == FFA_DENIED &&
1025 pa_addr(last_protected_pa) != (uintptr_t)0) {
1026 CHECK(arch_memory_unprotect(
1027 pa_begin,
1028 pa_add(last_protected_pa, PAGE_SIZE)));
1029 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1030 ret = (struct ffa_value){
1031 .func = FFA_SUCCESS_32,
1032 };
1033 }
1034 }
1035 } break;
1036 default:
1037 panic("%s: invalid action to process %x\n", __func__, action);
1038 }
1039
1040 if (memory_protected != NULL) {
1041 *memory_protected = is_memory_protected;
1042 }
1043
1044 return ret;
1045}
1046
1047static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1048 paddr_t pa_begin, paddr_t pa_end,
1049 struct mpool *ppool, uint32_t mode,
1050 enum ffa_map_action action)
1051{
1052 switch (action) {
1053 case MAP_ACTION_COMMIT_UNPROTECT:
1054 /*
1055 * Checking that it should succeed because SPM should be
1056 * unprotecting memory that it had protected before.
1057 */
1058 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1059 case MAP_ACTION_COMMIT:
1060 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1061 NULL);
1062 break;
1063 default:
1064 panic("%s: invalid action to process %x\n", __func__, action);
1065 }
1066}
1067
Jose Marinho09b1db82019-08-08 09:16:59 +01001068/**
J-Alves063ad832023-10-03 18:05:40 +01001069 * Helper function to revert a failed "Protect" action from the SPMC:
1070 * - `fragment_count`: should specify the number of fragments to traverse from
1071 * `fragments`. This may not be the full amount of fragments that are part of
1072 * the share_state structure.
1073 * - `fragment_constituent_counts`: array holding the amount of constituents
1074 * per fragment.
1075 * - `end`: pointer to the constituent that failed the "protect" action. It
1076 * shall be part of the last fragment, and it shall make the loop below break.
1077 */
1078static void ffa_region_group_fragments_revert_protect(
1079 struct ffa_memory_region_constituent **fragments,
1080 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1081 const struct ffa_memory_region_constituent *end)
1082{
1083 for (uint32_t i = 0; i < fragment_count; ++i) {
1084 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1085 struct ffa_memory_region_constituent *constituent =
1086 &fragments[i][j];
1087 size_t size = constituent->page_count * PAGE_SIZE;
1088 paddr_t pa_begin =
1089 pa_from_ipa(ipa_init(constituent->address));
1090 paddr_t pa_end = pa_add(pa_begin, size);
1091
Karl Meakine8937d92024-03-19 16:04:25 +00001092 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001093 __func__, pa_addr(pa_begin), size);
1094
1095 if (constituent == end) {
1096 /*
1097 * The last constituent is expected to be in the
1098 * last fragment.
1099 */
1100 assert(i == fragment_count - 1);
1101 break;
1102 }
1103
1104 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1105 }
1106 }
1107}
1108
1109/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001110 * Updates a VM's page table such that the given set of physical address ranges
1111 * are mapped in the address space at the corresponding address ranges, in the
1112 * mode provided.
1113 *
J-Alves0a83dc22023-05-05 09:50:37 +01001114 * The enum ffa_map_action determines the action taken from a call to the
1115 * function below:
1116 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1117 * mpool but no mappings will actually be updated. This function must always
1118 * be called first with action set to MAP_ACTION_CHECK to check that it will
1119 * succeed before calling ffa_region_group_identity_map with whichever one of
1120 * the remaining actions, to avoid leaving the page table in a half-updated
1121 * state.
1122 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1123 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001124 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1125 * invocation to the monitor to update the security state of the memory,
1126 * to that of the SPMC.
1127 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1128 * with a call into the monitor, to reset the security state of memory
1129 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001130 * vm_ptable_defrag should always be called after a series of page table
1131 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001132 *
J-Alvescf6253e2024-01-03 13:48:48 +00001133 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1134 * error codes:
1135 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1136 * - FFA_DENIED:
1137 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001138 * made to memory mappings.
1139 */
J-Alvescf6253e2024-01-03 13:48:48 +00001140struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001141 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001142 struct ffa_memory_region_constituent **fragments,
1143 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001144 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1145 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001146{
Andrew Walbranca808b12020-05-15 17:22:28 +01001147 uint32_t i;
1148 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001149 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001150
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001151 if (vm_locked.vm->el0_partition) {
1152 mode |= MM_MODE_USER | MM_MODE_NG;
1153 }
1154
Andrew Walbranca808b12020-05-15 17:22:28 +01001155 /* Iterate over the memory region constituents within each fragment. */
1156 for (i = 0; i < fragment_count; ++i) {
1157 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001158 struct ffa_memory_region_constituent *constituent =
1159 &fragments[i][j];
1160 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001161 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001162 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001163 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001164 uint32_t pa_bits =
1165 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001166
1167 /*
1168 * Ensure the requested region falls into system's PA
1169 * range.
1170 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001171 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1172 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001173 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001174 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001175 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001176
J-Alvescf6253e2024-01-03 13:48:48 +00001177 if (action <= MAP_ACTION_CHECK_PROTECT) {
1178 ret = ffa_region_group_check_actions(
1179 vm_locked, pa_begin, pa_end, ppool,
1180 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001181
1182 if (ret.func == FFA_ERROR_32 &&
1183 ffa_error_code(ret) == FFA_DENIED) {
1184 if (memory_protected != NULL) {
1185 assert(!*memory_protected);
1186 }
1187
1188 ffa_region_group_fragments_revert_protect(
1189 fragments,
1190 fragment_constituent_counts,
1191 i + 1, constituent);
1192 break;
1193 }
J-Alvescf6253e2024-01-03 13:48:48 +00001194 } else if (action >= MAP_ACTION_COMMIT &&
1195 action < MAP_ACTION_MAX) {
1196 ffa_region_group_commit_actions(
1197 vm_locked, pa_begin, pa_end, ppool,
1198 mode, action);
1199 ret = (struct ffa_value){
1200 .func = FFA_SUCCESS_32};
1201 } else {
1202 panic("%s: Unknown ffa_map_action.\n",
1203 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001204 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001205 }
1206 }
1207
J-Alvescf6253e2024-01-03 13:48:48 +00001208 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001209}
1210
1211/**
1212 * Clears a region of physical memory by overwriting it with zeros. The data is
1213 * flushed from the cache so the memory has been cleared across the system.
1214 */
J-Alves7db32002021-12-14 14:44:50 +00001215static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1216 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001217{
1218 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001219 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001220 * global mapping of the whole range. Such an approach will limit
1221 * the changes to stage-1 tables and will allow only local
1222 * invalidation.
1223 */
1224 bool ret;
1225 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001226 void *ptr = mm_identity_map(stage1_locked, begin, end,
1227 MM_MODE_W | (extra_mode_attributes &
1228 plat_ffa_other_world_mode()),
1229 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001230 size_t size = pa_difference(begin, end);
1231
1232 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001233 goto fail;
1234 }
1235
1236 memset_s(ptr, size, 0, size);
1237 arch_mm_flush_dcache(ptr, size);
1238 mm_unmap(stage1_locked, begin, end, ppool);
1239
1240 ret = true;
1241 goto out;
1242
1243fail:
1244 ret = false;
1245
1246out:
1247 mm_unlock_stage1(&stage1_locked);
1248
1249 return ret;
1250}
1251
1252/**
1253 * Clears a region of physical memory by overwriting it with zeros. The data is
1254 * flushed from the cache so the memory has been cleared across the system.
1255 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001256static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001257 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001258 struct ffa_memory_region_constituent **fragments,
1259 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1260 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001261{
1262 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001263 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001264 bool ret = false;
1265
1266 /*
1267 * Create a local pool so any freed memory can't be used by another
1268 * thread. This is to ensure each constituent that is mapped can be
1269 * unmapped again afterwards.
1270 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001271 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001272
Andrew Walbranca808b12020-05-15 17:22:28 +01001273 /* Iterate over the memory region constituents within each fragment. */
1274 for (i = 0; i < fragment_count; ++i) {
1275 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001276
J-Alves8457f932023-10-11 16:41:45 +01001277 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001278 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1279 paddr_t begin =
1280 pa_from_ipa(ipa_init(fragments[i][j].address));
1281 paddr_t end = pa_add(begin, size);
1282
J-Alves7db32002021-12-14 14:44:50 +00001283 if (!clear_memory(begin, end, &local_page_pool,
1284 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001285 /*
1286 * api_clear_memory will defrag on failure, so
1287 * no need to do it here.
1288 */
1289 goto out;
1290 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001291 }
1292 }
1293
Jose Marinho09b1db82019-08-08 09:16:59 +01001294 ret = true;
1295
1296out:
1297 mpool_fini(&local_page_pool);
1298 return ret;
1299}
1300
J-Alves5952d942022-12-22 16:03:00 +00001301static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1302 ipaddr_t in_begin, ipaddr_t in_end)
1303{
1304 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1305 ipa_addr(begin) < ipa_addr(in_end)) ||
1306 (ipa_addr(end) <= ipa_addr(in_end) &&
1307 ipa_addr(end) > ipa_addr(in_begin));
1308}
1309
1310/**
1311 * Receives a memory range and looks for overlaps with the remainder
1312 * constituents of the memory share/lend/donate operation. Assumes they are
1313 * passed in order to avoid having to loop over all the elements at each call.
1314 * The function only compares the received memory ranges with those that follow
1315 * within the same fragment, and subsequent fragments from the same operation.
1316 */
1317static bool ffa_memory_check_overlap(
1318 struct ffa_memory_region_constituent **fragments,
1319 const uint32_t *fragment_constituent_counts,
1320 const uint32_t fragment_count, const uint32_t current_fragment,
1321 const uint32_t current_constituent)
1322{
1323 uint32_t i = current_fragment;
1324 uint32_t j = current_constituent;
1325 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1326 const uint32_t current_page_count = fragments[i][j].page_count;
1327 size_t current_size = current_page_count * PAGE_SIZE;
1328 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1329
1330 if (current_size == 0 ||
1331 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001332 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1333 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001334 return false;
1335 }
1336
1337 for (; i < fragment_count; i++) {
1338 j = (i == current_fragment) ? j + 1 : 0;
1339
1340 for (; j < fragment_constituent_counts[i]; j++) {
1341 ipaddr_t begin = ipa_init(fragments[i][j].address);
1342 const uint32_t page_count = fragments[i][j].page_count;
1343 size_t size = page_count * PAGE_SIZE;
1344 ipaddr_t end = ipa_add(begin, size - 1);
1345
1346 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1347 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001348 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001349 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001350 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001351 return false;
1352 }
1353
1354 /*
1355 * Check if current ranges is within begin and end, as
1356 * well as the reverse. This should help optimize the
1357 * loop, and reduce the number of iterations.
1358 */
1359 if (is_memory_range_within(begin, end, current_begin,
1360 current_end) ||
1361 is_memory_range_within(current_begin, current_end,
1362 begin, end)) {
1363 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001364 "Overlapping memory ranges: %#lx - "
1365 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001366 ipa_addr(begin), ipa_addr(end),
1367 ipa_addr(current_begin),
1368 ipa_addr(current_end));
1369 return true;
1370 }
1371 }
1372 }
1373
1374 return false;
1375}
1376
Jose Marinho09b1db82019-08-08 09:16:59 +01001377/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001378 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001379 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001380 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001381 *
1382 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001383 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001384 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001385 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001386 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1387 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001390 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001391 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001392static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001393 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001394 struct ffa_memory_region_constituent **fragments,
1395 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001396 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001397 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1398 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001399{
Andrew Walbranca808b12020-05-15 17:22:28 +01001400 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001401 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001402 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001403 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001404 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001405 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001406 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001407 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001408 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001409 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001410
1411 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 * Make sure constituents are properly aligned to a 64-bit boundary. If
1413 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001414 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001415 for (i = 0; i < fragment_count; ++i) {
1416 if (!is_aligned(fragments[i], 8)) {
1417 dlog_verbose("Constituents not aligned.\n");
1418 return ffa_error(FFA_INVALID_PARAMETERS);
1419 }
J-Alves8f11cde2022-12-21 16:18:22 +00001420 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1421 constituents_total_page_count +=
1422 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001423 if (ffa_memory_check_overlap(
1424 fragments, fragment_constituent_counts,
1425 fragment_count, i, j)) {
1426 return ffa_error(FFA_INVALID_PARAMETERS);
1427 }
J-Alves8f11cde2022-12-21 16:18:22 +00001428 }
1429 }
1430
1431 if (constituents_total_page_count != composite_total_page_count) {
1432 dlog_verbose(
1433 "Composite page count differs from calculated page "
1434 "count from constituents.\n");
1435 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001436 }
1437
1438 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001439 * Check if the state transition is lawful for the sender, ensure that
1440 * all constituents of a memory region being shared are at the same
1441 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001442 */
J-Alves460d36c2023-10-12 17:02:15 +01001443 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001444 from_locked, share_func, memory_region, &orig_from_mode,
1445 fragments, fragment_constituent_counts, fragment_count,
1446 &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001447 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001448 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001449 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001450 }
1451
Andrew Walbran37c574e2020-06-03 11:45:46 +01001452 if (orig_from_mode_ret != NULL) {
1453 *orig_from_mode_ret = orig_from_mode;
1454 }
1455
Jose Marinho09b1db82019-08-08 09:16:59 +01001456 /*
1457 * Create a local pool so any freed memory can't be used by another
1458 * thread. This is to ensure the original mapping can be restored if the
1459 * clear fails.
1460 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001461 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001462
1463 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 * First reserve all required memory for the new page table entries
1465 * without committing, to make sure the entire operation will succeed
1466 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001467 * Provide the map_action as populated by 'ffa_send_check_transition'.
1468 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001469 */
J-Alvescf6253e2024-01-03 13:48:48 +00001470 ret = ffa_region_group_identity_map(
1471 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001472 fragment_count, from_mode, page_pool, map_action,
1473 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001474 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001475 goto out;
1476 }
1477
1478 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001479 * Update the mapping for the sender. This won't allocate because the
1480 * transaction was already prepared above, but may free pages in the
1481 * case that a whole block is being unmapped that was previously
1482 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001483 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001484 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001485 from_locked, fragments, fragment_constituent_counts,
1486 fragment_count, from_mode, &local_page_pool,
1487 MAP_ACTION_COMMIT, NULL)
1488 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001489
J-Alves460d36c2023-10-12 17:02:15 +01001490 /*
1491 * If memory has been protected, it is now part of the secure PAS
1492 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1493 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1494 * SPM's S1 translation.
1495 * In case memory hasn't been protected, and it is in the non-secure
1496 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1497 * perform a non-secure memory access. In such case `clean_mode` takes
1498 * the same mode as `orig_from_mode`.
1499 */
1500 clean_mode = (memory_protected != NULL && *memory_protected)
1501 ? orig_from_mode & ~plat_ffa_other_world_mode()
1502 : orig_from_mode;
1503
Jose Marinho09b1db82019-08-08 09:16:59 +01001504 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001505 if (clear && !ffa_clear_memory_constituents(
1506 clean_mode, fragments, fragment_constituent_counts,
1507 fragment_count, page_pool)) {
1508 map_action = (memory_protected != NULL && *memory_protected)
1509 ? MAP_ACTION_COMMIT_UNPROTECT
1510 : MAP_ACTION_COMMIT;
1511
Jose Marinho09b1db82019-08-08 09:16:59 +01001512 /*
1513 * On failure, roll back by returning memory to the sender. This
1514 * may allocate pages which were previously freed into
1515 * `local_page_pool` by the call above, but will never allocate
1516 * more pages than that so can never fail.
1517 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001519 from_locked, fragments,
1520 fragment_constituent_counts, fragment_count,
1521 orig_from_mode, &local_page_pool,
1522 MAP_ACTION_COMMIT, NULL)
1523 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001524 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001525 goto out;
1526 }
1527
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001528 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001529
1530out:
1531 mpool_fini(&local_page_pool);
1532
1533 /*
1534 * Tidy up the page table by reclaiming failed mappings (if there was an
1535 * error) or merging entries into blocks where possible (on success).
1536 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001537 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001538
1539 return ret;
1540}
1541
1542/**
1543 * Validates and maps memory shared from one VM to another.
1544 *
1545 * This function requires the calling context to hold the <to> lock.
1546 *
1547 * Returns:
1548 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001549 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001550 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001553 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001554 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001555struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001556 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001557 struct ffa_memory_region_constituent **fragments,
1558 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001559 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001560 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001561{
Andrew Walbranca808b12020-05-15 17:22:28 +01001562 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001563 uint32_t to_mode;
1564 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001565 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001566 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001567
1568 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 * Make sure constituents are properly aligned to a 64-bit boundary. If
1570 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001571 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001572 for (i = 0; i < fragment_count; ++i) {
1573 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001574 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001575 return ffa_error(FFA_INVALID_PARAMETERS);
1576 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001577 }
1578
1579 /*
1580 * Check if the state transition is lawful for the recipient, and ensure
1581 * that all constituents of the memory region being retrieved are at the
1582 * same state.
1583 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001584 ret = ffa_retrieve_check_transition(
1585 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001586 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1587 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001589 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001590 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001591 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001592 }
1593
1594 /*
1595 * Create a local pool so any freed memory can't be used by another
1596 * thread. This is to ensure the original mapping can be restored if the
1597 * clear fails.
1598 */
1599 mpool_init_with_fallback(&local_page_pool, page_pool);
1600
1601 /*
1602 * First reserve all required memory for the new page table entries in
1603 * the recipient page tables without committing, to make sure the entire
1604 * operation will succeed without exhausting the page pool.
1605 */
J-Alvescf6253e2024-01-03 13:48:48 +00001606 ret = ffa_region_group_identity_map(
1607 to_locked, fragments, fragment_constituent_counts,
1608 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1609 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001610 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001611 goto out;
1612 }
1613
1614 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001615 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001616 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1617 fragment_constituent_counts,
1618 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001619 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001621 goto out;
1622 }
1623
Jose Marinho09b1db82019-08-08 09:16:59 +01001624 /*
1625 * Complete the transfer by mapping the memory into the recipient. This
1626 * won't allocate because the transaction was already prepared above, so
1627 * it doesn't need to use the `local_page_pool`.
1628 */
J-Alvesfd206052023-05-22 16:45:00 +01001629 CHECK(ffa_region_group_identity_map(
1630 to_locked, fragments, fragment_constituent_counts,
1631 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001632 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001633
J-Alves460d36c2023-10-12 17:02:15 +01001634 /* Return the mode used in mapping the memory in retriever's PT. */
1635 if (response_mode != NULL) {
1636 *response_mode = to_mode;
1637 }
1638
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001639 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001640
1641out:
1642 mpool_fini(&local_page_pool);
1643
1644 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001645 * Tidy up the page table by reclaiming failed mappings (if there was an
1646 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001647 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001648 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001649
1650 return ret;
1651}
1652
Andrew Walbran996d1d12020-05-27 14:08:43 +01001653static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001654 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001655 struct ffa_memory_region_constituent **fragments,
1656 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1657 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001658{
1659 uint32_t orig_from_mode;
1660 uint32_t from_mode;
1661 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001662 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001663
Andrew Walbranca808b12020-05-15 17:22:28 +01001664 ret = ffa_relinquish_check_transition(
1665 from_locked, &orig_from_mode, fragments,
1666 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001667 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001668 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001669 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001670 }
1671
1672 /*
1673 * Create a local pool so any freed memory can't be used by another
1674 * thread. This is to ensure the original mapping can be restored if the
1675 * clear fails.
1676 */
1677 mpool_init_with_fallback(&local_page_pool, page_pool);
1678
1679 /*
1680 * First reserve all required memory for the new page table entries
1681 * without committing, to make sure the entire operation will succeed
1682 * without exhausting the page pool.
1683 */
J-Alvescf6253e2024-01-03 13:48:48 +00001684 ret = ffa_region_group_identity_map(
1685 from_locked, fragments, fragment_constituent_counts,
1686 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1687 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001688 goto out;
1689 }
1690
1691 /*
1692 * Update the mapping for the sender. This won't allocate because the
1693 * transaction was already prepared above, but may free pages in the
1694 * case that a whole block is being unmapped that was previously
1695 * partially mapped.
1696 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001697 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001698 from_locked, fragments, fragment_constituent_counts,
1699 fragment_count, from_mode, &local_page_pool,
1700 MAP_ACTION_COMMIT, NULL)
1701 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001702
1703 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001704 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001705 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1706 fragment_constituent_counts,
1707 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001708 /*
1709 * On failure, roll back by returning memory to the sender. This
1710 * may allocate pages which were previously freed into
1711 * `local_page_pool` by the call above, but will never allocate
1712 * more pages than that so can never fail.
1713 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001714 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001715 from_locked, fragments,
1716 fragment_constituent_counts, fragment_count,
1717 orig_from_mode, &local_page_pool,
1718 MAP_ACTION_COMMIT, NULL)
1719 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001720
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001721 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001722 goto out;
1723 }
1724
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001725 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001726
1727out:
1728 mpool_fini(&local_page_pool);
1729
1730 /*
1731 * Tidy up the page table by reclaiming failed mappings (if there was an
1732 * error) or merging entries into blocks where possible (on success).
1733 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001734 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001735
1736 return ret;
1737}
1738
1739/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001740 * Complete a memory sending operation by checking that it is valid, updating
1741 * the sender page table, and then either marking the share state as having
1742 * completed sending (on success) or freeing it (on failure).
1743 *
1744 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1745 */
J-Alvesfdd29272022-07-19 13:16:31 +01001746struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001747 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001748 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1749 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001750{
1751 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001752 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001753 struct ffa_value ret;
1754
1755 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001756 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001757 assert(memory_region != NULL);
1758 composite = ffa_memory_region_get_composite(memory_region, 0);
1759 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001760
1761 /* Check that state is valid in sender page table and update. */
1762 ret = ffa_send_check_update(
1763 from_locked, share_state->fragments,
1764 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001765 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001766 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001767 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001768 if (ret.func != FFA_SUCCESS_32) {
1769 /*
1770 * Free share state, it failed to send so it can't be retrieved.
1771 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001772 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1773 __func__, ffa_func_name(ret.func),
1774 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001775 share_state_free(share_states, share_state, page_pool);
1776 return ret;
1777 }
1778
1779 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001780 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001781
J-Alvesee68c542020-10-29 17:48:20 +00001782 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001783}
1784
1785/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001786 * Check that the memory attributes match Hafnium expectations.
1787 * Cacheability:
1788 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1789 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1790 *
1791 * Shareability:
1792 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001793 */
1794static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001795 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001796{
1797 enum ffa_memory_type memory_type;
1798 enum ffa_memory_cacheability cacheability;
1799 enum ffa_memory_shareability shareability;
1800
Karl Meakin84710f32023-10-12 15:14:49 +01001801 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001802 cacheability = attributes.cacheability;
1803 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1804 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1805 dlog_verbose(
1806 "Normal Memory: Invalid cacheability %s, "
1807 "expected %s.\n",
1808 ffa_memory_cacheability_name(cacheability),
1809 ffa_memory_cacheability_name(
1810 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001811 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001812 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001813 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1814 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1815 dlog_verbose(
1816 "Device Memory: Invalid cacheability %s, "
1817 "expected %s.\n",
1818 ffa_device_memory_cacheability_name(cacheability),
1819 ffa_device_memory_cacheability_name(
1820 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001821 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001822 }
1823
Karl Meakin84710f32023-10-12 15:14:49 +01001824 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001825 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001826 dlog_verbose("Invalid shareability %s, expected %s.\n",
1827 ffa_memory_shareability_name(shareability),
1828 ffa_memory_shareability_name(
1829 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001830 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001831 }
1832
1833 return (struct ffa_value){.func = FFA_SUCCESS_32};
1834}
1835
1836/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001837 * Check that the given `memory_region` represents a valid memory send request
1838 * of the given `share_func` type, return the clear flag and permissions via the
1839 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001840 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001841 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001842 * not.
1843 */
J-Alves66652252022-07-06 09:49:51 +01001844struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001845 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1846 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001847 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001848{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001849 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001850 struct ffa_memory_access *receiver =
1851 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001852 uint64_t receivers_end;
1853 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001854 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001855 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001856 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001857 enum ffa_data_access data_access;
1858 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001859 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001860 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001861 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001862 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001863 memory_region->receivers_offset +
1864 memory_region->memory_access_desc_size +
1865 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001866
1867 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001868 dlog_verbose("Fragment length %u too short (min %zu).\n",
1869 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001870 return ffa_error(FFA_INVALID_PARAMETERS);
1871 }
1872
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001873 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1874 "struct ffa_memory_region_constituent must be 16 bytes");
1875 if (!is_aligned(fragment_length,
1876 sizeof(struct ffa_memory_region_constituent)) ||
1877 !is_aligned(memory_share_length,
1878 sizeof(struct ffa_memory_region_constituent))) {
1879 dlog_verbose(
1880 "Fragment length %u or total length %u"
1881 " is not 16-byte aligned.\n",
1882 fragment_length, memory_share_length);
1883 return ffa_error(FFA_INVALID_PARAMETERS);
1884 }
1885
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001886 if (fragment_length > memory_share_length) {
1887 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001888 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001889 (size_t)fragment_length, (size_t)memory_share_length);
1890 return ffa_error(FFA_INVALID_PARAMETERS);
1891 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001892
J-Alves95df0ef2022-12-07 10:09:48 +00001893 /* The sender must match the caller. */
1894 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1895 vm_id_is_current_world(memory_region->sender)) ||
1896 (vm_id_is_current_world(from_locked.vm->id) &&
1897 memory_region->sender != from_locked.vm->id)) {
1898 dlog_verbose("Invalid memory sender ID.\n");
1899 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001900 }
1901
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001902 if (memory_region->receiver_count <= 0) {
1903 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001904 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001905 }
1906
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001907 /*
1908 * Ensure that the composite header is within the memory bounds and
1909 * doesn't overlap the first part of the message. Cast to uint64_t
1910 * to prevent overflow.
1911 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001912 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001913 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001914 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001915 min_length = receivers_end +
1916 sizeof(struct ffa_composite_memory_region) +
1917 sizeof(struct ffa_memory_region_constituent);
1918 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001919 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001920 (size_t)memory_share_length, (size_t)min_length);
1921 return ffa_error(FFA_INVALID_PARAMETERS);
1922 }
1923
1924 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001925 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001926
1927 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001928 * Check that the composite memory region descriptor is after the access
1929 * descriptors, is at least 16-byte aligned, and fits in the first
1930 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001931 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001932 if ((composite_memory_region_offset < receivers_end) ||
1933 (composite_memory_region_offset % 16 != 0) ||
1934 (composite_memory_region_offset >
1935 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1936 dlog_verbose(
1937 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00001938 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001939 (size_t)composite_memory_region_offset);
1940 return ffa_error(FFA_INVALID_PARAMETERS);
1941 }
1942
1943 /*
1944 * Compute the start of the constituent regions. Already checked
1945 * to be not more than fragment_length and thus not more than
1946 * memory_share_length.
1947 */
1948 constituents_start = composite_memory_region_offset +
1949 sizeof(struct ffa_composite_memory_region);
1950 constituents_length = memory_share_length - constituents_start;
1951
1952 /*
1953 * Check that the number of constituents is consistent with the length
1954 * of the constituent region.
1955 */
1956 composite = ffa_memory_region_get_composite(memory_region, 0);
1957 if ((constituents_length %
1958 sizeof(struct ffa_memory_region_constituent) !=
1959 0) ||
1960 ((constituents_length /
1961 sizeof(struct ffa_memory_region_constituent)) !=
1962 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001963 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001964 (size_t)memory_share_length,
1965 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001966 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001967 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001968 if (fragment_length < memory_share_length &&
1969 fragment_length < HF_MAILBOX_SIZE) {
1970 dlog_warning(
1971 "Initial fragment length %d smaller than mailbox "
1972 "size.\n",
1973 fragment_length);
1974 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001975
Andrew Walbrana65a1322020-04-06 19:32:32 +01001976 /*
1977 * Clear is not allowed for memory sharing, as the sender still has
1978 * access to the memory.
1979 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001980 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1981 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001982 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001983 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001984 }
1985
1986 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001988 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001989 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001990 }
1991
J-Alves363f5722022-04-25 17:37:37 +01001992 /* Check that the permissions are valid, for each specified receiver. */
1993 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001994 struct ffa_memory_region_attributes receiver_permissions;
1995
1996 receiver = ffa_memory_region_get_receiver(memory_region, i);
1997 assert(receiver != NULL);
1998 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001999 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002000 receiver_permissions.permissions;
2001 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002002
2003 if (memory_region->sender == receiver_id) {
2004 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002005 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002006 }
Federico Recanati85090c42021-12-15 13:17:54 +01002007
J-Alves363f5722022-04-25 17:37:37 +01002008 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2009 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002010 struct ffa_memory_access *other_receiver =
2011 ffa_memory_region_get_receiver(memory_region,
2012 j);
2013 assert(other_receiver != NULL);
2014
J-Alves363f5722022-04-25 17:37:37 +01002015 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002016 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002017 dlog_verbose(
2018 "Repeated receiver(%x) in memory send "
2019 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002020 other_receiver->receiver_permissions
2021 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002022 return ffa_error(FFA_INVALID_PARAMETERS);
2023 }
2024 }
2025
2026 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002027 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002028 dlog_verbose(
2029 "All ffa_memory_access should point to the "
2030 "same composite memory region offset.\n");
2031 return ffa_error(FFA_INVALID_PARAMETERS);
2032 }
2033
Karl Meakin84710f32023-10-12 15:14:49 +01002034 data_access = permissions.data_access;
2035 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002036 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2037 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2038 dlog_verbose(
2039 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002040 "(data_access = %s, instruction_access = %s)\n",
2041 ffa_data_access_name(data_access),
2042 ffa_instruction_access_name(
2043 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002044 return ffa_error(FFA_INVALID_PARAMETERS);
2045 }
2046 if (instruction_access !=
2047 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2048 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002049 "Invalid instruction access permissions %s "
2050 "for sending memory, expected %s.\n",
2051 ffa_instruction_access_name(instruction_access),
2052 ffa_instruction_access_name(
2053 FFA_INSTRUCTION_ACCESS_RESERVED));
J-Alves363f5722022-04-25 17:37:37 +01002054 return ffa_error(FFA_INVALID_PARAMETERS);
2055 }
2056 if (share_func == FFA_MEM_SHARE_32) {
2057 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2058 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002059 "Invalid data access permissions %s "
2060 "for sharing memory, expected %s.\n",
2061 ffa_data_access_name(data_access),
2062 ffa_data_access_name(
2063 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002064 return ffa_error(FFA_INVALID_PARAMETERS);
2065 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002066 /*
2067 * According to section 10.10.3 of the FF-A v1.1 EAC0
2068 * spec, NX is required for share operations (but must
2069 * not be specified by the sender) so set it in the
2070 * copy that we store, ready to be returned to the
2071 * retriever.
2072 */
2073 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002074 permissions.instruction_access =
2075 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002076 receiver_permissions.permissions = permissions;
2077 }
J-Alves363f5722022-04-25 17:37:37 +01002078 }
2079 if (share_func == FFA_MEM_LEND_32 &&
2080 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2081 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002082 "Invalid data access permissions %s for "
2083 "lending memory, expected %s.\n",
2084 ffa_data_access_name(data_access),
2085 ffa_data_access_name(
2086 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002087 return ffa_error(FFA_INVALID_PARAMETERS);
2088 }
2089
2090 if (share_func == FFA_MEM_DONATE_32 &&
2091 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2092 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002093 "Invalid data access permissions %s for "
2094 "donating memory, expected %s.\n",
2095 ffa_data_access_name(data_access),
2096 ffa_data_access_name(
2097 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002098 return ffa_error(FFA_INVALID_PARAMETERS);
2099 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002100 }
2101
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002102 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002103 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002104 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2105 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002106 "Invalid security state %s for memory share operation, "
2107 "expected %s.\n",
2108 ffa_memory_security_name(security_state),
2109 ffa_memory_security_name(
2110 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002111 return ffa_error(FFA_INVALID_PARAMETERS);
2112 }
2113
Federico Recanatid937f5e2021-12-20 17:38:23 +01002114 /*
J-Alves807794e2022-06-16 13:42:47 +01002115 * If a memory donate or lend with single borrower, the memory type
2116 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002117 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002118 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002119 if (share_func == FFA_MEM_DONATE_32 ||
2120 (share_func == FFA_MEM_LEND_32 &&
2121 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002122 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002123 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002124 "Invalid memory type %s for memory share "
2125 "operation, expected %s.\n",
2126 ffa_memory_type_name(type),
2127 ffa_memory_type_name(
2128 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002129 return ffa_error(FFA_INVALID_PARAMETERS);
2130 }
2131 } else {
2132 /*
2133 * Check that sender's memory attributes match Hafnium
2134 * expectations: Normal Memory, Inner shareable, Write-Back
2135 * Read-Allocate Write-Allocate Cacheable.
2136 */
2137 ret = ffa_memory_attributes_validate(memory_region->attributes);
2138 if (ret.func != FFA_SUCCESS_32) {
2139 return ret;
2140 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002141 }
2142
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002143 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002144}
2145
2146/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002147 * Gets the share state for continuing an operation to donate, lend or share
2148 * memory, and checks that it is a valid request.
2149 *
2150 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2151 * not.
2152 */
J-Alvesfdd29272022-07-19 13:16:31 +01002153struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002154 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002155 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002156 struct mpool *page_pool)
2157{
2158 struct ffa_memory_share_state *share_state;
2159 struct ffa_memory_region *memory_region;
2160
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002161 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002162
2163 /*
2164 * Look up the share state by handle and make sure that the VM ID
2165 * matches.
2166 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002167 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002168 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002169 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002170 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002171 handle);
2172 return ffa_error(FFA_INVALID_PARAMETERS);
2173 }
2174 memory_region = share_state->memory_region;
2175
J-Alvesfdd29272022-07-19 13:16:31 +01002176 if (vm_id_is_current_world(from_vm_id) &&
2177 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002178 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2179 return ffa_error(FFA_INVALID_PARAMETERS);
2180 }
2181
2182 if (share_state->sending_complete) {
2183 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002184 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002185 handle);
2186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
2188
2189 if (share_state->fragment_count == MAX_FRAGMENTS) {
2190 /*
2191 * Log a warning as this is a sign that MAX_FRAGMENTS should
2192 * probably be increased.
2193 */
2194 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002195 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002196 "only %d supported.\n",
2197 handle, MAX_FRAGMENTS);
2198 /* Free share state, as it's not possible to complete it. */
2199 share_state_free(share_states, share_state, page_pool);
2200 return ffa_error(FFA_NO_MEMORY);
2201 }
2202
2203 *share_state_ret = share_state;
2204
2205 return (struct ffa_value){.func = FFA_SUCCESS_32};
2206}
2207
2208/**
J-Alves95df0ef2022-12-07 10:09:48 +00002209 * Checks if there is at least one receiver from the other world.
2210 */
J-Alvesfdd29272022-07-19 13:16:31 +01002211bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002212 struct ffa_memory_region *memory_region)
2213{
2214 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002215 struct ffa_memory_access *receiver =
2216 ffa_memory_region_get_receiver(memory_region, i);
2217 assert(receiver != NULL);
2218 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2219
2220 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002221 return true;
2222 }
2223 }
2224 return false;
2225}
2226
2227/**
J-Alves9da280b2022-12-21 14:55:39 +00002228 * Validates a call to donate, lend or share memory in which Hafnium is the
2229 * designated allocator of the memory handle. In practice, this also means
2230 * Hafnium is responsible for managing the state structures for the transaction.
2231 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2232 * sender is an SP or there is at least one borrower that is an SP.
2233 * If Hafnium is the hypervisor, it should allocate the memory handle when
2234 * operation involves only NWd VMs.
2235 *
2236 * If validation goes well, Hafnium updates the stage-2 page tables of the
2237 * sender. Validation consists of checking if the message length and number of
2238 * memory region constituents match, and if the transition is valid for the
2239 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002240 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002241 * Assumes that the caller has already found and locked the sender VM and copied
2242 * the memory region descriptor from the sender's TX buffer to a freshly
2243 * allocated page from Hafnium's internal pool. The caller must have also
2244 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002245 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002246 * This function takes ownership of the `memory_region` passed in and will free
2247 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002248 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002249struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002250 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002251 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002252 uint32_t fragment_length, uint32_t share_func,
2253 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002254{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002255 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002256 struct share_states_locked share_states;
2257 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002258
2259 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002260 * If there is an error validating the `memory_region` then we need to
2261 * free it because we own it but we won't be storing it in a share state
2262 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002263 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002264 ret = ffa_memory_send_validate(from_locked, memory_region,
2265 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002266 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002267 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002268 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002269 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002270 }
2271
Andrew Walbrana65a1322020-04-06 19:32:32 +01002272 /* Set flag for share function, ready to be retrieved later. */
2273 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002274 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002275 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002276 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002277 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 case FFA_MEM_LEND_32:
2279 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002280 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002281 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002282 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002283 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002284 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002285 }
2286
Andrew Walbranca808b12020-05-15 17:22:28 +01002287 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002288 /*
2289 * Allocate a share state before updating the page table. Otherwise if
2290 * updating the page table succeeded but allocating the share state
2291 * failed then it would leave the memory in a state where nobody could
2292 * get it back.
2293 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002294 share_state = allocate_share_state(share_states, share_func,
2295 memory_region, fragment_length,
2296 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002297 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002298 dlog_verbose("Failed to allocate share state.\n");
2299 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002300 ret = ffa_error(FFA_NO_MEMORY);
2301 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002302 }
2303
Andrew Walbranca808b12020-05-15 17:22:28 +01002304 if (fragment_length == memory_share_length) {
2305 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002306 ret = ffa_memory_send_complete(
2307 from_locked, share_states, share_state, page_pool,
2308 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002309 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002310 /*
2311 * Use sender ID from 'memory_region' assuming
2312 * that at this point it has been validated:
2313 * - MBZ at virtual FF-A instance.
2314 */
J-Alves19e20cf2023-08-02 12:48:55 +01002315 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002316 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2317 ? memory_region->sender
2318 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002319 ret = (struct ffa_value){
2320 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002321 .arg1 = (uint32_t)memory_region->handle,
2322 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002323 .arg3 = fragment_length,
2324 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002325 }
2326
2327out:
2328 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002329 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002330 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002331}
2332
2333/**
J-Alves8505a8a2022-06-15 18:10:18 +01002334 * Continues an operation to donate, lend or share memory to a VM from current
2335 * world. If this is the last fragment then checks that the transition is valid
2336 * for the type of memory sending operation and updates the stage-2 page tables
2337 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002338 *
2339 * Assumes that the caller has already found and locked the sender VM and copied
2340 * the memory region descriptor from the sender's TX buffer to a freshly
2341 * allocated page from Hafnium's internal pool.
2342 *
2343 * This function takes ownership of the `fragment` passed in; it must not be
2344 * freed by the caller.
2345 */
2346struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2347 void *fragment,
2348 uint32_t fragment_length,
2349 ffa_memory_handle_t handle,
2350 struct mpool *page_pool)
2351{
2352 struct share_states_locked share_states = share_states_lock();
2353 struct ffa_memory_share_state *share_state;
2354 struct ffa_value ret;
2355 struct ffa_memory_region *memory_region;
2356
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002357 CHECK(is_aligned(fragment,
2358 alignof(struct ffa_memory_region_constituent)));
2359 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2360 0) {
2361 dlog_verbose("Fragment length %u misaligned.\n",
2362 fragment_length);
2363 ret = ffa_error(FFA_INVALID_PARAMETERS);
2364 goto out_free_fragment;
2365 }
2366
Andrew Walbranca808b12020-05-15 17:22:28 +01002367 ret = ffa_memory_send_continue_validate(share_states, handle,
2368 &share_state,
2369 from_locked.vm->id, page_pool);
2370 if (ret.func != FFA_SUCCESS_32) {
2371 goto out_free_fragment;
2372 }
2373 memory_region = share_state->memory_region;
2374
J-Alves95df0ef2022-12-07 10:09:48 +00002375 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002376 dlog_error(
2377 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002378 "other world. This should never happen, and indicates "
2379 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002380 "EL3 code.\n");
2381 ret = ffa_error(FFA_INVALID_PARAMETERS);
2382 goto out_free_fragment;
2383 }
2384
2385 /* Add this fragment. */
2386 share_state->fragments[share_state->fragment_count] = fragment;
2387 share_state->fragment_constituent_counts[share_state->fragment_count] =
2388 fragment_length / sizeof(struct ffa_memory_region_constituent);
2389 share_state->fragment_count++;
2390
2391 /* Check whether the memory send operation is now ready to complete. */
2392 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002393 ret = ffa_memory_send_complete(
2394 from_locked, share_states, share_state, page_pool,
2395 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002396 } else {
2397 ret = (struct ffa_value){
2398 .func = FFA_MEM_FRAG_RX_32,
2399 .arg1 = (uint32_t)handle,
2400 .arg2 = (uint32_t)(handle >> 32),
2401 .arg3 = share_state_next_fragment_offset(share_states,
2402 share_state)};
2403 }
2404 goto out;
2405
2406out_free_fragment:
2407 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002408
2409out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002410 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002411 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002412}
2413
Andrew Walbranca808b12020-05-15 17:22:28 +01002414/** Clean up after the receiver has finished retrieving a memory region. */
2415static void ffa_memory_retrieve_complete(
2416 struct share_states_locked share_states,
2417 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2418{
2419 if (share_state->share_func == FFA_MEM_DONATE_32) {
2420 /*
2421 * Memory that has been donated can't be relinquished,
2422 * so no need to keep the share state around.
2423 */
2424 share_state_free(share_states, share_state, page_pool);
2425 dlog_verbose("Freed share state for donate.\n");
2426 }
2427}
2428
J-Alves2d8457f2022-10-05 11:06:41 +01002429/**
2430 * Initialises the given memory region descriptor to be used for an
2431 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2432 * fragment.
2433 * The memory region descriptor is initialized according to retriever's
2434 * FF-A version.
2435 *
2436 * Returns true on success, or false if the given constituents won't all fit in
2437 * the first fragment.
2438 */
2439static bool ffa_retrieved_memory_region_init(
2440 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002441 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002442 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002443 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002444 struct ffa_memory_access *receivers, size_t receiver_count,
2445 uint32_t memory_access_desc_size, uint32_t page_count,
2446 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002447 const struct ffa_memory_region_constituent constituents[],
2448 uint32_t fragment_constituent_count, uint32_t *total_length,
2449 uint32_t *fragment_length)
2450{
2451 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002452 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002453 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002454 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002455
2456 assert(response != NULL);
2457
2458 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2459 struct ffa_memory_region_v1_0 *retrieve_response =
2460 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002461 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002462
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002463 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2464 attributes, flags, handle, 0,
2465 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002466
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002467 receiver = (struct ffa_memory_access_v1_0 *)
2468 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002469 receiver_count = retrieve_response->receiver_count;
2470
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002471 for (uint32_t i = 0; i < receiver_count; i++) {
2472 ffa_id_t receiver_id =
2473 receivers[i].receiver_permissions.receiver;
2474 ffa_memory_receiver_flags_t recv_flags =
2475 receivers[i].receiver_permissions.flags;
2476
2477 /*
2478 * Initialized here as in memory retrieve responses we
2479 * currently expect one borrower to be specified.
2480 */
2481 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002482 receiver, receiver_id, permissions.data_access,
2483 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002484 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002485
2486 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002487 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002488 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2489 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002490
2491 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2492 retrieve_response, 0);
2493 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002494 struct ffa_memory_region *retrieve_response =
2495 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002496 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002497
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002498 ffa_memory_region_init_header(
2499 retrieve_response, sender, attributes, flags, handle, 0,
2500 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002501
2502 /*
2503 * Note that `sizeof(struct_ffa_memory_region)` and
2504 * `sizeof(struct ffa_memory_access)` must both be multiples of
2505 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2506 * guaranteed that the offset we calculate here is aligned to a
2507 * 64-bit boundary and so 64-bit values can be copied without
2508 * alignment faults.
2509 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002510 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002511 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002512 (uint32_t)(receiver_count *
2513 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002514
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002515 retrieve_response_receivers =
2516 ffa_memory_region_get_receiver(retrieve_response, 0);
2517 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002518
2519 /*
2520 * Initialized here as in memory retrieve responses we currently
2521 * expect one borrower to be specified.
2522 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002523 memcpy_s(retrieve_response_receivers,
2524 sizeof(struct ffa_memory_access) * receiver_count,
2525 receivers,
2526 sizeof(struct ffa_memory_access) * receiver_count);
2527
2528 retrieve_response_receivers->composite_memory_region_offset =
2529 composite_offset;
2530
J-Alves2d8457f2022-10-05 11:06:41 +01002531 composite_memory_region =
2532 ffa_memory_region_get_composite(retrieve_response, 0);
2533 }
2534
J-Alves2d8457f2022-10-05 11:06:41 +01002535 assert(composite_memory_region != NULL);
2536
J-Alves2d8457f2022-10-05 11:06:41 +01002537 composite_memory_region->page_count = page_count;
2538 composite_memory_region->constituent_count = total_constituent_count;
2539 composite_memory_region->reserved_0 = 0;
2540
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002541 constituents_offset =
2542 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002543 if (constituents_offset +
2544 fragment_constituent_count *
2545 sizeof(struct ffa_memory_region_constituent) >
2546 response_max_size) {
2547 return false;
2548 }
2549
2550 for (i = 0; i < fragment_constituent_count; ++i) {
2551 composite_memory_region->constituents[i] = constituents[i];
2552 }
2553
2554 if (total_length != NULL) {
2555 *total_length =
2556 constituents_offset +
2557 composite_memory_region->constituent_count *
2558 sizeof(struct ffa_memory_region_constituent);
2559 }
2560 if (fragment_length != NULL) {
2561 *fragment_length =
2562 constituents_offset +
2563 fragment_constituent_count *
2564 sizeof(struct ffa_memory_region_constituent);
2565 }
2566
2567 return true;
2568}
2569
J-Alves96de29f2022-04-26 16:05:24 +01002570/**
2571 * Validates the retrieved permissions against those specified by the lender
2572 * of memory share operation. Optionally can help set the permissions to be used
2573 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002574 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2575 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2576 * specification for each ABI.
2577 * - FFA_DENIED -> if the permissions specified by the retriever are not
2578 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002579 */
J-Alvesdcad8992023-09-15 14:10:35 +01002580static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2581 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002582 enum ffa_data_access requested_data_access,
2583 enum ffa_instruction_access sent_instruction_access,
2584 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002585 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002586{
2587 switch (sent_data_access) {
2588 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2589 case FFA_DATA_ACCESS_RW:
2590 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2591 requested_data_access == FFA_DATA_ACCESS_RW) {
2592 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002593 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002594 }
2595 break;
2596 }
2597 /* Intentional fall-through. */
2598 case FFA_DATA_ACCESS_RO:
2599 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2600 requested_data_access == FFA_DATA_ACCESS_RO) {
2601 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002602 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002603 }
2604 break;
2605 }
2606 dlog_verbose(
2607 "Invalid data access requested; sender specified "
2608 "permissions %#x but receiver requested %#x.\n",
2609 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002610 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002611 case FFA_DATA_ACCESS_RESERVED:
2612 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2613 "checked before this point.");
2614 }
2615
J-Alvesdcad8992023-09-15 14:10:35 +01002616 /*
2617 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2618 * or FFA_MEMORY_DONATE the retriever should have specifed the
2619 * instruction permissions it wishes to receive.
2620 */
2621 switch (share_func) {
2622 case FFA_MEM_SHARE_32:
2623 if (requested_instruction_access !=
2624 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2625 dlog_verbose(
2626 "%s: for share instruction permissions must "
2627 "NOT be specified.\n",
2628 __func__);
2629 return ffa_error(FFA_INVALID_PARAMETERS);
2630 }
2631 break;
2632 case FFA_MEM_LEND_32:
2633 /*
2634 * For operations with multiple borrowers only permit XN
2635 * permissions, and both Sender and borrower should have used
2636 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2637 */
2638 if (multiple_borrowers) {
2639 if (requested_instruction_access !=
2640 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2641 dlog_verbose(
2642 "%s: lend/share/donate with multiple "
2643 "borrowers "
2644 "instruction permissions must NOT be "
2645 "specified.\n",
2646 __func__);
2647 return ffa_error(FFA_INVALID_PARAMETERS);
2648 }
2649 break;
2650 }
2651 /* Fall through if the operation targets a single borrower. */
2652 case FFA_MEM_DONATE_32:
2653 if (!multiple_borrowers &&
2654 requested_instruction_access ==
2655 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2656 dlog_verbose(
2657 "%s: for lend/donate with single borrower "
2658 "instruction permissions must be speficified "
2659 "by borrower\n",
2660 __func__);
2661 return ffa_error(FFA_INVALID_PARAMETERS);
2662 }
2663 break;
2664 default:
2665 panic("%s: Wrong func id provided.\n", __func__);
2666 }
2667
J-Alves96de29f2022-04-26 16:05:24 +01002668 switch (sent_instruction_access) {
2669 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2670 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002671 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002672 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002673 permissions->instruction_access =
2674 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002675 }
2676 break;
2677 }
J-Alvesdcad8992023-09-15 14:10:35 +01002678 /*
2679 * Fall through if requested permissions are less
2680 * permissive than those provided by the sender.
2681 */
J-Alves96de29f2022-04-26 16:05:24 +01002682 case FFA_INSTRUCTION_ACCESS_NX:
2683 if (requested_instruction_access ==
2684 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2685 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2686 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002687 permissions->instruction_access =
2688 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002689 }
2690 break;
2691 }
2692 dlog_verbose(
2693 "Invalid instruction access requested; sender "
2694 "specified permissions %#x but receiver requested "
2695 "%#x.\n",
2696 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002697 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002698 case FFA_INSTRUCTION_ACCESS_RESERVED:
2699 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2700 "be checked before this point.");
2701 }
2702
J-Alvesdcad8992023-09-15 14:10:35 +01002703 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002704}
2705
2706/**
2707 * Validate the receivers' permissions in the retrieve request against those
2708 * specified by the lender.
2709 * In the `permissions` argument returns the permissions to set at S2 for the
2710 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002711 * The function looks into the flag to bypass multiple borrower checks:
2712 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2713 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2714 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2715 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002716 */
2717static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2718 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002719 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002720 ffa_memory_access_permissions_t *permissions,
2721 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002722{
2723 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002724 bool bypass_multi_receiver_check =
2725 (retrieve_request->flags &
2726 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002727 const uint32_t region_receiver_count = memory_region->receiver_count;
2728 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002729
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002730 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002731 assert(permissions != NULL);
2732
Karl Meakin84710f32023-10-12 15:14:49 +01002733 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002734
J-Alves3456e032023-07-20 12:20:05 +01002735 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002736 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002737 dlog_verbose(
2738 "Retrieve request should contain same list of "
2739 "borrowers, as specified by the lender.\n");
2740 return ffa_error(FFA_INVALID_PARAMETERS);
2741 }
2742 } else {
2743 if (retrieve_request->receiver_count != 1) {
2744 dlog_verbose(
2745 "Set bypass multiple borrower check, receiver "
2746 "list must be sized 1 (%x)\n",
2747 memory_region->receiver_count);
2748 return ffa_error(FFA_INVALID_PARAMETERS);
2749 }
J-Alves96de29f2022-04-26 16:05:24 +01002750 }
2751
2752 retrieve_receiver_index = retrieve_request->receiver_count;
2753
J-Alves96de29f2022-04-26 16:05:24 +01002754 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2755 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002756 struct ffa_memory_access *retrieve_request_receiver =
2757 ffa_memory_region_get_receiver(retrieve_request, i);
2758 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002759 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002760 retrieve_request_receiver->receiver_permissions
2761 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002762 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002763 retrieve_request_receiver->receiver_permissions
2764 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002765 struct ffa_memory_access *receiver;
2766 uint32_t mem_region_receiver_index;
2767 bool permissions_RO;
2768 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002769 bool found_to_id = current_receiver_id == to_vm_id;
2770
J-Alves3456e032023-07-20 12:20:05 +01002771 if (bypass_multi_receiver_check && !found_to_id) {
2772 dlog_verbose(
2773 "Bypass multiple borrower check for id %x.\n",
2774 current_receiver_id);
2775 continue;
2776 }
2777
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002778 if (retrieve_request_receiver->composite_memory_region_offset !=
2779 0U) {
2780 dlog_verbose(
2781 "Retriever specified address ranges not "
2782 "supported (got offset %d).\n",
2783 retrieve_request_receiver
2784 ->composite_memory_region_offset);
2785 return ffa_error(FFA_INVALID_PARAMETERS);
2786 }
2787
J-Alves96de29f2022-04-26 16:05:24 +01002788 /*
2789 * Find the current receiver in the transaction descriptor from
2790 * sender.
2791 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002792 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002793 ffa_memory_region_get_receiver_index(
2794 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002795
2796 if (mem_region_receiver_index ==
2797 memory_region->receiver_count) {
2798 dlog_verbose("%s: receiver %x not found\n", __func__,
2799 current_receiver_id);
2800 return ffa_error(FFA_DENIED);
2801 }
2802
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002803 receiver = ffa_memory_region_get_receiver(
2804 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002805 assert(receiver != NULL);
2806
2807 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002808
2809 if (found_to_id) {
2810 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002811
2812 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002813 }
2814
2815 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002816 * Check if retrieve request memory access list is valid:
2817 * - The retrieve request complies with the specification.
2818 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002819 */
J-Alvesdcad8992023-09-15 14:10:35 +01002820 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002821 func_id, sent_permissions.data_access,
2822 requested_permissions.data_access,
2823 sent_permissions.instruction_access,
2824 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002825 found_to_id ? permissions : NULL,
2826 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002827
J-Alvesdcad8992023-09-15 14:10:35 +01002828 if (ret.func != FFA_SUCCESS_32) {
2829 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002830 }
2831
Karl Meakin84710f32023-10-12 15:14:49 +01002832 permissions_RO =
2833 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002834 clear_memory_flags =
2835 (retrieve_request->flags &
2836 (FFA_MEMORY_REGION_FLAG_CLEAR |
2837 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002838
J-Alves96de29f2022-04-26 16:05:24 +01002839 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002840 * Can't request PM to clear memory if only provided
2841 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002842 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002843 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002844 dlog_verbose(
2845 "Receiver has RO permissions can not request "
2846 "clear.\n");
2847 return ffa_error(FFA_DENIED);
2848 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002849
2850 /*
2851 * Check the impdef in the retrieve_request matches the value in
2852 * the original memory send.
2853 */
2854 if (ffa_version_from_memory_access_desc_size(
2855 memory_region->memory_access_desc_size) >=
2856 MAKE_FFA_VERSION(1, 2) &&
2857 ffa_version_from_memory_access_desc_size(
2858 retrieve_request->memory_access_desc_size) >=
2859 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002860 if (receiver->impdef.val[0] !=
2861 retrieve_request_receiver->impdef.val[0] ||
2862 receiver->impdef.val[1] !=
2863 retrieve_request_receiver->impdef.val[1]) {
2864 dlog_verbose(
2865 "Impdef value in memory send does not "
2866 "match retrieve request value "
Karl Meakine8937d92024-03-19 16:04:25 +00002867 "send value %#lx %#lx retrieve request "
2868 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002869 receiver->impdef.val[0],
2870 receiver->impdef.val[1],
2871 retrieve_request_receiver->impdef
2872 .val[0],
2873 retrieve_request_receiver->impdef
2874 .val[1]);
2875 return ffa_error(FFA_INVALID_PARAMETERS);
2876 }
2877 }
J-Alves96de29f2022-04-26 16:05:24 +01002878 }
2879
2880 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2881 dlog_verbose(
2882 "Retrieve request does not contain caller's (%x) "
2883 "permissions\n",
2884 to_vm_id);
2885 return ffa_error(FFA_INVALID_PARAMETERS);
2886 }
2887
2888 return (struct ffa_value){.func = FFA_SUCCESS_32};
2889}
2890
J-Alvesa9cd7e32022-07-01 13:49:33 +01002891/*
2892 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2893 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2894 * of a pending memory sharing operation whose allocator is the SPM, for
2895 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2896 * the memory region descriptor of the retrieve request must be zeroed with the
2897 * exception of the sender ID and handle.
2898 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002899bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2900 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002901{
2902 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01002903 request->attributes.shareability == 0U &&
2904 request->attributes.cacheability == 0U &&
2905 request->attributes.type == 0U &&
2906 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01002907 request->tag == 0U && request->receiver_count == 0U &&
2908 plat_ffa_memory_handle_allocated_by_current_world(
2909 request->handle);
2910}
2911
2912/*
2913 * Helper to reset count of fragments retrieved by the hypervisor.
2914 */
2915static void ffa_memory_retrieve_complete_from_hyp(
2916 struct ffa_memory_share_state *share_state)
2917{
2918 if (share_state->hypervisor_fragment_count ==
2919 share_state->fragment_count) {
2920 share_state->hypervisor_fragment_count = 0;
2921 }
2922}
2923
J-Alves089004f2022-07-13 14:25:44 +01002924/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002925 * Prepares the return of the ffa_value for the memory retrieve response.
2926 */
2927static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2928 uint32_t fragment_length)
2929{
2930 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2931 .arg1 = total_length,
2932 .arg2 = fragment_length};
2933}
2934
2935/**
J-Alves089004f2022-07-13 14:25:44 +01002936 * Validate that the memory region descriptor provided by the borrower on
2937 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2938 * memory sharing call.
2939 */
2940static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002941 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2942 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002943 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2944 uint32_t share_func)
2945{
2946 ffa_memory_region_flags_t transaction_type =
2947 retrieve_request->flags &
2948 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002949 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002950 const uint64_t memory_access_desc_size =
2951 retrieve_request->memory_access_desc_size;
2952 const uint32_t expected_retrieve_request_length =
2953 retrieve_request->receivers_offset +
2954 (uint32_t)(retrieve_request->receiver_count *
2955 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002956
2957 assert(retrieve_request != NULL);
2958 assert(memory_region != NULL);
2959 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01002960
J-Alves4f0d9c12024-01-17 17:23:11 +00002961 if (retrieve_request_length != expected_retrieve_request_length) {
2962 dlog_verbose(
2963 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2964 "but was %d.\n",
2965 expected_retrieve_request_length,
2966 retrieve_request_length);
2967 return ffa_error(FFA_INVALID_PARAMETERS);
2968 }
2969
2970 if (retrieve_request->sender != memory_region->sender) {
2971 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002972 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00002973 "retrieve.\n",
2974 memory_region->handle);
2975 return ffa_error(FFA_DENIED);
2976 }
2977
2978 /*
2979 * The SPMC can only process retrieve requests to memory share
2980 * operations with one borrower from the other world. It can't
2981 * determine the ID of the NWd VM that invoked the retrieve
2982 * request interface call. It relies on the hypervisor to
2983 * validate the caller's ID against that provided in the
2984 * `receivers` list of the retrieve response.
2985 * In case there is only one borrower from the NWd in the
2986 * transaction descriptor, record that in the `receiver_id` for
2987 * later use, and validate in the retrieve request message.
2988 * This limitation is due to the fact SPMC can't determine the
2989 * index in the memory share structures state to update.
2990 */
2991 if (to_id == HF_HYPERVISOR_VM_ID) {
2992 uint32_t other_world_count = 0;
2993
2994 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2995 struct ffa_memory_access *receiver =
2996 ffa_memory_region_get_receiver(retrieve_request,
2997 0);
2998 assert(receiver != NULL);
2999
3000 to_id = receiver->receiver_permissions.receiver;
3001
3002 if (!vm_id_is_current_world(to_id)) {
3003 other_world_count++;
3004 }
3005 }
3006
3007 if (other_world_count > 1) {
3008 dlog_verbose(
3009 "Support one receiver from the other "
3010 "world.\n");
3011 return ffa_error(FFA_NOT_SUPPORTED);
3012 }
3013 }
J-Alves089004f2022-07-13 14:25:44 +01003014 /*
3015 * Check that the transaction type expected by the receiver is
3016 * correct, if it has been specified.
3017 */
3018 if (transaction_type !=
3019 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3020 transaction_type != (memory_region->flags &
3021 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3022 dlog_verbose(
3023 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003024 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003025 transaction_type,
3026 memory_region->flags &
3027 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3028 retrieve_request->handle);
3029 return ffa_error(FFA_INVALID_PARAMETERS);
3030 }
3031
3032 if (retrieve_request->tag != memory_region->tag) {
3033 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003034 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3035 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003036 retrieve_request->tag, memory_region->tag,
3037 retrieve_request->handle);
3038 return ffa_error(FFA_INVALID_PARAMETERS);
3039 }
3040
J-Alves4f0d9c12024-01-17 17:23:11 +00003041 *receiver_index =
3042 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003043
3044 if (*receiver_index == memory_region->receiver_count) {
3045 dlog_verbose(
3046 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003047 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003048 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003049 return ffa_error(FFA_INVALID_PARAMETERS);
3050 }
3051
3052 if ((retrieve_request->flags &
3053 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3054 dlog_verbose(
3055 "Retriever specified 'address range alignment 'hint' "
3056 "not supported.\n");
3057 return ffa_error(FFA_INVALID_PARAMETERS);
3058 }
3059 if ((retrieve_request->flags &
3060 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3061 dlog_verbose(
3062 "Bits 8-5 must be zero in memory region's flags "
3063 "(address range alignment hint not supported).\n");
3064 return ffa_error(FFA_INVALID_PARAMETERS);
3065 }
3066
3067 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3068 dlog_verbose(
3069 "Bits 31-10 must be zero in memory region's flags.\n");
3070 return ffa_error(FFA_INVALID_PARAMETERS);
3071 }
3072
3073 if (share_func == FFA_MEM_SHARE_32 &&
3074 (retrieve_request->flags &
3075 (FFA_MEMORY_REGION_FLAG_CLEAR |
3076 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3077 dlog_verbose(
3078 "Memory Share operation can't clean after relinquish "
3079 "memory region.\n");
3080 return ffa_error(FFA_INVALID_PARAMETERS);
3081 }
3082
3083 /*
3084 * If the borrower needs the memory to be cleared before mapping
3085 * to its address space, the sender should have set the flag
3086 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3087 * FFA_DENIED.
3088 */
3089 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3090 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3091 dlog_verbose(
3092 "Borrower needs memory cleared. Sender needs to set "
3093 "flag for clearing memory.\n");
3094 return ffa_error(FFA_DENIED);
3095 }
3096
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003097 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003098 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003099 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3100 dlog_verbose(
3101 "Invalid security state for memory retrieve request "
3102 "operation.\n");
3103 return ffa_error(FFA_INVALID_PARAMETERS);
3104 }
3105
J-Alves089004f2022-07-13 14:25:44 +01003106 /*
3107 * If memory type is not specified, bypass validation of memory
3108 * attributes in the retrieve request. The retriever is expecting to
3109 * obtain this information from the SPMC.
3110 */
Karl Meakin84710f32023-10-12 15:14:49 +01003111 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003112 return (struct ffa_value){.func = FFA_SUCCESS_32};
3113 }
3114
3115 /*
3116 * Ensure receiver's attributes are compatible with how
3117 * Hafnium maps memory: Normal Memory, Inner shareable,
3118 * Write-Back Read-Allocate Write-Allocate Cacheable.
3119 */
3120 return ffa_memory_attributes_validate(retrieve_request->attributes);
3121}
3122
J-Alves4f0d9c12024-01-17 17:23:11 +00003123static struct ffa_value ffa_partition_retrieve_request(
3124 struct share_states_locked share_states,
3125 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3126 struct ffa_memory_region *retrieve_request,
3127 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003128{
Karl Meakin84710f32023-10-12 15:14:49 +01003129 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003130 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003131 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003132 struct ffa_composite_memory_region *composite;
3133 uint32_t total_length;
3134 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003135 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003136 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003137 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003138 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003139 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003140 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003141 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003142 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003143 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003144 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003145
J-Alves96de29f2022-04-26 16:05:24 +01003146 if (!share_state->sending_complete) {
3147 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003148 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003149 "retrieve.\n",
3150 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003151 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003152 }
3153
J-Alves4f0d9c12024-01-17 17:23:11 +00003154 /*
3155 * Validate retrieve request, according to what was sent by the
3156 * sender. Function will output the `receiver_index` from the
3157 * provided memory region.
3158 */
3159 ret = ffa_memory_retrieve_validate(
3160 receiver_id, retrieve_request, retrieve_request_length,
3161 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003162
J-Alves4f0d9c12024-01-17 17:23:11 +00003163 if (ret.func != FFA_SUCCESS_32) {
3164 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003165 }
J-Alves96de29f2022-04-26 16:05:24 +01003166
J-Alves4f0d9c12024-01-17 17:23:11 +00003167 /*
3168 * Validate the requested permissions against the sent
3169 * permissions.
3170 * Outputs the permissions to give to retriever at S2
3171 * PTs.
3172 */
3173 ret = ffa_memory_retrieve_validate_memory_access_list(
3174 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003175 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003176 if (ret.func != FFA_SUCCESS_32) {
3177 return ret;
3178 }
3179
3180 memory_to_mode = ffa_memory_permissions_to_mode(
3181 permissions, share_state->sender_orig_mode);
3182
3183 ret = ffa_retrieve_check_update(
3184 to_locked, share_state->fragments,
3185 share_state->fragment_constituent_counts,
3186 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003187 share_state->share_func, false, page_pool, &retrieve_mode,
3188 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003189
3190 if (ret.func != FFA_SUCCESS_32) {
3191 return ret;
3192 }
3193
3194 share_state->retrieved_fragment_count[receiver_index] = 1;
3195
3196 is_retrieve_complete =
3197 share_state->retrieved_fragment_count[receiver_index] ==
3198 share_state->fragment_count;
3199
J-Alvesb5084cf2022-07-06 14:20:12 +01003200 /* VMs acquire the RX buffer from SPMC. */
3201 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3202
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003203 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003204 * Copy response to RX buffer of caller and deliver the message.
3205 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003206 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003207 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003208
Andrew Walbranca808b12020-05-15 17:22:28 +01003209 /*
J-Alves460d36c2023-10-12 17:02:15 +01003210 * Set the security state in the memory retrieve response attributes
3211 * if specified by the target mode.
3212 */
3213 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3214 retrieve_mode);
3215
3216 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003217 * Constituents which we received in the first fragment should
3218 * always fit in the first fragment we are sending, because the
3219 * header is the same size in both cases and we have a fixed
3220 * message buffer size. So `ffa_retrieved_memory_region_init`
3221 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003222 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003223
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003224 /* Provide the permissions that had been provided. */
3225 receiver->receiver_permissions.permissions = permissions;
3226
3227 /*
3228 * Prepare the memory region descriptor for the retrieve response.
3229 * Provide the pointer to the receiver tracked in the share state
3230 * strucutures.
3231 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003232 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003233 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003234 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003235 memory_region->flags, handle, permissions, receiver, 1,
3236 memory_access_desc_size, composite->page_count,
3237 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003238 share_state->fragment_constituent_counts[0], &total_length,
3239 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003240
J-Alves4f0d9c12024-01-17 17:23:11 +00003241 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003242 ffa_memory_retrieve_complete(share_states, share_state,
3243 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003244 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003245
3246 return ffa_memory_retrieve_resp(total_length, fragment_length);
3247}
3248
3249static struct ffa_value ffa_hypervisor_retrieve_request(
3250 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3251 struct ffa_memory_region *retrieve_request)
3252{
3253 struct ffa_value ret;
3254 struct ffa_composite_memory_region *composite;
3255 uint32_t total_length;
3256 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003257 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003258 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003259 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003260 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003261 ffa_memory_handle_t handle = retrieve_request->handle;
3262
J-Alves4f0d9c12024-01-17 17:23:11 +00003263 memory_region = share_state->memory_region;
3264
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003265 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3266
J-Alves7b6ab612024-01-24 09:54:54 +00003267 switch (to_locked.vm->ffa_version) {
3268 case MAKE_FFA_VERSION(1, 2):
3269 memory_access_desc_size = sizeof(struct ffa_memory_access);
3270 break;
3271 case MAKE_FFA_VERSION(1, 0):
3272 case MAKE_FFA_VERSION(1, 1):
3273 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3274 break;
3275 default:
3276 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3277 }
3278
J-Alves4f0d9c12024-01-17 17:23:11 +00003279 if (share_state->hypervisor_fragment_count != 0U) {
3280 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003281 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003282 "the hypervisor.\n",
3283 handle);
3284 return ffa_error(FFA_DENIED);
3285 }
3286
3287 share_state->hypervisor_fragment_count = 1;
3288
3289 ffa_memory_retrieve_complete_from_hyp(share_state);
3290
3291 /* VMs acquire the RX buffer from SPMC. */
3292 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3293
3294 /*
3295 * Copy response to RX buffer of caller and deliver the message.
3296 * This must be done before the share_state is (possibly) freed.
3297 */
3298 composite = ffa_memory_region_get_composite(memory_region, 0);
3299
3300 /*
3301 * Constituents which we received in the first fragment should
3302 * always fit in the first fragment we are sending, because the
3303 * header is the same size in both cases and we have a fixed
3304 * message buffer size. So `ffa_retrieved_memory_region_init`
3305 * should never fail.
3306 */
3307
3308 /*
3309 * Set the security state in the memory retrieve response attributes
3310 * if specified by the target mode.
3311 */
3312 attributes = plat_ffa_memory_security_mode(
3313 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003314
3315 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3316
J-Alves4f0d9c12024-01-17 17:23:11 +00003317 CHECK(ffa_retrieved_memory_region_init(
3318 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3319 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003320 memory_region->flags, handle,
3321 receiver->receiver_permissions.permissions, receiver,
3322 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003323 composite->page_count, composite->constituent_count,
3324 share_state->fragments[0],
3325 share_state->fragment_constituent_counts[0], &total_length,
3326 &fragment_length));
3327
3328 return ffa_memory_retrieve_resp(total_length, fragment_length);
3329}
3330
3331struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3332 struct ffa_memory_region *retrieve_request,
3333 uint32_t retrieve_request_length,
3334 struct mpool *page_pool)
3335{
3336 ffa_memory_handle_t handle = retrieve_request->handle;
3337 struct share_states_locked share_states;
3338 struct ffa_memory_share_state *share_state;
3339 struct ffa_value ret;
3340
3341 dump_share_states();
3342
3343 share_states = share_states_lock();
3344 share_state = get_share_state(share_states, handle);
3345 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003346 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003347 handle);
3348 ret = ffa_error(FFA_INVALID_PARAMETERS);
3349 goto out;
3350 }
3351
3352 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3353 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3354 retrieve_request);
3355 } else {
3356 ret = ffa_partition_retrieve_request(
3357 share_states, share_state, to_locked, retrieve_request,
3358 retrieve_request_length, page_pool);
3359 }
3360
3361 /* Track use of the RX buffer if the handling has succeeded. */
3362 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3363 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3364 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3365 }
3366
Andrew Walbranca808b12020-05-15 17:22:28 +01003367out:
3368 share_states_unlock(&share_states);
3369 dump_share_states();
3370 return ret;
3371}
3372
J-Alves5da37d92022-10-24 16:33:48 +01003373/**
3374 * Determine expected fragment offset according to the FF-A version of
3375 * the caller.
3376 */
3377static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3378 struct ffa_memory_region *memory_region,
3379 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3380{
3381 uint32_t expected_fragment_offset;
3382 uint32_t composite_constituents_offset;
3383
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003384 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003385 /*
3386 * Hafnium operates memory regions in FF-A v1.1 format, so we
3387 * can retrieve the constituents offset from descriptor.
3388 */
3389 composite_constituents_offset =
3390 ffa_composite_constituent_offset(memory_region, 0);
3391 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3392 /*
3393 * If retriever is FF-A v1.0, determine the composite offset
3394 * as it is expected to have been configured in the
3395 * retrieve response.
3396 */
3397 composite_constituents_offset =
3398 sizeof(struct ffa_memory_region_v1_0) +
3399 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003400 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003401 sizeof(struct ffa_composite_memory_region);
3402 } else {
3403 panic("%s received an invalid FF-A version.\n", __func__);
3404 }
3405
3406 expected_fragment_offset =
3407 composite_constituents_offset +
3408 retrieved_constituents_count *
3409 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003410 (uint32_t)(memory_region->memory_access_desc_size *
3411 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003412
3413 return expected_fragment_offset;
3414}
3415
Andrew Walbranca808b12020-05-15 17:22:28 +01003416struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3417 ffa_memory_handle_t handle,
3418 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003419 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003420 struct mpool *page_pool)
3421{
3422 struct ffa_memory_region *memory_region;
3423 struct share_states_locked share_states;
3424 struct ffa_memory_share_state *share_state;
3425 struct ffa_value ret;
3426 uint32_t fragment_index;
3427 uint32_t retrieved_constituents_count;
3428 uint32_t i;
3429 uint32_t expected_fragment_offset;
3430 uint32_t remaining_constituent_count;
3431 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003432 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003433 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003434
3435 dump_share_states();
3436
3437 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003438 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003439 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003440 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003441 handle);
3442 ret = ffa_error(FFA_INVALID_PARAMETERS);
3443 goto out;
3444 }
3445
3446 memory_region = share_state->memory_region;
3447 CHECK(memory_region != NULL);
3448
Andrew Walbranca808b12020-05-15 17:22:28 +01003449 if (!share_state->sending_complete) {
3450 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003451 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003452 "retrieve.\n",
3453 handle);
3454 ret = ffa_error(FFA_INVALID_PARAMETERS);
3455 goto out;
3456 }
3457
J-Alves59ed0042022-07-28 18:26:41 +01003458 /*
3459 * If retrieve request from the hypervisor has been initiated in the
3460 * given share_state, continue it, else assume it is a continuation of
3461 * retrieve request from a NWd VM.
3462 */
3463 continue_ffa_hyp_mem_retrieve_req =
3464 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3465 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003466 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003467
J-Alves59ed0042022-07-28 18:26:41 +01003468 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003469 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003470 memory_region, to_locked.vm->id);
3471
3472 if (receiver_index == memory_region->receiver_count) {
3473 dlog_verbose(
3474 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003475 "borrower to memory sharing transaction "
3476 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003477 to_locked.vm->id, handle);
3478 ret = ffa_error(FFA_INVALID_PARAMETERS);
3479 goto out;
3480 }
3481
3482 if (share_state->retrieved_fragment_count[receiver_index] ==
3483 0 ||
3484 share_state->retrieved_fragment_count[receiver_index] >=
3485 share_state->fragment_count) {
3486 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003487 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003488 "started or already completed (%d/%d fragments "
3489 "retrieved).\n",
3490 handle,
3491 share_state->retrieved_fragment_count
3492 [receiver_index],
3493 share_state->fragment_count);
3494 ret = ffa_error(FFA_INVALID_PARAMETERS);
3495 goto out;
3496 }
3497
3498 fragment_index =
3499 share_state->retrieved_fragment_count[receiver_index];
3500 } else {
3501 if (share_state->hypervisor_fragment_count == 0 ||
3502 share_state->hypervisor_fragment_count >=
3503 share_state->fragment_count) {
3504 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003505 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003506 "started from hypervisor.\n",
3507 handle);
3508 ret = ffa_error(FFA_INVALID_PARAMETERS);
3509 goto out;
3510 }
3511
3512 if (memory_region->sender != sender_vm_id) {
3513 dlog_verbose(
3514 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003515 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003516 sender_vm_id, handle);
3517 ret = ffa_error(FFA_INVALID_PARAMETERS);
3518 goto out;
3519 }
3520
3521 fragment_index = share_state->hypervisor_fragment_count;
3522
3523 receiver_index = 0;
3524 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003525
3526 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003527 * Check that the given fragment offset is correct by counting
3528 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003529 */
3530 retrieved_constituents_count = 0;
3531 for (i = 0; i < fragment_index; ++i) {
3532 retrieved_constituents_count +=
3533 share_state->fragment_constituent_counts[i];
3534 }
J-Alvesc7484f12022-05-13 12:41:14 +01003535
3536 CHECK(memory_region->receiver_count > 0);
3537
Andrew Walbranca808b12020-05-15 17:22:28 +01003538 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003539 ffa_memory_retrieve_expected_offset_per_ffa_version(
3540 memory_region, retrieved_constituents_count,
3541 to_locked.vm->ffa_version);
3542
Andrew Walbranca808b12020-05-15 17:22:28 +01003543 if (fragment_offset != expected_fragment_offset) {
3544 dlog_verbose("Fragment offset was %d but expected %d.\n",
3545 fragment_offset, expected_fragment_offset);
3546 ret = ffa_error(FFA_INVALID_PARAMETERS);
3547 goto out;
3548 }
3549
J-Alves4f0d9c12024-01-17 17:23:11 +00003550 /*
3551 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3552 * is currently ownder by the SPMC.
3553 */
3554 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003555
Andrew Walbranca808b12020-05-15 17:22:28 +01003556 remaining_constituent_count = ffa_memory_fragment_init(
3557 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3558 share_state->fragments[fragment_index],
3559 share_state->fragment_constituent_counts[fragment_index],
3560 &fragment_length);
3561 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003562
Andrew Walbranca808b12020-05-15 17:22:28 +01003563 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003564 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003565
J-Alves59ed0042022-07-28 18:26:41 +01003566 if (!continue_ffa_hyp_mem_retrieve_req) {
3567 share_state->retrieved_fragment_count[receiver_index]++;
3568 if (share_state->retrieved_fragment_count[receiver_index] ==
3569 share_state->fragment_count) {
3570 ffa_memory_retrieve_complete(share_states, share_state,
3571 page_pool);
3572 }
3573 } else {
3574 share_state->hypervisor_fragment_count++;
3575
3576 ffa_memory_retrieve_complete_from_hyp(share_state);
3577 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003578 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3579 .arg1 = (uint32_t)handle,
3580 .arg2 = (uint32_t)(handle >> 32),
3581 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003582
3583out:
3584 share_states_unlock(&share_states);
3585 dump_share_states();
3586 return ret;
3587}
3588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003589struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003590 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003591 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003592{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003593 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003594 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003595 struct ffa_memory_share_state *share_state;
3596 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003597 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003598 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003599 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003600 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003601 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003602
Andrew Walbrana65a1322020-04-06 19:32:32 +01003603 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003604 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003605 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003606 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003607 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003608 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003609 }
3610
Andrew Walbrana65a1322020-04-06 19:32:32 +01003611 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003612 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003613 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003614 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003615 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003616 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003617 }
3618
3619 dump_share_states();
3620
3621 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003622 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003623 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003624 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003625 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003626 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003627 goto out;
3628 }
3629
Andrew Walbranca808b12020-05-15 17:22:28 +01003630 if (!share_state->sending_complete) {
3631 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003632 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003633 "relinquish.\n",
3634 handle);
3635 ret = ffa_error(FFA_INVALID_PARAMETERS);
3636 goto out;
3637 }
3638
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003639 memory_region = share_state->memory_region;
3640 CHECK(memory_region != NULL);
3641
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003642 receiver_index = ffa_memory_region_get_receiver_index(
3643 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003644
3645 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003646 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003647 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003648 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003649 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003650 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003651 goto out;
3652 }
3653
J-Alves8eb19162022-04-28 10:56:48 +01003654 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003655 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003656 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003657 "Memory with handle %#lx not yet fully "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003658 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003659 "receiver %x can't relinquish.\n",
3660 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003661 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003662 goto out;
3663 }
3664
J-Alves3c5b2072022-11-21 12:45:40 +00003665 /*
3666 * Either clear if requested in relinquish call, or in a retrieve
3667 * request from one of the borrowers.
3668 */
3669 receivers_relinquished_memory = true;
3670
3671 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3672 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003673 ffa_memory_region_get_receiver(memory_region, i);
3674 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003675 if (receiver->receiver_permissions.receiver ==
3676 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003677 receiver_permissions =
3678 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003679 continue;
3680 }
3681
3682 if (share_state->retrieved_fragment_count[i] != 0U) {
3683 receivers_relinquished_memory = false;
3684 break;
3685 }
3686 }
3687
3688 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003689 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3690 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003691
3692 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003693 * Clear is not allowed for memory that was shared, as the
3694 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003695 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003696 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003697 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003698 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003699 goto out;
3700 }
3701
J-Alvesb886d492024-04-15 10:55:29 +01003702 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003703 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3704 __func__);
3705 ret = ffa_error(FFA_DENIED);
3706 goto out;
3707 }
3708
Andrew Walbranca808b12020-05-15 17:22:28 +01003709 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003710 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003711 share_state->fragment_constituent_counts,
3712 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003713
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003714 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003715 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003716 * Mark memory handle as not retrieved, so it can be
3717 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003718 */
J-Alves8eb19162022-04-28 10:56:48 +01003719 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003720 }
3721
3722out:
3723 share_states_unlock(&share_states);
3724 dump_share_states();
3725 return ret;
3726}
3727
3728/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003729 * Validates that the reclaim transition is allowed for the given
3730 * handle, updates the page table of the reclaiming VM, and frees the
3731 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003732 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003733struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003734 ffa_memory_handle_t handle,
3735 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003736 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003737{
3738 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003739 struct ffa_memory_share_state *share_state;
3740 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003741 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003742
3743 dump_share_states();
3744
3745 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003746
Karl Meakin4a2854a2023-06-30 16:26:52 +01003747 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003748 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003749 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003750 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003751 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003752 goto out;
3753 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003754 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003755
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003756 CHECK(memory_region != NULL);
3757
J-Alvesa9cd7e32022-07-01 13:49:33 +01003758 if (vm_id_is_current_world(to_locked.vm->id) &&
3759 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003760 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003761 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003762 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003763 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003764 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003765 goto out;
3766 }
3767
Andrew Walbranca808b12020-05-15 17:22:28 +01003768 if (!share_state->sending_complete) {
3769 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003770 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003771 "reclaim.\n",
3772 handle);
3773 ret = ffa_error(FFA_INVALID_PARAMETERS);
3774 goto out;
3775 }
3776
J-Alves752236c2022-04-28 11:07:47 +01003777 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3778 if (share_state->retrieved_fragment_count[i] != 0) {
3779 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003780 "Tried to reclaim memory handle %#lx "
J-Alves3c5b2072022-11-21 12:45:40 +00003781 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003782 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003783 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003784 ffa_memory_region_get_receiver(memory_region, i)
3785 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003786 ret = ffa_error(FFA_DENIED);
3787 goto out;
3788 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003789 }
3790
Andrew Walbranca808b12020-05-15 17:22:28 +01003791 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003792 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003793 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003794 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003795 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003796 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003797
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003798 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003799 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003800 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003801 }
3802
3803out:
3804 share_states_unlock(&share_states);
3805 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003806}