blob: 164cb77722cb40897e9a7a223e0cc008114ea882 [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;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000523 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
524 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100525
526 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100527 /* Check the reserved fields are 0. */
528 if (memory_region_v1_0->reserved_0 != 0 ||
529 memory_region_v1_0->reserved_1 != 0) {
530 dlog_verbose("Reserved fields must be 0.\n");
531 return false;
532 }
533
534 receiver_count = memory_region_v1_0->receiver_count;
535 } else {
536 uint32_t receivers_size =
537 memory_region->memory_access_desc_size;
538 uint32_t receivers_offset = memory_region->receivers_offset;
539
540 /* Check the reserved field is 0. */
541 if (memory_region->reserved[0] != 0 ||
542 memory_region->reserved[1] != 0 ||
543 memory_region->reserved[2] != 0) {
544 dlog_verbose("Reserved fields must be 0.\n");
545 return false;
546 }
547
548 /*
549 * Check memory_access_desc_size matches the size of the struct
550 * for the senders FF-A version.
551 */
552 if (!receiver_size_and_offset_valid_for_version(
553 receivers_size, receivers_offset, ffa_version)) {
554 dlog_verbose(
555 "Invalid memory access descriptor size %d, "
556 " or receiver offset %d, "
557 "for FF-A version %#x\n",
558 receivers_size, receivers_offset, ffa_version);
559 return false;
560 }
561
562 receiver_count = memory_region->receiver_count;
563 }
564
565 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000566 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100567 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000568 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100569 "specified %u\n",
570 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
571 return false;
572 }
573
574 /* Check values in the memory access descriptors. */
575 /*
576 * The composite offset values must be the same for all recievers so
577 * check the first one is valid and then they are all the same.
578 */
579 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000580 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100581 ->receivers[0]
582 : ffa_memory_region_get_receiver(memory_region, 0);
583 assert(receiver != NULL);
584 composite_offset_0 = receiver->composite_memory_region_offset;
585
586 if (!send_transaction) {
587 if (composite_offset_0 != 0) {
588 dlog_verbose(
589 "Composite offset memory region descriptor "
590 "offset must be 0 for retrieve requests. "
591 "Currently %d",
592 composite_offset_0);
593 return false;
594 }
595 } else {
596 bool comp_offset_is_zero = composite_offset_0 == 0U;
597 bool comp_offset_lt_transaction_descriptor_size =
598 composite_offset_0 <
599 (sizeof(struct ffa_memory_region) +
600 (uint32_t)(memory_region->memory_access_desc_size *
601 memory_region->receiver_count));
602 bool comp_offset_with_comp_gt_fragment_length =
603 composite_offset_0 +
604 sizeof(struct ffa_composite_memory_region) >
605 fragment_length;
606 if (comp_offset_is_zero ||
607 comp_offset_lt_transaction_descriptor_size ||
608 comp_offset_with_comp_gt_fragment_length) {
609 dlog_verbose(
610 "Invalid composite memory region descriptor "
611 "offset for send transaction %u\n",
612 composite_offset_0);
613 return false;
614 }
615 }
616
617 for (int i = 0; i < memory_region->receiver_count; i++) {
618 uint32_t composite_offset;
619
620 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100621 struct ffa_memory_access_v1_0 *receiver_v1_0 =
622 &memory_region_v1_0->receivers[i];
623 /* Check reserved fields are 0 */
624 if (receiver_v1_0->reserved_0 != 0) {
625 dlog_verbose(
626 "Reserved field in the memory access "
627 " descriptor must be zero "
628 " Currently reciever %d has a reserved "
629 " field with a value of %d\n",
630 i, receiver_v1_0->reserved_0);
631 return false;
632 }
633 /*
634 * We can cast to the current version receiver as the
635 * remaining fields we are checking have the same
636 * offsets for all versions since memory access
637 * descriptors are forwards compatible.
638 */
639 receiver = (struct ffa_memory_access *)receiver_v1_0;
640 } else {
641 receiver = ffa_memory_region_get_receiver(memory_region,
642 i);
643 assert(receiver != NULL);
644
645 if (receiver->reserved_0 != 0) {
646 dlog_verbose(
647 "Reserved field in the memory access "
648 " descriptor must be zero "
649 " Currently reciever %d has a reserved "
650 " field with a value of %d\n",
651 i, receiver->reserved_0);
652 return false;
653 }
654 }
655
656 /* Check composite offset values are equal for all receivers. */
657 composite_offset = receiver->composite_memory_region_offset;
658 if (composite_offset != composite_offset_0) {
659 dlog_verbose(
660 "Composite offset %x differs from %x in index "
661 "%u\n",
662 composite_offset, composite_offset_0);
663 return false;
664 }
665 }
666 return true;
667}
668
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000669/**
J-Alves460d36c2023-10-12 17:02:15 +0100670 * If the receivers for the memory management operation are all from the
671 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
672 * state update by returning MAP_ACTION_CHECK_PROTECT.
673 */
674static enum ffa_map_action ffa_mem_send_get_map_action(
675 bool all_receivers_from_current_world, ffa_id_t sender_id,
676 uint32_t mem_func_id)
677{
678 bool protect_memory =
679 (mem_func_id != FFA_MEM_SHARE_32 &&
680 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
681
682 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
683}
684
685/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000686 * Verify that all pages have the same mode, that the starting mode
687 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100688 * to the sending VM. It outputs the mapping action that needs to be
689 * invoked for the given memory range. On memory lend/donate there
690 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 *
692 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693 * 1) FFA_DENIED if a state transition was not found;
694 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100695 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100697 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
699 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000700 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100702 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000703 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100704 struct ffa_memory_region_constituent **fragments,
705 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100706 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000707{
708 const uint32_t state_mask =
709 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100711 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000712 uint32_t receivers_count = memory_region->receiver_count;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000713
Andrew Walbranca808b12020-05-15 17:22:28 +0100714 ret = constituents_get_mode(from, orig_from_mode, fragments,
715 fragment_constituent_counts,
716 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100717 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100718 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100719 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100720 }
721
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000722 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100723 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000724 dlog_verbose("Can't share device memory (mode is %#x).\n",
725 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100726 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 }
728
729 /*
730 * Ensure the sender is the owner and has exclusive access to the
731 * memory.
732 */
733 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100734 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100735 }
736
Daniel Boulbya76fd912024-02-22 14:22:15 +0000737 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100738
J-Alves363f5722022-04-25 17:37:37 +0100739 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000740 struct ffa_memory_access *receiver =
741 ffa_memory_region_get_receiver(memory_region, i);
742 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100743 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000744 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100745 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
746 permissions, *orig_from_mode);
747
J-Alves788b4492023-04-18 14:01:23 +0100748 /*
749 * The assumption is that at this point, the operation from
750 * SP to a receiver VM, should have returned an FFA_ERROR
751 * already.
752 */
753 if (!ffa_is_vm_id(from.vm->id)) {
754 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000755 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100756 }
757
J-Alves460d36c2023-10-12 17:02:15 +0100758 /* Track if all senders are from current world. */
759 all_receivers_from_current_world =
760 all_receivers_from_current_world &&
761 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000762 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100763
J-Alves363f5722022-04-25 17:37:37 +0100764 if ((*orig_from_mode & required_from_mode) !=
765 required_from_mode) {
766 dlog_verbose(
767 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100768 "which required mode %#x but only had %#x "
769 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100770 required_from_mode, *orig_from_mode);
771 return ffa_error(FFA_DENIED);
772 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000773 }
774
J-Alves460d36c2023-10-12 17:02:15 +0100775 *map_action = ffa_mem_send_get_map_action(
776 all_receivers_from_current_world, from.vm->id, share_func);
777
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000778 /* Find the appropriate new mode. */
779 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000780 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100781 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000782 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100783 break;
784
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100785 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000786 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100787 break;
788
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100789 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000790 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100791 break;
792
Jose Marinho75509b42019-04-09 09:34:59 +0100793 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100794 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100795 }
796
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798}
799
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100800static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000801 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 struct ffa_memory_region_constituent **fragments,
803 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
804 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000805{
806 const uint32_t state_mask =
807 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
808 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100809 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000810
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 ret = constituents_get_mode(from, orig_from_mode, fragments,
812 fragment_constituent_counts,
813 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100815 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000816 }
817
818 /* Ensure the address range is normal memory and not a device. */
819 if (*orig_from_mode & MM_MODE_D) {
820 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
821 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100822 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000823 }
824
825 /*
826 * Ensure the relinquishing VM is not the owner but has access to the
827 * memory.
828 */
829 orig_from_state = *orig_from_mode & state_mask;
830 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
831 dlog_verbose(
832 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100833 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000834 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000836 }
837
838 /* Find the appropriate new mode. */
839 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
840
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100841 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000842}
843
844/**
845 * Verify that all pages have the same mode, that the starting mode
846 * constitutes a valid state and obtain the next mode to apply
847 * to the retrieving VM.
848 *
849 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 * 1) FFA_DENIED if a state transition was not found;
851 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100852 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100854 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
856 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857 */
J-Alvesfc19b372022-07-06 12:17:35 +0100858struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000859 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 struct ffa_memory_region_constituent **fragments,
861 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100862 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
863 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000864{
865 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867
Andrew Walbranca808b12020-05-15 17:22:28 +0100868 ret = constituents_get_mode(to, &orig_to_mode, fragments,
869 fragment_constituent_counts,
870 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100872 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100873 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000874 }
875
J-Alves460d36c2023-10-12 17:02:15 +0100876 /* Find the appropriate new mode. */
877 *to_mode = memory_to_attributes;
878
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000880 /*
881 * If the original ffa memory send call has been processed
882 * successfully, it is expected the orig_to_mode would overlay
883 * with `state_mask`, as a result of the function
884 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100885 *
886 * If Hafnium is the SPMC:
887 * - Caller of the reclaim interface is an SP, the memory shall
888 * have been protected throughout the flow.
889 * - Caller of the reclaim is from the NWd, the memory may have
890 * been protected at the time of lending/donating the memory.
891 * In such case, set action to unprotect memory in the
892 * handling of reclaim operation.
893 * - If Hafnium is the hypervisor memory shall never have been
894 * protected in memory lend/share/donate.
895 *
896 * More details in the doc comment of the function
897 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000898 */
J-Alves59ed0042022-07-28 18:26:41 +0100899 if (vm_id_is_current_world(to.vm->id)) {
900 assert((orig_to_mode &
901 (MM_MODE_INVALID | MM_MODE_UNOWNED |
902 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100903 assert(!memory_protected);
904 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
905 map_action != NULL && memory_protected) {
906 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100907 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000908 } else {
909 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100910 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000911 * Ensure the retriever has the expected state. We don't care
912 * about the MM_MODE_SHARED bit; either with or without it set
913 * are both valid representations of the !O-NA state.
914 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100915 if (vm_id_is_current_world(to.vm->id) &&
916 to.vm->id != HF_PRIMARY_VM_ID &&
917 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
918 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 }
J-Alves460d36c2023-10-12 17:02:15 +0100921
922 /*
923 * If memory has been protected before, clear the NS bit to
924 * allow the secure access from the SP.
925 */
926 if (memory_protected) {
927 *to_mode &= ~plat_ffa_other_world_mode();
928 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 }
930
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933 *to_mode |= 0;
934 break;
935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 *to_mode |= MM_MODE_UNOWNED;
938 break;
939
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100940 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000941 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
942 break;
943
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000945 *to_mode |= 0;
946 break;
947
948 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100949 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100950 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 }
952
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100953 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100954}
Jose Marinho09b1db82019-08-08 09:16:59 +0100955
J-Alvescf6253e2024-01-03 13:48:48 +0000956/*
957 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
958 * Returns:
959 * - FFA_SUCCESS_32: if all goes well.
960 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
961 * the page table update. Or error code provided by the function
962 * `arch_memory_protect`.
963 */
964static struct ffa_value ffa_region_group_check_actions(
965 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
966 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
967 bool *memory_protected)
968{
969 struct ffa_value ret;
970 bool is_memory_protected;
971
972 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
973 dlog_verbose(
974 "%s: memory can't be mapped to %x due to lack of "
975 "memory. Base: %lx end: %x\n",
976 __func__, vm_locked.vm->id, pa_addr(pa_begin),
977 pa_addr(pa_end));
978 return ffa_error(FFA_NO_MEMORY);
979 }
980
981 switch (action) {
982 case MAP_ACTION_CHECK:
983 /* No protect requested. */
984 is_memory_protected = false;
985 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
986 break;
987 case MAP_ACTION_CHECK_PROTECT: {
988 paddr_t last_protected_pa = pa_init(0);
989
990 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
991
992 is_memory_protected = (ret.func == FFA_SUCCESS_32);
993
994 /*
995 * - If protect memory has failed with FFA_DENIED, means some
996 * range of memory was in the wrong state. In such case, SPM
997 * reverts the state of the pages that were successfully
998 * updated.
999 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1000 * means the platform doesn't support the protection mechanism.
1001 * That said, it still permits the page table update to go
1002 * through. The variable
1003 * `is_memory_protected` will be equal to false.
1004 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1005 * break from switch and return the error.
1006 */
1007 if (ret.func == FFA_ERROR_32) {
1008 assert(!is_memory_protected);
1009 if (ffa_error_code(ret) == FFA_DENIED &&
1010 pa_addr(last_protected_pa) != (uintptr_t)0) {
1011 CHECK(arch_memory_unprotect(
1012 pa_begin,
1013 pa_add(last_protected_pa, PAGE_SIZE)));
1014 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1015 ret = (struct ffa_value){
1016 .func = FFA_SUCCESS_32,
1017 };
1018 }
1019 }
1020 } break;
1021 default:
1022 panic("%s: invalid action to process %x\n", __func__, action);
1023 }
1024
1025 if (memory_protected != NULL) {
1026 *memory_protected = is_memory_protected;
1027 }
1028
1029 return ret;
1030}
1031
1032static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1033 paddr_t pa_begin, paddr_t pa_end,
1034 struct mpool *ppool, uint32_t mode,
1035 enum ffa_map_action action)
1036{
1037 switch (action) {
1038 case MAP_ACTION_COMMIT_UNPROTECT:
1039 /*
1040 * Checking that it should succeed because SPM should be
1041 * unprotecting memory that it had protected before.
1042 */
1043 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1044 case MAP_ACTION_COMMIT:
1045 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1046 NULL);
1047 break;
1048 default:
1049 panic("%s: invalid action to process %x\n", __func__, action);
1050 }
1051}
1052
Jose Marinho09b1db82019-08-08 09:16:59 +01001053/**
J-Alves063ad832023-10-03 18:05:40 +01001054 * Helper function to revert a failed "Protect" action from the SPMC:
1055 * - `fragment_count`: should specify the number of fragments to traverse from
1056 * `fragments`. This may not be the full amount of fragments that are part of
1057 * the share_state structure.
1058 * - `fragment_constituent_counts`: array holding the amount of constituents
1059 * per fragment.
1060 * - `end`: pointer to the constituent that failed the "protect" action. It
1061 * shall be part of the last fragment, and it shall make the loop below break.
1062 */
1063static void ffa_region_group_fragments_revert_protect(
1064 struct ffa_memory_region_constituent **fragments,
1065 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1066 const struct ffa_memory_region_constituent *end)
1067{
1068 for (uint32_t i = 0; i < fragment_count; ++i) {
1069 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1070 struct ffa_memory_region_constituent *constituent =
1071 &fragments[i][j];
1072 size_t size = constituent->page_count * PAGE_SIZE;
1073 paddr_t pa_begin =
1074 pa_from_ipa(ipa_init(constituent->address));
1075 paddr_t pa_end = pa_add(pa_begin, size);
1076
1077 dlog_verbose("%s: reverting fragment %x size %x\n",
1078 __func__, pa_addr(pa_begin), size);
1079
1080 if (constituent == end) {
1081 /*
1082 * The last constituent is expected to be in the
1083 * last fragment.
1084 */
1085 assert(i == fragment_count - 1);
1086 break;
1087 }
1088
1089 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1090 }
1091 }
1092}
1093
1094/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001095 * Updates a VM's page table such that the given set of physical address ranges
1096 * are mapped in the address space at the corresponding address ranges, in the
1097 * mode provided.
1098 *
J-Alves0a83dc22023-05-05 09:50:37 +01001099 * The enum ffa_map_action determines the action taken from a call to the
1100 * function below:
1101 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1102 * mpool but no mappings will actually be updated. This function must always
1103 * be called first with action set to MAP_ACTION_CHECK to check that it will
1104 * succeed before calling ffa_region_group_identity_map with whichever one of
1105 * the remaining actions, to avoid leaving the page table in a half-updated
1106 * state.
1107 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1108 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001109 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1110 * invocation to the monitor to update the security state of the memory,
1111 * to that of the SPMC.
1112 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1113 * with a call into the monitor, to reset the security state of memory
1114 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001115 * vm_ptable_defrag should always be called after a series of page table
1116 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001117 *
J-Alvescf6253e2024-01-03 13:48:48 +00001118 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1119 * error codes:
1120 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1121 * - FFA_DENIED:
1122 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001123 * made to memory mappings.
1124 */
J-Alvescf6253e2024-01-03 13:48:48 +00001125struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001126 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001127 struct ffa_memory_region_constituent **fragments,
1128 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001129 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1130 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001131{
Andrew Walbranca808b12020-05-15 17:22:28 +01001132 uint32_t i;
1133 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001134 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001135
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001136 if (vm_locked.vm->el0_partition) {
1137 mode |= MM_MODE_USER | MM_MODE_NG;
1138 }
1139
Andrew Walbranca808b12020-05-15 17:22:28 +01001140 /* Iterate over the memory region constituents within each fragment. */
1141 for (i = 0; i < fragment_count; ++i) {
1142 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001143 struct ffa_memory_region_constituent *constituent =
1144 &fragments[i][j];
1145 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001146 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001147 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001149 uint32_t pa_bits =
1150 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001151
1152 /*
1153 * Ensure the requested region falls into system's PA
1154 * range.
1155 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001156 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1157 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001158 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001159 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001160 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001161
J-Alvescf6253e2024-01-03 13:48:48 +00001162 if (action <= MAP_ACTION_CHECK_PROTECT) {
1163 ret = ffa_region_group_check_actions(
1164 vm_locked, pa_begin, pa_end, ppool,
1165 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001166
1167 if (ret.func == FFA_ERROR_32 &&
1168 ffa_error_code(ret) == FFA_DENIED) {
1169 if (memory_protected != NULL) {
1170 assert(!*memory_protected);
1171 }
1172
1173 ffa_region_group_fragments_revert_protect(
1174 fragments,
1175 fragment_constituent_counts,
1176 i + 1, constituent);
1177 break;
1178 }
J-Alvescf6253e2024-01-03 13:48:48 +00001179 } else if (action >= MAP_ACTION_COMMIT &&
1180 action < MAP_ACTION_MAX) {
1181 ffa_region_group_commit_actions(
1182 vm_locked, pa_begin, pa_end, ppool,
1183 mode, action);
1184 ret = (struct ffa_value){
1185 .func = FFA_SUCCESS_32};
1186 } else {
1187 panic("%s: Unknown ffa_map_action.\n",
1188 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001190 }
1191 }
1192
J-Alvescf6253e2024-01-03 13:48:48 +00001193 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001194}
1195
1196/**
1197 * Clears a region of physical memory by overwriting it with zeros. The data is
1198 * flushed from the cache so the memory has been cleared across the system.
1199 */
J-Alves7db32002021-12-14 14:44:50 +00001200static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1201 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001202{
1203 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001204 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001205 * global mapping of the whole range. Such an approach will limit
1206 * the changes to stage-1 tables and will allow only local
1207 * invalidation.
1208 */
1209 bool ret;
1210 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001211 void *ptr = mm_identity_map(stage1_locked, begin, end,
1212 MM_MODE_W | (extra_mode_attributes &
1213 plat_ffa_other_world_mode()),
1214 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001215 size_t size = pa_difference(begin, end);
1216
1217 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001218 goto fail;
1219 }
1220
1221 memset_s(ptr, size, 0, size);
1222 arch_mm_flush_dcache(ptr, size);
1223 mm_unmap(stage1_locked, begin, end, ppool);
1224
1225 ret = true;
1226 goto out;
1227
1228fail:
1229 ret = false;
1230
1231out:
1232 mm_unlock_stage1(&stage1_locked);
1233
1234 return ret;
1235}
1236
1237/**
1238 * Clears a region of physical memory by overwriting it with zeros. The data is
1239 * flushed from the cache so the memory has been cleared across the system.
1240 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001241static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001242 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 struct ffa_memory_region_constituent **fragments,
1244 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1245 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001246{
1247 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001249 bool ret = false;
1250
1251 /*
1252 * Create a local pool so any freed memory can't be used by another
1253 * thread. This is to ensure each constituent that is mapped can be
1254 * unmapped again afterwards.
1255 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001256 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001257
Andrew Walbranca808b12020-05-15 17:22:28 +01001258 /* Iterate over the memory region constituents within each fragment. */
1259 for (i = 0; i < fragment_count; ++i) {
1260 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001261
J-Alves8457f932023-10-11 16:41:45 +01001262 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001263 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1264 paddr_t begin =
1265 pa_from_ipa(ipa_init(fragments[i][j].address));
1266 paddr_t end = pa_add(begin, size);
1267
J-Alves7db32002021-12-14 14:44:50 +00001268 if (!clear_memory(begin, end, &local_page_pool,
1269 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001270 /*
1271 * api_clear_memory will defrag on failure, so
1272 * no need to do it here.
1273 */
1274 goto out;
1275 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001276 }
1277 }
1278
Jose Marinho09b1db82019-08-08 09:16:59 +01001279 ret = true;
1280
1281out:
1282 mpool_fini(&local_page_pool);
1283 return ret;
1284}
1285
J-Alves5952d942022-12-22 16:03:00 +00001286static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1287 ipaddr_t in_begin, ipaddr_t in_end)
1288{
1289 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1290 ipa_addr(begin) < ipa_addr(in_end)) ||
1291 (ipa_addr(end) <= ipa_addr(in_end) &&
1292 ipa_addr(end) > ipa_addr(in_begin));
1293}
1294
1295/**
1296 * Receives a memory range and looks for overlaps with the remainder
1297 * constituents of the memory share/lend/donate operation. Assumes they are
1298 * passed in order to avoid having to loop over all the elements at each call.
1299 * The function only compares the received memory ranges with those that follow
1300 * within the same fragment, and subsequent fragments from the same operation.
1301 */
1302static bool ffa_memory_check_overlap(
1303 struct ffa_memory_region_constituent **fragments,
1304 const uint32_t *fragment_constituent_counts,
1305 const uint32_t fragment_count, const uint32_t current_fragment,
1306 const uint32_t current_constituent)
1307{
1308 uint32_t i = current_fragment;
1309 uint32_t j = current_constituent;
1310 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1311 const uint32_t current_page_count = fragments[i][j].page_count;
1312 size_t current_size = current_page_count * PAGE_SIZE;
1313 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1314
1315 if (current_size == 0 ||
1316 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1317 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1318 current_begin, current_page_count);
1319 return false;
1320 }
1321
1322 for (; i < fragment_count; i++) {
1323 j = (i == current_fragment) ? j + 1 : 0;
1324
1325 for (; j < fragment_constituent_counts[i]; j++) {
1326 ipaddr_t begin = ipa_init(fragments[i][j].address);
1327 const uint32_t page_count = fragments[i][j].page_count;
1328 size_t size = page_count * PAGE_SIZE;
1329 ipaddr_t end = ipa_add(begin, size - 1);
1330
1331 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1332 dlog_verbose(
1333 "Invalid page count. Addr: %x "
1334 "page_count: %x\n",
1335 begin, page_count);
1336 return false;
1337 }
1338
1339 /*
1340 * Check if current ranges is within begin and end, as
1341 * well as the reverse. This should help optimize the
1342 * loop, and reduce the number of iterations.
1343 */
1344 if (is_memory_range_within(begin, end, current_begin,
1345 current_end) ||
1346 is_memory_range_within(current_begin, current_end,
1347 begin, end)) {
1348 dlog_verbose(
1349 "Overlapping memory ranges: %#x - %#x "
1350 "with %#x - %#x\n",
1351 ipa_addr(begin), ipa_addr(end),
1352 ipa_addr(current_begin),
1353 ipa_addr(current_end));
1354 return true;
1355 }
1356 }
1357 }
1358
1359 return false;
1360}
1361
Jose Marinho09b1db82019-08-08 09:16:59 +01001362/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001363 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001364 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001365 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001366 *
1367 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001368 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001369 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001370 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001371 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1372 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001375 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001376 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001377static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001378 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001379 struct ffa_memory_region_constituent **fragments,
1380 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001381 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001382 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1383 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001384{
Andrew Walbranca808b12020-05-15 17:22:28 +01001385 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001386 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001387 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001388 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001389 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001390 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001392 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001393 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001394 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001395
1396 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001397 * Make sure constituents are properly aligned to a 64-bit boundary. If
1398 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001399 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001400 for (i = 0; i < fragment_count; ++i) {
1401 if (!is_aligned(fragments[i], 8)) {
1402 dlog_verbose("Constituents not aligned.\n");
1403 return ffa_error(FFA_INVALID_PARAMETERS);
1404 }
J-Alves8f11cde2022-12-21 16:18:22 +00001405 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1406 constituents_total_page_count +=
1407 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001408 if (ffa_memory_check_overlap(
1409 fragments, fragment_constituent_counts,
1410 fragment_count, i, j)) {
1411 return ffa_error(FFA_INVALID_PARAMETERS);
1412 }
J-Alves8f11cde2022-12-21 16:18:22 +00001413 }
1414 }
1415
1416 if (constituents_total_page_count != composite_total_page_count) {
1417 dlog_verbose(
1418 "Composite page count differs from calculated page "
1419 "count from constituents.\n");
1420 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001421 }
1422
1423 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001424 * Check if the state transition is lawful for the sender, ensure that
1425 * all constituents of a memory region being shared are at the same
1426 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001427 */
J-Alves460d36c2023-10-12 17:02:15 +01001428 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001429 from_locked, share_func, memory_region, &orig_from_mode,
1430 fragments, fragment_constituent_counts, fragment_count,
1431 &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001433 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001435 }
1436
Andrew Walbran37c574e2020-06-03 11:45:46 +01001437 if (orig_from_mode_ret != NULL) {
1438 *orig_from_mode_ret = orig_from_mode;
1439 }
1440
Jose Marinho09b1db82019-08-08 09:16:59 +01001441 /*
1442 * Create a local pool so any freed memory can't be used by another
1443 * thread. This is to ensure the original mapping can be restored if the
1444 * clear fails.
1445 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001446 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001447
1448 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001449 * First reserve all required memory for the new page table entries
1450 * without committing, to make sure the entire operation will succeed
1451 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001452 * Provide the map_action as populated by 'ffa_send_check_transition'.
1453 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001454 */
J-Alvescf6253e2024-01-03 13:48:48 +00001455 ret = ffa_region_group_identity_map(
1456 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001457 fragment_count, from_mode, page_pool, map_action,
1458 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001459 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001460 goto out;
1461 }
1462
1463 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 * Update the mapping for the sender. This won't allocate because the
1465 * transaction was already prepared above, but may free pages in the
1466 * case that a whole block is being unmapped that was previously
1467 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001468 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001469 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001470 from_locked, fragments, fragment_constituent_counts,
1471 fragment_count, from_mode, &local_page_pool,
1472 MAP_ACTION_COMMIT, NULL)
1473 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001474
J-Alves460d36c2023-10-12 17:02:15 +01001475 /*
1476 * If memory has been protected, it is now part of the secure PAS
1477 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1478 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1479 * SPM's S1 translation.
1480 * In case memory hasn't been protected, and it is in the non-secure
1481 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1482 * perform a non-secure memory access. In such case `clean_mode` takes
1483 * the same mode as `orig_from_mode`.
1484 */
1485 clean_mode = (memory_protected != NULL && *memory_protected)
1486 ? orig_from_mode & ~plat_ffa_other_world_mode()
1487 : orig_from_mode;
1488
Jose Marinho09b1db82019-08-08 09:16:59 +01001489 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001490 if (clear && !ffa_clear_memory_constituents(
1491 clean_mode, fragments, fragment_constituent_counts,
1492 fragment_count, page_pool)) {
1493 map_action = (memory_protected != NULL && *memory_protected)
1494 ? MAP_ACTION_COMMIT_UNPROTECT
1495 : MAP_ACTION_COMMIT;
1496
Jose Marinho09b1db82019-08-08 09:16:59 +01001497 /*
1498 * On failure, roll back by returning memory to the sender. This
1499 * may allocate pages which were previously freed into
1500 * `local_page_pool` by the call above, but will never allocate
1501 * more pages than that so can never fail.
1502 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001504 from_locked, fragments,
1505 fragment_constituent_counts, fragment_count,
1506 orig_from_mode, &local_page_pool,
1507 MAP_ACTION_COMMIT, NULL)
1508 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001509 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001510 goto out;
1511 }
1512
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001514
1515out:
1516 mpool_fini(&local_page_pool);
1517
1518 /*
1519 * Tidy up the page table by reclaiming failed mappings (if there was an
1520 * error) or merging entries into blocks where possible (on success).
1521 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001522 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001523
1524 return ret;
1525}
1526
1527/**
1528 * Validates and maps memory shared from one VM to another.
1529 *
1530 * This function requires the calling context to hold the <to> lock.
1531 *
1532 * Returns:
1533 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001535 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001537 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001539 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001540struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001541 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001542 struct ffa_memory_region_constituent **fragments,
1543 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001544 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001545 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001546{
Andrew Walbranca808b12020-05-15 17:22:28 +01001547 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001548 uint32_t to_mode;
1549 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001550 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001551 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552
1553 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001554 * Make sure constituents are properly aligned to a 64-bit boundary. If
1555 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001556 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001557 for (i = 0; i < fragment_count; ++i) {
1558 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001559 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 return ffa_error(FFA_INVALID_PARAMETERS);
1561 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 }
1563
1564 /*
1565 * Check if the state transition is lawful for the recipient, and ensure
1566 * that all constituents of the memory region being retrieved are at the
1567 * same state.
1568 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001569 ret = ffa_retrieve_check_transition(
1570 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001571 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1572 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001573
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001574 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001575 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001576 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001577 }
1578
1579 /*
1580 * Create a local pool so any freed memory can't be used by another
1581 * thread. This is to ensure the original mapping can be restored if the
1582 * clear fails.
1583 */
1584 mpool_init_with_fallback(&local_page_pool, page_pool);
1585
1586 /*
1587 * First reserve all required memory for the new page table entries in
1588 * the recipient page tables without committing, to make sure the entire
1589 * operation will succeed without exhausting the page pool.
1590 */
J-Alvescf6253e2024-01-03 13:48:48 +00001591 ret = ffa_region_group_identity_map(
1592 to_locked, fragments, fragment_constituent_counts,
1593 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1594 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001595 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001596 goto out;
1597 }
1598
1599 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001600 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001601 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1602 fragment_constituent_counts,
1603 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001604 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001605 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001606 goto out;
1607 }
1608
Jose Marinho09b1db82019-08-08 09:16:59 +01001609 /*
1610 * Complete the transfer by mapping the memory into the recipient. This
1611 * won't allocate because the transaction was already prepared above, so
1612 * it doesn't need to use the `local_page_pool`.
1613 */
J-Alvesfd206052023-05-22 16:45:00 +01001614 CHECK(ffa_region_group_identity_map(
1615 to_locked, fragments, fragment_constituent_counts,
1616 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001617 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001618
J-Alves460d36c2023-10-12 17:02:15 +01001619 /* Return the mode used in mapping the memory in retriever's PT. */
1620 if (response_mode != NULL) {
1621 *response_mode = to_mode;
1622 }
1623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001624 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001625
1626out:
1627 mpool_fini(&local_page_pool);
1628
1629 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001630 * Tidy up the page table by reclaiming failed mappings (if there was an
1631 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001632 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001633 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001634
1635 return ret;
1636}
1637
Andrew Walbran996d1d12020-05-27 14:08:43 +01001638static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001639 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 struct ffa_memory_region_constituent **fragments,
1641 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1642 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001643{
1644 uint32_t orig_from_mode;
1645 uint32_t from_mode;
1646 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648
Andrew Walbranca808b12020-05-15 17:22:28 +01001649 ret = ffa_relinquish_check_transition(
1650 from_locked, &orig_from_mode, fragments,
1651 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001653 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001654 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 }
1656
1657 /*
1658 * Create a local pool so any freed memory can't be used by another
1659 * thread. This is to ensure the original mapping can be restored if the
1660 * clear fails.
1661 */
1662 mpool_init_with_fallback(&local_page_pool, page_pool);
1663
1664 /*
1665 * First reserve all required memory for the new page table entries
1666 * without committing, to make sure the entire operation will succeed
1667 * without exhausting the page pool.
1668 */
J-Alvescf6253e2024-01-03 13:48:48 +00001669 ret = ffa_region_group_identity_map(
1670 from_locked, fragments, fragment_constituent_counts,
1671 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1672 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001673 goto out;
1674 }
1675
1676 /*
1677 * Update the mapping for the sender. This won't allocate because the
1678 * transaction was already prepared above, but may free pages in the
1679 * case that a whole block is being unmapped that was previously
1680 * partially mapped.
1681 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001683 from_locked, fragments, fragment_constituent_counts,
1684 fragment_count, from_mode, &local_page_pool,
1685 MAP_ACTION_COMMIT, NULL)
1686 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001687
1688 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001689 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001690 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1691 fragment_constituent_counts,
1692 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001693 /*
1694 * On failure, roll back by returning memory to the sender. This
1695 * may allocate pages which were previously freed into
1696 * `local_page_pool` by the call above, but will never allocate
1697 * more pages than that so can never fail.
1698 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001699 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001700 from_locked, fragments,
1701 fragment_constituent_counts, fragment_count,
1702 orig_from_mode, &local_page_pool,
1703 MAP_ACTION_COMMIT, NULL)
1704 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001705
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001706 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 goto out;
1708 }
1709
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001711
1712out:
1713 mpool_fini(&local_page_pool);
1714
1715 /*
1716 * Tidy up the page table by reclaiming failed mappings (if there was an
1717 * error) or merging entries into blocks where possible (on success).
1718 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001719 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001720
1721 return ret;
1722}
1723
1724/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 * Complete a memory sending operation by checking that it is valid, updating
1726 * the sender page table, and then either marking the share state as having
1727 * completed sending (on success) or freeing it (on failure).
1728 *
1729 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1730 */
J-Alvesfdd29272022-07-19 13:16:31 +01001731struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001732 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001733 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1734 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001735{
1736 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001737 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001738 struct ffa_value ret;
1739
1740 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001741 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001742 assert(memory_region != NULL);
1743 composite = ffa_memory_region_get_composite(memory_region, 0);
1744 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001745
1746 /* Check that state is valid in sender page table and update. */
1747 ret = ffa_send_check_update(
1748 from_locked, share_state->fragments,
1749 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001750 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001751 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001752 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001753 if (ret.func != FFA_SUCCESS_32) {
1754 /*
1755 * Free share state, it failed to send so it can't be retrieved.
1756 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001757 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1758 __func__, ffa_func_name(ret.func),
1759 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001760 share_state_free(share_states, share_state, page_pool);
1761 return ret;
1762 }
1763
1764 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001765 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001766
J-Alvesee68c542020-10-29 17:48:20 +00001767 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001768}
1769
1770/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001771 * Check that the memory attributes match Hafnium expectations:
1772 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1773 * Write-Allocate Cacheable.
1774 */
1775static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001776 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001777{
1778 enum ffa_memory_type memory_type;
1779 enum ffa_memory_cacheability cacheability;
1780 enum ffa_memory_shareability shareability;
1781
1782 memory_type = ffa_get_memory_type_attr(attributes);
1783 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1784 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1785 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001786 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001787 }
1788
1789 cacheability = ffa_get_memory_cacheability_attr(attributes);
1790 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1791 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1792 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001793 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001794 }
1795
1796 shareability = ffa_get_memory_shareability_attr(attributes);
1797 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1798 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1799 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001800 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001801 }
1802
1803 return (struct ffa_value){.func = FFA_SUCCESS_32};
1804}
1805
1806/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001807 * Check that the given `memory_region` represents a valid memory send request
1808 * of the given `share_func` type, return the clear flag and permissions via the
1809 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001810 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001811 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001812 * not.
1813 */
J-Alves66652252022-07-06 09:49:51 +01001814struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001815 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1816 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001817 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001818{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001819 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001820 struct ffa_memory_access *receiver =
1821 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001822 uint64_t receivers_end;
1823 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001824 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001825 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001826 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001827 enum ffa_data_access data_access;
1828 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001829 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001830 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001831 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001832 memory_region->receivers_offset +
1833 memory_region->memory_access_desc_size +
1834 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001835
1836 if (fragment_length < minimum_first_fragment_length) {
1837 dlog_verbose("Fragment length %u too short (min %u).\n",
1838 (size_t)fragment_length,
1839 minimum_first_fragment_length);
1840 return ffa_error(FFA_INVALID_PARAMETERS);
1841 }
1842
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001843 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1844 "struct ffa_memory_region_constituent must be 16 bytes");
1845 if (!is_aligned(fragment_length,
1846 sizeof(struct ffa_memory_region_constituent)) ||
1847 !is_aligned(memory_share_length,
1848 sizeof(struct ffa_memory_region_constituent))) {
1849 dlog_verbose(
1850 "Fragment length %u or total length %u"
1851 " is not 16-byte aligned.\n",
1852 fragment_length, memory_share_length);
1853 return ffa_error(FFA_INVALID_PARAMETERS);
1854 }
1855
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001856 if (fragment_length > memory_share_length) {
1857 dlog_verbose(
1858 "Fragment length %u greater than total length %u.\n",
1859 (size_t)fragment_length, (size_t)memory_share_length);
1860 return ffa_error(FFA_INVALID_PARAMETERS);
1861 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001862
J-Alves95df0ef2022-12-07 10:09:48 +00001863 /* The sender must match the caller. */
1864 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1865 vm_id_is_current_world(memory_region->sender)) ||
1866 (vm_id_is_current_world(from_locked.vm->id) &&
1867 memory_region->sender != from_locked.vm->id)) {
1868 dlog_verbose("Invalid memory sender ID.\n");
1869 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001870 }
1871
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001872 if (memory_region->receiver_count <= 0) {
1873 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001874 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001875 }
1876
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001877 /*
1878 * Ensure that the composite header is within the memory bounds and
1879 * doesn't overlap the first part of the message. Cast to uint64_t
1880 * to prevent overflow.
1881 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001882 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001883 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001884 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001885 min_length = receivers_end +
1886 sizeof(struct ffa_composite_memory_region) +
1887 sizeof(struct ffa_memory_region_constituent);
1888 if (min_length > memory_share_length) {
1889 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1890 (size_t)memory_share_length, (size_t)min_length);
1891 return ffa_error(FFA_INVALID_PARAMETERS);
1892 }
1893
1894 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001895 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001896
1897 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001898 * Check that the composite memory region descriptor is after the access
1899 * descriptors, is at least 16-byte aligned, and fits in the first
1900 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001901 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001902 if ((composite_memory_region_offset < receivers_end) ||
1903 (composite_memory_region_offset % 16 != 0) ||
1904 (composite_memory_region_offset >
1905 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1906 dlog_verbose(
1907 "Invalid composite memory region descriptor offset "
1908 "%u.\n",
1909 (size_t)composite_memory_region_offset);
1910 return ffa_error(FFA_INVALID_PARAMETERS);
1911 }
1912
1913 /*
1914 * Compute the start of the constituent regions. Already checked
1915 * to be not more than fragment_length and thus not more than
1916 * memory_share_length.
1917 */
1918 constituents_start = composite_memory_region_offset +
1919 sizeof(struct ffa_composite_memory_region);
1920 constituents_length = memory_share_length - constituents_start;
1921
1922 /*
1923 * Check that the number of constituents is consistent with the length
1924 * of the constituent region.
1925 */
1926 composite = ffa_memory_region_get_composite(memory_region, 0);
1927 if ((constituents_length %
1928 sizeof(struct ffa_memory_region_constituent) !=
1929 0) ||
1930 ((constituents_length /
1931 sizeof(struct ffa_memory_region_constituent)) !=
1932 composite->constituent_count)) {
1933 dlog_verbose("Invalid length %u or composite offset %u.\n",
1934 (size_t)memory_share_length,
1935 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001936 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001937 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001938 if (fragment_length < memory_share_length &&
1939 fragment_length < HF_MAILBOX_SIZE) {
1940 dlog_warning(
1941 "Initial fragment length %d smaller than mailbox "
1942 "size.\n",
1943 fragment_length);
1944 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001945
Andrew Walbrana65a1322020-04-06 19:32:32 +01001946 /*
1947 * Clear is not allowed for memory sharing, as the sender still has
1948 * access to the memory.
1949 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001950 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1951 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001952 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001953 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001954 }
1955
1956 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001957 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001958 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001960 }
1961
J-Alves363f5722022-04-25 17:37:37 +01001962 /* Check that the permissions are valid, for each specified receiver. */
1963 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001964 struct ffa_memory_region_attributes receiver_permissions;
1965
1966 receiver = ffa_memory_region_get_receiver(memory_region, i);
1967 assert(receiver != NULL);
1968 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001969 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001970 receiver_permissions.permissions;
1971 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001972
1973 if (memory_region->sender == receiver_id) {
1974 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001975 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001976 }
Federico Recanati85090c42021-12-15 13:17:54 +01001977
J-Alves363f5722022-04-25 17:37:37 +01001978 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1979 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001980 struct ffa_memory_access *other_receiver =
1981 ffa_memory_region_get_receiver(memory_region,
1982 j);
1983 assert(other_receiver != NULL);
1984
J-Alves363f5722022-04-25 17:37:37 +01001985 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001986 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001987 dlog_verbose(
1988 "Repeated receiver(%x) in memory send "
1989 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001990 other_receiver->receiver_permissions
1991 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001992 return ffa_error(FFA_INVALID_PARAMETERS);
1993 }
1994 }
1995
1996 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001997 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01001998 dlog_verbose(
1999 "All ffa_memory_access should point to the "
2000 "same composite memory region offset.\n");
2001 return ffa_error(FFA_INVALID_PARAMETERS);
2002 }
2003
2004 data_access = ffa_get_data_access_attr(permissions);
2005 instruction_access =
2006 ffa_get_instruction_access_attr(permissions);
2007 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2008 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2009 dlog_verbose(
2010 "Reserved value for receiver permissions "
2011 "%#x.\n",
2012 permissions);
2013 return ffa_error(FFA_INVALID_PARAMETERS);
2014 }
2015 if (instruction_access !=
2016 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2017 dlog_verbose(
2018 "Invalid instruction access permissions %#x "
2019 "for sending memory.\n",
2020 permissions);
2021 return ffa_error(FFA_INVALID_PARAMETERS);
2022 }
2023 if (share_func == FFA_MEM_SHARE_32) {
2024 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2025 dlog_verbose(
2026 "Invalid data access permissions %#x "
2027 "for sharing memory.\n",
2028 permissions);
2029 return ffa_error(FFA_INVALID_PARAMETERS);
2030 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002031 /*
2032 * According to section 10.10.3 of the FF-A v1.1 EAC0
2033 * spec, NX is required for share operations (but must
2034 * not be specified by the sender) so set it in the
2035 * copy that we store, ready to be returned to the
2036 * retriever.
2037 */
2038 if (vm_id_is_current_world(receiver_id)) {
2039 ffa_set_instruction_access_attr(
2040 &permissions,
2041 FFA_INSTRUCTION_ACCESS_NX);
2042 receiver_permissions.permissions = permissions;
2043 }
J-Alves363f5722022-04-25 17:37:37 +01002044 }
2045 if (share_func == FFA_MEM_LEND_32 &&
2046 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2047 dlog_verbose(
2048 "Invalid data access permissions %#x for "
2049 "lending memory.\n",
2050 permissions);
2051 return ffa_error(FFA_INVALID_PARAMETERS);
2052 }
2053
2054 if (share_func == FFA_MEM_DONATE_32 &&
2055 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2056 dlog_verbose(
2057 "Invalid data access permissions %#x for "
2058 "donating memory.\n",
2059 permissions);
2060 return ffa_error(FFA_INVALID_PARAMETERS);
2061 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002062 }
2063
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002064 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
2065 security_state =
2066 ffa_get_memory_security_attr(memory_region->attributes);
2067 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2068 dlog_verbose(
2069 "Invalid security state for memory share operation.\n");
2070 return ffa_error(FFA_INVALID_PARAMETERS);
2071 }
2072
Federico Recanatid937f5e2021-12-20 17:38:23 +01002073 /*
J-Alves807794e2022-06-16 13:42:47 +01002074 * If a memory donate or lend with single borrower, the memory type
2075 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002076 */
J-Alves807794e2022-06-16 13:42:47 +01002077 if (share_func == FFA_MEM_DONATE_32 ||
2078 (share_func == FFA_MEM_LEND_32 &&
2079 memory_region->receiver_count == 1)) {
2080 if (ffa_get_memory_type_attr(memory_region->attributes) !=
2081 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2082 dlog_verbose(
2083 "Memory type shall not be specified by "
2084 "sender.\n");
2085 return ffa_error(FFA_INVALID_PARAMETERS);
2086 }
2087 } else {
2088 /*
2089 * Check that sender's memory attributes match Hafnium
2090 * expectations: Normal Memory, Inner shareable, Write-Back
2091 * Read-Allocate Write-Allocate Cacheable.
2092 */
2093 ret = ffa_memory_attributes_validate(memory_region->attributes);
2094 if (ret.func != FFA_SUCCESS_32) {
2095 return ret;
2096 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002097 }
2098
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002099 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002100}
2101
2102/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002103 * Gets the share state for continuing an operation to donate, lend or share
2104 * memory, and checks that it is a valid request.
2105 *
2106 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2107 * not.
2108 */
J-Alvesfdd29272022-07-19 13:16:31 +01002109struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002110 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002111 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002112 struct mpool *page_pool)
2113{
2114 struct ffa_memory_share_state *share_state;
2115 struct ffa_memory_region *memory_region;
2116
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002117 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002118
2119 /*
2120 * Look up the share state by handle and make sure that the VM ID
2121 * matches.
2122 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002123 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002124 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002125 dlog_verbose(
2126 "Invalid handle %#x for memory send continuation.\n",
2127 handle);
2128 return ffa_error(FFA_INVALID_PARAMETERS);
2129 }
2130 memory_region = share_state->memory_region;
2131
J-Alvesfdd29272022-07-19 13:16:31 +01002132 if (vm_id_is_current_world(from_vm_id) &&
2133 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002134 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2135 return ffa_error(FFA_INVALID_PARAMETERS);
2136 }
2137
2138 if (share_state->sending_complete) {
2139 dlog_verbose(
2140 "Sending of memory handle %#x is already complete.\n",
2141 handle);
2142 return ffa_error(FFA_INVALID_PARAMETERS);
2143 }
2144
2145 if (share_state->fragment_count == MAX_FRAGMENTS) {
2146 /*
2147 * Log a warning as this is a sign that MAX_FRAGMENTS should
2148 * probably be increased.
2149 */
2150 dlog_warning(
2151 "Too many fragments for memory share with handle %#x; "
2152 "only %d supported.\n",
2153 handle, MAX_FRAGMENTS);
2154 /* Free share state, as it's not possible to complete it. */
2155 share_state_free(share_states, share_state, page_pool);
2156 return ffa_error(FFA_NO_MEMORY);
2157 }
2158
2159 *share_state_ret = share_state;
2160
2161 return (struct ffa_value){.func = FFA_SUCCESS_32};
2162}
2163
2164/**
J-Alves95df0ef2022-12-07 10:09:48 +00002165 * Checks if there is at least one receiver from the other world.
2166 */
J-Alvesfdd29272022-07-19 13:16:31 +01002167bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002168 struct ffa_memory_region *memory_region)
2169{
2170 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002171 struct ffa_memory_access *receiver =
2172 ffa_memory_region_get_receiver(memory_region, i);
2173 assert(receiver != NULL);
2174 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2175
2176 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002177 return true;
2178 }
2179 }
2180 return false;
2181}
2182
2183/**
J-Alves9da280b2022-12-21 14:55:39 +00002184 * Validates a call to donate, lend or share memory in which Hafnium is the
2185 * designated allocator of the memory handle. In practice, this also means
2186 * Hafnium is responsible for managing the state structures for the transaction.
2187 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2188 * sender is an SP or there is at least one borrower that is an SP.
2189 * If Hafnium is the hypervisor, it should allocate the memory handle when
2190 * operation involves only NWd VMs.
2191 *
2192 * If validation goes well, Hafnium updates the stage-2 page tables of the
2193 * sender. Validation consists of checking if the message length and number of
2194 * memory region constituents match, and if the transition is valid for the
2195 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002196 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002197 * Assumes that the caller has already found and locked the sender VM and copied
2198 * the memory region descriptor from the sender's TX buffer to a freshly
2199 * allocated page from Hafnium's internal pool. The caller must have also
2200 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002201 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002202 * This function takes ownership of the `memory_region` passed in and will free
2203 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002204 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002205struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002207 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002208 uint32_t fragment_length, uint32_t share_func,
2209 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002210{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002211 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002212 struct share_states_locked share_states;
2213 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002214
2215 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002216 * If there is an error validating the `memory_region` then we need to
2217 * free it because we own it but we won't be storing it in a share state
2218 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002219 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002220 ret = ffa_memory_send_validate(from_locked, memory_region,
2221 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002222 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002223 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002224 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002225 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002226 }
2227
Andrew Walbrana65a1322020-04-06 19:32:32 +01002228 /* Set flag for share function, ready to be retrieved later. */
2229 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002230 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002231 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002232 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002233 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002234 case FFA_MEM_LEND_32:
2235 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002236 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002237 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002238 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002240 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002241 }
2242
Andrew Walbranca808b12020-05-15 17:22:28 +01002243 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002244 /*
2245 * Allocate a share state before updating the page table. Otherwise if
2246 * updating the page table succeeded but allocating the share state
2247 * failed then it would leave the memory in a state where nobody could
2248 * get it back.
2249 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002250 share_state = allocate_share_state(share_states, share_func,
2251 memory_region, fragment_length,
2252 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002253 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002254 dlog_verbose("Failed to allocate share state.\n");
2255 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002256 ret = ffa_error(FFA_NO_MEMORY);
2257 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002258 }
2259
Andrew Walbranca808b12020-05-15 17:22:28 +01002260 if (fragment_length == memory_share_length) {
2261 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002262 ret = ffa_memory_send_complete(
2263 from_locked, share_states, share_state, page_pool,
2264 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002265 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002266 /*
2267 * Use sender ID from 'memory_region' assuming
2268 * that at this point it has been validated:
2269 * - MBZ at virtual FF-A instance.
2270 */
J-Alves19e20cf2023-08-02 12:48:55 +01002271 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002272 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2273 ? memory_region->sender
2274 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002275 ret = (struct ffa_value){
2276 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002277 .arg1 = (uint32_t)memory_region->handle,
2278 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002279 .arg3 = fragment_length,
2280 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002281 }
2282
2283out:
2284 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002285 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002286 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002287}
2288
2289/**
J-Alves8505a8a2022-06-15 18:10:18 +01002290 * Continues an operation to donate, lend or share memory to a VM from current
2291 * world. If this is the last fragment then checks that the transition is valid
2292 * for the type of memory sending operation and updates the stage-2 page tables
2293 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002294 *
2295 * Assumes that the caller has already found and locked the sender VM and copied
2296 * the memory region descriptor from the sender's TX buffer to a freshly
2297 * allocated page from Hafnium's internal pool.
2298 *
2299 * This function takes ownership of the `fragment` passed in; it must not be
2300 * freed by the caller.
2301 */
2302struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2303 void *fragment,
2304 uint32_t fragment_length,
2305 ffa_memory_handle_t handle,
2306 struct mpool *page_pool)
2307{
2308 struct share_states_locked share_states = share_states_lock();
2309 struct ffa_memory_share_state *share_state;
2310 struct ffa_value ret;
2311 struct ffa_memory_region *memory_region;
2312
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002313 CHECK(is_aligned(fragment,
2314 alignof(struct ffa_memory_region_constituent)));
2315 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2316 0) {
2317 dlog_verbose("Fragment length %u misaligned.\n",
2318 fragment_length);
2319 ret = ffa_error(FFA_INVALID_PARAMETERS);
2320 goto out_free_fragment;
2321 }
2322
Andrew Walbranca808b12020-05-15 17:22:28 +01002323 ret = ffa_memory_send_continue_validate(share_states, handle,
2324 &share_state,
2325 from_locked.vm->id, page_pool);
2326 if (ret.func != FFA_SUCCESS_32) {
2327 goto out_free_fragment;
2328 }
2329 memory_region = share_state->memory_region;
2330
J-Alves95df0ef2022-12-07 10:09:48 +00002331 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002332 dlog_error(
2333 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002334 "other world. This should never happen, and indicates "
2335 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002336 "EL3 code.\n");
2337 ret = ffa_error(FFA_INVALID_PARAMETERS);
2338 goto out_free_fragment;
2339 }
2340
2341 /* Add this fragment. */
2342 share_state->fragments[share_state->fragment_count] = fragment;
2343 share_state->fragment_constituent_counts[share_state->fragment_count] =
2344 fragment_length / sizeof(struct ffa_memory_region_constituent);
2345 share_state->fragment_count++;
2346
2347 /* Check whether the memory send operation is now ready to complete. */
2348 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002349 ret = ffa_memory_send_complete(
2350 from_locked, share_states, share_state, page_pool,
2351 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002352 } else {
2353 ret = (struct ffa_value){
2354 .func = FFA_MEM_FRAG_RX_32,
2355 .arg1 = (uint32_t)handle,
2356 .arg2 = (uint32_t)(handle >> 32),
2357 .arg3 = share_state_next_fragment_offset(share_states,
2358 share_state)};
2359 }
2360 goto out;
2361
2362out_free_fragment:
2363 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002364
2365out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002367 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002368}
2369
Andrew Walbranca808b12020-05-15 17:22:28 +01002370/** Clean up after the receiver has finished retrieving a memory region. */
2371static void ffa_memory_retrieve_complete(
2372 struct share_states_locked share_states,
2373 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2374{
2375 if (share_state->share_func == FFA_MEM_DONATE_32) {
2376 /*
2377 * Memory that has been donated can't be relinquished,
2378 * so no need to keep the share state around.
2379 */
2380 share_state_free(share_states, share_state, page_pool);
2381 dlog_verbose("Freed share state for donate.\n");
2382 }
2383}
2384
J-Alves2d8457f2022-10-05 11:06:41 +01002385/**
2386 * Initialises the given memory region descriptor to be used for an
2387 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2388 * fragment.
2389 * The memory region descriptor is initialized according to retriever's
2390 * FF-A version.
2391 *
2392 * Returns true on success, or false if the given constituents won't all fit in
2393 * the first fragment.
2394 */
2395static bool ffa_retrieved_memory_region_init(
2396 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002397 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002398 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002399 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002400 struct ffa_memory_access *receivers, size_t receiver_count,
2401 uint32_t memory_access_desc_size, uint32_t page_count,
2402 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002403 const struct ffa_memory_region_constituent constituents[],
2404 uint32_t fragment_constituent_count, uint32_t *total_length,
2405 uint32_t *fragment_length)
2406{
2407 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002408 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002409 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002410 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002411
2412 assert(response != NULL);
2413
2414 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2415 struct ffa_memory_region_v1_0 *retrieve_response =
2416 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002417 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002418
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002419 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2420 attributes, flags, handle, 0,
2421 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002422
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002423 receiver = (struct ffa_memory_access_v1_0 *)
2424 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002425 receiver_count = retrieve_response->receiver_count;
2426
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002427 for (uint32_t i = 0; i < receiver_count; i++) {
2428 ffa_id_t receiver_id =
2429 receivers[i].receiver_permissions.receiver;
2430 ffa_memory_receiver_flags_t recv_flags =
2431 receivers[i].receiver_permissions.flags;
2432
2433 /*
2434 * Initialized here as in memory retrieve responses we
2435 * currently expect one borrower to be specified.
2436 */
2437 ffa_memory_access_init_v1_0(
2438 receiver, receiver_id,
2439 ffa_get_data_access_attr(permissions),
2440 ffa_get_instruction_access_attr(permissions),
2441 recv_flags);
2442 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002443
2444 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002445 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002446 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2447 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002448
2449 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2450 retrieve_response, 0);
2451 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002452 struct ffa_memory_region *retrieve_response =
2453 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002454 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002455
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002456 ffa_memory_region_init_header(
2457 retrieve_response, sender, attributes, flags, handle, 0,
2458 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002459
2460 /*
2461 * Note that `sizeof(struct_ffa_memory_region)` and
2462 * `sizeof(struct ffa_memory_access)` must both be multiples of
2463 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2464 * guaranteed that the offset we calculate here is aligned to a
2465 * 64-bit boundary and so 64-bit values can be copied without
2466 * alignment faults.
2467 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002468 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002469 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002470 (uint32_t)(receiver_count *
2471 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002472
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002473 retrieve_response_receivers =
2474 ffa_memory_region_get_receiver(retrieve_response, 0);
2475 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002476
2477 /*
2478 * Initialized here as in memory retrieve responses we currently
2479 * expect one borrower to be specified.
2480 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002481 memcpy_s(retrieve_response_receivers,
2482 sizeof(struct ffa_memory_access) * receiver_count,
2483 receivers,
2484 sizeof(struct ffa_memory_access) * receiver_count);
2485
2486 retrieve_response_receivers->composite_memory_region_offset =
2487 composite_offset;
2488
J-Alves2d8457f2022-10-05 11:06:41 +01002489 composite_memory_region =
2490 ffa_memory_region_get_composite(retrieve_response, 0);
2491 }
2492
J-Alves2d8457f2022-10-05 11:06:41 +01002493 assert(composite_memory_region != NULL);
2494
J-Alves2d8457f2022-10-05 11:06:41 +01002495 composite_memory_region->page_count = page_count;
2496 composite_memory_region->constituent_count = total_constituent_count;
2497 composite_memory_region->reserved_0 = 0;
2498
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002499 constituents_offset =
2500 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002501 if (constituents_offset +
2502 fragment_constituent_count *
2503 sizeof(struct ffa_memory_region_constituent) >
2504 response_max_size) {
2505 return false;
2506 }
2507
2508 for (i = 0; i < fragment_constituent_count; ++i) {
2509 composite_memory_region->constituents[i] = constituents[i];
2510 }
2511
2512 if (total_length != NULL) {
2513 *total_length =
2514 constituents_offset +
2515 composite_memory_region->constituent_count *
2516 sizeof(struct ffa_memory_region_constituent);
2517 }
2518 if (fragment_length != NULL) {
2519 *fragment_length =
2520 constituents_offset +
2521 fragment_constituent_count *
2522 sizeof(struct ffa_memory_region_constituent);
2523 }
2524
2525 return true;
2526}
2527
J-Alves96de29f2022-04-26 16:05:24 +01002528/**
2529 * Validates the retrieved permissions against those specified by the lender
2530 * of memory share operation. Optionally can help set the permissions to be used
2531 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002532 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2533 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2534 * specification for each ABI.
2535 * - FFA_DENIED -> if the permissions specified by the retriever are not
2536 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002537 */
J-Alvesdcad8992023-09-15 14:10:35 +01002538static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2539 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002540 enum ffa_data_access requested_data_access,
2541 enum ffa_instruction_access sent_instruction_access,
2542 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002543 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002544{
2545 switch (sent_data_access) {
2546 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2547 case FFA_DATA_ACCESS_RW:
2548 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2549 requested_data_access == FFA_DATA_ACCESS_RW) {
2550 if (permissions != NULL) {
2551 ffa_set_data_access_attr(permissions,
2552 FFA_DATA_ACCESS_RW);
2553 }
2554 break;
2555 }
2556 /* Intentional fall-through. */
2557 case FFA_DATA_ACCESS_RO:
2558 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2559 requested_data_access == FFA_DATA_ACCESS_RO) {
2560 if (permissions != NULL) {
2561 ffa_set_data_access_attr(permissions,
2562 FFA_DATA_ACCESS_RO);
2563 }
2564 break;
2565 }
2566 dlog_verbose(
2567 "Invalid data access requested; sender specified "
2568 "permissions %#x but receiver requested %#x.\n",
2569 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002570 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002571 case FFA_DATA_ACCESS_RESERVED:
2572 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2573 "checked before this point.");
2574 }
2575
J-Alvesdcad8992023-09-15 14:10:35 +01002576 /*
2577 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2578 * or FFA_MEMORY_DONATE the retriever should have specifed the
2579 * instruction permissions it wishes to receive.
2580 */
2581 switch (share_func) {
2582 case FFA_MEM_SHARE_32:
2583 if (requested_instruction_access !=
2584 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2585 dlog_verbose(
2586 "%s: for share instruction permissions must "
2587 "NOT be specified.\n",
2588 __func__);
2589 return ffa_error(FFA_INVALID_PARAMETERS);
2590 }
2591 break;
2592 case FFA_MEM_LEND_32:
2593 /*
2594 * For operations with multiple borrowers only permit XN
2595 * permissions, and both Sender and borrower should have used
2596 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2597 */
2598 if (multiple_borrowers) {
2599 if (requested_instruction_access !=
2600 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2601 dlog_verbose(
2602 "%s: lend/share/donate with multiple "
2603 "borrowers "
2604 "instruction permissions must NOT be "
2605 "specified.\n",
2606 __func__);
2607 return ffa_error(FFA_INVALID_PARAMETERS);
2608 }
2609 break;
2610 }
2611 /* Fall through if the operation targets a single borrower. */
2612 case FFA_MEM_DONATE_32:
2613 if (!multiple_borrowers &&
2614 requested_instruction_access ==
2615 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2616 dlog_verbose(
2617 "%s: for lend/donate with single borrower "
2618 "instruction permissions must be speficified "
2619 "by borrower\n",
2620 __func__);
2621 return ffa_error(FFA_INVALID_PARAMETERS);
2622 }
2623 break;
2624 default:
2625 panic("%s: Wrong func id provided.\n", __func__);
2626 }
2627
J-Alves96de29f2022-04-26 16:05:24 +01002628 switch (sent_instruction_access) {
2629 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2630 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002631 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002632 if (permissions != NULL) {
2633 ffa_set_instruction_access_attr(
2634 permissions, FFA_INSTRUCTION_ACCESS_X);
2635 }
2636 break;
2637 }
J-Alvesdcad8992023-09-15 14:10:35 +01002638 /*
2639 * Fall through if requested permissions are less
2640 * permissive than those provided by the sender.
2641 */
J-Alves96de29f2022-04-26 16:05:24 +01002642 case FFA_INSTRUCTION_ACCESS_NX:
2643 if (requested_instruction_access ==
2644 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2645 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2646 if (permissions != NULL) {
2647 ffa_set_instruction_access_attr(
2648 permissions, FFA_INSTRUCTION_ACCESS_NX);
2649 }
2650 break;
2651 }
2652 dlog_verbose(
2653 "Invalid instruction access requested; sender "
2654 "specified permissions %#x but receiver requested "
2655 "%#x.\n",
2656 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002657 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002658 case FFA_INSTRUCTION_ACCESS_RESERVED:
2659 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2660 "be checked before this point.");
2661 }
2662
J-Alvesdcad8992023-09-15 14:10:35 +01002663 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002664}
2665
2666/**
2667 * Validate the receivers' permissions in the retrieve request against those
2668 * specified by the lender.
2669 * In the `permissions` argument returns the permissions to set at S2 for the
2670 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002671 * The function looks into the flag to bypass multiple borrower checks:
2672 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2673 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2674 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2675 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002676 */
2677static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2678 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002679 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002680 ffa_memory_access_permissions_t *permissions,
2681 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002682{
2683 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002684 bool bypass_multi_receiver_check =
2685 (retrieve_request->flags &
2686 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002687 const uint32_t region_receiver_count = memory_region->receiver_count;
2688 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002689
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002690 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002691 assert(permissions != NULL);
2692
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002693 *permissions = 0;
2694
J-Alves3456e032023-07-20 12:20:05 +01002695 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002696 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002697 dlog_verbose(
2698 "Retrieve request should contain same list of "
2699 "borrowers, as specified by the lender.\n");
2700 return ffa_error(FFA_INVALID_PARAMETERS);
2701 }
2702 } else {
2703 if (retrieve_request->receiver_count != 1) {
2704 dlog_verbose(
2705 "Set bypass multiple borrower check, receiver "
2706 "list must be sized 1 (%x)\n",
2707 memory_region->receiver_count);
2708 return ffa_error(FFA_INVALID_PARAMETERS);
2709 }
J-Alves96de29f2022-04-26 16:05:24 +01002710 }
2711
2712 retrieve_receiver_index = retrieve_request->receiver_count;
2713
J-Alves96de29f2022-04-26 16:05:24 +01002714 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2715 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002716 struct ffa_memory_access *retrieve_request_receiver =
2717 ffa_memory_region_get_receiver(retrieve_request, i);
2718 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002719 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002720 retrieve_request_receiver->receiver_permissions
2721 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002722 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002723 retrieve_request_receiver->receiver_permissions
2724 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002725 struct ffa_memory_access *receiver;
2726 uint32_t mem_region_receiver_index;
2727 bool permissions_RO;
2728 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002729 bool found_to_id = current_receiver_id == to_vm_id;
2730
J-Alves3456e032023-07-20 12:20:05 +01002731 if (bypass_multi_receiver_check && !found_to_id) {
2732 dlog_verbose(
2733 "Bypass multiple borrower check for id %x.\n",
2734 current_receiver_id);
2735 continue;
2736 }
2737
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002738 if (retrieve_request_receiver->composite_memory_region_offset !=
2739 0U) {
2740 dlog_verbose(
2741 "Retriever specified address ranges not "
2742 "supported (got offset %d).\n",
2743 retrieve_request_receiver
2744 ->composite_memory_region_offset);
2745 return ffa_error(FFA_INVALID_PARAMETERS);
2746 }
2747
J-Alves96de29f2022-04-26 16:05:24 +01002748 /*
2749 * Find the current receiver in the transaction descriptor from
2750 * sender.
2751 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002752 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002753 ffa_memory_region_get_receiver_index(
2754 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002755
2756 if (mem_region_receiver_index ==
2757 memory_region->receiver_count) {
2758 dlog_verbose("%s: receiver %x not found\n", __func__,
2759 current_receiver_id);
2760 return ffa_error(FFA_DENIED);
2761 }
2762
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002763 receiver = ffa_memory_region_get_receiver(
2764 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002765 assert(receiver != NULL);
2766
2767 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002768
2769 if (found_to_id) {
2770 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002771
2772 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002773 }
2774
2775 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002776 * Check if retrieve request memory access list is valid:
2777 * - The retrieve request complies with the specification.
2778 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002779 */
J-Alvesdcad8992023-09-15 14:10:35 +01002780 ret = ffa_memory_retrieve_is_memory_access_valid(
2781 func_id, ffa_get_data_access_attr(sent_permissions),
2782 ffa_get_data_access_attr(requested_permissions),
2783 ffa_get_instruction_access_attr(sent_permissions),
2784 ffa_get_instruction_access_attr(requested_permissions),
2785 found_to_id ? permissions : NULL,
2786 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002787
J-Alvesdcad8992023-09-15 14:10:35 +01002788 if (ret.func != FFA_SUCCESS_32) {
2789 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002790 }
2791
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002792 permissions_RO = (ffa_get_data_access_attr(*permissions) ==
2793 FFA_DATA_ACCESS_RO);
2794 clear_memory_flags = (retrieve_request->flags &
2795 FFA_MEMORY_REGION_FLAG_CLEAR) != 0U;
2796
J-Alves96de29f2022-04-26 16:05:24 +01002797 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002798 * Can't request PM to clear memory if only provided
2799 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002800 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002801 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002802 dlog_verbose(
2803 "Receiver has RO permissions can not request "
2804 "clear.\n");
2805 return ffa_error(FFA_DENIED);
2806 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002807
2808 /*
2809 * Check the impdef in the retrieve_request matches the value in
2810 * the original memory send.
2811 */
2812 if (ffa_version_from_memory_access_desc_size(
2813 memory_region->memory_access_desc_size) >=
2814 MAKE_FFA_VERSION(1, 2) &&
2815 ffa_version_from_memory_access_desc_size(
2816 retrieve_request->memory_access_desc_size) >=
2817 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002818 if (receiver->impdef.val[0] !=
2819 retrieve_request_receiver->impdef.val[0] ||
2820 receiver->impdef.val[1] !=
2821 retrieve_request_receiver->impdef.val[1]) {
2822 dlog_verbose(
2823 "Impdef value in memory send does not "
2824 "match retrieve request value "
2825 "send value %#x %#x retrieve request "
2826 "value %#x %#x\n",
2827 receiver->impdef.val[0],
2828 receiver->impdef.val[1],
2829 retrieve_request_receiver->impdef
2830 .val[0],
2831 retrieve_request_receiver->impdef
2832 .val[1]);
2833 return ffa_error(FFA_INVALID_PARAMETERS);
2834 }
2835 }
J-Alves96de29f2022-04-26 16:05:24 +01002836 }
2837
2838 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2839 dlog_verbose(
2840 "Retrieve request does not contain caller's (%x) "
2841 "permissions\n",
2842 to_vm_id);
2843 return ffa_error(FFA_INVALID_PARAMETERS);
2844 }
2845
2846 return (struct ffa_value){.func = FFA_SUCCESS_32};
2847}
2848
J-Alvesa9cd7e32022-07-01 13:49:33 +01002849/*
2850 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2851 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2852 * of a pending memory sharing operation whose allocator is the SPM, for
2853 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2854 * the memory region descriptor of the retrieve request must be zeroed with the
2855 * exception of the sender ID and handle.
2856 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002857bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2858 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002859{
2860 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2861 request->attributes == 0U && request->flags == 0U &&
2862 request->tag == 0U && request->receiver_count == 0U &&
2863 plat_ffa_memory_handle_allocated_by_current_world(
2864 request->handle);
2865}
2866
2867/*
2868 * Helper to reset count of fragments retrieved by the hypervisor.
2869 */
2870static void ffa_memory_retrieve_complete_from_hyp(
2871 struct ffa_memory_share_state *share_state)
2872{
2873 if (share_state->hypervisor_fragment_count ==
2874 share_state->fragment_count) {
2875 share_state->hypervisor_fragment_count = 0;
2876 }
2877}
2878
J-Alves089004f2022-07-13 14:25:44 +01002879/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002880 * Prepares the return of the ffa_value for the memory retrieve response.
2881 */
2882static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2883 uint32_t fragment_length)
2884{
2885 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2886 .arg1 = total_length,
2887 .arg2 = fragment_length};
2888}
2889
2890/**
J-Alves089004f2022-07-13 14:25:44 +01002891 * Validate that the memory region descriptor provided by the borrower on
2892 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2893 * memory sharing call.
2894 */
2895static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002896 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2897 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002898 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2899 uint32_t share_func)
2900{
2901 ffa_memory_region_flags_t transaction_type =
2902 retrieve_request->flags &
2903 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002904 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002905 const uint64_t memory_access_desc_size =
2906 retrieve_request->memory_access_desc_size;
2907 const uint32_t expected_retrieve_request_length =
2908 retrieve_request->receivers_offset +
2909 (uint32_t)(retrieve_request->receiver_count *
2910 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002911
2912 assert(retrieve_request != NULL);
2913 assert(memory_region != NULL);
2914 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01002915
J-Alves4f0d9c12024-01-17 17:23:11 +00002916 if (retrieve_request_length != expected_retrieve_request_length) {
2917 dlog_verbose(
2918 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2919 "but was %d.\n",
2920 expected_retrieve_request_length,
2921 retrieve_request_length);
2922 return ffa_error(FFA_INVALID_PARAMETERS);
2923 }
2924
2925 if (retrieve_request->sender != memory_region->sender) {
2926 dlog_verbose(
2927 "Memory with handle %#x not fully sent, can't "
2928 "retrieve.\n",
2929 memory_region->handle);
2930 return ffa_error(FFA_DENIED);
2931 }
2932
2933 /*
2934 * The SPMC can only process retrieve requests to memory share
2935 * operations with one borrower from the other world. It can't
2936 * determine the ID of the NWd VM that invoked the retrieve
2937 * request interface call. It relies on the hypervisor to
2938 * validate the caller's ID against that provided in the
2939 * `receivers` list of the retrieve response.
2940 * In case there is only one borrower from the NWd in the
2941 * transaction descriptor, record that in the `receiver_id` for
2942 * later use, and validate in the retrieve request message.
2943 * This limitation is due to the fact SPMC can't determine the
2944 * index in the memory share structures state to update.
2945 */
2946 if (to_id == HF_HYPERVISOR_VM_ID) {
2947 uint32_t other_world_count = 0;
2948
2949 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2950 struct ffa_memory_access *receiver =
2951 ffa_memory_region_get_receiver(retrieve_request,
2952 0);
2953 assert(receiver != NULL);
2954
2955 to_id = receiver->receiver_permissions.receiver;
2956
2957 if (!vm_id_is_current_world(to_id)) {
2958 other_world_count++;
2959 }
2960 }
2961
2962 if (other_world_count > 1) {
2963 dlog_verbose(
2964 "Support one receiver from the other "
2965 "world.\n");
2966 return ffa_error(FFA_NOT_SUPPORTED);
2967 }
2968 }
J-Alves089004f2022-07-13 14:25:44 +01002969 /*
2970 * Check that the transaction type expected by the receiver is
2971 * correct, if it has been specified.
2972 */
2973 if (transaction_type !=
2974 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2975 transaction_type != (memory_region->flags &
2976 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2977 dlog_verbose(
2978 "Incorrect transaction type %#x for "
2979 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2980 transaction_type,
2981 memory_region->flags &
2982 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2983 retrieve_request->handle);
2984 return ffa_error(FFA_INVALID_PARAMETERS);
2985 }
2986
2987 if (retrieve_request->tag != memory_region->tag) {
2988 dlog_verbose(
2989 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2990 "%d for handle %#x.\n",
2991 retrieve_request->tag, memory_region->tag,
2992 retrieve_request->handle);
2993 return ffa_error(FFA_INVALID_PARAMETERS);
2994 }
2995
J-Alves4f0d9c12024-01-17 17:23:11 +00002996 *receiver_index =
2997 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01002998
2999 if (*receiver_index == memory_region->receiver_count) {
3000 dlog_verbose(
3001 "Incorrect receiver VM ID %d for "
3002 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003003 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003004 return ffa_error(FFA_INVALID_PARAMETERS);
3005 }
3006
3007 if ((retrieve_request->flags &
3008 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3009 dlog_verbose(
3010 "Retriever specified 'address range alignment 'hint' "
3011 "not supported.\n");
3012 return ffa_error(FFA_INVALID_PARAMETERS);
3013 }
3014 if ((retrieve_request->flags &
3015 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3016 dlog_verbose(
3017 "Bits 8-5 must be zero in memory region's flags "
3018 "(address range alignment hint not supported).\n");
3019 return ffa_error(FFA_INVALID_PARAMETERS);
3020 }
3021
3022 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3023 dlog_verbose(
3024 "Bits 31-10 must be zero in memory region's flags.\n");
3025 return ffa_error(FFA_INVALID_PARAMETERS);
3026 }
3027
3028 if (share_func == FFA_MEM_SHARE_32 &&
3029 (retrieve_request->flags &
3030 (FFA_MEMORY_REGION_FLAG_CLEAR |
3031 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3032 dlog_verbose(
3033 "Memory Share operation can't clean after relinquish "
3034 "memory region.\n");
3035 return ffa_error(FFA_INVALID_PARAMETERS);
3036 }
3037
3038 /*
3039 * If the borrower needs the memory to be cleared before mapping
3040 * to its address space, the sender should have set the flag
3041 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3042 * FFA_DENIED.
3043 */
3044 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3045 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3046 dlog_verbose(
3047 "Borrower needs memory cleared. Sender needs to set "
3048 "flag for clearing memory.\n");
3049 return ffa_error(FFA_DENIED);
3050 }
3051
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003052 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
3053 security_state =
3054 ffa_get_memory_security_attr(retrieve_request->attributes);
3055 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3056 dlog_verbose(
3057 "Invalid security state for memory retrieve request "
3058 "operation.\n");
3059 return ffa_error(FFA_INVALID_PARAMETERS);
3060 }
3061
J-Alves089004f2022-07-13 14:25:44 +01003062 /*
3063 * If memory type is not specified, bypass validation of memory
3064 * attributes in the retrieve request. The retriever is expecting to
3065 * obtain this information from the SPMC.
3066 */
3067 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
3068 FFA_MEMORY_NOT_SPECIFIED_MEM) {
3069 return (struct ffa_value){.func = FFA_SUCCESS_32};
3070 }
3071
3072 /*
3073 * Ensure receiver's attributes are compatible with how
3074 * Hafnium maps memory: Normal Memory, Inner shareable,
3075 * Write-Back Read-Allocate Write-Allocate Cacheable.
3076 */
3077 return ffa_memory_attributes_validate(retrieve_request->attributes);
3078}
3079
J-Alves4f0d9c12024-01-17 17:23:11 +00003080static struct ffa_value ffa_partition_retrieve_request(
3081 struct share_states_locked share_states,
3082 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3083 struct ffa_memory_region *retrieve_request,
3084 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003085{
J-Alvesa9cd7e32022-07-01 13:49:33 +01003086 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003087 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003088 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003089 struct ffa_composite_memory_region *composite;
3090 uint32_t total_length;
3091 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003092 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003093 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003094 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003095 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003096 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003097 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003098 ffa_memory_handle_t handle = retrieve_request->handle;
J-Alves460d36c2023-10-12 17:02:15 +01003099 ffa_memory_attributes_t attributes = 0;
3100 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003101 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003102
J-Alves96de29f2022-04-26 16:05:24 +01003103 if (!share_state->sending_complete) {
3104 dlog_verbose(
3105 "Memory with handle %#x not fully sent, can't "
3106 "retrieve.\n",
3107 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003108 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003109 }
3110
J-Alves4f0d9c12024-01-17 17:23:11 +00003111 /*
3112 * Validate retrieve request, according to what was sent by the
3113 * sender. Function will output the `receiver_index` from the
3114 * provided memory region.
3115 */
3116 ret = ffa_memory_retrieve_validate(
3117 receiver_id, retrieve_request, retrieve_request_length,
3118 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003119
J-Alves4f0d9c12024-01-17 17:23:11 +00003120 if (ret.func != FFA_SUCCESS_32) {
3121 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003122 }
J-Alves96de29f2022-04-26 16:05:24 +01003123
J-Alves4f0d9c12024-01-17 17:23:11 +00003124 /*
3125 * Validate the requested permissions against the sent
3126 * permissions.
3127 * Outputs the permissions to give to retriever at S2
3128 * PTs.
3129 */
3130 ret = ffa_memory_retrieve_validate_memory_access_list(
3131 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003132 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003133 if (ret.func != FFA_SUCCESS_32) {
3134 return ret;
3135 }
3136
3137 memory_to_mode = ffa_memory_permissions_to_mode(
3138 permissions, share_state->sender_orig_mode);
3139
3140 ret = ffa_retrieve_check_update(
3141 to_locked, share_state->fragments,
3142 share_state->fragment_constituent_counts,
3143 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003144 share_state->share_func, false, page_pool, &retrieve_mode,
3145 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003146
3147 if (ret.func != FFA_SUCCESS_32) {
3148 return ret;
3149 }
3150
3151 share_state->retrieved_fragment_count[receiver_index] = 1;
3152
3153 is_retrieve_complete =
3154 share_state->retrieved_fragment_count[receiver_index] ==
3155 share_state->fragment_count;
3156
J-Alvesb5084cf2022-07-06 14:20:12 +01003157 /* VMs acquire the RX buffer from SPMC. */
3158 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3159
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003160 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003161 * Copy response to RX buffer of caller and deliver the message.
3162 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003163 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003164 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003165
Andrew Walbranca808b12020-05-15 17:22:28 +01003166 /*
J-Alves460d36c2023-10-12 17:02:15 +01003167 * Set the security state in the memory retrieve response attributes
3168 * if specified by the target mode.
3169 */
3170 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3171 retrieve_mode);
3172
3173 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003174 * Constituents which we received in the first fragment should
3175 * always fit in the first fragment we are sending, because the
3176 * header is the same size in both cases and we have a fixed
3177 * message buffer size. So `ffa_retrieved_memory_region_init`
3178 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003179 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003180
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003181 /* Provide the permissions that had been provided. */
3182 receiver->receiver_permissions.permissions = permissions;
3183
3184 /*
3185 * Prepare the memory region descriptor for the retrieve response.
3186 * Provide the pointer to the receiver tracked in the share state
3187 * strucutures.
3188 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003189 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003190 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003191 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003192 memory_region->flags, handle, permissions, receiver, 1,
3193 memory_access_desc_size, composite->page_count,
3194 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003195 share_state->fragment_constituent_counts[0], &total_length,
3196 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003197
J-Alves4f0d9c12024-01-17 17:23:11 +00003198 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003199 ffa_memory_retrieve_complete(share_states, share_state,
3200 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003201 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003202
3203 return ffa_memory_retrieve_resp(total_length, fragment_length);
3204}
3205
3206static struct ffa_value ffa_hypervisor_retrieve_request(
3207 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3208 struct ffa_memory_region *retrieve_request)
3209{
3210 struct ffa_value ret;
3211 struct ffa_composite_memory_region *composite;
3212 uint32_t total_length;
3213 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003214 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003215 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003216 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003217 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003218 ffa_memory_handle_t handle = retrieve_request->handle;
3219
J-Alves4f0d9c12024-01-17 17:23:11 +00003220 memory_region = share_state->memory_region;
3221
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003222 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3223
J-Alves7b6ab612024-01-24 09:54:54 +00003224 switch (to_locked.vm->ffa_version) {
3225 case MAKE_FFA_VERSION(1, 2):
3226 memory_access_desc_size = sizeof(struct ffa_memory_access);
3227 break;
3228 case MAKE_FFA_VERSION(1, 0):
3229 case MAKE_FFA_VERSION(1, 1):
3230 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3231 break;
3232 default:
3233 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3234 }
3235
J-Alves4f0d9c12024-01-17 17:23:11 +00003236 if (share_state->hypervisor_fragment_count != 0U) {
3237 dlog_verbose(
3238 "Memory with handle %#x already retrieved by "
3239 "the hypervisor.\n",
3240 handle);
3241 return ffa_error(FFA_DENIED);
3242 }
3243
3244 share_state->hypervisor_fragment_count = 1;
3245
3246 ffa_memory_retrieve_complete_from_hyp(share_state);
3247
3248 /* VMs acquire the RX buffer from SPMC. */
3249 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3250
3251 /*
3252 * Copy response to RX buffer of caller and deliver the message.
3253 * This must be done before the share_state is (possibly) freed.
3254 */
3255 composite = ffa_memory_region_get_composite(memory_region, 0);
3256
3257 /*
3258 * Constituents which we received in the first fragment should
3259 * always fit in the first fragment we are sending, because the
3260 * header is the same size in both cases and we have a fixed
3261 * message buffer size. So `ffa_retrieved_memory_region_init`
3262 * should never fail.
3263 */
3264
3265 /*
3266 * Set the security state in the memory retrieve response attributes
3267 * if specified by the target mode.
3268 */
3269 attributes = plat_ffa_memory_security_mode(
3270 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003271
3272 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3273
J-Alves4f0d9c12024-01-17 17:23:11 +00003274 CHECK(ffa_retrieved_memory_region_init(
3275 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3276 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003277 memory_region->flags, handle,
3278 receiver->receiver_permissions.permissions, receiver,
3279 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003280 composite->page_count, composite->constituent_count,
3281 share_state->fragments[0],
3282 share_state->fragment_constituent_counts[0], &total_length,
3283 &fragment_length));
3284
3285 return ffa_memory_retrieve_resp(total_length, fragment_length);
3286}
3287
3288struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3289 struct ffa_memory_region *retrieve_request,
3290 uint32_t retrieve_request_length,
3291 struct mpool *page_pool)
3292{
3293 ffa_memory_handle_t handle = retrieve_request->handle;
3294 struct share_states_locked share_states;
3295 struct ffa_memory_share_state *share_state;
3296 struct ffa_value ret;
3297
3298 dump_share_states();
3299
3300 share_states = share_states_lock();
3301 share_state = get_share_state(share_states, handle);
3302 if (share_state == NULL) {
3303 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3304 handle);
3305 ret = ffa_error(FFA_INVALID_PARAMETERS);
3306 goto out;
3307 }
3308
3309 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3310 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3311 retrieve_request);
3312 } else {
3313 ret = ffa_partition_retrieve_request(
3314 share_states, share_state, to_locked, retrieve_request,
3315 retrieve_request_length, page_pool);
3316 }
3317
3318 /* Track use of the RX buffer if the handling has succeeded. */
3319 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3320 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3321 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3322 }
3323
Andrew Walbranca808b12020-05-15 17:22:28 +01003324out:
3325 share_states_unlock(&share_states);
3326 dump_share_states();
3327 return ret;
3328}
3329
J-Alves5da37d92022-10-24 16:33:48 +01003330/**
3331 * Determine expected fragment offset according to the FF-A version of
3332 * the caller.
3333 */
3334static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3335 struct ffa_memory_region *memory_region,
3336 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3337{
3338 uint32_t expected_fragment_offset;
3339 uint32_t composite_constituents_offset;
3340
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003341 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003342 /*
3343 * Hafnium operates memory regions in FF-A v1.1 format, so we
3344 * can retrieve the constituents offset from descriptor.
3345 */
3346 composite_constituents_offset =
3347 ffa_composite_constituent_offset(memory_region, 0);
3348 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3349 /*
3350 * If retriever is FF-A v1.0, determine the composite offset
3351 * as it is expected to have been configured in the
3352 * retrieve response.
3353 */
3354 composite_constituents_offset =
3355 sizeof(struct ffa_memory_region_v1_0) +
3356 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003357 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003358 sizeof(struct ffa_composite_memory_region);
3359 } else {
3360 panic("%s received an invalid FF-A version.\n", __func__);
3361 }
3362
3363 expected_fragment_offset =
3364 composite_constituents_offset +
3365 retrieved_constituents_count *
3366 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003367 (uint32_t)(memory_region->memory_access_desc_size *
3368 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003369
3370 return expected_fragment_offset;
3371}
3372
Andrew Walbranca808b12020-05-15 17:22:28 +01003373struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3374 ffa_memory_handle_t handle,
3375 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003376 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003377 struct mpool *page_pool)
3378{
3379 struct ffa_memory_region *memory_region;
3380 struct share_states_locked share_states;
3381 struct ffa_memory_share_state *share_state;
3382 struct ffa_value ret;
3383 uint32_t fragment_index;
3384 uint32_t retrieved_constituents_count;
3385 uint32_t i;
3386 uint32_t expected_fragment_offset;
3387 uint32_t remaining_constituent_count;
3388 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003389 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003390 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003391
3392 dump_share_states();
3393
3394 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003395 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003396 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003397 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3398 handle);
3399 ret = ffa_error(FFA_INVALID_PARAMETERS);
3400 goto out;
3401 }
3402
3403 memory_region = share_state->memory_region;
3404 CHECK(memory_region != NULL);
3405
Andrew Walbranca808b12020-05-15 17:22:28 +01003406 if (!share_state->sending_complete) {
3407 dlog_verbose(
3408 "Memory with handle %#x not fully sent, can't "
3409 "retrieve.\n",
3410 handle);
3411 ret = ffa_error(FFA_INVALID_PARAMETERS);
3412 goto out;
3413 }
3414
J-Alves59ed0042022-07-28 18:26:41 +01003415 /*
3416 * If retrieve request from the hypervisor has been initiated in the
3417 * given share_state, continue it, else assume it is a continuation of
3418 * retrieve request from a NWd VM.
3419 */
3420 continue_ffa_hyp_mem_retrieve_req =
3421 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3422 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003423 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003424
J-Alves59ed0042022-07-28 18:26:41 +01003425 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003426 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003427 memory_region, to_locked.vm->id);
3428
3429 if (receiver_index == memory_region->receiver_count) {
3430 dlog_verbose(
3431 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3432 "borrower to memory sharing transaction (%x)\n",
3433 to_locked.vm->id, handle);
3434 ret = ffa_error(FFA_INVALID_PARAMETERS);
3435 goto out;
3436 }
3437
3438 if (share_state->retrieved_fragment_count[receiver_index] ==
3439 0 ||
3440 share_state->retrieved_fragment_count[receiver_index] >=
3441 share_state->fragment_count) {
3442 dlog_verbose(
3443 "Retrieval of memory with handle %#x not yet "
3444 "started or already completed (%d/%d fragments "
3445 "retrieved).\n",
3446 handle,
3447 share_state->retrieved_fragment_count
3448 [receiver_index],
3449 share_state->fragment_count);
3450 ret = ffa_error(FFA_INVALID_PARAMETERS);
3451 goto out;
3452 }
3453
3454 fragment_index =
3455 share_state->retrieved_fragment_count[receiver_index];
3456 } else {
3457 if (share_state->hypervisor_fragment_count == 0 ||
3458 share_state->hypervisor_fragment_count >=
3459 share_state->fragment_count) {
3460 dlog_verbose(
3461 "Retrieve of memory with handle %x not "
3462 "started from hypervisor.\n",
3463 handle);
3464 ret = ffa_error(FFA_INVALID_PARAMETERS);
3465 goto out;
3466 }
3467
3468 if (memory_region->sender != sender_vm_id) {
3469 dlog_verbose(
3470 "Sender ID (%x) is not as expected for memory "
3471 "handle %x\n",
3472 sender_vm_id, handle);
3473 ret = ffa_error(FFA_INVALID_PARAMETERS);
3474 goto out;
3475 }
3476
3477 fragment_index = share_state->hypervisor_fragment_count;
3478
3479 receiver_index = 0;
3480 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003481
3482 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003483 * Check that the given fragment offset is correct by counting
3484 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003485 */
3486 retrieved_constituents_count = 0;
3487 for (i = 0; i < fragment_index; ++i) {
3488 retrieved_constituents_count +=
3489 share_state->fragment_constituent_counts[i];
3490 }
J-Alvesc7484f12022-05-13 12:41:14 +01003491
3492 CHECK(memory_region->receiver_count > 0);
3493
Andrew Walbranca808b12020-05-15 17:22:28 +01003494 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003495 ffa_memory_retrieve_expected_offset_per_ffa_version(
3496 memory_region, retrieved_constituents_count,
3497 to_locked.vm->ffa_version);
3498
Andrew Walbranca808b12020-05-15 17:22:28 +01003499 if (fragment_offset != expected_fragment_offset) {
3500 dlog_verbose("Fragment offset was %d but expected %d.\n",
3501 fragment_offset, expected_fragment_offset);
3502 ret = ffa_error(FFA_INVALID_PARAMETERS);
3503 goto out;
3504 }
3505
J-Alves4f0d9c12024-01-17 17:23:11 +00003506 /*
3507 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3508 * is currently ownder by the SPMC.
3509 */
3510 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003511
Andrew Walbranca808b12020-05-15 17:22:28 +01003512 remaining_constituent_count = ffa_memory_fragment_init(
3513 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3514 share_state->fragments[fragment_index],
3515 share_state->fragment_constituent_counts[fragment_index],
3516 &fragment_length);
3517 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003518
Andrew Walbranca808b12020-05-15 17:22:28 +01003519 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003520 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003521
J-Alves59ed0042022-07-28 18:26:41 +01003522 if (!continue_ffa_hyp_mem_retrieve_req) {
3523 share_state->retrieved_fragment_count[receiver_index]++;
3524 if (share_state->retrieved_fragment_count[receiver_index] ==
3525 share_state->fragment_count) {
3526 ffa_memory_retrieve_complete(share_states, share_state,
3527 page_pool);
3528 }
3529 } else {
3530 share_state->hypervisor_fragment_count++;
3531
3532 ffa_memory_retrieve_complete_from_hyp(share_state);
3533 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003534 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3535 .arg1 = (uint32_t)handle,
3536 .arg2 = (uint32_t)(handle >> 32),
3537 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003538
3539out:
3540 share_states_unlock(&share_states);
3541 dump_share_states();
3542 return ret;
3543}
3544
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003545struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003546 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003547 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003548{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003549 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003550 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003551 struct ffa_memory_share_state *share_state;
3552 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003553 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003554 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003555 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003556 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00003557 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003558
Andrew Walbrana65a1322020-04-06 19:32:32 +01003559 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003560 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003561 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003562 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003563 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003564 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003565 }
3566
Andrew Walbrana65a1322020-04-06 19:32:32 +01003567 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003568 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003569 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003570 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003571 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003572 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003573 }
3574
3575 dump_share_states();
3576
3577 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003578 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003579 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003580 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003581 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003582 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003583 goto out;
3584 }
3585
Andrew Walbranca808b12020-05-15 17:22:28 +01003586 if (!share_state->sending_complete) {
3587 dlog_verbose(
3588 "Memory with handle %#x not fully sent, can't "
3589 "relinquish.\n",
3590 handle);
3591 ret = ffa_error(FFA_INVALID_PARAMETERS);
3592 goto out;
3593 }
3594
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003595 memory_region = share_state->memory_region;
3596 CHECK(memory_region != NULL);
3597
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003598 receiver_index = ffa_memory_region_get_receiver_index(
3599 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003600
3601 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003602 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003603 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003604 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003605 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003606 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003607 goto out;
3608 }
3609
J-Alves8eb19162022-04-28 10:56:48 +01003610 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003611 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003612 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003613 "Memory with handle %#x not yet fully "
3614 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003615 "receiver %x can't relinquish.\n",
3616 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003617 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003618 goto out;
3619 }
3620
J-Alves3c5b2072022-11-21 12:45:40 +00003621 /*
3622 * Either clear if requested in relinquish call, or in a retrieve
3623 * request from one of the borrowers.
3624 */
3625 receivers_relinquished_memory = true;
3626
3627 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3628 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003629 ffa_memory_region_get_receiver(memory_region, i);
3630 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003631 if (receiver->receiver_permissions.receiver ==
3632 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003633 receiver_permissions =
3634 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003635 continue;
3636 }
3637
3638 if (share_state->retrieved_fragment_count[i] != 0U) {
3639 receivers_relinquished_memory = false;
3640 break;
3641 }
3642 }
3643
3644 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003645 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3646 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003647
3648 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003649 * Clear is not allowed for memory that was shared, as the
3650 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003651 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003652 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003653 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003654 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003655 goto out;
3656 }
3657
J-Alves639ddfc2023-11-21 14:17:26 +00003658 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3659 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3660 __func__);
3661 ret = ffa_error(FFA_DENIED);
3662 goto out;
3663 }
3664
Andrew Walbranca808b12020-05-15 17:22:28 +01003665 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003666 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003667 share_state->fragment_constituent_counts,
3668 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003669
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003670 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003671 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003672 * Mark memory handle as not retrieved, so it can be
3673 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003674 */
J-Alves8eb19162022-04-28 10:56:48 +01003675 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003676 }
3677
3678out:
3679 share_states_unlock(&share_states);
3680 dump_share_states();
3681 return ret;
3682}
3683
3684/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003685 * Validates that the reclaim transition is allowed for the given
3686 * handle, updates the page table of the reclaiming VM, and frees the
3687 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003688 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003689struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003690 ffa_memory_handle_t handle,
3691 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003692 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003693{
3694 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003695 struct ffa_memory_share_state *share_state;
3696 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003697 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003698
3699 dump_share_states();
3700
3701 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003702
Karl Meakin4a2854a2023-06-30 16:26:52 +01003703 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003704 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003705 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003706 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003707 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003708 goto out;
3709 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003710 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003711
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003712 CHECK(memory_region != NULL);
3713
J-Alvesa9cd7e32022-07-01 13:49:33 +01003714 if (vm_id_is_current_world(to_locked.vm->id) &&
3715 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003716 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003717 "VM %#x attempted to reclaim memory handle %#x "
3718 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003719 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003720 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003721 goto out;
3722 }
3723
Andrew Walbranca808b12020-05-15 17:22:28 +01003724 if (!share_state->sending_complete) {
3725 dlog_verbose(
3726 "Memory with handle %#x not fully sent, can't "
3727 "reclaim.\n",
3728 handle);
3729 ret = ffa_error(FFA_INVALID_PARAMETERS);
3730 goto out;
3731 }
3732
J-Alves752236c2022-04-28 11:07:47 +01003733 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3734 if (share_state->retrieved_fragment_count[i] != 0) {
3735 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003736 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003737 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003738 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003739 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003740 ffa_memory_region_get_receiver(memory_region, i)
3741 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003742 ret = ffa_error(FFA_DENIED);
3743 goto out;
3744 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003745 }
3746
Andrew Walbranca808b12020-05-15 17:22:28 +01003747 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003748 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003749 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003750 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003751 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003752 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003753
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003754 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003755 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003756 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003757 }
3758
3759out:
3760 share_states_unlock(&share_states);
3761 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003762}