blob: b9544d52fedcf40b2540acbf2892c125fb2904ad [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
J-Alves460d36c2023-10-12 17:02:15 +010011#include <stdint.h>
12
Federico Recanati4fd065d2021-12-13 20:06:23 +010013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000016
J-Alves5952d942022-12-22 16:03:00 +000017#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000019#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010020#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010022#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010024#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010025#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000026#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000027#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000028#include "hf/panic.h"
29#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010030#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000031#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000032#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010033
J-Alves2d8457f2022-10-05 11:06:41 +010034#include "vmapi/hf/ffa_v1_0.h"
35
J-Alves5da37d92022-10-24 16:33:48 +010036#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
37
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000038/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040 * by this lock.
41 */
42static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000044
45/**
J-Alvesed508c82023-05-04 16:09:48 +010046 * Return the offset to the first constituent within the
47 * `ffa_composite_memory_region` for the given receiver from an
48 * `ffa_memory_region`. The caller must check that the receiver_index is within
49 * bounds, and that it has a composite memory region offset.
50 */
51static uint32_t ffa_composite_constituent_offset(
52 struct ffa_memory_region *memory_region, uint32_t receiver_index)
53{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000054 struct ffa_memory_access *receiver;
55 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010056
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000057 CHECK(receiver_index < memory_region->receiver_count);
58
59 receiver =
60 ffa_memory_region_get_receiver(memory_region, receiver_index);
61 CHECK(receiver != NULL);
62
63 composite_offset = receiver->composite_memory_region_offset;
64
65 CHECK(composite_offset != 0);
66
67 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010068}
69
70/**
J-Alves917d2f22020-10-30 18:39:30 +000071 * Extracts the index from a memory handle allocated by Hafnium's current world.
72 */
73uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
74{
75 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
76}
77
78/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010079 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
80 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
81 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010082 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010083 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
84 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010085 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010086struct ffa_memory_share_state *allocate_share_state(
87 struct share_states_locked share_states, uint32_t share_func,
88 struct ffa_memory_region *memory_region, uint32_t fragment_length,
89 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000091 assert(share_states.share_states != NULL);
92 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093
Karl Meakin52cdfe72023-06-30 14:49:10 +010094 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010095 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010097 &share_states.share_states[i];
98 struct ffa_composite_memory_region *composite =
99 ffa_memory_region_get_composite(memory_region,
100 0);
101
102 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000103 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200104 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100105 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000106 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100107 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108 allocated_state->share_func = share_func;
109 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100110 allocated_state->fragment_count = 1;
111 allocated_state->fragments[0] = composite->constituents;
112 allocated_state->fragment_constituent_counts[0] =
113 (fragment_length -
114 ffa_composite_constituent_offset(memory_region,
115 0)) /
116 sizeof(struct ffa_memory_region_constituent);
117 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100118 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
119 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100120 allocated_state->retrieved_fragment_count[j] =
121 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100123 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 }
125 }
126
Karl Meakin52cdfe72023-06-30 14:49:10 +0100127 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128}
129
130/** Locks the share states lock. */
131struct share_states_locked share_states_lock(void)
132{
133 sl_lock(&share_states_lock_instance);
134
135 return (struct share_states_locked){.share_states = share_states};
136}
137
138/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100139void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000141 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 share_states->share_states = NULL;
143 sl_unlock(&share_states_lock_instance);
144}
145
146/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100147 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100148 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150struct ffa_memory_share_state *get_share_state(
151 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000155 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100156
157 /*
158 * First look for a share_state allocated by us, in which case the
159 * handle is based on the index.
160 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100162 uint64_t index = ffa_memory_handle_get_index(handle);
163
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 if (index < MAX_MEM_SHARES) {
165 share_state = &share_states.share_states[index];
166 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100167 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 }
169 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 }
171
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100173 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000175 if (share_state->memory_region != NULL &&
176 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100178 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
181
Karl Meakin4a2854a2023-06-30 16:26:52 +0100182 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
185/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186void share_state_free(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state,
188 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189{
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 uint32_t i;
191
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000192 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 /*
197 * First fragment is part of the same page as the `memory_region`, so it
198 * doesn't need to be freed separately.
199 */
200 share_state->fragments[0] = NULL;
201 share_state->fragment_constituent_counts[0] = 0;
202 for (i = 1; i < share_state->fragment_count; ++i) {
203 mpool_free(page_pool, share_state->fragments[i]);
204 share_state->fragments[i] = NULL;
205 share_state->fragment_constituent_counts[i] = 0;
206 }
207 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100209 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210}
211
Andrew Walbranca808b12020-05-15 17:22:28 +0100212/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100213bool share_state_sending_complete(struct share_states_locked share_states,
214 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranca808b12020-05-15 17:22:28 +0100216 struct ffa_composite_memory_region *composite;
217 uint32_t expected_constituent_count;
218 uint32_t fragment_constituent_count_total = 0;
219 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000220
Andrew Walbranca808b12020-05-15 17:22:28 +0100221 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000222 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100223
224 /*
225 * Share state must already be valid, or it's not possible to get hold
226 * of it.
227 */
228 CHECK(share_state->memory_region != NULL &&
229 share_state->share_func != 0);
230
231 composite =
232 ffa_memory_region_get_composite(share_state->memory_region, 0);
233 expected_constituent_count = composite->constituent_count;
234 for (i = 0; i < share_state->fragment_count; ++i) {
235 fragment_constituent_count_total +=
236 share_state->fragment_constituent_counts[i];
237 }
238 dlog_verbose(
239 "Checking completion: constituent count %d/%d from %d "
240 "fragments.\n",
241 fragment_constituent_count_total, expected_constituent_count,
242 share_state->fragment_count);
243
244 return fragment_constituent_count_total == expected_constituent_count;
245}
246
247/**
248 * Calculates the offset of the next fragment expected for the given share
249 * state.
250 */
J-Alvesfdd29272022-07-19 13:16:31 +0100251uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100252 struct share_states_locked share_states,
253 struct ffa_memory_share_state *share_state)
254{
255 uint32_t next_fragment_offset;
256 uint32_t i;
257
258 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260
261 next_fragment_offset =
262 ffa_composite_constituent_offset(share_state->memory_region, 0);
263 for (i = 0; i < share_state->fragment_count; ++i) {
264 next_fragment_offset +=
265 share_state->fragment_constituent_counts[i] *
266 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 }
268
Andrew Walbranca808b12020-05-15 17:22:28 +0100269 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000270}
271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100272static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273{
274 uint32_t i;
275
276 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
277 return;
278 }
279
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000280 dlog("from VM %#x, attributes %#x, flags %#x, handle %#x "
281 "tag %u, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 "recipients [",
283 memory_region->sender, memory_region->attributes,
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000284 memory_region->flags, memory_region->handle, memory_region->tag,
285 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100286 memory_region->receiver_count);
287 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000288 struct ffa_memory_access *receiver =
289 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000290 if (i != 0) {
291 dlog(", ");
292 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000293 dlog("Receiver %#x: %#x (offset %u)",
294 receiver->receiver_permissions.receiver,
295 receiver->receiver_permissions.permissions,
296 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000297 /* The impdef field is only present from v1.2 and later */
298 if (ffa_version_from_memory_access_desc_size(
299 memory_region->memory_access_desc_size) >=
300 MAKE_FFA_VERSION(1, 2)) {
301 dlog(", impdef: %#x %#x", receiver->impdef.val[0],
302 receiver->impdef.val[1]);
303 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000304 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000305 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306}
307
J-Alves66652252022-07-06 09:49:51 +0100308void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309{
310 uint32_t i;
311
312 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
313 return;
314 }
315
316 dlog("Current share states:\n");
317 sl_lock(&share_states_lock_instance);
318 for (i = 0; i < MAX_MEM_SHARES; ++i) {
319 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000322 dlog("SHARE");
323 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000325 dlog("LEND");
326 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000328 dlog("DONATE");
329 break;
330 default:
331 dlog("invalid share_func %#x",
332 share_states[i].share_func);
333 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100334 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100336 if (share_states[i].sending_complete) {
337 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100339 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 }
J-Alves2a0d2882020-10-29 14:49:50 +0000341 dlog(" with %d fragments, %d retrieved, "
342 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100343 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000344 share_states[i].retrieved_fragment_count[0],
345 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346 }
347 }
348 sl_unlock(&share_states_lock_instance);
349}
350
Andrew Walbran475c1452020-02-07 13:22:22 +0000351/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100352static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100353 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000354{
355 uint32_t mode = 0;
356
Karl Meakin84710f32023-10-12 15:14:49 +0100357 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000359 mode = MM_MODE_R;
360 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000362 mode = MM_MODE_R | MM_MODE_W;
363 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100364 case FFA_DATA_ACCESS_NOT_SPECIFIED:
365 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
366 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_DATA_ACCESS_RESERVED:
368 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100369 }
370
Karl Meakin84710f32023-10-12 15:14:49 +0100371 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000373 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100375 mode |= MM_MODE_X;
376 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100377 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
378 mode |= (default_mode & MM_MODE_X);
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_INSTRUCTION_ACCESS_RESERVED:
381 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000382 }
383
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200384 /* Set the security state bit if necessary. */
385 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
386 mode |= plat_ffa_other_world_mode();
387 }
388
Andrew Walbran475c1452020-02-07 13:22:22 +0000389 return mode;
390}
391
Jose Marinho75509b42019-04-09 09:34:59 +0100392/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 * Get the current mode in the stage-2 page table of the given vm of all the
394 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100396 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000398 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 struct ffa_memory_region_constituent **fragments,
400 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100401{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100402 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100403 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100404
Andrew Walbranca808b12020-05-15 17:22:28 +0100405 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100406 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000407 * Fail if there are no constituents. Otherwise we would get an
408 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100409 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100410 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100412 }
413
Andrew Walbranca808b12020-05-15 17:22:28 +0100414 for (i = 0; i < fragment_count; ++i) {
415 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
416 ipaddr_t begin = ipa_init(fragments[i][j].address);
417 size_t size = fragments[i][j].page_count * PAGE_SIZE;
418 ipaddr_t end = ipa_add(begin, size);
419 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420
Andrew Walbranca808b12020-05-15 17:22:28 +0100421 /* Fail if addresses are not page-aligned. */
422 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
423 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100424 dlog_verbose("%s: addresses not page-aligned\n",
425 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100426 return ffa_error(FFA_INVALID_PARAMETERS);
427 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428
Andrew Walbranca808b12020-05-15 17:22:28 +0100429 /*
430 * Ensure that this constituent memory range is all
431 * mapped with the same mode.
432 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800433 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100434 dlog_verbose(
435 "%s: constituent memory range %#x..%#x "
436 "not mapped with the same mode\n",
437 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 return ffa_error(FFA_DENIED);
439 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100440
Andrew Walbranca808b12020-05-15 17:22:28 +0100441 /*
442 * Ensure that all constituents are mapped with the same
443 * mode.
444 */
445 if (i == 0) {
446 *orig_mode = current_mode;
447 } else if (current_mode != *orig_mode) {
448 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100449 "%s: expected mode %#x but was %#x for "
450 "%d pages at %#x.\n",
451 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 fragments[i][j].page_count,
453 ipa_addr(begin));
454 return ffa_error(FFA_DENIED);
455 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100456 }
Jose Marinho75509b42019-04-09 09:34:59 +0100457 }
458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460}
461
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100462uint32_t ffa_version_from_memory_access_desc_size(
463 uint32_t memory_access_desc_size)
464{
465 switch (memory_access_desc_size) {
466 /*
467 * v1.0 and v1.1 memory access descriptors are the same size however
468 * v1.1 is the first version to include the memory access descriptor
469 * size field so return v1.1.
470 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000471 case sizeof(struct ffa_memory_access_v1_0):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100472 return MAKE_FFA_VERSION(1, 1);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000473 case sizeof(struct ffa_memory_access):
474 return MAKE_FFA_VERSION(1, 2);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100475 }
476 return 0;
477}
478
479/**
480 * Check if the receivers size and offset given is valid for the senders
481 * FF-A version.
482 */
483static bool receiver_size_and_offset_valid_for_version(
484 uint32_t receivers_size, uint32_t receivers_offset,
485 uint32_t ffa_version)
486{
487 /*
488 * Check that the version that the memory access descriptor size belongs
489 * to is compatible with the FF-A version we believe the sender to be.
490 */
491 uint32_t expected_ffa_version =
492 ffa_version_from_memory_access_desc_size(receivers_size);
493 if (!FFA_VERSIONS_ARE_COMPATIBLE(expected_ffa_version, ffa_version)) {
494 return false;
495 }
496
497 /*
498 * Check the receivers_offset matches the version we found from
499 * memory access descriptor size.
500 */
501 switch (expected_ffa_version) {
502 case MAKE_FFA_VERSION(1, 1):
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000503 case MAKE_FFA_VERSION(1, 2):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100504 return receivers_offset == sizeof(struct ffa_memory_region);
505 default:
506 return false;
507 }
508}
509
510/**
511 * Check the values set for fields in the memory region are valid and safe.
512 * Offset values are within safe bounds, receiver count will not cause overflows
513 * and reserved fields are 0.
514 */
515bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
516 uint32_t ffa_version,
517 uint32_t fragment_length,
518 bool send_transaction)
519{
520 uint32_t receiver_count;
521 struct ffa_memory_access *receiver;
522 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000523 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
524 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100525
526 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100527 /* Check the reserved fields are 0. */
528 if (memory_region_v1_0->reserved_0 != 0 ||
529 memory_region_v1_0->reserved_1 != 0) {
530 dlog_verbose("Reserved fields must be 0.\n");
531 return false;
532 }
533
534 receiver_count = memory_region_v1_0->receiver_count;
535 } else {
536 uint32_t receivers_size =
537 memory_region->memory_access_desc_size;
538 uint32_t receivers_offset = memory_region->receivers_offset;
539
540 /* Check the reserved field is 0. */
541 if (memory_region->reserved[0] != 0 ||
542 memory_region->reserved[1] != 0 ||
543 memory_region->reserved[2] != 0) {
544 dlog_verbose("Reserved fields must be 0.\n");
545 return false;
546 }
547
548 /*
549 * Check memory_access_desc_size matches the size of the struct
550 * for the senders FF-A version.
551 */
552 if (!receiver_size_and_offset_valid_for_version(
553 receivers_size, receivers_offset, ffa_version)) {
554 dlog_verbose(
555 "Invalid memory access descriptor size %d, "
556 " or receiver offset %d, "
557 "for FF-A version %#x\n",
558 receivers_size, receivers_offset, ffa_version);
559 return false;
560 }
561
562 receiver_count = memory_region->receiver_count;
563 }
564
565 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000566 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100567 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000568 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100569 "specified %u\n",
570 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
571 return false;
572 }
573
574 /* Check values in the memory access descriptors. */
575 /*
576 * The composite offset values must be the same for all recievers so
577 * check the first one is valid and then they are all the same.
578 */
579 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000580 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100581 ->receivers[0]
582 : ffa_memory_region_get_receiver(memory_region, 0);
583 assert(receiver != NULL);
584 composite_offset_0 = receiver->composite_memory_region_offset;
585
586 if (!send_transaction) {
587 if (composite_offset_0 != 0) {
588 dlog_verbose(
589 "Composite offset memory region descriptor "
590 "offset must be 0 for retrieve requests. "
591 "Currently %d",
592 composite_offset_0);
593 return false;
594 }
595 } else {
596 bool comp_offset_is_zero = composite_offset_0 == 0U;
597 bool comp_offset_lt_transaction_descriptor_size =
598 composite_offset_0 <
599 (sizeof(struct ffa_memory_region) +
600 (uint32_t)(memory_region->memory_access_desc_size *
601 memory_region->receiver_count));
602 bool comp_offset_with_comp_gt_fragment_length =
603 composite_offset_0 +
604 sizeof(struct ffa_composite_memory_region) >
605 fragment_length;
606 if (comp_offset_is_zero ||
607 comp_offset_lt_transaction_descriptor_size ||
608 comp_offset_with_comp_gt_fragment_length) {
609 dlog_verbose(
610 "Invalid composite memory region descriptor "
611 "offset for send transaction %u\n",
612 composite_offset_0);
613 return false;
614 }
615 }
616
617 for (int i = 0; i < memory_region->receiver_count; i++) {
618 uint32_t composite_offset;
619
620 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100621 struct ffa_memory_access_v1_0 *receiver_v1_0 =
622 &memory_region_v1_0->receivers[i];
623 /* Check reserved fields are 0 */
624 if (receiver_v1_0->reserved_0 != 0) {
625 dlog_verbose(
626 "Reserved field in the memory access "
627 " descriptor must be zero "
628 " Currently reciever %d has a reserved "
629 " field with a value of %d\n",
630 i, receiver_v1_0->reserved_0);
631 return false;
632 }
633 /*
634 * We can cast to the current version receiver as the
635 * remaining fields we are checking have the same
636 * offsets for all versions since memory access
637 * descriptors are forwards compatible.
638 */
639 receiver = (struct ffa_memory_access *)receiver_v1_0;
640 } else {
641 receiver = ffa_memory_region_get_receiver(memory_region,
642 i);
643 assert(receiver != NULL);
644
645 if (receiver->reserved_0 != 0) {
646 dlog_verbose(
647 "Reserved field in the memory access "
648 " descriptor must be zero "
649 " Currently reciever %d has a reserved "
650 " field with a value of %d\n",
651 i, receiver->reserved_0);
652 return false;
653 }
654 }
655
656 /* Check composite offset values are equal for all receivers. */
657 composite_offset = receiver->composite_memory_region_offset;
658 if (composite_offset != composite_offset_0) {
659 dlog_verbose(
660 "Composite offset %x differs from %x in index "
661 "%u\n",
662 composite_offset, composite_offset_0);
663 return false;
664 }
665 }
666 return true;
667}
668
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669/**
J-Alves460d36c2023-10-12 17:02:15 +0100670 * If the receivers for the memory management operation are all from the
671 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
672 * state update by returning MAP_ACTION_CHECK_PROTECT.
673 */
674static enum ffa_map_action ffa_mem_send_get_map_action(
675 bool all_receivers_from_current_world, ffa_id_t sender_id,
676 uint32_t mem_func_id)
677{
678 bool protect_memory =
679 (mem_func_id != FFA_MEM_SHARE_32 &&
680 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
681
682 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
683}
684
685/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 * Verify that all pages have the same mode, that the starting mode
687 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100688 * to the sending VM. It outputs the mapping action that needs to be
689 * invoked for the given memory range. On memory lend/donate there
690 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 *
692 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 * 1) FFA_DENIED if a state transition was not found;
694 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100695 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100697 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
699 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100702 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000703 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100704 struct ffa_memory_region_constituent **fragments,
705 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100706 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000707{
708 const uint32_t state_mask =
709 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100711 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000712 uint32_t receivers_count = memory_region->receiver_count;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000713
Andrew Walbranca808b12020-05-15 17:22:28 +0100714 ret = constituents_get_mode(from, orig_from_mode, fragments,
715 fragment_constituent_counts,
716 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100717 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100718 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100719 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100720 }
721
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000722 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100723 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000724 dlog_verbose("Can't share device memory (mode is %#x).\n",
725 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100726 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 }
728
729 /*
730 * Ensure the sender is the owner and has exclusive access to the
731 * memory.
732 */
733 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100735 }
736
Daniel Boulbya76fd912024-02-22 14:22:15 +0000737 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100738
J-Alves363f5722022-04-25 17:37:37 +0100739 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000740 struct ffa_memory_access *receiver =
741 ffa_memory_region_get_receiver(memory_region, i);
742 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100743 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000744 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100745 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
746 permissions, *orig_from_mode);
747
J-Alves788b4492023-04-18 14:01:23 +0100748 /*
749 * The assumption is that at this point, the operation from
750 * SP to a receiver VM, should have returned an FFA_ERROR
751 * already.
752 */
753 if (!ffa_is_vm_id(from.vm->id)) {
754 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000755 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100756 }
757
J-Alves460d36c2023-10-12 17:02:15 +0100758 /* Track if all senders are from current world. */
759 all_receivers_from_current_world =
760 all_receivers_from_current_world &&
761 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000762 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100763
J-Alves363f5722022-04-25 17:37:37 +0100764 if ((*orig_from_mode & required_from_mode) !=
765 required_from_mode) {
766 dlog_verbose(
767 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100768 "which required mode %#x but only had %#x "
769 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100770 required_from_mode, *orig_from_mode);
771 return ffa_error(FFA_DENIED);
772 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000773 }
774
J-Alves460d36c2023-10-12 17:02:15 +0100775 *map_action = ffa_mem_send_get_map_action(
776 all_receivers_from_current_world, from.vm->id, share_func);
777
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000778 /* Find the appropriate new mode. */
779 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000780 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100781 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000782 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100783 break;
784
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000786 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100787 break;
788
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100789 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000790 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100791 break;
792
Jose Marinho75509b42019-04-09 09:34:59 +0100793 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100794 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100795 }
796
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798}
799
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100800static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000801 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 struct ffa_memory_region_constituent **fragments,
803 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
804 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000805{
806 const uint32_t state_mask =
807 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
808 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000810
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 ret = constituents_get_mode(from, orig_from_mode, fragments,
812 fragment_constituent_counts,
813 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100815 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000816 }
817
818 /* Ensure the address range is normal memory and not a device. */
819 if (*orig_from_mode & MM_MODE_D) {
820 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
821 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100822 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000823 }
824
825 /*
826 * Ensure the relinquishing VM is not the owner but has access to the
827 * memory.
828 */
829 orig_from_state = *orig_from_mode & state_mask;
830 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
831 dlog_verbose(
832 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100833 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000834 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000836 }
837
838 /* Find the appropriate new mode. */
839 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
840
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100841 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000842}
843
844/**
845 * Verify that all pages have the same mode, that the starting mode
846 * constitutes a valid state and obtain the next mode to apply
847 * to the retrieving VM.
848 *
849 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 * 1) FFA_DENIED if a state transition was not found;
851 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100852 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100854 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
856 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857 */
J-Alvesfc19b372022-07-06 12:17:35 +0100858struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000859 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 struct ffa_memory_region_constituent **fragments,
861 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100862 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
863 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000864{
865 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867
Andrew Walbranca808b12020-05-15 17:22:28 +0100868 ret = constituents_get_mode(to, &orig_to_mode, fragments,
869 fragment_constituent_counts,
870 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100872 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100873 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 }
875
J-Alves460d36c2023-10-12 17:02:15 +0100876 /* Find the appropriate new mode. */
877 *to_mode = memory_to_attributes;
878
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000880 /*
881 * If the original ffa memory send call has been processed
882 * successfully, it is expected the orig_to_mode would overlay
883 * with `state_mask`, as a result of the function
884 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100885 *
886 * If Hafnium is the SPMC:
887 * - Caller of the reclaim interface is an SP, the memory shall
888 * have been protected throughout the flow.
889 * - Caller of the reclaim is from the NWd, the memory may have
890 * been protected at the time of lending/donating the memory.
891 * In such case, set action to unprotect memory in the
892 * handling of reclaim operation.
893 * - If Hafnium is the hypervisor memory shall never have been
894 * protected in memory lend/share/donate.
895 *
896 * More details in the doc comment of the function
897 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000898 */
J-Alves59ed0042022-07-28 18:26:41 +0100899 if (vm_id_is_current_world(to.vm->id)) {
900 assert((orig_to_mode &
901 (MM_MODE_INVALID | MM_MODE_UNOWNED |
902 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100903 assert(!memory_protected);
904 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
905 map_action != NULL && memory_protected) {
906 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100907 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000908 } else {
909 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100910 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000911 * Ensure the retriever has the expected state. We don't care
912 * about the MM_MODE_SHARED bit; either with or without it set
913 * are both valid representations of the !O-NA state.
914 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100915 if (vm_id_is_current_world(to.vm->id) &&
916 to.vm->id != HF_PRIMARY_VM_ID &&
917 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
918 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 }
J-Alves460d36c2023-10-12 17:02:15 +0100921
922 /*
923 * If memory has been protected before, clear the NS bit to
924 * allow the secure access from the SP.
925 */
926 if (memory_protected) {
927 *to_mode &= ~plat_ffa_other_world_mode();
928 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 }
930
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933 *to_mode |= 0;
934 break;
935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 *to_mode |= MM_MODE_UNOWNED;
938 break;
939
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000941 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
942 break;
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000945 *to_mode |= 0;
946 break;
947
948 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100949 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 }
952
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100954}
Jose Marinho09b1db82019-08-08 09:16:59 +0100955
J-Alvescf6253e2024-01-03 13:48:48 +0000956/*
957 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
958 * Returns:
959 * - FFA_SUCCESS_32: if all goes well.
960 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
961 * the page table update. Or error code provided by the function
962 * `arch_memory_protect`.
963 */
964static struct ffa_value ffa_region_group_check_actions(
965 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
966 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
967 bool *memory_protected)
968{
969 struct ffa_value ret;
970 bool is_memory_protected;
971
972 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
973 dlog_verbose(
974 "%s: memory can't be mapped to %x due to lack of "
975 "memory. Base: %lx end: %x\n",
976 __func__, vm_locked.vm->id, pa_addr(pa_begin),
977 pa_addr(pa_end));
978 return ffa_error(FFA_NO_MEMORY);
979 }
980
981 switch (action) {
982 case MAP_ACTION_CHECK:
983 /* No protect requested. */
984 is_memory_protected = false;
985 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
986 break;
987 case MAP_ACTION_CHECK_PROTECT: {
988 paddr_t last_protected_pa = pa_init(0);
989
990 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
991
992 is_memory_protected = (ret.func == FFA_SUCCESS_32);
993
994 /*
995 * - If protect memory has failed with FFA_DENIED, means some
996 * range of memory was in the wrong state. In such case, SPM
997 * reverts the state of the pages that were successfully
998 * updated.
999 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1000 * means the platform doesn't support the protection mechanism.
1001 * That said, it still permits the page table update to go
1002 * through. The variable
1003 * `is_memory_protected` will be equal to false.
1004 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1005 * break from switch and return the error.
1006 */
1007 if (ret.func == FFA_ERROR_32) {
1008 assert(!is_memory_protected);
1009 if (ffa_error_code(ret) == FFA_DENIED &&
1010 pa_addr(last_protected_pa) != (uintptr_t)0) {
1011 CHECK(arch_memory_unprotect(
1012 pa_begin,
1013 pa_add(last_protected_pa, PAGE_SIZE)));
1014 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1015 ret = (struct ffa_value){
1016 .func = FFA_SUCCESS_32,
1017 };
1018 }
1019 }
1020 } break;
1021 default:
1022 panic("%s: invalid action to process %x\n", __func__, action);
1023 }
1024
1025 if (memory_protected != NULL) {
1026 *memory_protected = is_memory_protected;
1027 }
1028
1029 return ret;
1030}
1031
1032static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1033 paddr_t pa_begin, paddr_t pa_end,
1034 struct mpool *ppool, uint32_t mode,
1035 enum ffa_map_action action)
1036{
1037 switch (action) {
1038 case MAP_ACTION_COMMIT_UNPROTECT:
1039 /*
1040 * Checking that it should succeed because SPM should be
1041 * unprotecting memory that it had protected before.
1042 */
1043 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1044 case MAP_ACTION_COMMIT:
1045 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1046 NULL);
1047 break;
1048 default:
1049 panic("%s: invalid action to process %x\n", __func__, action);
1050 }
1051}
1052
Jose Marinho09b1db82019-08-08 09:16:59 +01001053/**
J-Alves063ad832023-10-03 18:05:40 +01001054 * Helper function to revert a failed "Protect" action from the SPMC:
1055 * - `fragment_count`: should specify the number of fragments to traverse from
1056 * `fragments`. This may not be the full amount of fragments that are part of
1057 * the share_state structure.
1058 * - `fragment_constituent_counts`: array holding the amount of constituents
1059 * per fragment.
1060 * - `end`: pointer to the constituent that failed the "protect" action. It
1061 * shall be part of the last fragment, and it shall make the loop below break.
1062 */
1063static void ffa_region_group_fragments_revert_protect(
1064 struct ffa_memory_region_constituent **fragments,
1065 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1066 const struct ffa_memory_region_constituent *end)
1067{
1068 for (uint32_t i = 0; i < fragment_count; ++i) {
1069 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1070 struct ffa_memory_region_constituent *constituent =
1071 &fragments[i][j];
1072 size_t size = constituent->page_count * PAGE_SIZE;
1073 paddr_t pa_begin =
1074 pa_from_ipa(ipa_init(constituent->address));
1075 paddr_t pa_end = pa_add(pa_begin, size);
1076
1077 dlog_verbose("%s: reverting fragment %x size %x\n",
1078 __func__, pa_addr(pa_begin), size);
1079
1080 if (constituent == end) {
1081 /*
1082 * The last constituent is expected to be in the
1083 * last fragment.
1084 */
1085 assert(i == fragment_count - 1);
1086 break;
1087 }
1088
1089 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1090 }
1091 }
1092}
1093
1094/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001095 * Updates a VM's page table such that the given set of physical address ranges
1096 * are mapped in the address space at the corresponding address ranges, in the
1097 * mode provided.
1098 *
J-Alves0a83dc22023-05-05 09:50:37 +01001099 * The enum ffa_map_action determines the action taken from a call to the
1100 * function below:
1101 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1102 * mpool but no mappings will actually be updated. This function must always
1103 * be called first with action set to MAP_ACTION_CHECK to check that it will
1104 * succeed before calling ffa_region_group_identity_map with whichever one of
1105 * the remaining actions, to avoid leaving the page table in a half-updated
1106 * state.
1107 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1108 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001109 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1110 * invocation to the monitor to update the security state of the memory,
1111 * to that of the SPMC.
1112 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1113 * with a call into the monitor, to reset the security state of memory
1114 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001115 * vm_ptable_defrag should always be called after a series of page table
1116 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001117 *
J-Alvescf6253e2024-01-03 13:48:48 +00001118 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1119 * error codes:
1120 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1121 * - FFA_DENIED:
1122 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001123 * made to memory mappings.
1124 */
J-Alvescf6253e2024-01-03 13:48:48 +00001125struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001126 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001127 struct ffa_memory_region_constituent **fragments,
1128 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001129 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1130 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001131{
Andrew Walbranca808b12020-05-15 17:22:28 +01001132 uint32_t i;
1133 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001134 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001135
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001136 if (vm_locked.vm->el0_partition) {
1137 mode |= MM_MODE_USER | MM_MODE_NG;
1138 }
1139
Andrew Walbranca808b12020-05-15 17:22:28 +01001140 /* Iterate over the memory region constituents within each fragment. */
1141 for (i = 0; i < fragment_count; ++i) {
1142 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001143 struct ffa_memory_region_constituent *constituent =
1144 &fragments[i][j];
1145 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001146 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001147 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001149 uint32_t pa_bits =
1150 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001151
1152 /*
1153 * Ensure the requested region falls into system's PA
1154 * range.
1155 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001156 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1157 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001158 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001159 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001160 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001161
J-Alvescf6253e2024-01-03 13:48:48 +00001162 if (action <= MAP_ACTION_CHECK_PROTECT) {
1163 ret = ffa_region_group_check_actions(
1164 vm_locked, pa_begin, pa_end, ppool,
1165 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001166
1167 if (ret.func == FFA_ERROR_32 &&
1168 ffa_error_code(ret) == FFA_DENIED) {
1169 if (memory_protected != NULL) {
1170 assert(!*memory_protected);
1171 }
1172
1173 ffa_region_group_fragments_revert_protect(
1174 fragments,
1175 fragment_constituent_counts,
1176 i + 1, constituent);
1177 break;
1178 }
J-Alvescf6253e2024-01-03 13:48:48 +00001179 } else if (action >= MAP_ACTION_COMMIT &&
1180 action < MAP_ACTION_MAX) {
1181 ffa_region_group_commit_actions(
1182 vm_locked, pa_begin, pa_end, ppool,
1183 mode, action);
1184 ret = (struct ffa_value){
1185 .func = FFA_SUCCESS_32};
1186 } else {
1187 panic("%s: Unknown ffa_map_action.\n",
1188 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001190 }
1191 }
1192
J-Alvescf6253e2024-01-03 13:48:48 +00001193 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001194}
1195
1196/**
1197 * Clears a region of physical memory by overwriting it with zeros. The data is
1198 * flushed from the cache so the memory has been cleared across the system.
1199 */
J-Alves7db32002021-12-14 14:44:50 +00001200static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1201 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001202{
1203 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001204 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001205 * global mapping of the whole range. Such an approach will limit
1206 * the changes to stage-1 tables and will allow only local
1207 * invalidation.
1208 */
1209 bool ret;
1210 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001211 void *ptr = mm_identity_map(stage1_locked, begin, end,
1212 MM_MODE_W | (extra_mode_attributes &
1213 plat_ffa_other_world_mode()),
1214 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001215 size_t size = pa_difference(begin, end);
1216
1217 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001218 goto fail;
1219 }
1220
1221 memset_s(ptr, size, 0, size);
1222 arch_mm_flush_dcache(ptr, size);
1223 mm_unmap(stage1_locked, begin, end, ppool);
1224
1225 ret = true;
1226 goto out;
1227
1228fail:
1229 ret = false;
1230
1231out:
1232 mm_unlock_stage1(&stage1_locked);
1233
1234 return ret;
1235}
1236
1237/**
1238 * Clears a region of physical memory by overwriting it with zeros. The data is
1239 * flushed from the cache so the memory has been cleared across the system.
1240 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001242 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 struct ffa_memory_region_constituent **fragments,
1244 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1245 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001246{
1247 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001249 bool ret = false;
1250
1251 /*
1252 * Create a local pool so any freed memory can't be used by another
1253 * thread. This is to ensure each constituent that is mapped can be
1254 * unmapped again afterwards.
1255 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001256 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001257
Andrew Walbranca808b12020-05-15 17:22:28 +01001258 /* Iterate over the memory region constituents within each fragment. */
1259 for (i = 0; i < fragment_count; ++i) {
1260 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001261
J-Alves8457f932023-10-11 16:41:45 +01001262 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001263 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1264 paddr_t begin =
1265 pa_from_ipa(ipa_init(fragments[i][j].address));
1266 paddr_t end = pa_add(begin, size);
1267
J-Alves7db32002021-12-14 14:44:50 +00001268 if (!clear_memory(begin, end, &local_page_pool,
1269 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001270 /*
1271 * api_clear_memory will defrag on failure, so
1272 * no need to do it here.
1273 */
1274 goto out;
1275 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001276 }
1277 }
1278
Jose Marinho09b1db82019-08-08 09:16:59 +01001279 ret = true;
1280
1281out:
1282 mpool_fini(&local_page_pool);
1283 return ret;
1284}
1285
J-Alves5952d942022-12-22 16:03:00 +00001286static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1287 ipaddr_t in_begin, ipaddr_t in_end)
1288{
1289 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1290 ipa_addr(begin) < ipa_addr(in_end)) ||
1291 (ipa_addr(end) <= ipa_addr(in_end) &&
1292 ipa_addr(end) > ipa_addr(in_begin));
1293}
1294
1295/**
1296 * Receives a memory range and looks for overlaps with the remainder
1297 * constituents of the memory share/lend/donate operation. Assumes they are
1298 * passed in order to avoid having to loop over all the elements at each call.
1299 * The function only compares the received memory ranges with those that follow
1300 * within the same fragment, and subsequent fragments from the same operation.
1301 */
1302static bool ffa_memory_check_overlap(
1303 struct ffa_memory_region_constituent **fragments,
1304 const uint32_t *fragment_constituent_counts,
1305 const uint32_t fragment_count, const uint32_t current_fragment,
1306 const uint32_t current_constituent)
1307{
1308 uint32_t i = current_fragment;
1309 uint32_t j = current_constituent;
1310 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1311 const uint32_t current_page_count = fragments[i][j].page_count;
1312 size_t current_size = current_page_count * PAGE_SIZE;
1313 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1314
1315 if (current_size == 0 ||
1316 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1317 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1318 current_begin, current_page_count);
1319 return false;
1320 }
1321
1322 for (; i < fragment_count; i++) {
1323 j = (i == current_fragment) ? j + 1 : 0;
1324
1325 for (; j < fragment_constituent_counts[i]; j++) {
1326 ipaddr_t begin = ipa_init(fragments[i][j].address);
1327 const uint32_t page_count = fragments[i][j].page_count;
1328 size_t size = page_count * PAGE_SIZE;
1329 ipaddr_t end = ipa_add(begin, size - 1);
1330
1331 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1332 dlog_verbose(
1333 "Invalid page count. Addr: %x "
1334 "page_count: %x\n",
1335 begin, page_count);
1336 return false;
1337 }
1338
1339 /*
1340 * Check if current ranges is within begin and end, as
1341 * well as the reverse. This should help optimize the
1342 * loop, and reduce the number of iterations.
1343 */
1344 if (is_memory_range_within(begin, end, current_begin,
1345 current_end) ||
1346 is_memory_range_within(current_begin, current_end,
1347 begin, end)) {
1348 dlog_verbose(
1349 "Overlapping memory ranges: %#x - %#x "
1350 "with %#x - %#x\n",
1351 ipa_addr(begin), ipa_addr(end),
1352 ipa_addr(current_begin),
1353 ipa_addr(current_end));
1354 return true;
1355 }
1356 }
1357 }
1358
1359 return false;
1360}
1361
Jose Marinho09b1db82019-08-08 09:16:59 +01001362/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001363 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001364 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001365 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001366 *
1367 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001368 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001369 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001370 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001371 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1372 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001376 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001377static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001378 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001379 struct ffa_memory_region_constituent **fragments,
1380 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001381 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001382 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1383 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001384{
Andrew Walbranca808b12020-05-15 17:22:28 +01001385 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001386 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001387 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001388 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001389 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001390 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001392 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001393 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001394 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001395
1396 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397 * Make sure constituents are properly aligned to a 64-bit boundary. If
1398 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001399 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001400 for (i = 0; i < fragment_count; ++i) {
1401 if (!is_aligned(fragments[i], 8)) {
1402 dlog_verbose("Constituents not aligned.\n");
1403 return ffa_error(FFA_INVALID_PARAMETERS);
1404 }
J-Alves8f11cde2022-12-21 16:18:22 +00001405 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1406 constituents_total_page_count +=
1407 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001408 if (ffa_memory_check_overlap(
1409 fragments, fragment_constituent_counts,
1410 fragment_count, i, j)) {
1411 return ffa_error(FFA_INVALID_PARAMETERS);
1412 }
J-Alves8f11cde2022-12-21 16:18:22 +00001413 }
1414 }
1415
1416 if (constituents_total_page_count != composite_total_page_count) {
1417 dlog_verbose(
1418 "Composite page count differs from calculated page "
1419 "count from constituents.\n");
1420 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001421 }
1422
1423 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001424 * Check if the state transition is lawful for the sender, ensure that
1425 * all constituents of a memory region being shared are at the same
1426 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001427 */
J-Alves460d36c2023-10-12 17:02:15 +01001428 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001429 from_locked, share_func, memory_region, &orig_from_mode,
1430 fragments, fragment_constituent_counts, fragment_count,
1431 &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001433 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001435 }
1436
Andrew Walbran37c574e2020-06-03 11:45:46 +01001437 if (orig_from_mode_ret != NULL) {
1438 *orig_from_mode_ret = orig_from_mode;
1439 }
1440
Jose Marinho09b1db82019-08-08 09:16:59 +01001441 /*
1442 * Create a local pool so any freed memory can't be used by another
1443 * thread. This is to ensure the original mapping can be restored if the
1444 * clear fails.
1445 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001446 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001447
1448 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001449 * First reserve all required memory for the new page table entries
1450 * without committing, to make sure the entire operation will succeed
1451 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001452 * Provide the map_action as populated by 'ffa_send_check_transition'.
1453 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001454 */
J-Alvescf6253e2024-01-03 13:48:48 +00001455 ret = ffa_region_group_identity_map(
1456 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001457 fragment_count, from_mode, page_pool, map_action,
1458 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001459 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001460 goto out;
1461 }
1462
1463 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 * Update the mapping for the sender. This won't allocate because the
1465 * transaction was already prepared above, but may free pages in the
1466 * case that a whole block is being unmapped that was previously
1467 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001468 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001469 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001470 from_locked, fragments, fragment_constituent_counts,
1471 fragment_count, from_mode, &local_page_pool,
1472 MAP_ACTION_COMMIT, NULL)
1473 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001474
J-Alves460d36c2023-10-12 17:02:15 +01001475 /*
1476 * If memory has been protected, it is now part of the secure PAS
1477 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1478 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1479 * SPM's S1 translation.
1480 * In case memory hasn't been protected, and it is in the non-secure
1481 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1482 * perform a non-secure memory access. In such case `clean_mode` takes
1483 * the same mode as `orig_from_mode`.
1484 */
1485 clean_mode = (memory_protected != NULL && *memory_protected)
1486 ? orig_from_mode & ~plat_ffa_other_world_mode()
1487 : orig_from_mode;
1488
Jose Marinho09b1db82019-08-08 09:16:59 +01001489 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001490 if (clear && !ffa_clear_memory_constituents(
1491 clean_mode, fragments, fragment_constituent_counts,
1492 fragment_count, page_pool)) {
1493 map_action = (memory_protected != NULL && *memory_protected)
1494 ? MAP_ACTION_COMMIT_UNPROTECT
1495 : MAP_ACTION_COMMIT;
1496
Jose Marinho09b1db82019-08-08 09:16:59 +01001497 /*
1498 * On failure, roll back by returning memory to the sender. This
1499 * may allocate pages which were previously freed into
1500 * `local_page_pool` by the call above, but will never allocate
1501 * more pages than that so can never fail.
1502 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001504 from_locked, fragments,
1505 fragment_constituent_counts, fragment_count,
1506 orig_from_mode, &local_page_pool,
1507 MAP_ACTION_COMMIT, NULL)
1508 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001509 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001510 goto out;
1511 }
1512
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001514
1515out:
1516 mpool_fini(&local_page_pool);
1517
1518 /*
1519 * Tidy up the page table by reclaiming failed mappings (if there was an
1520 * error) or merging entries into blocks where possible (on success).
1521 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001522 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001523
1524 return ret;
1525}
1526
1527/**
1528 * Validates and maps memory shared from one VM to another.
1529 *
1530 * This function requires the calling context to hold the <to> lock.
1531 *
1532 * Returns:
1533 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001535 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001537 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001539 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001540struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001541 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001542 struct ffa_memory_region_constituent **fragments,
1543 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001544 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001545 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001546{
Andrew Walbranca808b12020-05-15 17:22:28 +01001547 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001548 uint32_t to_mode;
1549 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001551 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552
1553 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001554 * Make sure constituents are properly aligned to a 64-bit boundary. If
1555 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001556 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001557 for (i = 0; i < fragment_count; ++i) {
1558 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001559 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 return ffa_error(FFA_INVALID_PARAMETERS);
1561 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 }
1563
1564 /*
1565 * Check if the state transition is lawful for the recipient, and ensure
1566 * that all constituents of the memory region being retrieved are at the
1567 * same state.
1568 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 ret = ffa_retrieve_check_transition(
1570 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001571 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1572 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001573
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001574 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001575 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001576 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001577 }
1578
1579 /*
1580 * Create a local pool so any freed memory can't be used by another
1581 * thread. This is to ensure the original mapping can be restored if the
1582 * clear fails.
1583 */
1584 mpool_init_with_fallback(&local_page_pool, page_pool);
1585
1586 /*
1587 * First reserve all required memory for the new page table entries in
1588 * the recipient page tables without committing, to make sure the entire
1589 * operation will succeed without exhausting the page pool.
1590 */
J-Alvescf6253e2024-01-03 13:48:48 +00001591 ret = ffa_region_group_identity_map(
1592 to_locked, fragments, fragment_constituent_counts,
1593 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1594 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001595 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001596 goto out;
1597 }
1598
1599 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001600 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001601 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1602 fragment_constituent_counts,
1603 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001604 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001606 goto out;
1607 }
1608
Jose Marinho09b1db82019-08-08 09:16:59 +01001609 /*
1610 * Complete the transfer by mapping the memory into the recipient. This
1611 * won't allocate because the transaction was already prepared above, so
1612 * it doesn't need to use the `local_page_pool`.
1613 */
J-Alvesfd206052023-05-22 16:45:00 +01001614 CHECK(ffa_region_group_identity_map(
1615 to_locked, fragments, fragment_constituent_counts,
1616 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001617 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001618
J-Alves460d36c2023-10-12 17:02:15 +01001619 /* Return the mode used in mapping the memory in retriever's PT. */
1620 if (response_mode != NULL) {
1621 *response_mode = to_mode;
1622 }
1623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001624 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001625
1626out:
1627 mpool_fini(&local_page_pool);
1628
1629 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001630 * Tidy up the page table by reclaiming failed mappings (if there was an
1631 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001632 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001633 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001634
1635 return ret;
1636}
1637
Andrew Walbran996d1d12020-05-27 14:08:43 +01001638static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001639 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 struct ffa_memory_region_constituent **fragments,
1641 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1642 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001643{
1644 uint32_t orig_from_mode;
1645 uint32_t from_mode;
1646 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648
Andrew Walbranca808b12020-05-15 17:22:28 +01001649 ret = ffa_relinquish_check_transition(
1650 from_locked, &orig_from_mode, fragments,
1651 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001653 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001654 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 }
1656
1657 /*
1658 * Create a local pool so any freed memory can't be used by another
1659 * thread. This is to ensure the original mapping can be restored if the
1660 * clear fails.
1661 */
1662 mpool_init_with_fallback(&local_page_pool, page_pool);
1663
1664 /*
1665 * First reserve all required memory for the new page table entries
1666 * without committing, to make sure the entire operation will succeed
1667 * without exhausting the page pool.
1668 */
J-Alvescf6253e2024-01-03 13:48:48 +00001669 ret = ffa_region_group_identity_map(
1670 from_locked, fragments, fragment_constituent_counts,
1671 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1672 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001673 goto out;
1674 }
1675
1676 /*
1677 * Update the mapping for the sender. This won't allocate because the
1678 * transaction was already prepared above, but may free pages in the
1679 * case that a whole block is being unmapped that was previously
1680 * partially mapped.
1681 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001683 from_locked, fragments, fragment_constituent_counts,
1684 fragment_count, from_mode, &local_page_pool,
1685 MAP_ACTION_COMMIT, NULL)
1686 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687
1688 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001689 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001690 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1691 fragment_constituent_counts,
1692 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001693 /*
1694 * On failure, roll back by returning memory to the sender. This
1695 * may allocate pages which were previously freed into
1696 * `local_page_pool` by the call above, but will never allocate
1697 * more pages than that so can never fail.
1698 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001699 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001700 from_locked, fragments,
1701 fragment_constituent_counts, fragment_count,
1702 orig_from_mode, &local_page_pool,
1703 MAP_ACTION_COMMIT, NULL)
1704 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001705
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001706 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 goto out;
1708 }
1709
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001711
1712out:
1713 mpool_fini(&local_page_pool);
1714
1715 /*
1716 * Tidy up the page table by reclaiming failed mappings (if there was an
1717 * error) or merging entries into blocks where possible (on success).
1718 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001719 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001720
1721 return ret;
1722}
1723
1724/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 * Complete a memory sending operation by checking that it is valid, updating
1726 * the sender page table, and then either marking the share state as having
1727 * completed sending (on success) or freeing it (on failure).
1728 *
1729 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1730 */
J-Alvesfdd29272022-07-19 13:16:31 +01001731struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001732 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001733 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1734 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001735{
1736 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001737 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001738 struct ffa_value ret;
1739
1740 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001741 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001742 assert(memory_region != NULL);
1743 composite = ffa_memory_region_get_composite(memory_region, 0);
1744 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001745
1746 /* Check that state is valid in sender page table and update. */
1747 ret = ffa_send_check_update(
1748 from_locked, share_state->fragments,
1749 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001750 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001751 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001752 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001753 if (ret.func != FFA_SUCCESS_32) {
1754 /*
1755 * Free share state, it failed to send so it can't be retrieved.
1756 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001757 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1758 __func__, ffa_func_name(ret.func),
1759 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001760 share_state_free(share_states, share_state, page_pool);
1761 return ret;
1762 }
1763
1764 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001765 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001766
J-Alvesee68c542020-10-29 17:48:20 +00001767 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001768}
1769
1770/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001771 * Check that the memory attributes match Hafnium expectations:
1772 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1773 * Write-Allocate Cacheable.
1774 */
1775static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001776 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001777{
1778 enum ffa_memory_type memory_type;
1779 enum ffa_memory_cacheability cacheability;
1780 enum ffa_memory_shareability shareability;
1781
Karl Meakin84710f32023-10-12 15:14:49 +01001782 memory_type = attributes.type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001783 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1784 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1785 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001786 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001787 }
1788
Karl Meakin84710f32023-10-12 15:14:49 +01001789 cacheability = attributes.cacheability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001790 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1791 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1792 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001793 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001794 }
1795
Karl Meakin84710f32023-10-12 15:14:49 +01001796 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001797 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1798 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1799 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001800 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001801 }
1802
1803 return (struct ffa_value){.func = FFA_SUCCESS_32};
1804}
1805
1806/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001807 * Check that the given `memory_region` represents a valid memory send request
1808 * of the given `share_func` type, return the clear flag and permissions via the
1809 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001810 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001811 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001812 * not.
1813 */
J-Alves66652252022-07-06 09:49:51 +01001814struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001815 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1816 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001817 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001818{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001819 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001820 struct ffa_memory_access *receiver =
1821 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001822 uint64_t receivers_end;
1823 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001824 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001825 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001826 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001827 enum ffa_data_access data_access;
1828 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001829 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001830 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001831 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001832 memory_region->receivers_offset +
1833 memory_region->memory_access_desc_size +
1834 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001835
1836 if (fragment_length < minimum_first_fragment_length) {
1837 dlog_verbose("Fragment length %u too short (min %u).\n",
1838 (size_t)fragment_length,
1839 minimum_first_fragment_length);
1840 return ffa_error(FFA_INVALID_PARAMETERS);
1841 }
1842
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001843 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1844 "struct ffa_memory_region_constituent must be 16 bytes");
1845 if (!is_aligned(fragment_length,
1846 sizeof(struct ffa_memory_region_constituent)) ||
1847 !is_aligned(memory_share_length,
1848 sizeof(struct ffa_memory_region_constituent))) {
1849 dlog_verbose(
1850 "Fragment length %u or total length %u"
1851 " is not 16-byte aligned.\n",
1852 fragment_length, memory_share_length);
1853 return ffa_error(FFA_INVALID_PARAMETERS);
1854 }
1855
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001856 if (fragment_length > memory_share_length) {
1857 dlog_verbose(
1858 "Fragment length %u greater than total length %u.\n",
1859 (size_t)fragment_length, (size_t)memory_share_length);
1860 return ffa_error(FFA_INVALID_PARAMETERS);
1861 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001862
J-Alves95df0ef2022-12-07 10:09:48 +00001863 /* The sender must match the caller. */
1864 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1865 vm_id_is_current_world(memory_region->sender)) ||
1866 (vm_id_is_current_world(from_locked.vm->id) &&
1867 memory_region->sender != from_locked.vm->id)) {
1868 dlog_verbose("Invalid memory sender ID.\n");
1869 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001870 }
1871
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001872 if (memory_region->receiver_count <= 0) {
1873 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001874 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001875 }
1876
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001877 /*
1878 * Ensure that the composite header is within the memory bounds and
1879 * doesn't overlap the first part of the message. Cast to uint64_t
1880 * to prevent overflow.
1881 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001882 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001883 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001884 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001885 min_length = receivers_end +
1886 sizeof(struct ffa_composite_memory_region) +
1887 sizeof(struct ffa_memory_region_constituent);
1888 if (min_length > memory_share_length) {
1889 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1890 (size_t)memory_share_length, (size_t)min_length);
1891 return ffa_error(FFA_INVALID_PARAMETERS);
1892 }
1893
1894 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001895 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001896
1897 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001898 * Check that the composite memory region descriptor is after the access
1899 * descriptors, is at least 16-byte aligned, and fits in the first
1900 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001901 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001902 if ((composite_memory_region_offset < receivers_end) ||
1903 (composite_memory_region_offset % 16 != 0) ||
1904 (composite_memory_region_offset >
1905 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1906 dlog_verbose(
1907 "Invalid composite memory region descriptor offset "
1908 "%u.\n",
1909 (size_t)composite_memory_region_offset);
1910 return ffa_error(FFA_INVALID_PARAMETERS);
1911 }
1912
1913 /*
1914 * Compute the start of the constituent regions. Already checked
1915 * to be not more than fragment_length and thus not more than
1916 * memory_share_length.
1917 */
1918 constituents_start = composite_memory_region_offset +
1919 sizeof(struct ffa_composite_memory_region);
1920 constituents_length = memory_share_length - constituents_start;
1921
1922 /*
1923 * Check that the number of constituents is consistent with the length
1924 * of the constituent region.
1925 */
1926 composite = ffa_memory_region_get_composite(memory_region, 0);
1927 if ((constituents_length %
1928 sizeof(struct ffa_memory_region_constituent) !=
1929 0) ||
1930 ((constituents_length /
1931 sizeof(struct ffa_memory_region_constituent)) !=
1932 composite->constituent_count)) {
1933 dlog_verbose("Invalid length %u or composite offset %u.\n",
1934 (size_t)memory_share_length,
1935 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001936 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001937 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001938 if (fragment_length < memory_share_length &&
1939 fragment_length < HF_MAILBOX_SIZE) {
1940 dlog_warning(
1941 "Initial fragment length %d smaller than mailbox "
1942 "size.\n",
1943 fragment_length);
1944 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001945
Andrew Walbrana65a1322020-04-06 19:32:32 +01001946 /*
1947 * Clear is not allowed for memory sharing, as the sender still has
1948 * access to the memory.
1949 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001950 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1951 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001952 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001953 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001954 }
1955
1956 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001957 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001958 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001960 }
1961
J-Alves363f5722022-04-25 17:37:37 +01001962 /* Check that the permissions are valid, for each specified receiver. */
1963 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001964 struct ffa_memory_region_attributes receiver_permissions;
1965
1966 receiver = ffa_memory_region_get_receiver(memory_region, i);
1967 assert(receiver != NULL);
1968 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001969 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001970 receiver_permissions.permissions;
1971 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001972
1973 if (memory_region->sender == receiver_id) {
1974 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001975 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001976 }
Federico Recanati85090c42021-12-15 13:17:54 +01001977
J-Alves363f5722022-04-25 17:37:37 +01001978 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1979 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001980 struct ffa_memory_access *other_receiver =
1981 ffa_memory_region_get_receiver(memory_region,
1982 j);
1983 assert(other_receiver != NULL);
1984
J-Alves363f5722022-04-25 17:37:37 +01001985 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001986 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001987 dlog_verbose(
1988 "Repeated receiver(%x) in memory send "
1989 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001990 other_receiver->receiver_permissions
1991 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001992 return ffa_error(FFA_INVALID_PARAMETERS);
1993 }
1994 }
1995
1996 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001997 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01001998 dlog_verbose(
1999 "All ffa_memory_access should point to the "
2000 "same composite memory region offset.\n");
2001 return ffa_error(FFA_INVALID_PARAMETERS);
2002 }
2003
Karl Meakin84710f32023-10-12 15:14:49 +01002004 data_access = permissions.data_access;
2005 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002006 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2007 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2008 dlog_verbose(
2009 "Reserved value for receiver permissions "
2010 "%#x.\n",
2011 permissions);
2012 return ffa_error(FFA_INVALID_PARAMETERS);
2013 }
2014 if (instruction_access !=
2015 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2016 dlog_verbose(
2017 "Invalid instruction access permissions %#x "
2018 "for sending memory.\n",
2019 permissions);
2020 return ffa_error(FFA_INVALID_PARAMETERS);
2021 }
2022 if (share_func == FFA_MEM_SHARE_32) {
2023 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2024 dlog_verbose(
2025 "Invalid data access permissions %#x "
2026 "for sharing memory.\n",
2027 permissions);
2028 return ffa_error(FFA_INVALID_PARAMETERS);
2029 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002030 /*
2031 * According to section 10.10.3 of the FF-A v1.1 EAC0
2032 * spec, NX is required for share operations (but must
2033 * not be specified by the sender) so set it in the
2034 * copy that we store, ready to be returned to the
2035 * retriever.
2036 */
2037 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002038 permissions.instruction_access =
2039 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002040 receiver_permissions.permissions = permissions;
2041 }
J-Alves363f5722022-04-25 17:37:37 +01002042 }
2043 if (share_func == FFA_MEM_LEND_32 &&
2044 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2045 dlog_verbose(
2046 "Invalid data access permissions %#x for "
2047 "lending memory.\n",
2048 permissions);
2049 return ffa_error(FFA_INVALID_PARAMETERS);
2050 }
2051
2052 if (share_func == FFA_MEM_DONATE_32 &&
2053 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2054 dlog_verbose(
2055 "Invalid data access permissions %#x for "
2056 "donating memory.\n",
2057 permissions);
2058 return ffa_error(FFA_INVALID_PARAMETERS);
2059 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002060 }
2061
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002062 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002063 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002064 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2065 dlog_verbose(
2066 "Invalid security state for memory share operation.\n");
2067 return ffa_error(FFA_INVALID_PARAMETERS);
2068 }
2069
Federico Recanatid937f5e2021-12-20 17:38:23 +01002070 /*
J-Alves807794e2022-06-16 13:42:47 +01002071 * If a memory donate or lend with single borrower, the memory type
2072 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002073 */
J-Alves807794e2022-06-16 13:42:47 +01002074 if (share_func == FFA_MEM_DONATE_32 ||
2075 (share_func == FFA_MEM_LEND_32 &&
2076 memory_region->receiver_count == 1)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002077 if (memory_region->attributes.type !=
J-Alves807794e2022-06-16 13:42:47 +01002078 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2079 dlog_verbose(
2080 "Memory type shall not be specified by "
2081 "sender.\n");
2082 return ffa_error(FFA_INVALID_PARAMETERS);
2083 }
2084 } else {
2085 /*
2086 * Check that sender's memory attributes match Hafnium
2087 * expectations: Normal Memory, Inner shareable, Write-Back
2088 * Read-Allocate Write-Allocate Cacheable.
2089 */
2090 ret = ffa_memory_attributes_validate(memory_region->attributes);
2091 if (ret.func != FFA_SUCCESS_32) {
2092 return ret;
2093 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002094 }
2095
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002096 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002097}
2098
2099/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002100 * Gets the share state for continuing an operation to donate, lend or share
2101 * memory, and checks that it is a valid request.
2102 *
2103 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2104 * not.
2105 */
J-Alvesfdd29272022-07-19 13:16:31 +01002106struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002107 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002108 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002109 struct mpool *page_pool)
2110{
2111 struct ffa_memory_share_state *share_state;
2112 struct ffa_memory_region *memory_region;
2113
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002114 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002115
2116 /*
2117 * Look up the share state by handle and make sure that the VM ID
2118 * matches.
2119 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002120 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002121 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002122 dlog_verbose(
2123 "Invalid handle %#x for memory send continuation.\n",
2124 handle);
2125 return ffa_error(FFA_INVALID_PARAMETERS);
2126 }
2127 memory_region = share_state->memory_region;
2128
J-Alvesfdd29272022-07-19 13:16:31 +01002129 if (vm_id_is_current_world(from_vm_id) &&
2130 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002131 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2132 return ffa_error(FFA_INVALID_PARAMETERS);
2133 }
2134
2135 if (share_state->sending_complete) {
2136 dlog_verbose(
2137 "Sending of memory handle %#x is already complete.\n",
2138 handle);
2139 return ffa_error(FFA_INVALID_PARAMETERS);
2140 }
2141
2142 if (share_state->fragment_count == MAX_FRAGMENTS) {
2143 /*
2144 * Log a warning as this is a sign that MAX_FRAGMENTS should
2145 * probably be increased.
2146 */
2147 dlog_warning(
2148 "Too many fragments for memory share with handle %#x; "
2149 "only %d supported.\n",
2150 handle, MAX_FRAGMENTS);
2151 /* Free share state, as it's not possible to complete it. */
2152 share_state_free(share_states, share_state, page_pool);
2153 return ffa_error(FFA_NO_MEMORY);
2154 }
2155
2156 *share_state_ret = share_state;
2157
2158 return (struct ffa_value){.func = FFA_SUCCESS_32};
2159}
2160
2161/**
J-Alves95df0ef2022-12-07 10:09:48 +00002162 * Checks if there is at least one receiver from the other world.
2163 */
J-Alvesfdd29272022-07-19 13:16:31 +01002164bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002165 struct ffa_memory_region *memory_region)
2166{
2167 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002168 struct ffa_memory_access *receiver =
2169 ffa_memory_region_get_receiver(memory_region, i);
2170 assert(receiver != NULL);
2171 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2172
2173 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002174 return true;
2175 }
2176 }
2177 return false;
2178}
2179
2180/**
J-Alves9da280b2022-12-21 14:55:39 +00002181 * Validates a call to donate, lend or share memory in which Hafnium is the
2182 * designated allocator of the memory handle. In practice, this also means
2183 * Hafnium is responsible for managing the state structures for the transaction.
2184 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2185 * sender is an SP or there is at least one borrower that is an SP.
2186 * If Hafnium is the hypervisor, it should allocate the memory handle when
2187 * operation involves only NWd VMs.
2188 *
2189 * If validation goes well, Hafnium updates the stage-2 page tables of the
2190 * sender. Validation consists of checking if the message length and number of
2191 * memory region constituents match, and if the transition is valid for the
2192 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002193 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002194 * Assumes that the caller has already found and locked the sender VM and copied
2195 * the memory region descriptor from the sender's TX buffer to a freshly
2196 * allocated page from Hafnium's internal pool. The caller must have also
2197 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002198 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002199 * This function takes ownership of the `memory_region` passed in and will free
2200 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002201 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002202struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002203 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002204 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002205 uint32_t fragment_length, uint32_t share_func,
2206 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002207{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002208 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002209 struct share_states_locked share_states;
2210 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002211
2212 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002213 * If there is an error validating the `memory_region` then we need to
2214 * free it because we own it but we won't be storing it in a share state
2215 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002216 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002217 ret = ffa_memory_send_validate(from_locked, memory_region,
2218 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002219 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002220 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002221 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002222 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002223 }
2224
Andrew Walbrana65a1322020-04-06 19:32:32 +01002225 /* Set flag for share function, ready to be retrieved later. */
2226 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002227 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002228 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002229 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002230 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002231 case FFA_MEM_LEND_32:
2232 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002234 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002235 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002236 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002237 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002238 }
2239
Andrew Walbranca808b12020-05-15 17:22:28 +01002240 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002241 /*
2242 * Allocate a share state before updating the page table. Otherwise if
2243 * updating the page table succeeded but allocating the share state
2244 * failed then it would leave the memory in a state where nobody could
2245 * get it back.
2246 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002247 share_state = allocate_share_state(share_states, share_func,
2248 memory_region, fragment_length,
2249 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002250 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002251 dlog_verbose("Failed to allocate share state.\n");
2252 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002253 ret = ffa_error(FFA_NO_MEMORY);
2254 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002255 }
2256
Andrew Walbranca808b12020-05-15 17:22:28 +01002257 if (fragment_length == memory_share_length) {
2258 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002259 ret = ffa_memory_send_complete(
2260 from_locked, share_states, share_state, page_pool,
2261 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002262 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002263 /*
2264 * Use sender ID from 'memory_region' assuming
2265 * that at this point it has been validated:
2266 * - MBZ at virtual FF-A instance.
2267 */
J-Alves19e20cf2023-08-02 12:48:55 +01002268 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002269 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2270 ? memory_region->sender
2271 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002272 ret = (struct ffa_value){
2273 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002274 .arg1 = (uint32_t)memory_region->handle,
2275 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002276 .arg3 = fragment_length,
2277 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002278 }
2279
2280out:
2281 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002282 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002283 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002284}
2285
2286/**
J-Alves8505a8a2022-06-15 18:10:18 +01002287 * Continues an operation to donate, lend or share memory to a VM from current
2288 * world. If this is the last fragment then checks that the transition is valid
2289 * for the type of memory sending operation and updates the stage-2 page tables
2290 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002291 *
2292 * Assumes that the caller has already found and locked the sender VM and copied
2293 * the memory region descriptor from the sender's TX buffer to a freshly
2294 * allocated page from Hafnium's internal pool.
2295 *
2296 * This function takes ownership of the `fragment` passed in; it must not be
2297 * freed by the caller.
2298 */
2299struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2300 void *fragment,
2301 uint32_t fragment_length,
2302 ffa_memory_handle_t handle,
2303 struct mpool *page_pool)
2304{
2305 struct share_states_locked share_states = share_states_lock();
2306 struct ffa_memory_share_state *share_state;
2307 struct ffa_value ret;
2308 struct ffa_memory_region *memory_region;
2309
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002310 CHECK(is_aligned(fragment,
2311 alignof(struct ffa_memory_region_constituent)));
2312 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2313 0) {
2314 dlog_verbose("Fragment length %u misaligned.\n",
2315 fragment_length);
2316 ret = ffa_error(FFA_INVALID_PARAMETERS);
2317 goto out_free_fragment;
2318 }
2319
Andrew Walbranca808b12020-05-15 17:22:28 +01002320 ret = ffa_memory_send_continue_validate(share_states, handle,
2321 &share_state,
2322 from_locked.vm->id, page_pool);
2323 if (ret.func != FFA_SUCCESS_32) {
2324 goto out_free_fragment;
2325 }
2326 memory_region = share_state->memory_region;
2327
J-Alves95df0ef2022-12-07 10:09:48 +00002328 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002329 dlog_error(
2330 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002331 "other world. This should never happen, and indicates "
2332 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002333 "EL3 code.\n");
2334 ret = ffa_error(FFA_INVALID_PARAMETERS);
2335 goto out_free_fragment;
2336 }
2337
2338 /* Add this fragment. */
2339 share_state->fragments[share_state->fragment_count] = fragment;
2340 share_state->fragment_constituent_counts[share_state->fragment_count] =
2341 fragment_length / sizeof(struct ffa_memory_region_constituent);
2342 share_state->fragment_count++;
2343
2344 /* Check whether the memory send operation is now ready to complete. */
2345 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002346 ret = ffa_memory_send_complete(
2347 from_locked, share_states, share_state, page_pool,
2348 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002349 } else {
2350 ret = (struct ffa_value){
2351 .func = FFA_MEM_FRAG_RX_32,
2352 .arg1 = (uint32_t)handle,
2353 .arg2 = (uint32_t)(handle >> 32),
2354 .arg3 = share_state_next_fragment_offset(share_states,
2355 share_state)};
2356 }
2357 goto out;
2358
2359out_free_fragment:
2360 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002361
2362out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002363 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002364 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002365}
2366
Andrew Walbranca808b12020-05-15 17:22:28 +01002367/** Clean up after the receiver has finished retrieving a memory region. */
2368static void ffa_memory_retrieve_complete(
2369 struct share_states_locked share_states,
2370 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2371{
2372 if (share_state->share_func == FFA_MEM_DONATE_32) {
2373 /*
2374 * Memory that has been donated can't be relinquished,
2375 * so no need to keep the share state around.
2376 */
2377 share_state_free(share_states, share_state, page_pool);
2378 dlog_verbose("Freed share state for donate.\n");
2379 }
2380}
2381
J-Alves2d8457f2022-10-05 11:06:41 +01002382/**
2383 * Initialises the given memory region descriptor to be used for an
2384 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2385 * fragment.
2386 * The memory region descriptor is initialized according to retriever's
2387 * FF-A version.
2388 *
2389 * Returns true on success, or false if the given constituents won't all fit in
2390 * the first fragment.
2391 */
2392static bool ffa_retrieved_memory_region_init(
2393 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002394 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002395 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002396 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002397 struct ffa_memory_access *receivers, size_t receiver_count,
2398 uint32_t memory_access_desc_size, uint32_t page_count,
2399 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002400 const struct ffa_memory_region_constituent constituents[],
2401 uint32_t fragment_constituent_count, uint32_t *total_length,
2402 uint32_t *fragment_length)
2403{
2404 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002405 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002406 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002407 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002408
2409 assert(response != NULL);
2410
2411 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2412 struct ffa_memory_region_v1_0 *retrieve_response =
2413 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002414 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002415
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002416 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2417 attributes, flags, handle, 0,
2418 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002419
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002420 receiver = (struct ffa_memory_access_v1_0 *)
2421 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002422 receiver_count = retrieve_response->receiver_count;
2423
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002424 for (uint32_t i = 0; i < receiver_count; i++) {
2425 ffa_id_t receiver_id =
2426 receivers[i].receiver_permissions.receiver;
2427 ffa_memory_receiver_flags_t recv_flags =
2428 receivers[i].receiver_permissions.flags;
2429
2430 /*
2431 * Initialized here as in memory retrieve responses we
2432 * currently expect one borrower to be specified.
2433 */
2434 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002435 receiver, receiver_id, permissions.data_access,
2436 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002437 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002438
2439 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002440 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002441 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2442 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002443
2444 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2445 retrieve_response, 0);
2446 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002447 struct ffa_memory_region *retrieve_response =
2448 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002449 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002450
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002451 ffa_memory_region_init_header(
2452 retrieve_response, sender, attributes, flags, handle, 0,
2453 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002454
2455 /*
2456 * Note that `sizeof(struct_ffa_memory_region)` and
2457 * `sizeof(struct ffa_memory_access)` must both be multiples of
2458 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2459 * guaranteed that the offset we calculate here is aligned to a
2460 * 64-bit boundary and so 64-bit values can be copied without
2461 * alignment faults.
2462 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002463 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002464 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002465 (uint32_t)(receiver_count *
2466 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002467
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002468 retrieve_response_receivers =
2469 ffa_memory_region_get_receiver(retrieve_response, 0);
2470 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002471
2472 /*
2473 * Initialized here as in memory retrieve responses we currently
2474 * expect one borrower to be specified.
2475 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002476 memcpy_s(retrieve_response_receivers,
2477 sizeof(struct ffa_memory_access) * receiver_count,
2478 receivers,
2479 sizeof(struct ffa_memory_access) * receiver_count);
2480
2481 retrieve_response_receivers->composite_memory_region_offset =
2482 composite_offset;
2483
J-Alves2d8457f2022-10-05 11:06:41 +01002484 composite_memory_region =
2485 ffa_memory_region_get_composite(retrieve_response, 0);
2486 }
2487
J-Alves2d8457f2022-10-05 11:06:41 +01002488 assert(composite_memory_region != NULL);
2489
J-Alves2d8457f2022-10-05 11:06:41 +01002490 composite_memory_region->page_count = page_count;
2491 composite_memory_region->constituent_count = total_constituent_count;
2492 composite_memory_region->reserved_0 = 0;
2493
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002494 constituents_offset =
2495 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002496 if (constituents_offset +
2497 fragment_constituent_count *
2498 sizeof(struct ffa_memory_region_constituent) >
2499 response_max_size) {
2500 return false;
2501 }
2502
2503 for (i = 0; i < fragment_constituent_count; ++i) {
2504 composite_memory_region->constituents[i] = constituents[i];
2505 }
2506
2507 if (total_length != NULL) {
2508 *total_length =
2509 constituents_offset +
2510 composite_memory_region->constituent_count *
2511 sizeof(struct ffa_memory_region_constituent);
2512 }
2513 if (fragment_length != NULL) {
2514 *fragment_length =
2515 constituents_offset +
2516 fragment_constituent_count *
2517 sizeof(struct ffa_memory_region_constituent);
2518 }
2519
2520 return true;
2521}
2522
J-Alves96de29f2022-04-26 16:05:24 +01002523/**
2524 * Validates the retrieved permissions against those specified by the lender
2525 * of memory share operation. Optionally can help set the permissions to be used
2526 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002527 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2528 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2529 * specification for each ABI.
2530 * - FFA_DENIED -> if the permissions specified by the retriever are not
2531 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002532 */
J-Alvesdcad8992023-09-15 14:10:35 +01002533static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2534 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002535 enum ffa_data_access requested_data_access,
2536 enum ffa_instruction_access sent_instruction_access,
2537 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002538 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002539{
2540 switch (sent_data_access) {
2541 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2542 case FFA_DATA_ACCESS_RW:
2543 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2544 requested_data_access == FFA_DATA_ACCESS_RW) {
2545 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002546 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002547 }
2548 break;
2549 }
2550 /* Intentional fall-through. */
2551 case FFA_DATA_ACCESS_RO:
2552 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2553 requested_data_access == FFA_DATA_ACCESS_RO) {
2554 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002555 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002556 }
2557 break;
2558 }
2559 dlog_verbose(
2560 "Invalid data access requested; sender specified "
2561 "permissions %#x but receiver requested %#x.\n",
2562 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002563 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002564 case FFA_DATA_ACCESS_RESERVED:
2565 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2566 "checked before this point.");
2567 }
2568
J-Alvesdcad8992023-09-15 14:10:35 +01002569 /*
2570 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2571 * or FFA_MEMORY_DONATE the retriever should have specifed the
2572 * instruction permissions it wishes to receive.
2573 */
2574 switch (share_func) {
2575 case FFA_MEM_SHARE_32:
2576 if (requested_instruction_access !=
2577 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2578 dlog_verbose(
2579 "%s: for share instruction permissions must "
2580 "NOT be specified.\n",
2581 __func__);
2582 return ffa_error(FFA_INVALID_PARAMETERS);
2583 }
2584 break;
2585 case FFA_MEM_LEND_32:
2586 /*
2587 * For operations with multiple borrowers only permit XN
2588 * permissions, and both Sender and borrower should have used
2589 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2590 */
2591 if (multiple_borrowers) {
2592 if (requested_instruction_access !=
2593 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2594 dlog_verbose(
2595 "%s: lend/share/donate with multiple "
2596 "borrowers "
2597 "instruction permissions must NOT be "
2598 "specified.\n",
2599 __func__);
2600 return ffa_error(FFA_INVALID_PARAMETERS);
2601 }
2602 break;
2603 }
2604 /* Fall through if the operation targets a single borrower. */
2605 case FFA_MEM_DONATE_32:
2606 if (!multiple_borrowers &&
2607 requested_instruction_access ==
2608 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2609 dlog_verbose(
2610 "%s: for lend/donate with single borrower "
2611 "instruction permissions must be speficified "
2612 "by borrower\n",
2613 __func__);
2614 return ffa_error(FFA_INVALID_PARAMETERS);
2615 }
2616 break;
2617 default:
2618 panic("%s: Wrong func id provided.\n", __func__);
2619 }
2620
J-Alves96de29f2022-04-26 16:05:24 +01002621 switch (sent_instruction_access) {
2622 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2623 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002624 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002625 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002626 permissions->instruction_access =
2627 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002628 }
2629 break;
2630 }
J-Alvesdcad8992023-09-15 14:10:35 +01002631 /*
2632 * Fall through if requested permissions are less
2633 * permissive than those provided by the sender.
2634 */
J-Alves96de29f2022-04-26 16:05:24 +01002635 case FFA_INSTRUCTION_ACCESS_NX:
2636 if (requested_instruction_access ==
2637 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2638 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2639 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002640 permissions->instruction_access =
2641 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002642 }
2643 break;
2644 }
2645 dlog_verbose(
2646 "Invalid instruction access requested; sender "
2647 "specified permissions %#x but receiver requested "
2648 "%#x.\n",
2649 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002650 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002651 case FFA_INSTRUCTION_ACCESS_RESERVED:
2652 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2653 "be checked before this point.");
2654 }
2655
J-Alvesdcad8992023-09-15 14:10:35 +01002656 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002657}
2658
2659/**
2660 * Validate the receivers' permissions in the retrieve request against those
2661 * specified by the lender.
2662 * In the `permissions` argument returns the permissions to set at S2 for the
2663 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002664 * The function looks into the flag to bypass multiple borrower checks:
2665 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2666 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2667 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2668 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002669 */
2670static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2671 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002672 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002673 ffa_memory_access_permissions_t *permissions,
2674 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002675{
2676 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002677 bool bypass_multi_receiver_check =
2678 (retrieve_request->flags &
2679 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002680 const uint32_t region_receiver_count = memory_region->receiver_count;
2681 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002682
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002683 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002684 assert(permissions != NULL);
2685
Karl Meakin84710f32023-10-12 15:14:49 +01002686 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002687
J-Alves3456e032023-07-20 12:20:05 +01002688 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002689 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002690 dlog_verbose(
2691 "Retrieve request should contain same list of "
2692 "borrowers, as specified by the lender.\n");
2693 return ffa_error(FFA_INVALID_PARAMETERS);
2694 }
2695 } else {
2696 if (retrieve_request->receiver_count != 1) {
2697 dlog_verbose(
2698 "Set bypass multiple borrower check, receiver "
2699 "list must be sized 1 (%x)\n",
2700 memory_region->receiver_count);
2701 return ffa_error(FFA_INVALID_PARAMETERS);
2702 }
J-Alves96de29f2022-04-26 16:05:24 +01002703 }
2704
2705 retrieve_receiver_index = retrieve_request->receiver_count;
2706
J-Alves96de29f2022-04-26 16:05:24 +01002707 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2708 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002709 struct ffa_memory_access *retrieve_request_receiver =
2710 ffa_memory_region_get_receiver(retrieve_request, i);
2711 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002712 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002713 retrieve_request_receiver->receiver_permissions
2714 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002715 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002716 retrieve_request_receiver->receiver_permissions
2717 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002718 struct ffa_memory_access *receiver;
2719 uint32_t mem_region_receiver_index;
2720 bool permissions_RO;
2721 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002722 bool found_to_id = current_receiver_id == to_vm_id;
2723
J-Alves3456e032023-07-20 12:20:05 +01002724 if (bypass_multi_receiver_check && !found_to_id) {
2725 dlog_verbose(
2726 "Bypass multiple borrower check for id %x.\n",
2727 current_receiver_id);
2728 continue;
2729 }
2730
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002731 if (retrieve_request_receiver->composite_memory_region_offset !=
2732 0U) {
2733 dlog_verbose(
2734 "Retriever specified address ranges not "
2735 "supported (got offset %d).\n",
2736 retrieve_request_receiver
2737 ->composite_memory_region_offset);
2738 return ffa_error(FFA_INVALID_PARAMETERS);
2739 }
2740
J-Alves96de29f2022-04-26 16:05:24 +01002741 /*
2742 * Find the current receiver in the transaction descriptor from
2743 * sender.
2744 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002745 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002746 ffa_memory_region_get_receiver_index(
2747 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002748
2749 if (mem_region_receiver_index ==
2750 memory_region->receiver_count) {
2751 dlog_verbose("%s: receiver %x not found\n", __func__,
2752 current_receiver_id);
2753 return ffa_error(FFA_DENIED);
2754 }
2755
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002756 receiver = ffa_memory_region_get_receiver(
2757 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002758 assert(receiver != NULL);
2759
2760 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002761
2762 if (found_to_id) {
2763 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002764
2765 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002766 }
2767
2768 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002769 * Check if retrieve request memory access list is valid:
2770 * - The retrieve request complies with the specification.
2771 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002772 */
J-Alvesdcad8992023-09-15 14:10:35 +01002773 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002774 func_id, sent_permissions.data_access,
2775 requested_permissions.data_access,
2776 sent_permissions.instruction_access,
2777 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002778 found_to_id ? permissions : NULL,
2779 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002780
J-Alvesdcad8992023-09-15 14:10:35 +01002781 if (ret.func != FFA_SUCCESS_32) {
2782 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002783 }
2784
Karl Meakin84710f32023-10-12 15:14:49 +01002785 permissions_RO =
2786 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002787 clear_memory_flags =
2788 (retrieve_request->flags &
2789 (FFA_MEMORY_REGION_FLAG_CLEAR |
2790 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002791
J-Alves96de29f2022-04-26 16:05:24 +01002792 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002793 * Can't request PM to clear memory if only provided
2794 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002795 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002796 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002797 dlog_verbose(
2798 "Receiver has RO permissions can not request "
2799 "clear.\n");
2800 return ffa_error(FFA_DENIED);
2801 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002802
2803 /*
2804 * Check the impdef in the retrieve_request matches the value in
2805 * the original memory send.
2806 */
2807 if (ffa_version_from_memory_access_desc_size(
2808 memory_region->memory_access_desc_size) >=
2809 MAKE_FFA_VERSION(1, 2) &&
2810 ffa_version_from_memory_access_desc_size(
2811 retrieve_request->memory_access_desc_size) >=
2812 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002813 if (receiver->impdef.val[0] !=
2814 retrieve_request_receiver->impdef.val[0] ||
2815 receiver->impdef.val[1] !=
2816 retrieve_request_receiver->impdef.val[1]) {
2817 dlog_verbose(
2818 "Impdef value in memory send does not "
2819 "match retrieve request value "
2820 "send value %#x %#x retrieve request "
2821 "value %#x %#x\n",
2822 receiver->impdef.val[0],
2823 receiver->impdef.val[1],
2824 retrieve_request_receiver->impdef
2825 .val[0],
2826 retrieve_request_receiver->impdef
2827 .val[1]);
2828 return ffa_error(FFA_INVALID_PARAMETERS);
2829 }
2830 }
J-Alves96de29f2022-04-26 16:05:24 +01002831 }
2832
2833 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2834 dlog_verbose(
2835 "Retrieve request does not contain caller's (%x) "
2836 "permissions\n",
2837 to_vm_id);
2838 return ffa_error(FFA_INVALID_PARAMETERS);
2839 }
2840
2841 return (struct ffa_value){.func = FFA_SUCCESS_32};
2842}
2843
J-Alvesa9cd7e32022-07-01 13:49:33 +01002844/*
2845 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2846 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2847 * of a pending memory sharing operation whose allocator is the SPM, for
2848 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2849 * the memory region descriptor of the retrieve request must be zeroed with the
2850 * exception of the sender ID and handle.
2851 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002852bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2853 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002854{
2855 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01002856 request->attributes.shareability == 0U &&
2857 request->attributes.cacheability == 0U &&
2858 request->attributes.type == 0U &&
2859 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01002860 request->tag == 0U && request->receiver_count == 0U &&
2861 plat_ffa_memory_handle_allocated_by_current_world(
2862 request->handle);
2863}
2864
2865/*
2866 * Helper to reset count of fragments retrieved by the hypervisor.
2867 */
2868static void ffa_memory_retrieve_complete_from_hyp(
2869 struct ffa_memory_share_state *share_state)
2870{
2871 if (share_state->hypervisor_fragment_count ==
2872 share_state->fragment_count) {
2873 share_state->hypervisor_fragment_count = 0;
2874 }
2875}
2876
J-Alves089004f2022-07-13 14:25:44 +01002877/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002878 * Prepares the return of the ffa_value for the memory retrieve response.
2879 */
2880static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2881 uint32_t fragment_length)
2882{
2883 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2884 .arg1 = total_length,
2885 .arg2 = fragment_length};
2886}
2887
2888/**
J-Alves089004f2022-07-13 14:25:44 +01002889 * Validate that the memory region descriptor provided by the borrower on
2890 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2891 * memory sharing call.
2892 */
2893static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002894 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2895 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002896 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2897 uint32_t share_func)
2898{
2899 ffa_memory_region_flags_t transaction_type =
2900 retrieve_request->flags &
2901 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002902 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002903 const uint64_t memory_access_desc_size =
2904 retrieve_request->memory_access_desc_size;
2905 const uint32_t expected_retrieve_request_length =
2906 retrieve_request->receivers_offset +
2907 (uint32_t)(retrieve_request->receiver_count *
2908 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002909
2910 assert(retrieve_request != NULL);
2911 assert(memory_region != NULL);
2912 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01002913
J-Alves4f0d9c12024-01-17 17:23:11 +00002914 if (retrieve_request_length != expected_retrieve_request_length) {
2915 dlog_verbose(
2916 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2917 "but was %d.\n",
2918 expected_retrieve_request_length,
2919 retrieve_request_length);
2920 return ffa_error(FFA_INVALID_PARAMETERS);
2921 }
2922
2923 if (retrieve_request->sender != memory_region->sender) {
2924 dlog_verbose(
2925 "Memory with handle %#x not fully sent, can't "
2926 "retrieve.\n",
2927 memory_region->handle);
2928 return ffa_error(FFA_DENIED);
2929 }
2930
2931 /*
2932 * The SPMC can only process retrieve requests to memory share
2933 * operations with one borrower from the other world. It can't
2934 * determine the ID of the NWd VM that invoked the retrieve
2935 * request interface call. It relies on the hypervisor to
2936 * validate the caller's ID against that provided in the
2937 * `receivers` list of the retrieve response.
2938 * In case there is only one borrower from the NWd in the
2939 * transaction descriptor, record that in the `receiver_id` for
2940 * later use, and validate in the retrieve request message.
2941 * This limitation is due to the fact SPMC can't determine the
2942 * index in the memory share structures state to update.
2943 */
2944 if (to_id == HF_HYPERVISOR_VM_ID) {
2945 uint32_t other_world_count = 0;
2946
2947 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2948 struct ffa_memory_access *receiver =
2949 ffa_memory_region_get_receiver(retrieve_request,
2950 0);
2951 assert(receiver != NULL);
2952
2953 to_id = receiver->receiver_permissions.receiver;
2954
2955 if (!vm_id_is_current_world(to_id)) {
2956 other_world_count++;
2957 }
2958 }
2959
2960 if (other_world_count > 1) {
2961 dlog_verbose(
2962 "Support one receiver from the other "
2963 "world.\n");
2964 return ffa_error(FFA_NOT_SUPPORTED);
2965 }
2966 }
J-Alves089004f2022-07-13 14:25:44 +01002967 /*
2968 * Check that the transaction type expected by the receiver is
2969 * correct, if it has been specified.
2970 */
2971 if (transaction_type !=
2972 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2973 transaction_type != (memory_region->flags &
2974 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2975 dlog_verbose(
2976 "Incorrect transaction type %#x for "
2977 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2978 transaction_type,
2979 memory_region->flags &
2980 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2981 retrieve_request->handle);
2982 return ffa_error(FFA_INVALID_PARAMETERS);
2983 }
2984
2985 if (retrieve_request->tag != memory_region->tag) {
2986 dlog_verbose(
2987 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2988 "%d for handle %#x.\n",
2989 retrieve_request->tag, memory_region->tag,
2990 retrieve_request->handle);
2991 return ffa_error(FFA_INVALID_PARAMETERS);
2992 }
2993
J-Alves4f0d9c12024-01-17 17:23:11 +00002994 *receiver_index =
2995 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01002996
2997 if (*receiver_index == memory_region->receiver_count) {
2998 dlog_verbose(
2999 "Incorrect receiver VM ID %d for "
3000 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003001 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003002 return ffa_error(FFA_INVALID_PARAMETERS);
3003 }
3004
3005 if ((retrieve_request->flags &
3006 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3007 dlog_verbose(
3008 "Retriever specified 'address range alignment 'hint' "
3009 "not supported.\n");
3010 return ffa_error(FFA_INVALID_PARAMETERS);
3011 }
3012 if ((retrieve_request->flags &
3013 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3014 dlog_verbose(
3015 "Bits 8-5 must be zero in memory region's flags "
3016 "(address range alignment hint not supported).\n");
3017 return ffa_error(FFA_INVALID_PARAMETERS);
3018 }
3019
3020 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3021 dlog_verbose(
3022 "Bits 31-10 must be zero in memory region's flags.\n");
3023 return ffa_error(FFA_INVALID_PARAMETERS);
3024 }
3025
3026 if (share_func == FFA_MEM_SHARE_32 &&
3027 (retrieve_request->flags &
3028 (FFA_MEMORY_REGION_FLAG_CLEAR |
3029 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3030 dlog_verbose(
3031 "Memory Share operation can't clean after relinquish "
3032 "memory region.\n");
3033 return ffa_error(FFA_INVALID_PARAMETERS);
3034 }
3035
3036 /*
3037 * If the borrower needs the memory to be cleared before mapping
3038 * to its address space, the sender should have set the flag
3039 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3040 * FFA_DENIED.
3041 */
3042 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3043 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3044 dlog_verbose(
3045 "Borrower needs memory cleared. Sender needs to set "
3046 "flag for clearing memory.\n");
3047 return ffa_error(FFA_DENIED);
3048 }
3049
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003050 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003051 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003052 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3053 dlog_verbose(
3054 "Invalid security state for memory retrieve request "
3055 "operation.\n");
3056 return ffa_error(FFA_INVALID_PARAMETERS);
3057 }
3058
J-Alves089004f2022-07-13 14:25:44 +01003059 /*
3060 * If memory type is not specified, bypass validation of memory
3061 * attributes in the retrieve request. The retriever is expecting to
3062 * obtain this information from the SPMC.
3063 */
Karl Meakin84710f32023-10-12 15:14:49 +01003064 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003065 return (struct ffa_value){.func = FFA_SUCCESS_32};
3066 }
3067
3068 /*
3069 * Ensure receiver's attributes are compatible with how
3070 * Hafnium maps memory: Normal Memory, Inner shareable,
3071 * Write-Back Read-Allocate Write-Allocate Cacheable.
3072 */
3073 return ffa_memory_attributes_validate(retrieve_request->attributes);
3074}
3075
J-Alves4f0d9c12024-01-17 17:23:11 +00003076static struct ffa_value ffa_partition_retrieve_request(
3077 struct share_states_locked share_states,
3078 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3079 struct ffa_memory_region *retrieve_request,
3080 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003081{
Karl Meakin84710f32023-10-12 15:14:49 +01003082 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003083 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003084 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003085 struct ffa_composite_memory_region *composite;
3086 uint32_t total_length;
3087 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003088 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003089 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003090 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003091 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003092 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003093 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003094 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003095 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003096 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003097 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003098
J-Alves96de29f2022-04-26 16:05:24 +01003099 if (!share_state->sending_complete) {
3100 dlog_verbose(
3101 "Memory with handle %#x not fully sent, can't "
3102 "retrieve.\n",
3103 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003104 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003105 }
3106
J-Alves4f0d9c12024-01-17 17:23:11 +00003107 /*
3108 * Validate retrieve request, according to what was sent by the
3109 * sender. Function will output the `receiver_index` from the
3110 * provided memory region.
3111 */
3112 ret = ffa_memory_retrieve_validate(
3113 receiver_id, retrieve_request, retrieve_request_length,
3114 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003115
J-Alves4f0d9c12024-01-17 17:23:11 +00003116 if (ret.func != FFA_SUCCESS_32) {
3117 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003118 }
J-Alves96de29f2022-04-26 16:05:24 +01003119
J-Alves4f0d9c12024-01-17 17:23:11 +00003120 /*
3121 * Validate the requested permissions against the sent
3122 * permissions.
3123 * Outputs the permissions to give to retriever at S2
3124 * PTs.
3125 */
3126 ret = ffa_memory_retrieve_validate_memory_access_list(
3127 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003128 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003129 if (ret.func != FFA_SUCCESS_32) {
3130 return ret;
3131 }
3132
3133 memory_to_mode = ffa_memory_permissions_to_mode(
3134 permissions, share_state->sender_orig_mode);
3135
3136 ret = ffa_retrieve_check_update(
3137 to_locked, share_state->fragments,
3138 share_state->fragment_constituent_counts,
3139 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003140 share_state->share_func, false, page_pool, &retrieve_mode,
3141 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003142
3143 if (ret.func != FFA_SUCCESS_32) {
3144 return ret;
3145 }
3146
3147 share_state->retrieved_fragment_count[receiver_index] = 1;
3148
3149 is_retrieve_complete =
3150 share_state->retrieved_fragment_count[receiver_index] ==
3151 share_state->fragment_count;
3152
J-Alvesb5084cf2022-07-06 14:20:12 +01003153 /* VMs acquire the RX buffer from SPMC. */
3154 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3155
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003156 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003157 * Copy response to RX buffer of caller and deliver the message.
3158 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003159 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003160 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003161
Andrew Walbranca808b12020-05-15 17:22:28 +01003162 /*
J-Alves460d36c2023-10-12 17:02:15 +01003163 * Set the security state in the memory retrieve response attributes
3164 * if specified by the target mode.
3165 */
3166 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3167 retrieve_mode);
3168
3169 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003170 * Constituents which we received in the first fragment should
3171 * always fit in the first fragment we are sending, because the
3172 * header is the same size in both cases and we have a fixed
3173 * message buffer size. So `ffa_retrieved_memory_region_init`
3174 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003175 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003176
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003177 /* Provide the permissions that had been provided. */
3178 receiver->receiver_permissions.permissions = permissions;
3179
3180 /*
3181 * Prepare the memory region descriptor for the retrieve response.
3182 * Provide the pointer to the receiver tracked in the share state
3183 * strucutures.
3184 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003185 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003186 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003187 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003188 memory_region->flags, handle, permissions, receiver, 1,
3189 memory_access_desc_size, composite->page_count,
3190 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003191 share_state->fragment_constituent_counts[0], &total_length,
3192 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003193
J-Alves4f0d9c12024-01-17 17:23:11 +00003194 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003195 ffa_memory_retrieve_complete(share_states, share_state,
3196 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003197 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003198
3199 return ffa_memory_retrieve_resp(total_length, fragment_length);
3200}
3201
3202static struct ffa_value ffa_hypervisor_retrieve_request(
3203 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3204 struct ffa_memory_region *retrieve_request)
3205{
3206 struct ffa_value ret;
3207 struct ffa_composite_memory_region *composite;
3208 uint32_t total_length;
3209 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003210 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003211 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003212 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003213 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003214 ffa_memory_handle_t handle = retrieve_request->handle;
3215
J-Alves4f0d9c12024-01-17 17:23:11 +00003216 memory_region = share_state->memory_region;
3217
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003218 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3219
J-Alves7b6ab612024-01-24 09:54:54 +00003220 switch (to_locked.vm->ffa_version) {
3221 case MAKE_FFA_VERSION(1, 2):
3222 memory_access_desc_size = sizeof(struct ffa_memory_access);
3223 break;
3224 case MAKE_FFA_VERSION(1, 0):
3225 case MAKE_FFA_VERSION(1, 1):
3226 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3227 break;
3228 default:
3229 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3230 }
3231
J-Alves4f0d9c12024-01-17 17:23:11 +00003232 if (share_state->hypervisor_fragment_count != 0U) {
3233 dlog_verbose(
3234 "Memory with handle %#x already retrieved by "
3235 "the hypervisor.\n",
3236 handle);
3237 return ffa_error(FFA_DENIED);
3238 }
3239
3240 share_state->hypervisor_fragment_count = 1;
3241
3242 ffa_memory_retrieve_complete_from_hyp(share_state);
3243
3244 /* VMs acquire the RX buffer from SPMC. */
3245 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3246
3247 /*
3248 * Copy response to RX buffer of caller and deliver the message.
3249 * This must be done before the share_state is (possibly) freed.
3250 */
3251 composite = ffa_memory_region_get_composite(memory_region, 0);
3252
3253 /*
3254 * Constituents which we received in the first fragment should
3255 * always fit in the first fragment we are sending, because the
3256 * header is the same size in both cases and we have a fixed
3257 * message buffer size. So `ffa_retrieved_memory_region_init`
3258 * should never fail.
3259 */
3260
3261 /*
3262 * Set the security state in the memory retrieve response attributes
3263 * if specified by the target mode.
3264 */
3265 attributes = plat_ffa_memory_security_mode(
3266 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003267
3268 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3269
J-Alves4f0d9c12024-01-17 17:23:11 +00003270 CHECK(ffa_retrieved_memory_region_init(
3271 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3272 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003273 memory_region->flags, handle,
3274 receiver->receiver_permissions.permissions, receiver,
3275 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003276 composite->page_count, composite->constituent_count,
3277 share_state->fragments[0],
3278 share_state->fragment_constituent_counts[0], &total_length,
3279 &fragment_length));
3280
3281 return ffa_memory_retrieve_resp(total_length, fragment_length);
3282}
3283
3284struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3285 struct ffa_memory_region *retrieve_request,
3286 uint32_t retrieve_request_length,
3287 struct mpool *page_pool)
3288{
3289 ffa_memory_handle_t handle = retrieve_request->handle;
3290 struct share_states_locked share_states;
3291 struct ffa_memory_share_state *share_state;
3292 struct ffa_value ret;
3293
3294 dump_share_states();
3295
3296 share_states = share_states_lock();
3297 share_state = get_share_state(share_states, handle);
3298 if (share_state == NULL) {
3299 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3300 handle);
3301 ret = ffa_error(FFA_INVALID_PARAMETERS);
3302 goto out;
3303 }
3304
3305 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3306 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3307 retrieve_request);
3308 } else {
3309 ret = ffa_partition_retrieve_request(
3310 share_states, share_state, to_locked, retrieve_request,
3311 retrieve_request_length, page_pool);
3312 }
3313
3314 /* Track use of the RX buffer if the handling has succeeded. */
3315 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3316 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3317 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3318 }
3319
Andrew Walbranca808b12020-05-15 17:22:28 +01003320out:
3321 share_states_unlock(&share_states);
3322 dump_share_states();
3323 return ret;
3324}
3325
J-Alves5da37d92022-10-24 16:33:48 +01003326/**
3327 * Determine expected fragment offset according to the FF-A version of
3328 * the caller.
3329 */
3330static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3331 struct ffa_memory_region *memory_region,
3332 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3333{
3334 uint32_t expected_fragment_offset;
3335 uint32_t composite_constituents_offset;
3336
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003337 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003338 /*
3339 * Hafnium operates memory regions in FF-A v1.1 format, so we
3340 * can retrieve the constituents offset from descriptor.
3341 */
3342 composite_constituents_offset =
3343 ffa_composite_constituent_offset(memory_region, 0);
3344 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3345 /*
3346 * If retriever is FF-A v1.0, determine the composite offset
3347 * as it is expected to have been configured in the
3348 * retrieve response.
3349 */
3350 composite_constituents_offset =
3351 sizeof(struct ffa_memory_region_v1_0) +
3352 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003353 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003354 sizeof(struct ffa_composite_memory_region);
3355 } else {
3356 panic("%s received an invalid FF-A version.\n", __func__);
3357 }
3358
3359 expected_fragment_offset =
3360 composite_constituents_offset +
3361 retrieved_constituents_count *
3362 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003363 (uint32_t)(memory_region->memory_access_desc_size *
3364 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003365
3366 return expected_fragment_offset;
3367}
3368
Andrew Walbranca808b12020-05-15 17:22:28 +01003369struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3370 ffa_memory_handle_t handle,
3371 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003372 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003373 struct mpool *page_pool)
3374{
3375 struct ffa_memory_region *memory_region;
3376 struct share_states_locked share_states;
3377 struct ffa_memory_share_state *share_state;
3378 struct ffa_value ret;
3379 uint32_t fragment_index;
3380 uint32_t retrieved_constituents_count;
3381 uint32_t i;
3382 uint32_t expected_fragment_offset;
3383 uint32_t remaining_constituent_count;
3384 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003385 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003386 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003387
3388 dump_share_states();
3389
3390 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003391 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003392 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003393 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3394 handle);
3395 ret = ffa_error(FFA_INVALID_PARAMETERS);
3396 goto out;
3397 }
3398
3399 memory_region = share_state->memory_region;
3400 CHECK(memory_region != NULL);
3401
Andrew Walbranca808b12020-05-15 17:22:28 +01003402 if (!share_state->sending_complete) {
3403 dlog_verbose(
3404 "Memory with handle %#x not fully sent, can't "
3405 "retrieve.\n",
3406 handle);
3407 ret = ffa_error(FFA_INVALID_PARAMETERS);
3408 goto out;
3409 }
3410
J-Alves59ed0042022-07-28 18:26:41 +01003411 /*
3412 * If retrieve request from the hypervisor has been initiated in the
3413 * given share_state, continue it, else assume it is a continuation of
3414 * retrieve request from a NWd VM.
3415 */
3416 continue_ffa_hyp_mem_retrieve_req =
3417 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3418 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003419 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003420
J-Alves59ed0042022-07-28 18:26:41 +01003421 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003422 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003423 memory_region, to_locked.vm->id);
3424
3425 if (receiver_index == memory_region->receiver_count) {
3426 dlog_verbose(
3427 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3428 "borrower to memory sharing transaction (%x)\n",
3429 to_locked.vm->id, handle);
3430 ret = ffa_error(FFA_INVALID_PARAMETERS);
3431 goto out;
3432 }
3433
3434 if (share_state->retrieved_fragment_count[receiver_index] ==
3435 0 ||
3436 share_state->retrieved_fragment_count[receiver_index] >=
3437 share_state->fragment_count) {
3438 dlog_verbose(
3439 "Retrieval of memory with handle %#x not yet "
3440 "started or already completed (%d/%d fragments "
3441 "retrieved).\n",
3442 handle,
3443 share_state->retrieved_fragment_count
3444 [receiver_index],
3445 share_state->fragment_count);
3446 ret = ffa_error(FFA_INVALID_PARAMETERS);
3447 goto out;
3448 }
3449
3450 fragment_index =
3451 share_state->retrieved_fragment_count[receiver_index];
3452 } else {
3453 if (share_state->hypervisor_fragment_count == 0 ||
3454 share_state->hypervisor_fragment_count >=
3455 share_state->fragment_count) {
3456 dlog_verbose(
3457 "Retrieve of memory with handle %x not "
3458 "started from hypervisor.\n",
3459 handle);
3460 ret = ffa_error(FFA_INVALID_PARAMETERS);
3461 goto out;
3462 }
3463
3464 if (memory_region->sender != sender_vm_id) {
3465 dlog_verbose(
3466 "Sender ID (%x) is not as expected for memory "
3467 "handle %x\n",
3468 sender_vm_id, handle);
3469 ret = ffa_error(FFA_INVALID_PARAMETERS);
3470 goto out;
3471 }
3472
3473 fragment_index = share_state->hypervisor_fragment_count;
3474
3475 receiver_index = 0;
3476 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003477
3478 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003479 * Check that the given fragment offset is correct by counting
3480 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003481 */
3482 retrieved_constituents_count = 0;
3483 for (i = 0; i < fragment_index; ++i) {
3484 retrieved_constituents_count +=
3485 share_state->fragment_constituent_counts[i];
3486 }
J-Alvesc7484f12022-05-13 12:41:14 +01003487
3488 CHECK(memory_region->receiver_count > 0);
3489
Andrew Walbranca808b12020-05-15 17:22:28 +01003490 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003491 ffa_memory_retrieve_expected_offset_per_ffa_version(
3492 memory_region, retrieved_constituents_count,
3493 to_locked.vm->ffa_version);
3494
Andrew Walbranca808b12020-05-15 17:22:28 +01003495 if (fragment_offset != expected_fragment_offset) {
3496 dlog_verbose("Fragment offset was %d but expected %d.\n",
3497 fragment_offset, expected_fragment_offset);
3498 ret = ffa_error(FFA_INVALID_PARAMETERS);
3499 goto out;
3500 }
3501
J-Alves4f0d9c12024-01-17 17:23:11 +00003502 /*
3503 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3504 * is currently ownder by the SPMC.
3505 */
3506 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003507
Andrew Walbranca808b12020-05-15 17:22:28 +01003508 remaining_constituent_count = ffa_memory_fragment_init(
3509 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3510 share_state->fragments[fragment_index],
3511 share_state->fragment_constituent_counts[fragment_index],
3512 &fragment_length);
3513 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003514
Andrew Walbranca808b12020-05-15 17:22:28 +01003515 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003516 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003517
J-Alves59ed0042022-07-28 18:26:41 +01003518 if (!continue_ffa_hyp_mem_retrieve_req) {
3519 share_state->retrieved_fragment_count[receiver_index]++;
3520 if (share_state->retrieved_fragment_count[receiver_index] ==
3521 share_state->fragment_count) {
3522 ffa_memory_retrieve_complete(share_states, share_state,
3523 page_pool);
3524 }
3525 } else {
3526 share_state->hypervisor_fragment_count++;
3527
3528 ffa_memory_retrieve_complete_from_hyp(share_state);
3529 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003530 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3531 .arg1 = (uint32_t)handle,
3532 .arg2 = (uint32_t)(handle >> 32),
3533 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003534
3535out:
3536 share_states_unlock(&share_states);
3537 dump_share_states();
3538 return ret;
3539}
3540
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003541struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003542 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003543 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003544{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003545 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003546 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003547 struct ffa_memory_share_state *share_state;
3548 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003549 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003550 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003551 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003552 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003553 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003554
Andrew Walbrana65a1322020-04-06 19:32:32 +01003555 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003556 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003557 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003558 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003559 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003560 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003561 }
3562
Andrew Walbrana65a1322020-04-06 19:32:32 +01003563 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003564 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003565 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003566 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003567 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003568 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003569 }
3570
3571 dump_share_states();
3572
3573 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003574 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003575 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003576 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003577 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003578 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003579 goto out;
3580 }
3581
Andrew Walbranca808b12020-05-15 17:22:28 +01003582 if (!share_state->sending_complete) {
3583 dlog_verbose(
3584 "Memory with handle %#x not fully sent, can't "
3585 "relinquish.\n",
3586 handle);
3587 ret = ffa_error(FFA_INVALID_PARAMETERS);
3588 goto out;
3589 }
3590
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003591 memory_region = share_state->memory_region;
3592 CHECK(memory_region != NULL);
3593
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003594 receiver_index = ffa_memory_region_get_receiver_index(
3595 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003596
3597 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003598 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003599 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003600 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003601 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003602 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003603 goto out;
3604 }
3605
J-Alves8eb19162022-04-28 10:56:48 +01003606 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003607 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003608 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003609 "Memory with handle %#x not yet fully "
3610 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003611 "receiver %x can't relinquish.\n",
3612 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003613 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003614 goto out;
3615 }
3616
J-Alves3c5b2072022-11-21 12:45:40 +00003617 /*
3618 * Either clear if requested in relinquish call, or in a retrieve
3619 * request from one of the borrowers.
3620 */
3621 receivers_relinquished_memory = true;
3622
3623 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3624 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003625 ffa_memory_region_get_receiver(memory_region, i);
3626 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003627 if (receiver->receiver_permissions.receiver ==
3628 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003629 receiver_permissions =
3630 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003631 continue;
3632 }
3633
3634 if (share_state->retrieved_fragment_count[i] != 0U) {
3635 receivers_relinquished_memory = false;
3636 break;
3637 }
3638 }
3639
3640 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003641 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3642 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003643
3644 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003645 * Clear is not allowed for memory that was shared, as the
3646 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003647 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003648 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003649 dlog_verbose("Memory which was shared can't be cleared.\n");
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
Karl Meakin84710f32023-10-12 15:14:49 +01003654 if (clear && receiver_permissions.instruction_access == 0 &&
3655 receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003656 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3657 __func__);
3658 ret = ffa_error(FFA_DENIED);
3659 goto out;
3660 }
3661
Andrew Walbranca808b12020-05-15 17:22:28 +01003662 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003663 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003664 share_state->fragment_constituent_counts,
3665 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003666
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003667 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003668 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003669 * Mark memory handle as not retrieved, so it can be
3670 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003671 */
J-Alves8eb19162022-04-28 10:56:48 +01003672 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003673 }
3674
3675out:
3676 share_states_unlock(&share_states);
3677 dump_share_states();
3678 return ret;
3679}
3680
3681/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003682 * Validates that the reclaim transition is allowed for the given
3683 * handle, updates the page table of the reclaiming VM, and frees the
3684 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003685 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003686struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003687 ffa_memory_handle_t handle,
3688 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003689 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003690{
3691 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003692 struct ffa_memory_share_state *share_state;
3693 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003694 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003695
3696 dump_share_states();
3697
3698 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003699
Karl Meakin4a2854a2023-06-30 16:26:52 +01003700 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003701 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003702 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003703 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003704 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003705 goto out;
3706 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003707 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003708
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003709 CHECK(memory_region != NULL);
3710
J-Alvesa9cd7e32022-07-01 13:49:33 +01003711 if (vm_id_is_current_world(to_locked.vm->id) &&
3712 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003713 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003714 "VM %#x attempted to reclaim memory handle %#x "
3715 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003716 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003717 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003718 goto out;
3719 }
3720
Andrew Walbranca808b12020-05-15 17:22:28 +01003721 if (!share_state->sending_complete) {
3722 dlog_verbose(
3723 "Memory with handle %#x not fully sent, can't "
3724 "reclaim.\n",
3725 handle);
3726 ret = ffa_error(FFA_INVALID_PARAMETERS);
3727 goto out;
3728 }
3729
J-Alves752236c2022-04-28 11:07:47 +01003730 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3731 if (share_state->retrieved_fragment_count[i] != 0) {
3732 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003733 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003734 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003735 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003736 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003737 ffa_memory_region_get_receiver(memory_region, i)
3738 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003739 ret = ffa_error(FFA_DENIED);
3740 goto out;
3741 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003742 }
3743
Andrew Walbranca808b12020-05-15 17:22:28 +01003744 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003745 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003746 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003747 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003748 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003749 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003750
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003751 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003752 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003753 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003754 }
3755
3756out:
3757 share_states_unlock(&share_states);
3758 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003759}