blob: a9f44a8142b294dcfc1a2151993ed8629afc61ed [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
J-Alves460d36c2023-10-12 17:02:15 +010011#include <stdint.h>
12
Federico Recanati4fd065d2021-12-13 20:06:23 +010013#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020014#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020015#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000016
J-Alves5952d942022-12-22 16:03:00 +000017#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000019#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010020#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010022#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010023#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010024#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010025#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000026#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000027#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000028#include "hf/panic.h"
29#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010030#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000031#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000032#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010033
J-Alves2d8457f2022-10-05 11:06:41 +010034#include "vmapi/hf/ffa_v1_0.h"
35
J-Alves5da37d92022-10-24 16:33:48 +010036#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
37
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000038/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040 * by this lock.
41 */
42static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000044
45/**
J-Alvesed508c82023-05-04 16:09:48 +010046 * Return the offset to the first constituent within the
47 * `ffa_composite_memory_region` for the given receiver from an
48 * `ffa_memory_region`. The caller must check that the receiver_index is within
49 * bounds, and that it has a composite memory region offset.
50 */
51static uint32_t ffa_composite_constituent_offset(
52 struct ffa_memory_region *memory_region, uint32_t receiver_index)
53{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000054 struct ffa_memory_access *receiver;
55 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010056
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000057 CHECK(receiver_index < memory_region->receiver_count);
58
59 receiver =
60 ffa_memory_region_get_receiver(memory_region, receiver_index);
61 CHECK(receiver != NULL);
62
63 composite_offset = receiver->composite_memory_region_offset;
64
65 CHECK(composite_offset != 0);
66
67 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010068}
69
70/**
J-Alves917d2f22020-10-30 18:39:30 +000071 * Extracts the index from a memory handle allocated by Hafnium's current world.
72 */
73uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
74{
75 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
76}
77
78/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010079 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
80 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
81 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010082 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010083 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
84 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010085 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010086struct ffa_memory_share_state *allocate_share_state(
87 struct share_states_locked share_states, uint32_t share_func,
88 struct ffa_memory_region *memory_region, uint32_t fragment_length,
89 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000091 assert(share_states.share_states != NULL);
92 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093
Karl Meakin52cdfe72023-06-30 14:49:10 +010094 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010095 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010097 &share_states.share_states[i];
98 struct ffa_composite_memory_region *composite =
99 ffa_memory_region_get_composite(memory_region,
100 0);
101
102 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000103 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200104 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100105 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000106 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100107 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108 allocated_state->share_func = share_func;
109 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100110 allocated_state->fragment_count = 1;
111 allocated_state->fragments[0] = composite->constituents;
112 allocated_state->fragment_constituent_counts[0] =
113 (fragment_length -
114 ffa_composite_constituent_offset(memory_region,
115 0)) /
116 sizeof(struct ffa_memory_region_constituent);
117 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100118 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
119 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100120 allocated_state->retrieved_fragment_count[j] =
121 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100123 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 }
125 }
126
Karl Meakin52cdfe72023-06-30 14:49:10 +0100127 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128}
129
130/** Locks the share states lock. */
131struct share_states_locked share_states_lock(void)
132{
133 sl_lock(&share_states_lock_instance);
134
135 return (struct share_states_locked){.share_states = share_states};
136}
137
138/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100139void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000141 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 share_states->share_states = NULL;
143 sl_unlock(&share_states_lock_instance);
144}
145
146/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100147 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100148 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150struct ffa_memory_share_state *get_share_state(
151 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100153 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000155 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100156
157 /*
158 * First look for a share_state allocated by us, in which case the
159 * handle is based on the index.
160 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200161 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100162 uint64_t index = ffa_memory_handle_get_index(handle);
163
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 if (index < MAX_MEM_SHARES) {
165 share_state = &share_states.share_states[index];
166 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100167 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 }
169 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 }
171
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100173 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000175 if (share_state->memory_region != NULL &&
176 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100178 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100179 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000180 }
181
Karl Meakin4a2854a2023-06-30 16:26:52 +0100182 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183}
184
185/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100186void share_state_free(struct share_states_locked share_states,
187 struct ffa_memory_share_state *share_state,
188 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189{
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 uint32_t i;
191
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000192 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000193 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100194 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 /*
197 * First fragment is part of the same page as the `memory_region`, so it
198 * doesn't need to be freed separately.
199 */
200 share_state->fragments[0] = NULL;
201 share_state->fragment_constituent_counts[0] = 0;
202 for (i = 1; i < share_state->fragment_count; ++i) {
203 mpool_free(page_pool, share_state->fragments[i]);
204 share_state->fragments[i] = NULL;
205 share_state->fragment_constituent_counts[i] = 0;
206 }
207 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100209 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210}
211
Andrew Walbranca808b12020-05-15 17:22:28 +0100212/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100213bool share_state_sending_complete(struct share_states_locked share_states,
214 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215{
Andrew Walbranca808b12020-05-15 17:22:28 +0100216 struct ffa_composite_memory_region *composite;
217 uint32_t expected_constituent_count;
218 uint32_t fragment_constituent_count_total = 0;
219 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000220
Andrew Walbranca808b12020-05-15 17:22:28 +0100221 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000222 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100223
224 /*
225 * Share state must already be valid, or it's not possible to get hold
226 * of it.
227 */
228 CHECK(share_state->memory_region != NULL &&
229 share_state->share_func != 0);
230
231 composite =
232 ffa_memory_region_get_composite(share_state->memory_region, 0);
233 expected_constituent_count = composite->constituent_count;
234 for (i = 0; i < share_state->fragment_count; ++i) {
235 fragment_constituent_count_total +=
236 share_state->fragment_constituent_counts[i];
237 }
238 dlog_verbose(
239 "Checking completion: constituent count %d/%d from %d "
240 "fragments.\n",
241 fragment_constituent_count_total, expected_constituent_count,
242 share_state->fragment_count);
243
244 return fragment_constituent_count_total == expected_constituent_count;
245}
246
247/**
248 * Calculates the offset of the next fragment expected for the given share
249 * state.
250 */
J-Alvesfdd29272022-07-19 13:16:31 +0100251uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100252 struct share_states_locked share_states,
253 struct ffa_memory_share_state *share_state)
254{
255 uint32_t next_fragment_offset;
256 uint32_t i;
257
258 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000259 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100260
261 next_fragment_offset =
262 ffa_composite_constituent_offset(share_state->memory_region, 0);
263 for (i = 0; i < share_state->fragment_count; ++i) {
264 next_fragment_offset +=
265 share_state->fragment_constituent_counts[i] *
266 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 }
268
Andrew Walbranca808b12020-05-15 17:22:28 +0100269 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000270}
271
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100272static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273{
274 uint32_t i;
275
276 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
277 return;
278 }
279
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000280 dlog("from VM %#x, attributes %#x, flags %#x, handle %#x "
281 "tag %u, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 "recipients [",
283 memory_region->sender, memory_region->attributes,
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000284 memory_region->flags, memory_region->handle, memory_region->tag,
285 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100286 memory_region->receiver_count);
287 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000288 struct ffa_memory_access *receiver =
289 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000290 if (i != 0) {
291 dlog(", ");
292 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000293 dlog("Receiver %#x: %#x (offset %u)",
294 receiver->receiver_permissions.receiver,
295 receiver->receiver_permissions.permissions,
296 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000297 /* The impdef field is only present from v1.2 and later */
298 if (ffa_version_from_memory_access_desc_size(
299 memory_region->memory_access_desc_size) >=
300 MAKE_FFA_VERSION(1, 2)) {
301 dlog(", impdef: %#x %#x", receiver->impdef.val[0],
302 receiver->impdef.val[1]);
303 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000304 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000305 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306}
307
J-Alves66652252022-07-06 09:49:51 +0100308void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309{
310 uint32_t i;
311
312 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
313 return;
314 }
315
316 dlog("Current share states:\n");
317 sl_lock(&share_states_lock_instance);
318 for (i = 0; i < MAX_MEM_SHARES; ++i) {
319 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000322 dlog("SHARE");
323 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000325 dlog("LEND");
326 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100327 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000328 dlog("DONATE");
329 break;
330 default:
331 dlog("invalid share_func %#x",
332 share_states[i].share_func);
333 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100334 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100336 if (share_states[i].sending_complete) {
337 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000338 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100339 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 }
J-Alves2a0d2882020-10-29 14:49:50 +0000341 dlog(" with %d fragments, %d retrieved, "
342 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100343 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000344 share_states[i].retrieved_fragment_count[0],
345 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346 }
347 }
348 sl_unlock(&share_states_lock_instance);
349}
350
Andrew Walbran475c1452020-02-07 13:22:22 +0000351/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100352static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100353 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000354{
355 uint32_t mode = 0;
356
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 switch (ffa_get_data_access_attr(permissions)) {
358 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000359 mode = MM_MODE_R;
360 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000362 mode = MM_MODE_R | MM_MODE_W;
363 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100364 case FFA_DATA_ACCESS_NOT_SPECIFIED:
365 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
366 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 case FFA_DATA_ACCESS_RESERVED:
368 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100369 }
370
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371 switch (ffa_get_instruction_access_attr(permissions)) {
372 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000373 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100375 mode |= MM_MODE_X;
376 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100377 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
378 mode |= (default_mode & MM_MODE_X);
379 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100380 case FFA_INSTRUCTION_ACCESS_RESERVED:
381 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000382 }
383
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200384 /* Set the security state bit if necessary. */
385 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
386 mode |= plat_ffa_other_world_mode();
387 }
388
Andrew Walbran475c1452020-02-07 13:22:22 +0000389 return mode;
390}
391
Jose Marinho75509b42019-04-09 09:34:59 +0100392/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 * Get the current mode in the stage-2 page table of the given vm of all the
394 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100396 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000398 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 struct ffa_memory_region_constituent **fragments,
400 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100401{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100402 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100403 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100404
Andrew Walbranca808b12020-05-15 17:22:28 +0100405 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100406 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000407 * Fail if there are no constituents. Otherwise we would get an
408 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100409 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100410 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100412 }
413
Andrew Walbranca808b12020-05-15 17:22:28 +0100414 for (i = 0; i < fragment_count; ++i) {
415 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
416 ipaddr_t begin = ipa_init(fragments[i][j].address);
417 size_t size = fragments[i][j].page_count * PAGE_SIZE;
418 ipaddr_t end = ipa_add(begin, size);
419 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420
Andrew Walbranca808b12020-05-15 17:22:28 +0100421 /* Fail if addresses are not page-aligned. */
422 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
423 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100424 dlog_verbose("%s: addresses not page-aligned\n",
425 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100426 return ffa_error(FFA_INVALID_PARAMETERS);
427 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428
Andrew Walbranca808b12020-05-15 17:22:28 +0100429 /*
430 * Ensure that this constituent memory range is all
431 * mapped with the same mode.
432 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800433 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100434 dlog_verbose(
435 "%s: constituent memory range %#x..%#x "
436 "not mapped with the same mode\n",
437 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 return ffa_error(FFA_DENIED);
439 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100440
Andrew Walbranca808b12020-05-15 17:22:28 +0100441 /*
442 * Ensure that all constituents are mapped with the same
443 * mode.
444 */
445 if (i == 0) {
446 *orig_mode = current_mode;
447 } else if (current_mode != *orig_mode) {
448 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100449 "%s: expected mode %#x but was %#x for "
450 "%d pages at %#x.\n",
451 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 fragments[i][j].page_count,
453 ipa_addr(begin));
454 return ffa_error(FFA_DENIED);
455 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100456 }
Jose Marinho75509b42019-04-09 09:34:59 +0100457 }
458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460}
461
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100462uint32_t ffa_version_from_memory_access_desc_size(
463 uint32_t memory_access_desc_size)
464{
465 switch (memory_access_desc_size) {
466 /*
467 * v1.0 and v1.1 memory access descriptors are the same size however
468 * v1.1 is the first version to include the memory access descriptor
469 * size field so return v1.1.
470 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000471 case sizeof(struct ffa_memory_access_v1_0):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100472 return MAKE_FFA_VERSION(1, 1);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000473 case sizeof(struct ffa_memory_access):
474 return MAKE_FFA_VERSION(1, 2);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100475 }
476 return 0;
477}
478
479/**
480 * Check if the receivers size and offset given is valid for the senders
481 * FF-A version.
482 */
483static bool receiver_size_and_offset_valid_for_version(
484 uint32_t receivers_size, uint32_t receivers_offset,
485 uint32_t ffa_version)
486{
487 /*
488 * Check that the version that the memory access descriptor size belongs
489 * to is compatible with the FF-A version we believe the sender to be.
490 */
491 uint32_t expected_ffa_version =
492 ffa_version_from_memory_access_desc_size(receivers_size);
493 if (!FFA_VERSIONS_ARE_COMPATIBLE(expected_ffa_version, ffa_version)) {
494 return false;
495 }
496
497 /*
498 * Check the receivers_offset matches the version we found from
499 * memory access descriptor size.
500 */
501 switch (expected_ffa_version) {
502 case MAKE_FFA_VERSION(1, 1):
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000503 case MAKE_FFA_VERSION(1, 2):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100504 return receivers_offset == sizeof(struct ffa_memory_region);
505 default:
506 return false;
507 }
508}
509
510/**
511 * Check the values set for fields in the memory region are valid and safe.
512 * Offset values are within safe bounds, receiver count will not cause overflows
513 * and reserved fields are 0.
514 */
515bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
516 uint32_t ffa_version,
517 uint32_t fragment_length,
518 bool send_transaction)
519{
520 uint32_t receiver_count;
521 struct ffa_memory_access *receiver;
522 uint32_t composite_offset_0;
523
524 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
525 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
526 (struct ffa_memory_region_v1_0 *)memory_region;
527 /* Check the reserved fields are 0. */
528 if (memory_region_v1_0->reserved_0 != 0 ||
529 memory_region_v1_0->reserved_1 != 0) {
530 dlog_verbose("Reserved fields must be 0.\n");
531 return false;
532 }
533
534 receiver_count = memory_region_v1_0->receiver_count;
535 } else {
536 uint32_t receivers_size =
537 memory_region->memory_access_desc_size;
538 uint32_t receivers_offset = memory_region->receivers_offset;
539
540 /* Check the reserved field is 0. */
541 if (memory_region->reserved[0] != 0 ||
542 memory_region->reserved[1] != 0 ||
543 memory_region->reserved[2] != 0) {
544 dlog_verbose("Reserved fields must be 0.\n");
545 return false;
546 }
547
548 /*
549 * Check memory_access_desc_size matches the size of the struct
550 * for the senders FF-A version.
551 */
552 if (!receiver_size_and_offset_valid_for_version(
553 receivers_size, receivers_offset, ffa_version)) {
554 dlog_verbose(
555 "Invalid memory access descriptor size %d, "
556 " or receiver offset %d, "
557 "for FF-A version %#x\n",
558 receivers_size, receivers_offset, ffa_version);
559 return false;
560 }
561
562 receiver_count = memory_region->receiver_count;
563 }
564
565 /* Check receiver count is not too large. */
566 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
567 dlog_verbose(
568 "Max number of recipients supported is %u "
569 "specified %u\n",
570 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
571 return false;
572 }
573
574 /* Check values in the memory access descriptors. */
575 /*
576 * The composite offset values must be the same for all recievers so
577 * check the first one is valid and then they are all the same.
578 */
579 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
580 ? (struct ffa_memory_access *)&(
581 (struct ffa_memory_region_v1_0 *)
582 memory_region)
583 ->receivers[0]
584 : ffa_memory_region_get_receiver(memory_region, 0);
585 assert(receiver != NULL);
586 composite_offset_0 = receiver->composite_memory_region_offset;
587
588 if (!send_transaction) {
589 if (composite_offset_0 != 0) {
590 dlog_verbose(
591 "Composite offset memory region descriptor "
592 "offset must be 0 for retrieve requests. "
593 "Currently %d",
594 composite_offset_0);
595 return false;
596 }
597 } else {
598 bool comp_offset_is_zero = composite_offset_0 == 0U;
599 bool comp_offset_lt_transaction_descriptor_size =
600 composite_offset_0 <
601 (sizeof(struct ffa_memory_region) +
602 (uint32_t)(memory_region->memory_access_desc_size *
603 memory_region->receiver_count));
604 bool comp_offset_with_comp_gt_fragment_length =
605 composite_offset_0 +
606 sizeof(struct ffa_composite_memory_region) >
607 fragment_length;
608 if (comp_offset_is_zero ||
609 comp_offset_lt_transaction_descriptor_size ||
610 comp_offset_with_comp_gt_fragment_length) {
611 dlog_verbose(
612 "Invalid composite memory region descriptor "
613 "offset for send transaction %u\n",
614 composite_offset_0);
615 return false;
616 }
617 }
618
619 for (int i = 0; i < memory_region->receiver_count; i++) {
620 uint32_t composite_offset;
621
622 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
623 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
624 (struct ffa_memory_region_v1_0 *)memory_region;
625
626 struct ffa_memory_access_v1_0 *receiver_v1_0 =
627 &memory_region_v1_0->receivers[i];
628 /* Check reserved fields are 0 */
629 if (receiver_v1_0->reserved_0 != 0) {
630 dlog_verbose(
631 "Reserved field in the memory access "
632 " descriptor must be zero "
633 " Currently reciever %d has a reserved "
634 " field with a value of %d\n",
635 i, receiver_v1_0->reserved_0);
636 return false;
637 }
638 /*
639 * We can cast to the current version receiver as the
640 * remaining fields we are checking have the same
641 * offsets for all versions since memory access
642 * descriptors are forwards compatible.
643 */
644 receiver = (struct ffa_memory_access *)receiver_v1_0;
645 } else {
646 receiver = ffa_memory_region_get_receiver(memory_region,
647 i);
648 assert(receiver != NULL);
649
650 if (receiver->reserved_0 != 0) {
651 dlog_verbose(
652 "Reserved field in the memory access "
653 " descriptor must be zero "
654 " Currently reciever %d has a reserved "
655 " field with a value of %d\n",
656 i, receiver->reserved_0);
657 return false;
658 }
659 }
660
661 /* Check composite offset values are equal for all receivers. */
662 composite_offset = receiver->composite_memory_region_offset;
663 if (composite_offset != composite_offset_0) {
664 dlog_verbose(
665 "Composite offset %x differs from %x in index "
666 "%u\n",
667 composite_offset, composite_offset_0);
668 return false;
669 }
670 }
671 return true;
672}
673
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000674/**
J-Alves460d36c2023-10-12 17:02:15 +0100675 * If the receivers for the memory management operation are all from the
676 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
677 * state update by returning MAP_ACTION_CHECK_PROTECT.
678 */
679static enum ffa_map_action ffa_mem_send_get_map_action(
680 bool all_receivers_from_current_world, ffa_id_t sender_id,
681 uint32_t mem_func_id)
682{
683 bool protect_memory =
684 (mem_func_id != FFA_MEM_SHARE_32 &&
685 all_receivers_from_current_world && ffa_is_vm_id(sender_id));
686
687 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
688}
689
690/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691 * Verify that all pages have the same mode, that the starting mode
692 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100693 * to the sending VM. It outputs the mapping action that needs to be
694 * invoked for the given memory range. On memory lend/donate there
695 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000696 *
697 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 * 1) FFA_DENIED if a state transition was not found;
699 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100700 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100701 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100702 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100703 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
704 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000705 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100706static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100707 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100708 struct ffa_memory_access *receivers, uint32_t receivers_count,
709 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100710 struct ffa_memory_region_constituent **fragments,
711 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves460d36c2023-10-12 17:02:15 +0100712 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000713{
714 const uint32_t state_mask =
715 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100717 bool all_receivers_from_current_world = true;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000718
Andrew Walbranca808b12020-05-15 17:22:28 +0100719 ret = constituents_get_mode(from, orig_from_mode, fragments,
720 fragment_constituent_counts,
721 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100722 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100723 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100724 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100725 }
726
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000727 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100728 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000729 dlog_verbose("Can't share device memory (mode is %#x).\n",
730 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100731 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000732 }
733
734 /*
735 * Ensure the sender is the owner and has exclusive access to the
736 * memory.
737 */
738 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100739 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100740 }
741
J-Alves363f5722022-04-25 17:37:37 +0100742 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100743
J-Alves363f5722022-04-25 17:37:37 +0100744 for (uint32_t i = 0U; i < receivers_count; i++) {
745 ffa_memory_access_permissions_t permissions =
746 receivers[i].receiver_permissions.permissions;
747 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
748 permissions, *orig_from_mode);
749
J-Alves788b4492023-04-18 14:01:23 +0100750 /*
751 * The assumption is that at this point, the operation from
752 * SP to a receiver VM, should have returned an FFA_ERROR
753 * already.
754 */
755 if (!ffa_is_vm_id(from.vm->id)) {
756 assert(!ffa_is_vm_id(
757 receivers[i].receiver_permissions.receiver));
758 }
759
J-Alves460d36c2023-10-12 17:02:15 +0100760 /* Track if all senders are from current world. */
761 all_receivers_from_current_world =
762 all_receivers_from_current_world &&
763 vm_id_is_current_world(
764 receivers[i].receiver_permissions.receiver);
765
J-Alves363f5722022-04-25 17:37:37 +0100766 if ((*orig_from_mode & required_from_mode) !=
767 required_from_mode) {
768 dlog_verbose(
769 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100770 "which required mode %#x but only had %#x "
771 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100772 required_from_mode, *orig_from_mode);
773 return ffa_error(FFA_DENIED);
774 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000775 }
776
J-Alves460d36c2023-10-12 17:02:15 +0100777 *map_action = ffa_mem_send_get_map_action(
778 all_receivers_from_current_world, from.vm->id, share_func);
779
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000780 /* Find the appropriate new mode. */
781 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000782 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100783 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000784 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100785 break;
786
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100787 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000788 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100789 break;
790
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100791 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000792 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100793 break;
794
Jose Marinho75509b42019-04-09 09:34:59 +0100795 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100796 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100797 }
798
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000800}
801
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100802static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000803 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100804 struct ffa_memory_region_constituent **fragments,
805 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
806 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000807{
808 const uint32_t state_mask =
809 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
810 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100811 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000812
Andrew Walbranca808b12020-05-15 17:22:28 +0100813 ret = constituents_get_mode(from, orig_from_mode, fragments,
814 fragment_constituent_counts,
815 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100816 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100817 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000818 }
819
820 /* Ensure the address range is normal memory and not a device. */
821 if (*orig_from_mode & MM_MODE_D) {
822 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
823 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100824 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000825 }
826
827 /*
828 * Ensure the relinquishing VM is not the owner but has access to the
829 * memory.
830 */
831 orig_from_state = *orig_from_mode & state_mask;
832 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
833 dlog_verbose(
834 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100835 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000836 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100837 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000838 }
839
840 /* Find the appropriate new mode. */
841 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
842
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100843 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000844}
845
846/**
847 * Verify that all pages have the same mode, that the starting mode
848 * constitutes a valid state and obtain the next mode to apply
849 * to the retrieving VM.
850 *
851 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100852 * 1) FFA_DENIED if a state transition was not found;
853 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100854 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100856 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100857 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
858 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000859 */
J-Alvesfc19b372022-07-06 12:17:35 +0100860struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000861 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100862 struct ffa_memory_region_constituent **fragments,
863 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvesfd206052023-05-22 16:45:00 +0100864 uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected,
865 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000866{
867 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000869
Andrew Walbranca808b12020-05-15 17:22:28 +0100870 ret = constituents_get_mode(to, &orig_to_mode, fragments,
871 fragment_constituent_counts,
872 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100873 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100874 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100875 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 }
877
J-Alves460d36c2023-10-12 17:02:15 +0100878 /* Find the appropriate new mode. */
879 *to_mode = memory_to_attributes;
880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000882 /*
883 * If the original ffa memory send call has been processed
884 * successfully, it is expected the orig_to_mode would overlay
885 * with `state_mask`, as a result of the function
886 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100887 *
888 * If Hafnium is the SPMC:
889 * - Caller of the reclaim interface is an SP, the memory shall
890 * have been protected throughout the flow.
891 * - Caller of the reclaim is from the NWd, the memory may have
892 * been protected at the time of lending/donating the memory.
893 * In such case, set action to unprotect memory in the
894 * handling of reclaim operation.
895 * - If Hafnium is the hypervisor memory shall never have been
896 * protected in memory lend/share/donate.
897 *
898 * More details in the doc comment of the function
899 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000900 */
J-Alves59ed0042022-07-28 18:26:41 +0100901 if (vm_id_is_current_world(to.vm->id)) {
902 assert((orig_to_mode &
903 (MM_MODE_INVALID | MM_MODE_UNOWNED |
904 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100905 assert(!memory_protected);
906 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
907 map_action != NULL && memory_protected) {
908 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100909 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000910 } else {
911 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100912 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000913 * Ensure the retriever has the expected state. We don't care
914 * about the MM_MODE_SHARED bit; either with or without it set
915 * are both valid representations of the !O-NA state.
916 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100917 if (vm_id_is_current_world(to.vm->id) &&
918 to.vm->id != HF_PRIMARY_VM_ID &&
919 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
920 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 }
J-Alves460d36c2023-10-12 17:02:15 +0100923
924 /*
925 * If memory has been protected before, clear the NS bit to
926 * allow the secure access from the SP.
927 */
928 if (memory_protected) {
929 *to_mode &= ~plat_ffa_other_world_mode();
930 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 }
932
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000935 *to_mode |= 0;
936 break;
937
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000939 *to_mode |= MM_MODE_UNOWNED;
940 break;
941
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100942 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000943 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
944 break;
945
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000947 *to_mode |= 0;
948 break;
949
950 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100951 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100952 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000953 }
954
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100956}
Jose Marinho09b1db82019-08-08 09:16:59 +0100957
J-Alvescf6253e2024-01-03 13:48:48 +0000958/*
959 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
960 * Returns:
961 * - FFA_SUCCESS_32: if all goes well.
962 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
963 * the page table update. Or error code provided by the function
964 * `arch_memory_protect`.
965 */
966static struct ffa_value ffa_region_group_check_actions(
967 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
968 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
969 bool *memory_protected)
970{
971 struct ffa_value ret;
972 bool is_memory_protected;
973
974 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
975 dlog_verbose(
976 "%s: memory can't be mapped to %x due to lack of "
977 "memory. Base: %lx end: %x\n",
978 __func__, vm_locked.vm->id, pa_addr(pa_begin),
979 pa_addr(pa_end));
980 return ffa_error(FFA_NO_MEMORY);
981 }
982
983 switch (action) {
984 case MAP_ACTION_CHECK:
985 /* No protect requested. */
986 is_memory_protected = false;
987 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
988 break;
989 case MAP_ACTION_CHECK_PROTECT: {
990 paddr_t last_protected_pa = pa_init(0);
991
992 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
993
994 is_memory_protected = (ret.func == FFA_SUCCESS_32);
995
996 /*
997 * - If protect memory has failed with FFA_DENIED, means some
998 * range of memory was in the wrong state. In such case, SPM
999 * reverts the state of the pages that were successfully
1000 * updated.
1001 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1002 * means the platform doesn't support the protection mechanism.
1003 * That said, it still permits the page table update to go
1004 * through. The variable
1005 * `is_memory_protected` will be equal to false.
1006 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1007 * break from switch and return the error.
1008 */
1009 if (ret.func == FFA_ERROR_32) {
1010 assert(!is_memory_protected);
1011 if (ffa_error_code(ret) == FFA_DENIED &&
1012 pa_addr(last_protected_pa) != (uintptr_t)0) {
1013 CHECK(arch_memory_unprotect(
1014 pa_begin,
1015 pa_add(last_protected_pa, PAGE_SIZE)));
1016 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1017 ret = (struct ffa_value){
1018 .func = FFA_SUCCESS_32,
1019 };
1020 }
1021 }
1022 } break;
1023 default:
1024 panic("%s: invalid action to process %x\n", __func__, action);
1025 }
1026
1027 if (memory_protected != NULL) {
1028 *memory_protected = is_memory_protected;
1029 }
1030
1031 return ret;
1032}
1033
1034static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1035 paddr_t pa_begin, paddr_t pa_end,
1036 struct mpool *ppool, uint32_t mode,
1037 enum ffa_map_action action)
1038{
1039 switch (action) {
1040 case MAP_ACTION_COMMIT_UNPROTECT:
1041 /*
1042 * Checking that it should succeed because SPM should be
1043 * unprotecting memory that it had protected before.
1044 */
1045 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1046 case MAP_ACTION_COMMIT:
1047 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1048 NULL);
1049 break;
1050 default:
1051 panic("%s: invalid action to process %x\n", __func__, action);
1052 }
1053}
1054
Jose Marinho09b1db82019-08-08 09:16:59 +01001055/**
1056 * Updates a VM's page table such that the given set of physical address ranges
1057 * are mapped in the address space at the corresponding address ranges, in the
1058 * mode provided.
1059 *
J-Alves0a83dc22023-05-05 09:50:37 +01001060 * The enum ffa_map_action determines the action taken from a call to the
1061 * function below:
1062 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1063 * mpool but no mappings will actually be updated. This function must always
1064 * be called first with action set to MAP_ACTION_CHECK to check that it will
1065 * succeed before calling ffa_region_group_identity_map with whichever one of
1066 * the remaining actions, to avoid leaving the page table in a half-updated
1067 * state.
1068 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1069 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001070 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1071 * invocation to the monitor to update the security state of the memory,
1072 * to that of the SPMC.
1073 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1074 * with a call into the monitor, to reset the security state of memory
1075 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001076 * vm_ptable_defrag should always be called after a series of page table
1077 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001078 *
J-Alvescf6253e2024-01-03 13:48:48 +00001079 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1080 * error codes:
1081 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1082 * - FFA_DENIED:
1083 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001084 * made to memory mappings.
1085 */
J-Alvescf6253e2024-01-03 13:48:48 +00001086struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001087 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001088 struct ffa_memory_region_constituent **fragments,
1089 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001090 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1091 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001092{
Andrew Walbranca808b12020-05-15 17:22:28 +01001093 uint32_t i;
1094 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001095 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001096
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001097 if (vm_locked.vm->el0_partition) {
1098 mode |= MM_MODE_USER | MM_MODE_NG;
1099 }
1100
Andrew Walbranca808b12020-05-15 17:22:28 +01001101 /* Iterate over the memory region constituents within each fragment. */
1102 for (i = 0; i < fragment_count; ++i) {
1103 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1104 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1105 paddr_t pa_begin =
1106 pa_from_ipa(ipa_init(fragments[i][j].address));
1107 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001108 uint32_t pa_bits =
1109 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001110
1111 /*
1112 * Ensure the requested region falls into system's PA
1113 * range.
1114 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001115 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1116 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001117 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001118 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001119 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001120
J-Alvescf6253e2024-01-03 13:48:48 +00001121 if (action <= MAP_ACTION_CHECK_PROTECT) {
1122 ret = ffa_region_group_check_actions(
1123 vm_locked, pa_begin, pa_end, ppool,
1124 mode, action, memory_protected);
1125 } else if (action >= MAP_ACTION_COMMIT &&
1126 action < MAP_ACTION_MAX) {
1127 ffa_region_group_commit_actions(
1128 vm_locked, pa_begin, pa_end, ppool,
1129 mode, action);
1130 ret = (struct ffa_value){
1131 .func = FFA_SUCCESS_32};
1132 } else {
1133 panic("%s: Unknown ffa_map_action.\n",
1134 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001135 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001136 }
1137 }
1138
J-Alvescf6253e2024-01-03 13:48:48 +00001139 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001140}
1141
1142/**
1143 * Clears a region of physical memory by overwriting it with zeros. The data is
1144 * flushed from the cache so the memory has been cleared across the system.
1145 */
J-Alves7db32002021-12-14 14:44:50 +00001146static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1147 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001148{
1149 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001150 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001151 * global mapping of the whole range. Such an approach will limit
1152 * the changes to stage-1 tables and will allow only local
1153 * invalidation.
1154 */
1155 bool ret;
1156 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001157 void *ptr = mm_identity_map(stage1_locked, begin, end,
1158 MM_MODE_W | (extra_mode_attributes &
1159 plat_ffa_other_world_mode()),
1160 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001161 size_t size = pa_difference(begin, end);
1162
1163 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001164 goto fail;
1165 }
1166
1167 memset_s(ptr, size, 0, size);
1168 arch_mm_flush_dcache(ptr, size);
1169 mm_unmap(stage1_locked, begin, end, ppool);
1170
1171 ret = true;
1172 goto out;
1173
1174fail:
1175 ret = false;
1176
1177out:
1178 mm_unlock_stage1(&stage1_locked);
1179
1180 return ret;
1181}
1182
1183/**
1184 * Clears a region of physical memory by overwriting it with zeros. The data is
1185 * flushed from the cache so the memory has been cleared across the system.
1186 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001188 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001189 struct ffa_memory_region_constituent **fragments,
1190 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1191 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001192{
1193 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001194 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001195 bool ret = false;
1196
1197 /*
1198 * Create a local pool so any freed memory can't be used by another
1199 * thread. This is to ensure each constituent that is mapped can be
1200 * unmapped again afterwards.
1201 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001202 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001203
Andrew Walbranca808b12020-05-15 17:22:28 +01001204 /* Iterate over the memory region constituents within each fragment. */
1205 for (i = 0; i < fragment_count; ++i) {
1206 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001207
J-Alves8457f932023-10-11 16:41:45 +01001208 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001209 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1210 paddr_t begin =
1211 pa_from_ipa(ipa_init(fragments[i][j].address));
1212 paddr_t end = pa_add(begin, size);
1213
J-Alves7db32002021-12-14 14:44:50 +00001214 if (!clear_memory(begin, end, &local_page_pool,
1215 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001216 /*
1217 * api_clear_memory will defrag on failure, so
1218 * no need to do it here.
1219 */
1220 goto out;
1221 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001222 }
1223 }
1224
Jose Marinho09b1db82019-08-08 09:16:59 +01001225 ret = true;
1226
1227out:
1228 mpool_fini(&local_page_pool);
1229 return ret;
1230}
1231
J-Alves5952d942022-12-22 16:03:00 +00001232static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1233 ipaddr_t in_begin, ipaddr_t in_end)
1234{
1235 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1236 ipa_addr(begin) < ipa_addr(in_end)) ||
1237 (ipa_addr(end) <= ipa_addr(in_end) &&
1238 ipa_addr(end) > ipa_addr(in_begin));
1239}
1240
1241/**
1242 * Receives a memory range and looks for overlaps with the remainder
1243 * constituents of the memory share/lend/donate operation. Assumes they are
1244 * passed in order to avoid having to loop over all the elements at each call.
1245 * The function only compares the received memory ranges with those that follow
1246 * within the same fragment, and subsequent fragments from the same operation.
1247 */
1248static bool ffa_memory_check_overlap(
1249 struct ffa_memory_region_constituent **fragments,
1250 const uint32_t *fragment_constituent_counts,
1251 const uint32_t fragment_count, const uint32_t current_fragment,
1252 const uint32_t current_constituent)
1253{
1254 uint32_t i = current_fragment;
1255 uint32_t j = current_constituent;
1256 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1257 const uint32_t current_page_count = fragments[i][j].page_count;
1258 size_t current_size = current_page_count * PAGE_SIZE;
1259 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1260
1261 if (current_size == 0 ||
1262 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1263 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1264 current_begin, current_page_count);
1265 return false;
1266 }
1267
1268 for (; i < fragment_count; i++) {
1269 j = (i == current_fragment) ? j + 1 : 0;
1270
1271 for (; j < fragment_constituent_counts[i]; j++) {
1272 ipaddr_t begin = ipa_init(fragments[i][j].address);
1273 const uint32_t page_count = fragments[i][j].page_count;
1274 size_t size = page_count * PAGE_SIZE;
1275 ipaddr_t end = ipa_add(begin, size - 1);
1276
1277 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1278 dlog_verbose(
1279 "Invalid page count. Addr: %x "
1280 "page_count: %x\n",
1281 begin, page_count);
1282 return false;
1283 }
1284
1285 /*
1286 * Check if current ranges is within begin and end, as
1287 * well as the reverse. This should help optimize the
1288 * loop, and reduce the number of iterations.
1289 */
1290 if (is_memory_range_within(begin, end, current_begin,
1291 current_end) ||
1292 is_memory_range_within(current_begin, current_end,
1293 begin, end)) {
1294 dlog_verbose(
1295 "Overlapping memory ranges: %#x - %#x "
1296 "with %#x - %#x\n",
1297 ipa_addr(begin), ipa_addr(end),
1298 ipa_addr(current_begin),
1299 ipa_addr(current_end));
1300 return true;
1301 }
1302 }
1303 }
1304
1305 return false;
1306}
1307
Jose Marinho09b1db82019-08-08 09:16:59 +01001308/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001309 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001310 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001311 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001312 *
1313 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001314 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001315 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001316 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001317 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1318 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001319 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001321 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001322 */
J-Alves66652252022-07-06 09:49:51 +01001323struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001324 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001325 struct ffa_memory_region_constituent **fragments,
1326 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001327 uint32_t composite_total_page_count, uint32_t share_func,
1328 struct ffa_memory_access *receivers, uint32_t receivers_count,
J-Alves460d36c2023-10-12 17:02:15 +01001329 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret,
1330 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001331{
Andrew Walbranca808b12020-05-15 17:22:28 +01001332 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001333 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001334 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001335 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001336 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001337 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001338 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001339 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001340 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Jose Marinho09b1db82019-08-08 09:16:59 +01001341
1342 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001343 * Make sure constituents are properly aligned to a 64-bit boundary. If
1344 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001345 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001346 for (i = 0; i < fragment_count; ++i) {
1347 if (!is_aligned(fragments[i], 8)) {
1348 dlog_verbose("Constituents not aligned.\n");
1349 return ffa_error(FFA_INVALID_PARAMETERS);
1350 }
J-Alves8f11cde2022-12-21 16:18:22 +00001351 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1352 constituents_total_page_count +=
1353 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001354 if (ffa_memory_check_overlap(
1355 fragments, fragment_constituent_counts,
1356 fragment_count, i, j)) {
1357 return ffa_error(FFA_INVALID_PARAMETERS);
1358 }
J-Alves8f11cde2022-12-21 16:18:22 +00001359 }
1360 }
1361
1362 if (constituents_total_page_count != composite_total_page_count) {
1363 dlog_verbose(
1364 "Composite page count differs from calculated page "
1365 "count from constituents.\n");
1366 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001367 }
1368
1369 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001370 * Check if the state transition is lawful for the sender, ensure that
1371 * all constituents of a memory region being shared are at the same
1372 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001373 */
J-Alves460d36c2023-10-12 17:02:15 +01001374 ret = ffa_send_check_transition(
1375 from_locked, share_func, receivers, receivers_count,
1376 &orig_from_mode, fragments, fragment_constituent_counts,
1377 fragment_count, &from_mode, &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001378 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001379 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001381 }
1382
Andrew Walbran37c574e2020-06-03 11:45:46 +01001383 if (orig_from_mode_ret != NULL) {
1384 *orig_from_mode_ret = orig_from_mode;
1385 }
1386
Jose Marinho09b1db82019-08-08 09:16:59 +01001387 /*
1388 * Create a local pool so any freed memory can't be used by another
1389 * thread. This is to ensure the original mapping can be restored if the
1390 * clear fails.
1391 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001392 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001393
1394 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001395 * First reserve all required memory for the new page table entries
1396 * without committing, to make sure the entire operation will succeed
1397 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001398 * Provide the map_action as populated by 'ffa_send_check_transition'.
1399 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001400 */
J-Alvescf6253e2024-01-03 13:48:48 +00001401 ret = ffa_region_group_identity_map(
1402 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001403 fragment_count, from_mode, page_pool, map_action,
1404 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001405 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001406 goto out;
1407 }
1408
1409 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001410 * Update the mapping for the sender. This won't allocate because the
1411 * transaction was already prepared above, but may free pages in the
1412 * case that a whole block is being unmapped that was previously
1413 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001414 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001415 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001416 from_locked, fragments, fragment_constituent_counts,
1417 fragment_count, from_mode, &local_page_pool,
1418 MAP_ACTION_COMMIT, NULL)
1419 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001420
J-Alves460d36c2023-10-12 17:02:15 +01001421 /*
1422 * If memory has been protected, it is now part of the secure PAS
1423 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1424 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1425 * SPM's S1 translation.
1426 * In case memory hasn't been protected, and it is in the non-secure
1427 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1428 * perform a non-secure memory access. In such case `clean_mode` takes
1429 * the same mode as `orig_from_mode`.
1430 */
1431 clean_mode = (memory_protected != NULL && *memory_protected)
1432 ? orig_from_mode & ~plat_ffa_other_world_mode()
1433 : orig_from_mode;
1434
Jose Marinho09b1db82019-08-08 09:16:59 +01001435 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001436 if (clear && !ffa_clear_memory_constituents(
1437 clean_mode, fragments, fragment_constituent_counts,
1438 fragment_count, page_pool)) {
1439 map_action = (memory_protected != NULL && *memory_protected)
1440 ? MAP_ACTION_COMMIT_UNPROTECT
1441 : MAP_ACTION_COMMIT;
1442
Jose Marinho09b1db82019-08-08 09:16:59 +01001443 /*
1444 * On failure, roll back by returning memory to the sender. This
1445 * may allocate pages which were previously freed into
1446 * `local_page_pool` by the call above, but will never allocate
1447 * more pages than that so can never fail.
1448 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001450 from_locked, fragments,
1451 fragment_constituent_counts, fragment_count,
1452 orig_from_mode, &local_page_pool,
1453 MAP_ACTION_COMMIT, NULL)
1454 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001456 goto out;
1457 }
1458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001460
1461out:
1462 mpool_fini(&local_page_pool);
1463
1464 /*
1465 * Tidy up the page table by reclaiming failed mappings (if there was an
1466 * error) or merging entries into blocks where possible (on success).
1467 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001468 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001469
1470 return ret;
1471}
1472
1473/**
1474 * Validates and maps memory shared from one VM to another.
1475 *
1476 * This function requires the calling context to hold the <to> lock.
1477 *
1478 * Returns:
1479 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001480 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001481 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001483 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001484 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001485 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001486struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001487 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001488 struct ffa_memory_region_constituent **fragments,
1489 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001490 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001491 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001492{
Andrew Walbranca808b12020-05-15 17:22:28 +01001493 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001494 uint32_t to_mode;
1495 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001496 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001497 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001498
1499 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001500 * Make sure constituents are properly aligned to a 64-bit boundary. If
1501 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001502 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001503 for (i = 0; i < fragment_count; ++i) {
1504 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001505 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001506 return ffa_error(FFA_INVALID_PARAMETERS);
1507 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001508 }
1509
1510 /*
1511 * Check if the state transition is lawful for the recipient, and ensure
1512 * that all constituents of the memory region being retrieved are at the
1513 * same state.
1514 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001515 ret = ffa_retrieve_check_transition(
1516 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001517 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1518 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001519
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001520 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001521 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001522 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001523 }
1524
1525 /*
1526 * Create a local pool so any freed memory can't be used by another
1527 * thread. This is to ensure the original mapping can be restored if the
1528 * clear fails.
1529 */
1530 mpool_init_with_fallback(&local_page_pool, page_pool);
1531
1532 /*
1533 * First reserve all required memory for the new page table entries in
1534 * the recipient page tables without committing, to make sure the entire
1535 * operation will succeed without exhausting the page pool.
1536 */
J-Alvescf6253e2024-01-03 13:48:48 +00001537 ret = ffa_region_group_identity_map(
1538 to_locked, fragments, fragment_constituent_counts,
1539 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK, NULL);
1540 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001541 /* TODO: partial defrag of failed range. */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001542 goto out;
1543 }
1544
1545 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001546 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001547 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1548 fragment_constituent_counts,
1549 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001550 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001551 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552 goto out;
1553 }
1554
Jose Marinho09b1db82019-08-08 09:16:59 +01001555 /*
1556 * Complete the transfer by mapping the memory into the recipient. This
1557 * won't allocate because the transaction was already prepared above, so
1558 * it doesn't need to use the `local_page_pool`.
1559 */
J-Alvesfd206052023-05-22 16:45:00 +01001560 CHECK(ffa_region_group_identity_map(
1561 to_locked, fragments, fragment_constituent_counts,
1562 fragment_count, to_mode, page_pool, map_action, NULL)
J-Alvescf6253e2024-01-03 13:48:48 +00001563 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001564
J-Alves460d36c2023-10-12 17:02:15 +01001565 /* Return the mode used in mapping the memory in retriever's PT. */
1566 if (response_mode != NULL) {
1567 *response_mode = to_mode;
1568 }
1569
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001570 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001571
1572out:
1573 mpool_fini(&local_page_pool);
1574
1575 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001576 * Tidy up the page table by reclaiming failed mappings (if there was an
1577 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001578 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001579 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001580
1581 return ret;
1582}
1583
Andrew Walbran996d1d12020-05-27 14:08:43 +01001584static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001585 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001586 struct ffa_memory_region_constituent **fragments,
1587 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1588 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001589{
1590 uint32_t orig_from_mode;
1591 uint32_t from_mode;
1592 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001593 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001594
Andrew Walbranca808b12020-05-15 17:22:28 +01001595 ret = ffa_relinquish_check_transition(
1596 from_locked, &orig_from_mode, fragments,
1597 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001599 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001600 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 }
1602
1603 /*
1604 * Create a local pool so any freed memory can't be used by another
1605 * thread. This is to ensure the original mapping can be restored if the
1606 * clear fails.
1607 */
1608 mpool_init_with_fallback(&local_page_pool, page_pool);
1609
1610 /*
1611 * First reserve all required memory for the new page table entries
1612 * without committing, to make sure the entire operation will succeed
1613 * without exhausting the page pool.
1614 */
J-Alvescf6253e2024-01-03 13:48:48 +00001615 ret = ffa_region_group_identity_map(
1616 from_locked, fragments, fragment_constituent_counts,
1617 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK, NULL);
1618 if (ret.func == FFA_ERROR_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001619 goto out;
1620 }
1621
1622 /*
1623 * Update the mapping for the sender. This won't allocate because the
1624 * transaction was already prepared above, but may free pages in the
1625 * case that a whole block is being unmapped that was previously
1626 * partially mapped.
1627 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001628 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001629 from_locked, fragments, fragment_constituent_counts,
1630 fragment_count, from_mode, &local_page_pool,
1631 MAP_ACTION_COMMIT, NULL)
1632 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001633
1634 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001635 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001636 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1637 fragment_constituent_counts,
1638 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001639 /*
1640 * On failure, roll back by returning memory to the sender. This
1641 * may allocate pages which were previously freed into
1642 * `local_page_pool` by the call above, but will never allocate
1643 * more pages than that so can never fail.
1644 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001645 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001646 from_locked, fragments,
1647 fragment_constituent_counts, fragment_count,
1648 orig_from_mode, &local_page_pool,
1649 MAP_ACTION_COMMIT, NULL)
1650 .func == FFA_SUCCESS_32);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001651
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653 goto out;
1654 }
1655
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001657
1658out:
1659 mpool_fini(&local_page_pool);
1660
1661 /*
1662 * Tidy up the page table by reclaiming failed mappings (if there was an
1663 * error) or merging entries into blocks where possible (on success).
1664 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001665 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001666
1667 return ret;
1668}
1669
1670/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001671 * Complete a memory sending operation by checking that it is valid, updating
1672 * the sender page table, and then either marking the share state as having
1673 * completed sending (on success) or freeing it (on failure).
1674 *
1675 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1676 */
J-Alvesfdd29272022-07-19 13:16:31 +01001677struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001678 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001679 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1680 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001681{
1682 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001683 struct ffa_composite_memory_region *composite;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001684 struct ffa_memory_access *receiver;
Andrew Walbranca808b12020-05-15 17:22:28 +01001685 struct ffa_value ret;
1686
1687 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001688 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001689 assert(memory_region != NULL);
1690 composite = ffa_memory_region_get_composite(memory_region, 0);
1691 assert(composite != NULL);
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001692 receiver = ffa_memory_region_get_receiver(memory_region, 0);
1693 assert(receiver != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001694
1695 /* Check that state is valid in sender page table and update. */
1696 ret = ffa_send_check_update(
1697 from_locked, share_state->fragments,
1698 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001699 share_state->fragment_count, composite->page_count,
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001700 share_state->share_func, receiver,
J-Alves8f11cde2022-12-21 16:18:22 +00001701 memory_region->receiver_count, page_pool,
1702 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
J-Alves460d36c2023-10-12 17:02:15 +01001703 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001704 if (ret.func != FFA_SUCCESS_32) {
1705 /*
1706 * Free share state, it failed to send so it can't be retrieved.
1707 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001708 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1709 __func__, ffa_func_name(ret.func),
1710 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001711 share_state_free(share_states, share_state, page_pool);
1712 return ret;
1713 }
1714
1715 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001716 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001717
J-Alvesee68c542020-10-29 17:48:20 +00001718 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001719}
1720
1721/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001722 * Check that the memory attributes match Hafnium expectations:
1723 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1724 * Write-Allocate Cacheable.
1725 */
1726static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001727 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001728{
1729 enum ffa_memory_type memory_type;
1730 enum ffa_memory_cacheability cacheability;
1731 enum ffa_memory_shareability shareability;
1732
1733 memory_type = ffa_get_memory_type_attr(attributes);
1734 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1735 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1736 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001737 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001738 }
1739
1740 cacheability = ffa_get_memory_cacheability_attr(attributes);
1741 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1742 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1743 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001744 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001745 }
1746
1747 shareability = ffa_get_memory_shareability_attr(attributes);
1748 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1749 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1750 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001751 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001752 }
1753
1754 return (struct ffa_value){.func = FFA_SUCCESS_32};
1755}
1756
1757/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001758 * Check that the given `memory_region` represents a valid memory send request
1759 * of the given `share_func` type, return the clear flag and permissions via the
1760 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001761 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001762 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001763 * not.
1764 */
J-Alves66652252022-07-06 09:49:51 +01001765struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001766 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1767 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001768 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001769{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001770 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001771 struct ffa_memory_access *receiver =
1772 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001773 uint64_t receivers_end;
1774 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001775 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001776 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001777 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001778 enum ffa_data_access data_access;
1779 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001780 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001781 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001782 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001783 memory_region->receivers_offset +
1784 memory_region->memory_access_desc_size +
1785 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001786
1787 if (fragment_length < minimum_first_fragment_length) {
1788 dlog_verbose("Fragment length %u too short (min %u).\n",
1789 (size_t)fragment_length,
1790 minimum_first_fragment_length);
1791 return ffa_error(FFA_INVALID_PARAMETERS);
1792 }
1793
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001794 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1795 "struct ffa_memory_region_constituent must be 16 bytes");
1796 if (!is_aligned(fragment_length,
1797 sizeof(struct ffa_memory_region_constituent)) ||
1798 !is_aligned(memory_share_length,
1799 sizeof(struct ffa_memory_region_constituent))) {
1800 dlog_verbose(
1801 "Fragment length %u or total length %u"
1802 " is not 16-byte aligned.\n",
1803 fragment_length, memory_share_length);
1804 return ffa_error(FFA_INVALID_PARAMETERS);
1805 }
1806
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001807 if (fragment_length > memory_share_length) {
1808 dlog_verbose(
1809 "Fragment length %u greater than total length %u.\n",
1810 (size_t)fragment_length, (size_t)memory_share_length);
1811 return ffa_error(FFA_INVALID_PARAMETERS);
1812 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001813
J-Alves95df0ef2022-12-07 10:09:48 +00001814 /* The sender must match the caller. */
1815 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1816 vm_id_is_current_world(memory_region->sender)) ||
1817 (vm_id_is_current_world(from_locked.vm->id) &&
1818 memory_region->sender != from_locked.vm->id)) {
1819 dlog_verbose("Invalid memory sender ID.\n");
1820 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001821 }
1822
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001823 if (memory_region->receiver_count <= 0) {
1824 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001825 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001826 }
1827
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001828 /*
1829 * Ensure that the composite header is within the memory bounds and
1830 * doesn't overlap the first part of the message. Cast to uint64_t
1831 * to prevent overflow.
1832 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001833 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001834 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001835 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001836 min_length = receivers_end +
1837 sizeof(struct ffa_composite_memory_region) +
1838 sizeof(struct ffa_memory_region_constituent);
1839 if (min_length > memory_share_length) {
1840 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1841 (size_t)memory_share_length, (size_t)min_length);
1842 return ffa_error(FFA_INVALID_PARAMETERS);
1843 }
1844
1845 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001846 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001847
1848 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001849 * Check that the composite memory region descriptor is after the access
1850 * descriptors, is at least 16-byte aligned, and fits in the first
1851 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001852 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001853 if ((composite_memory_region_offset < receivers_end) ||
1854 (composite_memory_region_offset % 16 != 0) ||
1855 (composite_memory_region_offset >
1856 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1857 dlog_verbose(
1858 "Invalid composite memory region descriptor offset "
1859 "%u.\n",
1860 (size_t)composite_memory_region_offset);
1861 return ffa_error(FFA_INVALID_PARAMETERS);
1862 }
1863
1864 /*
1865 * Compute the start of the constituent regions. Already checked
1866 * to be not more than fragment_length and thus not more than
1867 * memory_share_length.
1868 */
1869 constituents_start = composite_memory_region_offset +
1870 sizeof(struct ffa_composite_memory_region);
1871 constituents_length = memory_share_length - constituents_start;
1872
1873 /*
1874 * Check that the number of constituents is consistent with the length
1875 * of the constituent region.
1876 */
1877 composite = ffa_memory_region_get_composite(memory_region, 0);
1878 if ((constituents_length %
1879 sizeof(struct ffa_memory_region_constituent) !=
1880 0) ||
1881 ((constituents_length /
1882 sizeof(struct ffa_memory_region_constituent)) !=
1883 composite->constituent_count)) {
1884 dlog_verbose("Invalid length %u or composite offset %u.\n",
1885 (size_t)memory_share_length,
1886 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001887 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001888 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001889 if (fragment_length < memory_share_length &&
1890 fragment_length < HF_MAILBOX_SIZE) {
1891 dlog_warning(
1892 "Initial fragment length %d smaller than mailbox "
1893 "size.\n",
1894 fragment_length);
1895 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001896
Andrew Walbrana65a1322020-04-06 19:32:32 +01001897 /*
1898 * Clear is not allowed for memory sharing, as the sender still has
1899 * access to the memory.
1900 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001901 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1902 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001903 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001904 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001905 }
1906
1907 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001908 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001909 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001910 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001911 }
1912
J-Alves363f5722022-04-25 17:37:37 +01001913 /* Check that the permissions are valid, for each specified receiver. */
1914 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001915 struct ffa_memory_region_attributes receiver_permissions;
1916
1917 receiver = ffa_memory_region_get_receiver(memory_region, i);
1918 assert(receiver != NULL);
1919 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001920 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001921 receiver_permissions.permissions;
1922 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001923
1924 if (memory_region->sender == receiver_id) {
1925 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001926 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001927 }
Federico Recanati85090c42021-12-15 13:17:54 +01001928
J-Alves363f5722022-04-25 17:37:37 +01001929 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1930 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001931 struct ffa_memory_access *other_receiver =
1932 ffa_memory_region_get_receiver(memory_region,
1933 j);
1934 assert(other_receiver != NULL);
1935
J-Alves363f5722022-04-25 17:37:37 +01001936 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001937 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001938 dlog_verbose(
1939 "Repeated receiver(%x) in memory send "
1940 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001941 other_receiver->receiver_permissions
1942 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001943 return ffa_error(FFA_INVALID_PARAMETERS);
1944 }
1945 }
1946
1947 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001948 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01001949 dlog_verbose(
1950 "All ffa_memory_access should point to the "
1951 "same composite memory region offset.\n");
1952 return ffa_error(FFA_INVALID_PARAMETERS);
1953 }
1954
1955 data_access = ffa_get_data_access_attr(permissions);
1956 instruction_access =
1957 ffa_get_instruction_access_attr(permissions);
1958 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1959 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1960 dlog_verbose(
1961 "Reserved value for receiver permissions "
1962 "%#x.\n",
1963 permissions);
1964 return ffa_error(FFA_INVALID_PARAMETERS);
1965 }
1966 if (instruction_access !=
1967 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1968 dlog_verbose(
1969 "Invalid instruction access permissions %#x "
1970 "for sending memory.\n",
1971 permissions);
1972 return ffa_error(FFA_INVALID_PARAMETERS);
1973 }
1974 if (share_func == FFA_MEM_SHARE_32) {
1975 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1976 dlog_verbose(
1977 "Invalid data access permissions %#x "
1978 "for sharing memory.\n",
1979 permissions);
1980 return ffa_error(FFA_INVALID_PARAMETERS);
1981 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001982 /*
1983 * According to section 10.10.3 of the FF-A v1.1 EAC0
1984 * spec, NX is required for share operations (but must
1985 * not be specified by the sender) so set it in the
1986 * copy that we store, ready to be returned to the
1987 * retriever.
1988 */
1989 if (vm_id_is_current_world(receiver_id)) {
1990 ffa_set_instruction_access_attr(
1991 &permissions,
1992 FFA_INSTRUCTION_ACCESS_NX);
1993 receiver_permissions.permissions = permissions;
1994 }
J-Alves363f5722022-04-25 17:37:37 +01001995 }
1996 if (share_func == FFA_MEM_LEND_32 &&
1997 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1998 dlog_verbose(
1999 "Invalid data access permissions %#x for "
2000 "lending memory.\n",
2001 permissions);
2002 return ffa_error(FFA_INVALID_PARAMETERS);
2003 }
2004
2005 if (share_func == FFA_MEM_DONATE_32 &&
2006 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2007 dlog_verbose(
2008 "Invalid data access permissions %#x for "
2009 "donating memory.\n",
2010 permissions);
2011 return ffa_error(FFA_INVALID_PARAMETERS);
2012 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002013 }
2014
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002015 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
2016 security_state =
2017 ffa_get_memory_security_attr(memory_region->attributes);
2018 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2019 dlog_verbose(
2020 "Invalid security state for memory share operation.\n");
2021 return ffa_error(FFA_INVALID_PARAMETERS);
2022 }
2023
Federico Recanatid937f5e2021-12-20 17:38:23 +01002024 /*
J-Alves807794e2022-06-16 13:42:47 +01002025 * If a memory donate or lend with single borrower, the memory type
2026 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002027 */
J-Alves807794e2022-06-16 13:42:47 +01002028 if (share_func == FFA_MEM_DONATE_32 ||
2029 (share_func == FFA_MEM_LEND_32 &&
2030 memory_region->receiver_count == 1)) {
2031 if (ffa_get_memory_type_attr(memory_region->attributes) !=
2032 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2033 dlog_verbose(
2034 "Memory type shall not be specified by "
2035 "sender.\n");
2036 return ffa_error(FFA_INVALID_PARAMETERS);
2037 }
2038 } else {
2039 /*
2040 * Check that sender's memory attributes match Hafnium
2041 * expectations: Normal Memory, Inner shareable, Write-Back
2042 * Read-Allocate Write-Allocate Cacheable.
2043 */
2044 ret = ffa_memory_attributes_validate(memory_region->attributes);
2045 if (ret.func != FFA_SUCCESS_32) {
2046 return ret;
2047 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002048 }
2049
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002050 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002051}
2052
2053/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002054 * Gets the share state for continuing an operation to donate, lend or share
2055 * memory, and checks that it is a valid request.
2056 *
2057 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2058 * not.
2059 */
J-Alvesfdd29272022-07-19 13:16:31 +01002060struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002061 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002062 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002063 struct mpool *page_pool)
2064{
2065 struct ffa_memory_share_state *share_state;
2066 struct ffa_memory_region *memory_region;
2067
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002068 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002069
2070 /*
2071 * Look up the share state by handle and make sure that the VM ID
2072 * matches.
2073 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002074 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002075 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002076 dlog_verbose(
2077 "Invalid handle %#x for memory send continuation.\n",
2078 handle);
2079 return ffa_error(FFA_INVALID_PARAMETERS);
2080 }
2081 memory_region = share_state->memory_region;
2082
J-Alvesfdd29272022-07-19 13:16:31 +01002083 if (vm_id_is_current_world(from_vm_id) &&
2084 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002085 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2086 return ffa_error(FFA_INVALID_PARAMETERS);
2087 }
2088
2089 if (share_state->sending_complete) {
2090 dlog_verbose(
2091 "Sending of memory handle %#x is already complete.\n",
2092 handle);
2093 return ffa_error(FFA_INVALID_PARAMETERS);
2094 }
2095
2096 if (share_state->fragment_count == MAX_FRAGMENTS) {
2097 /*
2098 * Log a warning as this is a sign that MAX_FRAGMENTS should
2099 * probably be increased.
2100 */
2101 dlog_warning(
2102 "Too many fragments for memory share with handle %#x; "
2103 "only %d supported.\n",
2104 handle, MAX_FRAGMENTS);
2105 /* Free share state, as it's not possible to complete it. */
2106 share_state_free(share_states, share_state, page_pool);
2107 return ffa_error(FFA_NO_MEMORY);
2108 }
2109
2110 *share_state_ret = share_state;
2111
2112 return (struct ffa_value){.func = FFA_SUCCESS_32};
2113}
2114
2115/**
J-Alves95df0ef2022-12-07 10:09:48 +00002116 * Checks if there is at least one receiver from the other world.
2117 */
J-Alvesfdd29272022-07-19 13:16:31 +01002118bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002119 struct ffa_memory_region *memory_region)
2120{
2121 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002122 struct ffa_memory_access *receiver =
2123 ffa_memory_region_get_receiver(memory_region, i);
2124 assert(receiver != NULL);
2125 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2126
2127 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002128 return true;
2129 }
2130 }
2131 return false;
2132}
2133
2134/**
J-Alves9da280b2022-12-21 14:55:39 +00002135 * Validates a call to donate, lend or share memory in which Hafnium is the
2136 * designated allocator of the memory handle. In practice, this also means
2137 * Hafnium is responsible for managing the state structures for the transaction.
2138 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2139 * sender is an SP or there is at least one borrower that is an SP.
2140 * If Hafnium is the hypervisor, it should allocate the memory handle when
2141 * operation involves only NWd VMs.
2142 *
2143 * If validation goes well, Hafnium updates the stage-2 page tables of the
2144 * sender. Validation consists of checking if the message length and number of
2145 * memory region constituents match, and if the transition is valid for the
2146 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002147 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002148 * Assumes that the caller has already found and locked the sender VM and copied
2149 * the memory region descriptor from the sender's TX buffer to a freshly
2150 * allocated page from Hafnium's internal pool. The caller must have also
2151 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002152 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002153 * This function takes ownership of the `memory_region` passed in and will free
2154 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002155 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002156struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002157 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002158 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002159 uint32_t fragment_length, uint32_t share_func,
2160 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002161{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002162 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002163 struct share_states_locked share_states;
2164 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002165
2166 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002167 * If there is an error validating the `memory_region` then we need to
2168 * free it because we own it but we won't be storing it in a share state
2169 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002170 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002171 ret = ffa_memory_send_validate(from_locked, memory_region,
2172 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002173 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002174 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002175 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002176 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002177 }
2178
Andrew Walbrana65a1322020-04-06 19:32:32 +01002179 /* Set flag for share function, ready to be retrieved later. */
2180 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002181 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002182 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002183 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002184 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002185 case FFA_MEM_LEND_32:
2186 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002187 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002188 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002189 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002190 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002191 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002192 }
2193
Andrew Walbranca808b12020-05-15 17:22:28 +01002194 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002195 /*
2196 * Allocate a share state before updating the page table. Otherwise if
2197 * updating the page table succeeded but allocating the share state
2198 * failed then it would leave the memory in a state where nobody could
2199 * get it back.
2200 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002201 share_state = allocate_share_state(share_states, share_func,
2202 memory_region, fragment_length,
2203 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002204 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002205 dlog_verbose("Failed to allocate share state.\n");
2206 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002207 ret = ffa_error(FFA_NO_MEMORY);
2208 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002209 }
2210
Andrew Walbranca808b12020-05-15 17:22:28 +01002211 if (fragment_length == memory_share_length) {
2212 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002213 ret = ffa_memory_send_complete(
2214 from_locked, share_states, share_state, page_pool,
2215 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002216 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002217 /*
2218 * Use sender ID from 'memory_region' assuming
2219 * that at this point it has been validated:
2220 * - MBZ at virtual FF-A instance.
2221 */
J-Alves19e20cf2023-08-02 12:48:55 +01002222 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002223 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2224 ? memory_region->sender
2225 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002226 ret = (struct ffa_value){
2227 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002228 .arg1 = (uint32_t)memory_region->handle,
2229 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002230 .arg3 = fragment_length,
2231 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002232 }
2233
2234out:
2235 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002236 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002237 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002238}
2239
2240/**
J-Alves8505a8a2022-06-15 18:10:18 +01002241 * Continues an operation to donate, lend or share memory to a VM from current
2242 * world. If this is the last fragment then checks that the transition is valid
2243 * for the type of memory sending operation and updates the stage-2 page tables
2244 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002245 *
2246 * Assumes that the caller has already found and locked the sender VM and copied
2247 * the memory region descriptor from the sender's TX buffer to a freshly
2248 * allocated page from Hafnium's internal pool.
2249 *
2250 * This function takes ownership of the `fragment` passed in; it must not be
2251 * freed by the caller.
2252 */
2253struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2254 void *fragment,
2255 uint32_t fragment_length,
2256 ffa_memory_handle_t handle,
2257 struct mpool *page_pool)
2258{
2259 struct share_states_locked share_states = share_states_lock();
2260 struct ffa_memory_share_state *share_state;
2261 struct ffa_value ret;
2262 struct ffa_memory_region *memory_region;
2263
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002264 CHECK(is_aligned(fragment,
2265 alignof(struct ffa_memory_region_constituent)));
2266 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2267 0) {
2268 dlog_verbose("Fragment length %u misaligned.\n",
2269 fragment_length);
2270 ret = ffa_error(FFA_INVALID_PARAMETERS);
2271 goto out_free_fragment;
2272 }
2273
Andrew Walbranca808b12020-05-15 17:22:28 +01002274 ret = ffa_memory_send_continue_validate(share_states, handle,
2275 &share_state,
2276 from_locked.vm->id, page_pool);
2277 if (ret.func != FFA_SUCCESS_32) {
2278 goto out_free_fragment;
2279 }
2280 memory_region = share_state->memory_region;
2281
J-Alves95df0ef2022-12-07 10:09:48 +00002282 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002283 dlog_error(
2284 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002285 "other world. This should never happen, and indicates "
2286 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002287 "EL3 code.\n");
2288 ret = ffa_error(FFA_INVALID_PARAMETERS);
2289 goto out_free_fragment;
2290 }
2291
2292 /* Add this fragment. */
2293 share_state->fragments[share_state->fragment_count] = fragment;
2294 share_state->fragment_constituent_counts[share_state->fragment_count] =
2295 fragment_length / sizeof(struct ffa_memory_region_constituent);
2296 share_state->fragment_count++;
2297
2298 /* Check whether the memory send operation is now ready to complete. */
2299 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002300 ret = ffa_memory_send_complete(
2301 from_locked, share_states, share_state, page_pool,
2302 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002303 } else {
2304 ret = (struct ffa_value){
2305 .func = FFA_MEM_FRAG_RX_32,
2306 .arg1 = (uint32_t)handle,
2307 .arg2 = (uint32_t)(handle >> 32),
2308 .arg3 = share_state_next_fragment_offset(share_states,
2309 share_state)};
2310 }
2311 goto out;
2312
2313out_free_fragment:
2314 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002315
2316out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002317 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002318 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002319}
2320
Andrew Walbranca808b12020-05-15 17:22:28 +01002321/** Clean up after the receiver has finished retrieving a memory region. */
2322static void ffa_memory_retrieve_complete(
2323 struct share_states_locked share_states,
2324 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2325{
2326 if (share_state->share_func == FFA_MEM_DONATE_32) {
2327 /*
2328 * Memory that has been donated can't be relinquished,
2329 * so no need to keep the share state around.
2330 */
2331 share_state_free(share_states, share_state, page_pool);
2332 dlog_verbose("Freed share state for donate.\n");
2333 }
2334}
2335
J-Alves2d8457f2022-10-05 11:06:41 +01002336/**
2337 * Initialises the given memory region descriptor to be used for an
2338 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2339 * fragment.
2340 * The memory region descriptor is initialized according to retriever's
2341 * FF-A version.
2342 *
2343 * Returns true on success, or false if the given constituents won't all fit in
2344 * the first fragment.
2345 */
2346static bool ffa_retrieved_memory_region_init(
2347 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002348 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002349 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002350 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002351 struct ffa_memory_access *receivers, size_t receiver_count,
2352 uint32_t memory_access_desc_size, uint32_t page_count,
2353 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002354 const struct ffa_memory_region_constituent constituents[],
2355 uint32_t fragment_constituent_count, uint32_t *total_length,
2356 uint32_t *fragment_length)
2357{
2358 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002359 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002360 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002361 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002362
2363 assert(response != NULL);
2364
2365 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2366 struct ffa_memory_region_v1_0 *retrieve_response =
2367 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002368 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002369
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002370 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2371 attributes, flags, handle, 0,
2372 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002373
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002374 receiver = (struct ffa_memory_access_v1_0 *)
2375 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002376 receiver_count = retrieve_response->receiver_count;
2377
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002378 for (uint32_t i = 0; i < receiver_count; i++) {
2379 ffa_id_t receiver_id =
2380 receivers[i].receiver_permissions.receiver;
2381 ffa_memory_receiver_flags_t recv_flags =
2382 receivers[i].receiver_permissions.flags;
2383
2384 /*
2385 * Initialized here as in memory retrieve responses we
2386 * currently expect one borrower to be specified.
2387 */
2388 ffa_memory_access_init_v1_0(
2389 receiver, receiver_id,
2390 ffa_get_data_access_attr(permissions),
2391 ffa_get_instruction_access_attr(permissions),
2392 recv_flags);
2393 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002394
2395 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002396 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002397 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2398 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002399
2400 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2401 retrieve_response, 0);
2402 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002403 struct ffa_memory_region *retrieve_response =
2404 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002405 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002406
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002407 ffa_memory_region_init_header(
2408 retrieve_response, sender, attributes, flags, handle, 0,
2409 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002410
2411 /*
2412 * Note that `sizeof(struct_ffa_memory_region)` and
2413 * `sizeof(struct ffa_memory_access)` must both be multiples of
2414 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2415 * guaranteed that the offset we calculate here is aligned to a
2416 * 64-bit boundary and so 64-bit values can be copied without
2417 * alignment faults.
2418 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002419 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002420 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002421 (uint32_t)(receiver_count *
2422 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002423
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002424 retrieve_response_receivers =
2425 ffa_memory_region_get_receiver(retrieve_response, 0);
2426 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002427
2428 /*
2429 * Initialized here as in memory retrieve responses we currently
2430 * expect one borrower to be specified.
2431 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002432 memcpy_s(retrieve_response_receivers,
2433 sizeof(struct ffa_memory_access) * receiver_count,
2434 receivers,
2435 sizeof(struct ffa_memory_access) * receiver_count);
2436
2437 retrieve_response_receivers->composite_memory_region_offset =
2438 composite_offset;
2439
J-Alves2d8457f2022-10-05 11:06:41 +01002440 composite_memory_region =
2441 ffa_memory_region_get_composite(retrieve_response, 0);
2442 }
2443
J-Alves2d8457f2022-10-05 11:06:41 +01002444 assert(composite_memory_region != NULL);
2445
J-Alves2d8457f2022-10-05 11:06:41 +01002446 composite_memory_region->page_count = page_count;
2447 composite_memory_region->constituent_count = total_constituent_count;
2448 composite_memory_region->reserved_0 = 0;
2449
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002450 constituents_offset =
2451 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002452 if (constituents_offset +
2453 fragment_constituent_count *
2454 sizeof(struct ffa_memory_region_constituent) >
2455 response_max_size) {
2456 return false;
2457 }
2458
2459 for (i = 0; i < fragment_constituent_count; ++i) {
2460 composite_memory_region->constituents[i] = constituents[i];
2461 }
2462
2463 if (total_length != NULL) {
2464 *total_length =
2465 constituents_offset +
2466 composite_memory_region->constituent_count *
2467 sizeof(struct ffa_memory_region_constituent);
2468 }
2469 if (fragment_length != NULL) {
2470 *fragment_length =
2471 constituents_offset +
2472 fragment_constituent_count *
2473 sizeof(struct ffa_memory_region_constituent);
2474 }
2475
2476 return true;
2477}
2478
J-Alves96de29f2022-04-26 16:05:24 +01002479/**
2480 * Validates the retrieved permissions against those specified by the lender
2481 * of memory share operation. Optionally can help set the permissions to be used
2482 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002483 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2484 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2485 * specification for each ABI.
2486 * - FFA_DENIED -> if the permissions specified by the retriever are not
2487 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002488 */
J-Alvesdcad8992023-09-15 14:10:35 +01002489static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2490 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002491 enum ffa_data_access requested_data_access,
2492 enum ffa_instruction_access sent_instruction_access,
2493 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002494 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002495{
2496 switch (sent_data_access) {
2497 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2498 case FFA_DATA_ACCESS_RW:
2499 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2500 requested_data_access == FFA_DATA_ACCESS_RW) {
2501 if (permissions != NULL) {
2502 ffa_set_data_access_attr(permissions,
2503 FFA_DATA_ACCESS_RW);
2504 }
2505 break;
2506 }
2507 /* Intentional fall-through. */
2508 case FFA_DATA_ACCESS_RO:
2509 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2510 requested_data_access == FFA_DATA_ACCESS_RO) {
2511 if (permissions != NULL) {
2512 ffa_set_data_access_attr(permissions,
2513 FFA_DATA_ACCESS_RO);
2514 }
2515 break;
2516 }
2517 dlog_verbose(
2518 "Invalid data access requested; sender specified "
2519 "permissions %#x but receiver requested %#x.\n",
2520 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002521 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002522 case FFA_DATA_ACCESS_RESERVED:
2523 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2524 "checked before this point.");
2525 }
2526
J-Alvesdcad8992023-09-15 14:10:35 +01002527 /*
2528 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2529 * or FFA_MEMORY_DONATE the retriever should have specifed the
2530 * instruction permissions it wishes to receive.
2531 */
2532 switch (share_func) {
2533 case FFA_MEM_SHARE_32:
2534 if (requested_instruction_access !=
2535 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2536 dlog_verbose(
2537 "%s: for share instruction permissions must "
2538 "NOT be specified.\n",
2539 __func__);
2540 return ffa_error(FFA_INVALID_PARAMETERS);
2541 }
2542 break;
2543 case FFA_MEM_LEND_32:
2544 /*
2545 * For operations with multiple borrowers only permit XN
2546 * permissions, and both Sender and borrower should have used
2547 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2548 */
2549 if (multiple_borrowers) {
2550 if (requested_instruction_access !=
2551 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2552 dlog_verbose(
2553 "%s: lend/share/donate with multiple "
2554 "borrowers "
2555 "instruction permissions must NOT be "
2556 "specified.\n",
2557 __func__);
2558 return ffa_error(FFA_INVALID_PARAMETERS);
2559 }
2560 break;
2561 }
2562 /* Fall through if the operation targets a single borrower. */
2563 case FFA_MEM_DONATE_32:
2564 if (!multiple_borrowers &&
2565 requested_instruction_access ==
2566 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2567 dlog_verbose(
2568 "%s: for lend/donate with single borrower "
2569 "instruction permissions must be speficified "
2570 "by borrower\n",
2571 __func__);
2572 return ffa_error(FFA_INVALID_PARAMETERS);
2573 }
2574 break;
2575 default:
2576 panic("%s: Wrong func id provided.\n", __func__);
2577 }
2578
J-Alves96de29f2022-04-26 16:05:24 +01002579 switch (sent_instruction_access) {
2580 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2581 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002582 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002583 if (permissions != NULL) {
2584 ffa_set_instruction_access_attr(
2585 permissions, FFA_INSTRUCTION_ACCESS_X);
2586 }
2587 break;
2588 }
J-Alvesdcad8992023-09-15 14:10:35 +01002589 /*
2590 * Fall through if requested permissions are less
2591 * permissive than those provided by the sender.
2592 */
J-Alves96de29f2022-04-26 16:05:24 +01002593 case FFA_INSTRUCTION_ACCESS_NX:
2594 if (requested_instruction_access ==
2595 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2596 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2597 if (permissions != NULL) {
2598 ffa_set_instruction_access_attr(
2599 permissions, FFA_INSTRUCTION_ACCESS_NX);
2600 }
2601 break;
2602 }
2603 dlog_verbose(
2604 "Invalid instruction access requested; sender "
2605 "specified permissions %#x but receiver requested "
2606 "%#x.\n",
2607 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002608 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002609 case FFA_INSTRUCTION_ACCESS_RESERVED:
2610 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2611 "be checked before this point.");
2612 }
2613
J-Alvesdcad8992023-09-15 14:10:35 +01002614 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002615}
2616
2617/**
2618 * Validate the receivers' permissions in the retrieve request against those
2619 * specified by the lender.
2620 * In the `permissions` argument returns the permissions to set at S2 for the
2621 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002622 * The function looks into the flag to bypass multiple borrower checks:
2623 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2624 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2625 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2626 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002627 */
2628static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2629 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002630 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002631 ffa_memory_access_permissions_t *permissions,
2632 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002633{
2634 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002635 bool bypass_multi_receiver_check =
2636 (retrieve_request->flags &
2637 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002638 const uint32_t region_receiver_count = memory_region->receiver_count;
2639 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002640
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002641 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002642 assert(permissions != NULL);
2643
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002644 *permissions = 0;
2645
J-Alves3456e032023-07-20 12:20:05 +01002646 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002647 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002648 dlog_verbose(
2649 "Retrieve request should contain same list of "
2650 "borrowers, as specified by the lender.\n");
2651 return ffa_error(FFA_INVALID_PARAMETERS);
2652 }
2653 } else {
2654 if (retrieve_request->receiver_count != 1) {
2655 dlog_verbose(
2656 "Set bypass multiple borrower check, receiver "
2657 "list must be sized 1 (%x)\n",
2658 memory_region->receiver_count);
2659 return ffa_error(FFA_INVALID_PARAMETERS);
2660 }
J-Alves96de29f2022-04-26 16:05:24 +01002661 }
2662
2663 retrieve_receiver_index = retrieve_request->receiver_count;
2664
J-Alves96de29f2022-04-26 16:05:24 +01002665 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2666 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002667 struct ffa_memory_access *retrieve_request_receiver =
2668 ffa_memory_region_get_receiver(retrieve_request, i);
2669 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002670 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002671 retrieve_request_receiver->receiver_permissions
2672 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002673 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002674 retrieve_request_receiver->receiver_permissions
2675 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002676 struct ffa_memory_access *receiver;
2677 uint32_t mem_region_receiver_index;
2678 bool permissions_RO;
2679 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002680 bool found_to_id = current_receiver_id == to_vm_id;
2681
J-Alves3456e032023-07-20 12:20:05 +01002682 if (bypass_multi_receiver_check && !found_to_id) {
2683 dlog_verbose(
2684 "Bypass multiple borrower check for id %x.\n",
2685 current_receiver_id);
2686 continue;
2687 }
2688
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002689 if (retrieve_request_receiver->composite_memory_region_offset !=
2690 0U) {
2691 dlog_verbose(
2692 "Retriever specified address ranges not "
2693 "supported (got offset %d).\n",
2694 retrieve_request_receiver
2695 ->composite_memory_region_offset);
2696 return ffa_error(FFA_INVALID_PARAMETERS);
2697 }
2698
J-Alves96de29f2022-04-26 16:05:24 +01002699 /*
2700 * Find the current receiver in the transaction descriptor from
2701 * sender.
2702 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002703 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002704 ffa_memory_region_get_receiver_index(
2705 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002706
2707 if (mem_region_receiver_index ==
2708 memory_region->receiver_count) {
2709 dlog_verbose("%s: receiver %x not found\n", __func__,
2710 current_receiver_id);
2711 return ffa_error(FFA_DENIED);
2712 }
2713
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002714 receiver = ffa_memory_region_get_receiver(
2715 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002716 assert(receiver != NULL);
2717
2718 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002719
2720 if (found_to_id) {
2721 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002722
2723 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002724 }
2725
2726 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002727 * Check if retrieve request memory access list is valid:
2728 * - The retrieve request complies with the specification.
2729 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002730 */
J-Alvesdcad8992023-09-15 14:10:35 +01002731 ret = ffa_memory_retrieve_is_memory_access_valid(
2732 func_id, ffa_get_data_access_attr(sent_permissions),
2733 ffa_get_data_access_attr(requested_permissions),
2734 ffa_get_instruction_access_attr(sent_permissions),
2735 ffa_get_instruction_access_attr(requested_permissions),
2736 found_to_id ? permissions : NULL,
2737 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002738
J-Alvesdcad8992023-09-15 14:10:35 +01002739 if (ret.func != FFA_SUCCESS_32) {
2740 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002741 }
2742
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002743 permissions_RO = (ffa_get_data_access_attr(*permissions) ==
2744 FFA_DATA_ACCESS_RO);
2745 clear_memory_flags = (retrieve_request->flags &
2746 FFA_MEMORY_REGION_FLAG_CLEAR) != 0U;
2747
J-Alves96de29f2022-04-26 16:05:24 +01002748 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002749 * Can't request PM to clear memory if only provided
2750 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002751 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002752 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002753 dlog_verbose(
2754 "Receiver has RO permissions can not request "
2755 "clear.\n");
2756 return ffa_error(FFA_DENIED);
2757 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002758
2759 /*
2760 * Check the impdef in the retrieve_request matches the value in
2761 * the original memory send.
2762 */
2763 if (ffa_version_from_memory_access_desc_size(
2764 memory_region->memory_access_desc_size) >=
2765 MAKE_FFA_VERSION(1, 2) &&
2766 ffa_version_from_memory_access_desc_size(
2767 retrieve_request->memory_access_desc_size) >=
2768 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002769 if (receiver->impdef.val[0] !=
2770 retrieve_request_receiver->impdef.val[0] ||
2771 receiver->impdef.val[1] !=
2772 retrieve_request_receiver->impdef.val[1]) {
2773 dlog_verbose(
2774 "Impdef value in memory send does not "
2775 "match retrieve request value "
2776 "send value %#x %#x retrieve request "
2777 "value %#x %#x\n",
2778 receiver->impdef.val[0],
2779 receiver->impdef.val[1],
2780 retrieve_request_receiver->impdef
2781 .val[0],
2782 retrieve_request_receiver->impdef
2783 .val[1]);
2784 return ffa_error(FFA_INVALID_PARAMETERS);
2785 }
2786 }
J-Alves96de29f2022-04-26 16:05:24 +01002787 }
2788
2789 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2790 dlog_verbose(
2791 "Retrieve request does not contain caller's (%x) "
2792 "permissions\n",
2793 to_vm_id);
2794 return ffa_error(FFA_INVALID_PARAMETERS);
2795 }
2796
2797 return (struct ffa_value){.func = FFA_SUCCESS_32};
2798}
2799
J-Alvesa9cd7e32022-07-01 13:49:33 +01002800/*
2801 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2802 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2803 * of a pending memory sharing operation whose allocator is the SPM, for
2804 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2805 * the memory region descriptor of the retrieve request must be zeroed with the
2806 * exception of the sender ID and handle.
2807 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002808bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2809 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002810{
2811 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2812 request->attributes == 0U && request->flags == 0U &&
2813 request->tag == 0U && request->receiver_count == 0U &&
2814 plat_ffa_memory_handle_allocated_by_current_world(
2815 request->handle);
2816}
2817
2818/*
2819 * Helper to reset count of fragments retrieved by the hypervisor.
2820 */
2821static void ffa_memory_retrieve_complete_from_hyp(
2822 struct ffa_memory_share_state *share_state)
2823{
2824 if (share_state->hypervisor_fragment_count ==
2825 share_state->fragment_count) {
2826 share_state->hypervisor_fragment_count = 0;
2827 }
2828}
2829
J-Alves089004f2022-07-13 14:25:44 +01002830/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002831 * Prepares the return of the ffa_value for the memory retrieve response.
2832 */
2833static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2834 uint32_t fragment_length)
2835{
2836 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2837 .arg1 = total_length,
2838 .arg2 = fragment_length};
2839}
2840
2841/**
J-Alves089004f2022-07-13 14:25:44 +01002842 * Validate that the memory region descriptor provided by the borrower on
2843 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2844 * memory sharing call.
2845 */
2846static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002847 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2848 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002849 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2850 uint32_t share_func)
2851{
2852 ffa_memory_region_flags_t transaction_type =
2853 retrieve_request->flags &
2854 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002855 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002856 const uint64_t memory_access_desc_size =
2857 retrieve_request->memory_access_desc_size;
2858 const uint32_t expected_retrieve_request_length =
2859 retrieve_request->receivers_offset +
2860 (uint32_t)(retrieve_request->receiver_count *
2861 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002862
2863 assert(retrieve_request != NULL);
2864 assert(memory_region != NULL);
2865 assert(receiver_index != NULL);
2866 assert(retrieve_request->sender == memory_region->sender);
2867
J-Alves4f0d9c12024-01-17 17:23:11 +00002868 if (retrieve_request_length != expected_retrieve_request_length) {
2869 dlog_verbose(
2870 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2871 "but was %d.\n",
2872 expected_retrieve_request_length,
2873 retrieve_request_length);
2874 return ffa_error(FFA_INVALID_PARAMETERS);
2875 }
2876
2877 if (retrieve_request->sender != memory_region->sender) {
2878 dlog_verbose(
2879 "Memory with handle %#x not fully sent, can't "
2880 "retrieve.\n",
2881 memory_region->handle);
2882 return ffa_error(FFA_DENIED);
2883 }
2884
2885 /*
2886 * The SPMC can only process retrieve requests to memory share
2887 * operations with one borrower from the other world. It can't
2888 * determine the ID of the NWd VM that invoked the retrieve
2889 * request interface call. It relies on the hypervisor to
2890 * validate the caller's ID against that provided in the
2891 * `receivers` list of the retrieve response.
2892 * In case there is only one borrower from the NWd in the
2893 * transaction descriptor, record that in the `receiver_id` for
2894 * later use, and validate in the retrieve request message.
2895 * This limitation is due to the fact SPMC can't determine the
2896 * index in the memory share structures state to update.
2897 */
2898 if (to_id == HF_HYPERVISOR_VM_ID) {
2899 uint32_t other_world_count = 0;
2900
2901 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2902 struct ffa_memory_access *receiver =
2903 ffa_memory_region_get_receiver(retrieve_request,
2904 0);
2905 assert(receiver != NULL);
2906
2907 to_id = receiver->receiver_permissions.receiver;
2908
2909 if (!vm_id_is_current_world(to_id)) {
2910 other_world_count++;
2911 }
2912 }
2913
2914 if (other_world_count > 1) {
2915 dlog_verbose(
2916 "Support one receiver from the other "
2917 "world.\n");
2918 return ffa_error(FFA_NOT_SUPPORTED);
2919 }
2920 }
J-Alves089004f2022-07-13 14:25:44 +01002921 /*
2922 * Check that the transaction type expected by the receiver is
2923 * correct, if it has been specified.
2924 */
2925 if (transaction_type !=
2926 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2927 transaction_type != (memory_region->flags &
2928 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2929 dlog_verbose(
2930 "Incorrect transaction type %#x for "
2931 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2932 transaction_type,
2933 memory_region->flags &
2934 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2935 retrieve_request->handle);
2936 return ffa_error(FFA_INVALID_PARAMETERS);
2937 }
2938
2939 if (retrieve_request->tag != memory_region->tag) {
2940 dlog_verbose(
2941 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2942 "%d for handle %#x.\n",
2943 retrieve_request->tag, memory_region->tag,
2944 retrieve_request->handle);
2945 return ffa_error(FFA_INVALID_PARAMETERS);
2946 }
2947
J-Alves4f0d9c12024-01-17 17:23:11 +00002948 *receiver_index =
2949 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01002950
2951 if (*receiver_index == memory_region->receiver_count) {
2952 dlog_verbose(
2953 "Incorrect receiver VM ID %d for "
2954 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00002955 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002956 return ffa_error(FFA_INVALID_PARAMETERS);
2957 }
2958
2959 if ((retrieve_request->flags &
2960 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2961 dlog_verbose(
2962 "Retriever specified 'address range alignment 'hint' "
2963 "not supported.\n");
2964 return ffa_error(FFA_INVALID_PARAMETERS);
2965 }
2966 if ((retrieve_request->flags &
2967 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2968 dlog_verbose(
2969 "Bits 8-5 must be zero in memory region's flags "
2970 "(address range alignment hint not supported).\n");
2971 return ffa_error(FFA_INVALID_PARAMETERS);
2972 }
2973
2974 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2975 dlog_verbose(
2976 "Bits 31-10 must be zero in memory region's flags.\n");
2977 return ffa_error(FFA_INVALID_PARAMETERS);
2978 }
2979
2980 if (share_func == FFA_MEM_SHARE_32 &&
2981 (retrieve_request->flags &
2982 (FFA_MEMORY_REGION_FLAG_CLEAR |
2983 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2984 dlog_verbose(
2985 "Memory Share operation can't clean after relinquish "
2986 "memory region.\n");
2987 return ffa_error(FFA_INVALID_PARAMETERS);
2988 }
2989
2990 /*
2991 * If the borrower needs the memory to be cleared before mapping
2992 * to its address space, the sender should have set the flag
2993 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2994 * FFA_DENIED.
2995 */
2996 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2997 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2998 dlog_verbose(
2999 "Borrower needs memory cleared. Sender needs to set "
3000 "flag for clearing memory.\n");
3001 return ffa_error(FFA_DENIED);
3002 }
3003
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003004 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
3005 security_state =
3006 ffa_get_memory_security_attr(retrieve_request->attributes);
3007 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3008 dlog_verbose(
3009 "Invalid security state for memory retrieve request "
3010 "operation.\n");
3011 return ffa_error(FFA_INVALID_PARAMETERS);
3012 }
3013
J-Alves089004f2022-07-13 14:25:44 +01003014 /*
3015 * If memory type is not specified, bypass validation of memory
3016 * attributes in the retrieve request. The retriever is expecting to
3017 * obtain this information from the SPMC.
3018 */
3019 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
3020 FFA_MEMORY_NOT_SPECIFIED_MEM) {
3021 return (struct ffa_value){.func = FFA_SUCCESS_32};
3022 }
3023
3024 /*
3025 * Ensure receiver's attributes are compatible with how
3026 * Hafnium maps memory: Normal Memory, Inner shareable,
3027 * Write-Back Read-Allocate Write-Allocate Cacheable.
3028 */
3029 return ffa_memory_attributes_validate(retrieve_request->attributes);
3030}
3031
J-Alves4f0d9c12024-01-17 17:23:11 +00003032static struct ffa_value ffa_partition_retrieve_request(
3033 struct share_states_locked share_states,
3034 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3035 struct ffa_memory_region *retrieve_request,
3036 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003037{
J-Alvesa9cd7e32022-07-01 13:49:33 +01003038 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003039 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003040 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003041 struct ffa_composite_memory_region *composite;
3042 uint32_t total_length;
3043 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003044 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003045 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003046 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003047 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003048 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003049 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003050 ffa_memory_handle_t handle = retrieve_request->handle;
J-Alves460d36c2023-10-12 17:02:15 +01003051 ffa_memory_attributes_t attributes = 0;
3052 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003053 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003054
J-Alves96de29f2022-04-26 16:05:24 +01003055 if (!share_state->sending_complete) {
3056 dlog_verbose(
3057 "Memory with handle %#x not fully sent, can't "
3058 "retrieve.\n",
3059 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003060 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003061 }
3062
J-Alves4f0d9c12024-01-17 17:23:11 +00003063 /*
3064 * Validate retrieve request, according to what was sent by the
3065 * sender. Function will output the `receiver_index` from the
3066 * provided memory region.
3067 */
3068 ret = ffa_memory_retrieve_validate(
3069 receiver_id, retrieve_request, retrieve_request_length,
3070 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003071
J-Alves4f0d9c12024-01-17 17:23:11 +00003072 if (ret.func != FFA_SUCCESS_32) {
3073 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003074 }
J-Alves96de29f2022-04-26 16:05:24 +01003075
J-Alves4f0d9c12024-01-17 17:23:11 +00003076 /*
3077 * Validate the requested permissions against the sent
3078 * permissions.
3079 * Outputs the permissions to give to retriever at S2
3080 * PTs.
3081 */
3082 ret = ffa_memory_retrieve_validate_memory_access_list(
3083 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003084 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003085 if (ret.func != FFA_SUCCESS_32) {
3086 return ret;
3087 }
3088
3089 memory_to_mode = ffa_memory_permissions_to_mode(
3090 permissions, share_state->sender_orig_mode);
3091
3092 ret = ffa_retrieve_check_update(
3093 to_locked, share_state->fragments,
3094 share_state->fragment_constituent_counts,
3095 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003096 share_state->share_func, false, page_pool, &retrieve_mode,
3097 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003098
3099 if (ret.func != FFA_SUCCESS_32) {
3100 return ret;
3101 }
3102
3103 share_state->retrieved_fragment_count[receiver_index] = 1;
3104
3105 is_retrieve_complete =
3106 share_state->retrieved_fragment_count[receiver_index] ==
3107 share_state->fragment_count;
3108
J-Alvesb5084cf2022-07-06 14:20:12 +01003109 /* VMs acquire the RX buffer from SPMC. */
3110 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3111
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003112 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003113 * Copy response to RX buffer of caller and deliver the message.
3114 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003115 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003116 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003117
Andrew Walbranca808b12020-05-15 17:22:28 +01003118 /*
J-Alves460d36c2023-10-12 17:02:15 +01003119 * Set the security state in the memory retrieve response attributes
3120 * if specified by the target mode.
3121 */
3122 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3123 retrieve_mode);
3124
3125 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003126 * Constituents which we received in the first fragment should
3127 * always fit in the first fragment we are sending, because the
3128 * header is the same size in both cases and we have a fixed
3129 * message buffer size. So `ffa_retrieved_memory_region_init`
3130 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003131 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003132
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003133 /* Provide the permissions that had been provided. */
3134 receiver->receiver_permissions.permissions = permissions;
3135
3136 /*
3137 * Prepare the memory region descriptor for the retrieve response.
3138 * Provide the pointer to the receiver tracked in the share state
3139 * strucutures.
3140 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003141 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01003142 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003143 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003144 memory_region->flags, handle, permissions, receiver, 1,
3145 memory_access_desc_size, composite->page_count,
3146 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003147 share_state->fragment_constituent_counts[0], &total_length,
3148 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003149
J-Alves4f0d9c12024-01-17 17:23:11 +00003150 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003151 ffa_memory_retrieve_complete(share_states, share_state,
3152 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003153 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003154
3155 return ffa_memory_retrieve_resp(total_length, fragment_length);
3156}
3157
3158static struct ffa_value ffa_hypervisor_retrieve_request(
3159 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3160 struct ffa_memory_region *retrieve_request)
3161{
3162 struct ffa_value ret;
3163 struct ffa_composite_memory_region *composite;
3164 uint32_t total_length;
3165 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003166 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003167 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003168 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003169 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003170 ffa_memory_handle_t handle = retrieve_request->handle;
3171
J-Alves4f0d9c12024-01-17 17:23:11 +00003172 memory_region = share_state->memory_region;
3173
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003174 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3175
J-Alves7b6ab612024-01-24 09:54:54 +00003176 switch (to_locked.vm->ffa_version) {
3177 case MAKE_FFA_VERSION(1, 2):
3178 memory_access_desc_size = sizeof(struct ffa_memory_access);
3179 break;
3180 case MAKE_FFA_VERSION(1, 0):
3181 case MAKE_FFA_VERSION(1, 1):
3182 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3183 break;
3184 default:
3185 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3186 }
3187
J-Alves4f0d9c12024-01-17 17:23:11 +00003188 if (share_state->hypervisor_fragment_count != 0U) {
3189 dlog_verbose(
3190 "Memory with handle %#x already retrieved by "
3191 "the hypervisor.\n",
3192 handle);
3193 return ffa_error(FFA_DENIED);
3194 }
3195
3196 share_state->hypervisor_fragment_count = 1;
3197
3198 ffa_memory_retrieve_complete_from_hyp(share_state);
3199
3200 /* VMs acquire the RX buffer from SPMC. */
3201 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3202
3203 /*
3204 * Copy response to RX buffer of caller and deliver the message.
3205 * This must be done before the share_state is (possibly) freed.
3206 */
3207 composite = ffa_memory_region_get_composite(memory_region, 0);
3208
3209 /*
3210 * Constituents which we received in the first fragment should
3211 * always fit in the first fragment we are sending, because the
3212 * header is the same size in both cases and we have a fixed
3213 * message buffer size. So `ffa_retrieved_memory_region_init`
3214 * should never fail.
3215 */
3216
3217 /*
3218 * Set the security state in the memory retrieve response attributes
3219 * if specified by the target mode.
3220 */
3221 attributes = plat_ffa_memory_security_mode(
3222 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003223
3224 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3225
J-Alves4f0d9c12024-01-17 17:23:11 +00003226 CHECK(ffa_retrieved_memory_region_init(
3227 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3228 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003229 memory_region->flags, handle,
3230 receiver->receiver_permissions.permissions, receiver,
3231 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003232 composite->page_count, composite->constituent_count,
3233 share_state->fragments[0],
3234 share_state->fragment_constituent_counts[0], &total_length,
3235 &fragment_length));
3236
3237 return ffa_memory_retrieve_resp(total_length, fragment_length);
3238}
3239
3240struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3241 struct ffa_memory_region *retrieve_request,
3242 uint32_t retrieve_request_length,
3243 struct mpool *page_pool)
3244{
3245 ffa_memory_handle_t handle = retrieve_request->handle;
3246 struct share_states_locked share_states;
3247 struct ffa_memory_share_state *share_state;
3248 struct ffa_value ret;
3249
3250 dump_share_states();
3251
3252 share_states = share_states_lock();
3253 share_state = get_share_state(share_states, handle);
3254 if (share_state == NULL) {
3255 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3256 handle);
3257 ret = ffa_error(FFA_INVALID_PARAMETERS);
3258 goto out;
3259 }
3260
3261 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3262 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3263 retrieve_request);
3264 } else {
3265 ret = ffa_partition_retrieve_request(
3266 share_states, share_state, to_locked, retrieve_request,
3267 retrieve_request_length, page_pool);
3268 }
3269
3270 /* Track use of the RX buffer if the handling has succeeded. */
3271 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3272 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3273 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3274 }
3275
Andrew Walbranca808b12020-05-15 17:22:28 +01003276out:
3277 share_states_unlock(&share_states);
3278 dump_share_states();
3279 return ret;
3280}
3281
J-Alves5da37d92022-10-24 16:33:48 +01003282/**
3283 * Determine expected fragment offset according to the FF-A version of
3284 * the caller.
3285 */
3286static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3287 struct ffa_memory_region *memory_region,
3288 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3289{
3290 uint32_t expected_fragment_offset;
3291 uint32_t composite_constituents_offset;
3292
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003293 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003294 /*
3295 * Hafnium operates memory regions in FF-A v1.1 format, so we
3296 * can retrieve the constituents offset from descriptor.
3297 */
3298 composite_constituents_offset =
3299 ffa_composite_constituent_offset(memory_region, 0);
3300 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3301 /*
3302 * If retriever is FF-A v1.0, determine the composite offset
3303 * as it is expected to have been configured in the
3304 * retrieve response.
3305 */
3306 composite_constituents_offset =
3307 sizeof(struct ffa_memory_region_v1_0) +
3308 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003309 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003310 sizeof(struct ffa_composite_memory_region);
3311 } else {
3312 panic("%s received an invalid FF-A version.\n", __func__);
3313 }
3314
3315 expected_fragment_offset =
3316 composite_constituents_offset +
3317 retrieved_constituents_count *
3318 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003319 (uint32_t)(memory_region->memory_access_desc_size *
3320 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003321
3322 return expected_fragment_offset;
3323}
3324
Andrew Walbranca808b12020-05-15 17:22:28 +01003325struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3326 ffa_memory_handle_t handle,
3327 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003328 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003329 struct mpool *page_pool)
3330{
3331 struct ffa_memory_region *memory_region;
3332 struct share_states_locked share_states;
3333 struct ffa_memory_share_state *share_state;
3334 struct ffa_value ret;
3335 uint32_t fragment_index;
3336 uint32_t retrieved_constituents_count;
3337 uint32_t i;
3338 uint32_t expected_fragment_offset;
3339 uint32_t remaining_constituent_count;
3340 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003341 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003342 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003343
3344 dump_share_states();
3345
3346 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003347 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003348 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003349 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3350 handle);
3351 ret = ffa_error(FFA_INVALID_PARAMETERS);
3352 goto out;
3353 }
3354
3355 memory_region = share_state->memory_region;
3356 CHECK(memory_region != NULL);
3357
Andrew Walbranca808b12020-05-15 17:22:28 +01003358 if (!share_state->sending_complete) {
3359 dlog_verbose(
3360 "Memory with handle %#x not fully sent, can't "
3361 "retrieve.\n",
3362 handle);
3363 ret = ffa_error(FFA_INVALID_PARAMETERS);
3364 goto out;
3365 }
3366
J-Alves59ed0042022-07-28 18:26:41 +01003367 /*
3368 * If retrieve request from the hypervisor has been initiated in the
3369 * given share_state, continue it, else assume it is a continuation of
3370 * retrieve request from a NWd VM.
3371 */
3372 continue_ffa_hyp_mem_retrieve_req =
3373 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3374 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003375 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003376
J-Alves59ed0042022-07-28 18:26:41 +01003377 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003378 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003379 memory_region, to_locked.vm->id);
3380
3381 if (receiver_index == memory_region->receiver_count) {
3382 dlog_verbose(
3383 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3384 "borrower to memory sharing transaction (%x)\n",
3385 to_locked.vm->id, handle);
3386 ret = ffa_error(FFA_INVALID_PARAMETERS);
3387 goto out;
3388 }
3389
3390 if (share_state->retrieved_fragment_count[receiver_index] ==
3391 0 ||
3392 share_state->retrieved_fragment_count[receiver_index] >=
3393 share_state->fragment_count) {
3394 dlog_verbose(
3395 "Retrieval of memory with handle %#x not yet "
3396 "started or already completed (%d/%d fragments "
3397 "retrieved).\n",
3398 handle,
3399 share_state->retrieved_fragment_count
3400 [receiver_index],
3401 share_state->fragment_count);
3402 ret = ffa_error(FFA_INVALID_PARAMETERS);
3403 goto out;
3404 }
3405
3406 fragment_index =
3407 share_state->retrieved_fragment_count[receiver_index];
3408 } else {
3409 if (share_state->hypervisor_fragment_count == 0 ||
3410 share_state->hypervisor_fragment_count >=
3411 share_state->fragment_count) {
3412 dlog_verbose(
3413 "Retrieve of memory with handle %x not "
3414 "started from hypervisor.\n",
3415 handle);
3416 ret = ffa_error(FFA_INVALID_PARAMETERS);
3417 goto out;
3418 }
3419
3420 if (memory_region->sender != sender_vm_id) {
3421 dlog_verbose(
3422 "Sender ID (%x) is not as expected for memory "
3423 "handle %x\n",
3424 sender_vm_id, handle);
3425 ret = ffa_error(FFA_INVALID_PARAMETERS);
3426 goto out;
3427 }
3428
3429 fragment_index = share_state->hypervisor_fragment_count;
3430
3431 receiver_index = 0;
3432 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003433
3434 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003435 * Check that the given fragment offset is correct by counting
3436 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003437 */
3438 retrieved_constituents_count = 0;
3439 for (i = 0; i < fragment_index; ++i) {
3440 retrieved_constituents_count +=
3441 share_state->fragment_constituent_counts[i];
3442 }
J-Alvesc7484f12022-05-13 12:41:14 +01003443
3444 CHECK(memory_region->receiver_count > 0);
3445
Andrew Walbranca808b12020-05-15 17:22:28 +01003446 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003447 ffa_memory_retrieve_expected_offset_per_ffa_version(
3448 memory_region, retrieved_constituents_count,
3449 to_locked.vm->ffa_version);
3450
Andrew Walbranca808b12020-05-15 17:22:28 +01003451 if (fragment_offset != expected_fragment_offset) {
3452 dlog_verbose("Fragment offset was %d but expected %d.\n",
3453 fragment_offset, expected_fragment_offset);
3454 ret = ffa_error(FFA_INVALID_PARAMETERS);
3455 goto out;
3456 }
3457
J-Alves4f0d9c12024-01-17 17:23:11 +00003458 /*
3459 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3460 * is currently ownder by the SPMC.
3461 */
3462 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003463
Andrew Walbranca808b12020-05-15 17:22:28 +01003464 remaining_constituent_count = ffa_memory_fragment_init(
3465 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3466 share_state->fragments[fragment_index],
3467 share_state->fragment_constituent_counts[fragment_index],
3468 &fragment_length);
3469 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003470
Andrew Walbranca808b12020-05-15 17:22:28 +01003471 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003472 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003473
J-Alves59ed0042022-07-28 18:26:41 +01003474 if (!continue_ffa_hyp_mem_retrieve_req) {
3475 share_state->retrieved_fragment_count[receiver_index]++;
3476 if (share_state->retrieved_fragment_count[receiver_index] ==
3477 share_state->fragment_count) {
3478 ffa_memory_retrieve_complete(share_states, share_state,
3479 page_pool);
3480 }
3481 } else {
3482 share_state->hypervisor_fragment_count++;
3483
3484 ffa_memory_retrieve_complete_from_hyp(share_state);
3485 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003486 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3487 .arg1 = (uint32_t)handle,
3488 .arg2 = (uint32_t)(handle >> 32),
3489 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003490
3491out:
3492 share_states_unlock(&share_states);
3493 dump_share_states();
3494 return ret;
3495}
3496
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003497struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003498 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003499 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003500{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003501 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003502 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003503 struct ffa_memory_share_state *share_state;
3504 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003505 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003506 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003507 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003508 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00003509 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003510
Andrew Walbrana65a1322020-04-06 19:32:32 +01003511 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003512 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003513 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003514 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003515 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003516 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003517 }
3518
Andrew Walbrana65a1322020-04-06 19:32:32 +01003519 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003520 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003521 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003522 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003523 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003524 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003525 }
3526
3527 dump_share_states();
3528
3529 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003530 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003531 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003532 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003533 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003534 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003535 goto out;
3536 }
3537
Andrew Walbranca808b12020-05-15 17:22:28 +01003538 if (!share_state->sending_complete) {
3539 dlog_verbose(
3540 "Memory with handle %#x not fully sent, can't "
3541 "relinquish.\n",
3542 handle);
3543 ret = ffa_error(FFA_INVALID_PARAMETERS);
3544 goto out;
3545 }
3546
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003547 memory_region = share_state->memory_region;
3548 CHECK(memory_region != NULL);
3549
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003550 receiver_index = ffa_memory_region_get_receiver_index(
3551 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003552
3553 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003554 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003555 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003556 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003557 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003558 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003559 goto out;
3560 }
3561
J-Alves8eb19162022-04-28 10:56:48 +01003562 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003563 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003564 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003565 "Memory with handle %#x not yet fully "
3566 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003567 "receiver %x can't relinquish.\n",
3568 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003569 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003570 goto out;
3571 }
3572
J-Alves3c5b2072022-11-21 12:45:40 +00003573 /*
3574 * Either clear if requested in relinquish call, or in a retrieve
3575 * request from one of the borrowers.
3576 */
3577 receivers_relinquished_memory = true;
3578
3579 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3580 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003581 ffa_memory_region_get_receiver(memory_region, i);
3582 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003583 if (receiver->receiver_permissions.receiver ==
3584 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003585 receiver_permissions =
3586 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003587 continue;
3588 }
3589
3590 if (share_state->retrieved_fragment_count[i] != 0U) {
3591 receivers_relinquished_memory = false;
3592 break;
3593 }
3594 }
3595
3596 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003597 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3598 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003599
3600 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003601 * Clear is not allowed for memory that was shared, as the
3602 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003603 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003604 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003605 dlog_verbose("Memory which was shared can't be cleared.\n");
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-Alves639ddfc2023-11-21 14:17:26 +00003610 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3611 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3612 __func__);
3613 ret = ffa_error(FFA_DENIED);
3614 goto out;
3615 }
3616
Andrew Walbranca808b12020-05-15 17:22:28 +01003617 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003618 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003619 share_state->fragment_constituent_counts,
3620 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003621
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003622 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003623 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003624 * Mark memory handle as not retrieved, so it can be
3625 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003626 */
J-Alves8eb19162022-04-28 10:56:48 +01003627 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003628 }
3629
3630out:
3631 share_states_unlock(&share_states);
3632 dump_share_states();
3633 return ret;
3634}
3635
3636/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003637 * Validates that the reclaim transition is allowed for the given
3638 * handle, updates the page table of the reclaiming VM, and frees the
3639 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003640 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003641struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003642 ffa_memory_handle_t handle,
3643 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003644 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003645{
3646 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003647 struct ffa_memory_share_state *share_state;
3648 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003649 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003650
3651 dump_share_states();
3652
3653 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003654
Karl Meakin4a2854a2023-06-30 16:26:52 +01003655 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003656 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003657 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003658 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003659 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003660 goto out;
3661 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003662 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003663
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003664 CHECK(memory_region != NULL);
3665
J-Alvesa9cd7e32022-07-01 13:49:33 +01003666 if (vm_id_is_current_world(to_locked.vm->id) &&
3667 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003668 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003669 "VM %#x attempted to reclaim memory handle %#x "
3670 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003671 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003672 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003673 goto out;
3674 }
3675
Andrew Walbranca808b12020-05-15 17:22:28 +01003676 if (!share_state->sending_complete) {
3677 dlog_verbose(
3678 "Memory with handle %#x not fully sent, can't "
3679 "reclaim.\n",
3680 handle);
3681 ret = ffa_error(FFA_INVALID_PARAMETERS);
3682 goto out;
3683 }
3684
J-Alves752236c2022-04-28 11:07:47 +01003685 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3686 if (share_state->retrieved_fragment_count[i] != 0) {
3687 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003688 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003689 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003690 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003691 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003692 ffa_memory_region_get_receiver(memory_region, i)
3693 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003694 ret = ffa_error(FFA_DENIED);
3695 goto out;
3696 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003697 }
3698
Andrew Walbranca808b12020-05-15 17:22:28 +01003699 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003700 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003701 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003702 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003703 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01003704 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003705
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003706 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003707 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003708 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003709 }
3710
3711out:
3712 share_states_unlock(&share_states);
3713 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003714}