blob: cddaa026d55949452b59c3a3e949a3528a02610a [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
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 switch (ffa_get_data_access_attr(permissions)) {
358 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
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 switch (ffa_get_instruction_access_attr(permissions)) {
372 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;
523
524 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
525 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
526 (struct ffa_memory_region_v1_0 *)memory_region;
527 /* 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. */
566 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
567 dlog_verbose(
568 "Max number of recipients supported is %u "
569 "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)
580 ? (struct ffa_memory_access *)&(
581 (struct ffa_memory_region_v1_0 *)
582 memory_region)
583 ->receivers[0]
584 : ffa_memory_region_get_receiver(memory_region, 0);
585 assert(receiver != NULL);
586 composite_offset_0 = receiver->composite_memory_region_offset;
587
588 if (!send_transaction) {
589 if (composite_offset_0 != 0) {
590 dlog_verbose(
591 "Composite offset memory region descriptor "
592 "offset must be 0 for retrieve requests. "
593 "Currently %d",
594 composite_offset_0);
595 return false;
596 }
597 } else {
598 bool comp_offset_is_zero = composite_offset_0 == 0U;
599 bool comp_offset_lt_transaction_descriptor_size =
600 composite_offset_0 <
601 (sizeof(struct ffa_memory_region) +
602 (uint32_t)(memory_region->memory_access_desc_size *
603 memory_region->receiver_count));
604 bool comp_offset_with_comp_gt_fragment_length =
605 composite_offset_0 +
606 sizeof(struct ffa_composite_memory_region) >
607 fragment_length;
608 if (comp_offset_is_zero ||
609 comp_offset_lt_transaction_descriptor_size ||
610 comp_offset_with_comp_gt_fragment_length) {
611 dlog_verbose(
612 "Invalid composite memory region descriptor "
613 "offset for send transaction %u\n",
614 composite_offset_0);
615 return false;
616 }
617 }
618
619 for (int i = 0; i < memory_region->receiver_count; i++) {
620 uint32_t composite_offset;
621
622 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
623 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
624 (struct ffa_memory_region_v1_0 *)memory_region;
625
626 struct ffa_memory_access_v1_0 *receiver_v1_0 =
627 &memory_region_v1_0->receivers[i];
628 /* Check reserved fields are 0 */
629 if (receiver_v1_0->reserved_0 != 0) {
630 dlog_verbose(
631 "Reserved field in the memory access "
632 " descriptor must be zero "
633 " Currently reciever %d has a reserved "
634 " field with a value of %d\n",
635 i, receiver_v1_0->reserved_0);
636 return false;
637 }
638 /*
639 * We can cast to the current version receiver as the
640 * remaining fields we are checking have the same
641 * offsets for all versions since memory access
642 * descriptors are forwards compatible.
643 */
644 receiver = (struct ffa_memory_access *)receiver_v1_0;
645 } else {
646 receiver = ffa_memory_region_get_receiver(memory_region,
647 i);
648 assert(receiver != NULL);
649
650 if (receiver->reserved_0 != 0) {
651 dlog_verbose(
652 "Reserved field in the memory access "
653 " descriptor must be zero "
654 " Currently reciever %d has a reserved "
655 " field with a value of %d\n",
656 i, receiver->reserved_0);
657 return false;
658 }
659 }
660
661 /* Check composite offset values are equal for all receivers. */
662 composite_offset = receiver->composite_memory_region_offset;
663 if (composite_offset != composite_offset_0) {
664 dlog_verbose(
665 "Composite offset %x differs from %x in index "
666 "%u\n",
667 composite_offset, composite_offset_0);
668 return false;
669 }
670 }
671 return true;
672}
673
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000674/**
J-Alves460d36c2023-10-12 17:02:15 +0100675 * If the receivers for the memory management operation are all from the
676 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
677 * state update by returning MAP_ACTION_CHECK_PROTECT.
678 */
679static enum ffa_map_action ffa_mem_send_get_map_action(
680 bool all_receivers_from_current_world, ffa_id_t sender_id,
681 uint32_t mem_func_id)
682{
683 bool protect_memory =
684 (mem_func_id != FFA_MEM_SHARE_32 &&
685 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
686
687 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
688}
689
690/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 * Verify that all pages have the same mode, that the starting mode
692 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100693 * to the sending VM. It outputs the mapping action that needs to be
694 * invoked for the given memory range. On memory lend/donate there
695 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000696 *
697 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 * 1) FFA_DENIED if a state transition was not found;
699 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100700 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100702 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
704 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000705 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100706static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100707 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000708 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100709 struct ffa_memory_region_constituent **fragments,
710 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100711 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000712{
713 const uint32_t state_mask =
714 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100715 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100716 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000717 uint32_t receivers_count = memory_region->receiver_count;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000718
Andrew Walbranca808b12020-05-15 17:22:28 +0100719 ret = constituents_get_mode(from, orig_from_mode, fragments,
720 fragment_constituent_counts,
721 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100722 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100723 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100724 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100725 }
726
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100728 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000729 dlog_verbose("Can't share device memory (mode is %#x).\n",
730 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000732 }
733
734 /*
735 * Ensure the sender is the owner and has exclusive access to the
736 * memory.
737 */
738 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100739 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100740 }
741
Daniel Boulbya76fd912024-02-22 14:22:15 +0000742 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100743
J-Alves363f5722022-04-25 17:37:37 +0100744 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000745 struct ffa_memory_access *receiver =
746 ffa_memory_region_get_receiver(memory_region, i);
747 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100748 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000749 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100750 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
751 permissions, *orig_from_mode);
752
J-Alves788b4492023-04-18 14:01:23 +0100753 /*
754 * The assumption is that at this point, the operation from
755 * SP to a receiver VM, should have returned an FFA_ERROR
756 * already.
757 */
758 if (!ffa_is_vm_id(from.vm->id)) {
759 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000760 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100761 }
762
J-Alves460d36c2023-10-12 17:02:15 +0100763 /* Track if all senders are from current world. */
764 all_receivers_from_current_world =
765 all_receivers_from_current_world &&
766 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000767 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100768
J-Alves363f5722022-04-25 17:37:37 +0100769 if ((*orig_from_mode & required_from_mode) !=
770 required_from_mode) {
771 dlog_verbose(
772 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100773 "which required mode %#x but only had %#x "
774 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100775 required_from_mode, *orig_from_mode);
776 return ffa_error(FFA_DENIED);
777 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000778 }
779
J-Alves460d36c2023-10-12 17:02:15 +0100780 *map_action = ffa_mem_send_get_map_action(
781 all_receivers_from_current_world, from.vm->id, share_func);
782
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000783 /* Find the appropriate new mode. */
784 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000785 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100786 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000787 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100788 break;
789
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100790 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000791 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100792 break;
793
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100794 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000795 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100796 break;
797
Jose Marinho75509b42019-04-09 09:34:59 +0100798 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100800 }
801
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100802 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000803}
804
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000806 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100807 struct ffa_memory_region_constituent **fragments,
808 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
809 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000810{
811 const uint32_t state_mask =
812 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
813 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000815
Andrew Walbranca808b12020-05-15 17:22:28 +0100816 ret = constituents_get_mode(from, orig_from_mode, fragments,
817 fragment_constituent_counts,
818 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100819 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100820 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000821 }
822
823 /* Ensure the address range is normal memory and not a device. */
824 if (*orig_from_mode & MM_MODE_D) {
825 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
826 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100827 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000828 }
829
830 /*
831 * Ensure the relinquishing VM is not the owner but has access to the
832 * memory.
833 */
834 orig_from_state = *orig_from_mode & state_mask;
835 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
836 dlog_verbose(
837 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100838 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000839 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100840 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000841 }
842
843 /* Find the appropriate new mode. */
844 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
845
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100846 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000847}
848
849/**
850 * Verify that all pages have the same mode, that the starting mode
851 * constitutes a valid state and obtain the next mode to apply
852 * to the retrieving VM.
853 *
854 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 * 1) FFA_DENIED if a state transition was not found;
856 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100857 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100858 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100859 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100860 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
861 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000862 */
J-Alvesfc19b372022-07-06 12:17:35 +0100863struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000864 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100865 struct ffa_memory_region_constituent **fragments,
866 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100867 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
868 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000869{
870 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000872
Andrew Walbranca808b12020-05-15 17:22:28 +0100873 ret = constituents_get_mode(to, &orig_to_mode, fragments,
874 fragment_constituent_counts,
875 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100876 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100877 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100878 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000879 }
880
J-Alves460d36c2023-10-12 17:02:15 +0100881 /* Find the appropriate new mode. */
882 *to_mode = memory_to_attributes;
883
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000885 /*
886 * If the original ffa memory send call has been processed
887 * successfully, it is expected the orig_to_mode would overlay
888 * with `state_mask`, as a result of the function
889 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100890 *
891 * If Hafnium is the SPMC:
892 * - Caller of the reclaim interface is an SP, the memory shall
893 * have been protected throughout the flow.
894 * - Caller of the reclaim is from the NWd, the memory may have
895 * been protected at the time of lending/donating the memory.
896 * In such case, set action to unprotect memory in the
897 * handling of reclaim operation.
898 * - If Hafnium is the hypervisor memory shall never have been
899 * protected in memory lend/share/donate.
900 *
901 * More details in the doc comment of the function
902 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000903 */
J-Alves59ed0042022-07-28 18:26:41 +0100904 if (vm_id_is_current_world(to.vm->id)) {
905 assert((orig_to_mode &
906 (MM_MODE_INVALID | MM_MODE_UNOWNED |
907 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100908 assert(!memory_protected);
909 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
910 map_action != NULL && memory_protected) {
911 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100912 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000913 } else {
914 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100915 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 * Ensure the retriever has the expected state. We don't care
917 * about the MM_MODE_SHARED bit; either with or without it set
918 * are both valid representations of the !O-NA state.
919 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100920 if (vm_id_is_current_world(to.vm->id) &&
921 to.vm->id != HF_PRIMARY_VM_ID &&
922 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
923 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100924 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000925 }
J-Alves460d36c2023-10-12 17:02:15 +0100926
927 /*
928 * If memory has been protected before, clear the NS bit to
929 * allow the secure access from the SP.
930 */
931 if (memory_protected) {
932 *to_mode &= ~plat_ffa_other_world_mode();
933 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000934 }
935
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000936 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000938 *to_mode |= 0;
939 break;
940
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100941 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000942 *to_mode |= MM_MODE_UNOWNED;
943 break;
944
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100945 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000946 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
947 break;
948
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000950 *to_mode |= 0;
951 break;
952
953 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100954 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000956 }
957
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100958 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100959}
Jose Marinho09b1db82019-08-08 09:16:59 +0100960
J-Alvescf6253e2024-01-03 13:48:48 +0000961/*
962 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
963 * Returns:
964 * - FFA_SUCCESS_32: if all goes well.
965 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
966 * the page table update. Or error code provided by the function
967 * `arch_memory_protect`.
968 */
969static struct ffa_value ffa_region_group_check_actions(
970 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
971 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
972 bool *memory_protected)
973{
974 struct ffa_value ret;
975 bool is_memory_protected;
976
977 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
978 dlog_verbose(
979 "%s: memory can't be mapped to %x due to lack of "
980 "memory. Base: %lx end: %x\n",
981 __func__, vm_locked.vm->id, pa_addr(pa_begin),
982 pa_addr(pa_end));
983 return ffa_error(FFA_NO_MEMORY);
984 }
985
986 switch (action) {
987 case MAP_ACTION_CHECK:
988 /* No protect requested. */
989 is_memory_protected = false;
990 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
991 break;
992 case MAP_ACTION_CHECK_PROTECT: {
993 paddr_t last_protected_pa = pa_init(0);
994
995 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
996
997 is_memory_protected = (ret.func == FFA_SUCCESS_32);
998
999 /*
1000 * - If protect memory has failed with FFA_DENIED, means some
1001 * range of memory was in the wrong state. In such case, SPM
1002 * reverts the state of the pages that were successfully
1003 * updated.
1004 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1005 * means the platform doesn't support the protection mechanism.
1006 * That said, it still permits the page table update to go
1007 * through. The variable
1008 * `is_memory_protected` will be equal to false.
1009 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1010 * break from switch and return the error.
1011 */
1012 if (ret.func == FFA_ERROR_32) {
1013 assert(!is_memory_protected);
1014 if (ffa_error_code(ret) == FFA_DENIED &&
1015 pa_addr(last_protected_pa) != (uintptr_t)0) {
1016 CHECK(arch_memory_unprotect(
1017 pa_begin,
1018 pa_add(last_protected_pa, PAGE_SIZE)));
1019 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1020 ret = (struct ffa_value){
1021 .func = FFA_SUCCESS_32,
1022 };
1023 }
1024 }
1025 } break;
1026 default:
1027 panic("%s: invalid action to process %x\n", __func__, action);
1028 }
1029
1030 if (memory_protected != NULL) {
1031 *memory_protected = is_memory_protected;
1032 }
1033
1034 return ret;
1035}
1036
1037static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1038 paddr_t pa_begin, paddr_t pa_end,
1039 struct mpool *ppool, uint32_t mode,
1040 enum ffa_map_action action)
1041{
1042 switch (action) {
1043 case MAP_ACTION_COMMIT_UNPROTECT:
1044 /*
1045 * Checking that it should succeed because SPM should be
1046 * unprotecting memory that it had protected before.
1047 */
1048 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1049 case MAP_ACTION_COMMIT:
1050 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1051 NULL);
1052 break;
1053 default:
1054 panic("%s: invalid action to process %x\n", __func__, action);
1055 }
1056}
1057
Jose Marinho09b1db82019-08-08 09:16:59 +01001058/**
J-Alves063ad832023-10-03 18:05:40 +01001059 * Helper function to revert a failed "Protect" action from the SPMC:
1060 * - `fragment_count`: should specify the number of fragments to traverse from
1061 * `fragments`. This may not be the full amount of fragments that are part of
1062 * the share_state structure.
1063 * - `fragment_constituent_counts`: array holding the amount of constituents
1064 * per fragment.
1065 * - `end`: pointer to the constituent that failed the "protect" action. It
1066 * shall be part of the last fragment, and it shall make the loop below break.
1067 */
1068static void ffa_region_group_fragments_revert_protect(
1069 struct ffa_memory_region_constituent **fragments,
1070 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1071 const struct ffa_memory_region_constituent *end)
1072{
1073 for (uint32_t i = 0; i < fragment_count; ++i) {
1074 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1075 struct ffa_memory_region_constituent *constituent =
1076 &fragments[i][j];
1077 size_t size = constituent->page_count * PAGE_SIZE;
1078 paddr_t pa_begin =
1079 pa_from_ipa(ipa_init(constituent->address));
1080 paddr_t pa_end = pa_add(pa_begin, size);
1081
1082 dlog_verbose("%s: reverting fragment %x size %x\n",
1083 __func__, pa_addr(pa_begin), size);
1084
1085 if (constituent == end) {
1086 /*
1087 * The last constituent is expected to be in the
1088 * last fragment.
1089 */
1090 assert(i == fragment_count - 1);
1091 break;
1092 }
1093
1094 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1095 }
1096 }
1097}
1098
1099/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001100 * Updates a VM's page table such that the given set of physical address ranges
1101 * are mapped in the address space at the corresponding address ranges, in the
1102 * mode provided.
1103 *
J-Alves0a83dc22023-05-05 09:50:37 +01001104 * The enum ffa_map_action determines the action taken from a call to the
1105 * function below:
1106 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1107 * mpool but no mappings will actually be updated. This function must always
1108 * be called first with action set to MAP_ACTION_CHECK to check that it will
1109 * succeed before calling ffa_region_group_identity_map with whichever one of
1110 * the remaining actions, to avoid leaving the page table in a half-updated
1111 * state.
1112 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1113 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001114 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1115 * invocation to the monitor to update the security state of the memory,
1116 * to that of the SPMC.
1117 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1118 * with a call into the monitor, to reset the security state of memory
1119 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001120 * vm_ptable_defrag should always be called after a series of page table
1121 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001122 *
J-Alvescf6253e2024-01-03 13:48:48 +00001123 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1124 * error codes:
1125 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1126 * - FFA_DENIED:
1127 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001128 * made to memory mappings.
1129 */
J-Alvescf6253e2024-01-03 13:48:48 +00001130struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001131 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001132 struct ffa_memory_region_constituent **fragments,
1133 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001134 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1135 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001136{
Andrew Walbranca808b12020-05-15 17:22:28 +01001137 uint32_t i;
1138 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001139 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001140
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001141 if (vm_locked.vm->el0_partition) {
1142 mode |= MM_MODE_USER | MM_MODE_NG;
1143 }
1144
Andrew Walbranca808b12020-05-15 17:22:28 +01001145 /* Iterate over the memory region constituents within each fragment. */
1146 for (i = 0; i < fragment_count; ++i) {
1147 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001148 struct ffa_memory_region_constituent *constituent =
1149 &fragments[i][j];
1150 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001151 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001152 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001153 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001154 uint32_t pa_bits =
1155 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001156
1157 /*
1158 * Ensure the requested region falls into system's PA
1159 * range.
1160 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001161 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1162 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001163 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001164 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001165 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001166
J-Alvescf6253e2024-01-03 13:48:48 +00001167 if (action <= MAP_ACTION_CHECK_PROTECT) {
1168 ret = ffa_region_group_check_actions(
1169 vm_locked, pa_begin, pa_end, ppool,
1170 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001171
1172 if (ret.func == FFA_ERROR_32 &&
1173 ffa_error_code(ret) == FFA_DENIED) {
1174 if (memory_protected != NULL) {
1175 assert(!*memory_protected);
1176 }
1177
1178 ffa_region_group_fragments_revert_protect(
1179 fragments,
1180 fragment_constituent_counts,
1181 i + 1, constituent);
1182 break;
1183 }
J-Alvescf6253e2024-01-03 13:48:48 +00001184 } else if (action >= MAP_ACTION_COMMIT &&
1185 action < MAP_ACTION_MAX) {
1186 ffa_region_group_commit_actions(
1187 vm_locked, pa_begin, pa_end, ppool,
1188 mode, action);
1189 ret = (struct ffa_value){
1190 .func = FFA_SUCCESS_32};
1191 } else {
1192 panic("%s: Unknown ffa_map_action.\n",
1193 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001194 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001195 }
1196 }
1197
J-Alvescf6253e2024-01-03 13:48:48 +00001198 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001199}
1200
1201/**
1202 * Clears a region of physical memory by overwriting it with zeros. The data is
1203 * flushed from the cache so the memory has been cleared across the system.
1204 */
J-Alves7db32002021-12-14 14:44:50 +00001205static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1206 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001207{
1208 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001209 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001210 * global mapping of the whole range. Such an approach will limit
1211 * the changes to stage-1 tables and will allow only local
1212 * invalidation.
1213 */
1214 bool ret;
1215 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001216 void *ptr = mm_identity_map(stage1_locked, begin, end,
1217 MM_MODE_W | (extra_mode_attributes &
1218 plat_ffa_other_world_mode()),
1219 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001220 size_t size = pa_difference(begin, end);
1221
1222 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001223 goto fail;
1224 }
1225
1226 memset_s(ptr, size, 0, size);
1227 arch_mm_flush_dcache(ptr, size);
1228 mm_unmap(stage1_locked, begin, end, ppool);
1229
1230 ret = true;
1231 goto out;
1232
1233fail:
1234 ret = false;
1235
1236out:
1237 mm_unlock_stage1(&stage1_locked);
1238
1239 return ret;
1240}
1241
1242/**
1243 * Clears a region of physical memory by overwriting it with zeros. The data is
1244 * flushed from the cache so the memory has been cleared across the system.
1245 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001247 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 struct ffa_memory_region_constituent **fragments,
1249 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1250 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001251{
1252 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001253 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001254 bool ret = false;
1255
1256 /*
1257 * Create a local pool so any freed memory can't be used by another
1258 * thread. This is to ensure each constituent that is mapped can be
1259 * unmapped again afterwards.
1260 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001261 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001262
Andrew Walbranca808b12020-05-15 17:22:28 +01001263 /* Iterate over the memory region constituents within each fragment. */
1264 for (i = 0; i < fragment_count; ++i) {
1265 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001266
J-Alves8457f932023-10-11 16:41:45 +01001267 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001268 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1269 paddr_t begin =
1270 pa_from_ipa(ipa_init(fragments[i][j].address));
1271 paddr_t end = pa_add(begin, size);
1272
J-Alves7db32002021-12-14 14:44:50 +00001273 if (!clear_memory(begin, end, &local_page_pool,
1274 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001275 /*
1276 * api_clear_memory will defrag on failure, so
1277 * no need to do it here.
1278 */
1279 goto out;
1280 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001281 }
1282 }
1283
Jose Marinho09b1db82019-08-08 09:16:59 +01001284 ret = true;
1285
1286out:
1287 mpool_fini(&local_page_pool);
1288 return ret;
1289}
1290
J-Alves5952d942022-12-22 16:03:00 +00001291static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1292 ipaddr_t in_begin, ipaddr_t in_end)
1293{
1294 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1295 ipa_addr(begin) < ipa_addr(in_end)) ||
1296 (ipa_addr(end) <= ipa_addr(in_end) &&
1297 ipa_addr(end) > ipa_addr(in_begin));
1298}
1299
1300/**
1301 * Receives a memory range and looks for overlaps with the remainder
1302 * constituents of the memory share/lend/donate operation. Assumes they are
1303 * passed in order to avoid having to loop over all the elements at each call.
1304 * The function only compares the received memory ranges with those that follow
1305 * within the same fragment, and subsequent fragments from the same operation.
1306 */
1307static bool ffa_memory_check_overlap(
1308 struct ffa_memory_region_constituent **fragments,
1309 const uint32_t *fragment_constituent_counts,
1310 const uint32_t fragment_count, const uint32_t current_fragment,
1311 const uint32_t current_constituent)
1312{
1313 uint32_t i = current_fragment;
1314 uint32_t j = current_constituent;
1315 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1316 const uint32_t current_page_count = fragments[i][j].page_count;
1317 size_t current_size = current_page_count * PAGE_SIZE;
1318 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1319
1320 if (current_size == 0 ||
1321 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1322 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1323 current_begin, current_page_count);
1324 return false;
1325 }
1326
1327 for (; i < fragment_count; i++) {
1328 j = (i == current_fragment) ? j + 1 : 0;
1329
1330 for (; j < fragment_constituent_counts[i]; j++) {
1331 ipaddr_t begin = ipa_init(fragments[i][j].address);
1332 const uint32_t page_count = fragments[i][j].page_count;
1333 size_t size = page_count * PAGE_SIZE;
1334 ipaddr_t end = ipa_add(begin, size - 1);
1335
1336 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1337 dlog_verbose(
1338 "Invalid page count. Addr: %x "
1339 "page_count: %x\n",
1340 begin, page_count);
1341 return false;
1342 }
1343
1344 /*
1345 * Check if current ranges is within begin and end, as
1346 * well as the reverse. This should help optimize the
1347 * loop, and reduce the number of iterations.
1348 */
1349 if (is_memory_range_within(begin, end, current_begin,
1350 current_end) ||
1351 is_memory_range_within(current_begin, current_end,
1352 begin, end)) {
1353 dlog_verbose(
1354 "Overlapping memory ranges: %#x - %#x "
1355 "with %#x - %#x\n",
1356 ipa_addr(begin), ipa_addr(end),
1357 ipa_addr(current_begin),
1358 ipa_addr(current_end));
1359 return true;
1360 }
1361 }
1362 }
1363
1364 return false;
1365}
1366
Jose Marinho09b1db82019-08-08 09:16:59 +01001367/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001368 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001369 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001370 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001371 *
1372 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001373 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001374 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001375 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001376 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1377 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001378 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001379 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001380 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001381 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001382static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001383 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001384 struct ffa_memory_region_constituent **fragments,
1385 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001386 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001387 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1388 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001389{
Andrew Walbranca808b12020-05-15 17:22:28 +01001390 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001391 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001392 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001393 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001394 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001395 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001396 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001397 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001398 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001399 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001400
1401 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 * Make sure constituents are properly aligned to a 64-bit boundary. If
1403 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001404 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001405 for (i = 0; i < fragment_count; ++i) {
1406 if (!is_aligned(fragments[i], 8)) {
1407 dlog_verbose("Constituents not aligned.\n");
1408 return ffa_error(FFA_INVALID_PARAMETERS);
1409 }
J-Alves8f11cde2022-12-21 16:18:22 +00001410 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1411 constituents_total_page_count +=
1412 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001413 if (ffa_memory_check_overlap(
1414 fragments, fragment_constituent_counts,
1415 fragment_count, i, j)) {
1416 return ffa_error(FFA_INVALID_PARAMETERS);
1417 }
J-Alves8f11cde2022-12-21 16:18:22 +00001418 }
1419 }
1420
1421 if (constituents_total_page_count != composite_total_page_count) {
1422 dlog_verbose(
1423 "Composite page count differs from calculated page "
1424 "count from constituents.\n");
1425 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001426 }
1427
1428 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001429 * Check if the state transition is lawful for the sender, ensure that
1430 * all constituents of a memory region being shared are at the same
1431 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001432 */
J-Alves460d36c2023-10-12 17:02:15 +01001433 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001434 from_locked, share_func, memory_region, &orig_from_mode,
1435 fragments, fragment_constituent_counts, fragment_count,
1436 &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001438 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001440 }
1441
Andrew Walbran37c574e2020-06-03 11:45:46 +01001442 if (orig_from_mode_ret != NULL) {
1443 *orig_from_mode_ret = orig_from_mode;
1444 }
1445
Jose Marinho09b1db82019-08-08 09:16:59 +01001446 /*
1447 * Create a local pool so any freed memory can't be used by another
1448 * thread. This is to ensure the original mapping can be restored if the
1449 * clear fails.
1450 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001451 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001452
1453 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001454 * First reserve all required memory for the new page table entries
1455 * without committing, to make sure the entire operation will succeed
1456 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001457 * Provide the map_action as populated by 'ffa_send_check_transition'.
1458 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001459 */
J-Alvescf6253e2024-01-03 13:48:48 +00001460 ret = ffa_region_group_identity_map(
1461 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001462 fragment_count, from_mode, page_pool, map_action,
1463 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001464 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001465 goto out;
1466 }
1467
1468 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001469 * Update the mapping for the sender. This won't allocate because the
1470 * transaction was already prepared above, but may free pages in the
1471 * case that a whole block is being unmapped that was previously
1472 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001473 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001474 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001475 from_locked, fragments, fragment_constituent_counts,
1476 fragment_count, from_mode, &local_page_pool,
1477 MAP_ACTION_COMMIT, NULL)
1478 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001479
J-Alves460d36c2023-10-12 17:02:15 +01001480 /*
1481 * If memory has been protected, it is now part of the secure PAS
1482 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1483 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1484 * SPM's S1 translation.
1485 * In case memory hasn't been protected, and it is in the non-secure
1486 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1487 * perform a non-secure memory access. In such case `clean_mode` takes
1488 * the same mode as `orig_from_mode`.
1489 */
1490 clean_mode = (memory_protected != NULL && *memory_protected)
1491 ? orig_from_mode & ~plat_ffa_other_world_mode()
1492 : orig_from_mode;
1493
Jose Marinho09b1db82019-08-08 09:16:59 +01001494 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001495 if (clear && !ffa_clear_memory_constituents(
1496 clean_mode, fragments, fragment_constituent_counts,
1497 fragment_count, page_pool)) {
1498 map_action = (memory_protected != NULL && *memory_protected)
1499 ? MAP_ACTION_COMMIT_UNPROTECT
1500 : MAP_ACTION_COMMIT;
1501
Jose Marinho09b1db82019-08-08 09:16:59 +01001502 /*
1503 * On failure, roll back by returning memory to the sender. This
1504 * may allocate pages which were previously freed into
1505 * `local_page_pool` by the call above, but will never allocate
1506 * more pages than that so can never fail.
1507 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001508 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001509 from_locked, fragments,
1510 fragment_constituent_counts, fragment_count,
1511 orig_from_mode, &local_page_pool,
1512 MAP_ACTION_COMMIT, NULL)
1513 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001514 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001515 goto out;
1516 }
1517
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001519
1520out:
1521 mpool_fini(&local_page_pool);
1522
1523 /*
1524 * Tidy up the page table by reclaiming failed mappings (if there was an
1525 * error) or merging entries into blocks where possible (on success).
1526 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001527 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001528
1529 return ret;
1530}
1531
1532/**
1533 * Validates and maps memory shared from one VM to another.
1534 *
1535 * This function requires the calling context to hold the <to> lock.
1536 *
1537 * Returns:
1538 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001540 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001541 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001542 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001543 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001544 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001545struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001546 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001547 struct ffa_memory_region_constituent **fragments,
1548 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001549 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001550 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001551{
Andrew Walbranca808b12020-05-15 17:22:28 +01001552 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001553 uint32_t to_mode;
1554 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001555 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001556 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001557
1558 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001559 * Make sure constituents are properly aligned to a 64-bit boundary. If
1560 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001561 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001562 for (i = 0; i < fragment_count; ++i) {
1563 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001564 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001565 return ffa_error(FFA_INVALID_PARAMETERS);
1566 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001567 }
1568
1569 /*
1570 * Check if the state transition is lawful for the recipient, and ensure
1571 * that all constituents of the memory region being retrieved are at the
1572 * same state.
1573 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001574 ret = ffa_retrieve_check_transition(
1575 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001576 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1577 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001578
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001579 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001580 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001581 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001582 }
1583
1584 /*
1585 * Create a local pool so any freed memory can't be used by another
1586 * thread. This is to ensure the original mapping can be restored if the
1587 * clear fails.
1588 */
1589 mpool_init_with_fallback(&local_page_pool, page_pool);
1590
1591 /*
1592 * First reserve all required memory for the new page table entries in
1593 * the recipient page tables without committing, to make sure the entire
1594 * operation will succeed without exhausting the page pool.
1595 */
J-Alvescf6253e2024-01-03 13:48:48 +00001596 ret = ffa_region_group_identity_map(
1597 to_locked, fragments, fragment_constituent_counts,
1598 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1599 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001600 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 goto out;
1602 }
1603
1604 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001605 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001606 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1607 fragment_constituent_counts,
1608 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001609 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001610 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001611 goto out;
1612 }
1613
Jose Marinho09b1db82019-08-08 09:16:59 +01001614 /*
1615 * Complete the transfer by mapping the memory into the recipient. This
1616 * won't allocate because the transaction was already prepared above, so
1617 * it doesn't need to use the `local_page_pool`.
1618 */
J-Alvesfd206052023-05-22 16:45:00 +01001619 CHECK(ffa_region_group_identity_map(
1620 to_locked, fragments, fragment_constituent_counts,
1621 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001622 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001623
J-Alves460d36c2023-10-12 17:02:15 +01001624 /* Return the mode used in mapping the memory in retriever's PT. */
1625 if (response_mode != NULL) {
1626 *response_mode = to_mode;
1627 }
1628
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001629 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001630
1631out:
1632 mpool_fini(&local_page_pool);
1633
1634 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001635 * Tidy up the page table by reclaiming failed mappings (if there was an
1636 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001637 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001638 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001639
1640 return ret;
1641}
1642
Andrew Walbran996d1d12020-05-27 14:08:43 +01001643static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001644 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 struct ffa_memory_region_constituent **fragments,
1646 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1647 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648{
1649 uint32_t orig_from_mode;
1650 uint32_t from_mode;
1651 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653
Andrew Walbranca808b12020-05-15 17:22:28 +01001654 ret = ffa_relinquish_check_transition(
1655 from_locked, &orig_from_mode, fragments,
1656 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001657 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001658 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001659 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001660 }
1661
1662 /*
1663 * Create a local pool so any freed memory can't be used by another
1664 * thread. This is to ensure the original mapping can be restored if the
1665 * clear fails.
1666 */
1667 mpool_init_with_fallback(&local_page_pool, page_pool);
1668
1669 /*
1670 * First reserve all required memory for the new page table entries
1671 * without committing, to make sure the entire operation will succeed
1672 * without exhausting the page pool.
1673 */
J-Alvescf6253e2024-01-03 13:48:48 +00001674 ret = ffa_region_group_identity_map(
1675 from_locked, fragments, fragment_constituent_counts,
1676 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1677 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001678 goto out;
1679 }
1680
1681 /*
1682 * Update the mapping for the sender. This won't allocate because the
1683 * transaction was already prepared above, but may free pages in the
1684 * case that a whole block is being unmapped that was previously
1685 * partially mapped.
1686 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001687 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001688 from_locked, fragments, fragment_constituent_counts,
1689 fragment_count, from_mode, &local_page_pool,
1690 MAP_ACTION_COMMIT, NULL)
1691 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001692
1693 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001694 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001695 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1696 fragment_constituent_counts,
1697 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001698 /*
1699 * On failure, roll back by returning memory to the sender. This
1700 * may allocate pages which were previously freed into
1701 * `local_page_pool` by the call above, but will never allocate
1702 * more pages than that so can never fail.
1703 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001704 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001705 from_locked, fragments,
1706 fragment_constituent_counts, fragment_count,
1707 orig_from_mode, &local_page_pool,
1708 MAP_ACTION_COMMIT, NULL)
1709 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001710
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001711 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001712 goto out;
1713 }
1714
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001715 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001716
1717out:
1718 mpool_fini(&local_page_pool);
1719
1720 /*
1721 * Tidy up the page table by reclaiming failed mappings (if there was an
1722 * error) or merging entries into blocks where possible (on success).
1723 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001724 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001725
1726 return ret;
1727}
1728
1729/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001730 * Complete a memory sending operation by checking that it is valid, updating
1731 * the sender page table, and then either marking the share state as having
1732 * completed sending (on success) or freeing it (on failure).
1733 *
1734 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1735 */
J-Alvesfdd29272022-07-19 13:16:31 +01001736struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001737 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001738 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1739 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001740{
1741 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001742 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001743 struct ffa_value ret;
1744
1745 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001746 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001747 assert(memory_region != NULL);
1748 composite = ffa_memory_region_get_composite(memory_region, 0);
1749 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001750
1751 /* Check that state is valid in sender page table and update. */
1752 ret = ffa_send_check_update(
1753 from_locked, share_state->fragments,
1754 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001755 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001756 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001757 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001758 if (ret.func != FFA_SUCCESS_32) {
1759 /*
1760 * Free share state, it failed to send so it can't be retrieved.
1761 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001762 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1763 __func__, ffa_func_name(ret.func),
1764 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001765 share_state_free(share_states, share_state, page_pool);
1766 return ret;
1767 }
1768
1769 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001770 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001771
J-Alvesee68c542020-10-29 17:48:20 +00001772 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001773}
1774
1775/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001776 * Check that the memory attributes match Hafnium expectations:
1777 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1778 * Write-Allocate Cacheable.
1779 */
1780static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001781 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001782{
1783 enum ffa_memory_type memory_type;
1784 enum ffa_memory_cacheability cacheability;
1785 enum ffa_memory_shareability shareability;
1786
1787 memory_type = ffa_get_memory_type_attr(attributes);
1788 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1789 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1790 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001791 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001792 }
1793
1794 cacheability = ffa_get_memory_cacheability_attr(attributes);
1795 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1796 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1797 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001798 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001799 }
1800
1801 shareability = ffa_get_memory_shareability_attr(attributes);
1802 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1803 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1804 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001805 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001806 }
1807
1808 return (struct ffa_value){.func = FFA_SUCCESS_32};
1809}
1810
1811/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001812 * Check that the given `memory_region` represents a valid memory send request
1813 * of the given `share_func` type, return the clear flag and permissions via the
1814 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001815 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001816 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001817 * not.
1818 */
J-Alves66652252022-07-06 09:49:51 +01001819struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001820 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1821 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001822 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001823{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001824 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001825 struct ffa_memory_access *receiver =
1826 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001827 uint64_t receivers_end;
1828 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001829 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001830 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001831 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001832 enum ffa_data_access data_access;
1833 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001834 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001835 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001836 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001837 memory_region->receivers_offset +
1838 memory_region->memory_access_desc_size +
1839 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001840
1841 if (fragment_length < minimum_first_fragment_length) {
1842 dlog_verbose("Fragment length %u too short (min %u).\n",
1843 (size_t)fragment_length,
1844 minimum_first_fragment_length);
1845 return ffa_error(FFA_INVALID_PARAMETERS);
1846 }
1847
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001848 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1849 "struct ffa_memory_region_constituent must be 16 bytes");
1850 if (!is_aligned(fragment_length,
1851 sizeof(struct ffa_memory_region_constituent)) ||
1852 !is_aligned(memory_share_length,
1853 sizeof(struct ffa_memory_region_constituent))) {
1854 dlog_verbose(
1855 "Fragment length %u or total length %u"
1856 " is not 16-byte aligned.\n",
1857 fragment_length, memory_share_length);
1858 return ffa_error(FFA_INVALID_PARAMETERS);
1859 }
1860
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001861 if (fragment_length > memory_share_length) {
1862 dlog_verbose(
1863 "Fragment length %u greater than total length %u.\n",
1864 (size_t)fragment_length, (size_t)memory_share_length);
1865 return ffa_error(FFA_INVALID_PARAMETERS);
1866 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001867
J-Alves95df0ef2022-12-07 10:09:48 +00001868 /* The sender must match the caller. */
1869 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1870 vm_id_is_current_world(memory_region->sender)) ||
1871 (vm_id_is_current_world(from_locked.vm->id) &&
1872 memory_region->sender != from_locked.vm->id)) {
1873 dlog_verbose("Invalid memory sender ID.\n");
1874 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001875 }
1876
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001877 if (memory_region->receiver_count <= 0) {
1878 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001879 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001880 }
1881
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001882 /*
1883 * Ensure that the composite header is within the memory bounds and
1884 * doesn't overlap the first part of the message. Cast to uint64_t
1885 * to prevent overflow.
1886 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001887 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001888 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001889 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001890 min_length = receivers_end +
1891 sizeof(struct ffa_composite_memory_region) +
1892 sizeof(struct ffa_memory_region_constituent);
1893 if (min_length > memory_share_length) {
1894 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1895 (size_t)memory_share_length, (size_t)min_length);
1896 return ffa_error(FFA_INVALID_PARAMETERS);
1897 }
1898
1899 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001900 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001901
1902 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001903 * Check that the composite memory region descriptor is after the access
1904 * descriptors, is at least 16-byte aligned, and fits in the first
1905 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001906 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001907 if ((composite_memory_region_offset < receivers_end) ||
1908 (composite_memory_region_offset % 16 != 0) ||
1909 (composite_memory_region_offset >
1910 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1911 dlog_verbose(
1912 "Invalid composite memory region descriptor offset "
1913 "%u.\n",
1914 (size_t)composite_memory_region_offset);
1915 return ffa_error(FFA_INVALID_PARAMETERS);
1916 }
1917
1918 /*
1919 * Compute the start of the constituent regions. Already checked
1920 * to be not more than fragment_length and thus not more than
1921 * memory_share_length.
1922 */
1923 constituents_start = composite_memory_region_offset +
1924 sizeof(struct ffa_composite_memory_region);
1925 constituents_length = memory_share_length - constituents_start;
1926
1927 /*
1928 * Check that the number of constituents is consistent with the length
1929 * of the constituent region.
1930 */
1931 composite = ffa_memory_region_get_composite(memory_region, 0);
1932 if ((constituents_length %
1933 sizeof(struct ffa_memory_region_constituent) !=
1934 0) ||
1935 ((constituents_length /
1936 sizeof(struct ffa_memory_region_constituent)) !=
1937 composite->constituent_count)) {
1938 dlog_verbose("Invalid length %u or composite offset %u.\n",
1939 (size_t)memory_share_length,
1940 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001941 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001942 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001943 if (fragment_length < memory_share_length &&
1944 fragment_length < HF_MAILBOX_SIZE) {
1945 dlog_warning(
1946 "Initial fragment length %d smaller than mailbox "
1947 "size.\n",
1948 fragment_length);
1949 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001950
Andrew Walbrana65a1322020-04-06 19:32:32 +01001951 /*
1952 * Clear is not allowed for memory sharing, as the sender still has
1953 * access to the memory.
1954 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001955 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1956 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001957 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001958 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001959 }
1960
1961 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001962 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001963 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001964 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001965 }
1966
J-Alves363f5722022-04-25 17:37:37 +01001967 /* Check that the permissions are valid, for each specified receiver. */
1968 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001969 struct ffa_memory_region_attributes receiver_permissions;
1970
1971 receiver = ffa_memory_region_get_receiver(memory_region, i);
1972 assert(receiver != NULL);
1973 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001974 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001975 receiver_permissions.permissions;
1976 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001977
1978 if (memory_region->sender == receiver_id) {
1979 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001980 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001981 }
Federico Recanati85090c42021-12-15 13:17:54 +01001982
J-Alves363f5722022-04-25 17:37:37 +01001983 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1984 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001985 struct ffa_memory_access *other_receiver =
1986 ffa_memory_region_get_receiver(memory_region,
1987 j);
1988 assert(other_receiver != NULL);
1989
J-Alves363f5722022-04-25 17:37:37 +01001990 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001991 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001992 dlog_verbose(
1993 "Repeated receiver(%x) in memory send "
1994 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001995 other_receiver->receiver_permissions
1996 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001997 return ffa_error(FFA_INVALID_PARAMETERS);
1998 }
1999 }
2000
2001 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002002 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002003 dlog_verbose(
2004 "All ffa_memory_access should point to the "
2005 "same composite memory region offset.\n");
2006 return ffa_error(FFA_INVALID_PARAMETERS);
2007 }
2008
2009 data_access = ffa_get_data_access_attr(permissions);
2010 instruction_access =
2011 ffa_get_instruction_access_attr(permissions);
2012 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2013 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2014 dlog_verbose(
2015 "Reserved value for receiver permissions "
2016 "%#x.\n",
2017 permissions);
2018 return ffa_error(FFA_INVALID_PARAMETERS);
2019 }
2020 if (instruction_access !=
2021 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2022 dlog_verbose(
2023 "Invalid instruction access permissions %#x "
2024 "for sending memory.\n",
2025 permissions);
2026 return ffa_error(FFA_INVALID_PARAMETERS);
2027 }
2028 if (share_func == FFA_MEM_SHARE_32) {
2029 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2030 dlog_verbose(
2031 "Invalid data access permissions %#x "
2032 "for sharing memory.\n",
2033 permissions);
2034 return ffa_error(FFA_INVALID_PARAMETERS);
2035 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002036 /*
2037 * According to section 10.10.3 of the FF-A v1.1 EAC0
2038 * spec, NX is required for share operations (but must
2039 * not be specified by the sender) so set it in the
2040 * copy that we store, ready to be returned to the
2041 * retriever.
2042 */
2043 if (vm_id_is_current_world(receiver_id)) {
2044 ffa_set_instruction_access_attr(
2045 &permissions,
2046 FFA_INSTRUCTION_ACCESS_NX);
2047 receiver_permissions.permissions = permissions;
2048 }
J-Alves363f5722022-04-25 17:37:37 +01002049 }
2050 if (share_func == FFA_MEM_LEND_32 &&
2051 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2052 dlog_verbose(
2053 "Invalid data access permissions %#x for "
2054 "lending memory.\n",
2055 permissions);
2056 return ffa_error(FFA_INVALID_PARAMETERS);
2057 }
2058
2059 if (share_func == FFA_MEM_DONATE_32 &&
2060 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2061 dlog_verbose(
2062 "Invalid data access permissions %#x for "
2063 "donating memory.\n",
2064 permissions);
2065 return ffa_error(FFA_INVALID_PARAMETERS);
2066 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002067 }
2068
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002069 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
2070 security_state =
2071 ffa_get_memory_security_attr(memory_region->attributes);
2072 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2073 dlog_verbose(
2074 "Invalid security state for memory share operation.\n");
2075 return ffa_error(FFA_INVALID_PARAMETERS);
2076 }
2077
Federico Recanatid937f5e2021-12-20 17:38:23 +01002078 /*
J-Alves807794e2022-06-16 13:42:47 +01002079 * If a memory donate or lend with single borrower, the memory type
2080 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002081 */
J-Alves807794e2022-06-16 13:42:47 +01002082 if (share_func == FFA_MEM_DONATE_32 ||
2083 (share_func == FFA_MEM_LEND_32 &&
2084 memory_region->receiver_count == 1)) {
2085 if (ffa_get_memory_type_attr(memory_region->attributes) !=
2086 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2087 dlog_verbose(
2088 "Memory type shall not be specified by "
2089 "sender.\n");
2090 return ffa_error(FFA_INVALID_PARAMETERS);
2091 }
2092 } else {
2093 /*
2094 * Check that sender's memory attributes match Hafnium
2095 * expectations: Normal Memory, Inner shareable, Write-Back
2096 * Read-Allocate Write-Allocate Cacheable.
2097 */
2098 ret = ffa_memory_attributes_validate(memory_region->attributes);
2099 if (ret.func != FFA_SUCCESS_32) {
2100 return ret;
2101 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002102 }
2103
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002104 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002105}
2106
2107/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002108 * Gets the share state for continuing an operation to donate, lend or share
2109 * memory, and checks that it is a valid request.
2110 *
2111 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2112 * not.
2113 */
J-Alvesfdd29272022-07-19 13:16:31 +01002114struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002115 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002116 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002117 struct mpool *page_pool)
2118{
2119 struct ffa_memory_share_state *share_state;
2120 struct ffa_memory_region *memory_region;
2121
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002122 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002123
2124 /*
2125 * Look up the share state by handle and make sure that the VM ID
2126 * matches.
2127 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002128 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002129 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002130 dlog_verbose(
2131 "Invalid handle %#x for memory send continuation.\n",
2132 handle);
2133 return ffa_error(FFA_INVALID_PARAMETERS);
2134 }
2135 memory_region = share_state->memory_region;
2136
J-Alvesfdd29272022-07-19 13:16:31 +01002137 if (vm_id_is_current_world(from_vm_id) &&
2138 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002139 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2140 return ffa_error(FFA_INVALID_PARAMETERS);
2141 }
2142
2143 if (share_state->sending_complete) {
2144 dlog_verbose(
2145 "Sending of memory handle %#x is already complete.\n",
2146 handle);
2147 return ffa_error(FFA_INVALID_PARAMETERS);
2148 }
2149
2150 if (share_state->fragment_count == MAX_FRAGMENTS) {
2151 /*
2152 * Log a warning as this is a sign that MAX_FRAGMENTS should
2153 * probably be increased.
2154 */
2155 dlog_warning(
2156 "Too many fragments for memory share with handle %#x; "
2157 "only %d supported.\n",
2158 handle, MAX_FRAGMENTS);
2159 /* Free share state, as it's not possible to complete it. */
2160 share_state_free(share_states, share_state, page_pool);
2161 return ffa_error(FFA_NO_MEMORY);
2162 }
2163
2164 *share_state_ret = share_state;
2165
2166 return (struct ffa_value){.func = FFA_SUCCESS_32};
2167}
2168
2169/**
J-Alves95df0ef2022-12-07 10:09:48 +00002170 * Checks if there is at least one receiver from the other world.
2171 */
J-Alvesfdd29272022-07-19 13:16:31 +01002172bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002173 struct ffa_memory_region *memory_region)
2174{
2175 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002176 struct ffa_memory_access *receiver =
2177 ffa_memory_region_get_receiver(memory_region, i);
2178 assert(receiver != NULL);
2179 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2180
2181 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002182 return true;
2183 }
2184 }
2185 return false;
2186}
2187
2188/**
J-Alves9da280b2022-12-21 14:55:39 +00002189 * Validates a call to donate, lend or share memory in which Hafnium is the
2190 * designated allocator of the memory handle. In practice, this also means
2191 * Hafnium is responsible for managing the state structures for the transaction.
2192 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2193 * sender is an SP or there is at least one borrower that is an SP.
2194 * If Hafnium is the hypervisor, it should allocate the memory handle when
2195 * operation involves only NWd VMs.
2196 *
2197 * If validation goes well, Hafnium updates the stage-2 page tables of the
2198 * sender. Validation consists of checking if the message length and number of
2199 * memory region constituents match, and if the transition is valid for the
2200 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002201 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002202 * Assumes that the caller has already found and locked the sender VM and copied
2203 * the memory region descriptor from the sender's TX buffer to a freshly
2204 * allocated page from Hafnium's internal pool. The caller must have also
2205 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002206 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002207 * This function takes ownership of the `memory_region` passed in and will free
2208 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002209 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002210struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002211 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002212 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002213 uint32_t fragment_length, uint32_t share_func,
2214 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002215{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002216 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002217 struct share_states_locked share_states;
2218 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002219
2220 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002221 * If there is an error validating the `memory_region` then we need to
2222 * free it because we own it but we won't be storing it in a share state
2223 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002224 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002225 ret = ffa_memory_send_validate(from_locked, memory_region,
2226 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002227 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002228 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002229 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002230 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002231 }
2232
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 /* Set flag for share function, ready to be retrieved later. */
2234 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002235 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002237 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002238 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 case FFA_MEM_LEND_32:
2240 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002241 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002242 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002243 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002245 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002246 }
2247
Andrew Walbranca808b12020-05-15 17:22:28 +01002248 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002249 /*
2250 * Allocate a share state before updating the page table. Otherwise if
2251 * updating the page table succeeded but allocating the share state
2252 * failed then it would leave the memory in a state where nobody could
2253 * get it back.
2254 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002255 share_state = allocate_share_state(share_states, share_func,
2256 memory_region, fragment_length,
2257 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002258 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002259 dlog_verbose("Failed to allocate share state.\n");
2260 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002261 ret = ffa_error(FFA_NO_MEMORY);
2262 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002263 }
2264
Andrew Walbranca808b12020-05-15 17:22:28 +01002265 if (fragment_length == memory_share_length) {
2266 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002267 ret = ffa_memory_send_complete(
2268 from_locked, share_states, share_state, page_pool,
2269 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002270 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002271 /*
2272 * Use sender ID from 'memory_region' assuming
2273 * that at this point it has been validated:
2274 * - MBZ at virtual FF-A instance.
2275 */
J-Alves19e20cf2023-08-02 12:48:55 +01002276 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002277 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2278 ? memory_region->sender
2279 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002280 ret = (struct ffa_value){
2281 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002282 .arg1 = (uint32_t)memory_region->handle,
2283 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002284 .arg3 = fragment_length,
2285 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002286 }
2287
2288out:
2289 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002290 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002291 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002292}
2293
2294/**
J-Alves8505a8a2022-06-15 18:10:18 +01002295 * Continues an operation to donate, lend or share memory to a VM from current
2296 * world. If this is the last fragment then checks that the transition is valid
2297 * for the type of memory sending operation and updates the stage-2 page tables
2298 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002299 *
2300 * Assumes that the caller has already found and locked the sender VM and copied
2301 * the memory region descriptor from the sender's TX buffer to a freshly
2302 * allocated page from Hafnium's internal pool.
2303 *
2304 * This function takes ownership of the `fragment` passed in; it must not be
2305 * freed by the caller.
2306 */
2307struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2308 void *fragment,
2309 uint32_t fragment_length,
2310 ffa_memory_handle_t handle,
2311 struct mpool *page_pool)
2312{
2313 struct share_states_locked share_states = share_states_lock();
2314 struct ffa_memory_share_state *share_state;
2315 struct ffa_value ret;
2316 struct ffa_memory_region *memory_region;
2317
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002318 CHECK(is_aligned(fragment,
2319 alignof(struct ffa_memory_region_constituent)));
2320 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2321 0) {
2322 dlog_verbose("Fragment length %u misaligned.\n",
2323 fragment_length);
2324 ret = ffa_error(FFA_INVALID_PARAMETERS);
2325 goto out_free_fragment;
2326 }
2327
Andrew Walbranca808b12020-05-15 17:22:28 +01002328 ret = ffa_memory_send_continue_validate(share_states, handle,
2329 &share_state,
2330 from_locked.vm->id, page_pool);
2331 if (ret.func != FFA_SUCCESS_32) {
2332 goto out_free_fragment;
2333 }
2334 memory_region = share_state->memory_region;
2335
J-Alves95df0ef2022-12-07 10:09:48 +00002336 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002337 dlog_error(
2338 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002339 "other world. This should never happen, and indicates "
2340 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002341 "EL3 code.\n");
2342 ret = ffa_error(FFA_INVALID_PARAMETERS);
2343 goto out_free_fragment;
2344 }
2345
2346 /* Add this fragment. */
2347 share_state->fragments[share_state->fragment_count] = fragment;
2348 share_state->fragment_constituent_counts[share_state->fragment_count] =
2349 fragment_length / sizeof(struct ffa_memory_region_constituent);
2350 share_state->fragment_count++;
2351
2352 /* Check whether the memory send operation is now ready to complete. */
2353 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002354 ret = ffa_memory_send_complete(
2355 from_locked, share_states, share_state, page_pool,
2356 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002357 } else {
2358 ret = (struct ffa_value){
2359 .func = FFA_MEM_FRAG_RX_32,
2360 .arg1 = (uint32_t)handle,
2361 .arg2 = (uint32_t)(handle >> 32),
2362 .arg3 = share_state_next_fragment_offset(share_states,
2363 share_state)};
2364 }
2365 goto out;
2366
2367out_free_fragment:
2368 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002369
2370out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002371 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002372 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002373}
2374
Andrew Walbranca808b12020-05-15 17:22:28 +01002375/** Clean up after the receiver has finished retrieving a memory region. */
2376static void ffa_memory_retrieve_complete(
2377 struct share_states_locked share_states,
2378 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2379{
2380 if (share_state->share_func == FFA_MEM_DONATE_32) {
2381 /*
2382 * Memory that has been donated can't be relinquished,
2383 * so no need to keep the share state around.
2384 */
2385 share_state_free(share_states, share_state, page_pool);
2386 dlog_verbose("Freed share state for donate.\n");
2387 }
2388}
2389
J-Alves2d8457f2022-10-05 11:06:41 +01002390/**
2391 * Initialises the given memory region descriptor to be used for an
2392 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2393 * fragment.
2394 * The memory region descriptor is initialized according to retriever's
2395 * FF-A version.
2396 *
2397 * Returns true on success, or false if the given constituents won't all fit in
2398 * the first fragment.
2399 */
2400static bool ffa_retrieved_memory_region_init(
2401 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002402 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002403 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002404 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002405 struct ffa_memory_access *receivers, size_t receiver_count,
2406 uint32_t memory_access_desc_size, uint32_t page_count,
2407 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002408 const struct ffa_memory_region_constituent constituents[],
2409 uint32_t fragment_constituent_count, uint32_t *total_length,
2410 uint32_t *fragment_length)
2411{
2412 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002413 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002414 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002415 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002416
2417 assert(response != NULL);
2418
2419 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2420 struct ffa_memory_region_v1_0 *retrieve_response =
2421 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002422 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002423
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002424 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2425 attributes, flags, handle, 0,
2426 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002427
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002428 receiver = (struct ffa_memory_access_v1_0 *)
2429 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002430 receiver_count = retrieve_response->receiver_count;
2431
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002432 for (uint32_t i = 0; i < receiver_count; i++) {
2433 ffa_id_t receiver_id =
2434 receivers[i].receiver_permissions.receiver;
2435 ffa_memory_receiver_flags_t recv_flags =
2436 receivers[i].receiver_permissions.flags;
2437
2438 /*
2439 * Initialized here as in memory retrieve responses we
2440 * currently expect one borrower to be specified.
2441 */
2442 ffa_memory_access_init_v1_0(
2443 receiver, receiver_id,
2444 ffa_get_data_access_attr(permissions),
2445 ffa_get_instruction_access_attr(permissions),
2446 recv_flags);
2447 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002448
2449 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002450 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002451 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2452 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002453
2454 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2455 retrieve_response, 0);
2456 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002457 struct ffa_memory_region *retrieve_response =
2458 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002459 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002460
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002461 ffa_memory_region_init_header(
2462 retrieve_response, sender, attributes, flags, handle, 0,
2463 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002464
2465 /*
2466 * Note that `sizeof(struct_ffa_memory_region)` and
2467 * `sizeof(struct ffa_memory_access)` must both be multiples of
2468 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2469 * guaranteed that the offset we calculate here is aligned to a
2470 * 64-bit boundary and so 64-bit values can be copied without
2471 * alignment faults.
2472 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002473 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002474 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002475 (uint32_t)(receiver_count *
2476 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002477
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002478 retrieve_response_receivers =
2479 ffa_memory_region_get_receiver(retrieve_response, 0);
2480 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002481
2482 /*
2483 * Initialized here as in memory retrieve responses we currently
2484 * expect one borrower to be specified.
2485 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002486 memcpy_s(retrieve_response_receivers,
2487 sizeof(struct ffa_memory_access) * receiver_count,
2488 receivers,
2489 sizeof(struct ffa_memory_access) * receiver_count);
2490
2491 retrieve_response_receivers->composite_memory_region_offset =
2492 composite_offset;
2493
J-Alves2d8457f2022-10-05 11:06:41 +01002494 composite_memory_region =
2495 ffa_memory_region_get_composite(retrieve_response, 0);
2496 }
2497
J-Alves2d8457f2022-10-05 11:06:41 +01002498 assert(composite_memory_region != NULL);
2499
J-Alves2d8457f2022-10-05 11:06:41 +01002500 composite_memory_region->page_count = page_count;
2501 composite_memory_region->constituent_count = total_constituent_count;
2502 composite_memory_region->reserved_0 = 0;
2503
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002504 constituents_offset =
2505 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002506 if (constituents_offset +
2507 fragment_constituent_count *
2508 sizeof(struct ffa_memory_region_constituent) >
2509 response_max_size) {
2510 return false;
2511 }
2512
2513 for (i = 0; i < fragment_constituent_count; ++i) {
2514 composite_memory_region->constituents[i] = constituents[i];
2515 }
2516
2517 if (total_length != NULL) {
2518 *total_length =
2519 constituents_offset +
2520 composite_memory_region->constituent_count *
2521 sizeof(struct ffa_memory_region_constituent);
2522 }
2523 if (fragment_length != NULL) {
2524 *fragment_length =
2525 constituents_offset +
2526 fragment_constituent_count *
2527 sizeof(struct ffa_memory_region_constituent);
2528 }
2529
2530 return true;
2531}
2532
J-Alves96de29f2022-04-26 16:05:24 +01002533/**
2534 * Validates the retrieved permissions against those specified by the lender
2535 * of memory share operation. Optionally can help set the permissions to be used
2536 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002537 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2538 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2539 * specification for each ABI.
2540 * - FFA_DENIED -> if the permissions specified by the retriever are not
2541 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002542 */
J-Alvesdcad8992023-09-15 14:10:35 +01002543static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2544 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002545 enum ffa_data_access requested_data_access,
2546 enum ffa_instruction_access sent_instruction_access,
2547 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002548 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002549{
2550 switch (sent_data_access) {
2551 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2552 case FFA_DATA_ACCESS_RW:
2553 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2554 requested_data_access == FFA_DATA_ACCESS_RW) {
2555 if (permissions != NULL) {
2556 ffa_set_data_access_attr(permissions,
2557 FFA_DATA_ACCESS_RW);
2558 }
2559 break;
2560 }
2561 /* Intentional fall-through. */
2562 case FFA_DATA_ACCESS_RO:
2563 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2564 requested_data_access == FFA_DATA_ACCESS_RO) {
2565 if (permissions != NULL) {
2566 ffa_set_data_access_attr(permissions,
2567 FFA_DATA_ACCESS_RO);
2568 }
2569 break;
2570 }
2571 dlog_verbose(
2572 "Invalid data access requested; sender specified "
2573 "permissions %#x but receiver requested %#x.\n",
2574 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002575 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002576 case FFA_DATA_ACCESS_RESERVED:
2577 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2578 "checked before this point.");
2579 }
2580
J-Alvesdcad8992023-09-15 14:10:35 +01002581 /*
2582 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2583 * or FFA_MEMORY_DONATE the retriever should have specifed the
2584 * instruction permissions it wishes to receive.
2585 */
2586 switch (share_func) {
2587 case FFA_MEM_SHARE_32:
2588 if (requested_instruction_access !=
2589 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2590 dlog_verbose(
2591 "%s: for share instruction permissions must "
2592 "NOT be specified.\n",
2593 __func__);
2594 return ffa_error(FFA_INVALID_PARAMETERS);
2595 }
2596 break;
2597 case FFA_MEM_LEND_32:
2598 /*
2599 * For operations with multiple borrowers only permit XN
2600 * permissions, and both Sender and borrower should have used
2601 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2602 */
2603 if (multiple_borrowers) {
2604 if (requested_instruction_access !=
2605 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2606 dlog_verbose(
2607 "%s: lend/share/donate with multiple "
2608 "borrowers "
2609 "instruction permissions must NOT be "
2610 "specified.\n",
2611 __func__);
2612 return ffa_error(FFA_INVALID_PARAMETERS);
2613 }
2614 break;
2615 }
2616 /* Fall through if the operation targets a single borrower. */
2617 case FFA_MEM_DONATE_32:
2618 if (!multiple_borrowers &&
2619 requested_instruction_access ==
2620 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2621 dlog_verbose(
2622 "%s: for lend/donate with single borrower "
2623 "instruction permissions must be speficified "
2624 "by borrower\n",
2625 __func__);
2626 return ffa_error(FFA_INVALID_PARAMETERS);
2627 }
2628 break;
2629 default:
2630 panic("%s: Wrong func id provided.\n", __func__);
2631 }
2632
J-Alves96de29f2022-04-26 16:05:24 +01002633 switch (sent_instruction_access) {
2634 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2635 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002636 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002637 if (permissions != NULL) {
2638 ffa_set_instruction_access_attr(
2639 permissions, FFA_INSTRUCTION_ACCESS_X);
2640 }
2641 break;
2642 }
J-Alvesdcad8992023-09-15 14:10:35 +01002643 /*
2644 * Fall through if requested permissions are less
2645 * permissive than those provided by the sender.
2646 */
J-Alves96de29f2022-04-26 16:05:24 +01002647 case FFA_INSTRUCTION_ACCESS_NX:
2648 if (requested_instruction_access ==
2649 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2650 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2651 if (permissions != NULL) {
2652 ffa_set_instruction_access_attr(
2653 permissions, FFA_INSTRUCTION_ACCESS_NX);
2654 }
2655 break;
2656 }
2657 dlog_verbose(
2658 "Invalid instruction access requested; sender "
2659 "specified permissions %#x but receiver requested "
2660 "%#x.\n",
2661 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002662 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002663 case FFA_INSTRUCTION_ACCESS_RESERVED:
2664 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2665 "be checked before this point.");
2666 }
2667
J-Alvesdcad8992023-09-15 14:10:35 +01002668 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002669}
2670
2671/**
2672 * Validate the receivers' permissions in the retrieve request against those
2673 * specified by the lender.
2674 * In the `permissions` argument returns the permissions to set at S2 for the
2675 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002676 * The function looks into the flag to bypass multiple borrower checks:
2677 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2678 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2679 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2680 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002681 */
2682static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2683 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002684 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002685 ffa_memory_access_permissions_t *permissions,
2686 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002687{
2688 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002689 bool bypass_multi_receiver_check =
2690 (retrieve_request->flags &
2691 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002692 const uint32_t region_receiver_count = memory_region->receiver_count;
2693 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002694
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002695 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002696 assert(permissions != NULL);
2697
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002698 *permissions = 0;
2699
J-Alves3456e032023-07-20 12:20:05 +01002700 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002701 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002702 dlog_verbose(
2703 "Retrieve request should contain same list of "
2704 "borrowers, as specified by the lender.\n");
2705 return ffa_error(FFA_INVALID_PARAMETERS);
2706 }
2707 } else {
2708 if (retrieve_request->receiver_count != 1) {
2709 dlog_verbose(
2710 "Set bypass multiple borrower check, receiver "
2711 "list must be sized 1 (%x)\n",
2712 memory_region->receiver_count);
2713 return ffa_error(FFA_INVALID_PARAMETERS);
2714 }
J-Alves96de29f2022-04-26 16:05:24 +01002715 }
2716
2717 retrieve_receiver_index = retrieve_request->receiver_count;
2718
J-Alves96de29f2022-04-26 16:05:24 +01002719 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2720 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002721 struct ffa_memory_access *retrieve_request_receiver =
2722 ffa_memory_region_get_receiver(retrieve_request, i);
2723 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002724 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002725 retrieve_request_receiver->receiver_permissions
2726 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002727 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002728 retrieve_request_receiver->receiver_permissions
2729 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002730 struct ffa_memory_access *receiver;
2731 uint32_t mem_region_receiver_index;
2732 bool permissions_RO;
2733 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002734 bool found_to_id = current_receiver_id == to_vm_id;
2735
J-Alves3456e032023-07-20 12:20:05 +01002736 if (bypass_multi_receiver_check && !found_to_id) {
2737 dlog_verbose(
2738 "Bypass multiple borrower check for id %x.\n",
2739 current_receiver_id);
2740 continue;
2741 }
2742
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002743 if (retrieve_request_receiver->composite_memory_region_offset !=
2744 0U) {
2745 dlog_verbose(
2746 "Retriever specified address ranges not "
2747 "supported (got offset %d).\n",
2748 retrieve_request_receiver
2749 ->composite_memory_region_offset);
2750 return ffa_error(FFA_INVALID_PARAMETERS);
2751 }
2752
J-Alves96de29f2022-04-26 16:05:24 +01002753 /*
2754 * Find the current receiver in the transaction descriptor from
2755 * sender.
2756 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002757 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002758 ffa_memory_region_get_receiver_index(
2759 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002760
2761 if (mem_region_receiver_index ==
2762 memory_region->receiver_count) {
2763 dlog_verbose("%s: receiver %x not found\n", __func__,
2764 current_receiver_id);
2765 return ffa_error(FFA_DENIED);
2766 }
2767
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002768 receiver = ffa_memory_region_get_receiver(
2769 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002770 assert(receiver != NULL);
2771
2772 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002773
2774 if (found_to_id) {
2775 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002776
2777 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002778 }
2779
2780 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002781 * Check if retrieve request memory access list is valid:
2782 * - The retrieve request complies with the specification.
2783 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002784 */
J-Alvesdcad8992023-09-15 14:10:35 +01002785 ret = ffa_memory_retrieve_is_memory_access_valid(
2786 func_id, ffa_get_data_access_attr(sent_permissions),
2787 ffa_get_data_access_attr(requested_permissions),
2788 ffa_get_instruction_access_attr(sent_permissions),
2789 ffa_get_instruction_access_attr(requested_permissions),
2790 found_to_id ? permissions : NULL,
2791 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002792
J-Alvesdcad8992023-09-15 14:10:35 +01002793 if (ret.func != FFA_SUCCESS_32) {
2794 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002795 }
2796
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002797 permissions_RO = (ffa_get_data_access_attr(*permissions) ==
2798 FFA_DATA_ACCESS_RO);
2799 clear_memory_flags = (retrieve_request->flags &
2800 FFA_MEMORY_REGION_FLAG_CLEAR) != 0U;
2801
J-Alves96de29f2022-04-26 16:05:24 +01002802 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002803 * Can't request PM to clear memory if only provided
2804 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002805 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002806 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002807 dlog_verbose(
2808 "Receiver has RO permissions can not request "
2809 "clear.\n");
2810 return ffa_error(FFA_DENIED);
2811 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002812
2813 /*
2814 * Check the impdef in the retrieve_request matches the value in
2815 * the original memory send.
2816 */
2817 if (ffa_version_from_memory_access_desc_size(
2818 memory_region->memory_access_desc_size) >=
2819 MAKE_FFA_VERSION(1, 2) &&
2820 ffa_version_from_memory_access_desc_size(
2821 retrieve_request->memory_access_desc_size) >=
2822 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002823 if (receiver->impdef.val[0] !=
2824 retrieve_request_receiver->impdef.val[0] ||
2825 receiver->impdef.val[1] !=
2826 retrieve_request_receiver->impdef.val[1]) {
2827 dlog_verbose(
2828 "Impdef value in memory send does not "
2829 "match retrieve request value "
2830 "send value %#x %#x retrieve request "
2831 "value %#x %#x\n",
2832 receiver->impdef.val[0],
2833 receiver->impdef.val[1],
2834 retrieve_request_receiver->impdef
2835 .val[0],
2836 retrieve_request_receiver->impdef
2837 .val[1]);
2838 return ffa_error(FFA_INVALID_PARAMETERS);
2839 }
2840 }
J-Alves96de29f2022-04-26 16:05:24 +01002841 }
2842
2843 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2844 dlog_verbose(
2845 "Retrieve request does not contain caller's (%x) "
2846 "permissions\n",
2847 to_vm_id);
2848 return ffa_error(FFA_INVALID_PARAMETERS);
2849 }
2850
2851 return (struct ffa_value){.func = FFA_SUCCESS_32};
2852}
2853
J-Alvesa9cd7e32022-07-01 13:49:33 +01002854/*
2855 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2856 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2857 * of a pending memory sharing operation whose allocator is the SPM, for
2858 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2859 * the memory region descriptor of the retrieve request must be zeroed with the
2860 * exception of the sender ID and handle.
2861 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002862bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2863 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002864{
2865 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2866 request->attributes == 0U && request->flags == 0U &&
2867 request->tag == 0U && request->receiver_count == 0U &&
2868 plat_ffa_memory_handle_allocated_by_current_world(
2869 request->handle);
2870}
2871
2872/*
2873 * Helper to reset count of fragments retrieved by the hypervisor.
2874 */
2875static void ffa_memory_retrieve_complete_from_hyp(
2876 struct ffa_memory_share_state *share_state)
2877{
2878 if (share_state->hypervisor_fragment_count ==
2879 share_state->fragment_count) {
2880 share_state->hypervisor_fragment_count = 0;
2881 }
2882}
2883
J-Alves089004f2022-07-13 14:25:44 +01002884/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002885 * Prepares the return of the ffa_value for the memory retrieve response.
2886 */
2887static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2888 uint32_t fragment_length)
2889{
2890 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2891 .arg1 = total_length,
2892 .arg2 = fragment_length};
2893}
2894
2895/**
J-Alves089004f2022-07-13 14:25:44 +01002896 * Validate that the memory region descriptor provided by the borrower on
2897 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2898 * memory sharing call.
2899 */
2900static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002901 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2902 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002903 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2904 uint32_t share_func)
2905{
2906 ffa_memory_region_flags_t transaction_type =
2907 retrieve_request->flags &
2908 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002909 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002910 const uint64_t memory_access_desc_size =
2911 retrieve_request->memory_access_desc_size;
2912 const uint32_t expected_retrieve_request_length =
2913 retrieve_request->receivers_offset +
2914 (uint32_t)(retrieve_request->receiver_count *
2915 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002916
2917 assert(retrieve_request != NULL);
2918 assert(memory_region != NULL);
2919 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01002920
J-Alves4f0d9c12024-01-17 17:23:11 +00002921 if (retrieve_request_length != expected_retrieve_request_length) {
2922 dlog_verbose(
2923 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2924 "but was %d.\n",
2925 expected_retrieve_request_length,
2926 retrieve_request_length);
2927 return ffa_error(FFA_INVALID_PARAMETERS);
2928 }
2929
2930 if (retrieve_request->sender != memory_region->sender) {
2931 dlog_verbose(
2932 "Memory with handle %#x not fully sent, can't "
2933 "retrieve.\n",
2934 memory_region->handle);
2935 return ffa_error(FFA_DENIED);
2936 }
2937
2938 /*
2939 * The SPMC can only process retrieve requests to memory share
2940 * operations with one borrower from the other world. It can't
2941 * determine the ID of the NWd VM that invoked the retrieve
2942 * request interface call. It relies on the hypervisor to
2943 * validate the caller's ID against that provided in the
2944 * `receivers` list of the retrieve response.
2945 * In case there is only one borrower from the NWd in the
2946 * transaction descriptor, record that in the `receiver_id` for
2947 * later use, and validate in the retrieve request message.
2948 * This limitation is due to the fact SPMC can't determine the
2949 * index in the memory share structures state to update.
2950 */
2951 if (to_id == HF_HYPERVISOR_VM_ID) {
2952 uint32_t other_world_count = 0;
2953
2954 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2955 struct ffa_memory_access *receiver =
2956 ffa_memory_region_get_receiver(retrieve_request,
2957 0);
2958 assert(receiver != NULL);
2959
2960 to_id = receiver->receiver_permissions.receiver;
2961
2962 if (!vm_id_is_current_world(to_id)) {
2963 other_world_count++;
2964 }
2965 }
2966
2967 if (other_world_count > 1) {
2968 dlog_verbose(
2969 "Support one receiver from the other "
2970 "world.\n");
2971 return ffa_error(FFA_NOT_SUPPORTED);
2972 }
2973 }
J-Alves089004f2022-07-13 14:25:44 +01002974 /*
2975 * Check that the transaction type expected by the receiver is
2976 * correct, if it has been specified.
2977 */
2978 if (transaction_type !=
2979 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2980 transaction_type != (memory_region->flags &
2981 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2982 dlog_verbose(
2983 "Incorrect transaction type %#x for "
2984 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2985 transaction_type,
2986 memory_region->flags &
2987 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2988 retrieve_request->handle);
2989 return ffa_error(FFA_INVALID_PARAMETERS);
2990 }
2991
2992 if (retrieve_request->tag != memory_region->tag) {
2993 dlog_verbose(
2994 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2995 "%d for handle %#x.\n",
2996 retrieve_request->tag, memory_region->tag,
2997 retrieve_request->handle);
2998 return ffa_error(FFA_INVALID_PARAMETERS);
2999 }
3000
J-Alves4f0d9c12024-01-17 17:23:11 +00003001 *receiver_index =
3002 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003003
3004 if (*receiver_index == memory_region->receiver_count) {
3005 dlog_verbose(
3006 "Incorrect receiver VM ID %d for "
3007 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003008 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003009 return ffa_error(FFA_INVALID_PARAMETERS);
3010 }
3011
3012 if ((retrieve_request->flags &
3013 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3014 dlog_verbose(
3015 "Retriever specified 'address range alignment 'hint' "
3016 "not supported.\n");
3017 return ffa_error(FFA_INVALID_PARAMETERS);
3018 }
3019 if ((retrieve_request->flags &
3020 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3021 dlog_verbose(
3022 "Bits 8-5 must be zero in memory region's flags "
3023 "(address range alignment hint not supported).\n");
3024 return ffa_error(FFA_INVALID_PARAMETERS);
3025 }
3026
3027 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3028 dlog_verbose(
3029 "Bits 31-10 must be zero in memory region's flags.\n");
3030 return ffa_error(FFA_INVALID_PARAMETERS);
3031 }
3032
3033 if (share_func == FFA_MEM_SHARE_32 &&
3034 (retrieve_request->flags &
3035 (FFA_MEMORY_REGION_FLAG_CLEAR |
3036 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3037 dlog_verbose(
3038 "Memory Share operation can't clean after relinquish "
3039 "memory region.\n");
3040 return ffa_error(FFA_INVALID_PARAMETERS);
3041 }
3042
3043 /*
3044 * If the borrower needs the memory to be cleared before mapping
3045 * to its address space, the sender should have set the flag
3046 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3047 * FFA_DENIED.
3048 */
3049 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3050 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3051 dlog_verbose(
3052 "Borrower needs memory cleared. Sender needs to set "
3053 "flag for clearing memory.\n");
3054 return ffa_error(FFA_DENIED);
3055 }
3056
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003057 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
3058 security_state =
3059 ffa_get_memory_security_attr(retrieve_request->attributes);
3060 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3061 dlog_verbose(
3062 "Invalid security state for memory retrieve request "
3063 "operation.\n");
3064 return ffa_error(FFA_INVALID_PARAMETERS);
3065 }
3066
J-Alves089004f2022-07-13 14:25:44 +01003067 /*
3068 * If memory type is not specified, bypass validation of memory
3069 * attributes in the retrieve request. The retriever is expecting to
3070 * obtain this information from the SPMC.
3071 */
3072 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
3073 FFA_MEMORY_NOT_SPECIFIED_MEM) {
3074 return (struct ffa_value){.func = FFA_SUCCESS_32};
3075 }
3076
3077 /*
3078 * Ensure receiver's attributes are compatible with how
3079 * Hafnium maps memory: Normal Memory, Inner shareable,
3080 * Write-Back Read-Allocate Write-Allocate Cacheable.
3081 */
3082 return ffa_memory_attributes_validate(retrieve_request->attributes);
3083}
3084
J-Alves4f0d9c12024-01-17 17:23:11 +00003085static struct ffa_value ffa_partition_retrieve_request(
3086 struct share_states_locked share_states,
3087 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3088 struct ffa_memory_region *retrieve_request,
3089 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003090{
J-Alvesa9cd7e32022-07-01 13:49:33 +01003091 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003092 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003093 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003094 struct ffa_composite_memory_region *composite;
3095 uint32_t total_length;
3096 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003097 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003098 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003099 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003100 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003101 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003102 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003103 ffa_memory_handle_t handle = retrieve_request->handle;
J-Alves460d36c2023-10-12 17:02:15 +01003104 ffa_memory_attributes_t attributes = 0;
3105 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003106 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003107
J-Alves96de29f2022-04-26 16:05:24 +01003108 if (!share_state->sending_complete) {
3109 dlog_verbose(
3110 "Memory with handle %#x not fully sent, can't "
3111 "retrieve.\n",
3112 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003113 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003114 }
3115
J-Alves4f0d9c12024-01-17 17:23:11 +00003116 /*
3117 * Validate retrieve request, according to what was sent by the
3118 * sender. Function will output the `receiver_index` from the
3119 * provided memory region.
3120 */
3121 ret = ffa_memory_retrieve_validate(
3122 receiver_id, retrieve_request, retrieve_request_length,
3123 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003124
J-Alves4f0d9c12024-01-17 17:23:11 +00003125 if (ret.func != FFA_SUCCESS_32) {
3126 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003127 }
J-Alves96de29f2022-04-26 16:05:24 +01003128
J-Alves4f0d9c12024-01-17 17:23:11 +00003129 /*
3130 * Validate the requested permissions against the sent
3131 * permissions.
3132 * Outputs the permissions to give to retriever at S2
3133 * PTs.
3134 */
3135 ret = ffa_memory_retrieve_validate_memory_access_list(
3136 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003137 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003138 if (ret.func != FFA_SUCCESS_32) {
3139 return ret;
3140 }
3141
3142 memory_to_mode = ffa_memory_permissions_to_mode(
3143 permissions, share_state->sender_orig_mode);
3144
3145 ret = ffa_retrieve_check_update(
3146 to_locked, share_state->fragments,
3147 share_state->fragment_constituent_counts,
3148 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003149 share_state->share_func, false, page_pool, &retrieve_mode,
3150 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003151
3152 if (ret.func != FFA_SUCCESS_32) {
3153 return ret;
3154 }
3155
3156 share_state->retrieved_fragment_count[receiver_index] = 1;
3157
3158 is_retrieve_complete =
3159 share_state->retrieved_fragment_count[receiver_index] ==
3160 share_state->fragment_count;
3161
J-Alvesb5084cf2022-07-06 14:20:12 +01003162 /* VMs acquire the RX buffer from SPMC. */
3163 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3164
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003165 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003166 * Copy response to RX buffer of caller and deliver the message.
3167 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003168 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003169 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003170
Andrew Walbranca808b12020-05-15 17:22:28 +01003171 /*
J-Alves460d36c2023-10-12 17:02:15 +01003172 * Set the security state in the memory retrieve response attributes
3173 * if specified by the target mode.
3174 */
3175 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3176 retrieve_mode);
3177
3178 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003179 * Constituents which we received in the first fragment should
3180 * always fit in the first fragment we are sending, because the
3181 * header is the same size in both cases and we have a fixed
3182 * message buffer size. So `ffa_retrieved_memory_region_init`
3183 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003184 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003185
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003186 /* Provide the permissions that had been provided. */
3187 receiver->receiver_permissions.permissions = permissions;
3188
3189 /*
3190 * Prepare the memory region descriptor for the retrieve response.
3191 * Provide the pointer to the receiver tracked in the share state
3192 * strucutures.
3193 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003194 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003195 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003196 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003197 memory_region->flags, handle, permissions, receiver, 1,
3198 memory_access_desc_size, composite->page_count,
3199 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003200 share_state->fragment_constituent_counts[0], &total_length,
3201 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003202
J-Alves4f0d9c12024-01-17 17:23:11 +00003203 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003204 ffa_memory_retrieve_complete(share_states, share_state,
3205 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003206 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003207
3208 return ffa_memory_retrieve_resp(total_length, fragment_length);
3209}
3210
3211static struct ffa_value ffa_hypervisor_retrieve_request(
3212 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3213 struct ffa_memory_region *retrieve_request)
3214{
3215 struct ffa_value ret;
3216 struct ffa_composite_memory_region *composite;
3217 uint32_t total_length;
3218 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003219 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003220 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003221 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003222 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003223 ffa_memory_handle_t handle = retrieve_request->handle;
3224
J-Alves4f0d9c12024-01-17 17:23:11 +00003225 memory_region = share_state->memory_region;
3226
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003227 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3228
J-Alves7b6ab612024-01-24 09:54:54 +00003229 switch (to_locked.vm->ffa_version) {
3230 case MAKE_FFA_VERSION(1, 2):
3231 memory_access_desc_size = sizeof(struct ffa_memory_access);
3232 break;
3233 case MAKE_FFA_VERSION(1, 0):
3234 case MAKE_FFA_VERSION(1, 1):
3235 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3236 break;
3237 default:
3238 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3239 }
3240
J-Alves4f0d9c12024-01-17 17:23:11 +00003241 if (share_state->hypervisor_fragment_count != 0U) {
3242 dlog_verbose(
3243 "Memory with handle %#x already retrieved by "
3244 "the hypervisor.\n",
3245 handle);
3246 return ffa_error(FFA_DENIED);
3247 }
3248
3249 share_state->hypervisor_fragment_count = 1;
3250
3251 ffa_memory_retrieve_complete_from_hyp(share_state);
3252
3253 /* VMs acquire the RX buffer from SPMC. */
3254 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3255
3256 /*
3257 * Copy response to RX buffer of caller and deliver the message.
3258 * This must be done before the share_state is (possibly) freed.
3259 */
3260 composite = ffa_memory_region_get_composite(memory_region, 0);
3261
3262 /*
3263 * Constituents which we received in the first fragment should
3264 * always fit in the first fragment we are sending, because the
3265 * header is the same size in both cases and we have a fixed
3266 * message buffer size. So `ffa_retrieved_memory_region_init`
3267 * should never fail.
3268 */
3269
3270 /*
3271 * Set the security state in the memory retrieve response attributes
3272 * if specified by the target mode.
3273 */
3274 attributes = plat_ffa_memory_security_mode(
3275 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003276
3277 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3278
J-Alves4f0d9c12024-01-17 17:23:11 +00003279 CHECK(ffa_retrieved_memory_region_init(
3280 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3281 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003282 memory_region->flags, handle,
3283 receiver->receiver_permissions.permissions, receiver,
3284 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003285 composite->page_count, composite->constituent_count,
3286 share_state->fragments[0],
3287 share_state->fragment_constituent_counts[0], &total_length,
3288 &fragment_length));
3289
3290 return ffa_memory_retrieve_resp(total_length, fragment_length);
3291}
3292
3293struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3294 struct ffa_memory_region *retrieve_request,
3295 uint32_t retrieve_request_length,
3296 struct mpool *page_pool)
3297{
3298 ffa_memory_handle_t handle = retrieve_request->handle;
3299 struct share_states_locked share_states;
3300 struct ffa_memory_share_state *share_state;
3301 struct ffa_value ret;
3302
3303 dump_share_states();
3304
3305 share_states = share_states_lock();
3306 share_state = get_share_state(share_states, handle);
3307 if (share_state == NULL) {
3308 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3309 handle);
3310 ret = ffa_error(FFA_INVALID_PARAMETERS);
3311 goto out;
3312 }
3313
3314 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3315 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3316 retrieve_request);
3317 } else {
3318 ret = ffa_partition_retrieve_request(
3319 share_states, share_state, to_locked, retrieve_request,
3320 retrieve_request_length, page_pool);
3321 }
3322
3323 /* Track use of the RX buffer if the handling has succeeded. */
3324 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3325 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3326 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3327 }
3328
Andrew Walbranca808b12020-05-15 17:22:28 +01003329out:
3330 share_states_unlock(&share_states);
3331 dump_share_states();
3332 return ret;
3333}
3334
J-Alves5da37d92022-10-24 16:33:48 +01003335/**
3336 * Determine expected fragment offset according to the FF-A version of
3337 * the caller.
3338 */
3339static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3340 struct ffa_memory_region *memory_region,
3341 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3342{
3343 uint32_t expected_fragment_offset;
3344 uint32_t composite_constituents_offset;
3345
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003346 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003347 /*
3348 * Hafnium operates memory regions in FF-A v1.1 format, so we
3349 * can retrieve the constituents offset from descriptor.
3350 */
3351 composite_constituents_offset =
3352 ffa_composite_constituent_offset(memory_region, 0);
3353 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3354 /*
3355 * If retriever is FF-A v1.0, determine the composite offset
3356 * as it is expected to have been configured in the
3357 * retrieve response.
3358 */
3359 composite_constituents_offset =
3360 sizeof(struct ffa_memory_region_v1_0) +
3361 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003362 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003363 sizeof(struct ffa_composite_memory_region);
3364 } else {
3365 panic("%s received an invalid FF-A version.\n", __func__);
3366 }
3367
3368 expected_fragment_offset =
3369 composite_constituents_offset +
3370 retrieved_constituents_count *
3371 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003372 (uint32_t)(memory_region->memory_access_desc_size *
3373 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003374
3375 return expected_fragment_offset;
3376}
3377
Andrew Walbranca808b12020-05-15 17:22:28 +01003378struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3379 ffa_memory_handle_t handle,
3380 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003381 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003382 struct mpool *page_pool)
3383{
3384 struct ffa_memory_region *memory_region;
3385 struct share_states_locked share_states;
3386 struct ffa_memory_share_state *share_state;
3387 struct ffa_value ret;
3388 uint32_t fragment_index;
3389 uint32_t retrieved_constituents_count;
3390 uint32_t i;
3391 uint32_t expected_fragment_offset;
3392 uint32_t remaining_constituent_count;
3393 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003394 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003395 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003396
3397 dump_share_states();
3398
3399 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003400 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003401 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003402 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3403 handle);
3404 ret = ffa_error(FFA_INVALID_PARAMETERS);
3405 goto out;
3406 }
3407
3408 memory_region = share_state->memory_region;
3409 CHECK(memory_region != NULL);
3410
Andrew Walbranca808b12020-05-15 17:22:28 +01003411 if (!share_state->sending_complete) {
3412 dlog_verbose(
3413 "Memory with handle %#x not fully sent, can't "
3414 "retrieve.\n",
3415 handle);
3416 ret = ffa_error(FFA_INVALID_PARAMETERS);
3417 goto out;
3418 }
3419
J-Alves59ed0042022-07-28 18:26:41 +01003420 /*
3421 * If retrieve request from the hypervisor has been initiated in the
3422 * given share_state, continue it, else assume it is a continuation of
3423 * retrieve request from a NWd VM.
3424 */
3425 continue_ffa_hyp_mem_retrieve_req =
3426 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3427 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003428 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003429
J-Alves59ed0042022-07-28 18:26:41 +01003430 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003431 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003432 memory_region, to_locked.vm->id);
3433
3434 if (receiver_index == memory_region->receiver_count) {
3435 dlog_verbose(
3436 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3437 "borrower to memory sharing transaction (%x)\n",
3438 to_locked.vm->id, handle);
3439 ret = ffa_error(FFA_INVALID_PARAMETERS);
3440 goto out;
3441 }
3442
3443 if (share_state->retrieved_fragment_count[receiver_index] ==
3444 0 ||
3445 share_state->retrieved_fragment_count[receiver_index] >=
3446 share_state->fragment_count) {
3447 dlog_verbose(
3448 "Retrieval of memory with handle %#x not yet "
3449 "started or already completed (%d/%d fragments "
3450 "retrieved).\n",
3451 handle,
3452 share_state->retrieved_fragment_count
3453 [receiver_index],
3454 share_state->fragment_count);
3455 ret = ffa_error(FFA_INVALID_PARAMETERS);
3456 goto out;
3457 }
3458
3459 fragment_index =
3460 share_state->retrieved_fragment_count[receiver_index];
3461 } else {
3462 if (share_state->hypervisor_fragment_count == 0 ||
3463 share_state->hypervisor_fragment_count >=
3464 share_state->fragment_count) {
3465 dlog_verbose(
3466 "Retrieve of memory with handle %x not "
3467 "started from hypervisor.\n",
3468 handle);
3469 ret = ffa_error(FFA_INVALID_PARAMETERS);
3470 goto out;
3471 }
3472
3473 if (memory_region->sender != sender_vm_id) {
3474 dlog_verbose(
3475 "Sender ID (%x) is not as expected for memory "
3476 "handle %x\n",
3477 sender_vm_id, handle);
3478 ret = ffa_error(FFA_INVALID_PARAMETERS);
3479 goto out;
3480 }
3481
3482 fragment_index = share_state->hypervisor_fragment_count;
3483
3484 receiver_index = 0;
3485 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003486
3487 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003488 * Check that the given fragment offset is correct by counting
3489 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003490 */
3491 retrieved_constituents_count = 0;
3492 for (i = 0; i < fragment_index; ++i) {
3493 retrieved_constituents_count +=
3494 share_state->fragment_constituent_counts[i];
3495 }
J-Alvesc7484f12022-05-13 12:41:14 +01003496
3497 CHECK(memory_region->receiver_count > 0);
3498
Andrew Walbranca808b12020-05-15 17:22:28 +01003499 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003500 ffa_memory_retrieve_expected_offset_per_ffa_version(
3501 memory_region, retrieved_constituents_count,
3502 to_locked.vm->ffa_version);
3503
Andrew Walbranca808b12020-05-15 17:22:28 +01003504 if (fragment_offset != expected_fragment_offset) {
3505 dlog_verbose("Fragment offset was %d but expected %d.\n",
3506 fragment_offset, expected_fragment_offset);
3507 ret = ffa_error(FFA_INVALID_PARAMETERS);
3508 goto out;
3509 }
3510
J-Alves4f0d9c12024-01-17 17:23:11 +00003511 /*
3512 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3513 * is currently ownder by the SPMC.
3514 */
3515 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003516
Andrew Walbranca808b12020-05-15 17:22:28 +01003517 remaining_constituent_count = ffa_memory_fragment_init(
3518 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3519 share_state->fragments[fragment_index],
3520 share_state->fragment_constituent_counts[fragment_index],
3521 &fragment_length);
3522 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003523
Andrew Walbranca808b12020-05-15 17:22:28 +01003524 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003525 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003526
J-Alves59ed0042022-07-28 18:26:41 +01003527 if (!continue_ffa_hyp_mem_retrieve_req) {
3528 share_state->retrieved_fragment_count[receiver_index]++;
3529 if (share_state->retrieved_fragment_count[receiver_index] ==
3530 share_state->fragment_count) {
3531 ffa_memory_retrieve_complete(share_states, share_state,
3532 page_pool);
3533 }
3534 } else {
3535 share_state->hypervisor_fragment_count++;
3536
3537 ffa_memory_retrieve_complete_from_hyp(share_state);
3538 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003539 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3540 .arg1 = (uint32_t)handle,
3541 .arg2 = (uint32_t)(handle >> 32),
3542 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003543
3544out:
3545 share_states_unlock(&share_states);
3546 dump_share_states();
3547 return ret;
3548}
3549
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003550struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003551 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003552 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003553{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003554 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003555 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003556 struct ffa_memory_share_state *share_state;
3557 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003558 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003559 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003560 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003561 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00003562 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003563
Andrew Walbrana65a1322020-04-06 19:32:32 +01003564 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003565 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003566 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003567 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003568 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003569 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003570 }
3571
Andrew Walbrana65a1322020-04-06 19:32:32 +01003572 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003573 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003574 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003575 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003576 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003577 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003578 }
3579
3580 dump_share_states();
3581
3582 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003583 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003584 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003585 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003586 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003587 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003588 goto out;
3589 }
3590
Andrew Walbranca808b12020-05-15 17:22:28 +01003591 if (!share_state->sending_complete) {
3592 dlog_verbose(
3593 "Memory with handle %#x not fully sent, can't "
3594 "relinquish.\n",
3595 handle);
3596 ret = ffa_error(FFA_INVALID_PARAMETERS);
3597 goto out;
3598 }
3599
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003600 memory_region = share_state->memory_region;
3601 CHECK(memory_region != NULL);
3602
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003603 receiver_index = ffa_memory_region_get_receiver_index(
3604 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003605
3606 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003607 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003608 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003609 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003610 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003611 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003612 goto out;
3613 }
3614
J-Alves8eb19162022-04-28 10:56:48 +01003615 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003616 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003617 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003618 "Memory with handle %#x not yet fully "
3619 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003620 "receiver %x can't relinquish.\n",
3621 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003622 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003623 goto out;
3624 }
3625
J-Alves3c5b2072022-11-21 12:45:40 +00003626 /*
3627 * Either clear if requested in relinquish call, or in a retrieve
3628 * request from one of the borrowers.
3629 */
3630 receivers_relinquished_memory = true;
3631
3632 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3633 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003634 ffa_memory_region_get_receiver(memory_region, i);
3635 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003636 if (receiver->receiver_permissions.receiver ==
3637 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003638 receiver_permissions =
3639 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003640 continue;
3641 }
3642
3643 if (share_state->retrieved_fragment_count[i] != 0U) {
3644 receivers_relinquished_memory = false;
3645 break;
3646 }
3647 }
3648
3649 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003650 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3651 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003652
3653 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003654 * Clear is not allowed for memory that was shared, as the
3655 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003656 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003657 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003658 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003659 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003660 goto out;
3661 }
3662
J-Alves639ddfc2023-11-21 14:17:26 +00003663 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3664 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3665 __func__);
3666 ret = ffa_error(FFA_DENIED);
3667 goto out;
3668 }
3669
Andrew Walbranca808b12020-05-15 17:22:28 +01003670 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003671 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003672 share_state->fragment_constituent_counts,
3673 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003674
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003675 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003676 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003677 * Mark memory handle as not retrieved, so it can be
3678 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003679 */
J-Alves8eb19162022-04-28 10:56:48 +01003680 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003681 }
3682
3683out:
3684 share_states_unlock(&share_states);
3685 dump_share_states();
3686 return ret;
3687}
3688
3689/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003690 * Validates that the reclaim transition is allowed for the given
3691 * handle, updates the page table of the reclaiming VM, and frees the
3692 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003693 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003694struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003695 ffa_memory_handle_t handle,
3696 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003697 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003698{
3699 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003700 struct ffa_memory_share_state *share_state;
3701 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003702 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003703
3704 dump_share_states();
3705
3706 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003707
Karl Meakin4a2854a2023-06-30 16:26:52 +01003708 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003709 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003710 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003711 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003712 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003713 goto out;
3714 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003715 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003716
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003717 CHECK(memory_region != NULL);
3718
J-Alvesa9cd7e32022-07-01 13:49:33 +01003719 if (vm_id_is_current_world(to_locked.vm->id) &&
3720 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003721 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003722 "VM %#x attempted to reclaim memory handle %#x "
3723 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003724 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003725 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003726 goto out;
3727 }
3728
Andrew Walbranca808b12020-05-15 17:22:28 +01003729 if (!share_state->sending_complete) {
3730 dlog_verbose(
3731 "Memory with handle %#x not fully sent, can't "
3732 "reclaim.\n",
3733 handle);
3734 ret = ffa_error(FFA_INVALID_PARAMETERS);
3735 goto out;
3736 }
3737
J-Alves752236c2022-04-28 11:07:47 +01003738 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3739 if (share_state->retrieved_fragment_count[i] != 0) {
3740 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003741 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003742 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003743 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003744 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003745 ffa_memory_region_get_receiver(memory_region, i)
3746 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003747 ret = ffa_error(FFA_DENIED);
3748 goto out;
3749 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003750 }
3751
Andrew Walbranca808b12020-05-15 17:22:28 +01003752 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003753 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003754 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003755 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003756 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003757 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003758
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003759 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003760 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003761 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003762 }
3763
3764out:
3765 share_states_unlock(&share_states);
3766 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003767}